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 **************************************************************************/
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"
42 #define FOURCC_RGB 0x0000003
44 static enum pipe_format
XvIDToPipe(int xvimage_id
)
48 return PIPE_FORMAT_B8G8R8X8_UNORM
;
52 return PIPE_FORMAT_L4A4_UNORM
;
55 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
56 return PIPE_FORMAT_NONE
;
60 static unsigned NumPaletteEntries4XvID(int xvimage_id
)
71 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
76 static int PipeToComponentOrder(enum pipe_format format
, char *component_order
)
78 assert(component_order
);
81 case PIPE_FORMAT_B8G8R8X8_UNORM
:
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
;
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;
101 static Status
Validate(Display
*dpy
, XvPortID port
, int surface_type_id
, int xvimage_id
)
103 XvImageFormatValues
*subpictures
;
107 subpictures
= XvMCListSubpictureTypes(dpy
, port
, surface_type_id
, &num_subpics
);
108 if (num_subpics
< 1) {
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" \
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
);
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
);
157 return i
< num_subpics
? Success
: BadMatch
;
161 Status
XvMCCreateSubpicture(Display
*dpy
, XvMCContext
*context
, XvMCSubpicture
*subpicture
,
162 unsigned short width
, unsigned short height
, int xvimage_id
)
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
;
171 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating subpicture %p.\n", subpicture
);
176 return XvMCBadContext
;
178 context_priv
= context
->privData
;
179 vpipe
= context_priv
->vctx
->vpipe
;
182 return XvMCBadSubpicture
;
184 if (width
> context_priv
->subpicture_max_width
||
185 height
> context_priv
->subpicture_max_height
)
188 ret
= Validate(dpy
, context
->port
, context
->surface_type_id
, xvimage_id
);
192 subpicture_priv
= CALLOC(1, sizeof(XvMCSubpicturePrivate
));
193 if (!subpicture_priv
)
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
;
205 tex_templ
.width0
= util_next_power_of_two(width
);
206 tex_templ
.height0
= util_next_power_of_two(height
);
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
;
214 tex
= vpipe
->screen
->resource_create(vpipe
->screen
, &tex_templ
);
216 memset(&sampler_templ
, 0, sizeof(sampler_templ
));
217 u_sampler_view_default_template(&sampler_templ
, tex
, tex
->format
);
219 switch (xvimage_id
) {
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
;
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
;
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
;
242 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
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
);
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
;
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
;
269 tex
= vpipe
->screen
->resource_create(vpipe
->screen
, &tex_templ
);
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
);
283 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p created.\n", subpicture
);
289 Status
XvMCClearSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, short x
, short y
,
290 unsigned short width
, unsigned short height
, unsigned int color
)
292 XvMCSubpicturePrivate
*subpicture_priv
;
293 XvMCContextPrivate
*context_priv
;
294 struct pipe_box dst_box
= {x
, y
, 0, width
, height
, 1};
300 return XvMCBadSubpicture
;
302 /* Convert color to float */
303 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM
,
304 color_f
, 1, &color
, 4,
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
,
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
)
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};
327 XVMC_MSG(XVMC_TRACE
, "[XvMC] Compositing subpicture %p.\n", subpicture
);
332 return XvMCBadSubpicture
;
336 if (subpicture
->xvimage_id
!= image
->id
)
339 /* No planar support for now */
340 if (image
->num_planes
!= 1)
343 subpicture_priv
= subpicture
->privData
;
344 context_priv
= subpicture_priv
->context
->privData
;
345 vpipe
= context_priv
->vctx
->vpipe
;
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
);
351 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p composited.\n", subpicture
);
357 Status
XvMCDestroySubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
359 XvMCSubpicturePrivate
*subpicture_priv
;
361 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying subpicture %p.\n", subpicture
);
366 return XvMCBadSubpicture
;
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
);
373 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p destroyed.\n", subpicture
);
379 Status
XvMCSetSubpicturePalette(Display
*dpy
, XvMCSubpicture
*subpicture
, unsigned char *palette
)
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};
390 return XvMCBadSubpicture
;
392 subpicture_priv
= subpicture
->privData
;
393 context_priv
= subpicture_priv
->context
->privData
;
394 vpipe
= context_priv
->vctx
->vpipe
;
396 dst_box
.width
= subpicture
->num_palette_entries
;
398 vpipe
->upload_sampler(vpipe
, subpicture_priv
->palette
, &dst_box
, palette
, 0, 0, 0);
400 XVMC_MSG(XVMC_TRACE
, "[XvMC] Palette of Subpicture %p set.\n", subpicture
);
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
)
410 XvMCSurfacePrivate
*surface_priv
;
411 XvMCSubpicturePrivate
*subpicture_priv
;
413 XVMC_MSG(XVMC_TRACE
, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture
, target_surface
);
418 return XvMCBadSurface
;
421 return XvMCBadSubpicture
;
423 if (target_surface
->context_id
!= subpicture
->context_id
)
426 /* TODO: Verify against subpicture independent scaling */
428 surface_priv
= target_surface
->privData
;
429 subpicture_priv
= subpicture
->privData
;
431 /* TODO: Assert rects are within bounds? Or clip? */
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
;
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
)
454 if (!source_surface
|| !target_surface
)
455 return XvMCBadSurface
;
458 return XvMCBadSubpicture
;
460 if (source_surface
->context_id
!= subpicture
->context_id
)
463 if (source_surface
->context_id
!= subpicture
->context_id
)
466 /* TODO: Assert rects are within bounds? Or clip? */
472 Status
XvMCSyncSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
477 return XvMCBadSubpicture
;
483 Status
XvMCFlushSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
488 return XvMCBadSubpicture
;
494 Status
XvMCGetSubpictureStatus(Display
*dpy
, XvMCSubpicture
*subpicture
, int *status
)
499 return XvMCBadSubpicture
;