st-api: Rework how drawables are invalidated v3.
[mesa.git] / src / gallium / state_trackers / dri / sw / drisw.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /* TODO:
30 *
31 * xshm / texture_from_pixmap / EGLImage:
32 *
33 * Allow the loaders to use the XSHM extension. It probably requires callbacks
34 * for createImage/destroyImage similar to DRI2 getBuffers.
35 */
36
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40 #include "pipe/p_context.h"
41 #include "state_tracker/drisw_api.h"
42
43 #include "dri_screen.h"
44 #include "dri_context.h"
45 #include "dri_drawable.h"
46
47 DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE);
48 static boolean swrast_no_present = FALSE;
49
50 static INLINE void
51 get_drawable_info(__DRIdrawable *dPriv, int *w, int *h)
52 {
53 __DRIscreen *sPriv = dPriv->driScreenPriv;
54 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
55 int x, y;
56
57 loader->getDrawableInfo(dPriv,
58 &x, &y, w, h,
59 dPriv->loaderPrivate);
60 }
61
62 static INLINE void
63 put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height)
64 {
65 __DRIscreen *sPriv = dPriv->driScreenPriv;
66 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
67
68 loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
69 0, 0, width, height,
70 data, dPriv->loaderPrivate);
71 }
72
73 static void
74 drisw_update_drawable_info(struct dri_drawable *drawable)
75 {
76 __DRIdrawable *dPriv = drawable->dPriv;
77
78 get_drawable_info(dPriv, &dPriv->w, &dPriv->h);
79 }
80
81 static void
82 drisw_put_image(struct dri_drawable *drawable,
83 void *data, unsigned width, unsigned height)
84 {
85 __DRIdrawable *dPriv = drawable->dPriv;
86
87 put_image(dPriv, data, width, height);
88 }
89
90 static INLINE void
91 drisw_present_texture(__DRIdrawable *dPriv,
92 struct pipe_resource *ptex)
93 {
94 struct dri_drawable *drawable = dri_drawable(dPriv);
95 struct dri_screen *screen = dri_screen(drawable->sPriv);
96
97 if (swrast_no_present)
98 return;
99
100 screen->base.screen->flush_frontbuffer(screen->base.screen, ptex, 0, 0, drawable);
101 }
102
103 static INLINE void
104 drisw_invalidate_drawable(__DRIdrawable *dPriv)
105 {
106 struct dri_drawable *drawable = dri_drawable(dPriv);
107
108 drawable->texture_stamp = dPriv->lastStamp - 1;
109
110 p_atomic_inc(&drawable->base.stamp);
111 }
112
113 static INLINE void
114 drisw_copy_to_front(__DRIdrawable * dPriv,
115 struct pipe_resource *ptex)
116 {
117 drisw_present_texture(dPriv, ptex);
118
119 drisw_invalidate_drawable(dPriv);
120 }
121
122 /*
123 * Backend functions for st_framebuffer interface and swap_buffers.
124 */
125
126 static void
127 drisw_swap_buffers(__DRIdrawable *dPriv)
128 {
129 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
130 struct dri_drawable *drawable = dri_drawable(dPriv);
131 struct pipe_resource *ptex;
132
133 if (!ctx)
134 return;
135
136 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
137
138 if (ptex) {
139 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
140
141 drisw_copy_to_front(dPriv, ptex);
142 }
143 }
144
145 static void
146 drisw_flush_frontbuffer(struct dri_drawable *drawable,
147 enum st_attachment_type statt)
148 {
149 struct dri_context *ctx = dri_get_current(drawable->sPriv);
150 struct pipe_resource *ptex;
151
152 if (!ctx)
153 return;
154
155 ptex = drawable->textures[statt];
156
157 if (ptex) {
158 drisw_copy_to_front(ctx->dPriv, ptex);
159 }
160 }
161
162 /**
163 * Allocate framebuffer attachments.
164 *
165 * During fixed-size operation, the function keeps allocating new attachments
166 * as they are requested. Unused attachments are not removed, not until the
167 * framebuffer is resized or destroyed.
168 */
169 static void
170 drisw_allocate_textures(struct dri_drawable *drawable,
171 const enum st_attachment_type *statts,
172 unsigned count)
173 {
174 struct dri_screen *screen = dri_screen(drawable->sPriv);
175 struct pipe_resource templ;
176 unsigned width, height;
177 boolean resized;
178 unsigned i;
179
180 width = drawable->dPriv->w;
181 height = drawable->dPriv->h;
182
183 resized = (drawable->old_w != width ||
184 drawable->old_h != height);
185
186 /* remove outdated textures */
187 if (resized) {
188 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
189 pipe_resource_reference(&drawable->textures[i], NULL);
190 }
191
192 memset(&templ, 0, sizeof(templ));
193 templ.target = screen->target;
194 templ.width0 = width;
195 templ.height0 = height;
196 templ.depth0 = 1;
197 templ.array_size = 1;
198 templ.last_level = 0;
199
200 for (i = 0; i < count; i++) {
201 enum pipe_format format;
202 unsigned bind;
203
204 /* the texture already exists or not requested */
205 if (drawable->textures[statts[i]])
206 continue;
207
208 dri_drawable_get_format(drawable, statts[i], &format, &bind);
209
210 /* if we don't do any present, no need for display targets */
211 if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !swrast_no_present)
212 bind |= PIPE_BIND_DISPLAY_TARGET;
213
214 if (format == PIPE_FORMAT_NONE)
215 continue;
216
217 templ.format = format;
218 templ.bind = bind;
219
220 drawable->textures[statts[i]] =
221 screen->base.screen->resource_create(screen->base.screen, &templ);
222 }
223
224 drawable->old_w = width;
225 drawable->old_h = height;
226 }
227
228 /*
229 * Backend function for init_screen.
230 */
231
232 static const __DRIextension *drisw_screen_extensions[] = {
233 &driTexBufferExtension.base,
234 NULL
235 };
236
237 static struct drisw_loader_funcs drisw_lf = {
238 .put_image = drisw_put_image
239 };
240
241 static const __DRIconfig **
242 drisw_init_screen(__DRIscreen * sPriv)
243 {
244 const __DRIconfig **configs;
245 struct dri_screen *screen;
246 struct pipe_screen *pscreen;
247
248 screen = CALLOC_STRUCT(dri_screen);
249 if (!screen)
250 return NULL;
251
252 screen->sPriv = sPriv;
253 screen->fd = -1;
254
255 swrast_no_present = debug_get_option_swrast_no_present();
256
257 sPriv->private = (void *)screen;
258 sPriv->extensions = drisw_screen_extensions;
259
260 pscreen = drisw_create_screen(&drisw_lf);
261 /* dri_init_screen_helper checks pscreen for us */
262
263 configs = dri_init_screen_helper(screen, pscreen, 32);
264 if (!configs)
265 goto fail;
266
267 return configs;
268 fail:
269 dri_destroy_screen_helper(screen);
270 FREE(screen);
271 return NULL;
272 }
273
274 static boolean
275 drisw_create_buffer(__DRIscreen * sPriv,
276 __DRIdrawable * dPriv,
277 const struct gl_config * visual, boolean isPixmap)
278 {
279 struct dri_drawable *drawable = NULL;
280
281 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
282 return FALSE;
283
284 drawable = dPriv->driverPrivate;
285
286 drawable->allocate_textures = drisw_allocate_textures;
287 drawable->update_drawable_info = drisw_update_drawable_info;
288 drawable->flush_frontbuffer = drisw_flush_frontbuffer;
289
290 return TRUE;
291 }
292
293 /**
294 * DRI driver virtual function table.
295 *
296 * DRI versions differ in their implementation of init_screen and swap_buffers.
297 */
298 const struct __DriverAPIRec driDriverAPI = {
299 .InitScreen = drisw_init_screen,
300 .DestroyScreen = dri_destroy_screen,
301 .CreateContext = dri_create_context,
302 .DestroyContext = dri_destroy_context,
303 .CreateBuffer = drisw_create_buffer,
304 .DestroyBuffer = dri_destroy_buffer,
305 .MakeCurrent = dri_make_current,
306 .UnbindContext = dri_unbind_context,
307
308 .SwapBuffers = drisw_swap_buffers,
309 };
310
311 /* This is the table of extensions that the loader will dlsym() for. */
312 PUBLIC const __DRIextension *__driDriverExtensions[] = {
313 &driCoreExtension.base,
314 &driSWRastExtension.base,
315 NULL
316 };
317
318 /* vim: set sw=3 ts=8 sts=3 expandtab: */