aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/state_trackers/dri/dri_helpers.c
blob: 90a8a392fad52aa854b24543d9bae1d3d6ef6c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <dlfcn.h>
#include "drm-uapi/drm_fourcc.h"
#include "util/u_memory.h"
#include "pipe/p_screen.h"
#include "state_tracker/st_texture.h"
#include "state_tracker/st_context.h"
#include "state_tracker/st_cb_fbo.h"
#include "main/texobj.h"

#include "dri_helpers.h"

static bool
dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
{
   return screen->opencl_dri_event_add_ref &&
          screen->opencl_dri_event_release &&
          screen->opencl_dri_event_wait &&
          screen->opencl_dri_event_get_fence;
}

static bool
dri2_load_opencl_interop(struct dri_screen *screen)
{
#if defined(RTLD_DEFAULT)
   bool success;

   mtx_lock(&screen->opencl_func_mutex);

   if (dri2_is_opencl_interop_loaded_locked(screen)) {
      mtx_unlock(&screen->opencl_func_mutex);
      return true;
   }

   screen->opencl_dri_event_add_ref =
      dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
   screen->opencl_dri_event_release =
      dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
   screen->opencl_dri_event_wait =
      dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
   screen->opencl_dri_event_get_fence =
      dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");

   success = dri2_is_opencl_interop_loaded_locked(screen);
   mtx_unlock(&screen->opencl_func_mutex);
   return success;
#else
   return false;
#endif
}

struct dri2_fence {
   struct dri_screen *driscreen;
   struct pipe_fence_handle *pipe_fence;
   void *cl_event;
};

static unsigned dri2_fence_get_caps(__DRIscreen *_screen)
{
   struct dri_screen *driscreen = dri_screen(_screen);
   struct pipe_screen *screen = driscreen->base.screen;
   unsigned caps = 0;

   if (screen->get_param(screen, PIPE_CAP_NATIVE_FENCE_FD))
      caps |= __DRI_FENCE_CAP_NATIVE_FD;

   return caps;
}

static void *
dri2_create_fence(__DRIcontext *_ctx)
{
   struct st_context_iface *stapi = dri_context(_ctx)->st;
   struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);

   if (!fence)
      return NULL;

   stapi->flush(stapi, 0, &fence->pipe_fence, NULL, NULL);

   if (!fence->pipe_fence) {
      FREE(fence);
      return NULL;
   }

   fence->driscreen = dri_screen(_ctx->driScreenPriv);
   return fence;
}

static void *
dri2_create_fence_fd(__DRIcontext *_ctx, int fd)
{
   struct st_context_iface *stapi = dri_context(_ctx)->st;
   struct pipe_context *ctx = stapi->pipe;
   struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);

   if (fd == -1) {
      /* exporting driver created fence, flush: */
      stapi->flush(stapi, ST_FLUSH_FENCE_FD, &fence->pipe_fence, NULL, NULL);
   } else {
      /* importing a foreign fence fd: */
      ctx->create_fence_fd(ctx, &fence->pipe_fence, fd, PIPE_FD_TYPE_NATIVE_SYNC);
   }
   if (!fence->pipe_fence) {
      FREE(fence);
      return NULL;
   }

   fence->driscreen = dri_screen(_ctx->driScreenPriv);
   return fence;
}

static int
dri2_get_fence_fd(__DRIscreen *_screen, void *_fence)
{
   struct dri_screen *driscreen = dri_screen(_screen);
   struct pipe_screen *screen = driscreen->base.screen;
   struct dri2_fence *fence = (struct dri2_fence*)_fence;

   return screen->fence_get_fd(screen, fence->pipe_fence);
}

static void *
dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
{
   struct dri_screen *driscreen = dri_screen(_screen);
   struct dri2_fence *fence;

   if (!dri2_load_opencl_interop(driscreen))
      return NULL;

   fence = CALLOC_STRUCT(dri2_fence);
   if (!fence)
      return NULL;

   fence->cl_event = (void*)cl_event;

   if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
      free(fence);
      return NULL;
   }

   fence->driscreen = driscreen;
   return fence;
}

