Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / subpicture.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 <X11/extensions/XvMClib.h>
31 #include <xorg/fourcc.h>
32 #include <vl_winsys.h>
33 #include <pipe/p_screen.h>
34 #include <pipe/p_video_context.h>
35 #include <pipe/p_state.h>
36 #include <util/u_memory.h>
37 #include <util/u_math.h>
38 #include "xvmc_private.h"
39
40 #define FOURCC_RGB 0x0000003
41
42 static enum pipe_format XvIDToPipe(int xvimage_id)
43 {
44 switch (xvimage_id) {
45 case FOURCC_RGB:
46 return PIPE_FORMAT_B8G8R8X8_UNORM;
47 default:
48 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
49 return PIPE_FORMAT_NONE;
50 }
51 }
52
53 static int PipeToComponentOrder(enum pipe_format format, char *component_order)
54 {
55 assert(component_order);
56
57 switch (format) {
58 case PIPE_FORMAT_B8G8R8X8_UNORM:
59 return 0;
60 default:
61 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format);
62 component_order[0] = 0;
63 component_order[1] = 0;
64 component_order[2] = 0;
65 component_order[3] = 0;
66 }
67
68 return 0;
69 }
70
71 static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id)
72 {
73 XvImageFormatValues *subpictures;
74 int num_subpics;
75 unsigned int i;
76
77 subpictures = XvMCListSubpictureTypes(dpy, port, surface_type_id, &num_subpics);
78 if (num_subpics < 1) {
79 if (subpictures)
80 XFree(subpictures);
81 return BadMatch;
82 }
83 if (!subpictures)
84 return BadAlloc;
85
86 for (i = 0; i < num_subpics; ++i) {
87 if (subpictures[i].id == xvimage_id) {
88 XVMC_MSG(XVMC_TRACE, "[XvMC] Found requested subpicture format.\n" \
89 "[XvMC] port=%u\n" \
90 "[XvMC] surface id=0x%08X\n" \
91 "[XvMC] image id=0x%08X\n" \
92 "[XvMC] type=%08X\n" \
93 "[XvMC] byte order=%08X\n" \
94 "[XvMC] bits per pixel=%u\n" \
95 "[XvMC] format=%08X\n" \
96 "[XvMC] num planes=%d\n",
97 port, surface_type_id, xvimage_id, subpictures[i].type, subpictures[i].byte_order,
98 subpictures[i].bits_per_pixel, subpictures[i].format, subpictures[i].num_planes);
99 if (subpictures[i].type == XvRGB) {
100 XVMC_MSG(XVMC_TRACE, "[XvMC] depth=%d\n" \
101 "[XvMC] red mask=0x%08X\n" \
102 "[XvMC] green mask=0x%08X\n" \
103 "[XvMC] blue mask=0x%08X\n",
104 subpictures[i].depth, subpictures[i].red_mask, subpictures[i].green_mask, subpictures[i].blue_mask);
105 }
106 else if (subpictures[i].type == XvYUV) {
107 XVMC_MSG(XVMC_TRACE, "[XvMC] y sample bits=0x%08X\n" \
108 "[XvMC] u sample bits=0x%08X\n" \
109 "[XvMC] v sample bits=0x%08X\n" \
110 "[XvMC] horz y period=%u\n" \
111 "[XvMC] horz u period=%u\n" \
112 "[XvMC] horz v period=%u\n" \
113 "[XvMC] vert y period=%u\n" \
114 "[XvMC] vert u period=%u\n" \
115 "[XvMC] vert v period=%u\n",
116 subpictures[i].y_sample_bits, subpictures[i].u_sample_bits, subpictures[i].v_sample_bits,
117 subpictures[i].horz_y_period, subpictures[i].horz_u_period, subpictures[i].horz_v_period,
118 subpictures[i].vert_y_period, subpictures[i].vert_u_period, subpictures[i].vert_v_period);
119 }
120 break;
121 }
122 }
123
124 XFree(subpictures);
125
126 return i < num_subpics ? Success : BadMatch;
127 }
128
129 PUBLIC
130 Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *subpicture,
131 unsigned short width, unsigned short height, int xvimage_id)
132 {
133 XvMCContextPrivate *context_priv;
134 XvMCSubpicturePrivate *subpicture_priv;
135 struct pipe_video_context *vpipe;
136 struct pipe_resource template;
137 struct pipe_resource *tex;
138 Status ret;
139
140 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);
141
142 assert(dpy);
143
144 if (!context)
145 return XvMCBadContext;
146
147 context_priv = context->privData;
148 vpipe = context_priv->vctx->vpipe;
149
150 if (!subpicture)
151 return XvMCBadSubpicture;
152
153 if (width > context_priv->subpicture_max_width ||
154 height > context_priv->subpicture_max_height)
155 return BadValue;
156
157 ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id);
158 if (ret != Success)
159 return ret;
160
161 subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate));
162 if (!subpicture_priv)
163 return BadAlloc;
164
165 memset(&template, 0, sizeof(struct pipe_resource));
166 template.target = PIPE_TEXTURE_2D;
167 template.format = XvIDToPipe(xvimage_id);
168 template.last_level = 0;
169 if (vpipe->get_param(vpipe, PIPE_CAP_NPOT_TEXTURES)) {
170 template.width0 = width;
171 template.height0 = height;
172 }
173 else {
174 template.width0 = util_next_power_of_two(width);
175 template.height0 = util_next_power_of_two(height);
176 }
177 template.depth0 = 1;
178 template.usage = PIPE_USAGE_DYNAMIC;
179 template.bind = PIPE_BIND_SAMPLER_VIEW;
180 template.flags = 0;
181
182 subpicture_priv->context = context;
183 tex = vpipe->screen->resource_create(vpipe->screen, &template);
184 subpicture_priv->sfc = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
185 PIPE_BIND_SAMPLER_VIEW);
186 pipe_resource_reference(&tex, NULL);
187 if (!subpicture_priv->sfc) {
188 FREE(subpicture_priv);
189 return BadAlloc;
190 }
191
192 subpicture->subpicture_id = XAllocID(dpy);
193 subpicture->context_id = context->context_id;
194 subpicture->xvimage_id = xvimage_id;
195 subpicture->width = width;
196 subpicture->height = height;
197 subpicture->num_palette_entries = 0;
198 subpicture->entry_bytes = PipeToComponentOrder(template.format, subpicture->component_order);
199 subpicture->privData = subpicture_priv;
200
201 SyncHandle();
202
203 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);
204
205 return Success;
206 }
207
208 PUBLIC
209 Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y,
210 unsigned short width, unsigned short height, unsigned int color)
211 {
212 XvMCSubpicturePrivate *subpicture_priv;
213 XvMCContextPrivate *context_priv;
214
215 assert(dpy);
216
217 if (!subpicture)
218 return XvMCBadSubpicture;
219
220 subpicture_priv = subpicture->privData;
221 context_priv = subpicture_priv->context->privData;
222 /* TODO: Assert clear rect is within bounds? Or clip? */
223 context_priv->vctx->vpipe->surface_fill(context_priv->vctx->vpipe,
224 subpicture_priv->sfc, x, y,
225 width, height, color);
226
227 return Success;
228 }
229
230 PUBLIC
231 Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image,
232 short srcx, short srcy, unsigned short width, unsigned short height,
233 short dstx, short dsty)
234 {
235 XvMCSubpicturePrivate *subpicture_priv;
236 XvMCContextPrivate *context_priv;
237 struct pipe_video_context *vpipe;
238 struct pipe_transfer *xfer;
239 unsigned char *src, *dst, *dst_line;
240 unsigned x, y;
241 struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1};
242 struct pipe_subresource sr = {0, 0};
243
244 XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);
245
246 assert(dpy);
247
248 if (!subpicture)
249 return XvMCBadSubpicture;
250
251 assert(image);
252
253 if (subpicture->xvimage_id != image->id)
254 return BadMatch;
255
256 /* No planar support for now */
257 if (image->num_planes != 1)
258 return BadMatch;
259
260 subpicture_priv = subpicture->privData;
261 context_priv = subpicture_priv->context->privData;
262 vpipe = context_priv->vctx->vpipe;
263
264 /* TODO: Assert rects are within bounds? Or clip? */
265
266 xfer = vpipe->get_transfer(vpipe, subpicture_priv->sfc->texture,
267 sr, PIPE_TRANSFER_WRITE, &dst_box);
268 if (!xfer)
269 return BadAlloc;
270
271 src = image->data;
272 dst = vpipe->transfer_map(vpipe, xfer);
273 if (!dst) {
274 vpipe->transfer_destroy(vpipe, xfer);
275 return BadAlloc;
276 }
277
278 switch (image->id) {
279 case FOURCC_RGB:
280 assert(subpicture_priv->sfc->format == XvIDToPipe(image->id));
281 for (y = 0; y < height; ++y) {
282 dst_line = dst;
283 for (x = 0; x < width; ++x, src += 3, dst_line += 4) {
284 dst_line[0] = src[2]; /* B */
285 dst_line[1] = src[1]; /* G */
286 dst_line[2] = src[0]; /* R */
287 }
288 dst += xfer->stride;
289 }
290 break;
291 default:
292 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", image->id);
293 }
294
295 vpipe->transfer_unmap(vpipe, xfer);
296 vpipe->transfer_destroy(vpipe, xfer);
297
298 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);
299
300 return Success;
301 }
302
303 PUBLIC
304 Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
305 {
306 XvMCSubpicturePrivate *subpicture_priv;
307
308 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture);
309
310 assert(dpy);
311
312 if (!subpicture)
313 return XvMCBadSubpicture;
314
315 subpicture_priv = subpicture->privData;
316 pipe_surface_reference(&subpicture_priv->sfc, NULL);
317 FREE(subpicture_priv);
318
319 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);
320
321 return Success;
322 }
323
324 PUBLIC
325 Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)
326 {
327 assert(dpy);
328
329 if (!subpicture)
330 return XvMCBadSubpicture;
331
332 assert(palette);
333
334 /* We don't support paletted subpictures */
335 return BadMatch;
336 }
337
338 PUBLIC
339 Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture,
340 short subx, short suby, unsigned short subw, unsigned short subh,
341 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
342 {
343 XvMCSurfacePrivate *surface_priv;
344 XvMCSubpicturePrivate *subpicture_priv;
345
346 XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface);
347
348 assert(dpy);
349
350 if (!target_surface)
351 return XvMCBadSurface;
352
353 if (!subpicture)
354 return XvMCBadSubpicture;
355
356 if (target_surface->context_id != subpicture->context_id)
357 return BadMatch;
358
359 /* TODO: Verify against subpicture independent scaling */
360
361 surface_priv = target_surface->privData;
362 subpicture_priv = subpicture->privData;
363
364 /* TODO: Assert rects are within bounds? Or clip? */
365
366 surface_priv->subpicture = subpicture;
367 surface_priv->subx = subx;
368 surface_priv->suby = suby;
369 surface_priv->subw = subw;
370 surface_priv->subh = subh;
371 surface_priv->surfx = surfx;
372 surface_priv->surfy = surfy;
373 surface_priv->surfw = surfw;
374 surface_priv->surfh = surfh;
375 subpicture_priv->surface = target_surface;
376
377 return Success;
378 }
379
380 PUBLIC
381 Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface,
382 XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh,
383 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
384 {
385 assert(dpy);
386
387 if (!source_surface || !target_surface)
388 return XvMCBadSurface;
389
390 if (!subpicture)
391 return XvMCBadSubpicture;
392
393 if (source_surface->context_id != subpicture->context_id)
394 return BadMatch;
395
396 if (source_surface->context_id != subpicture->context_id)
397 return BadMatch;
398
399 /* TODO: Assert rects are within bounds? Or clip? */
400
401 return Success;
402 }
403
404 PUBLIC
405 Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture)
406 {
407 assert(dpy);
408
409 if (!subpicture)
410 return XvMCBadSubpicture;
411
412 return Success;
413 }
414
415 PUBLIC
416 Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture)
417 {
418 assert(dpy);
419
420 if (!subpicture)
421 return XvMCBadSubpicture;
422
423 return Success;
424 }
425
426 PUBLIC
427 Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status)
428 {
429 assert(dpy);
430
431 if (!subpicture)
432 return XvMCBadSubpicture;
433
434 assert(status);
435
436 /* TODO */
437 *status = 0;
438
439 return Success;
440 }