1 /*
2 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR 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 <dlfcn.h>
24 #include "drm-uapi/drm_fourcc.h"
25 #include "util/u_memory.h"
26 #include "pipe/p_screen.h"
27 #include "state_tracker/st_texture.h"
28 #include "state_tracker/st_context.h"
29 #include "main/texobj.h"
30 #include "util/libsync.h"
31
32 #include "dri_helpers.h"
33 #include "loader_dri_helper.h"
34
35 static bool
dri2_is_opencl_interop_loaded_locked(struct dri_screen * screen)36 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
37 {
38 return screen->opencl_dri_event_add_ref &&
39 screen->opencl_dri_event_release &&
40 screen->opencl_dri_event_wait &&
41 screen->opencl_dri_event_get_fence;
42 }
43
44 static bool
dri2_load_opencl_interop(struct dri_screen * screen)45 dri2_load_opencl_interop(struct dri_screen *screen)
46 {
47 #if defined(RTLD_DEFAULT)
48 bool success;
49
50 mtx_lock(&screen->opencl_func_mutex);
51
52 if (dri2_is_opencl_interop_loaded_locked(screen)) {
53 mtx_unlock(&screen->opencl_func_mutex);
54 return true;
55 }
56
57 screen->opencl_dri_event_add_ref =
58 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
59 screen->opencl_dri_event_release =
60 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
61 screen->opencl_dri_event_wait =
62 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
63 screen->opencl_dri_event_get_fence =
64 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
65
66 success = dri2_is_opencl_interop_loaded_locked(screen);
67 mtx_unlock(&screen->opencl_func_mutex);
68 return success;
69 #else
70 return false;
71 #endif
72 }
73
74 struct dri2_fence {
75 struct dri_screen *driscreen;
76 struct pipe_fence_handle *pipe_fence;
77 void *cl_event;
78 };
79
80 unsigned
dri_fence_get_caps(struct dri_screen * driscreen)81 dri_fence_get_caps(struct dri_screen *driscreen)
82 {
83 struct pipe_screen *screen = driscreen->base.screen;
84 unsigned caps = 0;
85
86 if (screen->caps.native_fence_fd)
87 caps |= __DRI_FENCE_CAP_NATIVE_FD;
88
89 return caps;
90 }
91
92 void *
dri_create_fence(struct dri_context * ctx)93 dri_create_fence(struct dri_context *ctx)
94 {
95 struct st_context *st = ctx->st;
96 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
97
98 if (!fence)
99 return NULL;
100
101 /* Wait for glthread to finish because we can't use pipe_context from
102 * multiple threads.
103 */
104 _mesa_glthread_finish(st->ctx);
105
106 st_context_flush(st, 0, &fence->pipe_fence, NULL, NULL);
107
108 if (!fence->pipe_fence) {
109 FREE(fence);
110 return NULL;
111 }
112
113 fence->driscreen = ctx->screen;
114 return fence;
115 }
116
117 void *
dri_create_fence_fd(struct dri_context * dri_ctx,int fd)118 dri_create_fence_fd(struct dri_context *dri_ctx, int fd)
119 {
120 struct st_context *st = dri_ctx->st;
121 struct pipe_context *ctx = st->pipe;
122 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
123
124 /* Wait for glthread to finish because we can't use pipe_context from
125 * multiple threads.
126 */
127 _mesa_glthread_finish(st->ctx);
128
129 if (fd == -1) {
130 /* exporting driver created fence, flush: */
131 st_context_flush(st, ST_FLUSH_FENCE_FD, &fence->pipe_fence, NULL, NULL);
132 } else {
133 /* importing a foreign fence fd: */
134 ctx->create_fence_fd(ctx, &fence->pipe_fence, fd, PIPE_FD_TYPE_NATIVE_SYNC);
135 }
136 if (!fence->pipe_fence) {
137 FREE(fence);
138 return NULL;
139 }
140
141 fence->driscreen = dri_ctx->screen;
142 return fence;
143 }
144
145 int
dri_get_fence_fd(struct dri_screen * driscreen,void * _fence)146 dri_get_fence_fd(struct dri_screen *driscreen, void *_fence)
147 {
148 struct pipe_screen *screen = driscreen->base.screen;
149 struct dri2_fence *fence = (struct dri2_fence*)_fence;
150
151 return screen->fence_get_fd(screen, fence->pipe_fence);
152 }
153
154 void *
dri_get_fence_from_cl_event(struct dri_screen * driscreen,intptr_t cl_event)155 dri_get_fence_from_cl_event(struct dri_screen *driscreen, intptr_t cl_event)
156 {
157 struct dri2_fence *fence;
158
159 if (!dri2_load_opencl_interop(driscreen))
160 return NULL;
161
162 fence = CALLOC_STRUCT(dri2_fence);
163 if (!fence)
164 return NULL;
165
166 fence->cl_event = (void*)cl_event;
167
168 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
169 free(fence);
170 return NULL;
171 }
172
173 fence->driscreen = driscreen;
174 return fence;
175 }
176
177 void
dri_destroy_fence(struct dri_screen * driscreen,void * _fence)178 dri_destroy_fence(struct dri_screen *driscreen, void *_fence)
179 {
180 struct pipe_screen *screen = driscreen->base.screen;
181 struct dri2_fence *fence = (struct dri2_fence*)_fence;
182
183 if (fence->pipe_fence)
184 screen->fence_reference(screen, &fence->pipe_fence, NULL);
185 else if (fence->cl_event)
186 driscreen->opencl_dri_event_release(fence->cl_event);
187 else
188 assert(0);
189
190 FREE(fence);
191 }
192
193 GLboolean
dri_client_wait_sync(struct dri_context * _ctx,void * _fence,unsigned flags,uint64_t timeout)194 dri_client_wait_sync(struct dri_context *_ctx, void *_fence, unsigned flags,
195 uint64_t timeout)
196 {
197 struct dri2_fence *fence = (struct dri2_fence*)_fence;
198 struct dri_screen *driscreen = fence->driscreen;
199 struct pipe_screen *screen = driscreen->base.screen;
200
201 /* No need to flush. The context was flushed when the fence was created. */
202
203 if (fence->pipe_fence)
204 return screen->fence_finish(screen, NULL, fence->pipe_fence, timeout);
205 else if (fence->cl_event) {
206 struct pipe_fence_handle *pipe_fence =
207 driscreen->opencl_dri_event_get_fence(fence->cl_event);
208
209 if (pipe_fence)
210 return screen->fence_finish(screen, NULL, pipe_fence, timeout);
211 else
212 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
213 }
214 else {
215 assert(0);
216 return false;
217 }
218 }
219
220 void
dri_server_wait_sync(struct dri_context * _ctx,void * _fence,unsigned flags)221 dri_server_wait_sync(struct dri_context *_ctx, void *_fence, unsigned flags)
222 {
223 struct st_context *st = _ctx->st;
224 struct pipe_context *ctx = st->pipe;
225 struct dri2_fence *fence = (struct dri2_fence*)_fence;
226
227 /* We might be called here with a NULL fence as a result of WaitSyncKHR
228 * on a EGL_KHR_reusable_sync fence. Nothing to do here in such case.
229 */
230 if (!fence)
231 return;
232
233 /* Wait for glthread to finish because we can't use pipe_context from
234 * multiple threads.
235 */
236 _mesa_glthread_finish(st->ctx);
237
238 if (ctx->fence_server_sync)
239 ctx->fence_server_sync(ctx, fence->pipe_fence);
240 }
241
242 const __DRI2fenceExtension dri2FenceExtension = {
243 .base = { __DRI2_FENCE, 2 },
244
245 .create_fence = dri_create_fence,
246 .get_fence_from_cl_event = dri_get_fence_from_cl_event,
247 .destroy_fence = dri_destroy_fence,
248 .client_wait_sync = dri_client_wait_sync,
249 .server_wait_sync = dri_server_wait_sync,
250 .get_capabilities = dri_fence_get_caps,
251 .create_fence_fd = dri_create_fence_fd,
252 .get_fence_fd = dri_get_fence_fd,
253 };
254
255 struct dri_image *
dri_create_image_from_renderbuffer(struct dri_context * dri_ctx,int renderbuffer,void * loaderPrivate,unsigned * error)256 dri_create_image_from_renderbuffer(struct dri_context *dri_ctx,
257 int renderbuffer, void *loaderPrivate,
258 unsigned *error)
259 {
260 struct st_context *st = dri_ctx->st;
261 struct gl_context *ctx = st->ctx;
262 struct pipe_context *p_ctx = st->pipe;
263 struct gl_renderbuffer *rb;
264 struct pipe_resource *tex;
265 struct dri_image *img;
266
267 /* Wait for glthread to finish to get up-to-date GL object lookups. */
268 _mesa_glthread_finish(st->ctx);
269
270 /* Section 3.9 (EGLImage Specification and Management) of the EGL 1.5
271 * specification says:
272 *
273 * "If target is EGL_GL_RENDERBUFFER and buffer is not the name of a
274 * renderbuffer object, or if buffer is the name of a multisampled
275 * renderbuffer object, the error EGL_BAD_PARAMETER is generated."
276 *
277 * "If target is EGL_GL_TEXTURE_2D , EGL_GL_TEXTURE_CUBE_MAP_*,
278 * EGL_GL_RENDERBUFFER or EGL_GL_TEXTURE_3D and buffer refers to the
279 * default GL texture object (0) for the corresponding GL target, the
280 * error EGL_BAD_PARAMETER is generated."
281 * (rely on _mesa_lookup_renderbuffer returning NULL in this case)
282 */
283 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
284 if (!rb || rb->NumSamples > 0) {
285 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
286 return NULL;
287 }
288
289 tex = rb->texture;
290 if (!tex) {
291 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
292 return NULL;
293 }
294
295 img = CALLOC_STRUCT(dri_image);
296 if (!img) {
297 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
298 return NULL;
299 }
300
301 img->dri_format = tex->format;
302 img->internal_format = rb->InternalFormat;
303 img->loader_private = loaderPrivate;
304 img->screen = dri_ctx->screen;
305 img->in_fence_fd = -1;
306
307 pipe_resource_reference(&img->texture, tex);
308
309 /* If the resource supports EGL_MESA_image_dma_buf_export, make sure that
310 * it's in a shareable state. Do this now while we still have the access to
311 * the context.
312 */
313 if (dri2_get_mapping_by_format(img->dri_format)) {
314 p_ctx->flush_resource(p_ctx, tex);
315 st_context_flush(st, 0, NULL, NULL, NULL);
316 }
317
318 ctx->Shared->HasExternallySharedImages = true;
319 *error = __DRI_IMAGE_ERROR_SUCCESS;
320 return img;
321 }
322
323 void
dri2_destroy_image(struct dri_image * img)324 dri2_destroy_image(struct dri_image *img)
325 {
326 const __DRIimageLoaderExtension *imgLoader = img->screen->image.loader;
327 const __DRIdri2LoaderExtension *dri2Loader = img->screen->dri2.loader;
328
329 if (imgLoader && imgLoader->base.version >= 4 &&
330 imgLoader->destroyLoaderImageState) {
331 imgLoader->destroyLoaderImageState(img->loader_private);
332 } else if (dri2Loader && dri2Loader->base.version >= 5 &&
333 dri2Loader->destroyLoaderImageState) {
334 dri2Loader->destroyLoaderImageState(img->loader_private);
335 }
336
337 pipe_resource_reference(&img->texture, NULL);
338
339 if (img->in_fence_fd != -1)
340 close(img->in_fence_fd);
341
342 FREE(img);
343 }
344
345
346 struct dri_image *
dri2_create_from_texture(struct dri_context * dri_ctx,int target,unsigned texture,int depth,int level,unsigned * error,void * loaderPrivate)347 dri2_create_from_texture(struct dri_context *dri_ctx, int target, unsigned texture,
348 int depth, int level, unsigned *error,
349 void *loaderPrivate)
350 {
351 struct dri_image *img;
352 struct st_context *st = dri_ctx->st;
353 struct gl_context *ctx = st->ctx;
354 struct pipe_context *p_ctx = st->pipe;
355 struct gl_texture_object *obj;
356 struct gl_texture_image *glimg;
357 GLuint face = 0;
358
359 /* Wait for glthread to finish to get up-to-date GL object lookups. */
360 _mesa_glthread_finish(st->ctx);
361
362 obj = _mesa_lookup_texture(ctx, texture);
363 if (!obj || obj->Target != target) {
364 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
365 return NULL;
366 }
367
368 if (target == GL_TEXTURE_CUBE_MAP)
369 face = depth;
370
371 _mesa_test_texobj_completeness(ctx, obj);
372 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
373 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
374 return NULL;
375 }
376
377 if (level < obj->Attrib.BaseLevel || level > obj->_MaxLevel) {
378 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
379 return NULL;
380 }
381
382 glimg = obj->Image[face][level];
383 if (!glimg || !glimg->pt) {
384 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
385 return NULL;
386 }
387
388 if (target == GL_TEXTURE_3D && glimg->Depth < depth) {
389 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
390 return NULL;
391 }
392
393 img = CALLOC_STRUCT(dri_image);
394 if (!img) {
395 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
396 return NULL;
397 }
398
399 img->level = level;
400 img->layer = depth;
401 img->in_fence_fd = -1;
402 img->dri_format = glimg->pt->format;
403 img->internal_format = glimg->InternalFormat;
404
405 img->loader_private = loaderPrivate;
406 img->screen = dri_ctx->screen;
407
408 pipe_resource_reference(&img->texture, glimg->pt);
409
410 /* If the resource supports EGL_MESA_image_dma_buf_export, make sure that
411 * it's in a shareable state. Do this now while we still have the access to
412 * the context.
413 */
414 if (dri2_get_mapping_by_format(img->dri_format)) {
415 p_ctx->flush_resource(p_ctx, glimg->pt);
416 st_context_flush(st, 0, NULL, NULL, NULL);
417 }
418
419 ctx->Shared->HasExternallySharedImages = true;
420 *error = __DRI_IMAGE_ERROR_SUCCESS;
421 return img;
422 }
423
424 static const struct dri2_format_mapping dri2_format_table[] = {
425 { DRM_FORMAT_ABGR16161616F, __DRI_IMAGE_FORMAT_ABGR16161616F,
426 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R16G16B16A16_FLOAT, 1,
427 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616F } } },
428 { DRM_FORMAT_XBGR16161616F, __DRI_IMAGE_FORMAT_XBGR16161616F,
429 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_R16G16B16X16_FLOAT, 1,
430 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR16161616F } } },
431 { DRM_FORMAT_ABGR16161616, __DRI_IMAGE_FORMAT_ABGR16161616,
432 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R16G16B16A16_UNORM, 1,
433 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
434 { DRM_FORMAT_XBGR16161616, __DRI_IMAGE_FORMAT_XBGR16161616,
435 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_R16G16B16X16_UNORM, 1,
436 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR16161616 } } },
437 { DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010,
438 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_B10G10R10A2_UNORM, 1,
439 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB2101010 } } },
440 { DRM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010,
441 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_B10G10R10X2_UNORM, 1,
442 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB2101010 } } },
443 { DRM_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ABGR2101010,
444 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R10G10B10A2_UNORM, 1,
445 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010 } } },
446 { DRM_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XBGR2101010,
447 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_R10G10B10X2_UNORM, 1,
448 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR2101010 } } },
449 { DRM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888,
450 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_BGRA8888_UNORM, 1,
451 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
452 { DRM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888,
453 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_RGBA8888_UNORM, 1,
454 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
455 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_FORMAT_SARGB8,
456 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_BGRA8888_SRGB, 1,
457 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8 } } },
458 { DRM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888,
459 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_BGRX8888_UNORM, 1,
460 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888 } } },
461 { DRM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888,
462 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_RGBX8888_UNORM, 1,
463 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888 } } },
464 { DRM_FORMAT_ARGB1555, __DRI_IMAGE_FORMAT_ARGB1555,
465 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_B5G5R5A1_UNORM, 1,
466 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555 } } },
467 { DRM_FORMAT_ABGR1555, __DRI_IMAGE_FORMAT_ABGR1555,
468 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R5G5B5A1_UNORM, 1,
469 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR1555 } } },
470 { DRM_FORMAT_ARGB4444, __DRI_IMAGE_FORMAT_ARGB4444,
471 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_B4G4R4A4_UNORM, 1,
472 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB4444 } } },
473 { DRM_FORMAT_ABGR4444, __DRI_IMAGE_FORMAT_ABGR4444,
474 __DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R4G4B4A4_UNORM, 1,
475 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR4444 } } },
476 { DRM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565,
477 __DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_B5G6R5_UNORM, 1,
478 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565 } } },
479 { DRM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8,
480 __DRI_IMAGE_COMPONENTS_R, PIPE_FORMAT_R8_UNORM, 1,
481 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
482 { DRM_FORMAT_R16, __DRI_IMAGE_FORMAT_R16,
483 __DRI_IMAGE_COMPONENTS_R, PIPE_FORMAT_R16_UNORM, 1,
484 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 } } },
485 { DRM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88,
486 __DRI_IMAGE_COMPONENTS_RG, PIPE_FORMAT_RG88_UNORM, 1,
487 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 } } },
488 { DRM_FORMAT_GR1616, __DRI_IMAGE_FORMAT_GR1616,
489 __DRI_IMAGE_COMPONENTS_RG, PIPE_FORMAT_RG1616_UNORM, 1,
490 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 } } },
491
492 { DRM_FORMAT_YUV410, __DRI_IMAGE_FORMAT_NONE,
493 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
494 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
495 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8 },
496 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8 } } },
497 { DRM_FORMAT_YUV411, __DRI_IMAGE_FORMAT_NONE,
498 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
499 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
500 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8 },
501 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8 } } },
502 { DRM_FORMAT_YUV420, __DRI_IMAGE_FORMAT_NONE,
503 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
504 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
505 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8 },
506 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8 } } },
507 { DRM_FORMAT_YUV422, __DRI_IMAGE_FORMAT_NONE,
508 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
509 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
510 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8 },
511 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8 } } },
512 { DRM_FORMAT_YUV444, __DRI_IMAGE_FORMAT_NONE,
513 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
514 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
515 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8 },
516 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
517
518 { DRM_FORMAT_YVU410, __DRI_IMAGE_FORMAT_NONE,
519 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
520 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
521 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8 },
522 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8 } } },
523 { DRM_FORMAT_YVU411, __DRI_IMAGE_FORMAT_NONE,
524 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
525 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
526 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8 },
527 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8 } } },
528 { DRM_FORMAT_YVU420, __DRI_IMAGE_FORMAT_NONE,
529 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
530 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
531 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8 },
532 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8 } } },
533 { DRM_FORMAT_YVU422, __DRI_IMAGE_FORMAT_NONE,
534 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
535 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
536 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8 },
537 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8 } } },
538 { DRM_FORMAT_YVU444, __DRI_IMAGE_FORMAT_NONE,
539 __DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
540 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
541 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8 },
542 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
543
544 { DRM_FORMAT_NV12, __DRI_IMAGE_FORMAT_NONE,
545 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV12, 2,
546 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
547 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88 } } },
548 { DRM_FORMAT_NV21, __DRI_IMAGE_FORMAT_NONE,
549 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV21, 2,
550 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
551 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88 } } },
552
553 /* 10 bit 4:2:0 and 4:2:2 formats; the components
554 are tightly packed, so the planes don't correspond
555 to any native DRI format */
556 { DRM_FORMAT_NV15, __DRI_IMAGE_FORMAT_NONE,
557 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV15, 2,
558 { { 0, 0, 0, __DRI_IMAGE_FORMAT_NONE },
559 { 1, 1, 1, __DRI_IMAGE_FORMAT_NONE } } },
560 { DRM_FORMAT_NV20, __DRI_IMAGE_FORMAT_NONE,
561 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV20, 2,
562 { { 0, 0, 0, __DRI_IMAGE_FORMAT_NONE },
563 { 1, 1, 0, __DRI_IMAGE_FORMAT_NONE } } },
564
565 { DRM_FORMAT_P010, __DRI_IMAGE_FORMAT_NONE,
566 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P010, 2,
567 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
568 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
569 { DRM_FORMAT_P012, __DRI_IMAGE_FORMAT_NONE,
570 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P012, 2,
571 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
572 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
573 { DRM_FORMAT_P016, __DRI_IMAGE_FORMAT_NONE,
574 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P016, 2,
575 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
576 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
577 { DRM_FORMAT_P030, __DRI_IMAGE_FORMAT_NONE,
578 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P030, 2,
579 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
580 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
581
582 { DRM_FORMAT_NV16, __DRI_IMAGE_FORMAT_NONE,
583 __DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV16, 2,
584 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
585 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88 } } },
586
587 { DRM_FORMAT_AYUV, __DRI_IMAGE_FORMAT_ABGR8888,
588 __DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_AYUV, 1,
589 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
590 { DRM_FORMAT_XYUV8888, __DRI_IMAGE_FORMAT_XBGR8888,
591 __DRI_IMAGE_COMPONENTS_XYUV, PIPE_FORMAT_XYUV, 1,
592 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888 } } },
593
594 { DRM_FORMAT_Y410, __DRI_IMAGE_FORMAT_ABGR2101010,
595 __DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_Y410, 1,
596 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010 } } },
597
598 /* Y412 is an unusual format. It has the same layout as Y416 (i.e.,
599 * 16-bits of physical storage per channel), but the low 4 bits of each
600 * component are unused padding. The writer is supposed to write zeros
601 * to these bits.
602 */
603 { DRM_FORMAT_Y412, __DRI_IMAGE_FORMAT_ABGR16161616,
604 __DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_Y412, 1,
605 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
606 { DRM_FORMAT_Y416, __DRI_IMAGE_FORMAT_ABGR16161616,
607 __DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_Y416, 1,
608 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
609
610 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
611 * and treat them as planar buffers in the compositors.
612 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
613 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
614 * clusters and places pairs and places U into the G component and
615 * V into A. This lets the texture sampler interpolate the Y
616 * components correctly when sampling from plane 0, and interpolate
617 * U and V correctly when sampling from plane 1. */
618 { DRM_FORMAT_YUYV, __DRI_IMAGE_FORMAT_NONE,
619 __DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_YUYV, 2,
620 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
621 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
622 { DRM_FORMAT_YVYU, __DRI_IMAGE_FORMAT_NONE,
623 __DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_YVYU, 2,
624 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
625 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
626 { DRM_FORMAT_UYVY, __DRI_IMAGE_FORMAT_NONE,
627 __DRI_IMAGE_COMPONENTS_Y_UXVX, PIPE_FORMAT_UYVY, 2,
628 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
629 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
630 { DRM_FORMAT_VYUY, __DRI_IMAGE_FORMAT_NONE,
631 __DRI_IMAGE_COMPONENTS_Y_UXVX, PIPE_FORMAT_VYUY, 2,
632 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
633 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
634
635 /* The Y21x formats work in a similar fashion to the YUYV and UYVY
636 * formats.
637 */
638 { DRM_FORMAT_Y210, __DRI_IMAGE_FORMAT_NONE,
639 __DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_Y210, 2,
640 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
641 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
642 /* Y212 is an unusual format. It has the same layout as Y216 (i.e.,
643 * 16-bits of physical storage per channel), but the low 4 bits of each
644 * component are unused padding. The writer is supposed to write zeros
645 * to these bits.
646 */
647 { DRM_FORMAT_Y212, __DRI_IMAGE_FORMAT_NONE,
648 __DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_Y212, 2,
649 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
650 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
651 { DRM_FORMAT_Y216, __DRI_IMAGE_FORMAT_NONE,
652 __DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_Y216, 2,
653 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
654 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
655 };
656
657 const struct dri2_format_mapping *
dri2_get_mapping_by_fourcc(int fourcc)658 dri2_get_mapping_by_fourcc(int fourcc)
659 {
660 for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
661 if (dri2_format_table[i].dri_fourcc == fourcc)
662 return &dri2_format_table[i];
663 }
664
665 return NULL;
666 }
667
668 const struct dri2_format_mapping *
dri2_get_mapping_by_format(int format)669 dri2_get_mapping_by_format(int format)
670 {
671 if (format == __DRI_IMAGE_FORMAT_NONE)
672 return NULL;
673
674 for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
675 if (dri2_format_table[i].dri_format == format)
676 return &dri2_format_table[i];
677 }
678
679 return NULL;
680 }
681
682 enum pipe_format
dri2_get_pipe_format_for_dri_format(int format)683 dri2_get_pipe_format_for_dri_format(int format)
684 {
685 for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
686 if (dri2_format_table[i].dri_format == format)
687 return dri2_format_table[i].pipe_format;
688 }
689
690 return PIPE_FORMAT_NONE;
691 }
692
693 static enum pipe_format
alt_pipe_format(enum pipe_format yuv_fmt)694 alt_pipe_format(enum pipe_format yuv_fmt)
695 {
696 switch(yuv_fmt) {
697 case PIPE_FORMAT_NV12:
698 return PIPE_FORMAT_R8_G8B8_420_UNORM;
699 case PIPE_FORMAT_NV16:
700 return PIPE_FORMAT_R8_G8B8_422_UNORM;
701 case PIPE_FORMAT_NV21:
702 return PIPE_FORMAT_R8_B8G8_420_UNORM;
703 case PIPE_FORMAT_NV15:
704 return PIPE_FORMAT_R10_G10B10_420_UNORM;
705 case PIPE_FORMAT_NV20:
706 return PIPE_FORMAT_R10_G10B10_422_UNORM;
707 default:
708 return yuv_fmt;
709 }
710 }
711
712 bool
dri2_yuv_dma_buf_supported(struct dri_screen * screen,const struct dri2_format_mapping * map)713 dri2_yuv_dma_buf_supported(struct dri_screen *screen,
714 const struct dri2_format_mapping *map)
715 {
716 struct pipe_screen *pscreen = screen->base.screen;
717
718 if (pscreen->is_format_supported(pscreen, alt_pipe_format(map->pipe_format),
719 screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW))
720 return true;
721 for (unsigned i = 0; i < map->nplanes; i++) {
722 if (!pscreen->is_format_supported(pscreen,
723 dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format),
724 screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW))
725 return false;
726 }
727 return true;
728 }
729
730 bool
dri_query_dma_buf_formats(struct dri_screen * screen,int max,int * formats,int * count)731 dri_query_dma_buf_formats(struct dri_screen *screen, int max, int *formats,
732 int *count)
733 {
734 struct pipe_screen *pscreen = screen->base.screen;
735 int i, j;
736
737 for (i = 0, j = 0; (i < ARRAY_SIZE(dri2_format_table)) &&
738 (j < max || max == 0); i++) {
739 const struct dri2_format_mapping *map = &dri2_format_table[i];
740
741 /* The sRGB format is not a real FourCC as defined by drm_fourcc.h, so we
742 * must not leak it out to clients. */
743 if (dri2_format_table[i].dri_fourcc == __DRI_IMAGE_FOURCC_SARGB8888)
744 continue;
745
746 if (pscreen->is_format_supported(pscreen, map->pipe_format,
747 screen->target, 0, 0,
748 PIPE_BIND_RENDER_TARGET) ||
749 pscreen->is_format_supported(pscreen, map->pipe_format,
750 screen->target, 0, 0,
751 PIPE_BIND_SAMPLER_VIEW) ||
752 dri2_yuv_dma_buf_supported(screen, map)) {
753 if (j < max)
754 formats[j] = map->dri_fourcc;
755 j++;
756 }
757 }
758 *count = j;
759 return true;
760 }
761
762
763 struct dri_image *
dri_create_image_with_modifiers(struct dri_screen * screen,uint32_t width,uint32_t height,uint32_t dri_format,uint32_t dri_usage,const uint64_t * modifiers,unsigned int modifiers_count,void * loaderPrivate)764 dri_create_image_with_modifiers(struct dri_screen *screen,
765 uint32_t width, uint32_t height,
766 uint32_t dri_format, uint32_t dri_usage,
767 const uint64_t *modifiers,
768 unsigned int modifiers_count,
769 void *loaderPrivate)
770 {
771 if (modifiers && modifiers_count > 0) {
772 bool has_valid_modifier = false;
773 int i;
774
775 /* It's acceptable to create an image with INVALID modifier in the list,
776 * but it cannot be on the only modifier (since it will certainly fail
777 * later). While we could easily catch this after modifier creation, doing
778 * the check here is a convenient debug check likely pointing at whatever
779 * interface the client is using to build its modifier list.
780 */
781 for (i = 0; i < modifiers_count; i++) {
782 if (modifiers[i] != DRM_FORMAT_MOD_INVALID) {
783 has_valid_modifier = true;
784 break;
785 }
786 }
787 if (!has_valid_modifier)
788 return NULL;
789 }
790
791 return dri_create_image(screen, width, height, dri_format,
792 modifiers, modifiers_count, dri_usage,
793 loaderPrivate);
794 }
795
796 void
dri_image_fence_sync(struct dri_context * ctx,struct dri_image * img)797 dri_image_fence_sync(struct dri_context *ctx, struct dri_image *img)
798 {
799 struct pipe_context *pipe = ctx->st->pipe;
800 struct pipe_fence_handle *fence;
801 int fd = img->in_fence_fd;
802
803 if (fd == -1)
804 return;
805
806 validate_fence_fd(fd);
807
808 img->in_fence_fd = -1;
809
810 pipe->create_fence_fd(pipe, &fence, fd, PIPE_FD_TYPE_NATIVE_SYNC);
811 pipe->fence_server_sync(pipe, fence);
812 pipe->screen->fence_reference(pipe->screen, &fence, NULL);
813
814 close(fd);
815 }
816 /* vim: set sw=3 ts=8 sts=3 expandtab: */
817