xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/api/test_mem_objects.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2020 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "testBase.h"
17 
18 static volatile cl_int sDestructorIndex;
19 
mem_destructor_callback(cl_mem memObject,void * userData)20 void CL_CALLBACK mem_destructor_callback(cl_mem memObject, void *userData)
21 {
22     int *userPtr = (int *)userData;
23 
24     // ordering of callbacks is guaranteed, meaning we don't need to do atomic
25     // operation here
26     *userPtr = ++sDestructorIndex;
27 }
28 
test_mem_object_destructor_callback_single(clMemWrapper & memObject)29 int test_mem_object_destructor_callback_single(clMemWrapper &memObject)
30 {
31     cl_int error;
32 
33     // Set up some variables to catch the order in which callbacks are called
34     volatile int callbackOrders[3] = { 0, 0, 0 };
35     sDestructorIndex = 0;
36 
37     // Set up the callbacks
38     error = clSetMemObjectDestructorCallback(memObject, mem_destructor_callback,
39                                              (void *)&callbackOrders[0]);
40     test_error(error, "Unable to set destructor callback");
41 
42     error = clSetMemObjectDestructorCallback(memObject, mem_destructor_callback,
43                                              (void *)&callbackOrders[1]);
44     test_error(error, "Unable to set destructor callback");
45 
46     error = clSetMemObjectDestructorCallback(memObject, mem_destructor_callback,
47                                              (void *)&callbackOrders[2]);
48     test_error(error, "Unable to set destructor callback");
49 
50     // Now release the buffer, which SHOULD call the callbacks
51     memObject.reset();
52 
53     // At this point, all three callbacks should have already been called
54     int numErrors = 0;
55     for (int i = 0; i < 3; i++)
56     {
57         // Spin waiting for the release to finish.  If you don't call the
58         // mem_destructor_callback, you will not pass the test.  bugzilla 6316
59         log_info("\tWaiting for callback %d...\n", i);
60         int wait = 0;
61         while (0 == callbackOrders[i])
62         {
63             usleep(100000); // 1/10th second
64             if (++wait >= 10 * 10)
65             {
66                 log_error("\tERROR: Callback %d was not called within 10 "
67                           "seconds!  Assuming failure.\n",
68                           i + 1);
69                 numErrors++;
70                 break;
71             }
72         }
73 
74         if (callbackOrders[i] != 3 - i)
75         {
76             log_error("\tERROR: Callback %d was called in the wrong order! "
77                       "(Was called order %d, should have been order %d)\n",
78                       i + 1, callbackOrders[i], 3 - i);
79             numErrors++;
80         }
81     }
82 
83     return (numErrors > 0) ? TEST_FAIL : TEST_PASS;
84 }
85 
test_mem_object_destructor_callback(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)86 int test_mem_object_destructor_callback(cl_device_id deviceID,
87                                         cl_context context,
88                                         cl_command_queue queue,
89                                         int num_elements)
90 {
91     clMemWrapper testBuffer, testImage;
92     cl_int error;
93 
94 
95     // Create a buffer and an image to test callbacks against
96     testBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, &error);
97     test_error(error, "Unable to create testing buffer");
98 
99     if (test_mem_object_destructor_callback_single(testBuffer) != TEST_PASS)
100     {
101         log_error("ERROR: Destructor callbacks for buffer object FAILED\n");
102         return TEST_FAIL;
103     }
104 
105     if (checkForImageSupport(deviceID) == 0)
106     {
107         cl_image_format imageFormat = { CL_RGBA, CL_SIGNED_INT8 };
108         testImage = create_image_2d(context, CL_MEM_READ_ONLY, &imageFormat, 16,
109                                     16, 0, NULL, &error);
110         test_error(error, "Unable to create testing image");
111 
112         if (test_mem_object_destructor_callback_single(testImage) != TEST_PASS)
113         {
114             log_error("ERROR: Destructor callbacks for image object FAILED\n");
115             return TEST_FAIL;
116         }
117     }
118 
119     return TEST_PASS;
120 }
121