xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/cl/CLContextCL.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLContextCL.h: Defines the class interface for CLContextCL, implementing CLContextImpl.
7 
8 #ifndef LIBANGLE_RENDERER_CL_CLCONTEXTCL_H_
9 #define LIBANGLE_RENDERER_CL_CLCONTEXTCL_H_
10 
11 #include "libANGLE/renderer/cl/cl_types.h"
12 
13 #include "libANGLE/renderer/CLContextImpl.h"
14 
15 #include "common/SynchronizedValue.h"
16 
17 #include <unordered_set>
18 
19 namespace rx
20 {
21 
22 class CLContextCL : public CLContextImpl
23 {
24   public:
25     CLContextCL(const cl::Context &context, cl_context native);
26     ~CLContextCL() override;
27 
28     bool hasMemory(cl_mem memory) const;
29     bool hasSampler(cl_sampler sampler) const;
30     bool hasDeviceQueue(cl_command_queue queue) const;
31 
32     angle::Result getDevices(cl::DevicePtrs *devicePtrsOut) const override;
33 
34     angle::Result createCommandQueue(const cl::CommandQueue &commandQueue,
35                                      CLCommandQueueImpl::Ptr *commandQueueOut) override;
36 
37     angle::Result createBuffer(const cl::Buffer &buffer,
38                                void *hostPtr,
39                                CLMemoryImpl::Ptr *bufferOut) override;
40 
41     angle::Result createImage(const cl::Image &image,
42                               void *hostPtr,
43                               CLMemoryImpl::Ptr *imageOut) override;
44 
45     angle::Result getSupportedImageFormats(cl::MemFlags flags,
46                                            cl::MemObjectType imageType,
47                                            cl_uint numEntries,
48                                            cl_image_format *imageFormats,
49                                            cl_uint *numImageFormats) override;
50 
51     angle::Result createSampler(const cl::Sampler &sampler,
52                                 CLSamplerImpl::Ptr *samplerOut) override;
53 
54     angle::Result createProgramWithSource(const cl::Program &program,
55                                           const std::string &source,
56                                           CLProgramImpl::Ptr *programOut) override;
57 
58     angle::Result createProgramWithIL(const cl::Program &program,
59                                       const void *il,
60                                       size_t length,
61                                       CLProgramImpl::Ptr *programOut) override;
62 
63     angle::Result createProgramWithBinary(const cl::Program &program,
64                                           const size_t *lengths,
65                                           const unsigned char **binaries,
66                                           cl_int *binaryStatus,
67                                           CLProgramImpl::Ptr *programOut) override;
68 
69     angle::Result createProgramWithBuiltInKernels(const cl::Program &program,
70                                                   const char *kernel_names,
71                                                   CLProgramImpl::Ptr *programOut) override;
72 
73     angle::Result linkProgram(const cl::Program &program,
74                               const cl::DevicePtrs &devices,
75                               const char *options,
76                               const cl::ProgramPtrs &inputPrograms,
77                               cl::Program *notify,
78                               CLProgramImpl::Ptr *programOut) override;
79 
80     angle::Result createUserEvent(const cl::Event &event, CLEventImpl::Ptr *eventOut) override;
81 
82     angle::Result waitForEvents(const cl::EventPtrs &events) override;
83 
84   private:
85     struct Mutable
86     {
87         std::unordered_set<const _cl_mem *> mMemories;
88         std::unordered_set<const _cl_sampler *> mSamplers;
89         std::unordered_set<const _cl_command_queue *> mDeviceQueues;
90     };
91     using MutableData = angle::SynchronizedValue<Mutable>;
92 
93     const cl_context mNative;
94     MutableData mData;
95 
96     friend class CLCommandQueueCL;
97     friend class CLMemoryCL;
98     friend class CLSamplerCL;
99 };
100 
hasMemory(cl_mem memory)101 inline bool CLContextCL::hasMemory(cl_mem memory) const
102 {
103     const auto data = mData.synchronize();
104     return data->mMemories.find(memory) != data->mMemories.cend();
105 }
106 
hasSampler(cl_sampler sampler)107 inline bool CLContextCL::hasSampler(cl_sampler sampler) const
108 {
109     const auto data = mData.synchronize();
110     return data->mSamplers.find(sampler) != data->mSamplers.cend();
111 }
112 
hasDeviceQueue(cl_command_queue queue)113 inline bool CLContextCL::hasDeviceQueue(cl_command_queue queue) const
114 {
115     const auto data = mData.synchronize();
116     return data->mDeviceQueues.find(queue) != data->mDeviceQueues.cend();
117 }
118 
119 }  // namespace rx
120 
121 #endif  // LIBANGLE_RENDERER_CL_CLCONTEXTCL_H_
122