Skip to content

Commit da95369

Browse files
Refactor imagecopy and imagereadwrite tests (#2362)
Refactor the following tests: 1. `test_imagecopy` 2. `test_imagecopy3d` 3. `test_imagereadwrite` 4. `test_imagereadwrite3d` The change does the following: 1. Use RAII to manage allocated resources 2. For `imagecopy` and `imagecopy3d`, the change allows for a custom src image memory flags and adjusts how the source image is created according to the input flags. Signed-off-by: Michael Rizkalla <michael.rizkalla@arm.com>
1 parent 9fead88 commit da95369

4 files changed

Lines changed: 460 additions & 407 deletions

File tree

Lines changed: 135 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Copyright (c) 2017 The Khronos Group Inc.
3-
//
3+
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
66
// You may obtain a copy of the License at
@@ -20,217 +20,229 @@
2020
#include <string.h>
2121
#include <sys/types.h>
2222
#include <sys/stat.h>
23+
#include <memory>
2324

2425
#include "testBase.h"
2526

26-
static unsigned char *
27-
generate_rgba8_image(int w, int h, MTdata d)
27+
static std::unique_ptr<unsigned char[]> generate_rgba8_image(int w, int h,
28+
MTdata d)
2829
{
29-
unsigned char *ptr = (unsigned char*)malloc(w * h * 4);
30-
int i;
30+
std::unique_ptr<unsigned char[]> ptr{ new unsigned char[w * h * 4] };
3131

32-
for (i=0; i<w*h*4; i++)
32+
for (int i = 0; i < w * h * 4; i++)
3333
ptr[i] = (unsigned char)genrand_int32(d);
3434

3535
return ptr;
3636
}
3737

38-
static int
39-
verify_rgba8_image(unsigned char *image, unsigned char *outptr, int w, int h)
38+
static int verify_rgba8_image(const unsigned char *image,
39+
const unsigned char *outptr, int w, int h)
4040
{
41-
int i;
41+
int i;
4242

43-
for (i=0; i<w*h*4; i++)
43+
for (i = 0; i < w * h * 4; i++)
4444
{
45-
if (outptr[i] != image[i])
46-
return -1;
45+
if (outptr[i] != image[i]) return -1;
4746
}
4847

4948
return 0;
5049
}
5150

5251

53-
static unsigned short *
54-
generate_rgba16_image(int w, int h, MTdata d)
52+
static std::unique_ptr<unsigned short[]> generate_rgba16_image(int w, int h,
53+
MTdata d)
5554
{
56-
unsigned short *ptr = (unsigned short *)malloc(w * h * 4 * sizeof(unsigned short));
57-
int i;
55+
std::unique_ptr<unsigned short[]> ptr{ new unsigned short[w * h * 4] };
5856

59-
for (i=0; i<w*h*4; i++)
57+
for (int i = 0; i < w * h * 4; i++)
6058
ptr[i] = (unsigned short)genrand_int32(d);
6159

6260
return ptr;
6361
}
6462

65-
static int
66-
verify_rgba16_image(unsigned short *image, unsigned short *outptr, int w, int h)
63+
static int verify_rgba16_image(const unsigned short *image,
64+
const unsigned short *outptr, int w, int h)
6765
{
68-
int i;
66+
int i;
6967

70-
for (i=0; i<w*h*4; i++)
68+
for (i = 0; i < w * h * 4; i++)
7169
{
72-
if (outptr[i] != image[i])
73-
return -1;
70+
if (outptr[i] != image[i]) return -1;
7471
}
7572

7673
return 0;
7774
}
7875

7976

80-
static float *
81-
generate_rgbafp_image(int w, int h, MTdata d)
77+
static std::unique_ptr<float[]> generate_rgbafp_image(int w, int h, MTdata d)
8278
{
83-
float *ptr = (float*)malloc(w * h * 4 * sizeof(float));
84-
int i;
79+
std::unique_ptr<float[]> ptr{ new float[w * h * 4] };
8580

86-
for (i=0; i<w*h*4; i++)
81+
for (int i = 0; i < w * h * 4; i++)
8782
ptr[i] = get_random_float(-0x40000000, 0x40000000, d);
8883

8984
return ptr;
9085
}
9186

92-
static int
93-
verify_rgbafp_image(float *image, float *outptr, int w, int h)
87+
static int verify_rgbafp_image(const float *image, const float *outptr, int w,
88+
int h)
9489
{
95-
int i;
90+
int i;
9691

97-
for (i=0; i<w*h*4; i++)
92+
for (i = 0; i < w * h * 4; i++)
9893
{
99-
if (outptr[i] != image[i])
100-
return -1;
94+
if (outptr[i] != image[i]) return -1;
10195
}
10296

10397
return 0;
10498
}
10599

100+
static constexpr cl_image_format image_formats[] = { { CL_RGBA, CL_UNORM_INT8 },
101+
{ CL_RGBA,
102+
CL_UNORM_INT16 },
103+
{ CL_RGBA, CL_FLOAT } };
106104

107-
REGISTER_TEST(imagecopy)
105+
static int test_imagecopy_impl(cl_device_id device, cl_context context,
106+
cl_command_queue queue, int num_elements,
107+
cl_mem_flags src_image_flags)
108108
{
109-
cl_image_format img_format;
110-
unsigned char *rgba8_inptr, *rgba8_outptr;
111-
unsigned short *rgba16_inptr, *rgba16_outptr;
112-
float *rgbafp_inptr, *rgbafp_outptr;
113-
clMemWrapper streams[6];
114-
int img_width = 512;
115-
int img_height = 512;
116-
int i, err;
117-
MTdata d;
118-
119-
PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
120-
121-
d = init_genrand( gRandomSeed );
122-
rgba8_inptr = (unsigned char *)generate_rgba8_image(img_width, img_height, d);
123-
rgba16_inptr = (unsigned short *)generate_rgba16_image(img_width, img_height, d);
124-
rgbafp_inptr = (float *)generate_rgbafp_image(img_width, img_height, d);
125-
free_mtdata(d); d = NULL;
126-
127-
rgba8_outptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * img_width * img_height);
128-
rgba16_outptr = (unsigned short*)malloc(sizeof(unsigned short) * 4 * img_width * img_height);
129-
rgbafp_outptr = (float*)malloc(sizeof(float) * 4 * img_width * img_height);
130-
131-
img_format.image_channel_order = CL_RGBA;
132-
img_format.image_channel_data_type = CL_UNORM_INT8;
133-
streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
134-
img_width, img_height, 0, NULL, &err);
135-
test_error(err, "create_image_2d failed");
136-
streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
137-
img_width, img_height, 0, NULL, &err);
138-
test_error(err, "create_image_2d failed");
139-
140-
img_format.image_channel_order = CL_RGBA;
141-
img_format.image_channel_data_type = CL_UNORM_INT16;
142-
streams[2] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
143-
img_width, img_height, 0, NULL, &err);
144-
test_error(err, "create_image_2d failed");
145-
streams[3] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
146-
img_width, img_height, 0, NULL, &err);
147-
test_error(err, "create_image_2d failed");
148-
149-
img_format.image_channel_order = CL_RGBA;
150-
img_format.image_channel_data_type = CL_FLOAT;
151-
streams[4] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
152-
img_width, img_height, 0, NULL, &err);
153-
test_error(err, "create_image_2d failed");
154-
streams[5] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
155-
img_width, img_height, 0, NULL, &err);
156-
test_error(err, "create_image_2d failed");
157-
158-
for (i=0; i<3; i++)
109+
constexpr size_t image_formats_count = ARRAY_SIZE(image_formats);
110+
std::unique_ptr<unsigned char[]> rgba8_inptr, rgba8_outptr;
111+
std::unique_ptr<unsigned short[]> rgba16_inptr, rgba16_outptr;
112+
std::unique_ptr<float[]> rgbafp_inptr, rgbafp_outptr;
113+
clMemWrapper streams[6];
114+
int img_width = 512;
115+
int img_height = 512;
116+
int i, err;
117+
MTdataHolder d(gRandomSeed);
118+
119+
rgba8_inptr = generate_rgba8_image(img_width, img_height, d);
120+
rgba16_inptr = generate_rgba16_image(img_width, img_height, d);
121+
rgbafp_inptr = generate_rgbafp_image(img_width, img_height, d);
122+
123+
rgba8_outptr.reset(new unsigned char[4 * img_width * img_height]);
124+
rgba16_outptr.reset(new unsigned short[4 * img_width * img_height]);
125+
rgbafp_outptr.reset(new float[4 * img_width * img_height]);
126+
127+
for (size_t index = 0; index < image_formats_count; ++index)
159128
{
160-
void *p, *outp;
161-
int x, y, delta_w = img_width/8, delta_h = img_height/16;
129+
void *ptr = nullptr;
130+
if (src_image_flags & CL_MEM_USE_HOST_PTR
131+
|| src_image_flags & CL_MEM_COPY_HOST_PTR)
132+
133+
{
134+
switch (index)
135+
{
136+
case 0: ptr = rgba8_inptr.get(); break;
137+
case 1: ptr = rgba16_inptr.get(); break;
138+
case 2: ptr = rgbafp_inptr.get(); break;
139+
default: break;
140+
}
141+
}
142+
streams[index * 2] =
143+
create_image_2d(context, src_image_flags, &image_formats[index],
144+
img_width, img_height, 0, ptr, &err);
145+
test_error(err, "create_image_2d failed");
146+
147+
streams[index * 2 + 1] =
148+
create_image_2d(context, CL_MEM_READ_WRITE, &image_formats[index],
149+
img_width, img_height, 0, nullptr, &err);
150+
test_error(err, "create_image_2d failed");
151+
}
152+
153+
for (i = 0; i < 3; i++)
154+
{
155+
void *p, *outp;
156+
int x, y, delta_w = img_width / 8, delta_h = img_height / 16;
162157

163158
switch (i)
164159
{
165160
case 0:
166-
p = (void *)rgba8_inptr;
167-
outp = (void *)rgba8_outptr;
168-
log_info("Testing CL_RGBA CL_UNORM_INT8\n");
161+
p = rgba8_inptr.get();
162+
outp = rgba8_outptr.get();
163+
log_info("Testing CL_RGBA CL_UNORM_INT8\n");
169164
break;
170165
case 1:
171-
p = (void *)rgba16_inptr;
172-
outp = (void *)rgba16_outptr;
173-
log_info("Testing CL_RGBA CL_UNORM_INT16\n");
166+
p = rgba16_inptr.get();
167+
outp = rgba16_outptr.get();
168+
log_info("Testing CL_RGBA CL_UNORM_INT16\n");
174169
break;
175170
case 2:
176-
p = (void *)rgbafp_inptr;
177-
outp = (void *)rgbafp_outptr;
178-
log_info("Testing CL_RGBA CL_FLOAT\n");
171+
p = rgbafp_inptr.get();
172+
outp = rgbafp_outptr.get();
173+
log_info("Testing CL_RGBA CL_FLOAT\n");
179174
break;
180175
}
181176

182-
size_t origin[3] = {0,0,0}, region[3] = {img_width, img_height, 1};
183-
err = clEnqueueWriteImage(queue, streams[i*2], CL_TRUE, origin, region, 0, 0, p, 0, NULL, NULL);
184-
test_error(err, "create_image_2d failed");
177+
size_t origin[3] = { 0, 0, 0 },
178+
region[3] = { img_width, img_height, 1 };
179+
if (!(src_image_flags & CL_MEM_USE_HOST_PTR
180+
|| src_image_flags & CL_MEM_COPY_HOST_PTR))
181+
{
182+
err = clEnqueueWriteImage(queue, streams[i * 2], CL_TRUE, origin,
183+
region, 0, 0, p, 0, nullptr, nullptr);
184+
test_error(err, "create_image_2d failed");
185+
}
185186

186187
int copy_number = 0;
187-
for (y=0; y<img_height; y+=delta_h)
188+
for (y = 0; y < img_height; y += delta_h)
188189
{
189-
for (x=0; x<img_width; x+=delta_w)
190+
for (x = 0; x < img_width; x += delta_w)
190191
{
191-
copy_number++;
192-
size_t copy_origin[3] = {x,y,0}, copy_region[3]={delta_w, delta_h, 1};
193-
err = clEnqueueCopyImage(queue, streams[i*2], streams[i*2+1],
194-
copy_origin, copy_origin, copy_region,
195-
0, NULL, NULL);
196-
if (err) {
197-
log_error("Copy %d (origin [%d, %d], size [%d, %d], image size [%d x %d]) Failed\n", copy_number, x, y, delta_w, delta_h, img_width, img_height);
198-
}
199-
test_error(err, "clEnqueueCopyImage failed");
192+
copy_number++;
193+
size_t copy_origin[3] = { x, y, 0 },
194+
copy_region[3] = { delta_w, delta_h, 1 };
195+
err = clEnqueueCopyImage(
196+
queue, streams[i * 2], streams[i * 2 + 1], copy_origin,
197+
copy_origin, copy_region, 0, NULL, NULL);
198+
if (err)
199+
{
200+
log_error("Copy %d (origin [%d, %d], size [%d, %d], image "
201+
"size [%d x %d]) Failed\n",
202+
copy_number, x, y, delta_w, delta_h, img_width,
203+
img_height);
204+
}
205+
test_error(err, "clEnqueueCopyImage failed");
200206
}
201207
}
202208

203-
err = clEnqueueReadImage(queue, streams[i*2+1], CL_TRUE, origin, region, 0, 0, outp, 0, NULL, NULL);
209+
err = clEnqueueReadImage(queue, streams[i * 2 + 1], CL_TRUE, origin,
210+
region, 0, 0, outp, 0, NULL, NULL);
204211
test_error(err, "clEnqueueReadImage failed");
205212

206213
switch (i)
207214
{
208215
case 0:
209-
err = verify_rgba8_image(rgba8_inptr, rgba8_outptr, img_width, img_height);
216+
err = verify_rgba8_image(rgba8_inptr.get(), rgba8_outptr.get(),
217+
img_width, img_height);
210218
break;
211219
case 1:
212-
err = verify_rgba16_image(rgba16_inptr, rgba16_outptr, img_width, img_height);
220+
err =
221+
verify_rgba16_image(rgba16_inptr.get(), rgba16_outptr.get(),
222+
img_width, img_height);
213223
break;
214224
case 2:
215-
err = verify_rgbafp_image(rgbafp_inptr, rgbafp_outptr, img_width, img_height);
225+
err =
226+
verify_rgbafp_image(rgbafp_inptr.get(), rgbafp_outptr.get(),
227+
img_width, img_height);
216228
break;
217229
}
218230

219-
if (err)
220-
break;
231+
if (err) break;
221232
}
222233

223-
free(rgba8_inptr);
224-
free(rgba16_inptr);
225-
free(rgbafp_inptr);
226-
free(rgba8_outptr);
227-
free(rgba16_outptr);
228-
free(rgbafp_outptr);
229-
230234
if (err)
231235
log_error("IMAGE copy test failed\n");
232236
else
233237
log_info("IMAGE copy test passed\n");
234238

235239
return err;
236240
}
241+
242+
REGISTER_TEST(imagecopy)
243+
{
244+
PASSIVE_REQUIRE_IMAGE_SUPPORT(device);
245+
246+
return test_imagecopy_impl(device, context, queue, num_elements,
247+
CL_MEM_READ_WRITE);
248+
}

0 commit comments

Comments
 (0)