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