Merge commit 'origin/tgsi-simplify-ext'
[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_X8R8G8B8_UNORM;
105 template.last_level = 0;
106 template.width0 = width;
107 template.height0 = height;
108 template.depth0 = 1;
109 pf_get_block(template.format, &template.block);
110 template.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
111
112 tex = vpipe->screen->texture_create(vpipe->screen, &template);
113 if (!tex)
114 return false;
115
116 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
117 PIPE_BUFFER_USAGE_GPU_READ |
118 PIPE_BUFFER_USAGE_GPU_WRITE);
119 pipe_texture_reference(&tex, NULL);
120
121 if (!*backbuffer)
122 return false;
123
124 /* Clear the backbuffer in case the video doesn't cover the whole window */
125 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
126 vpipe->clear_surface(vpipe, 0, 0, width, height, 0, *backbuffer);
127
128 return true;
129 }
130
131 static void
132 MacroBlocksToPipe(const XvMCMacroBlockArray *xvmc_macroblocks,
133 const XvMCBlockArray *xvmc_blocks,
134 unsigned int first_macroblock,
135 unsigned int num_macroblocks,
136 struct pipe_mpeg12_macroblock *pipe_macroblocks)
137 {
138 unsigned int i, j, k, l;
139 XvMCMacroBlock *xvmc_mb;
140
141 assert(xvmc_macroblocks);
142 assert(xvmc_blocks);
143 assert(pipe_macroblocks);
144 assert(num_macroblocks);
145
146 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
147
148 for (i = 0; i < num_macroblocks; ++i) {
149 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
150 pipe_macroblocks->mbx = xvmc_mb->x;
151 pipe_macroblocks->mby = xvmc_mb->y;
152 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
153 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
154 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
155 /* Get rid of Valgrind 'undefined' warnings */
156 else
157 pipe_macroblocks->mo_type = -1;
158 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
159 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
160
161 for (j = 0; j < 2; ++j)
162 for (k = 0; k < 2; ++k)
163 for (l = 0; l < 2; ++l)
164 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
165
166 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
167 pipe_macroblocks->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
168
169 ++pipe_macroblocks;
170 ++xvmc_mb;
171 }
172 }
173
174 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
175 {
176 XvMCContextPrivate *context_priv;
177 struct pipe_video_context *vpipe;
178 XvMCSurfacePrivate *surface_priv;
179 struct pipe_video_surface *vsfc;
180
181 assert(dpy);
182
183 if (!context)
184 return XvMCBadContext;
185 if (!surface)
186 return XvMCBadSurface;
187
188 context_priv = context->privData;
189 vpipe = context_priv->vpipe;
190
191 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
192 if (!surface_priv)
193 return BadAlloc;
194
195 vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
196 vpipe->width, vpipe->height);
197 if (!vsfc) {
198 FREE(surface_priv);
199 return BadAlloc;
200 }
201
202 surface_priv->pipe_vsfc = vsfc;
203 surface_priv->context = context;
204
205 surface->surface_id = XAllocID(dpy);
206 surface->context_id = context->context_id;
207 surface->surface_type_id = context->surface_type_id;
208 surface->width = context->width;
209 surface->height = context->height;
210 surface->privData = surface_priv;
211
212 SyncHandle();
213
214 return Success;
215 }
216
217 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
218 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
219 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
220 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
221 )
222 {
223 struct pipe_video_context *vpipe;
224 struct pipe_surface *t_vsfc;
225 struct pipe_surface *p_vsfc;
226 struct pipe_surface *f_vsfc;
227 XvMCContextPrivate *context_priv;
228 XvMCSurfacePrivate *target_surface_priv;
229 XvMCSurfacePrivate *past_surface_priv;
230 XvMCSurfacePrivate *future_surface_priv;
231 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
232
233 assert(dpy);
234
235 if (!context || !context->privData)
236 return XvMCBadContext;
237 if (!target_surface || !target_surface->privData)
238 return XvMCBadSurface;
239
240 if (picture_structure != XVMC_TOP_FIELD &&
241 picture_structure != XVMC_BOTTOM_FIELD &&
242 picture_structure != XVMC_FRAME_PICTURE)
243 return BadValue;
244 /* Bkwd pred equivalent to fwd (past && !future) */
245 if (future_surface && !past_surface)
246 return BadMatch;
247
248 assert(context->context_id == target_surface->context_id);
249 assert(!past_surface || context->context_id == past_surface->context_id);
250 assert(!future_surface || context->context_id == future_surface->context_id);
251
252 assert(macroblocks);
253 assert(blocks);
254
255 assert(macroblocks->context_id == context->context_id);
256 assert(blocks->context_id == context->context_id);
257
258 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
259
260 target_surface_priv = target_surface->privData;
261 past_surface_priv = past_surface ? past_surface->privData : NULL;
262 future_surface_priv = future_surface ? future_surface->privData : NULL;
263
264 assert(target_surface_priv->context == context);
265 assert(!past_surface || past_surface_priv->context == context);
266 assert(!future_surface || future_surface_priv->context == context);
267
268 context_priv = context->privData;
269 vpipe = context_priv->vpipe;
270
271 t_vsfc = target_surface_priv->pipe_vsfc;
272 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
273 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
274
275 MacroBlocksToPipe(macroblocks, blocks, first_macroblock,
276 num_macroblocks, pipe_macroblocks);
277
278 vpipe->set_decode_target(vpipe, t_vsfc);
279 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
280 &pipe_macroblocks->base, target_surface_priv->render_fence);
281
282 return Success;
283 }
284
285 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
286 {
287 assert(dpy);
288
289 if (!surface)
290 return XvMCBadSurface;
291
292 return Success;
293 }
294
295 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
296 {
297 assert(dpy);
298
299 if (!surface)
300 return XvMCBadSurface;
301
302 return Success;
303 }
304
305 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
306 short srcx, short srcy, unsigned short srcw, unsigned short srch,
307 short destx, short desty, unsigned short destw, unsigned short desth,
308 int flags)
309 {
310 Window root;
311 int x, y;
312 unsigned int width, height;
313 unsigned int border_width;
314 unsigned int depth;
315 struct pipe_video_context *vpipe;
316 XvMCSurfacePrivate *surface_priv;
317 XvMCContextPrivate *context_priv;
318 XvMCContext *context;
319 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
320 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
321
322 assert(dpy);
323
324 if (!surface || !surface->privData)
325 return XvMCBadSurface;
326
327 if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
328 return BadDrawable;
329
330 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
331 assert(srcx + srcw - 1 < surface->width);
332 assert(srcy + srch - 1 < surface->height);
333 /*
334 * Some apps (mplayer) hit these asserts because they call
335 * this function after the window has been resized by the WM
336 * but before they've handled the corresponding XEvent and
337 * know about the new dimensions. The output should be clipped
338 * until the app updates destw and desth.
339 */
340 /*
341 assert(destx + destw - 1 < width);
342 assert(desty + desth - 1 < height);
343 */
344
345 surface_priv = surface->privData;
346 context = surface_priv->context;
347 context_priv = context->privData;
348 vpipe = context_priv->vpipe;
349
350 if (!CreateOrResizeBackBuffer(vpipe, width, height, &context_priv->backbuffer))
351 return BadAlloc;
352
353 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
354 context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
355
356 vl_video_bind_drawable(vpipe, drawable);
357
358 vpipe->screen->flush_frontbuffer
359 (
360 vpipe->screen,
361 context_priv->backbuffer,
362 vpipe->priv
363 );
364
365 return Success;
366 }
367
368 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
369 {
370 assert(dpy);
371
372 if (!surface)
373 return XvMCBadSurface;
374
375 assert(status);
376
377 *status = 0;
378
379 return Success;
380 }
381
382 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
383 {
384 XvMCSurfacePrivate *surface_priv;
385
386 assert(dpy);
387
388 if (!surface || !surface->privData)
389 return XvMCBadSurface;
390
391 surface_priv = surface->privData;
392 pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
393 FREE(surface_priv);
394 surface->privData = NULL;
395
396 return Success;
397 }
398
399 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
400 {
401 assert(dpy);
402
403 if (!surface || !surface->privData)
404 return XvMCBadSurface;
405
406 /* No op, only for overlaid rendering */
407
408 return Success;
409 }