summaryrefslogtreecommitdiff
path: root/vmwgfx/vmwgfx_tex_video.c
blob: 2cc44bdb6c8d7e95a515e924f4b5c937c4681043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
 * Copyright 2009-2011 VMWare, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Author: Thomas Hellstrom <thellstrom@vmware.com>
 * Author: Zack Rusin <zackr@vmware.com>
 */

#include "vmwgfx_driver.h"
#include "vmwgfx_drmi.h"
#include "vmwgfx_saa.h"

#include <xf86xv.h>
#include <X11/extensions/Xv.h>
#include <fourcc.h>
#include <xa_tracker.h>
#include <xa_context.h>
#include <math.h>

/*XXX get these from pipe's texture limits */
#define IMAGE_MAX_WIDTH		2048
#define IMAGE_MAX_HEIGHT	2048

#define RES_720P_X 1280
#define RES_720P_Y 720


#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)

/*
 * ITU-R BT.601, BT.709 transfer matrices.
 * [R', G', B'] values are in the range [0, 1], Y' is in the range [0,1]
 * and [Pb, Pr] components are in the range [-0.5, 0.5].
 *
 * The matrices are transposed to fit the xa conversion matrix format.
 */

static const float bt_601[] = {
    1.f, 1.f, 1.f, 0.f,
    0.f, -0.344136f, 1.772f, 0.f,
    1.402f,  -0.714136f, 0.f, 0.f
};

static const float bt_709[] = {
    1.f, 1.f, 1.f, 0.f,
    0.f, -0.187324f, 1.8556f, 0.f,
    1.5748f, -0.468124f, 0.f, 0.f
};

static Atom xvBrightness, xvContrast, xvSaturation, xvHue;

#define NUM_TEXTURED_ATTRIBUTES 4
static XF86AttributeRec TexturedAttributes[NUM_TEXTURED_ATTRIBUTES] = {
   {XvSettable | XvGettable, -1000, 1000, "XV_BRIGHTNESS"},
   {XvSettable | XvGettable, -1000, 1000, "XV_CONTRAST"},
   {XvSettable | XvGettable, -1000, 1000, "XV_SATURATION"},
   {XvSettable | XvGettable, -1000, 1000, "XV_HUE"}
};

#define NUM_FORMATS 3
static XF86VideoFormatRec Formats[NUM_FORMATS] = {
   {15, TrueColor}, {16, TrueColor}, {24, TrueColor}
};

static XF86VideoEncodingRec DummyEncoding[1] = {
   {
      0,
      "XV_IMAGE",
      IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT,
      {1, 1}
   }
};

#define NUM_IMAGES 3
static XF86ImageRec Images[NUM_IMAGES] = {
   XVIMAGE_UYVY,
   XVIMAGE_YUY2,
   XVIMAGE_YV12,
};

struct xorg_xv_port_priv {
    struct xa_tracker *xat;
    struct xa_context *r;
    struct xa_fence *fence;

    RegionRec clip;

    int brightness;
    int contrast;
    int saturation;
    int hue;

    int current_set;
    struct vmwgfx_dmabuf *bounce[2][3];
    struct xa_surface *yuv[3];

    int drm_fd;

    Bool hdtv;
    float uv_offset;
    float uv_scale;
    float y_offset;
    float y_scale;
    float rgb_offset;
    float rgb_scale;
    float sinhue;
    float coshue;
    float cm[16];
};

/*
 * vmwgfx_update_conversion_matrix - Compute the effective color conversion
 * matrix.
 *
 * Applies yuv- and resulting rgb scales and offsets to compute the correct
 * color conversion matrix. These scales and offsets are properties of the
 * video stream and can be adjusted using XV properties as well.
 */
