xref: /aosp_15_r20/external/angle/src/libANGLE/CLContext.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 // CLContext.h: Defines the cl::Context class, which manages OpenCL objects such as command-queues,
7*8975f5c5SAndroid Build Coastguard Worker // memory, program and kernel objects and for executing kernels on one or more devices.
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #ifndef LIBANGLE_CLCONTEXT_H_
10*8975f5c5SAndroid Build Coastguard Worker #define LIBANGLE_CLCONTEXT_H_
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLDevice.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLPlatform.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/CLContextImpl.h"
15*8975f5c5SAndroid Build Coastguard Worker 
16*8975f5c5SAndroid Build Coastguard Worker namespace cl
17*8975f5c5SAndroid Build Coastguard Worker {
18*8975f5c5SAndroid Build Coastguard Worker 
19*8975f5c5SAndroid Build Coastguard Worker class Context final : public _cl_context, public Object
20*8975f5c5SAndroid Build Coastguard Worker {
21*8975f5c5SAndroid Build Coastguard Worker   public:
22*8975f5c5SAndroid Build Coastguard Worker     // Front end entry functions, only called from OpenCL entry points
23*8975f5c5SAndroid Build Coastguard Worker 
24*8975f5c5SAndroid Build Coastguard Worker     static bool IsValidAndVersionOrNewer(const _cl_context *context, cl_uint major, cl_uint minor);
25*8975f5c5SAndroid Build Coastguard Worker 
26*8975f5c5SAndroid Build Coastguard Worker     angle::Result getInfo(ContextInfo name,
27*8975f5c5SAndroid Build Coastguard Worker                           size_t valueSize,
28*8975f5c5SAndroid Build Coastguard Worker                           void *value,
29*8975f5c5SAndroid Build Coastguard Worker                           size_t *valueSizeRet) const;
30*8975f5c5SAndroid Build Coastguard Worker 
31*8975f5c5SAndroid Build Coastguard Worker     cl_command_queue createCommandQueueWithProperties(cl_device_id device,
32*8975f5c5SAndroid Build Coastguard Worker                                                       const cl_queue_properties *properties);
33*8975f5c5SAndroid Build Coastguard Worker 
34*8975f5c5SAndroid Build Coastguard Worker     cl_command_queue createCommandQueue(cl_device_id device, CommandQueueProperties properties);
35*8975f5c5SAndroid Build Coastguard Worker 
36*8975f5c5SAndroid Build Coastguard Worker     cl_mem createBuffer(const cl_mem_properties *properties,
37*8975f5c5SAndroid Build Coastguard Worker                         MemFlags flags,
38*8975f5c5SAndroid Build Coastguard Worker                         size_t size,
39*8975f5c5SAndroid Build Coastguard Worker                         void *hostPtr);
40*8975f5c5SAndroid Build Coastguard Worker 
41*8975f5c5SAndroid Build Coastguard Worker     cl_mem createImage(const cl_mem_properties *properties,
42*8975f5c5SAndroid Build Coastguard Worker                        MemFlags flags,
43*8975f5c5SAndroid Build Coastguard Worker                        const cl_image_format *format,
44*8975f5c5SAndroid Build Coastguard Worker                        const cl_image_desc *desc,
45*8975f5c5SAndroid Build Coastguard Worker                        void *hostPtr);
46*8975f5c5SAndroid Build Coastguard Worker 
47*8975f5c5SAndroid Build Coastguard Worker     cl_mem createImage2D(MemFlags flags,
48*8975f5c5SAndroid Build Coastguard Worker                          const cl_image_format *format,
49*8975f5c5SAndroid Build Coastguard Worker                          size_t width,
50*8975f5c5SAndroid Build Coastguard Worker                          size_t height,
51*8975f5c5SAndroid Build Coastguard Worker                          size_t rowPitch,
52*8975f5c5SAndroid Build Coastguard Worker                          void *hostPtr);
53*8975f5c5SAndroid Build Coastguard Worker 
54*8975f5c5SAndroid Build Coastguard Worker     cl_mem createImage3D(MemFlags flags,
55*8975f5c5SAndroid Build Coastguard Worker                          const cl_image_format *format,
56*8975f5c5SAndroid Build Coastguard Worker                          size_t width,
57*8975f5c5SAndroid Build Coastguard Worker                          size_t height,
58*8975f5c5SAndroid Build Coastguard Worker                          size_t depth,
59*8975f5c5SAndroid Build Coastguard Worker                          size_t rowPitch,
60*8975f5c5SAndroid Build Coastguard Worker                          size_t slicePitch,
61*8975f5c5SAndroid Build Coastguard Worker                          void *hostPtr);
62*8975f5c5SAndroid Build Coastguard Worker 
63*8975f5c5SAndroid Build Coastguard Worker     angle::Result getSupportedImageFormats(MemFlags flags,
64*8975f5c5SAndroid Build Coastguard Worker                                            MemObjectType imageType,
65*8975f5c5SAndroid Build Coastguard Worker                                            cl_uint numEntries,
66*8975f5c5SAndroid Build Coastguard Worker                                            cl_image_format *imageFormats,
67*8975f5c5SAndroid Build Coastguard Worker                                            cl_uint *numImageFormats);
68*8975f5c5SAndroid Build Coastguard Worker 
69*8975f5c5SAndroid Build Coastguard Worker     cl_sampler createSamplerWithProperties(const cl_sampler_properties *properties);
70*8975f5c5SAndroid Build Coastguard Worker 
71*8975f5c5SAndroid Build Coastguard Worker     cl_sampler createSampler(cl_bool normalizedCoords,
72*8975f5c5SAndroid Build Coastguard Worker                              AddressingMode addressingMode,
73*8975f5c5SAndroid Build Coastguard Worker                              FilterMode filterMode);
74*8975f5c5SAndroid Build Coastguard Worker 
75*8975f5c5SAndroid Build Coastguard Worker     cl_program createProgramWithSource(cl_uint count, const char **strings, const size_t *lengths);
76*8975f5c5SAndroid Build Coastguard Worker 
77*8975f5c5SAndroid Build Coastguard Worker     cl_program createProgramWithIL(const void *il, size_t length);
78*8975f5c5SAndroid Build Coastguard Worker 
79*8975f5c5SAndroid Build Coastguard Worker     cl_program createProgramWithBinary(cl_uint numDevices,
80*8975f5c5SAndroid Build Coastguard Worker                                        const cl_device_id *devices,
81*8975f5c5SAndroid Build Coastguard Worker                                        const size_t *lengths,
82*8975f5c5SAndroid Build Coastguard Worker                                        const unsigned char **binaries,
83*8975f5c5SAndroid Build Coastguard Worker                                        cl_int *binaryStatus);
84*8975f5c5SAndroid Build Coastguard Worker 
85*8975f5c5SAndroid Build Coastguard Worker     cl_program createProgramWithBuiltInKernels(cl_uint numDevices,
86*8975f5c5SAndroid Build Coastguard Worker                                                const cl_device_id *devices,
87*8975f5c5SAndroid Build Coastguard Worker                                                const char *kernelNames);
88*8975f5c5SAndroid Build Coastguard Worker 
89*8975f5c5SAndroid Build Coastguard Worker     cl_program linkProgram(cl_uint numDevices,
90*8975f5c5SAndroid Build Coastguard Worker                            const cl_device_id *deviceList,
91*8975f5c5SAndroid Build Coastguard Worker                            const char *options,
92*8975f5c5SAndroid Build Coastguard Worker                            cl_uint numInputPrograms,
93*8975f5c5SAndroid Build Coastguard Worker                            const cl_program *inputPrograms,
94*8975f5c5SAndroid Build Coastguard Worker                            ProgramCB pfnNotify,
95*8975f5c5SAndroid Build Coastguard Worker                            void *userData);
96*8975f5c5SAndroid Build Coastguard Worker 
97*8975f5c5SAndroid Build Coastguard Worker     cl_event createUserEvent();
98*8975f5c5SAndroid Build Coastguard Worker 
99*8975f5c5SAndroid Build Coastguard Worker     angle::Result waitForEvents(cl_uint numEvents, const cl_event *eventList);
100*8975f5c5SAndroid Build Coastguard Worker 
101*8975f5c5SAndroid Build Coastguard Worker   public:
102*8975f5c5SAndroid Build Coastguard Worker     using PropArray = std::vector<cl_context_properties>;
103*8975f5c5SAndroid Build Coastguard Worker 
104*8975f5c5SAndroid Build Coastguard Worker     ~Context() override;
105*8975f5c5SAndroid Build Coastguard Worker 
106*8975f5c5SAndroid Build Coastguard Worker     const Platform &getPlatform() const noexcept;
107*8975f5c5SAndroid Build Coastguard Worker     const DevicePtrs &getDevices() const;
108*8975f5c5SAndroid Build Coastguard Worker     bool hasDevice(const _cl_device_id *device) const;
109*8975f5c5SAndroid Build Coastguard Worker 
110*8975f5c5SAndroid Build Coastguard Worker     template <typename T = rx::CLContextImpl>
111*8975f5c5SAndroid Build Coastguard Worker     T &getImpl() const;
112*8975f5c5SAndroid Build Coastguard Worker 
113*8975f5c5SAndroid Build Coastguard Worker     bool supportsImages() const;
114*8975f5c5SAndroid Build Coastguard Worker     bool supportsIL() const;
115*8975f5c5SAndroid Build Coastguard Worker     bool supportsBuiltInKernel(const std::string &name) const;
116*8975f5c5SAndroid Build Coastguard Worker 
117*8975f5c5SAndroid Build Coastguard Worker     static void CL_CALLBACK ErrorCallback(const char *errinfo,
118*8975f5c5SAndroid Build Coastguard Worker                                           const void *privateInfo,
119*8975f5c5SAndroid Build Coastguard Worker                                           size_t cb,
120*8975f5c5SAndroid Build Coastguard Worker                                           void *userData);
121*8975f5c5SAndroid Build Coastguard Worker 
122*8975f5c5SAndroid Build Coastguard Worker   private:
123*8975f5c5SAndroid Build Coastguard Worker     Context(Platform &platform,
124*8975f5c5SAndroid Build Coastguard Worker             PropArray &&properties,
125*8975f5c5SAndroid Build Coastguard Worker             DevicePtrs &&devices,
126*8975f5c5SAndroid Build Coastguard Worker             ContextErrorCB notify,
127*8975f5c5SAndroid Build Coastguard Worker             void *userData,
128*8975f5c5SAndroid Build Coastguard Worker             bool userSync);
129*8975f5c5SAndroid Build Coastguard Worker 
130*8975f5c5SAndroid Build Coastguard Worker     Context(Platform &platform,
131*8975f5c5SAndroid Build Coastguard Worker             PropArray &&properties,
132*8975f5c5SAndroid Build Coastguard Worker             DeviceType deviceType,
133*8975f5c5SAndroid Build Coastguard Worker             ContextErrorCB notify,
134*8975f5c5SAndroid Build Coastguard Worker             void *userData,
135*8975f5c5SAndroid Build Coastguard Worker             bool userSync);
136*8975f5c5SAndroid Build Coastguard Worker 
137*8975f5c5SAndroid Build Coastguard Worker     Platform &mPlatform;
138*8975f5c5SAndroid Build Coastguard Worker     const PropArray mProperties;
139*8975f5c5SAndroid Build Coastguard Worker     const ContextErrorCB mNotify;
140*8975f5c5SAndroid Build Coastguard Worker     void *const mUserData;
141*8975f5c5SAndroid Build Coastguard Worker     rx::CLContextImpl::Ptr mImpl;
142*8975f5c5SAndroid Build Coastguard Worker     DevicePtrs mDevices;
143*8975f5c5SAndroid Build Coastguard Worker 
144*8975f5c5SAndroid Build Coastguard Worker     friend class Object;
145*8975f5c5SAndroid Build Coastguard Worker };
146*8975f5c5SAndroid Build Coastguard Worker 
IsValidAndVersionOrNewer(const _cl_context * context,cl_uint major,cl_uint minor)147*8975f5c5SAndroid Build Coastguard Worker inline bool Context::IsValidAndVersionOrNewer(const _cl_context *context,
148*8975f5c5SAndroid Build Coastguard Worker                                               cl_uint major,
149*8975f5c5SAndroid Build Coastguard Worker                                               cl_uint minor)
150*8975f5c5SAndroid Build Coastguard Worker {
151*8975f5c5SAndroid Build Coastguard Worker     return IsValid(context) &&
152*8975f5c5SAndroid Build Coastguard Worker            context->cast<Context>().getPlatform().isVersionOrNewer(major, minor);
153*8975f5c5SAndroid Build Coastguard Worker }
154*8975f5c5SAndroid Build Coastguard Worker 
getPlatform()155*8975f5c5SAndroid Build Coastguard Worker inline const Platform &Context::getPlatform() const noexcept
156*8975f5c5SAndroid Build Coastguard Worker {
157*8975f5c5SAndroid Build Coastguard Worker     return mPlatform;
158*8975f5c5SAndroid Build Coastguard Worker }
159*8975f5c5SAndroid Build Coastguard Worker 
getDevices()160*8975f5c5SAndroid Build Coastguard Worker inline const DevicePtrs &Context::getDevices() const
161*8975f5c5SAndroid Build Coastguard Worker {
162*8975f5c5SAndroid Build Coastguard Worker     return mDevices;
163*8975f5c5SAndroid Build Coastguard Worker }
164*8975f5c5SAndroid Build Coastguard Worker 
hasDevice(const _cl_device_id * device)165*8975f5c5SAndroid Build Coastguard Worker inline bool Context::hasDevice(const _cl_device_id *device) const
166*8975f5c5SAndroid Build Coastguard Worker {
167*8975f5c5SAndroid Build Coastguard Worker     return std::find(mDevices.cbegin(), mDevices.cend(), device) != mDevices.cend();
168*8975f5c5SAndroid Build Coastguard Worker }
169*8975f5c5SAndroid Build Coastguard Worker 
170*8975f5c5SAndroid Build Coastguard Worker template <typename T>
getImpl()171*8975f5c5SAndroid Build Coastguard Worker inline T &Context::getImpl() const
172*8975f5c5SAndroid Build Coastguard Worker {
173*8975f5c5SAndroid Build Coastguard Worker     return static_cast<T &>(*mImpl);
174*8975f5c5SAndroid Build Coastguard Worker }
175*8975f5c5SAndroid Build Coastguard Worker 
supportsImages()176*8975f5c5SAndroid Build Coastguard Worker inline bool Context::supportsImages() const
177*8975f5c5SAndroid Build Coastguard Worker {
178*8975f5c5SAndroid Build Coastguard Worker     return (std::find_if(mDevices.cbegin(), mDevices.cend(), [](const DevicePtr &ptr) {
179*8975f5c5SAndroid Build Coastguard Worker                 return ptr->getInfo().imageSupport == CL_TRUE;
180*8975f5c5SAndroid Build Coastguard Worker             }) != mDevices.cend());
181*8975f5c5SAndroid Build Coastguard Worker }
182*8975f5c5SAndroid Build Coastguard Worker 
supportsIL()183*8975f5c5SAndroid Build Coastguard Worker inline bool Context::supportsIL() const
184*8975f5c5SAndroid Build Coastguard Worker {
185*8975f5c5SAndroid Build Coastguard Worker     return (std::find_if(mDevices.cbegin(), mDevices.cend(), [](const DevicePtr &ptr) {
186*8975f5c5SAndroid Build Coastguard Worker                 return !ptr->getInfo().IL_Version.empty();
187*8975f5c5SAndroid Build Coastguard Worker             }) != mDevices.cend());
188*8975f5c5SAndroid Build Coastguard Worker }
189*8975f5c5SAndroid Build Coastguard Worker 
supportsBuiltInKernel(const std::string & name)190*8975f5c5SAndroid Build Coastguard Worker inline bool Context::supportsBuiltInKernel(const std::string &name) const
191*8975f5c5SAndroid Build Coastguard Worker {
192*8975f5c5SAndroid Build Coastguard Worker     return (std::find_if(mDevices.cbegin(), mDevices.cend(), [&](const DevicePtr &ptr) {
193*8975f5c5SAndroid Build Coastguard Worker                 return ptr->supportsBuiltInKernel(name);
194*8975f5c5SAndroid Build Coastguard Worker             }) != mDevices.cend());
195*8975f5c5SAndroid Build Coastguard Worker }
196*8975f5c5SAndroid Build Coastguard Worker 
197*8975f5c5SAndroid Build Coastguard Worker }  // namespace cl
198*8975f5c5SAndroid Build Coastguard Worker 
199*8975f5c5SAndroid Build Coastguard Worker #endif  // LIBANGLE_CLCONTEXT_H_
200