diff --git a/test_conformance/extensions/CMakeLists.txt b/test_conformance/extensions/CMakeLists.txt index aa57990b..497fc9cd 100644 --- a/test_conformance/extensions/CMakeLists.txt +++ b/test_conformance/extensions/CMakeLists.txt @@ -6,6 +6,7 @@ add_subdirectory( cl_ext_cxx_for_opencl ) add_subdirectory( cl_khr_command_buffer ) add_subdirectory( cl_khr_dx9_media_sharing ) add_subdirectory( cl_khr_external_memory_dma_buf ) +add_subdirectory( cl_khr_external_memory_ahb ) add_subdirectory( cl_khr_semaphore ) add_subdirectory( cl_khr_kernel_clock ) add_subdirectory( cl_ext_buffer_device_address ) diff --git a/test_conformance/extensions/cl_khr_external_memory_ahb/CMakeLists.txt b/test_conformance/extensions/cl_khr_external_memory_ahb/CMakeLists.txt new file mode 100644 index 00000000..d4ab735f --- /dev/null +++ b/test_conformance/extensions/cl_khr_external_memory_ahb/CMakeLists.txt @@ -0,0 +1,11 @@ +set(MODULE_NAME CL_KHR_EXTERNAL_MEMORY_AHB) + +set(${MODULE_NAME}_SOURCES + main.cpp + test_ahb.cpp + debug_ahb.cpp +) + +link_libraries(android OpenCL) + +include(../../CMakeCommon.txt) diff --git a/test_conformance/extensions/cl_khr_external_memory_ahb/debug_ahb.cpp b/test_conformance/extensions/cl_khr_external_memory_ahb/debug_ahb.cpp new file mode 100644 index 00000000..46dc4ae1 --- /dev/null +++ b/test_conformance/extensions/cl_khr_external_memory_ahb/debug_ahb.cpp @@ -0,0 +1,200 @@ +// +// Created by joshkell on 8/18/24. +// + +#include +#include +#include "debug_ahb.h" + +AHardwareBuffer_UsageFlags flag_list[] = { + AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, + AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, + AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER, + AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY, + AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, + AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK, + AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, + AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, + AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY, + AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT, + AHARDWAREBUFFER_USAGE_VIDEO_ENCODE, + AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA, + AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER, + AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP, + AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE, + AHARDWAREBUFFER_USAGE_FRONT_BUFFER, // This is not in older NDK 25 +}; + +char *ahardwareBufferDecodeUsageFlagsToString(AHardwareBuffer_UsageFlags flags) +{ + size_t flags_len = 0; + size_t num_flags = 0; + const char *separator = "|"; + + for (uint64_t f : flag_list) + { + if (((f & flags) != 0) && ((f & flags) == f)) + { + flags_len += strlen(ahardwareBufferUsageFlagToString( + static_cast(f))); + num_flags++; + } + } + + if (num_flags == 0) + { + const char *unknown_flag = "UNKNOWN_FLAG"; + size_t res_size = strlen(unknown_flag) + 1; + char *result = new char[res_size]; + strlcat(result, unknown_flag, res_size); + return result; + } + + size_t string_len = flags_len + ((num_flags - 1) * strlen(separator)) + 1; + char *result = new char[string_len]; + memset(result, 0, string_len); + + size_t flag_counter = 0; + for (uint64_t f : flag_list) + { + if (((f & flags) != 0) && ((f & flags) == f)) + { + flag_counter++; + strlcat(result, + ahardwareBufferUsageFlagToString( + static_cast(f)), + string_len); + if (flag_counter < num_flags) + { + strlcat(result, separator, string_len); + } + } + } + + return result; +} + +const char *ahardwareBufferUsageFlagToString(AHardwareBuffer_UsageFlags flag) +{ + const char *result = ""; + switch (flag) + { + case AHARDWAREBUFFER_USAGE_CPU_READ_NEVER: + result = "AHARDWAREBUFFER_USAGE_CPU_READ_NEVER"; + break; + case AHARDWAREBUFFER_USAGE_CPU_READ_RARELY: + result = "AHARDWAREBUFFER_USAGE_CPU_READ_RARELY"; + break; + case AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN: + result = "AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN"; + break; + case AHARDWAREBUFFER_USAGE_CPU_READ_MASK: + result = "AHARDWAREBUFFER_USAGE_CPU_READ_MASK"; + break; + case AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY: + result = "AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY"; + break; + case AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN: + result = "AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN"; + break; + case AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK: + result = "AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK"; + break; + case AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE: + result = "AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE"; + break; + case AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER: + result = "AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER"; + break; + case AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY: + result = "AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY"; + break; + case AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT: + result = "AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT"; + break; + case AHARDWAREBUFFER_USAGE_VIDEO_ENCODE: + result = "AHARDWAREBUFFER_USAGE_VIDEO_ENCODE"; + break; + case AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA: + result = "AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA"; + break; + case AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER: + result = "AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER"; + break; + case AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP: + result = "AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP"; + break; + case AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE: + result = "AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE"; + break; + default: result = "Unknown flag"; + } + return result; +} + +const char *ahardwareBufferFormatToString(AHardwareBuffer_Format format) +{ + const char *result = ""; + switch (format) + { + case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: + result = "AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: + result = "AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: + result = "AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: + result = "AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: + result = "AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT"; + break; + case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: + result = "AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_BLOB: + result = "AHARDWAREBUFFER_FORMAT_BLOB"; + break; + case AHARDWAREBUFFER_FORMAT_D16_UNORM: + result = "AHARDWAREBUFFER_FORMAT_D16_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_D24_UNORM: + result = "AHARDWAREBUFFER_FORMAT_D24_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT: + result = "AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT"; + break; + case AHARDWAREBUFFER_FORMAT_D32_FLOAT: + result = "AHARDWAREBUFFER_FORMAT_D32_FLOAT"; + break; + case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT: + result = "AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT"; + break; + case AHARDWAREBUFFER_FORMAT_S8_UINT: + result = "AHARDWAREBUFFER_FORMAT_S8_UINT"; + break; + case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420: + result = "AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420"; + break; + case AHARDWAREBUFFER_FORMAT_YCbCr_P010: + result = "AHARDWAREBUFFER_FORMAT_YCbCr_P010"; + break; + case AHARDWAREBUFFER_FORMAT_R8_UNORM: + result = "AHARDWAREBUFFER_FORMAT_R8_UNORM"; + break; + case AHARDWAREBUFFER_FORMAT_R16_UINT: // This is not in older NDK 25 + result = "AHARDWAREBUFFER_FORMAT_R16_UINT"; + break; + case AHARDWAREBUFFER_FORMAT_R16G16_UINT: // This is not in older NDK 25 + result = "AHARDWAREBUFFER_FORMAT_R16G16_UINT"; + break; + case AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM: // This is not in older + // NDK 25 + result = "AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM"; + break; + } + return result; +} \ No newline at end of file diff --git a/test_conformance/extensions/cl_khr_external_memory_ahb/debug_ahb.h b/test_conformance/extensions/cl_khr_external_memory_ahb/debug_ahb.h new file mode 100644 index 00000000..a8aac4b7 --- /dev/null +++ b/test_conformance/extensions/cl_khr_external_memory_ahb/debug_ahb.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +const char* ahardwareBufferFormatToString(AHardwareBuffer_Format format); +const char* ahardwareBufferUsageFlagToString(AHardwareBuffer_UsageFlags flag); +char* ahardwareBufferDecodeUsageFlagsToString(AHardwareBuffer_UsageFlags flags); + +#define CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR 0x41A5 \ No newline at end of file diff --git a/test_conformance/extensions/cl_khr_external_memory_ahb/main.cpp b/test_conformance/extensions/cl_khr_external_memory_ahb/main.cpp new file mode 100644 index 00000000..c2c320f2 --- /dev/null +++ b/test_conformance/extensions/cl_khr_external_memory_ahb/main.cpp @@ -0,0 +1,38 @@ +// +// Copyright (c) 2023 The Khronos Group Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#include "procs.h" +#include "harness/testHarness.h" + +#include + +test_definition test_list[] = { + ADD_TEST_VERSION(images, Version(1, 0)), + ADD_TEST_VERSION(blob, Version(1, 0)), + ADD_TEST_VERSION(images_read, Version(1, 0)), + ADD_TEST_VERSION(enqueue_read_image, Version(1, 0)), + ADD_TEST_VERSION(enqueue_copy_image, Version(1, 0)), + ADD_TEST_VERSION(enqueue_copy_image_to_buffer, Version(1, 0)), + ADD_TEST_VERSION(enqueue_copy_buffer_to_image, Version(1, 0)), + ADD_TEST_VERSION(enqueue_write_image, Version(1, 0)), + ADD_TEST_VERSION(enqueue_fill_image, Version(1, 0)) +}; + +const int test_num = ARRAY_SIZE(test_list); + +int main(int argc, const char *argv[]) +{ + return runTestHarness(argc, argv, test_num, test_list, false, 0); +} diff --git a/test_conformance/extensions/cl_khr_external_memory_ahb/procs.h b/test_conformance/extensions/cl_khr_external_memory_ahb/procs.h new file mode 100644 index 00000000..5a552fd4 --- /dev/null +++ b/test_conformance/extensions/cl_khr_external_memory_ahb/procs.h @@ -0,0 +1,47 @@ +// +// Copyright (c) 2023 The Khronos Group Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#include "harness/errorHelpers.h" +#include "harness/kernelHelpers.h" +#include "harness/typeWrappers.h" +#include "harness/clImageHelper.h" +#include "harness/imageHelpers.h" + +extern int test_images(cl_device_id deviceID, cl_context context, + cl_command_queue queue, int num_elements); +extern int test_blob(cl_device_id deviceID, cl_context context, + cl_command_queue queue, int num_elements); +extern int test_images_read(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements); +extern int test_enqueue_read_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, + int num_elements); +extern int test_enqueue_copy_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, + int num_elements); +extern int test_enqueue_copy_image_to_buffer(cl_device_id deviceID, + cl_context context, + cl_command_queue defaultQueue, + int num_elements); +extern int test_enqueue_copy_buffer_to_image(cl_device_id deviceID, + cl_context context, + cl_command_queue defaultQueue, + int num_elements); +extern int test_enqueue_write_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, + int num_elements); +extern int test_enqueue_fill_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, + int num_elements); diff --git a/test_conformance/extensions/cl_khr_external_memory_ahb/test_ahb.cpp b/test_conformance/extensions/cl_khr_external_memory_ahb/test_ahb.cpp new file mode 100644 index 00000000..89ceb837 --- /dev/null +++ b/test_conformance/extensions/cl_khr_external_memory_ahb/test_ahb.cpp @@ -0,0 +1,1921 @@ +// +// Copyright (c) 2023 The Khronos Group Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "harness/compat.h" +#include "harness/kernelHelpers.h" +#include "harness/clImageHelper.h" +#include "harness/imageHelpers.h" +#include "harness/typeWrappers.h" +#include "harness/extensionHelpers.h" +#include "harness/errorHelpers.h" +#include +#include "debug_ahb.h" + +static bool isAHBUsageReadable(AHardwareBuffer_UsageFlags usage) +{ + return (AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | usage) != 0; +} + +struct ahb_format_table +{ + AHardwareBuffer_Format aHardwareBufferFormat; + cl_image_format clImageFormat; + cl_mem_object_type clMemObjectType; +}; + +struct ahb_usage_table +{ + AHardwareBuffer_UsageFlags usageFlags; +}; + +struct ahb_image_size_table +{ + uint32_t width; + uint32_t height; +}; + +ahb_image_size_table test_sizes[] = { { 128, 128 } }; + +ahb_usage_table test_usages[] = { + { static_cast( + AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN + | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN + | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE + | AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) }, + { static_cast( + AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) }, + { static_cast( + AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) }, +}; + +ahb_format_table test_formats[] = { + { AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT, + { CL_RGBA, CL_HALF_FLOAT }, + CL_MEM_OBJECT_IMAGE2D }, + + { AHARDWAREBUFFER_FORMAT_R16G16_UINT, + { CL_RG, CL_UNSIGNED_INT16 }, + CL_MEM_OBJECT_IMAGE2D }, + + { AHARDWAREBUFFER_FORMAT_R16_UINT, + { CL_R, CL_UNSIGNED_INT16 }, + CL_MEM_OBJECT_IMAGE2D }, + + { AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, + { CL_RGBA, CL_UNORM_INT8 }, + CL_MEM_OBJECT_IMAGE2D }, + + { AHARDWAREBUFFER_FORMAT_R8_UNORM, + { CL_R, CL_UNORM_INT8 }, + CL_MEM_OBJECT_IMAGE2D }, +}; + +static const char *diff_images_kernel_source = { + R"( + #define PIXEL_FORMAT %s4 + __kernel void verify_image( read_only image2d_t ahb_image , read_only image2d_t ocl_image, global PIXEL_FORMAT *ocl_pixel, global PIXEL_FORMAT *ahb_pixel) + { + int tidX = get_global_id(0); + int tidY = get_global_id(1); + int idx = tidY * get_global_size(0) + tidX; + + sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST; + PIXEL_FORMAT a = read_image%s(ahb_image, sampler, (int2)( tidX, tidY ) ); + PIXEL_FORMAT o = read_image%s(ocl_image, sampler, (int2)( tidX, tidY ) ); + ahb_pixel[idx] = a; + ocl_pixel[idx] = o; + })" +}; + +// Confirm that a signal followed by a wait will complete successfully +int test_images(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + cl_int err; + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + cl_mem image = clCreateImageWithProperties( + context, props, CL_MEM_READ_WRITE, nullptr, nullptr, + nullptr, &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + cl_image_format imageFormat = { 0 }; + err = clGetImageInfo(image, CL_IMAGE_FORMAT, + sizeof(cl_image_format), &imageFormat, + nullptr); + test_error(err, "Failed to query image format"); + + if (imageFormat.image_channel_order + != format.clImageFormat.image_channel_order) + { + log_error("Expected channel order %d, got %d\n", + format.clImageFormat.image_channel_order, + imageFormat.image_channel_order); + return TEST_FAIL; + } + + if (imageFormat.image_channel_data_type + != format.clImageFormat.image_channel_data_type) + { + log_error("Expected image_channel_data_type %d, got %d\n", + format.clImageFormat.image_channel_data_type, + imageFormat.image_channel_data_type); + return TEST_FAIL; + } + + test_error(clReleaseMemObject(image), + "Failed to release image"); + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + } + } + } + + return TEST_PASS; +} + +// Confirm that a signal followed by a wait will complete successfully +int test_images_read(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + // Populate AHB with random data + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = hardware_buffer_desc.stride * pixelSize; + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t srcBytes = get_image_size(&imageInfo); + test_assert_error(srcBytes > 0, "Image cannot have zero size"); + + BufferOwningPtr srcData; + generate_random_image_data(&imageInfo, srcData, seed); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + memcpy(hardware_buffer_data, srcData, srcBytes); + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_ONLY, nullptr, nullptr, nullptr, + &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + cl_image_desc imageDesc = { 0 }; + imageDesc.image_type = imageInfo.type; + imageDesc.image_width = imageInfo.width; + imageDesc.image_height = imageInfo.height; + imageDesc.image_row_pitch = imageInfo.rowPitch; + + clMemWrapper opencl_image = clCreateImage( + context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, + imageInfo.format, &imageDesc, srcData, &err); + test_error(err, "Failed to create CL image"); + + ExplicitTypes outputType; + const char *readFormat; + + if (format.clImageFormat.image_channel_data_type + == CL_UNSIGNED_INT8) + { + readFormat = "ui"; + outputType = kUInt; + } + else + { + readFormat = "f"; + outputType = kFloat; + } + + size_t verify_buffer_size = imageInfo.width * imageInfo.height + * get_explicit_type_size(outputType) * 4; + + clMemWrapper ocl_pixel_buffer = + clCreateBuffer(context, CL_MEM_READ_WRITE, + verify_buffer_size, nullptr, &err); + test_error(err, "Failed to create ocl pixel buffer"); + + clMemWrapper ahb_pixel_buffer = + clCreateBuffer(context, CL_MEM_READ_WRITE, + verify_buffer_size, nullptr, &err); + test_error(err, "Failed to crete ahb pixel buffer"); + + // Populate kernel + std::vector programSrc( + 2 * strlen(diff_images_kernel_source)); + const char *outputTypeName = get_explicit_type_name(outputType); + + sprintf(programSrc.data(), diff_images_kernel_source, + outputTypeName, // Read image format 1 + readFormat, // Read image return type 1 + readFormat // Read image return type 2 + ); + const char *ptr = programSrc.data(); + clProgramWrapper program; + clKernelWrapper kernel; + err = create_single_kernel_helper(context, &program, &kernel, 1, + &ptr, "verify_image"); + + // Set kernel args + + err = + clSetKernelArg(kernel, 0, sizeof(cl_mem), &imported_image); + test_error(err, "clSetKernelArg failed"); + + err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &opencl_image); + test_error(err, "clSetKernelArg failed"); + + err = clSetKernelArg(kernel, 2, sizeof(cl_mem), + &ocl_pixel_buffer); + test_error(err, "clSetKernelArg failed"); + + err = clSetKernelArg(kernel, 3, sizeof(cl_mem), + &ahb_pixel_buffer); + test_error(err, "clSetKernelArg failed"); + + size_t global_work_size[] = { (imageInfo.width), + (imageInfo.height) }; + err = clEnqueueNDRangeKernel(defaultQueue, kernel, 2, nullptr, + global_work_size, nullptr, 0, + nullptr, nullptr); + + // Read buffer and verify + std::vector ocl_verify_data(verify_buffer_size); + err = clEnqueueReadBuffer(defaultQueue, ocl_pixel_buffer, + CL_BLOCKING, 0, verify_buffer_size, + ocl_verify_data.data(), 0, nullptr, + nullptr); + test_error(err, "clEnqueueReadBuffer failed"); + + std::vector ahb_verify_data(verify_buffer_size); + err = clEnqueueReadBuffer(defaultQueue, ahb_pixel_buffer, + CL_BLOCKING, 0, verify_buffer_size, + ahb_verify_data.data(), 0, nullptr, + nullptr); + test_error(err, "clEnqueueReadBuffer failed"); + + for (unsigned row = 0; row < imageInfo.height; row++) + { + for (unsigned col = 0; col < imageInfo.width; col++) + { + unsigned pixel_index = row * imageInfo.width + col; + switch (outputType) + { + case kFloat: { + auto *cl_ptr = reinterpret_cast( + ocl_verify_data.data()); + auto *ahb_ptr = reinterpret_cast( + ahb_verify_data.data()); + + if ((cl_ptr[pixel_index].s0 + != ahb_ptr[pixel_index].s0) + || (cl_ptr[pixel_index].s1 + != ahb_ptr[pixel_index].s1) + || (cl_ptr[pixel_index].s2 + != ahb_ptr[pixel_index].s2) + || (cl_ptr[pixel_index].s3 + != ahb_ptr[pixel_index].s3)) + { + log_error( + "At coord (%u, %u) expected " + "(%f,%f,%f,%f), got (%f,%f,%f,%f)", + col, row, cl_ptr[pixel_index].s0, + cl_ptr[pixel_index].s1, + cl_ptr[pixel_index].s2, + cl_ptr[pixel_index].s3, + ahb_ptr[pixel_index].s0, + ahb_ptr[pixel_index].s1, + ahb_ptr[pixel_index].s2, + ahb_ptr[pixel_index].s3); + + return TEST_FAIL; + } + } + break; + case kUInt: { + auto *cl_ptr = reinterpret_cast( + ocl_verify_data.data()); + auto *ahb_ptr = reinterpret_cast( + ahb_verify_data.data()); + + if ((cl_ptr[pixel_index].s0 + != ahb_ptr[pixel_index].s0) + || (cl_ptr[pixel_index].s1 + != ahb_ptr[pixel_index].s1) + || (cl_ptr[pixel_index].s2 + != ahb_ptr[pixel_index].s2) + || (cl_ptr[pixel_index].s3 + != ahb_ptr[pixel_index].s3)) + { + log_error( + "At coord (%u, %u) expected " + "(%u,%u,%u,%u), got (%u,%u,%u,%u)", + col, row, cl_ptr[pixel_index].s0, + cl_ptr[pixel_index].s1, + cl_ptr[pixel_index].s2, + cl_ptr[pixel_index].s3, + ahb_ptr[pixel_index].s0, + ahb_ptr[pixel_index].s1, + ahb_ptr[pixel_index].s2, + ahb_ptr[pixel_index].s3); + return TEST_FAIL; + } + } + break; + default: test_fail("Unknown output type"); + } + } + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + } + } + } + + return TEST_PASS; +} + +// clEnqueueReadImage + +int test_enqueue_read_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + // Populate AHB with random data + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = hardware_buffer_desc.stride * pixelSize; + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t srcBytes = get_image_size(&imageInfo); + test_assert_error(srcBytes > 0, "Image cannot have zero size"); + + BufferOwningPtr srcData; + generate_random_image_data(&imageInfo, srcData, seed); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + memcpy(hardware_buffer_data, srcData, srcBytes); + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_ONLY, nullptr, nullptr, nullptr, + &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + size_t origin[] = { 0, 0, 0 }; + size_t region[] = { imageInfo.width, imageInfo.height, 1 }; + + std::vector out_image(srcBytes); + err = clEnqueueReadImage(defaultQueue, imported_image, CL_TRUE, + origin, region, imageInfo.rowPitch, 0, + out_image.data(), 0, nullptr, nullptr); + test_error(err, "clEnqueueCopyImage failed"); + + char *out_image_ptr = out_image.data(); + char *srcData_ptr = (char *)srcData; + + size_t scanlineSize = + imageInfo.width * get_pixel_size(imageInfo.format); + + // Count the number of bytes successfully matched + size_t total_matched = 0; + for (size_t line = 0; line < imageInfo.height; line++) + { + + if (memcmp(srcData_ptr, out_image_ptr, scanlineSize) != 0) + { + // Find the first differing pixel + size_t pixel_size = get_pixel_size(imageInfo.format); + size_t where = compare_scanlines( + &imageInfo, srcData_ptr, out_image_ptr); + if (where < imageInfo.width) + { + print_first_pixel_difference_error( + where, srcData_ptr + pixel_size * where, + out_image_ptr + pixel_size * where, &imageInfo, + line, 1); + return TEST_FAIL; + } + } + + total_matched += scanlineSize; + srcData_ptr += imageInfo.rowPitch; + out_image_ptr += imageInfo.rowPitch; + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + + if (total_matched == 0) + { + test_fail("Zero bytes matched"); + } + } + } + } + + return TEST_PASS; +} + + +// clEnqueueCopyImage + +int test_enqueue_copy_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + // Populate AHB with random data + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = hardware_buffer_desc.stride * pixelSize; + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t srcBytes = get_image_size(&imageInfo); + test_assert_error(srcBytes > 0, "Image cannot have zero size"); + + BufferOwningPtr srcData; + generate_random_image_data(&imageInfo, srcData, seed); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + memcpy(hardware_buffer_data, srcData, srcBytes); + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_ONLY, nullptr, nullptr, nullptr, + &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + cl_image_desc imageDesc = { 0 }; + imageDesc.image_type = imageInfo.type; + imageDesc.image_width = imageInfo.width; + imageDesc.image_height = imageInfo.height; + + clMemWrapper opencl_image = + clCreateImage(context, CL_MEM_READ_WRITE, imageInfo.format, + &imageDesc, nullptr, &err); + test_error(err, "Failed to create CL image"); + + size_t origin[] = { 0, 0, 0 }; + size_t region[] = { imageInfo.width, imageInfo.height, 1 }; + err = clEnqueueCopyImage(defaultQueue, imported_image, + opencl_image, origin, origin, region, + 0, nullptr, nullptr); + test_error(err, "Failed calling clEnqueueCopyImage"); + + ExplicitTypes outputType; + const char *readFormat; + + if (format.clImageFormat.image_channel_data_type + == CL_UNSIGNED_INT8) + { + readFormat = "ui"; + outputType = kUInt; + } + else + { + readFormat = "f"; + outputType = kFloat; + } + + size_t verify_buffer_size = imageInfo.width * imageInfo.height + * get_explicit_type_size(outputType) * 4; + + clMemWrapper ocl_pixel_buffer = + clCreateBuffer(context, CL_MEM_READ_WRITE, + verify_buffer_size, nullptr, &err); + test_error(err, "Failed to create ocl pixel buffer"); + + clMemWrapper ahb_pixel_buffer = + clCreateBuffer(context, CL_MEM_READ_WRITE, + verify_buffer_size, nullptr, &err); + test_error(err, "Failed to crete ahb pixel buffer"); + + // sprintf the kernel + std::vector programSrc( + 2 * strlen(diff_images_kernel_source)); + const char *outputTypeName = get_explicit_type_name(outputType); + + sprintf(programSrc.data(), diff_images_kernel_source, + outputTypeName, /*read image format 1 */ + readFormat, /*read image return type 1 */ + readFormat /*read image return type 2 */ + ); + const char *ptr = programSrc.data(); + clProgramWrapper program; + clKernelWrapper kernel; + err = create_single_kernel_helper(context, &program, &kernel, 1, + &ptr, "verify_image"); + + // set kernel args + + err = clSetKernelArg(kernel, 0, sizeof(cl_mem), + &imported_image); /*imported image */ + test_error(err, "clSetKernelArg failed"); + + err = clSetKernelArg(kernel, 1, sizeof(cl_mem), + &opencl_image); /*image made in opencl*/ + test_error(err, "clSetKernelArg failed"); + + err = clSetKernelArg(kernel, 2, sizeof(cl_mem), + &ocl_pixel_buffer); /*verification buffer*/ + test_error(err, "clSetKernelArg failed"); + + err = clSetKernelArg(kernel, 3, sizeof(cl_mem), + &ahb_pixel_buffer); /*verification buffer*/ + test_error(err, "clSetKernelArg failed"); + + size_t global_work_size[] = { (imageInfo.width), + (imageInfo.height) }; + err = clEnqueueNDRangeKernel(defaultQueue, kernel, 2, nullptr, + global_work_size, nullptr, 0, + nullptr, nullptr); + + // Read buffer and verify + std::vector ocl_verify_data(verify_buffer_size); + err = clEnqueueReadBuffer(defaultQueue, ocl_pixel_buffer, + CL_BLOCKING, 0, verify_buffer_size, + ocl_verify_data.data(), 0, nullptr, + nullptr); + test_error(err, "clEnqueueReadBuffer failed"); + + std::vector ahb_verify_data(verify_buffer_size); + err = clEnqueueReadBuffer(defaultQueue, ahb_pixel_buffer, + CL_BLOCKING, 0, verify_buffer_size, + ahb_verify_data.data(), 0, nullptr, + nullptr); + test_error(err, "clEnqueueReadBuffer failed"); + + for (unsigned row = 0; row < imageInfo.height; row++) + { + for (unsigned col = 0; col < imageInfo.width; col++) + { + unsigned pixel_index = row * imageInfo.width + col; + switch (outputType) + { + case kFloat: { + auto *cl_ptr = reinterpret_cast( + ocl_verify_data.data()); + auto *ahb_ptr = reinterpret_cast( + ahb_verify_data.data()); + + if ((cl_ptr[pixel_index].s0 + != ahb_ptr[pixel_index].s0) + || (cl_ptr[pixel_index].s1 + != ahb_ptr[pixel_index].s1) + || (cl_ptr[pixel_index].s2 + != ahb_ptr[pixel_index].s2) + || (cl_ptr[pixel_index].s3 + != ahb_ptr[pixel_index].s3)) + { + printf("At %u\n", pixel_index); + printf("Expected %f,%f,%f,%f\n", + cl_ptr[pixel_index].s0, + cl_ptr[pixel_index].s1, + cl_ptr[pixel_index].s2, + cl_ptr[pixel_index].s3); + printf("Got %f,%f,%f,%f\n", + ahb_ptr[pixel_index].s0, + ahb_ptr[pixel_index].s1, + ahb_ptr[pixel_index].s2, + ahb_ptr[pixel_index].s3); + + return TEST_FAIL; + } + } + break; + case kUInt: { + auto *cl_ptr = reinterpret_cast( + ocl_verify_data.data()); + auto *ahb_ptr = reinterpret_cast( + ahb_verify_data.data()); + + if ((cl_ptr[pixel_index].s0 + != ahb_ptr[pixel_index].s0) + || (cl_ptr[pixel_index].s1 + != ahb_ptr[pixel_index].s1) + || (cl_ptr[pixel_index].s2 + != ahb_ptr[pixel_index].s2) + || (cl_ptr[pixel_index].s3 + != ahb_ptr[pixel_index].s3)) + { + printf("At %u\n", pixel_index); + printf("Expected %u,%u,%u,%u\n", + cl_ptr[pixel_index].s0, + cl_ptr[pixel_index].s1, + cl_ptr[pixel_index].s2, + cl_ptr[pixel_index].s3); + printf("Got %u,%u,%u,%u\n", + ahb_ptr[pixel_index].s0, + ahb_ptr[pixel_index].s1, + ahb_ptr[pixel_index].s2, + ahb_ptr[pixel_index].s3); + + return TEST_FAIL; + } + } + break; + default: test_fail("Unknown output type"); + } + } + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + } + } + } + + return TEST_PASS; +} + +// clEnqueueCopyImageToBuffer + +int test_enqueue_copy_image_to_buffer(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, + int num_elements) +{ + + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + // Populate AHB with random data + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = hardware_buffer_desc.stride * pixelSize; + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t srcBytes = get_image_size(&imageInfo); + test_assert_error(srcBytes > 0, "Image cannot have zero size"); + + BufferOwningPtr srcData; + generate_random_image_data(&imageInfo, srcData, seed); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + memcpy(hardware_buffer_data, srcData, srcBytes); + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_ONLY, nullptr, nullptr, nullptr, + &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + clMemWrapper opencl_buffer = clCreateBuffer( + context, CL_MEM_READ_WRITE, srcBytes, nullptr, &err); + test_error(err, "Failed to create CL buffer"); + + size_t origin[] = { 0, 0, 0 }; + size_t region[] = { imageInfo.width, imageInfo.height, 1 }; + + err = clEnqueueCopyImageToBuffer(defaultQueue, imported_image, + opencl_buffer, origin, region, + 0, 0, nullptr, nullptr); + test_error( + err, "Failed to copy imported AHB image to opencl buffer"); + + std::vector out_buffer(srcBytes); + err = clEnqueueReadBuffer(defaultQueue, opencl_buffer, CL_TRUE, + 0, srcBytes, out_buffer.data(), 0, + nullptr, nullptr); + test_error(err, "clEnqueueReadBuffer failed"); + + char *out_buffer_ptr = out_buffer.data(); + char *srcData_ptr = (char *)srcData; + + + size_t scanlineSize = + imageInfo.width * get_pixel_size(imageInfo.format); + + // Count the number of bytes successfully matched + size_t total_matched = 0; + for (size_t line = 0; line < imageInfo.height; line++) + { + + if (memcmp(srcData_ptr, out_buffer_ptr, scanlineSize) != 0) + { + // Find the first differing pixel + size_t pixel_size = get_pixel_size(imageInfo.format); + size_t where = compare_scanlines( + &imageInfo, srcData_ptr, out_buffer_ptr); + if (where < imageInfo.width) + { + print_first_pixel_difference_error( + where, srcData_ptr + pixel_size * where, + out_buffer_ptr + pixel_size * where, &imageInfo, + line, 1); + return TEST_FAIL; + } + } + + total_matched += scanlineSize; + srcData_ptr += imageInfo.rowPitch; + out_buffer_ptr += imageInfo.rowPitch; + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + + if (total_matched == 0) + { + test_fail("Zero bytes matched"); + } + } + } + } + + return TEST_PASS; +} + +// clEnqueueCopyBufferToImage + +int test_enqueue_copy_buffer_to_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, + int num_elements) +{ + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + // Generate random data for opencl buffer + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = resolution.width * resolution.height + * pixelSize; // data is tightly packed in buffer + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t srcBytes = get_image_size(&imageInfo); + test_assert_error(srcBytes > 0, "Image cannot have zero size"); + + BufferOwningPtr srcData; + generate_random_image_data(&imageInfo, srcData, seed); + + clMemWrapper opencl_buffer = clCreateBuffer( + context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, srcBytes, + srcData, &err); + test_error(err, "Failed to create CL buffer"); + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_WRITE, nullptr, nullptr, + nullptr, &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + size_t origin[] = { 0, 0, 0 }; + size_t region[] = { imageInfo.width, imageInfo.height, 1 }; + + err = clEnqueueCopyBufferToImage(defaultQueue, opencl_buffer, + imported_image, 0, origin, + region, 0, nullptr, nullptr); + test_error( + err, "Failed to copy opencl buffer to imported AHB image"); + + clFinish(defaultQueue); + + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + char *out_image_ptr = + reinterpret_cast(hardware_buffer_data); + char *srcData_ptr = (char *)srcData; + + size_t scanlineSize = + imageInfo.width * get_pixel_size(imageInfo.format); + + // Count the number of bytes successfully matched + size_t total_matched = 0; + for (size_t line = 0; line < imageInfo.height; line++) + { + + if (memcmp(srcData_ptr, out_image_ptr, scanlineSize) != 0) + { + // Find the first differing pixel + size_t where = compare_scanlines( + &imageInfo, srcData_ptr, out_image_ptr); + if (where < imageInfo.width) + { + print_first_pixel_difference_error( + where, srcData_ptr + pixelSize * where, + out_image_ptr + pixelSize * where, &imageInfo, + line, 1); + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, + nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with " + "code %d\n", + ahb_result); + return TEST_FAIL; + } + return TEST_FAIL; + } + } + + total_matched += scanlineSize; + srcData_ptr += + scanlineSize; // image data is tightly packed in buffer + out_image_ptr += hardware_buffer_desc.stride * pixelSize; + } + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + + if (total_matched == 0) + { + test_fail("Zero bytes matched"); + } + } + } + } + + return TEST_PASS; +} + +// clEnqueueWriteImage + +int test_enqueue_write_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_ONLY, nullptr, nullptr, nullptr, + &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + // Generate data to write to image + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = resolution.width * resolution.height + * pixelSize; // Data is tightly packed + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t srcBytes = get_image_size(&imageInfo); + test_assert_error(srcBytes > 0, "Image cannot have zero size"); + + BufferOwningPtr srcData; + generate_random_image_data(&imageInfo, srcData, seed); + + size_t origin[] = { 0, 0, 0 }; + size_t region[] = { imageInfo.width, imageInfo.height, 1 }; + + err = clEnqueueWriteImage(defaultQueue, imported_image, CL_TRUE, + origin, region, 0, 0, srcData, 0, + nullptr, nullptr); + test_error(err, "Failed calling clEnqueueWriteImage"); + + clFinish(defaultQueue); + + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + char *out_image_ptr = + reinterpret_cast(hardware_buffer_data); + char *srcData_ptr = (char *)srcData; + + size_t scanlineSize = + imageInfo.width * get_pixel_size(imageInfo.format); + + // Count the number of bytes successfully matched + size_t total_matched = 0; + for (size_t line = 0; line < imageInfo.height; line++) + { + + if (memcmp(srcData_ptr, out_image_ptr, scanlineSize) != 0) + { + // Find the first differing pixel + size_t pixel_size = get_pixel_size(imageInfo.format); + size_t where = compare_scanlines( + &imageInfo, srcData_ptr, out_image_ptr); + if (where < imageInfo.width) + { + print_first_pixel_difference_error( + where, srcData_ptr + pixel_size * where, + out_image_ptr + pixel_size * where, &imageInfo, + line, 1); + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, + nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with " + "code %d\n", + ahb_result); + return TEST_FAIL; + } + return TEST_FAIL; + } + } + + total_matched += scanlineSize; + srcData_ptr += scanlineSize; // Data is tightly packed + out_image_ptr += hardware_buffer_desc.stride * pixelSize; + } + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + + if (total_matched == 0) + { + test_fail("Zero bytes matched"); + } + } + } + } + + return TEST_PASS; +} + +// clEnqueueFillImage + +int test_enqueue_fill_image(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + cl_int err; + RandomSeed seed(gRandomSeed); + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + for (auto format : test_formats) + { + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = format.aHardwareBufferFormat; + for (auto usage : test_usages) + { + // Filter out usage flags that are not readable on device + if (!isAHBUsageReadable(usage.usageFlags)) + { + continue; + } + + aHardwareBufferDesc.usage = usage.usageFlags; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width; + aHardwareBufferDesc.height = resolution.height; + aHardwareBufferDesc.layers = 1; + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = + ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s:\n Usage flags %s\n " + "Size (%u, %u, layers = %u)\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat), + usage_string, aHardwareBufferDesc.width, + aHardwareBufferDesc.height, + aHardwareBufferDesc.layers); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = AHardwareBuffer_allocate(&aHardwareBufferDesc, + &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info("Testing %s\n", + ahardwareBufferFormatToString( + format.aHardwareBufferFormat)); + + // Determine AHB memory layout + AHardwareBuffer_Desc hardware_buffer_desc = {}; + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + test_assert_error(hardware_buffer_desc.width + == resolution.width, + "AHB has unexpected width"); + test_assert_error(hardware_buffer_desc.height + == resolution.height, + "AHB has unexpected height"); + + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + clMemWrapper imported_image = clCreateImageWithProperties( + context, props, CL_MEM_READ_ONLY, nullptr, nullptr, nullptr, + &err); + test_error(err, + "Failed to create CL image from AHardwareBuffer"); + + // Create image info struct + size_t pixelSize = get_pixel_size(&format.clImageFormat); + image_descriptor imageInfo = { 0 }; + imageInfo.format = &format.clImageFormat; + imageInfo.type = format.clMemObjectType; + imageInfo.width = resolution.width; + imageInfo.height = resolution.height; + imageInfo.rowPitch = resolution.width * resolution.height + * pixelSize; // Data is tightly packed + test_assert_error(imageInfo.rowPitch + >= pixelSize * imageInfo.width, + "Row pitch is smaller than width"); + + size_t origin[] = { 0, 0, 0 }; + size_t region[] = { imageInfo.width, imageInfo.height, 1 }; + + char *verificationValue = + static_cast(malloc(pixelSize)); + if (!verificationValue) + { + log_error( + "Unable to malloc %zu bytes for verificationValue", + pixelSize); + return TEST_FAIL; + } + + // Generate pixel color and fill image + switch (format.clImageFormat.image_channel_data_type) + { + case CL_HALF_FLOAT: + DetectFloatToHalfRoundingMode( + defaultQueue); // Intentional drop-through + case CL_UNORM_INT8: { + auto pattern_decimal = + static_cast(genrand_real1(seed)); + cl_float fillColor[4] = { pattern_decimal, + pattern_decimal, + pattern_decimal, + pattern_decimal }; + + err = clEnqueueFillImage(defaultQueue, imported_image, + fillColor, origin, region, 0, + nullptr, nullptr); + test_error(err, "Failed calling clEnqueueFillImage"); + + pack_image_pixel(fillColor, &format.clImageFormat, + verificationValue); + break; + } + case CL_UNSIGNED_INT16: { + cl_uint pattern_whole = genrand_int32(seed); + cl_uint fillColor[4] = { pattern_whole, pattern_whole, + pattern_whole, pattern_whole }; + + err = clEnqueueFillImage(defaultQueue, imported_image, + fillColor, origin, region, 0, + nullptr, nullptr); + test_error(err, "Failed calling clEnqueueFillImage"); + + pack_image_pixel(fillColor, &format.clImageFormat, + verificationValue); + break; + } + default: + log_info("Unsupported image channel data type"); + continue; + } + + clFinish(defaultQueue); + AHardwareBuffer_describe(aHardwareBuffer, + &hardware_buffer_desc); + + void *hardware_buffer_data = nullptr; + ahb_result = AHardwareBuffer_lock( + aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, -1, + nullptr, &hardware_buffer_data); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_lock failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + + char *out_image_ptr = + reinterpret_cast(hardware_buffer_data); + size_t scanlineSize = imageInfo.width * pixelSize; + + + char *verificationLine = + static_cast(malloc(pixelSize * scanlineSize)); + if (!verificationLine) + { + free(verificationValue); + log_error("Unable to malloc %zu bytes for verificationLine", + pixelSize * scanlineSize); + return TEST_FAIL; + } + char *index = verificationLine; + for (size_t x = 0; x < imageInfo.width; x++) + { + memcpy(index, verificationValue, pixelSize); + index += pixelSize; + } + + free(verificationValue); + + // Count the number of bytes successfully matched + size_t total_matched = 0; + for (size_t line = 0; line < imageInfo.height; line++) + { + + if (memcmp(verificationLine, out_image_ptr, scanlineSize) + != 0) + { + // Find the first differing pixel + size_t pixel_size = get_pixel_size(imageInfo.format); + size_t where = compare_scanlines( + &imageInfo, verificationLine, out_image_ptr); + if (where < imageInfo.width) + { + print_first_pixel_difference_error( + where, verificationLine + pixel_size * where, + out_image_ptr + pixel_size * where, &imageInfo, + line, 1); + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, + nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with " + "code %d\n", + ahb_result); + free(verificationLine); + return TEST_FAIL; + } + free(verificationLine); + return TEST_FAIL; + } + } + + total_matched += scanlineSize; + out_image_ptr += hardware_buffer_desc.stride * pixelSize; + } + + ahb_result = AHardwareBuffer_unlock(aHardwareBuffer, nullptr); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_unlock failed with code %d\n", + ahb_result); + free(verificationLine); + return TEST_FAIL; + } + + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + free(verificationLine); + + if (total_matched == 0) + { + test_fail("Zero bytes matched"); + } + } + } + } + + return TEST_PASS; +} + + +// Confirm that a signal followed by a wait will complete successfully +int test_blob(cl_device_id deviceID, cl_context context, + cl_command_queue defaultQueue, int num_elements) +{ + cl_int err; + + if (!is_extension_available( + deviceID, "cl_khr_external_memory_android_hardware_buffer")) + { + log_info("cl_khr_external_memory_android_hardware_buffer is not " + "supported on this platform. " + "Skipping test.\n"); + return TEST_SKIPPED_ITSELF; + } + + AHardwareBuffer_Desc aHardwareBufferDesc = { 0 }; + aHardwareBufferDesc.format = AHARDWAREBUFFER_FORMAT_BLOB; + aHardwareBufferDesc.usage = AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER; + for (auto resolution : test_sizes) + { + aHardwareBufferDesc.width = resolution.width * resolution.height; + aHardwareBufferDesc.height = 1; + aHardwareBufferDesc.layers = 1; + aHardwareBufferDesc.usage = AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER; + + if (!AHardwareBuffer_isSupported(&aHardwareBufferDesc)) + { + char *usage_string = ahardwareBufferDecodeUsageFlagsToString( + static_cast( + aHardwareBufferDesc.usage)); + log_info("Unsupported format %s, usage flags %s\n", + ahardwareBufferFormatToString( + static_cast( + aHardwareBufferDesc.format)), + usage_string); + delete[] usage_string; + continue; + } + + AHardwareBuffer *aHardwareBuffer = nullptr; + int ahb_result = + AHardwareBuffer_allocate(&aHardwareBufferDesc, &aHardwareBuffer); + if (ahb_result != 0) + { + log_error("AHardwareBuffer_allocate failed with code %d\n", + ahb_result); + return TEST_FAIL; + } + log_info( + "Testing %s\n", + ahardwareBufferFormatToString(static_cast( + aHardwareBufferDesc.format))); + + cl_mem_properties props[] = { + CL_EXTERNAL_MEMORY_HANDLE_AHB_KHR, + reinterpret_cast(aHardwareBuffer), 0 + }; + + cl_mem buffer = clCreateBufferWithProperties( + context, props, CL_MEM_READ_WRITE, 0, nullptr, &err); + test_error(err, "Failed to create CL buffer from AHardwareBuffer"); + + test_error(clReleaseMemObject(buffer), "Failed to release buffer"); + AHardwareBuffer_release(aHardwareBuffer); + aHardwareBuffer = nullptr; + } + + return TEST_PASS; +}