blob: 24887c55546d229d0a0910171ea879ed063e8f0a [file] [log] [blame]
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +00008#include "SkDebugCanvas.h"
robertphillips@google.com801cee12012-10-19 19:06:11 +00009#include "SkDevice.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000010#include "SkGraphics.h"
robertphillips@google.com4e4d75b2012-11-12 18:03:19 +000011#include "SkImageDecoder.h"
robertphillips@google.com801cee12012-10-19 19:06:11 +000012#include "SkImageEncoder.h"
djsollen@google.coma09e8832012-11-13 18:50:33 +000013#include "SkOSFile.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000014#include "SkPicture.h"
robertphillips@google.com801cee12012-10-19 19:06:11 +000015#include "SkPicturePlayback.h"
16#include "SkPictureRecord.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000017#include "SkStream.h"
djsollen@google.coma09e8832012-11-13 18:50:33 +000018#include "picture_utils.h"
robertphillips@google.comd3d377f2012-12-07 20:56:13 +000019#include "path_utils.h"
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000020
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000021static void usage() {
robertphillips@google.comd3d377f2012-12-07 20:56:13 +000022 SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n");
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +000023 SkDebugf(" [-h|--help]\n\n");
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000024 SkDebugf(" -i inFile : file to file.\n");
25 SkDebugf(" -o outFile : result of filtering.\n");
djsollen@google.coma09e8832012-11-13 18:50:33 +000026 SkDebugf(" --input-dir : process all files in dir with .skp extension.\n");
27 SkDebugf(" --output-dir : results of filtering the input dir.\n");
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +000028 SkDebugf(" -h|--help : Show this help message.\n");
29}
30
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +000031// Is the supplied paint simply a color?
32static bool is_simple(const SkPaint& p) {
33 return NULL == p.getPathEffect() &&
34 NULL == p.getShader() &&
35 NULL == p.getXfermode() &&
36 NULL == p.getMaskFilter() &&
37 NULL == p.getColorFilter() &&
38 NULL == p.getRasterizer() &&
39 NULL == p.getLooper() &&
40 NULL == p.getImageFilter();
41}
robertphillips@google.comd3d377f2012-12-07 20:56:13 +000042
robertphillips@google.com50c84da2013-04-01 18:18:49 +000043
robertphillips@google.com73743552013-02-05 20:51:49 +000044// Check for:
45// SAVE_LAYER
46// DRAW_BITMAP_RECT_TO_RECT
47// RESTORE
48// where the saveLayer's color can be moved into the drawBitmapRect
robertphillips@google.com50c84da2013-04-01 18:18:49 +000049static bool check_0(SkDebugCanvas* canvas, int curCommand) {
50 if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() ||
51 canvas->getSize() <= curCommand+2 ||
52 DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
53 RESTORE != canvas->getDrawCommandAt(curCommand+2)->getType()) {
robertphillips@google.com73743552013-02-05 20:51:49 +000054 return false;
robertphillips@google.com50c84da2013-04-01 18:18:49 +000055 }
robertphillips@google.com73743552013-02-05 20:51:49 +000056
robertphillips@google.com50c84da2013-04-01 18:18:49 +000057 SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
58 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+1);
robertphillips@google.com73743552013-02-05 20:51:49 +000059
60 const SkPaint* saveLayerPaint = saveLayer->paint();
61 SkPaint* dbmrPaint = dbmr->paint();
62
skia.committer@gmail.com3d18d062013-02-14 07:01:34 +000063 // For this optimization we only fold the saveLayer and drawBitmapRect
robertphillips@google.com1780a3c2013-02-13 13:27:44 +000064 // together if the saveLayer's draw is simple (i.e., no fancy effects) and
65 // and the only difference in the colors is that the saveLayer's can have
66 // an alpha while the drawBitmapRect's is opaque.
67 // TODO: it should be possible to fold them together even if they both
68 // have different non-255 alphas but this is low priority since we have
69 // never seen that case
70 // If either operation lacks a paint then the collapse is trivial
71 SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque
72
robertphillips@google.com73743552013-02-05 20:51:49 +000073 return NULL == saveLayerPaint ||
74 NULL == dbmrPaint ||
robertphillips@google.com1780a3c2013-02-13 13:27:44 +000075 (is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor);
robertphillips@google.com73743552013-02-05 20:51:49 +000076}
77
78// Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer
79// and restore
robertphillips@google.com50c84da2013-04-01 18:18:49 +000080static void apply_0(SkDebugCanvas* canvas, int curCommand) {
81 SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
robertphillips@google.com73743552013-02-05 20:51:49 +000082 const SkPaint* saveLayerPaint = saveLayer->paint();
robertphillips@google.com73743552013-02-05 20:51:49 +000083
robertphillips@google.com50c84da2013-04-01 18:18:49 +000084 // if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed
85 if (NULL != saveLayerPaint) {
86 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+1);
87 SkPaint* dbmrPaint = dbmr->paint();
88
89 if (NULL == dbmrPaint) {
90 // if the DBMR doesn't have a paint just use the saveLayer's
91 dbmr->setPaint(*saveLayerPaint);
92 } else if (NULL != saveLayerPaint) {
93 SkColor newColor = SkColorSetA(dbmrPaint->getColor(),
94 SkColorGetA(saveLayerPaint->getColor()));
95 dbmrPaint->setColor(newColor);
96 }
robertphillips@google.com73743552013-02-05 20:51:49 +000097 }
robertphillips@google.com50c84da2013-04-01 18:18:49 +000098
99 canvas->deleteDrawCommandAt(curCommand+2); // restore
100 canvas->deleteDrawCommandAt(curCommand); // saveLayer
robertphillips@google.com73743552013-02-05 20:51:49 +0000101}
102
103// Check for:
104// SAVE_LAYER
105// SAVE
106// CLIP_RECT
107// DRAW_BITMAP_RECT_TO_RECT
108// RESTORE
109// RESTORE
110// where the saveLayer's color can be moved into the drawBitmapRect
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000111static bool check_1(SkDebugCanvas* canvas, int curCommand) {
112 if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() ||
113 canvas->getSize() <= curCommand+5 ||
114 SAVE != canvas->getDrawCommandAt(curCommand+1)->getType() ||
115 CLIP_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
116 DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+3)->getType() ||
117 RESTORE != canvas->getDrawCommandAt(curCommand+4)->getType() ||
118 RESTORE != canvas->getDrawCommandAt(curCommand+5)->getType()) {
robertphillips@google.com73743552013-02-05 20:51:49 +0000119 return false;
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000120 }
robertphillips@google.com73743552013-02-05 20:51:49 +0000121
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000122 SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
123 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+3);
robertphillips@google.com73743552013-02-05 20:51:49 +0000124
125 const SkPaint* saveLayerPaint = saveLayer->paint();
126 SkPaint* dbmrPaint = dbmr->paint();
127
skia.committer@gmail.com3d18d062013-02-14 07:01:34 +0000128 // For this optimization we only fold the saveLayer and drawBitmapRect
robertphillips@google.com1780a3c2013-02-13 13:27:44 +0000129 // together if the saveLayer's draw is simple (i.e., no fancy effects) and
130 // and the only difference in the colors is that the saveLayer's can have
131 // an alpha while the drawBitmapRect's is opaque.
132 // TODO: it should be possible to fold them together even if they both
133 // have different non-255 alphas but this is low priority since we have
134 // never seen that case
135 // If either operation lacks a paint then the collapse is trivial
136 SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque
137
robertphillips@google.com73743552013-02-05 20:51:49 +0000138 return NULL == saveLayerPaint ||
139 NULL == dbmrPaint ||
robertphillips@google.com1780a3c2013-02-13 13:27:44 +0000140 (is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor);
robertphillips@google.com73743552013-02-05 20:51:49 +0000141}
142
143// Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer
144// and restore
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000145static void apply_1(SkDebugCanvas* canvas, int curCommand) {
146 SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand);
robertphillips@google.com73743552013-02-05 20:51:49 +0000147 const SkPaint* saveLayerPaint = saveLayer->paint();
robertphillips@google.com73743552013-02-05 20:51:49 +0000148
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000149 // if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed
150 if (NULL != saveLayerPaint) {
151 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+3);
152 SkPaint* dbmrPaint = dbmr->paint();
153
154 if (NULL == dbmrPaint) {
155 dbmr->setPaint(*saveLayerPaint);
156 } else {
157 SkColor newColor = SkColorSetA(dbmrPaint->getColor(),
158 SkColorGetA(saveLayerPaint->getColor()));
159 dbmrPaint->setColor(newColor);
160 }
robertphillips@google.com73743552013-02-05 20:51:49 +0000161 }
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000162
163 canvas->deleteDrawCommandAt(curCommand+5); // restore
164 canvas->deleteDrawCommandAt(curCommand); // saveLayer
robertphillips@google.com73743552013-02-05 20:51:49 +0000165}
166
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000167// Check for:
168// SAVE
169// CLIP_RECT
170// DRAW_RECT
171// RESTORE
172// where the rect is entirely within the clip and the clip is an intersect
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000173static bool check_2(SkDebugCanvas* canvas, int curCommand) {
174 if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
175 canvas->getSize() <= curCommand+4 ||
176 CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
177 DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
178 RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000179 return false;
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000180 }
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000181
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000182 ClipRect* cr = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
183 DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2);
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000184
185 if (SkRegion::kIntersect_Op != cr->op()) {
186 return false;
187 }
188
189 return cr->rect().contains(dr->rect());
190}
191
192// Remove everything but the drawRect
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000193static void apply_2(SkDebugCanvas* canvas, int curCommand) {
194 canvas->deleteDrawCommandAt(curCommand+3); // restore
195 // drawRect
196 canvas->deleteDrawCommandAt(curCommand+1); // clipRect
197 canvas->deleteDrawCommandAt(curCommand); // save
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000198}
199
200// Check for:
201// SAVE
202// CLIP_RRECT
203// DRAW_RECT
204// RESTORE
205// where the rect entirely encloses the clip
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000206static bool check_3(SkDebugCanvas* canvas, int curCommand) {
207 if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
208 canvas->getSize() <= curCommand+4 ||
209 CLIP_RRECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
210 DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
211 RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000212 return false;
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000213 }
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000214
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000215 ClipRRect* crr = (ClipRRect*) canvas->getDrawCommandAt(curCommand+1);
216 DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2);
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000217
218 if (SkRegion::kIntersect_Op != crr->op()) {
219 return false;
220 }
221
222 return dr->rect().contains(crr->rrect().rect());
223}
224
225// Replace everything with a drawRRect with the paint from the drawRect
226// and the AA settings from the clipRRect
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000227static void apply_3(SkDebugCanvas* canvas, int curCommand) {
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000228
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000229 canvas->deleteDrawCommandAt(curCommand+3); // restore
230
231 ClipRRect* crr = (ClipRRect*) canvas->getDrawCommandAt(curCommand+1);
232 DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2);
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000233
234 // TODO: could skip paint re-creation if the AA settings already match
robertphillips@google.com91217d02013-03-17 18:33:46 +0000235 SkPaint newPaint = dr->paint();
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000236 newPaint.setAntiAlias(crr->doAA());
237 DrawRRect* drr = new DrawRRect(crr->rrect(), newPaint);
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000238 canvas->setDrawCommandAt(curCommand+2, drr);
239
240 canvas->deleteDrawCommandAt(curCommand+1); // clipRRect
241 canvas->deleteDrawCommandAt(curCommand); // save
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000242}
243
244// Check for:
245// SAVE
246// CLIP_RECT
247// DRAW_BITMAP_RECT_TO_RECT
248// RESTORE
249// where the rect and drawBitmapRect dst exactly match
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000250static bool check_4(SkDebugCanvas* canvas, int curCommand) {
251 if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
252 canvas->getSize() <= curCommand+4 ||
253 CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
254 DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
255 RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000256 return false;
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000257 }
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000258
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000259 ClipRect* cr = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
260 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2);
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000261
262 if (SkRegion::kIntersect_Op != cr->op()) {
263 return false;
264 }
265
266 return dbmr->dstRect() == cr->rect();
267}
268
269// Remove everything but the drawBitmapRect
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000270static void apply_4(SkDebugCanvas* canvas, int curCommand) {
271 canvas->deleteDrawCommandAt(curCommand+3); // restore
272 // drawBitmapRectToRect
273 canvas->deleteDrawCommandAt(curCommand+1); // clipRect
274 canvas->deleteDrawCommandAt(curCommand); // save
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000275}
276
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000277// Check for:
278// TRANSLATE
279// where the translate is zero
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000280static bool check_5(SkDebugCanvas* canvas, int curCommand) {
281 if (TRANSLATE != canvas->getDrawCommandAt(curCommand)->getType()) {
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000282 return false;
283 }
284
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000285 Translate* t = (Translate*) canvas->getDrawCommandAt(curCommand);
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000286
287 return 0 == t->x() && 0 == t->y();
288}
289
290// Just remove the translate
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000291static void apply_5(SkDebugCanvas* canvas, int curCommand) {
292 canvas->deleteDrawCommandAt(curCommand); // translate
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000293}
294
295// Check for:
296// SCALE
297// where the scale is 1,1
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000298static bool check_6(SkDebugCanvas* canvas, int curCommand) {
299 if (SCALE != canvas->getDrawCommandAt(curCommand)->getType()) {
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000300 return false;
301 }
302
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000303 Scale* s = (Scale*) canvas->getDrawCommandAt(curCommand);
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000304
305 return SK_Scalar1 == s->x() && SK_Scalar1 == s->y();
306}
307
308// Just remove the scale
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000309static void apply_6(SkDebugCanvas* canvas, int curCommand) {
310 canvas->deleteDrawCommandAt(curCommand); // scale
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000311}
312
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000313// Check for:
314// SAVE
315// CLIP_RECT
316// SAVE_LAYER
317// SAVE
318// CLIP_RECT
319// SAVE_LAYER
320// SAVE
321// CLIP_RECT
322// DRAWBITMAPRECTTORECT
323// RESTORE
324// RESTORE
325// RESTORE
326// RESTORE
327// RESTORE
328// where:
329// all the clipRect's are BW, nested, intersections
330// the drawBitmapRectToRect is a 1-1 copy from src to dest
331// the last (smallest) clip rect is a subset of the drawBitmapRectToRect's dest rect
332// all the saveLayer's paints can be rolled into the drawBitmapRectToRect's paint
333// This pattern is used by Google spreadsheet when drawing the toolbar buttons
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000334static bool check_7(SkDebugCanvas* canvas, int curCommand) {
335 if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
336 canvas->getSize() <= curCommand+13 ||
337 CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
338 SAVE_LAYER != canvas->getDrawCommandAt(curCommand+2)->getType() ||
339 SAVE != canvas->getDrawCommandAt(curCommand+3)->getType() ||
340 CLIP_RECT != canvas->getDrawCommandAt(curCommand+4)->getType() ||
341 SAVE_LAYER != canvas->getDrawCommandAt(curCommand+5)->getType() ||
342 SAVE != canvas->getDrawCommandAt(curCommand+6)->getType() ||
343 CLIP_RECT != canvas->getDrawCommandAt(curCommand+7)->getType() ||
344 DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+8)->getType() ||
345 RESTORE != canvas->getDrawCommandAt(curCommand+9)->getType() ||
346 RESTORE != canvas->getDrawCommandAt(curCommand+10)->getType() ||
347 RESTORE != canvas->getDrawCommandAt(curCommand+11)->getType() ||
348 RESTORE != canvas->getDrawCommandAt(curCommand+12)->getType() ||
349 RESTORE != canvas->getDrawCommandAt(curCommand+13)->getType()) {
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000350 return false;
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000351 }
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000352
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000353 ClipRect* clip0 = (ClipRect*) canvas->getDrawCommandAt(curCommand+1);
354 SaveLayer* saveLayer0 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+2);
355 ClipRect* clip1 = (ClipRect*) canvas->getDrawCommandAt(curCommand+4);
356 SaveLayer* saveLayer1 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+5);
357 ClipRect* clip2 = (ClipRect*) canvas->getDrawCommandAt(curCommand+7);
358 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+8);
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000359
360 if (clip0->doAA() || clip1->doAA() || clip2->doAA()) {
361 return false;
362 }
363
364 if (SkRegion::kIntersect_Op != clip0->op() ||
365 SkRegion::kIntersect_Op != clip1->op() ||
366 SkRegion::kIntersect_Op != clip2->op()) {
367 return false;
368 }
369
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000370 if (!clip0->rect().contains(clip1->rect()) ||
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000371 !clip1->rect().contains(clip2->rect())) {
372 return false;
373 }
374
375 // The src->dest mapping needs to be 1-to-1
376 if (NULL == dbmr->srcRect()) {
377 if (dbmr->bitmap().width() != dbmr->dstRect().width() ||
378 dbmr->bitmap().height() != dbmr->dstRect().height()) {
379 return false;
380 }
381 } else {
382 if (dbmr->srcRect()->width() != dbmr->dstRect().width() ||
383 dbmr->srcRect()->height() != dbmr->dstRect().height()) {
384 return false;
385 }
386 }
387
388 if (!dbmr->dstRect().contains(clip2->rect())) {
389 return false;
390 }
391
392 const SkPaint* saveLayerPaint0 = saveLayer0->paint();
393 const SkPaint* saveLayerPaint1 = saveLayer1->paint();
394
395 if ((NULL != saveLayerPaint0 && !is_simple(*saveLayerPaint0)) ||
396 (NULL != saveLayerPaint1 && !is_simple(*saveLayerPaint1))) {
397 return false;
398 }
399
400 SkPaint* dbmrPaint = dbmr->paint();
401
402 if (NULL == dbmrPaint) {
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000403 return true;
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000404 }
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000405
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000406 if (NULL != saveLayerPaint0) {
407 SkColor layerColor0 = saveLayerPaint0->getColor() | 0xFF000000; // force opaque
408 if (dbmrPaint->getColor() != layerColor0) {
409 return false;
410 }
411 }
412
413 if (NULL != saveLayerPaint1) {
414 SkColor layerColor1 = saveLayerPaint1->getColor() | 0xFF000000; // force opaque
415 if (dbmrPaint->getColor() != layerColor1) {
416 return false;
417 }
418 }
419
420 return true;
421}
422
423// Reduce to a single drawBitmapRectToRect call by folding the clipRect's into
424// the src and dst Rects and the saveLayer paints into the drawBitmapRectToRect's
425// paint.
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000426static void apply_7(SkDebugCanvas* canvas, int curCommand) {
427 SaveLayer* saveLayer0 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+2);
428 SaveLayer* saveLayer1 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+5);
429 ClipRect* clip2 = (ClipRect*) canvas->getDrawCommandAt(curCommand+7);
430 DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+8);
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000431
432 SkScalar newSrcLeft = dbmr->srcRect()->fLeft + clip2->rect().fLeft - dbmr->dstRect().fLeft;
433 SkScalar newSrcTop = dbmr->srcRect()->fTop + clip2->rect().fTop - dbmr->dstRect().fTop;
434
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000435 SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop,
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000436 clip2->rect().width(), clip2->rect().height());
437
438 dbmr->setSrcRect(newSrc);
439 dbmr->setDstRect(clip2->rect());
440
441 SkColor color = 0xFF000000;
442 int a0, a1;
443
444 const SkPaint* saveLayerPaint0 = saveLayer0->paint();
445 if (NULL != saveLayerPaint0) {
446 color = saveLayerPaint0->getColor();
447 a0 = SkColorGetA(color);
448 } else {
449 a0 = 0xFF;
450 }
451
452 const SkPaint* saveLayerPaint1 = saveLayer1->paint();
453 if (NULL != saveLayerPaint1) {
454 color = saveLayerPaint1->getColor();
455 a1 = SkColorGetA(color);
456 } else {
457 a1 = 0xFF;
458 }
459
460 int newA = (a0 * a1) / 255;
461 SkASSERT(newA <= 0xFF);
462
463 SkPaint* dbmrPaint = dbmr->paint();
464
465 if (NULL != dbmrPaint) {
466 SkColor newColor = SkColorSetA(dbmrPaint->getColor(), newA);
467 dbmrPaint->setColor(newColor);
468 } else {
469 SkColor newColor = SkColorSetA(color, newA);
470
471 SkPaint newPaint;
472 newPaint.setColor(newColor);
473 dbmr->setPaint(newPaint);
474 }
475
476 // remove everything except the drawbitmaprect
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000477 canvas->deleteDrawCommandAt(curCommand+13); // restore
478 canvas->deleteDrawCommandAt(curCommand+12); // restore
479 canvas->deleteDrawCommandAt(curCommand+11); // restore
480 canvas->deleteDrawCommandAt(curCommand+10); // restore
481 canvas->deleteDrawCommandAt(curCommand+9); // restore
482 canvas->deleteDrawCommandAt(curCommand+7); // clipRect
483 canvas->deleteDrawCommandAt(curCommand+6); // save
484 canvas->deleteDrawCommandAt(curCommand+5); // saveLayer
485 canvas->deleteDrawCommandAt(curCommand+4); // clipRect
486 canvas->deleteDrawCommandAt(curCommand+3); // save
487 canvas->deleteDrawCommandAt(curCommand+2); // saveLayer
488 canvas->deleteDrawCommandAt(curCommand+1); // clipRect
489 canvas->deleteDrawCommandAt(curCommand); // save
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000490}
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000491
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000492typedef bool (*PFCheck)(SkDebugCanvas* canvas, int curCommand);
493typedef void (*PFApply)(SkDebugCanvas* canvas, int curCommand);
robertphillips@google.com73743552013-02-05 20:51:49 +0000494
495struct OptTableEntry {
496 PFCheck fCheck;
497 PFApply fApply;
498 int fNumTimesApplied;
499} gOptTable[] = {
500 { check_0, apply_0, 0 },
501 { check_1, apply_1, 0 },
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000502 { check_2, apply_2, 0 },
503 { check_3, apply_3, 0 },
504 { check_4, apply_4, 0 },
robertphillips@google.com9105ad02013-03-17 18:46:16 +0000505 { check_5, apply_5, 0 },
506 { check_6, apply_6, 0 },
robertphillips@google.comc3410b82013-03-28 12:25:25 +0000507 { check_7, apply_7, 0 },
robertphillips@google.com73743552013-02-05 20:51:49 +0000508};
509
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000510
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000511static int filter_picture(const SkString& inFile, const SkString& outFile) {
djsollen@google.coma09e8832012-11-13 18:50:33 +0000512 SkPicture* inPicture = NULL;
513
514 SkFILEStream inStream(inFile.c_str());
515 if (inStream.isValid()) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000516 inPicture = SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeMemory));
djsollen@google.coma09e8832012-11-13 18:50:33 +0000517 }
518
519 if (NULL == inPicture) {
520 SkDebugf("Could not read file %s\n", inFile.c_str());
521 return -1;
522 }
523
robertphillips@google.com73743552013-02-05 20:51:49 +0000524 int localCount[SK_ARRAY_COUNT(gOptTable)];
525
526 memset(localCount, 0, sizeof(localCount));
527
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000528 SkDebugCanvas debugCanvas(inPicture->width(), inPicture->height());
529 debugCanvas.setBounds(inPicture->width(), inPicture->height());
530 inPicture->draw(&debugCanvas);
djsollen@google.coma09e8832012-11-13 18:50:33 +0000531
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000532 // delete the initial save and restore since replaying the commands will
robertphillips@google.com73743552013-02-05 20:51:49 +0000533 // re-add them
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000534 if (debugCanvas.getSize() > 1) {
535 debugCanvas.deleteDrawCommandAt(0);
536 debugCanvas.deleteDrawCommandAt(debugCanvas.getSize()-1);
robertphillips@google.com73743552013-02-05 20:51:49 +0000537 }
538
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000539 for (int i = 0; i < debugCanvas.getSize(); ++i) {
robertphillips@google.com73743552013-02-05 20:51:49 +0000540 for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000541 if ((*gOptTable[opt].fCheck)(&debugCanvas, i)) {
542 (*gOptTable[opt].fApply)(&debugCanvas, i);
robertphillips@google.com73743552013-02-05 20:51:49 +0000543 ++gOptTable[opt].fNumTimesApplied;
544 ++localCount[opt];
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000545 }
546 }
547 }
djsollen@google.coma09e8832012-11-13 18:50:33 +0000548
549 if (!outFile.isEmpty()) {
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000550 SkPicture outPicture;
551
552 SkCanvas* canvas = outPicture.beginRecording(inPicture->width(), inPicture->height());
553 debugCanvas.draw(canvas);
554 outPicture.endRecording();
555
djsollen@google.coma09e8832012-11-13 18:50:33 +0000556 SkFILEWStream outStream(outFile.c_str());
557
558 outPicture.serialize(&outStream);
559 }
560
robertphillips@google.com73743552013-02-05 20:51:49 +0000561 bool someOptFired = false;
562 for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
563 if (0 != localCount[opt]) {
564 SkDebugf("%d: %d ", opt, localCount[opt]);
565 someOptFired = true;
566 }
567 }
568
569 if (!someOptFired) {
570 SkDebugf("No opts fired\n");
571 } else {
572 SkDebugf("\n");
573 }
574
djsollen@google.coma09e8832012-11-13 18:50:33 +0000575 return 0;
576}
577
tfarina@chromium.orga5b7cc02012-10-08 14:41:10 +0000578// This function is not marked as 'static' so it can be referenced externally
579// in the iOS build.
humper@google.com05af1af2013-01-07 16:47:43 +0000580int tool_main(int argc, char** argv); // suppress a warning on mac
581
caryclark@google.com9598f422012-10-09 12:32:37 +0000582int tool_main(int argc, char** argv) {
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000583 SkGraphics::Init();
584
robertphillips@google.com801cee12012-10-19 19:06:11 +0000585 if (argc < 3) {
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000586 usage();
587 return -1;
588 }
589
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000590 SkString inFile, outFile, inDir, outDir;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000591
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000592 char* const* stop = argv + argc;
593 for (++argv; argv < stop; ++argv) {
594 if (strcmp(*argv, "-i") == 0) {
595 argv++;
596 if (argv < stop && **argv) {
597 inFile.set(*argv);
598 } else {
robertphillips@google.com801cee12012-10-19 19:06:11 +0000599 SkDebugf("missing arg for -i\n");
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000600 usage();
601 return -1;
602 }
djsollen@google.coma09e8832012-11-13 18:50:33 +0000603 } else if (strcmp(*argv, "--input-dir") == 0) {
604 argv++;
605 if (argv < stop && **argv) {
606 inDir.set(*argv);
607 } else {
608 SkDebugf("missing arg for --input-dir\n");
609 usage();
610 return -1;
611 }
612 } else if (strcmp(*argv, "--output-dir") == 0) {
613 argv++;
614 if (argv < stop && **argv) {
615 outDir.set(*argv);
616 } else {
617 SkDebugf("missing arg for --output-dir\n");
618 usage();
619 return -1;
620 }
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000621 } else if (strcmp(*argv, "-o") == 0) {
622 argv++;
623 if (argv < stop && **argv) {
624 outFile.set(*argv);
625 } else {
robertphillips@google.com801cee12012-10-19 19:06:11 +0000626 SkDebugf("missing arg for -o\n");
627 usage();
628 return -1;
629 }
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000630 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
631 usage();
632 return 0;
633 } else {
634 SkDebugf("unknown arg %s\n", *argv);
635 usage();
636 return -1;
637 }
638 }
639
djsollen@google.coma09e8832012-11-13 18:50:33 +0000640 SkOSFile::Iter iter(inDir.c_str(), "skp");
humper@google.com05af1af2013-01-07 16:47:43 +0000641
djsollen@google.coma09e8832012-11-13 18:50:33 +0000642 SkString inputFilename, outputFilename;
643 if (iter.next(&inputFilename)) {
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000644
djsollen@google.coma09e8832012-11-13 18:50:33 +0000645 do {
646 sk_tools::make_filepath(&inFile, inDir, inputFilename);
647 if (!outDir.isEmpty()) {
648 sk_tools::make_filepath(&outFile, outDir, inputFilename);
649 }
650 SkDebugf("Executing %s\n", inputFilename.c_str());
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000651 filter_picture(inFile, outFile);
djsollen@google.coma09e8832012-11-13 18:50:33 +0000652 } while(iter.next(&inputFilename));
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000653
djsollen@google.coma09e8832012-11-13 18:50:33 +0000654 } else if (!inFile.isEmpty()) {
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000655 filter_picture(inFile, outFile);
djsollen@google.coma09e8832012-11-13 18:50:33 +0000656 } else {
657 usage();
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000658 return -1;
659 }
660
robertphillips@google.com73743552013-02-05 20:51:49 +0000661 for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
662 SkDebugf("opt %d: %d\n", opt, gOptTable[opt].fNumTimesApplied);
663 }
664
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000665 SkGraphics::Term();
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000666 return 0;
667}
668
669#if !defined SK_BUILD_FOR_IOS
670int main(int argc, char * const argv[]) {
caryclark@google.com9598f422012-10-09 12:32:37 +0000671 return tool_main(argc, (char**) argv);
robertphillips@google.comc7e4a5a2012-10-04 13:00:33 +0000672}
673#endif