aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/winsys/g3dvl/dri/dri_winsys.c
blob: b86f546dd61b47608168bde2b420150c452590ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**************************************************************************
 *
 * Copyright 2009 Younes Manton.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <vl_winsys.h>
#include <driclient.h>
#include <state_tracker/dri1_api.h>
#include <pipe/p_video_context.h>
#include <pipe/p_state.h>
#include <util/u_memory.h>

struct vl_dri_screen
{
   struct vl_screen base;
   Visual *visual;
   struct drm_api *api;
   dri_screen_t *dri_screen;
   dri_framebuffer_t dri_framebuf;
   struct dri1_api *api_hooks;
   boolean dri2;
};

struct vl_dri_context
{
   struct vl_context base;
   boolean is_locked;
   boolean lost_lock;
   drmLock *lock;
   dri_context_t *dri_context;
   int fd;
   struct pipe_video_context *vpipe;
   dri_drawable_t *drawable;
   struct pipe_surface *dri2_front;
};

static void
vl_dri_lock(void *priv)
{
   struct vl_dri_context *vl_dri_ctx = priv;
   drm_context_t hw_context;
   char ret = 0;

   assert(priv);

   hw_context = vl_dri_ctx->dri_context->drm_context;

   DRM_CAS(vl_dri_ctx->lock, hw_context, DRM_LOCK_HELD | hw_context, ret);
   if (ret) {
      drmGetLock(vl_dri_ctx->fd, hw_context, 0);
      vl_dri_ctx->lost_lock = TRUE;
   }
   vl_dri_ctx->is_locked = TRUE;
}

static void
vl_dri_unlock(void *priv)
{
   struct vl_dri_context *vl_dri_ctx = priv;
   drm_context_t hw_context;

   assert(priv);

   hw_context = vl_dri_ctx->dri_context->drm_context;

   vl_dri_ctx->is_locked = FALSE;
   DRM_UNLOCK(vl_dri_ctx->fd, vl_dri_ctx->lock, hw_context);
}

static boolean
vl_dri_is_locked(void *priv)
{
   struct vl_dri_context *vl_dri_ctx = priv;

   assert(priv);

   return vl_dri_ctx->is_locked;
}

static boolean
vl_dri_lost_lock(void *priv)
{
   struct vl_dri_context *vl_dri_ctx = priv;

   assert(priv);

   return vl_dri_ctx->lost_lock;
}

static void
vl_dri_clear_lost_lock(void *priv)
{
   struct vl_dri_context *vl_dri_ctx = priv;

   assert(priv);

   vl_dri_ctx->lost_lock = FALSE;
}

struct dri1_api_lock_funcs dri1_lf =
{
   .lock = vl_dri_lock,
   .unlock = vl_dri_unlock,
   .is_locked = vl_dri_is_locked,
   .is_lock_lost = vl_dri_lost_lock,
   .clear_lost_lock = vl_dri_clear_lost_lock
};

static void
vl_dri_copy_version(struct dri1_api_version *dst, dri_version_t *src)
{
   assert(src);
   assert(dst);
   dst->major = src->major;
   dst->minor = src->minor;
   dst->patch_level = src->patch;
}

static boolean
vl_dri_intersect_src_bbox(struct drm_clip_rect *dst, int dst_x, int dst_y,
                          const struct drm_clip_rect *src, const struct drm_clip_rect *bbox)
{
   int xy1;
   int xy2;

   assert(dst);
   assert(src);
   assert(bbox);

   xy1 = ((int)src->x1 > (int)bbox->x1 + dst_x) ? src->x1 :
      (int)bbox->x1 + dst_x;
   xy2 = ((int)src->x2 < (int)bbox->x2 + dst_x) ? src->x2 :
      (int)bbox->x2 + dst_x;
   if (xy1 >= xy2 || xy1 < 0)
      return FALSE;

   dst->x1 = xy1;
   dst->x2 = xy2;

   xy1 = ((int)src->y1 > (int)bbox->y1 + dst_y) ? src->y1 :
      (int)bbox->y1 + dst_y;
   xy2 = ((int)src->y2 < (int)bbox->y2 + dst_y) ? src->y2 :
      (int)bbox->y2 + dst_y;
   if (xy1 >= xy2 || xy1 < 0)
      return FALSE;

   dst->y1 = xy1;
   dst->y2 = xy2;
   return TRUE;
}

