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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
|
/***************************************************************************
Copyright 2000 Intel Corporation. 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 INTEL, 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.
**************************************************************************/
/*
* i830_video.c: i830/i845 Xv driver.
*
* Copyright © 2002 by Alan Hourihane and David Dawes
*
* Authors:
* Alan Hourihane <alanh@tungstengraphics.com>
* David Dawes <dawes@xfree86.org>
*
* Derived from i810 Xv driver:
*
* Authors of i810 code:
* Jonathan Bian <jonathan.bian@intel.com>
* Offscreen Images:
* Matt Sottek <matthew.j.sottek@intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <inttypes.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include "sna.h"
#include "sna_reg.h"
#include "sna_video.h"
#include "intel_options.h"
#include <xf86xv.h>
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/types.h>
#include <sys/endian.h>
#ifdef __OpenBSD__
#define bswap_32 swap32
#else
#define bswap_32 bswap32
#endif
#else
#include <byteswap.h>
#endif
#ifdef SNA_XVMC
#define _SNA_XVMC_SERVER_
#include "sna_video_hwmc.h"
#else
static inline void sna_video_xvmc_setup(struct sna *sna, ScreenPtr ptr)
{
DBG(("%s: XvMC not compiled in\n", __FUNCTION__));
}
#endif
void sna_video_free_buffers(struct sna_video *video)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(video->old_buf); i++) {
if (video->old_buf[i]) {
kgem_bo_destroy(&video->sna->kgem, video->old_buf[i]);
video->old_buf[i] = NULL;
}
}
if (video->buf) {
kgem_bo_destroy(&video->sna->kgem, video->buf);
video->buf = NULL;
}
}
struct kgem_bo *
sna_video_buffer(struct sna_video *video,
struct sna_video_frame *frame)
{
/* Free the current buffer if we're going to have to reallocate */
if (video->buf && __kgem_bo_size(video->buf) < frame->size)
sna_video_free_buffers(video);
if (video->buf && video->buf->scanout) {
if (frame->width != video->width ||
frame->height != video->height ||
frame->id != video->format)
sna_video_free_buffers(video);
}
if (video->buf == NULL) {
if (video->tiled) {
video->buf = kgem_create_2d(&video->sna->kgem,
frame->width, frame->height, 32,
I915_TILING_X, CREATE_EXACT);
} else {
video->buf = kgem_create_linear(&video->sna->kgem, frame->size,
CREATE_GTT_MAP);
}
}
video->width = frame->width;
video->height = frame->height;
video->format = frame->id;
return video->buf;
}
void sna_video_buffer_fini(struct sna_video *video)
{
struct kgem_bo *bo;
bo = video->old_buf[1];
video->old_buf[1] = video->old_buf[0];
video->old_buf[0] = video->buf;
video->buf = bo;
}
bool
sna_video_clip_helper(struct sna_video *video,
struct sna_video_frame *frame,
xf86CrtcPtr *crtc_ret,
BoxPtr dst,
short src_x, short src_y,
short drw_x, short drw_y,
short src_w, short src_h,
short drw_w, short drw_h,
RegionPtr reg)
{
bool ret;
RegionRec crtc_region_local;
RegionPtr crtc_region = reg;
INT32 x1, x2, y1, y2;
xf86CrtcPtr crtc;
x1 = src_x;
x2 = src_x + src_w;
y1 = src_y;
y2 = src_y + src_h;
dst->x1 = drw_x;
dst->x2 = drw_x + drw_w;
dst->y1 = drw_y;
dst->y2 = drw_y + drw_h;
/*
* For overlay video, compute the relevant CRTC and
* clip video to that
*/
crtc = sna_covering_crtc(video->sna, dst, video->desired_crtc);
/* For textured video, we don't actually want to clip at all. */
if (crtc && !video->textured) {
crtc_region_local.extents = crtc->bounds;
crtc_region_local.data = NULL;
crtc_region = &crtc_region_local;
RegionIntersect(crtc_region, crtc_region, reg);
}
*crtc_ret = crtc;
ret = xf86XVClipVideoHelper(dst, &x1, &x2, &y1, &y2,
crtc_region, frame->width, frame->height);
if (crtc_region != reg)
RegionUninit(crtc_region);
frame->src.x1 = x1 >> 16;
frame->src.y1 = y1 >> 16;
frame->src.x2 = (x2 + 0xffff) >> 16;
frame->src.y2 = (y2 + 0xffff) >> 16;
frame->image.x1 = frame->src.x1 & ~1;
frame->image.x2 = ALIGN(frame->src.x2, 2);
if (is_planar_fourcc(frame->id)) {
frame->image.y1 = frame->src.y1 & ~1;
frame->image.y2 = ALIGN(frame->src.y2, 2);
} else {
frame->image.y1 = frame->src.y1;
frame->image.y2 = frame->src.y2;
}
return ret;
}
void
sna_video_frame_init(struct sna_video *video,
int id, short width, short height,
struct sna_video_frame *frame)
{
DBG(("%s: id=%d [planar? %d], width=%d, height=%d, align=%d\n",
__FUNCTION__, id, is_planar_fourcc(id), width, height, video->alignment));
assert(width && height);
frame->bo = NULL;
frame->id = id;
frame->width = width;
frame->height = height;
frame->rotation = 0;
}
void
sna_video_frame_set_rotation(struct sna_video *video,
struct sna_video_frame *frame,
Rotation rotation)
{
unsigned width = frame->width;
unsigned height = frame->height;
unsigned align;
DBG(("%s: rotation=%d\n", __FUNCTION__, rotation));
frame->rotation = rotation;
align = video->alignment;
#if SNA_XVMC
/* for i915 xvmc, hw requires 1kb aligned surfaces */
if (frame->id == FOURCC_XVMC && video->sna->kgem.gen < 040 && align < 1024)
align = 1024;
#endif
/* Determine the desired destination pitch (representing the
* chroma's pitch in the planar case).
*/
if (is_nv12_fourcc(frame->id)) {
assert((width & 1) == 0);
assert((height & 1) == 0);
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->pitch[0] = ALIGN(height, align);
frame->pitch[1] = ALIGN(height, align);
frame->size = width * frame->pitch[1] +
width / 2 * frame->pitch[0];
} else {
frame->pitch[0] = ALIGN(width, align);
frame->pitch[1] = ALIGN(width, align);
frame->size = height * frame->pitch[1] +
height / 2 * frame->pitch[0];
}
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->UBufOffset = (int)frame->pitch[1] * width;
frame->VBufOffset = frame->UBufOffset;
} else {
frame->UBufOffset = (int)frame->pitch[1] * height;
frame->VBufOffset = frame->UBufOffset;
}
} else if (is_planar_fourcc(frame->id)) {
assert((width & 1) == 0);
assert((height & 1) == 0);
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->pitch[0] = ALIGN((height / 2), align);
frame->pitch[1] = ALIGN(height, align);
frame->size = width;
} else {
frame->pitch[0] = ALIGN((width / 2), align);
frame->pitch[1] = ALIGN(width, align);
frame->size = height;
}
frame->size *= frame->pitch[0] + frame->pitch[1];
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->UBufOffset = (int)frame->pitch[1] * width;
frame->VBufOffset =
frame->UBufOffset + (int)frame->pitch[0] * width / 2;
} else {
frame->UBufOffset = (int)frame->pitch[1] * height;
frame->VBufOffset =
frame->UBufOffset + (int)frame->pitch[0] * height / 2;
}
} else {
switch (frame->id) {
case FOURCC_RGB888:
case FOURCC_AYUV:
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->pitch[0] = ALIGN((height << 2), align);
frame->size = (int)frame->pitch[0] * width;
} else {
frame->pitch[0] = ALIGN((width << 2), align);
frame->size = (int)frame->pitch[0] * height;
}
frame->UBufOffset = frame->VBufOffset = 0;
break;
case FOURCC_RGB565:
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->pitch[0] = ALIGN((height << 1), align);
frame->size = (int)frame->pitch[0] * width;
} else {
frame->pitch[0] = ALIGN((width << 1), align);
frame->size = (int)frame->pitch[0] * height;
}
frame->UBufOffset = frame->VBufOffset = 0;
break;
default:
if (rotation & (RR_Rotate_90 | RR_Rotate_270)) {
frame->pitch[0] = ALIGN((height << 1), align);
frame->size = (int)frame->pitch[0] * width;
} else {
frame->pitch[0] = ALIGN((width << 1), align);
frame->size = (int)frame->pitch[0] * height;
}
break;
}
frame->pitch[1] = 0;
frame->UBufOffset = 0;
frame->VBufOffset = 0;
}
assert(frame->size);
}
static void plane_dims(const struct sna_video_frame *frame, int sub,
int *x, int *y, int *w, int *h)
{
*x = frame->image.x1;
*y = frame->image.y1;
*w = frame->image.x2 - frame->image.x1;
*h = frame->image.y2 - frame->image.y1;
if (sub) {
*x >>= 1; *w >>= 1;
*y >>= 1; *h >>= 1;
}
}
static void sna_memcpy_cbcr_plane(struct sna_video *video,
uint16_t *dst, const uint16_t *src,
const struct sna_video_frame *frame)
{
int dstPitch = frame->pitch[0] >> 1, srcPitch;
const uint16_t *s;
int i, j = 0;
int x, y, w, h;
plane_dims(frame, 1, &x, &y, &w, &h);
srcPitch = ALIGN((frame->width >> 1), 2);
src += y * srcPitch + x;
if (!video->textured)
x = y = 0;
switch (frame->rotation) {
case RR_Rotate_0:
dst += y * dstPitch + x;
if (srcPitch == dstPitch && srcPitch == w)
memcpy(dst, src, (srcPitch * h) << 1);
else while (h--) {
memcpy(dst, src, w << 1);
src += srcPitch;
dst += dstPitch;
}
break;
case RR_Rotate_90:
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j++)
dst[i + ((x + w - j - 1) * dstPitch)] = *s++;
src += srcPitch;
}
break;
case RR_Rotate_180:
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j++) {
dst[(x + w - j - 1) +
((h - i - 1) * dstPitch)] = *s++;
}
src += srcPitch;
}
break;
case RR_Rotate_270:
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j++) {
dst[(h - i - 1) + (x + j * dstPitch)] = *s++;
}
src += srcPitch;
}
break;
}
}
static void sna_memcpy_plane(struct sna_video *video,
uint8_t *dst, const uint8_t *src,
const struct sna_video_frame *frame, int sub)
{
int dstPitch = frame->pitch[!sub], srcPitch;
const uint8_t *s;
int i, j = 0;
int x, y, w, h;
plane_dims(frame, sub, &x, &y, &w, &h);
if (sub)
srcPitch = ALIGN((frame->width >> 1), 4);
else
srcPitch = ALIGN(frame->width, 4);
src += y * srcPitch + x;
if (!video->textured)
x = y = 0;
switch (frame->rotation) {
case RR_Rotate_0:
dst += y * dstPitch + x;
if (srcPitch == dstPitch && srcPitch == w)
memcpy(dst, src, srcPitch * h);
else while (h--) {
memcpy(dst, src, w);
src += srcPitch;
dst += dstPitch;
}
break;
case RR_Rotate_90:
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j++)
dst[i + ((x + w - j - 1) * dstPitch)] = *s++;
src += srcPitch;
}
break;
case RR_Rotate_180:
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j++) {
dst[(x + w - j - 1) +
((h - i - 1) * dstPitch)] = *s++;
}
src += srcPitch;
}
break;
case RR_Rotate_270:
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j++) {
dst[(h - i - 1) + (x + j * dstPitch)] = *s++;
}
src += srcPitch;
}
break;
}
}
static void
sna_copy_nv12_data(struct sna_video *video,
const struct sna_video_frame *frame,
const uint8_t *src, uint8_t *dst)
{
sna_memcpy_plane(video, dst, src, frame, 0);
src += frame->height * ALIGN(frame->width, 4);
dst += frame->UBufOffset;
sna_memcpy_cbcr_plane(video, (void*)dst, (void*)src, frame);
}
static void
sna_copy_planar_data(struct sna_video *video,
const struct sna_video_frame *frame,
const uint8_t *src, uint8_t *dst)
{
uint8_t *d;
sna_memcpy_plane(video, dst, src, frame, 0);
src += frame->height * ALIGN(frame->width, 4);
if (frame->id == FOURCC_I420)
d = dst + frame->UBufOffset;
else
d = dst + frame->VBufOffset;
sna_memcpy_plane(video, d, src, frame, 1);
src += (frame->height >> 1) * ALIGN(frame->width >> 1, 4);
if (frame->id == FOURCC_I420)
d = dst + frame->VBufOffset;
else
d = dst + frame->UBufOffset;
sna_memcpy_plane(video, d, src, frame, 1);
}
static void
sna_copy_packed_data(struct sna_video *video,
const struct sna_video_frame *frame,
const uint8_t *buf,
uint8_t *dst)
{
int pitch = frame->width << 1;
const uint8_t *src, *s;
int x, y, w, h;
int i, j;
if (video->textured) {
/* XXX support copying cropped extents */
x = y = 0;
w = frame->width;
h = frame->height;
} else {
x = frame->image.x1;
y = frame->image.y1;
w = frame->image.x2 - frame->image.x1;
h = frame->image.y2 - frame->image.y1;
}
src = buf + (y * pitch) + (x << 1);
switch (frame->rotation) {
case RR_Rotate_0:
w <<= 1;
for (i = 0; i < h; i++) {
memcpy(dst, src, w);
src += pitch;
dst += frame->pitch[0];
}
break;
case RR_Rotate_90:
h <<= 1;
for (i = 0; i < h; i += 2) {
s = src;
for (j = 0; j < w; j++) {
/* Copy Y */
dst[(i + 0) + ((w - j - 1) * frame->pitch[0])] = *s;
s += 2;
}
src += pitch;
}
h >>= 1;
src = buf + (y * pitch) + (x << 1);
for (i = 0; i < h; i += 2) {
for (j = 0; j < w; j += 2) {
/* Copy U */
dst[((i * 2) + 1) + ((w - j - 1) * frame->pitch[0])] = src[(j * 2) + 1 + (i * pitch)];
dst[((i * 2) + 1) + ((w - j - 2) * frame->pitch[0])] = src[(j * 2) + 1 + ((i + 1) * pitch)];
/* Copy V */ dst[((i * 2) + 3) + ((w - j - 1) * frame->pitch[0])] = src[(j * 2) + 3 + (i * pitch)];
dst[((i * 2) + 3) + ((w - j - 2) * frame->pitch[0])] = src[(j * 2) + 3 + ((i + 1) * pitch)];
}
}
break;
case RR_Rotate_180:
w <<= 1;
for (i = 0; i < h; i++) {
s = src;
for (j = 0; j < w; j += 4) {
dst[(w - j - 4) + ((h - i - 1) * frame->pitch[0])] = *s++;
dst[(w - j - 3) + ((h - i - 1) * frame->pitch[0])] = *s++;
dst[(w - j - 2) + ((h - i - 1) * frame->pitch[0])] = *s++;
dst[(w - j - 1) + ((h - i - 1) * frame->pitch[0])] = *s++;
}
src += pitch;
}
break;
case RR_Rotate_270:
h <<= 1;
for (i = 0; i < h; i += 2) {
s = src;
for (j = 0; j < w; j++) {
/* Copy Y */
dst[(h - i - 2) + (j * frame->pitch[0])] = *s;
s += 2;
}
src += pitch;
}
h >>= 1;
src = buf + (y * pitch) + (x << 1);
for (i = 0; i < h; i += 2) {
for (j = 0; j < w; j += 2) {
/* Copy U */
dst[(((h - i) * 2) - 3) + (j * frame->pitch[0])] = src[(j * 2) + 1 + (i * pitch)];
dst[(((h - i) * 2) - 3) + ((j + 1) * frame->pitch[0])] = src[(j * 2) + 1 + ((i + 1) * pitch)];
/* Copy V */
dst[(((h - i) * 2) - 1) + (j * frame->pitch[0])] = src[(j * 2) + 3 + (i * pitch)];
dst[(((h - i) * 2) - 1) + ((j + 1) * frame->pitch[0])] = src[(j * 2) + 3 + ((i + 1) * pitch)];
}
}
break;
}
}
static void
sna_copy_ayuv_data(struct sna_video *video,
const struct sna_video_frame *frame,
const uint8_t *buf,
uint8_t *dst)
{
int pitch = frame->width << 2;
const uint32_t *src_dw;
const uint8_t *src;
uint32_t *dst_dw = (uint32_t *)dst;
int x, y, w, h;
int i, j;
if (video->textured) {
/* XXX support copying cropped extents */
x = y = 0;
w = frame->width;
h = frame->height;
} else {
x = frame->image.x1;
y = frame->image.y1;
w = frame->image.x2 - frame->image.x1;
h = frame->image.y2 - frame->image.y1;
}
src = buf + (y * pitch) + (x << 2);
src_dw = (uint32_t *)src;
switch (frame->rotation) {
case RR_Rotate_0:
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
/*
* Have to reverse bytes order, because the only
* player which supports AYUV format currently is
* Gstreamer and it supports in bad way, even though
* spec says MSB:AYUV, we get the bytes opposite way.
*/
dst_dw[i * w + j] = bswap_32(src_dw[i * w + j]);
}
}
break;
case RR_Rotate_90:
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
dst_dw[(w - j - 1) * h + i] = bswap_32(src_dw[i * w + j]);
}
}
break;
case RR_Rotate_180:
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
dst_dw[(h - i - 1) * w + w - j - 1] = bswap_32(src_dw[i * w + j]);
}
}
break;
case RR_Rotate_270:
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
dst_dw[(w - j - 1) * h + i] = bswap_32(src_dw[i * w + j]);
}
}
break;
}
}
bool
sna_video_copy_data(struct sna_video *video,
struct sna_video_frame *frame,
const uint8_t *buf)
{
uint8_t *dst;
DBG(("%s: handle=%d, size=%dx%d [%d], pitch=[%d,%d] rotation=%d, is-texture=%d\n",
__FUNCTION__, frame->bo ? frame->bo->handle : 0,
frame->width, frame->height, frame->size, frame->pitch[0], frame->pitch[1],
frame->rotation, video->textured));
DBG(("%s: image=(%d, %d), (%d, %d), source=(%d, %d), (%d, %d)\n",
__FUNCTION__,
frame->image.x1, frame->image.y1, frame->image.x2, frame->image.y2,
frame->src.x1, frame->src.y1, frame->src.x2, frame->src.y2));
assert(frame->width && frame->height);
assert(frame->rotation);
assert(frame->size);
/* In the common case, we can simply the upload in a single pwrite */
if (frame->rotation == RR_Rotate_0 && !video->tiled && !is_ayuv_fourcc(frame->id)) {
DBG(("%s: unrotated, untiled fast paths: is-planar?=%d\n",
__FUNCTION__, is_planar_fourcc(frame->id)));
if (is_nv12_fourcc(frame->id)) {
int w = frame->image.x2 - frame->image.x1;
int h = frame->image.y2 - frame->image.y1;
if (ALIGN(h, 2) == frame->height &&
ALIGN(w, 4) == frame->pitch[0] &&
ALIGN(w, 4) == frame->pitch[1]) {
if (frame->bo) {
if (!kgem_bo_write(&video->sna->kgem, frame->bo,
buf, frame->size))
goto use_gtt;
} else {
frame->bo = kgem_create_buffer(&video->sna->kgem, frame->size,
KGEM_BUFFER_WRITE | KGEM_BUFFER_WRITE_INPLACE,
(void **)&dst);
if (frame->bo == NULL)
return false;
memcpy(dst, buf, frame->size);
}
return true;
}
} else if (is_planar_fourcc(frame->id)) {
int w = frame->image.x2 - frame->image.x1;
int h = frame->image.y2 - frame->image.y1;
if (ALIGN(h, 2) == frame->height &&
ALIGN(w >> 1, 4) == frame->pitch[0] &&
ALIGN(w, 4) == frame->pitch[1]) {
if (frame->bo) {
if (!kgem_bo_write(&video->sna->kgem, frame->bo,
buf, frame->size))
goto use_gtt;
} else {
frame->bo = kgem_create_buffer(&video->sna->kgem, frame->size,
KGEM_BUFFER_WRITE | KGEM_BUFFER_WRITE_INPLACE,
(void **)&dst);
if (frame->bo == NULL)
return false;
memcpy(dst, buf, frame->size);
}
if (frame->id != FOURCC_I420) {
uint32_t tmp;
tmp = frame->VBufOffset;
frame->VBufOffset = frame->UBufOffset;
frame->UBufOffset = tmp;
}
return true;
}
} else {
int x, y, w, h;
if (video->textured) {
/* XXX support copying cropped extents */
x = y = 0;
w = frame->width;
h = frame->height;
} else {
x = frame->image.x1;
y = frame->image.y1;
w = frame->image.x2 - frame->image.x1;
h = frame->image.y2 - frame->image.y1;
}
if (w*2 == frame->pitch[0]) {
buf += (2U*y * frame->width) + (x << 1);
if (frame->bo) {
if (!kgem_bo_write(&video->sna->kgem, frame->bo,
buf, 2U*h*frame->width))
goto use_gtt;
} else {
frame->bo = kgem_create_buffer(&video->sna->kgem, frame->size,
KGEM_BUFFER_WRITE | KGEM_BUFFER_WRITE_INPLACE,
(void **)&dst);
if (frame->bo == NULL)
return false;
memcpy(dst, buf, 2U*h*frame->width);
}
return true;
}
}
DBG(("%s: source cropped, fallback\n", __FUNCTION__));
}
use_gtt: /* copy data, must use GTT so that we keep the overlay uncached */
if (frame->bo) {
dst = kgem_bo_map__gtt(&video->sna->kgem, frame->bo);
if (dst == NULL)
return false;
} else {
frame->bo = kgem_create_buffer(&video->sna->kgem, frame->size,
KGEM_BUFFER_WRITE | KGEM_BUFFER_WRITE_INPLACE,
(void **)&dst);
if (frame->bo == NULL)
return false;
}
if (is_nv12_fourcc(frame->id))
sna_copy_nv12_data(video, frame, buf, dst);
else if (is_planar_fourcc(frame->id))
sna_copy_planar_data(video, frame, buf, dst);
else if (is_ayuv_fourcc(frame->id))
sna_copy_ayuv_data(video, frame, buf, dst);
else
sna_copy_packed_data(video, frame, buf, dst);
return true;
}
void sna_video_fill_colorkey(struct sna_video *video,
const RegionRec *clip)
{
struct sna *sna = video->sna;
PixmapPtr front = sna->front;
struct kgem_bo *bo = __sna_pixmap_get_bo(front);
uint8_t *dst, *tmp;
int w, width;
if (video->AlwaysOnTop || RegionEqual(&video->clip, (RegionPtr)clip))
return;
assert(bo);
if (!wedged(sna) &&
sna_blt_fill_boxes(sna, GXcopy, bo,
front->drawable.bitsPerPixel,
video->color_key,
region_rects(clip),
region_num_rects(clip))) {
RegionCopy(&video->clip, (RegionPtr)clip);
return;
}
dst = kgem_bo_map__gtt(&sna->kgem, bo);
if (dst == NULL)
return;
w = front->drawable.bitsPerPixel/8;
width = (clip->extents.x2 - clip->extents.x1) * w;
tmp = malloc(width);
if (tmp == NULL)
return;
memcpy(tmp, &video->color_key, w);
while (2 * w < width) {
memcpy(tmp + w, tmp, w);
w *= 2;
}
if (w < width)
memcpy(tmp + w, tmp, width - w);
if (sigtrap_get() == 0) {
const BoxRec *box = region_rects(clip);
int n = region_num_rects(clip);
w = front->drawable.bitsPerPixel/8;
do {
int y = box->y1;
uint8_t *row = dst + y*bo->pitch + w*box->x1;
width = (box->x2 - box->x1) * w;
while (y < box->y2) {
memcpy(row, tmp, width);
row += bo->pitch;
y++;
}
box++;
} while (--n);
sigtrap_put();
RegionCopy(&video->clip, (RegionPtr)clip);
}
free(tmp);
}
XvAdaptorPtr sna_xv_adaptor_alloc(struct sna *sna)
{
XvAdaptorPtr new_adaptors;
new_adaptors = realloc(sna->xv.adaptors,
(sna->xv.num_adaptors+1)*sizeof(XvAdaptorRec));
if (new_adaptors == NULL)
return NULL;
if (sna->xv.num_adaptors && new_adaptors != sna->xv.adaptors) {
XvAdaptorPtr adaptor = new_adaptors;
int i = sna->xv.num_adaptors, j;
while (i--) {
for (j = 0; j < adaptor->nPorts; j++)
adaptor->pPorts[j].pAdaptor = adaptor;
adaptor++;
}
}
sna->xv.adaptors = new_adaptors;
return &sna->xv.adaptors[sna->xv.num_adaptors++];
}
int
sna_xv_alloc_port(unsigned long port, XvPortPtr in, XvPortPtr *out)
{
*out = in;
return Success;
}
int
sna_xv_free_port(XvPortPtr port)
{
return Success;
}
int
sna_xv_fixup_formats(ScreenPtr screen, XvFormatPtr formats, int num_formats)
{
XvFormatPtr out = formats;
int count = 0;
while (num_formats--) {
int num_visuals = screen->numVisuals;
VisualPtr v = screen->visuals;
while (num_visuals--) {
if (v->class == TrueColor &&
v->nplanes == formats->depth) {
int tmp = out[count].depth;
out[count].depth = formats->depth;
out[count].visual = v->vid;
formats->depth = tmp;
count++;
break;
}
v++;
}
formats++;
}
return count;
}
#if XORG_XV_VERSION < 2
static int
sna_xv_query_adaptors(ScreenPtr screen,
XvAdaptorPtr *adaptors,
int *num_adaptors)
{
struct sna *sna = to_sna_from_screen(screen);
*num_adaptors = sna->xv.num_adaptors;
*adaptors = sna->xv.adaptors;
return Success;
}
static Bool
sna_xv_close_screen(CLOSE_SCREEN_ARGS_DECL)
{
struct sna *sna = to_sna_from_screen(screen);
sna_video_close(sna);
return TRUE;
}
#endif
void sna_video_init(struct sna *sna, ScreenPtr screen)
{
XvScreenPtr xv;
if (noXvExtension)
return;
if (xf86LoaderCheckSymbol("xf86XVListGenericAdaptors")) {
XF86VideoAdaptorPtr *adaptors = NULL;
int num_adaptors = xf86XVListGenericAdaptors(sna->scrn, &adaptors);
if (num_adaptors)
xf86DrvMsg(sna->scrn->scrnIndex, X_ERROR,
"Ignoring generic xf86XV adaptors");
free(adaptors);
}
if (XvScreenInit(screen) != Success)
return;
xv = to_xv(screen);
#if XORG_XV_VERSION < 2
xv->ddCloseScreen = sna_xv_close_screen;
xv->ddQueryAdaptors = sna_xv_query_adaptors;
#endif
sna_video_textured_setup(sna, screen);
sna_video_sprite_setup(sna, screen);
sna_video_overlay_setup(sna, screen);
if (sna->xv.num_adaptors >= 2 &&
xf86ReturnOptValBool(sna->Options, OPTION_PREFER_OVERLAY, false)) {
XvAdaptorRec tmp;
tmp = sna->xv.adaptors[0];
sna->xv.adaptors[0] = sna->xv.adaptors[1];
sna->xv.adaptors[1] = tmp;
}
xv->nAdaptors = sna->xv.num_adaptors;
xv->pAdaptors = sna->xv.adaptors;
sna_video_xvmc_setup(sna, screen);
}
void sna_video_destroy_window(WindowPtr win)
{
XvPortPtr port;
port = sna_window_get_port(win);
if (port) {
#if XORG_XV_VERSION < 2
port->pAdaptor->ddStopVideo(NULL, port, &win->drawable);
#else
port->pAdaptor->ddStopVideo(port, &win->drawable);
#endif
}
assert(sna_window_get_port(win) == NULL);
}
void sna_video_close(struct sna *sna)
{
int i;
for (i = 0; i < sna->xv.num_adaptors; i++) {
free(sna->xv.adaptors[i].pPorts->devPriv.ptr);
free(sna->xv.adaptors[i].pPorts);
free(sna->xv.adaptors[i].pEncodings);
}
free(sna->xv.adaptors);
sna->xv.adaptors = NULL;
sna->xv.num_adaptors = 0;
}
|