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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
|
/**************************************************************************
Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
All Rights Reserved.
Copyright (c) 2005 Jesse Barnes <jbarnes@virtuousgeek.org>
Based on code from i830_xaa.c.
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 PRECISION INSIGHT 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.
**************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "xf86.h"
#include "xaarop.h"
#include "i830.h"
#include "i810_reg.h"
#include "i915_drm.h"
#include "brw_defines.h"
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
const int I830CopyROP[16] = {
ROP_0, /* GXclear */
ROP_DSa, /* GXand */
ROP_SDna, /* GXandReverse */
ROP_S, /* GXcopy */
ROP_DSna, /* GXandInverted */
ROP_D, /* GXnoop */
ROP_DSx, /* GXxor */
ROP_DSo, /* GXor */
ROP_DSon, /* GXnor */
ROP_DSxn, /* GXequiv */
ROP_Dn, /* GXinvert */
ROP_SDno, /* GXorReverse */
ROP_Sn, /* GXcopyInverted */
ROP_DSno, /* GXorInverted */
ROP_DSan, /* GXnand */
ROP_1 /* GXset */
};
const int I830PatternROP[16] = {
ROP_0,
ROP_DPa,
ROP_PDna,
ROP_P,
ROP_DPna,
ROP_D,
ROP_DPx,
ROP_DPo,
ROP_DPon,
ROP_PDxn,
ROP_Dn,
ROP_PDno,
ROP_Pn,
ROP_DPno,
ROP_DPan,
ROP_1
};
#if HAS_DEVPRIVATEKEYREC
DevPrivateKeyRec uxa_pixmap_index;
#else
int uxa_pixmap_index;
#endif
static void
ironlake_blt_workaround(ScrnInfoPtr scrn)
{
intel_screen_private *intel = intel_get_screen_private(scrn);
/* Ironlake has a limitation that a 3D or Media command can't
* be the first command after a BLT, unless it's
* non-pipelined. Instead of trying to track it and emit a
* command at the right time, we just emit a dummy
* non-pipelined 3D instruction after each blit.
*/
if (IS_IGDNG(intel)) {
BEGIN_BATCH(2);
OUT_BATCH(CMD_POLY_STIPPLE_OFFSET << 16);
OUT_BATCH(0);
ADVANCE_BATCH();
}
}
Bool
i830_get_aperture_space(ScrnInfoPtr scrn, drm_intel_bo ** bo_table,
int num_bos)
{
intel_screen_private *intel = intel_get_screen_private(scrn);
if (intel->batch_bo == NULL) {
intel_debug_fallback(scrn, "VT inactive\n");
return FALSE;
}
bo_table[0] = intel->batch_bo;
if (drm_intel_bufmgr_check_aperture_space(bo_table, num_bos) != 0) {
intel_batch_submit(scrn);
bo_table[0] = intel->batch_bo;
if (drm_intel_bufmgr_check_aperture_space(bo_table, num_bos) !=
0) {
intel_debug_fallback(scrn, "Couldn't get aperture "
"space for BOs\n");
return FALSE;
}
}
return TRUE;
}
static unsigned long i830_pixmap_pitch(PixmapPtr pixmap)
{
return pixmap->devKind;
}
static int i830_pixmap_pitch_is_aligned(PixmapPtr pixmap)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
return i830_pixmap_pitch(pixmap) %
intel->accel_pixmap_pitch_alignment == 0;
}
static unsigned int
i830_uxa_pixmap_compute_size(PixmapPtr pixmap,
int w, int h,
uint32_t *tiling,
int *stride)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
int pitch, pitch_align;
int size;
if (*tiling != I915_TILING_NONE) {
/* First check whether tiling is necessary. */
pitch_align = intel->accel_pixmap_pitch_alignment;
size = ROUND_TO((w * pixmap->drawable.bitsPerPixel + 7) / 8,
pitch_align) * ALIGN (h, 2);
if (!IS_I965G(intel)) {
/* Older hardware requires fences to be pot size
* aligned with a minimum of 1 MiB, so causes
* massive overallocation for small textures.
*/
if (size < 1024*1024/2)
*tiling = I915_TILING_NONE;
} else if (size <= 4096) {
/* Disable tiling beneath a page size, we will not see
* any benefit from reducing TLB misses and instead
* just incur extra cost when we require a fence.
*/
*tiling = I915_TILING_NONE;
}
}
pitch = (w * pixmap->drawable.bitsPerPixel + 7) / 8;
if (pitch <= 256)
*tiling = I915_TILING_NONE;
repeat:
if (*tiling == I915_TILING_NONE) {
pitch_align = intel->accel_pixmap_pitch_alignment;
} else {
pitch_align = 512;
}
*stride = ROUND_TO(pitch, pitch_align);
if (*tiling == I915_TILING_NONE) {
/* Round the height up so that the GPU's access to a 2x2 aligned
* subspan doesn't address an invalid page offset beyond the
* end of the GTT.
*/
size = *stride * ALIGN(h, 2);
} else {
int aligned_h = h;
if (*tiling == I915_TILING_X)
aligned_h = ALIGN(h, 8);
else
aligned_h = ALIGN(h, 32);
*stride = i830_get_fence_pitch(intel, *stride, *tiling);
/* Round the object up to the size of the fence it will live in
* if necessary. We could potentially make the kernel allocate
* a larger aperture space and just bind the subset of pages in,
* but this is easier and also keeps us out of trouble (as much)
* with drm_intel_bufmgr_check_aperture().
*/
size = i830_get_fence_size(intel, *stride * aligned_h);
assert(size >= *stride * aligned_h);
}
if (*tiling != I915_TILING_NONE && size > intel->max_tiling_size) {
*tiling = I915_TILING_NONE;
goto repeat;
}
return size;
}
static Bool
i830_uxa_check_solid(DrawablePtr drawable, int alu, Pixel planemask)
{
ScrnInfoPtr scrn = xf86Screens[drawable->pScreen->myNum];
if (!UXA_PM_IS_SOLID(drawable, planemask)) {
intel_debug_fallback(scrn, "planemask is not solid\n");
return FALSE;
}
switch (drawable->bitsPerPixel) {
case 8:
case 16:
case 32:
break;
default:
return FALSE;
}
return TRUE;
}
/**
* Sets up hardware state for a series of solid fills.
*/
static Bool
i830_uxa_prepare_solid(PixmapPtr pixmap, int alu, Pixel planemask, Pixel fg)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
drm_intel_bo *bo_table[] = {
NULL, /* batch_bo */
i830_get_pixmap_bo(pixmap),
};
if (!intel_check_pitch_2d(pixmap))
return FALSE;
if (!i830_pixmap_pitch_is_aligned(pixmap)) {
intel_debug_fallback(scrn, "pixmap pitch not aligned");
return FALSE;
}
if (!i830_get_aperture_space(scrn, bo_table, ARRAY_SIZE(bo_table)))
return FALSE;
intel->BR[13] = (I830PatternROP[alu] & 0xff) << 16;
switch (pixmap->drawable.bitsPerPixel) {
case 8:
break;
case 16:
/* RGB565 */
intel->BR[13] |= (1 << 24);
break;
case 32:
/* RGB8888 */
intel->BR[13] |= ((1 << 24) | (1 << 25));
break;
}
intel->BR[16] = fg;
return TRUE;
}
static void i830_uxa_solid(PixmapPtr pixmap, int x1, int y1, int x2, int y2)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
unsigned long pitch;
uint32_t cmd;
if (x1 < 0)
x1 = 0;
if (y1 < 0)
y1 = 0;
if (x2 > pixmap->drawable.width)
x2 = pixmap->drawable.width;
if (y2 > pixmap->drawable.height)
y2 = pixmap->drawable.height;
if (x2 <= x1 || y2 <= y1)
return;
pitch = i830_pixmap_pitch(pixmap);
{
BEGIN_BATCH(6);
cmd = XY_COLOR_BLT_CMD;
if (pixmap->drawable.bitsPerPixel == 32)
cmd |=
XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB;
if (IS_I965G(intel) && i830_pixmap_tiled(pixmap)) {
assert((pitch % 512) == 0);
pitch >>= 2;
cmd |= XY_COLOR_BLT_TILED;
}
OUT_BATCH(cmd);
OUT_BATCH(intel->BR[13] | pitch);
OUT_BATCH((y1 << 16) | (x1 & 0xffff));
OUT_BATCH((y2 << 16) | (x2 & 0xffff));
OUT_RELOC_PIXMAP_FENCED(pixmap, I915_GEM_DOMAIN_RENDER,
I915_GEM_DOMAIN_RENDER, 0);
OUT_BATCH(intel->BR[16]);
ADVANCE_BATCH();
}
ironlake_blt_workaround(scrn);
}
static void i830_uxa_done_solid(PixmapPtr pixmap)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
i830_debug_flush(scrn);
}
/**
* TODO:
* - support planemask using FULL_BLT_CMD?
*/
static Bool
i830_uxa_check_copy(PixmapPtr source, PixmapPtr dest,
int alu, Pixel planemask)
{
ScrnInfoPtr scrn = xf86Screens[dest->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
if (!UXA_PM_IS_SOLID(&source->drawable, planemask)) {
intel_debug_fallback(scrn, "planemask is not solid");
return FALSE;
}
if (source->drawable.bitsPerPixel != dest->drawable.bitsPerPixel) {
intel_debug_fallback(scrn, "mixed bpp copies unsupported\n");
return FALSE;
}
switch (source->drawable.bitsPerPixel) {
case 8:
case 16:
case 32:
break;
default:
return FALSE;
}
if (!intel_check_pitch_2d(source))
return FALSE;
if (!intel_check_pitch_2d(dest))
return FALSE;
return TRUE;
}
static Bool
i830_uxa_prepare_copy(PixmapPtr source, PixmapPtr dest, int xdir,
int ydir, int alu, Pixel planemask)
{
ScrnInfoPtr scrn = xf86Screens[dest->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
drm_intel_bo *bo_table[] = {
NULL, /* batch_bo */
i830_get_pixmap_bo(source),
i830_get_pixmap_bo(dest),
};
if (!i830_get_aperture_space(scrn, bo_table, ARRAY_SIZE(bo_table)))
return FALSE;
intel->render_source = source;
intel->BR[13] = I830CopyROP[alu] << 16;
switch (source->drawable.bitsPerPixel) {
case 8:
break;
case 16:
intel->BR[13] |= (1 << 24);
break;
case 32:
intel->BR[13] |= ((1 << 25) | (1 << 24));
break;
}
return TRUE;
}
static void
i830_uxa_copy(PixmapPtr dest, int src_x1, int src_y1, int dst_x1,
int dst_y1, int w, int h)
{
ScrnInfoPtr scrn = xf86Screens[dest->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
uint32_t cmd;
int dst_x2, dst_y2;
unsigned int dst_pitch, src_pitch;
dst_x2 = dst_x1 + w;
dst_y2 = dst_y1 + h;
dst_pitch = i830_pixmap_pitch(dest);
src_pitch = i830_pixmap_pitch(intel->render_source);
{
BEGIN_BATCH(8);
cmd = XY_SRC_COPY_BLT_CMD;
if (dest->drawable.bitsPerPixel == 32)
cmd |=
XY_SRC_COPY_BLT_WRITE_ALPHA |
XY_SRC_COPY_BLT_WRITE_RGB;
if (IS_I965G(intel)) {
if (i830_pixmap_tiled(dest)) {
assert((dst_pitch % 512) == 0);
dst_pitch >>= 2;
cmd |= XY_SRC_COPY_BLT_DST_TILED;
}
if (i830_pixmap_tiled(intel->render_source)) {
assert((src_pitch % 512) == 0);
src_pitch >>= 2;
cmd |= XY_SRC_COPY_BLT_SRC_TILED;
}
}
OUT_BATCH(cmd);
OUT_BATCH(intel->BR[13] | dst_pitch);
OUT_BATCH((dst_y1 << 16) | (dst_x1 & 0xffff));
OUT_BATCH((dst_y2 << 16) | (dst_x2 & 0xffff));
OUT_RELOC_PIXMAP_FENCED(dest,
I915_GEM_DOMAIN_RENDER,
I915_GEM_DOMAIN_RENDER,
0);
OUT_BATCH((src_y1 << 16) | (src_x1 & 0xffff));
OUT_BATCH(src_pitch);
OUT_RELOC_PIXMAP_FENCED(intel->render_source,
I915_GEM_DOMAIN_RENDER, 0,
0);
ADVANCE_BATCH();
}
ironlake_blt_workaround(scrn);
}
static void i830_uxa_done_copy(PixmapPtr dest)
{
ScrnInfoPtr scrn = xf86Screens[dest->drawable.pScreen->myNum];
i830_debug_flush(scrn);
}
/**
* Do any cleanup from the Composite operation.
*
* This is shared between i830 through i965.
*/
void i830_done_composite(PixmapPtr dest)
{
ScrnInfoPtr scrn = xf86Screens[dest->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
if (intel->vertex_flush)
intel->vertex_flush(intel);
i830_debug_flush(scrn);
}
#define xFixedToFloat(val) \
((float)xFixedToInt(val) + ((float)xFixedFrac(val) / 65536.0))
static Bool
_i830_transform_point(PictTransformPtr transform,
float x, float y, float result[3])
{
int j;
for (j = 0; j < 3; j++) {
result[j] = (xFixedToFloat(transform->matrix[j][0]) * x +
xFixedToFloat(transform->matrix[j][1]) * y +
xFixedToFloat(transform->matrix[j][2]));
}
if (!result[2])
return FALSE;
return TRUE;
}
/**
* Returns the floating-point coordinates transformed by the given transform.
*
* transform may be null.
*/
Bool
i830_get_transformed_coordinates(int x, int y, PictTransformPtr transform,
float *x_out, float *y_out)
{
if (transform == NULL) {
*x_out = x;
*y_out = y;
} else {
float result[3];
if (!_i830_transform_point
(transform, (float)x, (float)y, result))
return FALSE;
*x_out = result[0] / result[2];
*y_out = result[1] / result[2];
}
return TRUE;
}
/**
* Returns the un-normalized floating-point coordinates transformed by the given transform.
*
* transform may be null.
*/
Bool
i830_get_transformed_coordinates_3d(int x, int y, PictTransformPtr transform,
float *x_out, float *y_out, float *w_out)
{
if (transform == NULL) {
*x_out = x;
*y_out = y;
*w_out = 1;
} else {
float result[3];
if (!_i830_transform_point
(transform, (float)x, (float)y, result))
return FALSE;
*x_out = result[0];
*y_out = result[1];
*w_out = result[2];
}
return TRUE;
}
/**
* Returns whether the provided transform is affine.
*
* transform may be null.
*/
Bool i830_transform_is_affine(PictTransformPtr t)
{
if (t == NULL)
return TRUE;
return t->matrix[2][0] == 0 && t->matrix[2][1] == 0;
}
dri_bo *i830_get_pixmap_bo(PixmapPtr pixmap)
{
struct intel_pixmap *intel;
intel = i830_get_pixmap_intel(pixmap);
if (intel == NULL)
return NULL;
return intel->bo;
}
void i830_set_pixmap_bo(PixmapPtr pixmap, dri_bo * bo)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
struct intel_pixmap *priv;
priv = i830_get_pixmap_intel(pixmap);
if (priv == NULL && bo == NULL)
return;
if (priv != NULL) {
if (priv->bo == bo)
return;
if (list_is_empty(&priv->batch)) {
dri_bo_unreference(priv->bo);
} else if (!drm_intel_bo_is_reusable(priv->bo)) {
dri_bo_unreference(priv->bo);
list_del(&priv->batch);
list_del(&priv->flush);
} else {
list_add(&priv->in_flight, &intel->in_flight);
priv = NULL;
}
if (intel->render_current_dest == pixmap)
intel->render_current_dest = NULL;
}
if (bo != NULL) {
uint32_t tiling;
uint32_t swizzle_mode;
int ret;
if (priv == NULL) {
priv = calloc(1, sizeof (struct intel_pixmap));
if (priv == NULL)
goto BAIL;
list_init(&priv->batch);
list_init(&priv->flush);
}
dri_bo_reference(bo);
priv->bo = bo;
priv->stride = i830_pixmap_pitch(pixmap);
ret = drm_intel_bo_get_tiling(bo,
&tiling,
&swizzle_mode);
if (ret != 0) {
FatalError("Couldn't get tiling on bo %p: %s\n",
bo, strerror(-ret));
}
priv->tiling = tiling;
priv->busy = -1;
} else {
if (priv != NULL) {
free(priv);
priv = NULL;
}
}
BAIL:
i830_set_pixmap_intel(pixmap, priv);
}
static Bool i830_uxa_prepare_access(PixmapPtr pixmap, uxa_access_t access)
{
ScrnInfoPtr scrn = xf86Screens[pixmap->drawable.pScreen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
struct intel_pixmap *priv = i830_get_pixmap_intel(pixmap);
dri_bo *bo = priv->bo;
int ret;
if (!list_is_empty(&priv->batch) &&
(access == UXA_ACCESS_RW || priv->batch_write))
intel_batch_submit(scrn);
if (bo->size > intel->max_gtt_map_size) {
ret = dri_bo_map(bo, access == UXA_ACCESS_RW);
if (ret != 0) {
xf86DrvMsg(scrn->scrnIndex, X_WARNING,
"%s: bo map failed: %s\n",
__FUNCTION__,
strerror(-ret));
return FALSE;
}
} else {
ret = drm_intel_gem_bo_map_gtt(bo);
if (ret != 0) {
xf86DrvMsg(scrn->scrnIndex, X_WARNING,
"%s: gtt bo map failed: %s\n",
__FUNCTION__,
strerror(-ret));
return FALSE;
}
}
pixmap->devPrivate.ptr = bo->virtual;
priv->busy = 0;
return TRUE;
}
static void i830_uxa_finish_access(PixmapPtr pixmap)
{
dri_bo *bo = i830_get_pixmap_bo(pixmap);
ScreenPtr screen = pixmap->drawable.pScreen;
ScrnInfoPtr scrn = xf86Screens[screen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
if (bo == intel->front_buffer->bo)
intel->need_flush = TRUE;
if (bo->size > intel->max_gtt_map_size)
dri_bo_unmap(bo);
else
drm_intel_gem_bo_unmap_gtt(bo);
pixmap->devPrivate.ptr = NULL;
}
static Bool i830_uxa_pixmap_put_image(PixmapPtr pixmap,
char *src, int src_pitch,
int x, int y, int w, int h)
{
struct intel_pixmap *priv = i830_get_pixmap_intel(pixmap);
int stride = i830_pixmap_pitch(pixmap);
int ret = FALSE;
if (src_pitch == stride && w == pixmap->drawable.width && priv->tiling == I915_TILING_NONE) {
ret = drm_intel_bo_subdata(priv->bo, y * stride, stride * h, src) == 0;
} else if (drm_intel_gem_bo_map_gtt(priv->bo) == 0) {
char *dst = priv->bo->virtual;
int cpp = pixmap->drawable.bitsPerPixel/8;
int row_length = w * cpp;
int num_rows = h;
if (row_length == src_pitch && src_pitch == stride)
num_rows = 1, row_length *= h;
dst += y * stride + x * cpp;
do {
memcpy (dst, src, row_length);
src += src_pitch;
dst += stride;
} while (--num_rows);
drm_intel_gem_bo_unmap_gtt(priv->bo);
ret = TRUE;
}
return ret;
}
static Bool i830_uxa_put_image(PixmapPtr pixmap,
int x, int y,
int w, int h,
char *src, int src_pitch)
{
struct intel_pixmap *priv;
priv = i830_get_pixmap_intel(pixmap);
if (!intel_pixmap_is_busy(priv)) {
/* bo is not busy so can be replaced without a stall, upload in-place. */
return i830_uxa_pixmap_put_image(pixmap, src, src_pitch, x, y, w, h);
} else {
ScreenPtr screen = pixmap->drawable.pScreen;
if (x == 0 && y == 0 &&
w == pixmap->drawable.width &&
h == pixmap->drawable.height)
{
intel_screen_private *intel = intel_get_screen_private(xf86Screens[screen->myNum]);
uint32_t tiling = priv->tiling;
int size, stride;
dri_bo *bo;
/* Replace busy bo. */
size = i830_uxa_pixmap_compute_size (pixmap, w, h,
&tiling, &stride);
if (size > intel->max_gtt_map_size)
return FALSE;
bo = drm_intel_bo_alloc(intel->bufmgr, "pixmap", size, 0);
if (bo == NULL)
return FALSE;
if (tiling != I915_TILING_NONE)
drm_intel_bo_set_tiling(bo, &tiling, stride);
screen->ModifyPixmapHeader(pixmap,
w, h,
0, 0,
stride, NULL);
i830_set_pixmap_bo(pixmap, bo);
dri_bo_unreference(bo);
return i830_uxa_pixmap_put_image(pixmap, src, src_pitch, 0, 0, w, h);
}
else
{
PixmapPtr scratch;
Bool ret;
/* Upload to a linear buffer and queue a blit. */
scratch = (*screen->CreatePixmap)(screen, w, h,
pixmap->drawable.depth,
UXA_CREATE_PIXMAP_FOR_MAP);
if (!scratch)
return FALSE;
ret = i830_uxa_pixmap_put_image(scratch, src, src_pitch, 0, 0, w, h);
if (ret) {
GCPtr gc = GetScratchGC(pixmap->drawable.depth, screen);
if (gc) {
ValidateGC(&pixmap->drawable, gc);
(*gc->ops->CopyArea)(&scratch->drawable,
&pixmap->drawable,
gc, 0, 0, w, h, x, y);
FreeScratchGC(gc);
} else
ret = FALSE;
}
(*screen->DestroyPixmap)(scratch);
return ret;
}
}
}
static Bool i830_uxa_pixmap_get_image(PixmapPtr pixmap,
int x, int y, int w, int h,
char *dst, int dst_pitch)
{
struct intel_pixmap *priv = i830_get_pixmap_intel(pixmap);
int stride = i830_pixmap_pitch(pixmap);
if (dst_pitch == stride && w == pixmap->drawable.width) {
return drm_intel_bo_get_subdata(priv->bo, y * stride, stride * h, dst) == 0;
} else {
char *src;
int cpp;
if (drm_intel_bo_map(priv->bo, FALSE))
return FALSE;
cpp = pixmap->drawable.bitsPerPixel/8;
src = (char *) priv->bo->virtual + y * stride + x * cpp;
w *= cpp;
do {
memcpy(dst, src, w);
src += stride;
dst += dst_pitch;
} while (--h);
drm_intel_bo_unmap(priv->bo);
return TRUE;
}
}
static Bool i830_uxa_get_image(PixmapPtr pixmap,
int x, int y,
int w, int h,
char *dst, int dst_pitch)
{
struct intel_pixmap *priv;
PixmapPtr scratch = NULL;
Bool ret;
/* The presumption is that we wish to keep the target hot, so
* copy to a new bo and move that to the CPU in preference to
* causing ping-pong of the original.
*
* Also the gpu is much faster at detiling.
*/
priv = i830_get_pixmap_intel(pixmap);
if (intel_pixmap_is_busy(priv) || priv->tiling != I915_TILING_NONE) {
ScreenPtr screen = pixmap->drawable.pScreen;
GCPtr gc;
/* Copy to a linear buffer and pull. */
scratch = screen->CreatePixmap(screen, w, h,
pixmap->drawable.depth,
INTEL_CREATE_PIXMAP_TILING_NONE);
if (!scratch)
return FALSE;
gc = GetScratchGC(pixmap->drawable.depth, screen);
if (!gc) {
screen->DestroyPixmap(scratch);
return FALSE;
}
ValidateGC(&pixmap->drawable, gc);
gc->ops->CopyArea(&pixmap->drawable,
&scratch->drawable,
gc, x, y, w, h, 0, 0);
FreeScratchGC(gc);
intel_batch_submit(xf86Screens[screen->myNum]);
x = y = 0;
pixmap = scratch;
}
ret = i830_uxa_pixmap_get_image(pixmap, x, y, w, h, dst, dst_pitch);
if (scratch)
scratch->drawable.pScreen->DestroyPixmap(scratch);
return ret;
}
void i830_uxa_block_handler(ScreenPtr screen)
{
ScrnInfoPtr scrn = xf86Screens[screen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
if (intel->need_flush) {
dri_bo_wait_rendering(intel->front_buffer->bo);
intel->need_flush = FALSE;
}
}
static Bool i830_uxa_pixmap_is_offscreen(PixmapPtr pixmap)
{
ScreenPtr screen = pixmap->drawable.pScreen;
/* The front buffer is always in memory and pinned */
if (screen->GetScreenPixmap(screen) == pixmap)
return TRUE;
return i830_get_pixmap_bo(pixmap) != NULL;
}
static PixmapPtr
i830_uxa_create_pixmap(ScreenPtr screen, int w, int h, int depth,
unsigned usage)
{
ScrnInfoPtr scrn = xf86Screens[screen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
PixmapPtr pixmap;
if (w > 32767 || h > 32767)
return NullPixmap;
if (depth == 1)
return fbCreatePixmap(screen, w, h, depth, usage);
if (usage == CREATE_PIXMAP_USAGE_GLYPH_PICTURE && w <= 32 && h <= 32)
return fbCreatePixmap(screen, w, h, depth, usage);
pixmap = fbCreatePixmap(screen, 0, 0, depth, usage);
if (w && h) {
struct intel_pixmap *priv;
unsigned int size, tiling;
int stride;
/* Always attempt to tile, compute_size() will remove the
* tiling for pixmaps that are either too large or too small
* to be effectively tiled.
*/
tiling = I915_TILING_X;
if (usage == INTEL_CREATE_PIXMAP_TILING_Y)
tiling = I915_TILING_Y;
if (usage == UXA_CREATE_PIXMAP_FOR_MAP || usage == INTEL_CREATE_PIXMAP_TILING_NONE)
tiling = I915_TILING_NONE;
if (tiling != I915_TILING_NONE) {
if (h <= 4)
tiling = I915_TILING_NONE;
if (h <= 16 && tiling == I915_TILING_Y)
tiling = I915_TILING_X;
}
size = i830_uxa_pixmap_compute_size(pixmap, w, h, &tiling, &stride);
/* Fail very large allocations. Large BOs will tend to hit SW fallbacks
* frequently, and also will tend to fail to successfully map when doing
* SW fallbacks because we overcommit address space for BO access.
*/
if (size > intel->max_bo_size) {
fbDestroyPixmap(pixmap);
return fbCreatePixmap(screen, w, h, depth, usage);
}
/* Perform a preliminary search for an in-flight bo */
if (usage != UXA_CREATE_PIXMAP_FOR_MAP) {
int aligned_h;
if (tiling == I915_TILING_X)
aligned_h = ALIGN(h, 8);
else if (tiling == I915_TILING_Y)
aligned_h = ALIGN(h, 32);
else
aligned_h = ALIGN(h, 2);
list_foreach_entry(priv, struct intel_pixmap,
&intel->in_flight,
in_flight) {
if (priv->tiling != tiling)
continue;
if (tiling == I915_TILING_NONE) {
if (priv->bo->size < size)
continue;
priv->stride = stride;
} else {
if (priv->stride < stride ||
priv->bo->size < priv->stride * aligned_h)
continue;
stride = priv->stride;
}
list_del(&priv->in_flight);
screen->ModifyPixmapHeader(pixmap, w, h, 0, 0, stride, NULL);
i830_set_pixmap_intel(pixmap, priv);
return pixmap;
}
}
priv = calloc(1, sizeof (struct intel_pixmap));
if (priv == NULL) {
fbDestroyPixmap(pixmap);
return NullPixmap;
}
if (usage == UXA_CREATE_PIXMAP_FOR_MAP) {
priv->busy = 0;
priv->bo = drm_intel_bo_alloc(intel->bufmgr,
"pixmap", size, 0);
} else {
priv->busy = -1;
priv->bo = drm_intel_bo_alloc_for_render(intel->bufmgr,
"pixmap",
size, 0);
}
if (!priv->bo) {
free(priv);
fbDestroyPixmap(pixmap);
if (errno == EFBIG)
return fbCreatePixmap(screen, w, h, depth, usage);
return NullPixmap;
}
if (tiling != I915_TILING_NONE)
drm_intel_bo_set_tiling(priv->bo, &tiling, stride);
priv->stride = stride;
priv->tiling = tiling;
screen->ModifyPixmapHeader(pixmap, w, h, 0, 0, stride, NULL);
list_init(&priv->batch);
list_init(&priv->flush);
i830_set_pixmap_intel(pixmap, priv);
}
return pixmap;
}
static Bool i830_uxa_destroy_pixmap(PixmapPtr pixmap)
{
if (pixmap->refcnt == 1)
i830_set_pixmap_bo(pixmap, NULL);
fbDestroyPixmap(pixmap);
return TRUE;
}
void i830_uxa_create_screen_resources(ScreenPtr screen)
{
ScrnInfoPtr scrn = xf86Screens[screen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
dri_bo *bo = intel->front_buffer->bo;
if (bo != NULL) {
PixmapPtr pixmap = screen->GetScreenPixmap(screen);
i830_set_pixmap_bo(pixmap, bo);
i830_get_pixmap_intel(pixmap)->busy = 1;
}
}
Bool i830_uxa_init(ScreenPtr screen)
{
ScrnInfoPtr scrn = xf86Screens[screen->myNum];
intel_screen_private *intel = intel_get_screen_private(scrn);
#if HAS_DIXREGISTERPRIVATEKEY
if (!dixRegisterPrivateKey(&uxa_pixmap_index, PRIVATE_PIXMAP, 0))
#else
if (!dixRequestPrivate(&uxa_pixmap_index, 0))
#endif
return FALSE;
intel->uxa_driver = uxa_driver_alloc();
if (intel->uxa_driver == NULL)
return FALSE;
memset(intel->uxa_driver, 0, sizeof(*intel->uxa_driver));
intel->bufferOffset = 0;
intel->uxa_driver->uxa_major = 1;
intel->uxa_driver->uxa_minor = 0;
intel->render_current_dest = NULL;
intel->prim_offset = 0;
intel->vertex_count = 0;
intel->floats_per_vertex = 0;
intel->last_floats_per_vertex = 0;
intel->vertex_bo = NULL;
/* Solid fill */
intel->uxa_driver->check_solid = i830_uxa_check_solid;
intel->uxa_driver->prepare_solid = i830_uxa_prepare_solid;
intel->uxa_driver->solid = i830_uxa_solid;
intel->uxa_driver->done_solid = i830_uxa_done_solid;
/* Copy */
intel->uxa_driver->check_copy = i830_uxa_check_copy;
intel->uxa_driver->prepare_copy = i830_uxa_prepare_copy;
intel->uxa_driver->copy = i830_uxa_copy;
intel->uxa_driver->done_copy = i830_uxa_done_copy;
/* Composite */
if (!IS_I9XX(intel)) {
intel->uxa_driver->check_composite = i830_check_composite;
intel->uxa_driver->check_composite_target = i830_check_composite_target;
intel->uxa_driver->check_composite_texture = i830_check_composite_texture;
intel->uxa_driver->prepare_composite = i830_prepare_composite;
intel->uxa_driver->composite = i830_composite;
intel->uxa_driver->done_composite = i830_done_composite;
} else if (IS_I915G(intel) || IS_I915GM(intel) ||
IS_I945G(intel) || IS_I945GM(intel) || IS_G33CLASS(intel)) {
intel->uxa_driver->check_composite = i915_check_composite;
intel->uxa_driver->check_composite_target = i915_check_composite_target;
intel->uxa_driver->check_composite_texture = i915_check_composite_texture;
intel->uxa_driver->prepare_composite = i915_prepare_composite;
intel->uxa_driver->composite = i915_composite;
intel->uxa_driver->done_composite = i830_done_composite;
} else {
intel->uxa_driver->check_composite = i965_check_composite;
intel->uxa_driver->check_composite_texture = i965_check_composite_texture;
intel->uxa_driver->prepare_composite = i965_prepare_composite;
intel->uxa_driver->composite = i965_composite;
intel->uxa_driver->done_composite = i830_done_composite;
}
/* PutImage */
intel->uxa_driver->put_image = i830_uxa_put_image;
intel->uxa_driver->get_image = i830_uxa_get_image;
intel->uxa_driver->prepare_access = i830_uxa_prepare_access;
intel->uxa_driver->finish_access = i830_uxa_finish_access;
intel->uxa_driver->pixmap_is_offscreen = i830_uxa_pixmap_is_offscreen;
screen->CreatePixmap = i830_uxa_create_pixmap;
screen->DestroyPixmap = i830_uxa_destroy_pixmap;
if (!uxa_driver_init(screen, intel->uxa_driver)) {
xf86DrvMsg(scrn->scrnIndex, X_ERROR,
"UXA initialization failed\n");
free(intel->uxa_driver);
return FALSE;
}
uxa_set_fallback_debug(screen, intel->fallback_debug);
return TRUE;
}
|