static void
vl_clip_copy(struct vl_dri_context *vl_dri_ctx,
             struct pipe_surface *dst,
             struct pipe_surface *src,
             const struct drm_clip_rect *src_bbox)
{
   struct pipe_video_context *vpipe;
   struct drm_clip_rect clip;
   struct drm_clip_rect *cur;
   int i;

   assert(vl_dri_ctx);
   assert(dst);
   assert(src);
   assert(src_bbox);

   vpipe = vl_dri_ctx->base.vpipe;

   assert(vl_dri_ctx->drawable->cliprects);
   assert(vl_dri_ctx->drawable->num_cliprects > 0);

   cur = vl_dri_ctx->drawable->cliprects;

   for (i = 0; i < vl_dri_ctx->drawable->num_cliprects; ++i) {
      if (vl_dri_intersect_src_bbox(&clip, vl_dri_ctx->drawable->x, vl_dri_ctx->drawable->y, cur++, src_bbox))
         vpipe->surface_copy
         (
            vpipe, dst, clip.x1, clip.y1, src,
            (int)clip.x1 - vl_dri_ctx->drawable->x,
            (int)clip.y1 - vl_dri_ctx->drawable->y,
            clip.x2 - clip.x1, clip.y2 - clip.y1
         );
   }
}

static void
vl_dri_update_drawables_locked(struct vl_dri_context *vl_dri_ctx)
{
   struct vl_dri_screen *vl_dri_scrn;

   assert(vl_dri_ctx);

   vl_dri_scrn = (struct vl_dri_screen*)vl_dri_ctx->base.vscreen;

   if (vl_dri_ctx->lost_lock) {
      vl_dri_ctx->lost_lock = FALSE;
      DRI_VALIDATE_DRAWABLE_INFO(vl_dri_scrn->dri_screen, vl_dri_ctx->drawable);
   }
}

static void
vl_dri_flush_frontbuffer(struct pipe_screen *screen,
                         struct pipe_surface *surf, void *context_private)
{
   struct vl_dri_context *vl_dri_ctx = (struct vl_dri_context*)context_private;
   struct vl_dri_screen *vl_dri_scrn;
   struct drm_clip_rect src_bbox;
   boolean save_lost_lock = FALSE;

   assert(screen);
   assert(surf);
   assert(context_private);

   vl_dri_scrn = (struct vl_dri_screen*)vl_dri_ctx->base.vscreen;

   vl_dri_lock(vl_dri_ctx);

   save_lost_lock = vl_dri_ctx->lost_lock;

   vl_dri_update_drawables_locked(vl_dri_ctx);

   if (vl_dri_ctx->drawable->cliprects) {
      src_bbox.x1 = 0;
      src_bbox.x2 = vl_dri_ctx->drawable->w;
      src_bbox.y1 = 0;
      src_bbox.y2 = vl_dri_ctx->drawable->h;

#if 0
      if (vl_dri_scrn->_api_hooks->present_locked)
         vl_dri_scrn->api_hooks->present_locked(pipe, surf,
                                                vl_dri_ctx->drawable->cliprects,
                                                vl_dri_ctx->drawable->num_cliprects,
                                                vl_dri_ctx->drawable->x, vl_dri_drawable->y,
                                                &bbox, NULL /*fence*/);
      else
#endif
      if (vl_dri_scrn->api_hooks->front_srf_locked) {
         struct pipe_surface *front = vl_dri_scrn->api_hooks->front_srf_locked(screen);

         if (front)
            vl_clip_copy(vl_dri_ctx, front, surf, &src_bbox);

         //st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
      }
   }

   vl_dri_ctx->lost_lock = save_lost_lock;

   vl_dri_unlock(vl_dri_ctx);
}

