st/dri: Factor out DRI2 to PIPE_FORMAT conversion
[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 <dlfcn.h>
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "util/u_format.h"
36 #include "util/u_debug.h"
37 #include "state_tracker/drm_driver.h"
38 #include "state_tracker/st_texture.h"
39 #include "state_tracker/st_context.h"
40 #include "pipe-loader/pipe_loader.h"
41 #include "main/texobj.h"
42
43 #include "dri_screen.h"
44 #include "dri_context.h"
45 #include "dri_drawable.h"
46 #include "dri_query_renderer.h"
47 #include "dri2_buffer.h"
48
49 static int convert_fourcc(int format, int *dri_components_p)
50 {
51 int dri_components;
52 switch(format) {
53 case __DRI_IMAGE_FOURCC_RGB565:
54 format = __DRI_IMAGE_FORMAT_RGB565;
55 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
56 break;
57 case __DRI_IMAGE_FOURCC_ARGB8888:
58 format = __DRI_IMAGE_FORMAT_ARGB8888;
59 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
60 break;
61 case __DRI_IMAGE_FOURCC_XRGB8888:
62 format = __DRI_IMAGE_FORMAT_XRGB8888;
63 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
64 break;
65 case __DRI_IMAGE_FOURCC_ABGR8888:
66 format = __DRI_IMAGE_FORMAT_ABGR8888;
67 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
68 break;
69 case __DRI_IMAGE_FOURCC_XBGR8888:
70 format = __DRI_IMAGE_FORMAT_XBGR8888;
71 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
72 break;
73 default:
74 return -1;
75 }
76 *dri_components_p = dri_components;
77 return format;
78 }
79
80 static int convert_to_fourcc(int format)
81 {
82 switch(format) {
83 case __DRI_IMAGE_FORMAT_RGB565:
84 format = __DRI_IMAGE_FOURCC_RGB565;
85 break;
86 case __DRI_IMAGE_FORMAT_ARGB8888:
87 format = __DRI_IMAGE_FOURCC_ARGB8888;
88 break;
89 case __DRI_IMAGE_FORMAT_XRGB8888:
90 format = __DRI_IMAGE_FOURCC_XRGB8888;
91 break;
92 case __DRI_IMAGE_FORMAT_ABGR8888:
93 format = __DRI_IMAGE_FOURCC_ABGR8888;
94 break;
95 case __DRI_IMAGE_FORMAT_XBGR8888:
96 format = __DRI_IMAGE_FOURCC_XBGR8888;
97 break;
98 default:
99 return -1;
100 }
101 return format;
102 }
103
104 static enum pipe_format dri2_format_to_pipe_format (int format)
105 {
106 enum pipe_format pf;
107
108 switch (format) {
109 case __DRI_IMAGE_FORMAT_RGB565:
110 pf = PIPE_FORMAT_B5G6R5_UNORM;
111 break;
112 case __DRI_IMAGE_FORMAT_XRGB8888:
113 pf = PIPE_FORMAT_BGRX8888_UNORM;
114 break;
115 case __DRI_IMAGE_FORMAT_ARGB8888:
116 pf = PIPE_FORMAT_BGRA8888_UNORM;
117 break;
118 case __DRI_IMAGE_FORMAT_ABGR8888:
119 pf = PIPE_FORMAT_RGBA8888_UNORM;
120 break;
121 default:
122 pf = PIPE_FORMAT_NONE;
123 break;
124 }
125
126 return pf;
127 }
128
129 /**
130 * DRI2 flush extension.
131 */
132 static void
133 dri2_flush_drawable(__DRIdrawable *dPriv)
134 {
135 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
136 }
137
138 static void
139 dri2_invalidate_drawable(__DRIdrawable *dPriv)
140 {
141 struct dri_drawable *drawable = dri_drawable(dPriv);
142
143 dri2InvalidateDrawable(dPriv);
144 drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
145
146 p_atomic_inc(&drawable->base.stamp);
147 }
148
149 static const __DRI2flushExtension dri2FlushExtension = {
150 .base = { __DRI2_FLUSH, 4 },
151
152 .flush = dri2_flush_drawable,
153 .invalidate = dri2_invalidate_drawable,
154 .flush_with_flags = dri_flush,
155 };
156
157 /**
158 * Retrieve __DRIbuffer from the DRI loader.
159 */
160 static __DRIbuffer *
161 dri2_drawable_get_buffers(struct dri_drawable *drawable,
162 const enum st_attachment_type *atts,
163 unsigned *count)
164 {
165 __DRIdrawable *dri_drawable = drawable->dPriv;
166 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
167 boolean with_format;
168 __DRIbuffer *buffers;
169 int num_buffers;
170 unsigned attachments[10];
171 unsigned num_attachments, i;
172
173 assert(loader);
174 with_format = dri_with_format(drawable->sPriv);
175
176 num_attachments = 0;
177
178 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
179 if (!with_format)
180 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
181
182 for (i = 0; i < *count; i++) {
183 enum pipe_format format;
184 unsigned bind;
185 int att, depth;
186
187 dri_drawable_get_format(drawable, atts[i], &format, &bind);
188 if (format == PIPE_FORMAT_NONE)
189 continue;
190
191 switch (atts[i]) {
192 case ST_ATTACHMENT_FRONT_LEFT:
193 /* already added */
194 if (!with_format)
195 continue;
196 att = __DRI_BUFFER_FRONT_LEFT;
197 break;
198 case ST_ATTACHMENT_BACK_LEFT:
199 att = __DRI_BUFFER_BACK_LEFT;
200 break;
201 case ST_ATTACHMENT_FRONT_RIGHT:
202 att = __DRI_BUFFER_FRONT_RIGHT;
203 break;
204 case ST_ATTACHMENT_BACK_RIGHT:
205 att = __DRI_BUFFER_BACK_RIGHT;
206 break;
207 default:
208 continue;
209 }
210
211 /*
212 * In this switch statement we must support all formats that
213 * may occur as the stvis->color_format.
214 */
215 switch(format) {
216 case PIPE_FORMAT_BGRA8888_UNORM:
217 depth = 32;
218 break;
219 case PIPE_FORMAT_BGRX8888_UNORM:
220 depth = 24;
221 break;
222 case PIPE_FORMAT_B5G6R5_UNORM:
223 depth = 16;
224 break;
225 default:
226 depth = util_format_get_blocksizebits(format);
227 assert(!"Unexpected format in dri2_drawable_get_buffers()");
228 }
229
230 attachments[num_attachments++] = att;
231 if (with_format) {
232 attachments[num_attachments++] = depth;
233 }
234 }
235
236 if (with_format) {
237 num_attachments /= 2;
238 buffers = loader->getBuffersWithFormat(dri_drawable,
239 &dri_drawable->w, &dri_drawable->h,
240 attachments, num_attachments,
241 &num_buffers, dri_drawable->loaderPrivate);
242 }
243 else {
244 buffers = loader->getBuffers(dri_drawable,
245 &dri_drawable->w, &dri_drawable->h,
246 attachments, num_attachments,
247 &num_buffers, dri_drawable->loaderPrivate);
248 }
249
250 if (buffers)
251 *count = num_buffers;
252
253 return buffers;
254 }
255
256 static bool
257 dri_image_drawable_get_buffers(struct dri_drawable *drawable,
258 struct __DRIimageList *images,
259 const enum st_attachment_type *statts,
260 unsigned statts_count)
261 {
262 __DRIdrawable *dPriv = drawable->dPriv;
263 __DRIscreen *sPriv = drawable->sPriv;
264 unsigned int image_format = __DRI_IMAGE_FORMAT_NONE;
265 enum pipe_format pf;
266 uint32_t buffer_mask = 0;
267 unsigned i, bind;
268
269 for (i = 0; i < statts_count; i++) {
270 dri_drawable_get_format(drawable, statts[i], &pf, &bind);
271 if (pf == PIPE_FORMAT_NONE)
272 continue;
273
274 switch (statts[i]) {
275 case ST_ATTACHMENT_FRONT_LEFT:
276 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
277 break;
278 case ST_ATTACHMENT_BACK_LEFT:
279 buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
280 break;
281 default:
282 continue;
283 }
284
285 switch (pf) {
286 case PIPE_FORMAT_B5G6R5_UNORM:
287 image_format = __DRI_IMAGE_FORMAT_RGB565;
288 break;
289 case PIPE_FORMAT_BGRX8888_UNORM:
290 image_format = __DRI_IMAGE_FORMAT_XRGB8888;
291 break;
292 case PIPE_FORMAT_BGRA8888_UNORM:
293 image_format = __DRI_IMAGE_FORMAT_ARGB8888;
294 break;
295 case PIPE_FORMAT_RGBA8888_UNORM:
296 image_format = __DRI_IMAGE_FORMAT_ABGR8888;
297 break;
298 default:
299 image_format = __DRI_IMAGE_FORMAT_NONE;
300 break;
301 }
302 }
303
304 return (*sPriv->image.loader->getBuffers) (dPriv, image_format,
305 (uint32_t *) &drawable->base.stamp,
306 dPriv->loaderPrivate, buffer_mask,
307 images);
308 }
309
310 static __DRIbuffer *
311 dri2_allocate_buffer(__DRIscreen *sPriv,
312 unsigned attachment, unsigned format,
313 int width, int height)
314 {
315 struct dri_screen *screen = dri_screen(sPriv);
316 struct dri2_buffer *buffer;
317 struct pipe_resource templ;
318 enum pipe_format pf;
319 unsigned bind = 0;
320 struct winsys_handle whandle;
321
322 switch (attachment) {
323 case __DRI_BUFFER_FRONT_LEFT:
324 case __DRI_BUFFER_FAKE_FRONT_LEFT:
325 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
326 break;
327 case __DRI_BUFFER_BACK_LEFT:
328 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
329 break;
330 case __DRI_BUFFER_DEPTH:
331 case __DRI_BUFFER_DEPTH_STENCIL:
332 case __DRI_BUFFER_STENCIL:
333 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
334 break;
335 }
336
337 /* because we get the handle and stride */
338 bind |= PIPE_BIND_SHARED;
339
340 switch (format) {
341 case 32:
342 pf = PIPE_FORMAT_BGRA8888_UNORM;
343 break;
344 case 24:
345 pf = PIPE_FORMAT_BGRX8888_UNORM;
346 break;
347 case 16:
348 pf = PIPE_FORMAT_Z16_UNORM;
349 break;
350 default:
351 return NULL;
352 }
353
354 buffer = CALLOC_STRUCT(dri2_buffer);
355 if (!buffer)
356 return NULL;
357
358 memset(&templ, 0, sizeof(templ));
359 templ.bind = bind;
360 templ.format = pf;
361 templ.target = PIPE_TEXTURE_2D;
362 templ.last_level = 0;
363 templ.width0 = width;
364 templ.height0 = height;
365 templ.depth0 = 1;
366 templ.array_size = 1;
367
368 buffer->resource =
369 screen->base.screen->resource_create(screen->base.screen, &templ);
370 if (!buffer->resource) {
371 FREE(buffer);
372 return NULL;
373 }
374
375 memset(&whandle, 0, sizeof(whandle));
376 if (screen->can_share_buffer)
377 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
378 else
379 whandle.type = DRM_API_HANDLE_TYPE_KMS;
380
381 screen->base.screen->resource_get_handle(screen->base.screen,
382 buffer->resource, &whandle,
383 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
384
385 buffer->base.attachment = attachment;
386 buffer->base.name = whandle.handle;
387 buffer->base.cpp = util_format_get_blocksize(pf);
388 buffer->base.pitch = whandle.stride;
389
390 return &buffer->base;
391 }
392
393 static void
394 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
395 {
396 struct dri2_buffer *buffer = dri2_buffer(bPriv);
397
398 pipe_resource_reference(&buffer->resource, NULL);
399 FREE(buffer);
400 }
401
402 /*
403 * Backend functions for st_framebuffer interface.
404 */
405
406 static void
407 dri2_allocate_textures(struct dri_context *ctx,
408 struct dri_drawable *drawable,
409 const enum st_attachment_type *statts,
410 unsigned statts_count)
411 {
412 __DRIscreen *sPriv = drawable->sPriv;
413 __DRIdrawable *dri_drawable = drawable->dPriv;
414 struct dri_screen *screen = dri_screen(sPriv);
415 struct pipe_resource templ;
416 boolean alloc_depthstencil = FALSE;
417 unsigned i, j, bind;
418 const __DRIimageLoaderExtension *image = sPriv->image.loader;
419 /* Image specific variables */
420 struct __DRIimageList images;
421 /* Dri2 specific variables */
422 __DRIbuffer *buffers = NULL;
423 struct winsys_handle whandle;
424 unsigned num_buffers = statts_count;
425
426 /* First get the buffers from the loader */
427 if (image) {
428 if (!dri_image_drawable_get_buffers(drawable, &images,
429 statts, statts_count))
430 return;
431 }
432 else {
433 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
434 if (!buffers || (drawable->old_num == num_buffers &&
435 drawable->old_w == dri_drawable->w &&
436 drawable->old_h == dri_drawable->h &&
437 memcmp(drawable->old, buffers,
438 sizeof(__DRIbuffer) * num_buffers) == 0))
439 return;
440 }
441
442 /* Second clean useless resources*/
443
444 /* See if we need a depth-stencil buffer. */
445 for (i = 0; i < statts_count; i++) {
446 if (statts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
447 alloc_depthstencil = TRUE;
448 break;
449 }
450 }
451
452 /* Delete the resources we won't need. */
453 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
454 /* Don't delete the depth-stencil buffer, we can reuse it. */
455 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
456 continue;
457
458 /* Flush the texture before unreferencing, so that other clients can
459 * see what the driver has rendered.
460 */
461 if (i != ST_ATTACHMENT_DEPTH_STENCIL && drawable->textures[i]) {
462 struct pipe_context *pipe = ctx->st->pipe;
463 pipe->flush_resource(pipe, drawable->textures[i]);
464 }
465
466 pipe_resource_reference(&drawable->textures[i], NULL);
467 }
468
469 if (drawable->stvis.samples > 1) {
470 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
471 boolean del = TRUE;
472
473 /* Don't delete MSAA resources for the attachments which are enabled,
474 * we can reuse them. */
475 for (j = 0; j < statts_count; j++) {
476 if (i == statts[j]) {
477 del = FALSE;
478 break;
479 }
480 }
481
482 if (del) {
483 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
484 }
485 }
486 }
487
488 /* Third use the buffers retrieved to fill the drawable info */
489
490 memset(&templ, 0, sizeof(templ));
491 templ.target = screen->target;
492 templ.last_level = 0;
493 templ.depth0 = 1;
494 templ.array_size = 1;
495
496 if (image) {
497 if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
498 struct pipe_resource **buf =
499 &drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
500 struct pipe_resource *texture = images.front->texture;
501
502 dri_drawable->w = texture->width0;
503 dri_drawable->h = texture->height0;
504
505 pipe_resource_reference(buf, texture);
506 }
507
508 if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
509 struct pipe_resource **buf =
510 &drawable->textures[ST_ATTACHMENT_BACK_LEFT];
511 struct pipe_resource *texture = images.back->texture;
512
513 dri_drawable->w = texture->width0;
514 dri_drawable->h = texture->height0;
515
516 pipe_resource_reference(buf, texture);
517 }
518
519 /* Note: if there is both a back and a front buffer,
520 * then they have the same size.
521 */
522 templ.width0 = dri_drawable->w;
523 templ.height0 = dri_drawable->h;
524 }
525 else {
526 memset(&whandle, 0, sizeof(whandle));
527
528 /* Process DRI-provided buffers and get pipe_resources. */
529 for (i = 0; i < num_buffers; i++) {
530 __DRIbuffer *buf = &buffers[i];
531 enum st_attachment_type statt;
532 enum pipe_format format;
533
534 switch (buf->attachment) {
535 case __DRI_BUFFER_FRONT_LEFT:
536 if (!screen->auto_fake_front) {
537 continue; /* invalid attachment */
538 }
539 /* fallthrough */
540 case __DRI_BUFFER_FAKE_FRONT_LEFT:
541 statt = ST_ATTACHMENT_FRONT_LEFT;
542 break;
543 case __DRI_BUFFER_BACK_LEFT:
544 statt = ST_ATTACHMENT_BACK_LEFT;
545 break;
546 default:
547 continue; /* invalid attachment */
548 }
549
550 dri_drawable_get_format(drawable, statt, &format, &bind);
551 if (format == PIPE_FORMAT_NONE)
552 continue;
553
554 /* dri2_drawable_get_buffers has already filled dri_drawable->w
555 * and dri_drawable->h */
556 templ.width0 = dri_drawable->w;
557 templ.height0 = dri_drawable->h;
558 templ.format = format;
559 templ.bind = bind;
560 whandle.handle = buf->name;
561 whandle.stride = buf->pitch;
562 whandle.offset = 0;
563 if (screen->can_share_buffer)
564 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
565 else
566 whandle.type = DRM_API_HANDLE_TYPE_KMS;
567 drawable->textures[statt] =
568 screen->base.screen->resource_from_handle(screen->base.screen,
569 &templ, &whandle,
570 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
571 assert(drawable->textures[statt]);
572 }
573 }
574
575 /* Allocate private MSAA colorbuffers. */
576 if (drawable->stvis.samples > 1) {
577 for (i = 0; i < statts_count; i++) {
578 enum st_attachment_type statt = statts[i];
579
580 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
581 continue;
582
583 if (drawable->textures[statt]) {
584 templ.format = drawable->textures[statt]->format;
585 templ.bind = drawable->textures[statt]->bind & ~PIPE_BIND_SCANOUT;
586 templ.nr_samples = drawable->stvis.samples;
587
588 /* Try to reuse the resource.
589 * (the other resource parameters should be constant)
590 */
591 if (!drawable->msaa_textures[statt] ||
592 drawable->msaa_textures[statt]->width0 != templ.width0 ||
593 drawable->msaa_textures[statt]->height0 != templ.height0) {
594 /* Allocate a new one. */
595 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
596
597 drawable->msaa_textures[statt] =
598 screen->base.screen->resource_create(screen->base.screen,
599 &templ);
600 assert(drawable->msaa_textures[statt]);
601
602 /* If there are any MSAA resources, we should initialize them
603 * such that they contain the same data as the single-sample
604 * resources we just got from the X server.
605 *
606 * The reason for this is that the state tracker (and
607 * therefore the app) can access the MSAA resources only.
608 * The single-sample resources are not exposed
609 * to the state tracker.
610 *
611 */
612 dri_pipe_blit(ctx->st->pipe,
613 drawable->msaa_textures[statt],
614 drawable->textures[statt]);
615 }
616 }
617 else {
618 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
619 }
620 }
621 }
622
623 /* Allocate a private depth-stencil buffer. */
624 if (alloc_depthstencil) {
625 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
626 struct pipe_resource **zsbuf;
627 enum pipe_format format;
628 unsigned bind;
629
630 dri_drawable_get_format(drawable, statt, &format, &bind);
631
632 if (format) {
633 templ.format = format;
634 templ.bind = bind;
635
636 if (drawable->stvis.samples > 1) {
637 templ.nr_samples = drawable->stvis.samples;
638 zsbuf = &drawable->msaa_textures[statt];
639 }
640 else {
641 templ.nr_samples = 0;
642 zsbuf = &drawable->textures[statt];
643 }
644
645 /* Try to reuse the resource.
646 * (the other resource parameters should be constant)
647 */
648 if (!*zsbuf ||
649 (*zsbuf)->width0 != templ.width0 ||
650 (*zsbuf)->height0 != templ.height0) {
651 /* Allocate a new one. */
652 pipe_resource_reference(zsbuf, NULL);
653 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
654 &templ);
655 assert(*zsbuf);
656 }
657 }
658 else {
659 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
660 pipe_resource_reference(&drawable->textures[statt], NULL);
661 }
662 }
663
664 /* For DRI2, we may get the same buffers again from the server.
665 * To prevent useless imports of gem names, drawable->old* is used
666 * to bypass the import if we get the same buffers. This doesn't apply
667 * to DRI3/Wayland, users of image.loader, since the buffer is managed
668 * by the client (no import), and the back buffer is going to change
669 * at every redraw.
670 */
671 if (!image) {
672 drawable->old_num = num_buffers;
673 drawable->old_w = dri_drawable->w;
674 drawable->old_h = dri_drawable->h;
675 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
676 }
677 }
678
679 static void
680 dri2_flush_frontbuffer(struct dri_context *ctx,
681 struct dri_drawable *drawable,
682 enum st_attachment_type statt)
683 {
684 __DRIdrawable *dri_drawable = drawable->dPriv;
685 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
686 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
687 struct pipe_context *pipe = ctx->st->pipe;
688
689 if (statt != ST_ATTACHMENT_FRONT_LEFT)
690 return;
691
692 if (drawable->stvis.samples > 1) {
693 /* Resolve the front buffer. */
694 dri_pipe_blit(ctx->st->pipe,
695 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
696 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
697 }
698
699 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
700 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
701 }
702
703 pipe->flush(pipe, NULL, 0);
704
705 if (image) {
706 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
707 }
708 else if (loader->flushFrontBuffer) {
709 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
710 }
711 }
712
713 static void
714 dri2_update_tex_buffer(struct dri_drawable *drawable,
715 struct dri_context *ctx,
716 struct pipe_resource *res)
717 {
718 /* no-op */
719 }
720
721 static __DRIimage *
722 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
723 {
724 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
725 __DRIimage *img;
726
727 if (!loader->lookupEGLImage)
728 return NULL;
729
730 img = loader->lookupEGLImage(screen->sPriv,
731 handle, screen->sPriv->loaderPrivate);
732
733 return img;
734 }
735
736 static __DRIimage *
737 dri2_create_image_from_winsys(__DRIscreen *_screen,
738 int width, int height, int format,
739 struct winsys_handle *whandle, int pitch,
740 void *loaderPrivate)
741 {
742 struct dri_screen *screen = dri_screen(_screen);
743 __DRIimage *img;
744 struct pipe_resource templ;
745 unsigned tex_usage;
746 enum pipe_format pf;
747
748 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
749
750 pf = dri2_format_to_pipe_format (format);
751 if (pf == PIPE_FORMAT_NONE)
752 return NULL;
753
754 img = CALLOC_STRUCT(__DRIimageRec);
755 if (!img)
756 return NULL;
757
758 memset(&templ, 0, sizeof(templ));
759 templ.bind = tex_usage;
760 templ.format = pf;
761 templ.target = screen->target;
762 templ.last_level = 0;
763 templ.width0 = width;
764 templ.height0 = height;
765 templ.depth0 = 1;
766 templ.array_size = 1;
767
768 whandle->stride = pitch * util_format_get_blocksize(pf);
769 whandle->offset = 0;
770
771 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
772 &templ, whandle, PIPE_HANDLE_USAGE_READ_WRITE);
773 if (!img->texture) {
774 FREE(img);
775 return NULL;
776 }
777
778 img->level = 0;
779 img->layer = 0;
780 img->dri_format = format;
781 img->use = 0;
782 img->loader_private = loaderPrivate;
783
784 return img;
785 }
786
787 static __DRIimage *
788 dri2_create_image_from_name(__DRIscreen *_screen,
789 int width, int height, int format,
790 int name, int pitch, void *loaderPrivate)
791 {
792 struct winsys_handle whandle;
793
794 memset(&whandle, 0, sizeof(whandle));
795 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
796 whandle.handle = name;
797
798 return dri2_create_image_from_winsys(_screen, width, height, format,
799 &whandle, pitch, loaderPrivate);
800 }
801
802 static __DRIimage *
803 dri2_create_image_from_fd(__DRIscreen *_screen,
804 int width, int height, int format,
805 int fd, int pitch, void *loaderPrivate)
806 {
807 struct winsys_handle whandle;
808
809 if (fd < 0)
810 return NULL;
811
812 memset(&whandle, 0, sizeof(whandle));
813 whandle.type = DRM_API_HANDLE_TYPE_FD;
814 whandle.handle = (unsigned)fd;
815
816 return dri2_create_image_from_winsys(_screen, width, height, format,
817 &whandle, pitch, loaderPrivate);
818 }
819
820 static __DRIimage *
821 dri2_create_image_from_renderbuffer(__DRIcontext *context,
822 int renderbuffer, void *loaderPrivate)
823 {
824 struct dri_context *ctx = dri_context(context);
825
826 if (!ctx->st->get_resource_for_egl_image)
827 return NULL;
828
829 /* TODO */
830 return NULL;
831 }
832
833 static __DRIimage *
834 dri2_create_image(__DRIscreen *_screen,
835 int width, int height, int format,
836 unsigned int use, void *loaderPrivate)
837 {
838 struct dri_screen *screen = dri_screen(_screen);
839 __DRIimage *img;
840 struct pipe_resource templ;
841 unsigned tex_usage;
842 enum pipe_format pf;
843
844 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
845 if (use & __DRI_IMAGE_USE_SCANOUT)
846 tex_usage |= PIPE_BIND_SCANOUT;
847 if (use & __DRI_IMAGE_USE_SHARE)
848 tex_usage |= PIPE_BIND_SHARED;
849 if (use & __DRI_IMAGE_USE_LINEAR)
850 tex_usage |= PIPE_BIND_LINEAR;
851 if (use & __DRI_IMAGE_USE_CURSOR) {
852 if (width != 64 || height != 64)
853 return NULL;
854 tex_usage |= PIPE_BIND_CURSOR;
855 }
856
857 pf = dri2_format_to_pipe_format (format);
858 if (pf == PIPE_FORMAT_NONE)
859 return NULL;
860
861 img = CALLOC_STRUCT(__DRIimageRec);
862 if (!img)
863 return NULL;
864
865 memset(&templ, 0, sizeof(templ));
866 templ.bind = tex_usage;
867 templ.format = pf;
868 templ.target = PIPE_TEXTURE_2D;
869 templ.last_level = 0;
870 templ.width0 = width;
871 templ.height0 = height;
872 templ.depth0 = 1;
873 templ.array_size = 1;
874
875 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
876 if (!img->texture) {
877 FREE(img);
878 return NULL;
879 }
880
881 img->level = 0;
882 img->layer = 0;
883 img->dri_format = format;
884 img->dri_components = 0;
885 img->use = use;
886
887 img->loader_private = loaderPrivate;
888 return img;
889 }
890
891 static GLboolean
892 dri2_query_image(__DRIimage *image, int attrib, int *value)
893 {
894 struct winsys_handle whandle;
895 unsigned usage;
896
897 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
898 usage = PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ;
899 else
900 usage = PIPE_HANDLE_USAGE_READ_WRITE;
901
902 memset(&whandle, 0, sizeof(whandle));
903
904 switch (attrib) {
905 case __DRI_IMAGE_ATTRIB_STRIDE:
906 whandle.type = DRM_API_HANDLE_TYPE_KMS;
907 image->texture->screen->resource_get_handle(image->texture->screen,
908 image->texture, &whandle, usage);
909 *value = whandle.stride;
910 return GL_TRUE;
911 case __DRI_IMAGE_ATTRIB_HANDLE:
912 whandle.type = DRM_API_HANDLE_TYPE_KMS;
913 image->texture->screen->resource_get_handle(image->texture->screen,
914 image->texture, &whandle, usage);
915 *value = whandle.handle;
916 return GL_TRUE;
917 case __DRI_IMAGE_ATTRIB_NAME:
918 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
919 image->texture->screen->resource_get_handle(image->texture->screen,
920 image->texture, &whandle, usage);
921 *value = whandle.handle;
922 return GL_TRUE;
923 case __DRI_IMAGE_ATTRIB_FD:
924 whandle.type= DRM_API_HANDLE_TYPE_FD;
925 image->texture->screen->resource_get_handle(image->texture->screen,
926 image->texture, &whandle, usage);
927 *value = whandle.handle;
928 return GL_TRUE;
929 case __DRI_IMAGE_ATTRIB_FORMAT:
930 *value = image->dri_format;
931 return GL_TRUE;
932 case __DRI_IMAGE_ATTRIB_WIDTH:
933 *value = image->texture->width0;
934 return GL_TRUE;
935 case __DRI_IMAGE_ATTRIB_HEIGHT:
936 *value = image->texture->height0;
937 return GL_TRUE;
938 case __DRI_IMAGE_ATTRIB_COMPONENTS:
939 if (image->dri_components == 0)
940 return GL_FALSE;
941 *value = image->dri_components;
942 return GL_TRUE;
943 case __DRI_IMAGE_ATTRIB_FOURCC:
944 *value = convert_to_fourcc(image->dri_format);
945 return GL_TRUE;
946 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
947 *value = 1;
948 return GL_TRUE;
949 default:
950 return GL_FALSE;
951 }
952 }
953
954 static __DRIimage *
955 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
956 {
957 __DRIimage *img;
958
959 img = CALLOC_STRUCT(__DRIimageRec);
960 if (!img)
961 return NULL;
962
963 img->texture = NULL;
964 pipe_resource_reference(&img->texture, image->texture);
965 img->level = image->level;
966 img->layer = image->layer;
967 img->dri_format = image->dri_format;
968 /* This should be 0 for sub images, but dup is also used for base images. */
969 img->dri_components = image->dri_components;
970 img->loader_private = loaderPrivate;
971
972 return img;
973 }
974
975 static GLboolean
976 dri2_validate_usage(__DRIimage *image, unsigned int use)
977 {
978 /*
979 * Gallium drivers are bad at adding usages to the resources
980 * once opened again in another process, which is the main use
981 * case for this, so we have to lie.
982 */
983 if (image != NULL)
984 return GL_TRUE;
985 else
986 return GL_FALSE;
987 }
988
989 static __DRIimage *
990 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
991 int *names, int num_names, int *strides, int *offsets,
992 void *loaderPrivate)
993 {
994 __DRIimage *img;
995 int stride, dri_components;
996
997 if (num_names != 1)
998 return NULL;
999 if (offsets[0] != 0)
1000 return NULL;
1001
1002 format = convert_fourcc(format, &dri_components);
1003 if (format == -1)
1004 return NULL;
1005
1006 /* Strides are in bytes not pixels. */
1007 stride = strides[0] /4;
1008
1009 img = dri2_create_image_from_name(screen, width, height, format,
1010 names[0], stride, loaderPrivate);
1011 if (img == NULL)
1012 return NULL;
1013
1014 img->dri_components = dri_components;
1015 return img;
1016 }
1017
1018 static __DRIimage *
1019 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1020 {
1021 __DRIimage *img;
1022
1023 if (plane != 0)
1024 return NULL;
1025
1026 if (image->dri_components == 0)
1027 return NULL;
1028
1029 img = dri2_dup_image(image, loaderPrivate);
1030 if (img == NULL)
1031 return NULL;
1032
1033 /* set this to 0 for sub images. */
1034 img->dri_components = 0;
1035 return img;
1036 }
1037
1038 static __DRIimage *
1039 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1040 int depth, int level, unsigned *error,
1041 void *loaderPrivate)
1042 {
1043 __DRIimage *img;
1044 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1045 struct gl_texture_object *obj;
1046 struct pipe_resource *tex;
1047 GLuint face = 0;
1048
1049 obj = _mesa_lookup_texture(ctx, texture);
1050 if (!obj || obj->Target != target) {
1051 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1052 return NULL;
1053 }
1054
1055 tex = st_get_texobj_resource(obj);
1056 if (!tex) {
1057 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1058 return NULL;
1059 }
1060
1061 if (target == GL_TEXTURE_CUBE_MAP)
1062 face = depth;
1063
1064 _mesa_test_texobj_completeness(ctx, obj);
1065 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1066 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1067 return NULL;
1068 }
1069
1070 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1071 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1072 return NULL;
1073 }
1074
1075 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1076 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1077 return NULL;
1078 }
1079
1080 img = CALLOC_STRUCT(__DRIimageRec);
1081 if (!img) {
1082 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1083 return NULL;
1084 }
1085
1086 img->level = level;
1087 img->layer = depth;
1088 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1089
1090 img->loader_private = loaderPrivate;
1091
1092 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1093 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1094 free(img);
1095 return NULL;
1096 }
1097
1098 pipe_resource_reference(&img->texture, tex);
1099
1100 *error = __DRI_IMAGE_ERROR_SUCCESS;
1101 return img;
1102 }
1103
1104 static __DRIimage *
1105 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1106 int *fds, int num_fds, int *strides, int *offsets,
1107 void *loaderPrivate)
1108 {
1109 __DRIimage *img;
1110 int format, stride, dri_components;
1111
1112 if (num_fds != 1)
1113 return NULL;
1114 if (offsets[0] != 0)
1115 return NULL;
1116
1117 format = convert_fourcc(fourcc, &dri_components);
1118 if (format == -1)
1119 return NULL;
1120
1121 /* Strides are in bytes not pixels. */
1122 stride = strides[0] /4;
1123
1124 img = dri2_create_image_from_fd(screen, width, height, format,
1125 fds[0], stride, loaderPrivate);
1126 if (img == NULL)
1127 return NULL;
1128
1129 img->dri_components = dri_components;
1130 return img;
1131 }
1132
1133 static __DRIimage *
1134 dri2_from_dma_bufs(__DRIscreen *screen,
1135 int width, int height, int fourcc,
1136 int *fds, int num_fds,
1137 int *strides, int *offsets,
1138 enum __DRIYUVColorSpace yuv_color_space,
1139 enum __DRISampleRange sample_range,
1140 enum __DRIChromaSiting horizontal_siting,
1141 enum __DRIChromaSiting vertical_siting,
1142 unsigned *error,
1143 void *loaderPrivate)
1144 {
1145 __DRIimage *img;
1146 int format, stride, dri_components;
1147
1148 if (num_fds != 1 || offsets[0] != 0) {
1149 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1150 return NULL;
1151 }
1152
1153 format = convert_fourcc(fourcc, &dri_components);
1154 if (format == -1) {
1155 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1156 return NULL;
1157 }
1158
1159 /* Strides are in bytes not pixels. */
1160 stride = strides[0] /4;
1161
1162 img = dri2_create_image_from_fd(screen, width, height, format,
1163 fds[0], stride, loaderPrivate);
1164 if (img == NULL) {
1165 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1166 return NULL;
1167 }
1168
1169 img->yuv_color_space = yuv_color_space;
1170 img->sample_range = sample_range;
1171 img->horizontal_siting = horizontal_siting;
1172 img->vertical_siting = vertical_siting;
1173 img->dri_components = dri_components;
1174
1175 *error = __DRI_IMAGE_ERROR_SUCCESS;
1176 return img;
1177 }
1178
1179 static void
1180 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1181 int dstx0, int dsty0, int dstwidth, int dstheight,
1182 int srcx0, int srcy0, int srcwidth, int srcheight,
1183 int flush_flag)
1184 {
1185 struct dri_context *ctx = dri_context(context);
1186 struct pipe_context *pipe = ctx->st->pipe;
1187 struct pipe_screen *screen;
1188 struct pipe_fence_handle *fence;
1189 struct pipe_blit_info blit;
1190
1191 if (!dst || !src)
1192 return;
1193
1194 memset(&blit, 0, sizeof(blit));
1195 blit.dst.resource = dst->texture;
1196 blit.dst.box.x = dstx0;
1197 blit.dst.box.y = dsty0;
1198 blit.dst.box.width = dstwidth;
1199 blit.dst.box.height = dstheight;
1200 blit.dst.box.depth = 1;
1201 blit.dst.format = dst->texture->format;
1202 blit.src.resource = src->texture;
1203 blit.src.box.x = srcx0;
1204 blit.src.box.y = srcy0;
1205 blit.src.box.width = srcwidth;
1206 blit.src.box.height = srcheight;
1207 blit.src.box.depth = 1;
1208 blit.src.format = src->texture->format;
1209 blit.mask = PIPE_MASK_RGBA;
1210 blit.filter = PIPE_TEX_FILTER_NEAREST;
1211
1212 pipe->blit(pipe, &blit);
1213
1214 if (flush_flag == __BLIT_FLAG_FLUSH) {
1215 pipe->flush_resource(pipe, dst->texture);
1216 ctx->st->flush(ctx->st, 0, NULL);
1217 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1218 screen = dri_screen(ctx->sPriv)->base.screen;
1219 pipe->flush_resource(pipe, dst->texture);
1220 ctx->st->flush(ctx->st, 0, &fence);
1221 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
1222 screen->fence_reference(screen, &fence, NULL);
1223 }
1224 }
1225
1226 static void
1227 dri2_destroy_image(__DRIimage *img)
1228 {
1229 pipe_resource_reference(&img->texture, NULL);
1230 FREE(img);
1231 }
1232
1233 static int
1234 dri2_get_capabilities(__DRIscreen *_screen)
1235 {
1236 struct dri_screen *screen = dri_screen(_screen);
1237
1238 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1239 }
1240
1241 /* The extension is modified during runtime if DRI_PRIME is detected */
1242 static __DRIimageExtension dri2ImageExtension = {
1243 .base = { __DRI_IMAGE, 11 },
1244
1245 .createImageFromName = dri2_create_image_from_name,
1246 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1247 .destroyImage = dri2_destroy_image,
1248 .createImage = dri2_create_image,
1249 .queryImage = dri2_query_image,
1250 .dupImage = dri2_dup_image,
1251 .validateUsage = dri2_validate_usage,
1252 .createImageFromNames = dri2_from_names,
1253 .fromPlanar = dri2_from_planar,
1254 .createImageFromTexture = dri2_create_from_texture,
1255 .createImageFromFds = NULL,
1256 .createImageFromDmaBufs = NULL,
1257 .blitImage = dri2_blit_image,
1258 .getCapabilities = dri2_get_capabilities,
1259 };
1260
1261
1262 static bool
1263 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
1264 {
1265 return screen->opencl_dri_event_add_ref &&
1266 screen->opencl_dri_event_release &&
1267 screen->opencl_dri_event_wait &&
1268 screen->opencl_dri_event_get_fence;
1269 }
1270
1271 static bool
1272 dri2_load_opencl_interop(struct dri_screen *screen)
1273 {
1274 #if defined(RTLD_DEFAULT)
1275 bool success;
1276
1277 pipe_mutex_lock(screen->opencl_func_mutex);
1278
1279 if (dri2_is_opencl_interop_loaded_locked(screen)) {
1280 pipe_mutex_unlock(screen->opencl_func_mutex);
1281 return true;
1282 }
1283
1284 screen->opencl_dri_event_add_ref =
1285 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
1286 screen->opencl_dri_event_release =
1287 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
1288 screen->opencl_dri_event_wait =
1289 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
1290 screen->opencl_dri_event_get_fence =
1291 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
1292
1293 success = dri2_is_opencl_interop_loaded_locked(screen);
1294 pipe_mutex_unlock(screen->opencl_func_mutex);
1295 return success;
1296 #else
1297 return false;
1298 #endif
1299 }
1300
1301 struct dri2_fence {
1302 struct dri_screen *driscreen;
1303 struct pipe_fence_handle *pipe_fence;
1304 void *cl_event;
1305 };
1306
1307 static void *
1308 dri2_create_fence(__DRIcontext *_ctx)
1309 {
1310 struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
1311 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
1312
1313 if (!fence)
1314 return NULL;
1315
1316 ctx->flush(ctx, &fence->pipe_fence, 0);
1317
1318 if (!fence->pipe_fence) {
1319 FREE(fence);
1320 return NULL;
1321 }
1322
1323 fence->driscreen = dri_screen(_ctx->driScreenPriv);
1324 return fence;
1325 }
1326
1327 static void *
1328 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
1329 {
1330 struct dri_screen *driscreen = dri_screen(_screen);
1331 struct dri2_fence *fence;
1332
1333 if (!dri2_load_opencl_interop(driscreen))
1334 return NULL;
1335
1336 fence = CALLOC_STRUCT(dri2_fence);
1337 if (!fence)
1338 return NULL;
1339
1340 fence->cl_event = (void*)cl_event;
1341
1342 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
1343 free(fence);
1344 return NULL;
1345 }
1346
1347 fence->driscreen = driscreen;
1348 return fence;
1349 }
1350
1351 static void
1352 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
1353 {
1354 struct dri_screen *driscreen = dri_screen(_screen);
1355 struct pipe_screen *screen = driscreen->base.screen;
1356 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1357
1358 if (fence->pipe_fence)
1359 screen->fence_reference(screen, &fence->pipe_fence, NULL);
1360 else if (fence->cl_event)
1361 driscreen->opencl_dri_event_release(fence->cl_event);
1362 else
1363 assert(0);
1364
1365 FREE(fence);
1366 }
1367
1368 static GLboolean
1369 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
1370 uint64_t timeout)
1371 {
1372 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1373 struct dri_screen *driscreen = fence->driscreen;
1374 struct pipe_screen *screen = driscreen->base.screen;
1375
1376 /* No need to flush. The context was flushed when the fence was created. */
1377
1378 if (fence->pipe_fence)
1379 return screen->fence_finish(screen, fence->pipe_fence, timeout);
1380 else if (fence->cl_event) {
1381 struct pipe_fence_handle *pipe_fence =
1382 driscreen->opencl_dri_event_get_fence(fence->cl_event);
1383
1384 if (pipe_fence)
1385 return screen->fence_finish(screen, pipe_fence, timeout);
1386 else
1387 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
1388 }
1389 else {
1390 assert(0);
1391 return false;
1392 }
1393 }
1394
1395 static void
1396 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
1397 {
1398 /* AFAIK, no driver currently supports parallel context execution. */
1399 }
1400
1401 static __DRI2fenceExtension dri2FenceExtension = {
1402 .base = { __DRI2_FENCE, 1 },
1403
1404 .create_fence = dri2_create_fence,
1405 .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
1406 .destroy_fence = dri2_destroy_fence,
1407 .client_wait_sync = dri2_client_wait_sync,
1408 .server_wait_sync = dri2_server_wait_sync
1409 };
1410
1411 static const __DRIrobustnessExtension dri2Robustness = {
1412 .base = { __DRI2_ROBUSTNESS, 1 }
1413 };
1414
1415 /*
1416 * Backend function init_screen.
1417 */
1418
1419 static const __DRIextension *dri_screen_extensions[] = {
1420 &driTexBufferExtension.base,
1421 &dri2FlushExtension.base,
1422 &dri2ImageExtension.base,
1423 &dri2RendererQueryExtension.base,
1424 &dri2ConfigQueryExtension.base,
1425 &dri2ThrottleExtension.base,
1426 &dri2FenceExtension.base,
1427 NULL
1428 };
1429
1430 static const __DRIextension *dri_robust_screen_extensions[] = {
1431 &driTexBufferExtension.base,
1432 &dri2FlushExtension.base,
1433 &dri2ImageExtension.base,
1434 &dri2RendererQueryExtension.base,
1435 &dri2ConfigQueryExtension.base,
1436 &dri2ThrottleExtension.base,
1437 &dri2FenceExtension.base,
1438 &dri2Robustness.base,
1439 NULL
1440 };
1441
1442 /**
1443 * This is the driver specific part of the createNewScreen entry point.
1444 *
1445 * Returns the struct gl_config supported by this driver.
1446 */
1447 static const __DRIconfig **
1448 dri2_init_screen(__DRIscreen * sPriv)
1449 {
1450 const __DRIconfig **configs;
1451 struct dri_screen *screen;
1452 struct pipe_screen *pscreen = NULL;
1453 const struct drm_conf_ret *throttle_ret;
1454 const struct drm_conf_ret *dmabuf_ret;
1455 int fd = -1;
1456
1457 screen = CALLOC_STRUCT(dri_screen);
1458 if (!screen)
1459 return NULL;
1460
1461 screen->sPriv = sPriv;
1462 screen->fd = sPriv->fd;
1463 pipe_mutex_init(screen->opencl_func_mutex);
1464
1465 sPriv->driverPrivate = (void *)screen;
1466
1467 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1468 goto fail;
1469
1470 if (pipe_loader_drm_probe_fd(&screen->dev, fd))
1471 pscreen = pipe_loader_create_screen(screen->dev);
1472
1473 if (!pscreen)
1474 goto fail;
1475
1476 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
1477 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
1478
1479 if (throttle_ret && throttle_ret->val.val_int != -1) {
1480 screen->throttling_enabled = TRUE;
1481 screen->default_throttle_frames = throttle_ret->val.val_int;
1482 }
1483
1484 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
1485 uint64_t cap;
1486
1487 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1488 (cap & DRM_PRIME_CAP_IMPORT)) {
1489 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1490 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1491 }
1492 }
1493
1494 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
1495 sPriv->extensions = dri_robust_screen_extensions;
1496 screen->has_reset_status_query = true;
1497 }
1498 else
1499 sPriv->extensions = dri_screen_extensions;
1500
1501 configs = dri_init_screen_helper(screen, pscreen, screen->dev->driver_name);
1502 if (!configs)
1503 goto fail;
1504
1505 screen->can_share_buffer = true;
1506 screen->auto_fake_front = dri_with_format(sPriv);
1507 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1508 screen->lookup_egl_image = dri2_lookup_egl_image;
1509
1510 return configs;
1511 fail:
1512 dri_destroy_screen_helper(screen);
1513 if (screen->dev)
1514 pipe_loader_release(&screen->dev, 1);
1515 else
1516 close(fd);
1517 FREE(screen);
1518 return NULL;
1519 }
1520
1521 /**
1522 * This is the driver specific part of the createNewScreen entry point.
1523 *
1524 * Returns the struct gl_config supported by this driver.
1525 */
1526 static const __DRIconfig **
1527 dri_kms_init_screen(__DRIscreen * sPriv)
1528 {
1529 #if defined(GALLIUM_SOFTPIPE)
1530 const __DRIconfig **configs;
1531 struct dri_screen *screen;
1532 struct pipe_screen *pscreen = NULL;
1533 uint64_t cap;
1534 int fd = -1;
1535
1536 screen = CALLOC_STRUCT(dri_screen);
1537 if (!screen)
1538 return NULL;
1539
1540 screen->sPriv = sPriv;
1541 screen->fd = sPriv->fd;
1542
1543 sPriv->driverPrivate = (void *)screen;
1544
1545 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1546 goto fail;
1547
1548 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
1549 pscreen = pipe_loader_create_screen(screen->dev);
1550
1551 if (!pscreen)
1552 goto fail;
1553
1554 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1555 (cap & DRM_PRIME_CAP_IMPORT)) {
1556 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1557 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1558 }
1559
1560 sPriv->extensions = dri_screen_extensions;
1561
1562 configs = dri_init_screen_helper(screen, pscreen, "swrast");
1563 if (!configs)
1564 goto fail;
1565
1566 screen->can_share_buffer = false;
1567 screen->auto_fake_front = dri_with_format(sPriv);
1568 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1569 screen->lookup_egl_image = dri2_lookup_egl_image;
1570
1571 return configs;
1572 fail:
1573 dri_destroy_screen_helper(screen);
1574 if (screen->dev)
1575 pipe_loader_release(&screen->dev, 1);
1576 else
1577 close(fd);
1578 FREE(screen);
1579 #endif // GALLIUM_SOFTPIPE
1580 return NULL;
1581 }
1582
1583 static boolean
1584 dri2_create_buffer(__DRIscreen * sPriv,
1585 __DRIdrawable * dPriv,
1586 const struct gl_config * visual, boolean isPixmap)
1587 {
1588 struct dri_drawable *drawable = NULL;
1589
1590 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
1591 return FALSE;
1592
1593 drawable = dPriv->driverPrivate;
1594
1595 drawable->allocate_textures = dri2_allocate_textures;
1596 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
1597 drawable->update_tex_buffer = dri2_update_tex_buffer;
1598
1599 return TRUE;
1600 }
1601
1602 /**
1603 * DRI driver virtual function table.
1604 *
1605 * DRI versions differ in their implementation of init_screen and swap_buffers.
1606 */
1607 const struct __DriverAPIRec galliumdrm_driver_api = {
1608 .InitScreen = dri2_init_screen,
1609 .DestroyScreen = dri_destroy_screen,
1610 .CreateContext = dri_create_context,
1611 .DestroyContext = dri_destroy_context,
1612 .CreateBuffer = dri2_create_buffer,
1613 .DestroyBuffer = dri_destroy_buffer,
1614 .MakeCurrent = dri_make_current,
1615 .UnbindContext = dri_unbind_context,
1616
1617 .AllocateBuffer = dri2_allocate_buffer,
1618 .ReleaseBuffer = dri2_release_buffer,
1619 };
1620
1621 /**
1622 * DRI driver virtual function table.
1623 *
1624 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1625 * hook. The latter is used to explicitly initialise the kms_swrast driver
1626 * rather than selecting the approapriate driver as suggested by the loader.
1627 */
1628 const struct __DriverAPIRec dri_kms_driver_api = {
1629 .InitScreen = dri_kms_init_screen,
1630 .DestroyScreen = dri_destroy_screen,
1631 .CreateContext = dri_create_context,
1632 .DestroyContext = dri_destroy_context,
1633 .CreateBuffer = dri2_create_buffer,
1634 .DestroyBuffer = dri_destroy_buffer,
1635 .MakeCurrent = dri_make_current,
1636 .UnbindContext = dri_unbind_context,
1637
1638 .AllocateBuffer = dri2_allocate_buffer,
1639 .ReleaseBuffer = dri2_release_buffer,
1640 };
1641
1642 /* This is the table of extensions that the loader will dlsym() for. */
1643 const __DRIextension *galliumdrm_driver_extensions[] = {
1644 &driCoreExtension.base,
1645 &driImageDriverExtension.base,
1646 &driDRI2Extension.base,
1647 &gallium_config_options.base,
1648 NULL
1649 };
1650
1651 /* vim: set sw=3 ts=8 sts=3 expandtab: */