static void
dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
{
   struct dri_screen *driscreen = dri_screen(_screen);
   struct pipe_screen *screen = driscreen->base.screen;
   struct dri2_fence *fence = (struct dri2_fence*)_fence;

   if (fence->pipe_fence)
      screen->fence_reference(screen, &fence->pipe_fence, NULL);
   else if (fence->cl_event)
      driscreen->opencl_dri_event_release(fence->cl_event);
   else
      assert(0);

   FREE(fence);
}

static GLboolean
dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
                      uint64_t timeout)
{
   struct dri2_fence *fence = (struct dri2_fence*)_fence;
   struct dri_screen *driscreen = fence->driscreen;
   struct pipe_screen *screen = driscreen->base.screen;

   /* No need to flush. The context was flushed when the fence was created. */

   if (fence->pipe_fence)
      return screen->fence_finish(screen, NULL, fence->pipe_fence, timeout);
   else if (fence->cl_event) {
      struct pipe_fence_handle *pipe_fence =
         driscreen->opencl_dri_event_get_fence(fence->cl_event);

      if (pipe_fence)
         return screen->fence_finish(screen, NULL, pipe_fence, timeout);
      else
         return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
   }
   else {
      assert(0);
      return false;
   }
}

static void
dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
{
   struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
   struct dri2_fence *fence = (struct dri2_fence*)_fence;

   /* We might be called here with a NULL fence as a result of WaitSyncKHR
    * on a EGL_KHR_reusable_sync fence. Nothing to do here in such case.
    */
   if (!fence)
      return;

   if (ctx->fence_server_sync)
      ctx->fence_server_sync(ctx, fence->pipe_fence);
}

const __DRI2fenceExtension dri2FenceExtension = {
   .base = { __DRI2_FENCE, 2 },

   .create_fence = dri2_create_fence,
   .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
   .destroy_fence = dri2_destroy_fence,
   .client_wait_sync = dri2_client_wait_sync,
   .server_wait_sync = dri2_server_wait_sync,
   .get_capabilities = dri2_fence_get_caps,
   .create_fence_fd = dri2_create_fence_fd,
   .get_fence_fd = dri2_get_fence_fd,
};

__DRIimage *
dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
{
   const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
   __DRIimage *img;

   if (!loader->lookupEGLImage)
      return NULL;

   img = loader->lookupEGLImage(screen->sPriv,
				handle, screen->sPriv->loaderPrivate);

   return img;
}

__DRIimage *
dri2_create_image_from_renderbuffer2(__DRIcontext *context,
				     int renderbuffer, void *loaderPrivate,
                                     unsigned *error)
{
   struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
   struct gl_renderbuffer *rb;
   struct pipe_resource *tex;
   __DRIimage *img;

   /* Section 3.9 (EGLImage Specification and Management) of the EGL 1.5
    * specification says:
    *
    *   "If target is EGL_GL_RENDERBUFFER and buffer is not the name of a
    *    renderbuffer object, or if buffer is the name of a multisampled
    *    renderbuffer object, the error EGL_BAD_PARAMETER is generated."
    *
    *   "If target is EGL_GL_TEXTURE_2D , EGL_GL_TEXTURE_CUBE_MAP_*,
    *    EGL_GL_RENDERBUFFER or EGL_GL_TEXTURE_3D and buffer refers to the
    *    default GL texture object (0) for the corresponding GL target, the
    *    error EGL_BAD_PARAMETER is generated."
    *   (rely on _mesa_lookup_renderbuffer returning NULL in this case)
    */
   rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
   if (!rb || rb->NumSamples > 0) {
      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
      return NULL;
   }

   tex = st_get_renderbuffer_resource(rb);
   if (!tex) {
      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
      return NULL;
   }

   img = CALLOC_STRUCT(__DRIimageRec);
   if (!img) {
      *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
      return NULL;
   }

   img->dri_format = driGLFormatToImageFormat(rb->Format);
   img->loader_private = loaderPrivate;

   pipe_resource_reference(&img->texture, tex);

   *error = __DRI_IMAGE_ERROR_SUCCESS;
   return img;
}

__DRIimage *
dri2_create_image_from_renderbuffer(__DRIcontext *context,
				    int renderbuffer, void *loaderPrivate)
{
   unsigned error;
   return dri2_create_image_from_renderbuffer2(context, renderbuffer,
                                               loaderPrivate, &error);
}

void
dri2_destroy_image(__DRIimage *img)
{
   pipe_resource_reference(&img->texture, NULL);
   FREE(img);
}