static struct pipe_surface*
vl_dri2_get_front(struct vl_dri_screen *vl_dri_scrn, Drawable drawable)
{
   int w, h;
   unsigned int attachments[1] = {DRI_BUFFER_FRONT_LEFT};
   int count;
   DRI2Buffer *dri2_front;
   struct pipe_resource template, *front_tex;
   struct pipe_surface *front_surf = NULL;

   assert(vl_dri_scrn);

   dri2_front = DRI2GetBuffers(vl_dri_scrn->dri_screen->display,
                               drawable, &w, &h, attachments, 1, &count);
   if (dri2_front) {
      struct winsys_handle dri2_front_handle =
      {
         .type = DRM_API_HANDLE_TYPE_SHARED,
         .handle = dri2_front->name,
         .stride = dri2_front->pitch
      };
      front_tex = vl_dri_scrn->base.pscreen->resource_from_handle(vl_dri_scrn->base.pscreen, &template, &dri2_front_handle);
      if (front_tex)
         front_surf = vl_dri_scrn->base.pscreen->get_tex_surface(vl_dri_scrn->base.pscreen,
                                                                 front_tex, 0, 0, 0,
                                                                 /*PIPE_BIND_RENDER_TARGET*/ PIPE_BIND_BLIT_DESTINATION);
      pipe_resource_reference(&front_tex, NULL);
   }

   return front_surf;
}

static void
vl_dri2_flush_frontbuffer(struct pipe_screen *screen,
                          struct pipe_surface *surf, void *context_private)
{
   struct vl_dri_context *vl_dri_ctx = (struct vl_dri_context*)context_private;
   struct vl_dri_screen *vl_dri_scrn;
   struct pipe_video_context *vpipe;

   assert(screen);
   assert(surf);
   assert(context_private);
   assert(vl_dri_ctx->dri2_front);

   vl_dri_scrn = (struct vl_dri_screen*)vl_dri_ctx->base.vscreen;
   vpipe = vl_dri_ctx->base.vpipe;

   /* XXX: Why not just render to fake front? */
   vpipe->surface_copy(vpipe, vl_dri_ctx->dri2_front, 0, 0, surf, 0, 0, surf->width, surf->height);

   //st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, fence);
}


Drawable
vl_video_bind_drawable(struct vl_context *vctx, Drawable drawable)
{
   struct vl_dri_context *vl_dri_ctx = (struct vl_dri_context*)vctx;
   struct vl_dri_screen *vl_dri_scrn;
   dri_drawable_t *dri_drawable;
   Drawable old_drawable = None;

   assert(vctx);

   if (vl_dri_ctx->drawable)
      old_drawable = vl_dri_ctx->drawable->x_drawable;

   if (drawable != old_drawable) {
      vl_dri_scrn = (struct vl_dri_screen*)vl_dri_ctx->base.vscreen;
      if (vl_dri_scrn->dri2) {
         /* XXX: Need dri2CreateDrawable()? */
         vl_dri_ctx->dri2_front = vl_dri2_get_front(vl_dri_scrn, drawable);
      }
      else {
         driCreateDrawable(vl_dri_scrn->dri_screen, drawable, &dri_drawable);
         vl_dri_ctx->drawable = dri_drawable;
      }
   }

   return old_drawable;
}

struct vl_screen*
vl_screen_create(Display *display, int screen)
{
   struct vl_dri_screen *vl_dri_scrn;
   struct dri1_create_screen_arg arg;

   assert(display);

   vl_dri_scrn = CALLOC_STRUCT(vl_dri_screen);
   if (!vl_dri_scrn)
      return NULL;

   /* Try DRI2 first */
   if (dri2CreateScreen(display, screen, &vl_dri_scrn->dri_screen)) {
      /* If not, try DRI */
      if (driCreateScreen(display, screen, &vl_dri_scrn->dri_screen, &vl_dri_scrn->dri_framebuf)) {
         /* Now what? */
         FREE(vl_dri_scrn);
         return NULL;
      }
      else {
         /* Got DRI */
         arg.base.mode = DRM_CREATE_DRI1;
         arg.lf = &dri1_lf;
         arg.ddx_info = vl_dri_scrn->dri_framebuf.private;
         arg.ddx_info_size = vl_dri_scrn->dri_framebuf.private_size;
         arg.sarea = vl_dri_scrn->dri_screen->sarea;
         vl_dri_copy_version(&arg.ddx_version, &vl_dri_scrn->dri_screen->ddx);
         vl_dri_copy_version(&arg.dri_version, &vl_dri_scrn->dri_screen->dri);
         vl_dri_copy_version(&arg.drm_version, &vl_dri_scrn->dri_screen->drm);
         arg.api = NULL;
         vl_dri_scrn->dri2 = FALSE;
      }
   }
   else {
      /* Got DRI2 */
      arg.base.mode = DRM_CREATE_NORMAL;
      vl_dri_scrn->dri2 = TRUE;
   }

   vl_dri_scrn->api = drm_api_create();
   if (!vl_dri_scrn->api) {
      FREE(vl_dri_scrn);
      return NULL;
   }

   vl_dri_scrn->base.pscreen = vl_dri_scrn->api->create_screen(vl_dri_scrn->api,
                                                               vl_dri_scrn->dri_screen->fd,
                                                               &arg.base);

   if (!vl_dri_scrn->base.pscreen) {
      FREE(vl_dri_scrn);
      return NULL;
   }

   if (!vl_dri_scrn->dri2) {
      vl_dri_scrn->visual = XDefaultVisual(display, screen);
      vl_dri_scrn->api_hooks = arg.api;
      vl_dri_scrn->base.format = vl_dri_scrn->api_hooks->front_srf_locked(vl_dri_scrn->base.pscreen)->format;
      vl_dri_scrn->base.pscreen->flush_frontbuffer = vl_dri_flush_frontbuffer;
   }
   else
      vl_dri_scrn->base.pscreen->flush_frontbuffer = vl_dri2_flush_frontbuffer;

   return &vl_dri_scrn->base;
}

