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_context.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>
43 #include <vl_winsys.h>
45 #include "xvmc_private.h"
47 #define FOURCC_RGB 0x0000003
49 static enum pipe_format
XvIDToPipe(int xvimage_id
)
53 return PIPE_FORMAT_B8G8R8X8_UNORM
;
57 return PIPE_FORMAT_L4A4_UNORM
;
60 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
61 return PIPE_FORMAT_NONE
;
65 static unsigned NumPaletteEntries4XvID(int xvimage_id
)
76 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
81 static void XvIDToSwizzle(int xvimage_id
, struct pipe_sampler_view
*tmpl
)
85 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id
);
89 tmpl
->swizzle_r
= PIPE_SWIZZLE_BLUE
;
90 tmpl
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
91 tmpl
->swizzle_b
= PIPE_SWIZZLE_RED
;
92 tmpl
->swizzle_a
= PIPE_SWIZZLE_ONE
;
96 tmpl
->swizzle_r
= PIPE_SWIZZLE_ALPHA
;
97 tmpl
->swizzle_g
= PIPE_SWIZZLE_ZERO
;
98 tmpl
->swizzle_b
= PIPE_SWIZZLE_ZERO
;
99 tmpl
->swizzle_a
= PIPE_SWIZZLE_RED
;
103 tmpl
->swizzle_r
= PIPE_SWIZZLE_RED
;
104 tmpl
->swizzle_g
= PIPE_SWIZZLE_ZERO
;
105 tmpl
->swizzle_b
= PIPE_SWIZZLE_ZERO
;
106 tmpl
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
111 static int PipeToComponentOrder(enum pipe_format format
, char *component_order
)
113 assert(component_order
);
116 case PIPE_FORMAT_B8G8R8X8_UNORM
:
119 case PIPE_FORMAT_L4A4_UNORM
:
120 component_order
[0] = PIPE_SWIZZLE_RED
;
121 component_order
[1] = PIPE_SWIZZLE_GREEN
;
122 component_order
[2] = PIPE_SWIZZLE_BLUE
;
123 component_order
[3] = PIPE_SWIZZLE_ALPHA
;
127 XVMC_MSG(XVMC_ERR
, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format
);
128 component_order
[0] = 0;
129 component_order
[1] = 0;
130 component_order
[2] = 0;
131 component_order
[3] = 0;
136 static Status
Validate(Display
*dpy
, XvPortID port
, int surface_type_id
, int xvimage_id
)
138 XvImageFormatValues
*subpictures
;
142 subpictures
= XvMCListSubpictureTypes(dpy
, port
, surface_type_id
, &num_subpics
);
143 if (num_subpics
< 1) {
151 for (i
= 0; i
< num_subpics
; ++i
) {
152 if (subpictures
[i
].id
== xvimage_id
) {
153 XVMC_MSG(XVMC_TRACE
, "[XvMC] Found requested subpicture format.\n" \
155 "[XvMC] surface id=0x%08X\n" \
156 "[XvMC] image id=0x%08X\n" \
157 "[XvMC] type=%08X\n" \
158 "[XvMC] byte order=%08X\n" \
159 "[XvMC] bits per pixel=%u\n" \
160 "[XvMC] format=%08X\n" \
161 "[XvMC] num planes=%d\n",
162 port
, surface_type_id
, xvimage_id
, subpictures
[i
].type
, subpictures
[i
].byte_order
,
163 subpictures
[i
].bits_per_pixel
, subpictures
[i
].format
, subpictures
[i
].num_planes
);
164 if (subpictures
[i
].type
== XvRGB
) {
165 XVMC_MSG(XVMC_TRACE
, "[XvMC] depth=%d\n" \
166 "[XvMC] red mask=0x%08X\n" \
167 "[XvMC] green mask=0x%08X\n" \
168 "[XvMC] blue mask=0x%08X\n",
169 subpictures
[i
].depth
, subpictures
[i
].red_mask
,
170 subpictures
[i
].green_mask
, subpictures
[i
].blue_mask
);
172 else if (subpictures
[i
].type
== XvYUV
) {
173 XVMC_MSG(XVMC_TRACE
, "[XvMC] y sample bits=0x%08X\n" \
174 "[XvMC] u sample bits=0x%08X\n" \
175 "[XvMC] v sample bits=0x%08X\n" \
176 "[XvMC] horz y period=%u\n" \
177 "[XvMC] horz u period=%u\n" \
178 "[XvMC] horz v period=%u\n" \
179 "[XvMC] vert y period=%u\n" \
180 "[XvMC] vert u period=%u\n" \
181 "[XvMC] vert v period=%u\n",
182 subpictures
[i
].y_sample_bits
, subpictures
[i
].u_sample_bits
, subpictures
[i
].v_sample_bits
,
183 subpictures
[i
].horz_y_period
, subpictures
[i
].horz_u_period
, subpictures
[i
].horz_v_period
,
184 subpictures
[i
].vert_y_period
, subpictures
[i
].vert_u_period
, subpictures
[i
].vert_v_period
);
192 return i
< num_subpics
? Success
: BadMatch
;
196 Status
XvMCCreateSubpicture(Display
*dpy
, XvMCContext
*context
, XvMCSubpicture
*subpicture
,
197 unsigned short width
, unsigned short height
, int xvimage_id
)
199 XvMCContextPrivate
*context_priv
;
200 XvMCSubpicturePrivate
*subpicture_priv
;
201 struct pipe_video_context
*vpipe
;
202 struct pipe_resource tex_templ
, *tex
;
203 struct pipe_sampler_view sampler_templ
;
206 XVMC_MSG(XVMC_TRACE
, "[XvMC] Creating subpicture %p.\n", subpicture
);
211 return XvMCBadContext
;
213 context_priv
= context
->privData
;
214 vpipe
= context_priv
->vctx
->vpipe
;
217 return XvMCBadSubpicture
;
219 if (width
> context_priv
->subpicture_max_width
||
220 height
> context_priv
->subpicture_max_height
)
223 ret
= Validate(dpy
, context
->port
, context
->surface_type_id
, xvimage_id
);
227 subpicture_priv
= CALLOC(1, sizeof(XvMCSubpicturePrivate
));
228 if (!subpicture_priv
)
231 memset(&tex_templ
, 0, sizeof(tex_templ
));
232 tex_templ
.target
= PIPE_TEXTURE_2D
;
233 tex_templ
.format
= XvIDToPipe(xvimage_id
);
234 tex_templ
.last_level
= 0;
235 if (vpipe
->get_param(vpipe
, PIPE_CAP_NPOT_TEXTURES
)) {
236 tex_templ
.width0
= width
;
237 tex_templ
.height0
= height
;
240 tex_templ
.width0
= util_next_power_of_two(width
);
241 tex_templ
.height0
= util_next_power_of_two(height
);
243 tex_templ
.depth0
= 1;
244 tex_templ
.array_size
= 1;
245 tex_templ
.usage
= PIPE_USAGE_DYNAMIC
;
246 tex_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
249 tex
= vpipe
->screen
->resource_create(vpipe
->screen
, &tex_templ
);
251 memset(&sampler_templ
, 0, sizeof(sampler_templ
));
252 u_sampler_view_default_template(&sampler_templ
, tex
, tex
->format
);
253 XvIDToSwizzle(xvimage_id
, &sampler_templ
);
255 subpicture_priv
->sampler
= vpipe
->create_sampler_view(vpipe
, tex
, &sampler_templ
);
256 pipe_resource_reference(&tex
, NULL
);
257 if (!subpicture_priv
->sampler
) {
258 FREE(subpicture_priv
);
262 subpicture_priv
->context
= context
;
263 subpicture
->subpicture_id
= XAllocID(dpy
);
264 subpicture
->context_id
= context
->context_id
;
265 subpicture
->xvimage_id
= xvimage_id
;
266 subpicture
->width
= width
;
267 subpicture
->height
= height
;
268 subpicture
->num_palette_entries
= NumPaletteEntries4XvID(xvimage_id
);
269 subpicture
->entry_bytes
= PipeToComponentOrder(tex_templ
.format
, subpicture
->component_order
);
270 subpicture
->privData
= subpicture_priv
;
272 if (subpicture
->num_palette_entries
> 0) {
273 tex_templ
.target
= PIPE_TEXTURE_1D
;
274 tex_templ
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
275 tex_templ
.width0
= subpicture
->num_palette_entries
;
276 tex_templ
.height0
= 1;
277 tex_templ
.usage
= PIPE_USAGE_STATIC
;
279 tex
= vpipe
->screen
->resource_create(vpipe
->screen
, &tex_templ
);
281 memset(&sampler_templ
, 0, sizeof(sampler_templ
));
282 u_sampler_view_default_template(&sampler_templ
, tex
, tex
->format
);
283 subpicture_priv
->palette
= vpipe
->create_sampler_view(vpipe
, tex
, &sampler_templ
);
284 pipe_resource_reference(&tex
, NULL
);
285 if (!subpicture_priv
->sampler
) {
286 FREE(subpicture_priv
);
293 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p created.\n", subpicture
);
299 Status
XvMCClearSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, short x
, short y
,
300 unsigned short width
, unsigned short height
, unsigned int color
)
302 XvMCSubpicturePrivate
*subpicture_priv
;
303 XvMCContextPrivate
*context_priv
;
304 struct pipe_box dst_box
= {x
, y
, 0, width
, height
, 1};
310 return XvMCBadSubpicture
;
312 /* Convert color to float */
313 util_format_read_4f(PIPE_FORMAT_B8G8R8A8_UNORM
,
314 color_f
, 1, &color
, 4,
317 subpicture_priv
= subpicture
->privData
;
318 context_priv
= subpicture_priv
->context
->privData
;
319 /* TODO: Assert clear rect is within bounds? Or clip? */
320 context_priv
->vctx
->vpipe
->clear_sampler(context_priv
->vctx
->vpipe
,
321 subpicture_priv
->sampler
, &dst_box
,
328 Status
XvMCCompositeSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
, XvImage
*image
,
329 short srcx
, short srcy
, unsigned short width
, unsigned short height
,
330 short dstx
, short dsty
)
332 XvMCSubpicturePrivate
*subpicture_priv
;
333 XvMCContextPrivate
*context_priv
;
334 struct pipe_video_context
*vpipe
;
335 struct pipe_box dst_box
= {dstx
, dsty
, 0, width
, height
, 1};
337 XVMC_MSG(XVMC_TRACE
, "[XvMC] Compositing subpicture %p.\n", subpicture
);
342 return XvMCBadSubpicture
;
346 if (subpicture
->xvimage_id
!= image
->id
)
349 /* No planar support for now */
350 if (image
->num_planes
!= 1)
353 subpicture_priv
= subpicture
->privData
;
354 context_priv
= subpicture_priv
->context
->privData
;
355 vpipe
= context_priv
->vctx
->vpipe
;
357 /* TODO: Assert rects are within bounds? Or clip? */
358 vpipe
->upload_sampler(vpipe
, subpicture_priv
->sampler
, &dst_box
,
359 image
->data
, image
->pitches
[0], srcx
, srcy
);
361 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p composited.\n", subpicture
);
367 Status
XvMCDestroySubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
369 XvMCSubpicturePrivate
*subpicture_priv
;
371 XVMC_MSG(XVMC_TRACE
, "[XvMC] Destroying subpicture %p.\n", subpicture
);
376 return XvMCBadSubpicture
;
378 subpicture_priv
= subpicture
->privData
;
379 pipe_sampler_view_reference(&subpicture_priv
->sampler
, NULL
);
380 pipe_sampler_view_reference(&subpicture_priv
->palette
, NULL
);
381 FREE(subpicture_priv
);
383 XVMC_MSG(XVMC_TRACE
, "[XvMC] Subpicture %p destroyed.\n", subpicture
);
389 Status
XvMCSetSubpicturePalette(Display
*dpy
, XvMCSubpicture
*subpicture
, unsigned char *palette
)
391 XvMCSubpicturePrivate
*subpicture_priv
;
392 XvMCContextPrivate
*context_priv
;
393 struct pipe_video_context
*vpipe
;
394 struct pipe_box dst_box
= {0, 0, 0, 0, 1, 1};
400 return XvMCBadSubpicture
;
402 subpicture_priv
= subpicture
->privData
;
403 context_priv
= subpicture_priv
->context
->privData
;
404 vpipe
= context_priv
->vctx
->vpipe
;
406 dst_box
.width
= subpicture
->num_palette_entries
;
408 vpipe
->upload_sampler(vpipe
, subpicture_priv
->palette
, &dst_box
, palette
, 0, 0, 0);
410 XVMC_MSG(XVMC_TRACE
, "[XvMC] Palette of Subpicture %p set.\n", subpicture
);
416 Status
XvMCBlendSubpicture(Display
*dpy
, XvMCSurface
*target_surface
, XvMCSubpicture
*subpicture
,
417 short subx
, short suby
, unsigned short subw
, unsigned short subh
,
418 short surfx
, short surfy
, unsigned short surfw
, unsigned short surfh
)
420 XvMCSurfacePrivate
*surface_priv
;
421 XvMCSubpicturePrivate
*subpicture_priv
;
423 XVMC_MSG(XVMC_TRACE
, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture
, target_surface
);
428 return XvMCBadSurface
;
431 return XvMCBadSubpicture
;
433 if (target_surface
->context_id
!= subpicture
->context_id
)
436 /* TODO: Verify against subpicture independent scaling */
438 surface_priv
= target_surface
->privData
;
439 subpicture_priv
= subpicture
->privData
;
441 /* TODO: Assert rects are within bounds? Or clip? */
443 surface_priv
->subpicture
= subpicture
;
444 surface_priv
->subx
= subx
;
445 surface_priv
->suby
= suby
;
446 surface_priv
->subw
= subw
;
447 surface_priv
->subh
= subh
;
448 surface_priv
->surfx
= surfx
;
449 surface_priv
->surfy
= surfy
;
450 surface_priv
->surfw
= surfw
;
451 surface_priv
->surfh
= surfh
;
452 subpicture_priv
->surface
= target_surface
;
458 Status
XvMCBlendSubpicture2(Display
*dpy
, XvMCSurface
*source_surface
, XvMCSurface
*target_surface
,
459 XvMCSubpicture
*subpicture
, short subx
, short suby
, unsigned short subw
, unsigned short subh
,
460 short surfx
, short surfy
, unsigned short surfw
, unsigned short surfh
)
464 if (!source_surface
|| !target_surface
)
465 return XvMCBadSurface
;
468 return XvMCBadSubpicture
;
470 if (source_surface
->context_id
!= subpicture
->context_id
)
473 if (source_surface
->context_id
!= subpicture
->context_id
)
476 /* TODO: Assert rects are within bounds? Or clip? */
482 Status
XvMCSyncSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
487 return XvMCBadSubpicture
;
493 Status
XvMCFlushSubpicture(Display
*dpy
, XvMCSubpicture
*subpicture
)
498 return XvMCBadSubpicture
;
504 Status
XvMCGetSubpictureStatus(Display
*dpy
, XvMCSubpicture
*subpicture
, int *status
)
509 return XvMCBadSubpicture
;