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 "state_tracker/st_cb_fbo.h"
30 #include "main/texobj.h"
31 
32 #include "dri_helpers.h"
33 
34 static bool
dri2_is_opencl_interop_loaded_locked(struct dri_screen * screen)35 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
36 {
37    return screen->opencl_dri_event_add_ref &&
38           screen->opencl_dri_event_release &&
39           screen->opencl_dri_event_wait &&
40           screen->opencl_dri_event_get_fence;
41 }
42 
43 static bool
dri2_load_opencl_interop(struct dri_screen * screen)44 dri2_load_opencl_interop(struct dri_screen *screen)
45 {
46 #if defined(RTLD_DEFAULT)
47    bool success;
48 
49    mtx_lock(&screen->opencl_func_mutex);
50 
51    if (dri2_is_opencl_interop_loaded_locked(screen)) {
52       mtx_unlock(&screen->opencl_func_mutex);
53       return true;
54    }
55 
56    screen->opencl_dri_event_add_ref =
57       dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
58    screen->opencl_dri_event_release =
59       dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
60    screen->opencl_dri_event_wait =
61       dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
62    screen->opencl_dri_event_get_fence =
63       dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
64 
65    success = dri2_is_opencl_interop_loaded_locked(screen);
66    mtx_unlock(&screen->opencl_func_mutex);
67    return success;
68 #else
69    return false;
70 #endif
71 }
72 
73 struct dri2_fence {
74    struct dri_screen *driscreen;
75    struct pipe_fence_handle *pipe_fence;
76    void *cl_event;
77 };
78 
dri2_fence_get_caps(__DRIscreen * _screen)79 static unsigned dri2_fence_get_caps(__DRIscreen *_screen)
80 {
81    struct dri_screen *driscreen = dri_screen(_screen);
82    struct pipe_screen *screen = driscreen->base.screen;
83    unsigned caps = 0;
84 
85    if (screen->get_param(screen, PIPE_CAP_NATIVE_FENCE_FD))
86       caps |= __DRI_FENCE_CAP_NATIVE_FD;
87 
88    return caps;
89 }
90 
91 static void *
dri2_create_fence(__DRIcontext * _ctx)92 dri2_create_fence(__DRIcontext *_ctx)
93 {
94    struct st_context_iface *stapi = dri_context(_ctx)->st;
95    struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
96 
97    if (!fence)
98       return NULL;
99 
100    stapi->flush(stapi, 0, &fence->pipe_fence, NULL, NULL);
101 
102    if (!fence->pipe_fence) {
103       FREE(fence);
104       return NULL;
105    }
106 
107    fence->driscreen = dri_screen(_ctx->driScreenPriv);
108    return fence;
109 }
110 
111 static void *
dri2_create_fence_fd(__DRIcontext * _ctx,int fd)112 dri2_create_fence_fd(__DRIcontext *_ctx, int fd)
113 {
114    struct st_context_iface *stapi = dri_context(_ctx)->st;
115    struct pipe_context *ctx = stapi->pipe;
116    struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
117 
118    if (fd == -1) {
119       /* exporting driver created fence, flush: */
120       stapi->flush(stapi, ST_FLUSH_FENCE_FD, &fence->pipe_fence, NULL, NULL);
121    } else {
122       /* importing a foreign fence fd: */
123       ctx->create_fence_fd(ctx, &fence->pipe_fence, fd, PIPE_FD_TYPE_NATIVE_SYNC);
124    }
125    if (!fence->pipe_fence) {
126       FREE(fence);
127       return NULL;
128    }
129 
130    fence->driscreen = dri_screen(_ctx->driScreenPriv);
131    return fence;
132 }
133 
134 static int
dri2_get_fence_fd(__DRIscreen * _screen,void * _fence)135 dri2_get_fence_fd(__DRIscreen *_screen, void *_fence)
136 {
137    struct dri_screen *driscreen = dri_screen(_screen);
138    struct pipe_screen *screen = driscreen->base.screen;
139    struct dri2_fence *fence = (struct dri2_fence*)_fence;
140 
141    return screen->fence_get_fd(screen, fence->pipe_fence);
142 }
143 
144 static void *
dri2_get_fence_from_cl_event(__DRIscreen * _screen,intptr_t cl_event)145 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
146 {
147    struct dri_screen *driscreen = dri_screen(_screen);
148    struct dri2_fence *fence;
149 
150    if (!dri2_load_opencl_interop(driscreen))
151       return NULL;
152 
153    fence = CALLOC_STRUCT(dri2_fence);
154    if (!fence)
155       return NULL;
156 
157    fence->cl_event = (void*)cl_event;
158 
159    if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
160       free(fence);
161       return NULL;
162    }
163 
164    fence->driscreen = driscreen;
165    return fence;
166 }
167 
168 static void
dri2_destroy_fence(__DRIscreen * _screen,void * _fence)169 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
170 {
171    struct dri_screen *driscreen = dri_screen(_screen);
172    struct pipe_screen *screen = driscreen->base.screen;
173    struct dri2_fence *fence = (struct dri2_fence*)_fence;
174 
175    if (fence->pipe_fence)
176       screen->fence_reference(screen, &fence->pipe_fence, NULL);
177    else if (fence->cl_event)
178       driscreen->opencl_dri_event_release(fence->cl_event);
179    else
180       assert(0);
181 
182    FREE(fence);
183 }
184 
185 static GLboolean
dri2_client_wait_sync(__DRIcontext * _ctx,void * _fence,unsigned flags,uint64_t timeout)186 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
187                       uint64_t timeout)
188 {
189    struct dri2_fence *fence = (struct dri2_fence*)_fence;
190    struct dri_screen *driscreen = fence->driscreen;
191    struct pipe_screen *screen = driscreen->base.screen;
192 
193    /* No need to flush. The context was flushed when the fence was created. */
194 
195    if (fence->pipe_fence)
196       return screen->fence_finish(screen, NULL, fence->pipe_fence, timeout);
197    else if (fence->cl_event) {
198       struct pipe_fence_handle *pipe_fence =
199          driscreen->opencl_dri_event_get_fence(fence->cl_event);
200 
201       if (pipe_fence)
202          return screen->fence_finish(screen, NULL, pipe_fence, timeout);
203       else
204          return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
205    }
206    else {
207       assert(0);
208       return false;
209    }
210 }
211 
212 static void
dri2_server_wait_sync(__DRIcontext * _ctx,void * _fence,unsigned flags)213 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
214 {
215    struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
216    struct dri2_fence *fence = (struct dri2_fence*)_fence;
217 
218    /* We might be called here with a NULL fence as a result of WaitSyncKHR
219     * on a EGL_KHR_reusable_sync fence. Nothing to do here in such case.
220     */
221    if (!fence)
222       return;
223 
224    if (ctx->fence_server_sync)
225       ctx->fence_server_sync(ctx, fence->pipe_fence);
226 }
227 
228 const __DRI2fenceExtension dri2FenceExtension = {
229    .base = { __DRI2_FENCE, 2 },
230 
231    .create_fence = dri2_create_fence,
232    .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
233    .destroy_fence = dri2_destroy_fence,
234    .client_wait_sync = dri2_client_wait_sync,
235    .server_wait_sync = dri2_server_wait_sync,
236    .get_capabilities = dri2_fence_get_caps,
237    .create_fence_fd = dri2_create_fence_fd,
238    .get_fence_fd = dri2_get_fence_fd,
239 };
240 
241 __DRIimage *
dri2_lookup_egl_image(struct dri_screen * screen,void * handle)242 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
243 {
244    const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
245    __DRIimage *img;
246 
247    if (!loader->lookupEGLImage)
248       return NULL;
249 
250    img = loader->lookupEGLImage(screen->sPriv,
251 				handle, screen->sPriv->loaderPrivate);
252 
253    return img;
254 }
255 
256 boolean
dri2_validate_egl_image(struct dri_screen * screen,void * handle)257 dri2_validate_egl_image(struct dri_screen *screen, void *handle)
258 {
259    const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
260 
261    return loader->validateEGLImage(handle, screen->sPriv->loaderPrivate);
262 }
263 
264 __DRIimage *
dri2_lookup_egl_image_validated(struct dri_screen * screen,void * handle)265 dri2_lookup_egl_image_validated(struct dri_screen *screen, void *handle)
266 {
267    const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
268 
269    return loader->lookupEGLImageValidated(handle, screen->sPriv->loaderPrivate);
270 }
271 
272 __DRIimage *
dri2_create_image_from_renderbuffer2(__DRIcontext * context,int renderbuffer,void * loaderPrivate,unsigned * error)273 dri2_create_image_from_renderbuffer2(__DRIcontext *context,
274 				     int renderbuffer, void *loaderPrivate,
275                                      unsigned *error)
276 {
277    struct st_context *st_ctx = (struct st_context *)dri_context(context)->st;
278    struct gl_context *ctx = st_ctx->ctx;
279    struct pipe_context *p_ctx = st_ctx->pipe;
280    struct gl_renderbuffer *rb;
281    struct pipe_resource *tex;
282    __DRIimage *img;
283 
284    /* Section 3.9 (EGLImage Specification and Management) of the EGL 1.5
285     * specification says:
286     *
287     *   "If target is EGL_GL_RENDERBUFFER and buffer is not the name of a
288     *    renderbuffer object, or if buffer is the name of a multisampled
289     *    renderbuffer object, the error EGL_BAD_PARAMETER is generated."
290     *
291     *   "If target is EGL_GL_TEXTURE_2D , EGL_GL_TEXTURE_CUBE_MAP_*,
292     *    EGL_GL_RENDERBUFFER or EGL_GL_TEXTURE_3D and buffer refers to the
293     *    default GL texture object (0) for the corresponding GL target, the
294     *    error EGL_BAD_PARAMETER is generated."
295     *   (rely on _mesa_lookup_renderbuffer returning NULL in this case)
296     */
297    rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
298    if (!rb || rb->NumSamples > 0) {
299       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
300       return NULL;
301    }
302 
303    tex = st_get_renderbuffer_resource(rb);
304    if (!tex) {
305       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
306       return NULL;
307    }
308 
309    img = CALLOC_STRUCT(__DRIimageRec);
310    if (!img) {
311       *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
312       return NULL;
313    }
314 
315    img->dri_format = driGLFormatToImageFormat(rb->Format);
316    img->loader_private = loaderPrivate;
317    img->sPriv = context->driScreenPriv;
318 
319    pipe_resource_reference(&img->texture, tex);
320 
321    /* If the resource supports EGL_MESA_image_dma_buf_export, make sure that
322     * it's in a shareable state. Do this now while we still have the access to
323     * the context.
324     */
325    if (dri2_get_mapping_by_format(img->dri_format))
326       p_ctx->flush_resource(p_ctx, tex);
327 
328    ctx->Shared->HasExternallySharedImages = true;
329    *error = __DRI_IMAGE_ERROR_SUCCESS;
330    return img;
331 }
332 
333 __DRIimage *
dri2_create_image_from_renderbuffer(__DRIcontext * context,int renderbuffer,void * loaderPrivate)334 dri2_create_image_from_renderbuffer(__DRIcontext *context,
335 				    int renderbuffer, void *loaderPrivate)
336 {
337    unsigned error;
338    return dri2_create_image_from_renderbuffer2(context, renderbuffer,
339                                                loaderPrivate, &error);
340 }
341 
342 void
dri2_destroy_image(__DRIimage * img)343 dri2_destroy_image(__DRIimage *img)
344 {
345    const __DRIimageLoaderExtension *imgLoader = img->sPriv->image.loader;
346    const __DRIdri2LoaderExtension *dri2Loader = img->sPriv->dri2.loader;
347 
348    if (imgLoader && imgLoader->base.version >= 4 &&
349          imgLoader->destroyLoaderImageState) {
350       imgLoader->destroyLoaderImageState(img->loader_private);
351    } else if (dri2Loader && dri2Loader->base.version >= 5 &&
352          dri2Loader->destroyLoaderImageState) {
353       dri2Loader->destroyLoaderImageState(img->loader_private);
354    }
355 
356    pipe_resource_reference(&img->texture, NULL);
357    FREE(img);
358 }
359 
360 
361 __DRIimage *
dri2_create_from_texture(__DRIcontext * context,int target,unsigned texture,int depth,int level,unsigned * error,void * loaderPrivate)362 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
363                          int depth, int level, unsigned *error,
364                          void *loaderPrivate)
365 {
366    __DRIimage *img;
367    struct st_context *st_ctx = (struct st_context *)dri_context(context)->st;
368    struct gl_context *ctx = st_ctx->ctx;
369    struct pipe_context *p_ctx = st_ctx->pipe;
370    struct gl_texture_object *obj;
371    struct pipe_resource *tex;
372    GLuint face = 0;
373 
374    obj = _mesa_lookup_texture(ctx, texture);
375    if (!obj || obj->Target != target) {
376       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
377       return NULL;
378    }
379 
380    tex = st_get_texobj_resource(obj);
381    if (!tex) {
382       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
383       return NULL;
384    }
385 
386    if (target == GL_TEXTURE_CUBE_MAP)
387       face = depth;
388 
389    _mesa_test_texobj_completeness(ctx, obj);
390    if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
391       *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
392       return NULL;
393    }
394 
395    if (level < obj->Attrib.BaseLevel || level > obj->_MaxLevel) {
396       *error = __DRI_IMAGE_ERROR_BAD_MATCH;
397       return NULL;
398    }
399 
400    if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
401       *error = __DRI_IMAGE_ERROR_BAD_MATCH;
402       return NULL;
403    }
404 
405    img = CALLOC_STRUCT(__DRIimageRec);
406    if (!img) {
407       *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
408       return NULL;
409    }
410 
411    img->level = level;
412    img->layer = depth;
413    img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
414 
415    img->loader_private = loaderPrivate;
416    img->sPriv = context->driScreenPriv;
417 
418    pipe_resource_reference(&img->texture, tex);
419 
420    /* If the resource supports EGL_MESA_image_dma_buf_export, make sure that
421     * it's in a shareable state. Do this now while we still have the access to
422     * the context.
423     */
424    if (dri2_get_mapping_by_format(img->dri_format))
425       p_ctx->flush_resource(p_ctx, tex);
426 
427    ctx->Shared->HasExternallySharedImages = true;
428    *error = __DRI_IMAGE_ERROR_SUCCESS;
429    return img;
430 }
431 
432 static const struct dri2_format_mapping dri2_format_table[] = {
433       { DRM_FORMAT_ABGR16161616F, __DRI_IMAGE_FORMAT_ABGR16161616F,
434         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_R16G16B16A16_FLOAT, 1,
435         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616F } } },
436       { DRM_FORMAT_XBGR16161616F, __DRI_IMAGE_FORMAT_XBGR16161616F,
437         __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_R16G16B16X16_FLOAT, 1,
438         { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR16161616F } } },
439       { __DRI_IMAGE_FOURCC_RGBA16161616, __DRI_IMAGE_FORMAT_ABGR16161616,
440         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_R16G16B16A16_UNORM, 1,
441         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
442       { DRM_FORMAT_ARGB2101010,   __DRI_IMAGE_FORMAT_ARGB2101010,
443         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_B10G10R10A2_UNORM, 1,
444         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB2101010 } } },
445       { DRM_FORMAT_XRGB2101010,   __DRI_IMAGE_FORMAT_XRGB2101010,
446         __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_B10G10R10X2_UNORM, 1,
447         { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB2101010 } } },
448       { DRM_FORMAT_ABGR2101010,   __DRI_IMAGE_FORMAT_ABGR2101010,
449         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_R10G10B10A2_UNORM, 1,
450         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010 } } },
451       { DRM_FORMAT_XBGR2101010,   __DRI_IMAGE_FORMAT_XBGR2101010,
452         __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_R10G10B10X2_UNORM, 1,
453         { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR2101010 } } },
454       { DRM_FORMAT_ARGB8888,      __DRI_IMAGE_FORMAT_ARGB8888,
455         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_BGRA8888_UNORM, 1,
456         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
457       { DRM_FORMAT_ABGR8888,      __DRI_IMAGE_FORMAT_ABGR8888,
458         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_RGBA8888_UNORM, 1,
459         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
460       { __DRI_IMAGE_FOURCC_SARGB8888,     __DRI_IMAGE_FORMAT_SARGB8,
461         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_BGRA8888_SRGB, 1,
462         { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8 } } },
463       { DRM_FORMAT_XRGB8888,      __DRI_IMAGE_FORMAT_XRGB8888,
464         __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_BGRX8888_UNORM, 1,
465         { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888 } } },
466       { DRM_FORMAT_XBGR8888,      __DRI_IMAGE_FORMAT_XBGR8888,
467         __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_RGBX8888_UNORM, 1,
468         { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888 } } },
469       { DRM_FORMAT_ARGB1555,      __DRI_IMAGE_FORMAT_ARGB1555,
470         __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_B5G5R5A1_UNORM, 1,
471         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555 } } },
472       { DRM_FORMAT_RGB565,        __DRI_IMAGE_FORMAT_RGB565,
473         __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_B5G6R5_UNORM, 1,
474         { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565 } } },
475       { DRM_FORMAT_R8,            __DRI_IMAGE_FORMAT_R8,
476         __DRI_IMAGE_COMPONENTS_R,         PIPE_FORMAT_R8_UNORM, 1,
477         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
478       { DRM_FORMAT_R16,           __DRI_IMAGE_FORMAT_R16,
479         __DRI_IMAGE_COMPONENTS_R,         PIPE_FORMAT_R16_UNORM, 1,
480         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 } } },
481       { DRM_FORMAT_GR88,          __DRI_IMAGE_FORMAT_GR88,
482         __DRI_IMAGE_COMPONENTS_RG,        PIPE_FORMAT_RG88_UNORM, 1,
483         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 } } },
484       { DRM_FORMAT_GR1616,        __DRI_IMAGE_FORMAT_GR1616,
485         __DRI_IMAGE_COMPONENTS_RG,        PIPE_FORMAT_RG1616_UNORM, 1,
486         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 } } },
487 
488       { DRM_FORMAT_YUV410, __DRI_IMAGE_FORMAT_NONE,
489         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
490         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
491           { 1, 2, 2, __DRI_IMAGE_FORMAT_R8 },
492           { 2, 2, 2, __DRI_IMAGE_FORMAT_R8 } } },
493       { DRM_FORMAT_YUV411, __DRI_IMAGE_FORMAT_NONE,
494         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
495         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
496           { 1, 2, 0, __DRI_IMAGE_FORMAT_R8 },
497           { 2, 2, 0, __DRI_IMAGE_FORMAT_R8 } } },
498       { DRM_FORMAT_YUV420,        __DRI_IMAGE_FORMAT_NONE,
499         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
500         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
501           { 1, 1, 1, __DRI_IMAGE_FORMAT_R8 },
502           { 2, 1, 1, __DRI_IMAGE_FORMAT_R8 } } },
503       { DRM_FORMAT_YUV422,        __DRI_IMAGE_FORMAT_NONE,
504         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
505         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
506           { 1, 1, 0, __DRI_IMAGE_FORMAT_R8 },
507           { 2, 1, 0, __DRI_IMAGE_FORMAT_R8 } } },
508       { DRM_FORMAT_YUV444,        __DRI_IMAGE_FORMAT_NONE,
509         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
510         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
511           { 1, 0, 0, __DRI_IMAGE_FORMAT_R8 },
512           { 2, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
513 
514       { DRM_FORMAT_YVU410,        __DRI_IMAGE_FORMAT_NONE,
515         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
516         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
517           { 2, 2, 2, __DRI_IMAGE_FORMAT_R8 },
518           { 1, 2, 2, __DRI_IMAGE_FORMAT_R8 } } },
519       { DRM_FORMAT_YVU411,        __DRI_IMAGE_FORMAT_NONE,
520         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
521         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
522           { 2, 2, 0, __DRI_IMAGE_FORMAT_R8 },
523           { 1, 2, 0, __DRI_IMAGE_FORMAT_R8 } } },
524       { DRM_FORMAT_YVU420,        __DRI_IMAGE_FORMAT_NONE,
525         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
526         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
527           { 2, 1, 1, __DRI_IMAGE_FORMAT_R8 },
528           { 1, 1, 1, __DRI_IMAGE_FORMAT_R8 } } },
529       { DRM_FORMAT_YVU422,        __DRI_IMAGE_FORMAT_NONE,
530         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
531         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
532           { 2, 1, 0, __DRI_IMAGE_FORMAT_R8 },
533           { 1, 1, 0, __DRI_IMAGE_FORMAT_R8 } } },
534       { DRM_FORMAT_YVU444,        __DRI_IMAGE_FORMAT_NONE,
535         __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
536         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
537           { 2, 0, 0, __DRI_IMAGE_FORMAT_R8 },
538           { 1, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
539 
540       { DRM_FORMAT_NV12,          __DRI_IMAGE_FORMAT_NONE,
541         __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_NV12, 2,
542         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
543           { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88 } } },
544 
545       { DRM_FORMAT_P010,          __DRI_IMAGE_FORMAT_NONE,
546         __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_P010, 2,
547         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
548           { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
549       { DRM_FORMAT_P012,          __DRI_IMAGE_FORMAT_NONE,
550         __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_P012, 2,
551         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
552           { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
553       { DRM_FORMAT_P016,          __DRI_IMAGE_FORMAT_NONE,
554         __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_P016, 2,
555         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
556           { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
557 
558       { DRM_FORMAT_NV16,          __DRI_IMAGE_FORMAT_NONE,
559         __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_NV12, 2,
560         { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
561           { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88 } } },
562 
563       { DRM_FORMAT_AYUV,      __DRI_IMAGE_FORMAT_ABGR8888,
564         __DRI_IMAGE_COMPONENTS_AYUV,      PIPE_FORMAT_AYUV, 1,
565         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
566       { DRM_FORMAT_XYUV8888,      __DRI_IMAGE_FORMAT_XBGR8888,
567         __DRI_IMAGE_COMPONENTS_XYUV,      PIPE_FORMAT_XYUV, 1,
568         { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888 } } },
569 
570       { DRM_FORMAT_Y410,          __DRI_IMAGE_FORMAT_ABGR2101010,
571         __DRI_IMAGE_COMPONENTS_AYUV,      PIPE_FORMAT_Y410, 1,
572         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010 } } },
573 
574       /* Y412 is an unusual format.  It has the same layout as Y416 (i.e.,
575        * 16-bits of physical storage per channel), but the low 4 bits of each
576        * component are unused padding.  The writer is supposed to write zeros
577        * to these bits.
578        */
579       { DRM_FORMAT_Y412,          __DRI_IMAGE_FORMAT_ABGR16161616,
580         __DRI_IMAGE_COMPONENTS_AYUV,      PIPE_FORMAT_Y412, 1,
581         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
582       { DRM_FORMAT_Y416,          __DRI_IMAGE_FORMAT_ABGR16161616,
583         __DRI_IMAGE_COMPONENTS_AYUV,      PIPE_FORMAT_Y416, 1,
584         { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
585 
586       /* For YUYV and UYVY buffers, we set up two overlapping DRI images
587        * and treat them as planar buffers in the compositors.
588        * Plane 0 is GR88 and samples YU or YV pairs and places Y into
589        * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
590        * clusters and places pairs and places U into the G component and
591        * V into A.  This lets the texture sampler interpolate the Y
592        * components correctly when sampling from plane 0, and interpolate
593        * U and V correctly when sampling from plane 1. */
594       { DRM_FORMAT_YUYV,          __DRI_IMAGE_FORMAT_NONE,
595         __DRI_IMAGE_COMPONENTS_Y_XUXV,    PIPE_FORMAT_YUYV, 2,
596         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
597           { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
598       { DRM_FORMAT_UYVY,          __DRI_IMAGE_FORMAT_NONE,
599         __DRI_IMAGE_COMPONENTS_Y_UXVX,    PIPE_FORMAT_UYVY, 2,
600         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
601           { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
602 
603       /* The Y21x formats work in a similar fashion to the YUYV and UYVY
604        * formats.
605        */
606       { DRM_FORMAT_Y210,          __DRI_IMAGE_FORMAT_NONE,
607         __DRI_IMAGE_COMPONENTS_Y_XUXV,    PIPE_FORMAT_Y210, 2,
608         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
609           { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
610       /* Y212 is an unusual format.  It has the same layout as Y216 (i.e.,
611        * 16-bits of physical storage per channel), but the low 4 bits of each
612        * component are unused padding.  The writer is supposed to write zeros
613        * to these bits.
614        */
615       { DRM_FORMAT_Y212,          __DRI_IMAGE_FORMAT_NONE,
616         __DRI_IMAGE_COMPONENTS_Y_XUXV,    PIPE_FORMAT_Y212, 2,
617         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
618           { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
619       { DRM_FORMAT_Y216,          __DRI_IMAGE_FORMAT_NONE,
620         __DRI_IMAGE_COMPONENTS_Y_XUXV,    PIPE_FORMAT_Y216, 2,
621         { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
622           { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
623 };
624 
625 const struct dri2_format_mapping *
dri2_get_mapping_by_fourcc(int fourcc)626 dri2_get_mapping_by_fourcc(int fourcc)
627 {
628    for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
629       if (dri2_format_table[i].dri_fourcc == fourcc)
630          return &dri2_format_table[i];
631    }
632 
633    return NULL;
634 }
635 
636 const struct dri2_format_mapping *
dri2_get_mapping_by_format(int format)637 dri2_get_mapping_by_format(int format)
638 {
639    if (format == __DRI_IMAGE_FORMAT_NONE)
640       return NULL;
641 
642    for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
643       if (dri2_format_table[i].dri_format == format)
644          return &dri2_format_table[i];
645    }
646 
647    return NULL;
648 }
649 
650 enum pipe_format
dri2_get_pipe_format_for_dri_format(int format)651 dri2_get_pipe_format_for_dri_format(int format)
652 {
653    for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
654       if (dri2_format_table[i].dri_format == format)
655          return dri2_format_table[i].pipe_format;
656    }
657 
658    return PIPE_FORMAT_NONE;
659 }
660 
661 boolean
dri2_yuv_dma_buf_supported(struct dri_screen * screen,const struct dri2_format_mapping * map)662 dri2_yuv_dma_buf_supported(struct dri_screen *screen,
663                            const struct dri2_format_mapping *map)
664 {
665    struct pipe_screen *pscreen = screen->base.screen;
666 
667    for (unsigned i = 0; i < map->nplanes; i++) {
668       if (!pscreen->is_format_supported(pscreen,
669             dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format),
670             screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW))
671          return false;
672    }
673    return true;
674 }
675 
676 boolean
dri2_query_dma_buf_formats(__DRIscreen * _screen,int max,int * formats,int * count)677 dri2_query_dma_buf_formats(__DRIscreen *_screen, int max, int *formats,
678                            int *count)
679 {
680    struct dri_screen *screen = dri_screen(_screen);
681    struct pipe_screen *pscreen = screen->base.screen;
682    int i, j;
683 
684    for (i = 0, j = 0; (i < ARRAY_SIZE(dri2_format_table)) &&
685          (j < max || max == 0); i++) {
686       const struct dri2_format_mapping *map = &dri2_format_table[i];
687 
688       /* The sRGB format is not a real FourCC as defined by drm_fourcc.h, so we
689        * must not leak it out to clients.  The RGBA16161616 format isn't
690        * real either, but at some point it could be.  Don't leak it out form
691        * now.
692        */
693       if (dri2_format_table[i].dri_fourcc == __DRI_IMAGE_FOURCC_SARGB8888 ||
694           dri2_format_table[i].dri_fourcc == __DRI_IMAGE_FOURCC_RGBA16161616)
695          continue;
696 
697       if (pscreen->is_format_supported(pscreen, map->pipe_format,
698                                        screen->target, 0, 0,
699                                        PIPE_BIND_RENDER_TARGET) ||
700           pscreen->is_format_supported(pscreen, map->pipe_format,
701                                        screen->target, 0, 0,
702                                        PIPE_BIND_SAMPLER_VIEW) ||
703           dri2_yuv_dma_buf_supported(screen, map)) {
704          if (j < max)
705             formats[j] = map->dri_fourcc;
706          j++;
707       }
708    }
709    *count = j;
710    return true;
711 }
712 
713 /* vim: set sw=3 ts=8 sts=3 expandtab: */
714