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"
36 #include "pipe/p_screen.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
41 #include "state_tracker/st_context.h"
44 dri_st_framebuffer_validate(struct st_framebuffer_iface
*stfbi
,
45 const enum st_attachment_type
*statts
,
47 struct pipe_resource
**out
)
49 struct dri_drawable
*drawable
=
50 (struct dri_drawable
*) stfbi
->st_manager_private
;
51 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
52 unsigned statt_mask
, new_mask
;
57 for (i
= 0; i
< count
; i
++)
58 statt_mask
|= (1 << statts
[i
]);
60 /* record newly allocated textures */
61 new_mask
= (statt_mask
& ~drawable
->texture_mask
);
64 * dPriv->pStamp is the server stamp. It should be accessed with a lock, at
65 * least for DRI1. dPriv->lastStamp is the client stamp. It has the value
66 * of the server stamp when last checked.
68 new_stamp
= (drawable
->texture_stamp
!= drawable
->dPriv
->lastStamp
);
70 if (new_stamp
|| new_mask
|| screen
->broken_invalidate
) {
71 if (new_stamp
&& drawable
->update_drawable_info
)
72 drawable
->update_drawable_info(drawable
);
74 drawable
->allocate_textures(drawable
, statts
, count
);
76 /* add existing textures */
77 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
78 if (drawable
->textures
[i
])
79 statt_mask
|= (1 << i
);
82 drawable
->texture_stamp
= drawable
->dPriv
->lastStamp
;
83 drawable
->texture_mask
= statt_mask
;
89 for (i
= 0; i
< count
; i
++) {
91 pipe_resource_reference(&out
[i
], drawable
->textures
[statts
[i
]]);
98 dri_st_framebuffer_flush_front(struct st_framebuffer_iface
*stfbi
,
99 enum st_attachment_type statt
)
101 struct dri_drawable
*drawable
=
102 (struct dri_drawable
*) stfbi
->st_manager_private
;
104 /* XXX remove this and just set the correct one on the framebuffer */
105 drawable
->flush_frontbuffer(drawable
, statt
);
111 * This is called when we need to set up GL rendering to a new X window.
114 dri_create_buffer(__DRIscreen
* sPriv
,
115 __DRIdrawable
* dPriv
,
116 const struct gl_config
* visual
, boolean isPixmap
)
118 struct dri_screen
*screen
= sPriv
->private;
119 struct dri_drawable
*drawable
= NULL
;
122 goto fail
; /* not implemented */
124 drawable
= CALLOC_STRUCT(dri_drawable
);
125 if (drawable
== NULL
)
128 dri_fill_st_visual(&drawable
->stvis
, screen
, visual
);
130 /* setup the st_framebuffer_iface */
131 drawable
->base
.visual
= &drawable
->stvis
;
132 drawable
->base
.flush_front
= dri_st_framebuffer_flush_front
;
133 drawable
->base
.validate
= dri_st_framebuffer_validate
;
134 drawable
->base
.st_manager_private
= (void *) drawable
;
136 drawable
->screen
= screen
;
137 drawable
->sPriv
= sPriv
;
138 drawable
->dPriv
= dPriv
;
139 dPriv
->driverPrivate
= (void *)drawable
;
140 p_atomic_set(&drawable
->base
.stamp
, 1);
149 dri_destroy_buffer(__DRIdrawable
* dPriv
)
151 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
154 pipe_surface_reference(&drawable
->drisw_surface
, NULL
);
156 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
157 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
163 * Validate the texture at an attachment. Allocate the texture if it does not
164 * exist. Used by the TFP extension.
167 dri_drawable_validate_att(struct dri_drawable
*drawable
,
168 enum st_attachment_type statt
)
170 enum st_attachment_type statts
[ST_ATTACHMENT_COUNT
];
171 unsigned i
, count
= 0;
173 /* check if buffer already exists */
174 if (drawable
->texture_mask
& (1 << statt
))
177 /* make sure DRI2 does not destroy existing buffers */
178 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
179 if (drawable
->texture_mask
& (1 << i
)) {
183 statts
[count
++] = statt
;
185 drawable
->texture_stamp
= drawable
->dPriv
->lastStamp
- 1;
187 drawable
->base
.validate(&drawable
->base
, statts
, count
, NULL
);
191 * These are used for GLX_EXT_texture_from_pixmap
194 dri_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
195 GLint format
, __DRIdrawable
*dPriv
)
197 struct dri_context
*ctx
= dri_context(pDRICtx
);
198 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
199 struct pipe_resource
*res
;
200 struct st_context
*stctx
= (struct st_context
*)ctx
->st
;
201 struct pipe_context
*pipe
= stctx
->pipe
;
202 struct pipe_transfer
*tex_xfer
;
204 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
205 int x
, y
, w
, h
, line
, ximage_stride
;
207 sPriv
->swrast_loader
->getDrawableInfo(dPriv
, &x
, &y
, &w
, &h
, dPriv
->loaderPrivate
);
209 dri_drawable_validate_att(drawable
, ST_ATTACHMENT_FRONT_LEFT
);
211 /* Use the pipe resource associated with the X drawable */
212 res
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
215 enum pipe_format internal_format
= res
->format
;
217 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
218 /* only need to cover the formats recognized by dri_fill_st_visual */
219 switch (internal_format
) {
220 case PIPE_FORMAT_B8G8R8A8_UNORM
:
221 internal_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
223 case PIPE_FORMAT_A8R8G8B8_UNORM
:
224 internal_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
232 tex_xfer
= pipe_get_transfer(pipe
, res
,
233 0, 0, // level, layer
239 map
= pipe_transfer_map(pipe
, tex_xfer
);
241 /* Copy the Drawable content to the mapped texture buffer */
242 sPriv
->swrast_loader
->getImage(dPriv
, x
, y
, w
, h
, map
,
243 dPriv
->loaderPrivate
);
245 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
246 We assume 32 bit pixels. */
247 ximage_stride
= w
* 4;
248 for (line
= h
-1; line
; --line
) {
249 memmove(&map
[line
* tex_xfer
->stride
], &map
[line
* ximage_stride
], ximage_stride
);
252 pipe_transfer_unmap(pipe
, tex_xfer
);
254 pipe_transfer_destroy(pipe
, tex_xfer
);
256 ctx
->st
->teximage(ctx
->st
,
257 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
258 0, internal_format
, res
, FALSE
);
264 dri_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
265 __DRIdrawable
*dPriv
)
267 dri_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
270 const __DRItexBufferExtension driTexBufferExtension
= {
271 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
278 * Get the format and binding of an attachment.
281 dri_drawable_get_format(struct dri_drawable
*drawable
,
282 enum st_attachment_type statt
,
283 enum pipe_format
*format
,
287 case ST_ATTACHMENT_FRONT_LEFT
:
288 case ST_ATTACHMENT_BACK_LEFT
:
289 case ST_ATTACHMENT_FRONT_RIGHT
:
290 case ST_ATTACHMENT_BACK_RIGHT
:
291 *format
= drawable
->stvis
.color_format
;
292 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
294 case ST_ATTACHMENT_DEPTH_STENCIL
:
295 *format
= drawable
->stvis
.depth_stencil_format
;
296 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
299 *format
= PIPE_FORMAT_NONE
;
305 /* vim: set sw=3 ts=8 sts=3 expandtab: */