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