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