drisw.c revision 2e4ad14a525f4028f0e0a93de2f8db785df33fb7
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 / texture_from_pixmap / 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 "pipe/p_context.h"
41#include "state_tracker/drisw_api.h"
42
43#include "dri_screen.h"
44#include "dri_context.h"
45#include "dri_drawable.h"
46#include "dri1_helper.h"
47
48DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE);
49static boolean swrast_no_present = FALSE;
50
51static INLINE void
52get_drawable_info(__DRIdrawable *dPriv, int *w, int *h)
53{
54   __DRIscreen *sPriv = dPriv->driScreenPriv;
55   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
56   int x, y;
57
58   loader->getDrawableInfo(dPriv,
59                           &x, &y, w, h,
60                           dPriv->loaderPrivate);
61}
62
63static INLINE void
64put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height)
65{
66   __DRIscreen *sPriv = dPriv->driScreenPriv;
67   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
68
69   loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
70                    0, 0, width, height,
71                    data, dPriv->loaderPrivate);
72}
73
74static void
75drisw_update_drawable_info(struct dri_drawable *drawable)
76{
77   __DRIdrawable *dPriv = drawable->dPriv;
78
79   get_drawable_info(dPriv, &dPriv->w, &dPriv->h);
80}
81
82static void
83drisw_put_image(struct dri_drawable *drawable,
84                void *data, unsigned width, unsigned height)
85{
86   __DRIdrawable *dPriv = drawable->dPriv;
87
88   put_image(dPriv, data, width, height);
89}
90
91static INLINE void
92drisw_present_texture(__DRIdrawable *dPriv,
93                      struct pipe_resource *ptex)
94{
95   struct dri_drawable *drawable = dri_drawable(dPriv);
96   struct dri_screen *screen = dri_screen(drawable->sPriv);
97   struct pipe_surface *psurf;
98
99   if (swrast_no_present)
100      return;
101
102   psurf = dri1_get_pipe_surface(drawable, ptex);
103   if (!psurf)
104      return;
105
106   screen->base.screen->flush_frontbuffer(screen->base.screen, psurf, drawable);
107}
108
109static INLINE void
110drisw_invalidate_drawable(__DRIdrawable *dPriv)
111{
112   struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
113   struct dri_drawable *drawable = dri_drawable(dPriv);
114
115   drawable->texture_stamp = dPriv->lastStamp - 1;
116
117   /* check if swapping currently bound buffer */
118   if (ctx && ctx->dPriv == dPriv)
119      ctx->st->notify_invalid_framebuffer(ctx->st, &drawable->base);
120}
121
122static INLINE void
123drisw_copy_to_front(__DRIdrawable * dPriv,
124                    struct pipe_resource *ptex)
125{
126   drisw_present_texture(dPriv, ptex);
127
128   drisw_invalidate_drawable(dPriv);
129}
130
131/*
132 * Backend functions for st_framebuffer interface and swap_buffers.
133 */
134
135static void
136drisw_swap_buffers(__DRIdrawable *dPriv)
137{
138   struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
139   struct dri_drawable *drawable = dri_drawable(dPriv);
140   struct pipe_resource *ptex;
141
142   if (!ctx)
143      return;
144
145   ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
146
147   if (ptex) {
148      ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
149
150      drisw_copy_to_front(dPriv, ptex);
151   }
152}
153
154static void
155drisw_flush_frontbuffer(struct dri_drawable *drawable,
156                        enum st_attachment_type statt)
157{
158   struct dri_context *ctx = dri_get_current(drawable->sPriv);
159   struct pipe_resource *ptex;
160
161   if (!ctx)
162      return;
163
164   ptex = drawable->textures[statt];
165
166   if (ptex) {
167      drisw_copy_to_front(ctx->dPriv, ptex);
168   }
169}
170
171/**
172 * Allocate framebuffer attachments.
173 *
174 * During fixed-size operation, the function keeps allocating new attachments
175 * as they are requested. Unused attachments are not removed, not until the
176 * framebuffer is resized or destroyed.
177 *
178 * It should be possible for DRI1 and DRISW to share this function, but it
179 * seems a better seperation and safer for each DRI version to provide its own
180 * function.
181 */
182static void
183drisw_allocate_textures(struct dri_drawable *drawable,
184                        const enum st_attachment_type *statts,
185                        unsigned count)
186{
187   struct dri_screen *screen = dri_screen(drawable->sPriv);
188   struct pipe_resource templ;
189   unsigned width, height;
190   boolean resized;
191   int i;
192
193   width  = drawable->dPriv->w;
194   height = drawable->dPriv->h;
195
196   resized = (drawable->old_w != width ||
197              drawable->old_h != height);
198
199   /* remove outdated textures */
200   if (resized) {
201      for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
202         pipe_resource_reference(&drawable->textures[i], NULL);
203   }
204
205   memset(&templ, 0, sizeof(templ));
206   templ.target = PIPE_TEXTURE_2D;
207   templ.width0 = width;
208   templ.height0 = height;
209   templ.depth0 = 1;
210   templ.last_level = 0;
211
212   for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
213      enum pipe_format format;
214      unsigned bind;
215
216      /* the texture already exists or not requested */
217      if (drawable->textures[statts[i]])
218         continue;
219
220      dri_drawable_get_format(drawable, statts[i], &format, &bind);
221
222      /* if we don't do any present, no need for display targets */
223      if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !swrast_no_present)
224         bind |= PIPE_BIND_DISPLAY_TARGET;
225
226      if (format == PIPE_FORMAT_NONE)
227         continue;
228
229      templ.format = format;
230      templ.bind = bind;
231
232      drawable->textures[statts[i]] =
233         screen->base.screen->resource_create(screen->base.screen, &templ);
234   }
235
236   drawable->old_w = width;
237   drawable->old_h = height;
238}
239
240/*
241 * Backend function for init_screen.
242 */
243
244static const __DRIextension *drisw_screen_extensions[] = {
245   NULL
246};
247
248static struct drisw_loader_funcs drisw_lf = {
249   .put_image = drisw_put_image
250};
251
252static const __DRIconfig **
253drisw_init_screen(__DRIscreen * sPriv)
254{
255   const __DRIconfig **configs;
256   struct dri_screen *screen;
257   struct pipe_screen *pscreen;
258
259   screen = CALLOC_STRUCT(dri_screen);
260   if (!screen)
261      return NULL;
262
263   screen->sPriv = sPriv;
264   screen->fd = -1;
265   screen->allocate_textures = drisw_allocate_textures;
266   screen->update_drawable_info = drisw_update_drawable_info;
267   screen->flush_frontbuffer = drisw_flush_frontbuffer;
268
269   swrast_no_present = debug_get_option_swrast_no_present();
270
271   sPriv->private = (void *)screen;
272   sPriv->extensions = drisw_screen_extensions;
273
274   pscreen = drisw_create_screen(&drisw_lf);
275   /* dri_init_screen_helper checks pscreen for us */
276
277   configs = dri_init_screen_helper(screen, pscreen, 32);
278   if (!configs)
279      goto fail;
280
281   return configs;
282fail:
283   dri_destroy_screen_helper(screen);
284   FREE(screen);
285   return NULL;
286}
287
288/**
289 * DRI driver virtual function table.
290 *
291 * DRI versions differ in their implementation of init_screen and swap_buffers.
292 */
293const struct __DriverAPIRec driDriverAPI = {
294   .DestroyScreen = dri_destroy_screen,
295   .CreateContext = dri_create_context,
296   .DestroyContext = dri_destroy_context,
297   .CreateBuffer = dri_create_buffer,
298   .DestroyBuffer = dri_destroy_buffer,
299   .MakeCurrent = dri_make_current,
300   .UnbindContext = dri_unbind_context,
301
302   .InitScreen = drisw_init_screen,
303   .SwapBuffers = drisw_swap_buffers,
304};
305
306/* This is the table of extensions that the loader will dlsym() for. */
307PUBLIC const __DRIextension *__driDriverExtensions[] = {
308    &driCoreExtension.base,
309    &driSWRastExtension.base,
310    NULL
311};
312
313/* vim: set sw=3 ts=8 sts=3 expandtab: */
314