1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 #include <X11/Xlibint.h>
31 #include <X11/extensions/XvMClib.h>
32 #include <xorg/fourcc.h>
34 #include <pipe/p_screen.h>
35 #include <pipe/p_video_decoder.h>
36 #include <pipe/p_state.h>
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>
44 #include <vl_winsys.h>
46 #include "xvmc_private.h"
48 #define FOURCC_RGB 0x0000003
50 static enum pipe_format
XvIDToPipe(int xvimage_id
)
54 return PIPE_FORMAT_B8G8R8X8_UNORM
;
58 return PIPE_FORMAT_L4A4_UNORM
;
61 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
62 return PIPE_FORMAT_NONE
;
66 static unsigned NumPaletteEntries4XvID(int xvimage_id
)
77 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
82 static void XvIDToSwizzle(int xvimage_id
, struct pipe_sampler_view
*tmpl
)
86 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
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
;
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
;
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
;
112 static int PipeToComponentOrder(enum pipe_format format
, char *component_order
)
114 assert(component_order
);
117 case PIPE_FORMAT_B8G8R8X8_UNORM
:
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';
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;
137 static Status
Validate(Display
*dpy
, XvPortID port
, int surface_type_id
, int xvimage_id
)
139 XvImageFormatValues
*subpictures
;
143 subpictures
= XvMCListSubpictureTypes(dpy
, port
, surface_type_id
, &num_subpics
);
144 if (num_subpics
< 1) {
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" \
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
);
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
);
193 return i
< num_subpics
? Success
: BadMatch
;
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
)
201 struct pipe_transfer
*transfer
;
204 transfer
= pipe
->get_transfer(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
, dst_box
);
208 map
= pipe
->transfer_map(pipe
, transfer
);
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
);
214 pipe
->transfer_unmap(pipe
, transfer
);
217 pipe
->transfer_destroy(pipe
, transfer
);
221 Status
XvMCCreateSubpicture(Display
*dpy
, XvMCContext
*context
, XvMCSubpicture
*subpicture
,
222 unsigned short width
, unsigned short height
, int xvimage_id
)
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
;
231 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating subpicture %p.\n", subpicture
);
236 return XvMCBadContext
;
238 context_priv
= context
->privData
;
239 pipe
= context_priv
->vctx
->pipe
;
242 return XvMCBadSubpicture
;
244 if (width
> context_priv
->subpicture_max_width
||
245 height
> context_priv
->subpicture_max_height
)
248 ret
= Validate(dpy
, context
->port
, context
->surface_type_id
, xvimage_id
);
252 subpicture_priv
= CALLOC(1, sizeof(XvMCSubpicturePrivate
));
253 if (!subpicture_priv
)
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
;
267 tex_templ
.width0
= util_next_power_of_two(width
);
268 tex_templ
.height0
= util_next_power_of_two(height
);
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
;
276 tex
= pipe
->screen
->resource_create(pipe
->screen
, &tex_templ
);
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
);
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
);
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
;
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
;
306 tex
= pipe
->screen
->resource_create(pipe
->screen
, &tex_templ
);
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
);
321 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p created.\n", subpicture
);
327 Status
XvMCClearSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, short x
, short y
,
328 unsigned short width
, unsigned short height
, unsigned int color
)
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
;
342 return XvMCBadSubpicture
;
344 /* Convert color to float */
345 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM
,
349 subpicture_priv
= subpicture
->privData
;
350 context_priv
= subpicture_priv
->context
->privData
;
351 pipe
= context_priv
->vctx
->pipe
;
352 dst
= subpicture_priv
->sampler
;
354 /* TODO: Assert clear rect is within bounds? Or clip? */
355 transfer
= pipe
->get_transfer(pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
, &dst_box
);
357 return XvMCBadSubpicture
;
359 map
= pipe
->transfer_map(pipe
, transfer
);
361 util_fill_rect(map
, dst
->texture
->format
, transfer
->stride
, 0, 0,
362 dst_box
.width
, dst_box
.height
, &uc
);
364 pipe
->transfer_unmap(pipe
, transfer
);
367 pipe
->transfer_destroy(pipe
, transfer
);
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
)
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};
383 XVMC_MSG(XVMC_TRACE
, "[XvMC] Compositing subpicture %p.\n", subpicture
);
388 return XvMCBadSubpicture
;
392 if (subpicture
->xvimage_id
!= image
->id
)
395 /* No planar support for now */
396 if (image
->num_planes
!= 1)
399 subpicture_priv
= subpicture
->privData
;
400 context_priv
= subpicture_priv
->context
->privData
;
401 pipe
= context_priv
->vctx
->pipe
;
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
);
408 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p composited.\n", subpicture
);
414 Status
XvMCDestroySubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
416 XvMCSubpicturePrivate
*subpicture_priv
;
418 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying subpicture %p.\n", subpicture
);
423 return XvMCBadSubpicture
;
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
);
430 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p destroyed.\n", subpicture
);
436 Status
XvMCSetSubpicturePalette(Display
*dpy
, XvMCSubpicture
*subpicture
, unsigned char *palette
)
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};
447 return XvMCBadSubpicture
;
449 subpicture_priv
= subpicture
->privData
;
450 context_priv
= subpicture_priv
->context
->privData
;
451 pipe
= context_priv
->vctx
->pipe
;
453 dst_box
.width
= subpicture
->num_palette_entries
;
455 upload_sampler(pipe
, subpicture_priv
->palette
, &dst_box
, palette
, 0, 0, 0);
457 XVMC_MSG(XVMC_TRACE
, "[XvMC] Palette of Subpicture %p set.\n", subpicture
);
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
)
467 struct pipe_video_rect src_rect
= {subx
, suby
, subw
, subh
};
468 struct pipe_video_rect dst_rect
= {surfx
, surfy
, surfw
, surfh
};
470 XvMCSurfacePrivate
*surface_priv
;
471 XvMCSubpicturePrivate
*subpicture_priv
;
473 XVMC_MSG(XVMC_TRACE
, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture
, target_surface
);
478 return XvMCBadSurface
;
481 return XvMCBadSubpicture
;
483 if (target_surface
->context_id
!= subpicture
->context_id
)
486 /* TODO: Verify against subpicture independent scaling */
488 surface_priv
= target_surface
->privData
;
489 subpicture_priv
= subpicture
->privData
;
491 /* TODO: Assert rects are within bounds? Or clip? */
492 subpicture_priv
->src_rect
= src_rect
;
493 subpicture_priv
->dst_rect
= dst_rect
;
495 surface_priv
->subpicture
= subpicture
;
496 subpicture_priv
->surface
= target_surface
;
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
)
508 if (!source_surface
|| !target_surface
)
509 return XvMCBadSurface
;
512 return XvMCBadSubpicture
;
514 if (source_surface
->context_id
!= subpicture
->context_id
)
517 if (source_surface
->context_id
!= subpicture
->context_id
)
520 /* TODO: Assert rects are within bounds? Or clip? */
526 Status
XvMCSyncSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
531 return XvMCBadSubpicture
;
537 Status
XvMCFlushSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
542 return XvMCBadSubpicture
;
548 Status
XvMCGetSubpictureStatus(Display
*dpy
, XvMCSubpicture
*subpicture
, int *status
)
553 return XvMCBadSubpicture
;