static void
vmwgfx_update_conversion_matrix(struct xorg_xv_port_priv *priv)
{
    int i;
    float *cm = priv->cm;
    static const float *bt;

    bt = (priv->hdtv) ? bt_709 : bt_601;

    memcpy(cm, bt, sizeof(bt_601));

    /*
     * Apply hue rotation
     */
    cm[4] = priv->coshue * bt[4] - priv->sinhue * bt[8];
    cm[8] = priv->sinhue * bt[4] + priv->coshue * bt[8];
    cm[5] = priv->coshue * bt[5] - priv->sinhue * bt[9];
    cm[9] = priv->sinhue * bt[5] + priv->coshue * bt[9];
    cm[6] = priv->coshue * bt[6] - priv->sinhue * bt[10];
    cm[10] = priv->sinhue * bt[6] + priv->coshue * bt[10];

    /*
     * Adjust for yuv scales in input and rgb scale in the converted output.
     */
    for(i = 0; i < 3; ++i) {
	cm[i] *= (priv->y_scale*priv->rgb_scale);
	cm[i+4] *= (priv->uv_scale*priv->rgb_scale);
	cm[i+8] *= (priv->uv_scale*priv->rgb_scale);
    }

    /*
     * Adjust for yuv offsets in input and rgb offset in the converted output.
     */
    for (i = 0; i < 3; ++i)
	cm[i+12] = -cm[i]*priv->y_offset - (cm[i+4] + cm[i+8])*priv->uv_offset
	    - priv->rgb_offset*priv->rgb_scale;

    /*
     * Alpha is 1, unconditionally.
     */
    cm[15] = 1.f;
}


static void
stop_video(ScrnInfoPtr pScrn, pointer data, Bool shutdown)
{
   struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;
   int i, j;

   REGION_EMPTY(pScrn->pScreen, &priv->clip);
   if (shutdown) {

       /*
	* No need to destroy the xa context or xa tracker since
	* they are copied from the screen resources.
	*/

       xa_fence_destroy(priv->fence);
       priv->fence = NULL;

       for (i=0; i<3; ++i) {
	   if (priv->yuv[i]) {
	       xa_surface_destroy(priv->yuv[i]);
	       priv->yuv[i] = NULL;
	   }
	   for (j=0; j<2; ++j) {
	       if (priv->bounce[j][i]) {
		   vmwgfx_dmabuf_destroy(priv->bounce[j][i]);
		   priv->bounce[0][i] = NULL;
	       }
	   }
       }
   }
}

static int
set_port_attribute(ScrnInfoPtr pScrn,
                   Atom attribute, INT32 value, pointer data)
{
   struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;

   if (attribute == xvBrightness) {
       if ((value < -1000) || (value > 1000))
	   return BadValue;

       priv->brightness = value;
       priv->y_offset = -((float) value)/1000.f;

   } else if (attribute == xvContrast) {
       if ((value < -1000) || (value > 1000))
	   return BadValue;

       priv->contrast = value;
       priv->rgb_scale = ((float) value + 1000.f)/1000.f;

   } else if (attribute == xvSaturation) {
       if ((value < -1000) || (value > 1000))
	   return BadValue;

       priv->saturation = value;
       priv->uv_scale = ((float) value + 1000.f)/1000.f;

   } else if (attribute == xvHue) {
       double hue_angle;

       if ((value < -1000) || (value > 1000))
	   return BadValue;

       priv->hue = value;
       hue_angle = (double) value * M_PI / 1000.;
       priv->sinhue = sin(hue_angle);
       priv->coshue = cos(hue_angle);

   } else
      return BadMatch;

   vmwgfx_update_conversion_matrix(priv);
   return Success;
}

static int
get_port_attribute(ScrnInfoPtr pScrn,
                   Atom attribute, INT32 * value, pointer data)
{
    struct xorg_xv_port_priv *priv = (struct xorg_xv_port_priv *)data;

    if (attribute == xvBrightness)
	*value = priv->brightness;
    else if (attribute == xvContrast)
	*value = priv->contrast;
    else if (attribute == xvSaturation)
	*value = priv->saturation;
    else if (attribute == xvHue)
	*value = priv->hue;
    else
	return BadMatch;

    return Success;
}

static void
query_best_size(ScrnInfoPtr pScrn,
                Bool motion,
                short vid_w, short vid_h,
                short drw_w, short drw_h,
                unsigned int *p_w, unsigned int *p_h, pointer data)
{
   if (vid_w > (drw_w << 1))
      drw_w = vid_w >> 1;
   if (vid_h > (drw_h << 1))
      drw_h = vid_h >> 1;

   *p_w = drw_w;
   *p_h = drw_h;
}