void vl_screen_destroy(struct vl_screen *vscreen)
{
   struct vl_dri_screen *vl_dri_scrn = (struct vl_dri_screen*)vscreen;

   assert(vscreen);

   vl_dri_scrn->base.pscreen->destroy(vl_dri_scrn->base.pscreen);
   if (vl_dri_scrn->dri2)
      dri2DestroyScreen(vl_dri_scrn->dri_screen);
   else
      driDestroyScreen(vl_dri_scrn->dri_screen);
   FREE(vl_dri_scrn);
}

struct vl_context*
vl_video_create(struct vl_screen *vscreen,
                enum pipe_video_profile profile,
                enum pipe_video_chroma_format chroma_format,
                unsigned width, unsigned height)
{
   struct vl_dri_screen *vl_dri_scrn = (struct vl_dri_screen*)vscreen;
   struct vl_dri_context *vl_dri_ctx;

   vl_dri_ctx = CALLOC_STRUCT(vl_dri_context);
   if (!vl_dri_ctx)
      return NULL;

   /* XXX: Is default visual correct/sufficient here? */
   if (!vl_dri_scrn->dri2)
      driCreateContext(vl_dri_scrn->dri_screen, vl_dri_scrn->visual, &vl_dri_ctx->dri_context);

   if (!vscreen->pscreen->video_context_create) {
      debug_printf("[G3DVL] No video support found on %s/%s.\n",
                   vscreen->pscreen->get_vendor(vscreen->pscreen),
                   vscreen->pscreen->get_name(vscreen->pscreen));
      FREE(vl_dri_ctx);
      return NULL;
   }

   vl_dri_ctx->base.vpipe = vscreen->pscreen->video_context_create(vscreen->pscreen,
                                                                   profile, chroma_format,
                                                                   width, height,
                                                                   vl_dri_ctx->dri_context);

   if (!vl_dri_ctx->base.vpipe) {
      FREE(vl_dri_ctx);
      return NULL;
   }

   vl_dri_ctx->base.vpipe->priv = vl_dri_ctx;
   vl_dri_ctx->base.vscreen = vscreen;
   vl_dri_ctx->fd = vl_dri_scrn->dri_screen->fd;
   if (!vl_dri_scrn->dri2)
      vl_dri_ctx->lock = (drmLock*)&vl_dri_scrn->dri_screen->sarea->lock;

   return &vl_dri_ctx->base;
}

void vl_video_destroy(struct vl_context *vctx)
{
   struct vl_dri_context *vl_dri_ctx = (struct vl_dri_context*)vctx;

   assert(vctx);

   vl_dri_ctx->base.vpipe->destroy(vl_dri_ctx->base.vpipe);
   if (!((struct vl_dri_screen *)vctx->vscreen)->dri2)
      driDestroyContext(vl_dri_ctx->dri_context);
   FREE(vl_dri_ctx);
}