Merge branch '7.8'
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / surface.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 TUNGSTEN GRAPHICS 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 #include <assert.h>
29 #include <X11/Xlibint.h>
30 #include <pipe/p_video_context.h>
31 #include <pipe/p_video_state.h>
32 #include <pipe/p_state.h>
33 #include <util/u_memory.h>
34 #include "xvmc_private.h"
35
36 static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
37 {
38 if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
39 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
40 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
41 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
42 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
43 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
44 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
45 return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
46
47 assert(0);
48
49 return -1;
50 }
51
52 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
53 {
54 switch (xvmc_pic) {
55 case XVMC_TOP_FIELD:
56 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
57 case XVMC_BOTTOM_FIELD:
58 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
59 case XVMC_FRAME_PICTURE:
60 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
61 default:
62 assert(0);
63 }
64
65 return -1;
66 }
67
68 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
69 {
70 switch (xvmc_motion_type) {
71 case XVMC_PREDICTION_FRAME:
72 return xvmc_dct_type == XVMC_DCT_TYPE_FIELD ?
73 PIPE_MPEG12_MOTION_TYPE_16x8 : PIPE_MPEG12_MOTION_TYPE_FRAME;
74 case XVMC_PREDICTION_FIELD:
75 return PIPE_MPEG12_MOTION_TYPE_FIELD;
76 case XVMC_PREDICTION_DUAL_PRIME:
77 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
78 default:
79 assert(0);
80 }
81
82 return -1;
83 }
84
85 static bool
86 CreateOrResizeBackBuffer(struct pipe_video_context *vpipe, unsigned int width, unsigned int height,
87 struct pipe_surface **backbuffer)
88 {
89 struct pipe_texture template;
90 struct pipe_texture *tex;
91
92 assert(vpipe);
93
94 if (*backbuffer) {
95 if ((*backbuffer)->width != width || (*backbuffer)->height != height)
96 pipe_surface_reference(backbuffer, NULL);
97 else
98 return true;
99 }
100
101 memset(&template, 0, sizeof(struct pipe_texture));
102 template.target = PIPE_TEXTURE_2D;
103 /* XXX: Needs to match the drawable's format? */
104 template.format = PIPE_FORMAT_B8G8R8X8_UNORM;
105 template.last_level = 0;
106 template.width0 = width;
107 template.height0 = height;
108 template.depth0 = 1;
109 template.tex_usage = PIPE_TEXTURE_USAGE_SHARED;
110
111 tex = vpipe->screen->texture_create(vpipe->screen, &template);
112 if (!tex)
113 return false;
114
115 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
116 PIPE_BUFFER_USAGE_GPU_READ |
117 PIPE_BUFFER_USAGE_GPU_WRITE);
118 pipe_texture_reference(&tex, NULL);
119
120 if (!*backbuffer)
121 return false;
122
123 /* Clear the backbuffer in case the video doesn't cover the whole window */
124 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
125 vpipe->clear_surface(vpipe, 0, 0, width, height, 0, *backbuffer);
126
127 return true;
128 }
129
130 static void
131 MacroBlocksToPipe(const XvMCMacroBlockArray *xvmc_macroblocks,
132 const XvMCBlockArray *xvmc_blocks,
133 unsigned int first_macroblock,
134 unsigned int num_macroblocks,
135 struct pipe_mpeg12_macroblock *pipe_macroblocks)
136 {
137 unsigned int i, j, k, l;
138 XvMCMacroBlock *xvmc_mb;
139
140 assert(xvmc_macroblocks);
141 assert(xvmc_blocks);
142 assert(pipe_macroblocks);
143 assert(num_macroblocks);
144
145 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
146
147 for (i = 0; i < num_macroblocks; ++i) {
148 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
149 pipe_macroblocks->mbx = xvmc_mb->x;
150 pipe_macroblocks->mby = xvmc_mb->y;
151 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
152 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
153 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
154 /* Get rid of Valgrind 'undefined' warnings */
155 else
156 pipe_macroblocks->mo_type = -1;
157 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
158 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
159
160 for (j = 0; j < 2; ++j)
161 for (k = 0; k < 2; ++k)
162 for (l = 0; l < 2; ++l)
163 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
164
165 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
166 pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
167
168 ++pipe_macroblocks;
169 ++xvmc_mb;
170 }
171 }
172
173 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
174 {
175 XvMCContextPrivate *context_priv;
176 struct pipe_video_context *vpipe;
177 XvMCSurfacePrivate *surface_priv;
178 struct pipe_video_surface *vsfc;
179
180 assert(dpy);
181
182 if (!context)
183 return XvMCBadContext;
184 if (!surface)
185 return XvMCBadSurface;
186
187 context_priv = context->privData;
188 vpipe = context_priv->vpipe;
189
190 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
191 if (!surface_priv)
192 return BadAlloc;
193
194 vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
195 vpipe->width, vpipe->height);
196 if (!vsfc) {
197 FREE(surface_priv);
198 return BadAlloc;
199 }
200
201 surface_priv->pipe_vsfc = vsfc;
202 surface_priv->context = context;
203
204 surface->surface_id = XAllocID(dpy);
205 surface->context_id = context->context_id;
206 surface->surface_type_id = context->surface_type_id;
207 surface->width = context->width;
208 surface->height = context->height;
209 surface->privData = surface_priv;
210
211 SyncHandle();
212
213 return Success;
214 }
215
216 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
217 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
218 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
219 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
220 )
221 {
222 struct pipe_video_context *vpipe;
223 struct pipe_surface *t_vsfc;
224 struct pipe_surface *p_vsfc;
225 struct pipe_surface *f_vsfc;
226 XvMCContextPrivate *context_priv;
227 XvMCSurfacePrivate *target_surface_priv;
228 XvMCSurfacePrivate *past_surface_priv;
229 XvMCSurfacePrivate *future_surface_priv;
230 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
231
232 assert(dpy);
233
234 if (!context || !context->privData)
235 return XvMCBadContext;
236 if (!target_surface || !target_surface->privData)
237 return XvMCBadSurface;
238
239 if (picture_structure != XVMC_TOP_FIELD &&
240 picture_structure != XVMC_BOTTOM_FIELD &&
241 picture_structure != XVMC_FRAME_PICTURE)
242 return BadValue;
243 /* Bkwd pred equivalent to fwd (past && !future) */
244 if (future_surface && !past_surface)
245 return BadMatch;
246
247 assert(context->context_id == target_surface->context_id);
248 assert(!past_surface || context->context_id == past_surface->context_id);
249 assert(!future_surface || context->context_id == future_surface->context_id);
250
251 assert(macroblocks);
252 assert(blocks);
253
254 assert(macroblocks->context_id == context->context_id);
255 assert(blocks->context_id == context->context_id);
256
257 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
258
259 target_surface_priv = target_surface->privData;
260 past_surface_priv = past_surface ? past_surface->privData : NULL;
261 future_surface_priv = future_surface ? future_surface->privData : NULL;
262
263 assert(target_surface_priv->context == context);
264 assert(!past_surface || past_surface_priv->context == context);
265 assert(!future_surface || future_surface_priv->context == context);
266
267 context_priv = context->privData;
268 vpipe = context_priv->vpipe;
269
270 t_vsfc = target_surface_priv->pipe_vsfc;
271 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
272 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
273
274 MacroBlocksToPipe(macroblocks, blocks, first_macroblock,
275 num_macroblocks, pipe_macroblocks);
276
277 vpipe->set_decode_target(vpipe, t_vsfc);
278 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
279 &pipe_macroblocks->base, target_surface_priv->render_fence);
280
281 return Success;
282 }
283
284 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
285 {
286 assert(dpy);
287
288 if (!surface)
289 return XvMCBadSurface;
290
291 return Success;
292 }
293
294 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
295 {
296 assert(dpy);
297
298 if (!surface)
299 return XvMCBadSurface;
300
301 return Success;
302 }
303
304 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
305 short srcx, short srcy, unsigned short srcw, unsigned short srch,
306 short destx, short desty, unsigned short destw, unsigned short desth,
307 int flags)
308 {
309 Window root;
310 int x, y;
311 unsigned int width, height;
312 unsigned int border_width;
313 unsigned int depth;
314 struct pipe_video_context *vpipe;
315 XvMCSurfacePrivate *surface_priv;
316 XvMCContextPrivate *context_priv;
317 XvMCContext *context;
318 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
319 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
320
321 assert(dpy);
322
323 if (!surface || !surface->privData)
324 return XvMCBadSurface;
325
326 if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
327 return BadDrawable;
328
329 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
330 assert(srcx + srcw - 1 < surface->width);
331 assert(srcy + srch - 1 < surface->height);
332 /*
333 * Some apps (mplayer) hit these asserts because they call
334 * this function after the window has been resized by the WM
335 * but before they've handled the corresponding XEvent and
336 * know about the new dimensions. The output should be clipped
337 * until the app updates destw and desth.
338 */
339 /*
340 assert(destx + destw - 1 < width);
341 assert(desty + desth - 1 < height);
342 */
343
344 surface_priv = surface->privData;
345 context = surface_priv->context;
346 context_priv = context->privData;
347 vpipe = context_priv->vpipe;
348
349 if (!CreateOrResizeBackBuffer(vpipe, width, height, &context_priv->backbuffer))
350 return BadAlloc;
351
352 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
353 context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
354
355 vl_video_bind_drawable(vpipe, drawable);
356
357 vpipe->screen->flush_frontbuffer
358 (
359 vpipe->screen,
360 context_priv->backbuffer,
361 vpipe->priv
362 );
363
364 return Success;
365 }
366
367 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
368 {
369 assert(dpy);
370
371 if (!surface)
372 return XvMCBadSurface;
373
374 assert(status);
375
376 *status = 0;
377
378 return Success;
379 }
380
381 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
382 {
383 XvMCSurfacePrivate *surface_priv;
384
385 assert(dpy);
386
387 if (!surface || !surface->privData)
388 return XvMCBadSurface;
389
390 surface_priv = surface->privData;
391 pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
392 FREE(surface_priv);
393 surface->privData = NULL;
394
395 return Success;
396 }
397
398 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
399 {
400 assert(dpy);
401
402 if (!surface || !surface->privData)
403 return XvMCBadSurface;
404
405 /* No op, only for overlaid rendering */
406
407 return Success;
408 }