clover: Switch device objects to the new model.
[mesa.git] / src / gallium / state_trackers / clover / api / context.cpp
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/context.hpp"
25
26 using namespace clover;
27
28 PUBLIC cl_context
29 clCreateContext(const cl_context_properties *props, cl_uint num_devs,
30 const cl_device_id *d_devs,
31 void (CL_CALLBACK *pfn_notify)(const char *, const void *,
32 size_t, void *),
33 void *user_data, cl_int *errcode_ret) try {
34 auto devs = map(addresses(), objs(d_devs, num_devs));
35 auto mprops = property_map(props);
36
37 if (!pfn_notify && user_data)
38 throw error(CL_INVALID_VALUE);
39
40 for (auto p : mprops) {
41 if (p.first != CL_CONTEXT_PLATFORM)
42 throw error(CL_INVALID_PROPERTY);
43 }
44
45 ret_error(errcode_ret, CL_SUCCESS);
46 return new context(property_vector(mprops), devs);
47
48 } catch(error &e) {
49 ret_error(errcode_ret, e);
50 return NULL;
51 }
52
53 PUBLIC cl_context
54 clCreateContextFromType(const cl_context_properties *props,
55 cl_device_type type,
56 void (CL_CALLBACK *pfn_notify)(
57 const char *, const void *, size_t, void *),
58 void *user_data, cl_int *errcode_ret) try {
59 cl_platform_id platform;
60 cl_uint num_platforms;
61 cl_device_id dev;
62 cl_int ret;
63
64 ret = clGetPlatformIDs(1, &platform, &num_platforms);
65 if (ret || !num_platforms)
66 throw error(CL_INVALID_PLATFORM);
67
68 ret = clGetDeviceIDs(platform, type, 1, &dev, 0);
69 if (ret)
70 throw error(CL_DEVICE_NOT_FOUND);
71
72 return clCreateContext(props, 1, &dev, pfn_notify, user_data, errcode_ret);
73
74 } catch(error &e) {
75 ret_error(errcode_ret, e);
76 return NULL;
77 }
78
79 PUBLIC cl_int
80 clRetainContext(cl_context ctx) {
81 if (!ctx)
82 return CL_INVALID_CONTEXT;
83
84 ctx->retain();
85 return CL_SUCCESS;
86 }
87
88 PUBLIC cl_int
89 clReleaseContext(cl_context ctx) {
90 if (!ctx)
91 return CL_INVALID_CONTEXT;
92
93 if (ctx->release())
94 delete ctx;
95
96 return CL_SUCCESS;
97 }
98
99 PUBLIC cl_int
100 clGetContextInfo(cl_context ctx, cl_context_info param,
101 size_t size, void *r_buf, size_t *r_size) try {
102 property_buffer buf { r_buf, size, r_size };
103
104 if (!ctx)
105 return CL_INVALID_CONTEXT;
106
107 switch (param) {
108 case CL_CONTEXT_REFERENCE_COUNT:
109 buf.as_scalar<cl_uint>() = ctx->ref_count();
110 break;
111
112 case CL_CONTEXT_NUM_DEVICES:
113 buf.as_scalar<cl_uint>() = ctx->devs.size();
114 break;
115
116 case CL_CONTEXT_DEVICES:
117 buf.as_vector<cl_device_id>() = ctx->devs;
118 break;
119
120 case CL_CONTEXT_PROPERTIES:
121 buf.as_vector<cl_context_properties>() = ctx->props();
122 break;
123
124 default:
125 throw error(CL_INVALID_VALUE);
126 }
127
128 return CL_SUCCESS;
129
130 } catch (error &e) {
131 return e.get();
132 }