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
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAnnotation.h"
#include "SkBitmapHeap.h"
#include "SkCanvas.h"
#include "SkColorFilter.h"
#include "SkData.h"
#include "SkDrawLooper.h"
#include "SkGPipe.h"
#include "SkGPipePriv.h"
#include "SkImageFilter.h"
#include "SkMaskFilter.h"
#include "SkWriteBuffer.h"
#include "SkPaint.h"
#include "SkPatchUtils.h"
#include "SkPathEffect.h"
#include "SkPictureFlat.h"
#include "SkPtrRecorder.h"
#include "SkRasterizer.h"
#include "SkRRect.h"
#include "SkShader.h"
#include "SkStream.h"
#include "SkTextBlob.h"
#include "SkTSearch.h"
#include "SkTypeface.h"
#include "SkWriter32.h"
enum {
kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
};
static bool is_cross_process(uint32_t flags) {
return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
}
static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
SkASSERT(paintFlat < kCount_PaintFlats);
switch (paintFlat) {
case kColorFilter_PaintFlat: return paint.getColorFilter();
case kDrawLooper_PaintFlat: return paint.getLooper();
case kMaskFilter_PaintFlat: return paint.getMaskFilter();
case kPathEffect_PaintFlat: return paint.getPathEffect();
case kRasterizer_PaintFlat: return paint.getRasterizer();
case kShader_PaintFlat: return paint.getShader();
case kImageFilter_PaintFlat: return paint.getImageFilter();
case kXfermode_PaintFlat: return paint.getXfermode();
}
SkDEBUGFAIL("never gets here");
return NULL;
}
static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
SkASSERT(typeface);
SkDynamicMemoryWStream stream;
typeface->serialize(&stream);
size_t size = stream.getOffset();
if (writer) {
writer->write32(SkToU32(size));
SkAutoDataUnref data(stream.copyToData());
writer->writePad(data->data(), size);
}
return 4 + SkAlign4(size);
}
///////////////////////////////////////////////////////////////////////////////
class FlattenableHeap : public SkFlatController {
public:
FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
: INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
, fNumFlatsToKeep(numFlatsToKeep) {
SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
if (isCrossProcess) {
this->setNamedFactorySet(fset);
}
}
~FlattenableHeap() {
fPointers.freeAll();
}
void* allocThrow(size_t bytes) override;
void unalloc(void* ptr) override;
void setBitmapStorage(SkBitmapHeap* heap) {
this->setBitmapHeap(heap);
}
const SkFlatData* flatToReplace() const;
// Mark an SkFlatData as one that should not be returned by flatToReplace.
// Takes the result of SkFlatData::index() as its parameter.
void markFlatForKeeping(int index) {
*fFlatsThatMustBeKept.append() = index;
}
void markAllFlatsSafeToDelete() {
fFlatsThatMustBeKept.reset();
}
private:
// Keep track of the indices (i.e. the result of SkFlatData::index()) of
// flats that must be kept, since they are on the current paint.
SkTDArray<int> fFlatsThatMustBeKept;
SkTDArray<void*> fPointers;
const int fNumFlatsToKeep;
typedef SkFlatController INHERITED;
};
void FlattenableHeap::unalloc(void* ptr) {
int indexToRemove = fPointers.rfind(ptr);
if (indexToRemove >= 0) {
sk_free(ptr);
fPointers.remove(indexToRemove);
}
}
void* FlattenableHeap::allocThrow(size_t bytes) {
void* ptr = sk_malloc_throw(bytes);
*fPointers.append() = ptr;
return ptr;
}
const SkFlatData* FlattenableHeap::flatToReplace() const {
// First, determine whether we should replace one.
if (fPointers.count() > fNumFlatsToKeep) {
// Look through the flattenable heap.
// TODO: Return the LRU flat.
for (int i = 0; i < fPointers.count(); i++) {
SkFlatData* potential = (SkFlatData*)fPointers[i];
// Make sure that it is not one that must be kept.
bool mustKeep = false;
for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
if (potential->index() == fFlatsThatMustBeKept[j]) {
mustKeep = true;
break;
}
}
if (!mustKeep) {
return potential;
}
}
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
struct SkFlattenableTraits {
static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
buffer.writeFlattenable(&flattenable);
}
// No need to define unflatten if we never call it.
};
typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
///////////////////////////////////////////////////////////////////////////////
/**
* If SkBitmaps are to be flattened to send to the reader, this class is
* provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
*/
class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
public:
BitmapShuttle(SkGPipeCanvas*);
~BitmapShuttle();
bool insert(const SkBitmap& bitmap, int32_t slot) override;
/**
* Remove the SkGPipeCanvas used for insertion. After this, calls to
* insert will crash.
*/
void removeCanvas();
private:
SkGPipeCanvas* fCanvas;
};
///////////////////////////////////////////////////////////////////////////////
class SkGPipeCanvas : public SkCanvas {
public:
SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
uint32_t width, uint32_t height);
virtual ~SkGPipeCanvas();
/**
* Called when nothing else is to be written to the stream. Any repeated
* calls are ignored.
*
* @param notifyReaders Whether to send a message to the reader(s) that
* the writer is through sending commands. Should generally be true,
* unless there is an error which prevents further messages from
* being sent.
*/
void finish(bool notifyReaders) {
if (fDone) {
return;
}
if (notifyReaders && this->needOpBytes()) {
this->writeOp(kDone_DrawOp);
this->doNotify();
}
if (shouldFlattenBitmaps(fFlags)) {
// The following circular references exist:
// fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
// fBitmapHeap -> fExternalStorage -> fCanvas
// fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
// Break them all by destroying the final link to this SkGPipeCanvas.
fBitmapShuttle->removeCanvas();
}
fDone = true;
}
void flushRecording(bool detachCurrentBlock);
size_t freeMemoryIfPossible(size_t bytesToFree);
size_t storageAllocatedForRecording() {
size_t bytesAllocated = 0;
if (NULL != fBitmapHeap) {
bytesAllocated += fBitmapHeap->bytesAllocated();
}
if (NULL != fImageHeap) {
bytesAllocated += fImageHeap->bytesInCache();
}
return bytesAllocated;
}
/**
* Flatten an SkBitmap to send to the reader, where it will be referenced
* according to slot.
*/
bool shuttleBitmap(const SkBitmap&, int32_t slot);
void resetImageHeap();
protected:
void willSave() override;
SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
void willRestore() override;
void didConcat(const SkMatrix&) override;
void didSetMatrix(const SkMatrix&) override;
void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
const SkPaint&) override;
void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
const SkPaint&) override;
void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
SkScalar constY, const SkPaint&) override;
void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
const SkMatrix* matrix, const SkPaint&) override;
void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
const SkPaint& paint) override;
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
const SkPoint texCoords[4], SkXfermode* xmode,
const SkPaint& paint) override;
void onDrawPaint(const SkPaint&) override;
void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
void onDrawRect(const SkRect&, const SkPaint&) override;
void onDrawOval(const SkRect&, const SkPaint&) override;
void onDrawRRect(const SkRRect&, const SkPaint&) override;
void onDrawPath(const SkPath&, const SkPaint&) override;
void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
DrawBitmapRectFlags flags) override;
void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
const SkPaint*) override;
void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
const SkPaint*) override;
void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
void onDrawVertices(VertexMode vmode, int vertexCount,
const SkPoint vertices[], const SkPoint texs[],
const SkColor colors[], SkXfermode* xmode,
const uint16_t indices[], int indexCount,
const SkPaint&) override;
void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
void onClipRegion(const SkRegion&, SkRegion::Op) override;
void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
private:
void recordTranslate(const SkMatrix&);
void recordScale(const SkMatrix&);
void recordConcat(const SkMatrix&);
SkNamedFactorySet* fFactorySet;
SkBitmapHeap* fBitmapHeap;
SkImageHeap* fImageHeap;
SkGPipeController* fController;
SkWriter32& fWriter;
size_t fBlockSize; // amount allocated for writer
size_t fBytesNotified;
bool fDone;
const uint32_t fFlags;
SkRefCntSet fTypefaceSet;
uint32_t getTypefaceID(SkTypeface*);
inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
}
inline void writeOp(DrawOps op) {
fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
}
bool needOpBytes(size_t size = 0);
inline void doNotify() {
if (!fDone) {
size_t bytes = fWriter.bytesWritten() - fBytesNotified;
if (bytes > 0) {
fController->notifyWritten(bytes);
fBytesNotified += bytes;
}
}
}
typedef SkAutoSTMalloc<128, uint8_t> TypefaceBuffer;
size_t getInProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
size_t getCrossProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
// Should be called after any calls to an SkFlatDictionary::findAndReplace
// if a new SkFlatData was added when in cross process mode
void flattenFactoryNames();
FlattenableHeap fFlattenableHeap;
FlatDictionary fFlatDictionary;
SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
int fCurrFlatIndex[kCount_PaintFlats];
int flattenToIndex(SkFlattenable* obj, PaintFlats);
// Common code used by drawBitmap*. Behaves differently depending on the
// type of SkBitmapHeap being used, which is determined by the flags used.
bool commonDrawBitmap(const SkBitmap&, DrawOps, unsigned flags, size_t bytes, const SkPaint*);
bool commonDrawImage(const SkImage*, DrawOps, unsigned flags, size_t bytes, const SkPaint*);
SkPaint fPaint;
void writePaint(const SkPaint&);
class AutoPipeNotify {
public:
AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
~AutoPipeNotify() { fCanvas->doNotify(); }
private:
SkGPipeCanvas* fCanvas;
};
friend class AutoPipeNotify;
typedef SkCanvas INHERITED;
};
void SkGPipeCanvas::flattenFactoryNames() {
const char* name;
while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
size_t len = strlen(name);
if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
this->writeOp(kDef_Factory_DrawOp);
fWriter.writeString(name, len);
}
}
}
bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
SkASSERT(shouldFlattenBitmaps(fFlags));
SkWriteBuffer buffer;
buffer.setNamedFactoryRecorder(fFactorySet);
buffer.writeBitmap(bm);
this->flattenFactoryNames();
size_t size = buffer.bytesWritten();
if (this->needOpBytes(size)) {
this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
void* dst = static_cast<void*>(fWriter.reserve(size));
buffer.writeToMemory(dst);
return true;
}
return false;
}
// return 0 for NULL (or unflattenable obj), or index-base-1
// return ~(index-base-1) if an old flattenable was replaced
int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
SkASSERT(!fDone && fBitmapHeap != NULL);
if (NULL == obj) {
return 0;
}
fBitmapHeap->deferAddingOwners();
bool added, replaced;
const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
&added, &replaced);
fBitmapHeap->endAddingOwnersDeferral(added);
int index = flat->index();
if (added) {
if (is_cross_process(fFlags)) {
this->flattenFactoryNames();
}
size_t flatSize = flat->flatSize();
if (this->needOpBytes(flatSize)) {
this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
fWriter.write(flat->data(), flatSize);
}
}
if (replaced) {
index = ~index;
}
return index;
}
///////////////////////////////////////////////////////////////////////////////
#define MIN_BLOCK_SIZE (16 * 1024)
#define BITMAPS_TO_KEEP 5
#define FLATTENABLES_TO_KEEP 10
SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
SkWriter32* writer, uint32_t flags,
uint32_t width, uint32_t height)
: SkCanvas(width, height)
, fFactorySet(is_cross_process(flags) ? SkNEW(SkNamedFactorySet) : NULL)
, fWriter(*writer)
, fFlags(flags)
, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, is_cross_process(flags))
, fFlatDictionary(&fFlattenableHeap)
{
fController = controller;
fDone = false;
fBlockSize = 0; // need first block from controller
fBytesNotified = 0;
sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
// Tell the reader the appropriate flags to use.
if (this->needOpBytes()) {
this->writeOp(kReportFlags_DrawOp, fFlags, 0);
}
if (shouldFlattenBitmaps(flags)) {
fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
} else {
fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
(BITMAPS_TO_KEEP, controller->numberOfReaders()));
if (this->needOpBytes(sizeof(void*))) {
this->writeOp(kShareBitmapHeap_DrawOp);
fWriter.writePtr(static_cast<void*>(fBitmapHeap));
}
}
fFlattenableHeap.setBitmapStorage(fBitmapHeap);
fImageHeap = SkNEW(SkImageHeap);
if (this->needOpBytes(sizeof(void*))) {
this->writeOp(kShareImageHeap_DrawOp);
fWriter.writePtr(static_cast<void*>(fImageHeap));
}
this->doNotify();
}
SkGPipeCanvas::~SkGPipeCanvas() {
this->finish(true);
SkSafeUnref(fFactorySet);
SkSafeUnref(fBitmapHeap);
SkSafeUnref(fImageHeap);
}
bool SkGPipeCanvas::needOpBytes(size_t needed) {
if (fDone) {
return false;
}
needed += 4; // size of DrawOp atom
needed = SkAlign4(needed);
if (fWriter.bytesWritten() + needed > fBlockSize) {
// Before we wipe out any data that has already been written, read it out.
this->doNotify();
// If we're going to allocate a new block, allocate enough to make it worthwhile.
needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
void* block = fController->requestBlock(needed, &fBlockSize);
if (NULL == block) {
// Do not notify the readers, which would call this function again.
this->finish(false);
return false;
}
SkASSERT(SkIsAlign4(fBlockSize));
fWriter.reset(block, fBlockSize);
fBytesNotified = 0;
}
return true;
}
uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
uint32_t id = 0; // 0 means default/null typeface
if (face) {
id = fTypefaceSet.find(face);
if (0 == id) {
id = fTypefaceSet.add(face);
size_t size = writeTypeface(NULL, face);
if (this->needOpBytes(size)) {
this->writeOp(kDef_Typeface_DrawOp);
writeTypeface(&fWriter, face);
}
}
}
return id;
}
///////////////////////////////////////////////////////////////////////////////
#define NOTIFY_SETUP(canvas) \
AutoPipeNotify apn(canvas)
void SkGPipeCanvas::willSave() {
NOTIFY_SETUP(this);
if (this->needOpBytes()) {
this->writeOp(kSave_DrawOp);
}
this->INHERITED::willSave();
}
SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
SaveFlags saveFlags) {
NOTIFY_SETUP(this);
size_t size = 0;
unsigned opFlags = 0;
if (bounds) {
opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
size += sizeof(SkRect);
}
if (paint) {
opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
this->writePaint(*paint);
}
if (this->needOpBytes(size)) {
this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
if (bounds) {
fWriter.writeRect(*bounds);
}
}
this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
// we don't create a layer
return kNoLayer_SaveLayerStrategy;
}
void SkGPipeCanvas::willRestore() {
NOTIFY_SETUP(this);
if (this->needOpBytes()) {
this->writeOp(kRestore_DrawOp);
}
this->INHERITED::willRestore();
}
void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
if (this->needOpBytes(2 * sizeof(SkScalar))) {
this->writeOp(kTranslate_DrawOp);
fWriter.writeScalar(m.getTranslateX());
fWriter.writeScalar(m.getTranslateY());
}
}
void SkGPipeCanvas::recordScale(const SkMatrix& m) {
if (this->needOpBytes(2 * sizeof(SkScalar))) {
this->writeOp(kScale_DrawOp);
fWriter.writeScalar(m.getScaleX());
fWriter.writeScalar(m.getScaleY());
}
}
void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
if (this->needOpBytes(m.writeToMemory(NULL))) {
this->writeOp(kConcat_DrawOp);
fWriter.writeMatrix(m);
}
}
void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
if (!matrix.isIdentity()) {
NOTIFY_SETUP(this);
switch (matrix.getType()) {
case SkMatrix::kTranslate_Mask:
this->recordTranslate(matrix);
break;
case SkMatrix::kScale_Mask:
this->recordScale(matrix);
break;
default:
this->recordConcat(matrix);
break;
}
}
this->INHERITED::didConcat(matrix);
}
void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
NOTIFY_SETUP(this);
if (this->needOpBytes(matrix.writeToMemory(NULL))) {
this->writeOp(kSetMatrix_DrawOp);
fWriter.writeMatrix(matrix);
}
this->INHERITED::didSetMatrix(matrix);
}
void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
ClipEdgeStyle edgeStyle) {
NOTIFY_SETUP(this);
if (this->needOpBytes(sizeof(SkRect))) {
unsigned flags = 0;
if (kSoft_ClipEdgeStyle == edgeStyle) {
flags = kClip_HasAntiAlias_DrawOpFlag;
}
this->writeOp(kClipRect_DrawOp, flags, rgnOp);
fWriter.writeRect(rect);
}
this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
}
void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
ClipEdgeStyle edgeStyle) {
NOTIFY_SETUP(this);
if (this->needOpBytes(kSizeOfFlatRRect)) {
unsigned flags = 0;
if (kSoft_ClipEdgeStyle == edgeStyle) {
flags = kClip_HasAntiAlias_DrawOpFlag;
}
this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
fWriter.writeRRect(rrect);
}
this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
}
void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
ClipEdgeStyle edgeStyle) {
NOTIFY_SETUP(this);
if (this->needOpBytes(path.writeToMemory(NULL))) {
unsigned flags = 0;
if (kSoft_ClipEdgeStyle == edgeStyle) {
flags = kClip_HasAntiAlias_DrawOpFlag;
}
this->writeOp(kClipPath_DrawOp, flags, rgnOp);
fWriter.writePath(path);
}
// we just pass on the bounds of the path
this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
}
void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
NOTIFY_SETUP(this);
if (this->needOpBytes(region.writeToMemory(NULL))) {
this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
fWriter.writeRegion(region);
}
this->INHERITED::onClipRegion(region, rgnOp);
}
///////////////////////////////////////////////////////////////////////////////
void SkGPipeCanvas::onDrawPaint(const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes()) {
this->writeOp(kDrawPaint_DrawOp);
}
}
void SkGPipeCanvas::onDrawPoints(PointMode mode, size_t count,
const SkPoint pts[], const SkPaint& paint) {
if (count) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
this->writeOp(kDrawPoints_DrawOp, mode, 0);
fWriter.write32(SkToU32(count));
fWriter.write(pts, count * sizeof(SkPoint));
}
}
}
void SkGPipeCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(sizeof(SkRect))) {
this->writeOp(kDrawOval_DrawOp);
fWriter.writeRect(rect);
}
}
void SkGPipeCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(sizeof(SkRect))) {
this->writeOp(kDrawRect_DrawOp);
fWriter.writeRect(rect);
}
}
void SkGPipeCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(kSizeOfFlatRRect)) {
this->writeOp(kDrawRRect_DrawOp);
fWriter.writeRRect(rrect);
}
}
void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
this->writeOp(kDrawDRRect_DrawOp);
fWriter.writeRRect(outer);
fWriter.writeRRect(inner);
}
}
void SkGPipeCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(path.writeToMemory(NULL))) {
this->writeOp(kDrawPath_DrawOp);
fWriter.writePath(path);
}
}
bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
unsigned flags,
size_t opBytesNeeded,
const SkPaint* paint) {
if (fDone) {
return false;
}
if (paint != NULL) {
flags |= kDrawBitmap_HasPaint_DrawOpFlag;
this->writePaint(*paint);
}
// This needs to run first so its calls to needOpBytes() and its writes
// don't interlace with the needOpBytes() and write below.
SkASSERT(fBitmapHeap != NULL);
int32_t bitmapIndex = fBitmapHeap->insert(bm);
if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
return false;
}
if (this->needOpBytes(opBytesNeeded)) {
this->writeOp(op, flags, bitmapIndex);
return true;
}
return false;
}
void SkGPipeCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
const SkPaint* paint) {
NOTIFY_SETUP(this);
size_t opBytesNeeded = sizeof(SkScalar) * 2;
if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
fWriter.writeScalar(left);
fWriter.writeScalar(top);
}
}
void SkGPipeCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
const SkPaint* paint, DrawBitmapRectFlags dbmrFlags) {
NOTIFY_SETUP(this);
size_t opBytesNeeded = sizeof(SkRect);
bool hasSrc = src != NULL;
unsigned flags;
if (hasSrc) {
flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
opBytesNeeded += sizeof(int32_t) * 4;
} else {
flags = 0;
}
if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
flags |= kDrawBitmap_Bleed_DrawOpFlag;
}
if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
if (hasSrc) {
fWriter.writeRect(*src);
}
fWriter.writeRect(dst);
}
}
void SkGPipeCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) {
NOTIFY_SETUP(this);
size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
fWriter.write32(center.fLeft);
fWriter.write32(center.fTop);
fWriter.write32(center.fRight);
fWriter.write32(center.fBottom);
fWriter.writeRect(dst);
}
}
void SkGPipeCanvas::onDrawSprite(const SkBitmap& bm, int left, int top, const SkPaint* paint) {
NOTIFY_SETUP(this);
size_t opBytesNeeded = sizeof(int32_t) * 2;
if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
fWriter.write32(left);
fWriter.write32(top);
}
}
bool SkGPipeCanvas::commonDrawImage(const SkImage* image, DrawOps op, unsigned flags,
size_t opBytesNeeded, const SkPaint* paint) {
if (fDone) {
return false;
}
if (paint != NULL) {
flags |= kDrawBitmap_HasPaint_DrawOpFlag;
this->writePaint(*paint);
}
// This needs to run first so its calls to needOpBytes() and its writes
// don't interlace with the needOpBytes() and write below.
int32_t slot = fImageHeap->insert(image);
SkASSERT(slot != 0);
if (this->needOpBytes(opBytesNeeded)) {
this->writeOp(op, flags, slot);
return true;
}
return false;
}
void SkGPipeCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y,
const SkPaint* paint) {
NOTIFY_SETUP(this);
if (this->commonDrawImage(image, kDrawImage_DrawOp, 0, sizeof(SkScalar) * 2, paint)) {
fWriter.writeScalar(x);
fWriter.writeScalar(y);
}
}
void SkGPipeCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
const SkPaint* paint) {
NOTIFY_SETUP(this);
unsigned flags = 0;
size_t opBytesNeeded = sizeof(SkRect); // dst
if (src) {
flags |= kDrawBitmap_HasSrcRect_DrawOpFlag;
opBytesNeeded += sizeof(SkRect); // src
}
if (this->commonDrawImage(image, kDrawImageRect_DrawOp, flags, opBytesNeeded, paint)) {
if (src) {
fWriter.writeRect(*src);
}
fWriter.writeRect(dst);
}
}
void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
const SkPaint& paint) {
if (byteLength) {
NOTIFY_SETUP(this);
this->writePaint(paint);
if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
this->writeOp(kDrawText_DrawOp);
fWriter.write32(SkToU32(byteLength));
fWriter.writePad(text, byteLength);
fWriter.writeScalar(x);
fWriter.writeScalar(y);
}
}
}
void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
const SkPaint& paint) {
if (byteLength) {
NOTIFY_SETUP(this);
this->writePaint(paint);
int count = paint.textToGlyphs(text, byteLength, NULL);
if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
this->writeOp(kDrawPosText_DrawOp);
fWriter.write32(SkToU32(byteLength));
fWriter.writePad(text, byteLength);
fWriter.write32(count);
fWriter.write(pos, count * sizeof(SkPoint));
}
}
}
void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
SkScalar constY, const SkPaint& paint) {
if (byteLength) {
NOTIFY_SETUP(this);
this->writePaint(paint);
int count = paint.textToGlyphs(text, byteLength, NULL);
if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
this->writeOp(kDrawPosTextH_DrawOp);
fWriter.write32(SkToU32(byteLength));
fWriter.writePad(text, byteLength);
fWriter.write32(count);
fWriter.write(xpos, count * sizeof(SkScalar));
fWriter.writeScalar(constY);
}
}
}
void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
const SkMatrix* matrix, const SkPaint& paint) {
if (byteLength) {
NOTIFY_SETUP(this);
unsigned flags = 0;
size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
if (matrix) {
flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
size += matrix->writeToMemory(NULL);
}
this->writePaint(paint);
if (this->needOpBytes(size)) {
this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
fWriter.write32(SkToU32(byteLength));
fWriter.writePad(text, byteLength);
fWriter.writePath(path);
if (matrix) {
fWriter.writeMatrix(*matrix);
}
}
}
}
size_t SkGPipeCanvas::getInProcessTypefaces(const SkRefCntSet& typefaceSet,
TypefaceBuffer* buffer) {
// When in-process, we simply write out the typeface pointers.
size_t size = typefaceSet.count() * sizeof(SkTypeface*);
buffer->reset(size);
typefaceSet.copyToArray(reinterpret_cast<SkRefCnt**>(buffer->get()));
return size;
}
size_t SkGPipeCanvas::getCrossProcessTypefaces(const SkRefCntSet& typefaceSet,
TypefaceBuffer* buffer) {
// For cross-process we use typeface IDs.
size_t size = typefaceSet.count() * sizeof(uint32_t);
buffer->reset(size);
uint32_t* idBuffer = reinterpret_cast<uint32_t*>(buffer->get());
SkRefCntSet::Iter iter(typefaceSet);
int i = 0;
for (void* setPtr = iter.next(); setPtr; setPtr = iter.next()) {
idBuffer[i++] = this->getTypefaceID(reinterpret_cast<SkTypeface*>(setPtr));
}
SkASSERT(i == typefaceSet.count());
return size;
}
void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
const SkPaint& paint) {
NOTIFY_SETUP(this);
this->writePaint(paint);
// FIXME: this is inefficient but avoids duplicating the blob serialization logic.
SkRefCntSet typefaceSet;
SkWriteBuffer blobBuffer;
blobBuffer.setTypefaceRecorder(&typefaceSet);
blob->flatten(blobBuffer);
// Unlike most draw ops (which only use one paint/typeface), text blobs may reference
// an arbitrary number of typefaces. Since the one-paint-per-op model is not applicable,
// we need to serialize these explicitly.
TypefaceBuffer typefaceBuffer;
size_t typefaceSize = is_cross_process(fFlags)
? this->getCrossProcessTypefaces(typefaceSet, &typefaceBuffer)
: this->getInProcessTypefaces(typefaceSet, &typefaceBuffer);
// blob byte count + typeface count + x + y + blob data + an index (cross-process)
// or pointer (in-process) for each typeface
size_t size = 2 * sizeof(uint32_t)
+ 2 * sizeof(SkScalar)
+ blobBuffer.bytesWritten()
+ typefaceSize;
if (this->needOpBytes(size)) {
this->writeOp(kDrawTextBlob_DrawOp);
SkDEBUGCODE(size_t initialOffset = fWriter.bytesWritten();)
fWriter.writeScalar(x);
fWriter.writeScalar(y);
fWriter.write32(typefaceSet.count());
fWriter.write(typefaceBuffer.get(), typefaceSize);
fWriter.write32(SkToU32(blobBuffer.bytesWritten()));
uint32_t* pad = fWriter.reservePad(blobBuffer.bytesWritten());
blobBuffer.writeToMemory(pad);
SkASSERT(initialOffset + size == fWriter.bytesWritten());
}
}
void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
const SkPaint* paint) {
// we want to playback the picture into individual draw calls
//
// todo: do we always have to unroll? If the pipe is not cross-process, seems like
// we could just ref the picture and move on...? <reed, scroggo>
//
this->INHERITED::onDrawPicture(picture, matrix, paint);
}
void SkGPipeCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
const SkPoint vertices[], const SkPoint texs[],
const SkColor colors[], SkXfermode* xfer,
const uint16_t indices[], int indexCount,
const SkPaint& paint) {
if (0 == vertexCount) {
return;
}
NOTIFY_SETUP(this);
this->writePaint(paint);
unsigned flags = 0; // packs with the op, so needs no extra space
size_t size = 0;
size += 4; // vmode
size += 4; // vertex count
size += vertexCount * sizeof(SkPoint); // vertices
if (texs) {
flags |= kDrawVertices_HasTexs_DrawOpFlag;
size += vertexCount * sizeof(SkPoint);
}
if (colors) {
flags |= kDrawVertices_HasColors_DrawOpFlag;
size += vertexCount * sizeof(SkColor);
}
if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
flags |= kDrawVertices_HasXfermode_DrawOpFlag;
size += sizeof(int32_t); // SkXfermode::Mode
}
if (indices && indexCount > 0) {
flags |= kDrawVertices_HasIndices_DrawOpFlag;
size += 4; // index count
size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
}
if (this->needOpBytes(size)) {
this->writeOp(kDrawVertices_DrawOp, flags, 0);
fWriter.write32(vmode);
fWriter.write32(vertexCount);
fWriter.write(vertices, vertexCount * sizeof(SkPoint));
if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
fWriter.write(texs, vertexCount * sizeof(SkPoint));
}
if (flags & kDrawVertices_HasColors_DrawOpFlag) {
fWriter.write(colors, vertexCount * sizeof(SkColor));
}
if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
SkAssertResult(xfer->asMode(&mode));
fWriter.write32(mode);
}
if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
fWriter.write32(indexCount);
fWriter.writePad(indices, indexCount * sizeof(uint16_t));
}
}
}
void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
const SkPoint texCoords[4], SkXfermode* xmode,
const SkPaint& paint) {
NOTIFY_SETUP(this);
size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
unsigned flags = 0;
if (colors) {
flags |= kDrawVertices_HasColors_DrawOpFlag;
size += SkPatchUtils::kNumCorners * sizeof(SkColor);
}
if (texCoords) {
flags |= kDrawVertices_HasTexs_DrawOpFlag;
size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
}
if (xmode) {
SkXfermode::Mode mode;
if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
flags |= kDrawVertices_HasXfermode_DrawOpFlag;
size += sizeof(int32_t);
}
}
this->writePaint(paint);
if (this->needOpBytes(size)) {
this->writeOp(kDrawPatch_DrawOp, flags, 0);
fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
if (colors) {
fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
}
if (texCoords) {
fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
}
if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
SkAssertResult(xmode->asMode(&mode));
fWriter.write32(mode);
}
}
}
void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
this->doNotify();
if (detachCurrentBlock) {
// force a new block to be requested for the next recorded command
fBlockSize = 0;
}
}
void SkGPipeCanvas::resetImageHeap() {
if (fImageHeap) {
fImageHeap->reset();
}
}
size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
}
///////////////////////////////////////////////////////////////////////////////
template <typename T> uint32_t castToU32(T value) {
union {
T fSrc;
uint32_t fDst;
} data;
data.fSrc = value;
return data.fDst;
}
void SkGPipeCanvas::writePaint(const SkPaint& paint) {
if (fDone) {
return;
}
SkPaint& base = fPaint;
uint32_t storage[32];
uint32_t* ptr = storage;
if (base.getFlags() != paint.getFlags()) {
*ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
base.setFlags(paint.getFlags());
}
if (base.getColor() != paint.getColor()) {
*ptr++ = PaintOp_packOp(kColor_PaintOp);
*ptr++ = paint.getColor();
base.setColor(paint.getColor());
}
if (base.getFilterQuality() != paint.getFilterQuality()) {
*ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterQuality());
base.setFilterQuality(paint.getFilterQuality());
}
if (base.getStyle() != paint.getStyle()) {
*ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
base.setStyle(paint.getStyle());
}
if (base.getStrokeJoin() != paint.getStrokeJoin()) {
*ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
base.setStrokeJoin(paint.getStrokeJoin());
}
if (base.getStrokeCap() != paint.getStrokeCap()) {
*ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
base.setStrokeCap(paint.getStrokeCap());
}
if (base.getStrokeWidth() != paint.getStrokeWidth()) {
*ptr++ = PaintOp_packOp(kWidth_PaintOp);
*ptr++ = castToU32(paint.getStrokeWidth());
base.setStrokeWidth(paint.getStrokeWidth());
}
if (base.getStrokeMiter() != paint.getStrokeMiter()) {
*ptr++ = PaintOp_packOp(kMiter_PaintOp);
*ptr++ = castToU32(paint.getStrokeMiter());
base.setStrokeMiter(paint.getStrokeMiter());
}
if (base.getTextEncoding() != paint.getTextEncoding()) {
*ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
base.setTextEncoding(paint.getTextEncoding());
}
if (base.getHinting() != paint.getHinting()) {
*ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
base.setHinting(paint.getHinting());
}
if (base.getTextAlign() != paint.getTextAlign()) {
*ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
base.setTextAlign(paint.getTextAlign());
}
if (base.getTextSize() != paint.getTextSize()) {
*ptr++ = PaintOp_packOp(kTextSize_PaintOp);
*ptr++ = castToU32(paint.getTextSize());
base.setTextSize(paint.getTextSize());
}
if (base.getTextScaleX() != paint.getTextScaleX()) {
*ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
*ptr++ = castToU32(paint.getTextScaleX());
base.setTextScaleX(paint.getTextScaleX());
}
if (base.getTextSkewX() != paint.getTextSkewX()) {
*ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
*ptr++ = castToU32(paint.getTextSkewX());
base.setTextSkewX(paint.getTextSkewX());
}
if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
if (is_cross_process(fFlags)) {
uint32_t id = this->getTypefaceID(paint.getTypeface());
*ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
} else if (this->needOpBytes(sizeof(void*))) {
// Add to the set for ref counting.
fTypefaceSet.add(paint.getTypeface());
// It is safe to write the typeface to the stream before the rest
// of the paint unless we ever send a kReset_PaintOp, which we
// currently never do.
this->writeOp(kSetTypeface_DrawOp);
fWriter.writePtr(paint.getTypeface());
}
base.setTypeface(paint.getTypeface());
}
// This is a new paint, so all old flats can be safely purged, if necessary.
fFlattenableHeap.markAllFlatsSafeToDelete();
for (int i = 0; i < kCount_PaintFlats; i++) {
int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
bool replaced = index < 0;
if (replaced) {
index = ~index;
}
// Store the index of any flat that needs to be kept. 0 means no flat.
if (index > 0) {
fFlattenableHeap.markFlatForKeeping(index);
}
SkASSERT(index >= 0 && index <= fFlatDictionary.count());
if (index != fCurrFlatIndex[i] || replaced) {
*ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
fCurrFlatIndex[i] = index;
}
}
size_t size = (char*)ptr - (char*)storage;
if (size && this->needOpBytes(size)) {
this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
fWriter.write(storage, size);
for (size_t i = 0; i < size/4; i++) {
// SkDebugf("[%d] %08X\n", i, storage[i]);
}
}
//
// Do these after we've written kPaintOp_DrawOp
if (base.getAnnotation() != paint.getAnnotation()) {
if (NULL == paint.getAnnotation()) {
if (this->needOpBytes()) {
this->writeOp(kSetAnnotation_DrawOp, 0, 0);
}
} else {
SkWriteBuffer buffer;
paint.getAnnotation()->writeToBuffer(buffer);
const size_t size = buffer.bytesWritten();
if (this->needOpBytes(size)) {
this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
buffer.writeToMemory(fWriter.reserve(size));
}
}
base.setAnnotation(paint.getAnnotation());
}
}
///////////////////////////////////////////////////////////////////////////////
#include "SkGPipe.h"
SkGPipeController::~SkGPipeController() {
SkSafeUnref(fCanvas);
}
void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
SkRefCnt_SafeAssign(fCanvas, canvas);
}
void SkGPipeController::purgeCaches()
{
fCanvas->resetImageHeap();
// Other caches are self-purging with a small MRU pool
// We could purge them as well, but it is not clear whether
// that would be a win.
}
///////////////////////////////////////////////////////////////////////////////
SkGPipeWriter::SkGPipeWriter()
: fWriter(0) {
fCanvas = NULL;
}
SkGPipeWriter::~SkGPipeWriter() {
this->endRecording();
}
SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
uint32_t width, uint32_t height) {
if (NULL == fCanvas) {
fWriter.reset(NULL, 0);
fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
}
controller->setCanvas(fCanvas);
return fCanvas;
}
void SkGPipeWriter::endRecording() {
if (fCanvas) {
fCanvas->finish(true);
fCanvas->unref();
fCanvas = NULL;
}
}
void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
if (fCanvas) {
fCanvas->flushRecording(detachCurrentBlock);
}
}
size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
if (fCanvas) {
return fCanvas->freeMemoryIfPossible(bytesToFree);
}
return 0;
}
size_t SkGPipeWriter::storageAllocatedForRecording() const {
return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
}
///////////////////////////////////////////////////////////////////////////////
BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
SkASSERT(canvas != NULL);
fCanvas = canvas;
fCanvas->ref();
}
BitmapShuttle::~BitmapShuttle() {
this->removeCanvas();
}
bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
SkASSERT(fCanvas != NULL);
return fCanvas->shuttleBitmap(bitmap, slot);
}
void BitmapShuttle::removeCanvas() {
if (NULL == fCanvas) {
return;
}
fCanvas->unref();
fCanvas = NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
SkImageHeap::SkImageHeap() : fBytesInCache (0) {}
SkImageHeap::~SkImageHeap() {
fArray.unrefAll();
}
void SkImageHeap::reset() {
fArray.unrefAll();
fArray.rewind();
fBytesInCache = 0;
}
const SkImage* SkImageHeap::get(int32_t slot) const {
SkASSERT(slot > 0);
return fArray[slot - 1];
}
int32_t SkImageHeap::find(const SkImage* img) const {
int index = fArray.find(img);
if (index >= 0) {
return index + 1; // found
}
return 0; // not found
}
int32_t SkImageHeap::insert(const SkImage* img) {
int32_t slot = this->find(img);
if (slot) {
return slot;
}
// TODO: SkImage does not expose bytes per pixel, 4 is just a best guess.
fBytesInCache += img->width() * img->height() * 4;
*fArray.append() = SkRef(img);
return fArray.count(); // slot is always index+1
}
|