1 #include "xorg_tracker.h"
4 #include <X11/extensions/Xv.h>
8 #include "xorg_renderer.h"
9 #include "xorg_exa_tgsi.h"
11 #include "cso_cache/cso_context.h"
13 #include "pipe/p_screen.h"
14 #include "pipe/p_inlines.h"
16 /*XXX get these from pipe's texture limits */
17 #define IMAGE_MAX_WIDTH 2048
18 #define IMAGE_MAX_HEIGHT 2048
20 #define RES_720P_X 1280
21 #define RES_720P_Y 720
24 /* The ITU-R BT.601 conversion matrix for SDTV. */
25 /* original, matrix, but we transpose it to
26 * make the shader easier
27 static const float bt_601[] = {
29 1.0, -0.3455, -0.7169, 0,
32 static const float bt_601
[] = {
34 0.0, -0.3455, 1.7790, 0,
35 1.4075, -0.7169, 0., 0,
38 /* The ITU-R BT.709 conversion matrix for HDTV. */
39 /* original, but we transpose to make the conversion
40 * in the shader easier
41 static const float bt_709[] = {
43 1.0, -0.1881, -0.47, 0,
46 static const float bt_709
[] = {
48 0.0, -0.1881, 1.8629, 0,
52 #define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
54 static Atom xvBrightness
, xvContrast
;
56 #define NUM_TEXTURED_ATTRIBUTES 2
57 static XF86AttributeRec TexturedAttributes
[NUM_TEXTURED_ATTRIBUTES
] = {
58 {XvSettable
| XvGettable
, -128, 127, "XV_BRIGHTNESS"},
59 {XvSettable
| XvGettable
, 0, 255, "XV_CONTRAST"}
63 static XF86VideoFormatRec Formats
[NUM_FORMATS
] = {
64 {15, TrueColor
}, {16, TrueColor
}, {24, TrueColor
}
67 static XF86VideoEncodingRec DummyEncoding
[1] = {
71 IMAGE_MAX_WIDTH
, IMAGE_MAX_HEIGHT
,
77 static XF86ImageRec Images
[NUM_IMAGES
] = {
83 struct xorg_xv_port_priv
{
84 struct xorg_renderer
*r
;
92 /* juggle two sets of seperate Y, U and V
94 struct pipe_texture
*yuv
[2][3];
99 stop_video(ScrnInfoPtr pScrn
, pointer data
, Bool shutdown
)
101 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
103 REGION_EMPTY(pScrn
->pScreen
, &priv
->clip
);
107 set_port_attribute(ScrnInfoPtr pScrn
,
108 Atom attribute
, INT32 value
, pointer data
)
110 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
112 if (attribute
== xvBrightness
) {
113 if ((value
< -128) || (value
> 127))
115 priv
->brightness
= value
;
116 } else if (attribute
== xvContrast
) {
117 if ((value
< 0) || (value
> 255))
119 priv
->contrast
= value
;
127 get_port_attribute(ScrnInfoPtr pScrn
,
128 Atom attribute
, INT32
* value
, pointer data
)
130 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
132 if (attribute
== xvBrightness
)
133 *value
= priv
->brightness
;
134 else if (attribute
== xvContrast
)
135 *value
= priv
->contrast
;
143 query_best_size(ScrnInfoPtr pScrn
,
145 short vid_w
, short vid_h
,
146 short drw_w
, short drw_h
,
147 unsigned int *p_w
, unsigned int *p_h
, pointer data
)
149 if (vid_w
> (drw_w
<< 1))
151 if (vid_h
> (drw_h
<< 1))
158 static INLINE
struct pipe_texture
*
159 create_component_texture(struct pipe_context
*pipe
,
160 int width
, int height
)
162 struct pipe_screen
*screen
= pipe
->screen
;
163 struct pipe_texture
*tex
= 0;
164 struct pipe_texture templ
;
166 memset(&templ
, 0, sizeof(templ
));
167 templ
.target
= PIPE_TEXTURE_2D
;
168 templ
.format
= PIPE_FORMAT_L8_UNORM
;
169 templ
.last_level
= 0;
170 templ
.width
[0] = width
;
171 templ
.height
[0] = height
;
173 pf_get_block(PIPE_FORMAT_L8_UNORM
, &templ
.block
);
174 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
176 tex
= screen
->texture_create(screen
, &templ
);
182 check_yuv_textures(struct xorg_xv_port_priv
*priv
, int width
, int height
)
184 struct pipe_texture
**dst
= priv
->yuv
[priv
->current_set
];
186 dst
[0]->width
[0] != width
||
187 dst
[0]->height
[0] != height
) {
188 pipe_texture_reference(&dst
[0], NULL
);
191 dst
[1]->width
[0] != width
||
192 dst
[1]->height
[0] != height
) {
193 pipe_texture_reference(&dst
[1], NULL
);
196 dst
[2]->width
[0] != width
||
197 dst
[2]->height
[0] != height
) {
198 pipe_texture_reference(&dst
[2], NULL
);
202 dst
[0] = create_component_texture(priv
->r
->pipe
, width
, height
);
205 dst
[1] = create_component_texture(priv
->r
->pipe
, width
, height
);
208 dst
[2] = create_component_texture(priv
->r
->pipe
, width
, height
);
210 if (!dst
[0] || !dst
[1] || !dst
[2])
217 query_image_attributes(ScrnInfoPtr pScrn
,
219 unsigned short *w
, unsigned short *h
,
220 int *pitches
, int *offsets
)
224 if (*w
> IMAGE_MAX_WIDTH
)
225 *w
= IMAGE_MAX_WIDTH
;
226 if (*h
> IMAGE_MAX_HEIGHT
)
227 *h
= IMAGE_MAX_HEIGHT
;
236 size
= (*w
+ 3) & ~3;
244 tmp
= ((*w
>> 1) + 3) & ~3;
246 pitches
[1] = pitches
[2] = tmp
;
269 copy_packed_data(ScrnInfoPtr pScrn
,
270 struct xorg_xv_port_priv
*port
,
275 unsigned short w
, unsigned short h
)
278 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
279 struct pipe_transfer
*ytrans
, *utrans
, *vtrans
;
280 struct pipe_screen
*screen
= port
->r
->pipe
->screen
;
281 char *ymap
, *vmap
, *umap
;
282 unsigned char y1
, y2
, u
, v
;
283 int yidx
, uidx
, vidx
;
284 int y_array_size
= w
* h
;
286 ytrans
= screen
->get_tex_transfer(screen
, dst
[0],
290 utrans
= screen
->get_tex_transfer(screen
, dst
[1],
294 vtrans
= screen
->get_tex_transfer(screen
, dst
[2],
299 ymap
= (char*)screen
->transfer_map(screen
, ytrans
);
300 umap
= (char*)screen
->transfer_map(screen
, utrans
);
301 vmap
= (char*)screen
->transfer_map(screen
, vtrans
);
303 yidx
= uidx
= vidx
= 0;
307 int pitches
[3], offsets
[3];
308 unsigned char *y
, *u
, *v
;
309 query_image_attributes(pScrn
, FOURCC_YV12
,
310 &w
, &h
, pitches
, offsets
);
312 y
= buf
+ offsets
[0];
313 v
= buf
+ offsets
[1];
314 u
= buf
+ offsets
[2];
315 for (i
= 0; i
< h
; ++i
) {
316 for (j
= 0; j
< w
; ++j
) {
317 int yoffset
= (w
*i
+j
);
318 int ii
= (i
|1), jj
= (j
|1);
319 int vuoffset
= (w
/2)*(ii
/2) + (jj
/2);
320 ymap
[yidx
++] = y
[yoffset
];
321 umap
[uidx
++] = u
[vuoffset
];
322 vmap
[vidx
++] = v
[vuoffset
];
328 for (i
= 0; i
< y_array_size
; i
+=2 ) {
329 /* extracting two pixels */
345 for (i
= 0; i
< y_array_size
; i
+=2 ) {
346 /* extracting two pixels */
363 debug_assert(!"Unsupported yuv format!");
367 screen
->transfer_unmap(screen
, ytrans
);
368 screen
->transfer_unmap(screen
, utrans
);
369 screen
->transfer_unmap(screen
, vtrans
);
370 screen
->tex_transfer_destroy(ytrans
);
371 screen
->tex_transfer_destroy(utrans
);
372 screen
->tex_transfer_destroy(vtrans
);
377 setup_fs_video_constants(struct xorg_renderer
*r
, boolean hdtv
)
379 const int param_bytes
= 12 * sizeof(float);
380 const float *video_constants
= (hdtv
) ? bt_709
: bt_601
;
382 renderer_set_constants(r
, PIPE_SHADER_FRAGMENT
,
383 video_constants
, param_bytes
);
387 draw_yuv(struct xorg_xv_port_priv
*port
,
388 int src_x
, int src_y
, int src_w
, int src_h
,
389 int dst_x
, int dst_y
, int dst_w
, int dst_h
)
391 struct pipe_texture
**textures
= port
->yuv
[port
->current_set
];
393 renderer_draw_yuv(port
->r
,
394 src_x
, src_y
, src_w
, src_h
,
395 dst_x
, dst_y
, dst_w
, dst_h
,
400 bind_blend_state(struct xorg_xv_port_priv
*port
)
402 struct pipe_blend_state blend
;
404 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
405 blend
.blend_enable
= 1;
406 blend
.colormask
|= PIPE_MASK_RGBA
;
408 /* porter&duff src */
409 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
410 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
411 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
412 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
414 cso_set_blend(port
->r
->cso
, &blend
);
419 bind_shaders(struct xorg_xv_port_priv
*port
)
421 unsigned vs_traits
= 0, fs_traits
= 0;
422 struct xorg_shader shader
;
427 shader
= xorg_shaders_get(port
->r
->shaders
, vs_traits
, fs_traits
);
428 cso_set_vertex_shader_handle(port
->r
->cso
, shader
.vs
);
429 cso_set_fragment_shader_handle(port
->r
->cso
, shader
.fs
);
433 conditional_flush(struct pipe_context
*pipe
, struct pipe_texture
**tex
,
437 for (i
= 0; i
< num
; ++i
) {
438 if (tex
[i
] && pipe
->is_texture_referenced(pipe
, tex
[i
], 0, 0) &
439 PIPE_REFERENCED_FOR_WRITE
) {
440 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
447 bind_samplers(struct xorg_xv_port_priv
*port
)
449 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
450 struct pipe_sampler_state sampler
;
451 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
453 memset(&sampler
, 0, sizeof(struct pipe_sampler_state
));
455 conditional_flush(port
->r
->pipe
, dst
, 3);
457 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
458 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
459 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
460 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
461 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
462 sampler
.normalized_coords
= 1;
464 samplers
[0] = &sampler
;
465 samplers
[1] = &sampler
;
466 samplers
[2] = &sampler
;
469 cso_set_samplers(port
->r
->cso
, 3,
470 (const struct pipe_sampler_state
**)samplers
);
471 cso_set_sampler_textures(port
->r
->cso
, 3,
476 display_video(ScrnInfoPtr pScrn
, struct xorg_xv_port_priv
*pPriv
, int id
,
478 int src_x
, int src_y
, int src_w
, int src_h
,
479 int dstX
, int dstY
, int dst_w
, int dst_h
,
482 modesettingPtr ms
= modesettingPTR(pScrn
);
488 struct exa_pixmap_priv
*dst
= exaGetPixmapDriverPrivate(pPixmap
);
489 struct pipe_surface
*dst_surf
= NULL
;
491 if (dst
&& !dst
->tex
) {
492 xorg_exa_set_shared_usage(pPixmap
);
493 pScrn
->pScreen
->ModifyPixmapHeader(pPixmap
, 0, 0, 0, 0, 0, NULL
);
496 if (!dst
|| !dst
->tex
)
497 XORG_FALLBACK("Xv destination %s", !dst
? "!dst" : "!dst->tex");
499 dst_surf
= xorg_gpu_surface(pPriv
->r
->pipe
->screen
, dst
);
500 hdtv
= ((src_w
>= RES_720P_X
) && (src_h
>= RES_720P_Y
));
502 REGION_TRANSLATE(pScrn
->pScreen
, dstRegion
, -pPixmap
->screen_x
,
505 dxo
= dstRegion
->extents
.x1
;
506 dyo
= dstRegion
->extents
.y1
;
508 pbox
= REGION_RECTS(dstRegion
);
509 nbox
= REGION_NUM_RECTS(dstRegion
);
511 renderer_bind_destination(pPriv
->r
, dst_surf
,
512 dst_surf
->width
, dst_surf
->height
);
514 bind_blend_state(pPriv
);
516 bind_samplers(pPriv
);
517 setup_fs_video_constants(pPriv
->r
, hdtv
);
519 exaMoveInPixmap(pPixmap
);
520 DamageDamageRegion(&pPixmap
->drawable
, dstRegion
);
523 int box_x1
= pbox
->x1
;
524 int box_y1
= pbox
->y1
;
525 int box_x2
= pbox
->x2
;
526 int box_y2
= pbox
->y2
;
527 float diff_x
= (float)src_w
/ (float)dst_w
;
528 float diff_y
= (float)src_h
/ (float)dst_h
;
529 int offset_x
= box_x1
- dstX
+ pPixmap
->screen_x
;
530 int offset_y
= box_y1
- dstY
+ pPixmap
->screen_y
;
539 offset_w
= dst_w
- w
;
540 offset_h
= dst_h
- h
;
542 draw_yuv(pPriv
, src_x
+ offset_x
*diff_x
, src_y
+ offset_y
*diff_y
,
543 src_w
- offset_w
*diff_x
, src_h
- offset_h
*diff_x
,
548 DamageRegionProcessPending(&pPixmap
->drawable
);
550 pipe_surface_reference(&dst_surf
, NULL
);
556 put_image(ScrnInfoPtr pScrn
,
557 short src_x
, short src_y
,
558 short drw_x
, short drw_y
,
559 short src_w
, short src_h
,
560 short drw_w
, short drw_h
,
561 int id
, unsigned char *buf
,
562 short width
, short height
,
563 Bool sync
, RegionPtr clipBoxes
, pointer data
,
566 struct xorg_xv_port_priv
*pPriv
= (struct xorg_xv_port_priv
*) data
;
567 ScreenPtr pScreen
= screenInfo
.screens
[pScrn
->scrnIndex
];
569 INT32 x1
, x2
, y1
, y2
;
580 dstBox
.x2
= drw_x
+ drw_w
;
582 dstBox
.y2
= drw_y
+ drw_h
;
584 if (!xf86XVClipVideoHelper(&dstBox
, &x1
, &x2
, &y1
, &y2
, clipBoxes
,
588 ret
= check_yuv_textures(pPriv
, width
, height
);
593 copy_packed_data(pScrn
, pPriv
, id
, buf
,
594 src_x
, src_y
, width
, height
);
596 if (pDraw
->type
== DRAWABLE_WINDOW
) {
597 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
)pDraw
);
599 pPixmap
= (PixmapPtr
)pDraw
;
602 display_video(pScrn
, pPriv
, id
, clipBoxes
,
603 src_x
, src_y
, src_w
, src_h
,
605 drw_w
, drw_h
, pPixmap
);
607 pPriv
->current_set
= (pPriv
->current_set
+ 1) & 1;
611 static struct xorg_xv_port_priv
*
612 port_priv_create(struct xorg_renderer
*r
)
614 struct xorg_xv_port_priv
*priv
= NULL
;
616 priv
= calloc(1, sizeof(struct xorg_xv_port_priv
));
623 REGION_NULL(pScreen
, &priv
->clip
);
625 debug_assert(priv
&& priv
->r
);
630 static XF86VideoAdaptorPtr
631 xorg_setup_textured_adapter(ScreenPtr pScreen
)
633 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
634 modesettingPtr ms
= modesettingPTR(pScrn
);
635 XF86VideoAdaptorPtr adapt
;
636 XF86AttributePtr attrs
;
637 DevUnion
*dev_unions
;
641 nattributes
= NUM_TEXTURED_ATTRIBUTES
;
643 debug_assert(ms
->exa
);
644 debug_assert(ms
->exa
->renderer
);
646 adapt
= calloc(1, sizeof(XF86VideoAdaptorRec
));
647 dev_unions
= calloc(nports
, sizeof(DevUnion
));
648 attrs
= calloc(nattributes
, sizeof(XF86AttributeRec
));
649 if (adapt
== NULL
|| dev_unions
== NULL
|| attrs
== NULL
) {
656 adapt
->type
= XvWindowMask
| XvInputMask
| XvImageMask
;
658 adapt
->name
= "Gallium3D Textured Video";
659 adapt
->nEncodings
= 1;
660 adapt
->pEncodings
= DummyEncoding
;
661 adapt
->nFormats
= NUM_FORMATS
;
662 adapt
->pFormats
= Formats
;
664 adapt
->pPortPrivates
= dev_unions
;
665 adapt
->nAttributes
= nattributes
;
666 adapt
->pAttributes
= attrs
;
667 memcpy(attrs
, TexturedAttributes
, nattributes
* sizeof(XF86AttributeRec
));
668 adapt
->nImages
= NUM_IMAGES
;
669 adapt
->pImages
= Images
;
670 adapt
->PutVideo
= NULL
;
671 adapt
->PutStill
= NULL
;
672 adapt
->GetVideo
= NULL
;
673 adapt
->GetStill
= NULL
;
674 adapt
->StopVideo
= stop_video
;
675 adapt
->SetPortAttribute
= set_port_attribute
;
676 adapt
->GetPortAttribute
= get_port_attribute
;
677 adapt
->QueryBestSize
= query_best_size
;
678 adapt
->PutImage
= put_image
;
679 adapt
->QueryImageAttributes
= query_image_attributes
;
681 for (i
= 0; i
< nports
; i
++) {
682 struct xorg_xv_port_priv
*priv
=
683 port_priv_create(ms
->exa
->renderer
);
685 adapt
->pPortPrivates
[i
].ptr
= (pointer
) (priv
);
693 xorg_xv_init(ScreenPtr pScreen
)
695 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
696 /*modesettingPtr ms = modesettingPTR(pScrn);*/
697 XF86VideoAdaptorPtr
*adaptors
, *new_adaptors
= NULL
;
698 XF86VideoAdaptorPtr textured_adapter
;
701 num_adaptors
= xf86XVListGenericAdaptors(pScrn
, &adaptors
);
702 new_adaptors
= malloc((num_adaptors
+ 1) * sizeof(XF86VideoAdaptorPtr
*));
703 if (new_adaptors
== NULL
)
706 memcpy(new_adaptors
, adaptors
, num_adaptors
* sizeof(XF86VideoAdaptorPtr
));
707 adaptors
= new_adaptors
;
709 /* Add the adaptors supported by our hardware. First, set up the atoms
710 * that will be used by both output adaptors.
712 xvBrightness
= MAKE_ATOM("XV_BRIGHTNESS");
713 xvContrast
= MAKE_ATOM("XV_CONTRAST");
715 textured_adapter
= xorg_setup_textured_adapter(pScreen
);
717 debug_assert(textured_adapter
);
719 if (textured_adapter
) {
720 adaptors
[num_adaptors
++] = textured_adapter
;
724 xf86XVScreenInit(pScreen
, adaptors
, num_adaptors
);
726 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
727 "Disabling Xv because no adaptors could be initialized.\n");