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