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 "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39 #include "main/mtypes.h"
40 #include "main/renderbuffer.h"
41 #include "state_tracker/drm_api.h"
42 #include "state_tracker/dri1_api.h"
43 #include "state_tracker/st_context.h"
44 #include "state_tracker/st_public.h"
45 #include "state_tracker/st_cb_fbo.h"
47 #include "util/u_format.h"
48 #include "util/u_memory.h"
49 #include "util/u_rect.h"
50 #include "util/u_inlines.h"
52 static struct pipe_surface
*
53 dri_surface_from_handle(struct drm_api
*api
,
54 struct pipe_screen
*screen
,
56 enum pipe_format format
,
57 unsigned width
, unsigned height
, unsigned pitch
)
59 struct pipe_surface
*surface
= NULL
;
60 struct pipe_texture
*texture
= NULL
;
61 struct pipe_texture templat
;
62 struct winsys_handle whandle
;
64 memset(&templat
, 0, sizeof(templat
));
65 templat
.tex_usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
66 templat
.target
= PIPE_TEXTURE_2D
;
67 templat
.last_level
= 0;
69 templat
.format
= format
;
70 templat
.width0
= width
;
71 templat
.height0
= height
;
73 memset(&whandle
, 0, sizeof(whandle
));
74 whandle
.handle
= handle
;
75 whandle
.stride
= pitch
;
77 texture
= screen
->texture_from_handle(screen
, &templat
, &whandle
);
80 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__
);
84 surface
= screen
->get_tex_surface(screen
, texture
, 0, 0, 0,
85 PIPE_BUFFER_USAGE_GPU_READ
|
86 PIPE_BUFFER_USAGE_GPU_WRITE
);
88 /* we don't need the texture from this point on */
89 pipe_texture_reference(&texture
, NULL
);
94 * Pixmaps have will have the same name of fake front and front.
97 dri2_check_if_pixmap(__DRIbuffer
*buffers
, int count
)
99 boolean found
= FALSE
;
100 boolean is_pixmap
= FALSE
;
104 for (i
= 0; i
< count
; i
++) {
105 switch (buffers
[i
].attachment
) {
106 case __DRI_BUFFER_FRONT_LEFT
:
107 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
109 is_pixmap
= buffers
[i
].name
== name
;
111 name
= buffers
[i
].name
;
123 * This will be called a drawable is known to have been resized.
126 dri_get_buffers(__DRIdrawable
* dPriv
)
129 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
130 struct pipe_surface
*surface
= NULL
;
131 struct dri_screen
*st_screen
= dri_screen(drawable
->sPriv
);
132 struct pipe_screen
*screen
= st_screen
->pipe_screen
;
133 __DRIbuffer
*buffers
= NULL
;
134 __DRIscreen
*dri_screen
= drawable
->sPriv
;
135 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
136 struct drm_api
*api
= st_screen
->api
;
137 boolean have_depth
= FALSE
;
140 if ((dri_screen
->dri2
.loader
141 && (dri_screen
->dri2
.loader
->base
.version
> 2)
142 && (dri_screen
->dri2
.loader
->getBuffersWithFormat
!= NULL
))) {
143 buffers
= (*dri_screen
->dri2
.loader
->getBuffersWithFormat
)
144 (dri_drawable
, &dri_drawable
->w
, &dri_drawable
->h
,
145 drawable
->attachments
, drawable
->num_attachments
,
146 &count
, dri_drawable
->loaderPrivate
);
148 assert(dri_screen
->dri2
.loader
);
149 buffers
= (*dri_screen
->dri2
.loader
->getBuffers
) (dri_drawable
,
152 drawable
->attachments
,
154 num_attachments
, &count
,
159 if (buffers
== NULL
) {
163 /* set one cliprect to cover the whole dri_drawable */
166 dri_drawable
->backX
= 0;
167 dri_drawable
->backY
= 0;
168 dri_drawable
->numClipRects
= 1;
169 dri_drawable
->pClipRects
[0].x1
= 0;
170 dri_drawable
->pClipRects
[0].y1
= 0;
171 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
172 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
173 dri_drawable
->numBackClipRects
= 1;
174 dri_drawable
->pBackClipRects
[0].x1
= 0;
175 dri_drawable
->pBackClipRects
[0].y1
= 0;
176 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
177 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
179 if (drawable
->old_num
== count
&&
180 drawable
->old_w
== dri_drawable
->w
&&
181 drawable
->old_h
== dri_drawable
->h
&&
182 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0) {
185 drawable
->old_num
= count
;
186 drawable
->old_w
= dri_drawable
->w
;
187 drawable
->old_h
= dri_drawable
->h
;
188 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
191 drawable
->is_pixmap
= dri2_check_if_pixmap(buffers
, count
);
193 for (i
= 0; i
< count
; i
++) {
194 enum pipe_format format
= 0;
197 switch (buffers
[i
].attachment
) {
198 case __DRI_BUFFER_FRONT_LEFT
:
199 if (!st_screen
->auto_fake_front
)
202 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
203 index
= ST_SURFACE_FRONT_LEFT
;
204 format
= drawable
->color_format
;
206 case __DRI_BUFFER_BACK_LEFT
:
207 index
= ST_SURFACE_BACK_LEFT
;
208 format
= drawable
->color_format
;
210 case __DRI_BUFFER_DEPTH
:
211 case __DRI_BUFFER_DEPTH_STENCIL
:
212 case __DRI_BUFFER_STENCIL
:
213 index
= ST_SURFACE_DEPTH
;
214 format
= drawable
->depth_stencil_format
;
216 case __DRI_BUFFER_ACCUM
:
221 if (index
== ST_SURFACE_DEPTH
) {
228 surface
= dri_surface_from_handle(api
,
233 dri_drawable
->h
, buffers
[i
].pitch
);
235 switch (buffers
[i
].attachment
) {
236 case __DRI_BUFFER_FRONT_LEFT
:
237 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
238 case __DRI_BUFFER_BACK_LEFT
:
239 drawable
->color_format
= surface
->format
;
241 case __DRI_BUFFER_DEPTH
:
242 case __DRI_BUFFER_DEPTH_STENCIL
:
243 case __DRI_BUFFER_STENCIL
:
244 drawable
->depth_stencil_format
= surface
->format
;
246 case __DRI_BUFFER_ACCUM
:
251 st_set_framebuffer_surface(drawable
->stfb
, index
, surface
);
252 pipe_surface_reference(&surface
, NULL
);
254 /* this needed, or else the state tracker fails to pick the new buffers */
255 st_resize_framebuffer(drawable
->stfb
, dri_drawable
->w
, dri_drawable
->h
);
259 * These are used for GLX_EXT_texture_from_pixmap
261 void dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
262 GLint format
, __DRIdrawable
*dPriv
)
264 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
265 struct pipe_surface
*ps
;
267 if (!drawable
->stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
) {
268 struct gl_renderbuffer
*rb
=
269 st_new_renderbuffer_fb(drawable
->color_format
, 0 /*XXX*/, FALSE
);
270 _mesa_add_renderbuffer(&drawable
->stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
273 dri_get_buffers(drawable
->dPriv
);
274 st_get_framebuffer_surface(drawable
->stfb
, ST_SURFACE_FRONT_LEFT
, &ps
);
279 st_bind_texture_surface(ps
, target
== GL_TEXTURE_2D
? ST_TEXTURE_2D
:
280 ST_TEXTURE_RECT
, 0, drawable
->color_format
);
283 void dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
284 __DRIdrawable
*dPriv
)
286 dri2_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
290 dri_update_buffer(struct pipe_screen
*screen
, void *context_private
)
292 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
294 if (ctx
->d_stamp
== *ctx
->dPriv
->pStamp
&&
295 ctx
->r_stamp
== *ctx
->rPriv
->pStamp
)
298 ctx
->d_stamp
= *ctx
->dPriv
->pStamp
;
299 ctx
->r_stamp
= *ctx
->rPriv
->pStamp
;
301 /* Ask the X server for new renderbuffers. */
302 dri_get_buffers(ctx
->dPriv
);
303 if (ctx
->dPriv
!= ctx
->rPriv
)
304 dri_get_buffers(ctx
->rPriv
);
309 dri_flush_frontbuffer(struct pipe_screen
*screen
,
310 struct pipe_surface
*surf
, void *context_private
)
312 struct dri_context
*ctx
= (struct dri_context
*)context_private
;
313 struct dri_drawable
*drawable
= dri_drawable(ctx
->dPriv
);
314 __DRIdrawable
*dri_drawable
= ctx
->dPriv
;
315 __DRIscreen
*dri_screen
= ctx
->sPriv
;
317 /* XXX Does this function get called with DRI1? */
319 if (ctx
->dPriv
== NULL
) {
320 debug_printf("%s: no drawable bound to context\n", __func__
);
325 /* TODO if rendering to pixmaps is slow enable this code. */
326 if (drawable
->is_pixmap
)
332 (*dri_screen
->dri2
.loader
->flushFrontBuffer
)(dri_drawable
,
333 dri_drawable
->loaderPrivate
);
337 * This is called when we need to set up GL rendering to a new X window.
340 dri_create_buffer(__DRIscreen
* sPriv
,
341 __DRIdrawable
* dPriv
,
342 const __GLcontextModes
* visual
, boolean isPixmap
)
344 struct dri_screen
*screen
= sPriv
->private;
345 struct dri_drawable
*drawable
= NULL
;
349 goto fail
; /* not implemented */
351 drawable
= CALLOC_STRUCT(dri_drawable
);
352 if (drawable
== NULL
)
355 if (visual
->redBits
== 8) {
356 if (visual
->alphaBits
== 8)
357 drawable
->color_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
359 drawable
->color_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
361 drawable
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
364 switch(visual
->depthBits
) {
367 drawable
->depth_stencil_format
= PIPE_FORMAT_NONE
;
370 drawable
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
373 if (visual
->stencilBits
== 0) {
374 drawable
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
375 PIPE_FORMAT_Z24X8_UNORM
:
376 PIPE_FORMAT_X8Z24_UNORM
;
378 drawable
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
379 PIPE_FORMAT_Z24S8_UNORM
:
380 PIPE_FORMAT_S8Z24_UNORM
;
384 drawable
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
388 drawable
->stfb
= st_create_framebuffer(visual
,
389 drawable
->color_format
,
390 drawable
->depth_stencil_format
,
391 drawable
->depth_stencil_format
,
393 dPriv
->h
, (void *)drawable
);
394 if (drawable
->stfb
== NULL
)
397 drawable
->sPriv
= sPriv
;
398 drawable
->dPriv
= dPriv
;
399 dPriv
->driverPrivate
= (void *)drawable
;
401 /* setup dri2 buffers information */
402 /* TODO incase of double buffer visual, delay fake creation */
404 if (sPriv
->dri2
.loader
405 && (sPriv
->dri2
.loader
->base
.version
> 2)
406 && (sPriv
->dri2
.loader
->getBuffersWithFormat
!= NULL
)) {
407 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
408 drawable
->attachments
[i
++] = visual
->rgbBits
;
409 if (!screen
->auto_fake_front
) {
410 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
411 drawable
->attachments
[i
++] = visual
->rgbBits
;
413 if (visual
->doubleBufferMode
) {
414 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
415 drawable
->attachments
[i
++] = visual
->rgbBits
;
417 if (visual
->depthBits
&& visual
->stencilBits
) {
418 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
419 drawable
->attachments
[i
++] = visual
->depthBits
+ visual
->stencilBits
;
420 } else if (visual
->depthBits
) {
421 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
422 drawable
->attachments
[i
++] = visual
->depthBits
;
423 } else if (visual
->stencilBits
) {
424 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
425 drawable
->attachments
[i
++] = visual
->stencilBits
;
427 drawable
->num_attachments
= i
/ 2;
429 drawable
->attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
430 if (!screen
->auto_fake_front
)
431 drawable
->attachments
[i
++] = __DRI_BUFFER_FAKE_FRONT_LEFT
;
432 if (visual
->doubleBufferMode
)
433 drawable
->attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
434 if (visual
->depthBits
&& visual
->stencilBits
)
435 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
436 else if (visual
->depthBits
)
437 drawable
->attachments
[i
++] = __DRI_BUFFER_DEPTH
;
438 else if (visual
->stencilBits
)
439 drawable
->attachments
[i
++] = __DRI_BUFFER_STENCIL
;
440 drawable
->num_attachments
= i
;
443 drawable
->desired_fences
= 2;
452 dri_destroy_buffer(__DRIdrawable
* dPriv
)
454 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
456 st_unreference_framebuffer(drawable
->stfb
);
457 drawable
->desired_fences
= 0;
459 dri1_swap_fences_clear(drawable
);
464 /* vim: set sw=3 ts=8 sts=3 expandtab: */