xref: /aosp_15_r20/external/angle/src/libANGLE/CLContext.cpp (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.cpp: Implements the cl::Context class.
7*8975f5c5SAndroid Build Coastguard Worker 
8*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLContext.h"
9*8975f5c5SAndroid Build Coastguard Worker 
10*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLBuffer.h"
11*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLCommandQueue.h"
12*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLEvent.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLImage.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLMemory.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLProgram.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLSampler.h"
17*8975f5c5SAndroid Build Coastguard Worker 
18*8975f5c5SAndroid Build Coastguard Worker #include <cstring>
19*8975f5c5SAndroid Build Coastguard Worker 
20*8975f5c5SAndroid Build Coastguard Worker namespace cl
21*8975f5c5SAndroid Build Coastguard Worker {
22*8975f5c5SAndroid Build Coastguard Worker 
getInfo(ContextInfo name,size_t valueSize,void * value,size_t * valueSizeRet) const23*8975f5c5SAndroid Build Coastguard Worker angle::Result Context::getInfo(ContextInfo name,
24*8975f5c5SAndroid Build Coastguard Worker                                size_t valueSize,
25*8975f5c5SAndroid Build Coastguard Worker                                void *value,
26*8975f5c5SAndroid Build Coastguard Worker                                size_t *valueSizeRet) const
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker     std::vector<cl_device_id> devices;
29*8975f5c5SAndroid Build Coastguard Worker     cl_uint valUInt       = 0u;
30*8975f5c5SAndroid Build Coastguard Worker     const void *copyValue = nullptr;
31*8975f5c5SAndroid Build Coastguard Worker     size_t copySize       = 0u;
32*8975f5c5SAndroid Build Coastguard Worker 
33*8975f5c5SAndroid Build Coastguard Worker     switch (name)
34*8975f5c5SAndroid Build Coastguard Worker     {
35*8975f5c5SAndroid Build Coastguard Worker         case ContextInfo::ReferenceCount:
36*8975f5c5SAndroid Build Coastguard Worker             valUInt   = getRefCount();
37*8975f5c5SAndroid Build Coastguard Worker             copyValue = &valUInt;
38*8975f5c5SAndroid Build Coastguard Worker             copySize  = sizeof(valUInt);
39*8975f5c5SAndroid Build Coastguard Worker             break;
40*8975f5c5SAndroid Build Coastguard Worker         case ContextInfo::NumDevices:
41*8975f5c5SAndroid Build Coastguard Worker             valUInt   = static_cast<decltype(valUInt)>(mDevices.size());
42*8975f5c5SAndroid Build Coastguard Worker             copyValue = &valUInt;
43*8975f5c5SAndroid Build Coastguard Worker             copySize  = sizeof(valUInt);
44*8975f5c5SAndroid Build Coastguard Worker             break;
45*8975f5c5SAndroid Build Coastguard Worker         case ContextInfo::Devices:
46*8975f5c5SAndroid Build Coastguard Worker             devices.reserve(mDevices.size());
47*8975f5c5SAndroid Build Coastguard Worker             for (const DevicePtr &device : mDevices)
48*8975f5c5SAndroid Build Coastguard Worker             {
49*8975f5c5SAndroid Build Coastguard Worker                 devices.emplace_back(device->getNative());
50*8975f5c5SAndroid Build Coastguard Worker             }
51*8975f5c5SAndroid Build Coastguard Worker             copyValue = devices.data();
52*8975f5c5SAndroid Build Coastguard Worker             copySize  = devices.size() * sizeof(decltype(devices)::value_type);
53*8975f5c5SAndroid Build Coastguard Worker             break;
54*8975f5c5SAndroid Build Coastguard Worker         case ContextInfo::Properties:
55*8975f5c5SAndroid Build Coastguard Worker             copyValue = mProperties.data();
56*8975f5c5SAndroid Build Coastguard Worker             copySize  = mProperties.size() * sizeof(decltype(mProperties)::value_type);
57*8975f5c5SAndroid Build Coastguard Worker             break;
58*8975f5c5SAndroid Build Coastguard Worker         default:
59*8975f5c5SAndroid Build Coastguard Worker             ASSERT(false);
60*8975f5c5SAndroid Build Coastguard Worker             ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
61*8975f5c5SAndroid Build Coastguard Worker     }
62*8975f5c5SAndroid Build Coastguard Worker 
63*8975f5c5SAndroid Build Coastguard Worker     if (value != nullptr)
64*8975f5c5SAndroid Build Coastguard Worker     {
65*8975f5c5SAndroid Build Coastguard Worker         // CL_INVALID_VALUE if size in bytes specified by param_value_size is < size of return type
66*8975f5c5SAndroid Build Coastguard Worker         // as specified in the Context Attributes table and param_value is not a NULL value.
67*8975f5c5SAndroid Build Coastguard Worker         if (valueSize < copySize)
68*8975f5c5SAndroid Build Coastguard Worker         {
69*8975f5c5SAndroid Build Coastguard Worker             ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
70*8975f5c5SAndroid Build Coastguard Worker         }
71*8975f5c5SAndroid Build Coastguard Worker         if (copyValue != nullptr)
72*8975f5c5SAndroid Build Coastguard Worker         {
73*8975f5c5SAndroid Build Coastguard Worker             std::memcpy(value, copyValue, copySize);
74*8975f5c5SAndroid Build Coastguard Worker         }
75*8975f5c5SAndroid Build Coastguard Worker     }
76*8975f5c5SAndroid Build Coastguard Worker     if (valueSizeRet != nullptr)
77*8975f5c5SAndroid Build Coastguard Worker     {
78*8975f5c5SAndroid Build Coastguard Worker         *valueSizeRet = copySize;
79*8975f5c5SAndroid Build Coastguard Worker     }
80*8975f5c5SAndroid Build Coastguard Worker     return angle::Result::Continue;
81*8975f5c5SAndroid Build Coastguard Worker }
82*8975f5c5SAndroid Build Coastguard Worker 
createCommandQueueWithProperties(cl_device_id device,const cl_queue_properties * properties)83*8975f5c5SAndroid Build Coastguard Worker cl_command_queue Context::createCommandQueueWithProperties(cl_device_id device,
84*8975f5c5SAndroid Build Coastguard Worker                                                            const cl_queue_properties *properties)
85*8975f5c5SAndroid Build Coastguard Worker {
86*8975f5c5SAndroid Build Coastguard Worker     CommandQueue::PropArray propArray;
87*8975f5c5SAndroid Build Coastguard Worker     CommandQueueProperties props;
88*8975f5c5SAndroid Build Coastguard Worker     cl_uint size = CommandQueue::kNoSize;
89*8975f5c5SAndroid Build Coastguard Worker     if (properties != nullptr)
90*8975f5c5SAndroid Build Coastguard Worker     {
91*8975f5c5SAndroid Build Coastguard Worker         const cl_queue_properties *propIt = properties;
92*8975f5c5SAndroid Build Coastguard Worker         while (*propIt != 0)
93*8975f5c5SAndroid Build Coastguard Worker         {
94*8975f5c5SAndroid Build Coastguard Worker             switch (*propIt++)
95*8975f5c5SAndroid Build Coastguard Worker             {
96*8975f5c5SAndroid Build Coastguard Worker                 case CL_QUEUE_PROPERTIES:
97*8975f5c5SAndroid Build Coastguard Worker                     props = static_cast<cl_command_queue_properties>(*propIt++);
98*8975f5c5SAndroid Build Coastguard Worker                     break;
99*8975f5c5SAndroid Build Coastguard Worker                 case CL_QUEUE_SIZE:
100*8975f5c5SAndroid Build Coastguard Worker                     size = static_cast<decltype(size)>(*propIt++);
101*8975f5c5SAndroid Build Coastguard Worker                     break;
102*8975f5c5SAndroid Build Coastguard Worker             }
103*8975f5c5SAndroid Build Coastguard Worker         }
104*8975f5c5SAndroid Build Coastguard Worker         // Include the trailing zero
105*8975f5c5SAndroid Build Coastguard Worker         ++propIt;
106*8975f5c5SAndroid Build Coastguard Worker         propArray.reserve(propIt - properties);
107*8975f5c5SAndroid Build Coastguard Worker         propArray.insert(propArray.cend(), properties, propIt);
108*8975f5c5SAndroid Build Coastguard Worker     }
109*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<CommandQueue>(*this, device->cast<Device>(), std::move(propArray), props,
110*8975f5c5SAndroid Build Coastguard Worker                                         size);
111*8975f5c5SAndroid Build Coastguard Worker }
112*8975f5c5SAndroid Build Coastguard Worker 
createCommandQueue(cl_device_id device,CommandQueueProperties properties)113*8975f5c5SAndroid Build Coastguard Worker cl_command_queue Context::createCommandQueue(cl_device_id device, CommandQueueProperties properties)
114*8975f5c5SAndroid Build Coastguard Worker {
115*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<CommandQueue>(*this, device->cast<Device>(), properties);
116*8975f5c5SAndroid Build Coastguard Worker }
117*8975f5c5SAndroid Build Coastguard Worker 
createBuffer(const cl_mem_properties * properties,MemFlags flags,size_t size,void * hostPtr)118*8975f5c5SAndroid Build Coastguard Worker cl_mem Context::createBuffer(const cl_mem_properties *properties,
119*8975f5c5SAndroid Build Coastguard Worker                              MemFlags flags,
120*8975f5c5SAndroid Build Coastguard Worker                              size_t size,
121*8975f5c5SAndroid Build Coastguard Worker                              void *hostPtr)
122*8975f5c5SAndroid Build Coastguard Worker {
123*8975f5c5SAndroid Build Coastguard Worker     // If the properties argument specified in clCreateBufferWithProperties or
124*8975f5c5SAndroid Build Coastguard Worker     // clCreateImageWithProperties used to create memobj was not NULL, the implementation must
125*8975f5c5SAndroid Build Coastguard Worker     // return the values specified in the properties argument in the same order and without
126*8975f5c5SAndroid Build Coastguard Worker     // including additional properties. So pass them as is.
127*8975f5c5SAndroid Build Coastguard Worker     Memory::PropArray propArray;
128*8975f5c5SAndroid Build Coastguard Worker     if (properties != nullptr)
129*8975f5c5SAndroid Build Coastguard Worker     {
130*8975f5c5SAndroid Build Coastguard Worker         const cl_mem_properties *propIt = properties;
131*8975f5c5SAndroid Build Coastguard Worker         while (*propIt != 0)
132*8975f5c5SAndroid Build Coastguard Worker         {
133*8975f5c5SAndroid Build Coastguard Worker             propArray.push_back(*propIt);
134*8975f5c5SAndroid Build Coastguard Worker             ++propIt;
135*8975f5c5SAndroid Build Coastguard Worker         }
136*8975f5c5SAndroid Build Coastguard Worker         // there is at least one property - special property 0
137*8975f5c5SAndroid Build Coastguard Worker         propArray.push_back(0);
138*8975f5c5SAndroid Build Coastguard Worker     }
139*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Buffer>(*this, std::move(propArray), flags, size, hostPtr);
140*8975f5c5SAndroid Build Coastguard Worker }
141*8975f5c5SAndroid Build Coastguard Worker 
createImage(const cl_mem_properties * properties,MemFlags flags,const cl_image_format * format,const cl_image_desc * desc,void * hostPtr)142*8975f5c5SAndroid Build Coastguard Worker cl_mem Context::createImage(const cl_mem_properties *properties,
143*8975f5c5SAndroid Build Coastguard Worker                             MemFlags flags,
144*8975f5c5SAndroid Build Coastguard Worker                             const cl_image_format *format,
145*8975f5c5SAndroid Build Coastguard Worker                             const cl_image_desc *desc,
146*8975f5c5SAndroid Build Coastguard Worker                             void *hostPtr)
147*8975f5c5SAndroid Build Coastguard Worker {
148*8975f5c5SAndroid Build Coastguard Worker     const ImageDescriptor imageDesc = {FromCLenum<MemObjectType>(desc->image_type),
149*8975f5c5SAndroid Build Coastguard Worker                                        desc->image_width,
150*8975f5c5SAndroid Build Coastguard Worker                                        desc->image_height,
151*8975f5c5SAndroid Build Coastguard Worker                                        desc->image_depth,
152*8975f5c5SAndroid Build Coastguard Worker                                        desc->image_array_size,
153*8975f5c5SAndroid Build Coastguard Worker                                        desc->image_row_pitch,
154*8975f5c5SAndroid Build Coastguard Worker                                        desc->image_slice_pitch,
155*8975f5c5SAndroid Build Coastguard Worker                                        desc->num_mip_levels,
156*8975f5c5SAndroid Build Coastguard Worker                                        desc->num_samples};
157*8975f5c5SAndroid Build Coastguard Worker     Memory::PropArray propArray;
158*8975f5c5SAndroid Build Coastguard Worker 
159*8975f5c5SAndroid Build Coastguard Worker     // If the properties argument specified in clCreateBufferWithProperties or
160*8975f5c5SAndroid Build Coastguard Worker     // clCreateImageWithProperties used to create memobj was not NULL, the implementation must
161*8975f5c5SAndroid Build Coastguard Worker     // return the values specified in the properties argument in the same order and without
162*8975f5c5SAndroid Build Coastguard Worker     // including additional properties. So Pass them as is.
163*8975f5c5SAndroid Build Coastguard Worker     if (properties != nullptr)
164*8975f5c5SAndroid Build Coastguard Worker     {
165*8975f5c5SAndroid Build Coastguard Worker         const cl_mem_properties *propIt = properties;
166*8975f5c5SAndroid Build Coastguard Worker         while (*propIt != 0)
167*8975f5c5SAndroid Build Coastguard Worker         {
168*8975f5c5SAndroid Build Coastguard Worker             propArray.push_back(*propIt);
169*8975f5c5SAndroid Build Coastguard Worker             ++propIt;
170*8975f5c5SAndroid Build Coastguard Worker         }
171*8975f5c5SAndroid Build Coastguard Worker         // there is at least one property - special property 0
172*8975f5c5SAndroid Build Coastguard Worker         propArray.push_back(0);
173*8975f5c5SAndroid Build Coastguard Worker     }
174*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Image>(*this, std::move(propArray), flags, *format, imageDesc,
175*8975f5c5SAndroid Build Coastguard Worker                                  Memory::Cast(desc->buffer), hostPtr);
176*8975f5c5SAndroid Build Coastguard Worker }
177*8975f5c5SAndroid Build Coastguard Worker 
createImage2D(MemFlags flags,const cl_image_format * format,size_t width,size_t height,size_t rowPitch,void * hostPtr)178*8975f5c5SAndroid Build Coastguard Worker cl_mem Context::createImage2D(MemFlags flags,
179*8975f5c5SAndroid Build Coastguard Worker                               const cl_image_format *format,
180*8975f5c5SAndroid Build Coastguard Worker                               size_t width,
181*8975f5c5SAndroid Build Coastguard Worker                               size_t height,
182*8975f5c5SAndroid Build Coastguard Worker                               size_t rowPitch,
183*8975f5c5SAndroid Build Coastguard Worker                               void *hostPtr)
184*8975f5c5SAndroid Build Coastguard Worker {
185*8975f5c5SAndroid Build Coastguard Worker     const ImageDescriptor imageDesc(MemObjectType::Image2D, width, height, 0u, 0u, rowPitch, 0u, 0u,
186*8975f5c5SAndroid Build Coastguard Worker                                     0u);
187*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Image>(*this, Memory::PropArray{}, flags, *format, imageDesc, nullptr,
188*8975f5c5SAndroid Build Coastguard Worker                                  hostPtr);
189*8975f5c5SAndroid Build Coastguard Worker }
190*8975f5c5SAndroid Build Coastguard Worker 
createImage3D(MemFlags flags,const cl_image_format * format,size_t width,size_t height,size_t depth,size_t rowPitch,size_t slicePitch,void * hostPtr)191*8975f5c5SAndroid Build Coastguard Worker cl_mem Context::createImage3D(MemFlags flags,
192*8975f5c5SAndroid Build Coastguard Worker                               const cl_image_format *format,
193*8975f5c5SAndroid Build Coastguard Worker                               size_t width,
194*8975f5c5SAndroid Build Coastguard Worker                               size_t height,
195*8975f5c5SAndroid Build Coastguard Worker                               size_t depth,
196*8975f5c5SAndroid Build Coastguard Worker                               size_t rowPitch,
197*8975f5c5SAndroid Build Coastguard Worker                               size_t slicePitch,
198*8975f5c5SAndroid Build Coastguard Worker                               void *hostPtr)
199*8975f5c5SAndroid Build Coastguard Worker {
200*8975f5c5SAndroid Build Coastguard Worker     const ImageDescriptor imageDesc(MemObjectType::Image3D, width, height, depth, 0u, rowPitch,
201*8975f5c5SAndroid Build Coastguard Worker                                     slicePitch, 0u, 0u);
202*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Image>(*this, Memory::PropArray{}, flags, *format, imageDesc, nullptr,
203*8975f5c5SAndroid Build Coastguard Worker                                  hostPtr);
204*8975f5c5SAndroid Build Coastguard Worker }
205*8975f5c5SAndroid Build Coastguard Worker 
getSupportedImageFormats(MemFlags flags,MemObjectType imageType,cl_uint numEntries,cl_image_format * imageFormats,cl_uint * numImageFormats)206*8975f5c5SAndroid Build Coastguard Worker angle::Result Context::getSupportedImageFormats(MemFlags flags,
207*8975f5c5SAndroid Build Coastguard Worker                                                 MemObjectType imageType,
208*8975f5c5SAndroid Build Coastguard Worker                                                 cl_uint numEntries,
209*8975f5c5SAndroid Build Coastguard Worker                                                 cl_image_format *imageFormats,
210*8975f5c5SAndroid Build Coastguard Worker                                                 cl_uint *numImageFormats)
211*8975f5c5SAndroid Build Coastguard Worker {
212*8975f5c5SAndroid Build Coastguard Worker     return mImpl->getSupportedImageFormats(flags, imageType, numEntries, imageFormats,
213*8975f5c5SAndroid Build Coastguard Worker                                            numImageFormats);
214*8975f5c5SAndroid Build Coastguard Worker }
215*8975f5c5SAndroid Build Coastguard Worker 
createSamplerWithProperties(const cl_sampler_properties * properties)216*8975f5c5SAndroid Build Coastguard Worker cl_sampler Context::createSamplerWithProperties(const cl_sampler_properties *properties)
217*8975f5c5SAndroid Build Coastguard Worker {
218*8975f5c5SAndroid Build Coastguard Worker     Sampler::PropArray propArray;
219*8975f5c5SAndroid Build Coastguard Worker     cl_bool normalizedCoords      = CL_TRUE;
220*8975f5c5SAndroid Build Coastguard Worker     AddressingMode addressingMode = AddressingMode::Clamp;
221*8975f5c5SAndroid Build Coastguard Worker     FilterMode filterMode         = FilterMode::Nearest;
222*8975f5c5SAndroid Build Coastguard Worker 
223*8975f5c5SAndroid Build Coastguard Worker     if (properties != nullptr)
224*8975f5c5SAndroid Build Coastguard Worker     {
225*8975f5c5SAndroid Build Coastguard Worker         const cl_sampler_properties *propIt = properties;
226*8975f5c5SAndroid Build Coastguard Worker         while (*propIt != 0)
227*8975f5c5SAndroid Build Coastguard Worker         {
228*8975f5c5SAndroid Build Coastguard Worker             switch (*propIt++)
229*8975f5c5SAndroid Build Coastguard Worker             {
230*8975f5c5SAndroid Build Coastguard Worker                 case CL_SAMPLER_NORMALIZED_COORDS:
231*8975f5c5SAndroid Build Coastguard Worker                     normalizedCoords = static_cast<decltype(normalizedCoords)>(*propIt++);
232*8975f5c5SAndroid Build Coastguard Worker                     break;
233*8975f5c5SAndroid Build Coastguard Worker                 case CL_SAMPLER_ADDRESSING_MODE:
234*8975f5c5SAndroid Build Coastguard Worker                     addressingMode = FromCLenum<AddressingMode>(static_cast<CLenum>(*propIt++));
235*8975f5c5SAndroid Build Coastguard Worker                     break;
236*8975f5c5SAndroid Build Coastguard Worker                 case CL_SAMPLER_FILTER_MODE:
237*8975f5c5SAndroid Build Coastguard Worker                     filterMode = FromCLenum<FilterMode>(static_cast<CLenum>(*propIt++));
238*8975f5c5SAndroid Build Coastguard Worker                     break;
239*8975f5c5SAndroid Build Coastguard Worker             }
240*8975f5c5SAndroid Build Coastguard Worker         }
241*8975f5c5SAndroid Build Coastguard Worker         // Include the trailing zero
242*8975f5c5SAndroid Build Coastguard Worker         ++propIt;
243*8975f5c5SAndroid Build Coastguard Worker         propArray.reserve(propIt - properties);
244*8975f5c5SAndroid Build Coastguard Worker         propArray.insert(propArray.cend(), properties, propIt);
245*8975f5c5SAndroid Build Coastguard Worker     }
246*8975f5c5SAndroid Build Coastguard Worker 
247*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Sampler>(*this, std::move(propArray), normalizedCoords, addressingMode,
248*8975f5c5SAndroid Build Coastguard Worker                                    filterMode);
249*8975f5c5SAndroid Build Coastguard Worker }
250*8975f5c5SAndroid Build Coastguard Worker 
createSampler(cl_bool normalizedCoords,AddressingMode addressingMode,FilterMode filterMode)251*8975f5c5SAndroid Build Coastguard Worker cl_sampler Context::createSampler(cl_bool normalizedCoords,
252*8975f5c5SAndroid Build Coastguard Worker                                   AddressingMode addressingMode,
253*8975f5c5SAndroid Build Coastguard Worker                                   FilterMode filterMode)
254*8975f5c5SAndroid Build Coastguard Worker {
255*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Sampler>(*this, Sampler::PropArray{}, normalizedCoords, addressingMode,
256*8975f5c5SAndroid Build Coastguard Worker                                    filterMode);
257*8975f5c5SAndroid Build Coastguard Worker }
258*8975f5c5SAndroid Build Coastguard Worker 
createProgramWithSource(cl_uint count,const char ** strings,const size_t * lengths)259*8975f5c5SAndroid Build Coastguard Worker cl_program Context::createProgramWithSource(cl_uint count,
260*8975f5c5SAndroid Build Coastguard Worker                                             const char **strings,
261*8975f5c5SAndroid Build Coastguard Worker                                             const size_t *lengths)
262*8975f5c5SAndroid Build Coastguard Worker {
263*8975f5c5SAndroid Build Coastguard Worker     std::string source;
264*8975f5c5SAndroid Build Coastguard Worker     if (lengths == nullptr)
265*8975f5c5SAndroid Build Coastguard Worker     {
266*8975f5c5SAndroid Build Coastguard Worker         while (count-- != 0u)
267*8975f5c5SAndroid Build Coastguard Worker         {
268*8975f5c5SAndroid Build Coastguard Worker             source.append(*strings++);
269*8975f5c5SAndroid Build Coastguard Worker         }
270*8975f5c5SAndroid Build Coastguard Worker     }
271*8975f5c5SAndroid Build Coastguard Worker     else
272*8975f5c5SAndroid Build Coastguard Worker     {
273*8975f5c5SAndroid Build Coastguard Worker         while (count-- != 0u)
274*8975f5c5SAndroid Build Coastguard Worker         {
275*8975f5c5SAndroid Build Coastguard Worker             if (*lengths != 0u)
276*8975f5c5SAndroid Build Coastguard Worker             {
277*8975f5c5SAndroid Build Coastguard Worker                 source.append(*strings++, *lengths);
278*8975f5c5SAndroid Build Coastguard Worker             }
279*8975f5c5SAndroid Build Coastguard Worker             else
280*8975f5c5SAndroid Build Coastguard Worker             {
281*8975f5c5SAndroid Build Coastguard Worker                 source.append(*strings++);
282*8975f5c5SAndroid Build Coastguard Worker             }
283*8975f5c5SAndroid Build Coastguard Worker             ++lengths;
284*8975f5c5SAndroid Build Coastguard Worker         }
285*8975f5c5SAndroid Build Coastguard Worker     }
286*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Program>(*this, std::move(source));
287*8975f5c5SAndroid Build Coastguard Worker }
288*8975f5c5SAndroid Build Coastguard Worker 
createProgramWithIL(const void * il,size_t length)289*8975f5c5SAndroid Build Coastguard Worker cl_program Context::createProgramWithIL(const void *il, size_t length)
290*8975f5c5SAndroid Build Coastguard Worker {
291*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Program>(*this, il, length);
292*8975f5c5SAndroid Build Coastguard Worker }
293*8975f5c5SAndroid Build Coastguard Worker 
createProgramWithBinary(cl_uint numDevices,const cl_device_id * devices,const size_t * lengths,const unsigned char ** binaries,cl_int * binaryStatus)294*8975f5c5SAndroid Build Coastguard Worker cl_program Context::createProgramWithBinary(cl_uint numDevices,
295*8975f5c5SAndroid Build Coastguard Worker                                             const cl_device_id *devices,
296*8975f5c5SAndroid Build Coastguard Worker                                             const size_t *lengths,
297*8975f5c5SAndroid Build Coastguard Worker                                             const unsigned char **binaries,
298*8975f5c5SAndroid Build Coastguard Worker                                             cl_int *binaryStatus)
299*8975f5c5SAndroid Build Coastguard Worker {
300*8975f5c5SAndroid Build Coastguard Worker     DevicePtrs devs;
301*8975f5c5SAndroid Build Coastguard Worker     devs.reserve(numDevices);
302*8975f5c5SAndroid Build Coastguard Worker     while (numDevices-- != 0u)
303*8975f5c5SAndroid Build Coastguard Worker     {
304*8975f5c5SAndroid Build Coastguard Worker         devs.emplace_back(&(*devices++)->cast<Device>());
305*8975f5c5SAndroid Build Coastguard Worker     }
306*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Program>(*this, std::move(devs), lengths, binaries, binaryStatus);
307*8975f5c5SAndroid Build Coastguard Worker }
308*8975f5c5SAndroid Build Coastguard Worker 
createProgramWithBuiltInKernels(cl_uint numDevices,const cl_device_id * devices,const char * kernelNames)309*8975f5c5SAndroid Build Coastguard Worker cl_program Context::createProgramWithBuiltInKernels(cl_uint numDevices,
310*8975f5c5SAndroid Build Coastguard Worker                                                     const cl_device_id *devices,
311*8975f5c5SAndroid Build Coastguard Worker                                                     const char *kernelNames)
312*8975f5c5SAndroid Build Coastguard Worker {
313*8975f5c5SAndroid Build Coastguard Worker     DevicePtrs devs;
314*8975f5c5SAndroid Build Coastguard Worker     devs.reserve(numDevices);
315*8975f5c5SAndroid Build Coastguard Worker     while (numDevices-- != 0u)
316*8975f5c5SAndroid Build Coastguard Worker     {
317*8975f5c5SAndroid Build Coastguard Worker         devs.emplace_back(&(*devices++)->cast<Device>());
318*8975f5c5SAndroid Build Coastguard Worker     }
319*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Program>(*this, std::move(devs), kernelNames);
320*8975f5c5SAndroid Build Coastguard Worker }
321*8975f5c5SAndroid Build Coastguard Worker 
linkProgram(cl_uint numDevices,const cl_device_id * deviceList,const char * options,cl_uint numInputPrograms,const cl_program * inputPrograms,ProgramCB pfnNotify,void * userData)322*8975f5c5SAndroid Build Coastguard Worker cl_program Context::linkProgram(cl_uint numDevices,
323*8975f5c5SAndroid Build Coastguard Worker                                 const cl_device_id *deviceList,
324*8975f5c5SAndroid Build Coastguard Worker                                 const char *options,
325*8975f5c5SAndroid Build Coastguard Worker                                 cl_uint numInputPrograms,
326*8975f5c5SAndroid Build Coastguard Worker                                 const cl_program *inputPrograms,
327*8975f5c5SAndroid Build Coastguard Worker                                 ProgramCB pfnNotify,
328*8975f5c5SAndroid Build Coastguard Worker                                 void *userData)
329*8975f5c5SAndroid Build Coastguard Worker {
330*8975f5c5SAndroid Build Coastguard Worker     DevicePtrs devices;
331*8975f5c5SAndroid Build Coastguard Worker     devices.reserve(numDevices);
332*8975f5c5SAndroid Build Coastguard Worker     while (numDevices-- != 0u)
333*8975f5c5SAndroid Build Coastguard Worker     {
334*8975f5c5SAndroid Build Coastguard Worker         devices.emplace_back(&(*deviceList++)->cast<Device>());
335*8975f5c5SAndroid Build Coastguard Worker     }
336*8975f5c5SAndroid Build Coastguard Worker     ProgramPtrs programs;
337*8975f5c5SAndroid Build Coastguard Worker     programs.reserve(numInputPrograms);
338*8975f5c5SAndroid Build Coastguard Worker     while (numInputPrograms-- != 0u)
339*8975f5c5SAndroid Build Coastguard Worker     {
340*8975f5c5SAndroid Build Coastguard Worker         programs.emplace_back(&(*inputPrograms++)->cast<Program>());
341*8975f5c5SAndroid Build Coastguard Worker     }
342*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Program>(*this, devices, options, programs, pfnNotify, userData);
343*8975f5c5SAndroid Build Coastguard Worker }
344*8975f5c5SAndroid Build Coastguard Worker 
createUserEvent()345*8975f5c5SAndroid Build Coastguard Worker cl_event Context::createUserEvent()
346*8975f5c5SAndroid Build Coastguard Worker {
347*8975f5c5SAndroid Build Coastguard Worker     return Object::Create<Event>(*this);
348*8975f5c5SAndroid Build Coastguard Worker }
349*8975f5c5SAndroid Build Coastguard Worker 
waitForEvents(cl_uint numEvents,const cl_event * eventList)350*8975f5c5SAndroid Build Coastguard Worker angle::Result Context::waitForEvents(cl_uint numEvents, const cl_event *eventList)
351*8975f5c5SAndroid Build Coastguard Worker {
352*8975f5c5SAndroid Build Coastguard Worker     return mImpl->waitForEvents(Event::Cast(numEvents, eventList));
353*8975f5c5SAndroid Build Coastguard Worker }
354*8975f5c5SAndroid Build Coastguard Worker 
355*8975f5c5SAndroid Build Coastguard Worker Context::~Context() = default;
356*8975f5c5SAndroid Build Coastguard Worker 
ErrorCallback(const char * errinfo,const void * privateInfo,size_t cb,void * userData)357*8975f5c5SAndroid Build Coastguard Worker void Context::ErrorCallback(const char *errinfo, const void *privateInfo, size_t cb, void *userData)
358*8975f5c5SAndroid Build Coastguard Worker {
359*8975f5c5SAndroid Build Coastguard Worker     Context *const context = static_cast<Context *>(userData);
360*8975f5c5SAndroid Build Coastguard Worker     if (!Context::IsValid(context))
361*8975f5c5SAndroid Build Coastguard Worker     {
362*8975f5c5SAndroid Build Coastguard Worker         WARN() << "Context error for invalid context";
363*8975f5c5SAndroid Build Coastguard Worker         return;
364*8975f5c5SAndroid Build Coastguard Worker     }
365*8975f5c5SAndroid Build Coastguard Worker     if (context->mNotify != nullptr)
366*8975f5c5SAndroid Build Coastguard Worker     {
367*8975f5c5SAndroid Build Coastguard Worker         context->mNotify(errinfo, privateInfo, cb, context->mUserData);
368*8975f5c5SAndroid Build Coastguard Worker     }
369*8975f5c5SAndroid Build Coastguard Worker }
370*8975f5c5SAndroid Build Coastguard Worker 
Context(Platform & platform,PropArray && properties,DevicePtrs && devices,ContextErrorCB notify,void * userData,bool userSync)371*8975f5c5SAndroid Build Coastguard Worker Context::Context(Platform &platform,
372*8975f5c5SAndroid Build Coastguard Worker                  PropArray &&properties,
373*8975f5c5SAndroid Build Coastguard Worker                  DevicePtrs &&devices,
374*8975f5c5SAndroid Build Coastguard Worker                  ContextErrorCB notify,
375*8975f5c5SAndroid Build Coastguard Worker                  void *userData,
376*8975f5c5SAndroid Build Coastguard Worker                  bool userSync)
377*8975f5c5SAndroid Build Coastguard Worker     : mPlatform(platform),
378*8975f5c5SAndroid Build Coastguard Worker       mProperties(std::move(properties)),
379*8975f5c5SAndroid Build Coastguard Worker       mNotify(notify),
380*8975f5c5SAndroid Build Coastguard Worker       mUserData(userData),
381*8975f5c5SAndroid Build Coastguard Worker       mImpl(nullptr),
382*8975f5c5SAndroid Build Coastguard Worker       mDevices(std::move(devices))
383*8975f5c5SAndroid Build Coastguard Worker {
384*8975f5c5SAndroid Build Coastguard Worker     ANGLE_CL_IMPL_TRY(platform.getImpl().createContext(*this, mDevices, userSync, &mImpl));
385*8975f5c5SAndroid Build Coastguard Worker }
386*8975f5c5SAndroid Build Coastguard Worker 
Context(Platform & platform,PropArray && properties,DeviceType deviceType,ContextErrorCB notify,void * userData,bool userSync)387*8975f5c5SAndroid Build Coastguard Worker Context::Context(Platform &platform,
388*8975f5c5SAndroid Build Coastguard Worker                  PropArray &&properties,
389*8975f5c5SAndroid Build Coastguard Worker                  DeviceType deviceType,
390*8975f5c5SAndroid Build Coastguard Worker                  ContextErrorCB notify,
391*8975f5c5SAndroid Build Coastguard Worker                  void *userData,
392*8975f5c5SAndroid Build Coastguard Worker                  bool userSync)
393*8975f5c5SAndroid Build Coastguard Worker     : mPlatform(platform),
394*8975f5c5SAndroid Build Coastguard Worker       mProperties(std::move(properties)),
395*8975f5c5SAndroid Build Coastguard Worker       mNotify(notify),
396*8975f5c5SAndroid Build Coastguard Worker       mUserData(userData),
397*8975f5c5SAndroid Build Coastguard Worker       mImpl(nullptr)
398*8975f5c5SAndroid Build Coastguard Worker {
399*8975f5c5SAndroid Build Coastguard Worker     if (!IsError(platform.getImpl().createContextFromType(*this, deviceType, userSync, &mImpl)))
400*8975f5c5SAndroid Build Coastguard Worker     {
401*8975f5c5SAndroid Build Coastguard Worker         ANGLE_CL_IMPL_TRY(mImpl->getDevices(&mDevices));
402*8975f5c5SAndroid Build Coastguard Worker     }
403*8975f5c5SAndroid Build Coastguard Worker }
404*8975f5c5SAndroid Build Coastguard Worker 
405*8975f5c5SAndroid Build Coastguard Worker }  // namespace cl
406