Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/frontends/dri/dri_helpers.c
4565 views
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
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
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
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 *
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 *
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
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 *
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
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
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
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 *
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
__DRIimage *
257
dri2_create_image_from_renderbuffer2(__DRIcontext *context,
258
int renderbuffer, void *loaderPrivate,
259
unsigned *error)
260
{
261
struct st_context *st_ctx = (struct st_context *)dri_context(context)->st;
262
struct gl_context *ctx = st_ctx->ctx;
263
struct pipe_context *p_ctx = st_ctx->pipe;
264
struct gl_renderbuffer *rb;
265
struct pipe_resource *tex;
266
__DRIimage *img;
267
268
/* Section 3.9 (EGLImage Specification and Management) of the EGL 1.5
269
* specification says:
270
*
271
* "If target is EGL_GL_RENDERBUFFER and buffer is not the name of a
272
* renderbuffer object, or if buffer is the name of a multisampled
273
* renderbuffer object, the error EGL_BAD_PARAMETER is generated."
274
*
275
* "If target is EGL_GL_TEXTURE_2D , EGL_GL_TEXTURE_CUBE_MAP_*,
276
* EGL_GL_RENDERBUFFER or EGL_GL_TEXTURE_3D and buffer refers to the
277
* default GL texture object (0) for the corresponding GL target, the
278
* error EGL_BAD_PARAMETER is generated."
279
* (rely on _mesa_lookup_renderbuffer returning NULL in this case)
280
*/
281
rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
282
if (!rb || rb->NumSamples > 0) {
283
*error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
284
return NULL;
285
}
286
287
tex = st_get_renderbuffer_resource(rb);
288
if (!tex) {
289
*error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
290
return NULL;
291
}
292
293
img = CALLOC_STRUCT(__DRIimageRec);
294
if (!img) {
295
*error = __DRI_IMAGE_ERROR_BAD_ALLOC;
296
return NULL;
297
}
298
299
img->dri_format = driGLFormatToImageFormat(rb->Format);
300
img->loader_private = loaderPrivate;
301
img->sPriv = context->driScreenPriv;
302
303
pipe_resource_reference(&img->texture, tex);
304
305
/* If the resource supports EGL_MESA_image_dma_buf_export, make sure that
306
* it's in a shareable state. Do this now while we still have the access to
307
* the context.
308
*/
309
if (dri2_get_mapping_by_format(img->dri_format))
310
p_ctx->flush_resource(p_ctx, tex);
311
312
ctx->Shared->HasExternallySharedImages = true;
313
*error = __DRI_IMAGE_ERROR_SUCCESS;
314
return img;
315
}
316
317
__DRIimage *
318
dri2_create_image_from_renderbuffer(__DRIcontext *context,
319
int renderbuffer, void *loaderPrivate)
320
{
321
unsigned error;
322
return dri2_create_image_from_renderbuffer2(context, renderbuffer,
323
loaderPrivate, &error);
324
}
325
326
void
327
dri2_destroy_image(__DRIimage *img)
328
{
329
const __DRIimageLoaderExtension *imgLoader = img->sPriv->image.loader;
330
const __DRIdri2LoaderExtension *dri2Loader = img->sPriv->dri2.loader;
331
332
if (imgLoader && imgLoader->base.version >= 4 &&
333
imgLoader->destroyLoaderImageState) {
334
imgLoader->destroyLoaderImageState(img->loader_private);
335
} else if (dri2Loader && dri2Loader->base.version >= 5 &&
336
dri2Loader->destroyLoaderImageState) {
337
dri2Loader->destroyLoaderImageState(img->loader_private);
338
}
339
340
pipe_resource_reference(&img->texture, NULL);
341
FREE(img);
342
}
343
344
345
__DRIimage *
346
dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
347
int depth, int level, unsigned *error,
348
void *loaderPrivate)
349
{
350
__DRIimage *img;
351
struct st_context *st_ctx = (struct st_context *)dri_context(context)->st;
352
struct gl_context *ctx = st_ctx->ctx;
353
struct pipe_context *p_ctx = st_ctx->pipe;
354
struct gl_texture_object *obj;
355
struct pipe_resource *tex;
356
GLuint face = 0;
357
358
obj = _mesa_lookup_texture(ctx, texture);
359
if (!obj || obj->Target != target) {
360
*error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
361
return NULL;
362
}
363
364
tex = st_get_texobj_resource(obj);
365
if (!tex) {
366
*error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
367
return NULL;
368
}
369
370
if (target == GL_TEXTURE_CUBE_MAP)
371
face = depth;
372
373
_mesa_test_texobj_completeness(ctx, obj);
374
if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
375
*error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
376
return NULL;
377
}
378
379
if (level < obj->Attrib.BaseLevel || level > obj->_MaxLevel) {
380
*error = __DRI_IMAGE_ERROR_BAD_MATCH;
381
return NULL;
382
}
383
384
if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
385
*error = __DRI_IMAGE_ERROR_BAD_MATCH;
386
return NULL;
387
}
388
389
img = CALLOC_STRUCT(__DRIimageRec);
390
if (!img) {
391
*error = __DRI_IMAGE_ERROR_BAD_ALLOC;
392
return NULL;
393
}
394
395
img->level = level;
396
img->layer = depth;
397
img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
398
399
img->loader_private = loaderPrivate;
400
img->sPriv = context->driScreenPriv;
401
402
pipe_resource_reference(&img->texture, tex);
403
404
/* If the resource supports EGL_MESA_image_dma_buf_export, make sure that
405
* it's in a shareable state. Do this now while we still have the access to
406
* the context.
407
*/
408
if (dri2_get_mapping_by_format(img->dri_format))
409
p_ctx->flush_resource(p_ctx, tex);
410
411
ctx->Shared->HasExternallySharedImages = true;
412
*error = __DRI_IMAGE_ERROR_SUCCESS;
413
return img;
414
}
415
416
static const struct dri2_format_mapping dri2_format_table[] = {
417
{ DRM_FORMAT_ABGR16161616F, __DRI_IMAGE_FORMAT_ABGR16161616F,
418
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R16G16B16A16_FLOAT, 1,
419
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616F } } },
420
{ DRM_FORMAT_XBGR16161616F, __DRI_IMAGE_FORMAT_XBGR16161616F,
421
__DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_R16G16B16X16_FLOAT, 1,
422
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR16161616F } } },
423
{ __DRI_IMAGE_FOURCC_RGBA16161616, __DRI_IMAGE_FORMAT_ABGR16161616,
424
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R16G16B16A16_UNORM, 1,
425
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
426
{ DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010,
427
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_B10G10R10A2_UNORM, 1,
428
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB2101010 } } },
429
{ DRM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010,
430
__DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_B10G10R10X2_UNORM, 1,
431
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB2101010 } } },
432
{ DRM_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ABGR2101010,
433
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_R10G10B10A2_UNORM, 1,
434
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010 } } },
435
{ DRM_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XBGR2101010,
436
__DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_R10G10B10X2_UNORM, 1,
437
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR2101010 } } },
438
{ DRM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888,
439
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_BGRA8888_UNORM, 1,
440
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
441
{ DRM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888,
442
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_RGBA8888_UNORM, 1,
443
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
444
{ __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_FORMAT_SARGB8,
445
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_BGRA8888_SRGB, 1,
446
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8 } } },
447
{ DRM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888,
448
__DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_BGRX8888_UNORM, 1,
449
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888 } } },
450
{ DRM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888,
451
__DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_RGBX8888_UNORM, 1,
452
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888 } } },
453
{ DRM_FORMAT_ARGB1555, __DRI_IMAGE_FORMAT_ARGB1555,
454
__DRI_IMAGE_COMPONENTS_RGBA, PIPE_FORMAT_B5G5R5A1_UNORM, 1,
455
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555 } } },
456
{ DRM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565,
457
__DRI_IMAGE_COMPONENTS_RGB, PIPE_FORMAT_B5G6R5_UNORM, 1,
458
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565 } } },
459
{ DRM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8,
460
__DRI_IMAGE_COMPONENTS_R, PIPE_FORMAT_R8_UNORM, 1,
461
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
462
{ DRM_FORMAT_R16, __DRI_IMAGE_FORMAT_R16,
463
__DRI_IMAGE_COMPONENTS_R, PIPE_FORMAT_R16_UNORM, 1,
464
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 } } },
465
{ DRM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88,
466
__DRI_IMAGE_COMPONENTS_RG, PIPE_FORMAT_RG88_UNORM, 1,
467
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 } } },
468
{ DRM_FORMAT_GR1616, __DRI_IMAGE_FORMAT_GR1616,
469
__DRI_IMAGE_COMPONENTS_RG, PIPE_FORMAT_RG1616_UNORM, 1,
470
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 } } },
471
472
{ DRM_FORMAT_YUV410, __DRI_IMAGE_FORMAT_NONE,
473
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
474
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
475
{ 1, 2, 2, __DRI_IMAGE_FORMAT_R8 },
476
{ 2, 2, 2, __DRI_IMAGE_FORMAT_R8 } } },
477
{ DRM_FORMAT_YUV411, __DRI_IMAGE_FORMAT_NONE,
478
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
479
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
480
{ 1, 2, 0, __DRI_IMAGE_FORMAT_R8 },
481
{ 2, 2, 0, __DRI_IMAGE_FORMAT_R8 } } },
482
{ DRM_FORMAT_YUV420, __DRI_IMAGE_FORMAT_NONE,
483
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
484
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
485
{ 1, 1, 1, __DRI_IMAGE_FORMAT_R8 },
486
{ 2, 1, 1, __DRI_IMAGE_FORMAT_R8 } } },
487
{ DRM_FORMAT_YUV422, __DRI_IMAGE_FORMAT_NONE,
488
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
489
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
490
{ 1, 1, 0, __DRI_IMAGE_FORMAT_R8 },
491
{ 2, 1, 0, __DRI_IMAGE_FORMAT_R8 } } },
492
{ DRM_FORMAT_YUV444, __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, 0, 0, __DRI_IMAGE_FORMAT_R8 },
496
{ 2, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
497
498
{ DRM_FORMAT_YVU410, __DRI_IMAGE_FORMAT_NONE,
499
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
500
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
501
{ 2, 2, 2, __DRI_IMAGE_FORMAT_R8 },
502
{ 1, 2, 2, __DRI_IMAGE_FORMAT_R8 } } },
503
{ DRM_FORMAT_YVU411, __DRI_IMAGE_FORMAT_NONE,
504
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
505
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
506
{ 2, 2, 0, __DRI_IMAGE_FORMAT_R8 },
507
{ 1, 2, 0, __DRI_IMAGE_FORMAT_R8 } } },
508
{ DRM_FORMAT_YVU420, __DRI_IMAGE_FORMAT_NONE,
509
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
510
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
511
{ 2, 1, 1, __DRI_IMAGE_FORMAT_R8 },
512
{ 1, 1, 1, __DRI_IMAGE_FORMAT_R8 } } },
513
{ DRM_FORMAT_YVU422, __DRI_IMAGE_FORMAT_NONE,
514
__DRI_IMAGE_COMPONENTS_Y_U_V, PIPE_FORMAT_IYUV, 3,
515
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
516
{ 2, 1, 0, __DRI_IMAGE_FORMAT_R8 },
517
{ 1, 1, 0, __DRI_IMAGE_FORMAT_R8 } } },
518
{ DRM_FORMAT_YVU444, __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, 0, 0, __DRI_IMAGE_FORMAT_R8 },
522
{ 1, 0, 0, __DRI_IMAGE_FORMAT_R8 } } },
523
524
{ DRM_FORMAT_NV12, __DRI_IMAGE_FORMAT_NONE,
525
__DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV12, 2,
526
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
527
{ 1, 1, 1, __DRI_IMAGE_FORMAT_GR88 } } },
528
529
{ DRM_FORMAT_P010, __DRI_IMAGE_FORMAT_NONE,
530
__DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P010, 2,
531
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
532
{ 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
533
{ DRM_FORMAT_P012, __DRI_IMAGE_FORMAT_NONE,
534
__DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P012, 2,
535
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
536
{ 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
537
{ DRM_FORMAT_P016, __DRI_IMAGE_FORMAT_NONE,
538
__DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_P016, 2,
539
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R16 },
540
{ 1, 1, 1, __DRI_IMAGE_FORMAT_GR1616 } } },
541
542
{ DRM_FORMAT_NV16, __DRI_IMAGE_FORMAT_NONE,
543
__DRI_IMAGE_COMPONENTS_Y_UV, PIPE_FORMAT_NV12, 2,
544
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
545
{ 1, 1, 0, __DRI_IMAGE_FORMAT_GR88 } } },
546
547
{ DRM_FORMAT_AYUV, __DRI_IMAGE_FORMAT_ABGR8888,
548
__DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_AYUV, 1,
549
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
550
{ DRM_FORMAT_XYUV8888, __DRI_IMAGE_FORMAT_XBGR8888,
551
__DRI_IMAGE_COMPONENTS_XYUV, PIPE_FORMAT_XYUV, 1,
552
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888 } } },
553
554
{ DRM_FORMAT_Y410, __DRI_IMAGE_FORMAT_ABGR2101010,
555
__DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_Y410, 1,
556
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR2101010 } } },
557
558
/* Y412 is an unusual format. It has the same layout as Y416 (i.e.,
559
* 16-bits of physical storage per channel), but the low 4 bits of each
560
* component are unused padding. The writer is supposed to write zeros
561
* to these bits.
562
*/
563
{ DRM_FORMAT_Y412, __DRI_IMAGE_FORMAT_ABGR16161616,
564
__DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_Y412, 1,
565
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
566
{ DRM_FORMAT_Y416, __DRI_IMAGE_FORMAT_ABGR16161616,
567
__DRI_IMAGE_COMPONENTS_AYUV, PIPE_FORMAT_Y416, 1,
568
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
569
570
/* For YUYV and UYVY buffers, we set up two overlapping DRI images
571
* and treat them as planar buffers in the compositors.
572
* Plane 0 is GR88 and samples YU or YV pairs and places Y into
573
* the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
574
* clusters and places pairs and places U into the G component and
575
* V into A. This lets the texture sampler interpolate the Y
576
* components correctly when sampling from plane 0, and interpolate
577
* U and V correctly when sampling from plane 1. */
578
{ DRM_FORMAT_YUYV, __DRI_IMAGE_FORMAT_NONE,
579
__DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_YUYV, 2,
580
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
581
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888 } } },
582
{ DRM_FORMAT_UYVY, __DRI_IMAGE_FORMAT_NONE,
583
__DRI_IMAGE_COMPONENTS_Y_UXVX, PIPE_FORMAT_UYVY, 2,
584
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
585
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888 } } },
586
587
/* The Y21x formats work in a similar fashion to the YUYV and UYVY
588
* formats.
589
*/
590
{ DRM_FORMAT_Y210, __DRI_IMAGE_FORMAT_NONE,
591
__DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_Y210, 2,
592
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
593
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
594
/* Y212 is an unusual format. It has the same layout as Y216 (i.e.,
595
* 16-bits of physical storage per channel), but the low 4 bits of each
596
* component are unused padding. The writer is supposed to write zeros
597
* to these bits.
598
*/
599
{ DRM_FORMAT_Y212, __DRI_IMAGE_FORMAT_NONE,
600
__DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_Y212, 2,
601
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
602
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
603
{ DRM_FORMAT_Y216, __DRI_IMAGE_FORMAT_NONE,
604
__DRI_IMAGE_COMPONENTS_Y_XUXV, PIPE_FORMAT_Y216, 2,
605
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616 },
606
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR16161616 } } },
607
};
608
609
const struct dri2_format_mapping *
610
dri2_get_mapping_by_fourcc(int fourcc)
611
{
612
for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
613
if (dri2_format_table[i].dri_fourcc == fourcc)
614
return &dri2_format_table[i];
615
}
616
617
return NULL;
618
}
619
620
const struct dri2_format_mapping *
621
dri2_get_mapping_by_format(int format)
622
{
623
if (format == __DRI_IMAGE_FORMAT_NONE)
624
return NULL;
625
626
for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
627
if (dri2_format_table[i].dri_format == format)
628
return &dri2_format_table[i];
629
}
630
631
return NULL;
632
}
633
634
enum pipe_format
635
dri2_get_pipe_format_for_dri_format(int format)
636
{
637
for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
638
if (dri2_format_table[i].dri_format == format)
639
return dri2_format_table[i].pipe_format;
640
}
641
642
return PIPE_FORMAT_NONE;
643
}
644
645
boolean
646
dri2_yuv_dma_buf_supported(struct dri_screen *screen,
647
const struct dri2_format_mapping *map)
648
{
649
struct pipe_screen *pscreen = screen->base.screen;
650
651
for (unsigned i = 0; i < map->nplanes; i++) {
652
if (!pscreen->is_format_supported(pscreen,
653
dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format),
654
screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW))
655
return false;
656
}
657
return true;
658
}
659
660
boolean
661
dri2_query_dma_buf_formats(__DRIscreen *_screen, int max, int *formats,
662
int *count)
663
{
664
struct dri_screen *screen = dri_screen(_screen);
665
struct pipe_screen *pscreen = screen->base.screen;
666
int i, j;
667
668
for (i = 0, j = 0; (i < ARRAY_SIZE(dri2_format_table)) &&
669
(j < max || max == 0); i++) {
670
const struct dri2_format_mapping *map = &dri2_format_table[i];
671
672
/* The sRGB format is not a real FourCC as defined by drm_fourcc.h, so we
673
* must not leak it out to clients. The RGBA16161616 format isn't
674
* real either, but at some point it could be. Don't leak it out form
675
* now.
676
*/
677
if (dri2_format_table[i].dri_fourcc == __DRI_IMAGE_FOURCC_SARGB8888 ||
678
dri2_format_table[i].dri_fourcc == __DRI_IMAGE_FOURCC_RGBA16161616)
679
continue;
680
681
if (pscreen->is_format_supported(pscreen, map->pipe_format,
682
screen->target, 0, 0,
683
PIPE_BIND_RENDER_TARGET) ||
684
pscreen->is_format_supported(pscreen, map->pipe_format,
685
screen->target, 0, 0,
686
PIPE_BIND_SAMPLER_VIEW) ||
687
dri2_yuv_dma_buf_supported(screen, map)) {
688
if (j < max)
689
formats[j] = map->dri_fourcc;
690
j++;
691
}
692
}
693
*count = j;
694
return true;
695
}
696
697
/* vim: set sw=3 ts=8 sts=3 expandtab: */
698
699