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