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_api.h"
38 #include "dri_screen.h"
39 #include "dri_context.h"
40 #include "dri_drawable.h"
41 #include "dri_st_api.h"
45 * DRI2 flush extension.
48 dri2_flush_drawable(__DRIdrawable *draw)
53 dri2_invalidate_drawable(__DRIdrawable *dPriv)
55 struct dri_drawable *drawable = dri_drawable(dPriv);
56 struct dri_context *ctx = dri_context(dPriv->driContextPriv);
58 dri2InvalidateDrawable(dPriv);
59 drawable->dPriv->lastStamp = *drawable->dPriv->pStamp;
62 ctx->st->notify_invalid_framebuffer(ctx->st, drawable->stfb);
65 static const __DRI2flushExtension dri2FlushExtension = {
66 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
68 dri2_invalidate_drawable,
72 * These are used for GLX_EXT_texture_from_pixmap
75 dri2_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
76 GLint format, __DRIdrawable *dPriv)
78 struct dri_context *ctx = dri_context(pDRICtx);
79 struct dri_drawable *drawable = dri_drawable(dPriv);
80 struct pipe_resource *pt;
82 dri_st_framebuffer_validate_att(drawable->stfb, ST_ATTACHMENT_FRONT_LEFT);
84 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
87 enum pipe_format internal_format = pt->format;
89 if (format == __DRI_TEXTURE_FORMAT_RGB) {
90 /* only need to cover the formats recognized by dri_fill_st_visual */
91 switch (internal_format) {
92 case PIPE_FORMAT_B8G8R8A8_UNORM:
93 internal_format = PIPE_FORMAT_B8G8R8X8_UNORM;
95 case PIPE_FORMAT_A8R8G8B8_UNORM:
96 internal_format = PIPE_FORMAT_X8R8G8B8_UNORM;
103 ctx->st->teximage(ctx->st,
104 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
105 0, internal_format, pt, FALSE);
110 dri2_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
111 __DRIdrawable *dPriv)
113 dri2_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
116 static const __DRItexBufferExtension dri2TexBufferExtension = {
117 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
119 dri2_set_tex_buffer2,
123 * Get the format of an attachment.
125 static INLINE enum pipe_format
126 dri2_drawable_get_format(struct dri_drawable *drawable,
127 enum st_attachment_type statt)
129 enum pipe_format format;
132 case ST_ATTACHMENT_FRONT_LEFT:
133 case ST_ATTACHMENT_BACK_LEFT:
134 case ST_ATTACHMENT_FRONT_RIGHT:
135 case ST_ATTACHMENT_BACK_RIGHT:
136 format = drawable->stvis.color_format;
138 case ST_ATTACHMENT_DEPTH_STENCIL:
139 format = drawable->stvis.depth_stencil_format;
142 format = PIPE_FORMAT_NONE;
151 * Retrieve __DRIbuffer from the DRI loader.
154 dri2_drawable_get_buffers(struct dri_drawable *drawable,
155 const enum st_attachment_type *statts,
158 __DRIdrawable *dri_drawable = drawable->dPriv;
159 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
161 __DRIbuffer *buffers;
163 unsigned attachments[10];
164 unsigned num_attachments, i;
167 with_format = dri_with_format(drawable->sPriv);
171 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
173 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
175 for (i = 0; i < *count; i++) {
176 enum pipe_format format;
179 format = dri2_drawable_get_format(drawable, statts[i]);
180 if (format == PIPE_FORMAT_NONE)
184 case ST_ATTACHMENT_FRONT_LEFT:
188 att = __DRI_BUFFER_FRONT_LEFT;
190 case ST_ATTACHMENT_BACK_LEFT:
191 att = __DRI_BUFFER_BACK_LEFT;
193 case ST_ATTACHMENT_FRONT_RIGHT:
194 att = __DRI_BUFFER_FRONT_RIGHT;
196 case ST_ATTACHMENT_BACK_RIGHT:
197 att = __DRI_BUFFER_BACK_RIGHT;
199 case ST_ATTACHMENT_DEPTH_STENCIL:
200 att = __DRI_BUFFER_DEPTH_STENCIL;
207 bpp = util_format_get_blocksizebits(format);
210 attachments[num_attachments++] = att;
212 attachments[num_attachments++] = bpp;
218 num_attachments /= 2;
219 buffers = loader->getBuffersWithFormat(dri_drawable,
220 &dri_drawable->w, &dri_drawable->h,
221 attachments, num_attachments,
222 &num_buffers, dri_drawable->loaderPrivate);
225 buffers = loader->getBuffers(dri_drawable,
226 &dri_drawable->w, &dri_drawable->h,
227 attachments, num_attachments,
228 &num_buffers, dri_drawable->loaderPrivate);
232 /* set one cliprect to cover the whole dri_drawable */
235 dri_drawable->backX = 0;
236 dri_drawable->backY = 0;
237 dri_drawable->numClipRects = 1;
238 dri_drawable->pClipRects[0].x1 = 0;
239 dri_drawable->pClipRects[0].y1 = 0;
240 dri_drawable->pClipRects[0].x2 = dri_drawable->w;
241 dri_drawable->pClipRects[0].y2 = dri_drawable->h;
242 dri_drawable->numBackClipRects = 1;
243 dri_drawable->pBackClipRects[0].x1 = 0;
244 dri_drawable->pBackClipRects[0].y1 = 0;
245 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
246 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
248 *count = num_buffers;
255 * Process __DRIbuffer and convert them into pipe_resources.
258 dri2_drawable_process_buffers(struct dri_drawable *drawable,
259 __DRIbuffer *buffers, unsigned count)
261 struct dri_screen *screen = dri_screen(drawable->sPriv);
262 __DRIdrawable *dri_drawable = drawable->dPriv;
263 struct pipe_resource templ;
264 struct winsys_handle whandle;
265 boolean have_depth = FALSE;
268 if (drawable->old_num == count &&
269 drawable->old_w == dri_drawable->w &&
270 drawable->old_h == dri_drawable->h &&
271 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0)
274 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
275 pipe_resource_reference(&drawable->textures[i], NULL);
277 memset(&templ, 0, sizeof(templ));
278 templ.bind = PIPE_BIND_RENDER_TARGET;
279 templ.target = PIPE_TEXTURE_2D;
280 templ.last_level = 0;
281 templ.width0 = dri_drawable->w;
282 templ.height0 = dri_drawable->h;
285 memset(&whandle, 0, sizeof(whandle));
287 for (i = 0; i < count; i++) {
288 __DRIbuffer *buf = &buffers[i];
289 enum st_attachment_type statt;
290 enum pipe_format format;
292 switch (buf->attachment) {
293 case __DRI_BUFFER_FRONT_LEFT:
294 if (!screen->auto_fake_front) {
295 statt = ST_ATTACHMENT_INVALID;
299 case __DRI_BUFFER_FAKE_FRONT_LEFT:
300 statt = ST_ATTACHMENT_FRONT_LEFT;
302 case __DRI_BUFFER_BACK_LEFT:
303 statt = ST_ATTACHMENT_BACK_LEFT;
305 case __DRI_BUFFER_DEPTH:
306 case __DRI_BUFFER_DEPTH_STENCIL:
307 case __DRI_BUFFER_STENCIL:
308 /* use only the first depth/stencil buffer */
311 statt = ST_ATTACHMENT_DEPTH_STENCIL;
314 statt = ST_ATTACHMENT_INVALID;
318 statt = ST_ATTACHMENT_INVALID;
322 format = dri2_drawable_get_format(drawable, statt);
323 if (statt == ST_ATTACHMENT_INVALID || format == PIPE_FORMAT_NONE)
326 templ.format = format;
327 whandle.handle = buf->name;
328 whandle.stride = buf->pitch;
330 drawable->textures[statt] =
331 screen->pipe_screen->resource_from_handle(screen->pipe_screen,
335 drawable->old_num = count;
336 drawable->old_w = dri_drawable->w;
337 drawable->old_h = dri_drawable->h;
338 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
342 * Backend functions for st_framebuffer interface.
346 dri2_allocate_textures(struct dri_drawable *drawable,
347 const enum st_attachment_type *statts,
350 __DRIbuffer *buffers;
351 unsigned num_buffers = count;
353 buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
354 dri2_drawable_process_buffers(drawable, buffers, num_buffers);
358 dri2_flush_frontbuffer(struct dri_drawable *drawable,
359 enum st_attachment_type statt)
361 __DRIdrawable *dri_drawable = drawable->dPriv;
362 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
364 if (loader->flushFrontBuffer == NULL)
367 if (statt == ST_ATTACHMENT_FRONT_LEFT) {
368 loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
373 dri2_lookup_egl_image(struct dri_context *ctx, void *handle)
375 __DRIimageLookupExtension *loader = ctx->sPriv->dri2.image;
378 if (!loader->lookupEGLImage)
381 img = loader->lookupEGLImage(ctx->cPriv, handle, ctx->cPriv->loaderPrivate);
387 dri2_create_image_from_name(__DRIcontext *context,
388 int width, int height, int format,
389 int name, int pitch, void *loaderPrivate)
391 struct dri_screen *screen = dri_screen(context->driScreenPriv);
393 struct pipe_resource templ;
394 struct winsys_handle whandle;
398 tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
401 case __DRI_IMAGE_FORMAT_RGB565:
402 pf = PIPE_FORMAT_B5G6R5_UNORM;
404 case __DRI_IMAGE_FORMAT_XRGB8888:
405 pf = PIPE_FORMAT_B8G8R8X8_UNORM;
407 case __DRI_IMAGE_FORMAT_ARGB8888:
408 pf = PIPE_FORMAT_B8G8R8A8_UNORM;
411 pf = PIPE_FORMAT_NONE;
414 if (pf == PIPE_FORMAT_NONE)
417 img = CALLOC_STRUCT(__DRIimageRec);
421 memset(&templ, 0, sizeof(templ));
422 templ.bind = tex_usage;
424 templ.target = PIPE_TEXTURE_2D;
425 templ.last_level = 0;
426 templ.width0 = width;
427 templ.height0 = height;
430 memset(&whandle, 0, sizeof(whandle));
431 whandle.handle = name;
432 whandle.stride = pitch * util_format_get_blocksize(pf);
434 img->texture = screen->pipe_screen->resource_from_handle(screen->pipe_screen,
444 img->loader_private = loaderPrivate;
450 dri2_create_image_from_renderbuffer(__DRIcontext *context,
451 int renderbuffer, void *loaderPrivate)
453 struct dri_context *ctx = dri_context(context->driverPrivate);
455 if (!ctx->st->get_resource_for_egl_image)
463 dri2_destroy_image(__DRIimage *img)
465 pipe_resource_reference(&img->texture, NULL);
469 static struct __DRIimageExtensionRec dri2ImageExtension = {
470 { __DRI_IMAGE, __DRI_IMAGE_VERSION },
471 dri2_create_image_from_name,
472 dri2_create_image_from_renderbuffer,
477 * Backend function init_screen.
480 static const __DRIextension *dri_screen_extensions[] = {
481 &driReadDrawableExtension,
482 &driCopySubBufferExtension.base,
483 &driSwapControlExtension.base,
484 &driFrameTrackingExtension.base,
485 &driMediaStreamCounterExtension.base,
486 &dri2TexBufferExtension.base,
487 &dri2FlushExtension.base,
488 &dri2ImageExtension.base,
493 * This is the driver specific part of the createNewScreen entry point.
495 * Returns the __GLcontextModes supported by this driver.
498 dri2_init_screen(__DRIscreen * sPriv)
500 const __DRIconfig **configs;
501 struct dri_screen *screen;
502 struct drm_create_screen_arg arg;
504 screen = CALLOC_STRUCT(dri_screen);
508 screen->api = drm_api_create();
509 screen->sPriv = sPriv;
510 screen->fd = sPriv->fd;
512 sPriv->private = (void *)screen;
513 sPriv->extensions = dri_screen_extensions;
515 arg.mode = DRM_CREATE_NORMAL;
517 configs = dri_init_screen_helper(screen, &arg, 32);
521 screen->auto_fake_front = dri_with_format(sPriv);
525 dri_destroy_screen_helper(screen);
530 /* This is the table of extensions that the loader will dlsym() for. */
531 PUBLIC const __DRIextension *__driDriverExtensions[] = {
532 &driCoreExtension.base,
533 &driLegacyExtension.base,
534 &driDRI2Extension.base,
538 /* vim: set sw=3 ts=8 sts=3 expandtab: */