__DRIimage *
dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
                         int depth, int level, unsigned *error,
                         void *loaderPrivate)
{
   __DRIimage *img;
   struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
   struct gl_texture_object *obj;
   struct pipe_resource *tex;
   GLuint face = 0;

   obj = _mesa_lookup_texture(ctx, texture);
   if (!obj || obj->Target != target) {
      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
      return NULL;
   }

   tex = st_get_texobj_resource(obj);
   if (!tex) {
      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
      return NULL;
   }

   if (target == GL_TEXTURE_CUBE_MAP)
      face = depth;

   _mesa_test_texobj_completeness(ctx, obj);
   if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
      *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
      return NULL;
   }

   if (level < obj->BaseLevel || level > obj->_MaxLevel) {
      *error = __DRI_IMAGE_ERROR_BAD_MATCH;
      return NULL;
   }

   if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
      *error = __DRI_IMAGE_ERROR_BAD_MATCH;
      return NULL;
   }

   img = CALLOC_STRUCT(__DRIimageRec);
   if (!img) {
      *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
      return NULL;
   }

   img->level = level;
   img->layer = depth;
   img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);

   img->loader_private = loaderPrivate;

   pipe_resource_reference(&img->texture, tex);

   *error = __DRI_IMAGE_ERROR_SUCCESS;
   return img;
}

