xm_st.c revision 5d2fad5444ebe0e2a66c49bd6254e2bc81618f6e
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.9
4 *
5 * Copyright (C) 2010 LunarG Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 *    Chia-I Wu <olv@lunarg.com>
27 */
28
29#include "xm_api.h"
30#include "xm_st.h"
31
32#include "util/u_inlines.h"
33#include "util/u_atomic.h"
34
35struct xmesa_st_framebuffer {
36   XMesaDisplay display;
37   XMesaBuffer buffer;
38   struct pipe_screen *screen;
39
40   struct st_visual stvis;
41   enum pipe_texture_target target;
42
43   unsigned texture_width, texture_height, texture_mask;
44   struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
45
46   struct pipe_resource *display_resource;
47};
48
49static INLINE struct xmesa_st_framebuffer *
50xmesa_st_framebuffer(struct st_framebuffer_iface *stfbi)
51{
52   return (struct xmesa_st_framebuffer *) stfbi->st_manager_private;
53}
54
55/**
56 * Display an attachment to the xlib_drawable of the framebuffer.
57 */
58static boolean
59xmesa_st_framebuffer_display(struct st_framebuffer_iface *stfbi,
60                             enum st_attachment_type statt)
61{
62   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
63   struct pipe_resource *ptex = xstfb->textures[statt];
64   struct pipe_resource *pres;
65
66   if (!ptex)
67      return TRUE;
68
69   pres = xstfb->display_resource;
70   /* (re)allocate the surface for the texture to be displayed */
71   if (!pres || pres != ptex) {
72      pipe_resource_reference(&xstfb->display_resource, ptex);
73      pres = xstfb->display_resource;
74   }
75
76   xstfb->screen->flush_frontbuffer(xstfb->screen, pres, 0, 0, &xstfb->buffer->ws);
77
78   return TRUE;
79}
80
81/**
82 * Copy the contents between the attachments.
83 */
84static void
85xmesa_st_framebuffer_copy_textures(struct st_framebuffer_iface *stfbi,
86                                   enum st_attachment_type src_statt,
87                                   enum st_attachment_type dst_statt,
88                                   unsigned x, unsigned y,
89                                   unsigned width, unsigned height)
90{
91   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
92   struct pipe_resource *src_ptex = xstfb->textures[src_statt];
93   struct pipe_resource *dst_ptex = xstfb->textures[dst_statt];
94   struct pipe_box src_box;
95   struct pipe_context *pipe;
96
97   if (!src_ptex || !dst_ptex)
98      return;
99
100   pipe = xstfb->display->pipe;
101   if (!pipe) {
102      pipe = xstfb->screen->context_create(xstfb->screen, NULL);
103      if (!pipe)
104         return;
105      xstfb->display->pipe = pipe;
106   }
107
108   u_box_2d(x, y, width, height, &src_box);
109
110   if (src_ptex && dst_ptex)
111      pipe->resource_copy_region(pipe, dst_ptex, 0, x, y, 0,
112                                 src_ptex, 0, &src_box);
113}
114
115/**
116 * Remove outdated textures and create the requested ones.
117 * This is a helper used during framebuffer validation.
118 */
119static boolean
120xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface *stfbi,
121                                       unsigned width, unsigned height,
122                                       unsigned mask)
123{
124   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
125   struct pipe_resource templ;
126   enum st_attachment_type i;
127
128   /* remove outdated textures */
129   if (xstfb->texture_width != width || xstfb->texture_height != height) {
130      for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
131         pipe_resource_reference(&xstfb->textures[i], NULL);
132   }
133
134   memset(&templ, 0, sizeof(templ));
135   templ.target = xstfb->target;
136   templ.width0 = width;
137   templ.height0 = height;
138   templ.depth0 = 1;
139   templ.array_size = 1;
140   templ.last_level = 0;
141
142   for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
143      enum pipe_format format;
144      unsigned bind;
145
146      /* the texture already exists or not requested */
147      if (xstfb->textures[i] || !(mask & (1 << i))) {
148         /* remember the texture */
149         if (xstfb->textures[i])
150            mask |= (1 << i);
151         continue;
152      }
153
154      switch (i) {
155      case ST_ATTACHMENT_FRONT_LEFT:
156      case ST_ATTACHMENT_BACK_LEFT:
157      case ST_ATTACHMENT_FRONT_RIGHT:
158      case ST_ATTACHMENT_BACK_RIGHT:
159         format = xstfb->stvis.color_format;
160         bind = PIPE_BIND_DISPLAY_TARGET |
161                     PIPE_BIND_RENDER_TARGET;
162         break;
163      case ST_ATTACHMENT_DEPTH_STENCIL:
164         format = xstfb->stvis.depth_stencil_format;
165         bind = PIPE_BIND_DEPTH_STENCIL;
166         break;
167      default:
168         format = PIPE_FORMAT_NONE;
169         break;
170      }
171
172      if (format != PIPE_FORMAT_NONE) {
173         templ.format = format;
174         templ.bind = bind;
175
176         xstfb->textures[i] =
177            xstfb->screen->resource_create(xstfb->screen, &templ);
178         if (!xstfb->textures[i])
179            return FALSE;
180      }
181   }
182
183   xstfb->texture_width = width;
184   xstfb->texture_height = height;
185   xstfb->texture_mask = mask;
186
187   return TRUE;
188}
189
190
191/**
192 * Check that a framebuffer's attachments match the window's size.
193 *
194 * Called via st_framebuffer_iface::validate()
195 *
196 * \param statts  array of framebuffer attachments
197 * \param count  number of framebuffer attachments in statts[]
198 * \param out  returns resources for each of the attachments
199 */
200static boolean
201xmesa_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
202                              const enum st_attachment_type *statts,
203                              unsigned count,
204                              struct pipe_resource **out)
205{
206   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
207   unsigned statt_mask, new_mask, i;
208   boolean resized;
209   boolean ret;
210
211   /* build mask of ST_ATTACHMENT bits */
212   statt_mask = 0x0;
213   for (i = 0; i < count; i++)
214      statt_mask |= 1 << statts[i];
215
216   /* record newly allocated textures */
217   new_mask = statt_mask & ~xstfb->texture_mask;
218
219   /* If xmesa_strict_invalidate is not set, we will not yet have
220    * called XGetGeometry().  Do so here:
221    */
222   if (!xmesa_strict_invalidate)
223      xmesa_check_buffer_size(xstfb->buffer);
224
225   resized = (xstfb->buffer->width != xstfb->texture_width ||
226              xstfb->buffer->height != xstfb->texture_height);
227
228   /* revalidate textures */
229   if (resized || new_mask) {
230      ret = xmesa_st_framebuffer_validate_textures(stfbi,
231                  xstfb->buffer->width, xstfb->buffer->height, statt_mask);
232      if (!ret)
233         return ret;
234
235      if (!resized) {
236         enum st_attachment_type back, front;
237
238         back = ST_ATTACHMENT_BACK_LEFT;
239         front = ST_ATTACHMENT_FRONT_LEFT;
240         /* copy the contents if front is newly allocated and back is not */
241         if ((statt_mask & (1 << back)) &&
242             (new_mask & (1 << front)) &&
243             !(new_mask & (1 << back))) {
244            xmesa_st_framebuffer_copy_textures(stfbi, back, front,
245                  0, 0, xstfb->texture_width, xstfb->texture_height);
246         }
247      }
248   }
249
250   for (i = 0; i < count; i++) {
251      out[i] = NULL;
252      pipe_resource_reference(&out[i], xstfb->textures[statts[i]]);
253   }
254
255   return TRUE;
256}
257
258/**
259 * Called via st_framebuffer_iface::flush_front()
260 */
261static boolean
262xmesa_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
263                                 enum st_attachment_type statt)
264{
265   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
266   boolean ret;
267
268   ret = xmesa_st_framebuffer_display(stfbi, statt);
269
270   if (ret && xmesa_strict_invalidate)
271      xmesa_check_buffer_size(xstfb->buffer);
272
273   return ret;
274}
275
276struct st_framebuffer_iface *
277xmesa_create_st_framebuffer(XMesaDisplay xmdpy, XMesaBuffer b)
278{
279   struct st_framebuffer_iface *stfbi;
280   struct xmesa_st_framebuffer *xstfb;
281
282   assert(xmdpy->display == b->xm_visual->display);
283
284   stfbi = CALLOC_STRUCT(st_framebuffer_iface);
285   xstfb = CALLOC_STRUCT(xmesa_st_framebuffer);
286   if (!stfbi || !xstfb) {
287      if (stfbi)
288         FREE(stfbi);
289      if (xstfb)
290         FREE(xstfb);
291      return NULL;
292   }
293
294   xstfb->display = xmdpy;
295   xstfb->buffer = b;
296   xstfb->screen = xmdpy->screen;
297   xstfb->stvis = b->xm_visual->stvis;
298   if(xstfb->screen->get_param(xstfb->screen, PIPE_CAP_NPOT_TEXTURES))
299      xstfb->target = PIPE_TEXTURE_2D;
300   else
301      xstfb->target = PIPE_TEXTURE_RECT;
302
303   stfbi->visual = &xstfb->stvis;
304   stfbi->flush_front = xmesa_st_framebuffer_flush_front;
305   stfbi->validate = xmesa_st_framebuffer_validate;
306   p_atomic_set(&stfbi->stamp, 1);
307   stfbi->st_manager_private = (void *) xstfb;
308
309   return stfbi;
310}
311
312void
313xmesa_destroy_st_framebuffer(struct st_framebuffer_iface *stfbi)
314{
315   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
316   int i;
317
318   pipe_resource_reference(&xstfb->display_resource, NULL);
319
320   for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
321      pipe_resource_reference(&xstfb->textures[i], NULL);
322
323   FREE(xstfb);
324   FREE(stfbi);
325}
326
327void
328xmesa_swap_st_framebuffer(struct st_framebuffer_iface *stfbi)
329{
330   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
331   boolean ret;
332
333   ret = xmesa_st_framebuffer_display(stfbi, ST_ATTACHMENT_BACK_LEFT);
334   if (ret) {
335      struct pipe_resource **front, **back, *tmp;
336
337      front = &xstfb->textures[ST_ATTACHMENT_FRONT_LEFT];
338      back = &xstfb->textures[ST_ATTACHMENT_BACK_LEFT];
339      /* swap textures only if the front texture has been allocated */
340      if (*front) {
341         tmp = *front;
342         *front = *back;
343         *back = tmp;
344
345         /* the current context should validate the buffer after swapping */
346         if (!xmesa_strict_invalidate)
347            xmesa_notify_invalid_buffer(xstfb->buffer);
348      }
349
350      if (xmesa_strict_invalidate)
351	 xmesa_check_buffer_size(xstfb->buffer);
352   }
353}
354
355void
356xmesa_copy_st_framebuffer(struct st_framebuffer_iface *stfbi,
357                          enum st_attachment_type src,
358                          enum st_attachment_type dst,
359                          int x, int y, int w, int h)
360{
361   xmesa_st_framebuffer_copy_textures(stfbi, src, dst, x, y, w, h);
362   if (dst == ST_ATTACHMENT_FRONT_LEFT)
363      xmesa_st_framebuffer_display(stfbi, dst);
364}
365