st/dri: Headers and public symbols clean up.
[mesa.git] / src / gallium / state_trackers / dri / dri_drawable.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_drawable.h"
35 #include "dri1.h"
36
37 #include "main/mtypes.h"
38 #include "main/renderbuffer.h"
39 #include "state_tracker/st_context.h"
40 #include "state_tracker/st_public.h"
41 #include "state_tracker/st_cb_fbo.h"
42 #include "state_tracker/drm_api.h"
43
44 #include "pipe/p_screen.h"
45 #include "util/u_format.h"
46 #include "util/u_memory.h"
47 #include "util/u_inlines.h"
48
49 static struct pipe_surface *
50 dri_surface_from_handle(struct drm_api *api,
51 struct pipe_screen *screen,
52 unsigned handle,
53 enum pipe_format format,
54 unsigned width, unsigned height, unsigned pitch)
55 {
56 struct pipe_surface *surface = NULL;
57 struct pipe_texture *texture = NULL;
58 struct pipe_texture templat;
59 struct winsys_handle whandle;
60
61 memset(&templat, 0, sizeof(templat));
62 templat.tex_usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
63 templat.target = PIPE_TEXTURE_2D;
64 templat.last_level = 0;
65 templat.depth0 = 1;
66 templat.format = format;
67 templat.width0 = width;
68 templat.height0 = height;
69
70 memset(&whandle, 0, sizeof(whandle));
71 whandle.handle = handle;
72 whandle.stride = pitch;
73
74 texture = screen->texture_from_handle(screen, &templat, &whandle);
75
76 if (!texture) {
77 debug_printf("%s: Failed to blanket the buffer with a texture\n", __func__);
78 return NULL;
79 }
80
81 surface = screen->get_tex_surface(screen, texture, 0, 0, 0,
82 PIPE_BUFFER_USAGE_GPU_READ |
83 PIPE_BUFFER_USAGE_GPU_WRITE);
84
85 /* we don't need the texture from this point on */
86 pipe_texture_reference(&texture, NULL);
87 return surface;
88 }
89
90 /**
91 * Pixmaps have will have the same name of fake front and front.
92 */
93 static boolean
94 dri2_check_if_pixmap(__DRIbuffer *buffers, int count)
95 {
96 boolean found = FALSE;
97 boolean is_pixmap = FALSE;
98 unsigned name;
99 int i;
100
101 for (i = 0; i < count; i++) {
102 switch (buffers[i].attachment) {
103 case __DRI_BUFFER_FRONT_LEFT:
104 case __DRI_BUFFER_FAKE_FRONT_LEFT:
105 if (found) {
106 is_pixmap = buffers[i].name == name;
107 } else {
108 name = buffers[i].name;
109 found = TRUE;
110 }
111 default:
112 continue;
113 }
114 }
115
116 return is_pixmap;
117 }
118
119 /**
120 * This will be called a drawable is known to have been resized.
121 */
122 void
123 dri_get_buffers(__DRIdrawable * dPriv)
124 {
125
126 struct dri_drawable *drawable = dri_drawable(dPriv);
127 struct pipe_surface *surface = NULL;
128 struct dri_screen *st_screen = dri_screen(drawable->sPriv);
129 struct pipe_screen *screen = st_screen->pipe_screen;
130 __DRIbuffer *buffers = NULL;
131 __DRIscreen *dri_screen = drawable->sPriv;
132 __DRIdrawable *dri_drawable = drawable->dPriv;
133 struct drm_api *api = st_screen->api;
134 boolean have_depth = FALSE;
135 int i, count;
136
137 if ((dri_screen->dri2.loader
138 && (dri_screen->dri2.loader->base.version > 2)
139 && (dri_screen->dri2.loader->getBuffersWithFormat != NULL))) {
140 buffers = (*dri_screen->dri2.loader->getBuffersWithFormat)
141 (dri_drawable, &dri_drawable->w, &dri_drawable->h,
142 drawable->attachments, drawable->num_attachments,
143 &count, dri_drawable->loaderPrivate);
144 } else {
145 assert(dri_screen->dri2.loader);
146 buffers = (*dri_screen->dri2.loader->getBuffers) (dri_drawable,
147 &dri_drawable->w,
148 &dri_drawable->h,
149 drawable->attachments,
150 drawable->
151 num_attachments, &count,
152 dri_drawable->
153 loaderPrivate);
154 }
155
156 if (buffers == NULL) {
157 return;
158 }
159
160 /* set one cliprect to cover the whole dri_drawable */
161 dri_drawable->x = 0;
162 dri_drawable->y = 0;
163 dri_drawable->backX = 0;
164 dri_drawable->backY = 0;
165 dri_drawable->numClipRects = 1;
166 dri_drawable->pClipRects[0].x1 = 0;
167 dri_drawable->pClipRects[0].y1 = 0;
168 dri_drawable->pClipRects[0].x2 = dri_drawable->w;
169 dri_drawable->pClipRects[0].y2 = dri_drawable->h;
170 dri_drawable->numBackClipRects = 1;
171 dri_drawable->pBackClipRects[0].x1 = 0;
172 dri_drawable->pBackClipRects[0].y1 = 0;
173 dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
174 dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
175
176 if (drawable->old_num == count &&
177 drawable->old_w == dri_drawable->w &&
178 drawable->old_h == dri_drawable->h &&
179 memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0) {
180 return;
181 } else {
182 drawable->old_num = count;
183 drawable->old_w = dri_drawable->w;
184 drawable->old_h = dri_drawable->h;
185 memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
186 }
187
188 drawable->is_pixmap = dri2_check_if_pixmap(buffers, count);
189
190 for (i = 0; i < count; i++) {
191 enum pipe_format format = 0;
192 int index = 0;
193
194 switch (buffers[i].attachment) {
195 case __DRI_BUFFER_FRONT_LEFT:
196 if (!st_screen->auto_fake_front)
197 continue;
198 /* fallthrough */
199 case __DRI_BUFFER_FAKE_FRONT_LEFT:
200 index = ST_SURFACE_FRONT_LEFT;
201 format = drawable->color_format;
202 break;
203 case __DRI_BUFFER_BACK_LEFT:
204 index = ST_SURFACE_BACK_LEFT;
205 format = drawable->color_format;
206 break;
207 case __DRI_BUFFER_DEPTH:
208 case __DRI_BUFFER_DEPTH_STENCIL:
209 case __DRI_BUFFER_STENCIL:
210 index = ST_SURFACE_DEPTH;
211 format = drawable->depth_stencil_format;
212 break;
213 case __DRI_BUFFER_ACCUM:
214 default:
215 assert(0);
216 }
217
218 if (index == ST_SURFACE_DEPTH) {
219 if (have_depth)
220 continue;
221 else
222 have_depth = TRUE;
223 }
224
225 surface = dri_surface_from_handle(api,
226 screen,
227 buffers[i].name,
228 format,
229 dri_drawable->w,
230 dri_drawable->h, buffers[i].pitch);
231
232 switch (buffers[i].attachment) {
233 case __DRI_BUFFER_FRONT_LEFT:
234 case __DRI_BUFFER_FAKE_FRONT_LEFT:
235 case __DRI_BUFFER_BACK_LEFT:
236 drawable->color_format = surface->format;
237 break;
238 case __DRI_BUFFER_DEPTH:
239 case __DRI_BUFFER_DEPTH_STENCIL:
240 case __DRI_BUFFER_STENCIL:
241 drawable->depth_stencil_format = surface->format;
242 break;
243 case __DRI_BUFFER_ACCUM:
244 default:
245 assert(0);
246 }
247
248 st_set_framebuffer_surface(drawable->stfb, index, surface);
249 pipe_surface_reference(&surface, NULL);
250 }
251 /* this needed, or else the state tracker fails to pick the new buffers */
252 st_resize_framebuffer(drawable->stfb, dri_drawable->w, dri_drawable->h);
253 }
254
255 /**
256 * These are used for GLX_EXT_texture_from_pixmap
257 */
258 void dri2_set_tex_buffer2(__DRIcontext *pDRICtx, GLint target,
259 GLint format, __DRIdrawable *dPriv)
260 {
261 struct dri_drawable *drawable = dri_drawable(dPriv);
262 struct pipe_surface *ps;
263
264 if (!drawable->stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer) {
265 struct gl_renderbuffer *rb =
266 st_new_renderbuffer_fb(drawable->color_format, 0 /*XXX*/, FALSE);
267 _mesa_add_renderbuffer(&drawable->stfb->Base, BUFFER_FRONT_LEFT, rb);
268 }
269
270 dri_get_buffers(drawable->dPriv);
271 st_get_framebuffer_surface(drawable->stfb, ST_SURFACE_FRONT_LEFT, &ps);
272
273 if (!ps)
274 return;
275
276 st_bind_texture_surface(ps, target == GL_TEXTURE_2D ? ST_TEXTURE_2D :
277 ST_TEXTURE_RECT, 0, drawable->color_format);
278 }
279
280 void dri2_set_tex_buffer(__DRIcontext *pDRICtx, GLint target,
281 __DRIdrawable *dPriv)
282 {
283 dri2_set_tex_buffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
284 }
285
286 void
287 dri_update_buffer(struct pipe_screen *screen, void *context_private)
288 {
289 struct dri_context *ctx = (struct dri_context *)context_private;
290
291 if (ctx->d_stamp == *ctx->dPriv->pStamp &&
292 ctx->r_stamp == *ctx->rPriv->pStamp)
293 return;
294
295 ctx->d_stamp = *ctx->dPriv->pStamp;
296 ctx->r_stamp = *ctx->rPriv->pStamp;
297
298 /* Ask the X server for new renderbuffers. */
299 dri_get_buffers(ctx->dPriv);
300 if (ctx->dPriv != ctx->rPriv)
301 dri_get_buffers(ctx->rPriv);
302
303 }
304
305 void
306 dri_flush_frontbuffer(struct pipe_screen *screen,
307 struct pipe_surface *surf, void *context_private)
308 {
309 struct dri_context *ctx = (struct dri_context *)context_private;
310 struct dri_drawable *drawable = dri_drawable(ctx->dPriv);
311 __DRIdrawable *dri_drawable = ctx->dPriv;
312 __DRIscreen *dri_screen = ctx->sPriv;
313
314 /* XXX Does this function get called with DRI1? */
315
316 if (ctx->dPriv == NULL) {
317 debug_printf("%s: no drawable bound to context\n", __func__);
318 return;
319 }
320
321 #if 0
322 /* TODO if rendering to pixmaps is slow enable this code. */
323 if (drawable->is_pixmap)
324 return;
325 #else
326 (void)drawable;
327 #endif
328
329 (*dri_screen->dri2.loader->flushFrontBuffer)(dri_drawable,
330 dri_drawable->loaderPrivate);
331 }
332
333 /**
334 * This is called when we need to set up GL rendering to a new X window.
335 */
336 boolean
337 dri_create_buffer(__DRIscreen * sPriv,
338 __DRIdrawable * dPriv,
339 const __GLcontextModes * visual, boolean isPixmap)
340 {
341 struct dri_screen *screen = sPriv->private;
342 struct dri_drawable *drawable = NULL;
343 int i;
344
345 if (isPixmap)
346 goto fail; /* not implemented */
347
348 drawable = CALLOC_STRUCT(dri_drawable);
349 if (drawable == NULL)
350 goto fail;
351
352 if (visual->redBits == 8) {
353 if (visual->alphaBits == 8)
354 drawable->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
355 else
356 drawable->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
357 } else {
358 drawable->color_format = PIPE_FORMAT_B5G6R5_UNORM;
359 }
360
361 switch(visual->depthBits) {
362 default:
363 case 0:
364 drawable->depth_stencil_format = PIPE_FORMAT_NONE;
365 break;
366 case 16:
367 drawable->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
368 break;
369 case 24:
370 if (visual->stencilBits == 0) {
371 drawable->depth_stencil_format = (screen->d_depth_bits_last) ?
372 PIPE_FORMAT_Z24X8_UNORM:
373 PIPE_FORMAT_X8Z24_UNORM;
374 } else {
375 drawable->depth_stencil_format = (screen->sd_depth_bits_last) ?
376 PIPE_FORMAT_Z24S8_UNORM:
377 PIPE_FORMAT_S8Z24_UNORM;
378 }
379 break;
380 case 32:
381 drawable->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
382 break;
383 }
384
385 drawable->stfb = st_create_framebuffer(visual,
386 drawable->color_format,
387 drawable->depth_stencil_format,
388 drawable->depth_stencil_format,
389 dPriv->w,
390 dPriv->h, (void *)drawable);
391 if (drawable->stfb == NULL)
392 goto fail;
393
394 drawable->sPriv = sPriv;
395 drawable->dPriv = dPriv;
396 dPriv->driverPrivate = (void *)drawable;
397
398 /* setup dri2 buffers information */
399 /* TODO incase of double buffer visual, delay fake creation */
400 i = 0;
401 if (sPriv->dri2.loader
402 && (sPriv->dri2.loader->base.version > 2)
403 && (sPriv->dri2.loader->getBuffersWithFormat != NULL)) {
404 drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
405 drawable->attachments[i++] = visual->rgbBits;
406 if (!screen->auto_fake_front) {
407 drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
408 drawable->attachments[i++] = visual->rgbBits;
409 }
410 if (visual->doubleBufferMode) {
411 drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
412 drawable->attachments[i++] = visual->rgbBits;
413 }
414 if (visual->depthBits && visual->stencilBits) {
415 drawable->attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
416 drawable->attachments[i++] = visual->depthBits + visual->stencilBits;
417 } else if (visual->depthBits) {
418 drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
419 drawable->attachments[i++] = visual->depthBits;
420 } else if (visual->stencilBits) {
421 drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
422 drawable->attachments[i++] = visual->stencilBits;
423 }
424 drawable->num_attachments = i / 2;
425 } else {
426 drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
427 if (!screen->auto_fake_front)
428 drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
429 if (visual->doubleBufferMode)
430 drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
431 if (visual->depthBits && visual->stencilBits)
432 drawable->attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
433 else if (visual->depthBits)
434 drawable->attachments[i++] = __DRI_BUFFER_DEPTH;
435 else if (visual->stencilBits)
436 drawable->attachments[i++] = __DRI_BUFFER_STENCIL;
437 drawable->num_attachments = i;
438 }
439
440 drawable->desired_fences = 2;
441
442 return GL_TRUE;
443 fail:
444 FREE(drawable);
445 return GL_FALSE;
446 }
447
448 void
449 dri_destroy_buffer(__DRIdrawable * dPriv)
450 {
451 struct dri_drawable *drawable = dri_drawable(dPriv);
452
453 st_unreference_framebuffer(drawable->stfb);
454 drawable->desired_fences = 0;
455
456 dri1_swap_fences_clear(drawable);
457
458 FREE(drawable);
459 }
460
461 /* vim: set sw=3 ts=8 sts=3 expandtab: */