OSDN Git Service

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