static int
check_yuv_surfaces(struct xorg_xv_port_priv *priv,  int width, int height)
{
    struct xa_surface **yuv = priv->yuv;
    struct vmwgfx_dmabuf **bounce = priv->bounce[priv->current_set];
    int ret = 0;
    int i;
    size_t size;

    for (i=0; i<3; ++i) {
	if (!yuv[i])
	    yuv[i] = xa_surface_create(priv->xat, width, height, 8,
				       xa_type_yuv_component,
				       xa_format_unknown, 0);
	else
	    ret = xa_surface_redefine(yuv[i], width, height, 8,
				      xa_type_yuv_component,
				      xa_format_unknown, 0, 0);
	if (ret || !yuv[i])
	    return BadAlloc;

	size = width * height;

	if (bounce[i] && (bounce[i]->size < size ||
			  bounce[i]->size > 2*size)) {
	    vmwgfx_dmabuf_destroy(bounce[i]);
	    bounce[i] = NULL;
	}

	if (!bounce[i]) {
	    bounce[i] = vmwgfx_dmabuf_alloc(priv->drm_fd, size);
	    if (!bounce[i])
		return BadAlloc;
	}
    }
    return Success;
}

static int
query_image_attributes(ScrnInfoPtr pScrn,
                       int id,
                       unsigned short *w, unsigned short *h,
                       int *pitches, int *offsets)
{
   int size, tmp;

   if (*w > IMAGE_MAX_WIDTH)
      *w = IMAGE_MAX_WIDTH;
   if (*h > IMAGE_MAX_HEIGHT)
      *h = IMAGE_MAX_HEIGHT;

   *w = (*w + 1) & ~1;
   if (offsets)
      offsets[0] = 0;

   switch (id) {
   case FOURCC_YV12:
      *h = (*h + 1) & ~1;
      size = (*w + 3) & ~3;
      if (pitches) {
         pitches[0] = size;
      }
      size *= *h;
      if (offsets) {
         offsets[1] = size;
      }
      tmp = ((*w >> 1) + 3) & ~3;
      if (pitches) {
         pitches[1] = pitches[2] = tmp;
      }
      tmp *= (*h >> 1);
      size += tmp;
      if (offsets) {
         offsets[2] = size;
      }
      size += tmp;
      break;
   case FOURCC_UYVY:
   case FOURCC_YUY2:
   default:
      size = *w << 1;
      if (pitches)
	 pitches[0] = size;
      size *= *h;
      break;
   }

   return size;
}

