2cdfd257b011fc24a33e8e5dbe855b4a50e79be7
[mesa.git] / src / gallium / state_trackers / dri / dri_st_api.c
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
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "util/u_memory.h"
29 #include "util/u_inlines.h"
30 #include "util/u_format.h"
31 #include "util/u_debug.h"
32 #include "state_tracker/drm_api.h"
33 #include "state_tracker/st_manager.h" /* for st_manager_create_api */
34
35 #include "dri_screen.h"
36 #include "dri_context.h"
37 #include "dri_drawable.h"
38 #include "dri_st_api.h"
39 #include "dri1.h"
40
41 static struct {
42 int32_t refcnt;
43 struct st_api *stapi;
44 } dri_st_api;
45
46 /**
47 * Get the format of an attachment.
48 */
49 static INLINE enum pipe_format
50 dri_drawable_get_format(struct dri_drawable *drawable,
51 enum st_attachment_type statt)
52 {
53 enum pipe_format format;
54
55 switch (statt) {
56 case ST_ATTACHMENT_FRONT_LEFT:
57 case ST_ATTACHMENT_BACK_LEFT:
58 case ST_ATTACHMENT_FRONT_RIGHT:
59 case ST_ATTACHMENT_BACK_RIGHT:
60 format = drawable->stvis.color_format;
61 break;
62 case ST_ATTACHMENT_DEPTH_STENCIL:
63 format = drawable->stvis.depth_stencil_format;
64 break;
65 default:
66 format = PIPE_FORMAT_NONE;
67 break;
68 }
69
70 return format;
71 }
72
73 /**
74 * Process __DRIbuffer and convert them into pipe_textures.
75 */
76 static void
77 dri_drawable_process_buffers(struct dri_drawable *drawable,
78 __DRIbuffer *buffers, unsigned count)
79 {
80 struct dri_screen *screen = dri_screen(drawable->sPriv);
81 __DRIdrawable *dri_drawable = drawable->dPriv;
82 struct pipe_texture templ;
83 struct winsys_handle whandle;
84 boolean have_depth = FALSE;
85 unsigned i;
86
87 if (drawable->old_num == count &&
88 drawable->old_w == dri_drawable->w &&
89 drawable->old_h == dri_drawable->h &&
90 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0)
91 return;
92
93 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
94 pipe_texture_reference(&drawable->textures[i], NULL);
95
96 memset(&templ, 0, sizeof(templ));
97 templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
98 templ.target = PIPE_TEXTURE_2D;
99 templ.last_level = 0;
100 templ.width0 = dri_drawable->w;
101 templ.height0 = dri_drawable->h;
102 templ.depth0 = 1;
103
104 memset(&whandle, 0, sizeof(whandle));
105
106 for (i = 0; i < count; i++) {
107 __DRIbuffer *buf = &buffers[i];
108 enum st_attachment_type statt;
109 enum pipe_format format;
110
111 switch (buf->attachment) {
112 case __DRI_BUFFER_FRONT_LEFT:
113 if (!screen->auto_fake_front) {
114 statt = ST_ATTACHMENT_INVALID;
115 break;
116 }
117 /* fallthrough */
118 case __DRI_BUFFER_FAKE_FRONT_LEFT:
119 statt = ST_ATTACHMENT_FRONT_LEFT;
120 break;
121 case __DRI_BUFFER_BACK_LEFT:
122 statt = ST_ATTACHMENT_BACK_LEFT;
123 break;
124 case __DRI_BUFFER_DEPTH:
125 case __DRI_BUFFER_DEPTH_STENCIL:
126 case __DRI_BUFFER_STENCIL:
127 /* use only the first depth/stencil buffer */
128 if (!have_depth) {
129 have_depth = TRUE;
130 statt = ST_ATTACHMENT_DEPTH_STENCIL;
131 }
132 else {
133 statt = ST_ATTACHMENT_INVALID;
134 }
135 break;
136 default:
137 statt = ST_ATTACHMENT_INVALID;
138 break;
139 }
140
141 format = dri_drawable_get_format(drawable, statt);
142 if (statt == ST_ATTACHMENT_INVALID || format == PIPE_FORMAT_NONE)
143 continue;
144
145 templ.format = format;
146 whandle.handle = buf->name;
147 whandle.stride = buf->pitch;
148
149 drawable->textures[statt] =
150 screen->pipe_screen->texture_from_handle(screen->pipe_screen,
151 &templ, &whandle);
152 }
153
154 drawable->old_num = count;
155 drawable->old_w = dri_drawable->w;
156 drawable->old_h = dri_drawable->h;
157 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
158 }
159
160 /**
161 * Retrieve __DRIbuffer from the DRI loader.
162 */
163 static __DRIbuffer *
164 dri_drawable_get_buffers(struct dri_drawable *drawable,
165 const enum st_attachment_type *statts,
166 unsigned *count)
167 {
168 __DRIdrawable *dri_drawable = drawable->dPriv;
169 struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
170 boolean with_format;
171 __DRIbuffer *buffers;
172 int num_buffers;
173 unsigned attachments[10];
174 unsigned num_attachments, i;
175
176 assert(loader);
177 with_format = dri_with_format(drawable->sPriv);
178
179 num_attachments = 0;
180
181 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
182 if (!with_format)
183 attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
184
185 for (i = 0; i < *count; i++) {
186 enum pipe_format format;
187 int att, bpp;
188
189 format = dri_drawable_get_format(drawable, statts[i]);
190 if (format == PIPE_FORMAT_NONE)
191 continue;
192
193 switch (statts[i]) {
194 case ST_ATTACHMENT_FRONT_LEFT:
195 /* already added */
196 if (!with_format)
197 continue;
198 att = __DRI_BUFFER_FRONT_LEFT;
199 break;
200 case ST_ATTACHMENT_BACK_LEFT:
201 att = __DRI_BUFFER_BACK_LEFT;
202 break;
203 case ST_ATTACHMENT_FRONT_RIGHT:
204 att = __DRI_BUFFER_FRONT_RIGHT;
205 break;
206 case ST_ATTACHMENT_BACK_RIGHT:
207 att = __DRI_BUFFER_BACK_RIGHT;
208 break;
209 case ST_ATTACHMENT_DEPTH_STENCIL:
210 att = __DRI_BUFFER_DEPTH_STENCIL;
211 break;
212 default:
213 att = -1;
214 break;
215 }
216
217 bpp = util_format_get_blocksizebits(format);
218
219 if (att >= 0) {
220 attachments[num_attachments++] = att;
221 if (with_format) {
222 attachments[num_attachments++] = bpp;
223 }
224 }
225 }
226
227 if (with_format) {
228 num_attachments /= 2;
229 buffers = loader->getBuffersWithFormat(dri_drawable,
230 &dri_drawable->w, &dri_drawable->h,
231 attachments, num_attachments,
232 &num_buffers, dri_drawable->loaderPrivate);
233 }
234 else {
235 buffers = loader->getBuffers(dri_drawable,
236 &dri_drawable->w, &dri_drawable->h,
237 attachments, num_attachments,
238 &num_buffers, dri_drawable->loaderPrivate);
239 }
240
241 if (buffers) {
242 /* set one cliprect to cover the whole dri_drawable */
243 dri_drawable->x = 0;
244 dri_drawable->y = 0;
245 dri_drawable->backX = 0;
246 dri_drawable->backY = 0;
247 dri_drawable->numClipRects = 1;
248 dri_drawable->pClipRects[0].x1 = 0;
249 dri_drawable->pClipRects[0].y1 = 0;
250 dri_drawable->pClipRects[0].x2 = dri_drawable->w;
251 dri_drawable->pClipRects[0].y2 = dri_drawable->h;
252 dri_drawable->numBackClipRects = 1;
253 dri_drawable->pBackClipRects[0].x1 = 0;
254 dri_drawable->pBackClipRects[0].y1 = 0;
255 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
256 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
257
258 *count = num_buffers;
259 }
260
261 return buffers;
262 }
263
264 static boolean
265 dri_st_framebuffer_validate(struct st_framebuffer_iface *stfbi,
266 const enum st_attachment_type *statts,
267 unsigned count,
268 struct pipe_texture **out)
269 {
270 struct dri_drawable *drawable =
271 (struct dri_drawable *) stfbi->st_manager_private;
272 unsigned statt_mask, i;
273
274 statt_mask = 0x0;
275 for (i = 0; i < count; i++)
276 statt_mask |= (1 << statts[i]);
277
278 /*
279 * dPriv->pStamp is the server stamp. It should be accessed with a lock, at
280 * least for DRI1. dPriv->lastStamp is the client stamp. It has the value
281 * of the server stamp when last checked.
282 *
283 * This function updates the textures and records the stamp of the textures.
284 */
285 if (drawable->texture_stamp != drawable->dPriv->lastStamp ||
286 (statt_mask & ~drawable->texture_mask)) {
287 if (__dri1_api_hooks) {
288 dri1_allocate_textures(drawable, statt_mask);
289 }
290 else {
291 __DRIbuffer *buffers;
292 unsigned num_buffers = count;
293
294 buffers = dri_drawable_get_buffers(drawable, statts, &num_buffers);
295 dri_drawable_process_buffers(drawable, buffers, num_buffers);
296 }
297
298 /* add existing textures */
299 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
300 if (drawable->textures[i])
301 statt_mask |= (1 << i);
302 }
303
304 drawable->texture_stamp = drawable->dPriv->lastStamp;
305 drawable->texture_mask = statt_mask;
306 }
307
308 if (!out)
309 return TRUE;
310
311 for (i = 0; i < count; i++) {
312 out[i] = NULL;
313 pipe_texture_reference(&out[i], drawable->textures[statts[i]]);
314 }
315
316 return TRUE;
317 }
318
319 static boolean
320 dri_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi,
321 enum st_attachment_type statt)
322 {
323 struct dri_drawable *drawable =
324 (struct dri_drawable *) stfbi->st_manager_private;
325 struct __DRIdri2LoaderExtensionRec *loader =
326 drawable->sPriv->dri2.loader;
327
328 if (__dri1_api_hooks) {
329 dri1_flush_frontbuffer(drawable, statt);
330 return TRUE;
331 }
332
333 if (statt == ST_ATTACHMENT_FRONT_LEFT && loader->flushFrontBuffer) {
334 loader->flushFrontBuffer(drawable->dPriv,
335 drawable->dPriv->loaderPrivate);
336 }
337
338 return TRUE;
339 }
340
341 /**
342 * Create a framebuffer from the given drawable.
343 */
344 struct st_framebuffer_iface *
345 dri_create_st_framebuffer(struct dri_drawable *drawable)
346 {
347 struct st_framebuffer_iface *stfbi;
348
349 stfbi = CALLOC_STRUCT(st_framebuffer_iface);
350 if (stfbi) {
351 stfbi->visual = &drawable->stvis;
352 stfbi->flush_front = dri_st_framebuffer_flush_front;
353 stfbi->validate = dri_st_framebuffer_validate;
354 stfbi->st_manager_private = (void *) drawable;
355 }
356
357 return stfbi;
358 }
359
360 /**
361 * Destroy a framebuffer.
362 */
363 void
364 dri_destroy_st_framebuffer(struct st_framebuffer_iface *stfbi)
365 {
366 FREE(stfbi);
367 }
368
369 /**
370 * Return the texture at an attachment. Allocate the texture if it does not
371 * exist.
372 */
373 struct pipe_texture *
374 dri_get_st_framebuffer_texture(struct st_framebuffer_iface *stfbi,
375 enum st_attachment_type statt)
376 {
377 struct dri_drawable *drawable =
378 (struct dri_drawable *) stfbi->st_manager_private;
379
380 if (!(drawable->texture_mask & (1 << statt))) {
381 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
382 unsigned i, count = 0;
383
384 /* make sure DRI2 does not destroy existing buffers */
385 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
386 if (drawable->texture_mask & (1 << i)) {
387 statts[count++] = i;
388 }
389 }
390 statts[count++] = statt;
391
392 drawable->texture_stamp = drawable->dPriv->lastStamp - 1;
393 dri_st_framebuffer_validate(stfbi, statts, count, NULL);
394 }
395
396 return drawable->textures[statt];
397 }
398
399 /**
400 * Add a reference to the st_api of the state tracker.
401 */
402 static void
403 _dri_get_st_api(void)
404 {
405 p_atomic_inc(&dri_st_api.refcnt);
406 if (p_atomic_read(&dri_st_api.refcnt) == 1)
407 dri_st_api.stapi = st_manager_create_api();
408 }
409
410 /**
411 * Remove a reference to the st_api of the state tracker.
412 */
413 static void
414 _dri_put_st_api(void)
415 {
416 struct st_api *stapi = dri_st_api.stapi;
417
418 if (p_atomic_dec_zero(&dri_st_api.refcnt)) {
419 stapi->destroy(dri_st_api.stapi);
420 dri_st_api.stapi = NULL;
421 }
422 }
423
424 /**
425 * Create a state tracker manager from the given screen.
426 */
427 struct st_manager *
428 dri_create_st_manager(struct dri_screen *screen)
429 {
430 struct st_manager *smapi;
431
432 smapi = CALLOC_STRUCT(st_manager);
433 if (smapi) {
434 smapi->screen = screen->pipe_screen;
435 _dri_get_st_api();
436 }
437
438 return smapi;
439 }
440
441 /**
442 * Destroy a state tracker manager.
443 */
444 void
445 dri_destroy_st_manager(struct st_manager *smapi)
446 {
447 _dri_put_st_api();
448 FREE(smapi);
449 }
450
451 /**
452 * Return the st_api of OpenGL state tracker.
453 */
454 struct st_api *
455 dri_get_st_api(void)
456 {
457 assert(dri_st_api.stapi);
458 return dri_st_api.stapi;
459 }