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