static int
copy_packed_data(ScrnInfoPtr pScrn,
                 struct xorg_xv_port_priv *port,
                 int id,
                 unsigned char *buf,
                 int left,
                 int top,
                 unsigned short w, unsigned short h)
{
   int i, j;
   struct vmwgfx_dmabuf **bounce = port->bounce[port->current_set];
   char *ymap, *vmap, *umap;
   unsigned char y1, y2, u, v;
   int yidx, uidx, vidx;
   int y_array_size = w * h;
   int ret = BadAlloc;

   /*
    * Here, we could use xa_surface_[map|unmap], but given the size of
    * the yuv textures, that could stress the xa tracker dma buffer pool,
    * particularaly with multiple videos rendering simultaneously.
    *
    * Instead, cheat and allocate vmwgfx dma buffers directly.
    */

   ymap = (char *)vmwgfx_dmabuf_map(bounce[0]);
   if (!ymap)
       return BadAlloc;
   umap = (char *)vmwgfx_dmabuf_map(bounce[1]);
   if (!umap)
       goto out_no_umap;
   vmap = (char *)vmwgfx_dmabuf_map(bounce[2]);
   if (!vmap)
       goto out_no_vmap;


   yidx = uidx = vidx = 0;

   switch (id) {
   case FOURCC_YV12: {
      int pitches[3], offsets[3];
      unsigned char *y, *u, *v;
      query_image_attributes(pScrn, FOURCC_YV12,
                             &w, &h, pitches, offsets);

      y = buf + offsets[0];
      v = buf + offsets[1];
      u = buf + offsets[2];
      for (i = 0; i < h; ++i) {
         for (j = 0; j < w; ++j) {
            int yoffset = (w*i+j);
            int ii = (i|1), jj = (j|1);
            int vuoffset = (w/2)*(ii/2) + (jj/2);
            ymap[yidx++] = y[yoffset];
            umap[uidx++] = u[vuoffset];
            vmap[vidx++] = v[vuoffset];
         }
      }
   }
      break;
   case FOURCC_UYVY:
      for (i = 0; i < y_array_size; i +=2 ) {
         /* extracting two pixels */
         u  = buf[0];
         y1 = buf[1];
         v  = buf[2];
         y2 = buf[3];
         buf += 4;

         ymap[yidx++] = y1;
         ymap[yidx++] = y2;
         umap[uidx++] = u;
         umap[uidx++] = u;
         vmap[vidx++] = v;
         vmap[vidx++] = v;
      }
      break;
   case FOURCC_YUY2:
      for (i = 0; i < y_array_size; i +=2 ) {
         /* extracting two pixels */
         y1 = buf[0];
         u  = buf[1];
         y2 = buf[2];
         v  = buf[3];

         buf += 4;

         ymap[yidx++] = y1;
         ymap[yidx++] = y2;
         umap[uidx++] = u;
         umap[uidx++] = u;
         vmap[vidx++] = v;
         vmap[vidx++] = v;
      }
      break;
   default:
       ret = BadAlloc;
       break;
   }

   ret = Success;
   vmwgfx_dmabuf_unmap(bounce[2]);
  out_no_vmap:
   vmwgfx_dmabuf_unmap(bounce[1]);
  out_no_umap:
   vmwgfx_dmabuf_unmap(bounce[0]);

   if (ret == Success) {
       struct xa_surface *srf;
       struct vmwgfx_dmabuf *buf;
       uint32_t handle;
       unsigned int stride;
       BoxRec box;
       RegionRec reg;

       box.x1 = 0;
       box.x2 = w;
       box.y1 = 0;
       box.y2 = h;

       REGION_INIT(pScrn->pScreen, &reg, &box, 1);

       for (i=0; i<3; ++i) {
	   srf = port->yuv[i];
	   buf = bounce[i];

	   if (xa_surface_handle(srf, &handle, &stride) != 0) {
	       ret = BadAlloc;
	       break;
	   }

	   if (vmwgfx_dma(0, 0, &reg, buf, w, handle, 1) != 0) {
	       ret = BadAlloc;
	       break;
	   }
       }
       REGION_UNINIT(pScrn->pScreen, &reg);
   }

   return ret;
}


static int
display_video(ScreenPtr pScreen, struct xorg_xv_port_priv *pPriv, int id,
              RegionPtr dstRegion,
              int src_x, int src_y, int src_w, int src_h,
              int dst_x, int dst_y, int dst_w, int dst_h,
              PixmapPtr pPixmap)
{
    struct vmwgfx_saa_pixmap *vpix = vmwgfx_saa_pixmap(pPixmap);
    Bool hdtv;
    RegionRec reg;
    int ret = BadAlloc;
    int blit_ret;

    REGION_NULL(pScreen, &reg);

    if (!vmwgfx_hw_accel_validate(pPixmap, 0, XA_FLAG_RENDER_TARGET, 0, &reg))
	goto out_no_dst;

   hdtv = ((src_w >= RES_720P_X) && (src_h >= RES_720P_Y));
   if (hdtv != pPriv->hdtv) {
       pPriv->hdtv = hdtv;
       vmwgfx_update_conversion_matrix(pPriv);
   }

#ifdef COMPOSITE

   /*
    * For redirected windows, we need to fix up the destination coordinates.
    */

   REGION_TRANSLATE(pScreen, dstRegion, -pPixmap->screen_x,
		    -pPixmap->screen_y);
   dst_x -= pPixmap->screen_x;
   dst_y -= pPixmap->screen_y;
#endif

   /*
    * Throttle on previous blit.
    */

   if (pPriv->fence) {
       (void) xa_fence_wait(pPriv->fence, 1000000000ULL);
       xa_fence_destroy(pPriv->fence);
       pPriv->fence = NULL;
   }

   DamageRegionAppend(&pPixmap->drawable, dstRegion);

   blit_ret = xa_yuv_planar_blit(pPriv->r, src_x, src_y, src_w, src_h,
				 dst_x, dst_y, dst_w, dst_h,
				 (struct xa_box *)REGION_RECTS(dstRegion),
				 REGION_NUM_RECTS(dstRegion),
				 pPriv->cm,
				 vpix->hw, pPriv->yuv);

   saa_pixmap_dirty(pPixmap, TRUE, dstRegion);
   DamageRegionProcessPending(&pPixmap->drawable);
   ret = Success;

   if (!blit_ret) {
       ret = Success;
       pPriv->fence = xa_fence_get(pPriv->r);
   } else
       ret = BadAlloc;

   out_no_dst:
   REGION_UNINIT(pScreen, &reg);
   return ret;
}

