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 #include "util/u_format.h"
18 /*XXX get these from pipe's texture limits */
19 #define IMAGE_MAX_WIDTH 2048
20 #define IMAGE_MAX_HEIGHT 2048
22 #define RES_720P_X 1280
23 #define RES_720P_Y 720
26 /* The ITU-R BT.601 conversion matrix for SDTV. */
27 /* original, matrix, but we transpose it to
28 * make the shader easier
29 static const float bt_601[] = {
31 1.0, -0.3455, -0.7169, 0,
34 static const float bt_601
[] = {
36 0.0, -0.3455, 1.7790, 0,
37 1.4075, -0.7169, 0., 0,
40 /* The ITU-R BT.709 conversion matrix for HDTV. */
41 /* original, but we transpose to make the conversion
42 * in the shader easier
43 static const float bt_709[] = {
45 1.0, -0.1881, -0.47, 0,
48 static const float bt_709
[] = {
50 0.0, -0.1881, 1.8629, 0,
54 #define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)
56 static Atom xvBrightness
, xvContrast
;
58 #define NUM_TEXTURED_ATTRIBUTES 2
59 static XF86AttributeRec TexturedAttributes
[NUM_TEXTURED_ATTRIBUTES
] = {
60 {XvSettable
| XvGettable
, -128, 127, "XV_BRIGHTNESS"},
61 {XvSettable
| XvGettable
, 0, 255, "XV_CONTRAST"}
65 static XF86VideoFormatRec Formats
[NUM_FORMATS
] = {
66 {15, TrueColor
}, {16, TrueColor
}, {24, TrueColor
}
69 static XF86VideoEncodingRec DummyEncoding
[1] = {
73 IMAGE_MAX_WIDTH
, IMAGE_MAX_HEIGHT
,
79 static XF86ImageRec Images
[NUM_IMAGES
] = {
85 struct xorg_xv_port_priv
{
86 struct xorg_renderer
*r
;
94 /* juggle two sets of seperate Y, U and V
96 struct pipe_texture
*yuv
[2][3];
101 stop_video(ScrnInfoPtr pScrn
, pointer data
, Bool shutdown
)
103 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
105 REGION_EMPTY(pScrn
->pScreen
, &priv
->clip
);
109 set_port_attribute(ScrnInfoPtr pScrn
,
110 Atom attribute
, INT32 value
, pointer data
)
112 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
114 if (attribute
== xvBrightness
) {
115 if ((value
< -128) || (value
> 127))
117 priv
->brightness
= value
;
118 } else if (attribute
== xvContrast
) {
119 if ((value
< 0) || (value
> 255))
121 priv
->contrast
= value
;
129 get_port_attribute(ScrnInfoPtr pScrn
,
130 Atom attribute
, INT32
* value
, pointer data
)
132 struct xorg_xv_port_priv
*priv
= (struct xorg_xv_port_priv
*)data
;
134 if (attribute
== xvBrightness
)
135 *value
= priv
->brightness
;
136 else if (attribute
== xvContrast
)
137 *value
= priv
->contrast
;
145 query_best_size(ScrnInfoPtr pScrn
,
147 short vid_w
, short vid_h
,
148 short drw_w
, short drw_h
,
149 unsigned int *p_w
, unsigned int *p_h
, pointer data
)
151 if (vid_w
> (drw_w
<< 1))
153 if (vid_h
> (drw_h
<< 1))
160 static INLINE
struct pipe_texture
*
161 create_component_texture(struct pipe_context
*pipe
,
162 int width
, int height
)
164 struct pipe_screen
*screen
= pipe
->screen
;
165 struct pipe_texture
*tex
= 0;
166 struct pipe_texture templ
;
168 memset(&templ
, 0, sizeof(templ
));
169 templ
.target
= PIPE_TEXTURE_2D
;
170 templ
.format
= PIPE_FORMAT_L8_UNORM
;
171 templ
.last_level
= 0;
172 templ
.width0
= width
;
173 templ
.height0
= height
;
175 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
177 tex
= screen
->texture_create(screen
, &templ
);
183 check_yuv_textures(struct xorg_xv_port_priv
*priv
, int width
, int height
)
185 struct pipe_texture
**dst
= priv
->yuv
[priv
->current_set
];
187 dst
[0]->width0
!= width
||
188 dst
[0]->height0
!= height
) {
189 pipe_texture_reference(&dst
[0], NULL
);
192 dst
[1]->width0
!= width
||
193 dst
[1]->height0
!= height
) {
194 pipe_texture_reference(&dst
[1], NULL
);
197 dst
[2]->width0
!= width
||
198 dst
[2]->height0
!= height
) {
199 pipe_texture_reference(&dst
[2], NULL
);
203 dst
[0] = create_component_texture(priv
->r
->pipe
, width
, height
);
206 dst
[1] = create_component_texture(priv
->r
->pipe
, width
, height
);
209 dst
[2] = create_component_texture(priv
->r
->pipe
, width
, height
);
211 if (!dst
[0] || !dst
[1] || !dst
[2])
218 query_image_attributes(ScrnInfoPtr pScrn
,
220 unsigned short *w
, unsigned short *h
,
221 int *pitches
, int *offsets
)
225 if (*w
> IMAGE_MAX_WIDTH
)
226 *w
= IMAGE_MAX_WIDTH
;
227 if (*h
> IMAGE_MAX_HEIGHT
)
228 *h
= IMAGE_MAX_HEIGHT
;
237 size
= (*w
+ 3) & ~3;
245 tmp
= ((*w
>> 1) + 3) & ~3;
247 pitches
[1] = pitches
[2] = tmp
;
270 copy_packed_data(ScrnInfoPtr pScrn
,
271 struct xorg_xv_port_priv
*port
,
276 unsigned short w
, unsigned short h
)
279 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
280 struct pipe_transfer
*ytrans
, *utrans
, *vtrans
;
281 struct pipe_screen
*screen
= port
->r
->pipe
->screen
;
282 char *ymap
, *vmap
, *umap
;
283 unsigned char y1
, y2
, u
, v
;
284 int yidx
, uidx
, vidx
;
285 int y_array_size
= w
* h
;
287 ytrans
= screen
->get_tex_transfer(screen
, dst
[0],
291 utrans
= screen
->get_tex_transfer(screen
, dst
[1],
295 vtrans
= screen
->get_tex_transfer(screen
, dst
[2],
300 ymap
= (char*)screen
->transfer_map(screen
, ytrans
);
301 umap
= (char*)screen
->transfer_map(screen
, utrans
);
302 vmap
= (char*)screen
->transfer_map(screen
, vtrans
);
304 yidx
= uidx
= vidx
= 0;
308 int pitches
[3], offsets
[3];
309 unsigned char *y
, *u
, *v
;
310 query_image_attributes(pScrn
, FOURCC_YV12
,
311 &w
, &h
, pitches
, offsets
);
313 y
= buf
+ offsets
[0];
314 v
= buf
+ offsets
[1];
315 u
= buf
+ offsets
[2];
316 for (i
= 0; i
< h
; ++i
) {
317 for (j
= 0; j
< w
; ++j
) {
318 int yoffset
= (w
*i
+j
);
319 int ii
= (i
|1), jj
= (j
|1);
320 int vuoffset
= (w
/2)*(ii
/2) + (jj
/2);
321 ymap
[yidx
++] = y
[yoffset
];
322 umap
[uidx
++] = u
[vuoffset
];
323 vmap
[vidx
++] = v
[vuoffset
];
329 for (i
= 0; i
< y_array_size
; i
+=2 ) {
330 /* extracting two pixels */
346 for (i
= 0; i
< y_array_size
; i
+=2 ) {
347 /* extracting two pixels */
364 debug_assert(!"Unsupported yuv format!");
368 screen
->transfer_unmap(screen
, ytrans
);
369 screen
->transfer_unmap(screen
, utrans
);
370 screen
->transfer_unmap(screen
, vtrans
);
371 screen
->tex_transfer_destroy(ytrans
);
372 screen
->tex_transfer_destroy(utrans
);
373 screen
->tex_transfer_destroy(vtrans
);
378 setup_fs_video_constants(struct xorg_renderer
*r
, boolean hdtv
)
380 const int param_bytes
= 12 * sizeof(float);
381 const float *video_constants
= (hdtv
) ? bt_709
: bt_601
;
383 renderer_set_constants(r
, PIPE_SHADER_FRAGMENT
,
384 video_constants
, param_bytes
);
388 draw_yuv(struct xorg_xv_port_priv
*port
,
389 int src_x
, int src_y
, int src_w
, int src_h
,
390 int dst_x
, int dst_y
, int dst_w
, int dst_h
)
392 struct pipe_texture
**textures
= port
->yuv
[port
->current_set
];
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
.blend_enable
= 1;
407 blend
.colormask
|= PIPE_MASK_RGBA
;
409 /* porter&duff src */
410 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
411 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
412 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
413 blend
.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 if (dst
&& !dst
->tex
) {
496 xorg_exa_set_shared_usage(pPixmap
);
497 pScrn
->pScreen
->ModifyPixmapHeader(pPixmap
, 0, 0, 0, 0, 0, NULL
);
500 if (!dst
|| !dst
->tex
)
501 XORG_FALLBACK("Xv destination %s", !dst
? "!dst" : "!dst->tex");
503 dst_surf
= xorg_gpu_surface(pPriv
->r
->pipe
->screen
, dst
);
504 hdtv
= ((src_w
>= RES_720P_X
) && (src_h
>= RES_720P_Y
));
506 REGION_TRANSLATE(pScrn
->pScreen
, dstRegion
, -pPixmap
->screen_x
,
509 dxo
= dstRegion
->extents
.x1
;
510 dyo
= dstRegion
->extents
.y1
;
512 pbox
= REGION_RECTS(dstRegion
);
513 nbox
= REGION_NUM_RECTS(dstRegion
);
515 renderer_bind_destination(pPriv
->r
, dst_surf
,
516 dst_surf
->width
, dst_surf
->height
);
518 bind_blend_state(pPriv
);
520 bind_samplers(pPriv
);
521 setup_fs_video_constants(pPriv
->r
, hdtv
);
523 DamageDamageRegion(&pPixmap
->drawable
, dstRegion
);
526 int box_x1
= pbox
->x1
;
527 int box_y1
= pbox
->y1
;
528 int box_x2
= pbox
->x2
;
529 int box_y2
= pbox
->y2
;
530 float diff_x
= (float)src_w
/ (float)dst_w
;
531 float diff_y
= (float)src_h
/ (float)dst_h
;
532 int offset_x
= box_x1
- dstX
+ pPixmap
->screen_x
;
533 int offset_y
= box_y1
- dstY
+ pPixmap
->screen_y
;
542 offset_w
= dst_w
- w
;
543 offset_h
= dst_h
- h
;
545 draw_yuv(pPriv
, src_x
+ offset_x
*diff_x
, src_y
+ offset_y
*diff_y
,
546 src_w
- offset_w
*diff_x
, src_h
- offset_h
*diff_x
,
551 DamageRegionProcessPending(&pPixmap
->drawable
);
553 pipe_surface_reference(&dst_surf
, NULL
);
559 put_image(ScrnInfoPtr pScrn
,
560 short src_x
, short src_y
,
561 short drw_x
, short drw_y
,
562 short src_w
, short src_h
,
563 short drw_w
, short drw_h
,
564 int id
, unsigned char *buf
,
565 short width
, short height
,
566 Bool sync
, RegionPtr clipBoxes
, pointer data
,
569 struct xorg_xv_port_priv
*pPriv
= (struct xorg_xv_port_priv
*) data
;
570 ScreenPtr pScreen
= screenInfo
.screens
[pScrn
->scrnIndex
];
572 INT32 x1
, x2
, y1
, y2
;
583 dstBox
.x2
= drw_x
+ drw_w
;
585 dstBox
.y2
= drw_y
+ drw_h
;
587 if (!xf86XVClipVideoHelper(&dstBox
, &x1
, &x2
, &y1
, &y2
, clipBoxes
,
591 ret
= check_yuv_textures(pPriv
, width
, height
);
596 copy_packed_data(pScrn
, pPriv
, id
, buf
,
597 src_x
, src_y
, width
, height
);
599 if (pDraw
->type
== DRAWABLE_WINDOW
) {
600 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
)pDraw
);
602 pPixmap
= (PixmapPtr
)pDraw
;
605 display_video(pScrn
, pPriv
, id
, clipBoxes
,
606 src_x
, src_y
, src_w
, src_h
,
608 drw_w
, drw_h
, pPixmap
);
610 pPriv
->current_set
= (pPriv
->current_set
+ 1) & 1;
614 static struct xorg_xv_port_priv
*
615 port_priv_create(struct xorg_renderer
*r
)
617 struct xorg_xv_port_priv
*priv
= NULL
;
619 priv
= calloc(1, sizeof(struct xorg_xv_port_priv
));
626 REGION_NULL(pScreen
, &priv
->clip
);
628 debug_assert(priv
&& priv
->r
);
633 static XF86VideoAdaptorPtr
634 xorg_setup_textured_adapter(ScreenPtr pScreen
)
636 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
637 modesettingPtr ms
= modesettingPTR(pScrn
);
638 XF86VideoAdaptorPtr adapt
;
639 XF86AttributePtr attrs
;
640 DevUnion
*dev_unions
;
644 nattributes
= NUM_TEXTURED_ATTRIBUTES
;
646 debug_assert(ms
->exa
);
647 debug_assert(ms
->exa
->renderer
);
649 adapt
= calloc(1, sizeof(XF86VideoAdaptorRec
));
650 dev_unions
= calloc(nports
, sizeof(DevUnion
));
651 attrs
= calloc(nattributes
, sizeof(XF86AttributeRec
));
652 if (adapt
== NULL
|| dev_unions
== NULL
|| attrs
== NULL
) {
659 adapt
->type
= XvWindowMask
| XvInputMask
| XvImageMask
;
661 adapt
->name
= "Gallium3D Textured Video";
662 adapt
->nEncodings
= 1;
663 adapt
->pEncodings
= DummyEncoding
;
664 adapt
->nFormats
= NUM_FORMATS
;
665 adapt
->pFormats
= Formats
;
667 adapt
->pPortPrivates
= dev_unions
;
668 adapt
->nAttributes
= nattributes
;
669 adapt
->pAttributes
= attrs
;
670 memcpy(attrs
, TexturedAttributes
, nattributes
* sizeof(XF86AttributeRec
));
671 adapt
->nImages
= NUM_IMAGES
;
672 adapt
->pImages
= Images
;
673 adapt
->PutVideo
= NULL
;
674 adapt
->PutStill
= NULL
;
675 adapt
->GetVideo
= NULL
;
676 adapt
->GetStill
= NULL
;
677 adapt
->StopVideo
= stop_video
;
678 adapt
->SetPortAttribute
= set_port_attribute
;
679 adapt
->GetPortAttribute
= get_port_attribute
;
680 adapt
->QueryBestSize
= query_best_size
;
681 adapt
->PutImage
= put_image
;
682 adapt
->QueryImageAttributes
= query_image_attributes
;
684 for (i
= 0; i
< nports
; i
++) {
685 struct xorg_xv_port_priv
*priv
=
686 port_priv_create(ms
->exa
->renderer
);
688 adapt
->pPortPrivates
[i
].ptr
= (pointer
) (priv
);
696 xorg_xv_init(ScreenPtr pScreen
)
698 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
699 /*modesettingPtr ms = modesettingPTR(pScrn);*/
700 XF86VideoAdaptorPtr
*adaptors
, *new_adaptors
= NULL
;
701 XF86VideoAdaptorPtr textured_adapter
;
704 num_adaptors
= xf86XVListGenericAdaptors(pScrn
, &adaptors
);
705 new_adaptors
= malloc((num_adaptors
+ 1) * sizeof(XF86VideoAdaptorPtr
*));
706 if (new_adaptors
== NULL
)
709 memcpy(new_adaptors
, adaptors
, num_adaptors
* sizeof(XF86VideoAdaptorPtr
));
710 adaptors
= new_adaptors
;
712 /* Add the adaptors supported by our hardware. First, set up the atoms
713 * that will be used by both output adaptors.
715 xvBrightness
= MAKE_ATOM("XV_BRIGHTNESS");
716 xvContrast
= MAKE_ATOM("XV_CONTRAST");
718 textured_adapter
= xorg_setup_textured_adapter(pScreen
);
720 debug_assert(textured_adapter
);
722 if (textured_adapter
) {
723 adaptors
[num_adaptors
++] = textured_adapter
;
727 xf86XVScreenInit(pScreen
, adaptors
, num_adaptors
);
729 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
730 "Disabling Xv because no adaptors could be initialized.\n");