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"
12 #include "util/u_sampler.h"
14 #include "pipe/p_screen.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_resource *yuv[2][3];
95 struct pipe_sampler_view *yuv_views[2][3];
100 stop_video(ScrnInfoPtr pScrn, pointer data, Bool shutdown)
102 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
104 REGION_EMPTY(pScrn->pScreen, &priv->clip);
108 set_port_attribute(ScrnInfoPtr pScrn,
109 Atom attribute, INT32 value, pointer data)
111 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
113 if (attribute == xvBrightness) {
114 if ((value < -128) || (value > 127))
116 priv->brightness = value;
117 } else if (attribute == xvContrast) {
118 if ((value < 0) || (value > 255))
120 priv->contrast = value;
128 get_port_attribute(ScrnInfoPtr pScrn,
129 Atom attribute, INT32 * value, pointer data)
131 struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
133 if (attribute == xvBrightness)
134 *value = priv->brightness;
135 else if (attribute == xvContrast)
136 *value = priv->contrast;
144 query_best_size(ScrnInfoPtr pScrn,
146 short vid_w, short vid_h,
147 short drw_w, short drw_h,
148 unsigned int *p_w, unsigned int *p_h, pointer data)
150 if (vid_w > (drw_w << 1))
152 if (vid_h > (drw_h << 1))
159 static INLINE struct pipe_resource *
160 create_component_texture(struct pipe_context *pipe,
161 int width, int height)
163 struct pipe_screen *screen = pipe->screen;
164 struct pipe_resource *tex = 0;
165 struct pipe_resource templ;
167 memset(&templ, 0, sizeof(templ));
168 templ.target = PIPE_TEXTURE_2D;
169 templ.format = PIPE_FORMAT_L8_UNORM;
170 templ.last_level = 0;
171 templ.width0 = width;
172 templ.height0 = height;
174 templ.array_size = 1;
175 templ.bind = PIPE_BIND_SAMPLER_VIEW;
177 tex = screen->resource_create(screen, &templ);
183 check_yuv_textures(struct xorg_xv_port_priv *priv, int width, int height)
185 struct pipe_resource **dst = priv->yuv[priv->current_set];
186 struct pipe_sampler_view **dst_view = priv->yuv_views[priv->current_set];
187 struct pipe_sampler_view view_templ;
188 struct pipe_context *pipe = priv->r->pipe;
191 dst[0]->width0 != width ||
192 dst[0]->height0 != height) {
193 pipe_resource_reference(&dst[0], NULL);
194 pipe_sampler_view_reference(&dst_view[0], NULL);
197 dst[1]->width0 != width ||
198 dst[1]->height0 != height) {
199 pipe_resource_reference(&dst[1], NULL);
200 pipe_sampler_view_reference(&dst_view[1], NULL);
203 dst[2]->width0 != width ||
204 dst[2]->height0 != height) {
205 pipe_resource_reference(&dst[2], NULL);
206 pipe_sampler_view_reference(&dst_view[2], NULL);
210 dst[0] = create_component_texture(priv->r->pipe, width, height);
212 u_sampler_view_default_template(&view_templ,
215 dst_view[0] = pipe->create_sampler_view(pipe, dst[0], &view_templ);
220 dst[1] = create_component_texture(priv->r->pipe, width, height);
222 u_sampler_view_default_template(&view_templ,
225 dst_view[1] = pipe->create_sampler_view(pipe, dst[1], &view_templ);
230 dst[2] = create_component_texture(priv->r->pipe, width, height);
232 u_sampler_view_default_template(&view_templ,
235 dst_view[2] = pipe->create_sampler_view(pipe, dst[2], &view_templ);
239 if (!dst[0] || !dst[1] || !dst[2] || !dst_view[0] || !dst_view[1] || !dst_view[2] )
246 query_image_attributes(ScrnInfoPtr pScrn,
248 unsigned short *w, unsigned short *h,
249 int *pitches, int *offsets)
253 if (*w > IMAGE_MAX_WIDTH)
254 *w = IMAGE_MAX_WIDTH;
255 if (*h > IMAGE_MAX_HEIGHT)
256 *h = IMAGE_MAX_HEIGHT;
265 size = (*w + 3) & ~3;
273 tmp = ((*w >> 1) + 3) & ~3;
275 pitches[1] = pitches[2] = tmp;
298 copy_packed_data(ScrnInfoPtr pScrn,
299 struct xorg_xv_port_priv *port,
304 unsigned short w, unsigned short h)
307 struct pipe_resource **dst = port->yuv[port->current_set];
308 struct pipe_transfer *ytrans, *utrans, *vtrans;
309 struct pipe_context *pipe = port->r->pipe;
310 char *ymap, *vmap, *umap;
311 unsigned char y1, y2, u, v;
312 int yidx, uidx, vidx;
313 int y_array_size = w * h;
315 ytrans = pipe_get_transfer(pipe, dst[0],
319 utrans = pipe_get_transfer(pipe, dst[1],
323 vtrans = pipe_get_transfer(pipe, dst[2],
328 ymap = (char*)pipe->transfer_map(pipe, ytrans);
329 umap = (char*)pipe->transfer_map(pipe, utrans);
330 vmap = (char*)pipe->transfer_map(pipe, vtrans);
332 yidx = uidx = vidx = 0;
336 int pitches[3], offsets[3];
337 unsigned char *y, *u, *v;
338 query_image_attributes(pScrn, FOURCC_YV12,
339 &w, &h, pitches, offsets);
341 y = buf + offsets[0];
342 v = buf + offsets[1];
343 u = buf + offsets[2];
344 for (i = 0; i < h; ++i) {
345 for (j = 0; j < w; ++j) {
346 int yoffset = (w*i+j);
347 int ii = (i|1), jj = (j|1);
348 int vuoffset = (w/2)*(ii/2) + (jj/2);
349 ymap[yidx++] = y[yoffset];
350 umap[uidx++] = u[vuoffset];
351 vmap[vidx++] = v[vuoffset];
357 for (i = 0; i < y_array_size; i +=2 ) {
358 /* extracting two pixels */
374 for (i = 0; i < y_array_size; i +=2 ) {
375 /* extracting two pixels */
392 debug_assert(!"Unsupported yuv format!");
396 pipe->transfer_unmap(pipe, ytrans);
397 pipe->transfer_unmap(pipe, utrans);
398 pipe->transfer_unmap(pipe, vtrans);
399 pipe->transfer_destroy(pipe, ytrans);
400 pipe->transfer_destroy(pipe, utrans);
401 pipe->transfer_destroy(pipe, vtrans);
406 setup_fs_video_constants(struct xorg_renderer *r, boolean hdtv)
408 const int param_bytes = 12 * sizeof(float);
409 const float *video_constants = (hdtv) ? bt_709 : bt_601;
411 renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
412 video_constants, param_bytes);
416 draw_yuv(struct xorg_xv_port_priv *port,
417 float src_x, float src_y, float src_w, float src_h,
418 int dst_x, int dst_y, int dst_w, int dst_h)
420 struct pipe_resource **textures = port->yuv[port->current_set];
422 /*debug_printf(" draw_yuv([%d, %d, %d ,%d], [%d, %d, %d, %d])\n",
423 src_x, src_y, src_w, src_h,
424 dst_x, dst_y, dst_w, dst_h);*/
425 renderer_draw_yuv(port->r,
426 src_x, src_y, src_w, src_h,
427 dst_x, dst_y, dst_w, dst_h,
432 bind_blend_state(struct xorg_xv_port_priv *port)
434 struct pipe_blend_state blend;
436 memset(&blend, 0, sizeof(struct pipe_blend_state));
437 blend.rt[0].blend_enable = 0;
438 blend.rt[0].colormask = PIPE_MASK_RGBA;
440 /* porter&duff src */
441 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
442 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
443 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
444 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
446 cso_set_blend(port->r->cso, &blend);
451 bind_shaders(struct xorg_xv_port_priv *port)
453 unsigned vs_traits = 0, fs_traits = 0;
454 struct xorg_shader shader;
459 shader = xorg_shaders_get(port->r->shaders, vs_traits, fs_traits);
460 cso_set_vertex_shader_handle(port->r->cso, shader.vs);
461 cso_set_fragment_shader_handle(port->r->cso, shader.fs);
465 bind_samplers(struct xorg_xv_port_priv *port)
467 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
468 struct pipe_sampler_state sampler;
469 struct pipe_sampler_view **dst_views = port->yuv_views[port->current_set];
471 memset(&sampler, 0, sizeof(struct pipe_sampler_state));
473 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
474 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
475 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
476 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
477 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
478 sampler.normalized_coords = 1;
480 samplers[0] = &sampler;
481 samplers[1] = &sampler;
482 samplers[2] = &sampler;
485 cso_set_samplers(port->r->cso, 3,
486 (const struct pipe_sampler_state **)samplers);
487 cso_set_fragment_sampler_views(port->r->cso, 3, dst_views);
491 display_video(ScrnInfoPtr pScrn, struct xorg_xv_port_priv *pPriv, int id,
493 int src_x, int src_y, int src_w, int src_h,
494 int dstX, int dstY, int dst_w, int dst_h,
497 modesettingPtr ms = modesettingPTR(pScrn);
503 struct exa_pixmap_priv *dst;
504 struct pipe_surface *dst_surf = NULL;
506 exaMoveInPixmap(pPixmap);
507 dst = exaGetPixmapDriverPrivate(pPixmap);
509 /*debug_printf("display_video([%d, %d, %d, %d], [%d, %d, %d, %d])\n",
510 src_x, src_y, src_w, src_h, dstX, dstY, dst_w, dst_h);*/
512 if (dst && !dst->tex) {
513 xorg_exa_set_shared_usage(pPixmap);
514 pScrn->pScreen->ModifyPixmapHeader(pPixmap, 0, 0, 0, 0, 0, NULL);
517 if (!dst || !dst->tex)
518 XORG_FALLBACK("Xv destination %s", !dst ? "!dst" : "!dst->tex");
520 dst_surf = xorg_gpu_surface(pPriv->r->pipe, dst);
521 hdtv = ((src_w >= RES_720P_X) && (src_h >= RES_720P_Y));
524 REGION_TRANSLATE(pScrn->pScreen, dstRegion, -pPixmap->screen_x,
528 dxo = dstRegion->extents.x1;
529 dyo = dstRegion->extents.y1;
531 pbox = REGION_RECTS(dstRegion);
532 nbox = REGION_NUM_RECTS(dstRegion);
534 renderer_bind_destination(pPriv->r, dst_surf,
535 dst_surf->width, dst_surf->height);
537 bind_blend_state(pPriv);
539 bind_samplers(pPriv);
540 setup_fs_video_constants(pPriv->r, hdtv);
542 DamageDamageRegion(&pPixmap->drawable, dstRegion);
545 int box_x1 = pbox->x1;
546 int box_y1 = pbox->y1;
547 int box_x2 = pbox->x2;
548 int box_y2 = pbox->y2;
549 float diff_x = (float)src_w / (float)dst_w;
550 float diff_y = (float)src_h / (float)dst_h;
551 float offset_x = box_x1 - dstX;
552 float offset_y = box_y1 - dstY;
557 offset_x += pPixmap->screen_x;
558 offset_y += pPixmap->screen_y;
566 offset_w = dst_w - w;
567 offset_h = dst_h - h;
570 (float) src_x + offset_x*diff_x, (float) src_y + offset_y*diff_y,
571 (float) src_w - offset_w*diff_x, (float) src_h - offset_h*diff_y,
576 DamageRegionProcessPending(&pPixmap->drawable);
578 pipe_surface_reference(&dst_surf, NULL);
584 put_image(ScrnInfoPtr pScrn,
585 short src_x, short src_y,
586 short drw_x, short drw_y,
587 short src_w, short src_h,
588 short drw_w, short drw_h,
589 int id, unsigned char *buf,
590 short width, short height,
591 Bool sync, RegionPtr clipBoxes, pointer data,
594 struct xorg_xv_port_priv *pPriv = (struct xorg_xv_port_priv *) data;
595 ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
597 INT32 x1, x2, y1, y2;
608 dstBox.x2 = drw_x + drw_w;
610 dstBox.y2 = drw_y + drw_h;
612 if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes,
616 ret = check_yuv_textures(pPriv, width, height);
621 copy_packed_data(pScrn, pPriv, id, buf,
622 src_x, src_y, width, height);
624 if (pDraw->type == DRAWABLE_WINDOW) {
625 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr)pDraw);
627 pPixmap = (PixmapPtr)pDraw;
630 display_video(pScrn, pPriv, id, clipBoxes,
631 src_x, src_y, src_w, src_h,
633 drw_w, drw_h, pPixmap);
635 pPriv->current_set = (pPriv->current_set + 1) & 1;
639 static struct xorg_xv_port_priv *
640 port_priv_create(struct xorg_renderer *r)
642 struct xorg_xv_port_priv *priv = NULL;
644 priv = calloc(1, sizeof(struct xorg_xv_port_priv));
651 REGION_NULL(pScreen, &priv->clip);
653 debug_assert(priv && priv->r);
658 static XF86VideoAdaptorPtr
659 xorg_setup_textured_adapter(ScreenPtr pScreen)
661 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
662 modesettingPtr ms = modesettingPTR(pScrn);
663 XF86VideoAdaptorPtr adapt;
664 XF86AttributePtr attrs;
665 DevUnion *dev_unions;
669 nattributes = NUM_TEXTURED_ATTRIBUTES;
671 debug_assert(ms->exa);
672 debug_assert(ms->exa->renderer);
674 adapt = calloc(1, sizeof(XF86VideoAdaptorRec));
675 dev_unions = calloc(nports, sizeof(DevUnion));
676 attrs = calloc(nattributes, sizeof(XF86AttributeRec));
677 if (adapt == NULL || dev_unions == NULL || attrs == NULL) {
684 adapt->type = XvWindowMask | XvInputMask | XvImageMask;
686 adapt->name = "Gallium3D Textured Video";
687 adapt->nEncodings = 1;
688 adapt->pEncodings = DummyEncoding;
689 adapt->nFormats = NUM_FORMATS;
690 adapt->pFormats = Formats;
692 adapt->pPortPrivates = dev_unions;
693 adapt->nAttributes = nattributes;
694 adapt->pAttributes = attrs;
695 memcpy(attrs, TexturedAttributes, nattributes * sizeof(XF86AttributeRec));
696 adapt->nImages = NUM_IMAGES;
697 adapt->pImages = Images;
698 adapt->PutVideo = NULL;
699 adapt->PutStill = NULL;
700 adapt->GetVideo = NULL;
701 adapt->GetStill = NULL;
702 adapt->StopVideo = stop_video;
703 adapt->SetPortAttribute = set_port_attribute;
704 adapt->GetPortAttribute = get_port_attribute;
705 adapt->QueryBestSize = query_best_size;
706 adapt->PutImage = put_image;
707 adapt->QueryImageAttributes = query_image_attributes;
709 for (i = 0; i < nports; i++) {
710 struct xorg_xv_port_priv *priv =
711 port_priv_create(ms->exa->renderer);
713 adapt->pPortPrivates[i].ptr = (pointer) (priv);
721 xorg_xv_init(ScreenPtr pScreen)
723 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
724 /*modesettingPtr ms = modesettingPTR(pScrn);*/
725 XF86VideoAdaptorPtr *adaptors, *new_adaptors = NULL;
726 XF86VideoAdaptorPtr textured_adapter;
729 num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);
730 new_adaptors = malloc((num_adaptors + 1) * sizeof(XF86VideoAdaptorPtr *));
731 if (new_adaptors == NULL)
734 memcpy(new_adaptors, adaptors, num_adaptors * sizeof(XF86VideoAdaptorPtr));
735 adaptors = new_adaptors;
737 /* Add the adaptors supported by our hardware. First, set up the atoms
738 * that will be used by both output adaptors.
740 xvBrightness = MAKE_ATOM("XV_BRIGHTNESS");
741 xvContrast = MAKE_ATOM("XV_CONTRAST");
743 textured_adapter = xorg_setup_textured_adapter(pScreen);
745 debug_assert(textured_adapter);
747 if (textured_adapter) {
748 adaptors[num_adaptors++] = textured_adapter;
752 xf86XVScreenInit(pScreen, adaptors, num_adaptors);
754 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
755 "Disabling Xv because no adaptors could be initialized.\n");