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 if (screen->can_share_buffer)
538 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
539 else
540 whandle.type = DRM_API_HANDLE_TYPE_KMS;
541 drawable->textures[statt] =
542 screen->base.screen->resource_from_handle(screen->base.screen,
543 &templ, &whandle,
544 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ);
545 assert(drawable->textures[statt]);
546 }
547 }
548
549 /* Allocate private MSAA colorbuffers. */
550 if (drawable->stvis.samples > 1) {
551 for (i = 0; i < statts_count; i++) {
552 enum st_attachment_type statt = statts[i];
553
554 if (statt == ST_ATTACHMENT_DEPTH_STENCIL)
555 continue;
556
557 if (drawable->textures[statt]) {
558 templ.format = drawable->textures[statt]->format;
559 templ.bind = drawable->textures[statt]->bind & ~PIPE_BIND_SCANOUT;
560 templ.nr_samples = drawable->stvis.samples;
561
562 /* Try to reuse the resource.
563 * (the other resource parameters should be constant)
564 */
565 if (!drawable->msaa_textures[statt] ||
566 drawable->msaa_textures[statt]->width0 != templ.width0 ||
567 drawable->msaa_textures[statt]->height0 != templ.height0) {
568 /* Allocate a new one. */
569 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
570
571 drawable->msaa_textures[statt] =
572 screen->base.screen->resource_create(screen->base.screen,
573 &templ);
574 assert(drawable->msaa_textures[statt]);
575
576 /* If there are any MSAA resources, we should initialize them
577 * such that they contain the same data as the single-sample
578 * resources we just got from the X server.
579 *
580 * The reason for this is that the state tracker (and
581 * therefore the app) can access the MSAA resources only.
582 * The single-sample resources are not exposed
583 * to the state tracker.
584 *
585 */
586 dri_pipe_blit(ctx->st->pipe,
587 drawable->msaa_textures[statt],
588 drawable->textures[statt]);
589 }
590 }
591 else {
592 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
593 }
594 }
595 }
596
597 /* Allocate a private depth-stencil buffer. */
598 if (alloc_depthstencil) {
599 enum st_attachment_type statt = ST_ATTACHMENT_DEPTH_STENCIL;
600 struct pipe_resource **zsbuf;
601 enum pipe_format format;
602 unsigned bind;
603
604 dri_drawable_get_format(drawable, statt, &format, &bind);
605
606 if (format) {
607 templ.format = format;
608 templ.bind = bind;
609
610 if (drawable->stvis.samples > 1) {
611 templ.nr_samples = drawable->stvis.samples;
612 zsbuf = &drawable->msaa_textures[statt];
613 }
614 else {
615 templ.nr_samples = 0;
616 zsbuf = &drawable->textures[statt];
617 }
618
619 /* Try to reuse the resource.
620 * (the other resource parameters should be constant)
621 */
622 if (!*zsbuf ||
623 (*zsbuf)->width0 != templ.width0 ||
624 (*zsbuf)->height0 != templ.height0) {
625 /* Allocate a new one. */
626 pipe_resource_reference(zsbuf, NULL);
627 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
628 &templ);
629 assert(*zsbuf);
630 }
631 }
632 else {
633 pipe_resource_reference(&drawable->msaa_textures[statt], NULL);
634 pipe_resource_reference(&drawable->textures[statt], NULL);
635 }
636 }
637
638 /* For DRI2, we may get the same buffers again from the server.
639 * To prevent useless imports of gem names, drawable->old* is used
640 * to bypass the import if we get the same buffers. This doesn't apply
641 * to DRI3/Wayland, users of image.loader, since the buffer is managed
642 * by the client (no import), and the back buffer is going to change
643 * at every redraw.
644 */
645 if (!image) {
646 drawable->old_num = num_buffers;
647 drawable->old_w = dri_drawable->w;
648 drawable->old_h = dri_drawable->h;
649 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * num_buffers);
650 }
651 }
652
653 static void
654 dri2_flush_frontbuffer(struct dri_context *ctx,
655 struct dri_drawable *drawable,
656 enum st_attachment_type statt)
657 {
658 __DRIdrawable *dri_drawable = drawable->dPriv;
659 const __DRIimageLoaderExtension *image = drawable->sPriv->image.loader;
660 const __DRIdri2LoaderExtension *loader = drawable->sPriv->dri2.loader;
661 struct pipe_context *pipe = ctx->st->pipe;
662
663 if (statt != ST_ATTACHMENT_FRONT_LEFT)
664 return;
665
666 if (drawable->stvis.samples > 1) {
667 /* Resolve the front buffer. */
668 dri_pipe_blit(ctx->st->pipe,
669 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
670 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
671 }
672
673 if (drawable->textures[ST_ATTACHMENT_FRONT_LEFT]) {
674 pipe->flush_resource(pipe, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
675 }
676
677 pipe->flush(pipe, NULL, 0);
678
679 if (image) {
680 image->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
681 }
682 else if (loader->flushFrontBuffer) {
683 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
684 }
685 }
686
687 static void
688 dri2_update_tex_buffer(struct dri_drawable *drawable,
689 struct dri_context *ctx,
690 struct pipe_resource *res)
691 {
692 /* no-op */
693 }
694
695 static __DRIimage *
696 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
697 {
698 const __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
699 __DRIimage *img;
700
701 if (!loader->lookupEGLImage)
702 return NULL;
703
704 img = loader->lookupEGLImage(screen->sPriv,
705 handle, screen->sPriv->loaderPrivate);
706
707 return img;
708 }
709
710 static __DRIimage *
711 dri2_create_image_from_winsys(__DRIscreen *_screen,
712 int width, int height, int format,
713 struct winsys_handle *whandle, int pitch,
714 void *loaderPrivate)
715 {
716 struct dri_screen *screen = dri_screen(_screen);
717 __DRIimage *img;
718 struct pipe_resource templ;
719 unsigned tex_usage;
720 enum pipe_format pf;
721
722 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
723
724 switch (format) {
725 case __DRI_IMAGE_FORMAT_RGB565:
726 pf = PIPE_FORMAT_B5G6R5_UNORM;
727 break;
728 case __DRI_IMAGE_FORMAT_XRGB8888:
729 pf = PIPE_FORMAT_BGRX8888_UNORM;
730 break;
731 case __DRI_IMAGE_FORMAT_ARGB8888:
732 pf = PIPE_FORMAT_BGRA8888_UNORM;
733 break;
734 case __DRI_IMAGE_FORMAT_ABGR8888:
735 pf = PIPE_FORMAT_RGBA8888_UNORM;
736 break;
737 default:
738 pf = PIPE_FORMAT_NONE;
739 break;
740 }
741 if (pf == PIPE_FORMAT_NONE)
742 return NULL;
743
744 img = CALLOC_STRUCT(__DRIimageRec);
745 if (!img)
746 return NULL;
747
748 memset(&templ, 0, sizeof(templ));
749 templ.bind = tex_usage;
750 templ.format = pf;
751 templ.target = screen->target;
752 templ.last_level = 0;
753 templ.width0 = width;
754 templ.height0 = height;
755 templ.depth0 = 1;
756 templ.array_size = 1;
757
758 whandle->stride = pitch * util_format_get_blocksize(pf);
759
760 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
761 &templ, whandle, PIPE_HANDLE_USAGE_READ_WRITE);
762 if (!img->texture) {
763 FREE(img);
764 return NULL;
765 }
766
767 img->level = 0;
768 img->layer = 0;
769 img->dri_format = format;
770 img->use = 0;
771 img->loader_private = loaderPrivate;
772
773 return img;
774 }
775
776 static __DRIimage *
777 dri2_create_image_from_name(__DRIscreen *_screen,
778 int width, int height, int format,
779 int name, int pitch, void *loaderPrivate)
780 {
781 struct winsys_handle whandle;
782
783 memset(&whandle, 0, sizeof(whandle));
784 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
785 whandle.handle = name;
786
787 return dri2_create_image_from_winsys(_screen, width, height, format,
788 &whandle, pitch, loaderPrivate);
789 }
790
791 static __DRIimage *
792 dri2_create_image_from_fd(__DRIscreen *_screen,
793 int width, int height, int format,
794 int fd, int pitch, void *loaderPrivate)
795 {
796 struct winsys_handle whandle;
797
798 if (fd < 0)
799 return NULL;
800
801 memset(&whandle, 0, sizeof(whandle));
802 whandle.type = DRM_API_HANDLE_TYPE_FD;
803 whandle.handle = (unsigned)fd;
804
805 return dri2_create_image_from_winsys(_screen, width, height, format,
806 &whandle, pitch, loaderPrivate);
807 }
808
809 static __DRIimage *
810 dri2_create_image_from_renderbuffer(__DRIcontext *context,
811 int renderbuffer, void *loaderPrivate)
812 {
813 struct dri_context *ctx = dri_context(context);
814
815 if (!ctx->st->get_resource_for_egl_image)
816 return NULL;
817
818 /* TODO */
819 return NULL;
820 }
821
822 static __DRIimage *
823 dri2_create_image(__DRIscreen *_screen,
824 int width, int height, int format,
825 unsigned int use, void *loaderPrivate)
826 {
827 struct dri_screen *screen = dri_screen(_screen);
828 __DRIimage *img;
829 struct pipe_resource templ;
830 unsigned tex_usage;
831 enum pipe_format pf;
832
833 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
834 if (use & __DRI_IMAGE_USE_SCANOUT)
835 tex_usage |= PIPE_BIND_SCANOUT;
836 if (use & __DRI_IMAGE_USE_SHARE)
837 tex_usage |= PIPE_BIND_SHARED;
838 if (use & __DRI_IMAGE_USE_LINEAR)
839 tex_usage |= PIPE_BIND_LINEAR;
840 if (use & __DRI_IMAGE_USE_CURSOR) {
841 if (width != 64 || height != 64)
842 return NULL;
843 tex_usage |= PIPE_BIND_CURSOR;
844 }
845
846 switch (format) {
847 case __DRI_IMAGE_FORMAT_RGB565:
848 pf = PIPE_FORMAT_B5G6R5_UNORM;
849 break;
850 case __DRI_IMAGE_FORMAT_XRGB8888:
851 pf = PIPE_FORMAT_BGRX8888_UNORM;
852 break;
853 case __DRI_IMAGE_FORMAT_ARGB8888:
854 pf = PIPE_FORMAT_BGRA8888_UNORM;
855 break;
856 case __DRI_IMAGE_FORMAT_ABGR8888:
857 pf = PIPE_FORMAT_RGBA8888_UNORM;
858 break;
859 default:
860 pf = PIPE_FORMAT_NONE;
861 break;
862 }
863 if (pf == PIPE_FORMAT_NONE)
864 return NULL;
865
866 img = CALLOC_STRUCT(__DRIimageRec);
867 if (!img)
868 return NULL;
869
870 memset(&templ, 0, sizeof(templ));
871 templ.bind = tex_usage;
872 templ.format = pf;
873 templ.target = PIPE_TEXTURE_2D;
874 templ.last_level = 0;
875 templ.width0 = width;
876 templ.height0 = height;
877 templ.depth0 = 1;
878 templ.array_size = 1;
879
880 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
881 if (!img->texture) {
882 FREE(img);
883 return NULL;
884 }
885
886 img->level = 0;
887 img->layer = 0;
888 img->dri_format = format;
889 img->dri_components = 0;
890 img->use = use;
891
892 img->loader_private = loaderPrivate;
893 return img;
894 }
895
896 static GLboolean
897 dri2_query_image(__DRIimage *image, int attrib, int *value)
898 {
899 struct winsys_handle whandle;
900 unsigned usage;
901
902 if (image->use & __DRI_IMAGE_USE_BACKBUFFER)
903 usage = PIPE_HANDLE_USAGE_EXPLICIT_FLUSH | PIPE_HANDLE_USAGE_READ;
904 else
905 usage = PIPE_HANDLE_USAGE_READ_WRITE;
906
907 memset(&whandle, 0, sizeof(whandle));
908
909 switch (attrib) {
910 case __DRI_IMAGE_ATTRIB_STRIDE:
911 whandle.type = DRM_API_HANDLE_TYPE_KMS;
912 image->texture->screen->resource_get_handle(image->texture->screen,
913 image->texture, &whandle, usage);
914 *value = whandle.stride;
915 return GL_TRUE;
916 case __DRI_IMAGE_ATTRIB_HANDLE:
917 whandle.type = DRM_API_HANDLE_TYPE_KMS;
918 image->texture->screen->resource_get_handle(image->texture->screen,
919 image->texture, &whandle, usage);
920 *value = whandle.handle;
921 return GL_TRUE;
922 case __DRI_IMAGE_ATTRIB_NAME:
923 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
924 image->texture->screen->resource_get_handle(image->texture->screen,
925 image->texture, &whandle, usage);
926 *value = whandle.handle;
927 return GL_TRUE;
928 case __DRI_IMAGE_ATTRIB_FD:
929 whandle.type= DRM_API_HANDLE_TYPE_FD;
930 image->texture->screen->resource_get_handle(image->texture->screen,
931 image->texture, &whandle, usage);
932 *value = whandle.handle;
933 return GL_TRUE;
934 case __DRI_IMAGE_ATTRIB_FORMAT:
935 *value = image->dri_format;
936 return GL_TRUE;
937 case __DRI_IMAGE_ATTRIB_WIDTH:
938 *value = image->texture->width0;
939 return GL_TRUE;
940 case __DRI_IMAGE_ATTRIB_HEIGHT:
941 *value = image->texture->height0;
942 return GL_TRUE;
943 case __DRI_IMAGE_ATTRIB_COMPONENTS:
944 if (image->dri_components == 0)
945 return GL_FALSE;
946 *value = image->dri_components;
947 return GL_TRUE;
948 case __DRI_IMAGE_ATTRIB_FOURCC:
949 *value = convert_to_fourcc(image->dri_format);
950 return GL_TRUE;
951 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
952 *value = 1;
953 return GL_TRUE;
954 default:
955 return GL_FALSE;
956 }
957 }
958
959 static __DRIimage *
960 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
961 {
962 __DRIimage *img;
963
964 img = CALLOC_STRUCT(__DRIimageRec);
965 if (!img)
966 return NULL;
967
968 img->texture = NULL;
969 pipe_resource_reference(&img->texture, image->texture);
970 img->level = image->level;
971 img->layer = image->layer;
972 img->dri_format = image->dri_format;
973 /* This should be 0 for sub images, but dup is also used for base images. */
974 img->dri_components = image->dri_components;
975 img->loader_private = loaderPrivate;
976
977 return img;
978 }
979
980 static GLboolean
981 dri2_validate_usage(__DRIimage *image, unsigned int use)
982 {
983 /*
984 * Gallium drivers are bad at adding usages to the resources
985 * once opened again in another process, which is the main use
986 * case for this, so we have to lie.
987 */
988 if (image != NULL)
989 return GL_TRUE;
990 else
991 return GL_FALSE;
992 }
993
994 static __DRIimage *
995 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
996 int *names, int num_names, int *strides, int *offsets,
997 void *loaderPrivate)
998 {
999 __DRIimage *img;
1000 int stride, dri_components;
1001
1002 if (num_names != 1)
1003 return NULL;
1004 if (offsets[0] != 0)
1005 return NULL;
1006
1007 format = convert_fourcc(format, &dri_components);
1008 if (format == -1)
1009 return NULL;
1010
1011 /* Strides are in bytes not pixels. */
1012 stride = strides[0] /4;
1013
1014 img = dri2_create_image_from_name(screen, width, height, format,
1015 names[0], stride, loaderPrivate);
1016 if (img == NULL)
1017 return NULL;
1018
1019 img->dri_components = dri_components;
1020 return img;
1021 }
1022
1023 static __DRIimage *
1024 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
1025 {
1026 __DRIimage *img;
1027
1028 if (plane != 0)
1029 return NULL;
1030
1031 if (image->dri_components == 0)
1032 return NULL;
1033
1034 img = dri2_dup_image(image, loaderPrivate);
1035 if (img == NULL)
1036 return NULL;
1037
1038 /* set this to 0 for sub images. */
1039 img->dri_components = 0;
1040 return img;
1041 }
1042
1043 static __DRIimage *
1044 dri2_create_from_texture(__DRIcontext *context, int target, unsigned texture,
1045 int depth, int level, unsigned *error,
1046 void *loaderPrivate)
1047 {
1048 __DRIimage *img;
1049 struct gl_context *ctx = ((struct st_context *)dri_context(context)->st)->ctx;
1050 struct gl_texture_object *obj;
1051 struct pipe_resource *tex;
1052 GLuint face = 0;
1053
1054 obj = _mesa_lookup_texture(ctx, texture);
1055 if (!obj || obj->Target != target) {
1056 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1057 return NULL;
1058 }
1059
1060 tex = st_get_texobj_resource(obj);
1061 if (!tex) {
1062 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1063 return NULL;
1064 }
1065
1066 if (target == GL_TEXTURE_CUBE_MAP)
1067 face = depth;
1068
1069 _mesa_test_texobj_completeness(ctx, obj);
1070 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
1071 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1072 return NULL;
1073 }
1074
1075 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
1076 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1077 return NULL;
1078 }
1079
1080 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
1081 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1082 return NULL;
1083 }
1084
1085 img = CALLOC_STRUCT(__DRIimageRec);
1086 if (!img) {
1087 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1088 return NULL;
1089 }
1090
1091 img->level = level;
1092 img->layer = depth;
1093 img->dri_format = driGLFormatToImageFormat(obj->Image[face][level]->TexFormat);
1094
1095 img->loader_private = loaderPrivate;
1096
1097 if (img->dri_format == __DRI_IMAGE_FORMAT_NONE) {
1098 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
1099 free(img);
1100 return NULL;
1101 }
1102
1103 pipe_resource_reference(&img->texture, tex);
1104
1105 *error = __DRI_IMAGE_ERROR_SUCCESS;
1106 return img;
1107 }
1108
1109 static __DRIimage *
1110 dri2_from_fds(__DRIscreen *screen, int width, int height, int fourcc,
1111 int *fds, int num_fds, int *strides, int *offsets,
1112 void *loaderPrivate)
1113 {
1114 __DRIimage *img;
1115 int format, stride, dri_components;
1116
1117 if (num_fds != 1)
1118 return NULL;
1119 if (offsets[0] != 0)
1120 return NULL;
1121
1122 format = convert_fourcc(fourcc, &dri_components);
1123 if (format == -1)
1124 return NULL;
1125
1126 /* Strides are in bytes not pixels. */
1127 stride = strides[0] /4;
1128
1129 img = dri2_create_image_from_fd(screen, width, height, format,
1130 fds[0], stride, loaderPrivate);
1131 if (img == NULL)
1132 return NULL;
1133
1134 img->dri_components = dri_components;
1135 return img;
1136 }
1137
1138 static __DRIimage *
1139 dri2_from_dma_bufs(__DRIscreen *screen,
1140 int width, int height, int fourcc,
1141 int *fds, int num_fds,
1142 int *strides, int *offsets,
1143 enum __DRIYUVColorSpace yuv_color_space,
1144 enum __DRISampleRange sample_range,
1145 enum __DRIChromaSiting horizontal_siting,
1146 enum __DRIChromaSiting vertical_siting,
1147 unsigned *error,
1148 void *loaderPrivate)
1149 {
1150 __DRIimage *img;
1151 int format, stride, dri_components;
1152
1153 if (num_fds != 1 || offsets[0] != 0) {
1154 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1155 return NULL;
1156 }
1157
1158 format = convert_fourcc(fourcc, &dri_components);
1159 if (format == -1) {
1160 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1161 return NULL;
1162 }
1163
1164 /* Strides are in bytes not pixels. */
1165 stride = strides[0] /4;
1166
1167 img = dri2_create_image_from_fd(screen, width, height, format,
1168 fds[0], stride, loaderPrivate);
1169 if (img == NULL) {
1170 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1171 return NULL;
1172 }
1173
1174 img->yuv_color_space = yuv_color_space;
1175 img->sample_range = sample_range;
1176 img->horizontal_siting = horizontal_siting;
1177 img->vertical_siting = vertical_siting;
1178 img->dri_components = dri_components;
1179
1180 *error = __DRI_IMAGE_ERROR_SUCCESS;
1181 return img;
1182 }
1183
1184 static void
1185 dri2_blit_image(__DRIcontext *context, __DRIimage *dst, __DRIimage *src,
1186 int dstx0, int dsty0, int dstwidth, int dstheight,
1187 int srcx0, int srcy0, int srcwidth, int srcheight,
1188 int flush_flag)
1189 {
1190 struct dri_context *ctx = dri_context(context);
1191 struct pipe_context *pipe = ctx->st->pipe;
1192 struct pipe_screen *screen;
1193 struct pipe_fence_handle *fence;
1194 struct pipe_blit_info blit;
1195
1196 if (!dst || !src)
1197 return;
1198
1199 memset(&blit, 0, sizeof(blit));
1200 blit.dst.resource = dst->texture;
1201 blit.dst.box.x = dstx0;
1202 blit.dst.box.y = dsty0;
1203 blit.dst.box.width = dstwidth;
1204 blit.dst.box.height = dstheight;
1205 blit.dst.box.depth = 1;
1206 blit.dst.format = dst->texture->format;
1207 blit.src.resource = src->texture;
1208 blit.src.box.x = srcx0;
1209 blit.src.box.y = srcy0;
1210 blit.src.box.width = srcwidth;
1211 blit.src.box.height = srcheight;
1212 blit.src.box.depth = 1;
1213 blit.src.format = src->texture->format;
1214 blit.mask = PIPE_MASK_RGBA;
1215 blit.filter = PIPE_TEX_FILTER_NEAREST;
1216
1217 pipe->blit(pipe, &blit);
1218
1219 if (flush_flag == __BLIT_FLAG_FLUSH) {
1220 pipe->flush_resource(pipe, dst->texture);
1221 ctx->st->flush(ctx->st, 0, NULL);
1222 } else if (flush_flag == __BLIT_FLAG_FINISH) {
1223 screen = dri_screen(ctx->sPriv)->base.screen;
1224 pipe->flush_resource(pipe, dst->texture);
1225 ctx->st->flush(ctx->st, 0, &fence);
1226 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
1227 screen->fence_reference(screen, &fence, NULL);
1228 }
1229 }
1230
1231 static void
1232 dri2_destroy_image(__DRIimage *img)
1233 {
1234 pipe_resource_reference(&img->texture, NULL);
1235 FREE(img);
1236 }
1237
1238 static int
1239 dri2_get_capabilities(__DRIscreen *_screen)
1240 {
1241 struct dri_screen *screen = dri_screen(_screen);
1242
1243 return (screen->can_share_buffer ? __DRI_IMAGE_CAP_GLOBAL_NAMES : 0);
1244 }
1245
1246 /* The extension is modified during runtime if DRI_PRIME is detected */
1247 static __DRIimageExtension dri2ImageExtension = {
1248 .base = { __DRI_IMAGE, 11 },
1249
1250 .createImageFromName = dri2_create_image_from_name,
1251 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
1252 .destroyImage = dri2_destroy_image,
1253 .createImage = dri2_create_image,
1254 .queryImage = dri2_query_image,
1255 .dupImage = dri2_dup_image,
1256 .validateUsage = dri2_validate_usage,
1257 .createImageFromNames = dri2_from_names,
1258 .fromPlanar = dri2_from_planar,
1259 .createImageFromTexture = dri2_create_from_texture,
1260 .createImageFromFds = NULL,
1261 .createImageFromDmaBufs = NULL,
1262 .blitImage = dri2_blit_image,
1263 .getCapabilities = dri2_get_capabilities,
1264 };
1265
1266
1267 static bool
1268 dri2_is_opencl_interop_loaded_locked(struct dri_screen *screen)
1269 {
1270 return screen->opencl_dri_event_add_ref &&
1271 screen->opencl_dri_event_release &&
1272 screen->opencl_dri_event_wait &&
1273 screen->opencl_dri_event_get_fence;
1274 }
1275
1276 static bool
1277 dri2_load_opencl_interop(struct dri_screen *screen)
1278 {
1279 #if defined(RTLD_DEFAULT)
1280 bool success;
1281
1282 pipe_mutex_lock(screen->opencl_func_mutex);
1283
1284 if (dri2_is_opencl_interop_loaded_locked(screen)) {
1285 pipe_mutex_unlock(screen->opencl_func_mutex);
1286 return true;
1287 }
1288
1289 screen->opencl_dri_event_add_ref =
1290 dlsym(RTLD_DEFAULT, "opencl_dri_event_add_ref");
1291 screen->opencl_dri_event_release =
1292 dlsym(RTLD_DEFAULT, "opencl_dri_event_release");
1293 screen->opencl_dri_event_wait =
1294 dlsym(RTLD_DEFAULT, "opencl_dri_event_wait");
1295 screen->opencl_dri_event_get_fence =
1296 dlsym(RTLD_DEFAULT, "opencl_dri_event_get_fence");
1297
1298 success = dri2_is_opencl_interop_loaded_locked(screen);
1299 pipe_mutex_unlock(screen->opencl_func_mutex);
1300 return success;
1301 #else
1302 return false;
1303 #endif
1304 }
1305
1306 struct dri2_fence {
1307 struct dri_screen *driscreen;
1308 struct pipe_fence_handle *pipe_fence;
1309 void *cl_event;
1310 };
1311
1312 static void *
1313 dri2_create_fence(__DRIcontext *_ctx)
1314 {
1315 struct pipe_context *ctx = dri_context(_ctx)->st->pipe;
1316 struct dri2_fence *fence = CALLOC_STRUCT(dri2_fence);
1317
1318 if (!fence)
1319 return NULL;
1320
1321 ctx->flush(ctx, &fence->pipe_fence, 0);
1322
1323 if (!fence->pipe_fence) {
1324 FREE(fence);
1325 return NULL;
1326 }
1327
1328 fence->driscreen = dri_screen(_ctx->driScreenPriv);
1329 return fence;
1330 }
1331
1332 static void *
1333 dri2_get_fence_from_cl_event(__DRIscreen *_screen, intptr_t cl_event)
1334 {
1335 struct dri_screen *driscreen = dri_screen(_screen);
1336 struct dri2_fence *fence;
1337
1338 if (!dri2_load_opencl_interop(driscreen))
1339 return NULL;
1340
1341 fence = CALLOC_STRUCT(dri2_fence);
1342 if (!fence)
1343 return NULL;
1344
1345 fence->cl_event = (void*)cl_event;
1346
1347 if (!driscreen->opencl_dri_event_add_ref(fence->cl_event)) {
1348 free(fence);
1349 return NULL;
1350 }
1351
1352 fence->driscreen = driscreen;
1353 return fence;
1354 }
1355
1356 static void
1357 dri2_destroy_fence(__DRIscreen *_screen, void *_fence)
1358 {
1359 struct dri_screen *driscreen = dri_screen(_screen);
1360 struct pipe_screen *screen = driscreen->base.screen;
1361 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1362
1363 if (fence->pipe_fence)
1364 screen->fence_reference(screen, &fence->pipe_fence, NULL);
1365 else if (fence->cl_event)
1366 driscreen->opencl_dri_event_release(fence->cl_event);
1367 else
1368 assert(0);
1369
1370 FREE(fence);
1371 }
1372
1373 static GLboolean
1374 dri2_client_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags,
1375 uint64_t timeout)
1376 {
1377 struct dri2_fence *fence = (struct dri2_fence*)_fence;
1378 struct dri_screen *driscreen = fence->driscreen;
1379 struct pipe_screen *screen = driscreen->base.screen;
1380
1381 /* No need to flush. The context was flushed when the fence was created. */
1382
1383 if (fence->pipe_fence)
1384 return screen->fence_finish(screen, fence->pipe_fence, timeout);
1385 else if (fence->cl_event) {
1386 struct pipe_fence_handle *pipe_fence =
1387 driscreen->opencl_dri_event_get_fence(fence->cl_event);
1388
1389 if (pipe_fence)
1390 return screen->fence_finish(screen, pipe_fence, timeout);
1391 else
1392 return driscreen->opencl_dri_event_wait(fence->cl_event, timeout);
1393 }
1394 else {
1395 assert(0);
1396 return false;
1397 }
1398 }
1399
1400 static void
1401 dri2_server_wait_sync(__DRIcontext *_ctx, void *_fence, unsigned flags)
1402 {
1403 /* AFAIK, no driver currently supports parallel context execution. */
1404 }
1405
1406 static __DRI2fenceExtension dri2FenceExtension = {
1407 .base = { __DRI2_FENCE, 1 },
1408
1409 .create_fence = dri2_create_fence,
1410 .get_fence_from_cl_event = dri2_get_fence_from_cl_event,
1411 .destroy_fence = dri2_destroy_fence,
1412 .client_wait_sync = dri2_client_wait_sync,
1413 .server_wait_sync = dri2_server_wait_sync
1414 };
1415
1416 static const __DRIrobustnessExtension dri2Robustness = {
1417 .base = { __DRI2_ROBUSTNESS, 1 }
1418 };
1419
1420 /*
1421 * Backend function init_screen.
1422 */
1423
1424 static const __DRIextension *dri_screen_extensions[] = {
1425 &driTexBufferExtension.base,
1426 &dri2FlushExtension.base,
1427 &dri2ImageExtension.base,
1428 &dri2RendererQueryExtension.base,
1429 &dri2ConfigQueryExtension.base,
1430 &dri2ThrottleExtension.base,
1431 &dri2FenceExtension.base,
1432 NULL
1433 };
1434
1435 static const __DRIextension *dri_robust_screen_extensions[] = {
1436 &driTexBufferExtension.base,
1437 &dri2FlushExtension.base,
1438 &dri2ImageExtension.base,
1439 &dri2RendererQueryExtension.base,
1440 &dri2ConfigQueryExtension.base,
1441 &dri2ThrottleExtension.base,
1442 &dri2FenceExtension.base,
1443 &dri2Robustness.base,
1444 NULL
1445 };
1446
1447 /**
1448 * This is the driver specific part of the createNewScreen entry point.
1449 *
1450 * Returns the struct gl_config supported by this driver.
1451 */
1452 static const __DRIconfig **
1453 dri2_init_screen(__DRIscreen * sPriv)
1454 {
1455 const __DRIconfig **configs;
1456 struct dri_screen *screen;
1457 struct pipe_screen *pscreen = NULL;
1458 const struct drm_conf_ret *throttle_ret;
1459 const struct drm_conf_ret *dmabuf_ret;
1460 int fd = -1;
1461
1462 screen = CALLOC_STRUCT(dri_screen);
1463 if (!screen)
1464 return NULL;
1465
1466 screen->sPriv = sPriv;
1467 screen->fd = sPriv->fd;
1468 pipe_mutex_init(screen->opencl_func_mutex);
1469
1470 sPriv->driverPrivate = (void *)screen;
1471
1472 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1473 goto fail;
1474
1475 if (pipe_loader_drm_probe_fd(&screen->dev, fd))
1476 pscreen = pipe_loader_create_screen(screen->dev);
1477
1478 if (!pscreen)
1479 goto fail;
1480
1481 throttle_ret = pipe_loader_configuration(screen->dev, DRM_CONF_THROTTLE);
1482 dmabuf_ret = pipe_loader_configuration(screen->dev, DRM_CONF_SHARE_FD);
1483
1484 if (throttle_ret && throttle_ret->val.val_int != -1) {
1485 screen->throttling_enabled = TRUE;
1486 screen->default_throttle_frames = throttle_ret->val.val_int;
1487 }
1488
1489 if (dmabuf_ret && dmabuf_ret->val.val_bool) {
1490 uint64_t cap;
1491
1492 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1493 (cap & DRM_PRIME_CAP_IMPORT)) {
1494 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1495 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1496 }
1497 }
1498
1499 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
1500 sPriv->extensions = dri_robust_screen_extensions;
1501 screen->has_reset_status_query = true;
1502 }
1503 else
1504 sPriv->extensions = dri_screen_extensions;
1505
1506 configs = dri_init_screen_helper(screen, pscreen, screen->dev->driver_name);
1507 if (!configs)
1508 goto fail;
1509
1510 screen->can_share_buffer = true;
1511 screen->auto_fake_front = dri_with_format(sPriv);
1512 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1513 screen->lookup_egl_image = dri2_lookup_egl_image;
1514
1515 return configs;
1516 fail:
1517 dri_destroy_screen_helper(screen);
1518 if (screen->dev)
1519 pipe_loader_release(&screen->dev, 1);
1520 else
1521 close(fd);
1522 FREE(screen);
1523 return NULL;
1524 }
1525
1526 /**
1527 * This is the driver specific part of the createNewScreen entry point.
1528 *
1529 * Returns the struct gl_config supported by this driver.
1530 */
1531 static const __DRIconfig **
1532 dri_kms_init_screen(__DRIscreen * sPriv)
1533 {
1534 #if defined(GALLIUM_SOFTPIPE)
1535 const __DRIconfig **configs;
1536 struct dri_screen *screen;
1537 struct pipe_screen *pscreen = NULL;
1538 uint64_t cap;
1539 int fd = -1;
1540
1541 screen = CALLOC_STRUCT(dri_screen);
1542 if (!screen)
1543 return NULL;
1544
1545 screen->sPriv = sPriv;
1546 screen->fd = sPriv->fd;
1547
1548 sPriv->driverPrivate = (void *)screen;
1549
1550 if (screen->fd < 0 || (fd = dup(screen->fd)) < 0)
1551 goto fail;
1552
1553 if (pipe_loader_sw_probe_kms(&screen->dev, fd))
1554 pscreen = pipe_loader_create_screen(screen->dev);
1555
1556 if (!pscreen)
1557 goto fail;
1558
1559 if (drmGetCap(sPriv->fd, DRM_CAP_PRIME, &cap) == 0 &&
1560 (cap & DRM_PRIME_CAP_IMPORT)) {
1561 dri2ImageExtension.createImageFromFds = dri2_from_fds;
1562 dri2ImageExtension.createImageFromDmaBufs = dri2_from_dma_bufs;
1563 }
1564
1565 sPriv->extensions = dri_screen_extensions;
1566
1567 configs = dri_init_screen_helper(screen, pscreen, "swrast");
1568 if (!configs)
1569 goto fail;
1570
1571 screen->can_share_buffer = false;
1572 screen->auto_fake_front = dri_with_format(sPriv);
1573 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
1574 screen->lookup_egl_image = dri2_lookup_egl_image;
1575
1576 return configs;
1577 fail:
1578 dri_destroy_screen_helper(screen);
1579 if (screen->dev)
1580 pipe_loader_release(&screen->dev, 1);
1581 else
1582 close(fd);
1583 FREE(screen);
1584 #endif // GALLIUM_SOFTPIPE
1585 return NULL;
1586 }
1587
1588 static boolean
1589 dri2_create_buffer(__DRIscreen * sPriv,
1590 __DRIdrawable * dPriv,
1591 const struct gl_config * visual, boolean isPixmap)
1592 {
1593 struct dri_drawable *drawable = NULL;
1594
1595 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
1596 return FALSE;
1597
1598 drawable = dPriv->driverPrivate;
1599
1600 drawable->allocate_textures = dri2_allocate_textures;
1601 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
1602 drawable->update_tex_buffer = dri2_update_tex_buffer;
1603
1604 return TRUE;
1605 }
1606
1607 /**
1608 * DRI driver virtual function table.
1609 *
1610 * DRI versions differ in their implementation of init_screen and swap_buffers.
1611 */
1612 const struct __DriverAPIRec galliumdrm_driver_api = {
1613 .InitScreen = dri2_init_screen,
1614 .DestroyScreen = dri_destroy_screen,
1615 .CreateContext = dri_create_context,
1616 .DestroyContext = dri_destroy_context,
1617 .CreateBuffer = dri2_create_buffer,
1618 .DestroyBuffer = dri_destroy_buffer,
1619 .MakeCurrent = dri_make_current,
1620 .UnbindContext = dri_unbind_context,
1621
1622 .AllocateBuffer = dri2_allocate_buffer,
1623 .ReleaseBuffer = dri2_release_buffer,
1624 };
1625
1626 /**
1627 * DRI driver virtual function table.
1628 *
1629 * KMS/DRM version of the DriverAPI above sporting a different InitScreen
1630 * hook. The latter is used to explicitly initialise the kms_swrast driver
1631 * rather than selecting the approapriate driver as suggested by the loader.
1632 */
1633 const struct __DriverAPIRec dri_kms_driver_api = {
1634 .InitScreen = dri_kms_init_screen,
1635 .DestroyScreen = dri_destroy_screen,
1636 .CreateContext = dri_create_context,
1637 .DestroyContext = dri_destroy_context,
1638 .CreateBuffer = dri2_create_buffer,
1639 .DestroyBuffer = dri_destroy_buffer,
1640 .MakeCurrent = dri_make_current,
1641 .UnbindContext = dri_unbind_context,
1642
1643 .AllocateBuffer = dri2_allocate_buffer,
1644 .ReleaseBuffer = dri2_release_buffer,
1645 };
1646
1647 /* This is the table of extensions that the loader will dlsym() for. */
1648 const __DRIextension *galliumdrm_driver_extensions[] = {
1649 &driCoreExtension.base,
1650 &driImageDriverExtension.base,
1651 &driDRI2Extension.base,
1652 &gallium_config_options.base,
1653 NULL
1654 };
1655
1656 /* vim: set sw=3 ts=8 sts=3 expandtab: */