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