Path: blob/21.2-virgl/src/gallium/frontends/clover/api/context.cpp
4572 views
//1// Copyright 2012 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#include "api/util.hpp"23#include "core/context.hpp"24#include "core/platform.hpp"2526using namespace clover;2728CLOVER_API cl_context29clCreateContext(const cl_context_properties *d_props, cl_uint num_devs,30const cl_device_id *d_devs,31void (CL_CALLBACK *pfn_notify)(const char *, const void *,32size_t, void *),33void *user_data, cl_int *r_errcode) try {34auto props = obj<property_list_tag>(d_props);35auto devs = objs(d_devs, num_devs);3637if (!pfn_notify && user_data)38throw error(CL_INVALID_VALUE);3940for (auto &prop : props) {41if (prop.first == CL_CONTEXT_PLATFORM)42obj(prop.second.as<cl_platform_id>());43else44throw error(CL_INVALID_PROPERTY);45}4647const auto notify = (!pfn_notify ? context::notify_action() :48[=](const char *s) {49pfn_notify(s, NULL, 0, user_data);50});5152ret_error(r_errcode, CL_SUCCESS);53return desc(new context(props, devs, notify));5455} catch (error &e) {56ret_error(r_errcode, e);57return NULL;58}5960CLOVER_API cl_context61clCreateContextFromType(const cl_context_properties *d_props,62cl_device_type type,63void (CL_CALLBACK *pfn_notify)(64const char *, const void *, size_t, void *),65void *user_data, cl_int *r_errcode) try {66cl_platform_id d_platform;67cl_uint num_platforms;68cl_int ret;69std::vector<cl_device_id> devs;70cl_uint num_devices;7172ret = clGetPlatformIDs(1, &d_platform, &num_platforms);73if (ret || !num_platforms)74throw error(CL_INVALID_PLATFORM);7576ret = clGetDeviceIDs(d_platform, type, 0, NULL, &num_devices);77if (ret)78throw error(CL_DEVICE_NOT_FOUND);79devs.resize(num_devices);80ret = clGetDeviceIDs(d_platform, type, num_devices, devs.data(), 0);81if (ret)82throw error(CL_DEVICE_NOT_FOUND);8384return clCreateContext(d_props, num_devices, devs.data(), pfn_notify,85user_data, r_errcode);8687} catch (error &e) {88ret_error(r_errcode, e);89return NULL;90}9192CLOVER_API cl_int93clRetainContext(cl_context d_ctx) try {94obj(d_ctx).retain();95return CL_SUCCESS;9697} catch (error &e) {98return e.get();99}100101CLOVER_API cl_int102clReleaseContext(cl_context d_ctx) try {103if (obj(d_ctx).release())104delete pobj(d_ctx);105106return CL_SUCCESS;107108} catch (error &e) {109return e.get();110}111112CLOVER_API cl_int113clGetContextInfo(cl_context d_ctx, cl_context_info param,114size_t size, void *r_buf, size_t *r_size) try {115property_buffer buf { r_buf, size, r_size };116auto &ctx = obj(d_ctx);117118switch (param) {119case CL_CONTEXT_REFERENCE_COUNT:120buf.as_scalar<cl_uint>() = ctx.ref_count();121break;122123case CL_CONTEXT_NUM_DEVICES:124buf.as_scalar<cl_uint>() = ctx.devices().size();125break;126127case CL_CONTEXT_DEVICES:128buf.as_vector<cl_device_id>() = descs(ctx.devices());129break;130131case CL_CONTEXT_PROPERTIES:132buf.as_vector<cl_context_properties>() = desc(ctx.properties());133break;134135default:136throw error(CL_INVALID_VALUE);137}138139return CL_SUCCESS;140141} catch (error &e) {142return e.get();143}144145CLOVER_API cl_int146clSetContextDestructorCallback(cl_context d_ctx,147void (CL_CALLBACK *pfn_notify)(cl_context, void *),148void *user_data) try {149CLOVER_NOT_SUPPORTED_UNTIL("3.0");150auto &ctx = obj(d_ctx);151152if (!pfn_notify)153return CL_INVALID_VALUE;154155ctx.destroy_notify([=]{ pfn_notify(d_ctx, user_data); });156157return CL_SUCCESS;158159} catch (error &e) {160return e.get();161}162163164