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
36 #include "pipe/p_screen.h"
37 #include "util/format/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_inlines.h"
40
41 #include "state_tracker/st_context.h"
42
43 static uint32_t drifb_ID = 0;
44
45 static bool
dri_st_framebuffer_validate(struct st_context * st,struct pipe_frontend_drawable * pdrawable,const enum st_attachment_type * statts,unsigned count,struct pipe_resource ** out,struct pipe_resource ** resolve)46 dri_st_framebuffer_validate(struct st_context *st,
47 struct pipe_frontend_drawable *pdrawable,
48 const enum st_attachment_type *statts,
49 unsigned count,
50 struct pipe_resource **out,
51 struct pipe_resource **resolve)
52 {
53 struct dri_context *ctx = (struct dri_context *)st->frontend_context;
54 struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
55 struct dri_screen *screen = drawable->screen;
56 unsigned statt_mask, new_mask;
57 bool new_stamp;
58 int i;
59 unsigned int lastStamp;
60 struct pipe_resource **textures =
61 drawable->stvis.samples > 1 ? drawable->msaa_textures
62 : drawable->textures;
63
64 statt_mask = 0x0;
65 for (i = 0; i < count; i++)
66 statt_mask |= (1 << statts[i]);
67
68 /* record newly allocated textures */
69 new_mask = (statt_mask & ~drawable->texture_mask);
70
71 do {
72 lastStamp = drawable->lastStamp;
73 new_stamp = (drawable->texture_stamp != lastStamp);
74
75 if (new_stamp || new_mask) {
76 if (new_stamp && drawable->update_drawable_info)
77 drawable->update_drawable_info(drawable);
78
79 drawable->allocate_textures(ctx, drawable, statts, count);
80
81 /* add existing textures */
82 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
83 if (textures[i])
84 statt_mask |= (1 << i);
85 }
86
87 drawable->texture_stamp = lastStamp;
88 drawable->texture_mask = statt_mask;
89 }
90 } while (lastStamp != drawable->lastStamp);
91
92 /* Flush the pending set_damage_region request. */
93 struct pipe_screen *pscreen = screen->base.screen;
94
95 if (new_mask & (1 << ST_ATTACHMENT_BACK_LEFT) &&
96 pscreen->set_damage_region) {
97 struct pipe_resource *resource = textures[ST_ATTACHMENT_BACK_LEFT];
98
99 pscreen->set_damage_region(pscreen, resource,
100 drawable->num_damage_rects,
101 drawable->damage_rects);
102 }
103
104 if (!out)
105 return true;
106
107 /* Set the window-system buffers for the gallium frontend. */
108 for (i = 0; i < count; i++)
109 pipe_resource_reference(&out[i], textures[statts[i]]);
110 if (resolve && drawable->stvis.samples > 1) {
111 if (statt_mask & BITFIELD_BIT(ST_ATTACHMENT_FRONT_LEFT))
112 pipe_resource_reference(resolve, drawable->textures[ST_ATTACHMENT_FRONT_LEFT]);
113 else if (statt_mask & BITFIELD_BIT(ST_ATTACHMENT_BACK_LEFT))
114 pipe_resource_reference(resolve, drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
115 }
116
117 return true;
118 }
119
120 static bool
dri_st_framebuffer_flush_front(struct st_context * st,struct pipe_frontend_drawable * pdrawable,enum st_attachment_type statt)121 dri_st_framebuffer_flush_front(struct st_context *st,
122 struct pipe_frontend_drawable *pdrawable,
123 enum st_attachment_type statt)
124 {
125 struct dri_context *ctx = (struct dri_context *)st->frontend_context;
126 struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
127
128 /* XXX remove this and just set the correct one on the framebuffer */
129 return drawable->flush_frontbuffer(ctx, drawable, statt);
130 }
131
132 /**
133 * The gallium frontend framebuffer interface flush_swapbuffers callback
134 */
135 static bool
dri_st_framebuffer_flush_swapbuffers(struct st_context * st,struct pipe_frontend_drawable * pdrawable)136 dri_st_framebuffer_flush_swapbuffers(struct st_context *st,
137 struct pipe_frontend_drawable *pdrawable)
138 {
139 struct dri_context *ctx = (struct dri_context *)st->frontend_context;
140 struct dri_drawable *drawable = (struct dri_drawable *)pdrawable;
141
142 if (drawable->flush_swapbuffers)
143 drawable->flush_swapbuffers(ctx, drawable);
144
145 return true;
146 }
147
148 /**
149 * This is called when we need to set up GL rendering to a new X window.
150 */
151 struct dri_drawable *
dri_create_drawable(struct dri_screen * screen,const struct dri_config * config,bool isPixmap,void * loaderPrivate)152 dri_create_drawable(struct dri_screen *screen, const struct dri_config *config,
153 bool isPixmap, void *loaderPrivate)
154 {
155 const struct gl_config *visual = &config->modes;
156 struct dri_drawable *drawable = NULL;
157
158 drawable = CALLOC_STRUCT(dri_drawable);
159 if (!drawable)
160 return NULL;
161
162 drawable->loaderPrivate = loaderPrivate;
163 drawable->refcount = 1;
164 drawable->lastStamp = 0;
165 drawable->w = 0;
166 drawable->h = 0;
167
168 dri_fill_st_visual(&drawable->stvis, screen, visual);
169
170 /* setup the pipe_frontend_drawable */
171 drawable->base.visual = &drawable->stvis;
172 drawable->base.flush_front = dri_st_framebuffer_flush_front;
173 drawable->base.validate = dri_st_framebuffer_validate;
174 drawable->base.flush_swapbuffers = dri_st_framebuffer_flush_swapbuffers;
175
176 drawable->screen = screen;
177
178 p_atomic_set(&drawable->base.stamp, 1);
179 drawable->base.ID = p_atomic_inc_return(&drifb_ID);
180 drawable->base.fscreen = &screen->base;
181
182 switch (screen->type) {
183 case DRI_SCREEN_DRI3:
184 case DRI_SCREEN_KMS_SWRAST:
185 dri2_init_drawable(drawable, isPixmap, visual->alphaBits);
186 break;
187 case DRI_SCREEN_SWRAST:
188 drisw_init_drawable(drawable, isPixmap, visual->alphaBits);
189 break;
190 case DRI_SCREEN_KOPPER:
191 kopper_init_drawable(drawable, isPixmap, visual->alphaBits);
192 break;
193 }
194
195 return drawable;
196 }
197
198 static void
dri_destroy_drawable(struct dri_drawable * drawable)199 dri_destroy_drawable(struct dri_drawable *drawable)
200 {
201 struct dri_screen *screen = drawable->screen;
202 int i;
203
204 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
205 pipe_resource_reference(&drawable->textures[i], NULL);
206 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
207 pipe_resource_reference(&drawable->msaa_textures[i], NULL);
208
209 screen->base.screen->fence_reference(screen->base.screen,
210 &drawable->throttle_fence, NULL);
211
212 /* Notify the st manager that this drawable is no longer valid */
213 st_api_destroy_drawable(&drawable->base);
214
215 FREE(drawable->damage_rects);
216 FREE(drawable);
217 }
218
219 void
dri_put_drawable(struct dri_drawable * drawable)220 dri_put_drawable(struct dri_drawable *drawable)
221 {
222 if (drawable) {
223 int refcount = --drawable->refcount;
224 assert(refcount >= 0);
225
226 if (!refcount)
227 dri_destroy_drawable(drawable);
228 }
229 }
230
231 /**
232 * Validate the texture at an attachment. Allocate the texture if it does not
233 * exist. Used by the TFP extension.
234 */
235 static void
dri_drawable_validate_att(struct dri_context * ctx,struct dri_drawable * drawable,enum st_attachment_type statt)236 dri_drawable_validate_att(struct dri_context *ctx,
237 struct dri_drawable *drawable,
238 enum st_attachment_type statt)
239 {
240 enum st_attachment_type statts[ST_ATTACHMENT_COUNT];
241 unsigned i, count = 0;
242
243 /* check if buffer already exists */
244 if (drawable->texture_mask & (1 << statt))
245 return;
246
247 /* make sure DRI2 does not destroy existing buffers */
248 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
249 if (drawable->texture_mask & (1 << i)) {
250 statts[count++] = i;
251 }
252 }
253 statts[count++] = statt;
254
255 drawable->texture_stamp = drawable->lastStamp - 1;
256
257 drawable->base.validate(ctx->st, &drawable->base, statts, count, NULL, NULL);
258 }
259
260 /**
261 * These are used for GLX_EXT_texture_from_pixmap
262 */
263 void
dri_set_tex_buffer2(struct dri_context * ctx,GLint target,GLint format,struct dri_drawable * drawable)264 dri_set_tex_buffer2(struct dri_context *ctx, GLint target,
265 GLint format, struct dri_drawable *drawable)
266 {
267 struct st_context *st = ctx->st;
268 struct pipe_resource *pt;
269
270 _mesa_glthread_finish(st->ctx);
271
272 dri_drawable_validate_att(ctx, drawable, ST_ATTACHMENT_FRONT_LEFT);
273
274 /* Use the pipe resource associated with the X drawable */
275 pt = drawable->textures[ST_ATTACHMENT_FRONT_LEFT];
276
277 if (pt) {
278 enum pipe_format internal_format = pt->format;
279
280 if (format == __DRI_TEXTURE_FORMAT_RGB) {
281 /* only need to cover the formats recognized by dri_fill_st_visual */
282 switch (internal_format) {
283 case PIPE_FORMAT_R16G16B16A16_FLOAT:
284 internal_format = PIPE_FORMAT_R16G16B16X16_FLOAT;
285 break;
286 case PIPE_FORMAT_B10G10R10A2_UNORM:
287 internal_format = PIPE_FORMAT_B10G10R10X2_UNORM;
288 break;
289 case PIPE_FORMAT_R10G10B10A2_UNORM:
290 internal_format = PIPE_FORMAT_R10G10B10X2_UNORM;
291 break;
292 case PIPE_FORMAT_BGRA8888_UNORM:
293 internal_format = PIPE_FORMAT_BGRX8888_UNORM;
294 break;
295 case PIPE_FORMAT_ARGB8888_UNORM:
296 internal_format = PIPE_FORMAT_XRGB8888_UNORM;
297 break;
298 default:
299 break;
300 }
301 }
302
303 drawable->update_tex_buffer(drawable, ctx, pt);
304
305 st_context_teximage(ctx->st, target, 0, internal_format, pt, false);
306 }
307 }
308
309 const __DRItexBufferExtension driTexBufferExtension = {
310 .base = { __DRI_TEX_BUFFER, 2 },
311
312 .setTexBuffer2 = dri_set_tex_buffer2,
313 };
314
315 /**
316 * Get the format and binding of an attachment.
317 */
318 void
dri_drawable_get_format(struct dri_drawable * drawable,enum st_attachment_type statt,enum pipe_format * format,unsigned * bind)319 dri_drawable_get_format(struct dri_drawable *drawable,
320 enum st_attachment_type statt,
321 enum pipe_format *format,
322 unsigned *bind)
323 {
324 switch (statt) {
325 case ST_ATTACHMENT_FRONT_LEFT:
326 case ST_ATTACHMENT_BACK_LEFT:
327 case ST_ATTACHMENT_FRONT_RIGHT:
328 case ST_ATTACHMENT_BACK_RIGHT:
329 /* Other pieces of the driver stack get confused and behave incorrectly
330 * when they get an sRGB drawable. st/mesa receives "drawable->stvis"
331 * though other means and handles it correctly, so we don't really need
332 * to use an sRGB format here.
333 */
334 *format = util_format_linear(drawable->stvis.color_format);
335 *bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
336 break;
337 case ST_ATTACHMENT_DEPTH_STENCIL:
338 *format = drawable->stvis.depth_stencil_format;
339 *bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
340 break;
341 default:
342 *format = PIPE_FORMAT_NONE;
343 *bind = 0;
344 break;
345 }
346 }
347
348 void
dri_pipe_blit(struct pipe_context * pipe,struct pipe_resource * dst,struct pipe_resource * src)349 dri_pipe_blit(struct pipe_context *pipe,
350 struct pipe_resource *dst,
351 struct pipe_resource *src)
352 {
353 struct pipe_blit_info blit;
354
355 if (!dst || !src)
356 return;
357
358 /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
359 * Fragment Operations):
360 *
361 * If a framebuffer object is not bound, after all operations have
362 * been completed on the multisample buffer, the sample values for
363 * each color in the multisample buffer are combined to produce a
364 * single color value, and that value is written into the
365 * corresponding color buffers selected by DrawBuffer or
366 * DrawBuffers. An implementation may defer the writing of the color
367 * buffers until a later time, but the state of the framebuffer must
368 * behave as if the color buffers were updated as each fragment was
369 * processed. The method of combination is not specified. If the
370 * framebuffer contains sRGB values, then it is recommended that the
371 * an average of sample values is computed in a linearized space, as
372 * for blending (see section 4.1.7).
373 *
374 * In other words, to do a resolve operation in a linear space, we have
375 * to set sRGB formats if the original resources were sRGB, so don't use
376 * util_format_linear.
377 */
378
379 memset(&blit, 0, sizeof(blit));
380 blit.dst.resource = dst;
381 blit.dst.box.width = dst->width0;
382 blit.dst.box.height = dst->height0;
383 blit.dst.box.depth = 1;
384 blit.dst.format = dst->format;
385 blit.src.resource = src;
386 blit.src.box.width = src->width0;
387 blit.src.box.height = src->height0;
388 blit.src.box.depth = 1;
389 blit.src.format = src->format;
390 blit.mask = PIPE_MASK_RGBA;
391 blit.filter = PIPE_TEX_FILTER_NEAREST;
392
393 pipe->blit(pipe, &blit);
394 }
395
396 static void
dri_postprocessing(struct dri_context * ctx,struct dri_drawable * drawable,enum st_attachment_type att)397 dri_postprocessing(struct dri_context *ctx,
398 struct dri_drawable *drawable,
399 enum st_attachment_type att)
400 {
401 struct pipe_resource *src = drawable->textures[att];
402 struct pipe_resource *zsbuf = drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL];
403
404 if (ctx->pp && src)
405 pp_run(ctx->pp, src, src, zsbuf);
406 }
407
408 struct notify_before_flush_cb_args {
409 struct dri_context *ctx;
410 struct dri_drawable *drawable;
411 unsigned flags;
412 enum __DRI2throttleReason reason;
413 bool swap_msaa_buffers;
414 };
415
416 static void
notify_before_flush_cb(void * _args)417 notify_before_flush_cb(void* _args)
418 {
419 struct notify_before_flush_cb_args *args = (struct notify_before_flush_cb_args *) _args;
420 struct st_context *st = args->ctx->st;
421 struct pipe_context *pipe = st->pipe;
422
423 /* Wait for glthread to finish because we can't use pipe_context from
424 * multiple threads.
425 */
426 _mesa_glthread_finish(st->ctx);
427
428 if (args->drawable->stvis.samples > 1 &&
429 (args->reason == __DRI2_THROTTLE_SWAPBUFFER ||
430 args->reason == __DRI2_NOTHROTTLE_SWAPBUFFER ||
431 args->reason == __DRI2_THROTTLE_COPYSUBBUFFER)) {
432 /* Resolve the MSAA back buffer. */
433 dri_pipe_blit(st->pipe,
434 args->drawable->textures[ST_ATTACHMENT_BACK_LEFT],
435 args->drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
436
437 if ((args->reason == __DRI2_THROTTLE_SWAPBUFFER ||
438 args->reason == __DRI2_NOTHROTTLE_SWAPBUFFER) &&
439 args->drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] &&
440 args->drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT]) {
441 args->swap_msaa_buffers = true;
442 }
443
444 /* FRONT_LEFT is resolved in drawable->flush_frontbuffer. */
445 }
446
447 dri_postprocessing(args->ctx, args->drawable, ST_ATTACHMENT_BACK_LEFT);
448
449 if (pipe->invalidate_resource &&
450 (args->flags & __DRI2_FLUSH_INVALIDATE_ANCILLARY)) {
451 if (args->drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
452 pipe->invalidate_resource(pipe, args->drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
453 if (args->drawable->msaa_textures[ST_ATTACHMENT_DEPTH_STENCIL])
454 pipe->invalidate_resource(pipe, args->drawable->msaa_textures[ST_ATTACHMENT_DEPTH_STENCIL]);
455 }
456
457 if (args->ctx->hud) {
458 hud_run(args->ctx->hud, args->ctx->st->cso_context,
459 args->drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
460 }
461
462 pipe->flush_resource(pipe, args->drawable->textures[ST_ATTACHMENT_BACK_LEFT]);
463 }
464
465 /**
466 * DRI2 flush extension, the flush_with_flags function.
467 *
468 * \param context the context
469 * \param drawable the drawable to flush
470 * \param flags a combination of _DRI2_FLUSH_xxx flags
471 * \param throttle_reason the reason for throttling, 0 = no throttling
472 */
473 void
dri_flush(struct dri_context * ctx,struct dri_drawable * drawable,unsigned flags,enum __DRI2throttleReason reason)474 dri_flush(struct dri_context *ctx,
475 struct dri_drawable *drawable,
476 unsigned flags,
477 enum __DRI2throttleReason reason)
478 {
479 struct st_context *st;
480 unsigned flush_flags;
481 struct notify_before_flush_cb_args args = { 0 };
482
483 if (!ctx) {
484 assert(0);
485 return;
486 }
487
488 st = ctx->st;
489 _mesa_glthread_finish(st->ctx);
490
491 if (drawable) {
492 /* prevent recursion */
493 if (drawable->flushing)
494 return;
495
496 drawable->flushing = true;
497 }
498 else {
499 flags &= ~__DRI2_FLUSH_DRAWABLE;
500 }
501
502 if ((flags & __DRI2_FLUSH_DRAWABLE) &&
503 drawable->textures[ST_ATTACHMENT_BACK_LEFT]) {
504 /* We can't do operations on the back buffer here, because there
505 * may be some pending operations that will get flushed by the
506 * call to st->flush (eg: FLUSH_VERTICES).
507 * Instead we register a callback to be notified when all operations
508 * have been submitted but before the call to st_flush.
509 */
510 args.ctx = ctx;
511 args.drawable = drawable;
512 args.flags = flags;
513 args.reason = reason;
514 }
515
516 flush_flags = 0;
517 if (flags & __DRI2_FLUSH_CONTEXT)
518 flush_flags |= ST_FLUSH_FRONT;
519 if (reason == __DRI2_THROTTLE_SWAPBUFFER ||
520 reason == __DRI2_NOTHROTTLE_SWAPBUFFER)
521 flush_flags |= ST_FLUSH_END_OF_FRAME;
522
523 /* Flush the context and throttle if needed. */
524 if (ctx->screen->throttle &&
525 drawable &&
526 (reason == __DRI2_THROTTLE_SWAPBUFFER ||
527 reason == __DRI2_THROTTLE_FLUSHFRONT)) {
528
529 struct pipe_screen *screen = drawable->screen->base.screen;
530 struct pipe_fence_handle *new_fence = NULL;
531
532 st_context_flush(st, flush_flags, &new_fence, args.ctx ? notify_before_flush_cb : NULL, &args);
533
534 /* throttle on the previous fence */
535 if (drawable->throttle_fence) {
536 screen->fence_finish(screen, NULL, drawable->throttle_fence, OS_TIMEOUT_INFINITE);
537 screen->fence_reference(screen, &drawable->throttle_fence, NULL);
538 }
539 drawable->throttle_fence = new_fence;
540 }
541 else if (flags & (__DRI2_FLUSH_DRAWABLE | __DRI2_FLUSH_CONTEXT)) {
542 st_context_flush(st, flush_flags, NULL, args.ctx ? notify_before_flush_cb : NULL, &args);
543 }
544
545 if (drawable) {
546 drawable->flushing = false;
547 }
548
549 /* Swap the MSAA front and back buffers, so that reading
550 * from the front buffer after SwapBuffers returns what was
551 * in the back buffer.
552 */
553 if (args.swap_msaa_buffers) {
554 struct pipe_resource *tmp =
555 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT];
556
557 drawable->msaa_textures[ST_ATTACHMENT_FRONT_LEFT] =
558 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT];
559 drawable->msaa_textures[ST_ATTACHMENT_BACK_LEFT] = tmp;
560
561 /* Now that we have swapped the buffers, this tells the gallium
562 * frontend to revalidate the framebuffer.
563 */
564 p_atomic_inc(&drawable->base.stamp);
565 }
566
567 st_context_invalidate_state(st, ST_INVALIDATE_FB_STATE);
568 }
569
570 /**
571 * DRI2 flush extension.
572 */
573 void
dri_flush_drawable(struct dri_drawable * dPriv)574 dri_flush_drawable(struct dri_drawable *dPriv)
575 {
576 struct dri_context *ctx = dri_get_current();
577
578 if (ctx)
579 dri_flush(ctx, dPriv, __DRI2_FLUSH_DRAWABLE, -1);
580 }
581
582 /**
583 * dri_throttle - A DRI2ThrottleExtension throttling function.
584 */
585 void
dri_throttle(struct dri_context * cPriv,struct dri_drawable * dPriv,enum __DRI2throttleReason reason)586 dri_throttle(struct dri_context *cPriv, struct dri_drawable *dPriv,
587 enum __DRI2throttleReason reason)
588 {
589 dri_flush(cPriv, dPriv, 0, reason);
590 }
591
592 /* vim: set sw=3 ts=8 sts=3 expandtab: */
593