1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_drawable.h"
37 #include "main/mtypes.h"
38 #include "main/renderbuffer.h"
39 #include "state_tracker/st_context.h"
40 #include "state_tracker/st_public.h"
41 #include "state_tracker/st_cb_fbo.h"
42 #include "state_tracker/drm_api.h"
44 #include "pipe/p_screen.h"
45 #include "util/u_format.h"
46 #include "util/u_memory.h"
47 #include "util/u_inlines.h"
49 static struct pipe_surface
*
50 dri_surface_from_handle(struct drm_api
*api
,
51 struct pipe_screen
*screen
,
53 enum pipe_format format
,
54 unsigned width
, unsigned height
, unsigned pitch
)
56 struct pipe_surface
*surface
= NULL
;
57 struct pipe_texture
*texture
= NULL
;
58 struct pipe_texture templat
;
59 struct winsys_handle whandle
;
61 memset(&templat
, 0, sizeof(templat
));
62 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
63 templat
.target
= PIPE_TEXTURE_2D
;
64 templat
.last_level
= 0;
66 templat
.format
= format
;
67 templat
.width0
= width
;
68 templat
.height0
= height
;
70 memset(&whandle
, 0, sizeof(whandle
));
71 whandle
.handle
= handle
;
72 whandle
.stride
= pitch
;
74 texture
= screen
->texture_from_handle(screen
, &templat
, &whandle
);
77 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__
);
81 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
82 PIPE_BUFFER_USAGE_GPU_READ
|
83 PIPE_BUFFER_USAGE_GPU_WRITE
);
85 /* we don't need the texture from this point on */
86 pipe_texture_reference(&texture
, NULL
);
91 * Pixmaps have will have the same name of fake front and front.
94 dri2_check_if_pixmap(__DRIbuffer
*buffers
, int count
)
96 boolean found
= FALSE
;
97 boolean is_pixmap
= FALSE
;
101 for (i
= 0; i
< count
; i
++) {
102 switch (buffers
[i
].attachment
) {
103 case __DRI_BUFFER_FRONT_LEFT
:
104 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
106 is_pixmap
= buffers
[i
].name
== name
;
108 name
= buffers
[i
].name
;
120 * This will be called a drawable is known to have been resized.
123 dri_get_buffers(__DRIdrawable
* dPriv
)
126 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
127 struct pipe_surface
*surface
= NULL
;
128 struct dri_screen
*st_screen
= dri_screen(drawable
->sPriv
);
129 struct pipe_screen
*screen
= st_screen
->pipe_screen
;
130 __DRIbuffer
*buffers
= NULL
;
131 __DRIscreen
*dri_screen
= drawable
->sPriv
;
132 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
133 struct drm_api
*api
= st_screen
->api
;
134 boolean have_depth
= FALSE
;
137 if ((dri_screen
->dri2
.loader
138 && (dri_screen
->dri2
.loader
->base
.version
> 2)
139 && (dri_screen
->dri2
.loader
->getBuffersWithFormat
!= NULL
))) {
140 buffers
= (*dri_screen
->dri2
.loader
->getBuffersWithFormat
)
141 (dri_drawable
, &dri_drawable
->w
, &dri_drawable
->h
,
142 drawable
->attachments
, drawable
->num_attachments
,
143 &count
, dri_drawable
->loaderPrivate
);
145 assert(dri_screen
->dri2
.loader
);
146 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
149 drawable
->attachments
,
151 num_attachments
, &count
,
156 if (buffers
== NULL
) {
160 /* set one cliprect to cover the whole dri_drawable */
163 dri_drawable
->backX
= 0;
164 dri_drawable
->backY
= 0;
165 dri_drawable
->numClipRects
= 1;
166 dri_drawable
->pClipRects
[0].x1
= 0;
167 dri_drawable
->pClipRects
[0].y1
= 0;
168 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
169 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
170 dri_drawable
->numBackClipRects
= 1;
171 dri_drawable
->pBackClipRects
[0].x1
= 0;
172 dri_drawable
->pBackClipRects
[0].y1
= 0;
173 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
174 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
176 if (drawable
->old_num
== count
&&
177 drawable
->old_w
== dri_drawable
->w
&&
178 drawable
->old_h
== dri_drawable
->h
&&
179 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
182 drawable
->old_num
= count
;
183 drawable
->old_w
= dri_drawable
->w
;
184 drawable
->old_h
= dri_drawable
->h
;
185 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
188 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
190 for (i
= 0; i
< count
; i
++) {
191 enum pipe_format format
= 0;
194 switch (buffers
[i
].attachment
) {
195 case __DRI_BUFFER_FRONT_LEFT
:
196 if (!st_screen
->auto_fake_front
)
199 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
200 index
= ST_SURFACE_FRONT_LEFT
;
201 format
= drawable
->color_format
;
203 case __DRI_BUFFER_BACK_LEFT
:
204 index
= ST_SURFACE_BACK_LEFT
;
205 format
= drawable
->color_format
;
207 case __DRI_BUFFER_DEPTH
:
208 case __DRI_BUFFER_DEPTH_STENCIL
:
209 case __DRI_BUFFER_STENCIL
:
210 index
= ST_SURFACE_DEPTH
;
211 format
= drawable
->depth_stencil_format
;
213 case __DRI_BUFFER_ACCUM
:
218 if (index
== ST_SURFACE_DEPTH
) {
225 surface
= dri_surface_from_handle(api
,
230 dri_drawable
->h
, buffers
[i
].pitch
);
232 switch (buffers
[i
].attachment
) {
233 case __DRI_BUFFER_FRONT_LEFT
:
234 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
235 case __DRI_BUFFER_BACK_LEFT
:
236 drawable
->color_format
= surface
->format
;
238 case __DRI_BUFFER_DEPTH
:
239 case __DRI_BUFFER_DEPTH_STENCIL
:
240 case __DRI_BUFFER_STENCIL
:
241 drawable
->depth_stencil_format
= surface
->format
;
243 case __DRI_BUFFER_ACCUM
:
248 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
249 pipe_surface_reference(&surface
, NULL
);
251 /* this needed, or else the state tracker fails to pick the new buffers */
252 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
256 * These are used for GLX_EXT_texture_from_pixmap
258 void dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
259 GLint format
, __DRIdrawable
*dPriv
)
261 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
262 struct pipe_surface
*ps
;
264 if (!drawable
->stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
265 struct gl_renderbuffer
*rb
=
266 st_new_renderbuffer_fb(drawable
->color_format
, 0 /*XXX*/, FALSE
);
267 _mesa_add_renderbuffer(&drawable
->stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
270 dri_get_buffers(drawable
->dPriv
);
271 st_get_framebuffer_surface(drawable
->stfb
, ST_SURFACE_FRONT_LEFT
, &ps
);
276 st_bind_texture_surface(ps
, target
== GL_TEXTURE_2D
? ST_TEXTURE_2D
:
277 ST_TEXTURE_RECT
, 0, drawable
->color_format
);
280 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
281 __DRIdrawable
*dPriv
)
283 dri2_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
287 dri_update_buffer(struct pipe_screen
*screen
, void *context_private
)
289 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
291 if (ctx
->d_stamp
== *ctx
->dPriv
->pStamp
&&
292 ctx
->r_stamp
== *ctx
->rPriv
->pStamp
)
295 ctx
->d_stamp
= *ctx
->dPriv
->pStamp
;
296 ctx
->r_stamp
= *ctx
->rPriv
->pStamp
;
298 /* Ask the X server for new renderbuffers. */
299 dri_get_buffers(ctx
->dPriv
);
300 if (ctx
->dPriv
!= ctx
->rPriv
)
301 dri_get_buffers(ctx
->rPriv
);
306 dri_flush_frontbuffer(struct pipe_screen
*screen
,
307 struct pipe_surface
*surf
, void *context_private
)
309 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
310 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
311 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
312 __DRIscreen
*dri_screen
= ctx
->sPriv
;
314 /* XXX Does this function get called with DRI1? */
316 if (ctx
->dPriv
== NULL
) {
317 debug_printf("%s: no drawable bound to context\n", __func__
);
322 /* TODO if rendering to pixmaps is slow enable this code. */
323 if (drawable
->is_pixmap
)
329 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
330 dri_drawable
->loaderPrivate
);
334 * This is called when we need to set up GL rendering to a new X window.
337 dri_create_buffer(__DRIscreen
* sPriv
,
338 __DRIdrawable
* dPriv
,
339 const __GLcontextModes
* visual
, boolean isPixmap
)
341 struct dri_screen
*screen
= sPriv
->private;
342 struct dri_drawable
*drawable
= NULL
;
346 goto fail
; /* not implemented */
348 drawable
= CALLOC_STRUCT(dri_drawable
);
349 if (drawable
== NULL
)
352 if (visual
->redBits
== 8) {
353 if (visual
->alphaBits
== 8)
354 drawable
->color_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
356 drawable
->color_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
358 drawable
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
361 switch(visual
->depthBits
) {
364 drawable
->depth_stencil_format
= PIPE_FORMAT_NONE
;
367 drawable
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
370 if (visual
->stencilBits
== 0) {
371 drawable
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
372 PIPE_FORMAT_Z24X8_UNORM
:
373 PIPE_FORMAT_X8Z24_UNORM
;
375 drawable
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
376 PIPE_FORMAT_Z24S8_UNORM
:
377 PIPE_FORMAT_S8Z24_UNORM
;
381 drawable
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
385 drawable
->stfb
= st_create_framebuffer(visual
,
386 drawable
->color_format
,
387 drawable
->depth_stencil_format
,
388 drawable
->depth_stencil_format
,
390 dPriv
->h
, (void *)drawable
);
391 if (drawable
->stfb
== NULL
)
394 drawable
->sPriv
= sPriv
;
395 drawable
->dPriv
= dPriv
;
396 dPriv
->driverPrivate
= (void *)drawable
;
398 /* setup dri2 buffers information */
399 /* TODO incase of double buffer visual, delay fake creation */
401 if (sPriv
->dri2
.loader
402 && (sPriv
->dri2
.loader
->base
.version
> 2)
403 && (sPriv
->dri2
.loader
->getBuffersWithFormat
!= NULL
)) {
404 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
405 drawable
->attachments
[i
++] = visual
->rgbBits
;
406 if (!screen
->auto_fake_front
) {
407 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
408 drawable
->attachments
[i
++] = visual
->rgbBits
;
410 if (visual
->doubleBufferMode
) {
411 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
412 drawable
->attachments
[i
++] = visual
->rgbBits
;
414 if (visual
->depthBits
&& visual
->stencilBits
) {
415 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
416 drawable
->attachments
[i
++] = visual
->depthBits
+ visual
->stencilBits
;
417 } else if (visual
->depthBits
) {
418 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
419 drawable
->attachments
[i
++] = visual
->depthBits
;
420 } else if (visual
->stencilBits
) {
421 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
422 drawable
->attachments
[i
++] = visual
->stencilBits
;
424 drawable
->num_attachments
= i
/ 2;
426 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
427 if (!screen
->auto_fake_front
)
428 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
429 if (visual
->doubleBufferMode
)
430 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
431 if (visual
->depthBits
&& visual
->stencilBits
)
432 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
433 else if (visual
->depthBits
)
434 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
435 else if (visual
->stencilBits
)
436 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
437 drawable
->num_attachments
= i
;
440 drawable
->desired_fences
= 2;
449 dri_destroy_buffer(__DRIdrawable
* dPriv
)
451 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
453 st_unreference_framebuffer(drawable
->stfb
);
454 drawable
->desired_fences
= 0;
456 dri1_swap_fences_clear(drawable
);
461 /* vim: set sw=3 ts=8 sts=3 expandtab: */