dri2.c revision 4c7001462607e6e99e474d6271dd481d3f8f201c
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.9
4 *
5 * Copyright 2009, VMware, Inc.
6 * All Rights Reserved.
7 * Copyright (C) 2010 LunarG Inc.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 *    Keith Whitwell <keithw@vmware.com>
28 *    Jakob Bornecrantz <wallbraker@gmail.com>
29 *    Chia-I Wu <olv@lunarg.com>
30 */
31
32#include "util/u_memory.h"
33#include "util/u_inlines.h"
34#include "util/u_format.h"
35#include "util/u_debug.h"
36#include "state_tracker/drm_driver.h"
37
38#include "dri_screen.h"
39#include "dri_context.h"
40#include "dri_drawable.h"
41
42/**
43 * DRI2 flush extension.
44 */
45static void
46dri2_flush_drawable(__DRIdrawable *draw)
47{
48}
49
50static void
51dri2_invalidate_drawable(__DRIdrawable *dPriv)
52{
53   struct dri_drawable *drawable = dri_drawable(dPriv);
54   struct dri_context *ctx = dri_context(dPriv->driContextPriv);
55
56   dri2InvalidateDrawable(dPriv);
57   drawable->dPriv->lastStamp = *drawable->dPriv->pStamp;
58
59   if (ctx)
60      ctx->st->notify_invalid_framebuffer(ctx->st, &drawable->base);
61}
62
63static const __DRI2flushExtension dri2FlushExtension = {
64    { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
65    dri2_flush_drawable,
66    dri2_invalidate_drawable,
67};
68
69/**
70 * Retrieve __DRIbuffer from the DRI loader.
71 */
72static __DRIbuffer *
73dri2_drawable_get_buffers(struct dri_drawable *drawable,
74                          const enum st_attachment_type *statts,
75                          unsigned *count)
76{
77   __DRIdrawable *dri_drawable = drawable->dPriv;
78   struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
79   boolean with_format;
80   __DRIbuffer *buffers;
81   int num_buffers;
82   unsigned attachments[10];
83   unsigned num_attachments, i;
84
85   assert(loader);
86   with_format = dri_with_format(drawable->sPriv);
87
88   num_attachments = 0;
89
90   /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
91   if (!with_format)
92      attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
93
94   for (i = 0; i < *count; i++) {
95      enum pipe_format format;
96      unsigned bind;
97      int att, bpp;
98
99      dri_drawable_get_format(drawable, statts[i], &format, &bind);
100      if (format == PIPE_FORMAT_NONE)
101         continue;
102
103      switch (statts[i]) {
104      case ST_ATTACHMENT_FRONT_LEFT:
105         /* already added */
106         if (!with_format)
107            continue;
108         att = __DRI_BUFFER_FRONT_LEFT;
109         break;
110      case ST_ATTACHMENT_BACK_LEFT:
111         att = __DRI_BUFFER_BACK_LEFT;
112         break;
113      case ST_ATTACHMENT_FRONT_RIGHT:
114         att = __DRI_BUFFER_FRONT_RIGHT;
115         break;
116      case ST_ATTACHMENT_BACK_RIGHT:
117         att = __DRI_BUFFER_BACK_RIGHT;
118         break;
119      case ST_ATTACHMENT_DEPTH_STENCIL:
120         att = __DRI_BUFFER_DEPTH_STENCIL;
121         break;
122      default:
123         att = -1;
124         break;
125      }
126
127      bpp = util_format_get_blocksizebits(format);
128
129      if (att >= 0) {
130         attachments[num_attachments++] = att;
131         if (with_format) {
132            attachments[num_attachments++] = bpp;
133         }
134      }
135   }
136
137   if (with_format) {
138      num_attachments /= 2;
139      buffers = loader->getBuffersWithFormat(dri_drawable,
140            &dri_drawable->w, &dri_drawable->h,
141            attachments, num_attachments,
142            &num_buffers, dri_drawable->loaderPrivate);
143   }
144   else {
145      buffers = loader->getBuffers(dri_drawable,
146            &dri_drawable->w, &dri_drawable->h,
147            attachments, num_attachments,
148            &num_buffers, dri_drawable->loaderPrivate);
149   }
150
151   if (buffers) {
152      /* set one cliprect to cover the whole dri_drawable */
153      dri_drawable->x = 0;
154      dri_drawable->y = 0;
155      dri_drawable->backX = 0;
156      dri_drawable->backY = 0;
157      dri_drawable->numClipRects = 1;
158      dri_drawable->pClipRects[0].x1 = 0;
159      dri_drawable->pClipRects[0].y1 = 0;
160      dri_drawable->pClipRects[0].x2 = dri_drawable->w;
161      dri_drawable->pClipRects[0].y2 = dri_drawable->h;
162      dri_drawable->numBackClipRects = 1;
163      dri_drawable->pBackClipRects[0].x1 = 0;
164      dri_drawable->pBackClipRects[0].y1 = 0;
165      dri_drawable->pBackClipRects[0].x2 = dri_drawable->w;
166      dri_drawable->pBackClipRects[0].y2 = dri_drawable->h;
167
168      *count = num_buffers;
169   }
170
171   return buffers;
172}
173
174/**
175 * Process __DRIbuffer and convert them into pipe_resources.
176 */
177static void
178dri2_drawable_process_buffers(struct dri_drawable *drawable,
179                              __DRIbuffer *buffers, unsigned count)
180{
181   struct dri_screen *screen = dri_screen(drawable->sPriv);
182   __DRIdrawable *dri_drawable = drawable->dPriv;
183   struct pipe_resource templ;
184   struct winsys_handle whandle;
185   boolean have_depth = FALSE;
186   unsigned i, bind;
187
188   if (drawable->old_num == count &&
189       drawable->old_w == dri_drawable->w &&
190       drawable->old_h == dri_drawable->h &&
191       memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0)
192      return;
193
194   for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
195      pipe_resource_reference(&drawable->textures[i], NULL);
196
197   memset(&templ, 0, sizeof(templ));
198   templ.target = screen->target;
199   templ.last_level = 0;
200   templ.width0 = dri_drawable->w;
201   templ.height0 = dri_drawable->h;
202   templ.depth0 = 1;
203   templ.array_size = 1;
204
205   memset(&whandle, 0, sizeof(whandle));
206
207   for (i = 0; i < count; i++) {
208      __DRIbuffer *buf = &buffers[i];
209      enum st_attachment_type statt;
210      enum pipe_format format;
211
212      switch (buf->attachment) {
213      case __DRI_BUFFER_FRONT_LEFT:
214         if (!screen->auto_fake_front) {
215            statt = ST_ATTACHMENT_INVALID;
216            break;
217         }
218         /* fallthrough */
219      case __DRI_BUFFER_FAKE_FRONT_LEFT:
220         statt = ST_ATTACHMENT_FRONT_LEFT;
221         break;
222      case __DRI_BUFFER_BACK_LEFT:
223         statt = ST_ATTACHMENT_BACK_LEFT;
224         break;
225      case __DRI_BUFFER_DEPTH:
226      case __DRI_BUFFER_DEPTH_STENCIL:
227      case __DRI_BUFFER_STENCIL:
228         /* use only the first depth/stencil buffer */
229         if (!have_depth) {
230            have_depth = TRUE;
231            statt = ST_ATTACHMENT_DEPTH_STENCIL;
232         }
233         else {
234            statt = ST_ATTACHMENT_INVALID;
235         }
236         break;
237      default:
238         statt = ST_ATTACHMENT_INVALID;
239         break;
240      }
241
242      dri_drawable_get_format(drawable, statt, &format, &bind);
243      if (statt == ST_ATTACHMENT_INVALID || format == PIPE_FORMAT_NONE)
244         continue;
245
246      templ.format = format;
247      templ.bind = bind;
248      whandle.handle = buf->name;
249      whandle.stride = buf->pitch;
250
251      drawable->textures[statt] =
252         screen->base.screen->resource_from_handle(screen->base.screen,
253               &templ, &whandle);
254   }
255
256   drawable->old_num = count;
257   drawable->old_w = dri_drawable->w;
258   drawable->old_h = dri_drawable->h;
259   memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
260}
261
262/*
263 * Backend functions for st_framebuffer interface.
264 */
265
266static void
267dri2_allocate_textures(struct dri_drawable *drawable,
268                       const enum st_attachment_type *statts,
269                       unsigned count)
270{
271   __DRIbuffer *buffers;
272   unsigned num_buffers = count;
273
274   buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
275   if (buffers)
276      dri2_drawable_process_buffers(drawable, buffers, num_buffers);
277}
278
279static void
280dri2_flush_frontbuffer(struct dri_drawable *drawable,
281                       enum st_attachment_type statt)
282{
283   __DRIdrawable *dri_drawable = drawable->dPriv;
284   struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
285
286   if (loader->flushFrontBuffer == NULL)
287      return;
288
289   if (statt == ST_ATTACHMENT_FRONT_LEFT) {
290      loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
291   }
292}
293
294static __DRIimage *
295dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
296{
297   __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
298   __DRIimage *img;
299
300   if (!loader->lookupEGLImage)
301      return NULL;
302
303   img = loader->lookupEGLImage(screen->sPriv,
304				handle, screen->sPriv->loaderPrivate);
305
306   return img;
307}
308
309static __DRIimage *
310dri2_create_image_from_name(__DRIscreen *_screen,
311                            int width, int height, int format,
312                            int name, int pitch, void *loaderPrivate)
313{
314   struct dri_screen *screen = dri_screen(_screen);
315   __DRIimage *img;
316   struct pipe_resource templ;
317   struct winsys_handle whandle;
318   unsigned tex_usage;
319   enum pipe_format pf;
320
321   tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
322
323   switch (format) {
324   case __DRI_IMAGE_FORMAT_RGB565:
325      pf = PIPE_FORMAT_B5G6R5_UNORM;
326      break;
327   case __DRI_IMAGE_FORMAT_XRGB8888:
328      pf = PIPE_FORMAT_B8G8R8X8_UNORM;
329      break;
330   case __DRI_IMAGE_FORMAT_ARGB8888:
331      pf = PIPE_FORMAT_B8G8R8A8_UNORM;
332      break;
333   default:
334      pf = PIPE_FORMAT_NONE;
335      break;
336   }
337   if (pf == PIPE_FORMAT_NONE)
338      return NULL;
339
340   img = CALLOC_STRUCT(__DRIimageRec);
341   if (!img)
342      return NULL;
343
344   memset(&templ, 0, sizeof(templ));
345   templ.bind = tex_usage;
346   templ.format = pf;
347   templ.target = screen->target;
348   templ.last_level = 0;
349   templ.width0 = width;
350   templ.height0 = height;
351   templ.depth0 = 1;
352   templ.array_size = 1;
353
354   memset(&whandle, 0, sizeof(whandle));
355   whandle.handle = name;
356   whandle.stride = pitch * util_format_get_blocksize(pf);
357
358   img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
359         &templ, &whandle);
360   if (!img->texture) {
361      FREE(img);
362      return NULL;
363   }
364
365   img->level = 0;
366   img->layer = 0;
367   img->loader_private = loaderPrivate;
368
369   return img;
370}
371
372static __DRIimage *
373dri2_create_image_from_renderbuffer(__DRIcontext *context,
374				    int renderbuffer, void *loaderPrivate)
375{
376   struct dri_context *ctx = dri_context(context->driverPrivate);
377
378   if (!ctx->st->get_resource_for_egl_image)
379      return NULL;
380
381   /* TODO */
382   return NULL;
383}
384
385static __DRIimage *
386dri2_create_image(__DRIscreen *_screen,
387                   int width, int height, int format,
388                   unsigned int use, void *loaderPrivate)
389{
390   struct dri_screen *screen = dri_screen(_screen);
391   __DRIimage *img;
392   struct pipe_resource templ;
393   unsigned tex_usage;
394   enum pipe_format pf;
395
396   tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
397
398   switch (format) {
399   case __DRI_IMAGE_FORMAT_RGB565:
400      pf = PIPE_FORMAT_B5G6R5_UNORM;
401      break;
402   case __DRI_IMAGE_FORMAT_XRGB8888:
403      pf = PIPE_FORMAT_B8G8R8X8_UNORM;
404      break;
405   case __DRI_IMAGE_FORMAT_ARGB8888:
406      pf = PIPE_FORMAT_B8G8R8A8_UNORM;
407      break;
408   default:
409      pf = PIPE_FORMAT_NONE;
410      break;
411   }
412   if (pf == PIPE_FORMAT_NONE)
413      return NULL;
414
415   img = CALLOC_STRUCT(__DRIimageRec);
416   if (!img)
417      return NULL;
418
419   memset(&templ, 0, sizeof(templ));
420   templ.bind = tex_usage;
421   templ.format = pf;
422   templ.target = PIPE_TEXTURE_2D;
423   templ.last_level = 0;
424   templ.width0 = width;
425   templ.height0 = height;
426   templ.depth0 = 1;
427
428   img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
429   if (!img->texture) {
430      FREE(img);
431      return NULL;
432   }
433
434   img->level = 0;
435   img->layer = 0;
436
437   img->loader_private = loaderPrivate;
438   return img;
439}
440
441static GLboolean
442dri2_query_image(__DRIimage *image, int attrib, int *value)
443{
444   struct winsys_handle whandle;
445   memset(&whandle, 0, sizeof(whandle));
446
447   switch (attrib) {
448   case __DRI_IMAGE_ATTRIB_STRIDE:
449      image->texture->screen->resource_get_handle(image->texture->screen,
450            image->texture, &whandle);
451      *value = whandle.stride;
452      return GL_TRUE;
453   case __DRI_IMAGE_ATTRIB_HANDLE:
454      whandle.type = DRM_API_HANDLE_TYPE_KMS;
455      image->texture->screen->resource_get_handle(image->texture->screen,
456         image->texture, &whandle);
457      *value = whandle.handle;
458      return GL_TRUE;
459   case __DRI_IMAGE_ATTRIB_NAME:
460      whandle.type = DRM_API_HANDLE_TYPE_SHARED;
461      image->texture->screen->resource_get_handle(image->texture->screen,
462         image->texture, &whandle);
463      *value = whandle.handle;
464      return GL_TRUE;
465   default:
466      return GL_FALSE;
467   }
468}
469
470static void
471dri2_destroy_image(__DRIimage *img)
472{
473   pipe_resource_reference(&img->texture, NULL);
474   FREE(img);
475}
476
477static struct __DRIimageExtensionRec dri2ImageExtension = {
478    { __DRI_IMAGE, __DRI_IMAGE_VERSION },
479    dri2_create_image_from_name,
480    dri2_create_image_from_renderbuffer,
481    dri2_destroy_image,
482    dri2_create_image,
483    dri2_query_image,
484};
485
486/*
487 * Backend function init_screen.
488 */
489
490static const __DRIextension *dri_screen_extensions[] = {
491   &driReadDrawableExtension,
492   &driCopySubBufferExtension.base,
493   &driSwapControlExtension.base,
494   &driMediaStreamCounterExtension.base,
495   &driTexBufferExtension.base,
496   &dri2FlushExtension.base,
497   &dri2ImageExtension.base,
498   &dri2ConfigQueryExtension.base,
499   NULL
500};
501
502/**
503 * This is the driver specific part of the createNewScreen entry point.
504 *
505 * Returns the struct gl_config supported by this driver.
506 */
507static const __DRIconfig **
508dri2_init_screen(__DRIscreen * sPriv)
509{
510   const __DRIconfig **configs;
511   struct dri_screen *screen;
512   struct pipe_screen *pscreen;
513
514   screen = CALLOC_STRUCT(dri_screen);
515   if (!screen)
516      return NULL;
517
518   screen->sPriv = sPriv;
519   screen->fd = sPriv->fd;
520
521   sPriv->private = (void *)screen;
522   sPriv->extensions = dri_screen_extensions;
523
524   pscreen = driver_descriptor.create_screen(screen->fd);
525   /* dri_init_screen_helper checks pscreen for us */
526
527   configs = dri_init_screen_helper(screen, pscreen, 32);
528   if (!configs)
529      goto fail;
530
531   sPriv->api_mask = 0;
532   if (screen->st_api->profile_mask & ST_PROFILE_DEFAULT_MASK)
533      sPriv->api_mask |= 1 << __DRI_API_OPENGL;
534   if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES1_MASK)
535      sPriv->api_mask |= 1 << __DRI_API_GLES;
536   if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES2_MASK)
537      sPriv->api_mask |= 1 << __DRI_API_GLES2;
538
539   screen->auto_fake_front = dri_with_format(sPriv);
540   screen->broken_invalidate = !sPriv->dri2.useInvalidate;
541   screen->lookup_egl_image = dri2_lookup_egl_image;
542
543   return configs;
544fail:
545   dri_destroy_screen_helper(screen);
546   FREE(screen);
547   return NULL;
548}
549
550static boolean
551dri2_create_context(gl_api api, const struct gl_config * visual,
552                    __DRIcontext * cPriv, void *sharedContextPrivate)
553{
554   struct dri_context *ctx = NULL;
555
556   if (!dri_create_context(api, visual, cPriv, sharedContextPrivate))
557      return FALSE;
558
559   ctx = cPriv->driverPrivate;
560
561   return TRUE;
562}
563
564static boolean
565dri2_create_buffer(__DRIscreen * sPriv,
566                   __DRIdrawable * dPriv,
567                   const struct gl_config * visual, boolean isPixmap)
568{
569   struct dri_drawable *drawable = NULL;
570
571   if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
572      return FALSE;
573
574   drawable = dPriv->driverPrivate;
575
576   drawable->allocate_textures = dri2_allocate_textures;
577   drawable->flush_frontbuffer = dri2_flush_frontbuffer;
578
579   return TRUE;
580}
581
582/**
583 * DRI driver virtual function table.
584 *
585 * DRI versions differ in their implementation of init_screen and swap_buffers.
586 */
587const struct __DriverAPIRec driDriverAPI = {
588   .InitScreen = NULL,
589   .InitScreen2 = dri2_init_screen,
590   .DestroyScreen = dri_destroy_screen,
591   .CreateContext = dri2_create_context,
592   .DestroyContext = dri_destroy_context,
593   .CreateBuffer = dri2_create_buffer,
594   .DestroyBuffer = dri_destroy_buffer,
595   .MakeCurrent = dri_make_current,
596   .UnbindContext = dri_unbind_context,
597
598   .GetSwapInfo = NULL,
599   .GetDrawableMSC = NULL,
600   .WaitForMSC = NULL,
601
602   .SwapBuffers = NULL,
603   .CopySubBuffer = NULL,
604};
605
606/* This is the table of extensions that the loader will dlsym() for. */
607PUBLIC const __DRIextension *__driDriverExtensions[] = {
608    &driCoreExtension.base,
609    &driLegacyExtension.base,
610    &driDRI2Extension.base,
611    NULL
612};
613
614/* vim: set sw=3 ts=8 sts=3 expandtab: */
615