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_texture
*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
;
150 * Retrieve __DRIbuffer from the DRI loader.
153 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
154 const enum st_attachment_type
*statts
,
157 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
158 struct __DRIdri2LoaderExtensionRec
*loader
= drawable
->sPriv
->dri2
.loader
;
160 __DRIbuffer
*buffers
;
162 unsigned attachments
[10];
163 unsigned num_attachments
, i
;
166 with_format
= dri_with_format(drawable
->sPriv
);
170 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
172 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
174 for (i
= 0; i
< *count
; i
++) {
175 enum pipe_format format
;
178 format
= dri2_drawable_get_format(drawable
, statts
[i
]);
179 if (format
== PIPE_FORMAT_NONE
)
183 case ST_ATTACHMENT_FRONT_LEFT
:
187 att
= __DRI_BUFFER_FRONT_LEFT
;
189 case ST_ATTACHMENT_BACK_LEFT
:
190 att
= __DRI_BUFFER_BACK_LEFT
;
192 case ST_ATTACHMENT_FRONT_RIGHT
:
193 att
= __DRI_BUFFER_FRONT_RIGHT
;
195 case ST_ATTACHMENT_BACK_RIGHT
:
196 att
= __DRI_BUFFER_BACK_RIGHT
;
198 case ST_ATTACHMENT_DEPTH_STENCIL
:
199 att
= __DRI_BUFFER_DEPTH_STENCIL
;
206 bpp
= util_format_get_blocksizebits(format
);
209 attachments
[num_attachments
++] = att
;
211 attachments
[num_attachments
++] = bpp
;
217 num_attachments
/= 2;
218 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
219 &dri_drawable
->w
, &dri_drawable
->h
,
220 attachments
, num_attachments
,
221 &num_buffers
, dri_drawable
->loaderPrivate
);
224 buffers
= loader
->getBuffers(dri_drawable
,
225 &dri_drawable
->w
, &dri_drawable
->h
,
226 attachments
, num_attachments
,
227 &num_buffers
, dri_drawable
->loaderPrivate
);
231 /* set one cliprect to cover the whole dri_drawable */
234 dri_drawable
->backX
= 0;
235 dri_drawable
->backY
= 0;
236 dri_drawable
->numClipRects
= 1;
237 dri_drawable
->pClipRects
[0].x1
= 0;
238 dri_drawable
->pClipRects
[0].y1
= 0;
239 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
240 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
241 dri_drawable
->numBackClipRects
= 1;
242 dri_drawable
->pBackClipRects
[0].x1
= 0;
243 dri_drawable
->pBackClipRects
[0].y1
= 0;
244 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
245 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
247 *count
= num_buffers
;
254 * Process __DRIbuffer and convert them into pipe_textures.
257 dri2_drawable_process_buffers(struct dri_drawable
*drawable
,
258 __DRIbuffer
*buffers
, unsigned count
)
260 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
261 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
262 struct pipe_texture templ
;
263 struct winsys_handle whandle
;
264 boolean have_depth
= FALSE
;
267 if (drawable
->old_num
== count
&&
268 drawable
->old_w
== dri_drawable
->w
&&
269 drawable
->old_h
== dri_drawable
->h
&&
270 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0)
273 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
274 pipe_texture_reference(&drawable
->textures
[i
], NULL
);
276 memset(&templ
, 0, sizeof(templ
));
277 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
278 templ
.target
= PIPE_TEXTURE_2D
;
279 templ
.last_level
= 0;
280 templ
.width0
= dri_drawable
->w
;
281 templ
.height0
= dri_drawable
->h
;
284 memset(&whandle
, 0, sizeof(whandle
));
286 for (i
= 0; i
< count
; i
++) {
287 __DRIbuffer
*buf
= &buffers
[i
];
288 enum st_attachment_type statt
;
289 enum pipe_format format
;
291 switch (buf
->attachment
) {
292 case __DRI_BUFFER_FRONT_LEFT
:
293 if (!screen
->auto_fake_front
) {
294 statt
= ST_ATTACHMENT_INVALID
;
298 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
299 statt
= ST_ATTACHMENT_FRONT_LEFT
;
301 case __DRI_BUFFER_BACK_LEFT
:
302 statt
= ST_ATTACHMENT_BACK_LEFT
;
304 case __DRI_BUFFER_DEPTH
:
305 case __DRI_BUFFER_DEPTH_STENCIL
:
306 case __DRI_BUFFER_STENCIL
:
307 /* use only the first depth/stencil buffer */
310 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
313 statt
= ST_ATTACHMENT_INVALID
;
317 statt
= ST_ATTACHMENT_INVALID
;
321 format
= dri2_drawable_get_format(drawable
, statt
);
322 if (statt
== ST_ATTACHMENT_INVALID
|| format
== PIPE_FORMAT_NONE
)
325 templ
.format
= format
;
326 whandle
.handle
= buf
->name
;
327 whandle
.stride
= buf
->pitch
;
329 drawable
->textures
[statt
] =
330 screen
->pipe_screen
->texture_from_handle(screen
->pipe_screen
,
334 drawable
->old_num
= count
;
335 drawable
->old_w
= dri_drawable
->w
;
336 drawable
->old_h
= dri_drawable
->h
;
337 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
341 * Backend functions for st_framebuffer interface.
345 dri2_allocate_textures(struct dri_drawable
*drawable
,
346 const enum st_attachment_type
*statts
,
349 __DRIbuffer
*buffers
;
350 unsigned num_buffers
= count
;
352 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
353 dri2_drawable_process_buffers(drawable
, buffers
, num_buffers
);
357 dri2_flush_frontbuffer(struct dri_drawable
*drawable
,
358 enum st_attachment_type statt
)
360 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
361 struct __DRIdri2LoaderExtensionRec
*loader
= drawable
->sPriv
->dri2
.loader
;
363 if (loader
->flushFrontBuffer
== NULL
)
366 if (statt
== ST_ATTACHMENT_FRONT_LEFT
) {
367 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
372 dri2_lookup_egl_image(struct dri_context
*ctx
, void *handle
)
374 __DRIimageLookupExtension
*loader
= ctx
->sPriv
->dri2
.image
;
377 if (!loader
->lookupEGLImage
)
380 img
= loader
->lookupEGLImage(ctx
->cPriv
, handle
, ctx
->cPriv
->loaderPrivate
);
386 dri2_create_image_from_name(__DRIcontext
*context
,
387 int width
, int height
, int format
,
388 int name
, int pitch
, void *loaderPrivate
)
390 struct dri_screen
*screen
= dri_screen(context
->driScreenPriv
);
392 struct pipe_texture templ
;
393 struct winsys_handle whandle
;
397 tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
| PIPE_TEXTURE_USAGE_SAMPLER
;
400 case __DRI_IMAGE_FORMAT_RGB565
:
401 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
403 case __DRI_IMAGE_FORMAT_XRGB8888
:
404 pf
= PIPE_FORMAT_B8G8R8X8_UNORM
;
406 case __DRI_IMAGE_FORMAT_ARGB8888
:
407 pf
= PIPE_FORMAT_B8G8R8A8_UNORM
;
410 pf
= PIPE_FORMAT_NONE
;
413 if (pf
== PIPE_FORMAT_NONE
)
416 img
= CALLOC_STRUCT(__DRIimageRec
);
420 memset(&templ
, 0, sizeof(templ
));
421 templ
.tex_usage
= tex_usage
;
423 templ
.target
= PIPE_TEXTURE_2D
;
424 templ
.last_level
= 0;
425 templ
.width0
= width
;
426 templ
.height0
= height
;
429 memset(&whandle
, 0, sizeof(whandle
));
430 whandle
.handle
= name
;
431 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
433 img
->texture
= screen
->pipe_screen
->texture_from_handle(screen
->pipe_screen
,
443 img
->loader_private
= loaderPrivate
;
449 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
450 int renderbuffer
, void *loaderPrivate
)
452 struct dri_context
*ctx
= dri_context(context
->driverPrivate
);
454 if (!ctx
->st
->get_resource_for_egl_image
)
462 dri2_destroy_image(__DRIimage
*img
)
464 pipe_texture_reference(&img
->texture
, NULL
);
468 static struct __DRIimageExtensionRec dri2ImageExtension
= {
469 { __DRI_IMAGE
, __DRI_IMAGE_VERSION
},
470 dri2_create_image_from_name
,
471 dri2_create_image_from_renderbuffer
,
476 * Backend function init_screen.
479 static const __DRIextension
*dri_screen_extensions
[] = {
480 &driReadDrawableExtension
,
481 &driCopySubBufferExtension
.base
,
482 &driSwapControlExtension
.base
,
483 &driFrameTrackingExtension
.base
,
484 &driMediaStreamCounterExtension
.base
,
485 &dri2TexBufferExtension
.base
,
486 &dri2FlushExtension
.base
,
487 &dri2ImageExtension
.base
,
492 * This is the driver specific part of the createNewScreen entry point.
494 * Returns the __GLcontextModes supported by this driver.
497 dri2_init_screen(__DRIscreen
* sPriv
)
499 const __DRIconfig
**configs
;
500 struct dri_screen
*screen
;
501 struct drm_create_screen_arg arg
;
503 screen
= CALLOC_STRUCT(dri_screen
);
507 screen
->api
= drm_api_create();
508 screen
->sPriv
= sPriv
;
509 screen
->fd
= sPriv
->fd
;
511 sPriv
->private = (void *)screen
;
512 sPriv
->extensions
= dri_screen_extensions
;
514 arg
.mode
= DRM_CREATE_NORMAL
;
516 configs
= dri_init_screen_helper(screen
, &arg
, 32);
520 screen
->auto_fake_front
= dri_with_format(sPriv
);
524 dri_destroy_screen_helper(screen
);
529 /* This is the table of extensions that the loader will dlsym() for. */
530 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
531 &driCoreExtension
.base
,
532 &driLegacyExtension
.base
,
533 &driDRI2Extension
.base
,
537 /* vim: set sw=3 ts=8 sts=3 expandtab: */