1//
2// Copyright 2012 Francisco Jerez
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 in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// 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 "api/util.hpp"
24#include "core/platform.hpp"
25#include "git_sha1.h"
26#include "util/u_debug.h"
27
28using namespace clover;
29
30namespace {
31   platform _clover_platform;
32}
33
34CLOVER_API cl_int
35clGetPlatformIDs(cl_uint num_entries, cl_platform_id *rd_platforms,
36                 cl_uint *rnum_platforms) {
37   if ((!num_entries && rd_platforms) ||
38       (!rnum_platforms && !rd_platforms))
39      return CL_INVALID_VALUE;
40
41   if (rnum_platforms)
42      *rnum_platforms = 1;
43   if (rd_platforms)
44      *rd_platforms = desc(_clover_platform);
45
46   return CL_SUCCESS;
47}
48
49cl_int
50clover::GetPlatformInfo(cl_platform_id d_platform, cl_platform_info param,
51                        size_t size, void *r_buf, size_t *r_size) try {
52   property_buffer buf { r_buf, size, r_size };
53
54   auto &platform = obj(d_platform);
55
56   switch (param) {
57   case CL_PLATFORM_PROFILE:
58      buf.as_string() = "FULL_PROFILE";
59      break;
60
61   case CL_PLATFORM_VERSION: {
62      static const std::string version_string =
63            debug_get_option("CLOVER_PLATFORM_VERSION_OVERRIDE", "1.1");
64
65      buf.as_string() = "OpenCL " + version_string + " Mesa " PACKAGE_VERSION MESA_GIT_SHA1;
66      break;
67   }
68   case CL_PLATFORM_NAME:
69      buf.as_string() = "Clover";
70      break;
71
72   case CL_PLATFORM_VENDOR:
73      buf.as_string() = "Mesa";
74      break;
75
76   case CL_PLATFORM_EXTENSIONS:
77      buf.as_string() = platform.supported_extensions();
78      break;
79
80   case CL_PLATFORM_ICD_SUFFIX_KHR:
81      buf.as_string() = "MESA";
82      break;
83
84   default:
85      throw error(CL_INVALID_VALUE);
86   }
87
88   return CL_SUCCESS;
89
90} catch (error &e) {
91   return e.get();
92}
93
94void *
95clover::GetExtensionFunctionAddressForPlatform(cl_platform_id d_platform,
96                                               const char *p_name) try {
97   obj(d_platform);
98   return GetExtensionFunctionAddress(p_name);
99
100} catch (error &e) {
101   return NULL;
102}
103
104void *
105clover::GetExtensionFunctionAddress(const char *p_name) {
106   std::string name { p_name };
107
108   if (name == "clIcdGetPlatformIDsKHR")
109      return reinterpret_cast<void *>(IcdGetPlatformIDsKHR);
110   else
111      return NULL;
112}
113
114cl_int
115clover::IcdGetPlatformIDsKHR(cl_uint num_entries, cl_platform_id *rd_platforms,
116                             cl_uint *rnum_platforms) {
117   return clGetPlatformIDs(num_entries, rd_platforms, rnum_platforms);
118}
119
120CLOVER_ICD_API cl_int
121clGetPlatformInfo(cl_platform_id d_platform, cl_platform_info param,
122                  size_t size, void *r_buf, size_t *r_size) {
123   return GetPlatformInfo(d_platform, param, size, r_buf, r_size);
124}
125
126CLOVER_ICD_API void *
127clGetExtensionFunctionAddress(const char *p_name) {
128   return GetExtensionFunctionAddress(p_name);
129}
130
131CLOVER_ICD_API void *
132clGetExtensionFunctionAddressForPlatform(cl_platform_id d_platform,
133                                         const char *p_name) {
134   return GetExtensionFunctionAddressForPlatform(d_platform, p_name);
135}
136
137CLOVER_ICD_API cl_int
138clIcdGetPlatformIDsKHR(cl_uint num_entries, cl_platform_id *rd_platforms,
139                       cl_uint *rnum_platforms) {
140   return IcdGetPlatformIDsKHR(num_entries, rd_platforms, rnum_platforms);
141}
142