• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                               cl::MemFlags flags,
43                               const cl_image_format &format,
44                               const cl::ImageDescriptor &desc,
45                               void *hostPtr,
46                               CLMemoryImpl::Ptr *imageOut) override;
47 
48     angle::Result getSupportedImageFormats(cl::MemFlags flags,
49                                            cl::MemObjectType imageType,
50                                            cl_uint numEntries,
51                                            cl_image_format *imageFormats,
52                                            cl_uint *numImageFormats) override;
53 
54     angle::Result createSampler(const cl::Sampler &sampler,
55                                 CLSamplerImpl::Ptr *samplerOut) override;
56 
57     angle::Result createProgramWithSource(const cl::Program &program,
58                                           const std::string &source,
59                                           CLProgramImpl::Ptr *programOut) override;
60 
61     angle::Result createProgramWithIL(const cl::Program &program,
62                                       const void *il,
63                                       size_t length,
64                                       CLProgramImpl::Ptr *programOut) override;
65 
66     angle::Result createProgramWithBinary(const cl::Program &program,
67                                           const size_t *lengths,
68                                           const unsigned char **binaries,
69                                           cl_int *binaryStatus,
70                                           CLProgramImpl::Ptr *programOut) override;
71 
72     angle::Result createProgramWithBuiltInKernels(const cl::Program &program,
73                                                   const char *kernel_names,
74                                                   CLProgramImpl::Ptr *programOut) override;
75 
76     angle::Result linkProgram(const cl::Program &program,
77                               const cl::DevicePtrs &devices,
78                               const char *options,
79                               const cl::ProgramPtrs &inputPrograms,
80                               cl::Program *notify,
81                               CLProgramImpl::Ptr *programOut) override;
82 
83     angle::Result createUserEvent(const cl::Event &event, CLEventImpl::Ptr *eventOut) override;
84 
85     angle::Result waitForEvents(const cl::EventPtrs &events) override;
86 
87   private:
88     struct Mutable
89     {
90         std::unordered_set<const _cl_mem *> mMemories;
91         std::unordered_set<const _cl_sampler *> mSamplers;
92         std::unordered_set<const _cl_command_queue *> mDeviceQueues;
93     };
94     using MutableData = angle::SynchronizedValue<Mutable>;
95 
96     const cl_context mNative;
97     MutableData mData;
98 
99     friend class CLCommandQueueCL;
100     friend class CLMemoryCL;
101     friend class CLSamplerCL;
102 };
103 
hasMemory(cl_mem memory)104 inline bool CLContextCL::hasMemory(cl_mem memory) const
105 {
106     const auto data = mData.synchronize();
107     return data->mMemories.find(memory) != data->mMemories.cend();
108 }
109 
hasSampler(cl_sampler sampler)110 inline bool CLContextCL::hasSampler(cl_sampler sampler) const
111 {
112     const auto data = mData.synchronize();
113     return data->mSamplers.find(sampler) != data->mSamplers.cend();
114 }
115 
hasDeviceQueue(cl_command_queue queue)116 inline bool CLContextCL::hasDeviceQueue(cl_command_queue queue) const
117 {
118     const auto data = mData.synchronize();
119     return data->mDeviceQueues.find(queue) != data->mDeviceQueues.cend();
120 }
121 
122 }  // namespace rx
123 
124 #endif  // LIBANGLE_RENDERER_CL_CLCONTEXTCL_H_
125