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
] = {
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
.width
[0] = width
;
170 templ
.height
[0] = height
;
172 pf_get_block(PIPE_FORMAT_L8_UNORM
, &templ
.block
);
173 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
175 tex
= screen
->texture_create(screen
, &templ
);
181 check_yuv_textures(struct xorg_xv_port_priv
*priv
, int width
, int height
)
183 struct pipe_texture
**dst
= priv
->yuv
[priv
->current_set
];
185 dst
[0]->width
[0] != width
||
186 dst
[0]->height
[0] != height
) {
187 pipe_texture_reference(&dst
[0], NULL
);
190 dst
[1]->width
[0] != width
||
191 dst
[1]->height
[0] != height
) {
192 pipe_texture_reference(&dst
[1], NULL
);
195 dst
[2]->width
[0] != width
||
196 dst
[2]->height
[0] != height
) {
197 pipe_texture_reference(&dst
[2], NULL
);
201 dst
[0] = create_component_texture(priv
->r
->pipe
, width
, height
);
204 dst
[1] = create_component_texture(priv
->r
->pipe
, width
, height
);
207 dst
[2] = create_component_texture(priv
->r
->pipe
, width
, height
);
209 if (!dst
[0] || !dst
[1] || !dst
[2])
216 copy_packed_data(ScrnInfoPtr pScrn
,
217 struct xorg_xv_port_priv
*port
,
227 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
228 struct pipe_transfer
*ytrans
, *utrans
, *vtrans
;
229 struct pipe_screen
*screen
= port
->r
->pipe
->screen
;
230 char *ymap
, *vmap
, *umap
;
231 unsigned char y1
, y2
, u
, v
;
232 int yidx
, uidx
, vidx
;
233 int y_array_size
= w
* h
;
235 src
= buf
+ (top
* srcPitch
) + (left
<< 1);
237 ytrans
= screen
->get_tex_transfer(screen
, dst
[0],
241 utrans
= screen
->get_tex_transfer(screen
, dst
[1],
245 vtrans
= screen
->get_tex_transfer(screen
, dst
[2],
250 ymap
= (char*)screen
->transfer_map(screen
, ytrans
);
251 umap
= (char*)screen
->transfer_map(screen
, utrans
);
252 vmap
= (char*)screen
->transfer_map(screen
, vtrans
);
254 yidx
= uidx
= vidx
= 0;
258 for (i
= 0; i
< w
; ++i
) {
259 for (j
= 0; j
< h
; ++j
) {
262 u
= buf
[(j
/2) * (w
/2) + i
/2 + y_array_size
];
263 v
= buf
[(j
/2) * (w
/2) + i
/2 + y_array_size
+ y_array_size
/4];
272 for (i
= 0; i
< y_array_size
; i
+=2 ) {
273 /* extracting two pixels */
289 for (i
= 0; i
< y_array_size
; i
+=2 ) {
290 /* extracting two pixels */
307 debug_assert(!"Unsupported yuv format!");
311 screen
->transfer_unmap(screen
, ytrans
);
312 screen
->transfer_unmap(screen
, utrans
);
313 screen
->transfer_unmap(screen
, vtrans
);
314 screen
->tex_transfer_destroy(ytrans
);
315 screen
->tex_transfer_destroy(utrans
);
316 screen
->tex_transfer_destroy(vtrans
);
321 setup_fs_video_constants(struct xorg_renderer
*r
, boolean hdtv
)
323 const int param_bytes
= 12 * sizeof(float);
324 const float *video_constants
= (hdtv
) ? bt_709
: bt_601
;
326 renderer_set_constants(r
, PIPE_SHADER_FRAGMENT
,
327 video_constants
, param_bytes
);
331 draw_yuv(struct xorg_xv_port_priv
*port
,
332 int src_x
, int src_y
, int src_w
, int src_h
,
333 int dst_x
, int dst_y
, int dst_w
, int dst_h
)
335 struct pipe_texture
**textures
= port
->yuv
[port
->current_set
];
337 renderer_draw_yuv(port
->r
,
338 src_x
, src_y
, src_w
, src_h
,
339 dst_x
, dst_y
, dst_w
, dst_h
,
344 bind_blend_state(struct xorg_xv_port_priv
*port
)
346 struct pipe_blend_state blend
;
348 memset(&blend
, 0, sizeof(struct pipe_blend_state
));
349 blend
.blend_enable
= 1;
350 blend
.colormask
|= PIPE_MASK_RGBA
;
352 /* porter&duff src */
353 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
354 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
355 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
356 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
358 cso_set_blend(port
->r
->cso
, &blend
);
363 bind_shaders(struct xorg_xv_port_priv
*port
)
365 unsigned vs_traits
= 0, fs_traits
= 0;
366 struct xorg_shader shader
;
371 shader
= xorg_shaders_get(port
->r
->shaders
, vs_traits
, fs_traits
);
372 cso_set_vertex_shader_handle(port
->r
->cso
, shader
.vs
);
373 cso_set_fragment_shader_handle(port
->r
->cso
, shader
.fs
);
377 conditional_flush(struct pipe_context
*pipe
, struct pipe_texture
**tex
,
381 for (i
= 0; i
< num
; ++i
) {
382 if (tex
[i
] && pipe
->is_texture_referenced(pipe
, tex
[i
], 0, 0) &
383 PIPE_REFERENCED_FOR_WRITE
) {
384 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
391 bind_samplers(struct xorg_xv_port_priv
*port
)
393 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
394 struct pipe_sampler_state sampler
;
395 struct pipe_texture
**dst
= port
->yuv
[port
->current_set
];
397 memset(&sampler
, 0, sizeof(struct pipe_sampler_state
));
399 conditional_flush(port
->r
->pipe
, dst
, 3);
401 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP
;
402 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP
;
403 sampler
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
404 sampler
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
405 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
406 sampler
.normalized_coords
= 1;
408 samplers
[0] = &sampler
;
409 samplers
[1] = &sampler
;
410 samplers
[2] = &sampler
;
413 cso_set_samplers(port
->r
->cso
, 3,
414 (const struct pipe_sampler_state
**)samplers
);
415 cso_set_sampler_textures(port
->r
->cso
, 3,
420 display_video(ScrnInfoPtr pScrn
, struct xorg_xv_port_priv
*pPriv
, int id
,
422 int src_x
, int src_y
, int src_w
, int src_h
,
423 int dstX
, int dstY
, int dst_w
, int dst_h
,
426 modesettingPtr ms
= modesettingPTR(pScrn
);
432 struct exa_pixmap_priv
*dst
= exaGetPixmapDriverPrivate(pPixmap
);
433 struct pipe_surface
*dst_surf
= xorg_gpu_surface(pPriv
->r
->pipe
->screen
, dst
);
435 if (dst
&& !dst
->tex
) {
436 xorg_exa_set_shared_usage(pPixmap
);
437 pScrn
->pScreen
->ModifyPixmapHeader(pPixmap
, 0, 0, 0, 0, 0, NULL
);
440 if (!dst
|| !dst
->tex
)
441 XORG_FALLBACK("Xv destination %s", !dst
? "!dst" : "!dst->tex");
443 hdtv
= ((src_w
>= RES_720P_X
) && (src_h
>= RES_720P_Y
));
445 REGION_TRANSLATE(pScrn
->pScreen
, dstRegion
, -pPixmap
->screen_x
,
448 dxo
= dstRegion
->extents
.x1
;
449 dyo
= dstRegion
->extents
.y1
;
451 pbox
= REGION_RECTS(dstRegion
);
452 nbox
= REGION_NUM_RECTS(dstRegion
);
454 renderer_bind_destination(pPriv
->r
, dst_surf
);
455 bind_blend_state(pPriv
);
456 renderer_bind_rasterizer(pPriv
->r
);
458 bind_samplers(pPriv
);
459 setup_fs_video_constants(pPriv
->r
, hdtv
);
461 exaMoveInPixmap(pPixmap
);
462 DamageDamageRegion(&pPixmap
->drawable
, dstRegion
);
465 int box_x1
= pbox
->x1
;
466 int box_y1
= pbox
->y1
;
467 int box_x2
= pbox
->x2
;
468 int box_y2
= pbox
->y2
;
469 float diff_x
= (float)src_w
/ (float)dst_w
;
470 float diff_y
= (float)src_h
/ (float)dst_h
;
471 int offset_x
= box_x1
- dstX
+ pPixmap
->screen_x
;
472 int offset_y
= box_y1
- dstY
+ pPixmap
->screen_y
;
481 offset_w
= dst_w
- w
;
482 offset_h
= dst_h
- h
;
484 draw_yuv(pPriv
, src_x
+ offset_x
*diff_x
, src_y
+ offset_y
*diff_y
,
485 src_w
- offset_w
*diff_x
, src_h
- offset_h
*diff_x
,
490 DamageRegionProcessPending(&pPixmap
->drawable
);
496 put_image(ScrnInfoPtr pScrn
,
497 short src_x
, short src_y
,
498 short drw_x
, short drw_y
,
499 short src_w
, short src_h
,
500 short drw_w
, short drw_h
,
501 int id
, unsigned char *buf
,
502 short width
, short height
,
503 Bool sync
, RegionPtr clipBoxes
, pointer data
,
506 struct xorg_xv_port_priv
*pPriv
= (struct xorg_xv_port_priv
*) data
;
507 ScreenPtr pScreen
= screenInfo
.screens
[pScrn
->scrnIndex
];
509 INT32 x1
, x2
, y1
, y2
;
521 dstBox
.x2
= drw_x
+ drw_w
;
523 dstBox
.y2
= drw_y
+ drw_h
;
525 if (!xf86XVClipVideoHelper(&dstBox
, &x1
, &x2
, &y1
, &y2
, clipBoxes
,
533 srcPitch
= width
<< 1;
537 ret
= check_yuv_textures(pPriv
, width
, height
);
542 copy_packed_data(pScrn
, pPriv
, id
, buf
, srcPitch
,
543 src_x
, src_y
, width
, height
);
545 if (pDraw
->type
== DRAWABLE_WINDOW
) {
546 pPixmap
= (*pScreen
->GetWindowPixmap
)((WindowPtr
)pDraw
);
548 pPixmap
= (PixmapPtr
)pDraw
;
551 display_video(pScrn
, pPriv
, id
, clipBoxes
,
552 src_x
, src_y
, src_w
, src_h
,
554 drw_w
, drw_h
, pPixmap
);
556 pPriv
->current_set
= (pPriv
->current_set
+ 1) & 1;
561 query_image_attributes(ScrnInfoPtr pScrn
,
563 unsigned short *w
, unsigned short *h
,
564 int *pitches
, int *offsets
)
568 if (*w
> IMAGE_MAX_WIDTH
)
569 *w
= IMAGE_MAX_WIDTH
;
570 if (*h
> IMAGE_MAX_HEIGHT
)
571 *h
= IMAGE_MAX_HEIGHT
;
591 static struct xorg_xv_port_priv
*
592 port_priv_create(struct xorg_renderer
*r
)
594 struct xorg_xv_port_priv
*priv
= NULL
;
596 priv
= calloc(1, sizeof(struct xorg_xv_port_priv
));
603 REGION_NULL(pScreen
, &priv
->clip
);
605 debug_assert(priv
&& priv
->r
);
610 static XF86VideoAdaptorPtr
611 xorg_setup_textured_adapter(ScreenPtr pScreen
)
613 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
614 modesettingPtr ms
= modesettingPTR(pScrn
);
615 XF86VideoAdaptorPtr adapt
;
616 XF86AttributePtr attrs
;
617 DevUnion
*dev_unions
;
621 nattributes
= NUM_TEXTURED_ATTRIBUTES
;
623 debug_assert(ms
->exa
);
624 debug_assert(ms
->exa
->renderer
);
626 adapt
= calloc(1, sizeof(XF86VideoAdaptorRec
));
627 dev_unions
= calloc(nports
, sizeof(DevUnion
));
628 attrs
= calloc(nattributes
, sizeof(XF86AttributeRec
));
629 if (adapt
== NULL
|| dev_unions
== NULL
|| attrs
== NULL
) {
636 adapt
->type
= XvWindowMask
| XvInputMask
| XvImageMask
;
638 adapt
->name
= "Gallium3D Textured Video";
639 adapt
->nEncodings
= 1;
640 adapt
->pEncodings
= DummyEncoding
;
641 adapt
->nFormats
= NUM_FORMATS
;
642 adapt
->pFormats
= Formats
;
644 adapt
->pPortPrivates
= dev_unions
;
645 adapt
->nAttributes
= nattributes
;
646 adapt
->pAttributes
= attrs
;
647 memcpy(attrs
, TexturedAttributes
, nattributes
* sizeof(XF86AttributeRec
));
648 adapt
->nImages
= NUM_IMAGES
;
649 adapt
->pImages
= Images
;
650 adapt
->PutVideo
= NULL
;
651 adapt
->PutStill
= NULL
;
652 adapt
->GetVideo
= NULL
;
653 adapt
->GetStill
= NULL
;
654 adapt
->StopVideo
= stop_video
;
655 adapt
->SetPortAttribute
= set_port_attribute
;
656 adapt
->GetPortAttribute
= get_port_attribute
;
657 adapt
->QueryBestSize
= query_best_size
;
658 adapt
->PutImage
= put_image
;
659 adapt
->QueryImageAttributes
= query_image_attributes
;
661 for (i
= 0; i
< nports
; i
++) {
662 struct xorg_xv_port_priv
*priv
=
663 port_priv_create(ms
->exa
->renderer
);
665 adapt
->pPortPrivates
[i
].ptr
= (pointer
) (priv
);
673 xorg_xv_init(ScreenPtr pScreen
)
675 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
676 /*modesettingPtr ms = modesettingPTR(pScrn);*/
677 XF86VideoAdaptorPtr
*adaptors
, *new_adaptors
= NULL
;
678 XF86VideoAdaptorPtr textured_adapter
;
681 num_adaptors
= xf86XVListGenericAdaptors(pScrn
, &adaptors
);
682 new_adaptors
= malloc((num_adaptors
+ 1) * sizeof(XF86VideoAdaptorPtr
*));
683 if (new_adaptors
== NULL
)
686 memcpy(new_adaptors
, adaptors
, num_adaptors
* sizeof(XF86VideoAdaptorPtr
));
687 adaptors
= new_adaptors
;
689 /* Add the adaptors supported by our hardware. First, set up the atoms
690 * that will be used by both output adaptors.
692 xvBrightness
= MAKE_ATOM("XV_BRIGHTNESS");
693 xvContrast
= MAKE_ATOM("XV_CONTRAST");
695 textured_adapter
= xorg_setup_textured_adapter(pScreen
);
697 debug_assert(textured_adapter
);
699 if (textured_adapter
) {
700 adaptors
[num_adaptors
++] = textured_adapter
;
704 xf86XVScreenInit(pScreen
, adaptors
, num_adaptors
);
706 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
707 "Disabling Xv because no adaptors could be initialized.\n");