static int
put_image(ScrnInfoPtr pScrn,
          short src_x, short src_y,
          short drw_x, short drw_y,
          short src_w, short src_h,
          short drw_w, short drw_h,
          int id, unsigned char *buf,
          short width, short height,
          Bool sync, RegionPtr clipBoxes, pointer data,
          DrawablePtr pDraw)
{
   struct xorg_xv_port_priv *pPriv = (struct xorg_xv_port_priv *) data;
   ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
   PixmapPtr pPixmap;
   INT32 x1, x2, y1, y2;
   BoxRec dstBox;
   int ret;

   /* Clip */
   x1 = src_x;
   x2 = src_x + src_w;
   y1 = src_y;
   y2 = src_y + src_h;

   dstBox.x1 = drw_x;
   dstBox.x2 = drw_x + drw_w;
   dstBox.y1 = drw_y;
   dstBox.y2 = drw_y + drw_h;

   if (!xf86XVClipVideoHelper(&dstBox, &x1, &x2, &y1, &y2, clipBoxes,
			      width, height))
      return Success;

   ret = check_yuv_surfaces(pPriv, width, height);
   if (ret)
       return ret;

   ret = copy_packed_data(pScrn, pPriv, id, buf,
			  src_x, src_y, width, height);
   if (ret)
       return ret;

   if (pDraw->type == DRAWABLE_WINDOW) {
      pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr)pDraw);
   } else {
      pPixmap = (PixmapPtr)pDraw;
   }

   display_video(pScrn->pScreen, pPriv, id, clipBoxes,
                 src_x, src_y, src_w, src_h,
                 drw_x, drw_y,
                 drw_w, drw_h, pPixmap);

   pPriv->current_set = (pPriv->current_set + 1) & 1;
   return Success;
}

static struct xorg_xv_port_priv *
port_priv_create(struct xa_tracker *xat, struct xa_context *r,
		 int drm_fd)
{
   struct xorg_xv_port_priv *priv = NULL;

   priv = calloc(1, sizeof(struct xorg_xv_port_priv));

   if (!priv)
      return NULL;

   priv->r = r;
   priv->xat = xat;
   priv->drm_fd = drm_fd;
   REGION_NULL(pScreen, &priv->clip);
   priv->hdtv = FALSE;
   priv->uv_offset = 0.5f;
   priv->uv_scale = 1.f;
   priv->y_offset = 0.f;
   priv->y_scale = 1.f;
   priv->rgb_offset = 0.f;
   priv->rgb_scale = 1.f;
   priv->sinhue = 0.f;
   priv->coshue = 1.f;

   vmwgfx_update_conversion_matrix(priv);

   return priv;
}

static void
vmwgfx_free_textured_adaptor(XF86VideoAdaptorPtr adaptor, Bool free_ports)
{
    if (free_ports) {
	int i;

	for(i=0; i<adaptor->nPorts; ++i) {
	    free(adaptor->pPortPrivates[i].ptr);
	}
    }

    free(adaptor->pAttributes);
    free(adaptor->pPortPrivates);
    xf86XVFreeVideoAdaptorRec(adaptor);
}

