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/Spinlock.h"
16 #include "common/SynchronizedValue.h"
17 
18 #include <unordered_set>
19 
20 namespace rx
21 {
22 
23 class CLContextCL : public CLContextImpl
24 {
25   public:
26     CLContextCL(const cl::Context &context, cl_context native);
27     ~CLContextCL() override;
28 
29     bool hasMemory(cl_mem memory) const;
30     bool hasSampler(cl_sampler sampler) const;
31     bool hasDeviceQueue(cl_command_queue queue) const;
32 
33     cl::DevicePtrs getDevices(cl_int &errorCode) const override;
34 
35     CLCommandQueueImpl::Ptr createCommandQueue(const cl::CommandQueue &commandQueue,
36                                                cl_int &errorCode) override;
37 
38     CLMemoryImpl::Ptr createBuffer(const cl::Buffer &buffer,
39                                    size_t size,
40                                    void *hostPtr,
41                                    cl_int &errorCode) override;
42 
43     CLMemoryImpl::Ptr createImage(const cl::Image &image,
44                                   cl::MemFlags flags,
45                                   const cl_image_format &format,
46                                   const cl::ImageDescriptor &desc,
47                                   void *hostPtr,
48                                   cl_int &errorCode) override;
49 
50     cl_int getSupportedImageFormats(cl::MemFlags flags,
51                                     cl::MemObjectType imageType,
52                                     cl_uint numEntries,
53                                     cl_image_format *imageFormats,
54                                     cl_uint *numImageFormats) override;
55 
56     CLSamplerImpl::Ptr createSampler(const cl::Sampler &sampler, cl_int &errorCode) override;
57 
58     CLProgramImpl::Ptr createProgramWithSource(const cl::Program &program,
59                                                const std::string &source,
60                                                cl_int &errorCode) override;
61 
62     CLProgramImpl::Ptr createProgramWithIL(const cl::Program &program,
63                                            const void *il,
64                                            size_t length,
65                                            cl_int &errorCode) override;
66 
67     CLProgramImpl::Ptr createProgramWithBinary(const cl::Program &program,
68                                                const size_t *lengths,
69                                                const unsigned char **binaries,
70                                                cl_int *binaryStatus,
71                                                cl_int &errorCode) override;
72 
73     CLProgramImpl::Ptr createProgramWithBuiltInKernels(const cl::Program &program,
74                                                        const char *kernel_names,
75                                                        cl_int &errorCode) override;
76 
77     CLProgramImpl::Ptr linkProgram(const cl::Program &program,
78                                    const cl::DevicePtrs &devices,
79                                    const char *options,
80                                    const cl::ProgramPtrs &inputPrograms,
81                                    cl::Program *notify,
82                                    cl_int &errorCode) override;
83 
84     CLEventImpl::Ptr createUserEvent(const cl::Event &event, cl_int &errorCode) override;
85 
86     cl_int waitForEvents(const cl::EventPtrs &events) override;
87 
88   private:
89     struct Mutable
90     {
91         std::unordered_set<const _cl_mem *> mMemories;
92         std::unordered_set<const _cl_sampler *> mSamplers;
93         std::unordered_set<const _cl_command_queue *> mDeviceQueues;
94     };
95     using MutableData = angle::SynchronizedValue<Mutable, angle::Spinlock>;
96 
97     const cl_context mNative;
98     MutableData mData;
99 
100     friend class CLCommandQueueCL;
101     friend class CLMemoryCL;
102     friend class CLSamplerCL;
103 };
104 
hasMemory(cl_mem memory)105 inline bool CLContextCL::hasMemory(cl_mem memory) const
106 {
107     const auto data = mData.synchronize();
108     return data->mMemories.find(memory) != data->mMemories.cend();
109 }
110 
hasSampler(cl_sampler sampler)111 inline bool CLContextCL::hasSampler(cl_sampler sampler) const
112 {
113     const auto data = mData.synchronize();
114     return data->mSamplers.find(sampler) != data->mSamplers.cend();
115 }
116 
hasDeviceQueue(cl_command_queue queue)117 inline bool CLContextCL::hasDeviceQueue(cl_command_queue queue) const
118 {
119     const auto data = mData.synchronize();
120     return data->mDeviceQueues.find(queue) != data->mDeviceQueues.cend();
121 }
122 
123 }  // namespace rx
124 
125 #endif  // LIBANGLE_RENDERER_CL_CLCONTEXTCL_H_
126