|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Intel Corporation |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included |
| 12 | + * in all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "runtime/mem_obj/image.h" |
| 24 | +#include "runtime/mem_obj/buffer.h" |
| 25 | +#include "runtime/helpers/aligned_memory.h" |
| 26 | +#include "unit_tests/fixtures/device_fixture.h" |
| 27 | +#include "unit_tests/mocks/mock_context.h" |
| 28 | +#include "test.h" |
| 29 | + |
| 30 | +#include <memory> |
| 31 | +using namespace OCLRT; |
| 32 | + |
| 33 | +// Tests for cl_khr_image2d_from_buffer |
| 34 | +class ImageFromSubBufferTest : public DeviceFixture, public ::testing::Test { |
| 35 | + public: |
| 36 | + ImageFromSubBufferTest() {} |
| 37 | + |
| 38 | + protected: |
| 39 | + void SetUp() override { |
| 40 | + imageFormat.image_channel_data_type = CL_UNORM_INT8; |
| 41 | + imageFormat.image_channel_order = CL_RGBA; |
| 42 | + |
| 43 | + imageDesc.image_array_size = 0; |
| 44 | + imageDesc.image_depth = 0; |
| 45 | + imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D; |
| 46 | + imageDesc.image_height = 128 / 2; |
| 47 | + imageDesc.image_width = 256 / 2; |
| 48 | + imageDesc.num_mip_levels = 0; |
| 49 | + imageDesc.image_row_pitch = 0; |
| 50 | + imageDesc.image_slice_pitch = 0; |
| 51 | + imageDesc.num_samples = 0; |
| 52 | + |
| 53 | + size = 128 * 256 * 4; |
| 54 | + hostPtr = alignedMalloc(size, 16); |
| 55 | + ASSERT_NE(nullptr, hostPtr); |
| 56 | + |
| 57 | + parentBuffer = clCreateBuffer(&context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, size, hostPtr, &retVal); |
| 58 | + ASSERT_EQ(CL_SUCCESS, retVal); |
| 59 | + |
| 60 | + const cl_buffer_region region = {size / 2, size / 2}; |
| 61 | + |
| 62 | + subBuffer = clCreateSubBuffer(parentBuffer, CL_MEM_READ_WRITE, CL_BUFFER_CREATE_TYPE_REGION, |
| 63 | + reinterpret_cast<const void *>(®ion), &retVal); |
| 64 | + ASSERT_EQ(CL_SUCCESS, retVal); |
| 65 | + |
| 66 | + imageDesc.mem_object = subBuffer; |
| 67 | + ASSERT_NE(nullptr, imageDesc.mem_object); |
| 68 | + } |
| 69 | + void TearDown() override { |
| 70 | + clReleaseMemObject(subBuffer); |
| 71 | + clReleaseMemObject(parentBuffer); |
| 72 | + alignedFree(hostPtr); |
| 73 | + } |
| 74 | + |
| 75 | + Image *createImage() { |
| 76 | + cl_mem_flags flags = CL_MEM_READ_ONLY; |
| 77 | + auto surfaceFormat = (SurfaceFormatInfo *)Image::getSurfaceFormatFromTable(flags, &imageFormat); |
| 78 | + return Image::create(&context, flags, surfaceFormat, &imageDesc, NULL, retVal); |
| 79 | + } |
| 80 | + cl_image_format imageFormat; |
| 81 | + cl_image_desc imageDesc; |
| 82 | + cl_int retVal = CL_SUCCESS; |
| 83 | + MockContext context; |
| 84 | + void *hostPtr; |
| 85 | + size_t size; |
| 86 | + cl_mem parentBuffer; |
| 87 | + cl_mem subBuffer; |
| 88 | +}; |
| 89 | + |
| 90 | +TEST_F(ImageFromSubBufferTest, CreateImage2dFromSubBufferWithOffset) { |
| 91 | + std::unique_ptr<Image> imageFromSubBuffer(createImage()); |
| 92 | + EXPECT_NE(nullptr, imageFromSubBuffer); |
| 93 | + |
| 94 | + SurfaceOffsets surfaceOffsets = {0}; |
| 95 | + imageFromSubBuffer->getSurfaceOffsets(surfaceOffsets); |
| 96 | + |
| 97 | + uint32_t offsetExpected = static_cast<uint32_t>(size) / 2; |
| 98 | + |
| 99 | + EXPECT_EQ(offsetExpected, surfaceOffsets.offset); |
| 100 | + EXPECT_EQ(0u, surfaceOffsets.xOffset); |
| 101 | + EXPECT_EQ(0u, surfaceOffsets.yOffset); |
| 102 | + EXPECT_EQ(0u, surfaceOffsets.yOffsetForUVplane); |
| 103 | +} |
0 commit comments