Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/frontends/dri/dri2.c
4566 views
1
/*
2
* Mesa 3-D graphics library
3
*
4
* Copyright 2009, VMware, Inc.
5
* All Rights Reserved.
6
* Copyright (C) 2010 LunarG Inc.
7
*
8
* Permission is hereby granted, free of charge, to any person obtaining a
9
* copy of this software and associated documentation files (the "Software"),
10
* to deal in the Software without restriction, including without limitation
11
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
* and/or sell copies of the Software, and to permit persons to whom the
13
* Software is furnished to do so, subject to the following conditions:
14
*
15
* The above copyright notice and this permission notice shall be included
16
* in all copies or substantial portions of the Software.
17
*
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
* OTHER DEALINGS IN THE SOFTWARE.
25
*
26
* Authors:
27
* Keith Whitwell <[email protected]> Jakob Bornecrantz
28
* <[email protected]> Chia-I Wu <[email protected]>
29
*/
30
31
#include <xf86drm.h>
32
#include "GL/mesa_glinterop.h"
33
#include "util/disk_cache.h"
34
#include "util/u_memory.h"
35
#include "util/u_inlines.h"
36
#include "util/format/u_format.h"
37
#include "util/u_debug.h"
38
#include "frontend/drm_driver.h"
39
#include "state_tracker/st_cb_bufferobjects.h"
40
#include "state_tracker/st_cb_fbo.h"
41
#include "state_tracker/st_cb_texture.h"
42
#include "state_tracker/st_texture.h"
43
#include "state_tracker/st_context.h"
44
#include "pipe-loader/pipe_loader.h"
45
#include "main/bufferobj.h"
46
#include "main/texobj.h"
47
48
#include "dri_util.h"
49
50
#include "dri_helpers.h"
51
#include "dri_drawable.h"
52
#include "dri_query_renderer.h"
53
54
#include "drm-uapi/drm_fourcc.h"
55
56
struct dri2_buffer
57
{
58
__DRIbuffer base;
59
struct pipe_resource *resource;
60
};
61
62
static inline struct dri2_buffer *
63
dri2_buffer(__DRIbuffer * driBufferPriv)
64
{
65
return (struct dri2_buffer *) driBufferPriv;
66
}
67
68
/**
69
* DRI2 flush extension.
70
*/
71
static void
72
dri2_flush_drawable(__DRIdrawable *dPriv)
73
{
74
dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
75
}
76
77
static void
78
dri2_invalidate_drawable(__DRIdrawable *dPriv)
79
{
80
struct dri_drawable *drawable = dri_drawable(dPriv);
81
82
dri2InvalidateDrawable(dPriv);
83
drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
84
drawable->texture_mask = 0;
85
86
p_atomic_inc(&drawable->base.stamp);
87
}
88
89
static const __DRI2flushExtension dri2FlushExtension = {
90
.base = { __DRI2_FLUSH, 4 },
91
92
.flush = dri2_flush_drawable,
93
.invalidate = dri2_invalidate_drawable,
94
.flush_with_flags = dri_flush,
95
};
96
97
/**
98
* Retrieve __DRIbuffer from the DRI loader.
99
*/
100
static __DRIbuffer *
101
dri2_drawable_get_buffers(struct dri_drawable *drawable,
102
const enum st_attachment_type *atts,
103
unsigned *count)
104
{
105
__DRIdrawable *dri_drawable = drawable->dPriv;
106
const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
107
boolean with_format;
108
__DRIbuffer *buffers;
109
int num_buffers;
110
unsigned attachments[__DRI_BUFFER_COUNT];
111
unsigned num_attachments, i;
112
113
assert(loader);
114
assert(*count <= __DRI_BUFFER_COUNT);
115
with_format = dri_with_format(drawable->sPriv);
116
117
num_attachments = 0;
118
119
/* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
120
if (!with_format)
121
attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
122
123
for (i = 0; i < *count; i++) {
124
enum pipe_format format;
125
unsigned bind;
126
int att, depth;
127
128
dri_drawable_get_format(drawable, atts[i], &format, &bind);
129
if (format == PIPE_FORMAT_NONE)
130
continue;
131
132
switch (atts[i]) {
133
case ST_ATTACHMENT_FRONT_LEFT:
134
/* already added */
135
if (!with_format)
136
continue;
137
att = __DRI_BUFFER_FRONT_LEFT;
138
break;
139
case ST_ATTACHMENT_BACK_LEFT:
140
att = __DRI_BUFFER_BACK_LEFT;
141
break;
142
case ST_ATTACHMENT_FRONT_RIGHT:
143
att = __DRI_BUFFER_FRONT_RIGHT;
144
break;
145
case ST_ATTACHMENT_BACK_RIGHT:
146
att = __DRI_BUFFER_BACK_RIGHT;
147
break;
148
default:
149
continue;
150
}
151
152
/*
153
* In this switch statement we must support all formats that
154
* may occur as the stvis->color_format.
155
*/
156
switch(format) {
157
case PIPE_FORMAT_R16G16B16A16_FLOAT:
158
depth = 64;
159
break;
160
case PIPE_FORMAT_R16G16B16X16_FLOAT:
161
depth = 48;
162
break;
163
case PIPE_FORMAT_B10G10R10A2_UNORM:
164
case PIPE_FORMAT_R10G10B10A2_UNORM:
165
case PIPE_FORMAT_BGRA8888_UNORM:
166
case PIPE_FORMAT_RGBA8888_UNORM:
167
depth = 32;
168
break;
169
case PIPE_FORMAT_R10G10B10X2_UNORM:
170
case PIPE_FORMAT_B10G10R10X2_UNORM:
171
depth = 30;
172
break;
173
case PIPE_FORMAT_BGRX8888_UNORM:
174
case PIPE_FORMAT_RGBX8888_UNORM:
175
depth = 24;
176
break;
177
case PIPE_FORMAT_B5G6R5_UNORM:
178
depth = 16;
179
break;
180
default:
181
depth = util_format_get_blocksizebits(format);
182
assert(!"Unexpected format in dri2_drawable_get_buffers()");
183
}
184
185
attachments[num_attachments++] = att;
186
if (with_format) {
187
attachments[num_attachments++] = depth;
188
}
189
}
190
191
if (with_format) {
192
num_attachments /= 2;
193
buffers = loader->getBuffersWithFormat(dri_drawable,
194
&dri_drawable->w, &dri_drawable->h,
195
attachments, num_attachments,
196
&num_buffers, dri_drawable->loaderPrivate);
197
}
198
else {
199
buffers = loader->getBuffers(dri_drawable,
200
&dri_drawable->w, &dri_drawable->h,
201
attachments, num_attachments,
202
&num_buffers, dri_drawable->loaderPrivate);
203
}
204
205
if (buffers)
206
*count = num_buffers;
207
208
return buffers;
209
}
210
211
static bool
212
dri_image_drawable_get_buffers(struct dri_drawable *drawable,
213
struct __DRIimageList *images,
214
const enum st_attachment_type *statts,
215
unsigned statts_count)
216
{
217
__DRIdrawable *dPriv = drawable->dPriv;
218
__DRIscreen *sPriv = drawable->sPriv;
219
unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
220
enum pipe_format pf;
221
uint32_t buffer_mask = 0;
222
unsigned i, bind;
223
224
for (i = 0; i < statts_count; i++) {
225
dri_drawable_get_format(drawable, statts[i], &pf, &bind);
226
if (pf == PIPE_FORMAT_NONE)
227
continue;
228
229
switch (statts[i]) {
230
case ST_ATTACHMENT_FRONT_LEFT:
231
buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
232
break;
233
case ST_ATTACHMENT_BACK_LEFT:
234
buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
235
break;
236
default:
237
continue;
238
}
239
240
switch (pf) {
241
case PIPE_FORMAT_R16G16B16A16_FLOAT:
242
image_format = __DRI_IMAGE_FORMAT_ABGR16161616F;
243
break;
244
case PIPE_FORMAT_R16G16B16X16_FLOAT:
245
image_format = __DRI_IMAGE_FORMAT_XBGR16161616F;
246
break;
247
case PIPE_FORMAT_B5G5R5A1_UNORM:
248
image_format = __DRI_IMAGE_FORMAT_ARGB1555;
249
break;
250
case PIPE_FORMAT_B5G6R5_UNORM:
251
image_format = __DRI_IMAGE_FORMAT_RGB565;
252
break;
253
case PIPE_FORMAT_BGRX8888_UNORM:
254
image_format = __DRI_IMAGE_FORMAT_XRGB8888;
255
break;
256
case PIPE_FORMAT_BGRA8888_UNORM:
257
image_format = __DRI_IMAGE_FORMAT_ARGB8888;
258
break;
259
case PIPE_FORMAT_RGBX8888_UNORM:
260
image_format = __DRI_IMAGE_FORMAT_XBGR8888;
261
break;
262
case PIPE_FORMAT_RGBA8888_UNORM:
263
image_format = __DRI_IMAGE_FORMAT_ABGR8888;
264
break;
265
case PIPE_FORMAT_B10G10R10X2_UNORM:
266
image_format = __DRI_IMAGE_FORMAT_XRGB2101010;
267
break;
268
case PIPE_FORMAT_B10G10R10A2_UNORM:
269
image_format = __DRI_IMAGE_FORMAT_ARGB2101010;
270
break;
271
case PIPE_FORMAT_R10G10B10X2_UNORM:
272
image_format = __DRI_IMAGE_FORMAT_XBGR2101010;
273
break;
274
case PIPE_FORMAT_R10G10B10A2_UNORM:
275
image_format = __DRI_IMAGE_FORMAT_ABGR2101010;
276
break;
277
default:
278
image_format = __DRI_IMAGE_FORMAT_NONE;
279
break;
280
}
281
}
282
283
return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
284
(uint32_t *) &drawable->base.stamp,
285
dPriv->loaderPrivate, buffer_mask,
286
images);
287
}
288
289
static __DRIbuffer *
290
dri2_allocate_buffer(__DRIscreen *sPriv,
291
unsigned attachment, unsigned format,
292
int width, int height)
293
{
294
struct dri_screen *screen = dri_screen(sPriv);
295
struct dri2_buffer *buffer;
296
struct pipe_resource templ;
297
enum pipe_format pf;
298
unsigned bind = 0;
299
struct winsys_handle whandle;
300
301
switch (attachment) {
302
case __DRI_BUFFER_FRONT_LEFT:
303
case __DRI_BUFFER_FAKE_FRONT_LEFT:
304
bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
305
break;
306
case __DRI_BUFFER_BACK_LEFT:
307
bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
308
break;
309
case __DRI_BUFFER_DEPTH:
310
case __DRI_BUFFER_DEPTH_STENCIL:
311
case __DRI_BUFFER_STENCIL:
312
bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
313
break;
314
}
315
316
/* because we get the handle and stride */
317
bind |= PIPE_BIND_SHARED;
318
319
switch (format) {
320
case 64:
321
pf = PIPE_FORMAT_R16G16B16A16_FLOAT;
322
break;
323
case 48:
324
pf = PIPE_FORMAT_R16G16B16X16_FLOAT;
325
break;
326
case 32:
327
pf = PIPE_FORMAT_BGRA8888_UNORM;
328
break;
329
case 30:
330
pf = PIPE_FORMAT_B10G10R10X2_UNORM;
331
break;
332
case 24:
333
pf = PIPE_FORMAT_BGRX8888_UNORM;
334
break;
335
case 16:
336
pf = PIPE_FORMAT_Z16_UNORM;
337
break;
338
default:
339
return NULL;
340
}
341
342
buffer = CALLOC_STRUCT(dri2_buffer);
343
if (!buffer)
344
return NULL;
345
346
memset(&templ, 0, sizeof(templ));
347
templ.bind = bind;
348
templ.format = pf;
349
templ.target = PIPE_TEXTURE_2D;
350
templ.last_level = 0;
351
templ.width0 = width;
352
templ.height0 = height;
353
templ.depth0 = 1;
354
templ.array_size = 1;
355
356
buffer->resource =
357
screen->base.screen->resource_create(screen->base.screen, &templ);
358
if (!buffer->resource) {
359
FREE(buffer);
360
return NULL;
361
}
362
363
memset(&whandle, 0, sizeof(whandle));
364
if (screen->can_share_buffer)
365
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
366
else
367
whandle.type = WINSYS_HANDLE_TYPE_KMS;
368
369
screen->base.screen->resource_get_handle(screen->base.screen, NULL,
370
buffer->resource, &whandle,
371
PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
372
373
buffer->base.attachment = attachment;
374
buffer->base.name = whandle.handle;
375
buffer->base.cpp = util_format_get_blocksize(pf);
376
buffer->base.pitch = whandle.stride;
377
378
return &buffer->base;
379
}
380
381
static void
382
dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
383
{
384
struct dri2_buffer *buffer = dri2_buffer(bPriv);
385
386
pipe_resource_reference(&buffer->resource, NULL);
387
FREE(buffer);
388
}
389
390
/*
391
* Backend functions for st_framebuffer interface.
392
*/
393
394
static void
395
dri2_allocate_textures(struct dri_context *ctx,
396
struct dri_drawable *drawable,
397
const enum st_attachment_type *statts,
398
unsigned statts_count)
399
{
400
__DRIscreen *sPriv = drawable->sPriv;
401
__DRIdrawable *dri_drawable = drawable->dPriv;
402
struct dri_screen *screen = dri_screen(sPriv);
403
struct pipe_resource templ;
404
boolean alloc_depthstencil = FALSE;
405
unsigned i, j, bind;
406
const __DRIimageLoaderExtension *image = sPriv->image.loader;
407
/* Image specific variables */
408
struct __DRIimageList images;
409
/* Dri2 specific variables */
410
__DRIbuffer *buffers = NULL;
411
struct winsys_handle whandle;
412
unsigned num_buffers = statts_count;
413
414
assert(num_buffers <= __DRI_BUFFER_COUNT);
415
416
/* First get the buffers from the loader */
417
if (image) {
418
if (!dri_image_drawable_get_buffers(drawable, &images,
419
statts, statts_count))
420
return;
421
}
422
else {
423
buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
424
if (!buffers || (drawable->old_num == num_buffers &&
425
drawable->old_w == dri_drawable->w &&
426
drawable->old_h == dri_drawable->h &&
427
memcmp(drawable->old, buffers,
428
sizeof(__DRIbuffer) * num_buffers) == 0))
429
return;
430
}
431
432
/* Second clean useless resources*/
433
434
/* See if we need a depth-stencil buffer. */
435
for (i = 0; i < statts_count; i++) {
436
if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
437
alloc_depthstencil = TRUE;
438
break;
439
}
440
}
441
442
/* Delete the resources we won't need. */
443
for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
444
/* Don't delete the depth-stencil buffer, we can reuse it. */
445
if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
446
continue;
447
448
/* Flush the texture before unreferencing, so that other clients can
449
* see what the driver has rendered.
450
*/
451
if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
452
struct pipe_context *pipe = ctx->st->pipe;
453
pipe->flush_resource(pipe, drawable->textures[i]);
454
}
455
456
pipe_resource_reference(&drawable->textures[i], NULL);
457
}
458
459
if (drawable->stvis.samples > 1) {
460
for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
461
boolean del = TRUE;
462
463
/* Don't delete MSAA resources for the attachments which are enabled,
464
* we can reuse them. */
465
for (j = 0; j < statts_count; j++) {
466
if (i == statts[j]) {
467
del = FALSE;
468
break;
469
}
470
}
471
472
if (del) {
473
pipe_resource_reference(&drawable->msaa_textures[i], NULL);
474
}
475
}
476
}
477
478
/* Third use the buffers retrieved to fill the drawable info */
479
480
memset(&templ, 0, sizeof(templ));
481
templ.target = screen->target;
482
templ.last_level = 0;
483
templ.depth0 = 1;
484
templ.array_size = 1;
485
486
if (image) {
487
if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
488
struct pipe_resource **buf =
489
&drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
490
struct pipe_resource *texture = images.front->texture;
491
492
dri_drawable->w = texture->width0;
493
dri_drawable->h = texture->height0;
494
495
pipe_resource_reference(buf, texture);
496
}
497
498
if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
499
struct pipe_resource **buf =
500
&drawable->textures[ST_ATTACHMENT_BACK_LEFT];
501
struct pipe_resource *texture = images.back->texture;
502
503
dri_drawable->w = texture->width0;
504
dri_drawable->h = texture->height0;
505
506
pipe_resource_reference(buf, texture);
507
}
508
509
if (images.image_mask & __DRI_IMAGE_BUFFER_SHARED) {
510
struct pipe_resource **buf =
511
&drawable->textures[ST_ATTACHMENT_BACK_LEFT];
512
struct pipe_resource *texture = images.back->texture;
513
514
dri_drawable->w = texture->width0;
515
dri_drawable->h = texture->height0;
516
517
pipe_resource_reference(buf, texture);
518
519
ctx->is_shared_buffer_bound = true;
520
} else {
521
ctx->is_shared_buffer_bound = false;
522
}
523
524
/* Note: if there is both a back and a front buffer,
525
* then they have the same size.
526
*/
527
templ.width0 = dri_drawable->w;
528
templ.height0 = dri_drawable->h;
529
}
530
else {
531
memset(&whandle, 0, sizeof(whandle));
532
533
/* Process DRI-provided buffers and get pipe_resources. */
534
for (i = 0; i < num_buffers; i++) {
535
__DRIbuffer *buf = &buffers[i];
536
enum st_attachment_type statt;
537
enum pipe_format format;
538
539
switch (buf->attachment) {
540
case __DRI_BUFFER_FRONT_LEFT:
541
if (!screen->auto_fake_front) {
542
continue; /* invalid attachment */
543
}
544
FALLTHROUGH;
545
case __DRI_BUFFER_FAKE_FRONT_LEFT:
546
statt = ST_ATTACHMENT_FRONT_LEFT;
547
break;
548
case __DRI_BUFFER_BACK_LEFT:
549
statt = ST_ATTACHMENT_BACK_LEFT;
550
break;
551
default:
552
continue; /* invalid attachment */
553
}
554
555
dri_drawable_get_format(drawable, statt, &format, &bind);
556
if (format == PIPE_FORMAT_NONE)
557
continue;
558
559
/* dri2_drawable_get_buffers has already filled dri_drawable->w
560
* and dri_drawable->h */
561
templ.width0 = dri_drawable->w;
562
templ.height0 = dri_drawable->h;
563
templ.format = format;
564
templ.bind = bind;
565
whandle.handle = buf->name;
566
whandle.stride = buf->pitch;
567
whandle.offset = 0;
568
whandle.format = format;
569
whandle.modifier = DRM_FORMAT_MOD_INVALID;
570
if (screen->can_share_buffer)
571
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
572
else
573
whandle.type = WINSYS_HANDLE_TYPE_KMS;
574
drawable->textures[statt] =
575
screen->base.screen->resource_from_handle(screen->base.screen,
576
&templ, &whandle,
577
PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
578
assert(drawable->textures[statt]);
579
}
580
}
581
582
/* Allocate private MSAA colorbuffers. */
583
if (drawable->stvis.samples > 1) {
584
for (i = 0; i < statts_count; i++) {
585
enum st_attachment_type statt = statts[i];
586
587
if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
588
continue;
589
590
if (drawable->textures[statt]) {
591
templ.format = drawable->textures[statt]->format;
592
templ.bind = drawable->textures[statt]->bind &
593
~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
594
templ.nr_samples = drawable->stvis.samples;
595
templ.nr_storage_samples = drawable->stvis.samples;
596
597
/* Try to reuse the resource.
598
* (the other resource parameters should be constant)
599
*/
600
if (!drawable->msaa_textures[statt] ||
601
drawable->msaa_textures[statt]->width0 != templ.width0 ||
602
drawable->msaa_textures[statt]->height0 != templ.height0) {
603
/* Allocate a new one. */
604
pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
605
606
drawable->msaa_textures[statt] =
607
screen->base.screen->resource_create(screen->base.screen,
608
&templ);
609
assert(drawable->msaa_textures[statt]);
610
611
/* If there are any MSAA resources, we should initialize them
612
* such that they contain the same data as the single-sample
613
* resources we just got from the X server.
614
*
615
* The reason for this is that the gallium frontend (and
616
* therefore the app) can access the MSAA resources only.
617
* The single-sample resources are not exposed
618
* to the gallium frontend.
619
*
620
*/
621
dri_pipe_blit(ctx->st->pipe,
622
drawable->msaa_textures[statt],
623
drawable->textures[statt]);
624
}
625
}
626
else {
627
pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
628
}
629
}
630
}
631
632
/* Allocate a private depth-stencil buffer. */
633
if (alloc_depthstencil) {
634
enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
635
struct pipe_resource **zsbuf;
636
enum pipe_format format;
637
unsigned bind;
638
639
dri_drawable_get_format(drawable, statt, &format, &bind);
640
641
if (format) {
642
templ.format = format;
643
templ.bind = bind & ~PIPE_BIND_SHARED;
644
645
if (drawable->stvis.samples > 1) {
646
templ.nr_samples = drawable->stvis.samples;
647
templ.nr_storage_samples = drawable->stvis.samples;
648
zsbuf = &drawable->msaa_textures[statt];
649
}
650
else {
651
templ.nr_samples = 0;
652
templ.nr_storage_samples = 0;
653
zsbuf = &drawable->textures[statt];
654
}
655
656
/* Try to reuse the resource.
657
* (the other resource parameters should be constant)
658
*/
659
if (!*zsbuf ||
660
(*zsbuf)->width0 != templ.width0 ||
661
(*zsbuf)->height0 != templ.height0) {
662
/* Allocate a new one. */
663
pipe_resource_reference(zsbuf, NULL);
664
*zsbuf = screen->base.screen->resource_create(screen->base.screen,
665
&templ);
666
assert(*zsbuf);
667
}
668
}
669
else {
670
pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
671
pipe_resource_reference(&drawable->textures[statt], NULL);
672
}
673
}
674
675
/* For DRI2, we may get the same buffers again from the server.
676
* To prevent useless imports of gem names, drawable->old* is used
677
* to bypass the import if we get the same buffers. This doesn't apply
678
* to DRI3/Wayland, users of image.loader, since the buffer is managed
679
* by the client (no import), and the back buffer is going to change
680
* at every redraw.
681
*/
682
if (!image) {
683
drawable->old_num = num_buffers;
684
drawable->old_w = dri_drawable->w;
685
drawable->old_h = dri_drawable->h;
686
memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
687
}
688
}
689
690
static bool
691
dri2_flush_frontbuffer(struct dri_context *ctx,
692
struct dri_drawable *drawable,
693
enum st_attachment_type statt)
694
{
695
__DRIdrawable *dri_drawable = drawable->dPriv;
696
const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
697
const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
698
const __DRImutableRenderBufferLoaderExtension *shared_buffer_loader =
699
drawable->sPriv->mutableRenderBuffer.loader;
700
struct pipe_context *pipe = ctx->st->pipe;
701
struct pipe_fence_handle *fence = NULL;
702
int fence_fd = -1;
703
704
/* We need to flush for front buffer rendering when either we're using the
705
* front buffer at the GL API level, or when EGL_KHR_mutable_render_buffer
706
* has redirected GL_BACK to the front buffer.
707
*/
708
if (statt != ST_ATTACHMENT_FRONT_LEFT &&
709
(!ctx->is_shared_buffer_bound || statt != ST_ATTACHMENT_BACK_LEFT))
710
return false;
711
712
if (drawable->stvis.samples > 1) {
713
/* Resolve the buffer used for front rendering. */
714
dri_pipe_blit(ctx->st->pipe, drawable->textures[statt],
715
drawable->msaa_textures[statt]);
716
}
717
718
if (drawable->textures[statt]) {
719
pipe->flush_resource(pipe, drawable->textures[statt]);
720
}
721
722
if (ctx->is_shared_buffer_bound) {
723
/* is_shared_buffer_bound should only be true with image extension: */
724
assert(image);
725
pipe->flush(pipe, &fence, PIPE_FLUSH_FENCE_FD);
726
} else {
727
pipe->flush(pipe, NULL, 0);
728
}
729
730
if (image) {
731
image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
732
if (ctx->is_shared_buffer_bound) {
733
if (fence)
734
fence_fd = pipe->screen->fence_get_fd(pipe->screen, fence);
735
736
shared_buffer_loader->displaySharedBuffer(dri_drawable, fence_fd,
737
dri_drawable->loaderPrivate);
738
739
pipe->screen->fence_reference(pipe->screen, &fence, NULL);
740
}
741
}
742
else if (loader->flushFrontBuffer) {
743
loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
744
}
745
746
return true;
747
}
748
749
/**
750
* The struct dri_drawable flush_swapbuffers callback
751
*/
752
static void
753
dri2_flush_swapbuffers(struct dri_context *ctx,
754
struct dri_drawable *drawable)
755
{
756
__DRIdrawable *dri_drawable = drawable->dPriv;
757
const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
758
759
if (image && image->base.version >= 3 && image->flushSwapBuffers) {
760
image->flushSwapBuffers(dri_drawable, dri_drawable->loaderPrivate);
761
}
762
}
763
764
static void
765
dri2_update_tex_buffer(struct dri_drawable *drawable,
766
struct dri_context *ctx,
767
struct pipe_resource *res)
768
{
769
/* no-op */
770
}
771
772
static const struct dri2_format_mapping r8_g8b8_mapping = {
773
DRM_FORMAT_NV12,
774
__DRI_IMAGE_FORMAT_NONE,
775
__DRI_IMAGE_COMPONENTS_Y_UV,
776
PIPE_FORMAT_R8_G8B8_420_UNORM,
777
2,
778
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_R8 },
779
{ 1, 1, 1, __DRI_IMAGE_FORMAT_GR88 } }
780
};
781
782
static const struct dri2_format_mapping r8g8_r8b8_mapping = {
783
DRM_FORMAT_YUYV,
784
__DRI_IMAGE_FORMAT_NONE,
785
__DRI_IMAGE_COMPONENTS_Y_XUXV,
786
PIPE_FORMAT_R8G8_R8B8_UNORM, 2,
787
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
788
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888 } }
789
};
790
791
static const struct dri2_format_mapping g8r8_b8r8_mapping = {
792
DRM_FORMAT_UYVY,
793
__DRI_IMAGE_FORMAT_NONE,
794
__DRI_IMAGE_COMPONENTS_Y_XUXV,
795
PIPE_FORMAT_G8R8_B8R8_UNORM, 2,
796
{ { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88 },
797
{ 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888 } }
798
};
799
800
static __DRIimage *
801
dri2_create_image_from_winsys(__DRIscreen *_screen,
802
int width, int height, const struct dri2_format_mapping *map,
803
int num_handles, struct winsys_handle *whandle,
804
bool is_protected_content,
805
void *loaderPrivate)
806
{
807
struct dri_screen *screen = dri_screen(_screen);
808
struct pipe_screen *pscreen = screen->base.screen;
809
__DRIimage *img;
810
struct pipe_resource templ;
811
unsigned tex_usage = 0;
812
int i;
813
bool use_lowered = false;
814
const unsigned format_planes = util_format_get_num_planes(map->pipe_format);
815
816
if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
817
PIPE_BIND_RENDER_TARGET))
818
tex_usage |= PIPE_BIND_RENDER_TARGET;
819
if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
820
PIPE_BIND_SAMPLER_VIEW))
821
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
822
823
/* For NV12, see if we have support for sampling r8_b8g8 */
824
if (!tex_usage && map->pipe_format == PIPE_FORMAT_NV12 &&
825
pscreen->is_format_supported(pscreen, PIPE_FORMAT_R8_G8B8_420_UNORM,
826
screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW)) {
827
map = &r8_g8b8_mapping;
828
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
829
}
830
831
/* If the hardware supports R8G8_R8B8 style subsampled RGB formats, these
832
* can be used for YUYV and UYVY formats.
833
*/
834
if (!tex_usage && map->pipe_format == PIPE_FORMAT_YUYV &&
835
pscreen->is_format_supported(pscreen, PIPE_FORMAT_R8G8_R8B8_UNORM,
836
screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW)) {
837
map = &r8g8_r8b8_mapping;
838
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
839
}
840
841
if (!tex_usage && map->pipe_format == PIPE_FORMAT_UYVY &&
842
pscreen->is_format_supported(pscreen, PIPE_FORMAT_G8R8_B8R8_UNORM,
843
screen->target, 0, 0, PIPE_BIND_SAMPLER_VIEW)) {
844
map = &g8r8_b8r8_mapping;
845
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
846
}
847
848
if (!tex_usage && util_format_is_yuv(map->pipe_format)) {
849
/* YUV format sampling can be emulated by the GL gallium frontend by
850
* using multiple samplers of varying formats.
851
* If no tex_usage is set and we detect a YUV format,
852
* test for support of all planes' sampler formats and
853
* add sampler view usage.
854
*/
855
use_lowered = true;
856
if (dri2_yuv_dma_buf_supported(screen, map))
857
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
858
}
859
860
if (!tex_usage)
861
return NULL;
862
863
if (is_protected_content)
864
tex_usage |= PIPE_BIND_PROTECTED;
865
866
img = CALLOC_STRUCT(__DRIimageRec);
867
if (!img)
868
return NULL;
869
870
memset(&templ, 0, sizeof(templ));
871
templ.bind = tex_usage;
872
templ.target = screen->target;
873
templ.last_level = 0;
874
templ.depth0 = 1;
875
templ.array_size = 1;
876
877
for (i = num_handles - 1; i >= format_planes; i--) {
878
struct pipe_resource *tex;
879
880
templ.next = img->texture;
881
882
tex = pscreen->resource_from_handle(pscreen, &templ, &whandle[i],
883
PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
884
if (!tex) {
885
pipe_resource_reference(&img->texture, NULL);
886
FREE(img);
887
return NULL;
888
}
889
890
img->texture = tex;
891
}
892
893
for (i = (use_lowered ? map->nplanes : format_planes) - 1; i >= 0; i--) {
894
struct pipe_resource *tex;
895
896
templ.next = img->texture;
897
templ.width0 = width >> map->planes[i].width_shift;
898
templ.height0 = height >> map->planes[i].height_shift;
899
if (use_lowered)
900
templ.format = dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format);
901
else
902
templ.format = map->pipe_format;
903
assert(templ.format != PIPE_FORMAT_NONE);
904
905
tex = pscreen->resource_from_handle(pscreen,
906
&templ, &whandle[use_lowered ? map->planes[i].buffer_index : i],
907
PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
908
if (!tex) {
909
pipe_resource_reference(&img->texture, NULL);
910
FREE(img);
911
return NULL;
912
}
913
914
/* Reject image creation if there's an inconsistency between
915
* content protection status of tex and img.
916
*/
917
const struct driOptionCache *optionCache = &screen->dev->option_cache;
918
if (!driQueryOptionb(optionCache, "disable_protected_content_check") &&
919
(bool)(tex->bind & PIPE_BIND_PROTECTED) != is_protected_content) {
920
pipe_resource_reference(&img->texture, NULL);
921
pipe_resource_reference(&tex, NULL);
922
FREE(img);
923
return NULL;
924
}
925
926
img->texture = tex;
927
}
928
929
img->level = 0;
930
img->layer = 0;
931
img->use = 0;
932
img->loader_private = loaderPrivate;
933
img->sPriv = _screen;
934
935
return img;
936
}
937
938
static __DRIimage *
939
dri2_create_image_from_name(__DRIscreen *_screen,
940
int width, int height, int format,
941
int name, int pitch, void *loaderPrivate)
942
{
943
const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
944
struct winsys_handle whandle;
945
__DRIimage *img;
946
947
if (!map)
948
return NULL;
949
950
memset(&whandle, 0, sizeof(whandle));
951
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
952
whandle.handle = name;
953
whandle.format = map->pipe_format;
954
whandle.modifier = DRM_FORMAT_MOD_INVALID;
955
956
whandle.stride = pitch * util_format_get_blocksize(map->pipe_format);
957
958
img = dri2_create_image_from_winsys(_screen, width, height, map,
959
1, &whandle, false, loaderPrivate);
960
961
if (!img)
962
return NULL;
963
964
img->dri_components = map->dri_components;
965
img->dri_fourcc = map->dri_fourcc;
966
img->dri_format = map->dri_format;
967
968
return img;
969
}
970
971
static unsigned
972
dri2_get_modifier_num_planes(__DRIscreen *_screen,
973
uint64_t modifier, int fourcc)
974
{
975
struct pipe_screen *pscreen = dri_screen(_screen)->base.screen;
976
const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
977
978
if (!map)
979
return 0;
980
981
switch (modifier) {
982
case DRM_FORMAT_MOD_LINEAR:
983
/* DRM_FORMAT_MOD_NONE is the same as LINEAR */
984
case DRM_FORMAT_MOD_INVALID:
985
return util_format_get_num_planes(map->pipe_format);
986
default:
987
if (!pscreen->is_dmabuf_modifier_supported ||
988
!pscreen->is_dmabuf_modifier_supported(pscreen, modifier,
989
map->pipe_format, NULL)) {
990
return 0;
991
}
992
993
if (pscreen->get_dmabuf_modifier_planes) {
994
return pscreen->get_dmabuf_modifier_planes(pscreen, modifier,
995
map->pipe_format);
996
}
997
998
return map->nplanes;
999
}
1000
}
1001
1002
static __DRIimage *
1003
dri2_create_image_from_fd(__DRIscreen *_screen,
1004
int width, int height, int fourcc,
1005
uint64_t modifier, int *fds, int num_fds,
1006
int *strides, int *offsets, bool protected_content,
1007
unsigned *error, void *loaderPrivate)
1008
{
1009
struct winsys_handle whandles[4];
1010
const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
1011
__DRIimage *img = NULL;
1012
unsigned err = __DRI_IMAGE_ERROR_SUCCESS;
1013
int i;
1014
const int expected_num_fds = dri2_get_modifier_num_planes(_screen, modifier, fourcc);
1015
1016
if (!map || expected_num_fds == 0) {
1017
err = __DRI_IMAGE_ERROR_BAD_MATCH;
1018
goto exit;
1019
}
1020
1021
if (num_fds != expected_num_fds) {
1022
err = __DRI_IMAGE_ERROR_BAD_MATCH;
1023
goto exit;
1024
}
1025
1026
memset(whandles, 0, sizeof(whandles));
1027
1028
for (i = 0; i < num_fds; i++) {
1029
if (fds[i] < 0) {
1030
err = __DRI_IMAGE_ERROR_BAD_ALLOC;
1031
goto exit;
1032
}
1033
1034
whandles[i].type = WINSYS_HANDLE_TYPE_FD;
1035
whandles[i].handle = (unsigned)fds[i];
1036
whandles[i].stride = (unsigned)strides[i];
1037
whandles[i].offset = (unsigned)offsets[i];
1038
whandles[i].format = map->pipe_format;
1039
whandles[i].modifier = modifier;
1040
whandles[i].plane = i;
1041
}
1042
1043
img = dri2_create_image_from_winsys(_screen, width, height, map,
1044
num_fds, whandles, protected_content,
1045
loaderPrivate);
1046
if(img == NULL) {
1047
err = __DRI_IMAGE_ERROR_BAD_ALLOC;
1048
goto exit;
1049
}
1050
1051
img->dri_components = map->dri_components;
1052
img->dri_fourcc = fourcc;
1053
img->dri_format = map->dri_format;
1054
img->imported_dmabuf = TRUE;
1055
1056
exit:
1057
if (error)
1058
*error = err;
1059
1060
return img;
1061
}
1062
1063
static __DRIimage *
1064
dri2_create_image_common(__DRIscreen *_screen,
1065
int width, int height,
1066
int format, unsigned int use,
1067
const uint64_t *modifiers,
1068
const unsigned count,
1069
void *loaderPrivate)
1070
{
1071
const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
1072
struct dri_screen *screen = dri_screen(_screen);
1073
struct pipe_screen *pscreen = screen->base.screen;
1074
__DRIimage *img;
1075
struct pipe_resource templ;
1076
unsigned tex_usage = 0;
1077
1078
if (!map)
1079
return NULL;
1080
1081
if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target,
1082
0, 0, PIPE_BIND_RENDER_TARGET))
1083
tex_usage |= PIPE_BIND_RENDER_TARGET;
1084
if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target,
1085
0, 0, PIPE_BIND_SAMPLER_VIEW))
1086
tex_usage |= PIPE_BIND_SAMPLER_VIEW;
1087
1088
if (!tex_usage)
1089
return NULL;
1090
1091
if (use & __DRI_IMAGE_USE_SCANOUT)
1092
tex_usage |= PIPE_BIND_SCANOUT;
1093
if (use & __DRI_IMAGE_USE_SHARE)
1094
tex_usage |= PIPE_BIND_SHARED;
1095
if (use & __DRI_IMAGE_USE_LINEAR)
1096
tex_usage |= PIPE_BIND_LINEAR;
1097
if (use & __DRI_IMAGE_USE_CURSOR) {
1098
if (width != 64 || height != 64)
1099
return NULL;
1100
tex_usage |= PIPE_BIND_CURSOR;
1101
}
1102
if (use & __DRI_IMAGE_USE_PROTECTED)
1103
tex_usage |= PIPE_BIND_PROTECTED;
1104
1105
img = CALLOC_STRUCT(__DRIimageRec);
1106
if (!img)
1107
return NULL;
1108
1109
memset(&templ, 0, sizeof(templ));
1110
templ.bind = tex_usage;
1111
templ.format = map->pipe_format;
1112
templ.target = PIPE_TEXTURE_2D;
1113
templ.last_level = 0;
1114
templ.width0 = width;
1115
templ.height0 = height;
1116
templ.depth0 = 1;
1117
templ.array_size = 1;
1118
1119
if (modifiers)
1120
img->texture =
1121
screen->base.screen
1122
->resource_create_with_modifiers(screen->base.screen,
1123
&templ,
1124
modifiers,
1125
count);
1126
else
1127
img->texture =
1128
screen->base.screen->resource_create(screen->base.screen, &templ);
1129
if (!img->texture) {
1130
FREE(img);
1131
return NULL;
1132
}
1133
1134
img->level = 0;
1135
img->layer = 0;
1136
img->dri_format = format;
1137
img->dri_fourcc = map->dri_fourcc;
1138
img->dri_components = 0;
1139
img->use = use;
1140
1141
img->loader_private = loaderPrivate;
1142
img->sPriv = _screen;
1143
return img;
1144
}
1145
1146
static __DRIimage *
1147
dri2_create_image(__DRIscreen *_screen,
1148
int width, int height, int format,
1149
unsigned int use, void *loaderPrivate)
1150
{
1151
return dri2_create_image_common(_screen, width, height, format, use,
1152
NULL /* modifiers */, 0 /* count */,
1153
loaderPrivate);
1154
}
1155
1156
static __DRIimage *
1157
dri2_create_image_with_modifiers(__DRIscreen *dri_screen,
1158
int width, int height, int format,
1159
const uint64_t *modifiers,
1160
const unsigned count,
1161
void *loaderPrivate)
1162
{
1163
return dri2_create_image_common(dri_screen, width, height, format,
1164
__DRI_IMAGE_USE_SHARE, modifiers, count,
1165
loaderPrivate);
1166
}
1167
1168
static __DRIimage *
1169
dri2_create_image_with_modifiers2(__DRIscreen *dri_screen,
1170
int width, int height, int format,
1171
const uint64_t *modifiers,
1172
const unsigned count, unsigned int use,
1173
void *loaderPrivate)
1174
{
1175
return dri2_create_image_common(dri_screen, width, height, format, use,
1176
modifiers, count, loaderPrivate);
1177
}
1178
1179
static bool
1180
dri2_query_image_common(__DRIimage *image, int attrib, int *value)
1181
{
1182
switch (attrib) {
1183
case __DRI_IMAGE_ATTRIB_FORMAT:
1184
*value = image->dri_format;
1185
return true;
1186
case __DRI_IMAGE_ATTRIB_WIDTH:
1187
*value = image->texture->width0;
1188
return true;
1189
case __DRI_IMAGE_ATTRIB_HEIGHT:
1190
*value = image->texture->height0;
1191
return true;
1192
case __DRI_IMAGE_ATTRIB_COMPONENTS:
1193
if (image->dri_components == 0)
1194
return false;
1195
*value = image->dri_components;
1196
return true;
1197
case __DRI_IMAGE_ATTRIB_FOURCC:
1198
if (image->dri_fourcc) {
1199
*value = image->dri_fourcc;
1200
} else {
1201
const struct dri2_format_mapping *map;
1202
1203
map = dri2_get_mapping_by_format(image->dri_format);
1204
if (!map)
1205
return false;
1206
1207
*value = map->dri_fourcc;
1208
}
1209
return true;
1210
default:
1211
return false;
1212
}
1213
}
1214
1215
static bool
1216
dri2_query_image_by_resource_handle(__DRIimage *image, int attrib, int *value)
1217
{
1218
struct pipe_screen *pscreen = image->texture->screen;
1219
struct winsys_handle whandle;
1220
struct pipe_resource *tex;
1221
unsigned usage;
1222
memset(&whandle, 0, sizeof(whandle));
1223
whandle.plane = image->plane;
1224
int i;
1225
1226
switch (attrib) {
1227
case __DRI_IMAGE_ATTRIB_STRIDE:
1228
case __DRI_IMAGE_ATTRIB_OFFSET:
1229
case __DRI_IMAGE_ATTRIB_HANDLE:
1230
whandle.type = WINSYS_HANDLE_TYPE_KMS;
1231
break;
1232
case __DRI_IMAGE_ATTRIB_NAME:
1233
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
1234
break;
1235
case __DRI_IMAGE_ATTRIB_FD:
1236
whandle.type = WINSYS_HANDLE_TYPE_FD;
1237
break;
1238
case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1239
for (i = 0, tex = image->texture; tex; tex = tex->next)
1240
i++;
1241
*value = i;
1242
return true;
1243
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1244
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1245
whandle.type = WINSYS_HANDLE_TYPE_KMS;
1246
whandle.modifier = DRM_FORMAT_MOD_INVALID;
1247
break;
1248
default:
1249
return false;
1250
}
1251
1252
usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1253
1254
if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1255
usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
1256
1257
if (!pscreen->resource_get_handle(pscreen, NULL, image->texture,
1258
&whandle, usage))
1259
return false;
1260
1261
switch (attrib) {
1262
case __DRI_IMAGE_ATTRIB_STRIDE:
1263
*value = whandle.stride;
1264
return true;
1265
case __DRI_IMAGE_ATTRIB_OFFSET:
1266
*value = whandle.offset;
1267
return true;
1268
case __DRI_IMAGE_ATTRIB_HANDLE:
1269
case __DRI_IMAGE_ATTRIB_NAME:
1270
case __DRI_IMAGE_ATTRIB_FD:
1271
*value = whandle.handle;
1272
return true;
1273
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1274
if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
1275
return false;
1276
*value = (whandle.modifier >> 32) & 0xffffffff;
1277
return true;
1278
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1279
if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
1280
return false;
1281
*value = whandle.modifier & 0xffffffff;
1282
return true;
1283
default:
1284
return false;
1285
}
1286
}
1287
1288
static bool
1289
dri2_resource_get_param(__DRIimage *image, enum pipe_resource_param param,
1290
unsigned handle_usage, uint64_t *value)
1291
{
1292
struct pipe_screen *pscreen = image->texture->screen;
1293
if (!pscreen->resource_get_param)
1294
return false;
1295
1296
if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1297
handle_usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
1298
1299
return pscreen->resource_get_param(pscreen, NULL, image->texture,
1300
image->plane, 0, 0, param, handle_usage,
1301
value);
1302
}
1303
1304
static bool
1305
dri2_query_image_by_resource_param(__DRIimage *image, int attrib, int *value)
1306
{
1307
enum pipe_resource_param param;
1308
uint64_t res_param;
1309
unsigned handle_usage;
1310
1311
if (!image->texture->screen->resource_get_param)
1312
return false;
1313
1314
switch (attrib) {
1315
case __DRI_IMAGE_ATTRIB_STRIDE:
1316
param = PIPE_RESOURCE_PARAM_STRIDE;
1317
break;
1318
case __DRI_IMAGE_ATTRIB_OFFSET:
1319
param = PIPE_RESOURCE_PARAM_OFFSET;
1320
break;
1321
case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1322
param = PIPE_RESOURCE_PARAM_NPLANES;
1323
break;
1324
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1325
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1326
param = PIPE_RESOURCE_PARAM_MODIFIER;
1327
break;
1328
case __DRI_IMAGE_ATTRIB_HANDLE:
1329
param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS;
1330
break;
1331
case __DRI_IMAGE_ATTRIB_NAME:
1332
param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED;
1333
break;
1334
case __DRI_IMAGE_ATTRIB_FD:
1335
param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD;
1336
break;
1337
default:
1338
return false;
1339
}
1340
1341
handle_usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1342
1343
if (!dri2_resource_get_param(image, param, handle_usage, &res_param))
1344
return false;
1345
1346
switch (attrib) {
1347
case __DRI_IMAGE_ATTRIB_STRIDE:
1348
case __DRI_IMAGE_ATTRIB_OFFSET:
1349
case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1350
if (res_param > INT_MAX)
1351
return false;
1352
*value = (int)res_param;
1353
return true;
1354
case __DRI_IMAGE_ATTRIB_HANDLE:
1355
case __DRI_IMAGE_ATTRIB_NAME:
1356
case __DRI_IMAGE_ATTRIB_FD:
1357
if (res_param > UINT_MAX)
1358
return false;
1359
*value = (int)res_param;
1360
return true;
1361
case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1362
if (res_param == DRM_FORMAT_MOD_INVALID)
1363
return false;
1364
*value = (res_param >> 32) & 0xffffffff;
1365
return true;
1366
case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1367
if (res_param == DRM_FORMAT_MOD_INVALID)
1368
return false;
1369
*value = res_param & 0xffffffff;
1370
return true;
1371
default:
1372
return false;
1373
}
1374
}
1375
1376
static GLboolean
1377
dri2_query_image(__DRIimage *image, int attrib, int *value)
1378
{
1379
if (dri2_query_image_common(image, attrib, value))
1380
return GL_TRUE;
1381
else if (dri2_query_image_by_resource_param(image, attrib, value))
1382
return GL_TRUE;
1383
else if (dri2_query_image_by_resource_handle(image, attrib, value))
1384
return GL_TRUE;
1385
else
1386
return GL_FALSE;
1387
}
1388
1389
static __DRIimage *
1390
dri2_dup_image(__DRIimage *image, void *loaderPrivate)
1391
{
1392
__DRIimage *img;
1393
1394
img = CALLOC_STRUCT(__DRIimageRec);
1395
if (!img)
1396
return NULL;
1397
1398
img->texture = NULL;
1399
pipe_resource_reference(&img->texture, image->texture);
1400
img->level = image->level;
1401
img->layer = image->layer;
1402
img->dri_format = image->dri_format;
1403
/* This should be 0 for sub images, but dup is also used for base images. */
1404
img->dri_components = image->dri_components;
1405
img->use = image->use;
1406
img->loader_private = loaderPrivate;
1407
img->sPriv = image->sPriv;
1408
1409
return img;
1410
}
1411
1412
static GLboolean
1413
dri2_validate_usage(__DRIimage *image, unsigned int use)
1414
{
1415
if (!image || !image->texture)
1416
return false;
1417
1418
struct pipe_screen *screen = image->texture->screen;
1419
if (!screen->check_resource_capability)
1420
return true;
1421
1422
/* We don't want to check these:
1423
* __DRI_IMAGE_USE_SHARE (all images are shareable)
1424
* __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1425
*/
1426
unsigned bind = 0;
1427
if (use & __DRI_IMAGE_USE_SCANOUT)
1428
bind |= PIPE_BIND_SCANOUT;
1429
if (use & __DRI_IMAGE_USE_LINEAR)
1430
bind |= PIPE_BIND_LINEAR;
1431
if (use & __DRI_IMAGE_USE_CURSOR)
1432
bind |= PIPE_BIND_CURSOR;
1433
1434
if (!bind)
1435
return true;
1436
1437
return screen->check_resource_capability(screen, image->texture, bind);
1438
}
1439
1440
static __DRIimage *
1441
dri2_from_names(__DRIscreen *screen, int width, int height, int format,
1442
int *names, int num_names, int *strides, int *offsets,
1443
void *loaderPrivate)
1444
{
1445
const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
1446
__DRIimage *img;
1447
struct winsys_handle whandle;
1448
1449
if (!map)
1450
return NULL;
1451
1452
if (num_names != 1)
1453
return NULL;
1454
1455
memset(&whandle, 0, sizeof(whandle));
1456
whandle.type = WINSYS_HANDLE_TYPE_SHARED;
1457
whandle.handle = names[0];
1458
whandle.stride = strides[0];
1459
whandle.offset = offsets[0];
1460
whandle.format = map->pipe_format;
1461
whandle.modifier = DRM_FORMAT_MOD_INVALID;
1462
1463
img = dri2_create_image_from_winsys(screen, width, height, map,
1464
1, &whandle, false, loaderPrivate);
1465
if (img == NULL)
1466
return NULL;
1467
1468
img->dri_components = map->dri_components;
1469
img->dri_fourcc = map->dri_fourcc;
1470
img->dri_format = map->pipe_format;
1471
1472
return img;
1473
}
1474
1475
static __DRIimage *
1476
dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1477
{
1478
__DRIimage *img;
1479
1480
if (plane < 0) {
1481
return NULL;
1482
} else if (plane > 0) {
1483
uint64_t planes;
1484
if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_NPLANES, 0,
1485
&planes) ||
1486
plane >= planes) {
1487
return NULL;
1488
}
1489
}
1490
1491
if (image->dri_components == 0) {
1492
uint64_t modifier;
1493
if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_MODIFIER, 0,
1494
&modifier) ||
1495
modifier == DRM_FORMAT_MOD_INVALID) {
1496
return NULL;
1497
}
1498
}
1499
1500
img = dri2_dup_image(image, loaderPrivate);
1501
if (img == NULL)
1502
return NULL;
1503
1504
if (img->texture->screen->resource_changed)
1505
img->texture->screen->resource_changed(img->texture->screen,
1506
img->texture);
1507
1508
/* set this to 0 for sub images. */
1509
img->dri_components = 0;
1510
img->plane = plane;
1511
return img;
1512
}
1513
1514
static __DRIimage *
1515
dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1516
int *fds, int num_fds, int *strides, int *offsets,
1517
void *loaderPrivate)
1518
{
1519
return dri2_create_image_from_fd(screen, width, height, fourcc,
1520
DRM_FORMAT_MOD_INVALID, fds, num_fds,
1521
strides, offsets, false, NULL, loaderPrivate);
1522
}
1523
1524
static boolean
1525
dri2_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1526
uint64_t *modifiers, unsigned int *external_only,
1527
int *count)
1528
{
1529
struct dri_screen *screen = dri_screen(_screen);
1530
struct pipe_screen *pscreen = screen->base.screen;
1531
const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
1532
enum pipe_format format;
1533
1534
if (!map)
1535
return false;
1536
1537
format = map->pipe_format;
1538
1539
if (pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
1540
PIPE_BIND_RENDER_TARGET) ||
1541
pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
1542
PIPE_BIND_SAMPLER_VIEW) ||
1543
dri2_yuv_dma_buf_supported(screen, map)) {
1544
if (pscreen->query_dmabuf_modifiers != NULL)
1545
pscreen->query_dmabuf_modifiers(pscreen, format, max, modifiers,
1546
external_only, count);
1547
else
1548
*count = 0;
1549
return true;
1550
}
1551
return false;
1552
}
1553
1554
static boolean
1555
dri2_query_dma_buf_format_modifier_attribs(__DRIscreen *_screen,
1556
uint32_t fourcc, uint64_t modifier,
1557
int attrib, uint64_t *value)
1558
{
1559
struct dri_screen *screen = dri_screen(_screen);
1560
struct pipe_screen *pscreen = screen->base.screen;
1561
1562
if (!pscreen->query_dmabuf_modifiers)
1563
return false;
1564
1565
switch (attrib) {
1566
case __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT: {
1567
uint64_t mod_planes = dri2_get_modifier_num_planes(_screen, modifier,
1568
fourcc);
1569
if (mod_planes > 0)
1570
*value = mod_planes;
1571
return mod_planes > 0;
1572
}
1573
default:
1574
return false;
1575
}
1576
}
1577
1578
static __DRIimage *
1579
dri2_from_dma_bufs(__DRIscreen *screen,
1580
int width, int height, int fourcc,
1581
int *fds, int num_fds,
1582
int *strides, int *offsets,
1583
enum __DRIYUVColorSpace yuv_color_space,
1584
enum __DRISampleRange sample_range,
1585
enum __DRIChromaSiting horizontal_siting,
1586
enum __DRIChromaSiting vertical_siting,
1587
unsigned *error,
1588
void *loaderPrivate)
1589
{
1590
__DRIimage *img;
1591
1592
img = dri2_create_image_from_fd(screen, width, height, fourcc,
1593
DRM_FORMAT_MOD_INVALID, fds, num_fds,
1594
strides, offsets, false, error, loaderPrivate);
1595
if (img == NULL)
1596
return NULL;
1597
1598
img->yuv_color_space = yuv_color_space;
1599
img->sample_range = sample_range;
1600
img->horizontal_siting = horizontal_siting;
1601
img->vertical_siting = vertical_siting;
1602
1603
*error = __DRI_IMAGE_ERROR_SUCCESS;
1604
return img;
1605
}
1606
1607
static __DRIimage *
1608
dri2_from_dma_bufs2(__DRIscreen *screen,
1609
int width, int height, int fourcc,
1610
uint64_t modifier, int *fds, int num_fds,
1611
int *strides, int *offsets,
1612
enum __DRIYUVColorSpace yuv_color_space,
1613
enum __DRISampleRange sample_range,
1614
enum __DRIChromaSiting horizontal_siting,
1615
enum __DRIChromaSiting vertical_siting,
1616
unsigned *error,
1617
void *loaderPrivate)
1618
{
1619
__DRIimage *img;
1620
1621
img = dri2_create_image_from_fd(screen, width, height, fourcc,
1622
modifier, fds, num_fds, strides, offsets,
1623
false, error, loaderPrivate);
1624
if (img == NULL)
1625
return NULL;
1626
1627
img->yuv_color_space = yuv_color_space;
1628
img->sample_range = sample_range;
1629
img->horizontal_siting = horizontal_siting;
1630
img->vertical_siting = vertical_siting;
1631
1632
*error = __DRI_IMAGE_ERROR_SUCCESS;
1633
return img;
1634
}
1635
1636
static __DRIimage *
1637
dri2_from_dma_bufs3(__DRIscreen *screen,
1638
int width, int height, int fourcc,
1639
uint64_t modifier, int *fds, int num_fds,
1640
int *strides, int *offsets,
1641
enum __DRIYUVColorSpace yuv_color_space,
1642
enum __DRISampleRange sample_range,
1643
enum __DRIChromaSiting horizontal_siting,
1644
enum __DRIChromaSiting vertical_siting,
1645
uint32_t flags,
1646
unsigned *error,
1647
void *loaderPrivate)
1648
{
1649
__DRIimage *img;
1650
1651
img = dri2_create_image_from_fd(screen, width, height, fourcc,
1652
modifier, fds, num_fds, strides, offsets,
1653
flags & __DRI_IMAGE_PROTECTED_CONTENT_FLAG,
1654
error, loaderPrivate);
1655
if (img == NULL)
1656
return NULL;
1657
1658
img->yuv_color_space = yuv_color_space;
1659
img->sample_range = sample_range;
1660
img->horizontal_siting = horizontal_siting;
1661
img->vertical_siting = vertical_siting;
1662
1663
*error = __DRI_IMAGE_ERROR_SUCCESS;
1664
return img;
1665
}
1666
1667
static void
1668
dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1669
int dstx0, int dsty0, int dstwidth, int dstheight,
1670
int srcx0, int srcy0, int srcwidth, int srcheight,
1671
int flush_flag)
1672
{
1673
struct dri_context *ctx = dri_context(context);
1674
struct pipe_context *pipe = ctx->st->pipe;
1675
struct pipe_screen *screen;
1676
struct pipe_fence_handle *fence;
1677
struct pipe_blit_info blit;
1678
1679
if (!dst || !src)
1680
return;
1681
1682
memset(&blit, 0, sizeof(blit));
1683
blit.dst.resource = dst->texture;
1684
blit.dst.box.x = dstx0;
1685
blit.dst.box.y = dsty0;
1686
blit.dst.box.width = dstwidth;
1687
blit.dst.box.height = dstheight;
1688
blit.dst.box.depth = 1;
1689
blit.dst.format = dst->texture->format;
1690
blit.src.resource = src->texture;
1691
blit.src.box.x = srcx0;
1692
blit.src.box.y = srcy0;
1693
blit.src.box.width = srcwidth;
1694
blit.src.box.height = srcheight;
1695
blit.src.box.depth = 1;
1696
blit.src.format = src->texture->format;
1697
blit.mask = PIPE_MASK_RGBA;
1698
blit.filter = PIPE_TEX_FILTER_NEAREST;
1699
1700
pipe->blit(pipe, &blit);
1701
1702
if (flush_flag == __BLIT_FLAG_FLUSH) {
1703
pipe->flush_resource(pipe, dst->texture);
1704
ctx->st->flush(ctx->st, 0, NULL, NULL, NULL);
1705
} else if (flush_flag == __BLIT_FLAG_FINISH) {
1706
screen = dri_screen(ctx->sPriv)->base.screen;
1707
pipe->flush_resource(pipe, dst->texture);
1708
ctx->st->flush(ctx->st, 0, &fence, NULL, NULL);
1709
(void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
1710
screen->fence_reference(screen, &fence, NULL);
1711
}
1712
}
1713
1714
static void *
1715
dri2_map_image(__DRIcontext *context, __DRIimage *image,
1716
int x0, int y0, int width, int height,
1717
unsigned int flags, int *stride, void **data)
1718
{
1719
struct dri_context *ctx = dri_context(context);
1720
struct pipe_context *pipe = ctx->st->pipe;
1721
enum pipe_map_flags pipe_access = 0;
1722
struct pipe_transfer *trans;
1723
void *map;
1724
1725
if (!image || !data || *data)
1726
return NULL;
1727
1728
unsigned plane = image->plane;
1729
if (plane >= dri2_get_mapping_by_format(image->dri_format)->nplanes)
1730
return NULL;
1731
1732
struct pipe_resource *resource = image->texture;
1733
while (plane--)
1734
resource = resource->next;
1735
1736
if (flags & __DRI_IMAGE_TRANSFER_READ)
1737
pipe_access |= PIPE_MAP_READ;
1738
if (flags & __DRI_IMAGE_TRANSFER_WRITE)
1739
pipe_access |= PIPE_MAP_WRITE;
1740
1741
map = pipe_texture_map(pipe, resource, 0, 0, pipe_access, x0, y0,
1742
width, height, &trans);
1743
if (map) {
1744
*data = trans;
1745
*stride = trans->stride;
1746
}
1747
1748
return map;
1749
}
1750
1751
static void
1752
dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
1753
{
1754
struct dri_context *ctx = dri_context(context);
1755
struct pipe_context *pipe = ctx->st->pipe;
1756
1757
pipe_texture_unmap(pipe, (struct pipe_transfer *)data);
1758
}
1759
1760
static int
1761
dri2_get_capabilities(__DRIscreen *_screen)
1762
{
1763
struct dri_screen *screen = dri_screen(_screen);
1764
1765
return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1766
}
1767
1768
/* The extension is modified during runtime if DRI_PRIME is detected */
1769
static const __DRIimageExtension dri2ImageExtensionTempl = {
1770
.base = { __DRI_IMAGE, 19 },
1771
1772
.createImageFromName = dri2_create_image_from_name,
1773
.createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1774
.destroyImage = dri2_destroy_image,
1775
.createImage = dri2_create_image,
1776
.queryImage = dri2_query_image,
1777
.dupImage = dri2_dup_image,
1778
.validateUsage = dri2_validate_usage,
1779
.createImageFromNames = dri2_from_names,
1780
.fromPlanar = dri2_from_planar,
1781
.createImageFromTexture = dri2_create_from_texture,
1782
.createImageFromFds = NULL,
1783
.createImageFromDmaBufs = NULL,
1784
.blitImage = dri2_blit_image,
1785
.getCapabilities = dri2_get_capabilities,
1786
.mapImage = dri2_map_image,
1787
.unmapImage = dri2_unmap_image,
1788
.createImageWithModifiers = NULL,
1789
.createImageFromDmaBufs2 = NULL,
1790
.createImageFromDmaBufs3 = NULL,
1791
.queryDmaBufFormats = NULL,
1792
.queryDmaBufModifiers = NULL,
1793
.queryDmaBufFormatModifierAttribs = NULL,
1794
.createImageFromRenderbuffer2 = dri2_create_image_from_renderbuffer2,
1795
.createImageWithModifiers2 = NULL,
1796
};
1797
1798
static const __DRIrobustnessExtension dri2Robustness = {
1799
.base = { __DRI2_ROBUSTNESS, 1 }
1800
};
1801
1802
static int
1803
dri2_interop_query_device_info(__DRIcontext *_ctx,
1804
struct mesa_glinterop_device_info *out)
1805
{
1806
struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
1807
1808
/* There is no version 0, thus we do not support it */
1809
if (out->version == 0)
1810
return MESA_GLINTEROP_INVALID_VERSION;
1811
1812
out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
1813
out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
1814
out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
1815
out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
1816
1817
out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
1818
out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
1819
1820
/* Instruct the caller that we support up-to version one of the interface */
1821
out->version = 1;
1822
1823
return MESA_GLINTEROP_SUCCESS;
1824
}
1825
1826
static int
1827
dri2_interop_export_object(__DRIcontext *_ctx,
1828
struct mesa_glinterop_export_in *in,
1829
struct mesa_glinterop_export_out *out)
1830
{
1831
struct st_context_iface *st = dri_context(_ctx)->st;
1832
struct pipe_screen *screen = st->pipe->screen;
1833
struct gl_context *ctx = ((struct st_context *)st)->ctx;
1834
struct pipe_resource *res = NULL;
1835
struct winsys_handle whandle;
1836
unsigned target, usage;
1837
boolean success;
1838
1839
/* There is no version 0, thus we do not support it */
1840
if (in->version == 0 || out->version == 0)
1841
return MESA_GLINTEROP_INVALID_VERSION;
1842
1843
/* Validate the target. */
1844
switch (in->target) {
1845
case GL_TEXTURE_BUFFER:
1846
case GL_TEXTURE_1D:
1847
case GL_TEXTURE_2D:
1848
case GL_TEXTURE_3D:
1849
case GL_TEXTURE_RECTANGLE:
1850
case GL_TEXTURE_1D_ARRAY:
1851
case GL_TEXTURE_2D_ARRAY:
1852
case GL_TEXTURE_CUBE_MAP_ARRAY:
1853
case GL_TEXTURE_CUBE_MAP:
1854
case GL_TEXTURE_2D_MULTISAMPLE:
1855
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1856
case GL_TEXTURE_EXTERNAL_OES:
1857
case GL_RENDERBUFFER:
1858
case GL_ARRAY_BUFFER:
1859
target = in->target;
1860
break;
1861
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1862
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1863
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1864
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1865
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1866
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1867
target = GL_TEXTURE_CUBE_MAP;
1868
break;
1869
default:
1870
return MESA_GLINTEROP_INVALID_TARGET;
1871
}
1872
1873
/* Validate the simple case of miplevel. */
1874
if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
1875
in->miplevel != 0)
1876
return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1877
1878
/* Validate the OpenGL object and get pipe_resource. */
1879
simple_mtx_lock(&ctx->Shared->Mutex);
1880
1881
if (target == GL_ARRAY_BUFFER) {
1882
/* Buffer objects.
1883
*
1884
* The error checking is based on the documentation of
1885
* clCreateFromGLBuffer from OpenCL 2.0 SDK.
1886
*/
1887
struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
1888
1889
/* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1890
* "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1891
* a GL buffer object but does not have an existing data store or
1892
* the size of the buffer is 0."
1893
*/
1894
if (!buf || buf->Size == 0) {
1895
simple_mtx_unlock(&ctx->Shared->Mutex);
1896
return MESA_GLINTEROP_INVALID_OBJECT;
1897
}
1898
1899
res = st_buffer_object(buf)->buffer;
1900
if (!res) {
1901
/* this shouldn't happen */
1902
simple_mtx_unlock(&ctx->Shared->Mutex);
1903
return MESA_GLINTEROP_INVALID_OBJECT;
1904
}
1905
1906
out->buf_offset = 0;
1907
out->buf_size = buf->Size;
1908
1909
buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1910
} else if (target == GL_RENDERBUFFER) {
1911
/* Renderbuffers.
1912
*
1913
* The error checking is based on the documentation of
1914
* clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1915
*/
1916
struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
1917
1918
/* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1919
* "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1920
* object or if the width or height of renderbuffer is zero."
1921
*/
1922
if (!rb || rb->Width == 0 || rb->Height == 0) {
1923
simple_mtx_unlock(&ctx->Shared->Mutex);
1924
return MESA_GLINTEROP_INVALID_OBJECT;
1925
}
1926
1927
/* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1928
* "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1929
* renderbuffer object."
1930
*/
1931
if (rb->NumSamples > 1) {
1932
simple_mtx_unlock(&ctx->Shared->Mutex);
1933
return MESA_GLINTEROP_INVALID_OPERATION;
1934
}
1935
1936
/* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1937
* "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1938
* required by the OpenCL implementation on the device."
1939
*/
1940
res = st_renderbuffer(rb)->texture;
1941
if (!res) {
1942
simple_mtx_unlock(&ctx->Shared->Mutex);
1943
return MESA_GLINTEROP_OUT_OF_RESOURCES;
1944
}
1945
1946
out->internal_format = rb->InternalFormat;
1947
out->view_minlevel = 0;
1948
out->view_numlevels = 1;
1949
out->view_minlayer = 0;
1950
out->view_numlayers = 1;
1951
} else {
1952
/* Texture objects.
1953
*
1954
* The error checking is based on the documentation of
1955
* clCreateFromGLTexture from OpenCL 2.0 SDK.
1956
*/
1957
struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
1958
1959
if (obj)
1960
_mesa_test_texobj_completeness(ctx, obj);
1961
1962
/* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1963
* "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1964
* type matches texture_target, if the specified miplevel of texture
1965
* is not defined, or if the width or height of the specified
1966
* miplevel is zero or if the GL texture object is incomplete."
1967
*/
1968
if (!obj ||
1969
obj->Target != target ||
1970
!obj->_BaseComplete ||
1971
(in->miplevel > 0 && !obj->_MipmapComplete)) {
1972
simple_mtx_unlock(&ctx->Shared->Mutex);
1973
return MESA_GLINTEROP_INVALID_OBJECT;
1974
}
1975
1976
if (target == GL_TEXTURE_BUFFER) {
1977
struct st_buffer_object *stBuf =
1978
st_buffer_object(obj->BufferObject);
1979
1980
if (!stBuf || !stBuf->buffer) {
1981
/* this shouldn't happen */
1982
simple_mtx_unlock(&ctx->Shared->Mutex);
1983
return MESA_GLINTEROP_INVALID_OBJECT;
1984
}
1985
res = stBuf->buffer;
1986
1987
out->internal_format = obj->BufferObjectFormat;
1988
out->buf_offset = obj->BufferOffset;
1989
out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
1990
obj->BufferSize;
1991
1992
obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1993
} else {
1994
/* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1995
* "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1996
* levelbase (for OpenGL implementations) or zero (for OpenGL ES
1997
* implementations); or greater than the value of q (for both OpenGL
1998
* and OpenGL ES). levelbase and q are defined for the texture in
1999
* section 3.8.10 (Texture Completeness) of the OpenGL 2.1
2000
* specification and section 3.7.10 of the OpenGL ES 2.0."
2001
*/
2002
if (in->miplevel < obj->Attrib.BaseLevel || in->miplevel > obj->_MaxLevel) {
2003
simple_mtx_unlock(&ctx->Shared->Mutex);
2004
return MESA_GLINTEROP_INVALID_MIP_LEVEL;
2005
}
2006
2007
if (!st_finalize_texture(ctx, st->pipe, obj, 0)) {
2008
simple_mtx_unlock(&ctx->Shared->Mutex);
2009
return MESA_GLINTEROP_OUT_OF_RESOURCES;
2010
}
2011
2012
res = st_get_texobj_resource(obj);
2013
if (!res) {
2014
/* Incomplete texture buffer object? This shouldn't really occur. */
2015
simple_mtx_unlock(&ctx->Shared->Mutex);
2016
return MESA_GLINTEROP_INVALID_OBJECT;
2017
}
2018
2019
out->internal_format = obj->Image[0][0]->InternalFormat;
2020
out->view_minlevel = obj->Attrib.MinLevel;
2021
out->view_numlevels = obj->Attrib.NumLevels;
2022
out->view_minlayer = obj->Attrib.MinLayer;
2023
out->view_numlayers = obj->Attrib.NumLayers;
2024
}
2025
}
2026
2027
/* Get the handle. */
2028
switch (in->access) {
2029
case MESA_GLINTEROP_ACCESS_READ_ONLY:
2030
usage = 0;
2031
break;
2032
case MESA_GLINTEROP_ACCESS_READ_WRITE:
2033
case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
2034
usage = PIPE_HANDLE_USAGE_SHADER_WRITE;
2035
break;
2036
default:
2037
usage = 0;
2038
}
2039
2040
memset(&whandle, 0, sizeof(whandle));
2041
whandle.type = WINSYS_HANDLE_TYPE_FD;
2042
2043
success = screen->resource_get_handle(screen, st->pipe, res, &whandle,
2044
usage);
2045
simple_mtx_unlock(&ctx->Shared->Mutex);
2046
2047
if (!success)
2048
return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
2049
2050
out->dmabuf_fd = whandle.handle;
2051
out->out_driver_data_written = 0;
2052
2053
if (res->target == PIPE_BUFFER)
2054
out->buf_offset += whandle.offset;
2055
2056
/* Instruct the caller that we support up-to version one of the interface */
2057
in->version = 1;
2058
out->version = 1;
2059
2060
return MESA_GLINTEROP_SUCCESS;
2061
}
2062
2063
static const __DRI2interopExtension dri2InteropExtension = {
2064
.base = { __DRI2_INTEROP, 1 },
2065
.query_device_info = dri2_interop_query_device_info,
2066
.export_object = dri2_interop_export_object
2067
};
2068
2069
/**
2070
* \brief the DRI2bufferDamageExtension set_damage_region method
2071
*/
2072
static void
2073
dri2_set_damage_region(__DRIdrawable *dPriv, unsigned int nrects, int *rects)
2074
{
2075
struct dri_drawable *drawable = dri_drawable(dPriv);
2076
struct pipe_box *boxes = NULL;
2077
2078
if (nrects) {
2079
boxes = CALLOC(nrects, sizeof(*boxes));
2080
assert(boxes);
2081
2082
for (unsigned int i = 0; i < nrects; i++) {
2083
int *rect = &rects[i * 4];
2084
2085
u_box_2d(rect[0], rect[1], rect[2], rect[3], &boxes[i]);
2086
}
2087
}
2088
2089
FREE(drawable->damage_rects);
2090
drawable->damage_rects = boxes;
2091
drawable->num_damage_rects = nrects;
2092
2093
/* Only apply the damage region if the BACK_LEFT texture is up-to-date. */
2094
if (drawable->texture_stamp == drawable->dPriv->lastStamp &&
2095
(drawable->texture_mask & (1 << ST_ATTACHMENT_BACK_LEFT))) {
2096
struct pipe_screen *screen = drawable->screen->base.screen;
2097
struct pipe_resource *resource;
2098
2099
if (drawable->stvis.samples > 1)
2100
resource = drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
2101
else
2102
resource = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
2103
2104
screen->set_damage_region(screen, resource,
2105
drawable->num_damage_rects,
2106
drawable->damage_rects);
2107
}
2108
}
2109
2110
static const __DRI2bufferDamageExtension dri2BufferDamageExtensionTempl = {
2111
.base = { __DRI2_BUFFER_DAMAGE, 1 },
2112
};
2113
2114
/**
2115
* \brief the DRI2ConfigQueryExtension configQueryb method
2116
*/
2117
static int
2118
dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
2119
unsigned char *val)
2120
{
2121
struct dri_screen *screen = dri_screen(sPriv);
2122
2123
if (!driCheckOption(&screen->dev->option_cache, var, DRI_BOOL))
2124
return dri2ConfigQueryExtension.configQueryb(sPriv, var, val);
2125
2126
*val = driQueryOptionb(&screen->dev->option_cache, var);
2127
2128
return 0;
2129
}
2130
2131
/**
2132
* \brief the DRI2ConfigQueryExtension configQueryi method
2133
*/
2134
static int
2135
dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
2136
{
2137
struct dri_screen *screen = dri_screen(sPriv);
2138
2139
if (!driCheckOption(&screen->dev->option_cache, var, DRI_INT) &&
2140
!driCheckOption(&screen->dev->option_cache, var, DRI_ENUM))
2141
return dri2ConfigQueryExtension.configQueryi(sPriv, var, val);
2142
2143
*val = driQueryOptioni(&screen->dev->option_cache, var);
2144
2145
return 0;
2146
}
2147
2148
/**
2149
* \brief the DRI2ConfigQueryExtension configQueryf method
2150
*/
2151
static int
2152
dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
2153
{
2154
struct dri_screen *screen = dri_screen(sPriv);
2155
2156
if (!driCheckOption(&screen->dev->option_cache, var, DRI_FLOAT))
2157
return dri2ConfigQueryExtension.configQueryf(sPriv, var, val);
2158
2159
*val = driQueryOptionf(&screen->dev->option_cache, var);
2160
2161
return 0;
2162
}
2163
2164
/**
2165
* \brief the DRI2ConfigQueryExtension configQuerys method
2166
*/
2167
static int
2168
dri2GalliumConfigQuerys(__DRIscreen *sPriv, const char *var, char **val)
2169
{
2170
struct dri_screen *screen = dri_screen(sPriv);
2171
2172
if (!driCheckOption(&screen->dev->option_cache, var, DRI_STRING))
2173
return dri2ConfigQueryExtension.configQuerys(sPriv, var, val);
2174
2175
*val = driQueryOptionstr(&screen->dev->option_cache, var);
2176
2177
return 0;
2178
}
2179
2180
/**
2181
* \brief the DRI2ConfigQueryExtension struct.
2182
*
2183
* We first query the driver option cache. Then the dri2 option cache.
2184
*/
2185
static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
2186
.base = { __DRI2_CONFIG_QUERY, 2 },
2187
2188
.configQueryb = dri2GalliumConfigQueryb,
2189
.configQueryi = dri2GalliumConfigQueryi,
2190
.configQueryf = dri2GalliumConfigQueryf,
2191
.configQuerys = dri2GalliumConfigQuerys,
2192
};
2193
2194
/**
2195
* \brief the DRI2blobExtension set_cache_funcs method
2196
*/
2197
static void
2198
set_blob_cache_funcs(__DRIscreen *sPriv, __DRIblobCacheSet set,
2199
__DRIblobCacheGet get)
2200
{
2201
struct dri_screen *screen = dri_screen(sPriv);
2202
struct pipe_screen *pscreen = screen->base.screen;
2203
2204
if (!pscreen->get_disk_shader_cache)
2205
return;
2206
2207
struct disk_cache *cache = pscreen->get_disk_shader_cache(pscreen);
2208
2209
if (!cache)
2210
return;
2211
2212
disk_cache_set_callbacks(cache, set, get);
2213
}
2214
2215
static const __DRI2blobExtension driBlobExtension = {
2216
.base = { __DRI2_BLOB, 1 },
2217
.set_cache_funcs = set_blob_cache_funcs
2218
};
2219
2220
static const __DRImutableRenderBufferDriverExtension driMutableRenderBufferExtension = {
2221
.base = { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1 },
2222
};
2223
2224
/*
2225
* Backend function init_screen.
2226
*/
2227
2228
static const __DRIextension *dri_screen_extensions_base[] = {
2229
&driTexBufferExtension.base,
2230
&dri2FlushExtension.base,
2231
&dri2RendererQueryExtension.base,
2232
&dri2GalliumConfigQueryExtension.base,
2233
&dri2ThrottleExtension.base,
2234
&dri2FenceExtension.base,
2235
&dri2InteropExtension.base,
2236
&dri2NoErrorExtension.base,
2237
&driBlobExtension.base,
2238
&driMutableRenderBufferExtension.base,
2239
};
2240
2241
/**
2242
* Set up the DRI extension list for this screen based on its underlying
2243
* gallium screen's capabilities.
2244
*/
2245
static void
2246
dri2_init_screen_extensions(struct dri_screen *screen,
2247
struct pipe_screen *pscreen,
2248
bool is_kms_screen)
2249
{
2250
const __DRIextension **nExt;
2251
2252
STATIC_ASSERT(sizeof(screen->screen_extensions) >=
2253
sizeof(dri_screen_extensions_base));
2254
memcpy(&screen->screen_extensions, dri_screen_extensions_base,
2255
sizeof(dri_screen_extensions_base));
2256
screen->sPriv->extensions = screen->screen_extensions;
2257
2258
/* Point nExt at the end of the extension list */
2259
nExt = &screen->screen_extensions[ARRAY_SIZE(dri_screen_extensions_base)];
2260
2261
screen->image_extension = dri2ImageExtensionTempl;
2262
if (pscreen->resource_create_with_modifiers) {
2263
screen->image_extension.createImageWithModifiers =
2264
dri2_create_image_with_modifiers;
2265
screen->image_extension.createImageWithModifiers2 =
2266
dri2_create_image_with_modifiers2;
2267
}
2268
2269
if (pscreen->get_param(pscreen, PIPE_CAP_DMABUF)) {
2270
uint64_t cap;
2271
2272
if (drmGetCap(screen->sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2273
(cap & DRM_PRIME_CAP_IMPORT)) {
2274
screen->image_extension.createImageFromFds = dri2_from_fds;
2275
screen->image_extension.createImageFromDmaBufs = dri2_from_dma_bufs;
2276
screen->image_extension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2277
screen->image_extension.createImageFromDmaBufs3 = dri2_from_dma_bufs3;
2278
screen->image_extension.queryDmaBufFormats =
2279
dri2_query_dma_buf_formats;
2280
screen->image_extension.queryDmaBufModifiers =
2281
dri2_query_dma_buf_modifiers;
2282
if (!is_kms_screen) {
2283
screen->image_extension.queryDmaBufFormatModifierAttribs =
2284
dri2_query_dma_buf_format_modifier_attribs;
2285
}
2286
}
2287
}
2288
*nExt++ = &screen->image_extension.base;
2289
2290
if (!is_kms_screen) {
2291
screen->buffer_damage_extension = dri2BufferDamageExtensionTempl;
2292
if (pscreen->set_damage_region)
2293
screen->buffer_damage_extension.set_damage_region =
2294
dri2_set_damage_region;
2295
*nExt++ = &screen->buffer_damage_extension.base;
2296
2297
if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
2298
*nExt++ = &dri2Robustness.base;
2299
screen->has_reset_status_query = true;
2300
}
2301
}
2302
2303
/* Ensure the extension list didn't overrun its buffer and is still
2304
* NULL-terminated */
2305
assert(nExt - screen->screen_extensions <=
2306
ARRAY_SIZE(screen->screen_extensions) - 1);
2307
assert(!*nExt);
2308
}
2309
2310
/**
2311
* This is the driver specific part of the createNewScreen entry point.
2312
*
2313
* Returns the struct gl_config supported by this driver.
2314
*/
2315
static const __DRIconfig **
2316
dri2_init_screen(__DRIscreen * sPriv)
2317
{
2318
const __DRIconfig **configs;
2319
struct dri_screen *screen;
2320
struct pipe_screen *pscreen = NULL;
2321
2322
screen = CALLOC_STRUCT(dri_screen);
2323
if (!screen)
2324
return NULL;
2325
2326
screen->sPriv = sPriv;
2327
screen->fd = sPriv->fd;
2328
(void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
2329
2330
sPriv->driverPrivate = (void *)screen;
2331
2332
if (pipe_loader_drm_probe_fd(&screen->dev, screen->fd)) {
2333
dri_init_options(screen);
2334
2335
pscreen = pipe_loader_create_screen(screen->dev);
2336
}
2337
2338
if (!pscreen)
2339
goto release_pipe;
2340
2341
screen->throttle = pscreen->get_param(pscreen, PIPE_CAP_THROTTLE);
2342
2343
dri2_init_screen_extensions(screen, pscreen, false);
2344
2345
configs = dri_init_screen_helper(screen, pscreen);
2346
if (!configs)
2347
goto destroy_screen;
2348
2349
screen->can_share_buffer = true;
2350
screen->auto_fake_front = dri_with_format(sPriv);
2351
screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2352
screen->lookup_egl_image = dri2_lookup_egl_image;
2353
2354
return configs;
2355
2356
destroy_screen:
2357
dri_destroy_screen_helper(screen);
2358
2359
release_pipe:
2360
if (screen->dev)
2361
pipe_loader_release(&screen->dev, 1);
2362
2363
FREE(screen);
2364
return NULL;
2365
}
2366
2367
/**
2368
* This is the driver specific part of the createNewScreen entry point.
2369
*
2370
* Returns the struct gl_config supported by this driver.
2371
*/
2372
static const __DRIconfig **
2373
dri_kms_init_screen(__DRIscreen * sPriv)
2374
{
2375
#if defined(GALLIUM_SOFTPIPE)
2376
const __DRIconfig **configs;
2377
struct dri_screen *screen;
2378
struct pipe_screen *pscreen = NULL;
2379
2380
screen = CALLOC_STRUCT(dri_screen);
2381
if (!screen)
2382
return NULL;
2383
2384
screen->sPriv = sPriv;
2385
screen->fd = sPriv->fd;
2386
2387
sPriv->driverPrivate = (void *)screen;
2388
2389
if (pipe_loader_sw_probe_kms(&screen->dev, screen->fd)) {
2390
dri_init_options(screen);
2391
pscreen = pipe_loader_create_screen(screen->dev);
2392
}
2393
2394
if (!pscreen)
2395
goto release_pipe;
2396
2397
dri2_init_screen_extensions(screen, pscreen, true);
2398
2399
configs = dri_init_screen_helper(screen, pscreen);
2400
if (!configs)
2401
goto destroy_screen;
2402
2403
screen->can_share_buffer = false;
2404
screen->auto_fake_front = dri_with_format(sPriv);
2405
screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2406
screen->lookup_egl_image = dri2_lookup_egl_image;
2407
2408
return configs;
2409
2410
destroy_screen:
2411
dri_destroy_screen_helper(screen);
2412
2413
release_pipe:
2414
if (screen->dev)
2415
pipe_loader_release(&screen->dev, 1);
2416
2417
FREE(screen);
2418
#endif // GALLIUM_SOFTPIPE
2419
return NULL;
2420
}
2421
2422
static boolean
2423
dri2_create_buffer(__DRIscreen * sPriv,
2424
__DRIdrawable * dPriv,
2425
const struct gl_config * visual, boolean isPixmap)
2426
{
2427
struct dri_drawable *drawable = NULL;
2428
2429
if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
2430
return FALSE;
2431
2432
drawable = dPriv->driverPrivate;
2433
2434
drawable->allocate_textures = dri2_allocate_textures;
2435
drawable->flush_frontbuffer = dri2_flush_frontbuffer;
2436
drawable->update_tex_buffer = dri2_update_tex_buffer;
2437
drawable->flush_swapbuffers = dri2_flush_swapbuffers;
2438
2439
return TRUE;
2440
}
2441
2442
/**
2443
* DRI driver virtual function table.
2444
*
2445
* DRI versions differ in their implementation of init_screen and swap_buffers.
2446
*/
2447
const struct __DriverAPIRec galliumdrm_driver_api = {
2448
.InitScreen = dri2_init_screen,
2449
.DestroyScreen = dri_destroy_screen,
2450
.CreateContext = dri_create_context,
2451
.DestroyContext = dri_destroy_context,
2452
.CreateBuffer = dri2_create_buffer,
2453
.DestroyBuffer = dri_destroy_buffer,
2454
.MakeCurrent = dri_make_current,
2455
.UnbindContext = dri_unbind_context,
2456
2457
.AllocateBuffer = dri2_allocate_buffer,
2458
.ReleaseBuffer = dri2_release_buffer,
2459
};
2460
2461
/**
2462
* DRI driver virtual function table.
2463
*
2464
* KMS/DRM version of the DriverAPI above sporting a different InitScreen
2465
* hook. The latter is used to explicitly initialise the kms_swrast driver
2466
* rather than selecting the approapriate driver as suggested by the loader.
2467
*/
2468
const struct __DriverAPIRec dri_kms_driver_api = {
2469
.InitScreen = dri_kms_init_screen,
2470
.DestroyScreen = dri_destroy_screen,
2471
.CreateContext = dri_create_context,
2472
.DestroyContext = dri_destroy_context,
2473
.CreateBuffer = dri2_create_buffer,
2474
.DestroyBuffer = dri_destroy_buffer,
2475
.MakeCurrent = dri_make_current,
2476
.UnbindContext = dri_unbind_context,
2477
2478
.AllocateBuffer = dri2_allocate_buffer,
2479
.ReleaseBuffer = dri2_release_buffer,
2480
};
2481
2482
/* This is the table of extensions that the loader will dlsym() for. */
2483
const __DRIextension *galliumdrm_driver_extensions[] = {
2484
&driCoreExtension.base,
2485
&driImageDriverExtension.base,
2486
&driDRI2Extension.base,
2487
&gallium_config_options.base,
2488
NULL
2489
};
2490
2491
/* vim: set sw=3 ts=8 sts=3 expandtab: */
2492
2493