Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 4b38c76

Browse files
hujiajieOlli Syrjälä
authored and
Olli Syrjälä
committed
[Android] Enable WebCL on devices which only have OpenCL 1.1 support.
WebCL 1.0 is based on OpenCL 1.1, so this feature should be available as long as the device supports OpenCL 1.1 or above. This commit eliminates the hard dependencies on several OpenCL 1.2 APIs. WebCLDevice::release() is deliberately removed since clReleaseDevice() is only available since OpenCL 1.2 to release a sub device, while it is actually not required by the WebCL 1.0 implementation. BUG=XWALK-3975
1 parent b76abfb commit 4b38c76

10 files changed

+133
-55
lines changed

Source/modules/webcl/WebCL.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ PassRefPtr<WebCL> WebCL::create()
6060
WebCL::~WebCL()
6161
{
6262
releaseAll();
63-
64-
for (auto platform : m_platforms)
65-
platform->releaseAll();
6663
}
6764

6865
Vector<RefPtr<WebCLPlatform>> WebCL::getPlatforms(ExceptionState& es)

Source/modules/webcl/WebCLCommandQueue.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void WebCLCommandQueue::finish(WebCLCallback* whenFinished, ExceptionState& es)
106106
void WebCLCommandQueue::finishCommandQueues(SyncMethod method)
107107
{
108108
if (method == ASYNC) {
109-
cl_int err = clEnqueueMarkerWithWaitList(m_clCommandQueue, 0, nullptr, &m_eventForCallback);
109+
cl_int err = clEnqueueMarker(m_clCommandQueue, &m_eventForCallback);
110110
if (err != CL_SUCCESS || !m_eventForCallback)
111111
return;
112112
WebCLCommandQueueHolder* holder = new WebCLCommandQueueHolder;
@@ -154,7 +154,7 @@ void WebCLCommandQueue::enqueueBarrier(ExceptionState& es)
154154
return;
155155
}
156156

157-
cl_int err = clEnqueueBarrierWithWaitList(m_clCommandQueue, 0, nullptr, nullptr);
157+
cl_int err = clEnqueueBarrier(m_clCommandQueue);
158158
if (err != CL_SUCCESS)
159159
WebCLException::throwException(err, es);
160160
}
@@ -175,7 +175,7 @@ void WebCLCommandQueue::enqueueMarker(WebCLEvent* event, ExceptionState& es)
175175
if (event && !clEventId)
176176
return;
177177

178-
cl_int err = clEnqueueMarkerWithWaitList(m_clCommandQueue, 0, nullptr, clEventId);
178+
cl_int err = clEnqueueMarker(m_clCommandQueue, clEventId);
179179
if (err != CL_SUCCESS)
180180
WebCLException::throwException(err, es);
181181
}
@@ -196,7 +196,7 @@ void WebCLCommandQueue::enqueueWaitForEvents(const Vector<RefPtr<WebCLEvent>>& e
196196
if (clEvents.size() != events.size())
197197
return;
198198

199-
cl_int err = clEnqueueBarrierWithWaitList(m_clCommandQueue, clEvents.size(), clEvents.data(), nullptr);
199+
cl_int err = clEnqueueWaitForEvents(m_clCommandQueue, clEvents.size(), clEvents.data());
200200
if (err != CL_SUCCESS)
201201
WebCLException::throwException(err, es);
202202
}

