mesa: add support for memory object creation/import/delete
[mesa.git] / src / mesa / main / externalobjects.c
1 /*
2 * Copyright © 2016 Red Hat.
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "macros.h"
25 #include "mtypes.h"
26 #include "externalobjects.h"
27
28 /**
29 * Allocate and initialize a new memory object. But don't put it into the
30 * memory object hash table.
31 *
32 * Called via ctx->Driver.NewMemoryObject, unless overridden by a device
33 * driver.
34 *
35 * \return pointer to new memory object.
36 */
37 static struct gl_memory_object *
38 _mesa_new_memory_object(struct gl_context *ctx, GLuint name)
39 {
40 struct gl_memory_object *obj = MALLOC_STRUCT(gl_memory_object);
41 if (!obj)
42 return NULL;
43
44 _mesa_initialize_memory_object(ctx, obj, name);
45 return obj;
46 }
47
48 /**
49 * Delete a memory object. Called via ctx->Driver.DeleteMemory().
50 * Not removed from hash table here.
51 */
52 void
53 _mesa_delete_memory_object(struct gl_context *ctx,
54 struct gl_memory_object *memObj)
55 {
56 free(memObj);
57 }
58
59 void
60 _mesa_init_memory_object_functions(struct dd_function_table *driver)
61 {
62 driver->NewMemoryObject = _mesa_new_memory_object;
63 driver->DeleteMemoryObject = _mesa_delete_memory_object;
64 }
65
66 /**
67 * Initialize a buffer object to default values.
68 */
69 void
70 _mesa_initialize_memory_object(struct gl_context *ctx,
71 struct gl_memory_object *obj,
72 GLuint name)
73 {
74 memset(obj, 0, sizeof(struct gl_memory_object));
75 obj->Name = name;
76 }
77
78 void GLAPIENTRY
79 _mesa_DeleteMemoryObjectsEXT(GLsizei n, const GLuint *memoryObjects)
80 {
81 GET_CURRENT_CONTEXT(ctx);
82
83 if (MESA_VERBOSE & (VERBOSE_API)) {
84 _mesa_debug(ctx, "glDeleteMemoryObjectsEXT(%d, %p)\n", n,
85 memoryObjects);
86 }
87
88 if (n < 0) {
89 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteMemoryObjectsEXT(n < 0)");
90 return;
91 }
92
93 if (!memoryObjects)
94 return;
95
96 _mesa_HashLockMutex(ctx->Shared->MemoryObjects);
97 for (GLint i = 0; i < n; i++) {
98 if (memoryObjects[i] > 0) {
99 struct gl_memory_object *delObj
100 = _mesa_lookup_memory_object_locked(ctx, memoryObjects[i]);
101
102 if (delObj) {
103 _mesa_HashRemoveLocked(ctx->Shared->MemoryObjects,
104 memoryObjects[i]);
105 ctx->Driver.DeleteMemoryObject(ctx, delObj);
106 }
107 }
108 }
109 _mesa_HashUnlockMutex(ctx->Shared->MemoryObjects);
110 }
111
112 GLboolean GLAPIENTRY
113 _mesa_IsMemoryObjectEXT(GLuint memoryObject)
114 {
115 GET_CURRENT_CONTEXT(ctx);
116 struct gl_memory_object *obj =
117 _mesa_lookup_memory_object(ctx, memoryObject);
118
119 return obj ? GL_TRUE : GL_FALSE;
120 }
121
122 void GLAPIENTRY
123 _mesa_CreateMemoryObjectsEXT(GLsizei n, GLuint *memoryObjects)
124 {
125 GET_CURRENT_CONTEXT(ctx);
126
127 const char *func = "glCreateMemoryObjectsEXT";
128
129 if (MESA_VERBOSE & (VERBOSE_API))
130 _mesa_debug(ctx, "%s(%d, %p)", func, n, memoryObjects);
131
132 if (n < 0) {
133 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
134 return;
135 }
136
137 if (!memoryObjects)
138 return;
139
140 _mesa_HashLockMutex(ctx->Shared->MemoryObjects);
141 GLuint first = _mesa_HashFindFreeKeyBlock(ctx->Shared->MemoryObjects, n);
142 if (first) {
143 for (GLsizei i = 0; i < n; i++) {
144 struct gl_memory_object *memObj;
145
146 memoryObjects[i] = first + i;
147
148 /* allocate memory object */
149 memObj = ctx->Driver.NewMemoryObject(ctx, memoryObjects[i]);
150 if (!memObj) {
151 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
152 _mesa_HashUnlockMutex(ctx->Shared->MemoryObjects);
153 return;
154 }
155
156 /* insert into hash table */
157 _mesa_HashInsertLocked(ctx->Shared->MemoryObjects,
158 memoryObjects[i],
159 memObj);
160 }
161 }
162
163 _mesa_HashUnlockMutex(ctx->Shared->MemoryObjects);
164 }
165
166 void GLAPIENTRY
167 _mesa_MemoryObjectParameterivEXT(GLuint memoryObject,
168 GLenum pname,
169 const GLint *params)
170 {
171
172 }
173
174 void GLAPIENTRY
175 _mesa_GetMemoryObjectParameterivEXT(GLuint memoryObject,
176 GLenum pname,
177 GLint *params)
178 {
179
180 }
181
182 void GLAPIENTRY
183 _mesa_TexStorageMem2DEXT(GLenum target,
184 GLsizei levels,
185 GLenum internalFormat,
186 GLsizei width,
187 GLsizei height,
188 GLuint memory,
189 GLuint64 offset)
190 {
191
192 }
193
194 void GLAPIENTRY
195 _mesa_TexStorageMem2DMultisampleEXT(GLenum target,
196 GLsizei samples,
197 GLenum internalFormat,
198 GLsizei width,
199 GLsizei height,
200 GLboolean fixedSampleLocations,
201 GLuint memory,
202 GLuint64 offset)
203 {
204
205 }
206
207 void GLAPIENTRY
208 _mesa_TexStorageMem3DEXT(GLenum target,
209 GLsizei levels,
210 GLenum internalFormat,
211 GLsizei width,
212 GLsizei height,
213 GLsizei depth,
214 GLuint memory,
215 GLuint64 offset)
216 {
217
218 }
219
220 void GLAPIENTRY
221 _mesa_TexStorageMem3DMultisampleEXT(GLenum target,
222 GLsizei samples,
223 GLenum internalFormat,
224 GLsizei width,
225 GLsizei height,
226 GLsizei depth,
227 GLboolean fixedSampleLocations,
228 GLuint memory,
229 GLuint64 offset)
230 {
231
232 }
233
234 void GLAPIENTRY
235 _mesa_TextureStorageMem2DEXT(GLuint texture,
236 GLsizei levels,
237 GLenum internalFormat,
238 GLsizei width,
239 GLsizei height,
240 GLuint memory,
241 GLuint64 offset)
242 {
243
244 }
245
246 void GLAPIENTRY
247 _mesa_TextureStorageMem2DMultisampleEXT(GLuint texture,
248 GLsizei samples,
249 GLenum internalFormat,
250 GLsizei width,
251 GLsizei height,
252 GLboolean fixedSampleLocations,
253 GLuint memory,
254 GLuint64 offset)
255 {
256
257 }
258
259 void GLAPIENTRY
260 _mesa_TextureStorageMem3DEXT(GLuint texture,
261 GLsizei levels,
262 GLenum internalFormat,
263 GLsizei width,
264 GLsizei height,
265 GLsizei depth,
266 GLuint memory,
267 GLuint64 offset)
268 {
269
270 }
271
272 void GLAPIENTRY
273 _mesa_TextureStorageMem3DMultisampleEXT(GLuint texture,
274 GLsizei samples,
275 GLenum internalFormat,
276 GLsizei width,
277 GLsizei height,
278 GLsizei depth,
279 GLboolean fixedSampleLocations,
280 GLuint memory,
281 GLuint64 offset)
282 {
283
284 }
285
286 void GLAPIENTRY
287 _mesa_TexStorageMem1DEXT(GLenum target,
288 GLsizei levels,
289 GLenum internalFormat,
290 GLsizei width,
291 GLuint memory,
292 GLuint64 offset)
293 {
294
295 }
296
297 void GLAPIENTRY
298 _mesa_TextureStorageMem1DEXT(GLuint texture,
299 GLsizei levels,
300 GLenum internalFormat,
301 GLsizei width,
302 GLuint memory,
303 GLuint64 offset)
304 {
305
306 }
307
308 void GLAPIENTRY
309 _mesa_GenSemaphoresEXT(GLsizei n, GLuint *semaphores)
310 {
311
312 }
313
314 void GLAPIENTRY
315 _mesa_DeleteSemaphoresEXT(GLsizei n, const GLuint *semaphores)
316 {
317
318 }
319
320 GLboolean GLAPIENTRY
321 _mesa_IsSemaphoreEXT(GLuint semaphore)
322 {
323 return GL_FALSE;
324 }
325
326 void GLAPIENTRY
327 _mesa_SemaphoreParameterui64vEXT(GLuint semaphore,
328 GLenum pname,
329 const GLuint64 *params)
330 {
331
332 }
333
334 void GLAPIENTRY
335 _mesa_GetSemaphoreParameterui64vEXT(GLuint semaphore,
336 GLenum pname,
337 GLuint64 *params)
338 {
339
340 }
341
342 void GLAPIENTRY
343 _mesa_WaitSemaphoreEXT(GLuint semaphore,
344 GLuint numBufferBarriers,
345 const GLuint *buffers,
346 GLuint numTextureBarriers,
347 const GLuint *textures,
348 const GLenum *srcLayouts)
349 {
350
351 }
352
353 void GLAPIENTRY
354 _mesa_SignalSemaphoreEXT(GLuint semaphore,
355 GLuint numBufferBarriers,
356 const GLuint *buffers,
357 GLuint numTextureBarriers,
358 const GLuint *textures,
359 const GLenum *dstLayouts)
360 {
361
362 }
363
364 void GLAPIENTRY
365 _mesa_ImportMemoryFdEXT(GLuint memory,
366 GLuint64 size,
367 GLenum handleType,
368 GLint fd)
369 {
370 GET_CURRENT_CONTEXT(ctx);
371
372 if (handleType != GL_HANDLE_TYPE_OPAQUE_FD_EXT) {
373 _mesa_error(ctx, GL_INVALID_VALUE, "glImportMemoryFdEXT(handleType=%u)",
374 handleType);
375 return;
376 }
377
378 struct gl_memory_object *memObj = _mesa_lookup_memory_object(ctx, memory);
379 if (!memObj)
380 return;
381
382 ctx->Driver.ImportMemoryObjectFd(ctx, memObj, size, fd);
383 }
384
385 void GLAPIENTRY
386 _mesa_ImportSemaphoreFdEXT(GLuint semaphore,
387 GLenum handleType,
388 GLint fd)
389 {
390
391 }