Merge remote branch 'origin/master' 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 struct pipe_surface surf_template;
139 Status ret;
140
141 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);
142
143 assert(dpy);
144
145 if (!context)
146 return XvMCBadContext;
147
148 context_priv = context->privData;
149 vpipe = context_priv->vctx->vpipe;
150
151 if (!subpicture)
152 return XvMCBadSubpicture;
153
154 if (width > context_priv->subpicture_max_width ||
155 height > context_priv->subpicture_max_height)
156 return BadValue;
157
158 ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id);
159 if (ret != Success)
160 return ret;
161
162 subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate));
163 if (!subpicture_priv)
164 return BadAlloc;
165
166 memset(&template, 0, sizeof(struct pipe_resource));
167 template.target = PIPE_TEXTURE_2D;
168 template.format = XvIDToPipe(xvimage_id);
169 template.last_level = 0;
170 if (vpipe->get_param(vpipe, PIPE_CAP_NPOT_TEXTURES)) {
171 template.width0 = width;
172 template.height0 = height;
173 }
174 else {
175 template.width0 = util_next_power_of_two(width);
176 template.height0 = util_next_power_of_two(height);
177 }
178 template.depth0 = 1;
179 template.array_size = 1;
180 template.usage = PIPE_USAGE_DYNAMIC;
181 template.bind = PIPE_BIND_SAMPLER_VIEW;
182 template.flags = 0;
183
184 subpicture_priv->context = context;
185 tex = vpipe->screen->resource_create(vpipe->screen, &template);
186
187 memset(&surf_template, 0, sizeof(surf_template));
188 surf_template.format = tex->format;
189 surf_template.usage = PIPE_BIND_SAMPLER_VIEW;
190 subpicture_priv->sfc = vpipe->create_surface(vpipe, tex, &surf_template);
191 pipe_resource_reference(&tex, NULL);
192 if (!subpicture_priv->sfc) {
193 FREE(subpicture_priv);
194 return BadAlloc;
195 }
196
197 subpicture->subpicture_id = XAllocID(dpy);
198 subpicture->context_id = context->context_id;
199 subpicture->xvimage_id = xvimage_id;
200 subpicture->width = width;
201 subpicture->height = height;
202 subpicture->num_palette_entries = 0;
203 subpicture->entry_bytes = PipeToComponentOrder(template.format, subpicture->component_order);
204 subpicture->privData = subpicture_priv;
205
206 SyncHandle();
207
208 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);
209
210 return Success;
211 }
212
213 PUBLIC
214 Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y,
215 unsigned short width, unsigned short height, unsigned int color)
216 {
217 XvMCSubpicturePrivate *subpicture_priv;
218 XvMCContextPrivate *context_priv;
219 unsigned int tmp_color;
220 float color_f[4];
221
222 assert(dpy);
223
224 if (!subpicture)
225 return XvMCBadSubpicture;
226
227 /* Convert color to float */
228 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM,
229 color_f, 1,
230 &color, 4,
231 0, 0, 1, 1);
232
233 subpicture_priv = subpicture->privData;
234 context_priv = subpicture_priv->context->privData;
235 /* TODO: Assert clear rect is within bounds? Or clip? */
236 context_priv->vctx->vpipe->clear_render_target(context_priv->vctx->vpipe,
237 subpicture_priv->sfc, x, y,
238 color_f,
239 width, height);
240
241 return Success;
242 }
243
244 PUBLIC
245 Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image,
246 short srcx, short srcy, unsigned short width, unsigned short height,
247 short dstx, short dsty)
248 {
249 XvMCSubpicturePrivate *subpicture_priv;
250 XvMCContextPrivate *context_priv;
251 struct pipe_video_context *vpipe;
252 struct pipe_transfer *xfer;
253 unsigned char *src, *dst, *dst_line;
254 unsigned x, y;
255 struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1};
256
257 XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);
258
259 assert(dpy);
260
261 if (!subpicture)
262 return XvMCBadSubpicture;
263
264 assert(image);
265
266 if (subpicture->xvimage_id != image->id)
267 return BadMatch;
268
269 /* No planar support for now */
270 if (image->num_planes != 1)
271 return BadMatch;
272
273 subpicture_priv = subpicture->privData;
274 context_priv = subpicture_priv->context->privData;
275 vpipe = context_priv->vctx->vpipe;
276
277 /* TODO: Assert rects are within bounds? Or clip? */
278
279 xfer = vpipe->get_transfer(vpipe, subpicture_priv->sfc->texture,
280 0, PIPE_TRANSFER_WRITE, &dst_box);
281 if (!xfer)
282 return BadAlloc;
283
284 src = image->data;
285 dst = vpipe->transfer_map(vpipe, xfer);
286 if (!dst) {
287 vpipe->transfer_destroy(vpipe, xfer);
288 return BadAlloc;
289 }
290
291 switch (image->id) {
292 case FOURCC_RGB:
293 assert(subpicture_priv->sfc->format == XvIDToPipe(image->id));
294 for (y = 0; y < height; ++y) {
295 dst_line = dst;
296 for (x = 0; x < width; ++x, src += 3, dst_line += 4) {
297 dst_line[0] = src[2]; /* B */
298 dst_line[1] = src[1]; /* G */
299 dst_line[2] = src[0]; /* R */
300 }
301 dst += xfer->stride;
302 }
303 break;
304 default:
305 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", image->id);
306 }
307
308 vpipe->transfer_unmap(vpipe, xfer);
309 vpipe->transfer_destroy(vpipe, xfer);
310
311 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);
312
313 return Success;
314 }
315
316 PUBLIC
317 Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
318 {
319 XvMCSubpicturePrivate *subpicture_priv;
320
321 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture);
322
323 assert(dpy);
324
325 if (!subpicture)
326 return XvMCBadSubpicture;
327
328 subpicture_priv = subpicture->privData;
329 pipe_surface_reference(&subpicture_priv->sfc, NULL);
330 FREE(subpicture_priv);
331
332 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);
333
334 return Success;
335 }
336
337 PUBLIC
338 Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)
339 {
340 assert(dpy);
341
342 if (!subpicture)
343 return XvMCBadSubpicture;
344
345 assert(palette);
346
347 /* We don't support paletted subpictures */
348 return BadMatch;
349 }
350
351 PUBLIC
352 Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture,
353 short subx, short suby, unsigned short subw, unsigned short subh,
354 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
355 {
356 XvMCSurfacePrivate *surface_priv;
357 XvMCSubpicturePrivate *subpicture_priv;
358
359 XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface);
360
361 assert(dpy);
362
363 if (!target_surface)
364 return XvMCBadSurface;
365
366 if (!subpicture)
367 return XvMCBadSubpicture;
368
369 if (target_surface->context_id != subpicture->context_id)
370 return BadMatch;
371
372 /* TODO: Verify against subpicture independent scaling */
373
374 surface_priv = target_surface->privData;
375 subpicture_priv = subpicture->privData;
376
377 /* TODO: Assert rects are within bounds? Or clip? */
378
379 surface_priv->subpicture = subpicture;
380 surface_priv->subx = subx;
381 surface_priv->suby = suby;
382 surface_priv->subw = subw;
383 surface_priv->subh = subh;
384 surface_priv->surfx = surfx;
385 surface_priv->surfy = surfy;
386 surface_priv->surfw = surfw;
387 surface_priv->surfh = surfh;
388 subpicture_priv->surface = target_surface;
389
390 return Success;
391 }
392
393 PUBLIC
394 Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface,
395 XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh,
396 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
397 {
398 assert(dpy);
399
400 if (!source_surface || !target_surface)
401 return XvMCBadSurface;
402
403 if (!subpicture)
404 return XvMCBadSubpicture;
405
406 if (source_surface->context_id != subpicture->context_id)
407 return BadMatch;
408
409 if (source_surface->context_id != subpicture->context_id)
410 return BadMatch;
411
412 /* TODO: Assert rects are within bounds? Or clip? */
413
414 return Success;
415 }
416
417 PUBLIC
418 Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture)
419 {
420 assert(dpy);
421
422 if (!subpicture)
423 return XvMCBadSubpicture;
424
425 return Success;
426 }
427
428 PUBLIC
429 Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture)
430 {
431 assert(dpy);
432
433 if (!subpicture)
434 return XvMCBadSubpicture;
435
436 return Success;
437 }
438
439 PUBLIC
440 Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status)
441 {
442 assert(dpy);
443
444 if (!subpicture)
445 return XvMCBadSubpicture;
446
447 assert(status);
448
449 /* TODO */
450 *status = 0;
451
452 return Success;
453 }