|
| 1 | +// Copyright 2025 The Khronos Group Inc. |
| 2 | +// SPDX-License-Identifier: CC-BY-4.0 |
| 3 | + |
| 4 | +include::{generated}/meta/{refprefix}cl_khr_external_memory_android_hardware_buffer.txt[] |
| 5 | + |
| 6 | +=== Other Extension Metadata |
| 7 | + |
| 8 | +*Last Modified Date*:: |
| 9 | + 2025-05-02 |
| 10 | +*IP Status*:: |
| 11 | + No known IP claims. |
| 12 | +*Contributors*:: |
| 13 | + - Joshua Kelly, QUALCOMM |
| 14 | + - Balaji Calidas, QUALCOMM |
| 15 | + - Kevin Petit, ARM |
| 16 | + - Jeremy Kemp, GOOGLE |
| 17 | + - Paul Fradgley, IMAGINATION |
| 18 | + |
| 19 | +=== Description |
| 20 | + |
| 21 | +{cl_khr_external_memory_android_hardware_buffer_EXT} extends the functionality provided by {cl_khr_external_memory_EXT}. |
| 22 | +It enables an application to create an OpenCL image or buffer object from an AHardwareBuffer. |
| 23 | +When creating an image, the implementation will infer the image format and dimensions from the AHardwareBuffer. |
| 24 | +When creating a buffer from an AHardwareBuffer, the format must be AHARDWAREBUFFER_FORMAT_BLOB. |
| 25 | + |
| 26 | +AHardwareBuffers are reference counted objects. |
| 27 | +The OpenCL implementation will increment the reference count on the AHardwareBuffer when the image or buffer is created, and decrement the reference count when the corresponding {cl_mem_TYPE} object is deleted. |
| 28 | + |
| 29 | +Applications should use {clEnqueueMapBuffer}/{clEnqueueMapImage} for host access to the imported {cl_mem_TYPE} object. |
| 30 | +If a {clEnqueueMapBuffer}/{clEnqueueMapImage} command on the imported cl_mem object has completed, the corresponding {clEnqueueUnmapMemObject} command must also complete before AHardwareBuffer_lock may be called. |
| 31 | +Conversely, if an application has called AHardwareBuffer_lock on an AHardwareBuffer, it must also call AHardwareBuffer_unlock before any {clEnqueueMapBuffer}/{clEnqueueMapImage} command on the imported {cl_mem_TYPE} object reaches the running, ended, or completed state. |
| 32 | + |
| 33 | +=== New API Enums |
| 34 | + |
| 35 | +=== New Enums |
| 36 | + * {cl_external_memory_handle_type_khr_TYPE} |
| 37 | + ** {CL_EXTERNAL_MEMORY_HANDLE_ANDROID_HARDWARE_BUFFER_KHR} |
| 38 | + |
| 39 | +List of minimum supported AHardwareBuffer formats and the corresponding OpenCL formats: |
| 40 | + |
| 41 | +[[cl_khr_external_memory_android_hardware_buffer-mapping-of-image-formats]] |
| 42 | +.Minimum list of AHardwareBuffer formats and corresponding OpenCL formats |
| 43 | +[width="100%",cols="3,1,1,1",options="header"] |
| 44 | +|=== |
| 45 | +| AHardwareBuffer Format | OpenCL Image Channel Order | OpenCL Image Channel Data Type | OpenCL Memory Object Type |
| 46 | + |
| 47 | +| `AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM` |
| 48 | +| {CL_RGBA} |
| 49 | +| {CL_UNORM_INT8} |
| 50 | +| Image, e.g. {CL_MEM_OBJECT_IMAGE2D} |
| 51 | + |
| 52 | +| `AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT` |
| 53 | +| {CL_RGBA} |
| 54 | +| {CL_HALF_FLOAT} |
| 55 | +| Image, e.g. {CL_MEM_OBJECT_IMAGE2D} |
| 56 | + |
| 57 | +| `AHARDWAREBUFFER_FORMAT_R8_UNORM` |
| 58 | +| {CL_R} |
| 59 | +| {CL_UNORM_INT8} |
| 60 | +| Image, e.g. {CL_MEM_OBJECT_IMAGE2D} |
| 61 | + |
| 62 | +| `AHARDWAREBUFFER_FORMAT_BLOB` |
| 63 | +| `N/A` |
| 64 | +| `N/A` |
| 65 | +| {CL_MEM_OBJECT_BUFFER} |
| 66 | +|=== |
| 67 | + |
| 68 | +[[cl_khr_external_memory_android_hardware_buffer-Sample-Code]] |
| 69 | +=== Sample Code |
| 70 | + |
| 71 | +. Using the extension for CL buffer objects |
| 72 | ++ |
| 73 | +-- |
| 74 | +[source,c] |
| 75 | +---- |
| 76 | +// Create AHB |
| 77 | +const AHardwareBuffer_Desc desc = {, AHARDWAREBUFFER_FORMAT_BLOB, .....}; |
| 78 | +AHardwareBuffer *ahb_handle = NULL; |
| 79 | +AHardwareBuffer_allocate(&desc, &ahb_handle); |
| 80 | +cl_mem_properties props[] = { |
| 81 | + (cl_mem_properties) CL_EXTERNAL_MEMORY_HANDLE_ANDROID_HARDWARE_BUFFER_KHR, |
| 82 | + (cl_mem_properties) ahb_handle, |
| 83 | + 0}; |
| 84 | +cl_mem imported_buffer = clCreateBufferWithProperties(context, props, CL_MEM_READ_WRITE, |
| 85 | + 0 /* size */, NULL, &error); |
| 86 | +If (error == CL_SUCCESS) |
| 87 | +{ |
| 88 | + clEnqueueAcquireExternalMemObjectsKHR(queue, 1, &imported_buffer, ...); |
| 89 | + clEnqueueCopyBuffer(queue, imported_buffer, ...); |
| 90 | + clEnqueueReleaseExternalMemObjectsKHR(queue, 1, &imported_buffer, ...); |
| 91 | +} |
| 92 | +clReleaseMemObject(imported_buffer); |
| 93 | +---- |
| 94 | +-- |
| 95 | + |
| 96 | +. Using the extension for CL image objects |
| 97 | ++ |
| 98 | +-- |
| 99 | +[source, c] |
| 100 | +---- |
| 101 | +const AHardwareBuffer_Desc desc = {, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, .....}; |
| 102 | +AHardwareBuffer *ahb_handle = NULL; |
| 103 | +AHardwareBuffer_allocate(&desc, &ahb_handle); |
| 104 | +cl_mem_properties props[] = { |
| 105 | + (cl_mem_properties) CL_EXTERNAL_MEMORY_HANDLE_ANDROID_HARDWARE_BUFFER_KHR, |
| 106 | + (cl_mem_properties) ahb_handle, |
| 107 | + 0}; |
| 108 | +cl_mem imported_image = clCreateImageWithProperties(context, props, CL_MEM_READ_WRITE, |
| 109 | + NULL, NULL, NULL, &error); |
| 110 | +if (error == CL_SUCCESS) |
| 111 | +{ |
| 112 | + clGetImageInfo(imported_image, ...); |
| 113 | +} |
| 114 | +---- |
| 115 | +-- |
| 116 | + |
| 117 | +==== Version History |
| 118 | + |
| 119 | + * Revision 0.9.0, 2025-03-13 |
| 120 | + ** First assigned version (provisional). |
| 121 | + * Revision 0.9.2 2025-05-02 |
| 122 | + ** Reduced required formats, first published version (provisional). |
0 commit comments