Merge branch '7.8'
[mesa.git] / src / gallium / state_trackers / dri / drm / dri1.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 /* XXX DRI1 is untested after the switch to st_api.h */
33
34 #include "util/u_memory.h"
35 #include "util/u_rect.h"
36 #include "util/u_inlines.h"
37 #include "pipe/p_context.h"
38 #include "state_tracker/dri1_api.h"
39
40 #include "dri_screen.h"
41 #include "dri_context.h"
42 #include "dri_drawable.h"
43 #include "dri1_helper.h"
44 #include "dri1.h"
45
46 static INLINE void
47 dri1_lock(struct dri_context *ctx)
48 {
49 drm_context_t hw_context = ctx->cPriv->hHWContext;
50 char ret = 0;
51
52 DRM_CAS(ctx->lock, hw_context, DRM_LOCK_HELD | hw_context, ret);
53 if (ret) {
54 drmGetLock(ctx->sPriv->fd, hw_context, 0);
55 ctx->stLostLock = TRUE;
56 ctx->wsLostLock = TRUE;
57 }
58 ctx->isLocked = TRUE;
59 }
60
61 static INLINE void
62 dri1_unlock(struct dri_context *ctx)
63 {
64 ctx->isLocked = FALSE;
65 DRM_UNLOCK(ctx->sPriv->fd, ctx->lock, ctx->cPriv->hHWContext);
66 }
67
68 static void
69 dri1_update_drawables_locked(struct dri_context *ctx,
70 __DRIdrawable * driDrawPriv,
71 __DRIdrawable * driReadPriv)
72 {
73 if (ctx->stLostLock) {
74 ctx->stLostLock = FALSE;
75 if (driDrawPriv == driReadPriv)
76 DRI_VALIDATE_DRAWABLE_INFO(ctx->sPriv, driDrawPriv);
77 else
78 DRI_VALIDATE_TWO_DRAWABLES_INFO(ctx->sPriv, driDrawPriv,
79 driReadPriv);
80 }
81 }
82
83 /**
84 * This ensures all contexts which bind to a drawable pick up the
85 * drawable change and signal new buffer state.
86 */
87 static void
88 dri1_propagate_drawable_change(struct dri_context *ctx)
89 {
90 __DRIdrawable *dPriv = ctx->dPriv;
91 __DRIdrawable *rPriv = ctx->rPriv;
92 struct dri_drawable *draw;
93 struct dri_drawable *read;
94 boolean flushed = FALSE;
95
96 if (dPriv) {
97 draw = dri_drawable(dPriv);
98 }
99
100 if (rPriv) {
101 read = dri_drawable(rPriv);
102 }
103
104 if (dPriv && draw->texture_stamp != dPriv->lastStamp) {
105 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
106 flushed = TRUE;
107 ctx->st->notify_invalid_framebuffer(ctx->st, &draw->base);
108 }
109
110 if (rPriv && dPriv != rPriv && read->texture_stamp != rPriv->lastStamp) {
111 if (!flushed)
112 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
113 ctx->st->notify_invalid_framebuffer(ctx->st, &read->base);
114 }
115 }
116
117 static INLINE boolean
118 dri1_intersect_src_bbox(struct drm_clip_rect *dst,
119 int dst_x,
120 int dst_y,
121 const struct drm_clip_rect *src,
122 const struct drm_clip_rect *bbox)
123 {
124 int xy1;
125 int xy2;
126
127 xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
128 (int)bbox->x1 + dst_x;
129 xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
130 (int)bbox->x2 + dst_x;
131 if (xy1 >= xy2 || xy1 < 0)
132 return FALSE;
133
134 dst->x1 = xy1;
135 dst->x2 = xy2;
136
137 xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
138 (int)bbox->y1 + dst_y;
139 xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
140 (int)bbox->y2 + dst_y;
141 if (xy1 >= xy2 || xy1 < 0)
142 return FALSE;
143
144 dst->y1 = xy1;
145 dst->y2 = xy2;
146 return TRUE;
147 }
148
149 static void
150 dri1_swap_copy(struct pipe_context *pipe,
151 struct pipe_surface *dst,
152 struct pipe_surface *src,
153 __DRIdrawable * dPriv, const struct drm_clip_rect *bbox)
154 {
155 struct drm_clip_rect clip;
156 struct drm_clip_rect *cur;
157 int i;
158
159 cur = dPriv->pClipRects;
160
161 for (i = 0; i < dPriv->numClipRects; ++i) {
162 if (dri1_intersect_src_bbox(&clip, dPriv->x, dPriv->y, cur++, bbox)) {
163 if (pipe->surface_copy) {
164 pipe->surface_copy(pipe, dst, clip.x1, clip.y1,
165 src,
166 (int)clip.x1 - dPriv->x,
167 (int)clip.y1 - dPriv->y,
168 clip.x2 - clip.x1, clip.y2 - clip.y1);
169 } else {
170 util_surface_copy(pipe, FALSE, dst, clip.x1, clip.y1,
171 src,
172 (int)clip.x1 - dPriv->x,
173 (int)clip.y1 - dPriv->y,
174 clip.x2 - clip.x1, clip.y2 - clip.y1);
175 }
176 }
177 }
178 }
179
180 static void
181 dri1_present_texture_locked(__DRIdrawable * dPriv,
182 struct pipe_resource *ptex,
183 const struct drm_clip_rect *sub_box,
184 struct pipe_fence_handle **fence)
185 {
186 struct dri_drawable *drawable = dri_drawable(dPriv);
187 struct dri_screen *screen = dri_screen(drawable->sPriv);
188 struct pipe_context *pipe;
189 struct pipe_surface *psurf;
190 struct drm_clip_rect bbox;
191 boolean visible = TRUE;
192
193 *fence = NULL;
194
195 bbox.x1 = 0;
196 bbox.x2 = ptex->width0;
197 bbox.y1 = 0;
198 bbox.y2 = ptex->height0;
199
200 if (sub_box)
201 visible = dri1_intersect_src_bbox(&bbox, 0, 0, &bbox, sub_box);
202 if (!visible)
203 return;
204
205 pipe = dri1_get_pipe_context(screen);
206 psurf = dri1_get_pipe_surface(drawable, ptex);
207 if (!pipe || !psurf)
208 return;
209
210 if (__dri1_api_hooks->present_locked) {
211 __dri1_api_hooks->present_locked(pipe, psurf,
212 dPriv->pClipRects, dPriv->numClipRects,
213 dPriv->x, dPriv->y, &bbox, fence);
214 } else if (__dri1_api_hooks->front_srf_locked) {
215 struct pipe_surface *front = __dri1_api_hooks->front_srf_locked(pipe);
216
217 if (front)
218 dri1_swap_copy(pipe, front, psurf, dPriv, &bbox);
219
220 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, fence);
221 }
222 }
223
224 static void
225 dri1_copy_to_front(struct dri_context *ctx,
226 struct pipe_resource *ptex,
227 __DRIdrawable * dPriv,
228 const struct drm_clip_rect *sub_box,
229 struct pipe_fence_handle **fence)
230 {
231 boolean save_lost_lock;
232
233 dri1_lock(ctx);
234 save_lost_lock = ctx->stLostLock;
235 dri1_update_drawables_locked(ctx, dPriv, dPriv);
236
237 dri1_present_texture_locked(dPriv, ptex, sub_box, fence);
238
239 ctx->stLostLock = save_lost_lock;
240
241 /**
242 * FIXME: Revisit this: Update drawables on copy_sub_buffer ?
243 */
244
245 if (!sub_box)
246 dri1_update_drawables_locked(ctx, ctx->dPriv, ctx->rPriv);
247
248 dri1_unlock(ctx);
249 dri1_propagate_drawable_change(ctx);
250 }
251
252 /*
253 * Backend functions for st_framebuffer interface and swap_buffers.
254 */
255
256 static void
257 dri1_flush_frontbuffer(struct dri_drawable *draw,
258 enum st_attachment_type statt)
259 {
260 struct dri_context *ctx = dri_get_current(draw->sPriv);
261 struct dri_screen *screen = dri_screen(draw->sPriv);
262 struct pipe_screen *pipe_screen = screen->base.screen;
263 struct pipe_fence_handle *dummy_fence;
264 struct pipe_resource *ptex;
265
266 if (!ctx)
267 return; /* For now */
268
269 ptex = draw->textures[statt];
270 if (ptex) {
271 dri1_copy_to_front(ctx, ptex, ctx->dPriv, NULL, &dummy_fence);
272 pipe_screen->fence_reference(pipe_screen, &dummy_fence, NULL);
273 }
274
275 /**
276 * FIXME: Do we need swap throttling here?
277 */
278 }
279
280 void
281 dri1_swap_buffers(__DRIdrawable * dPriv)
282 {
283 struct dri_drawable *draw = dri_drawable(dPriv);
284 struct dri_context *ctx = dri_get_current(draw->sPriv);
285 struct dri_screen *screen = dri_screen(draw->sPriv);
286 struct pipe_screen *pipe_screen = screen->base.screen;
287 struct pipe_fence_handle *fence;
288 struct pipe_resource *ptex;
289
290 assert(__dri1_api_hooks != NULL);
291
292 if (!ctx)
293 return; /* For now */
294
295 ptex = draw->textures[ST_ATTACHMENT_BACK_LEFT];
296 if (ptex) {
297 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
298 fence = dri1_swap_fences_pop_front(draw);
299 if (fence) {
300 (void)pipe_screen->fence_finish(pipe_screen, fence, 0);
301 pipe_screen->fence_reference(pipe_screen, &fence, NULL);
302 }
303 dri1_copy_to_front(ctx, ptex, dPriv, NULL, &fence);
304 dri1_swap_fences_push_back(draw, fence);
305 pipe_screen->fence_reference(pipe_screen, &fence, NULL);
306 }
307 }
308
309 void
310 dri1_copy_sub_buffer(__DRIdrawable * dPriv, int x, int y, int w, int h)
311 {
312 struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
313 struct dri_screen *screen = dri_screen(dPriv->driScreenPriv);
314 struct pipe_screen *pipe_screen = screen->base.screen;
315 struct drm_clip_rect sub_bbox;
316 struct dri_drawable *draw = dri_drawable(dPriv);
317 struct pipe_fence_handle *dummy_fence;
318 struct pipe_resource *ptex;
319
320 assert(__dri1_api_hooks != NULL);
321
322 if (!ctx)
323 return;
324
325 sub_bbox.x1 = x;
326 sub_bbox.x2 = x + w;
327 sub_bbox.y1 = y;
328 sub_bbox.y2 = y + h;
329
330 ptex = draw->textures[ST_ATTACHMENT_BACK_LEFT];
331 if (ptex) {
332 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
333 dri1_copy_to_front(ctx, ptex, dPriv, &sub_bbox, &dummy_fence);
334 pipe_screen->fence_reference(pipe_screen, &dummy_fence, NULL);
335 }
336 }
337
338 /**
339 * Allocate framebuffer attachments.
340 *
341 * During fixed-size operation, the function keeps allocating new attachments
342 * as they are requested. Unused attachments are not removed, not until the
343 * framebuffer is resized or destroyed.
344 */
345 static void
346 dri1_allocate_textures(struct dri_drawable *drawable,
347 const enum st_attachment_type *statts,
348 unsigned count)
349 {
350 struct dri_screen *screen = dri_screen(drawable->sPriv);
351 struct pipe_resource templ;
352 unsigned width, height;
353 boolean resized;
354 int i;
355
356 width = drawable->dPriv->w;
357 height = drawable->dPriv->h;
358
359 resized = (drawable->old_w != width ||
360 drawable->old_h != height);
361
362 /* remove outdated textures */
363 if (resized) {
364 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
365 pipe_resource_reference(&drawable->textures[i], NULL);
366 }
367
368 memset(&templ, 0, sizeof(templ));
369 templ.target = PIPE_TEXTURE_2D;
370 templ.width0 = width;
371 templ.height0 = height;
372 templ.depth0 = 1;
373 templ.last_level = 0;
374
375 for (i = 0; i < count; i++) {
376 enum pipe_format format;
377 unsigned bind;
378
379 /* the texture already exists */
380 if (drawable->textures[statts[i]])
381 continue;
382
383 dri_drawable_get_format(drawable, statts[i], &format, &bind);
384
385 if (format == PIPE_FORMAT_NONE)
386 continue;
387
388 templ.format = format;
389 templ.bind = bind;
390
391 drawable->textures[statts[i]] =
392 screen->base.screen->resource_create(screen->base.screen, &templ);
393 }
394
395 drawable->old_w = width;
396 drawable->old_h = height;
397 }
398
399 /*
400 * Backend function for init_screen.
401 */
402
403 static const __DRIextension *dri1_screen_extensions[] = {
404 &driReadDrawableExtension,
405 &driCopySubBufferExtension.base,
406 &driSwapControlExtension.base,
407 &driFrameTrackingExtension.base,
408 &driMediaStreamCounterExtension.base,
409 NULL
410 };
411
412 static void
413 st_dri_lock(struct pipe_context *pipe)
414 {
415 dri1_lock((struct dri_context *)pipe->priv);
416 }
417
418 static void
419 st_dri_unlock(struct pipe_context *pipe)
420 {
421 dri1_unlock((struct dri_context *)pipe->priv);
422 }
423
424 static boolean
425 st_dri_is_locked(struct pipe_context *pipe)
426 {
427 return ((struct dri_context *)pipe->priv)->isLocked;
428 }
429
430 static boolean
431 st_dri_lost_lock(struct pipe_context *pipe)
432 {
433 return ((struct dri_context *)pipe->priv)->wsLostLock;
434 }
435
436 static void
437 st_dri_clear_lost_lock(struct pipe_context *pipe)
438 {
439 ((struct dri_context *)pipe->priv)->wsLostLock = FALSE;
440 }
441
442 static struct dri1_api_lock_funcs dri1_lf = {
443 .lock = st_dri_lock,
444 .unlock = st_dri_unlock,
445 .is_locked = st_dri_is_locked,
446 .is_lock_lost = st_dri_lost_lock,
447 .clear_lost_lock = st_dri_clear_lost_lock
448 };
449
450 static INLINE void
451 dri1_copy_version(struct dri1_api_version *dst,
452 const struct __DRIversionRec *src)
453 {
454 dst->major = src->major;
455 dst->minor = src->minor;
456 dst->patch_level = src->patch;
457 }
458
459 struct dri1_api *__dri1_api_hooks = NULL;
460
461 const __DRIconfig **
462 dri1_init_screen(__DRIscreen * sPriv)
463 {
464 const __DRIconfig **configs;
465 struct pipe_screen *pscreen;
466 struct dri_screen *screen;
467 struct dri1_create_screen_arg arg;
468
469 screen = CALLOC_STRUCT(dri_screen);
470 if (!screen)
471 return NULL;
472
473 screen->api = drm_api_create();
474 screen->sPriv = sPriv;
475 screen->fd = sPriv->fd;
476 screen->drmLock = (drmLock *) & sPriv->pSAREA->lock;
477 screen->allocate_textures = dri1_allocate_textures;
478 screen->flush_frontbuffer = dri1_flush_frontbuffer;
479
480 sPriv->private = (void *)screen;
481 sPriv->extensions = dri1_screen_extensions;
482
483 arg.base.mode = DRM_CREATE_DRI1;
484 arg.lf = &dri1_lf;
485 arg.ddx_info = sPriv->pDevPriv;
486 arg.ddx_info_size = sPriv->devPrivSize;
487 arg.sarea = sPriv->pSAREA;
488 dri1_copy_version(&arg.ddx_version, &sPriv->ddx_version);
489 dri1_copy_version(&arg.dri_version, &sPriv->dri_version);
490 dri1_copy_version(&arg.drm_version, &sPriv->drm_version);
491 arg.api = NULL;
492
493 /**
494 * FIXME: If the driver supports format conversion swapbuffer blits, we might
495 * want to support other color bit depths than the server is currently
496 * using.
497 */
498
499 pscreen = screen->api->create_screen(screen->api, screen->fd, &arg.base);
500 /* dri_init_screen_helper checks pscreen for us */
501
502 configs = dri_init_screen_helper(screen, pscreen, sPriv->fbBPP);
503 if (!configs)
504 goto fail;
505
506 if (!arg.api) {
507 debug_printf("%s: failed to create dri1 screen\n", __FUNCTION__);
508 goto fail;
509 }
510
511 __dri1_api_hooks = arg.api;
512
513 return configs;
514 fail:
515 if (configs)
516 FREE(configs);
517 dri_destroy_screen_helper(screen);
518 FREE(screen);
519 return NULL;
520 }