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
|
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <cairo.h>
#include <stdlib.h>
#include <setjmp.h>
#include <stdio.h>
#include <spice/draw.h>
#include "quic.h"
#include "lz.h"
#include "canvas_base.h"
#include "canvas_utils.h"
#include "rect.h"
#include "mutex.h"
#ifndef CANVAS_ERROR
#define CANVAS_ERROR(format, ...) { \
printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__); \
abort(); \
}
#endif
#ifdef CAIRO_CANVAS_ACCESS_TEST
#define access_test(cancas, ptr, size) \
if ((unsigned long)(ptr) < (cancas)->base || \
(unsigned long)(ptr) + (size) > (cancas)->max) { \
CANVAS_ERROR("access violation 0x%lx %lu", (unsigned long)ptr, (unsigned long)(size)); \
}
#else
#define access_test(cancas, base, size)
#endif
#ifndef ASSERT
#define ASSERT(x) if (!(x)) { \
printf("%s: ASSERT %s failed\n", __FUNCTION__, #x); \
abort(); \
}
#endif
#ifndef WARN
#define WARN(x) printf("warning: %s\n", x)
#endif
#ifndef DBG
#define DBG(level, format, ...) printf("%s: debug: " format "\n", __FUNCTION__, ## __VA_ARGS__);
#endif
#ifndef ALIGN
#define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
#endif
#ifndef MIN
#define MIN(x, y) (((x) <= (y)) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) (((x) >= (y)) ? (x) : (y))
#endif
#ifdef WIN32
typedef struct __declspec (align(1)) LZImage {
#else
typedef struct __attribute__ ((__packed__)) LZImage {
#endif
SpiceImageDescriptor descriptor;
union {
SpiceLZRGBData lz_rgb;
SpiceLZPLTData lz_plt;
};
} LZImage;
static const cairo_user_data_key_t invers_data_type = {0};
#ifdef CAIRO_CANVAS_CACH_IS_SHARED
/* should be defined and initialized once in application.cpp */
extern mutex_t cairo_surface_user_data_mutex;
#endif
static inline double fix_to_double(SPICE_FIXED28_4 fixed)
{
return (double)(fixed & 0x0f) / 0x0f + (fixed >> 4);
}
static inline uint32_t canvas_16bpp_to_32bpp(uint32_t color)
{
uint32_t ret;
ret = ((color & 0x001f) << 3) | ((color & 0x001c) >> 2);
ret |= ((color & 0x03e0) << 6) | ((color & 0x0380) << 1);
ret |= ((color & 0x7c00) << 9) | ((color & 0x7000) << 4);
return ret;
}
static inline int test_bit(void* addr, int bit)
{
return !!(((uint32_t*)addr)[bit >> 5] & (1 << (bit & 0x1f)));
}
static inline int test_bit_be(void* addr, int bit)
{
return !!(((uint8_t*)addr)[bit >> 3] & (0x80 >> (bit & 0x07)));
}
#ifdef WIN32
static HDC create_compatible_dc()
{
HDC dc = CreateCompatibleDC(NULL);
if (!dc) {
CANVAS_ERROR("create compatible DC failed");
}
return dc;
}
#endif
typedef struct LzData {
LzUsrContext usr;
LzContext *lz;
LzDecodeUsrData decode_data;
jmp_buf jmp_env;
char message_buf[512];
} LzData;
typedef struct GlzData {
void *decoder_opaque;
glz_decode_fn_t decode;
LzDecodeUsrData decode_data;
} GlzData;
typedef struct QuicData {
QuicUsrContext usr;
QuicContext *quic;
jmp_buf jmp_env;
#ifndef CAIRO_CANVAS_NO_CHUNKS
SPICE_ADDRESS next;
get_virt_fn_t get_virt;
void *get_virt_opaque;
validate_virt_fn_t validate_virt;
void *validate_virt_opaque;
#endif
char message_buf[512];
} QuicData;
typedef struct CanvasBase {
uint32_t color_shift;
uint32_t color_mask;
QuicData quic_data;
#ifdef CAIRO_CANVAS_ACCESS_TEST
unsigned long base;
unsigned long max;
#endif
#if defined(CAIRO_CANVAS_CACHE) || defined(CAIRO_CANVAS_IMAGE_CACHE)
void *bits_cache_opaque;
bits_cache_put_fn_t bits_cache_put;
bits_cache_get_fn_t bits_cache_get;
#endif
#ifdef CAIRO_CANVAS_CACHE
void *palette_cache_opaque;
palette_cache_put_fn_t palette_cache_put;
palette_cache_get_fn_t palette_cache_get;
palette_cache_release_fn_t palette_cache_release;
#endif
#ifdef WIN32
HDC dc;
#endif
LzData lz_data;
GlzData glz_data;
} CanvasBase;
#ifndef CAIRO_CANVAS_NO_CHUNKS
#ifdef __GNUC__
#define ATTR_PACKED __attribute__ ((__packed__))
#else
#pragma pack(push)
#pragma pack(1)
#define ATTR_PACKED
#endif
typedef struct ATTR_PACKED DataChunk {
uint32_t size;
SPICE_ADDRESS prev;
SPICE_ADDRESS next;
uint8_t data[0];
} DataChunk;
#undef ATTR_PACKED
#ifndef __GNUC__
#pragma pack(pop)
#endif
#endif
static inline void canvas_localize_palette(CanvasBase *canvas, SpicePalette *palette)
{
if (canvas->color_shift == 5) {
uint32_t *now = palette->ents;
uint32_t *end = now + palette->num_ents;
for (; now < end; now++) {
*now = canvas_16bpp_to_32bpp(*now);
}
}
}
//#define DEBUG_DUMP_COMPRESS
#ifdef DEBUG_DUMP_COMPRESS
static void dump_surface(cairo_surface_t *surface, int cache);
#endif
static cairo_surface_t *canvas_get_quic(CanvasBase *canvas, SpiceQUICImage *image, int invers)
{
cairo_surface_t *surface = NULL;
QuicData *quic_data = &canvas->quic_data;
QuicImageType type;
uint8_t *dest;
int stride;
int width;
int height;
int alpha;
#ifndef CAIRO_CANVAS_NO_CHUNKS
DataChunk **tmp;
DataChunk *chunk;
#endif
if (setjmp(quic_data->jmp_env)) {
cairo_surface_destroy(surface);
CANVAS_ERROR("quic error, %s", quic_data->message_buf);
}
#ifdef CAIRO_CANVAS_NO_CHUNKS
if (quic_decode_begin(quic_data->quic, (uint32_t *)image->quic.data,
image->quic.data_size >> 2, &type, &width, &height) == QUIC_ERROR) {
CANVAS_ERROR("quic decode begin failed");
}
#else
tmp = (DataChunk **)image->quic.data;
chunk = *tmp;
quic_data->next = chunk->next;
if (quic_decode_begin(quic_data->quic, (uint32_t *)chunk->data, chunk->size >> 2,
&type, &width, &height) == QUIC_ERROR) {
CANVAS_ERROR("quic decode begin failed");
}
#endif
switch (type) {
case QUIC_IMAGE_TYPE_RGBA:
alpha = 1;
break;
case QUIC_IMAGE_TYPE_RGB32:
case QUIC_IMAGE_TYPE_RGB24:
case QUIC_IMAGE_TYPE_RGB16:
alpha = 0;
break;
case QUIC_IMAGE_TYPE_INVALID:
case QUIC_IMAGE_TYPE_GRAY:
default:
CANVAS_ERROR("unexpected image type");
}
ASSERT((uint32_t)width == image->descriptor.width);
ASSERT((uint32_t)height == image->descriptor.height);
surface = surface_create(
#ifdef WIN32
canvas->dc,
#endif
alpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
width, height, FALSE);
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(surface)));
}
dest = cairo_image_surface_get_data(surface);
stride = cairo_image_surface_get_stride(surface);
if (quic_decode(quic_data->quic, alpha ? QUIC_IMAGE_TYPE_RGBA : QUIC_IMAGE_TYPE_RGB32,
dest, stride) == QUIC_ERROR) {
CANVAS_ERROR("quic decode failed");
}
if (invers) {
uint8_t *end = dest + height * stride;
for (; dest != end; dest += stride) {
uint32_t *pix;
uint32_t *end_pix;
pix = (uint32_t *)dest;
end_pix = pix + width;
for (; pix < end_pix; pix++) {
*pix ^= 0x00ffffff;
}
}
}
#ifdef DEBUG_DUMP_COMPRESS
dump_surface(surface, 0);
#endif
return surface;
}
static inline void canvas_copy_32bpp(uint8_t* dest, int dest_stride, uint8_t* src, int src_stride,
int width, uint8_t* end)
{
for (; src != end; src += src_stride, dest += dest_stride) {
memcpy(dest, src, width << 2);
}
}
static inline void canvas_copy_24bpp(uint8_t* dest, int dest_stride, uint8_t* src, int src_stride,
int width, uint8_t* end)
{
for (; src != end; src += src_stride, dest += dest_stride) {
uint8_t* src_line = src;
uint8_t* src_line_end = src_line + width * 3;
uint8_t* dest_line = dest;
for (; src_line < src_line_end; ++dest_line) {
*(dest_line++) = *(src_line++);
*(dest_line++) = *(src_line++);
*(dest_line++) = *(src_line++);
}
}
}
static inline void canvas_copy_16bpp(uint8_t* dest, int dest_stride, uint8_t* src, int src_stride,
int width, uint8_t* end)
{
for (; src != end; src += src_stride, dest += dest_stride) {
uint16_t* src_line = (uint16_t*)src;
uint16_t* src_line_end = src_line + width;
uint32_t* dest_line = (uint32_t*)dest;
for (; src_line < src_line_end; ++dest_line, src_line++) {
*dest_line = canvas_16bpp_to_32bpp(*src_line);
}
}
}
static inline void canvas_copy_8bpp(uint8_t *dest, int dest_stride, uint8_t *src, int src_stride,
int width, uint8_t *end, SpicePalette *palette)
{
if (!palette) {
CANVAS_ERROR("no palette");
}
for (; src != end; src += src_stride, dest += dest_stride) {
uint32_t *dest_line = (uint32_t*)dest;
uint8_t *src_line = src;
uint8_t *src_line_end = src_line + width;
while (src_line < src_line_end) {
ASSERT(*src_line < palette->num_ents);
*(dest_line++) = palette->ents[*(src_line++)];
}
}
}
static inline void canvas_copy_4bpp_be(uint8_t* dest, int dest_stride, uint8_t* src, int src_stride,
int width, uint8_t* end, SpicePalette *palette)
{
if (!palette) {
CANVAS_ERROR("no palette");
}
for (; src != end; src += src_stride, dest += dest_stride) {
uint32_t *dest_line = (uint32_t *)dest;
uint8_t *now = src;
int i;
for (i = 0; i < (width >> 1); i++) {
ASSERT((*now & 0x0f) < palette->num_ents);
ASSERT(((*now >> 4) & 0x0f) < palette->num_ents);
*(dest_line++) = palette->ents[(*now >> 4) & 0x0f];
*(dest_line++) = palette->ents[*(now++) & 0x0f];
}
if (width & 1) {
*(dest_line) = palette->ents[(*src >> 4) & 0x0f];
}
}
}
static inline void canvas_copy_1bpp_be(uint8_t* dest, int dest_stride, uint8_t* src, int src_stride,
int width, uint8_t* end, SpicePalette *palette)
{
uint32_t fore_color;
uint32_t back_color;
if (!palette) {
CANVAS_ERROR("no palette");
}
fore_color = palette->ents[1];
back_color = palette->ents[0];
for (; src != end; src += src_stride, dest += dest_stride) {
uint32_t* dest_line = (uint32_t*)dest;
int i;
for (i = 0; i < width; i++) {
if (test_bit_be(src, i)) {
*(dest_line++) = fore_color;
} else {
*(dest_line++) = back_color;
}
}
}
}
static cairo_surface_t *canvas_bitmap_to_surface(CanvasBase *canvas, SpiceBitmap* bitmap,
SpicePalette *palette)
{
uint8_t* src = (uint8_t *)SPICE_GET_ADDRESS(bitmap->data);
int src_stride;
uint8_t* end;
uint8_t* dest;
int dest_stride;
cairo_surface_t* cairo_surface;
src_stride = bitmap->stride;
end = src + (bitmap->y * src_stride);
access_test(canvas, src, bitmap->y * src_stride);
cairo_surface = surface_create(
#ifdef WIN32
canvas->dc,
#endif
(bitmap->format == SPICE_BITMAP_FMT_RGBA) ? CAIRO_FORMAT_ARGB32 :
CAIRO_FORMAT_RGB24,
bitmap->x, bitmap->y, FALSE);
if (cairo_surface_status(cairo_surface) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(cairo_surface)));
}
dest = cairo_image_surface_get_data(cairo_surface);
dest_stride = cairo_image_surface_get_stride(cairo_surface);
if (!(bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN)) {
ASSERT(bitmap->y > 0);
dest += dest_stride * ((int)bitmap->y - 1);
dest_stride = -dest_stride;
}
switch (bitmap->format) {
case SPICE_BITMAP_FMT_32BIT:
case SPICE_BITMAP_FMT_RGBA:
canvas_copy_32bpp(dest, dest_stride, src, src_stride, bitmap->x, end);
break;
case SPICE_BITMAP_FMT_24BIT:
canvas_copy_24bpp(dest, dest_stride, src, src_stride, bitmap->x, end);
break;
case SPICE_BITMAP_FMT_16BIT:
canvas_copy_16bpp(dest, dest_stride, src, src_stride, bitmap->x, end);
break;
case SPICE_BITMAP_FMT_8BIT:
canvas_copy_8bpp(dest, dest_stride, src, src_stride, bitmap->x, end, palette);
break;
case SPICE_BITMAP_FMT_4BIT_BE:
canvas_copy_4bpp_be(dest, dest_stride, src, src_stride, bitmap->x, end, palette);
break;
case SPICE_BITMAP_FMT_1BIT_BE:
canvas_copy_1bpp_be(dest, dest_stride, src, src_stride, bitmap->x, end, palette);
break;
}
return cairo_surface;
}
#ifdef CAIRO_CANVAS_CACHE
static inline SpicePalette *canvas_get_palett(CanvasBase *canvas, SPICE_ADDRESS base_palette, uint8_t flags)
{
SpicePalette *palette;
if (!base_palette) {
return NULL;
}
if (flags & SPICE_BITMAP_FLAGS_PAL_FROM_CACHE) {
palette = canvas->palette_cache_get(canvas->palette_cache_opaque, base_palette);
} else if (flags & SPICE_BITMAP_FLAGS_PAL_CACHE_ME) {
palette = (SpicePalette *)SPICE_GET_ADDRESS(base_palette);
access_test(canvas, palette, sizeof(SpicePalette));
access_test(canvas, palette, sizeof(SpicePalette) + palette->num_ents * sizeof(uint32_t));
canvas_localize_palette(canvas, palette);
canvas->palette_cache_put(canvas->palette_cache_opaque, palette);
} else {
palette = (SpicePalette *)SPICE_GET_ADDRESS(base_palette);
canvas_localize_palette(canvas, palette);
}
return palette;
}
static cairo_surface_t *canvas_get_lz(CanvasBase *canvas, LZImage *image, int invers)
{
LzData *lz_data = &canvas->lz_data;
uint8_t *comp_buf = NULL;
int comp_size;
uint8_t *decomp_buf = NULL;
uint8_t *src;
LzImageType type;
SpicePalette *palette;
int alpha;
int n_comp_pixels;
int width;
int height;
int top_down;
int stride;
if (setjmp(lz_data->jmp_env)) {
if (decomp_buf) {
free(decomp_buf);
}
CANVAS_ERROR("lz error, %s", lz_data->message_buf);
}
if (image->descriptor.type == SPICE_IMAGE_TYPE_LZ_RGB) {
comp_buf = image->lz_rgb.data;
comp_size = image->lz_rgb.data_size;
palette = NULL;
} else if (image->descriptor.type == SPICE_IMAGE_TYPE_LZ_PLT) {
comp_buf = image->lz_plt.data;
comp_size = image->lz_plt.data_size;
palette = canvas_get_palett(canvas, image->lz_plt.palette, image->lz_plt.flags);
} else {
CANVAS_ERROR("unexpected image type");
}
lz_decode_begin(lz_data->lz, comp_buf, comp_size, &type,
&width, &height, &n_comp_pixels, &top_down, palette);
switch (type) {
case LZ_IMAGE_TYPE_RGBA:
alpha = 1;
break;
case LZ_IMAGE_TYPE_RGB32:
case LZ_IMAGE_TYPE_RGB24:
case LZ_IMAGE_TYPE_RGB16:
case LZ_IMAGE_TYPE_PLT1_LE:
case LZ_IMAGE_TYPE_PLT1_BE:
case LZ_IMAGE_TYPE_PLT4_LE:
case LZ_IMAGE_TYPE_PLT4_BE:
case LZ_IMAGE_TYPE_PLT8:
alpha = 0;
break;
default:
CANVAS_ERROR("unexpected LZ image type");
}
ASSERT(width == image->descriptor.width);
ASSERT(height == image->descriptor.height);
ASSERT((image->descriptor.type == SPICE_IMAGE_TYPE_LZ_PLT) || (n_comp_pixels == width * height));
#ifdef WIN32
lz_data->decode_data.dc = canvas->dc;
#endif
alloc_lz_image_surface(&lz_data->decode_data, alpha ? LZ_IMAGE_TYPE_RGBA : LZ_IMAGE_TYPE_RGB32,
width, height, n_comp_pixels, top_down);
src = cairo_image_surface_get_data(lz_data->decode_data.out_surface);
stride = (n_comp_pixels / height) * 4;
if (!top_down) {
stride = -stride;
decomp_buf = src + stride * (height - 1);
} else {
decomp_buf = src;
}
lz_decode(lz_data->lz, alpha ? LZ_IMAGE_TYPE_RGBA : LZ_IMAGE_TYPE_RGB32, decomp_buf);
if (invers) {
uint8_t *line = src;
uint8_t *end = src + height * stride;
for (; line != end; line += stride) {
uint32_t *pix;
uint32_t *end_pix;
pix = (uint32_t *)line;
end_pix = pix + width;
for (; pix < end_pix; pix++) {
*pix ^= 0x00ffffff;
}
}
}
return lz_data->decode_data.out_surface;
}
// don't handle plts since bitmaps with plt can be decoded globaly to RGB32 (because
// same byte sequence can be transformed to different RGB pixels by different plts)
static cairo_surface_t *canvas_get_glz(CanvasBase *canvas, LZImage *image)
{
ASSERT(image->descriptor.type == SPICE_IMAGE_TYPE_GLZ_RGB);
#ifdef WIN32
canvas->glz_data.decode_data.dc = canvas->dc;
#endif
canvas->glz_data.decode(canvas->glz_data.decoder_opaque, image->lz_rgb.data, NULL,
&canvas->glz_data.decode_data);
/* global_decode calls alloc_lz_image, which sets canvas->glz_data.surface */
return (canvas->glz_data.decode_data.out_surface);
}
//#define DEBUG_DUMP_BITMAP
#ifdef DEBUG_DUMP_BITMAP
static void dump_bitmap(SpiceBitmap *bitmap, SpicePalette *palette)
{
uint8_t* data = (uint8_t *)SPICE_GET_ADDRESS(bitmap->data);
static uint32_t file_id = 0;
uint32_t i, j;
char file_str[200];
uint32_t id = ++file_id;
#ifdef WIN32
sprintf(file_str, "c:\\tmp\\spice_dump\\%u.%ubpp", id, bitmap->format);
#else
sprintf(file_str, "/tmp/spice_dump/%u.%ubpp", id, bitmap->format);
#endif
FILE *f = fopen(file_str, "wb");
if (!f) {
return;
}
fprintf(f, "%d\n", bitmap->format); // 1_LE,1_BE,....
fprintf(f, "%d %d\n", bitmap->x, bitmap->y); // width and height
fprintf(f, "%d\n", palette->num_ents); // #plt entries
for (i = 0; i < palette->num_ents; i++) {
fwrite(&(palette->ents[i]), 4, 1, f);
}
fprintf(f, "\n");
for (i = 0; i < bitmap->y; i++, data += bitmap->stride) {
uint8_t *now = data;
for (j = 0; j < bitmap->x; j++) {
fwrite(now, 1, 1, f);
now++;
}
}
}
#endif
static cairo_surface_t *canvas_get_bits(CanvasBase *canvas, SpiceBitmap *bitmap)
{
cairo_surface_t* surface;
SpicePalette *palette;
palette = canvas_get_palett(canvas, bitmap->palette, bitmap->flags);
#ifdef DEBUG_DUMP_BITMAP
if (palette) {
dump_bitmap(bitmap, palette);
}
#endif
surface = canvas_bitmap_to_surface(canvas, bitmap, palette);
if (palette && (bitmap->flags & SPICE_BITMAP_FLAGS_PAL_FROM_CACHE)) {
canvas->palette_cache_release(palette);
}
return surface;
}
#else
static cairo_surface_t *canvas_get_bits(CanvasBase *canvas, SpiceBitmap *bitmap)
{
SpicePalette *palette;
if (!bitmap->palette) {
return canvas_bitmap_to_surface(canvas, bitmap, NULL);
}
palette = (SpicePalette *)SPICE_GET_ADDRESS(bitmap->palette);
if (canvas->color_shift == 5) {
int size = sizeof(SpicePalette) + (palette->num_ents << 2);
SpicePalette *local_palette = malloc(size);
cairo_surface_t* surface;
memcpy(local_palette, palette, size);
canvas_localize_palette(canvas, local_palette);
surface = canvas_bitmap_to_surface(canvas, bitmap, local_palette);
free(local_palette);
return surface;
} else {
return canvas_bitmap_to_surface(canvas, bitmap, palette);
}
}
#endif
// caution: defining DEBUG_DUMP_SURFACE will dump both cached & non-cached
// images to disk. it will reduce performance dramatically & eat
// disk space rapidly. use it only for debugging.
//#define DEBUG_DUMP_SURFACE
#if defined(DEBUG_DUMP_SURFACE) || defined(DEBUG_DUMP_COMPRESS)
static void dump_surface(cairo_surface_t *surface, int cache)
{
static uint32_t file_id = 0;
int i, j;
char file_str[200];
cairo_format_t format = cairo_image_surface_get_format(surface);
if (format != CAIRO_FORMAT_RGB24 && format != CAIRO_FORMAT_ARGB32) {
return;
}
uint8_t *data = cairo_image_surface_get_data(surface);
int width = cairo_image_surface_get_width(surface);
int height = cairo_image_surface_get_height(surface);
int stride = cairo_image_surface_get_stride(surface);
uint32_t id = ++file_id;
#ifdef WIN32
sprintf(file_str, "c:\\tmp\\spice_dump\\%d\\%u.ppm", cache, id);
#else
sprintf(file_str, "/tmp/spice_dump/%u.ppm", id);
#endif
FILE *f = fopen(file_str, "wb");
if (!f) {
return;
}
fprintf(f, "P6\n");
fprintf(f, "%d %d\n", width, height);
fprintf(f, "#spicec dump\n");
fprintf(f, "255\n");
for (i = 0; i < height; i++, data += stride) {
uint8_t *now = data;
for (j = 0; j < width; j++) {
fwrite(&now[2], 1, 1, f);
fwrite(&now[1], 1, 1, f);
fwrite(&now[0], 1, 1, f);
now += 4;
}
}
fclose(f);
}
#endif
#if defined(CAIRO_CANVAS_CACHE) || defined(CAIRO_CANVAS_IMAGE_CACHE)
static void __release_surface(void *inv_surf)
{
cairo_surface_destroy((cairo_surface_t *)inv_surf);
}
//#define DEBUG_LZ
static cairo_surface_t *canvas_get_image(CanvasBase *canvas, SPICE_ADDRESS addr)
{
SpiceImageDescriptor *descriptor = (SpiceImageDescriptor *)SPICE_GET_ADDRESS(addr);
cairo_surface_t *surface;
access_test(canvas, descriptor, sizeof(SpiceImageDescriptor));
#ifdef DEBUG_LZ
LOG_DEBUG("canvas_get_image image type: " << (int)descriptor->type);
#endif
switch (descriptor->type) {
case SPICE_IMAGE_TYPE_QUIC: {
SpiceQUICImage *image = (SpiceQUICImage *)descriptor;
access_test(canvas, descriptor, sizeof(SpiceQUICImage));
surface = canvas_get_quic(canvas, image, 0);
break;
}
#ifdef CAIRO_CANVAS_NO_CHUNKS
case SPICE_IMAGE_TYPE_LZ_PLT: {
access_test(canvas, descriptor, sizeof(SpiceLZPLTImage));
LZImage *image = (LZImage *)descriptor;
surface = canvas_get_lz(canvas, image, 0);
break;
}
case SPICE_IMAGE_TYPE_LZ_RGB: {
access_test(canvas, descriptor, sizeof(SpiceLZRGBImage));
LZImage *image = (LZImage *)descriptor;
surface = canvas_get_lz(canvas, image, 0);
break;
}
#endif
#ifdef USE_GLZ
case SPICE_IMAGE_TYPE_GLZ_RGB: {
access_test(canvas, descriptor, sizeof(SpiceLZRGBImage));
LZImage *image = (LZImage *)descriptor;
surface = canvas_get_glz(canvas, image);
break;
}
#endif
case SPICE_IMAGE_TYPE_FROM_CACHE:
return canvas->bits_cache_get(canvas->bits_cache_opaque, descriptor->id);
case SPICE_IMAGE_TYPE_BITMAP: {
SpiceBitmapImage *bitmap = (SpiceBitmapImage *)descriptor;
access_test(canvas, descriptor, sizeof(SpiceBitmapImage));
surface = canvas_get_bits(canvas, &bitmap->bitmap);
break;
}
default:
CANVAS_ERROR("invalid image type");
}
if (descriptor->flags & SPICE_IMAGE_FLAGS_CACHE_ME) {
canvas->bits_cache_put(canvas->bits_cache_opaque, descriptor->id, surface);
#ifdef DEBUG_DUMP_SURFACE
dump_surface(surface, 1);
#endif
} else if (descriptor->type != SPICE_IMAGE_TYPE_FROM_CACHE) {
#ifdef DEBUG_DUMP_SURFACE
dump_surface(surface, 0);
#endif
}
return surface;
}
#else
static cairo_surface_t *canvas_get_image(CairoCanvas *canvas, SPICE_ADDRESS addr)
{
SpiceImageDescriptor *descriptor = (SpiceImageDescriptor *)SPICE_GET_ADDRESS(addr);
access_test(canvas, descriptor, sizeof(SpiceImageDescriptor));
switch (descriptor->type) {
case SPICE_IMAGE_TYPE_QUIC: {
SpiceQUICImage *image = (SpiceQUICImage *)descriptor;
access_test(canvas, descriptor, sizeof(SpiceQUICImage));
return canvas_get_quic(canvas, image, 0);
}
case SPICE_IMAGE_TYPE_BITMAP: {
SpiceBitmapImage *bitmap = (SpiceBitmapImage *)descriptor;
access_test(canvas, descriptor, sizeof(SpiceBitmapImage));
return canvas_get_bits(canvas, &bitmap->bitmap);
}
default:
CANVAS_ERROR("invalid image type");
}
}
#endif
static inline uint8_t revers_bits(uint8_t byte)
{
uint8_t ret = 0;
int i;
for (i = 0; i < 4; i++) {
int shift = 7 - i * 2;
ret |= (byte & (1 << i)) << shift;
ret |= (byte & (0x80 >> i)) >> shift;
}
return ret;
}
static cairo_surface_t *canvas_get_bitmap_mask(CanvasBase *canvas, SpiceBitmap* bitmap, int invers)
{
cairo_surface_t *surface;
uint8_t *src_line;
uint8_t *end_line;
uint8_t *dest_line;
int src_stride;
int line_size;
int dest_stride;
surface = surface_create(
#ifdef WIN32
canvas->dc,
#endif
CAIRO_FORMAT_A1, bitmap->x, bitmap->y, TRUE);
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(surface)));
}
src_line = (uint8_t *)SPICE_GET_ADDRESS(bitmap->data);
src_stride = bitmap->stride;
end_line = src_line + (bitmap->y * src_stride);
access_test(canvas, src_line, end_line - src_line);
line_size = ALIGN(bitmap->x, 8) >> 3;
dest_stride = cairo_image_surface_get_stride(surface);
dest_line = cairo_image_surface_get_data(surface);
#if defined(GL_CANVAS)
if ((bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN)) {
#else
if (!(bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN)) {
#endif
ASSERT(bitmap->y > 0);
dest_line += dest_stride * ((int)bitmap->y - 1);
dest_stride = -dest_stride;
}
if (invers) {
switch (bitmap->format) {
#if defined(GL_CANVAS) || defined(GDI_CANVAS)
case SPICE_BITMAP_FMT_1BIT_BE:
#else
case SPICE_BITMAP_FMT_1BIT_LE:
#endif
for (; src_line != end_line; src_line += src_stride, dest_line += dest_stride) {
uint8_t *dest = dest_line;
uint8_t *now = src_line;
uint8_t *end = now + line_size;
while (now < end) {
*(dest++) = ~*(now++);
}
}
break;
#if defined(GL_CANVAS) || defined(GDI_CANVAS)
case SPICE_BITMAP_FMT_1BIT_LE:
#else
case SPICE_BITMAP_FMT_1BIT_BE:
#endif
for (; src_line != end_line; src_line += src_stride, dest_line += dest_stride) {
uint8_t *dest = dest_line;
uint8_t *now = src_line;
uint8_t *end = now + line_size;
while (now < end) {
*(dest++) = ~revers_bits(*(now++));
}
}
break;
default:
cairo_surface_destroy(surface);
CANVAS_ERROR("invalid bitmap format");
}
} else {
switch (bitmap->format) {
#if defined(GL_CANVAS) || defined(GDI_CANVAS)
case SPICE_BITMAP_FMT_1BIT_BE:
#else
case SPICE_BITMAP_FMT_1BIT_LE:
#endif
for (; src_line != end_line; src_line += src_stride, dest_line += dest_stride) {
memcpy(dest_line, src_line, line_size);
}
break;
#if defined(GL_CANVAS) || defined(GDI_CANVAS)
case SPICE_BITMAP_FMT_1BIT_LE:
#else
case SPICE_BITMAP_FMT_1BIT_BE:
#endif
for (; src_line != end_line; src_line += src_stride, dest_line += dest_stride) {
uint8_t *dest = dest_line;
uint8_t *now = src_line;
uint8_t *end = now + line_size;
while (now < end) {
*(dest++) = revers_bits(*(now++));
}
}
break;
default:
cairo_surface_destroy(surface);
CANVAS_ERROR("invalid bitmap format");
}
}
return surface;
}
static inline cairo_surface_t *canvas_A1_invers(cairo_surface_t *src_surf)
{
int width = cairo_image_surface_get_width(src_surf);
int height = cairo_image_surface_get_height(src_surf);
cairo_surface_t * invers = cairo_image_surface_create(CAIRO_FORMAT_A1, width, height);
if (cairo_surface_status(invers) == CAIRO_STATUS_SUCCESS) {
uint8_t *src_line = cairo_image_surface_get_data(src_surf);
int src_stride = cairo_image_surface_get_stride(src_surf);
uint8_t *end_line = src_line + (height * src_stride);
int line_size = ALIGN(width, 8) >> 3;
uint8_t *dest_line = cairo_image_surface_get_data(invers);
int dest_stride = cairo_image_surface_get_stride(invers);
for (; src_line != end_line; src_line += src_stride, dest_line += dest_stride) {
uint8_t *dest = dest_line;
uint8_t *now = src_line;
uint8_t *end = now + line_size;
while (now < end) {
*(dest++) = ~*(now++);
}
}
}
return invers;
}
static cairo_surface_t *canvas_surf_to_invers(cairo_surface_t *surf)
{
int width = cairo_image_surface_get_width(surf);
int height = cairo_image_surface_get_height(surf);
uint8_t *dest_line;
uint8_t *dest_line_end;
uint8_t *src_line;
int dest_stride;
int src_stride;
ASSERT(cairo_image_surface_get_format(surf) == CAIRO_FORMAT_RGB24);
cairo_surface_t *invers = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
if (cairo_surface_status(invers) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(invers)));
}
dest_line = cairo_image_surface_get_data(invers);
dest_stride = cairo_image_surface_get_stride(invers);
dest_line_end = dest_line + dest_stride * height;
src_line = cairo_image_surface_get_data(surf);
src_stride = cairo_image_surface_get_stride(surf);
for (; dest_line != dest_line_end; dest_line += dest_stride, src_line += src_stride) {
uint32_t *src = (uint32_t *)src_line;
uint32_t *dest = (uint32_t *)dest_line;
uint32_t *end = dest + width;
while (dest < end) {
*(dest++) = ~*(src++) & 0x00ffffff;
}
}
return invers;
}
/*
* Return the inversed surface and assigns it to the user data of the given surface.
* The routine also handles the reference count of the inversed surface. It you don't use
* the returned reference, you must call cairo_surface_destroy.
* Thread safe with respect to the user data.
*/
static inline cairo_surface_t* canvas_handle_inverse_user_data(cairo_surface_t* surface)
{
cairo_surface_t *inv_surf = NULL;
#ifdef CAIRO_CANVAS_CACH_IS_SHARED
MUTEX_LOCK(cairo_surface_user_data_mutex);
#endif
inv_surf = (cairo_surface_t *)cairo_surface_get_user_data(surface, &invers_data_type);
#ifdef CAIRO_CANVAS_CACH_IS_SHARED
MUTEX_UNLOCK(cairo_surface_user_data_mutex);
#endif
if (!inv_surf) {
if (cairo_image_surface_get_format(surface) == CAIRO_FORMAT_A1) {
inv_surf = canvas_A1_invers(surface);
} else {
inv_surf = canvas_surf_to_invers(surface);
}
if (cairo_surface_status(inv_surf) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(inv_surf);
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(surface)));
}
#ifdef CAIRO_CANVAS_CACH_IS_SHARED
MUTEX_LOCK(cairo_surface_user_data_mutex);
// checking if other thread has already assigned the user data
if (!cairo_surface_get_user_data(surface, &invers_data_type)) {
#endif
if (cairo_surface_set_user_data(surface, &invers_data_type, inv_surf,
__release_surface) == CAIRO_STATUS_SUCCESS) {
cairo_surface_reference(inv_surf);
}
#ifdef CAIRO_CANVAS_CACH_IS_SHARED
}
MUTEX_UNLOCK(cairo_surface_user_data_mutex);
#endif
} else {
cairo_surface_reference(inv_surf);
}
return inv_surf;
}
static cairo_surface_t *canvas_get_mask(CanvasBase *canvas, SpiceQMask *mask)
{
SpiceImageDescriptor *descriptor;
cairo_surface_t *surface;
int need_invers;
int is_invers;
int cache_me;
if (!mask->bitmap) {
return NULL;
}
descriptor = (SpiceImageDescriptor *)SPICE_GET_ADDRESS(mask->bitmap);
access_test(canvas, descriptor, sizeof(SpiceImageDescriptor));
need_invers = mask->flags & SPICE_MASK_FLAGS_INVERS;
#ifdef CAIRO_CANVAS_CACHE
cache_me = descriptor->flags & SPICE_IMAGE_FLAGS_CACHE_ME;
#else
cache_me = 0;
#endif
switch (descriptor->type) {
case SPICE_IMAGE_TYPE_BITMAP: {
SpiceBitmapImage *bitmap = (SpiceBitmapImage *)descriptor;
access_test(canvas, descriptor, sizeof(SpiceBitmapImage));
is_invers = need_invers && !cache_me;
surface = canvas_get_bitmap_mask(canvas, &bitmap->bitmap, is_invers);
break;
}
#if defined(CAIRO_CANVAS_CACHE) || defined(CAIRO_CANVAS_IMAGE_CACHE)
case SPICE_IMAGE_TYPE_FROM_CACHE:
surface = canvas->bits_cache_get(canvas->bits_cache_opaque, descriptor->id);
is_invers = 0;
break;
#endif
default:
CANVAS_ERROR("invalid image type");
}
#if defined(CAIRO_CANVAS_CACHE) || defined(CAIRO_CANVAS_IMAGE_CACHE)
if (cache_me) {
canvas->bits_cache_put(canvas->bits_cache_opaque, descriptor->id, surface);
}
if (need_invers && !is_invers) { // surface is in cache
cairo_surface_t *inv_surf;
inv_surf = canvas_handle_inverse_user_data(surface);
cairo_surface_destroy(surface);
surface = inv_surf;
}
#endif
return surface;
}
static inline SpiceRasterGlyph *canvas_next_raster_glyph(const SpiceRasterGlyph *glyph, int bpp)
{
return (SpiceRasterGlyph *)((uint8_t *)(glyph + 1) +
(ALIGN(glyph->width * bpp, 8) * glyph->height >> 3));
}
static inline void canvas_raster_glyph_box(const SpiceRasterGlyph *glyph, SpiceRect *r)
{
ASSERT(r);
r->top = glyph->render_pos.y + glyph->glyph_origin.y;
r->bottom = r->top + glyph->height;
r->left = glyph->render_pos.x + glyph->glyph_origin.x;
r->right = r->left + glyph->width;
}
#ifdef GL_CANVAS
static inline void __canvas_put_bits(uint8_t *dest, int offset, uint8_t val, int n)
{
uint8_t mask;
int now;
dest = dest + (offset >> 3);
offset &= 0x07;
now = MIN(8 - offset, n);
mask = ~((1 << (8 - now)) - 1);
mask >>= offset;
*dest = ((val >> offset) & mask) | *dest;
if ((n = n - now)) {
mask = ~((1 << (8 - n)) - 1);
dest++;
*dest = ((val << now) & mask) | *dest;
}
}
#else
static inline void __canvas_put_bits(uint8_t *dest, int offset, uint8_t val, int n)
{
uint8_t mask;
int now;
dest = dest + (offset >> 3);
offset &= 0x07;
now = MIN(8 - offset, n);
mask = (1 << now) - 1;
mask <<= offset;
val = revers_bits(val);
*dest = ((val << offset) & mask) | *dest;
if ((n = n - now)) {
mask = (1 << n) - 1;
dest++;
*dest = ((val >> now) & mask) | *dest;
}
}
#endif
static inline void canvas_put_bits(uint8_t *dest, int dest_offset, uint8_t *src, int n)
{
while (n) {
int now = MIN(n, 8);
n -= now;
__canvas_put_bits(dest, dest_offset, *src, now);
dest_offset += now;
src++;
}
}
static void canvas_put_glyph_bits(SpiceRasterGlyph *glyph, int bpp, uint8_t *dest, int dest_stride,
SpiceRect *bounds)
{
SpiceRect glyph_box;
uint8_t *src;
int lines;
int width;
//todo: support SPICE_STRING_FLAGS_RASTER_TOP_DOWN
canvas_raster_glyph_box(glyph, &glyph_box);
ASSERT(glyph_box.top >= bounds->top && glyph_box.bottom <= bounds->bottom);
ASSERT(glyph_box.left >= bounds->left && glyph_box.right <= bounds->right);
rect_offset(&glyph_box, -bounds->left, -bounds->top);
dest += glyph_box.top * dest_stride;
src = glyph->data;
lines = glyph_box.bottom - glyph_box.top;
width = glyph_box.right - glyph_box.left;
switch (bpp) {
case 1: {
int src_stride = ALIGN(width, 8) >> 3;
int i;
src += src_stride * (lines);
for (i = 0; i < lines; i++) {
src -= src_stride;
canvas_put_bits(dest, glyph_box.left, src, width);
dest += dest_stride;
}
break;
}
case 4: {
uint8_t *end;
int src_stride = ALIGN(width * 4, 8) >> 3;
src += src_stride * lines;
dest += glyph_box.left;
end = dest + dest_stride * lines;
for (; dest != end; dest += dest_stride) {
int i = 0;
uint8_t *now;
src -= src_stride;
now = src;
while (i < (width & ~1)) {
dest[i] = MAX(dest[i], *now & 0xf0);
dest[i + 1] = MAX(dest[i + 1], *now << 4);
i += 2;
now++;
}
if (i < width) {
dest[i] = MAX(dest[i], *now & 0xf0);
now++;
}
}
break;
}
case 8: {
uint8_t *end;
src += width * lines;
dest += glyph_box.left;
end = dest + dest_stride * lines;
for (; dest != end; dest += dest_stride, src -= width) {
int i;
for (i = 0; i < width; i++) {
dest[i] = MAX(dest[i], src[i]);
}
}
break;
}
default:
CANVAS_ERROR("invalid bpp");
}
}
static cairo_surface_t *canvas_get_str_mask(CanvasBase *canvas, SpiceString *str, int bpp, SpicePoint *pos)
{
SpiceRasterGlyph *glyph = (SpiceRasterGlyph *)str->data;
SpiceRasterGlyph *next_glyph;
SpiceRect bounds;
cairo_surface_t *str_mask;
uint8_t *dest;
int dest_stride;
int i;
ASSERT(str->length > 0);
access_test(canvas, glyph, sizeof(SpiceRasterGlyph));
next_glyph = canvas_next_raster_glyph(glyph, bpp);
access_test(canvas, glyph, (uint8_t*)next_glyph - (uint8_t*)glyph);
canvas_raster_glyph_box(glyph, &bounds);
for (i = 1; i < str->length; i++) {
SpiceRect glyph_box;
glyph = next_glyph;
access_test(canvas, glyph, sizeof(SpiceRasterGlyph));
next_glyph = canvas_next_raster_glyph(glyph, bpp);
access_test(canvas, glyph, (uint8_t*)next_glyph - (uint8_t*)glyph);
canvas_raster_glyph_box(glyph, &glyph_box);
rect_union(&bounds, &glyph_box);
}
str_mask = cairo_image_surface_create((bpp == 1) ? CAIRO_FORMAT_A1 : CAIRO_FORMAT_A8,
bounds.right - bounds.left,
bounds.bottom - bounds.top);
if (cairo_surface_status(str_mask) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(str_mask)));
}
dest = cairo_image_surface_get_data(str_mask);
dest_stride = cairo_image_surface_get_stride(str_mask);
glyph = (SpiceRasterGlyph *)str->data;
for (i = 0; i < str->length; i++) {
#if defined(GL_CANVAS)
canvas_put_glyph_bits(glyph, bpp, dest + (bounds.bottom - bounds.top - 1) * dest_stride,
-dest_stride, &bounds);
#else
canvas_put_glyph_bits(glyph, bpp, dest, dest_stride, &bounds);
#endif
glyph = canvas_next_raster_glyph(glyph, bpp);
}
pos->x = bounds.left;
pos->y = bounds.top;
return str_mask;
}
static inline SpiceVectorGlyph *canvas_next_vector_glyph(const SpiceVectorGlyph *glyph)
{
return (SpiceVectorGlyph *)((uint8_t *)(glyph + 1) + glyph->data_size);
}
static cairo_surface_t *canvas_scale_surface(cairo_surface_t *src, const SpiceRect *src_area, int width,
int hight, int scale_mode)
{
cairo_t *cairo;
cairo_surface_t *surface;
cairo_pattern_t *pattern;
cairo_matrix_t matrix;
double sx, sy;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, hight);
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s",
cairo_status_to_string(cairo_surface_status(surface)));
}
cairo = cairo_create(surface);
if (cairo_status(cairo) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create surface failed, %s", cairo_status_to_string(cairo_status(cairo)));
}
pattern = cairo_pattern_create_for_surface(src);
if (cairo_pattern_status(pattern) != CAIRO_STATUS_SUCCESS) {
CANVAS_ERROR("create pattern failed, %s",
cairo_status_to_string(cairo_pattern_status(pattern)));
}
sx = (double)(src_area->right - src_area->left) / width;
sy = (double)(src_area->bottom - src_area->top) / hight;
cairo_matrix_init_translate(&matrix, src_area->left, src_area->top);
cairo_matrix_scale(&matrix, sx, sy);
cairo_pattern_set_matrix(pattern, &matrix);
ASSERT(scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE || scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST);
cairo_pattern_set_filter(pattern, (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) ?
CAIRO_FILTER_NEAREST : CAIRO_FILTER_GOOD);
cairo_set_source(cairo, pattern);
cairo_pattern_destroy(pattern);
cairo_paint(cairo);
cairo_destroy(cairo);
return surface;
}
static void quic_usr_error(QuicUsrContext *usr, const char *fmt, ...)
{
QuicData *usr_data = (QuicData *)usr;
va_list ap;
va_start(ap, fmt);
vsnprintf(usr_data->message_buf, sizeof(usr_data->message_buf), fmt, ap);
va_end(ap);
longjmp(usr_data->jmp_env, 1);
}
static void quic_usr_warn(QuicUsrContext *usr, const char *fmt, ...)
{
QuicData *usr_data = (QuicData *)usr;
va_list ap;
va_start(ap, fmt);
vsnprintf(usr_data->message_buf, sizeof(usr_data->message_buf), fmt, ap);
va_end(ap);
}
static void *quic_usr_malloc(QuicUsrContext *usr, int size)
{
return malloc(size);
}
static void quic_usr_free(QuicUsrContext *usr, void *ptr)
{
free(ptr);
}
#ifdef CAIRO_CANVAS_NO_CHUNKS
static int quic_usr_more_space(QuicUsrContext *usr, uint32_t **io_ptr, int rows_completed)
{
return 0;
}
static void lz_usr_warn(LzUsrContext *usr, const char *fmt, ...)
{
LzData *usr_data = (LzData *)usr;
va_list ap;
va_start(ap, fmt);
vsnprintf(usr_data->message_buf, sizeof(usr_data->message_buf), fmt, ap);
va_end(ap);
}
static void lz_usr_error(LzUsrContext *usr, const char *fmt, ...)
{
LzData *usr_data = (LzData *)usr;
va_list ap;
va_start(ap, fmt);
vsnprintf(usr_data->message_buf, sizeof(usr_data->message_buf), fmt, ap);
va_end(ap);
longjmp(usr_data->jmp_env, 1);
}
static void *lz_usr_malloc(LzUsrContext *usr, int size)
{
return malloc(size);
}
static void lz_usr_free(LzUsrContext *usr, void *ptr)
{
free(ptr);
}
static int lz_usr_more_space(LzUsrContext *usr, uint8_t **io_ptr)
{
return 0;
}
static int lz_usr_more_lines(LzUsrContext *usr, uint8_t **lines)
{
return 0;
}
#else
static int quic_usr_more_space(QuicUsrContext *usr, uint32_t **io_ptr, int rows_completed)
{
QuicData *quic_data = (QuicData *)usr;
DataChunk *chunk;
uint32_t size;
if (!quic_data->next) {
return 0;
}
chunk = (DataChunk *)quic_data->get_virt(quic_data->get_virt_opaque, quic_data->next,
sizeof(DataChunk));
size = chunk->size;
quic_data->validate_virt(quic_data->validate_virt_opaque, (unsigned long)chunk->data,
quic_data->next, size);
quic_data->next = chunk->next;
*io_ptr = (uint32_t *)chunk->data;
return size >> 2;
}
#endif
static int quic_usr_more_lines(QuicUsrContext *usr, uint8_t **lines)
{
return 0;
}
#ifdef CAIRO_CANVAS_ACCESS_TEST
static void __canvas_set_access_params(CanvasBase *canvas, unsigned long base, unsigned long max)
{
canvas->base = base;
canvas->max = max;
}
#endif
static void canvas_base_destroy(CanvasBase *canvas)
{
quic_destroy(canvas->quic_data.quic);
#ifdef CAIRO_CANVAS_NO_CHUNKS
lz_destroy(canvas->lz_data.lz);
#endif
#ifdef GDI_CANVAS
DeleteDC(canvas->dc);
#endif
}
#ifdef CAIRO_CANVAS_CACHE
static int canvas_base_init(CanvasBase *canvas, int depth,
void *bits_cache_opaque,
bits_cache_put_fn_t bits_cache_put,
bits_cache_get_fn_t bits_cache_get,
void *palette_cache_opaque,
palette_cache_put_fn_t palette_cache_put,
palette_cache_get_fn_t palette_cache_get,
palette_cache_release_fn_t palette_cache_release
#elif defined(CAIRO_CANVAS_IMAGE_CACHE)
static int canvas_base_init(CanvasBase *canvas, int depth,
void *bits_cache_opaque,
bits_cache_put_fn_t bits_cache_put,
bits_cache_get_fn_t bits_cache_get
#else
static int canvas_base_init(CanvasBase *canvas, int depth
#endif
#ifdef USE_GLZ
, void *glz_decoder_opaque, glz_decode_fn_t glz_decode
#endif
#ifndef CAIRO_CANVAS_NO_CHUNKS
, void *get_virt_opaque, get_virt_fn_t get_virt,
void *validate_virt_opaque, validate_virt_fn_t validate_virt
#endif
)
{
canvas->quic_data.usr.error = quic_usr_error;
canvas->quic_data.usr.warn = quic_usr_warn;
canvas->quic_data.usr.info = quic_usr_warn;
canvas->quic_data.usr.malloc = quic_usr_malloc;
canvas->quic_data.usr.free = quic_usr_free;
canvas->quic_data.usr.more_space = quic_usr_more_space;
canvas->quic_data.usr.more_lines = quic_usr_more_lines;
#ifndef CAIRO_CANVAS_NO_CHUNKS
canvas->quic_data.get_virt_opaque = get_virt_opaque;
canvas->quic_data.get_virt = get_virt;
canvas->quic_data.validate_virt_opaque = validate_virt_opaque;
canvas->quic_data.validate_virt = validate_virt;
#endif
if (!(canvas->quic_data.quic = quic_create(&canvas->quic_data.usr))) {
return 0;
}
#ifdef CAIRO_CANVAS_NO_CHUNKS
canvas->lz_data.usr.error = lz_usr_error;
canvas->lz_data.usr.warn = lz_usr_warn;
canvas->lz_data.usr.info = lz_usr_warn;
canvas->lz_data.usr.malloc = lz_usr_malloc;
canvas->lz_data.usr.free = lz_usr_free;
canvas->lz_data.usr.more_space = lz_usr_more_space;
canvas->lz_data.usr.more_lines = lz_usr_more_lines;
if (!(canvas->lz_data.lz = lz_create(&canvas->lz_data.usr))) {
return 0;
}
#endif
#ifdef USE_GLZ
canvas->glz_data.decoder_opaque = glz_decoder_opaque;
canvas->glz_data.decode = glz_decode;
#endif
if (depth == 16) {
canvas->color_shift = 5;
canvas->color_mask = 0x1f;
} else {
canvas->color_shift = 8;
canvas->color_mask = 0xff;
}
#if defined(CAIRO_CANVAS_CACHE) || defined(CAIRO_CANVAS_IMAGE_CACHE)
canvas->bits_cache_opaque = bits_cache_opaque;
canvas->bits_cache_put = bits_cache_put;
canvas->bits_cache_get = bits_cache_get;
#endif
#ifdef CAIRO_CANVAS_CACHE
canvas->palette_cache_opaque = palette_cache_opaque;
canvas->palette_cache_put = palette_cache_put;
canvas->palette_cache_get = palette_cache_get;
canvas->palette_cache_release = palette_cache_release;
#endif
#ifdef WIN32
canvas->dc = NULL;
#endif
#ifdef GDI_CANVAS
canvas->dc = create_compatible_dc();
if (!canvas->dc) {
lz_destroy(canvas->lz_data.lz);
return 0;
}
#endif
return 1;
}
|