static const struct dri2_format_mapping dri2_format_table[] = {
      { DRM_FORMAT_ABGR16161616F, __DRI_IMAGE_FORMAT_ABGR16161616F,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_R16G16B16A16_FLOAT, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616F, 4 } } },
      { DRM_FORMAT_XBGR16161616F, __DRI_IMAGE_FORMAT_XBGR16161616F,
        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_R16G16B16X16_FLOAT, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR16161616F, 4 } } },
      { DRM_FORMAT_ARGB2101010,   __DRI_IMAGE_FORMAT_ARGB2101010,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_B10G10R10A2_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB2101010, 4 } } },
      { DRM_FORMAT_XRGB2101010,   __DRI_IMAGE_FORMAT_XRGB2101010,
        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_B10G10R10X2_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB2101010, 4 } } },
      { DRM_FORMAT_ABGR2101010,   __DRI_IMAGE_FORMAT_ABGR2101010,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_R10G10B10A2_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010, 4 } } },
      { DRM_FORMAT_XBGR2101010,   __DRI_IMAGE_FORMAT_XBGR2101010,
        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_R10G10B10X2_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR2101010, 4 } } },
      { DRM_FORMAT_ARGB8888,      __DRI_IMAGE_FORMAT_ARGB8888,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_BGRA8888_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
      { DRM_FORMAT_ABGR8888,      __DRI_IMAGE_FORMAT_ABGR8888,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_RGBA8888_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
      { __DRI_IMAGE_FOURCC_SARGB8888,     __DRI_IMAGE_FORMAT_SARGB8,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_BGRA8888_SRGB, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
      { DRM_FORMAT_XRGB8888,      __DRI_IMAGE_FORMAT_XRGB8888,
        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_BGRX8888_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
      { DRM_FORMAT_XBGR8888,      __DRI_IMAGE_FORMAT_XBGR8888,
        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_RGBX8888_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
      { DRM_FORMAT_ARGB1555,      __DRI_IMAGE_FORMAT_ARGB1555,
        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_B5G5R5A1_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
      { DRM_FORMAT_RGB565,        __DRI_IMAGE_FORMAT_RGB565,
        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_B5G6R5_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
      { DRM_FORMAT_R8,            __DRI_IMAGE_FORMAT_R8,
        __DRI_IMAGE_COMPONENTS_R,         PIPE_FORMAT_R8_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
      { DRM_FORMAT_R16,           __DRI_IMAGE_FORMAT_R16,
        __DRI_IMAGE_COMPONENTS_R,         PIPE_FORMAT_R16_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
      { DRM_FORMAT_GR88,          __DRI_IMAGE_FORMAT_GR88,
        __DRI_IMAGE_COMPONENTS_RG,        PIPE_FORMAT_RG88_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
      { DRM_FORMAT_GR1616,        __DRI_IMAGE_FORMAT_GR1616,
        __DRI_IMAGE_COMPONENTS_RG,        PIPE_FORMAT_RG1616_UNORM, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },

      { DRM_FORMAT_YUV410, __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YUV411, __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YUV420,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YUV422,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YUV444,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },

      { DRM_FORMAT_YVU410,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YVU411,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YVU420,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YVU422,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
      { DRM_FORMAT_YVU444,        __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV, 3,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },

      { DRM_FORMAT_NV12,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_NV12, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },

      { DRM_FORMAT_P010,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_P016, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 2 },
          { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616, 4 } } },
      { DRM_FORMAT_P012,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_P016, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 2 },
          { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616, 4 } } },
      { DRM_FORMAT_P016,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_P016, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 2 },
          { 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616, 4 } } },

      { DRM_FORMAT_NV16,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_NV12, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
          { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },

      { DRM_FORMAT_AYUV,      __DRI_IMAGE_FORMAT_ABGR8888,
        __DRI_IMAGE_COMPONENTS_AYUV,      PIPE_FORMAT_AYUV, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
      { DRM_FORMAT_XYUV8888,      __DRI_IMAGE_FORMAT_XBGR8888,
        __DRI_IMAGE_COMPONENTS_XYUV,      PIPE_FORMAT_XYUV, 1,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 } } },

      /* For YUYV and UYVY buffers, we set up two overlapping DRI images
       * and treat them as planar buffers in the compositors.
       * Plane 0 is GR88 and samples YU or YV pairs and places Y into
       * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
       * clusters and places pairs and places U into the G component and
       * V into A.  This lets the texture sampler interpolate the Y
       * components correctly when sampling from plane 0, and interpolate
       * U and V correctly when sampling from plane 1. */
      { DRM_FORMAT_YUYV,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_XUXV,    PIPE_FORMAT_YUYV, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
          { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
      { DRM_FORMAT_UYVY,          __DRI_IMAGE_FORMAT_NONE,
        __DRI_IMAGE_COMPONENTS_Y_UXVX,    PIPE_FORMAT_UYVY, 2,
        { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
          { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } }
};

const struct dri2_format_mapping *
dri2_get_mapping_by_fourcc(int fourcc)
{
   for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
      if (dri2_format_table[i].dri_fourcc == fourcc)
         return &dri2_format_table[i];
   }

   return NULL;
}

const struct dri2_format_mapping *
dri2_get_mapping_by_format(int format)
{
   for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
      if (dri2_format_table[i].dri_format == format)
         return &dri2_format_table[i];
   }

   return NULL;
}

enum pipe_format
dri2_get_pipe_format_for_dri_format(int format)
{
   for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
      if (dri2_format_table[i].dri_format == format)
         return dri2_format_table[i].pipe_format;
   }

   return PIPE_FORMAT_NONE;
}

boolean
dri2_yuv_dma_buf_supported(struct dri_screen *screen,
                           const struct dri2_format_mapping *map)
{
   struct pipe_screen *pscreen = screen->base.screen;

   for (unsigned i = 0; i < map->nplanes; i++) {
      if (!pscreen->is_format_supported(pscreen,
            dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format),
            screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW))
         return false;
   }
   return true;
}

boolean
dri2_query_dma_buf_formats(__DRIscreen *_screen, int max, int *formats,
                           int *count)
{
   struct dri_screen *screen = dri_screen(_screen);
   struct pipe_screen *pscreen = screen->base.screen;
   int i, j;

   for (i = 0, j = 0; (i < ARRAY_SIZE(dri2_format_table)) &&
         (j < max || max == 0); i++) {
      const struct dri2_format_mapping *map = &dri2_format_table[i];

      /* The sRGB format is not a real FourCC as defined by drm_fourcc.h, so we
       * must not leak it out to clients.
       */
      if (dri2_format_table[i].dri_fourcc == __DRI_IMAGE_FOURCC_SARGB8888)
         continue;

      if (pscreen->is_format_supported(pscreen, map->pipe_format,
                                       screen->target, 0, 0,
                                       PIPE_BIND_RENDER_TARGET) ||
          pscreen->is_format_supported(pscreen, map->pipe_format,
                                       screen->target, 0, 0,
                                       PIPE_BIND_SAMPLER_VIEW) ||
          dri2_yuv_dma_buf_supported(screen, map)) {
         if (j < max)
            formats[j] = map->dri_fourcc;
         j++;
      }
   }
   *count = j;
   return true;
}

/* vim: set sw=3 ts=8 sts=3 expandtab: */