xm_st.c revision d500ad440503c79656f5610051dcb0ff76bafc58
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
34struct xmesa_st_framebuffer {
35   XMesaDisplay display;
36   XMesaBuffer buffer;
37   struct pipe_screen *screen;
38
39   struct st_visual stvis;
40   enum pipe_texture_target target;
41
42   unsigned texture_width, texture_height, texture_mask;
43   struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
44
45   struct pipe_surface *display_surface;
46};
47
48static INLINE struct xmesa_st_framebuffer *
49xmesa_st_framebuffer(struct st_framebuffer_iface *stfbi)
50{
51   return (struct xmesa_st_framebuffer *) stfbi->st_manager_private;
52}
53
54/**
55 * Display an attachment to the xlib_drawable of the framebuffer.
56 */
57static boolean
58xmesa_st_framebuffer_display(struct st_framebuffer_iface *stfbi,
59                             enum st_attachment_type statt)
60{
61   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
62   struct pipe_resource *ptex = xstfb->textures[statt];
63   struct pipe_surface *psurf;
64
65   if (!ptex)
66      return TRUE;
67
68   psurf = xstfb->display_surface;
69   /* (re)allocate the surface for the texture to be displayed */
70   if (!psurf || psurf->texture != ptex) {
71      pipe_surface_reference(&xstfb->display_surface, NULL);
72
73      psurf = xstfb->screen->get_tex_surface(xstfb->screen,
74            ptex, 0, 0, 0, PIPE_BIND_DISPLAY_TARGET);
75      if (!psurf)
76         return FALSE;
77
78      xstfb->display_surface = psurf;
79   }
80
81   xstfb->screen->flush_frontbuffer(xstfb->screen, psurf, &xstfb->buffer->ws);
82
83   return TRUE;
84}
85
86/**
87 * Copy the contents between the attachments.
88 */
89static void
90xmesa_st_framebuffer_copy_textures(struct st_framebuffer_iface *stfbi,
91                                   enum st_attachment_type src_statt,
92                                   enum st_attachment_type dst_statt,
93                                   unsigned x, unsigned y,
94                                   unsigned width, unsigned height)
95{
96   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
97   struct pipe_resource *src_ptex = xstfb->textures[src_statt];
98   struct pipe_resource *dst_ptex = xstfb->textures[dst_statt];
99   struct pipe_subresource subsrc, subdst;
100   struct pipe_context *pipe;
101
102   if (!src_ptex || !dst_ptex)
103      return;
104
105   pipe = xstfb->display->pipe;
106   if (!pipe) {
107      pipe = xstfb->screen->context_create(xstfb->screen, NULL);
108      if (!pipe)
109         return;
110      xstfb->display->pipe = pipe;
111   }
112
113   subsrc.face = 0;
114   subsrc.level = 0;
115   subdst.face = 0;
116   subdst.level = 0;
117
118   if (src_ptex && dst_ptex)
119      pipe->resource_copy_region(pipe, dst_ptex, subdst, x, y, 0,
120                                 src_ptex, subsrc, x, y, 0, width, height);
121}
122
123/**
124 * Remove outdated textures and create the requested ones.
125 * This is a helper used during framebuffer validation.
126 */
127static boolean
128xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface *stfbi,
129                                       unsigned width, unsigned height,
130                                       unsigned mask)
131{
132   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
133   struct pipe_resource templ;
134   enum st_attachment_type i;
135
136   /* remove outdated textures */
137   if (xstfb->texture_width != width || xstfb->texture_height != height) {
138      for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
139         pipe_resource_reference(&xstfb->textures[i], NULL);
140   }
141
142   memset(&templ, 0, sizeof(templ));
143   templ.target = xstfb->target;
144   templ.width0 = width;
145   templ.height0 = height;
146   templ.depth0 = 1;
147   templ.last_level = 0;
148
149   for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
150      enum pipe_format format;
151      unsigned bind;
152
153      /* the texture already exists or not requested */
154      if (xstfb->textures[i] || !(mask & (1 << i))) {
155         /* remember the texture */
156         if (xstfb->textures[i])
157            mask |= (1 << i);
158         continue;
159      }
160
161      switch (i) {
162      case ST_ATTACHMENT_FRONT_LEFT:
163      case ST_ATTACHMENT_BACK_LEFT:
164      case ST_ATTACHMENT_FRONT_RIGHT:
165      case ST_ATTACHMENT_BACK_RIGHT:
166         format = xstfb->stvis.color_format;
167         bind = PIPE_BIND_DISPLAY_TARGET |
168                     PIPE_BIND_RENDER_TARGET;
169         break;
170      case ST_ATTACHMENT_DEPTH_STENCIL:
171         format = xstfb->stvis.depth_stencil_format;
172         bind = PIPE_BIND_DEPTH_STENCIL;
173         break;
174      default:
175         format = PIPE_FORMAT_NONE;
176         break;
177      }
178
179      if (format != PIPE_FORMAT_NONE) {
180         templ.format = format;
181         templ.bind = bind;
182
183         xstfb->textures[i] =
184            xstfb->screen->resource_create(xstfb->screen, &templ);
185         if (!xstfb->textures[i])
186            return FALSE;
187      }
188   }
189
190   xstfb->texture_width = width;
191   xstfb->texture_height = height;
192   xstfb->texture_mask = mask;
193
194   return TRUE;
195}
196
197
198/**
199 * Called via st_framebuffer_iface::validate()
200 */
201static boolean
202xmesa_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
203                              const enum st_attachment_type *statts,
204                              unsigned count,
205                              struct pipe_resource **out)
206{
207   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
208   unsigned statt_mask, new_mask, i;
209   boolean resized;
210   boolean ret;
211
212   statt_mask = 0x0;
213   for (i = 0; i < count; i++)
214      statt_mask |= 1 << statts[i];
215   /* record newly allocated textures */
216   new_mask = statt_mask & ~xstfb->texture_mask;
217
218   /* If xmesa_strict_invalidate is not set, we will not yet have
219    * called XGetGeometry().  Do so here:
220    */
221   if (!xmesa_strict_invalidate)
222      xmesa_check_buffer_size(xstfb->buffer);
223
224   resized = (xstfb->buffer->width != xstfb->texture_width ||
225              xstfb->buffer->height != xstfb->texture_height);
226
227   /* revalidate textures */
228   if (resized || new_mask) {
229      ret = xmesa_st_framebuffer_validate_textures(stfbi,
230                  xstfb->buffer->width, xstfb->buffer->height, statt_mask);
231      if (!ret)
232         return ret;
233
234      if (!resized) {
235         enum st_attachment_type back, front;
236
237         back = ST_ATTACHMENT_BACK_LEFT;
238         front = ST_ATTACHMENT_FRONT_LEFT;
239         /* copy the contents if front is newly allocated and back is not */
240         if ((statt_mask & (1 << back)) &&
241             (new_mask & (1 << front)) &&
242             !(new_mask & (1 << back))) {
243            xmesa_st_framebuffer_copy_textures(stfbi, back, front,
244                  0, 0, xstfb->texture_width, xstfb->texture_height);
245         }
246      }
247   }
248
249   for (i = 0; i < count; i++) {
250      out[i] = NULL;
251      pipe_resource_reference(&out[i], xstfb->textures[statts[i]]);
252   }
253
254   return TRUE;
255}
256
257/**
258 * Called via st_framebuffer_iface::flush_front()
259 */
260static boolean
261xmesa_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
262                                 enum st_attachment_type statt)
263{
264   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
265   boolean ret;
266
267   ret = xmesa_st_framebuffer_display(stfbi, statt);
268
269   if (ret && xmesa_strict_invalidate)
270      xmesa_check_buffer_size(xstfb->buffer);
271
272   return ret;
273}
274
275struct st_framebuffer_iface *
276xmesa_create_st_framebuffer(XMesaDisplay xmdpy, XMesaBuffer b)
277{
278   struct st_framebuffer_iface *stfbi;
279   struct xmesa_st_framebuffer *xstfb;
280
281   assert(xmdpy->display == b->xm_visual->display);
282
283   stfbi = CALLOC_STRUCT(st_framebuffer_iface);
284   xstfb = CALLOC_STRUCT(xmesa_st_framebuffer);
285   if (!stfbi || !xstfb) {
286      if (stfbi)
287         FREE(stfbi);
288      if (xstfb)
289         FREE(xstfb);
290      return NULL;
291   }
292
293   xstfb->display = xmdpy;
294   xstfb->buffer = b;
295   xstfb->screen = xmdpy->screen;
296   xstfb->stvis = b->xm_visual->stvis;
297   if(xstfb->screen->get_param(xstfb->screen, PIPE_CAP_NPOT_TEXTURES))
298      xstfb->target = PIPE_TEXTURE_2D;
299   else
300      xstfb->target = PIPE_TEXTURE_RECT;
301
302   stfbi->visual = &xstfb->stvis;
303   stfbi->flush_front = xmesa_st_framebuffer_flush_front;
304   stfbi->validate = xmesa_st_framebuffer_validate;
305   stfbi->st_manager_private = (void *) xstfb;
306
307   return stfbi;
308}
309
310void
311xmesa_destroy_st_framebuffer(struct st_framebuffer_iface *stfbi)
312{
313   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
314   int i;
315
316   pipe_surface_reference(&xstfb->display_surface, NULL);
317
318   for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
319      pipe_resource_reference(&xstfb->textures[i], NULL);
320
321   FREE(xstfb);
322   FREE(stfbi);
323}
324
325void
326xmesa_swap_st_framebuffer(struct st_framebuffer_iface *stfbi)
327{
328   struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi);
329   boolean ret;
330
331   ret = xmesa_st_framebuffer_display(stfbi, ST_ATTACHMENT_BACK_LEFT);
332   if (ret) {
333      struct pipe_resource **front, **back, *tmp;
334
335      front = &xstfb->textures[ST_ATTACHMENT_FRONT_LEFT];
336      back = &xstfb->textures[ST_ATTACHMENT_BACK_LEFT];
337      /* swap textures only if the front texture has been allocated */
338      if (*front) {
339         tmp = *front;
340         *front = *back;
341         *back = tmp;
342      }
343
344      if (xmesa_strict_invalidate)
345	 xmesa_check_buffer_size(xstfb->buffer);
346   }
347}
348
349void
350xmesa_copy_st_framebuffer(struct st_framebuffer_iface *stfbi,
351                          enum st_attachment_type src,
352                          enum st_attachment_type dst,
353                          int x, int y, int w, int h)
354{
355   xmesa_st_framebuffer_copy_textures(stfbi, src, dst, x, y, w, h);
356   if (dst == ST_ATTACHMENT_FRONT_LEFT)
357      xmesa_st_framebuffer_display(stfbi, dst);
358}
359