st/dri: merge dri/drm and dri/sw backends
[mesa.git] / src / gallium / state_trackers / dri / 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 / 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 "util/u_box.h"
41 #include "pipe/p_context.h"
42 #include "state_tracker/drisw_api.h"
43 #include "state_tracker/st_context.h"
44
45 #include "dri_screen.h"
46 #include "dri_context.h"
47 #include "dri_drawable.h"
48
49 DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE);
50 static boolean swrast_no_present = FALSE;
51
52 static INLINE void
53 get_drawable_info(__DRIdrawable *dPriv, int *x, int *y, int *w, int *h)
54 {
55 __DRIscreen *sPriv = dPriv->driScreenPriv;
56 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
57
58 loader->getDrawableInfo(dPriv,
59 x, y, w, h,
60 dPriv->loaderPrivate);
61 }
62
63 static INLINE void
64 put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height)
65 {
66 __DRIscreen *sPriv = dPriv->driScreenPriv;
67 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
68
69 loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
70 0, 0, width, height,
71 data, dPriv->loaderPrivate);
72 }
73
74 static INLINE void
75 put_image2(__DRIdrawable *dPriv, void *data, int x, int y,
76 unsigned width, unsigned height, unsigned stride)
77 {
78 __DRIscreen *sPriv = dPriv->driScreenPriv;
79 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
80
81 loader->putImage2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
82 x, y, width, height, stride,
83 data, dPriv->loaderPrivate);
84 }
85
86 static INLINE void
87 get_image(__DRIdrawable *dPriv, int x, int y, int width, int height, void *data)
88 {
89 __DRIscreen *sPriv = dPriv->driScreenPriv;
90 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
91
92 loader->getImage(dPriv,
93 x, y, width, height,
94 data, dPriv->loaderPrivate);
95 }
96
97 static void
98 drisw_update_drawable_info(struct dri_drawable *drawable)
99 {
100 __DRIdrawable *dPriv = drawable->dPriv;
101 int x, y;
102
103 get_drawable_info(dPriv, &x, &y, &dPriv->w, &dPriv->h);
104 }
105
106 static void
107 drisw_put_image(struct dri_drawable *drawable,
108 void *data, unsigned width, unsigned height)
109 {
110 __DRIdrawable *dPriv = drawable->dPriv;
111
112 put_image(dPriv, data, width, height);
113 }
114
115 static void
116 drisw_put_image2(struct dri_drawable *drawable,
117 void *data, int x, int y, unsigned width, unsigned height,
118 unsigned stride)
119 {
120 __DRIdrawable *dPriv = drawable->dPriv;
121
122 put_image2(dPriv, data, x, y, width, height, stride);
123 }
124
125 static INLINE void
126 drisw_present_texture(__DRIdrawable *dPriv,
127 struct pipe_resource *ptex, struct pipe_box *sub_box)
128 {
129 struct dri_drawable *drawable = dri_drawable(dPriv);
130 struct dri_screen *screen = dri_screen(drawable->sPriv);
131
132 if (swrast_no_present)
133 return;
134
135 screen->base.screen->flush_frontbuffer(screen->base.screen, ptex, 0, 0, drawable, sub_box);
136 }
137
138 static INLINE void
139 drisw_invalidate_drawable(__DRIdrawable *dPriv)
140 {
141 struct dri_drawable *drawable = dri_drawable(dPriv);
142
143 drawable->texture_stamp = dPriv->lastStamp - 1;
144
145 p_atomic_inc(&drawable->base.stamp);
146 }
147
148 static INLINE void
149 drisw_copy_to_front(__DRIdrawable * dPriv,
150 struct pipe_resource *ptex)
151 {
152 drisw_present_texture(dPriv, ptex, NULL);
153
154 drisw_invalidate_drawable(dPriv);
155 }
156
157 /*
158 * Backend functions for st_framebuffer interface and swap_buffers.
159 */
160
161 static void
162 drisw_swap_buffers(__DRIdrawable *dPriv)
163 {
164 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
165 struct dri_drawable *drawable = dri_drawable(dPriv);
166 struct pipe_resource *ptex;
167
168 if (!ctx)
169 return;
170
171 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
172
173 if (ptex) {
174 if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
175 pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
176
177 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
178
179 drisw_copy_to_front(dPriv, ptex);
180 }
181 }
182
183 static void
184 drisw_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
185 int w, int h)
186 {
187 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
188 struct dri_drawable *drawable = dri_drawable(dPriv);
189 struct pipe_resource *ptex;
190 struct pipe_box box;
191 if (!ctx)
192 return;
193
194 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
195
196 if (ptex) {
197 if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
198 pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
199
200 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
201
202 u_box_2d(x, dPriv->h - y - h, w, h, &box);
203 drisw_present_texture(dPriv, ptex, &box);
204 }
205 }
206
207 static void
208 drisw_flush_frontbuffer(struct dri_context *ctx,
209 struct dri_drawable *drawable,
210 enum st_attachment_type statt)
211 {
212 struct pipe_resource *ptex;
213
214 if (!ctx)
215 return;
216
217 ptex = drawable->textures[statt];
218
219 if (ptex) {
220 drisw_copy_to_front(ctx->dPriv, ptex);
221 }
222 }
223
224 /**
225 * Allocate framebuffer attachments.
226 *
227 * During fixed-size operation, the function keeps allocating new attachments
228 * as they are requested. Unused attachments are not removed, not until the
229 * framebuffer is resized or destroyed.
230 */
231 static void
232 drisw_allocate_textures(struct dri_context *stctx,
233 struct dri_drawable *drawable,
234 const enum st_attachment_type *statts,
235 unsigned count)
236 {
237 struct dri_screen *screen = dri_screen(drawable->sPriv);
238 struct pipe_resource templ;
239 unsigned width, height;
240 boolean resized;
241 unsigned i;
242
243 width = drawable->dPriv->w;
244 height = drawable->dPriv->h;
245
246 resized = (drawable->old_w != width ||
247 drawable->old_h != height);
248
249 /* remove outdated textures */
250 if (resized) {
251 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
252 pipe_resource_reference(&drawable->textures[i], NULL);
253 }
254
255 memset(&templ, 0, sizeof(templ));
256 templ.target = screen->target;
257 templ.width0 = width;
258 templ.height0 = height;
259 templ.depth0 = 1;
260 templ.array_size = 1;
261 templ.last_level = 0;
262
263 for (i = 0; i < count; i++) {
264 enum pipe_format format;
265 unsigned bind;
266
267 /* the texture already exists or not requested */
268 if (drawable->textures[statts[i]])
269 continue;
270
271 dri_drawable_get_format(drawable, statts[i], &format, &bind);
272
273 /* if we don't do any present, no need for display targets */
274 if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !swrast_no_present)
275 bind |= PIPE_BIND_DISPLAY_TARGET;
276
277 if (format == PIPE_FORMAT_NONE)
278 continue;
279
280 templ.format = format;
281 templ.bind = bind;
282
283 drawable->textures[statts[i]] =
284 screen->base.screen->resource_create(screen->base.screen, &templ);
285 }
286
287 drawable->old_w = width;
288 drawable->old_h = height;
289 }
290
291 static void
292 drisw_update_tex_buffer(struct dri_drawable *drawable,
293 struct dri_context *ctx,
294 struct pipe_resource *res)
295 {
296 __DRIdrawable *dPriv = drawable->dPriv;
297
298 struct st_context *st_ctx = (struct st_context *)ctx->st;
299 struct pipe_context *pipe = st_ctx->pipe;
300 struct pipe_transfer *transfer;
301 char *map;
302 int x, y, w, h;
303 int ximage_stride, line;
304 int cpp = util_format_get_blocksize(res->format);
305
306 get_drawable_info(dPriv, &x, &y, &w, &h);
307
308 map = pipe_transfer_map(pipe, res,
309 0, 0, // level, layer,
310 PIPE_TRANSFER_WRITE,
311 x, y, w, h, &transfer);
312
313 /* Copy the Drawable content to the mapped texture buffer */
314 get_image(dPriv, x, y, w, h, map);
315
316 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
317 get_image() has a pitch rounded up to 4 bytes. */
318 ximage_stride = ((w * cpp) + 3) & -4;
319 for (line = h-1; line; --line) {
320 memmove(&map[line * transfer->stride],
321 &map[line * ximage_stride],
322 ximage_stride);
323 }
324
325 pipe_transfer_unmap(pipe, transfer);
326 }
327
328 /*
329 * Backend function for init_screen.
330 */
331
332 static const __DRIextension *drisw_screen_extensions[] = {
333 &driTexBufferExtension.base,
334 NULL
335 };
336
337 static struct drisw_loader_funcs drisw_lf = {
338 .put_image = drisw_put_image,
339 .put_image2 = drisw_put_image2
340 };
341
342 static const __DRIconfig **
343 drisw_init_screen(__DRIscreen * sPriv)
344 {
345 const __DRIconfig **configs;
346 struct dri_screen *screen;
347 struct pipe_screen *pscreen;
348
349 screen = CALLOC_STRUCT(dri_screen);
350 if (!screen)
351 return NULL;
352
353 screen->sPriv = sPriv;
354 screen->fd = -1;
355
356 swrast_no_present = debug_get_option_swrast_no_present();
357
358 sPriv->driverPrivate = (void *)screen;
359 sPriv->extensions = drisw_screen_extensions;
360
361 pscreen = drisw_create_screen(&drisw_lf);
362 /* dri_init_screen_helper checks pscreen for us */
363
364 configs = dri_init_screen_helper(screen, pscreen, "swrast");
365 if (!configs)
366 goto fail;
367
368 return configs;
369 fail:
370 dri_destroy_screen_helper(screen);
371 FREE(screen);
372 return NULL;
373 }
374
375 static boolean
376 drisw_create_buffer(__DRIscreen * sPriv,
377 __DRIdrawable * dPriv,
378 const struct gl_config * visual, boolean isPixmap)
379 {
380 struct dri_drawable *drawable = NULL;
381
382 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
383 return FALSE;
384
385 drawable = dPriv->driverPrivate;
386
387 drawable->allocate_textures = drisw_allocate_textures;
388 drawable->update_drawable_info = drisw_update_drawable_info;
389 drawable->flush_frontbuffer = drisw_flush_frontbuffer;
390 drawable->update_tex_buffer = drisw_update_tex_buffer;
391
392 return TRUE;
393 }
394
395 /**
396 * DRI driver virtual function table.
397 *
398 * DRI versions differ in their implementation of init_screen and swap_buffers.
399 */
400 const struct __DriverAPIRec galliumsw_driver_api = {
401 .InitScreen = drisw_init_screen,
402 .DestroyScreen = dri_destroy_screen,
403 .CreateContext = dri_create_context,
404 .DestroyContext = dri_destroy_context,
405 .CreateBuffer = drisw_create_buffer,
406 .DestroyBuffer = dri_destroy_buffer,
407 .SwapBuffers = drisw_swap_buffers,
408 .MakeCurrent = dri_make_current,
409 .UnbindContext = dri_unbind_context,
410 .CopySubBuffer = drisw_copy_sub_buffer,
411 };
412
413 /* This is the table of extensions that the loader will dlsym() for. */
414 const __DRIextension *galliumsw_driver_extensions[] = {
415 &driCoreExtension.base,
416 &driSWRastExtension.base,
417 &driCopySubBufferExtension.base,
418 &gallium_config_options.base,
419 NULL
420 };
421
422 /* vim: set sw=3 ts=8 sts=3 expandtab: */