1c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com/*
2c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com * Copyright 2012 Google Inc.
3c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com *
4c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
5c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com * found in the LICENSE file.
6c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com */
7c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
83b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com#include "SkDebugCanvas.h"
9801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com#include "SkDevice.h"
107def5e1630d47cdbfa4b58a9c86bc060693c4d79scroggo@google.com#include "SkForceLinking.h"
11c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com#include "SkGraphics.h"
124e4d75b06bdb484e4e7628461fe5d63088386e91robertphillips@google.com#include "SkImageDecoder.h"
13801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com#include "SkImageEncoder.h"
14a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com#include "SkOSFile.h"
15c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com#include "SkPicture.h"
16801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com#include "SkPictureRecord.h"
17770963f23f4fc313db0fa3bac18b1b8aafb55f17robertphillips@google.com#include "SkPictureRecorder.h"
18c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com#include "SkStream.h"
19a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com#include "picture_utils.h"
20c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
217def5e1630d47cdbfa4b58a9c86bc060693c4d79scroggo@google.com__SK_FORCE_IMAGE_DECODER_LINKING;
227def5e1630d47cdbfa4b58a9c86bc060693c4d79scroggo@google.com
23c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.comstatic void usage() {
24d3d377f1d6f2b4450ca34a3c1b9de880b8a0632crobertphillips@google.com    SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n");
253b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com    SkDebugf("                        [-h|--help]\n\n");
262e87ba0c7c0dfe57e39e6e030db59b69275966cdrobertphillips@google.com    SkDebugf("    -i inFile  : file to filter.\n");
27c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    SkDebugf("    -o outFile : result of filtering.\n");
28a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    SkDebugf("    --input-dir : process all files in dir with .skp extension.\n");
29a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    SkDebugf("    --output-dir : results of filtering the input dir.\n");
30c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    SkDebugf("    -h|--help  : Show this help message.\n");
31c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com}
32c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
333b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com// Is the supplied paint simply a color?
343b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.comstatic bool is_simple(const SkPaint& p) {
353b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com    return NULL == p.getPathEffect() &&
363b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getShader() &&
373b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getXfermode() &&
383b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getMaskFilter() &&
393b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getColorFilter() &&
403b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getRasterizer() &&
413b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getLooper() &&
423b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com           NULL == p.getImageFilter();
433b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com}
4463f1127a70428dca12141c0ad12e1103ae8db35frobertphillips@google.com
4550c84da68b17647371a81593402e897d639989e4robertphillips@google.com
467374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// Check for:
477374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//    SAVE_LAYER
487374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//        DRAW_BITMAP_RECT_TO_RECT
497374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//    RESTORE
507374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// where the saveLayer's color can be moved into the drawBitmapRect
5150c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_0(SkDebugCanvas* canvas, int curCommand) {
5250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() ||
5350c84da68b17647371a81593402e897d639989e4robertphillips@google.com        canvas->getSize() <= curCommand+2 ||
5450c84da68b17647371a81593402e897d639989e4robertphillips@google.com        DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
5550c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+2)->getType()) {
567374355408dd552c773db10ff599699183ba4a46robertphillips@google.com        return false;
5750c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
587374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
597a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer =
607a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand);
617a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
627a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+1);
637374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
647374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    const SkPaint* saveLayerPaint = saveLayer->paint();
657374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    SkPaint* dbmrPaint = dbmr->paint();
667374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
673d18d063f0c6b97b25b88707cfbc1c8cb584caa0skia.committer@gmail.com    // For this optimization we only fold the saveLayer and drawBitmapRect
68c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com    // together if the saveLayer's draw is simple (i.e., no fancy effects)
69c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com    // and the only difference in the colors is their alpha value
701780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque
71c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com    SkColor dbmrColor = dbmrPaint->getColor() | 0xFF000000;       // force opaque
721780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com
73c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com    // If either operation lacks a paint then the collapse is trivial
747374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    return NULL == saveLayerPaint ||
757374355408dd552c773db10ff599699183ba4a46robertphillips@google.com           NULL == dbmrPaint ||
76c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com           (is_simple(*saveLayerPaint) && dbmrColor == layerColor);
777374355408dd552c773db10ff599699183ba4a46robertphillips@google.com}
787374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
797374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer
807374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// and restore
8150c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_0(SkDebugCanvas* canvas, int curCommand) {
827a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer =
837a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand);
847374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    const SkPaint* saveLayerPaint = saveLayer->paint();
857374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
8650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    // if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed
8749f085dddff10473b6ebf832a974288300224e60bsalomon    if (saveLayerPaint) {
887a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        SkDrawBitmapRectCommand* dbmr =
897a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org            (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+1);
9050c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SkPaint* dbmrPaint = dbmr->paint();
9150c84da68b17647371a81593402e897d639989e4robertphillips@google.com
9250c84da68b17647371a81593402e897d639989e4robertphillips@google.com        if (NULL == dbmrPaint) {
9350c84da68b17647371a81593402e897d639989e4robertphillips@google.com            // if the DBMR doesn't have a paint just use the saveLayer's
9450c84da68b17647371a81593402e897d639989e4robertphillips@google.com            dbmr->setPaint(*saveLayerPaint);
9549f085dddff10473b6ebf832a974288300224e60bsalomon        } else if (saveLayerPaint) {
96c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            // Both paints are present so their alphas need to be combined
97c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            SkColor color = saveLayerPaint->getColor();
98c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            int a0 = SkColorGetA(color);
99c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com
100c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            color = dbmrPaint->getColor();
101c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            int a1 = SkColorGetA(color);
102c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com
103c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            int newA = SkMulDiv255Round(a0, a1);
104c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            SkASSERT(newA <= 0xFF);
105c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com
106c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com            SkColor newColor = SkColorSetA(color, newA);
10750c84da68b17647371a81593402e897d639989e4robertphillips@google.com            dbmrPaint->setColor(newColor);
10850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        }
1097374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    }
11050c84da68b17647371a81593402e897d639989e4robertphillips@google.com
11150c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+2);  // restore
11250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);    // saveLayer
1137374355408dd552c773db10ff599699183ba4a46robertphillips@google.com}
1147374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
1157374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// Check for:
1167374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//    SAVE_LAYER
1177374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//        SAVE
1187374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//            CLIP_RECT
1197374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//            DRAW_BITMAP_RECT_TO_RECT
1207374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//        RESTORE
1217374355408dd552c773db10ff599699183ba4a46robertphillips@google.com//    RESTORE
1227374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// where the saveLayer's color can be moved into the drawBitmapRect
12350c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_1(SkDebugCanvas* canvas, int curCommand) {
12450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() ||
12550c84da68b17647371a81593402e897d639989e4robertphillips@google.com        canvas->getSize() <= curCommand+5 ||
12650c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SAVE != canvas->getDrawCommandAt(curCommand+1)->getType() ||
12750c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
12850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+3)->getType() ||
12950c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+4)->getType() ||
13050c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+5)->getType()) {
1317374355408dd552c773db10ff599699183ba4a46robertphillips@google.com        return false;
13250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
1337374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
1347a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer =
1357a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand);
1367a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
1377a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+3);
1387374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
1397374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    const SkPaint* saveLayerPaint = saveLayer->paint();
1407374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    SkPaint* dbmrPaint = dbmr->paint();
1417374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
1423d18d063f0c6b97b25b88707cfbc1c8cb584caa0skia.committer@gmail.com    // For this optimization we only fold the saveLayer and drawBitmapRect
1431780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // together if the saveLayer's draw is simple (i.e., no fancy effects) and
1441780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // and the only difference in the colors is that the saveLayer's can have
1451780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // an alpha while the drawBitmapRect's is opaque.
1461780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // TODO: it should be possible to fold them together even if they both
1471780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // have different non-255 alphas but this is low priority since we have
1481780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // never seen that case
1491780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    // If either operation lacks a paint then the collapse is trivial
1501780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com    SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque
1511780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com
1527374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    return NULL == saveLayerPaint ||
1537374355408dd552c773db10ff599699183ba4a46robertphillips@google.com           NULL == dbmrPaint ||
1541780a3cc84b670f02d11858305598577cdea7730robertphillips@google.com           (is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor);
1557374355408dd552c773db10ff599699183ba4a46robertphillips@google.com}
1567374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
1577374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer
1587374355408dd552c773db10ff599699183ba4a46robertphillips@google.com// and restore
15950c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_1(SkDebugCanvas* canvas, int curCommand) {
1607a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer =
1617a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand);
1627374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    const SkPaint* saveLayerPaint = saveLayer->paint();
1637374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
16450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    // if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed
16549f085dddff10473b6ebf832a974288300224e60bsalomon    if (saveLayerPaint) {
1667a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        SkDrawBitmapRectCommand* dbmr =
1677a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org            (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+3);
16850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SkPaint* dbmrPaint = dbmr->paint();
16950c84da68b17647371a81593402e897d639989e4robertphillips@google.com
17050c84da68b17647371a81593402e897d639989e4robertphillips@google.com        if (NULL == dbmrPaint) {
17150c84da68b17647371a81593402e897d639989e4robertphillips@google.com            dbmr->setPaint(*saveLayerPaint);
17250c84da68b17647371a81593402e897d639989e4robertphillips@google.com        } else {
17350c84da68b17647371a81593402e897d639989e4robertphillips@google.com            SkColor newColor = SkColorSetA(dbmrPaint->getColor(),
17450c84da68b17647371a81593402e897d639989e4robertphillips@google.com                                           SkColorGetA(saveLayerPaint->getColor()));
17550c84da68b17647371a81593402e897d639989e4robertphillips@google.com            dbmrPaint->setColor(newColor);
17650c84da68b17647371a81593402e897d639989e4robertphillips@google.com        }
1777374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    }
17850c84da68b17647371a81593402e897d639989e4robertphillips@google.com
17950c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+5);    // restore
18050c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);      // saveLayer
1817374355408dd552c773db10ff599699183ba4a46robertphillips@google.com}
1827374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
183febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// Check for:
184febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//    SAVE
185febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//        CLIP_RECT
186febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//        DRAW_RECT
187febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//    RESTORE
188febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// where the rect is entirely within the clip and the clip is an intersect
18950c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_2(SkDebugCanvas* canvas, int curCommand) {
19050c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
19150c84da68b17647371a81593402e897d639989e4robertphillips@google.com        canvas->getSize() <= curCommand+4 ||
19250c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
19350c84da68b17647371a81593402e897d639989e4robertphillips@google.com        DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
19450c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
195febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com        return false;
19650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
197febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
1987a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* cr =
1997a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+1);
2007a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawRectCommand* dr =
2017a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawRectCommand*) canvas->getDrawCommandAt(curCommand+2);
202febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
203febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    if (SkRegion::kIntersect_Op != cr->op()) {
204febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com        return false;
205febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    }
206febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
207febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    return cr->rect().contains(dr->rect());
208febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com}
209febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
210febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// Remove everything but the drawRect
21150c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_2(SkDebugCanvas* canvas, int curCommand) {
21250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+3);   // restore
21350c84da68b17647371a81593402e897d639989e4robertphillips@google.com    // drawRect
21450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+1);   // clipRect
21550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);     // save
216febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com}
217febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
218febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// Check for:
219febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//    SAVE
220febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//        CLIP_RRECT
221febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//        DRAW_RECT
222febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//    RESTORE
223febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// where the rect entirely encloses the clip
22450c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_3(SkDebugCanvas* canvas, int curCommand) {
22550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
22650c84da68b17647371a81593402e897d639989e4robertphillips@google.com        canvas->getSize() <= curCommand+4 ||
22750c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RRECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
22850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
22950c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
230febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com        return false;
23150c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
232febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
2337a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRRectCommand* crr =
2347a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRRectCommand*) canvas->getDrawCommandAt(curCommand+1);
2357a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawRectCommand* dr  =
2367a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawRectCommand*) canvas->getDrawCommandAt(curCommand+2);
237febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
238febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    if (SkRegion::kIntersect_Op != crr->op()) {
239febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com        return false;
240febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    }
241febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
242febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    return dr->rect().contains(crr->rrect().rect());
243febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com}
244febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
245febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// Replace everything with a drawRRect with the paint from the drawRect
246febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// and the AA settings from the clipRRect
24750c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_3(SkDebugCanvas* canvas, int curCommand) {
24850c84da68b17647371a81593402e897d639989e4robertphillips@google.com
24950c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+3);    // restore
250febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
2517a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRRectCommand* crr =
2527a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRRectCommand*) canvas->getDrawCommandAt(curCommand+1);
2537a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawRectCommand* dr  =
2547a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawRectCommand*) canvas->getDrawCommandAt(curCommand+2);
255febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
256febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    // TODO: could skip paint re-creation if the AA settings already match
25791217d0b0cdf80a1f18ca24d49e4a925d4629f1frobertphillips@google.com    SkPaint newPaint = dr->paint();
258febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    newPaint.setAntiAlias(crr->doAA());
2597a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawRRectCommand* drr = new SkDrawRRectCommand(crr->rrect(), newPaint);
26050c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->setDrawCommandAt(curCommand+2, drr);
26150c84da68b17647371a81593402e897d639989e4robertphillips@google.com
26250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+1);    // clipRRect
26350c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);      // save
264febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com}
265febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
266febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// Check for:
267febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//    SAVE
268febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//        CLIP_RECT
269febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//        DRAW_BITMAP_RECT_TO_RECT
270febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com//    RESTORE
271febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// where the rect and drawBitmapRect dst exactly match
27250c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_4(SkDebugCanvas* canvas, int curCommand) {
27350c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
27450c84da68b17647371a81593402e897d639989e4robertphillips@google.com        canvas->getSize() <= curCommand+4 ||
27550c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
27650c84da68b17647371a81593402e897d639989e4robertphillips@google.com        DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
27750c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
278febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com        return false;
27950c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
280febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
2817a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* cr =
2827a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+1);
2837a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr  =
2847a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+2);
285febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
286febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    if (SkRegion::kIntersect_Op != cr->op()) {
287febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com        return false;
288febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    }
289febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
290febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    return dbmr->dstRect() == cr->rect();
291febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com}
292febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
293febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com// Remove everything but the drawBitmapRect
29450c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_4(SkDebugCanvas* canvas, int curCommand) {
29550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+3);    // restore
29650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    // drawBitmapRectToRect
29750c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+1);    // clipRect
29850c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);      // save
299febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com}
300febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com
3019105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com// Check for:
3029105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com//    TRANSLATE
3039105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com// where the translate is zero
30450c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_5(SkDebugCanvas* canvas, int curCommand) {
30550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (TRANSLATE != canvas->getDrawCommandAt(curCommand)->getType()) {
3069105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com        return false;
3079105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com    }
3089105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3097a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkTranslateCommand* t =
3107a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkTranslateCommand*) canvas->getDrawCommandAt(curCommand);
3119105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3129105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com    return 0 == t->x() && 0 == t->y();
3139105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com}
3149105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3159105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com// Just remove the translate
31650c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_5(SkDebugCanvas* canvas, int curCommand) {
31750c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);    // translate
3189105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com}
3199105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3209105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com// Check for:
3219105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com//    SCALE
3229105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com// where the scale is 1,1
32350c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_6(SkDebugCanvas* canvas, int curCommand) {
32450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SCALE != canvas->getDrawCommandAt(curCommand)->getType()) {
3259105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com        return false;
3269105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com    }
3279105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3287a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkScaleCommand* s = (SkScaleCommand*) canvas->getDrawCommandAt(curCommand);
3299105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3309105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com    return SK_Scalar1 == s->x() && SK_Scalar1 == s->y();
3319105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com}
3329105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
3339105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com// Just remove the scale
33450c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_6(SkDebugCanvas* canvas, int curCommand) {
33550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);   // scale
3369105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com}
3379105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
338c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com// Check for:
339c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//  SAVE
340c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      CLIP_RECT
341c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      SAVE_LAYER
342c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//          SAVE
343c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//              CLIP_RECT
344c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//              SAVE_LAYER
345c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//                  SAVE
346c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//                      CLIP_RECT
347c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//                      DRAWBITMAPRECTTORECT
348c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//                  RESTORE
349c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//              RESTORE
350c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//          RESTORE
351c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      RESTORE
352c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//  RESTORE
353c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com// where:
354c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      all the clipRect's are BW, nested, intersections
355c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      the drawBitmapRectToRect is a 1-1 copy from src to dest
356c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      the last (smallest) clip rect is a subset of the drawBitmapRectToRect's dest rect
357c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com//      all the saveLayer's paints can be rolled into the drawBitmapRectToRect's paint
358c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com// This pattern is used by Google spreadsheet when drawing the toolbar buttons
35950c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic bool check_7(SkDebugCanvas* canvas, int curCommand) {
36050c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
36150c84da68b17647371a81593402e897d639989e4robertphillips@google.com        canvas->getSize() <= curCommand+13 ||
36250c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
36350c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SAVE_LAYER != canvas->getDrawCommandAt(curCommand+2)->getType() ||
36450c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SAVE != canvas->getDrawCommandAt(curCommand+3)->getType() ||
36550c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RECT != canvas->getDrawCommandAt(curCommand+4)->getType() ||
36650c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SAVE_LAYER != canvas->getDrawCommandAt(curCommand+5)->getType() ||
36750c84da68b17647371a81593402e897d639989e4robertphillips@google.com        SAVE != canvas->getDrawCommandAt(curCommand+6)->getType() ||
36850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        CLIP_RECT != canvas->getDrawCommandAt(curCommand+7)->getType() ||
36950c84da68b17647371a81593402e897d639989e4robertphillips@google.com        DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+8)->getType() ||
37050c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+9)->getType() ||
37150c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+10)->getType() ||
37250c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+11)->getType() ||
37350c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+12)->getType() ||
37450c84da68b17647371a81593402e897d639989e4robertphillips@google.com        RESTORE != canvas->getDrawCommandAt(curCommand+13)->getType()) {
375c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        return false;
37650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
377c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
3787a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip0 =
3797a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+1);
3807a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer0 =
3817a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand+2);
3827a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip1 =
3837a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+4);
3847a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer1 =
3857a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand+5);
3867a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip2 =
3877a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+7);
3887a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
3897a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+8);
390c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
391c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    if (clip0->doAA() || clip1->doAA() || clip2->doAA()) {
392c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        return false;
393c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
394c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
395c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    if (SkRegion::kIntersect_Op != clip0->op() ||
396c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkRegion::kIntersect_Op != clip1->op() ||
397c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkRegion::kIntersect_Op != clip2->op()) {
398c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        return false;
399c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
400c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
4016acd09e2b07ef547dbc0c3a69425a4dcda0cca51skia.committer@gmail.com    if (!clip0->rect().contains(clip1->rect()) ||
402c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        !clip1->rect().contains(clip2->rect())) {
403c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        return false;
404c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
405c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
406c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    // The src->dest mapping needs to be 1-to-1
407c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    if (NULL == dbmr->srcRect()) {
408c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        if (dbmr->bitmap().width() != dbmr->dstRect().width() ||
409c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com            dbmr->bitmap().height() != dbmr->dstRect().height()) {
410c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com            return false;
411c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        }
412c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    } else {
413c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        if (dbmr->srcRect()->width() != dbmr->dstRect().width() ||
414c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com            dbmr->srcRect()->height() != dbmr->dstRect().height()) {
415c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com            return false;
416c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        }
417c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
418c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
419c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    if (!dbmr->dstRect().contains(clip2->rect())) {
420c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        return false;
421c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
422c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
423c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    const SkPaint* saveLayerPaint0 = saveLayer0->paint();
424c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    const SkPaint* saveLayerPaint1 = saveLayer1->paint();
425c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
42649f085dddff10473b6ebf832a974288300224e60bsalomon    if ((saveLayerPaint0 && !is_simple(*saveLayerPaint0)) ||
42749f085dddff10473b6ebf832a974288300224e60bsalomon        (saveLayerPaint1 && !is_simple(*saveLayerPaint1))) {
428c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        return false;
429c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
430c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
431c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    SkPaint* dbmrPaint = dbmr->paint();
432c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
433c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    if (NULL == dbmrPaint) {
4346acd09e2b07ef547dbc0c3a69425a4dcda0cca51skia.committer@gmail.com        return true;
435c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
4366acd09e2b07ef547dbc0c3a69425a4dcda0cca51skia.committer@gmail.com
43749f085dddff10473b6ebf832a974288300224e60bsalomon    if (saveLayerPaint0) {
438c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkColor layerColor0 = saveLayerPaint0->getColor() | 0xFF000000; // force opaque
439c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        if (dbmrPaint->getColor() != layerColor0) {
440c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com            return false;
441c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        }
442c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
443c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
44449f085dddff10473b6ebf832a974288300224e60bsalomon    if (saveLayerPaint1) {
445c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkColor layerColor1 = saveLayerPaint1->getColor() | 0xFF000000; // force opaque
446c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        if (dbmrPaint->getColor() != layerColor1) {
447c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com            return false;
448c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        }
449c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
450c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
451c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    return true;
452c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com}
453c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
454c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com// Reduce to a single drawBitmapRectToRect call by folding the clipRect's into
455c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com// the src and dst Rects and the saveLayer paints into the drawBitmapRectToRect's
456c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com// paint.
45750c84da68b17647371a81593402e897d639989e4robertphillips@google.comstatic void apply_7(SkDebugCanvas* canvas, int curCommand) {
4587a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer0 =
4597a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand+2);
4607a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkSaveLayerCommand* saveLayer1 =
4617a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkSaveLayerCommand*) canvas->getDrawCommandAt(curCommand+5);
4627a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip2 =
4637a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+7);
4647a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
4657a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+8);
466c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
467c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    SkScalar newSrcLeft = dbmr->srcRect()->fLeft + clip2->rect().fLeft - dbmr->dstRect().fLeft;
468c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    SkScalar newSrcTop = dbmr->srcRect()->fTop + clip2->rect().fTop - dbmr->dstRect().fTop;
469c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
4706acd09e2b07ef547dbc0c3a69425a4dcda0cca51skia.committer@gmail.com    SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop,
471c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com                                     clip2->rect().width(), clip2->rect().height());
472c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
473c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    dbmr->setSrcRect(newSrc);
474c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    dbmr->setDstRect(clip2->rect());
475c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
476c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    SkColor color = 0xFF000000;
477c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    int a0, a1;
478c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
479c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    const SkPaint* saveLayerPaint0 = saveLayer0->paint();
48049f085dddff10473b6ebf832a974288300224e60bsalomon    if (saveLayerPaint0) {
481c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        color = saveLayerPaint0->getColor();
482c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        a0 = SkColorGetA(color);
483c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    } else {
484c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        a0 = 0xFF;
485c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
486c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
487c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    const SkPaint* saveLayerPaint1 = saveLayer1->paint();
48849f085dddff10473b6ebf832a974288300224e60bsalomon    if (saveLayerPaint1) {
489c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        color = saveLayerPaint1->getColor();
490c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        a1 = SkColorGetA(color);
491c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    } else {
492c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        a1 = 0xFF;
493c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
494c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
495c52570475f00d5b62b8d82ba50b4b911dc38ce43robertphillips@google.com    int newA = SkMulDiv255Round(a0, a1);
496c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    SkASSERT(newA <= 0xFF);
497c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
498c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    SkPaint* dbmrPaint = dbmr->paint();
499c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
50049f085dddff10473b6ebf832a974288300224e60bsalomon    if (dbmrPaint) {
501c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkColor newColor = SkColorSetA(dbmrPaint->getColor(), newA);
502c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        dbmrPaint->setColor(newColor);
503c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    } else {
504c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkColor newColor = SkColorSetA(color, newA);
505c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
506c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        SkPaint newPaint;
507c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        newPaint.setColor(newColor);
508c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com        dbmr->setPaint(newPaint);
509c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    }
510c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com
511c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    // remove everything except the drawbitmaprect
51250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+13);   // restore
51350c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+12);   // restore
51450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+11);   // restore
51550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+10);   // restore
51650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+9);    // restore
51750c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+7);    // clipRect
51850c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+6);    // save
51950c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+5);    // saveLayer
52050c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+4);    // clipRect
52150c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+3);    // save
52250c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+2);    // saveLayer
52350c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand+1);    // clipRect
52450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    canvas->deleteDrawCommandAt(curCommand);      // save
525c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com}
5269105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com
5273bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org// Check for:
5283bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//    SAVE
5293bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//       CLIP_RECT
5303bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//       DRAWBITMAPRECTTORECT
5313bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//    RESTORE
5323bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org// where:
5333bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//      the drawBitmapRectToRect is a 1-1 copy from src to dest
5343bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//      the clip rect is BW and a subset of the drawBitmapRectToRect's dest rect
5353bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.orgstatic bool check_8(SkDebugCanvas* canvas, int curCommand) {
5363bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
5373bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        canvas->getSize() <= curCommand+4 ||
5383bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
5393bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
5403bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
5413bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        return false;
5423bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
5433bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5447a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip =
5457a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+1);
5467a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
5477a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+2);
5483bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5493bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (clip->doAA() || SkRegion::kIntersect_Op != clip->op()) {
5503bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        return false;
5513bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
5523bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5533bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    // The src->dest mapping needs to be 1-to-1
5543bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (NULL == dbmr->srcRect()) {
5553bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        if (dbmr->bitmap().width() != dbmr->dstRect().width() ||
5563bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org            dbmr->bitmap().height() != dbmr->dstRect().height()) {
5573bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org            return false;
5583bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        }
5593bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    } else {
5603bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        if (dbmr->srcRect()->width() != dbmr->dstRect().width() ||
5613bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org            dbmr->srcRect()->height() != dbmr->dstRect().height()) {
5623bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org            return false;
5633bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        }
5643bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
5653bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5663bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (!dbmr->dstRect().contains(clip->rect())) {
5673bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        return false;
5683bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
5693bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5703bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    return true;
5713bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org}
5723bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5733bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org// Fold the clipRect into the drawBitmapRectToRect's src and dest rects
5743bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.orgstatic void apply_8(SkDebugCanvas* canvas, int curCommand) {
5757a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip =
5767a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+1);
5777a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
5787a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+2);
5793bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5803bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    SkScalar newSrcLeft, newSrcTop;
5813bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
58249f085dddff10473b6ebf832a974288300224e60bsalomon    if (dbmr->srcRect()) {
5833bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        newSrcLeft = dbmr->srcRect()->fLeft + clip->rect().fLeft - dbmr->dstRect().fLeft;
5843bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        newSrcTop  = dbmr->srcRect()->fTop + clip->rect().fTop - dbmr->dstRect().fTop;
5853bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    } else {
5863bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        newSrcLeft = clip->rect().fLeft - dbmr->dstRect().fLeft;
5873bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        newSrcTop  = clip->rect().fTop - dbmr->dstRect().fTop;
5883bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
5893bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5903bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop,
5913bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org                                     clip->rect().width(), clip->rect().height());
5923bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5933bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    dbmr->setSrcRect(newSrc);
5943bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    dbmr->setDstRect(clip->rect());
5953bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
5963bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    // remove everything except the drawbitmaprect
5973bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    canvas->deleteDrawCommandAt(curCommand+3);
5983bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    canvas->deleteDrawCommandAt(curCommand+1);
5993bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    canvas->deleteDrawCommandAt(curCommand);
6003bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org}
6013bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
6023bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org// Check for:
6033bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//  SAVE
6043bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//    CLIP_RECT
6053bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//    DRAWBITMAPRECTTORECT
6063bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//  RESTORE
6073bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org// where:
6083bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org//      clipRect is BW and encloses the DBMR2R's dest rect
6093bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.orgstatic bool check_9(SkDebugCanvas* canvas, int curCommand) {
6103bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() ||
6113bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        canvas->getSize() <= curCommand+4 ||
6123bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() ||
6133bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() ||
6143bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) {
6153bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        return false;
6163bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
6173bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
6187a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkClipRectCommand* clip =
6197a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkClipRectCommand*) canvas->getDrawCommandAt(curCommand+1);
6207a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org    SkDrawBitmapRectCommand* dbmr =
6217a11591e5e09493f1589c93d53fd1fe10086920acommit-bot@chromium.org        (SkDrawBitmapRectCommand*) canvas->getDrawCommandAt(curCommand+2);
6223bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
6233bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (clip->doAA() || SkRegion::kIntersect_Op != clip->op()) {
6243bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        return false;
6253bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
6263bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
6273bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    if (!clip->rect().contains(dbmr->dstRect())) {
6283bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org        return false;
6293bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    }
6303bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
6313bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    return true;
6323bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org}
6333bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
6343bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org// remove everything except the drawbitmaprect
6353bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.orgstatic void apply_9(SkDebugCanvas* canvas, int curCommand) {
6363bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    canvas->deleteDrawCommandAt(curCommand+3);   // restore
6373bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    // drawBitmapRectToRect
6383bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    canvas->deleteDrawCommandAt(curCommand+1);   // clipRect
6393bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    canvas->deleteDrawCommandAt(curCommand);     // save
6403bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org}
6413bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org
64250c84da68b17647371a81593402e897d639989e4robertphillips@google.comtypedef bool (*PFCheck)(SkDebugCanvas* canvas, int curCommand);
64350c84da68b17647371a81593402e897d639989e4robertphillips@google.comtypedef void (*PFApply)(SkDebugCanvas* canvas, int curCommand);
6447374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
6457374355408dd552c773db10ff599699183ba4a46robertphillips@google.comstruct OptTableEntry {
6467374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    PFCheck fCheck;
6477374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    PFApply fApply;
6487374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    int fNumTimesApplied;
6497374355408dd552c773db10ff599699183ba4a46robertphillips@google.com} gOptTable[] = {
6507374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    { check_0, apply_0, 0 },
6517374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    { check_1, apply_1, 0 },
652febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    { check_2, apply_2, 0 },
653febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    { check_3, apply_3, 0 },
654febc0ec41b4cff6ea69f2b89d72c0d330d198283robertphillips@google.com    { check_4, apply_4, 0 },
6559105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com    { check_5, apply_5, 0 },
6569105ad0f85489057509b7c7db6c58ed41d9489c9robertphillips@google.com    { check_6, apply_6, 0 },
657c3410b8cbbf63ac7968262c25c996bdbaab20588robertphillips@google.com    { check_7, apply_7, 0 },
6583bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    { check_8, apply_8, 0 },
6593bdf16496faedab584715a5d7ca859f7d60e8ae9commit-bot@chromium.org    { check_9, apply_9, 0 },
6607374355408dd552c773db10ff599699183ba4a46robertphillips@google.com};
6617374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
66250c84da68b17647371a81593402e897d639989e4robertphillips@google.com
6633b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.comstatic int filter_picture(const SkString& inFile, const SkString& outFile) {
6644d98b740cdfa4adf3f5832a466294382570928fcrobertphillips@google.com    SkAutoTDelete<SkPicture> inPicture;
665a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
666a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    SkFILEStream inStream(inFile.c_str());
667a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    if (inStream.isValid()) {
668f1754ec69131801c1a6ed3c704501a9400bbf324scroggo@google.com        inPicture.reset(SkPicture::CreateFromStream(&inStream));
669a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    }
670a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
6714d98b740cdfa4adf3f5832a466294382570928fcrobertphillips@google.com    if (NULL == inPicture.get()) {
672a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        SkDebugf("Could not read file %s\n", inFile.c_str());
673a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        return -1;
674a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    }
675a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
6767374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    int localCount[SK_ARRAY_COUNT(gOptTable)];
6777374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
6787374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    memset(localCount, 0, sizeof(localCount));
6797374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
680a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips    SkDebugCanvas debugCanvas(SkScalarCeilToInt(inPicture->cullRect().width()),
681a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                              SkScalarCeilToInt(inPicture->cullRect().height()));
682c5ba71d2e5cd426def66fa49dcf003e5b2c98dc7robertphillips    inPicture->playback(&debugCanvas);
6833b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com
68450c84da68b17647371a81593402e897d639989e4robertphillips@google.com    // delete the initial save and restore since replaying the commands will
6857374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    // re-add them
68650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if (debugCanvas.getSize() > 1) {
68750c84da68b17647371a81593402e897d639989e4robertphillips@google.com        debugCanvas.deleteDrawCommandAt(0);
68850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        debugCanvas.deleteDrawCommandAt(debugCanvas.getSize()-1);
6897374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    }
6907374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
691d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com    bool changed = true;
6922e87ba0c7c0dfe57e39e6e030db59b69275966cdrobertphillips@google.com    int numBefore = debugCanvas.getSize();
693d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com
694d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com    while (changed) {
695d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com        changed = false;
696d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com        for (int i = 0; i < debugCanvas.getSize(); ++i) {
697d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com            for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
698d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                if ((*gOptTable[opt].fCheck)(&debugCanvas, i)) {
699d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    (*gOptTable[opt].fApply)(&debugCanvas, i);
700d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com
701d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    ++gOptTable[opt].fNumTimesApplied;
702d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    ++localCount[opt];
703d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com
704d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    if (debugCanvas.getSize() == i) {
705d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                        // the optimization removed all the remaining operations
706d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                        break;
707d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    }
708d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com
709d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    opt = 0;          // try all the opts all over again
710d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                    changed = true;
711d9c1853fb88ef02f74d003a5abd5531315a726dbrobertphillips@google.com                }
7123b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com            }
7133b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com        }
7143b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com    }
715a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
7162e87ba0c7c0dfe57e39e6e030db59b69275966cdrobertphillips@google.com    int numAfter = debugCanvas.getSize();
7172e87ba0c7c0dfe57e39e6e030db59b69275966cdrobertphillips@google.com
7183b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com    if (!outFile.isEmpty()) {
71984b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com        SkPictureRecorder recorder;
720a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips        SkCanvas* canvas = recorder.beginRecording(inPicture->cullRect().width(),
721a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                                                   inPicture->cullRect().height(),
722a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips                                                   NULL, 0);
7233b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com        debugCanvas.draw(canvas);
72484b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com        SkAutoTUnref<SkPicture> outPicture(recorder.endRecording());
725a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
726a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        SkFILEWStream outStream(outFile.c_str());
727a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
72884b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com        outPicture->serialize(&outStream);
729a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    }
730a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
7317374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    bool someOptFired = false;
7327374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
7337374355408dd552c773db10ff599699183ba4a46robertphillips@google.com        if (0 != localCount[opt]) {
7347374355408dd552c773db10ff599699183ba4a46robertphillips@google.com            SkDebugf("%d: %d ", opt, localCount[opt]);
7357374355408dd552c773db10ff599699183ba4a46robertphillips@google.com            someOptFired = true;
7367374355408dd552c773db10ff599699183ba4a46robertphillips@google.com        }
7377374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    }
7387374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
7397374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    if (!someOptFired) {
7407374355408dd552c773db10ff599699183ba4a46robertphillips@google.com        SkDebugf("No opts fired\n");
7417374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    } else {
7423284017a60ea4fc3dc5b95838ba0c301ee1e4e8dskia.committer@gmail.com        SkDebugf("\t before: %d after: %d delta: %d\n",
7432e87ba0c7c0dfe57e39e6e030db59b69275966cdrobertphillips@google.com                 numBefore, numAfter, numBefore-numAfter);
7447374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    }
7457374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
746a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    return 0;
747a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com}
748a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com
749a5b7cc00b9e95c4c6c02b5bd26aba5e6401f7fd7tfarina@chromium.org// This function is not marked as 'static' so it can be referenced externally
750a5b7cc00b9e95c4c6c02b5bd26aba5e6401f7fd7tfarina@chromium.org// in the iOS build.
75105af1afd429808913683da75644e48bece12e820humper@google.comint tool_main(int argc, char** argv); // suppress a warning on mac
75205af1afd429808913683da75644e48bece12e820humper@google.com
7539598f4256d729434a9e7273a7de1e4876eaacee9caryclark@google.comint tool_main(int argc, char** argv) {
7544d98b740cdfa4adf3f5832a466294382570928fcrobertphillips@google.com#if SK_ENABLE_INST_COUNT
7554d98b740cdfa4adf3f5832a466294382570928fcrobertphillips@google.com    gPrintInstCount = true;
7564d98b740cdfa4adf3f5832a466294382570928fcrobertphillips@google.com#endif
7574d98b740cdfa4adf3f5832a466294382570928fcrobertphillips@google.com
758c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    SkGraphics::Init();
759c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
760801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com    if (argc < 3) {
761c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        usage();
762c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        return -1;
763c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    }
764c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
7653b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com    SkString inFile, outFile, inDir, outDir;
766801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com
767c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    char* const* stop = argv + argc;
768c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    for (++argv; argv < stop; ++argv) {
769c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        if (strcmp(*argv, "-i") == 0) {
770c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            argv++;
771c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            if (argv < stop && **argv) {
772c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com                inFile.set(*argv);
773c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            } else {
774801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com                SkDebugf("missing arg for -i\n");
775c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com                usage();
776c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com                return -1;
777c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            }
778a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        } else if (strcmp(*argv, "--input-dir") == 0) {
779a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            argv++;
780a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            if (argv < stop && **argv) {
781a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                inDir.set(*argv);
782a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            } else {
783a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                SkDebugf("missing arg for --input-dir\n");
784a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                usage();
785a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                return -1;
786a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            }
787a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        } else if (strcmp(*argv, "--output-dir") == 0) {
788a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            argv++;
789a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            if (argv < stop && **argv) {
790a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                outDir.set(*argv);
791a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            } else {
792a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                SkDebugf("missing arg for --output-dir\n");
793a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                usage();
794a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com                return -1;
795a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            }
796c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        } else if (strcmp(*argv, "-o") == 0) {
797c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            argv++;
798c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            if (argv < stop && **argv) {
799c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com                outFile.set(*argv);
800c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            } else {
801801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com                SkDebugf("missing arg for -o\n");
802801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com                usage();
803801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com                return -1;
804801cee1d4cad2c382059c0f367edd77298b05caarobertphillips@google.com            }
805c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
806c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            usage();
807c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            return 0;
808c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        } else {
809c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            SkDebugf("unknown arg %s\n", *argv);
810c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            usage();
811c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com            return -1;
812c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        }
813c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    }
814c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
815a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    SkOSFile::Iter iter(inDir.c_str(), "skp");
81605af1afd429808913683da75644e48bece12e820humper@google.com
817a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    SkString inputFilename, outputFilename;
818a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    if (iter.next(&inputFilename)) {
819c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
820a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        do {
821a8e2e1504b9af6ba791637f228debaa23953064atfarina            inFile = SkOSPath::Join(inDir.c_str(), inputFilename.c_str());
822a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            if (!outDir.isEmpty()) {
823a8e2e1504b9af6ba791637f228debaa23953064atfarina                outFile = SkOSPath::Join(outDir.c_str(), inputFilename.c_str());
824a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            }
825a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com            SkDebugf("Executing %s\n", inputFilename.c_str());
8263b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com            filter_picture(inFile, outFile);
827a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        } while(iter.next(&inputFilename));
828c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
829a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    } else if (!inFile.isEmpty()) {
8303b0a9fe5672e7339ec3e5e6d3986b15f57ae24e7robertphillips@google.com        filter_picture(inFile, outFile);
831a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com    } else {
832a09e8838f2534d42046338752482d6759f6c649ddjsollen@google.com        usage();
833c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com        return -1;
834c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    }
835c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
8367374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) {
8377374355408dd552c773db10ff599699183ba4a46robertphillips@google.com        SkDebugf("opt %d: %d\n", opt, gOptTable[opt].fNumTimesApplied);
8387374355408dd552c773db10ff599699183ba4a46robertphillips@google.com    }
8397374355408dd552c773db10ff599699183ba4a46robertphillips@google.com
840c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    SkGraphics::Term();
841c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com    return 0;
842c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com}
843c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com
844c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com#if !defined SK_BUILD_FOR_IOS
845c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.comint main(int argc, char * const argv[]) {
8469598f4256d729434a9e7273a7de1e4876eaacee9caryclark@google.com    return tool_main(argc, (char**) argv);
847c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com}
848c7e4a5a02a16b73d86e90e240c1708d6600f7318robertphillips@google.com#endif
849