8d7148ef6dc229a0637a5e64401aa405faa89a53
[mesa.git] / src / gallium / frontends / 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 "frontend/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 if (loader->base.version > 5 && loader->getImageShm2)
142 return loader->getImageShm2(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
143
144 loader->getImageShm(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
145 return TRUE;
146 }
147
148 static void
149 drisw_update_drawable_info(struct dri_drawable *drawable)
150 {
151 __DRIdrawable *dPriv = drawable->dPriv;
152 int x, y;
153
154 get_drawable_info(dPriv, &x, &y, &dPriv->w, &dPriv->h);
155 }
156
157 static void
158 drisw_get_image(struct dri_drawable *drawable,
159 int x, int y, unsigned width, unsigned height, unsigned stride,
160 void *data)
161 {
162 __DRIdrawable *dPriv = drawable->dPriv;
163 int draw_x, draw_y, draw_w, draw_h;
164
165 get_drawable_info(dPriv, &draw_x, &draw_y, &draw_w, &draw_h);
166 get_image2(dPriv, x, y, draw_w, draw_h, stride, data);
167 }
168
169 static void
170 drisw_put_image(struct dri_drawable *drawable,
171 void *data, unsigned width, unsigned height)
172 {
173 __DRIdrawable *dPriv = drawable->dPriv;
174
175 put_image(dPriv, data, width, height);
176 }
177
178 static void
179 drisw_put_image2(struct dri_drawable *drawable,
180 void *data, int x, int y, unsigned width, unsigned height,
181 unsigned stride)
182 {
183 __DRIdrawable *dPriv = drawable->dPriv;
184
185 put_image2(dPriv, data, x, y, width, height, stride);
186 }
187
188 static inline void
189 drisw_put_image_shm(struct dri_drawable *drawable,
190 int shmid, char *shmaddr, unsigned offset,
191 unsigned offset_x,
192 int x, int y, unsigned width, unsigned height,
193 unsigned stride)
194 {
195 __DRIdrawable *dPriv = drawable->dPriv;
196
197 put_image_shm(dPriv, shmid, shmaddr, offset, offset_x, x, y, width, height, stride);
198 }
199
200 static inline void
201 drisw_present_texture(__DRIdrawable *dPriv,
202 struct pipe_resource *ptex, struct pipe_box *sub_box)
203 {
204 struct dri_drawable *drawable = dri_drawable(dPriv);
205 struct dri_screen *screen = dri_screen(drawable->sPriv);
206
207 if (screen->swrast_no_present)
208 return;
209
210 screen->base.screen->flush_frontbuffer(screen->base.screen, ptex, 0, 0, drawable, sub_box);
211 }
212
213 static inline void
214 drisw_invalidate_drawable(__DRIdrawable *dPriv)
215 {
216 struct dri_drawable *drawable = dri_drawable(dPriv);
217
218 drawable->texture_stamp = dPriv->lastStamp - 1;
219
220 p_atomic_inc(&drawable->base.stamp);
221 }
222
223 static inline void
224 drisw_copy_to_front(__DRIdrawable * dPriv,
225 struct pipe_resource *ptex)
226 {
227 drisw_present_texture(dPriv, ptex, NULL);
228
229 drisw_invalidate_drawable(dPriv);
230 }
231
232 /*
233 * Backend functions for st_framebuffer interface and swap_buffers.
234 */
235
236 static void
237 drisw_swap_buffers(__DRIdrawable *dPriv)
238 {
239 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
240 struct dri_drawable *drawable = dri_drawable(dPriv);
241 struct pipe_resource *ptex;
242
243 if (!ctx)
244 return;
245
246 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
247
248 if (ptex) {
249 if (ctx->pp)
250 pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
251
252 if (ctx->hud)
253 hud_run(ctx->hud, ctx->st->cso_context, ptex);
254
255 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
256
257 if (drawable->stvis.samples > 1) {
258 /* Resolve the back buffer. */
259 dri_pipe_blit(ctx->st->pipe,
260 drawable->textures[ST_ATTACHMENT_BACK_LEFT],
261 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
262 }
263
264 drisw_copy_to_front(dPriv, ptex);
265 }
266 }
267
268 static void
269 drisw_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
270 int w, int h)
271 {
272 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
273 struct dri_drawable *drawable = dri_drawable(dPriv);
274 struct pipe_resource *ptex;
275 struct pipe_box box;
276 if (!ctx)
277 return;
278
279 ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
280
281 if (ptex) {
282 if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
283 pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
284
285 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
286
287 u_box_2d(x, dPriv->h - y - h, w, h, &box);
288 drisw_present_texture(dPriv, ptex, &box);
289 }
290 }
291
292 static void
293 drisw_flush_frontbuffer(struct dri_context *ctx,
294 struct dri_drawable *drawable,
295 enum st_attachment_type statt)
296 {
297 struct pipe_resource *ptex;
298
299 if (!ctx)
300 return;
301
302 if (drawable->stvis.samples > 1) {
303 /* Resolve the front buffer. */
304 dri_pipe_blit(ctx->st->pipe,
305 drawable->textures[ST_ATTACHMENT_FRONT_LEFT],
306 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT]);
307 }
308 ptex = drawable->textures[statt];
309
310 if (ptex) {
311 drisw_copy_to_front(ctx->dPriv, ptex);
312 }
313 }
314
315 /**
316 * Allocate framebuffer attachments.
317 *
318 * During fixed-size operation, the function keeps allocating new attachments
319 * as they are requested. Unused attachments are not removed, not until the
320 * framebuffer is resized or destroyed.
321 */
322 static void
323 drisw_allocate_textures(struct dri_context *stctx,
324 struct dri_drawable *drawable,
325 const enum st_attachment_type *statts,
326 unsigned count)
327 {
328 struct dri_screen *screen = dri_screen(drawable->sPriv);
329 const __DRIswrastLoaderExtension *loader = drawable->dPriv->driScreenPriv->swrast_loader;
330 struct pipe_resource templ;
331 unsigned width, height;
332 boolean resized;
333 unsigned i;
334
335 width = drawable->dPriv->w;
336 height = drawable->dPriv->h;
337
338 resized = (drawable->old_w != width ||
339 drawable->old_h != height);
340
341 /* remove outdated textures */
342 if (resized) {
343 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
344 pipe_resource_reference(&drawable->textures[i], NULL);
345 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
346 }
347 }
348
349 memset(&templ, 0, sizeof(templ));
350 templ.target = screen->target;
351 templ.width0 = width;
352 templ.height0 = height;
353 templ.depth0 = 1;
354 templ.array_size = 1;
355 templ.last_level = 0;
356
357 for (i = 0; i < count; i++) {
358 enum pipe_format format;
359 unsigned bind;
360
361 /* the texture already exists or not requested */
362 if (drawable->textures[statts[i]])
363 continue;
364
365 dri_drawable_get_format(drawable, statts[i], &format, &bind);
366
367 /* if we don't do any present, no need for display targets */
368 if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !screen->swrast_no_present)
369 bind |= PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_LINEAR;
370
371 if (format == PIPE_FORMAT_NONE)
372 continue;
373
374 templ.format = format;
375 templ.bind = bind;
376 templ.nr_samples = 0;
377 templ.nr_storage_samples = 0;
378
379 if (statts[i] == ST_ATTACHMENT_FRONT_LEFT &&
380 screen->base.screen->resource_create_front &&
381 loader->base.version >= 3) {
382 drawable->textures[statts[i]] =
383 screen->base.screen->resource_create_front(screen->base.screen, &templ, (const void *)drawable);
384 } else
385 drawable->textures[statts[i]] =
386 screen->base.screen->resource_create(screen->base.screen, &templ);
387
388 if (drawable->stvis.samples > 1) {
389 templ.bind = templ.bind &
390 ~(PIPE_BIND_SCANOUT | PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET);
391 templ.nr_samples = drawable->stvis.samples;
392 templ.nr_storage_samples = drawable->stvis.samples;
393 drawable->msaa_textures[statts[i]] =
394 screen->base.screen->resource_create(screen->base.screen, &templ);
395
396 dri_pipe_blit(stctx->st->pipe,
397 drawable->msaa_textures[statts[i]],
398 drawable->textures[statts[i]]);
399 }
400 }
401
402 drawable->old_w = width;
403 drawable->old_h = height;
404 }
405
406 static void
407 drisw_update_tex_buffer(struct dri_drawable *drawable,
408 struct dri_context *ctx,
409 struct pipe_resource *res)
410 {
411 __DRIdrawable *dPriv = drawable->dPriv;
412
413 struct st_context *st_ctx = (struct st_context *)ctx->st;
414 struct pipe_context *pipe = st_ctx->pipe;
415 struct pipe_transfer *transfer;
416 char *map;
417 int x, y, w, h;
418 int ximage_stride, line;
419 int cpp = util_format_get_blocksize(res->format);
420
421 get_drawable_info(dPriv, &x, &y, &w, &h);
422
423 map = pipe_transfer_map(pipe, res,
424 0, 0, // level, layer,
425 PIPE_TRANSFER_WRITE,
426 x, y, w, h, &transfer);
427
428 /* Copy the Drawable content to the mapped texture buffer */
429 if (!get_image_shm(dPriv, x, y, w, h, res))
430 get_image(dPriv, x, y, w, h, map);
431
432 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
433 get_image() has a pitch rounded up to 4 bytes. */
434 ximage_stride = ((w * cpp) + 3) & -4;
435 for (line = h-1; line; --line) {
436 memmove(&map[line * transfer->stride],
437 &map[line * ximage_stride],
438 ximage_stride);
439 }
440
441 pipe_transfer_unmap(pipe, transfer);
442 }
443
444 static __DRIimageExtension driSWImageExtension = {
445 .base = { __DRI_IMAGE, 6 },
446
447 .createImageFromRenderbuffer = dri2_create_image_from_renderbuffer,
448 .createImageFromTexture = dri2_create_from_texture,
449 .destroyImage = dri2_destroy_image,
450 };
451
452 static const __DRIrobustnessExtension dri2Robustness = {
453 .base = { __DRI2_ROBUSTNESS, 1 }
454 };
455
456 /*
457 * Backend function for init_screen.
458 */
459
460 static const __DRIextension *drisw_screen_extensions[] = {
461 &driTexBufferExtension.base,
462 &dri2RendererQueryExtension.base,
463 &dri2ConfigQueryExtension.base,
464 &dri2FenceExtension.base,
465 &dri2NoErrorExtension.base,
466 &driSWImageExtension.base,
467 &dri2FlushControlExtension.base,
468 NULL
469 };
470
471 static const __DRIextension *drisw_robust_screen_extensions[] = {
472 &driTexBufferExtension.base,
473 &dri2RendererQueryExtension.base,
474 &dri2ConfigQueryExtension.base,
475 &dri2FenceExtension.base,
476 &dri2NoErrorExtension.base,
477 &dri2Robustness.base,
478 &driSWImageExtension.base,
479 &dri2FlushControlExtension.base,
480 NULL
481 };
482
483 static const struct drisw_loader_funcs drisw_lf = {
484 .get_image = drisw_get_image,
485 .put_image = drisw_put_image,
486 .put_image2 = drisw_put_image2
487 };
488
489 static const struct drisw_loader_funcs drisw_shm_lf = {
490 .get_image = drisw_get_image,
491 .put_image = drisw_put_image,
492 .put_image2 = drisw_put_image2,
493 .put_image_shm = drisw_put_image_shm
494 };
495
496 static const __DRIconfig **
497 drisw_init_screen(__DRIscreen * sPriv)
498 {
499 const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
500 const __DRIconfig **configs;
501 struct dri_screen *screen;
502 struct pipe_screen *pscreen = NULL;
503 const struct drisw_loader_funcs *lf = &drisw_lf;
504
505 screen = CALLOC_STRUCT(dri_screen);
506 if (!screen)
507 return NULL;
508
509 screen->sPriv = sPriv;
510 screen->fd = -1;
511
512 screen->swrast_no_present = debug_get_option_swrast_no_present();
513
514 sPriv->driverPrivate = (void *)screen;
515
516 if (loader->base.version >= 4) {
517 if (loader->putImageShm)
518 lf = &drisw_shm_lf;
519 }
520
521 if (pipe_loader_sw_probe_dri(&screen->dev, lf)) {
522 dri_init_options(screen);
523
524 pscreen = pipe_loader_create_screen(screen->dev);
525 }
526
527 if (!pscreen)
528 goto fail;
529
530 configs = dri_init_screen_helper(screen, pscreen);
531 if (!configs)
532 goto fail;
533
534 if (pscreen->get_param(pscreen, PIPE_CAP_DEVICE_RESET_STATUS_QUERY)) {
535 sPriv->extensions = drisw_robust_screen_extensions;
536 screen->has_reset_status_query = true;
537 }
538 else
539 sPriv->extensions = drisw_screen_extensions;
540 screen->lookup_egl_image = dri2_lookup_egl_image;
541
542 return configs;
543 fail:
544 dri_destroy_screen_helper(screen);
545 if (screen->dev)
546 pipe_loader_release(&screen->dev, 1);
547 FREE(screen);
548 return NULL;
549 }
550
551 static boolean
552 drisw_create_buffer(__DRIscreen * sPriv,
553 __DRIdrawable * dPriv,
554 const struct gl_config * visual, boolean isPixmap)
555 {
556 struct dri_drawable *drawable = NULL;
557
558 if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
559 return FALSE;
560
561 drawable = dPriv->driverPrivate;
562
563 drawable->allocate_textures = drisw_allocate_textures;
564 drawable->update_drawable_info = drisw_update_drawable_info;
565 drawable->flush_frontbuffer = drisw_flush_frontbuffer;
566 drawable->update_tex_buffer = drisw_update_tex_buffer;
567
568 return TRUE;
569 }
570
571 /**
572 * DRI driver virtual function table.
573 *
574 * DRI versions differ in their implementation of init_screen and swap_buffers.
575 */
576 const struct __DriverAPIRec galliumsw_driver_api = {
577 .InitScreen = drisw_init_screen,
578 .DestroyScreen = dri_destroy_screen,
579 .CreateContext = dri_create_context,
580 .DestroyContext = dri_destroy_context,
581 .CreateBuffer = drisw_create_buffer,
582 .DestroyBuffer = dri_destroy_buffer,
583 .SwapBuffers = drisw_swap_buffers,
584 .MakeCurrent = dri_make_current,
585 .UnbindContext = dri_unbind_context,
586 .CopySubBuffer = drisw_copy_sub_buffer,
587 };
588
589 /* This is the table of extensions that the loader will dlsym() for. */
590 const __DRIextension *galliumsw_driver_extensions[] = {
591 &driCoreExtension.base,
592 &driSWRastExtension.base,
593 &driCopySubBufferExtension.base,
594 &gallium_config_options.base,
595 NULL
596 };
597
598 /* vim: set sw=3 ts=8 sts=3 expandtab: */