2 * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <X11/Xlibint.h>
34 #include "va_backend.h"
37 #include "va_dri2tokens.h"
38 #include "va_dricommon.h"
40 #define __DRI_BUFFER_FRONT_LEFT 0
41 #define __DRI_BUFFER_BACK_LEFT 1
42 #define __DRI_BUFFER_FRONT_RIGHT 2
43 #define __DRI_BUFFER_BACK_RIGHT 3
44 #define __DRI_BUFFER_DEPTH 4
45 #define __DRI_BUFFER_STENCIL 5
46 #define __DRI_BUFFER_ACCUM 6
47 #define __DRI_BUFFER_FAKE_FRONT_LEFT 7
48 #define __DRI_BUFFER_FAKE_FRONT_RIGHT 8
52 struct dri_drawable base;
53 union dri_buffer buffers[5];
61 static int gsDRI2SwapAvailable;
63 static struct dri_drawable *
64 dri2CreateDrawable(VADriverContextP ctx, XID x_drawable)
66 struct dri2_drawable *dri2_drawable;
68 dri2_drawable = calloc(1, sizeof(*dri2_drawable));
73 dri2_drawable->base.x_drawable = x_drawable;
74 dri2_drawable->base.x = 0;
75 dri2_drawable->base.y = 0;
76 VA_DRI2CreateDrawable(ctx->native_dpy, x_drawable);
78 return &dri2_drawable->base;
82 dri2DestroyDrawable(VADriverContextP ctx, struct dri_drawable *dri_drawable)
84 VA_DRI2DestroyDrawable(ctx->native_dpy, dri_drawable->x_drawable);
89 dri2SwapBuffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
91 struct dri2_drawable *dri2_drawable = (struct dri2_drawable *)dri_drawable;
95 if (dri2_drawable->has_backbuffer) {
96 if (gsDRI2SwapAvailable) {
98 VA_DRI2SwapBuffers(ctx->native_dpy, dri_drawable->x_drawable,
104 xrect.width = dri2_drawable->width;
105 xrect.height = dri2_drawable->height;
107 region = XFixesCreateRegion(ctx->native_dpy, &xrect, 1);
108 VA_DRI2CopyRegion(ctx->native_dpy, dri_drawable->x_drawable, region,
109 DRI2BufferFrontLeft, DRI2BufferBackLeft);
110 XFixesDestroyRegion(ctx->native_dpy, region);
115 static union dri_buffer *
116 dri2GetRenderingBuffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
118 struct dri2_drawable *dri2_drawable = (struct dri2_drawable *)dri_drawable;
121 unsigned int attachments[5];
122 VA_DRI2Buffer *buffers;
125 if (dri_drawable->is_window)
126 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
128 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
130 buffers = VA_DRI2GetBuffers(ctx->native_dpy, dri_drawable->x_drawable,
131 &dri2_drawable->width, &dri2_drawable->height,
132 attachments, i, &count);
136 dri2_drawable->has_backbuffer = 0;
138 for (i = 0; i < count; i++) {
139 dri2_drawable->buffers[i].dri2.attachment = buffers[i].attachment;
140 dri2_drawable->buffers[i].dri2.name = buffers[i].name;
141 dri2_drawable->buffers[i].dri2.pitch = buffers[i].pitch;
142 dri2_drawable->buffers[i].dri2.cpp = buffers[i].cpp;
143 dri2_drawable->buffers[i].dri2.flags = buffers[i].flags;
145 if (buffers[i].attachment == __DRI_BUFFER_BACK_LEFT) {
146 dri2_drawable->has_backbuffer = 1;
147 dri2_drawable->back_index = i;
150 if (buffers[i].attachment == __DRI_BUFFER_FRONT_LEFT)
151 dri2_drawable->front_index = i;
154 dri_drawable->width = dri2_drawable->width;
155 dri_drawable->height = dri2_drawable->height;
158 if (dri2_drawable->has_backbuffer)
159 return &dri2_drawable->buffers[dri2_drawable->back_index];
161 return &dri2_drawable->buffers[dri2_drawable->front_index];
165 dri2Close(VADriverContextP ctx)
167 struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
169 free_drawable_hashtable(ctx);
171 if (dri_state->base.fd >= 0)
172 close(dri_state->base.fd);
176 isDRI2Connected(VADriverContextP ctx, char **driver_name)
178 struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
182 char *device_name = NULL;
185 dri_state->base.fd = -1;
186 dri_state->base.auth_type = VA_NONE;
187 if (!VA_DRI2QueryExtension(ctx->native_dpy, &event_base, &error_base))
190 if (!VA_DRI2QueryVersion(ctx->native_dpy, &major, &minor))
194 if (!VA_DRI2Connect(ctx->native_dpy, RootWindow(ctx->native_dpy, ctx->x11_screen),
195 driver_name, &device_name))
198 dri_state->base.fd = open(device_name, O_RDWR);
200 if (dri_state->base.fd < 0)
203 if (drmGetMagic(dri_state->base.fd, &magic))
206 if (!VA_DRI2Authenticate(ctx->native_dpy, RootWindow(ctx->native_dpy, ctx->x11_screen),
210 dri_state->base.auth_type = VA_DRI2;
211 dri_state->createDrawable = dri2CreateDrawable;
212 dri_state->destroyDrawable = dri2DestroyDrawable;
213 dri_state->swapBuffer = dri2SwapBuffer;
214 dri_state->getRenderingBuffer = dri2GetRenderingBuffer;
215 dri_state->close = dri2Close;
216 gsDRI2SwapAvailable = (minor >= 2);
230 if (dri_state->base.fd >= 0)
231 close(dri_state->base.fd);
234 dri_state->base.fd = -1;