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"
42 swap_fences_unref(struct dri_drawable *draw);
45 dri_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
46 const enum st_attachment_type *statts,
48 struct pipe_resource **out)
50 struct dri_drawable *drawable =
51 (struct dri_drawable *) stfbi->st_manager_private;
52 struct dri_screen *screen = dri_screen(drawable->sPriv);
53 unsigned statt_mask, new_mask;
56 unsigned int lastStamp;
59 for (i = 0; i < count; i++)
60 statt_mask |= (1 << statts[i]);
62 /* record newly allocated textures */
63 new_mask = (statt_mask & ~drawable->texture_mask);
66 * dPriv->dri2.stamp is the server stamp. dPriv->lastStamp is the
67 * client stamp. It has the value of the server stamp when last
71 lastStamp = drawable->dPriv->lastStamp;
72 new_stamp = (drawable->texture_stamp != lastStamp);
74 if (new_stamp || new_mask || screen->broken_invalidate) {
75 if (new_stamp && drawable->update_drawable_info)
76 drawable->update_drawable_info(drawable);
78 drawable->allocate_textures(drawable, statts, count);
80 /* add existing textures */
81 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
82 if (drawable->textures[i])
83 statt_mask |= (1 << i);
86 drawable->texture_stamp = lastStamp;
87 drawable->texture_mask = statt_mask;
89 } while (lastStamp != drawable->dPriv->lastStamp);
94 for (i = 0; i < count; i++) {
96 pipe_resource_reference(&out[i], drawable->textures[statts[i]]);
103 dri_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
104 enum st_attachment_type statt)
106 struct dri_drawable *drawable =
107 (struct dri_drawable *) stfbi->st_manager_private;
109 /* XXX remove this and just set the correct one on the framebuffer */
110 drawable->flush_frontbuffer(drawable, statt);
116 * This is called when we need to set up GL rendering to a new X window.
119 dri_create_buffer(__DRIscreen * sPriv,
120 __DRIdrawable * dPriv,
121 const struct gl_config * visual, boolean isPixmap)
123 struct dri_screen *screen = sPriv->driverPrivate;
124 struct dri_drawable *drawable = NULL;
127 goto fail; /* not implemented */
129 drawable = CALLOC_STRUCT(dri_drawable);
130 if (drawable == NULL)
133 dri_fill_st_visual(&drawable->stvis, screen, visual);
135 /* setup the st_framebuffer_iface */
136 drawable->base.visual = &drawable->stvis;
137 drawable->base.flush_front = dri_st_framebuffer_flush_front;
138 drawable->base.validate = dri_st_framebuffer_validate;
139 drawable->base.st_manager_private = (void *) drawable;
141 drawable->screen = screen;
142 drawable->sPriv = sPriv;
143 drawable->dPriv = dPriv;
144 drawable->desired_fences = screen->default_throttle_frames;
145 if (drawable->desired_fences > DRI_SWAP_FENCES_MAX)
146 drawable->desired_fences = DRI_SWAP_FENCES_MAX;
148 dPriv->driverPrivate = (void *)drawable;
149 p_atomic_set(&drawable->base.stamp, 1);
158 dri_destroy_buffer(__DRIdrawable * dPriv)
160 struct dri_drawable *drawable = dri_drawable(dPriv);
163 pipe_surface_reference(&drawable->drisw_surface, NULL);
165 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
166 pipe_resource_reference(&drawable->textures[i], NULL);
168 swap_fences_unref(drawable);
174 * Validate the texture at an attachment. Allocate the texture if it does not
175 * exist. Used by the TFP extension.
178 dri_drawable_validate_att(struct dri_drawable *drawable,
179 enum st_attachment_type statt)
181 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
182 unsigned i, count = 0;
184 /* check if buffer already exists */
185 if (drawable->texture_mask & (1 << statt))
188 /* make sure DRI2 does not destroy existing buffers */
189 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
190 if (drawable->texture_mask & (1 << i)) {
194 statts[count++] = statt;
196 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
198 drawable->base.validate(&drawable->base, statts, count, NULL);
202 * These are used for GLX_EXT_texture_from_pixmap
205 dri_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
206 GLint format, __DRIdrawable *dPriv)
208 struct dri_context *ctx = dri_context(pDRICtx);
209 struct dri_drawable *drawable = dri_drawable(dPriv);
210 struct pipe_resource *pt;
212 dri_drawable_validate_att(drawable, ST_ATTACHMENT_FRONT_LEFT);
214 /* Use the pipe resource associated with the X drawable */
215 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
218 enum pipe_format internal_format = pt->format;
220 if (format == __DRI_TEXTURE_FORMAT_RGB) {
221 /* only need to cover the formats recognized by dri_fill_st_visual */
222 switch (internal_format) {
223 case PIPE_FORMAT_B8G8R8A8_UNORM:
224 internal_format = PIPE_FORMAT_B8G8R8X8_UNORM;
226 case PIPE_FORMAT_A8R8G8B8_UNORM:
227 internal_format = PIPE_FORMAT_X8R8G8B8_UNORM;
234 drawable->update_tex_buffer(drawable, ctx, pt);
236 ctx->st->teximage(ctx->st,
237 (target == GL_TEXTURE_2D) ? ST_TEXTURE_2D : ST_TEXTURE_RECT,
238 0, internal_format, pt, FALSE);
243 dri_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
244 __DRIdrawable *dPriv)
246 dri_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
249 const __DRItexBufferExtension driTexBufferExtension = {
250 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
257 * Get the format and binding of an attachment.
260 dri_drawable_get_format(struct dri_drawable *drawable,
261 enum st_attachment_type statt,
262 enum pipe_format *format,
266 case ST_ATTACHMENT_FRONT_LEFT:
267 case ST_ATTACHMENT_BACK_LEFT:
268 case ST_ATTACHMENT_FRONT_RIGHT:
269 case ST_ATTACHMENT_BACK_RIGHT:
270 *format = drawable->stvis.color_format;
271 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
273 case ST_ATTACHMENT_DEPTH_STENCIL:
274 *format = drawable->stvis.depth_stencil_format;
275 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
278 *format = PIPE_FORMAT_NONE;
286 * swap_fences_pop_front - pull a fence from the throttle queue
288 * If the throttle queue is filled to the desired number of fences,
289 * pull fences off the queue until the number is less than the desired
290 * number of fences, and return the last fence pulled.
292 static struct pipe_fence_handle *
293 swap_fences_pop_front(struct dri_drawable *draw)
295 struct pipe_screen *screen = draw->screen->base.screen;
296 struct pipe_fence_handle *fence = NULL;
298 if (draw->desired_fences == 0)
301 if (draw->cur_fences >= draw->desired_fences) {
302 screen->fence_reference(screen, &fence, draw->swap_fences[draw->tail]);
303 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
304 draw->tail &= DRI_SWAP_FENCES_MASK;
312 * swap_fences_push_back - push a fence onto the throttle queue
314 * push a fence onto the throttle queue and pull fences of the queue
315 * so that the desired number of fences are on the queue.
318 swap_fences_push_back(struct dri_drawable *draw,
319 struct pipe_fence_handle *fence)
321 struct pipe_screen *screen = draw->screen->base.screen;
323 if (!fence || draw->desired_fences == 0)
326 while(draw->cur_fences == draw->desired_fences)
327 swap_fences_pop_front(draw);
330 screen->fence_reference(screen, &draw->swap_fences[draw->head++],
332 draw->head &= DRI_SWAP_FENCES_MASK;
337 * swap_fences_unref - empty the throttle queue
339 * pulls fences of the throttle queue until it is empty.
342 swap_fences_unref(struct dri_drawable *draw)
344 struct pipe_screen *screen = draw->screen->base.screen;
346 while(draw->cur_fences) {
347 screen->fence_reference(screen, &draw->swap_fences[draw->tail++], NULL);
348 draw->tail &= DRI_SWAP_FENCES_MASK;
355 * dri_throttle - A DRI2ThrottleExtension throttling function.
357 * pulls a fence off the throttling queue and waits for it if the
358 * number of fences on the throttling queue has reached the desired
361 * Then flushes to insert a fence at the current rendering position, and
362 * pushes that fence on the queue. This requires that the st_context_iface
363 * flush method returns a fence even if there are no commands to flush.
366 dri_throttle(__DRIcontext *driCtx, __DRIdrawable *dPriv,
367 enum __DRI2throttleReason reason)
369 struct dri_drawable *draw = dri_drawable(dPriv);
370 struct st_context_iface *ctxi;
371 struct pipe_screen *screen = draw->screen->base.screen;
372 struct pipe_fence_handle *fence;
374 if (reason != __DRI2_THROTTLE_SWAPBUFFER &&
375 reason != __DRI2_THROTTLE_FLUSHFRONT)
378 fence = swap_fences_pop_front(draw);
380 (void) screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
381 screen->fence_reference(screen, &fence, NULL);
387 ctxi = dri_context(driCtx)->st;
388 ctxi->flush(ctxi, 0, &fence);
390 swap_fences_push_back(draw, fence);
391 screen->fence_reference(screen, &fence, NULL);
396 const __DRI2throttleExtension dri2ThrottleExtension = {
397 .base = { __DRI2_THROTTLE, __DRI2_THROTTLE_VERSION },
398 .throttle = dri_throttle,
402 /* vim: set sw=3 ts=8 sts=3 expandtab: */