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