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"
15 /*XXX get these from pipe's texture limits */
16 #define IMAGE_MAX_WIDTH 2048
17 #define IMAGE_MAX_HEIGHT 2048
19 #define RES_720P_X 1280
20 #define RES_720P_Y 720
23 /* The ITU-R BT.601 conversion matrix for SDTV. */
24 /* original, matrix, but we transpose it to
25 * make the shader easier
26 static const float bt_601[] = {
28 1.0, -0.3455, -0.7169, 0,
31 static const float bt_601
[] = {
33 0.0, -0.3455, 1.7790, 0,
34 1.4075, -0.7169, 0., 0,
37 /* The ITU-R BT.709 conversion matrix for HDTV. */
38 /* original, but we transpose to make the conversion
39 * in the shader easier
40 static const float bt_709[] = {
42 1.0, -0.1881, -0.47, 0,
45 static const float bt_709
[] = {
47 0.0, -0.1881, 1.8629, 0,
51 #define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
53 static Atom xvBrightness
, xvContrast
;
55 #define NUM_TEXTURED_ATTRIBUTES 2
56 static XF86AttributeRec TexturedAttributes
[NUM_TEXTURED_ATTRIBUTES
] = {
57 {XvSettable
| XvGettable
, -128, 127, "XV_BRIGHTNESS"},
58 {XvSettable
| XvGettable
, 0, 255, "XV_CONTRAST"}
62 static XF86VideoFormatRec Formats
[NUM_FORMATS
] = {
63 {15, TrueColor
}, {16, TrueColor
}, {24, TrueColor
}
66 static XF86VideoEncodingRec DummyEncoding
[1] = {
70 IMAGE_MAX_WIDTH
, IMAGE_MAX_HEIGHT
,
76 static XF86ImageRec Images
[NUM_IMAGES
] = {
82 struct xorg_xv_port_priv
{
83 struct xorg_renderer
*r
;
91 /* juggle two sets of seperate Y, U and V
93 struct pipe_texture
*yuv
[2][3];
98 stop_video(ScrnInfoPtr pScrn
, pointer data
, Bool shutdown
)
100 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
102 REGION_EMPTY(pScrn
->pScreen
, &priv
->clip
);
106 set_port_attribute(ScrnInfoPtr pScrn
,
107 Atom attribute
, INT32 value
, pointer data
)
109 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
111 if (attribute
== xvBrightness
) {
112 if ((value
< -128) || (value
> 127))
114 priv
->brightness
= value
;
115 } else if (attribute
== xvContrast
) {
116 if ((value
< 0) || (value
> 255))
118 priv
->contrast
= value
;
126 get_port_attribute(ScrnInfoPtr pScrn
,
127 Atom attribute
, INT32
* value
, pointer data
)
129 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
131 if (attribute
== xvBrightness
)
132 *value
= priv
->brightness
;
133 else if (attribute
== xvContrast
)
134 *value
= priv
->contrast
;
142 query_best_size(ScrnInfoPtr pScrn
,
144 short vid_w
, short vid_h
,
145 short drw_w
, short drw_h
,
146 unsigned int *p_w
, unsigned int *p_h
, pointer data
)
148 if (vid_w
> (drw_w
<< 1))
150 if (vid_h
> (drw_h
<< 1))
157 static INLINE
struct pipe_texture
*
158 create_component_texture(struct pipe_context
*pipe
,
159 int width
, int height
)
161 struct pipe_screen
*screen
= pipe
->screen
;
162 struct pipe_texture
*tex
= 0;
163 struct pipe_texture templ
;
165 memset(&templ
, 0, sizeof(templ
));
166 templ
.target
= PIPE_TEXTURE_2D
;
167 templ
.format
= PIPE_FORMAT_L8_UNORM
;
168 templ
.last_level
= 0;
169 templ
.width0
= width
;
170 templ
.height0
= height
;
172 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
174 tex
= screen
->texture_create(screen
, &templ
);
180 check_yuv_textures(struct xorg_xv_port_priv
*priv
, int width
, int height
)
182 struct pipe_texture
**dst
= priv
->yuv
[priv
->current_set
];
184 dst
[0]->width0
!= width
||
185 dst
[0]->height0
!= height
) {
186 pipe_texture_reference(&dst
[0], NULL
);
189 dst
[1]->width0
!= width
||
190 dst
[1]->height0
!= height
) {
191 pipe_texture_reference(&dst
[1], NULL
);
194 dst
[2]->width0
!= width
||
195 dst
[2]->height0
!= height
) {
196 pipe_texture_reference(&dst
[2], NULL
);
200 dst
[0] = create_component_texture(priv
->r
->pipe
, width
, height
);
203 dst
[1] = create_component_texture(priv
->r
->pipe
, width
, height
);
206 dst
[2] = create_component_texture(priv
->r
->pipe
, width
, height
);
208 if (!dst
[0] || !dst
[1] || !dst
[2])
215 query_image_attributes(ScrnInfoPtr pScrn
,
217 unsigned short *w
, unsigned short *h
,
218 int *pitches
, int *offsets
)
222 if (*w
> IMAGE_MAX_WIDTH
)
223 *w
= IMAGE_MAX_WIDTH
;
224 if (*h
> IMAGE_MAX_HEIGHT
)
225 *h
= IMAGE_MAX_HEIGHT
;
234 size
= (*w
+ 3) & ~3;
242 tmp
= ((*w
>> 1) + 3) & ~3;
244 pitches
[1] = pitches
[2] = tmp
;
267 copy_packed_data(ScrnInfoPtr pScrn
,
268 struct xorg_xv_port_priv
*port
,
273 unsigned short w
, unsigned short h
)
276 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
277 struct pipe_transfer
*ytrans
, *utrans
, *vtrans
;
278 struct pipe_context
*pipe
= port
->r
->pipe
;
279 char *ymap
, *vmap
, *umap
;
280 unsigned char y1
, y2
, u
, v
;
281 int yidx
, uidx
, vidx
;
282 int y_array_size
= w
* h
;
284 ytrans
= pipe
->get_tex_transfer(pipe
, dst
[0],
288 utrans
= pipe
->get_tex_transfer(pipe
, dst
[1],
292 vtrans
= pipe
->get_tex_transfer(pipe
, dst
[2],
297 ymap
= (char*)pipe
->transfer_map(pipe
, ytrans
);
298 umap
= (char*)pipe
->transfer_map(pipe
, utrans
);
299 vmap
= (char*)pipe
->transfer_map(pipe
, vtrans
);
301 yidx
= uidx
= vidx
= 0;
305 int pitches
[3], offsets
[3];
306 unsigned char *y
, *u
, *v
;
307 query_image_attributes(pScrn
, FOURCC_YV12
,
308 &w
, &h
, pitches
, offsets
);
310 y
= buf
+ offsets
[0];
311 v
= buf
+ offsets
[1];
312 u
= buf
+ offsets
[2];
313 for (i
= 0; i
< h
; ++i
) {
314 for (j
= 0; j
< w
; ++j
) {
315 int yoffset
= (w
*i
+j
);
316 int ii
= (i
|1), jj
= (j
|1);
317 int vuoffset
= (w
/2)*(ii
/2) + (jj
/2);
318 ymap
[yidx
++] = y
[yoffset
];
319 umap
[uidx
++] = u
[vuoffset
];
320 vmap
[vidx
++] = v
[vuoffset
];
326 for (i
= 0; i
< y_array_size
; i
+=2 ) {
327 /* extracting two pixels */
343 for (i
= 0; i
< y_array_size
; i
+=2 ) {
344 /* extracting two pixels */
361 debug_assert(!"Unsupported yuv format!");
365 pipe
->transfer_unmap(pipe
, ytrans
);
366 pipe
->transfer_unmap(pipe
, utrans
);
367 pipe
->transfer_unmap(pipe
, vtrans
);
368 pipe
->tex_transfer_destroy(pipe
, ytrans
);
369 pipe
->tex_transfer_destroy(pipe
, utrans
);
370 pipe
->tex_transfer_destroy(pipe
, vtrans
);
375 setup_fs_video_constants(struct xorg_renderer
*r
, boolean hdtv
)
377 const int param_bytes
= 12 * sizeof(float);
378 const float *video_constants
= (hdtv
) ? bt_709
: bt_601
;
380 renderer_set_constants(r
, PIPE_SHADER_FRAGMENT
,
381 video_constants
, param_bytes
);
385 draw_yuv(struct xorg_xv_port_priv
*port
,
386 int src_x
, int src_y
, int src_w
, int src_h
,
387 int dst_x
, int dst_y
, int dst_w
, int dst_h
)
389 struct pipe_texture
**textures
= port
->yuv
[port
->current_set
];
391 /*debug_printf(" draw_yuv([%d, %d, %d ,%d], [%d, %d, %d, %d])\n",
392 src_x, src_y, src_w, src_h,
393 dst_x, dst_y, dst_w, dst_h);*/
394 renderer_draw_yuv(port
->r
,
395 src_x
, src_y
, src_w
, src_h
,
396 dst_x
, dst_y
, dst_w
, dst_h
,
401 bind_blend_state(struct xorg_xv_port_priv
*port
)
403 struct pipe_blend_state blend
;
405 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
406 blend
.rt
[0].blend_enable
= 0;
407 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
409 /* porter&duff src */
410 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
411 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
412 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
413 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
415 cso_set_blend(port
->r
->cso
, &blend
);
420 bind_shaders(struct xorg_xv_port_priv
*port
)
422 unsigned vs_traits
= 0, fs_traits
= 0;
423 struct xorg_shader shader
;
428 shader
= xorg_shaders_get(port
->r
->shaders
, vs_traits
, fs_traits
);
429 cso_set_vertex_shader_handle(port
->r
->cso
, shader
.vs
);
430 cso_set_fragment_shader_handle(port
->r
->cso
, shader
.fs
);
434 conditional_flush(struct pipe_context
*pipe
, struct pipe_texture
**tex
,
438 for (i
= 0; i
< num
; ++i
) {
439 if (tex
[i
] && pipe
->is_texture_referenced(pipe
, tex
[i
], 0, 0) &
440 PIPE_REFERENCED_FOR_WRITE
) {
441 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
448 bind_samplers(struct xorg_xv_port_priv
*port
)
450 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
451 struct pipe_sampler_state sampler
;
452 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
454 memset(&sampler
, 0, sizeof(struct pipe_sampler_state
));
456 conditional_flush(port
->r
->pipe
, dst
, 3);
458 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
459 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
460 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
461 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
462 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
463 sampler
.normalized_coords
= 1;
465 samplers
[0] = &sampler
;
466 samplers
[1] = &sampler
;
467 samplers
[2] = &sampler
;
470 cso_set_samplers(port
->r
->cso
, 3,
471 (const struct pipe_sampler_state
**)samplers
);
472 cso_set_sampler_textures(port
->r
->cso
, 3,
477 display_video(ScrnInfoPtr pScrn
, struct xorg_xv_port_priv
*pPriv
, int id
,
479 int src_x
, int src_y
, int src_w
, int src_h
,
480 int dstX
, int dstY
, int dst_w
, int dst_h
,
483 modesettingPtr ms
= modesettingPTR(pScrn
);
489 struct exa_pixmap_priv
*dst
;
490 struct pipe_surface
*dst_surf
= NULL
;
492 exaMoveInPixmap(pPixmap
);
493 dst
= exaGetPixmapDriverPrivate(pPixmap
);
495 /*debug_printf("display_video([%d, %d, %d, %d], [%d, %d, %d, %d])\n",
496 src_x, src_y, src_w, src_h, dstX, dstY, dst_w, dst_h);*/
498 if (dst
&& !dst
->tex
) {
499 xorg_exa_set_shared_usage(pPixmap
);
500 pScrn
->pScreen
->ModifyPixmapHeader(pPixmap
, 0, 0, 0, 0, 0, NULL
);
503 if (!dst
|| !dst
->tex
)
504 XORG_FALLBACK("Xv destination %s", !dst
? "!dst" : "!dst->tex");
506 dst_surf
= xorg_gpu_surface(pPriv
->r
->pipe
->screen
, dst
);
507 hdtv
= ((src_w
>= RES_720P_X
) && (src_h
>= RES_720P_Y
));
509 REGION_TRANSLATE(pScrn
->pScreen
, dstRegion
, -pPixmap
->screen_x
,
512 dxo
= dstRegion
->extents
.x1
;
513 dyo
= dstRegion
->extents
.y1
;
515 pbox
= REGION_RECTS(dstRegion
);
516 nbox
= REGION_NUM_RECTS(dstRegion
);
518 renderer_bind_destination(pPriv
->r
, dst_surf
,
519 dst_surf
->width
, dst_surf
->height
);
521 bind_blend_state(pPriv
);
523 bind_samplers(pPriv
);
524 setup_fs_video_constants(pPriv
->r
, hdtv
);
526 DamageDamageRegion(&pPixmap
->drawable
, dstRegion
);
529 int box_x1
= pbox
->x1
;
530 int box_y1
= pbox
->y1
;
531 int box_x2
= pbox
->x2
;
532 int box_y2
= pbox
->y2
;
533 float diff_x
= (float)src_w
/ (float)dst_w
;
534 float diff_y
= (float)src_h
/ (float)dst_h
;
535 int offset_x
= box_x1
- dstX
+ pPixmap
->screen_x
;
536 int offset_y
= box_y1
- dstY
+ pPixmap
->screen_y
;
545 offset_w
= dst_w
- w
;
546 offset_h
= dst_h
- h
;
549 src_x
+ offset_x
*diff_x
, src_y
+ offset_y
*diff_y
,
550 src_w
- offset_w
*diff_x
, src_h
- offset_h
*diff_y
,
555 DamageRegionProcessPending(&pPixmap
->drawable
);
557 pipe_surface_reference(&dst_surf
, NULL
);
563 put_image(ScrnInfoPtr pScrn
,
564 short src_x
, short src_y
,
565 short drw_x
, short drw_y
,
566 short src_w
, short src_h
,
567 short drw_w
, short drw_h
,
568 int id
, unsigned char *buf
,
569 short width
, short height
,
570 Bool sync
, RegionPtr clipBoxes
, pointer data
,
573 struct xorg_xv_port_priv
*pPriv
= (struct xorg_xv_port_priv
*) data
;
574 ScreenPtr pScreen
= screenInfo
.screens
[pScrn
->scrnIndex
];
576 INT32 x1
, x2
, y1
, y2
;
587 dstBox
.x2
= drw_x
+ drw_w
;
589 dstBox
.y2
= drw_y
+ drw_h
;
591 if (!xf86XVClipVideoHelper(&dstBox
, &x1
, &x2
, &y1
, &y2
, clipBoxes
,
595 ret
= check_yuv_textures(pPriv
, width
, height
);
600 copy_packed_data(pScrn
, pPriv
, id
, buf
,
601 src_x
, src_y
, width
, height
);
603 if (pDraw
->type
== DRAWABLE_WINDOW
) {
604 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
)pDraw
);
606 pPixmap
= (PixmapPtr
)pDraw
;
609 display_video(pScrn
, pPriv
, id
, clipBoxes
,
610 src_x
, src_y
, src_w
, src_h
,
612 drw_w
, drw_h
, pPixmap
);
614 pPriv
->current_set
= (pPriv
->current_set
+ 1) & 1;
618 static struct xorg_xv_port_priv
*
619 port_priv_create(struct xorg_renderer
*r
)
621 struct xorg_xv_port_priv
*priv
= NULL
;
623 priv
= calloc(1, sizeof(struct xorg_xv_port_priv
));
630 REGION_NULL(pScreen
, &priv
->clip
);
632 debug_assert(priv
&& priv
->r
);
637 static XF86VideoAdaptorPtr
638 xorg_setup_textured_adapter(ScreenPtr pScreen
)
640 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
641 modesettingPtr ms
= modesettingPTR(pScrn
);
642 XF86VideoAdaptorPtr adapt
;
643 XF86AttributePtr attrs
;
644 DevUnion
*dev_unions
;
648 nattributes
= NUM_TEXTURED_ATTRIBUTES
;
650 debug_assert(ms
->exa
);
651 debug_assert(ms
->exa
->renderer
);
653 adapt
= calloc(1, sizeof(XF86VideoAdaptorRec
));
654 dev_unions
= calloc(nports
, sizeof(DevUnion
));
655 attrs
= calloc(nattributes
, sizeof(XF86AttributeRec
));
656 if (adapt
== NULL
|| dev_unions
== NULL
|| attrs
== NULL
) {
663 adapt
->type
= XvWindowMask
| XvInputMask
| XvImageMask
;
665 adapt
->name
= "Gallium3D Textured Video";
666 adapt
->nEncodings
= 1;
667 adapt
->pEncodings
= DummyEncoding
;
668 adapt
->nFormats
= NUM_FORMATS
;
669 adapt
->pFormats
= Formats
;
671 adapt
->pPortPrivates
= dev_unions
;
672 adapt
->nAttributes
= nattributes
;
673 adapt
->pAttributes
= attrs
;
674 memcpy(attrs
, TexturedAttributes
, nattributes
* sizeof(XF86AttributeRec
));
675 adapt
->nImages
= NUM_IMAGES
;
676 adapt
->pImages
= Images
;
677 adapt
->PutVideo
= NULL
;
678 adapt
->PutStill
= NULL
;
679 adapt
->GetVideo
= NULL
;
680 adapt
->GetStill
= NULL
;
681 adapt
->StopVideo
= stop_video
;
682 adapt
->SetPortAttribute
= set_port_attribute
;
683 adapt
->GetPortAttribute
= get_port_attribute
;
684 adapt
->QueryBestSize
= query_best_size
;
685 adapt
->PutImage
= put_image
;
686 adapt
->QueryImageAttributes
= query_image_attributes
;
688 for (i
= 0; i
< nports
; i
++) {
689 struct xorg_xv_port_priv
*priv
=
690 port_priv_create(ms
->exa
->renderer
);
692 adapt
->pPortPrivates
[i
].ptr
= (pointer
) (priv
);
700 xorg_xv_init(ScreenPtr pScreen
)
702 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
703 /*modesettingPtr ms = modesettingPTR(pScrn);*/
704 XF86VideoAdaptorPtr
*adaptors
, *new_adaptors
= NULL
;
705 XF86VideoAdaptorPtr textured_adapter
;
708 num_adaptors
= xf86XVListGenericAdaptors(pScrn
, &adaptors
);
709 new_adaptors
= malloc((num_adaptors
+ 1) * sizeof(XF86VideoAdaptorPtr
*));
710 if (new_adaptors
== NULL
)
713 memcpy(new_adaptors
, adaptors
, num_adaptors
* sizeof(XF86VideoAdaptorPtr
));
714 adaptors
= new_adaptors
;
716 /* Add the adaptors supported by our hardware. First, set up the atoms
717 * that will be used by both output adaptors.
719 xvBrightness
= MAKE_ATOM("XV_BRIGHTNESS");
720 xvContrast
= MAKE_ATOM("XV_CONTRAST");
722 textured_adapter
= xorg_setup_textured_adapter(pScreen
);
724 debug_assert(textured_adapter
);
726 if (textured_adapter
) {
727 adaptors
[num_adaptors
++] = textured_adapter
;
731 xf86XVScreenInit(pScreen
, adaptors
, num_adaptors
);
733 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
734 "Disabling Xv because no adaptors could be initialized.\n");