2 * Mesa 3-D graphics library
5 * Copyright 2009, VMware, Inc.
7 * Copyright (C) 2010 LunarG Inc.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@vmware.com>
28 * Jakob Bornecrantz <wallbraker@gmail.com>
29 * Chia-I Wu <olv@lunarg.com>
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"
38 #include "dri_screen.h"
39 #include "dri_context.h"
40 #include "dri_drawable.h"
41 #include "dri2_buffer.h"
44 * DRI2 flush extension.
47 dri2_flush_drawable(__DRIdrawable *dPriv)
49 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
50 struct dri_drawable *drawable = dri_drawable(dPriv);
52 struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
55 if (ptex && ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
56 pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
58 ctx->st->flush(ctx->st, 0, NULL);
63 dri2_invalidate_drawable(__DRIdrawable *dPriv)
65 struct dri_drawable *drawable = dri_drawable(dPriv);
67 dri2InvalidateDrawable(dPriv);
68 drawable->dPriv->lastStamp = *drawable->dPriv->pStamp;
70 p_atomic_inc(&drawable->base.stamp);
73 static const __DRI2flushExtension dri2FlushExtension = {
74 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
76 dri2_invalidate_drawable,
80 * Retrieve __DRIbuffer from the DRI loader.
83 dri2_drawable_get_buffers(struct dri_drawable *drawable,
84 const enum st_attachment_type *statts,
87 __DRIdrawable *dri_drawable = drawable->dPriv;
88 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
92 unsigned attachments[10];
93 unsigned num_attachments, i;
96 with_format = dri_with_format(drawable->sPriv);
100 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
102 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
104 for (i = 0; i < *count; i++) {
105 enum pipe_format format;
109 dri_drawable_get_format(drawable, statts[i], &format, &bind);
110 if (format == PIPE_FORMAT_NONE)
114 case ST_ATTACHMENT_FRONT_LEFT:
118 att = __DRI_BUFFER_FRONT_LEFT;
120 case ST_ATTACHMENT_BACK_LEFT:
121 att = __DRI_BUFFER_BACK_LEFT;
123 case ST_ATTACHMENT_FRONT_RIGHT:
124 att = __DRI_BUFFER_FRONT_RIGHT;
126 case ST_ATTACHMENT_BACK_RIGHT:
127 att = __DRI_BUFFER_BACK_RIGHT;
129 case ST_ATTACHMENT_DEPTH_STENCIL:
130 att = __DRI_BUFFER_DEPTH_STENCIL;
137 bpp = util_format_get_blocksizebits(format);
140 attachments[num_attachments++] = att;
142 attachments[num_attachments++] = bpp;
148 num_attachments /= 2;
149 buffers = loader->getBuffersWithFormat(dri_drawable,
150 &dri_drawable->w, &dri_drawable->h,
151 attachments, num_attachments,
152 &num_buffers, dri_drawable->loaderPrivate);
155 buffers = loader->getBuffers(dri_drawable,
156 &dri_drawable->w, &dri_drawable->h,
157 attachments, num_attachments,
158 &num_buffers, dri_drawable->loaderPrivate);
162 /* set one cliprect to cover the whole dri_drawable */
165 dri_drawable->backX = 0;
166 dri_drawable->backY = 0;
167 dri_drawable->numClipRects = 1;
168 dri_drawable->pClipRects[0].x1 = 0;
169 dri_drawable->pClipRects[0].y1 = 0;
170 dri_drawable->pClipRects[0].x2 = dri_drawable->w;
171 dri_drawable->pClipRects[0].y2 = dri_drawable->h;
172 dri_drawable->numBackClipRects = 1;
173 dri_drawable->pBackClipRects[0].x1 = 0;
174 dri_drawable->pBackClipRects[0].y1 = 0;
175 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
176 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
178 *count = num_buffers;
185 * Process __DRIbuffer and convert them into pipe_resources.
188 dri2_drawable_process_buffers(struct dri_drawable *drawable,
189 __DRIbuffer *buffers, unsigned count)
191 struct dri_screen *screen = dri_screen(drawable->sPriv);
192 __DRIdrawable *dri_drawable = drawable->dPriv;
193 struct pipe_resource templ;
194 struct winsys_handle whandle;
195 boolean have_depth = FALSE;
198 if (drawable->old_num == count &&
199 drawable->old_w == dri_drawable->w &&
200 drawable->old_h == dri_drawable->h &&
201 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0)
204 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
205 pipe_resource_reference(&drawable->textures[i], NULL);
207 memset(&templ, 0, sizeof(templ));
208 templ.target = screen->target;
209 templ.last_level = 0;
210 templ.width0 = dri_drawable->w;
211 templ.height0 = dri_drawable->h;
213 templ.array_size = 1;
215 memset(&whandle, 0, sizeof(whandle));
217 for (i = 0; i < count; i++) {
218 __DRIbuffer *buf = &buffers[i];
219 enum st_attachment_type statt;
220 enum pipe_format format;
222 switch (buf->attachment) {
223 case __DRI_BUFFER_FRONT_LEFT:
224 if (!screen->auto_fake_front) {
225 statt = ST_ATTACHMENT_INVALID;
229 case __DRI_BUFFER_FAKE_FRONT_LEFT:
230 statt = ST_ATTACHMENT_FRONT_LEFT;
232 case __DRI_BUFFER_BACK_LEFT:
233 statt = ST_ATTACHMENT_BACK_LEFT;
235 case __DRI_BUFFER_DEPTH:
236 case __DRI_BUFFER_DEPTH_STENCIL:
237 case __DRI_BUFFER_STENCIL:
238 /* use only the first depth/stencil buffer */
241 statt = ST_ATTACHMENT_DEPTH_STENCIL;
244 statt = ST_ATTACHMENT_INVALID;
248 statt = ST_ATTACHMENT_INVALID;
252 dri_drawable_get_format(drawable, statt, &format, &bind);
253 if (statt == ST_ATTACHMENT_INVALID || format == PIPE_FORMAT_NONE)
256 templ.format = format;
258 whandle.handle = buf->name;
259 whandle.stride = buf->pitch;
261 drawable->textures[statt] =
262 screen->base.screen->resource_from_handle(screen->base.screen,
266 drawable->old_num = count;
267 drawable->old_w = dri_drawable->w;
268 drawable->old_h = dri_drawable->h;
269 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
273 dri2_allocate_buffer(__DRIscreen *sPriv,
274 unsigned attachment, unsigned format,
275 int width, int height)
277 struct dri_screen *screen = dri_screen(sPriv);
278 struct dri2_buffer *buffer;
279 struct pipe_resource templ;
282 struct winsys_handle whandle;
284 switch (attachment) {
285 case __DRI_BUFFER_FRONT_LEFT:
286 case __DRI_BUFFER_FAKE_FRONT_LEFT:
287 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
289 case __DRI_BUFFER_BACK_LEFT:
290 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
292 case __DRI_BUFFER_DEPTH:
293 case __DRI_BUFFER_DEPTH_STENCIL:
294 case __DRI_BUFFER_STENCIL:
295 bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
301 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
304 pf = PIPE_FORMAT_Z16_UNORM;
310 buffer = CALLOC_STRUCT(dri2_buffer);
314 memset(&templ, 0, sizeof(templ));
317 templ.target = PIPE_TEXTURE_2D;
318 templ.last_level = 0;
319 templ.width0 = width;
320 templ.height0 = height;
322 templ.array_size = 1;
325 screen->base.screen->resource_create(screen->base.screen, &templ);
326 if (!buffer->resource)
329 memset(&whandle, 0, sizeof(whandle));
330 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
331 screen->base.screen->resource_get_handle(screen->base.screen,
332 buffer->resource, &whandle);
334 buffer->base.attachment = attachment;
335 buffer->base.name = whandle.handle;
336 buffer->base.cpp = util_format_get_blocksize(pf);
337 buffer->base.pitch = whandle.stride;
339 return &buffer->base;
343 dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
345 struct dri2_buffer *buffer = dri2_buffer(bPriv);
347 pipe_resource_reference(&buffer->resource, NULL);
352 * Backend functions for st_framebuffer interface.
356 dri2_allocate_textures(struct dri_drawable *drawable,
357 const enum st_attachment_type *statts,
360 __DRIbuffer *buffers;
361 unsigned num_buffers = count;
363 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
365 dri2_drawable_process_buffers(drawable, buffers, num_buffers);
369 dri2_flush_frontbuffer(struct dri_drawable *drawable,
370 enum st_attachment_type statt)
372 __DRIdrawable *dri_drawable = drawable->dPriv;
373 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
375 if (loader->flushFrontBuffer == NULL)
378 if (statt == ST_ATTACHMENT_FRONT_LEFT) {
379 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
384 dri2_update_tex_buffer(struct dri_drawable *drawable,
385 struct dri_context *ctx,
386 struct pipe_resource *res)
392 dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
394 __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
397 if (!loader->lookupEGLImage)
400 img = loader->lookupEGLImage(screen->sPriv,
401 handle, screen->sPriv->loaderPrivate);
407 dri2_create_image_from_name(__DRIscreen *_screen,
408 int width, int height, int format,
409 int name, int pitch, void *loaderPrivate)
411 struct dri_screen *screen = dri_screen(_screen);
413 struct pipe_resource templ;
414 struct winsys_handle whandle;
418 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
421 case __DRI_IMAGE_FORMAT_RGB565:
422 pf = PIPE_FORMAT_B5G6R5_UNORM;
424 case __DRI_IMAGE_FORMAT_XRGB8888:
425 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
427 case __DRI_IMAGE_FORMAT_ARGB8888:
428 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
430 case __DRI_IMAGE_FORMAT_ABGR8888:
431 pf = PIPE_FORMAT_R8G8B8A8_UNORM;
434 pf = PIPE_FORMAT_NONE;
437 if (pf == PIPE_FORMAT_NONE)
440 img = CALLOC_STRUCT(__DRIimageRec);
444 memset(&templ, 0, sizeof(templ));
445 templ.bind = tex_usage;
447 templ.target = screen->target;
448 templ.last_level = 0;
449 templ.width0 = width;
450 templ.height0 = height;
452 templ.array_size = 1;
454 memset(&whandle, 0, sizeof(whandle));
455 whandle.handle = name;
456 whandle.stride = pitch * util_format_get_blocksize(pf);
458 img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
467 img->loader_private = loaderPrivate;
473 dri2_create_image_from_renderbuffer(__DRIcontext *context,
474 int renderbuffer, void *loaderPrivate)
476 struct dri_context *ctx = dri_context(context);
478 if (!ctx->st->get_resource_for_egl_image)
486 dri2_create_image(__DRIscreen *_screen,
487 int width, int height, int format,
488 unsigned int use, void *loaderPrivate)
490 struct dri_screen *screen = dri_screen(_screen);
492 struct pipe_resource templ;
496 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
497 if (use & __DRI_IMAGE_USE_SCANOUT)
498 tex_usage |= PIPE_BIND_SCANOUT;
499 if (use & __DRI_IMAGE_USE_SHARE)
500 tex_usage |= PIPE_BIND_SHARED;
501 if (use & __DRI_IMAGE_USE_CURSOR) {
502 if (width != 64 || height != 64)
504 tex_usage |= PIPE_BIND_CURSOR;
508 case __DRI_IMAGE_FORMAT_RGB565:
509 pf = PIPE_FORMAT_B5G6R5_UNORM;
511 case __DRI_IMAGE_FORMAT_XRGB8888:
512 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
514 case __DRI_IMAGE_FORMAT_ARGB8888:
515 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
517 case __DRI_IMAGE_FORMAT_ABGR8888:
518 pf = PIPE_FORMAT_R8G8B8A8_UNORM;
521 pf = PIPE_FORMAT_NONE;
524 if (pf == PIPE_FORMAT_NONE)
527 img = CALLOC_STRUCT(__DRIimageRec);
531 memset(&templ, 0, sizeof(templ));
532 templ.bind = tex_usage;
534 templ.target = PIPE_TEXTURE_2D;
535 templ.last_level = 0;
536 templ.width0 = width;
537 templ.height0 = height;
539 templ.array_size = 1;
541 img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
550 img->loader_private = loaderPrivate;
555 dri2_query_image(__DRIimage *image, int attrib, int *value)
557 struct winsys_handle whandle;
558 memset(&whandle, 0, sizeof(whandle));
561 case __DRI_IMAGE_ATTRIB_STRIDE:
562 image->texture->screen->resource_get_handle(image->texture->screen,
563 image->texture, &whandle);
564 *value = whandle.stride;
566 case __DRI_IMAGE_ATTRIB_HANDLE:
567 whandle.type = DRM_API_HANDLE_TYPE_KMS;
568 image->texture->screen->resource_get_handle(image->texture->screen,
569 image->texture, &whandle);
570 *value = whandle.handle;
572 case __DRI_IMAGE_ATTRIB_NAME:
573 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
574 image->texture->screen->resource_get_handle(image->texture->screen,
575 image->texture, &whandle);
576 *value = whandle.handle;
584 dri2_dup_image(__DRIimage *image, void *loaderPrivate)
588 img = CALLOC_STRUCT(__DRIimageRec);
593 pipe_resource_reference(&img->texture, image->texture);
594 img->level = image->level;
595 img->layer = image->layer;
596 img->loader_private = loaderPrivate;
602 dri2_destroy_image(__DRIimage *img)
604 pipe_resource_reference(&img->texture, NULL);
608 static struct __DRIimageExtensionRec dri2ImageExtension = {
609 { __DRI_IMAGE, __DRI_IMAGE_VERSION },
610 dri2_create_image_from_name,
611 dri2_create_image_from_renderbuffer,
619 * Backend function init_screen.
622 static const __DRIextension *dri_screen_extensions[] = {
623 &driReadDrawableExtension,
624 &driCopySubBufferExtension.base,
625 &driSwapControlExtension.base,
626 &driMediaStreamCounterExtension.base,
627 &driTexBufferExtension.base,
628 &dri2FlushExtension.base,
629 &dri2ImageExtension.base,
630 &dri2ConfigQueryExtension.base,
634 static const __DRIextension *dri_screen_extensions_throttle[] = {
635 &driReadDrawableExtension,
636 &driCopySubBufferExtension.base,
637 &driSwapControlExtension.base,
638 &driMediaStreamCounterExtension.base,
639 &driTexBufferExtension.base,
640 &dri2FlushExtension.base,
641 &dri2ImageExtension.base,
642 &dri2ConfigQueryExtension.base,
643 &dri2ThrottleExtension.base,
648 * This is the driver specific part of the createNewScreen entry point.
650 * Returns the struct gl_config supported by this driver.
652 static const __DRIconfig **
653 dri2_init_screen(__DRIscreen * sPriv)
655 const __DRIconfig **configs;
656 struct dri_screen *screen;
657 struct pipe_screen *pscreen;
658 const struct drm_conf_ret *throttle_ret = NULL;
660 screen = CALLOC_STRUCT(dri_screen);
664 screen->sPriv = sPriv;
665 screen->fd = sPriv->fd;
667 sPriv->private = (void *)screen;
669 pscreen = driver_descriptor.create_screen(screen->fd);
670 if (driver_descriptor.configuration)
671 throttle_ret = driver_descriptor.configuration(DRM_CONF_THROTTLE);
673 if (throttle_ret && throttle_ret->val.val_int != -1) {
674 sPriv->extensions = dri_screen_extensions_throttle;
675 screen->default_throttle_frames = throttle_ret->val.val_int;
677 sPriv->extensions = dri_screen_extensions;
679 /* dri_init_screen_helper checks pscreen for us */
681 configs = dri_init_screen_helper(screen, pscreen, 32);
686 if (screen->st_api->profile_mask & ST_PROFILE_DEFAULT_MASK)
687 sPriv->api_mask |= 1 << __DRI_API_OPENGL;
688 if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES1_MASK)
689 sPriv->api_mask |= 1 << __DRI_API_GLES;
690 if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES2_MASK)
691 sPriv->api_mask |= 1 << __DRI_API_GLES2;
693 screen->auto_fake_front = dri_with_format(sPriv);
694 screen->broken_invalidate = !sPriv->dri2.useInvalidate;
695 screen->lookup_egl_image = dri2_lookup_egl_image;
699 dri_destroy_screen_helper(screen);
705 dri2_create_buffer(__DRIscreen * sPriv,
706 __DRIdrawable * dPriv,
707 const struct gl_config * visual, boolean isPixmap)
709 struct dri_drawable *drawable = NULL;
711 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
714 drawable = dPriv->driverPrivate;
716 drawable->allocate_textures = dri2_allocate_textures;
717 drawable->flush_frontbuffer = dri2_flush_frontbuffer;
718 drawable->update_tex_buffer = dri2_update_tex_buffer;
724 * DRI driver virtual function table.
726 * DRI versions differ in their implementation of init_screen and swap_buffers.
728 const struct __DriverAPIRec driDriverAPI = {
730 .InitScreen2 = dri2_init_screen,
731 .DestroyScreen = dri_destroy_screen,
732 .CreateContext = dri_create_context,
733 .DestroyContext = dri_destroy_context,
734 .CreateBuffer = dri2_create_buffer,
735 .DestroyBuffer = dri_destroy_buffer,
736 .MakeCurrent = dri_make_current,
737 .UnbindContext = dri_unbind_context,
740 .GetDrawableMSC = NULL,
744 .CopySubBuffer = NULL,
746 .AllocateBuffer = dri2_allocate_buffer,
747 .ReleaseBuffer = dri2_release_buffer,
750 /* This is the table of extensions that the loader will dlsym() for. */
751 PUBLIC const __DRIextension *__driDriverExtensions[] = {
752 &driCoreExtension.base,
753 &driDRI2Extension.base,
757 /* vim: set sw=3 ts=8 sts=3 expandtab: */