st/dri: refactor dri_msaa_resolve
[mesa.git] / src / gallium / state_trackers / dri / drm / 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 "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34 #include "util/u_debug.h"
35 #include "state_tracker/drm_driver.h"
36
37 #include "dri_screen.h"
38 #include "dri_context.h"
39 #include "dri_drawable.h"
40 #include "dri2_buffer.h"
41
42 /**
43 * DRI2 flush extension.
44 */
45 static void
46 dri2_flush_drawable(__DRIdrawable *dPriv)
47 {
48 dri_flush(dPriv->driContextPriv, dPriv, __DRI2_FLUSH_DRAWABLE, 0);
49 }
50
51 static void
52 dri2_invalidate_drawable(__DRIdrawable *dPriv)
53 {
54 struct dri_drawable *drawable = dri_drawable(dPriv);
55
56 dri2InvalidateDrawable(dPriv);
57 drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
58
59 p_atomic_inc(&drawable->base.stamp);
60 }
61
62 static const __DRI2flushExtension dri2FlushExtension = {
63 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
64 dri2_flush_drawable,
65 dri2_invalidate_drawable,
66 dri_flush,
67 };
68
69 /**
70 * Retrieve __DRIbuffer from the DRI loader.
71 */
72 static __DRIbuffer *
73 dri2_drawable_get_buffers(struct dri_drawable *drawable,
74 const enum st_attachment_type *atts,
75 unsigned *count)
76 {
77 __DRIdrawable *dri_drawable = drawable->dPriv;
78 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
79 boolean with_format;
80 __DRIbuffer *buffers;
81 int num_buffers;
82 unsigned attachments[10];
83 unsigned num_attachments, i;
84
85 assert(loader);
86 with_format = dri_with_format(drawable->sPriv);
87
88 num_attachments = 0;
89
90 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
91 if (!with_format)
92 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
93
94 for (i = 0; i < *count; i++) {
95 enum pipe_format format;
96 unsigned bind;
97 int att, depth;
98
99 dri_drawable_get_format(drawable, atts[i], &format, &bind);
100 if (format == PIPE_FORMAT_NONE)
101 continue;
102
103 switch (atts[i]) {
104 case ST_ATTACHMENT_FRONT_LEFT:
105 /* already added */
106 if (!with_format)
107 continue;
108 att = __DRI_BUFFER_FRONT_LEFT;
109 break;
110 case ST_ATTACHMENT_BACK_LEFT:
111 att = __DRI_BUFFER_BACK_LEFT;
112 break;
113 case ST_ATTACHMENT_FRONT_RIGHT:
114 att = __DRI_BUFFER_FRONT_RIGHT;
115 break;
116 case ST_ATTACHMENT_BACK_RIGHT:
117 att = __DRI_BUFFER_BACK_RIGHT;
118 break;
119 default:
120 continue;
121 }
122
123 /*
124 * In this switch statement we must support all formats that
125 * may occur as the stvis->color_format.
126 */
127 switch(format) {
128 case PIPE_FORMAT_B8G8R8A8_UNORM:
129 depth = 32;
130 break;
131 case PIPE_FORMAT_B8G8R8X8_UNORM:
132 depth = 24;
133 break;
134 case PIPE_FORMAT_B5G6R5_UNORM:
135 depth = 16;
136 break;
137 default:
138 depth = util_format_get_blocksizebits(format);
139 assert(!"Unexpected format in dri2_drawable_get_buffers()");
140 }
141
142 attachments[num_attachments++] = att;
143 if (with_format) {
144 attachments[num_attachments++] = depth;
145 }
146 }
147
148 if (with_format) {
149 num_attachments /= 2;
150 buffers = loader->getBuffersWithFormat(dri_drawable,
151 &dri_drawable->w, &dri_drawable->h,
152 attachments, num_attachments,
153 &num_buffers, dri_drawable->loaderPrivate);
154 }
155 else {
156 buffers = loader->getBuffers(dri_drawable,
157 &dri_drawable->w, &dri_drawable->h,
158 attachments, num_attachments,
159 &num_buffers, dri_drawable->loaderPrivate);
160 }
161
162 if (buffers)
163 *count = num_buffers;
164
165 return buffers;
166 }
167
168 /**
169 * Process __DRIbuffer and convert them into pipe_resources.
170 */
171 static void
172 dri2_drawable_process_buffers(struct dri_drawable *drawable,
173 __DRIbuffer *buffers, unsigned buffer_count,
174 const enum st_attachment_type *atts,
175 unsigned att_count)
176 {
177 struct dri_screen *screen = dri_screen(drawable->sPriv);
178 __DRIdrawable *dri_drawable = drawable->dPriv;
179 struct pipe_resource templ;
180 struct winsys_handle whandle;
181 boolean alloc_depthstencil = FALSE;
182 unsigned i, j, bind;
183
184 if (drawable->old_num == buffer_count &&
185 drawable->old_w == dri_drawable->w &&
186 drawable->old_h == dri_drawable->h &&
187 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * buffer_count) == 0)
188 return;
189
190 /* See if we need a depth-stencil buffer. */
191 for (i = 0; i < att_count; i++) {
192 if (atts[i] == ST_ATTACHMENT_DEPTH_STENCIL) {
193 alloc_depthstencil = TRUE;
194 break;
195 }
196 }
197
198 /* Delete the resources we won't need. */
199 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
200 /* Don't delete the depth-stencil buffer, we can reuse it. */
201 if (i == ST_ATTACHMENT_DEPTH_STENCIL && alloc_depthstencil)
202 continue;
203
204 pipe_resource_reference(&drawable->textures[i], NULL);
205 }
206
207 if (drawable->stvis.samples > 1) {
208 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
209 boolean del = TRUE;
210
211 /* Don't delete MSAA resources for the attachments which are enabled,
212 * we can reuse them. */
213 for (j = 0; j < att_count; j++) {
214 if (i == atts[j]) {
215 del = FALSE;
216 break;
217 }
218 }
219
220 if (del) {
221 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
222 }
223 }
224 }
225
226 memset(&templ, 0, sizeof(templ));
227 templ.target = screen->target;
228 templ.last_level = 0;
229 templ.width0 = dri_drawable->w;
230 templ.height0 = dri_drawable->h;
231 templ.depth0 = 1;
232 templ.array_size = 1;
233
234 memset(&whandle, 0, sizeof(whandle));
235
236 /* Process DRI-provided buffers and get pipe_resources. */
237 for (i = 0; i < buffer_count; i++) {
238 __DRIbuffer *buf = &buffers[i];
239 enum st_attachment_type statt;
240 enum pipe_format format;
241
242 switch (buf->attachment) {
243 case __DRI_BUFFER_FRONT_LEFT:
244 if (!screen->auto_fake_front) {
245 continue; /* invalid attachment */
246 }
247 /* fallthrough */
248 case __DRI_BUFFER_FAKE_FRONT_LEFT:
249 statt = ST_ATTACHMENT_FRONT_LEFT;
250 break;
251 case __DRI_BUFFER_BACK_LEFT:
252 statt = ST_ATTACHMENT_BACK_LEFT;
253 break;
254 default:
255 continue; /* invalid attachment */
256 }
257
258 dri_drawable_get_format(drawable, statt, &format, &bind);
259 if (format == PIPE_FORMAT_NONE)
260 continue;
261
262 templ.format = format;
263 templ.bind = bind;
264 whandle.handle = buf->name;
265 whandle.stride = buf->pitch;
266
267 drawable->textures[statt] =
268 screen->base.screen->resource_from_handle(screen->base.screen,
269 &templ, &whandle);
270 assert(drawable->textures[statt]);
271 }
272
273 /* Allocate private MSAA colorbuffers. */
274 if (drawable->stvis.samples > 1) {
275 for (i = 0; i < att_count; i++) {
276 enum st_attachment_type att = atts[i];
277
278 if (att == ST_ATTACHMENT_DEPTH_STENCIL)
279 continue;
280
281 if (drawable->textures[att]) {
282 templ.format = drawable->textures[att]->format;
283 templ.bind = drawable->textures[att]->bind;
284 templ.nr_samples = drawable->stvis.samples;
285
286 /* Try to reuse the resource.
287 * (the other resource parameters should be constant)
288 */
289 if (!drawable->msaa_textures[att] ||
290 drawable->msaa_textures[att]->width0 != templ.width0 ||
291 drawable->msaa_textures[att]->height0 != templ.height0) {
292 /* Allocate a new one. */
293 pipe_resource_reference(&drawable->msaa_textures[att], NULL);
294
295 drawable->msaa_textures[att] =
296 screen->base.screen->resource_create(screen->base.screen,
297 &templ);
298 assert(drawable->msaa_textures[att]);
299 }
300 }
301 else {
302 pipe_resource_reference(&drawable->msaa_textures[att], NULL);
303 }
304 }
305 }
306
307 /* Allocate a private depth-stencil buffer. */
308 if (alloc_depthstencil) {
309 enum st_attachment_type att = ST_ATTACHMENT_DEPTH_STENCIL;
310 struct pipe_resource **zsbuf;
311 enum pipe_format format;
312 unsigned bind;
313
314 dri_drawable_get_format(drawable, att, &format, &bind);
315
316 if (format) {
317 templ.format = format;
318 templ.bind = bind;
319
320 if (drawable->stvis.samples > 1) {
321 templ.nr_samples = drawable->stvis.samples;
322 zsbuf = &drawable->msaa_textures[att];
323 }
324 else {
325 templ.nr_samples = 0;
326 zsbuf = &drawable->textures[att];
327 }
328
329 /* Try to reuse the resource.
330 * (the other resource parameters should be constant)
331 */
332 if (!*zsbuf ||
333 (*zsbuf)->width0 != templ.width0 ||
334 (*zsbuf)->height0 != templ.height0) {
335 /* Allocate a new one. */
336 pipe_resource_reference(zsbuf, NULL);
337 *zsbuf = screen->base.screen->resource_create(screen->base.screen,
338 &templ);
339 assert(*zsbuf);
340 }
341 }
342 else {
343 pipe_resource_reference(&drawable->msaa_textures[att], NULL);
344 pipe_resource_reference(&drawable->textures[att], NULL);
345 }
346 }
347
348 drawable->old_num = buffer_count;
349 drawable->old_w = dri_drawable->w;
350 drawable->old_h = dri_drawable->h;
351 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * buffer_count);
352 }
353
354 static __DRIbuffer *
355 dri2_allocate_buffer(__DRIscreen *sPriv,
356 unsigned attachment, unsigned format,
357 int width, int height)
358 {
359 struct dri_screen *screen = dri_screen(sPriv);
360 struct dri2_buffer *buffer;
361 struct pipe_resource templ;
362 enum pipe_format pf;
363 unsigned bind = 0;
364 struct winsys_handle whandle;
365
366 switch (attachment) {
367 case __DRI_BUFFER_FRONT_LEFT:
368 case __DRI_BUFFER_FAKE_FRONT_LEFT:
369 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
370 break;
371 case __DRI_BUFFER_BACK_LEFT:
372 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
373 break;
374 case __DRI_BUFFER_DEPTH:
375 case __DRI_BUFFER_DEPTH_STENCIL:
376 case __DRI_BUFFER_STENCIL:
377 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
378 break;
379 }
380
381 /* because we get the handle and stride */
382 bind |= PIPE_BIND_SHARED;
383
384 switch (format) {
385 case 32:
386 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
387 break;
388 case 24:
389 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
390 break;
391 case 16:
392 pf = PIPE_FORMAT_Z16_UNORM;
393 break;
394 default:
395 return NULL;
396 }
397
398 buffer = CALLOC_STRUCT(dri2_buffer);
399 if (!buffer)
400 return NULL;
401
402 memset(&templ, 0, sizeof(templ));
403 templ.bind = bind;
404 templ.format = pf;
405 templ.target = PIPE_TEXTURE_2D;
406 templ.last_level = 0;
407 templ.width0 = width;
408 templ.height0 = height;
409 templ.depth0 = 1;
410 templ.array_size = 1;
411
412 buffer->resource =
413 screen->base.screen->resource_create(screen->base.screen, &templ);
414 if (!buffer->resource) {
415 FREE(buffer);
416 return NULL;
417 }
418
419 memset(&whandle, 0, sizeof(whandle));
420 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
421 screen->base.screen->resource_get_handle(screen->base.screen,
422 buffer->resource, &whandle);
423
424 buffer->base.attachment = attachment;
425 buffer->base.name = whandle.handle;
426 buffer->base.cpp = util_format_get_blocksize(pf);
427 buffer->base.pitch = whandle.stride;
428
429 return &buffer->base;
430 }
431
432 static void
433 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
434 {
435 struct dri2_buffer *buffer = dri2_buffer(bPriv);
436
437 pipe_resource_reference(&buffer->resource, NULL);
438 FREE(buffer);
439 }
440
441 /*
442 * Backend functions for st_framebuffer interface.
443 */
444
445 static void
446 dri2_allocate_textures(struct dri_drawable *drawable,
447 const enum st_attachment_type *statts,
448 unsigned statts_count)
449 {
450 __DRIbuffer *buffers;
451 unsigned num_buffers = statts_count;
452
453 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
454 if (buffers)
455 dri2_drawable_process_buffers(drawable, buffers, num_buffers,
456 statts, statts_count);
457 }
458
459 static void
460 dri2_flush_frontbuffer(struct dri_context *ctx,
461 struct dri_drawable *drawable,
462 enum st_attachment_type statt)
463 {
464 __DRIdrawable *dri_drawable = drawable->dPriv;
465 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
466
467 if (statt != ST_ATTACHMENT_FRONT_LEFT)
468 return;
469
470 if (drawable->stvis.samples > 1) {
471 struct pipe_context *pipe = ctx->st->pipe;
472
473 /* Resolve the front buffer. */
474 dri_pipe_blit(ctx->st->pipe,
475 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
476 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
477 pipe->flush(pipe, NULL, 0);
478 }
479
480 if (loader->flushFrontBuffer) {
481 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
482 }
483 }
484
485 static void
486 dri2_update_tex_buffer(struct dri_drawable *drawable,
487 struct dri_context *ctx,
488 struct pipe_resource *res)
489 {
490 /* no-op */
491 }
492
493 static __DRIimage *
494 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
495 {
496 __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
497 __DRIimage *img;
498
499 if (!loader->lookupEGLImage)
500 return NULL;
501
502 img = loader->lookupEGLImage(screen->sPriv,
503 handle, screen->sPriv->loaderPrivate);
504
505 return img;
506 }
507
508 static __DRIimage *
509 dri2_create_image_from_name(__DRIscreen *_screen,
510 int width, int height, int format,
511 int name, int pitch, void *loaderPrivate)
512 {
513 struct dri_screen *screen = dri_screen(_screen);
514 __DRIimage *img;
515 struct pipe_resource templ;
516 struct winsys_handle whandle;
517 unsigned tex_usage;
518 enum pipe_format pf;
519
520 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
521
522 switch (format) {
523 case __DRI_IMAGE_FORMAT_RGB565:
524 pf = PIPE_FORMAT_B5G6R5_UNORM;
525 break;
526 case __DRI_IMAGE_FORMAT_XRGB8888:
527 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
528 break;
529 case __DRI_IMAGE_FORMAT_ARGB8888:
530 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
531 break;
532 case __DRI_IMAGE_FORMAT_ABGR8888:
533 pf = PIPE_FORMAT_R8G8B8A8_UNORM;
534 break;
535 default:
536 pf = PIPE_FORMAT_NONE;
537 break;
538 }
539 if (pf == PIPE_FORMAT_NONE)
540 return NULL;
541
542 img = CALLOC_STRUCT(__DRIimageRec);
543 if (!img)
544 return NULL;
545
546 memset(&templ, 0, sizeof(templ));
547 templ.bind = tex_usage;
548 templ.format = pf;
549 templ.target = screen->target;
550 templ.last_level = 0;
551 templ.width0 = width;
552 templ.height0 = height;
553 templ.depth0 = 1;
554 templ.array_size = 1;
555
556 memset(&whandle, 0, sizeof(whandle));
557 whandle.handle = name;
558 whandle.stride = pitch * util_format_get_blocksize(pf);
559
560 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
561 &templ, &whandle);
562 if (!img->texture) {
563 FREE(img);
564 return NULL;
565 }
566
567 img->level = 0;
568 img->layer = 0;
569 img->dri_format = format;
570 img->loader_private = loaderPrivate;
571
572 return img;
573 }
574
575 static __DRIimage *
576 dri2_create_image_from_renderbuffer(__DRIcontext *context,
577 int renderbuffer, void *loaderPrivate)
578 {
579 struct dri_context *ctx = dri_context(context);
580
581 if (!ctx->st->get_resource_for_egl_image)
582 return NULL;
583
584 /* TODO */
585 return NULL;
586 }
587
588 static __DRIimage *
589 dri2_create_image(__DRIscreen *_screen,
590 int width, int height, int format,
591 unsigned int use, void *loaderPrivate)
592 {
593 struct dri_screen *screen = dri_screen(_screen);
594 __DRIimage *img;
595 struct pipe_resource templ;
596 unsigned tex_usage;
597 enum pipe_format pf;
598
599 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
600 if (use & __DRI_IMAGE_USE_SCANOUT)
601 tex_usage |= PIPE_BIND_SCANOUT;
602 if (use & __DRI_IMAGE_USE_SHARE)
603 tex_usage |= PIPE_BIND_SHARED;
604 if (use & __DRI_IMAGE_USE_CURSOR) {
605 if (width != 64 || height != 64)
606 return NULL;
607 tex_usage |= PIPE_BIND_CURSOR;
608 }
609
610 switch (format) {
611 case __DRI_IMAGE_FORMAT_RGB565:
612 pf = PIPE_FORMAT_B5G6R5_UNORM;
613 break;
614 case __DRI_IMAGE_FORMAT_XRGB8888:
615 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
616 break;
617 case __DRI_IMAGE_FORMAT_ARGB8888:
618 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
619 break;
620 case __DRI_IMAGE_FORMAT_ABGR8888:
621 pf = PIPE_FORMAT_R8G8B8A8_UNORM;
622 break;
623 default:
624 pf = PIPE_FORMAT_NONE;
625 break;
626 }
627 if (pf == PIPE_FORMAT_NONE)
628 return NULL;
629
630 img = CALLOC_STRUCT(__DRIimageRec);
631 if (!img)
632 return NULL;
633
634 memset(&templ, 0, sizeof(templ));
635 templ.bind = tex_usage;
636 templ.format = pf;
637 templ.target = PIPE_TEXTURE_2D;
638 templ.last_level = 0;
639 templ.width0 = width;
640 templ.height0 = height;
641 templ.depth0 = 1;
642 templ.array_size = 1;
643
644 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
645 if (!img->texture) {
646 FREE(img);
647 return NULL;
648 }
649
650 img->level = 0;
651 img->layer = 0;
652 img->dri_format = format;
653 img->dri_components = 0;
654
655 img->loader_private = loaderPrivate;
656 return img;
657 }
658
659 static GLboolean
660 dri2_query_image(__DRIimage *image, int attrib, int *value)
661 {
662 struct winsys_handle whandle;
663 memset(&whandle, 0, sizeof(whandle));
664
665 switch (attrib) {
666 case __DRI_IMAGE_ATTRIB_STRIDE:
667 image->texture->screen->resource_get_handle(image->texture->screen,
668 image->texture, &whandle);
669 *value = whandle.stride;
670 return GL_TRUE;
671 case __DRI_IMAGE_ATTRIB_HANDLE:
672 whandle.type = DRM_API_HANDLE_TYPE_KMS;
673 image->texture->screen->resource_get_handle(image->texture->screen,
674 image->texture, &whandle);
675 *value = whandle.handle;
676 return GL_TRUE;
677 case __DRI_IMAGE_ATTRIB_NAME:
678 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
679 image->texture->screen->resource_get_handle(image->texture->screen,
680 image->texture, &whandle);
681 *value = whandle.handle;
682 return GL_TRUE;
683 case __DRI_IMAGE_ATTRIB_FORMAT:
684 *value = image->dri_format;
685 return GL_TRUE;
686 case __DRI_IMAGE_ATTRIB_WIDTH:
687 *value = image->texture->width0;
688 return GL_TRUE;
689 case __DRI_IMAGE_ATTRIB_HEIGHT:
690 *value = image->texture->height0;
691 return GL_TRUE;
692 case __DRI_IMAGE_ATTRIB_COMPONENTS:
693 if (image->dri_components == 0)
694 return GL_FALSE;
695 *value = image->dri_components;
696 return GL_TRUE;
697 default:
698 return GL_FALSE;
699 }
700 }
701
702 static __DRIimage *
703 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
704 {
705 __DRIimage *img;
706
707 img = CALLOC_STRUCT(__DRIimageRec);
708 if (!img)
709 return NULL;
710
711 img->texture = NULL;
712 pipe_resource_reference(&img->texture, image->texture);
713 img->level = image->level;
714 img->layer = image->layer;
715 /* This should be 0 for sub images, but dup is also used for base images. */
716 img->dri_components = image->dri_components;
717 img->loader_private = loaderPrivate;
718
719 return img;
720 }
721
722 static GLboolean
723 dri2_validate_usage(__DRIimage *image, unsigned int use)
724 {
725 /*
726 * Gallium drivers are bad at adding usages to the resources
727 * once opened again in another process, which is the main use
728 * case for this, so we have to lie.
729 */
730 if (image != NULL)
731 return GL_TRUE;
732 else
733 return GL_FALSE;
734 }
735
736 static __DRIimage *
737 dri2_from_names(__DRIscreen *screen, int width, int height, int format,
738 int *names, int num_names, int *strides, int *offsets,
739 void *loaderPrivate)
740 {
741 __DRIimage *img;
742 int stride, dri_components;
743
744 if (num_names != 1)
745 return NULL;
746 if (offsets[0] != 0)
747 return NULL;
748
749 switch(format) {
750 case __DRI_IMAGE_FOURCC_RGB565:
751 format = __DRI_IMAGE_FORMAT_RGB565;
752 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
753 break;
754 case __DRI_IMAGE_FOURCC_ARGB8888:
755 format = __DRI_IMAGE_FORMAT_ARGB8888;
756 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
757 break;
758 case __DRI_IMAGE_FOURCC_XRGB8888:
759 format = __DRI_IMAGE_FORMAT_XRGB8888;
760 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
761 break;
762 case __DRI_IMAGE_FOURCC_ABGR8888:
763 format = __DRI_IMAGE_FORMAT_ABGR8888;
764 dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
765 break;
766 case __DRI_IMAGE_FOURCC_XBGR8888:
767 format = __DRI_IMAGE_FORMAT_XBGR8888;
768 dri_components = __DRI_IMAGE_COMPONENTS_RGB;
769 break;
770 default:
771 return NULL;
772 }
773
774 /* Strides are in bytes not pixels. */
775 stride = strides[0] /4;
776
777 img = dri2_create_image_from_name(screen, width, height, format,
778 names[0], stride, loaderPrivate);
779 if (img == NULL)
780 return NULL;
781
782 img->dri_components = dri_components;
783 return img;
784 }
785
786 static __DRIimage *
787 dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
788 {
789 __DRIimage *img;
790
791 if (plane != 0)
792 return NULL;
793
794 if (image->dri_components == 0)
795 return NULL;
796
797 img = dri2_dup_image(image, loaderPrivate);
798 if (img == NULL)
799 return NULL;
800
801 /* set this to 0 for sub images. */
802 img->dri_components = 0;
803 return img;
804 }
805
806 static void
807 dri2_destroy_image(__DRIimage *img)
808 {
809 pipe_resource_reference(&img->texture, NULL);
810 FREE(img);
811 }
812
813 static struct __DRIimageExtensionRec dri2ImageExtension = {
814 { __DRI_IMAGE, 5 },
815 dri2_create_image_from_name,
816 dri2_create_image_from_renderbuffer,
817 dri2_destroy_image,
818 dri2_create_image,
819 dri2_query_image,
820 dri2_dup_image,
821 dri2_validate_usage,
822 dri2_from_names,
823 dri2_from_planar,
824 };
825
826 /*
827 * Backend function init_screen.
828 */
829
830 static const __DRIextension *dri_screen_extensions[] = {
831 &driTexBufferExtension.base,
832 &dri2FlushExtension.base,
833 &dri2ImageExtension.base,
834 &dri2ConfigQueryExtension.base,
835 &dri2ThrottleExtension.base,
836 NULL
837 };
838
839 /**
840 * This is the driver specific part of the createNewScreen entry point.
841 *
842 * Returns the struct gl_config supported by this driver.
843 */
844 static const __DRIconfig **
845 dri2_init_screen(__DRIscreen * sPriv)
846 {
847 const __DRIconfig **configs;
848 struct dri_screen *screen;
849 struct pipe_screen *pscreen;
850 const struct drm_conf_ret *throttle_ret = NULL;
851
852 screen = CALLOC_STRUCT(dri_screen);
853 if (!screen)
854 return NULL;
855
856 screen->sPriv = sPriv;
857 screen->fd = sPriv->fd;
858
859 sPriv->driverPrivate = (void *)screen;
860
861 pscreen = driver_descriptor.create_screen(screen->fd);
862 if (driver_descriptor.configuration)
863 throttle_ret = driver_descriptor.configuration(DRM_CONF_THROTTLE);
864
865 if (throttle_ret && throttle_ret->val.val_int != -1) {
866 screen->throttling_enabled = TRUE;
867 screen->default_throttle_frames = throttle_ret->val.val_int;
868 }
869
870 sPriv->extensions = dri_screen_extensions;
871
872 /* dri_init_screen_helper checks pscreen for us */
873
874 configs = dri_init_screen_helper(screen, pscreen);
875 if (!configs)
876 goto fail;
877
878 sPriv->api_mask = 0;
879 if (screen->st_api->profile_mask & ST_PROFILE_DEFAULT_MASK)
880 sPriv->api_mask |= 1 << __DRI_API_OPENGL;
881 if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES1_MASK)
882 sPriv->api_mask |= 1 << __DRI_API_GLES;
883 if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES2_MASK)
884 sPriv->api_mask |= 1 << __DRI_API_GLES2;
885
886 screen->auto_fake_front = dri_with_format(sPriv);
887 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
888 screen->lookup_egl_image = dri2_lookup_egl_image;
889
890 return configs;
891 fail:
892 dri_destroy_screen_helper(screen);
893 FREE(screen);
894 return NULL;
895 }
896
897 static boolean
898 dri2_create_buffer(__DRIscreen * sPriv,
899 __DRIdrawable * dPriv,
900 const struct gl_config * visual, boolean isPixmap)
901 {
902 struct dri_drawable *drawable = NULL;
903
904 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
905 return FALSE;
906
907 drawable = dPriv->driverPrivate;
908
909 drawable->allocate_textures = dri2_allocate_textures;
910 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
911 drawable->update_tex_buffer = dri2_update_tex_buffer;
912
913 return TRUE;
914 }
915
916 /**
917 * DRI driver virtual function table.
918 *
919 * DRI versions differ in their implementation of init_screen and swap_buffers.
920 */
921 const struct __DriverAPIRec driDriverAPI = {
922 .InitScreen = dri2_init_screen,
923 .DestroyScreen = dri_destroy_screen,
924 .CreateContext = dri_create_context,
925 .DestroyContext = dri_destroy_context,
926 .CreateBuffer = dri2_create_buffer,
927 .DestroyBuffer = dri_destroy_buffer,
928 .MakeCurrent = dri_make_current,
929 .UnbindContext = dri_unbind_context,
930
931 .AllocateBuffer = dri2_allocate_buffer,
932 .ReleaseBuffer = dri2_release_buffer,
933 };
934
935 /* This is the table of extensions that the loader will dlsym() for. */
936 PUBLIC const __DRIextension *__driDriverExtensions[] = {
937 &driCoreExtension.base,
938 &driDRI2Extension.base,
939 NULL
940 };
941
942 /* vim: set sw=3 ts=8 sts=3 expandtab: */