Source/modules/webcl/WebCLContext.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,9 @@ PassRefPtr<WebCLImage> WebCLContext::createImage2DBase(unsigned flags, unsigned
390390
imageDescriptor.setChannelOrder(channelOrder);
391391
imageDescriptor.setChannelType(channelType);
392392
cl_image_format image_format = {channelOrder, channelType};
393-
cl_image_desc desc = {CL_MEM_OBJECT_IMAGE2D, static_cast<size_t>(width), static_cast<size_t>(height), 0, 0, static_cast<size_t>(rowPitch), 0, 0, 0, 0};
394393

395394
cl_int err = CL_SUCCESS;
396-
cl_mem clMemId = clCreateImage(m_clContext, flags, &image_format, &desc, data, &err);
395+
cl_mem clMemId = clCreateImage2D(m_clContext, flags, &image_format, width, height, rowPitch, data, &err);
397396
if (err != CL_SUCCESS) {
398397
WebCLException::throwException(err, es);
399398
return nullptr;

Source/modules/webcl/WebCLDevice.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ namespace blink {
1717

1818
WebCLDevice::~WebCLDevice()
1919
{
20-
release();
21-
ASSERT(!m_clDeviceId);
20+
// Unlike WebCLContext / WebCLCommandQueue / WebCLProgram / ...,
21+
// WebCLDevice does not need to call a release() method here:
22+
// 1) OpenCL 1.1 runtime has no clReleaseDevice() or an alternative, so
23+
// there's no need to release the device.
24+
// 2) The OpenCL 1.2 (or above) spec implies that clReleaseDevice() is only
25+
// meaningful for sub devices, but no sub device is created in our WebCL
26+
// 1.0 implementation.
2227
}
2328

2429
PassRefPtr<WebCLDevice> WebCLDevice::create(cl_device_id deviceId)
@@ -455,18 +460,6 @@ void WebCLDevice::getEnabledExtensions(HashSet<String>& extensions)
455460
m_extension.getEnabledExtensions(extensions);
456461
}
457462

458-
void WebCLDevice::release()
459-
{
460-
if (isReleased())
461-
return;
462-
463-
cl_int err = clReleaseDevice(m_clDeviceId);
464-
if (err != CL_SUCCESS)
465-
ASSERT_NOT_REACHED();
466-
467-
m_clDeviceId = 0;
468-
}
469-
470463
WebCLDevice::WebCLDevice(cl_device_id device, WebCLPlatform* platform)
471464
: m_platform(platform)
472465
, m_clDeviceId(device)

Source/modules/webcl/WebCLDevice.h

-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ class WebCLDevice : public RefCounted<WebCLDevice>, public ScriptWrappable {
4040
unsigned getImage2DMaxHeight();
4141
unsigned getMaxWorkGroup();
4242
Vector<unsigned> getMaxWorkItem();
43-
void release();
4443
PassRefPtr<WebCLPlatform> getPlatform() const { return m_platform; }
4544
cl_device_id getDeviceId() { return m_clDeviceId; }
4645

4746
private:
4847
WebCLDevice(cl_device_id, WebCLPlatform* platform);
49-
bool isReleased() { return !m_clDeviceId; }
5048

5149
WebCLPlatform* m_platform;
5250
WebCLExtension m_extension;

Source/modules/webcl/WebCLException.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ void WebCLException::throwException(int& code, ExceptionState& es)
215215
case CL_INVALID_PROPERTY:
216216
es.throwWebCLException(WebCLException::INVALID_PROPERTY, WebCLException::invalidPropertyMessage);
217217
break;
218-
case CL_INVALID_IMAGE_DESCRIPTOR:
219-
es.throwWebCLException(WebCLException::INVALID_IMAGE_FORMAT_DESCRIPTOR, WebCLException::invalidImageFormatDescriptorMessage);
220-
break;
221218
default:
222219
es.throwWebCLException(WebCLException::FAILURE, WebCLException::failureMessage);
223220
break;

Source/modules/webcl/WebCLOpenCL.cpp

+108-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ cl_int (CL_API_CALL *web_clGetPlatformIDs)(cl_uint num_entries, cl_platform_id*
4343

4444
cl_int (CL_API_CALL *web_clGetPlatformInfo)(cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void* param_value, size_t* param_value_size_ret);
4545

46+
cl_int (CL_API_CALL *web_clUnloadCompiler)(cl_platform_id platform);
47+
4648
cl_int (CL_API_CALL *web_clUnloadPlatformCompiler)(cl_platform_id platform);
4749

4850
/* Device APIs */
@@ -79,6 +81,8 @@ cl_int (CL_API_CALL *web_clReleaseMemObject)(cl_mem memobj);
7981

8082
cl_int (CL_API_CALL *web_clGetImageInfo)(cl_mem image, cl_image_info param_name, size_t param_value_size, void* param_value, size_t* param_value_size_ret);
8183

84+
cl_mem (CL_API_CALL *web_clCreateImage2D)(cl_context context, cl_mem_flags flags, const cl_image_format* image_format, size_t image_width, size_t image_height, size_t image_row_pitch, void* host_ptr, cl_int* errcode_ret);
85+
8286
cl_mem (CL_API_CALL *web_clCreateImage)(cl_context context, cl_mem_flags flags, const cl_image_format* image_format, const cl_image_desc* image_desc, void* host_ptr, cl_int* errcode_ret);
8387

8488
cl_int (CL_API_CALL *web_clGetSupportedImageFormats)(cl_context context, cl_mem_flags flags, cl_mem_object_type image_type, cl_uint num_entries, cl_image_format* image_formats, cl_uint* num_image_formats);
@@ -145,8 +149,12 @@ cl_int (CL_API_CALL *web_clEnqueueWriteImage)(cl_command_queue command_queue, cl
145149

146150
cl_int (CL_API_CALL *web_clEnqueueCopyBuffer)(cl_command_queue command_queue, cl_mem src_buffer, cl_mem dst_buffer, size_t src_offset, size_t dst_offset, size_t size, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event);
147151

152+
cl_int (CL_API_CALL *web_clEnqueueBarrier)(cl_command_queue command_queue);
153+
148154
cl_int (CL_API_CALL *web_clEnqueueBarrierWithWaitList)(cl_command_queue command_queue, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event);
149155

156+
cl_int (CL_API_CALL *web_clEnqueueMarker)(cl_command_queue command_queue, cl_event* event);
157+
150158
cl_int (CL_API_CALL *web_clEnqueueMarkerWithWaitList)(cl_command_queue command_queue, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event);
151159

152160
cl_int (CL_API_CALL *web_clEnqueueTask)(cl_command_queue command_queue, cl_kernel kernel, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event);
@@ -165,6 +173,8 @@ cl_int (CL_API_CALL *web_clEnqueueCopyImageToBuffer)(cl_command_queue command_qu
165173

166174
cl_int (CL_API_CALL *web_clEnqueueCopyBufferToImage)(cl_command_queue command_queue, cl_mem src_buffer, cl_mem dst_image, size_t src_offset, const size_t* dst_origin, const size_t* region, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event);
167175

176+
cl_int (CL_API_CALL *web_clEnqueueWaitForEvents)(cl_command_queue command_queue, cl_uint num_events, const cl_event* event_list);
177+
168178
/* OpenCL Extention */
169179
cl_int (CL_API_CALL *web_clEnqueueAcquireGLObjects)(cl_command_queue command_queue, cl_uint num_objects, const cl_mem* mem_objects, cl_uint num_events_in_wait_list, const cl_event* event_wait_list, cl_event* event);
170180

@@ -174,13 +184,27 @@ cl_mem (CL_API_CALL *web_clCreateFromGLBuffer)(cl_context context, cl_mem_flags
174184

175185
cl_mem (CL_API_CALL *web_clCreateFromGLRenderbuffer)(cl_context context, cl_mem_flags flags, GLuint renderbuffer, cl_int* errcode_ret);
176186

187+
cl_mem (CL_API_CALL *web_clCreateFromGLTexture2D)(cl_context context, cl_mem_flags flags, GLenum texture_target, GLint miplevel, GLuint texture, cl_int* errcode_ret);
188+
177189
cl_mem (CL_API_CALL *web_clCreateFromGLTexture)(cl_context context, cl_mem_flags flags, GLenum texture_target, GLint miplevel, GLuint texture, cl_int* errcode_ret);
178190

179191
cl_int (CL_API_CALL *web_clGetGLTextureInfo)(cl_mem, cl_gl_texture_info, size_t, void *, size_t *);
180192

193+
// These aliases are missing from WebCLOpenCL.h. Put them here for internal use only.
194+
#define clReleaseDevice web_clReleaseDevice
195+
#define clCreateImage web_clCreateImage
196+
#define clUnloadPlatformCompiler web_clUnloadPlatformCompiler
197+
#define clEnqueueMarkerWithWaitList web_clEnqueueMarkerWithWaitList
198+
#define clEnqueueBarrierWithWaitList web_clEnqueueBarrierWithWaitList
199+
#define clCreateFromGLTexture web_clCreateFromGLTexture
200+
181201
#if defined(WTF_OS_LINUX) || OS(ANDROID)
182202
#define MAP_FUNC(fn) { *(void**)(&fn) = dlsym(handle, #fn); }
183203
#define MAP_FUNC_OR_BAIL(fn) { *(void**)(&fn) = dlsym(handle, #fn); if(!fn) return false; }
204+
// In case `fn' is not defined or deprecated in the OpenCL spec tagged by
205+
// `major' and `minor', map `fn' to a wrapper implemented with APIs defined
206+
// by this spec.
207+
#define MAP_FUNC_TO_WRAPPER(fn, major, minor) { *(void**)(&fn) = (void*)fn##Impl##major##minor; }
184208

185209
static const char* DEFAULT_SO[] = LIBS;
186210
static const int DEFAULT_SO_LEN = SO_LEN;
@@ -198,6 +222,49 @@ static bool getCLHandle(const char** libs, int length)
198222
}
199223
#endif // defined(WTF_OS_LINUX) || OS(ANDROID)
200224

225+
// In OpenCL 1.1 spec, no release opertion is needed for device.
226+
static cl_int CL_API_CALL clReleaseDeviceImpl11(cl_device_id device)
227+
{
228+
return CL_SUCCESS;
229+
}
230+
231+
static cl_mem CL_API_CALL clCreateImage2DImpl12(cl_context context, cl_mem_flags flags, const cl_image_format* format, size_t width, size_t height, size_t rowPitch, void* hostPtr, cl_int* err)
232+
{
233+
cl_image_desc desc = {CL_MEM_OBJECT_IMAGE2D, static_cast<size_t>(width), static_cast<size_t>(height), 0, 0, static_cast<size_t>(rowPitch), 0, 0, 0, 0};
234+
ASSERT(clCreateImage);
235+
return clCreateImage(context, flags, format, &desc, hostPtr, err);
236+
}
237+
238+
static cl_int CL_API_CALL clUnloadCompilerImpl12(cl_platform_id platform)
239+
{
240+
ASSERT(clUnloadPlatformCompiler);
241+
return clUnloadPlatformCompiler(platform);
242+
}
243+
244+
static cl_int CL_API_CALL clEnqueueMarkerImpl12(cl_command_queue queue, cl_event* event)
245+
{
246+
ASSERT(clEnqueueMarkerWithWaitList);
247+
return clEnqueueMarkerWithWaitList(queue, 0, nullptr, event);
248+
}
249+
250+
static cl_int CL_API_CALL clEnqueueBarrierImpl12(cl_command_queue queue)
251+
{
252+
ASSERT(clEnqueueBarrierWithWaitList);
253+
return clEnqueueBarrierWithWaitList(queue, 0, nullptr, nullptr);
254+
}
255+
256+
static cl_int CL_API_CALL clEnqueueWaitForEventsImpl12(cl_command_queue queue, cl_uint numEvents, const cl_event* eventList)
257+
{
258+
ASSERT(clEnqueueBarrierWithWaitList);
259+
return clEnqueueBarrierWithWaitList(queue, numEvents, eventList, nullptr);
260+
}
261+
262+
static cl_mem CL_API_CALL clCreateFromGLTexture2DImpl12(cl_context context, cl_mem_flags flags, GLenum textureTarget, GLint miplevel, GLuint texture, cl_int* err)
263+
{
264+
ASSERT(clCreateFromGLTexture);
265+
return clCreateFromGLTexture(context, flags, textureTarget, miplevel, texture, err);
266+
}
267+
201268
bool init(const char** libs, int length)
202269
{
203270
const char** mLibs = (libs == 0 ? DEFAULT_SO : libs);
@@ -211,7 +278,6 @@ bool init(const char** libs, int length)
211278
MAP_FUNC_OR_BAIL(clCreateCommandQueue);
212279
MAP_FUNC_OR_BAIL(clCreateContext);
213280
MAP_FUNC_OR_BAIL(clCreateContextFromType);
214-
MAP_FUNC_OR_BAIL(clCreateImage);
215281
MAP_FUNC_OR_BAIL(clCreateKernel);
216282
MAP_FUNC_OR_BAIL(clCreateKernelsInProgram);
217283
MAP_FUNC_OR_BAIL(clCreateProgramWithSource);
@@ -230,8 +296,6 @@ bool init(const char** libs, int length)
230296
MAP_FUNC_OR_BAIL(clEnqueueCopyImageToBuffer);
231297
MAP_FUNC_OR_BAIL(clEnqueueReadBufferRect);
232298
MAP_FUNC_OR_BAIL(clEnqueueWriteBufferRect);
233-
MAP_FUNC_OR_BAIL(clEnqueueBarrierWithWaitList);
234-
MAP_FUNC_OR_BAIL(clEnqueueMarkerWithWaitList);
235299
MAP_FUNC_OR_BAIL(clEnqueueNDRangeKernel);
236300
MAP_FUNC_OR_BAIL(clEnqueueTask);
237301

@@ -258,7 +322,6 @@ bool init(const char** libs, int length)
258322

259323
MAP_FUNC_OR_BAIL(clReleaseCommandQueue);
260324
MAP_FUNC_OR_BAIL(clReleaseContext);
261-
MAP_FUNC_OR_BAIL(clReleaseDevice);
262325
MAP_FUNC_OR_BAIL(clReleaseEvent);
263326
MAP_FUNC_OR_BAIL(clReleaseKernel);
264327
MAP_FUNC_OR_BAIL(clReleaseMemObject);
@@ -269,7 +332,6 @@ bool init(const char** libs, int length)
269332
MAP_FUNC_OR_BAIL(clSetKernelArg);
270333
MAP_FUNC_OR_BAIL(clSetUserEventStatus);
271334

272-
MAP_FUNC_OR_BAIL(clUnloadPlatformCompiler);
273335
MAP_FUNC_OR_BAIL(clWaitForEvents);
274336

275337
// They depends on whether OpenCL library support gl_sharing extension.
@@ -278,9 +340,49 @@ bool init(const char** libs, int length)
278340
MAP_FUNC(clEnqueueReleaseGLObjects);
279341
MAP_FUNC(clCreateFromGLBuffer);
280342
MAP_FUNC(clCreateFromGLRenderbuffer);
281-
MAP_FUNC(clCreateFromGLTexture);
282343
MAP_FUNC(clGetGLTextureInfo);
283344

345+
// The following APIs are not available in all versions of the OpenCL
346+
// spec, so wrappers may be needed if they are not exported by the OpenCL
347+
// runtime library.
348+
MAP_FUNC(clReleaseDevice)
349+
if (!clReleaseDevice)
350+
MAP_FUNC_TO_WRAPPER(clReleaseDevice, 1, 1)
351+
352+
MAP_FUNC(clCreateImage)
353+
if (clCreateImage)
354+
MAP_FUNC_TO_WRAPPER(clCreateImage2D, 1, 2)
355+
else
356+
MAP_FUNC_OR_BAIL(clCreateImage2D)
357+
358+
MAP_FUNC(clUnloadPlatformCompiler)
359+
if (clUnloadPlatformCompiler)
360+
MAP_FUNC_TO_WRAPPER(clUnloadCompiler, 1, 2)
361+
else
362+
MAP_FUNC_OR_BAIL(clUnloadCompiler)
363+
364+
MAP_FUNC(clEnqueueMarkerWithWaitList)
365+
if (clEnqueueMarkerWithWaitList)
366+
MAP_FUNC_TO_WRAPPER(clEnqueueMarker, 1, 2)
367+
else
368+
MAP_FUNC_OR_BAIL(clEnqueueMarker)
369+
370+
MAP_FUNC(clEnqueueBarrierWithWaitList)
371+
if (clEnqueueBarrierWithWaitList)
372+
MAP_FUNC_TO_WRAPPER(clEnqueueBarrier, 1, 2)
373+
else
374+
MAP_FUNC_OR_BAIL(clEnqueueBarrier)
375+
376+
MAP_FUNC(clEnqueueWaitForEvents)
377+
if (!clEnqueueWaitForEvents)
378+
MAP_FUNC_TO_WRAPPER(clEnqueueWaitForEvents, 1, 2)
379+
380+
MAP_FUNC(clCreateFromGLTexture)
381+
if (clCreateFromGLTexture)
382+
MAP_FUNC_TO_WRAPPER(clCreateFromGLTexture2D, 1, 2)
383+
else
384+
MAP_FUNC(clCreateFromGLTexture2D)
385+
284386
return true;
285387
}
286388

0 commit comments

Comments
 (0)