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
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkDebugCanvas.h"
#include "SkDevice.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "SkPicturePlayback.h"
#include "SkPictureRecord.h"
#include "SkStream.h"
#include "picture_utils.h"
#include "path_utils.h"
static void usage() {
SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n");
SkDebugf(" [-h|--help]\n\n");
SkDebugf(" -i inFile : file to filter.\n");
SkDebugf(" -o outFile : result of filtering.\n");
SkDebugf(" --input-dir : process all files in dir with .skp extension.\n");
SkDebugf(" --output-dir : results of filtering the input dir.\n");
SkDebugf(" -h|--help : Show this help message.\n");
}
// Is the supplied paint simply a color?
static bool is_simple(const SkPaint& p) {
return NULL == p.getPathEffect() &&
NULL == p.getShader() &&
NULL == p.getXfermode() &&
NULL == p.getMaskFilter() &&
NULL == p.getColorFilter() &&
NULL == p.getRasterizer() &&
NULL == p.getLooper() &&
NULL == p.getImageFilter();
}
// Check for:
// SAVE_LAYER
// DRAW_BITMAP_RECT_TO_RECT
// RESTORE
// where the saveLayer's color can be moved into the drawBitmapRect
static bool check_0(SkDebugCanvas* canvas, int curCommand) {
if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+2 ||
DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+2)->getType()) {
return false;
}
SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+1);
const SkPaint* saveLayerPaint = saveLayer->paint();
SkPaint* dbmrPaint = dbmr->paint();
// For this optimization we only fold the saveLayer and drawBitmapRect
// together if the saveLayer's draw is simple (i.e., no fancy effects)
// and the only difference in the colors is their alpha value
SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque
SkColor dbmrColor = dbmrPaint->getColor() | 0xFF000000; // force opaque
// If either operation lacks a paint then the collapse is trivial
return NULL == saveLayerPaint ||
NULL == dbmrPaint ||
(is_simple(*saveLayerPaint) && dbmrColor == layerColor);
}
// Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer
// and restore
static void apply_0(SkDebugCanvas* canvas, int curCommand) {
SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
const SkPaint* saveLayerPaint = saveLayer->paint();
// if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed
if (NULL != saveLayerPaint) {
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+1);
SkPaint* dbmrPaint = dbmr->paint();
if (NULL == dbmrPaint) {
// if the DBMR doesn't have a paint just use the saveLayer's
dbmr->setPaint(*saveLayerPaint);
} else if (NULL != saveLayerPaint) {
// Both paints are present so their alphas need to be combined
SkColor color = saveLayerPaint->getColor();
int a0 = SkColorGetA(color);
color = dbmrPaint->getColor();
int a1 = SkColorGetA(color);
int newA = SkMulDiv255Round(a0, a1);
SkASSERT(newA <= 0xFF);
SkColor newColor = SkColorSetA(color, newA);
dbmrPaint->setColor(newColor);
}
}
canvas->deleteDrawCommandAt(curCommand+2); // restore
canvas->deleteDrawCommandAt(curCommand); // saveLayer
}
// Check for:
// SAVE_LAYER
// SAVE
// CLIP_RECT
// DRAW_BITMAP_RECT_TO_RECT
// RESTORE
// RESTORE
// where the saveLayer's color can be moved into the drawBitmapRect
static bool check_1(SkDebugCanvas* canvas, int curCommand) {
if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+5 ||
SAVE != canvas->getDrawCommandAt(curCommand+1)->getType() ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+3)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+4)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+5)->getType()) {
return false;
}
SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+3);
const SkPaint* saveLayerPaint = saveLayer->paint();
SkPaint* dbmrPaint = dbmr->paint();
// For this optimization we only fold the saveLayer and drawBitmapRect
// together if the saveLayer's draw is simple (i.e., no fancy effects) and
// and the only difference in the colors is that the saveLayer's can have
// an alpha while the drawBitmapRect's is opaque.
// TODO: it should be possible to fold them together even if they both
// have different non-255 alphas but this is low priority since we have
// never seen that case
// If either operation lacks a paint then the collapse is trivial
SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque
return NULL == saveLayerPaint ||
NULL == dbmrPaint ||
(is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor);
}
// Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer
// and restore
static void apply_1(SkDebugCanvas* canvas, int curCommand) {
SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
const SkPaint* saveLayerPaint = saveLayer->paint();
// if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed
if (NULL != saveLayerPaint) {
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+3);
SkPaint* dbmrPaint = dbmr->paint();
if (NULL == dbmrPaint) {
dbmr->setPaint(*saveLayerPaint);
} else {
SkColor newColor = SkColorSetA(dbmrPaint->getColor(),
SkColorGetA(saveLayerPaint->getColor()));
dbmrPaint->setColor(newColor);
}
}
canvas->deleteDrawCommandAt(curCommand+5); // restore
canvas->deleteDrawCommandAt(curCommand); // saveLayer
}
// Check for:
// SAVE
// CLIP_RECT
// DRAW_RECT
// RESTORE
// where the rect is entirely within the clip and the clip is an intersect
static bool check_2(SkDebugCanvas* canvas, int curCommand) {
if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+4 ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
return false;
}
ClipRect* cr = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2);
if (SkRegion::kIntersect_Op != cr->op()) {
return false;
}
return cr->rect().contains(dr->rect());
}
// Remove everything but the drawRect
static void apply_2(SkDebugCanvas* canvas, int curCommand) {
canvas->deleteDrawCommandAt(curCommand+3); // restore
// drawRect
canvas->deleteDrawCommandAt(curCommand+1); // clipRect
canvas->deleteDrawCommandAt(curCommand); // save
}
// Check for:
// SAVE
// CLIP_RRECT
// DRAW_RECT
// RESTORE
// where the rect entirely encloses the clip
static bool check_3(SkDebugCanvas* canvas, int curCommand) {
if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+4 ||
CLIP_RRECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
return false;
}
ClipRRect* crr = (ClipRRect*) canvas->getDrawCommandAt(curCommand+1);
DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2);
if (SkRegion::kIntersect_Op != crr->op()) {
return false;
}
return dr->rect().contains(crr->rrect().rect());
}
// Replace everything with a drawRRect with the paint from the drawRect
// and the AA settings from the clipRRect
static void apply_3(SkDebugCanvas* canvas, int curCommand) {
canvas->deleteDrawCommandAt(curCommand+3); // restore
ClipRRect* crr = (ClipRRect*) canvas->getDrawCommandAt(curCommand+1);
DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2);
// TODO: could skip paint re-creation if the AA settings already match
SkPaint newPaint = dr->paint();
newPaint.setAntiAlias(crr->doAA());
DrawRRect* drr = new DrawRRect(crr->rrect(), newPaint);
canvas->setDrawCommandAt(curCommand+2, drr);
canvas->deleteDrawCommandAt(curCommand+1); // clipRRect
canvas->deleteDrawCommandAt(curCommand); // save
}
// Check for:
// SAVE
// CLIP_RECT
// DRAW_BITMAP_RECT_TO_RECT
// RESTORE
// where the rect and drawBitmapRect dst exactly match
static bool check_4(SkDebugCanvas* canvas, int curCommand) {
if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+4 ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
return false;
}
ClipRect* cr = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2);
if (SkRegion::kIntersect_Op != cr->op()) {
return false;
}
return dbmr->dstRect() == cr->rect();
}
// Remove everything but the drawBitmapRect
static void apply_4(SkDebugCanvas* canvas, int curCommand) {
canvas->deleteDrawCommandAt(curCommand+3); // restore
// drawBitmapRectToRect
canvas->deleteDrawCommandAt(curCommand+1); // clipRect
canvas->deleteDrawCommandAt(curCommand); // save
}
// Check for:
// TRANSLATE
// where the translate is zero
static bool check_5(SkDebugCanvas* canvas, int curCommand) {
if (TRANSLATE != canvas->getDrawCommandAt(curCommand)->getType()) {
return false;
}
Translate* t = (Translate*) canvas->getDrawCommandAt(curCommand);
return 0 == t->x() && 0 == t->y();
}
// Just remove the translate
static void apply_5(SkDebugCanvas* canvas, int curCommand) {
canvas->deleteDrawCommandAt(curCommand); // translate
}
// Check for:
// SCALE
// where the scale is 1,1
static bool check_6(SkDebugCanvas* canvas, int curCommand) {
if (SCALE != canvas->getDrawCommandAt(curCommand)->getType()) {
return false;
}
Scale* s = (Scale*) canvas->getDrawCommandAt(curCommand);
return SK_Scalar1 == s->x() && SK_Scalar1 == s->y();
}
// Just remove the scale
static void apply_6(SkDebugCanvas* canvas, int curCommand) {
canvas->deleteDrawCommandAt(curCommand); // scale
}
// Check for:
// SAVE
// CLIP_RECT
// SAVE_LAYER
// SAVE
// CLIP_RECT
// SAVE_LAYER
// SAVE
// CLIP_RECT
// DRAWBITMAPRECTTORECT
// RESTORE
// RESTORE
// RESTORE
// RESTORE
// RESTORE
// where:
// all the clipRect's are BW, nested, intersections
// the drawBitmapRectToRect is a 1-1 copy from src to dest
// the last (smallest) clip rect is a subset of the drawBitmapRectToRect's dest rect
// all the saveLayer's paints can be rolled into the drawBitmapRectToRect's paint
// This pattern is used by Google spreadsheet when drawing the toolbar buttons
static bool check_7(SkDebugCanvas* canvas, int curCommand) {
if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+13 ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
SAVE_LAYER != canvas->getDrawCommandAt(curCommand+2)->getType() ||
SAVE != canvas->getDrawCommandAt(curCommand+3)->getType() ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+4)->getType() ||
SAVE_LAYER != canvas->getDrawCommandAt(curCommand+5)->getType() ||
SAVE != canvas->getDrawCommandAt(curCommand+6)->getType() ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+7)->getType() ||
DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+8)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+9)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+10)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+11)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+12)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+13)->getType()) {
return false;
}
ClipRect* clip0 = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
SaveLayer* saveLayer0 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+2);
ClipRect* clip1 = (ClipRect*) canvas->getDrawCommandAt(curCommand+4);
SaveLayer* saveLayer1 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+5);
ClipRect* clip2 = (ClipRect*) canvas->getDrawCommandAt(curCommand+7);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+8);
if (clip0->doAA() || clip1->doAA() || clip2->doAA()) {
return false;
}
if (SkRegion::kIntersect_Op != clip0->op() ||
SkRegion::kIntersect_Op != clip1->op() ||
SkRegion::kIntersect_Op != clip2->op()) {
return false;
}
if (!clip0->rect().contains(clip1->rect()) ||
!clip1->rect().contains(clip2->rect())) {
return false;
}
// The src->dest mapping needs to be 1-to-1
if (NULL == dbmr->srcRect()) {
if (dbmr->bitmap().width() != dbmr->dstRect().width() ||
dbmr->bitmap().height() != dbmr->dstRect().height()) {
return false;
}
} else {
if (dbmr->srcRect()->width() != dbmr->dstRect().width() ||
dbmr->srcRect()->height() != dbmr->dstRect().height()) {
return false;
}
}
if (!dbmr->dstRect().contains(clip2->rect())) {
return false;
}
const SkPaint* saveLayerPaint0 = saveLayer0->paint();
const SkPaint* saveLayerPaint1 = saveLayer1->paint();
if ((NULL != saveLayerPaint0 && !is_simple(*saveLayerPaint0)) ||
(NULL != saveLayerPaint1 && !is_simple(*saveLayerPaint1))) {
return false;
}
SkPaint* dbmrPaint = dbmr->paint();
if (NULL == dbmrPaint) {
return true;
}
if (NULL != saveLayerPaint0) {
SkColor layerColor0 = saveLayerPaint0->getColor() | 0xFF000000; // force opaque
if (dbmrPaint->getColor() != layerColor0) {
return false;
}
}
if (NULL != saveLayerPaint1) {
SkColor layerColor1 = saveLayerPaint1->getColor() | 0xFF000000; // force opaque
if (dbmrPaint->getColor() != layerColor1) {
return false;
}
}
return true;
}
// Reduce to a single drawBitmapRectToRect call by folding the clipRect's into
// the src and dst Rects and the saveLayer paints into the drawBitmapRectToRect's
// paint.
static void apply_7(SkDebugCanvas* canvas, int curCommand) {
SaveLayer* saveLayer0 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+2);
SaveLayer* saveLayer1 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+5);
ClipRect* clip2 = (ClipRect*) canvas->getDrawCommandAt(curCommand+7);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+8);
SkScalar newSrcLeft = dbmr->srcRect()->fLeft + clip2->rect().fLeft - dbmr->dstRect().fLeft;
SkScalar newSrcTop = dbmr->srcRect()->fTop + clip2->rect().fTop - dbmr->dstRect().fTop;
SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop,
clip2->rect().width(), clip2->rect().height());
dbmr->setSrcRect(newSrc);
dbmr->setDstRect(clip2->rect());
SkColor color = 0xFF000000;
int a0, a1;
const SkPaint* saveLayerPaint0 = saveLayer0->paint();
if (NULL != saveLayerPaint0) {
color = saveLayerPaint0->getColor();
a0 = SkColorGetA(color);
} else {
a0 = 0xFF;
}
const SkPaint* saveLayerPaint1 = saveLayer1->paint();
if (NULL != saveLayerPaint1) {
color = saveLayerPaint1->getColor();
a1 = SkColorGetA(color);
} else {
a1 = 0xFF;
}
int newA = SkMulDiv255Round(a0, a1);
SkASSERT(newA <= 0xFF);
SkPaint* dbmrPaint = dbmr->paint();
if (NULL != dbmrPaint) {
SkColor newColor = SkColorSetA(dbmrPaint->getColor(), newA);
dbmrPaint->setColor(newColor);
} else {
SkColor newColor = SkColorSetA(color, newA);
SkPaint newPaint;
newPaint.setColor(newColor);
dbmr->setPaint(newPaint);
}
// remove everything except the drawbitmaprect
canvas->deleteDrawCommandAt(curCommand+13); // restore
canvas->deleteDrawCommandAt(curCommand+12); // restore
canvas->deleteDrawCommandAt(curCommand+11); // restore
canvas->deleteDrawCommandAt(curCommand+10); // restore
canvas->deleteDrawCommandAt(curCommand+9); // restore
canvas->deleteDrawCommandAt(curCommand+7); // clipRect
canvas->deleteDrawCommandAt(curCommand+6); // save
canvas->deleteDrawCommandAt(curCommand+5); // saveLayer
canvas->deleteDrawCommandAt(curCommand+4); // clipRect
canvas->deleteDrawCommandAt(curCommand+3); // save
canvas->deleteDrawCommandAt(curCommand+2); // saveLayer
canvas->deleteDrawCommandAt(curCommand+1); // clipRect
canvas->deleteDrawCommandAt(curCommand); // save
}
// Check for:
// SAVE
// CLIP_RECT
// DRAWBITMAPRECTTORECT
// RESTORE
// where:
// the drawBitmapRectToRect is a 1-1 copy from src to dest
// the clip rect is BW and a subset of the drawBitmapRectToRect's dest rect
static bool check_8(SkDebugCanvas* canvas, int curCommand) {
if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+4 ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
return false;
}
ClipRect* clip = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2);
if (clip->doAA() || SkRegion::kIntersect_Op != clip->op()) {
return false;
}
// The src->dest mapping needs to be 1-to-1
if (NULL == dbmr->srcRect()) {
if (dbmr->bitmap().width() != dbmr->dstRect().width() ||
dbmr->bitmap().height() != dbmr->dstRect().height()) {
return false;
}
} else {
if (dbmr->srcRect()->width() != dbmr->dstRect().width() ||
dbmr->srcRect()->height() != dbmr->dstRect().height()) {
return false;
}
}
if (!dbmr->dstRect().contains(clip->rect())) {
return false;
}
return true;
}
// Fold the clipRect into the drawBitmapRectToRect's src and dest rects
static void apply_8(SkDebugCanvas* canvas, int curCommand) {
ClipRect* clip = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2);
SkScalar newSrcLeft, newSrcTop;
if (NULL != dbmr->srcRect()) {
newSrcLeft = dbmr->srcRect()->fLeft + clip->rect().fLeft - dbmr->dstRect().fLeft;
newSrcTop = dbmr->srcRect()->fTop + clip->rect().fTop - dbmr->dstRect().fTop;
} else {
newSrcLeft = clip->rect().fLeft - dbmr->dstRect().fLeft;
newSrcTop = clip->rect().fTop - dbmr->dstRect().fTop;
}
SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop,
clip->rect().width(), clip->rect().height());
dbmr->setSrcRect(newSrc);
dbmr->setDstRect(clip->rect());
// remove everything except the drawbitmaprect
canvas->deleteDrawCommandAt(curCommand+3);
canvas->deleteDrawCommandAt(curCommand+1);
canvas->deleteDrawCommandAt(curCommand);
}
// Check for:
// SAVE
// CLIP_RECT
// DRAWBITMAPRECTTORECT
// RESTORE
// where:
// clipRect is BW and encloses the DBMR2R's dest rect
static bool check_9(SkDebugCanvas* canvas, int curCommand) {
if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
canvas->getSize() <= curCommand+4 ||
CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
return false;
}
ClipRect* clip = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2);
if (clip->doAA() || SkRegion::kIntersect_Op != clip->op()) {
return false;
}
if (!clip->rect().contains(dbmr->dstRect())) {
return false;
}
return true;
}
// remove everything except the drawbitmaprect
static void apply_9(SkDebugCanvas* canvas, int curCommand) {
canvas->deleteDrawCommandAt(curCommand+3); // restore
// drawBitmapRectToRect
canvas->deleteDrawCommandAt(curCommand+1); // clipRect
canvas->deleteDrawCommandAt(curCommand); // save
}
typedef bool (*PFCheck)(SkDebugCanvas* canvas, int curCommand);
typedef void (*PFApply)(SkDebugCanvas* canvas, int curCommand);
struct OptTableEntry {
PFCheck fCheck;
PFApply fApply;
int fNumTimesApplied;
} gOptTable[] = {
{ check_0, apply_0, 0 },
{ check_1, apply_1, 0 },
{ check_2, apply_2, 0 },
{ check_3, apply_3, 0 },
{ check_4, apply_4, 0 },
{ check_5, apply_5, 0 },
{ check_6, apply_6, 0 },
{ check_7, apply_7, 0 },
{ check_8, apply_8, 0 },
{ check_9, apply_9, 0 },
};
static int filter_picture(const SkString& inFile, const SkString& outFile) {
SkAutoTDelete<SkPicture> inPicture;
SkFILEStream inStream(inFile.c_str());
if (inStream.isValid()) {
inPicture.reset(SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeMemory)));
}
if (NULL == inPicture.get()) {
SkDebugf("Could not read file %s\n", inFile.c_str());
return -1;
}
int localCount[SK_ARRAY_COUNT(gOptTable)];
memset(localCount, 0, sizeof(localCount));
SkDebugCanvas debugCanvas(inPicture->width(), inPicture->height());
debugCanvas.setBounds(inPicture->width(), inPicture->height());
inPicture->draw(&debugCanvas);
// delete the initial save and restore since replaying the commands will
// re-add them
if (debugCanvas.getSize() > 1) {
debugCanvas.deleteDrawCommandAt(0);
debugCanvas.deleteDrawCommandAt(debugCanvas.getSize()-1);
}
bool changed = true;
int numBefore = debugCanvas.getSize();
while (changed) {
changed = false;
for (int i = 0; i < debugCanvas.getSize(); ++i) {
for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
if ((*gOptTable[opt].fCheck)(&debugCanvas, i)) {
(*gOptTable[opt].fApply)(&debugCanvas, i);
++gOptTable[opt].fNumTimesApplied;
++localCount[opt];
if (debugCanvas.getSize() == i) {
// the optimization removed all the remaining operations
break;
}
opt = 0; // try all the opts all over again
changed = true;
}
}
}
}
int numAfter = debugCanvas.getSize();
if (!outFile.isEmpty()) {
SkPicture outPicture;
SkCanvas* canvas = outPicture.beginRecording(inPicture->width(), inPicture->height());
debugCanvas.draw(canvas);
outPicture.endRecording();
SkFILEWStream outStream(outFile.c_str());
outPicture.serialize(&outStream);
}
bool someOptFired = false;
for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
if (0 != localCount[opt]) {
SkDebugf("%d: %d ", opt, localCount[opt]);
someOptFired = true;
}
}
if (!someOptFired) {
SkDebugf("No opts fired\n");
} else {
SkDebugf("\t before: %d after: %d delta: %d\n",
numBefore, numAfter, numBefore-numAfter);
}
return 0;
}
// This function is not marked as 'static' so it can be referenced externally
// in the iOS build.
int tool_main(int argc, char** argv); // suppress a warning on mac
int tool_main(int argc, char** argv) {
#if SK_ENABLE_INST_COUNT
gPrintInstCount = true;
#endif
SkGraphics::Init();
if (argc < 3) {
usage();
return -1;
}
SkString inFile, outFile, inDir, outDir;
char* const* stop = argv + argc;
for (++argv; argv < stop; ++argv) {
if (strcmp(*argv, "-i") == 0) {
argv++;
if (argv < stop && **argv) {
inFile.set(*argv);
} else {
SkDebugf("missing arg for -i\n");
usage();
return -1;
}
} else if (strcmp(*argv, "--input-dir") == 0) {
argv++;
if (argv < stop && **argv) {
inDir.set(*argv);
} else {
SkDebugf("missing arg for --input-dir\n");
usage();
return -1;
}
} else if (strcmp(*argv, "--output-dir") == 0) {
argv++;
if (argv < stop && **argv) {
outDir.set(*argv);
} else {
SkDebugf("missing arg for --output-dir\n");
usage();
return -1;
}
} else if (strcmp(*argv, "-o") == 0) {
argv++;
if (argv < stop && **argv) {
outFile.set(*argv);
} else {
SkDebugf("missing arg for -o\n");
usage();
return -1;
}
} else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
usage();
return 0;
} else {
SkDebugf("unknown arg %s\n", *argv);
usage();
return -1;
}
}
SkOSFile::Iter iter(inDir.c_str(), "skp");
SkString inputFilename, outputFilename;
if (iter.next(&inputFilename)) {
do {
sk_tools::make_filepath(&inFile, inDir, inputFilename);
if (!outDir.isEmpty()) {
sk_tools::make_filepath(&outFile, outDir, inputFilename);
}
SkDebugf("Executing %s\n", inputFilename.c_str());
filter_picture(inFile, outFile);
} while(iter.next(&inputFilename));
} else if (!inFile.isEmpty()) {
filter_picture(inFile, outFile);
} else {
usage();
return -1;
}
for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
SkDebugf("opt %d: %d\n", opt, gOptTable[opt].fNumTimesApplied);
}
SkGraphics::Term();
return 0;
}
#if !defined SK_BUILD_FOR_IOS
int main(int argc, char * const argv[]) {
return tool_main(argc, (char**) argv);
}
#endif
|