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