e6f0e40465a234d7bde9933b400b2e8d3964df12
[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/format/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.format = format;
551 whandle.modifier = DRM_FORMAT_MOD_INVALID;
552 if (screen->can_share_buffer)
553 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
554 else
555 whandle.type = WINSYS_HANDLE_TYPE_KMS;
556 drawable->textures[statt] =
557 screen->base.screen->resource_from_handle(screen->base.screen,
558 &templ, &whandle,
559 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH);
560 assert(drawable->textures[statt]);
561 }
562 }
563
564 /* Allocate private MSAA colorbuffers. */
565 if (drawable->stvis.samples > 1) {
566 for (i = 0; i < statts_count; i++) {
567 enum st_attachment_type statt = statts[i];
568
569 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
570 continue;
571
572 if (drawable->textures[statt]) {
573 templ.format = drawable->textures[statt]->format;
574 templ.bind = drawable->textures[statt]->bind &
575 ~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED);
576 templ.nr_samples = drawable->stvis.samples;
577 templ.nr_storage_samples = drawable->stvis.samples;
578
579 /* Try to reuse the resource.
580 * (the other resource parameters should be constant)
581 */
582 if (!drawable->msaa_textures[statt] ||
583 drawable->msaa_textures[statt]->width0 != templ.width0 ||
584 drawable->msaa_textures[statt]->height0 != templ.height0) {
585 /* Allocate a new one. */
586 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
587
588 drawable->msaa_textures[statt] =
589 screen->base.screen->resource_create(screen->base.screen,
590 &templ);
591 assert(drawable->msaa_textures[statt]);
592
593 /* If there are any MSAA resources, we should initialize them
594 * such that they contain the same data as the single-sample
595 * resources we just got from the X server.
596 *
597 * The reason for this is that the state tracker (and
598 * therefore the app) can access the MSAA resources only.
599 * The single-sample resources are not exposed
600 * to the state tracker.
601 *
602 */
603 dri_pipe_blit(ctx->st->pipe,
604 drawable->msaa_textures[statt],
605 drawable->textures[statt]);
606 }
607 }
608 else {
609 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
610 }
611 }
612 }
613
614 /* Allocate a private depth-stencil buffer. */
615 if (alloc_depthstencil) {
616 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
617 struct pipe_resource **zsbuf;
618 enum pipe_format format;
619 unsigned bind;
620
621 dri_drawable_get_format(drawable, statt, &format, &bind);
622
623 if (format) {
624 templ.format = format;
625 templ.bind = bind & ~PIPE_BIND_SHARED;
626
627 if (drawable->stvis.samples > 1) {
628 templ.nr_samples = drawable->stvis.samples;
629 templ.nr_storage_samples = drawable->stvis.samples;
630 zsbuf = &drawable->msaa_textures[statt];
631 }
632 else {
633 templ.nr_samples = 0;
634 templ.nr_storage_samples = 0;
635 zsbuf = &drawable->textures[statt];
636 }
637
638 /* Try to reuse the resource.
639 * (the other resource parameters should be constant)
640 */
641 if (!*zsbuf ||
642 (*zsbuf)->width0 != templ.width0 ||
643 (*zsbuf)->height0 != templ.height0) {
644 /* Allocate a new one. */
645 pipe_resource_reference(zsbuf, NULL);
646 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
647 &templ);
648 assert(*zsbuf);
649 }
650 }
651 else {
652 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
653 pipe_resource_reference(&drawable->textures[statt], NULL);
654 }
655 }
656
657 /* For DRI2, we may get the same buffers again from the server.
658 * To prevent useless imports of gem names, drawable->old* is used
659 * to bypass the import if we get the same buffers. This doesn't apply
660 * to DRI3/Wayland, users of image.loader, since the buffer is managed
661 * by the client (no import), and the back buffer is going to change
662 * at every redraw.
663 */
664 if (!image) {
665 drawable->old_num = num_buffers;
666 drawable->old_w = dri_drawable->w;
667 drawable->old_h = dri_drawable->h;
668 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
669 }
670 }
671
672 static void
673 dri2_flush_frontbuffer(struct dri_context *ctx,
674 struct dri_drawable *drawable,
675 enum st_attachment_type statt)
676 {
677 __DRIdrawable *dri_drawable = drawable->dPriv;
678 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
679 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
680 struct pipe_context *pipe = ctx->st->pipe;
681
682 if (statt != ST_ATTACHMENT_FRONT_LEFT)
683 return;
684
685 if (drawable->stvis.samples > 1) {
686 /* Resolve the front buffer. */
687 dri_pipe_blit(ctx->st->pipe,
688 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
689 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
690 }
691
692 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
693 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
694 }
695
696 pipe->flush(pipe, NULL, 0);
697
698 if (image) {
699 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
700 }
701 else if (loader->flushFrontBuffer) {
702 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
703 }
704 }
705
706 /**
707 * The struct dri_drawable flush_swapbuffers callback
708 */
709 static void
710 dri2_flush_swapbuffers(struct dri_context *ctx,
711 struct dri_drawable *drawable)
712 {
713 __DRIdrawable *dri_drawable = drawable->dPriv;
714 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
715
716 if (image && image->base.version >= 3 && image->flushSwapBuffers) {
717 image->flushSwapBuffers(dri_drawable, dri_drawable->loaderPrivate);
718 }
719 }
720
721 static void
722 dri2_update_tex_buffer(struct dri_drawable *drawable,
723 struct dri_context *ctx,
724 struct pipe_resource *res)
725 {
726 /* no-op */
727 }
728
729 static __DRIimage *
730 dri2_create_image_from_winsys(__DRIscreen *_screen,
731 int width, int height, const struct dri2_format_mapping *map,
732 int num_handles, struct winsys_handle *whandle,
733 void *loaderPrivate)
734 {
735 struct dri_screen *screen = dri_screen(_screen);
736 struct pipe_screen *pscreen = screen->base.screen;
737 __DRIimage *img;
738 struct pipe_resource templ;
739 unsigned tex_usage = 0;
740 int i;
741 bool use_lowered = false;
742
743 if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
744 PIPE_BIND_RENDER_TARGET))
745 tex_usage |= PIPE_BIND_RENDER_TARGET;
746 if (pscreen->is_format_supported(pscreen, map->pipe_format, screen->target, 0, 0,
747 PIPE_BIND_SAMPLER_VIEW))
748 tex_usage |= PIPE_BIND_SAMPLER_VIEW;
749
750 if (!tex_usage && util_format_is_yuv(map->pipe_format)) {
751 /* YUV format sampling can be emulated by the Mesa state tracker by
752 * using multiple samplers of varying formats.
753 * If no tex_usage is set and we detect a YUV format,
754 * test for support of the first plane's sampler format and
755 * add sampler view usage.
756 */
757 use_lowered = true;
758 if (pscreen->is_format_supported(pscreen,
759 dri2_get_pipe_format_for_dri_format(map->planes[0].dri_format),
760 screen->target, 0, 0,
761 PIPE_BIND_SAMPLER_VIEW))
762 tex_usage |= PIPE_BIND_SAMPLER_VIEW;
763 }
764
765 if (!tex_usage)
766 return NULL;
767
768 img = CALLOC_STRUCT(__DRIimageRec);
769 if (!img)
770 return NULL;
771
772 memset(&templ, 0, sizeof(templ));
773 templ.bind = tex_usage;
774 templ.target = screen->target;
775 templ.last_level = 0;
776 templ.depth0 = 1;
777 templ.array_size = 1;
778
779 for (i = (use_lowered ? map->nplanes : num_handles) - 1; i >= 0; i--) {
780 struct pipe_resource *tex;
781
782 templ.width0 = width >> map->planes[i].width_shift;
783 templ.height0 = height >> map->planes[i].height_shift;
784 if (use_lowered)
785 templ.format = dri2_get_pipe_format_for_dri_format(map->planes[i].dri_format);
786 else
787 templ.format = map->pipe_format;
788 assert(templ.format != PIPE_FORMAT_NONE);
789
790 tex = pscreen->resource_from_handle(pscreen,
791 &templ, &whandle[use_lowered ? map->planes[i].buffer_index : i],
792 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
793 if (!tex) {
794 pipe_resource_reference(&img->texture, NULL);
795 FREE(img);
796 return NULL;
797 }
798
799 tex->next = img->texture;
800 img->texture = tex;
801 }
802
803 img->level = 0;
804 img->layer = 0;
805 img->use = 0;
806 img->loader_private = loaderPrivate;
807
808 return img;
809 }
810
811 static __DRIimage *
812 dri2_create_image_from_name(__DRIscreen *_screen,
813 int width, int height, int format,
814 int name, int pitch, void *loaderPrivate)
815 {
816 const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
817 struct winsys_handle whandle;
818 __DRIimage *img;
819
820 if (!map)
821 return NULL;
822
823 memset(&whandle, 0, sizeof(whandle));
824 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
825 whandle.handle = name;
826 whandle.format = map->pipe_format;
827 whandle.modifier = DRM_FORMAT_MOD_INVALID;
828
829 whandle.stride = pitch * util_format_get_blocksize(map->pipe_format);
830
831 img = dri2_create_image_from_winsys(_screen, width, height, map,
832 1, &whandle, loaderPrivate);
833
834 if (!img)
835 return NULL;
836
837 img->dri_components = map->dri_components;
838 img->dri_fourcc = map->dri_fourcc;
839 img->dri_format = map->dri_format;
840
841 return img;
842 }
843
844 static unsigned
845 dri2_get_modifier_num_planes(uint64_t modifier, int fourcc)
846 {
847 const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
848
849 if (!map)
850 return 0;
851
852 switch (modifier) {
853 case I915_FORMAT_MOD_Y_TILED_CCS:
854 return 2;
855 case DRM_FORMAT_MOD_BROADCOM_UIF:
856 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
857 case DRM_FORMAT_MOD_LINEAR:
858 /* DRM_FORMAT_MOD_NONE is the same as LINEAR */
859 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_EIGHT_GOB:
860 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_FOUR_GOB:
861 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_ONE_GOB:
862 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_SIXTEEN_GOB:
863 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_THIRTYTWO_GOB:
864 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK_TWO_GOB:
865 case DRM_FORMAT_MOD_QCOM_COMPRESSED:
866 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
867 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
868 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
869 case DRM_FORMAT_MOD_VIVANTE_TILED:
870 /* FD_FORMAT_MOD_QCOM_TILED is not in drm_fourcc.h */
871 case I915_FORMAT_MOD_X_TILED:
872 case I915_FORMAT_MOD_Y_TILED:
873 case DRM_FORMAT_MOD_INVALID:
874 return map->nplanes;
875 default:
876 return 0;
877 }
878 }
879
880 static __DRIimage *
881 dri2_create_image_from_fd(__DRIscreen *_screen,
882 int width, int height, int fourcc,
883 uint64_t modifier, int *fds, int num_fds,
884 int *strides, int *offsets, unsigned *error,
885 void *loaderPrivate)
886 {
887 struct winsys_handle whandles[3];
888 const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
889 __DRIimage *img = NULL;
890 unsigned err = __DRI_IMAGE_ERROR_SUCCESS;
891 int i, expected_num_fds;
892 int num_handles = dri2_get_modifier_num_planes(modifier, fourcc);
893
894 if (!map || num_handles == 0) {
895 err = __DRI_IMAGE_ERROR_BAD_MATCH;
896 goto exit;
897 }
898
899 switch (fourcc) {
900 case DRM_FORMAT_YUYV:
901 case DRM_FORMAT_UYVY:
902 expected_num_fds = 1;
903 break;
904 default:
905 expected_num_fds = num_handles;
906 break;
907 }
908
909 if (num_fds != expected_num_fds) {
910 err = __DRI_IMAGE_ERROR_BAD_MATCH;
911 goto exit;
912 }
913
914 memset(whandles, 0, sizeof(whandles));
915
916 for (i = 0; i < num_fds; i++) {
917 if (fds[i] < 0) {
918 err = __DRI_IMAGE_ERROR_BAD_ALLOC;
919 goto exit;
920 }
921
922 whandles[i].type = WINSYS_HANDLE_TYPE_FD;
923 whandles[i].handle = (unsigned)fds[i];
924 whandles[i].stride = (unsigned)strides[i];
925 whandles[i].offset = (unsigned)offsets[i];
926 whandles[i].format = map->pipe_format;
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_fds, 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 img->imported_dmabuf = TRUE;
942
943 exit:
944 if (error)
945 *error = err;
946
947 return img;
948 }
949
950 static __DRIimage *
951 dri2_create_image_common(__DRIscreen *_screen,
952 int width, int height,
953 int format, unsigned int use,
954 const uint64_t *modifiers,
955 const unsigned count,
956 void *loaderPrivate)
957 {
958 const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
959 struct dri_screen *screen = dri_screen(_screen);
960 __DRIimage *img;
961 struct pipe_resource templ;
962 unsigned tex_usage;
963
964 if (!map)
965 return NULL;
966
967 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
968
969 if (use & __DRI_IMAGE_USE_SCANOUT)
970 tex_usage |= PIPE_BIND_SCANOUT;
971 if (use & __DRI_IMAGE_USE_SHARE)
972 tex_usage |= PIPE_BIND_SHARED;
973 if (use & __DRI_IMAGE_USE_LINEAR)
974 tex_usage |= PIPE_BIND_LINEAR;
975 if (use & __DRI_IMAGE_USE_CURSOR) {
976 if (width != 64 || height != 64)
977 return NULL;
978 tex_usage |= PIPE_BIND_CURSOR;
979 }
980
981 img = CALLOC_STRUCT(__DRIimageRec);
982 if (!img)
983 return NULL;
984
985 memset(&templ, 0, sizeof(templ));
986 templ.bind = tex_usage;
987 templ.format = map->pipe_format;
988 templ.target = PIPE_TEXTURE_2D;
989 templ.last_level = 0;
990 templ.width0 = width;
991 templ.height0 = height;
992 templ.depth0 = 1;
993 templ.array_size = 1;
994
995 if (modifiers)
996 img->texture =
997 screen->base.screen
998 ->resource_create_with_modifiers(screen->base.screen,
999 &templ,
1000 modifiers,
1001 count);
1002 else
1003 img->texture =
1004 screen->base.screen->resource_create(screen->base.screen, &templ);
1005 if (!img->texture) {
1006 FREE(img);
1007 return NULL;
1008 }
1009
1010 img->level = 0;
1011 img->layer = 0;
1012 img->dri_format = format;
1013 img->dri_fourcc = map->dri_fourcc;
1014 img->dri_components = 0;
1015 img->use = use;
1016
1017 img->loader_private = loaderPrivate;
1018 return img;
1019 }
1020
1021 static __DRIimage *
1022 dri2_create_image(__DRIscreen *_screen,
1023 int width, int height, int format,
1024 unsigned int use, void *loaderPrivate)
1025 {
1026 return dri2_create_image_common(_screen, width, height, format, use,
1027 NULL /* modifiers */, 0 /* count */,
1028 loaderPrivate);
1029 }
1030
1031 static __DRIimage *
1032 dri2_create_image_with_modifiers(__DRIscreen *dri_screen,
1033 int width, int height, int format,
1034 const uint64_t *modifiers,
1035 const unsigned count,
1036 void *loaderPrivate)
1037 {
1038 return dri2_create_image_common(dri_screen, width, height, format,
1039 __DRI_IMAGE_USE_SHARE, modifiers, count,
1040 loaderPrivate);
1041 }
1042
1043 static bool
1044 dri2_query_image_common(__DRIimage *image, int attrib, int *value)
1045 {
1046 switch (attrib) {
1047 case __DRI_IMAGE_ATTRIB_FORMAT:
1048 *value = image->dri_format;
1049 return true;
1050 case __DRI_IMAGE_ATTRIB_WIDTH:
1051 *value = image->texture->width0;
1052 return true;
1053 case __DRI_IMAGE_ATTRIB_HEIGHT:
1054 *value = image->texture->height0;
1055 return true;
1056 case __DRI_IMAGE_ATTRIB_COMPONENTS:
1057 if (image->dri_components == 0)
1058 return false;
1059 *value = image->dri_components;
1060 return true;
1061 case __DRI_IMAGE_ATTRIB_FOURCC:
1062 if (image->dri_fourcc) {
1063 *value = image->dri_fourcc;
1064 } else {
1065 const struct dri2_format_mapping *map;
1066
1067 map = dri2_get_mapping_by_format(image->dri_format);
1068 if (!map)
1069 return false;
1070
1071 *value = map->dri_fourcc;
1072 }
1073 return true;
1074 default:
1075 return false;
1076 }
1077 }
1078
1079 static bool
1080 dri2_query_image_by_resource_handle(__DRIimage *image, int attrib, int *value)
1081 {
1082 struct pipe_screen *pscreen = image->texture->screen;
1083 struct winsys_handle whandle;
1084 unsigned usage;
1085 memset(&whandle, 0, sizeof(whandle));
1086 whandle.plane = image->plane;
1087
1088 switch (attrib) {
1089 case __DRI_IMAGE_ATTRIB_STRIDE:
1090 case __DRI_IMAGE_ATTRIB_OFFSET:
1091 case __DRI_IMAGE_ATTRIB_HANDLE:
1092 whandle.type = WINSYS_HANDLE_TYPE_KMS;
1093 break;
1094 case __DRI_IMAGE_ATTRIB_NAME:
1095 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
1096 break;
1097 case __DRI_IMAGE_ATTRIB_FD:
1098 whandle.type = WINSYS_HANDLE_TYPE_FD;
1099 break;
1100 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1101 *value = 1;
1102 return true;
1103 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1104 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1105 whandle.type = WINSYS_HANDLE_TYPE_KMS;
1106 whandle.modifier = DRM_FORMAT_MOD_INVALID;
1107 break;
1108 default:
1109 return false;
1110 }
1111
1112 usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1113
1114 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1115 usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
1116
1117 if (!pscreen->resource_get_handle(pscreen, NULL, image->texture,
1118 &whandle, usage))
1119 return false;
1120
1121 switch (attrib) {
1122 case __DRI_IMAGE_ATTRIB_STRIDE:
1123 *value = whandle.stride;
1124 return true;
1125 case __DRI_IMAGE_ATTRIB_OFFSET:
1126 *value = whandle.offset;
1127 return true;
1128 case __DRI_IMAGE_ATTRIB_HANDLE:
1129 case __DRI_IMAGE_ATTRIB_NAME:
1130 case __DRI_IMAGE_ATTRIB_FD:
1131 *value = whandle.handle;
1132 return true;
1133 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1134 if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
1135 return false;
1136 *value = (whandle.modifier >> 32) & 0xffffffff;
1137 return true;
1138 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1139 if (whandle.modifier == DRM_FORMAT_MOD_INVALID)
1140 return false;
1141 *value = whandle.modifier & 0xffffffff;
1142 return true;
1143 default:
1144 return false;
1145 }
1146 }
1147
1148 static bool
1149 dri2_resource_get_param(__DRIimage *image, enum pipe_resource_param param,
1150 unsigned handle_usage, uint64_t *value)
1151 {
1152 struct pipe_screen *pscreen = image->texture->screen;
1153 if (!pscreen->resource_get_param)
1154 return false;
1155
1156 return pscreen->resource_get_param(pscreen, NULL, image->texture,
1157 image->plane, 0, param, handle_usage,
1158 value);
1159 }
1160
1161 static bool
1162 dri2_query_image_by_resource_param(__DRIimage *image, int attrib, int *value)
1163 {
1164 enum pipe_resource_param param;
1165 uint64_t res_param;
1166 unsigned handle_usage;
1167
1168 if (!image->texture->screen->resource_get_param)
1169 return false;
1170
1171 switch (attrib) {
1172 case __DRI_IMAGE_ATTRIB_STRIDE:
1173 param = PIPE_RESOURCE_PARAM_STRIDE;
1174 break;
1175 case __DRI_IMAGE_ATTRIB_OFFSET:
1176 param = PIPE_RESOURCE_PARAM_OFFSET;
1177 break;
1178 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1179 param = PIPE_RESOURCE_PARAM_NPLANES;
1180 break;
1181 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1182 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1183 param = PIPE_RESOURCE_PARAM_MODIFIER;
1184 break;
1185 case __DRI_IMAGE_ATTRIB_HANDLE:
1186 param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS;
1187 break;
1188 case __DRI_IMAGE_ATTRIB_NAME:
1189 param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED;
1190 break;
1191 case __DRI_IMAGE_ATTRIB_FD:
1192 param = PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD;
1193 break;
1194 default:
1195 return false;
1196 }
1197
1198 handle_usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
1199
1200 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
1201 handle_usage |= PIPE_HANDLE_USAGE_EXPLICIT_FLUSH;
1202
1203 if (!dri2_resource_get_param(image, param, handle_usage, &res_param))
1204 return false;
1205
1206 switch (attrib) {
1207 case __DRI_IMAGE_ATTRIB_STRIDE:
1208 case __DRI_IMAGE_ATTRIB_OFFSET:
1209 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
1210 if (res_param > INT_MAX)
1211 return false;
1212 *value = (int)res_param;
1213 return true;
1214 case __DRI_IMAGE_ATTRIB_HANDLE:
1215 case __DRI_IMAGE_ATTRIB_NAME:
1216 case __DRI_IMAGE_ATTRIB_FD:
1217 if (res_param > UINT_MAX)
1218 return false;
1219 *value = (int)res_param;
1220 return true;
1221 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
1222 if (res_param == DRM_FORMAT_MOD_INVALID)
1223 return false;
1224 *value = (res_param >> 32) & 0xffffffff;
1225 return true;
1226 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
1227 if (res_param == DRM_FORMAT_MOD_INVALID)
1228 return false;
1229 *value = res_param & 0xffffffff;
1230 return true;
1231 default:
1232 return false;
1233 }
1234 }
1235
1236 static GLboolean
1237 dri2_query_image(__DRIimage *image, int attrib, int *value)
1238 {
1239 if (dri2_query_image_common(image, attrib, value))
1240 return GL_TRUE;
1241 else if (dri2_query_image_by_resource_param(image, attrib, value))
1242 return GL_TRUE;
1243 else if (dri2_query_image_by_resource_handle(image, attrib, value))
1244 return GL_TRUE;
1245 else
1246 return GL_FALSE;
1247 }
1248
1249 static __DRIimage *
1250 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
1251 {
1252 __DRIimage *img;
1253
1254 img = CALLOC_STRUCT(__DRIimageRec);
1255 if (!img)
1256 return NULL;
1257
1258 img->texture = NULL;
1259 pipe_resource_reference(&img->texture, image->texture);
1260 img->level = image->level;
1261 img->layer = image->layer;
1262 img->dri_format = image->dri_format;
1263 /* This should be 0 for sub images, but dup is also used for base images. */
1264 img->dri_components = image->dri_components;
1265 img->loader_private = loaderPrivate;
1266
1267 return img;
1268 }
1269
1270 static GLboolean
1271 dri2_validate_usage(__DRIimage *image, unsigned int use)
1272 {
1273 if (!image || !image->texture)
1274 return false;
1275
1276 struct pipe_screen *screen = image->texture->screen;
1277 if (!screen->check_resource_capability)
1278 return true;
1279
1280 /* We don't want to check these:
1281 * __DRI_IMAGE_USE_SHARE (all images are shareable)
1282 * __DRI_IMAGE_USE_BACKBUFFER (all images support this)
1283 */
1284 unsigned bind = 0;
1285 if (use & __DRI_IMAGE_USE_SCANOUT)
1286 bind |= PIPE_BIND_SCANOUT;
1287 if (use & __DRI_IMAGE_USE_LINEAR)
1288 bind |= PIPE_BIND_LINEAR;
1289 if (use & __DRI_IMAGE_USE_CURSOR)
1290 bind |= PIPE_BIND_CURSOR;
1291
1292 if (!bind)
1293 return true;
1294
1295 return screen->check_resource_capability(screen, image->texture, bind);
1296 }
1297
1298 static __DRIimage *
1299 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
1300 int *names, int num_names, int *strides, int *offsets,
1301 void *loaderPrivate)
1302 {
1303 const struct dri2_format_mapping *map = dri2_get_mapping_by_format(format);
1304 __DRIimage *img;
1305 struct winsys_handle whandle;
1306
1307 if (!map)
1308 return NULL;
1309
1310 if (num_names != 1)
1311 return NULL;
1312
1313 memset(&whandle, 0, sizeof(whandle));
1314 whandle.type = WINSYS_HANDLE_TYPE_SHARED;
1315 whandle.handle = names[0];
1316 whandle.stride = strides[0];
1317 whandle.offset = offsets[0];
1318 whandle.format = map->pipe_format;
1319 whandle.modifier = DRM_FORMAT_MOD_INVALID;
1320
1321 img = dri2_create_image_from_winsys(screen, width, height, map,
1322 1, &whandle, loaderPrivate);
1323 if (img == NULL)
1324 return NULL;
1325
1326 img->dri_components = map->dri_components;
1327 img->dri_fourcc = map->dri_fourcc;
1328 img->dri_format = map->pipe_format;
1329
1330 return img;
1331 }
1332
1333 static __DRIimage *
1334 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1335 {
1336 __DRIimage *img;
1337
1338 if (plane < 0) {
1339 return NULL;
1340 } else if (plane > 0) {
1341 uint64_t planes;
1342 if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_NPLANES, 0,
1343 &planes) ||
1344 plane >= planes) {
1345 return NULL;
1346 }
1347 }
1348
1349 if (image->dri_components == 0) {
1350 uint64_t modifier;
1351 if (!dri2_resource_get_param(image, PIPE_RESOURCE_PARAM_MODIFIER, 0,
1352 &modifier) ||
1353 modifier == DRM_FORMAT_MOD_INVALID) {
1354 return NULL;
1355 }
1356 }
1357
1358 img = dri2_dup_image(image, loaderPrivate);
1359 if (img == NULL)
1360 return NULL;
1361
1362 if (img->texture->screen->resource_changed)
1363 img->texture->screen->resource_changed(img->texture->screen,
1364 img->texture);
1365
1366 /* set this to 0 for sub images. */
1367 img->dri_components = 0;
1368 img->plane = plane;
1369 return img;
1370 }
1371
1372 static __DRIimage *
1373 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1374 int *fds, int num_fds, int *strides, int *offsets,
1375 void *loaderPrivate)
1376 {
1377 return dri2_create_image_from_fd(screen, width, height, fourcc,
1378 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1379 strides, offsets, NULL, loaderPrivate);
1380 }
1381
1382 static boolean
1383 dri2_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1384 uint64_t *modifiers, unsigned int *external_only,
1385 int *count)
1386 {
1387 struct dri_screen *screen = dri_screen(_screen);
1388 struct pipe_screen *pscreen = screen->base.screen;
1389 const struct dri2_format_mapping *map = dri2_get_mapping_by_fourcc(fourcc);
1390 enum pipe_format format;
1391
1392 if (!map)
1393 return false;
1394
1395 format = map->pipe_format;
1396
1397 if (pscreen->query_dmabuf_modifiers != NULL &&
1398 (pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
1399 PIPE_BIND_RENDER_TARGET) ||
1400 pscreen->is_format_supported(pscreen, format, screen->target, 0, 0,
1401 PIPE_BIND_SAMPLER_VIEW))) {
1402 pscreen->query_dmabuf_modifiers(pscreen, format, max, modifiers,
1403 external_only, count);
1404 return true;
1405 }
1406 return false;
1407 }
1408
1409 static boolean
1410 dri2_query_dma_buf_format_modifier_attribs(__DRIscreen *_screen,
1411 uint32_t fourcc, uint64_t modifier,
1412 int attrib, uint64_t *value)
1413 {
1414 switch (attrib) {
1415 case __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT: {
1416 uint64_t mod_planes = dri2_get_modifier_num_planes(modifier, fourcc);
1417 if (mod_planes > 0)
1418 *value = mod_planes;
1419 return mod_planes > 0;
1420 }
1421 default:
1422 return false;
1423 }
1424 }
1425
1426 static __DRIimage *
1427 dri2_from_dma_bufs(__DRIscreen *screen,
1428 int width, int height, int fourcc,
1429 int *fds, int num_fds,
1430 int *strides, int *offsets,
1431 enum __DRIYUVColorSpace yuv_color_space,
1432 enum __DRISampleRange sample_range,
1433 enum __DRIChromaSiting horizontal_siting,
1434 enum __DRIChromaSiting vertical_siting,
1435 unsigned *error,
1436 void *loaderPrivate)
1437 {
1438 __DRIimage *img;
1439
1440 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1441 DRM_FORMAT_MOD_INVALID, fds, num_fds,
1442 strides, offsets, error, loaderPrivate);
1443 if (img == NULL)
1444 return NULL;
1445
1446 img->yuv_color_space = yuv_color_space;
1447 img->sample_range = sample_range;
1448 img->horizontal_siting = horizontal_siting;
1449 img->vertical_siting = vertical_siting;
1450
1451 *error = __DRI_IMAGE_ERROR_SUCCESS;
1452 return img;
1453 }
1454
1455 static __DRIimage *
1456 dri2_from_dma_bufs2(__DRIscreen *screen,
1457 int width, int height, int fourcc,
1458 uint64_t modifier, int *fds, int num_fds,
1459 int *strides, int *offsets,
1460 enum __DRIYUVColorSpace yuv_color_space,
1461 enum __DRISampleRange sample_range,
1462 enum __DRIChromaSiting horizontal_siting,
1463 enum __DRIChromaSiting vertical_siting,
1464 unsigned *error,
1465 void *loaderPrivate)
1466 {
1467 __DRIimage *img;
1468
1469 img = dri2_create_image_from_fd(screen, width, height, fourcc,
1470 modifier, fds, num_fds, strides, offsets,
1471 error, loaderPrivate);
1472 if (img == NULL)
1473 return NULL;
1474
1475 img->yuv_color_space = yuv_color_space;
1476 img->sample_range = sample_range;
1477 img->horizontal_siting = horizontal_siting;
1478 img->vertical_siting = vertical_siting;
1479
1480 *error = __DRI_IMAGE_ERROR_SUCCESS;
1481 return img;
1482 }
1483
1484 static void
1485 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1486 int dstx0, int dsty0, int dstwidth, int dstheight,
1487 int srcx0, int srcy0, int srcwidth, int srcheight,
1488 int flush_flag)
1489 {
1490 struct dri_context *ctx = dri_context(context);
1491 struct pipe_context *pipe = ctx->st->pipe;
1492 struct pipe_screen *screen;
1493 struct pipe_fence_handle *fence;
1494 struct pipe_blit_info blit;
1495
1496 if (!dst || !src)
1497 return;
1498
1499 memset(&blit, 0, sizeof(blit));
1500 blit.dst.resource = dst->texture;
1501 blit.dst.box.x = dstx0;
1502 blit.dst.box.y = dsty0;
1503 blit.dst.box.width = dstwidth;
1504 blit.dst.box.height = dstheight;
1505 blit.dst.box.depth = 1;
1506 blit.dst.format = dst->texture->format;
1507 blit.src.resource = src->texture;
1508 blit.src.box.x = srcx0;
1509 blit.src.box.y = srcy0;
1510 blit.src.box.width = srcwidth;
1511 blit.src.box.height = srcheight;
1512 blit.src.box.depth = 1;
1513 blit.src.format = src->texture->format;
1514 blit.mask = PIPE_MASK_RGBA;
1515 blit.filter = PIPE_TEX_FILTER_NEAREST;
1516
1517 pipe->blit(pipe, &blit);
1518
1519 if (flush_flag == __BLIT_FLAG_FLUSH) {
1520 pipe->flush_resource(pipe, dst->texture);
1521 ctx->st->flush(ctx->st, 0, NULL, NULL, NULL);
1522 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1523 screen = dri_screen(ctx->sPriv)->base.screen;
1524 pipe->flush_resource(pipe, dst->texture);
1525 ctx->st->flush(ctx->st, 0, &fence, NULL, NULL);
1526 (void) screen->fence_finish(screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
1527 screen->fence_reference(screen, &fence, NULL);
1528 }
1529 }
1530
1531 static void *
1532 dri2_map_image(__DRIcontext *context, __DRIimage *image,
1533 int x0, int y0, int width, int height,
1534 unsigned int flags, int *stride, void **data)
1535 {
1536 struct dri_context *ctx = dri_context(context);
1537 struct pipe_context *pipe = ctx->st->pipe;
1538 enum pipe_transfer_usage pipe_access = 0;
1539 struct pipe_transfer *trans;
1540 void *map;
1541
1542 if (!image || !data || *data)
1543 return NULL;
1544
1545 if (flags & __DRI_IMAGE_TRANSFER_READ)
1546 pipe_access |= PIPE_TRANSFER_READ;
1547 if (flags & __DRI_IMAGE_TRANSFER_WRITE)
1548 pipe_access |= PIPE_TRANSFER_WRITE;
1549
1550 map = pipe_transfer_map(pipe, image->texture,
1551 0, 0, pipe_access, x0, y0, width, height,
1552 &trans);
1553 if (map) {
1554 *data = trans;
1555 *stride = trans->stride;
1556 }
1557
1558 return map;
1559 }
1560
1561 static void
1562 dri2_unmap_image(__DRIcontext *context, __DRIimage *image, void *data)
1563 {
1564 struct dri_context *ctx = dri_context(context);
1565 struct pipe_context *pipe = ctx->st->pipe;
1566
1567 pipe_transfer_unmap(pipe, (struct pipe_transfer *)data);
1568 }
1569
1570 static int
1571 dri2_get_capabilities(__DRIscreen *_screen)
1572 {
1573 struct dri_screen *screen = dri_screen(_screen);
1574
1575 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1576 }
1577
1578 /* The extension is modified during runtime if DRI_PRIME is detected */
1579 static __DRIimageExtension dri2ImageExtension = {
1580 .base = { __DRI_IMAGE, 17 },
1581
1582 .createImageFromName = dri2_create_image_from_name,
1583 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1584 .destroyImage = dri2_destroy_image,
1585 .createImage = dri2_create_image,
1586 .queryImage = dri2_query_image,
1587 .dupImage = dri2_dup_image,
1588 .validateUsage = dri2_validate_usage,
1589 .createImageFromNames = dri2_from_names,
1590 .fromPlanar = dri2_from_planar,
1591 .createImageFromTexture = dri2_create_from_texture,
1592 .createImageFromFds = NULL,
1593 .createImageFromDmaBufs = NULL,
1594 .blitImage = dri2_blit_image,
1595 .getCapabilities = dri2_get_capabilities,
1596 .mapImage = dri2_map_image,
1597 .unmapImage = dri2_unmap_image,
1598 .createImageWithModifiers = NULL,
1599 .createImageFromDmaBufs2 = NULL,
1600 .queryDmaBufFormats = NULL,
1601 .queryDmaBufModifiers = NULL,
1602 .queryDmaBufFormatModifierAttribs = NULL,
1603 .createImageFromRenderbuffer2 = dri2_create_image_from_renderbuffer2,
1604 };
1605
1606 static const __DRIrobustnessExtension dri2Robustness = {
1607 .base = { __DRI2_ROBUSTNESS, 1 }
1608 };
1609
1610 static int
1611 dri2_interop_query_device_info(__DRIcontext *_ctx,
1612 struct mesa_glinterop_device_info *out)
1613 {
1614 struct pipe_screen *screen = dri_context(_ctx)->st->pipe->screen;
1615
1616 /* There is no version 0, thus we do not support it */
1617 if (out->version == 0)
1618 return MESA_GLINTEROP_INVALID_VERSION;
1619
1620 out->pci_segment_group = screen->get_param(screen, PIPE_CAP_PCI_GROUP);
1621 out->pci_bus = screen->get_param(screen, PIPE_CAP_PCI_BUS);
1622 out->pci_device = screen->get_param(screen, PIPE_CAP_PCI_DEVICE);
1623 out->pci_function = screen->get_param(screen, PIPE_CAP_PCI_FUNCTION);
1624
1625 out->vendor_id = screen->get_param(screen, PIPE_CAP_VENDOR_ID);
1626 out->device_id = screen->get_param(screen, PIPE_CAP_DEVICE_ID);
1627
1628 /* Instruct the caller that we support up-to version one of the interface */
1629 out->version = 1;
1630
1631 return MESA_GLINTEROP_SUCCESS;
1632 }
1633
1634 static int
1635 dri2_interop_export_object(__DRIcontext *_ctx,
1636 struct mesa_glinterop_export_in *in,
1637 struct mesa_glinterop_export_out *out)
1638 {
1639 struct st_context_iface *st = dri_context(_ctx)->st;
1640 struct pipe_screen *screen = st->pipe->screen;
1641 struct gl_context *ctx = ((struct st_context *)st)->ctx;
1642 struct pipe_resource *res = NULL;
1643 struct winsys_handle whandle;
1644 unsigned target, usage;
1645 boolean success;
1646
1647 /* There is no version 0, thus we do not support it */
1648 if (in->version == 0 || out->version == 0)
1649 return MESA_GLINTEROP_INVALID_VERSION;
1650
1651 /* Validate the target. */
1652 switch (in->target) {
1653 case GL_TEXTURE_BUFFER:
1654 case GL_TEXTURE_1D:
1655 case GL_TEXTURE_2D:
1656 case GL_TEXTURE_3D:
1657 case GL_TEXTURE_RECTANGLE:
1658 case GL_TEXTURE_1D_ARRAY:
1659 case GL_TEXTURE_2D_ARRAY:
1660 case GL_TEXTURE_CUBE_MAP_ARRAY:
1661 case GL_TEXTURE_CUBE_MAP:
1662 case GL_TEXTURE_2D_MULTISAMPLE:
1663 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1664 case GL_TEXTURE_EXTERNAL_OES:
1665 case GL_RENDERBUFFER:
1666 case GL_ARRAY_BUFFER:
1667 target = in->target;
1668 break;
1669 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1670 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1671 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1672 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1673 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1674 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1675 target = GL_TEXTURE_CUBE_MAP;
1676 break;
1677 default:
1678 return MESA_GLINTEROP_INVALID_TARGET;
1679 }
1680
1681 /* Validate the simple case of miplevel. */
1682 if ((target == GL_RENDERBUFFER || target == GL_ARRAY_BUFFER) &&
1683 in->miplevel != 0)
1684 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1685
1686 /* Validate the OpenGL object and get pipe_resource. */
1687 simple_mtx_lock(&ctx->Shared->Mutex);
1688
1689 if (target == GL_ARRAY_BUFFER) {
1690 /* Buffer objects.
1691 *
1692 * The error checking is based on the documentation of
1693 * clCreateFromGLBuffer from OpenCL 2.0 SDK.
1694 */
1695 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, in->obj);
1696
1697 /* From OpenCL 2.0 SDK, clCreateFromGLBuffer:
1698 * "CL_INVALID_GL_OBJECT if bufobj is not a GL buffer object or is
1699 * a GL buffer object but does not have an existing data store or
1700 * the size of the buffer is 0."
1701 */
1702 if (!buf || buf->Size == 0) {
1703 simple_mtx_unlock(&ctx->Shared->Mutex);
1704 return MESA_GLINTEROP_INVALID_OBJECT;
1705 }
1706
1707 res = st_buffer_object(buf)->buffer;
1708 if (!res) {
1709 /* this shouldn't happen */
1710 simple_mtx_unlock(&ctx->Shared->Mutex);
1711 return MESA_GLINTEROP_INVALID_OBJECT;
1712 }
1713
1714 out->buf_offset = 0;
1715 out->buf_size = buf->Size;
1716
1717 buf->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1718 } else if (target == GL_RENDERBUFFER) {
1719 /* Renderbuffers.
1720 *
1721 * The error checking is based on the documentation of
1722 * clCreateFromGLRenderbuffer from OpenCL 2.0 SDK.
1723 */
1724 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, in->obj);
1725
1726 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1727 * "CL_INVALID_GL_OBJECT if renderbuffer is not a GL renderbuffer
1728 * object or if the width or height of renderbuffer is zero."
1729 */
1730 if (!rb || rb->Width == 0 || rb->Height == 0) {
1731 simple_mtx_unlock(&ctx->Shared->Mutex);
1732 return MESA_GLINTEROP_INVALID_OBJECT;
1733 }
1734
1735 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1736 * "CL_INVALID_OPERATION if renderbuffer is a multi-sample GL
1737 * renderbuffer object."
1738 */
1739 if (rb->NumSamples > 1) {
1740 simple_mtx_unlock(&ctx->Shared->Mutex);
1741 return MESA_GLINTEROP_INVALID_OPERATION;
1742 }
1743
1744 /* From OpenCL 2.0 SDK, clCreateFromGLRenderbuffer:
1745 * "CL_OUT_OF_RESOURCES if there is a failure to allocate resources
1746 * required by the OpenCL implementation on the device."
1747 */
1748 res = st_renderbuffer(rb)->texture;
1749 if (!res) {
1750 simple_mtx_unlock(&ctx->Shared->Mutex);
1751 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1752 }
1753
1754 out->internal_format = rb->InternalFormat;
1755 out->view_minlevel = 0;
1756 out->view_numlevels = 1;
1757 out->view_minlayer = 0;
1758 out->view_numlayers = 1;
1759 } else {
1760 /* Texture objects.
1761 *
1762 * The error checking is based on the documentation of
1763 * clCreateFromGLTexture from OpenCL 2.0 SDK.
1764 */
1765 struct gl_texture_object *obj = _mesa_lookup_texture(ctx, in->obj);
1766
1767 if (obj)
1768 _mesa_test_texobj_completeness(ctx, obj);
1769
1770 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1771 * "CL_INVALID_GL_OBJECT if texture is not a GL texture object whose
1772 * type matches texture_target, if the specified miplevel of texture
1773 * is not defined, or if the width or height of the specified
1774 * miplevel is zero or if the GL texture object is incomplete."
1775 */
1776 if (!obj ||
1777 obj->Target != target ||
1778 !obj->_BaseComplete ||
1779 (in->miplevel > 0 && !obj->_MipmapComplete)) {
1780 simple_mtx_unlock(&ctx->Shared->Mutex);
1781 return MESA_GLINTEROP_INVALID_OBJECT;
1782 }
1783
1784 if (target == GL_TEXTURE_BUFFER) {
1785 struct st_buffer_object *stBuf =
1786 st_buffer_object(obj->BufferObject);
1787
1788 if (!stBuf || !stBuf->buffer) {
1789 /* this shouldn't happen */
1790 simple_mtx_unlock(&ctx->Shared->Mutex);
1791 return MESA_GLINTEROP_INVALID_OBJECT;
1792 }
1793 res = stBuf->buffer;
1794
1795 out->internal_format = obj->BufferObjectFormat;
1796 out->buf_offset = obj->BufferOffset;
1797 out->buf_size = obj->BufferSize == -1 ? obj->BufferObject->Size :
1798 obj->BufferSize;
1799
1800 obj->BufferObject->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE;
1801 } else {
1802 /* From OpenCL 2.0 SDK, clCreateFromGLTexture:
1803 * "CL_INVALID_MIP_LEVEL if miplevel is less than the value of
1804 * levelbase (for OpenGL implementations) or zero (for OpenGL ES
1805 * implementations); or greater than the value of q (for both OpenGL
1806 * and OpenGL ES). levelbase and q are defined for the texture in
1807 * section 3.8.10 (Texture Completeness) of the OpenGL 2.1
1808 * specification and section 3.7.10 of the OpenGL ES 2.0."
1809 */
1810 if (in->miplevel < obj->BaseLevel || in->miplevel > obj->_MaxLevel) {
1811 simple_mtx_unlock(&ctx->Shared->Mutex);
1812 return MESA_GLINTEROP_INVALID_MIP_LEVEL;
1813 }
1814
1815 if (!st_finalize_texture(ctx, st->pipe, obj, 0)) {
1816 simple_mtx_unlock(&ctx->Shared->Mutex);
1817 return MESA_GLINTEROP_OUT_OF_RESOURCES;
1818 }
1819
1820 res = st_get_texobj_resource(obj);
1821 if (!res) {
1822 /* Incomplete texture buffer object? This shouldn't really occur. */
1823 simple_mtx_unlock(&ctx->Shared->Mutex);
1824 return MESA_GLINTEROP_INVALID_OBJECT;
1825 }
1826
1827 out->internal_format = obj->Image[0][0]->InternalFormat;
1828 out->view_minlevel = obj->MinLevel;
1829 out->view_numlevels = obj->NumLevels;
1830 out->view_minlayer = obj->MinLayer;
1831 out->view_numlayers = obj->NumLayers;
1832 }
1833 }
1834
1835 /* Get the handle. */
1836 switch (in->access) {
1837 case MESA_GLINTEROP_ACCESS_READ_ONLY:
1838 usage = 0;
1839 break;
1840 case MESA_GLINTEROP_ACCESS_READ_WRITE:
1841 case MESA_GLINTEROP_ACCESS_WRITE_ONLY:
1842 usage = PIPE_HANDLE_USAGE_SHADER_WRITE;
1843 break;
1844 default:
1845 usage = 0;
1846 }
1847
1848 memset(&whandle, 0, sizeof(whandle));
1849 whandle.type = WINSYS_HANDLE_TYPE_FD;
1850
1851 success = screen->resource_get_handle(screen, st->pipe, res, &whandle,
1852 usage);
1853 simple_mtx_unlock(&ctx->Shared->Mutex);
1854
1855 if (!success)
1856 return MESA_GLINTEROP_OUT_OF_HOST_MEMORY;
1857
1858 out->dmabuf_fd = whandle.handle;
1859 out->out_driver_data_written = 0;
1860
1861 if (res->target == PIPE_BUFFER)
1862 out->buf_offset += whandle.offset;
1863
1864 /* Instruct the caller that we support up-to version one of the interface */
1865 in->version = 1;
1866 out->version = 1;
1867
1868 return MESA_GLINTEROP_SUCCESS;
1869 }
1870
1871 static const __DRI2interopExtension dri2InteropExtension = {
1872 .base = { __DRI2_INTEROP, 1 },
1873 .query_device_info = dri2_interop_query_device_info,
1874 .export_object = dri2_interop_export_object
1875 };
1876
1877 /**
1878 * \brief the DRI2bufferDamageExtension set_damage_region method
1879 */
1880 static void
1881 dri2_set_damage_region(__DRIdrawable *dPriv, unsigned int nrects, int *rects)
1882 {
1883 struct dri_drawable *drawable = dri_drawable(dPriv);
1884 struct pipe_box *boxes = NULL;
1885
1886 if (nrects) {
1887 boxes = CALLOC(nrects, sizeof(*boxes));
1888 assert(boxes);
1889
1890 for (unsigned int i = 0; i < nrects; i++) {
1891 int *rect = &rects[i * 4];
1892
1893 u_box_2d(rect[0], rect[1], rect[2], rect[3], &boxes[i]);
1894 }
1895 }
1896
1897 FREE(drawable->damage_rects);
1898 drawable->damage_rects = boxes;
1899 drawable->num_damage_rects = nrects;
1900
1901 /* Only apply the damage region if the BACK_LEFT texture is up-to-date. */
1902 if (drawable->texture_stamp == drawable->dPriv->lastStamp &&
1903 (drawable->texture_mask & (1 << ST_ATTACHMENT_BACK_LEFT))) {
1904 struct pipe_screen *screen = drawable->screen->base.screen;
1905 struct pipe_resource *resource;
1906
1907 if (drawable->stvis.samples > 1)
1908 resource = drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
1909 else
1910 resource = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
1911
1912 screen->set_damage_region(screen, resource,
1913 drawable->num_damage_rects,
1914 drawable->damage_rects);
1915 }
1916 }
1917
1918 static __DRI2bufferDamageExtension dri2BufferDamageExtension = {
1919 .base = { __DRI2_BUFFER_DAMAGE, 1 },
1920 };
1921
1922 /**
1923 * \brief the DRI2ConfigQueryExtension configQueryb method
1924 */
1925 static int
1926 dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
1927 unsigned char *val)
1928 {
1929 struct dri_screen *screen = dri_screen(sPriv);
1930
1931 if (!driCheckOption(&screen->dev->option_cache, var, DRI_BOOL))
1932 return dri2ConfigQueryExtension.configQueryb(sPriv, var, val);
1933
1934 *val = driQueryOptionb(&screen->dev->option_cache, var);
1935
1936 return 0;
1937 }
1938
1939 /**
1940 * \brief the DRI2ConfigQueryExtension configQueryi method
1941 */
1942 static int
1943 dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
1944 {
1945 struct dri_screen *screen = dri_screen(sPriv);
1946
1947 if (!driCheckOption(&screen->dev->option_cache, var, DRI_INT) &&
1948 !driCheckOption(&screen->dev->option_cache, var, DRI_ENUM))
1949 return dri2ConfigQueryExtension.configQueryi(sPriv, var, val);
1950
1951 *val = driQueryOptioni(&screen->dev->option_cache, var);
1952
1953 return 0;
1954 }
1955
1956 /**
1957 * \brief the DRI2ConfigQueryExtension configQueryf method
1958 */
1959 static int
1960 dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
1961 {
1962 struct dri_screen *screen = dri_screen(sPriv);
1963
1964 if (!driCheckOption(&screen->dev->option_cache, var, DRI_FLOAT))
1965 return dri2ConfigQueryExtension.configQueryf(sPriv, var, val);
1966
1967 *val = driQueryOptionf(&screen->dev->option_cache, var);
1968
1969 return 0;
1970 }
1971
1972 /**
1973 * \brief the DRI2ConfigQueryExtension struct.
1974 *
1975 * We first query the driver option cache. Then the dri2 option cache.
1976 */
1977 static const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
1978 .base = { __DRI2_CONFIG_QUERY, 1 },
1979
1980 .configQueryb = dri2GalliumConfigQueryb,
1981 .configQueryi = dri2GalliumConfigQueryi,
1982 .configQueryf = dri2GalliumConfigQueryf,
1983 };
1984
1985 /**
1986 * \brief the DRI2blobExtension set_cache_funcs method
1987 */
1988 static void
1989 set_blob_cache_funcs(__DRIscreen *sPriv, __DRIblobCacheSet set,
1990 __DRIblobCacheGet get)
1991 {
1992 struct dri_screen *screen = dri_screen(sPriv);
1993 struct pipe_screen *pscreen = screen->base.screen;
1994
1995 if (!pscreen->get_disk_shader_cache)
1996 return;
1997
1998 struct disk_cache *cache = pscreen->get_disk_shader_cache(pscreen);
1999
2000 if (!cache)
2001 return;
2002
2003 disk_cache_set_callbacks(cache, set, get);
2004 }
2005
2006 static const __DRI2blobExtension driBlobExtension = {
2007 .base = { __DRI2_BLOB, 1 },
2008 .set_cache_funcs = set_blob_cache_funcs
2009 };
2010
2011 /*
2012 * Backend function init_screen.
2013 */
2014
2015 static const __DRIextension *dri_screen_extensions[] = {
2016 &driTexBufferExtension.base,
2017 &dri2FlushExtension.base,
2018 &dri2ImageExtension.base,
2019 &dri2RendererQueryExtension.base,
2020 &dri2GalliumConfigQueryExtension.base,
2021 &dri2ThrottleExtension.base,
2022 &dri2FenceExtension.base,
2023 &dri2BufferDamageExtension.base,
2024 &dri2InteropExtension.base,
2025 &dri2NoErrorExtension.base,
2026 &driBlobExtension.base,
2027 NULL
2028 };
2029
2030 static const __DRIextension *dri_robust_screen_extensions[] = {
2031 &driTexBufferExtension.base,
2032 &dri2FlushExtension.base,
2033 &dri2ImageExtension.base,
2034 &dri2RendererQueryExtension.base,
2035 &dri2GalliumConfigQueryExtension.base,
2036 &dri2ThrottleExtension.base,
2037 &dri2FenceExtension.base,
2038 &dri2InteropExtension.base,
2039 &dri2BufferDamageExtension.base,
2040 &dri2Robustness.base,
2041 &dri2NoErrorExtension.base,
2042 &driBlobExtension.base,
2043 NULL
2044 };
2045
2046 /**
2047 * This is the driver specific part of the createNewScreen entry point.
2048 *
2049 * Returns the struct gl_config supported by this driver.
2050 */
2051 static const __DRIconfig **
2052 dri2_init_screen(__DRIscreen * sPriv)
2053 {
2054 const __DRIconfig **configs;
2055 struct dri_screen *screen;
2056 struct pipe_screen *pscreen = NULL;
2057
2058 screen = CALLOC_STRUCT(dri_screen);
2059 if (!screen)
2060 return NULL;
2061
2062 screen->sPriv = sPriv;
2063 screen->fd = sPriv->fd;
2064 (void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
2065
2066 sPriv->driverPrivate = (void *)screen;
2067
2068 if (pipe_loader_drm_probe_fd(&screen->dev, screen->fd)) {
2069 dri_init_options(screen);
2070
2071 pscreen = pipe_loader_create_screen(screen->dev);
2072 }
2073
2074 if (!pscreen)
2075 goto release_pipe;
2076
2077 screen->throttle = pscreen->get_param(pscreen, PIPE_CAP_THROTTLE);
2078
2079 if (pscreen->resource_create_with_modifiers)
2080 dri2ImageExtension.createImageWithModifiers =
2081 dri2_create_image_with_modifiers;
2082
2083 if (pscreen->get_param(pscreen, PIPE_CAP_DMABUF)) {
2084 uint64_t cap;
2085
2086 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2087 (cap & DRM_PRIME_CAP_IMPORT)) {
2088 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2089 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2090 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2091 if (pscreen->query_dmabuf_modifiers) {
2092 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2093 dri2ImageExtension.queryDmaBufModifiers =
2094 dri2_query_dma_buf_modifiers;
2095 dri2ImageExtension.queryDmaBufFormatModifierAttribs =
2096 dri2_query_dma_buf_format_modifier_attribs;
2097 }
2098 }
2099 }
2100
2101 if (pscreen->set_damage_region)
2102 dri2BufferDamageExtension.set_damage_region = dri2_set_damage_region;
2103
2104 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
2105 sPriv->extensions = dri_robust_screen_extensions;
2106 screen->has_reset_status_query = true;
2107 }
2108 else
2109 sPriv->extensions = dri_screen_extensions;
2110
2111 configs = dri_init_screen_helper(screen, pscreen);
2112 if (!configs)
2113 goto destroy_screen;
2114
2115 screen->can_share_buffer = true;
2116 screen->auto_fake_front = dri_with_format(sPriv);
2117 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2118 screen->lookup_egl_image = dri2_lookup_egl_image;
2119
2120 return configs;
2121
2122 destroy_screen:
2123 dri_destroy_screen_helper(screen);
2124
2125 release_pipe:
2126 if (screen->dev)
2127 pipe_loader_release(&screen->dev, 1);
2128
2129 FREE(screen);
2130 return NULL;
2131 }
2132
2133 /**
2134 * This is the driver specific part of the createNewScreen entry point.
2135 *
2136 * Returns the struct gl_config supported by this driver.
2137 */
2138 static const __DRIconfig **
2139 dri_kms_init_screen(__DRIscreen * sPriv)
2140 {
2141 #if defined(GALLIUM_SOFTPIPE)
2142 const __DRIconfig **configs;
2143 struct dri_screen *screen;
2144 struct pipe_screen *pscreen = NULL;
2145 uint64_t cap;
2146
2147 screen = CALLOC_STRUCT(dri_screen);
2148 if (!screen)
2149 return NULL;
2150
2151 screen->sPriv = sPriv;
2152 screen->fd = sPriv->fd;
2153
2154 sPriv->driverPrivate = (void *)screen;
2155
2156 if (pipe_loader_sw_probe_kms(&screen->dev, screen->fd)) {
2157 dri_init_options(screen);
2158 pscreen = pipe_loader_create_screen(screen->dev);
2159 }
2160
2161 if (!pscreen)
2162 goto release_pipe;
2163
2164 if (pscreen->resource_create_with_modifiers)
2165 dri2ImageExtension.createImageWithModifiers =
2166 dri2_create_image_with_modifiers;
2167
2168 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
2169 (cap & DRM_PRIME_CAP_IMPORT)) {
2170 dri2ImageExtension.createImageFromFds = dri2_from_fds;
2171 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
2172 dri2ImageExtension.createImageFromDmaBufs2 = dri2_from_dma_bufs2;
2173 if (pscreen->query_dmabuf_modifiers) {
2174 dri2ImageExtension.queryDmaBufFormats = dri2_query_dma_buf_formats;
2175 dri2ImageExtension.queryDmaBufModifiers = dri2_query_dma_buf_modifiers;
2176 }
2177 }
2178
2179 sPriv->extensions = dri_screen_extensions;
2180
2181 configs = dri_init_screen_helper(screen, pscreen);
2182 if (!configs)
2183 goto destroy_screen;
2184
2185 screen->can_share_buffer = false;
2186 screen->auto_fake_front = dri_with_format(sPriv);
2187 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
2188 screen->lookup_egl_image = dri2_lookup_egl_image;
2189
2190 return configs;
2191
2192 destroy_screen:
2193 dri_destroy_screen_helper(screen);
2194
2195 release_pipe:
2196 if (screen->dev)
2197 pipe_loader_release(&screen->dev, 1);
2198
2199 FREE(screen);
2200 #endif // GALLIUM_SOFTPIPE
2201 return NULL;
2202 }
2203
2204 static boolean
2205 dri2_create_buffer(__DRIscreen * sPriv,
2206 __DRIdrawable * dPriv,
2207 const struct gl_config * visual, boolean isPixmap)
2208 {
2209 struct dri_drawable *drawable = NULL;
2210
2211 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
2212 return FALSE;
2213
2214 drawable = dPriv->driverPrivate;
2215
2216 drawable->allocate_textures = dri2_allocate_textures;
2217 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
2218 drawable->update_tex_buffer = dri2_update_tex_buffer;
2219 drawable->flush_swapbuffers = dri2_flush_swapbuffers;
2220
2221 return TRUE;
2222 }
2223
2224 /**
2225 * DRI driver virtual function table.
2226 *
2227 * DRI versions differ in their implementation of init_screen and swap_buffers.
2228 */
2229 const struct __DriverAPIRec galliumdrm_driver_api = {
2230 .InitScreen = dri2_init_screen,
2231 .DestroyScreen = dri_destroy_screen,
2232 .CreateContext = dri_create_context,
2233 .DestroyContext = dri_destroy_context,
2234 .CreateBuffer = dri2_create_buffer,
2235 .DestroyBuffer = dri_destroy_buffer,
2236 .MakeCurrent = dri_make_current,
2237 .UnbindContext = dri_unbind_context,
2238
2239 .AllocateBuffer = dri2_allocate_buffer,
2240 .ReleaseBuffer = dri2_release_buffer,
2241 };
2242
2243 /**
2244 * DRI driver virtual function table.
2245 *
2246 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
2247 * hook. The latter is used to explicitly initialise the kms_swrast driver
2248 * rather than selecting the approapriate driver as suggested by the loader.
2249 */
2250 const struct __DriverAPIRec dri_kms_driver_api = {
2251 .InitScreen = dri_kms_init_screen,
2252 .DestroyScreen = dri_destroy_screen,
2253 .CreateContext = dri_create_context,
2254 .DestroyContext = dri_destroy_context,
2255 .CreateBuffer = dri2_create_buffer,
2256 .DestroyBuffer = dri_destroy_buffer,
2257 .MakeCurrent = dri_make_current,
2258 .UnbindContext = dri_unbind_context,
2259
2260 .AllocateBuffer = dri2_allocate_buffer,
2261 .ReleaseBuffer = dri2_release_buffer,
2262 };
2263
2264 /* This is the table of extensions that the loader will dlsym() for. */
2265 const __DRIextension *galliumdrm_driver_extensions[] = {
2266 &driCoreExtension.base,
2267 &driImageDriverExtension.base,
2268 &driDRI2Extension.base,
2269 &gallium_config_options.base,
2270 NULL
2271 };
2272
2273 /* vim: set sw=3 ts=8 sts=3 expandtab: */