drisw.c revision 421235d42ad9921fd45332ec7b33bcee5c1ad33d
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/**************************************************************************
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2009, VMware, Inc.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All Rights Reserved.
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "Software"), to deal in the Software without restriction, including
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish,
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * distribute, sub license, and/or sell copies of the Software, and to
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * permit persons to whom the Software is furnished to do so, subject to
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * the following conditions:
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) *
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * The above copyright notice and this permission notice (including the
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * next paragraph) shall be included in all copies or substantial portions
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * of the Software.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) **************************************************************************/
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* TODO:
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * xshm / texture_from_pixmap / EGLImage:
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Allow the loaders to use the XSHM extension. It probably requires callbacks
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * for createImage/destroyImage similar to DRI2 getBuffers.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_format.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_memory.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_inlines.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_context.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "state_tracker/drisw_api.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dri_screen.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dri_context.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dri_drawable.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static boolean swrast_no_present = FALSE;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static INLINE void
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)get_drawable_info(__DRIdrawable *dPriv, int *w, int *h)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   __DRIscreen *sPriv = dPriv->driScreenPriv;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   int x, y;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   loader->getDrawableInfo(dPriv,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           &x, &y, w, h,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           dPriv->loaderPrivate);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
62static INLINE void
63put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height)
64{
65   __DRIscreen *sPriv = dPriv->driScreenPriv;
66   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
67
68   loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
69                    0, 0, width, height,
70                    data, dPriv->loaderPrivate);
71}
72
73static void
74drisw_update_drawable_info(struct dri_drawable *drawable)
75{
76   __DRIdrawable *dPriv = drawable->dPriv;
77
78   get_drawable_info(dPriv, &dPriv->w, &dPriv->h);
79}
80
81static void
82drisw_put_image(struct dri_drawable *drawable,
83                void *data, unsigned width, unsigned height)
84{
85   __DRIdrawable *dPriv = drawable->dPriv;
86
87   put_image(dPriv, data, width, height);
88}
89
90static INLINE void
91drisw_present_texture(__DRIdrawable *dPriv,
92                      struct pipe_resource *ptex)
93{
94   struct dri_drawable *drawable = dri_drawable(dPriv);
95   struct dri_screen *screen = dri_screen(drawable->sPriv);
96
97   if (swrast_no_present)
98      return;
99
100   screen->base.screen->flush_frontbuffer(screen->base.screen, ptex, 0, 0, drawable);
101}
102
103static INLINE void
104drisw_invalidate_drawable(__DRIdrawable *dPriv)
105{
106   struct dri_drawable *drawable = dri_drawable(dPriv);
107
108   drawable->texture_stamp = dPriv->lastStamp - 1;
109
110   p_atomic_inc(&drawable->base.stamp);
111}
112
113static INLINE void
114drisw_copy_to_front(__DRIdrawable * dPriv,
115                    struct pipe_resource *ptex)
116{
117   drisw_present_texture(dPriv, ptex);
118
119   drisw_invalidate_drawable(dPriv);
120}
121
122/*
123 * Backend functions for st_framebuffer interface and swap_buffers.
124 */
125
126static void
127drisw_swap_buffers(__DRIdrawable *dPriv)
128{
129   struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
130   struct dri_drawable *drawable = dri_drawable(dPriv);
131   struct pipe_resource *ptex;
132
133   if (!ctx)
134      return;
135
136   ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
137
138   if (ptex) {
139      if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
140         pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
141
142      ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
143
144      drisw_copy_to_front(dPriv, ptex);
145   }
146}
147
148static void
149drisw_flush_frontbuffer(struct dri_drawable *drawable,
150                        enum st_attachment_type statt)
151{
152   struct dri_context *ctx = dri_get_current(drawable->sPriv);
153   struct pipe_resource *ptex;
154
155   if (!ctx)
156      return;
157
158   ptex = drawable->textures[statt];
159
160   if (ptex) {
161      drisw_copy_to_front(ctx->dPriv, ptex);
162   }
163}
164
165/**
166 * Allocate framebuffer attachments.
167 *
168 * During fixed-size operation, the function keeps allocating new attachments
169 * as they are requested. Unused attachments are not removed, not until the
170 * framebuffer is resized or destroyed.
171 */
172static void
173drisw_allocate_textures(struct dri_drawable *drawable,
174                        const enum st_attachment_type *statts,
175                        unsigned count)
176{
177   struct dri_screen *screen = dri_screen(drawable->sPriv);
178   struct pipe_resource templ;
179   unsigned width, height;
180   boolean resized;
181   unsigned i;
182
183   width  = drawable->dPriv->w;
184   height = drawable->dPriv->h;
185
186   resized = (drawable->old_w != width ||
187              drawable->old_h != height);
188
189   /* remove outdated textures */
190   if (resized) {
191      for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
192         pipe_resource_reference(&drawable->textures[i], NULL);
193   }
194
195   memset(&templ, 0, sizeof(templ));
196   templ.target = screen->target;
197   templ.width0 = width;
198   templ.height0 = height;
199   templ.depth0 = 1;
200   templ.array_size = 1;
201   templ.last_level = 0;
202
203   for (i = 0; i < count; i++) {
204      enum pipe_format format;
205      unsigned bind;
206
207      /* the texture already exists or not requested */
208      if (drawable->textures[statts[i]])
209         continue;
210
211      dri_drawable_get_format(drawable, statts[i], &format, &bind);
212
213      /* if we don't do any present, no need for display targets */
214      if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !swrast_no_present)
215         bind |= PIPE_BIND_DISPLAY_TARGET;
216
217      if (format == PIPE_FORMAT_NONE)
218         continue;
219
220      templ.format = format;
221      templ.bind = bind;
222
223      drawable->textures[statts[i]] =
224         screen->base.screen->resource_create(screen->base.screen, &templ);
225   }
226
227   drawable->old_w = width;
228   drawable->old_h = height;
229}
230
231/*
232 * Backend function for init_screen.
233 */
234
235static const __DRIextension *drisw_screen_extensions[] = {
236   &driTexBufferExtension.base,
237   NULL
238};
239
240static struct drisw_loader_funcs drisw_lf = {
241   .put_image = drisw_put_image
242};
243
244static const __DRIconfig **
245drisw_init_screen(__DRIscreen * sPriv)
246{
247   const __DRIconfig **configs;
248   struct dri_screen *screen;
249   struct pipe_screen *pscreen;
250
251   screen = CALLOC_STRUCT(dri_screen);
252   if (!screen)
253      return NULL;
254
255   screen->sPriv = sPriv;
256   screen->fd = -1;
257
258   swrast_no_present = debug_get_option_swrast_no_present();
259
260   sPriv->private = (void *)screen;
261   sPriv->extensions = drisw_screen_extensions;
262
263   pscreen = drisw_create_screen(&drisw_lf);
264   /* dri_init_screen_helper checks pscreen for us */
265
266   configs = dri_init_screen_helper(screen, pscreen, 32);
267   if (!configs)
268      goto fail;
269
270   return configs;
271fail:
272   dri_destroy_screen_helper(screen);
273   FREE(screen);
274   return NULL;
275}
276
277static boolean
278drisw_create_buffer(__DRIscreen * sPriv,
279                    __DRIdrawable * dPriv,
280                    const struct gl_config * visual, boolean isPixmap)
281{
282   struct dri_drawable *drawable = NULL;
283
284   if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
285      return FALSE;
286
287   drawable = dPriv->driverPrivate;
288
289   drawable->allocate_textures = drisw_allocate_textures;
290   drawable->update_drawable_info = drisw_update_drawable_info;
291   drawable->flush_frontbuffer = drisw_flush_frontbuffer;
292
293   return TRUE;
294}
295
296/**
297 * DRI driver virtual function table.
298 *
299 * DRI versions differ in their implementation of init_screen and swap_buffers.
300 */
301const struct __DriverAPIRec driDriverAPI = {
302   .InitScreen = drisw_init_screen,
303   .DestroyScreen = dri_destroy_screen,
304   .CreateContext = dri_create_context,
305   .DestroyContext = dri_destroy_context,
306   .CreateBuffer = drisw_create_buffer,
307   .DestroyBuffer = dri_destroy_buffer,
308   .MakeCurrent = dri_make_current,
309   .UnbindContext = dri_unbind_context,
310
311   .SwapBuffers = drisw_swap_buffers,
312};
313
314/* This is the table of extensions that the loader will dlsym() for. */
315PUBLIC const __DRIextension *__driDriverExtensions[] = {
316    &driCoreExtension.base,
317    &driSWRastExtension.base,
318    NULL
319};
320
321/* vim: set sw=3 ts=8 sts=3 expandtab: */
322