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