static XF86VideoAdaptorPtr
xorg_setup_textured_adapter(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   modesettingPtr ms = modesettingPTR(pScrn);
   XF86VideoAdaptorPtr adapt;
   XF86AttributePtr attrs;
   DevUnion *dev_unions;
   int nports = 16, i;
   int nattributes;
   struct xa_context *xar;

   /*
    * Use the XA default context since we don't expect the X server
    * to render from multiple threads.
    */

   xar = xa_context_default(ms->xat);
   nattributes = NUM_TEXTURED_ATTRIBUTES;

   adapt = calloc(1, sizeof(XF86VideoAdaptorRec));
   dev_unions = calloc(nports, sizeof(DevUnion));
   attrs = calloc(nattributes, sizeof(XF86AttributeRec));
   if (adapt == NULL || dev_unions == NULL || attrs == NULL) {
      free(adapt);
      free(dev_unions);
      free(attrs);
      return NULL;
   }

   adapt->type = XvWindowMask | XvInputMask | XvImageMask;
   adapt->flags = 0;
   adapt->name = "XA G3D Textured Video";
   adapt->nEncodings = 1;
   adapt->pEncodings = DummyEncoding;
   adapt->nFormats = NUM_FORMATS;
   adapt->pFormats = Formats;
   adapt->nPorts = 0;
   adapt->pPortPrivates = dev_unions;
   adapt->nAttributes = nattributes;
   adapt->pAttributes = attrs;
   memcpy(attrs, TexturedAttributes, nattributes * sizeof(XF86AttributeRec));
   adapt->nImages = NUM_IMAGES;
   adapt->pImages = Images;
   adapt->PutVideo = NULL;
   adapt->PutStill = NULL;
   adapt->GetVideo = NULL;
   adapt->GetStill = NULL;
   adapt->StopVideo = stop_video;
   adapt->SetPortAttribute = set_port_attribute;
   adapt->GetPortAttribute = get_port_attribute;
   adapt->QueryBestSize = query_best_size;
   adapt->PutImage = put_image;
   adapt->QueryImageAttributes = query_image_attributes;


   for (i = 0; i < nports; i++) {
       struct xorg_xv_port_priv *priv =
	  port_priv_create(ms->xat, xar, ms->fd);

      adapt->pPortPrivates[i].ptr = (pointer) (priv);
      adapt->nPorts++;
   }

   return adapt;
}

void
xorg_xv_init(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   modesettingPtr ms = modesettingPTR(pScrn);
   XF86VideoAdaptorPtr *adaptors, *new_adaptors = NULL;
   XF86VideoAdaptorPtr textured_adapter = NULL, overlay_adaptor = NULL;
   int num_adaptors;

   num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);
   new_adaptors = malloc((num_adaptors + 2) * sizeof(XF86VideoAdaptorPtr *));
   if (new_adaptors == NULL)
       return;

   memcpy(new_adaptors, adaptors, num_adaptors * sizeof(XF86VideoAdaptorPtr));
   adaptors = new_adaptors;

   /* Add the adaptors supported by our hardware.  First, set up the atoms
    * that will be used by both output adaptors.
    */
   xvBrightness = MAKE_ATOM("XV_BRIGHTNESS");
   xvContrast = MAKE_ATOM("XV_CONTRAST");
   xvSaturation = MAKE_ATOM("XV_SATURATION");
   xvHue = MAKE_ATOM("XV_HUE");

   if (ms->xat) {
       textured_adapter = xorg_setup_textured_adapter(pScreen);
       if (textured_adapter)
	   adaptors[num_adaptors++] = textured_adapter;
   } else {
       xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		  "No 3D acceleration. Not setting up textured video.\n");
   }

   overlay_adaptor = vmw_video_init_adaptor(pScrn);
   if (overlay_adaptor)
       adaptors[num_adaptors++] = overlay_adaptor;

   if (num_adaptors) {
       Bool ret;
       ret = xf86XVScreenInit(pScreen, adaptors, num_adaptors);
       if (textured_adapter)
	   vmwgfx_free_textured_adaptor(textured_adapter, !ret);
       if (overlay_adaptor)
	   vmw_video_free_adaptor(overlay_adaptor, !ret);
       if (!ret)
	   xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		      "Failed to initialize Xv.\n");
   } else {
       xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
		  "Disabling Xv because no adaptors could be initialized.\n");
   }


  out_err_mem:
   free(adaptors);
}