xref: /aosp_15_r20/external/OpenCL-ICD-Loader/test/layer/icd_print_layer.c (revision 1cddb830dba8aa7c1cc1039338e56b3b9fa24952)
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  * OpenCL is a trademark of Apple Inc. used under license by Khronos.
17  */
18 
19 #include "icd_print_layer.h"
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 struct _cl_icd_dispatch dispatch;
25 
26 const struct _cl_icd_dispatch *tdispatch;
27 
28 static cl_layer_api_version api_version = CL_LAYER_API_VERSION_100;
29 static const char name[] = "print_layer";
30 
31 static inline cl_int
set_param_value(size_t param_value_size,void * param_value,size_t * param_value_size_ret,size_t src_size,const void * src)32 set_param_value(
33     size_t      param_value_size,
34     void       *param_value,
35     size_t     *param_value_size_ret,
36     size_t      src_size,
37     const void *src) {
38   if (param_value && param_value_size < src_size)
39     return CL_INVALID_VALUE;
40   if (param_value)
41     memcpy(param_value, src, src_size);
42   if (param_value_size_ret)
43     *param_value_size_ret = src_size;
44   return CL_SUCCESS;
45 }
46 
47 CL_API_ENTRY cl_int CL_API_CALL
clGetLayerInfo(cl_layer_info param_name,size_t param_value_size,void * param_value,size_t * param_value_size_ret)48 clGetLayerInfo(
49     cl_layer_info  param_name,
50     size_t         param_value_size,
51     void          *param_value,
52     size_t        *param_value_size_ret) {
53   size_t sz = 0;
54   const void *src = NULL;
55   if (param_value_size && !param_value)
56     return CL_INVALID_VALUE;
57   if (!param_value && !param_value_size_ret)
58     return CL_INVALID_VALUE;
59   switch (param_name) {
60   case CL_LAYER_API_VERSION:
61     sz = sizeof(api_version);
62     src = &api_version;
63     break;
64   case CL_LAYER_NAME:
65     sz = sizeof(name);
66     src = name;
67     break;
68   default:
69     return CL_INVALID_VALUE;
70   }
71   return set_param_value(param_value_size, param_value, param_value_size_ret, sz, src);
72 }
73 
74 CL_API_ENTRY cl_int CL_API_CALL
clInitLayer(cl_uint num_entries,const struct _cl_icd_dispatch * target_dispatch,cl_uint * num_entries_out,const struct _cl_icd_dispatch ** layer_dispatch_ret)75 clInitLayer(
76     cl_uint                         num_entries,
77     const struct _cl_icd_dispatch  *target_dispatch,
78     cl_uint                        *num_entries_out,
79     const struct _cl_icd_dispatch **layer_dispatch_ret) {
80   if (!target_dispatch || !layer_dispatch_ret || !num_entries_out || num_entries < sizeof(dispatch)/sizeof(dispatch.clGetPlatformIDs))
81     return CL_INVALID_VALUE;
82 
83   _init_dispatch();
84 
85   tdispatch = target_dispatch;
86   *layer_dispatch_ret = &dispatch;
87   *num_entries_out = sizeof(dispatch)/sizeof(dispatch.clGetPlatformIDs);
88 
89   return CL_SUCCESS;
90 }
91 
92 
93