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