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
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
|
/* $NetBSD: rasops.c,v 1.101.2.1 2019/08/15 12:21:27 martin Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.101.2.1 2019/08/15 12:21:27 martin Exp $");
#ifdef _KERNEL_OPT
#include "opt_rasops.h"
#include "opt_wsmsgattrs.h"
#include "rasops_glue.h"
#endif
#include <sys/param.h>
#include <sys/bswap.h>
#include <sys/kmem.h>
#include <machine/endian.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wsfont/wsfont.h>
#define _RASOPS_PRIVATE
#include <dev/rasops/rasops.h>
#include <dev/rasops/rasops_masks.h> /* XXX for MBE */
#ifndef _KERNEL
#include <errno.h>
#endif
#ifdef RASOPS_DEBUG
#define DPRINTF(...) aprint_error(...)
#else
#define DPRINTF(...) __nothing
#endif
struct rasops_matchdata {
struct rasops_info *ri;
int wantcols, wantrows;
int bestscore;
struct wsdisplay_font *pick;
int ident;
};
static const uint32_t rasops_lmask32[4 + 1] = {
MBE(0x00000000), MBE(0x00ffffff), MBE(0x0000ffff), MBE(0x000000ff),
MBE(0x00000000),
};
static const uint32_t rasops_rmask32[4 + 1] = {
MBE(0x00000000), MBE(0xff000000), MBE(0xffff0000), MBE(0xffffff00),
MBE(0xffffffff),
};
/* ANSI colormap (R,G,B). Upper 8 are high-intensity */
const uint8_t rasops_cmap[256 * 3] = {
0x00, 0x00, 0x00, /* black */
0x7f, 0x00, 0x00, /* red */
0x00, 0x7f, 0x00, /* green */
0x7f, 0x7f, 0x00, /* brown */
0x00, 0x00, 0x7f, /* blue */
0x7f, 0x00, 0x7f, /* magenta */
0x00, 0x7f, 0x7f, /* cyan */
0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
0x7f, 0x7f, 0x7f, /* black */
0xff, 0x00, 0x00, /* red */
0x00, 0xff, 0x00, /* green */
0xff, 0xff, 0x00, /* brown */
0x00, 0x00, 0xff, /* blue */
0xff, 0x00, 0xff, /* magenta */
0x00, 0xff, 0xff, /* cyan */
0xff, 0xff, 0xff, /* white */
/*
* For the cursor, we need at least the last (255th)
* color to be white. Fill up white completely for
* simplicity.
*/
#define _CMWHITE 0xff, 0xff, 0xff,
#define _CMWHITE16 _CMWHITE _CMWHITE _CMWHITE _CMWHITE \
_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
_CMWHITE _CMWHITE _CMWHITE _CMWHITE
_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 /* but not the last one */
#undef _CMWHITE16
#undef _CMWHITE
/*
* For the cursor the fg/bg indices are bit inverted, so
* provide complimentary colors in the upper 16 entries.
*/
0x7f, 0x7f, 0x7f, /* black */
0xff, 0x00, 0x00, /* red */
0x00, 0xff, 0x00, /* green */
0xff, 0xff, 0x00, /* brown */
0x00, 0x00, 0xff, /* blue */
0xff, 0x00, 0xff, /* magenta */
0x00, 0xff, 0xff, /* cyan */
0xff, 0xff, 0xff, /* white */
0x00, 0x00, 0x00, /* black */
0x7f, 0x00, 0x00, /* red */
0x00, 0x7f, 0x00, /* green */
0x7f, 0x7f, 0x00, /* brown */
0x00, 0x00, 0x7f, /* blue */
0x7f, 0x00, 0x7f, /* magenta */
0x00, 0x7f, 0x7f, /* cyan */
0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
};
/* True if color is gray */
static const uint8_t rasops_isgray[16] = {
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,
};
#ifdef RASOPS_APPLE_PALETTE
/*
* Approximate ANSI colormap for legacy Apple color palettes
*/
static const uint8_t apple8_devcmap[16] = {
0xff, /* black 0x00, 0x00, 0x00 */
0x6b, /* red 0x99, 0x00, 0x00 */
0xc5, /* green 0x00, 0x99, 0x00 */
0x59, /* yellow 0x99, 0x99, 0x00 */
0xd4, /* blue 0x00, 0x00, 0x99 */
0x68, /* magenta 0x99, 0x00, 0x99 */
0xc2, /* cyan 0x00, 0x99, 0x99 */
0x2b, /* white 0xcc, 0xcc, 0xcc */
0x56, /* black 0x99, 0x99, 0x99 */
0x23, /* red 0xff, 0x00, 0x00 */
0xb9, /* green 0x00, 0xff, 0x00 */
0x05, /* yellow 0xff, 0xff, 0x00 */
0xd2, /* blue 0x00, 0x00, 0xff */
0x1e, /* magenta 0xff, 0x00, 0xff */
0xb4, /* cyan 0x00, 0xff, 0xff */
0x00, /* white 0xff, 0xff, 0xff */
};
static const uint8_t apple4_devcmap[16] = {
15, /* black */
3, /* red */
9, /* dark green */
1, /* yellow */
6, /* blue */
4, /* magenta */
7, /* cyan */
12, /* light grey */
13, /* medium grey */
3, /* red */
9, /* dark green */
1, /* yellow */
6, /* blue */
4, /* magenta */
7, /* cyan */
0, /* white */
};
#endif
/* Generic functions */
static void rasops_copyrows(void *, int, int, int);
static void rasops_copycols(void *, int, int, int, int);
static int rasops_mapchar(void *, int, u_int *);
static void rasops_cursor(void *, int, int, int);
static int rasops_allocattr_color(void *, int, int, int, long *);
static int rasops_allocattr_mono(void *, int, int, int, long *);
static void rasops_do_cursor(struct rasops_info *);
static void rasops_init_devcmap(struct rasops_info *);
static void rasops_make_box_chars_8(struct rasops_info *);
static void rasops_make_box_chars_16(struct rasops_info *);
static void rasops_make_box_chars_32(struct rasops_info *);
static void rasops_make_box_chars_alpha(struct rasops_info *);
#if NRASOPS_ROTATION > 0
static void rasops_rotate_font(int *, int);
static void rasops_copychar(void *, int, int, int, int);
/* rotate clockwise */
static void rasops_copycols_rotated_cw(void *, int, int, int, int);
static void rasops_copyrows_rotated_cw(void *, int, int, int);
static void rasops_erasecols_rotated_cw(void *, int, int, int, long);
static void rasops_eraserows_rotated_cw(void *, int, int, long);
static void rasops_putchar_rotated_cw(void *, int, int, u_int, long);
/* rotate counter-clockwise */
static void rasops_copychar_ccw(void *, int, int, int, int);
static void rasops_copycols_rotated_ccw(void *, int, int, int, int);
static void rasops_copyrows_rotated_ccw(void *, int, int, int);
#define rasops_erasecols_rotated_ccw rasops_erasecols_rotated_cw
#define rasops_eraserows_rotated_ccw rasops_eraserows_rotated_cw
static void rasops_putchar_rotated_ccw(void *, int, int, u_int, long);
/*
* List of all rotated fonts
*/
SLIST_HEAD(, rotatedfont) rotatedfonts = SLIST_HEAD_INITIALIZER(rotatedfonts);
struct rotatedfont {
SLIST_ENTRY(rotatedfont) rf_next;
int rf_cookie;
int rf_rotated;
};
#endif /* NRASOPS_ROTATION > 0 */
/*
* Initialize a 'rasops_info' descriptor.
*/
int
rasops_init(struct rasops_info *ri, int wantrows, int wantcols)
{
memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
#ifdef _KERNEL
/* Select a font if the caller doesn't care */
if (ri->ri_font == NULL) {
int cookie = -1;
int flags;
wsfont_init();
/*
* first, try to find something that's as close as possible
* to the caller's requested terminal size
*/
if (wantrows == 0)
wantrows = RASOPS_DEFAULT_HEIGHT;
if (wantcols == 0)
wantcols = RASOPS_DEFAULT_WIDTH;
flags = WSFONT_FIND_BESTWIDTH | WSFONT_FIND_BITMAP;
if ((ri->ri_flg & RI_ENABLE_ALPHA) != 0)
flags |= WSFONT_FIND_ALPHA;
if ((ri->ri_flg & RI_PREFER_ALPHA) != 0)
flags |= WSFONT_PREFER_ALPHA;
cookie = wsfont_find(NULL,
ri->ri_width / wantcols,
0,
0,
WSDISPLAY_FONTORDER_L2R,
WSDISPLAY_FONTORDER_L2R,
flags);
/*
* this means there is no supported font in the list
*/
if (cookie <= 0) {
aprint_error("%s: font table is empty\n", __func__);
return -1;
}
#if NRASOPS_ROTATION > 0
/*
* Pick the rotated version of this font. This will create it
* if necessary.
*/
if (ri->ri_flg & RI_ROTATE_MASK) {
if (ri->ri_flg & RI_ROTATE_CW)
rasops_rotate_font(&cookie, WSFONT_ROTATE_CW);
else if (ri->ri_flg & RI_ROTATE_CCW)
rasops_rotate_font(&cookie, WSFONT_ROTATE_CCW);
}
#endif
if (wsfont_lock(cookie, &ri->ri_font)) {
aprint_error("%s: couldn't lock font\n", __func__);
return -1;
}
ri->ri_wsfcookie = cookie;
}
#endif
/* This should never happen in reality... */
if ((uintptr_t)ri->ri_bits & 3) {
aprint_error("%s: bits not aligned on 32-bit boundary\n",
__func__);
return -1;
}
if (ri->ri_stride & 3) {
aprint_error("%s: stride not aligned on 32-bit boundary\n",
__func__);
return -1;
}
if (rasops_reconfig(ri, wantrows, wantcols))
return -1;
rasops_init_devcmap(ri);
return 0;
}
/*
* Reconfigure (because parameters have changed in some way).
*/
int
rasops_reconfig(struct rasops_info *ri, int wantrows, int wantcols)
{
int bpp, height, s;
size_t len;
s = splhigh();
if (wantrows == 0)
wantrows = RASOPS_DEFAULT_HEIGHT;
if (wantcols == 0)
wantcols = RASOPS_DEFAULT_WIDTH;
/* throw away old line drawing character bitmaps, if we have any */
if (ri->ri_optfont.data != NULL) {
kmem_free(ri->ri_optfont.data, ri->ri_optfont.stride *
ri->ri_optfont.fontheight * ri->ri_optfont.numchars);
ri->ri_optfont.data = NULL;
}
/* autogenerate box drawing characters */
ri->ri_optfont.firstchar = WSFONT_FLAG_OPT;
ri->ri_optfont.numchars = 16;
ri->ri_optfont.fontwidth = ri->ri_font->fontwidth;
ri->ri_optfont.fontheight = ri->ri_font->fontheight;
ri->ri_optfont.stride = ri->ri_font->stride;
len = ri->ri_optfont.fontheight * ri->ri_optfont.stride *
ri->ri_optfont.numchars;
if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4) {
aprint_error("%s: fontwidth assumptions botched", __func__);
splx(s);
return -1;
}
if ((ri->ri_flg & RI_NO_AUTO) == 0) {
ri->ri_optfont.data = kmem_zalloc(len, KM_SLEEP);
if (FONT_IS_ALPHA(&ri->ri_optfont))
rasops_make_box_chars_alpha(ri);
else {
switch (ri->ri_optfont.stride) {
case 1:
rasops_make_box_chars_8(ri);
break;
case 2:
rasops_make_box_chars_16(ri);
break;
case 4:
rasops_make_box_chars_32(ri);
break;
default:
aprint_error(
"%s: font stride assumptions botched",
__func__);
splx(s);
return -1;
}
}
} else
memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
/* Need this to frob the setup below */
bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
if ((ri->ri_flg & RI_CFGDONE) != 0) {
ri->ri_bits = ri->ri_origbits;
ri->ri_hwbits = ri->ri_hworigbits;
}
/* Don't care if the caller wants a hideously small console */
if (wantrows < 10)
wantrows = 10;
if (wantcols < 20)
wantcols = 20;
/* Now constrain what they get */
ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols;
ri->ri_emuheight = ri->ri_font->fontheight * wantrows;
if (ri->ri_emuwidth > ri->ri_width)
ri->ri_emuwidth = ri->ri_width;
if (ri->ri_emuheight > ri->ri_height)
ri->ri_emuheight = ri->ri_height;
/* Reduce width until aligned on a 32-bit boundary */
while ((ri->ri_emuwidth * bpp & 31) != 0)
ri->ri_emuwidth--;
#if NRASOPS_ROTATION > 0
if (ri->ri_flg & (RI_ROTATE_CW|RI_ROTATE_CCW)) {
ri->ri_rows = ri->ri_emuwidth / ri->ri_font->fontwidth;
ri->ri_cols = ri->ri_emuheight / ri->ri_font->fontheight;
} else
#endif
{
ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth;
ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight;
}
ri->ri_emustride = ri->ri_emuwidth * bpp >> 3;
ri->ri_ccol = 0;
ri->ri_crow = 0;
ri->ri_pelbytes = bpp >> 3;
ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
ri->ri_origbits = ri->ri_bits;
ri->ri_hworigbits = ri->ri_hwbits;
/* Clear the entire display */
if ((ri->ri_flg & RI_CLEAR) != 0) {
rasops_memset32(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
if (ri->ri_hwbits)
rasops_memset32(ri->ri_hwbits, 0,
ri->ri_stride * ri->ri_height);
}
/* Now centre our window if needs be */
if ((ri->ri_flg & RI_CENTER) != 0) {
uint32_t xoff, yoff;
xoff = ((ri->ri_width * bpp >> 3) - ri->ri_emustride) >> 1;
if (ri->ri_depth != 24) {
/*
* Truncate to word boundary.
*/
xoff &= ~3;
} else {
/*
* Truncate to both word and 24-bit color boundary.
*/
xoff -= xoff % 12;
}
yoff = ((ri->ri_height - ri->ri_emuheight) >> 1) *
ri->ri_stride;
ri->ri_bits += xoff;
ri->ri_bits += yoff;
if (ri->ri_hwbits != NULL) {
ri->ri_hwbits += xoff;
ri->ri_hwbits += yoff;
}
ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits) /
ri->ri_stride;
ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits) %
ri->ri_stride) * 8 / bpp);
} else
ri->ri_xorigin = ri->ri_yorigin = 0;
/* Scaling underline by font height */
height = ri->ri_font->fontheight;
ri->ri_ul.off = rounddown(height, 16) / 16; /* offset from bottom */
ri->ri_ul.height = roundup(height, 16) / 16; /* height */
/*
* Fill in defaults for operations set. XXX this nukes private
* routines used by accelerated fb drivers.
*/
ri->ri_ops.mapchar = rasops_mapchar;
ri->ri_ops.copyrows = rasops_copyrows;
ri->ri_ops.copycols = rasops_copycols;
ri->ri_ops.erasecols = rasops_erasecols;
ri->ri_ops.eraserows = rasops_eraserows;
ri->ri_ops.cursor = rasops_cursor;
ri->ri_do_cursor = rasops_do_cursor;
ri->ri_caps &= ~(WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
WSSCREEN_WSCOLORS | WSSCREEN_REVERSE);
if ((ri->ri_flg & RI_FORCEMONO) != 0 ||
#ifndef RASOPS_APPLE_PALETTE
ri->ri_depth < 8
#else
ri->ri_depth < 4
#endif
) {
ri->ri_ops.allocattr = rasops_allocattr_mono;
ri->ri_caps |= WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
} else {
ri->ri_ops.allocattr = rasops_allocattr_color;
ri->ri_caps |= WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
}
switch (ri->ri_depth) {
#if NRASOPS1 > 0
case 1:
rasops1_init(ri);
break;
#endif
#if NRASOPS2 > 0
case 2:
rasops2_init(ri);
break;
#endif
#if NRASOPS4 > 0
case 4:
rasops4_init(ri);
break;
#endif
#if NRASOPS8 > 0
case 8:
rasops8_init(ri);
break;
#endif
#if (NRASOPS15 + NRASOPS16) > 0
case 15:
case 16:
rasops15_init(ri);
break;
#endif
#if NRASOPS24 > 0
case 24:
rasops24_init(ri);
break;
#endif
#if NRASOPS32 > 0
case 32:
rasops32_init(ri);
break;
#endif
default:
ri->ri_flg &= ~RI_CFGDONE;
aprint_error("%s: depth not supported\n", __func__);
splx(s);
return -1;
}
#if NRASOPS_ROTATION > 0
if (ri->ri_flg & RI_ROTATE_MASK) {
if (ri->ri_flg & RI_ROTATE_CW) {
ri->ri_real_ops = ri->ri_ops;
ri->ri_ops.copycols = rasops_copycols_rotated_cw;
ri->ri_ops.copyrows = rasops_copyrows_rotated_cw;
ri->ri_ops.erasecols = rasops_erasecols_rotated_cw;
ri->ri_ops.eraserows = rasops_eraserows_rotated_cw;
ri->ri_ops.putchar = rasops_putchar_rotated_cw;
} else if (ri->ri_flg & RI_ROTATE_CCW) {
ri->ri_real_ops = ri->ri_ops;
ri->ri_ops.copycols = rasops_copycols_rotated_ccw;
ri->ri_ops.copyrows = rasops_copyrows_rotated_ccw;
ri->ri_ops.erasecols = rasops_erasecols_rotated_ccw;
ri->ri_ops.eraserows = rasops_eraserows_rotated_ccw;
ri->ri_ops.putchar = rasops_putchar_rotated_ccw;
}
}
#endif
ri->ri_flg |= RI_CFGDONE;
splx(s);
return 0;
}
/*
* Map a character.
*/
static int
rasops_mapchar(void *cookie, int c, u_int *cp)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
KASSERT(ri->ri_font != NULL);
if ((c = wsfont_map_unichar(ri->ri_font, c)) < 0 ||
!CHAR_IN_FONT(c, ri->ri_font)) {
*cp = ' ';
return 0;
}
*cp = c;
return 5;
}
/*
* Allocate a color attribute.
*/
static int
rasops_allocattr_color(void *cookie, int fg0, int bg0, int flg, long *attr)
{
uint32_t fg = fg0, bg = bg0;
if (__predict_false(fg >= sizeof(rasops_isgray) ||
bg >= sizeof(rasops_isgray)))
return EINVAL;
#ifdef RASOPS_CLIPPING
fg &= 7;
bg &= 7;
#endif
if ((flg & WSATTR_BLINK) != 0)
return EINVAL;
if ((flg & WSATTR_WSCOLORS) == 0) {
#ifdef WS_DEFAULT_FG
fg = WS_DEFAULT_FG;
#else
fg = WSCOL_WHITE;
#endif
#ifdef WS_DEFAULT_BG
bg = WS_DEFAULT_BG;
#else
bg = WSCOL_BLACK;
#endif
}
if ((flg & WSATTR_HILIT) != 0 && fg < 8)
fg += 8;
if ((flg & WSATTR_REVERSE) != 0) {
uint32_t swap = fg;
fg = bg;
bg = swap;
}
flg &= WSATTR_USERMASK;
if (rasops_isgray[fg])
flg |= WSATTR_PRIVATE1;
if (rasops_isgray[bg])
flg |= WSATTR_PRIVATE2;
*attr = (bg << 16) | (fg << 24) | flg;
return 0;
}
/*
* Allocate a mono attribute.
*/
static int
rasops_allocattr_mono(void *cookie, int fg0, int bg0, int flg, long *attr)
{
uint32_t fg = fg0, bg = bg0;
if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
return EINVAL;
fg = 0xff;
bg = 0;
if ((flg & WSATTR_REVERSE) != 0) {
uint32_t swap = fg;
fg = bg;
bg = swap;
}
*attr = (bg << 16) | (fg << 24) | flg;
return 0;
}
/*
* Copy rows.
*/
static void
rasops_copyrows(void *cookie, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int stride;
uint8_t *sp, *dp, *hp;
hp = NULL; /* XXX GCC */
if (__predict_false(dst == src))
return;
#ifdef RASOPS_CLIPPING
if (src < 0) {
num += src;
src = 0;
}
if (src + num > ri->ri_rows)
num = ri->ri_rows - src;
if (dst < 0) {
num += dst;
dst = 0;
}
if (dst + num > ri->ri_rows)
num = ri->ri_rows - dst;
if (num <= 0)
return;
#endif
src *= ri->ri_yscale;
dst *= ri->ri_yscale;
num *= ri->ri_font->fontheight;
stride = ri->ri_stride;
if (src < dst) {
/* backward copy */
src += (num - 1) * stride;
dst += (num - 1) * stride;
stride *= -1;
}
sp = ri->ri_bits + src;
dp = ri->ri_bits + dst;
if (ri->ri_hwbits)
hp = ri->ri_hwbits + dst;
while (num--) {
memcpy(dp, sp, ri->ri_emustride);
if (ri->ri_hwbits) {
memcpy(hp, dp, ri->ri_emustride);
hp += stride;
}
sp += stride;
dp += stride;
}
}
/*
* Copy columns. This is slow, and hard to optimize due to alignment,
* and the fact that we have to copy both left->right and right->left.
* We simply cop-out here and use memmove(), since it handles all of
* these cases anyway.
*/
static void
rasops_copycols(void *cookie, int row, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int height;
uint8_t *sp, *dp, *hp;
hp = NULL; /* XXX GCC */
if (__predict_false(dst == src))
return;
#ifdef RASOPS_CLIPPING
/* Catches < 0 case too */
if ((unsigned)row >= (unsigned)ri->ri_rows)
return;
if (src < 0) {
num += src;
src = 0;
}
if (src + num > ri->ri_cols)
num = ri->ri_cols - src;
if (dst < 0) {
num += dst;
dst = 0;
}
if (dst + num > ri->ri_cols)
num = ri->ri_cols - dst;
if (num <= 0)
return;
#endif
height = ri->ri_font->fontheight;
row *= ri->ri_yscale;
num *= ri->ri_xscale;
sp = ri->ri_bits + row + src * ri->ri_xscale;
dp = ri->ri_bits + row + dst * ri->ri_xscale;
if (ri->ri_hwbits)
hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
while (height--) {
memmove(dp, sp, num);
if (ri->ri_hwbits) {
memcpy(hp, dp, num);
hp += ri->ri_stride;
}
sp += ri->ri_stride;
dp += ri->ri_stride;
}
}
/*
* Turn cursor off/on.
*/
static void
rasops_cursor(void *cookie, int on, int row, int col)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
/* Turn old cursor off */
if ((ri->ri_flg & RI_CURSOR) != 0)
#ifdef RASOPS_CLIPPING
if ((ri->ri_flg & RI_CURSORCLIP) == 0)
#endif
ri->ri_do_cursor(ri);
/* Select new cursor */
ri->ri_crow = row;
ri->ri_ccol = col;
#ifdef RASOPS_CLIPPING
ri->ri_flg &= ~RI_CURSORCLIP;
if (row < 0 || row >= ri->ri_rows)
ri->ri_flg |= RI_CURSORCLIP;
else if (col < 0 || col >= ri->ri_cols)
ri->ri_flg |= RI_CURSORCLIP;
#endif
if (on) {
ri->ri_flg |= RI_CURSOR;
#ifdef RASOPS_CLIPPING
if ((ri->ri_flg & RI_CURSORCLIP) == 0)
#endif
ri->ri_do_cursor(ri);
} else
ri->ri_flg &= ~RI_CURSOR;
}
/*
* Make the device colormap
*/
static void
rasops_init_devcmap(struct rasops_info *ri)
{
int i;
uint32_t c;
const uint8_t *p;
switch (ri->ri_depth) {
case 1:
ri->ri_devcmap[0] = 0;
for (i = 1; i < 16; i++)
ri->ri_devcmap[i] = -1;
return;
case 2:
for (i = 1; i < 15; i++)
ri->ri_devcmap[i] = 0xaaaaaaaa;
ri->ri_devcmap[0] = 0;
ri->ri_devcmap[8] = 0x55555555;
ri->ri_devcmap[15] = -1;
return;
case 4:
for (i = 0; i < 16; i++) {
#ifdef RASOPS_APPLE_PALETTE
c = apple4_devcmap[i];
#else
c = i;
#endif
ri->ri_devcmap[i] =
(c << 0) | (c << 4) | (c << 8) | (c << 12) |
(c << 16) | (c << 20) | (c << 24) | (c << 28);
}
return;
case 8:
if ((ri->ri_flg & RI_8BIT_IS_RGB) == 0) {
for (i = 0; i < 16; i++) {
#ifdef RASOPS_APPLE_PALETTE
c = apple8_devcmap[i];
#else
c = i;
#endif
ri->ri_devcmap[i] =
c | (c << 8) | (c << 16) | (c << 24);
}
return;
}
}
p = rasops_cmap;
for (i = 0; i < 16; i++) {
if (ri->ri_rnum <= 8)
c = (uint32_t)(*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
else
c = (uint32_t)(*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
p++;
if (ri->ri_gnum <= 8)
c |= (uint32_t)(*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
else
c |= (uint32_t)(*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
p++;
if (ri->ri_bnum <= 8)
c |= (uint32_t)(*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
else
c |= (uint32_t)(*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
p++;
/*
* Swap byte order if necessary. Then, fill the word for
* generic routines, which want this.
*/
switch (ri->ri_depth) {
case 8:
c |= c << 8;
c |= c << 16;
break;
case 15:
case 16:
if ((ri->ri_flg & RI_BSWAP) != 0)
c = bswap16(c);
c |= c << 16;
break;
case 24:
#if BYTE_ORDER == LITTLE_ENDIAN
if ((ri->ri_flg & RI_BSWAP) == 0)
#else
if ((ri->ri_flg & RI_BSWAP) != 0)
#endif
{
/*
* Convert to ``big endian'' if not RI_BSWAP.
*/
c = (c & 0x0000ff) << 16|
(c & 0x00ff00) |
(c & 0xff0000) >> 16;
}
/*
* No worries, we use generic routines only for
* gray colors, where all 3 bytes are same.
*/
c |= (c & 0xff) << 24;
break;
case 32:
if ((ri->ri_flg & RI_BSWAP) != 0)
c = bswap32(c);
break;
}
ri->ri_devcmap[i] = c;
}
}
/*
* Unpack a rasops attribute
*/
void
rasops_unpack_attr(long attr, int *fg, int *bg, int *underline)
{
*fg = ((uint32_t)attr >> 24) & 0xf;
*bg = ((uint32_t)attr >> 16) & 0xf;
if (underline != NULL)
*underline = (uint32_t)attr & WSATTR_UNDERLINE;
}
/*
* Erase rows. This isn't static, since 24-bpp uses it in special cases.
*/
void
rasops_eraserows(void *cookie, int row, int num, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int bytes;
uint32_t bg, *rp, *hp;
hp = NULL; /* XXX GCC */
#ifdef RASOPS_CLIPPING
if (row < 0) {
num += row;
row = 0;
}
if (row + num > ri->ri_rows)
num = ri->ri_rows - row;
if (num <= 0)
return;
#endif
/*
* XXX The wsdisplay_emulops interface seems a little deficient in
* that there is no way to clear the *entire* screen. We provide a
* workaround here: if the entire console area is being cleared, and
* the RI_FULLCLEAR flag is set, clear the entire display.
*/
if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
bytes = ri->ri_stride;
num = ri->ri_height;
rp = (uint32_t *)ri->ri_origbits;
if (ri->ri_hwbits)
hp = (uint32_t *)ri->ri_hworigbits;
} else {
bytes = ri->ri_emustride;
num *= ri->ri_font->fontheight;
rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
if (ri->ri_hwbits)
hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale);
}
bg = ATTR_BG(ri, attr);
while (num--) {
rasops_memset32(rp, bg, bytes);
if (ri->ri_hwbits) {
memcpy(hp, rp, bytes);
DELTA(hp, ri->ri_stride, uint32_t *);
}
DELTA(rp, ri->ri_stride, uint32_t *);
}
}
/*
* Actually turn the cursor on or off. This does the dirty work for
* rasops_cursor().
*/
static void
rasops_do_cursor(struct rasops_info *ri)
{
int row, col, height, slop1, slop2, full, cnt;
uint32_t mask1, mask2, *dp;
uint8_t tmp, *rp, *hp;
hp = NULL; /* XXX GCC */
#if NRASOPS_ROTATION > 0
if (ri->ri_flg & RI_ROTATE_MASK) {
if (ri->ri_flg & RI_ROTATE_CW) {
/* Rotate rows/columns */
row = ri->ri_ccol;
col = ri->ri_rows - ri->ri_crow - 1;
} else if (ri->ri_flg & RI_ROTATE_CCW) {
/* Rotate rows/columns */
row = ri->ri_cols - ri->ri_ccol - 1;
col = ri->ri_crow;
} else { /* upside-down */
row = ri->ri_crow;
col = ri->ri_ccol;
}
} else
#endif
{
row = ri->ri_crow;
col = ri->ri_ccol;
}
height = ri->ri_font->fontheight;
rp = ri->ri_bits + FBOFFSET(ri, row, col);
if (ri->ri_hwbits)
hp = ri->ri_hwbits + FBOFFSET(ri, row, col);
/*
* For ri_xscale = 1:
*
* Logic below does not work for ri_xscale = 1, e.g.,
* fontwidth = 8 and bpp = 1. So we take care of it.
*/
if (ri->ri_xscale == 1) {
while (height--) {
tmp = ~*rp;
*rp = tmp;
if (ri->ri_hwbits) {
*hp = tmp;
hp += ri->ri_stride;
}
rp += ri->ri_stride;
}
return;
}
/*
* For ri_xscale = 2, 3, 4, ...:
*
* Note that siop1 <= ri_xscale even for ri_xscale = 2,
* since rp % 3 = 0 or 2 (ri_stride % 4 = 0).
*/
slop1 = (4 - ((uintptr_t)rp & 3)) & 3;
slop2 = (ri->ri_xscale - slop1) & 3;
full = (ri->ri_xscale - slop1 /* - slop2 */) >> 2;
rp = (uint8_t *)((uintptr_t)rp & ~3);
hp = (uint8_t *)((uintptr_t)hp & ~3);
mask1 = rasops_lmask32[4 - slop1];
mask2 = rasops_rmask32[slop2];
while (height--) {
dp = (uint32_t *)rp;
if (slop1) {
*dp = *dp ^ mask1;
dp++;
}
for (cnt = full; cnt; cnt--) {
*dp = ~*(uint32_t *)dp;
dp++;
}
if (slop2)
*dp = *dp ^ mask2;
if (ri->ri_hwbits) {
memcpy(hp, rp, ((slop1 != 0) + full +
(slop2 != 0)) << 2);
hp += ri->ri_stride;
}
rp += ri->ri_stride;
}
}
/*
* Erase columns.
*/
void
rasops_erasecols(void *cookie, int row, int col, int num, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int height;
uint32_t bg, *rp, *hp;
hp = NULL; /* XXX GCC */
#ifdef RASOPS_CLIPPING
if ((unsigned)row >= (unsigned)ri->ri_rows)
return;
if (col < 0) {
num += col;
col = 0;
}
if (col + num > ri->ri_cols)
num = ri->ri_cols - col;
if (num <= 0)
return;
#endif
height = ri->ri_font->fontheight;
num *= ri->ri_xscale;
rp = (uint32_t *)(ri->ri_bits + FBOFFSET(ri, row, col));
if (ri->ri_hwbits)
hp = (uint32_t *)(ri->ri_hwbits + FBOFFSET(ri, row, col));
bg = ATTR_BG(ri, attr);
while (height--) {
rasops_memset32(rp, bg, num);
if (ri->ri_hwbits) {
memcpy(hp, rp, num);
DELTA(hp, ri->ri_stride, uint32_t *);
}
DELTA(rp, ri->ri_stride, uint32_t *);
}
}
void
rasops_make_box_chars_16(struct rasops_info *ri)
{
int c, i, mid;
uint16_t vert_mask, hmask_left, hmask_right;
uint16_t *data = (uint16_t *)ri->ri_optfont.data;
vert_mask = 0xc000U >> ((ri->ri_font->fontwidth >> 1) - 1);
hmask_left = 0xff00U << (8 - (ri->ri_font->fontwidth >> 1));
hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
mid = (ri->ri_font->fontheight + 1) >> 1;
/* 0x00 would be empty anyway so don't bother */
for (c = 1; c < 16; c++) {
data += ri->ri_font->fontheight;
if (c & 1) {
/* upper segment */
for (i = 0; i < mid; i++)
data[i] = vert_mask;
}
if (c & 4) {
/* lower segment */
for (i = mid; i < ri->ri_font->fontheight; i++)
data[i] = vert_mask;
}
if (c & 2) {
/* right segment */
i = ri->ri_font->fontheight >> 1;
data[mid - 1] |= hmask_right;
data[mid] |= hmask_right;
}
if (c & 8) {
/* left segment */
data[mid - 1] |= hmask_left;
data[mid] |= hmask_left;
}
}
}
void
rasops_make_box_chars_8(struct rasops_info *ri)
{
int c, i, mid;
uint8_t vert_mask, hmask_left, hmask_right;
uint8_t *data = (uint8_t *)ri->ri_optfont.data;
vert_mask = 0xc0U >> ((ri->ri_font->fontwidth >> 1) - 1);
hmask_left = 0xf0U << (4 - (ri->ri_font->fontwidth >> 1));
hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
mid = (ri->ri_font->fontheight + 1) >> 1;
/* 0x00 would be empty anyway so don't bother */
for (c = 1; c < 16; c++) {
data += ri->ri_font->fontheight;
if (c & 1) {
/* upper segment */
for (i = 0; i < mid; i++)
data[i] = vert_mask;
}
if (c & 4) {
/* lower segment */
for (i = mid; i < ri->ri_font->fontheight; i++)
data[i] = vert_mask;
}
if (c & 2) {
/* right segment */
i = ri->ri_font->fontheight >> 1;
data[mid - 1] |= hmask_right;
data[mid] |= hmask_right;
}
if (c & 8) {
/* left segment */
data[mid - 1] |= hmask_left;
data[mid] |= hmask_left;
}
}
}
void
rasops_make_box_chars_32(struct rasops_info *ri)
{
int c, i, mid;
uint32_t vert_mask, hmask_left, hmask_right;
uint32_t *data = (uint32_t *)ri->ri_optfont.data;
vert_mask = 0xc0000000U >> ((ri->ri_font->fontwidth >> 1) - 1);
hmask_left = 0xffff0000U << (16 - (ri->ri_font->fontwidth >> 1));
hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
mid = (ri->ri_font->fontheight + 1) >> 1;
/* 0x00 would be empty anyway so don't bother */
for (c = 1; c < 16; c++) {
data += ri->ri_font->fontheight;
if (c & 1) {
/* upper segment */
for (i = 0; i < mid; i++)
data[i] = vert_mask;
}
if (c & 4) {
/* lower segment */
for (i = mid; i < ri->ri_font->fontheight; i++)
data[i] = vert_mask;
}
if (c & 2) {
/* right segment */
i = ri->ri_font->fontheight >> 1;
data[mid - 1] |= hmask_right;
data[mid] |= hmask_right;
}
if (c & 8) {
/* left segment */
data[mid - 1] |= hmask_left;
data[mid] |= hmask_left;
}
}
}
void
rasops_make_box_chars_alpha(struct rasops_info *ri)
{
int c, i, hmid, vmid, wi, he;
uint8_t *data = (uint8_t *)ri->ri_optfont.data;
uint8_t *ddata;
he = ri->ri_font->fontheight;
wi = ri->ri_font->fontwidth;
vmid = (he + 1) >> 1;
hmid = (wi + 1) >> 1;
/* 0x00 would be empty anyway so don't bother */
for (c = 1; c < 16; c++) {
data += ri->ri_fontscale;
if (c & 1) {
/* upper segment */
ddata = data + hmid;
for (i = 0; i <= vmid; i++) {
*ddata = 0xff;
ddata += wi;
}
}
if (c & 4) {
/* lower segment */
ddata = data + wi * vmid + hmid;
for (i = vmid; i < he; i++) {
*ddata = 0xff;
ddata += wi;
}
}
if (c & 2) {
/* right segment */
ddata = data + wi * vmid + hmid;
for (i = hmid; i < wi; i++) {
*ddata = 0xff;
ddata++;
}
}
if (c & 8) {
/* left segment */
ddata = data + wi * vmid;
for (i = 0; i <= hmid; i++) {
*ddata = 0xff;
ddata++;
}
}
}
}
/*
* Return a colour map appropriate for the given struct rasops_info in the
* same form used by rasops_cmap[]
* For now this is either a copy of rasops_cmap[] or an R3G3B2 map, it should
* probably be a linear ( or gamma corrected? ) ramp for higher depths.
*/
int
rasops_get_cmap(struct rasops_info *ri, uint8_t *palette, size_t bytes)
{
if ((ri->ri_depth == 8) && ((ri->ri_flg & RI_8BIT_IS_RGB) != 0)) {
/* generate an R3G3B2 palette */
int i, idx = 0;
uint8_t tmp;
if (bytes < 256 * 3)
return EINVAL;
for (i = 0; i < 256; i++) {
tmp = i & 0xe0;
/*
* replicate bits so 0xe0 maps to a red value of 0xff
* in order to make white look actually white
*/
tmp |= (tmp >> 3) | (tmp >> 6);
palette[idx] = tmp;
idx++;
tmp = (i & 0x1c) << 3;
tmp |= (tmp >> 3) | (tmp >> 6);
palette[idx] = tmp;
idx++;
tmp = (i & 0x03) << 6;
tmp |= tmp >> 2;
tmp |= tmp >> 4;
palette[idx] = tmp;
idx++;
}
} else
memcpy(palette, rasops_cmap, uimin(bytes, sizeof(rasops_cmap)));
return 0;
}
#if NRASOPS_ROTATION > 0
/*
* Quarter clockwise rotation routines (originally intended for the
* built-in Zaurus C3x00 display in 16bpp).
*/
static void
rasops_rotate_font(int *cookie, int rotate)
{
struct rotatedfont *f;
int ncookie;
SLIST_FOREACH(f, &rotatedfonts, rf_next) {
if (f->rf_cookie == *cookie) {
*cookie = f->rf_rotated;
return;
}
}
/*
* We did not find a rotated version of this font. Ask the wsfont
* code to compute one for us.
*/
f = kmem_alloc(sizeof(*f), KM_SLEEP);
if ((ncookie = wsfont_rotate(*cookie, rotate)) == -1)
goto fail;
f->rf_cookie = *cookie;
f->rf_rotated = ncookie;
SLIST_INSERT_HEAD(&rotatedfonts, f, rf_next);
*cookie = ncookie;
return;
fail: kmem_free(f, sizeof(*f));
return;
}
static void
rasops_copychar(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int r_srcrow, r_dstrow, r_srccol, r_dstcol, height;
uint8_t *sp, *dp;
r_srcrow = srccol;
r_dstrow = dstcol;
r_srccol = ri->ri_rows - srcrow - 1;
r_dstcol = ri->ri_rows - dstrow - 1;
r_srcrow *= ri->ri_yscale;
r_dstrow *= ri->ri_yscale;
height = ri->ri_font->fontheight;
sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
while (height--) {
memmove(dp, sp, ri->ri_xscale);
dp += ri->ri_stride;
sp += ri->ri_stride;
}
}
static void
rasops_putchar_rotated_cw(void *cookie, int row, int col, u_int uc, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int height;
uint16_t fg, *rp;
if (__predict_false((unsigned int)row > ri->ri_rows ||
(unsigned int)col > ri->ri_cols))
return;
/* Avoid underflow */
if (ri->ri_rows - row - 1 < 0)
return;
/* Do rotated char sans (side)underline */
ri->ri_real_ops.putchar(cookie, col, ri->ri_rows - row - 1, uc,
attr & ~WSATTR_UNDERLINE);
/*
* Do rotated underline
* XXX this assumes 16-bit color depth
*/
if ((attr & WSATTR_UNDERLINE) != 0) {
height = ri->ri_font->fontheight;
rp = (uint16_t *)(ri->ri_bits + col * ri->ri_yscale +
(ri->ri_rows - row - 1) * ri->ri_xscale);
fg = (uint16_t)ATTR_FG(ri, attr);
while (height--) {
*rp = fg;
DELTA(rp, ri->ri_stride, uint16_t *);
}
}
}
static void
rasops_erasecols_rotated_cw(void *cookie, int row, int col, int num, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int i;
for (i = col; i < col + num; i++)
ri->ri_ops.putchar(cookie, row, i, ' ', attr);
}
/* XXX: these could likely be optimised somewhat. */
static void
rasops_copyrows_rotated_cw(void *cookie, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int col, roff;
if (src > dst)
for (roff = 0; roff < num; roff++)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar(cookie, src + roff, dst + roff,
col, col);
else
for (roff = num - 1; roff >= 0; roff--)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar(cookie, src + roff, dst + roff,
col, col);
}
static void
rasops_copycols_rotated_cw(void *cookie, int row, int src, int dst, int num)
{
int coff;
if (src > dst)
for (coff = 0; coff < num; coff++)
rasops_copychar(cookie, row, row, src + coff,
dst + coff);
else
for (coff = num - 1; coff >= 0; coff--)
rasops_copychar(cookie, row, row, src + coff,
dst + coff);
}
static void
rasops_eraserows_rotated_cw(void *cookie, int row, int num, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int col, rn;
for (rn = row; rn < row + num; rn++)
for (col = 0; col < ri->ri_cols; col++)
ri->ri_ops.putchar(cookie, rn, col, ' ', attr);
}
/*
* Quarter counter-clockwise rotation routines (originally intended for the
* built-in Sharp W-ZERO3 display in 16bpp).
*/
static void
rasops_copychar_ccw(void *cookie, int srcrow, int dstrow, int srccol,
int dstcol)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int r_srcrow, r_dstrow, r_srccol, r_dstcol, height;
uint8_t *sp, *dp;
r_srcrow = ri->ri_cols - srccol - 1;
r_dstrow = ri->ri_cols - dstcol - 1;
r_srccol = srcrow;
r_dstcol = dstrow;
r_srcrow *= ri->ri_yscale;
r_dstrow *= ri->ri_yscale;
height = ri->ri_font->fontheight;
sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
while (height--) {
memmove(dp, sp, ri->ri_xscale);
dp += ri->ri_stride;
sp += ri->ri_stride;
}
}
static void
rasops_putchar_rotated_ccw(void *cookie, int row, int col, u_int uc, long attr)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int height;
uint16_t fg, *rp;
if (__predict_false((unsigned int)row > ri->ri_rows ||
(unsigned int)col > ri->ri_cols))
return;
/* Avoid underflow */
if (ri->ri_cols - col - 1 < 0)
return;
/* Do rotated char sans (side)underline */
ri->ri_real_ops.putchar(cookie, ri->ri_cols - col - 1, row, uc,
attr & ~WSATTR_UNDERLINE);
/*
* Do rotated underline
* XXX this assumes 16-bit color depth
*/
if ((attr & WSATTR_UNDERLINE) != 0) {
height = ri->ri_font->fontheight;
rp = (uint16_t *)(ri->ri_bits +
(ri->ri_cols - col - 1) * ri->ri_yscale +
row * ri->ri_xscale +
(ri->ri_font->fontwidth - 1) * ri->ri_pelbytes);
fg = (uint16_t)ATTR_FG(ri, attr);
while (height--) {
*rp = fg;
DELTA(rp, ri->ri_stride, uint16_t *);
}
}
}
/* XXX: these could likely be optimised somewhat. */
static void
rasops_copyrows_rotated_ccw(void *cookie, int src, int dst, int num)
{
struct rasops_info *ri = (struct rasops_info *)cookie;
int col, roff;
if (src > dst)
for (roff = 0; roff < num; roff++)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar_ccw(cookie,
src + roff, dst + roff, col, col);
else
for (roff = num - 1; roff >= 0; roff--)
for (col = 0; col < ri->ri_cols; col++)
rasops_copychar_ccw(cookie,
src + roff, dst + roff, col, col);
}
static void
rasops_copycols_rotated_ccw(void *cookie, int row, int src, int dst, int num)
{
int coff;
if (src > dst)
for (coff = 0; coff < num; coff++)
rasops_copychar_ccw(cookie, row, row,
src + coff, dst + coff);
else
for (coff = num - 1; coff >= 0; coff--)
rasops_copychar_ccw(cookie, row, row,
src + coff, dst + coff);
}
#endif /* NRASOPS_ROTATION */
|