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