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