Merge branch 'gallium-polygon-stipple'
[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
30 #include <X11/Xlibint.h>
31 #include <X11/extensions/XvMClib.h>
32 #include <xorg/fourcc.h>
33
34 #include <pipe/p_screen.h>
35 #include <pipe/p_video_decoder.h>
36 #include <pipe/p_state.h>
37
38 #include <util/u_memory.h>
39 #include <util/u_math.h>
40 #include <util/u_format.h>
41 #include <util/u_sampler.h>
42 #include <util/u_rect.h>
43
44 #include <vl_winsys.h>
45
46 #include "xvmc_private.h"
47
48 #define FOURCC_RGB 0x0000003
49
50 static enum pipe_format XvIDToPipe(int xvimage_id)
51 {
52 switch (xvimage_id) {
53 case FOURCC_RGB:
54 return PIPE_FORMAT_B8G8R8X8_UNORM;
55
56 case FOURCC_AI44:
57 case FOURCC_IA44:
58 return PIPE_FORMAT_L4A4_UNORM;
59
60 default:
61 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
62 return PIPE_FORMAT_NONE;
63 }
64 }
65
66 static unsigned NumPaletteEntries4XvID(int xvimage_id)
67 {
68 switch (xvimage_id) {
69 case FOURCC_RGB:
70 return 0;
71
72 case FOURCC_AI44:
73 case FOURCC_IA44:
74 return 16;
75
76 default:
77 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
78 return 0;
79 }
80 }
81
82 static void XvIDToSwizzle(int xvimage_id, struct pipe_sampler_view *tmpl)
83 {
84 switch (xvimage_id) {
85 default:
86 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
87
88 /* fall through */
89 case FOURCC_RGB:
90 tmpl->swizzle_r = PIPE_SWIZZLE_BLUE;
91 tmpl->swizzle_g = PIPE_SWIZZLE_GREEN;
92 tmpl->swizzle_b = PIPE_SWIZZLE_RED;
93 tmpl->swizzle_a = PIPE_SWIZZLE_ONE;
94 break;
95
96 case FOURCC_IA44:
97 tmpl->swizzle_r = PIPE_SWIZZLE_ALPHA;
98 tmpl->swizzle_g = PIPE_SWIZZLE_ZERO;
99 tmpl->swizzle_b = PIPE_SWIZZLE_ZERO;
100 tmpl->swizzle_a = PIPE_SWIZZLE_RED;
101 break;
102
103 case FOURCC_AI44:
104 tmpl->swizzle_r = PIPE_SWIZZLE_RED;
105 tmpl->swizzle_g = PIPE_SWIZZLE_ZERO;
106 tmpl->swizzle_b = PIPE_SWIZZLE_ZERO;
107 tmpl->swizzle_a = PIPE_SWIZZLE_ALPHA;
108 break;
109 }
110 }
111
112 static int PipeToComponentOrder(enum pipe_format format, char *component_order)
113 {
114 assert(component_order);
115
116 switch (format) {
117 case PIPE_FORMAT_B8G8R8X8_UNORM:
118 return 0;
119
120 case PIPE_FORMAT_L4A4_UNORM:
121 component_order[0] = 'Y';
122 component_order[1] = 'U';
123 component_order[2] = 'V';
124 component_order[3] = 'A';
125 return 4;
126
127 default:
128 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format);
129 component_order[0] = 0;
130 component_order[1] = 0;
131 component_order[2] = 0;
132 component_order[3] = 0;
133 return 0;
134 }
135 }
136
137 static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id)
138 {
139 XvImageFormatValues *subpictures;
140 int num_subpics;
141 unsigned int i;
142
143 subpictures = XvMCListSubpictureTypes(dpy, port, surface_type_id, &num_subpics);
144 if (num_subpics < 1) {
145 if (subpictures)
146 XFree(subpictures);
147 return BadMatch;
148 }
149 if (!subpictures)
150 return BadAlloc;
151
152 for (i = 0; i < num_subpics; ++i) {
153 if (subpictures[i].id == xvimage_id) {
154 XVMC_MSG(XVMC_TRACE, "[XvMC] Found requested subpicture format.\n" \
155 "[XvMC] port=%u\n" \
156 "[XvMC] surface id=0x%08X\n" \
157 "[XvMC] image id=0x%08X\n" \
158 "[XvMC] type=%08X\n" \
159 "[XvMC] byte order=%08X\n" \
160 "[XvMC] bits per pixel=%u\n" \
161 "[XvMC] format=%08X\n" \
162 "[XvMC] num planes=%d\n",
163 port, surface_type_id, xvimage_id, subpictures[i].type, subpictures[i].byte_order,
164 subpictures[i].bits_per_pixel, subpictures[i].format, subpictures[i].num_planes);
165 if (subpictures[i].type == XvRGB) {
166 XVMC_MSG(XVMC_TRACE, "[XvMC] depth=%d\n" \
167 "[XvMC] red mask=0x%08X\n" \
168 "[XvMC] green mask=0x%08X\n" \
169 "[XvMC] blue mask=0x%08X\n",
170 subpictures[i].depth, subpictures[i].red_mask,
171 subpictures[i].green_mask, subpictures[i].blue_mask);
172 }
173 else if (subpictures[i].type == XvYUV) {
174 XVMC_MSG(XVMC_TRACE, "[XvMC] y sample bits=0x%08X\n" \
175 "[XvMC] u sample bits=0x%08X\n" \
176 "[XvMC] v sample bits=0x%08X\n" \
177 "[XvMC] horz y period=%u\n" \
178 "[XvMC] horz u period=%u\n" \
179 "[XvMC] horz v period=%u\n" \
180 "[XvMC] vert y period=%u\n" \
181 "[XvMC] vert u period=%u\n" \
182 "[XvMC] vert v period=%u\n",
183 subpictures[i].y_sample_bits, subpictures[i].u_sample_bits, subpictures[i].v_sample_bits,
184 subpictures[i].horz_y_period, subpictures[i].horz_u_period, subpictures[i].horz_v_period,
185 subpictures[i].vert_y_period, subpictures[i].vert_u_period, subpictures[i].vert_v_period);
186 }
187 break;
188 }
189 }
190
191 XFree(subpictures);
192
193 return i < num_subpics ? Success : BadMatch;
194 }
195
196 static void
197 upload_sampler(struct pipe_context *pipe, struct pipe_sampler_view *dst,
198 const struct pipe_box *dst_box, const void *src, unsigned src_stride,
199 unsigned src_x, unsigned src_y)
200 {
201 struct pipe_transfer *transfer;
202 void *map;
203
204 transfer = pipe->get_transfer(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE, dst_box);
205 if (!transfer)
206 return;
207
208 map = pipe->transfer_map(pipe, transfer);
209 if (map) {
210 util_copy_rect(map, dst->texture->format, transfer->stride, 0, 0,
211 dst_box->width, dst_box->height,
212 src, src_stride, src_x, src_y);
213
214 pipe->transfer_unmap(pipe, transfer);
215 }
216
217 pipe->transfer_destroy(pipe, transfer);
218 }
219
220 PUBLIC
221 Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *subpicture,
222 unsigned short width, unsigned short height, int xvimage_id)
223 {
224 XvMCContextPrivate *context_priv;
225 XvMCSubpicturePrivate *subpicture_priv;
226 struct pipe_context *pipe;
227 struct pipe_resource tex_templ, *tex;
228 struct pipe_sampler_view sampler_templ;
229 Status ret;
230
231 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);
232
233 assert(dpy);
234
235 if (!context)
236 return XvMCBadContext;
237
238 context_priv = context->privData;
239 pipe = context_priv->vctx->pipe;
240
241 if (!subpicture)
242 return XvMCBadSubpicture;
243
244 if (width > context_priv->subpicture_max_width ||
245 height > context_priv->subpicture_max_height)
246 return BadValue;
247
248 ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id);
249 if (ret != Success)
250 return ret;
251
252 subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate));
253 if (!subpicture_priv)
254 return BadAlloc;
255
256 memset(&tex_templ, 0, sizeof(tex_templ));
257 tex_templ.target = PIPE_TEXTURE_2D;
258 tex_templ.format = XvIDToPipe(xvimage_id);
259 tex_templ.last_level = 0;
260 if (pipe->screen->get_video_param(pipe->screen,
261 PIPE_VIDEO_PROFILE_UNKNOWN,
262 PIPE_VIDEO_CAP_NPOT_TEXTURES)) {
263 tex_templ.width0 = width;
264 tex_templ.height0 = height;
265 }
266 else {
267 tex_templ.width0 = util_next_power_of_two(width);
268 tex_templ.height0 = util_next_power_of_two(height);
269 }
270 tex_templ.depth0 = 1;
271 tex_templ.array_size = 1;
272 tex_templ.usage = PIPE_USAGE_DYNAMIC;
273 tex_templ.bind = PIPE_BIND_SAMPLER_VIEW;
274 tex_templ.flags = 0;
275
276 tex = pipe->screen->resource_create(pipe->screen, &tex_templ);
277
278 memset(&sampler_templ, 0, sizeof(sampler_templ));
279 u_sampler_view_default_template(&sampler_templ, tex, tex->format);
280 XvIDToSwizzle(xvimage_id, &sampler_templ);
281
282 subpicture_priv->sampler = pipe->create_sampler_view(pipe, tex, &sampler_templ);
283 pipe_resource_reference(&tex, NULL);
284 if (!subpicture_priv->sampler) {
285 FREE(subpicture_priv);
286 return BadAlloc;
287 }
288
289 subpicture_priv->context = context;
290 subpicture->subpicture_id = XAllocID(dpy);
291 subpicture->context_id = context->context_id;
292 subpicture->xvimage_id = xvimage_id;
293 subpicture->width = width;
294 subpicture->height = height;
295 subpicture->num_palette_entries = NumPaletteEntries4XvID(xvimage_id);
296 subpicture->entry_bytes = PipeToComponentOrder(tex_templ.format, subpicture->component_order);
297 subpicture->privData = subpicture_priv;
298
299 if (subpicture->num_palette_entries > 0) {
300 tex_templ.target = PIPE_TEXTURE_1D;
301 tex_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
302 tex_templ.width0 = subpicture->num_palette_entries;
303 tex_templ.height0 = 1;
304 tex_templ.usage = PIPE_USAGE_STATIC;
305
306 tex = pipe->screen->resource_create(pipe->screen, &tex_templ);
307
308 memset(&sampler_templ, 0, sizeof(sampler_templ));
309 u_sampler_view_default_template(&sampler_templ, tex, tex->format);
310 sampler_templ.swizzle_a = PIPE_SWIZZLE_ONE;
311 subpicture_priv->palette = pipe->create_sampler_view(pipe, tex, &sampler_templ);
312 pipe_resource_reference(&tex, NULL);
313 if (!subpicture_priv->sampler) {
314 FREE(subpicture_priv);
315 return BadAlloc;
316 }
317 }
318
319 SyncHandle();
320
321 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);
322
323 return Success;
324 }
325
326 PUBLIC
327 Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y,
328 unsigned short width, unsigned short height, unsigned int color)
329 {
330 XvMCSubpicturePrivate *subpicture_priv;
331 XvMCContextPrivate *context_priv;
332 struct pipe_context *pipe;
333 struct pipe_sampler_view *dst;
334 struct pipe_box dst_box = {x, y, 0, width, height, 1};
335 struct pipe_transfer *transfer;
336 union util_color uc;
337 void *map;
338
339 assert(dpy);
340
341 if (!subpicture)
342 return XvMCBadSubpicture;
343
344 /* Convert color to float */
345 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM,
346 uc.f, 1, &color, 4,
347 0, 0, 1, 1);
348
349 subpicture_priv = subpicture->privData;
350 context_priv = subpicture_priv->context->privData;
351 pipe = context_priv->vctx->pipe;
352 dst = subpicture_priv->sampler;
353
354 /* TODO: Assert clear rect is within bounds? Or clip? */
355 transfer = pipe->get_transfer(pipe, dst->texture, 0, PIPE_TRANSFER_WRITE, &dst_box);
356 if (!transfer)
357 return XvMCBadSubpicture;
358
359 map = pipe->transfer_map(pipe, transfer);
360 if (map) {
361 util_fill_rect(map, dst->texture->format, transfer->stride, 0, 0,
362 dst_box.width, dst_box.height, &uc);
363
364 pipe->transfer_unmap(pipe, transfer);
365 }
366
367 pipe->transfer_destroy(pipe, transfer);
368
369 return Success;
370 }
371
372 PUBLIC
373 Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image,
374 short srcx, short srcy, unsigned short width, unsigned short height,
375 short dstx, short dsty)
376 {
377 XvMCSubpicturePrivate *subpicture_priv;
378 XvMCContextPrivate *context_priv;
379 struct pipe_context *pipe;
380 struct pipe_box dst_box = {dstx, dsty, 0, width, height, 1};
381 unsigned src_stride;
382
383 XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);
384
385 assert(dpy);
386
387 if (!subpicture)
388 return XvMCBadSubpicture;
389
390 assert(image);
391
392 if (subpicture->xvimage_id != image->id)
393 return BadMatch;
394
395 /* No planar support for now */
396 if (image->num_planes != 1)
397 return BadMatch;
398
399 subpicture_priv = subpicture->privData;
400 context_priv = subpicture_priv->context->privData;
401 pipe = context_priv->vctx->pipe;
402
403 /* clipping should be done by upload_sampler and regardles what the documentation
404 says image->pitches[0] doesn't seems to be in bytes, so don't use it */
405 src_stride = image->width * util_format_get_blocksize(subpicture_priv->sampler->texture->format);
406 upload_sampler(pipe, subpicture_priv->sampler, &dst_box, image->data, src_stride, srcx, srcy);
407
408 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);
409
410 return Success;
411 }
412
413 PUBLIC
414 Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
415 {
416 XvMCSubpicturePrivate *subpicture_priv;
417
418 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture);
419
420 assert(dpy);
421
422 if (!subpicture)
423 return XvMCBadSubpicture;
424
425 subpicture_priv = subpicture->privData;
426 pipe_sampler_view_reference(&subpicture_priv->sampler, NULL);
427 pipe_sampler_view_reference(&subpicture_priv->palette, NULL);
428 FREE(subpicture_priv);
429
430 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);
431
432 return Success;
433 }
434
435 PUBLIC
436 Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)
437 {
438 XvMCSubpicturePrivate *subpicture_priv;
439 XvMCContextPrivate *context_priv;
440 struct pipe_context *pipe;
441 struct pipe_box dst_box = {0, 0, 0, 0, 1, 1};
442
443 assert(dpy);
444 assert(palette);
445
446 if (!subpicture)
447 return XvMCBadSubpicture;
448
449 subpicture_priv = subpicture->privData;
450 context_priv = subpicture_priv->context->privData;
451 pipe = context_priv->vctx->pipe;
452
453 dst_box.width = subpicture->num_palette_entries;
454
455 upload_sampler(pipe, subpicture_priv->palette, &dst_box, palette, 0, 0, 0);
456
457 XVMC_MSG(XVMC_TRACE, "[XvMC] Palette of Subpicture %p set.\n", subpicture);
458
459 return Success;
460 }
461
462 PUBLIC
463 Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture,
464 short subx, short suby, unsigned short subw, unsigned short subh,
465 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
466 {
467 struct pipe_video_rect src_rect = {subx, suby, subw, subh};
468 struct pipe_video_rect dst_rect = {surfx, surfy, surfw, surfh};
469
470 XvMCSurfacePrivate *surface_priv;
471 XvMCSubpicturePrivate *subpicture_priv;
472
473 XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface);
474
475 assert(dpy);
476
477 if (!target_surface)
478 return XvMCBadSurface;
479
480 if (!subpicture)
481 return XvMCBadSubpicture;
482
483 if (target_surface->context_id != subpicture->context_id)
484 return BadMatch;
485
486 /* TODO: Verify against subpicture independent scaling */
487
488 surface_priv = target_surface->privData;
489 subpicture_priv = subpicture->privData;
490
491 /* TODO: Assert rects are within bounds? Or clip? */
492 subpicture_priv->src_rect = src_rect;
493 subpicture_priv->dst_rect = dst_rect;
494
495 surface_priv->subpicture = subpicture;
496 subpicture_priv->surface = target_surface;
497
498 return Success;
499 }
500
501 PUBLIC
502 Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface,
503 XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh,
504 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
505 {
506 assert(dpy);
507
508 if (!source_surface || !target_surface)
509 return XvMCBadSurface;
510
511 if (!subpicture)
512 return XvMCBadSubpicture;
513
514 if (source_surface->context_id != subpicture->context_id)
515 return BadMatch;
516
517 if (source_surface->context_id != subpicture->context_id)
518 return BadMatch;
519
520 /* TODO: Assert rects are within bounds? Or clip? */
521
522 return Success;
523 }
524
525 PUBLIC
526 Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture)
527 {
528 assert(dpy);
529
530 if (!subpicture)
531 return XvMCBadSubpicture;
532
533 return Success;
534 }
535
536 PUBLIC
537 Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture)
538 {
539 assert(dpy);
540
541 if (!subpicture)
542 return XvMCBadSubpicture;
543
544 return Success;
545 }
546
547 PUBLIC
548 Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status)
549 {
550 assert(dpy);
551
552 if (!subpicture)
553 return XvMCBadSubpicture;
554
555 assert(status);
556
557 /* TODO */
558 *status = 0;
559
560 return Success;
561 }