1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
90716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com#include "SkColor.h"
108b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkReadBuffer.h"
118b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkWriteBuffer.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkLayerDrawLooper.h"
134991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#include "SkString.h"
144991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#include "SkStringUtils.h"
150716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com#include "SkUnPreMultiply.h"
160716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
170716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.comSkLayerDrawLooper::LayerInfo::LayerInfo() {
18a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    fPaintBits = 0;                     // ignore our paint fields
190716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    fColorMode = SkXfermode::kDst_Mode; // ignore our color
200716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    fOffset.set(0, 0);
210716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    fPostTranslate = false;
220716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com}
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
243c898186c9082c535e589807752a0a9dc5d28aa0vandebo@chromium.orgSkLayerDrawLooper::SkLayerDrawLooper()
253c898186c9082c535e589807752a0a9dc5d28aa0vandebo@chromium.org        : fRecs(NULL),
268f838259ab65e44562902679fa88cb00575b99cecommit-bot@chromium.org          fTopRec(NULL),
2779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org          fCount(0) {
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkLayerDrawLooper::~SkLayerDrawLooper() {
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec* rec = fRecs;
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (rec) {
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Rec* next = rec->fNext;
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkDELETE(rec);
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rec = next;
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
380716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
3979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgSkLayerDrawLooper::Context* SkLayerDrawLooper::createContext(SkCanvas* canvas, void* storage) const {
40091a594dbc4116ec2e54724432472bf37dae794acommit-bot@chromium.org    canvas->save();
4179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    return SkNEW_PLACEMENT_ARGS(storage, LayerDrawLooperContext, (this));
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
440716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.comstatic SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) {
450716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    switch (mode) {
460716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        case SkXfermode::kSrc_Mode:
470716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com            return src;
480716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        case SkXfermode::kDst_Mode:
490716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com            return dst;
500716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        default: {
510716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com            SkPMColor pmS = SkPreMultiplyColor(src);
520716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com            SkPMColor pmD = SkPreMultiplyColor(dst);
530716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com            SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
540716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com            return SkUnPreMultiply::PMColorToColor(result);
550716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        }
560716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    }
570716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com}
580716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
598426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com// Even with kEntirePaint_Bits, we always ensure that the master paint's
608426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com// text-encoding is respected, since that controls how we interpret the
618426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com// text/length parameters of a draw[Pos]Text call.
6279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgvoid SkLayerDrawLooper::LayerDrawLooperContext::ApplyInfo(
6379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        SkPaint* dst, const SkPaint& src, const LayerInfo& info) {
64a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org
65a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode));
66a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org
67a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    BitFlags bits = info.fPaintBits;
688426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com    SkPaint::TextEncoding encoding = dst->getTextEncoding();
690716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
703c1225bee4d9ea0963acf47fc5df080b2d5b26b2mike@reedtribe.org    if (0 == bits) {
713c1225bee4d9ea0963acf47fc5df080b2d5b26b2mike@reedtribe.org        return;
723c1225bee4d9ea0963acf47fc5df080b2d5b26b2mike@reedtribe.org    }
730e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (kEntirePaint_Bits == bits) {
74a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org        // we've already computed these, so save it from the assignment
75a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org        uint32_t f = dst->getFlags();
760716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        SkColor c = dst->getColor();
770e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        *dst = src;
78a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org        dst->setFlags(f);
790716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        dst->setColor(c);
808426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com        dst->setTextEncoding(encoding);
810e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        return;
820e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
830e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
840e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kStyle_Bit) {
850e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setStyle(src.getStyle());
860e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setStrokeWidth(src.getStrokeWidth());
870e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setStrokeMiter(src.getStrokeMiter());
880e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setStrokeCap(src.getStrokeCap());
890e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setStrokeJoin(src.getStrokeJoin());
900e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
910e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
920e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kTextSkewX_Bit) {
930e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setTextSkewX(src.getTextSkewX());
940e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
950e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
960e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kPathEffect_Bit) {
970e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setPathEffect(src.getPathEffect());
980e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
990e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kMaskFilter_Bit) {
1000e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setMaskFilter(src.getMaskFilter());
1010e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
1020e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kShader_Bit) {
1030e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setShader(src.getShader());
1040e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
1050e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kColorFilter_Bit) {
1060e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setColorFilter(src.getColorFilter());
1070e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
1080e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    if (bits & kXfermode_Bit) {
1090e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        dst->setXfermode(src.getXfermode());
1100e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    }
1110e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
1128426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com    // we don't override these
1130e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org#if 0
1140e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setTypeface(src.getTypeface());
1150e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setTextSize(src.getTextSize());
1160e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setTextScaleX(src.getTextScaleX());
1170e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setRasterizer(src.getRasterizer());
1180e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setLooper(src.getLooper());
1190e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setTextEncoding(src.getTextEncoding());
1200e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    dst->setHinting(src.getHinting());
1210e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org#endif
1220e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org}
1230e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
1240716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com// Should we add this to canvas?
1250716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.comstatic void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
1260716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    SkMatrix m = canvas->getTotalMatrix();
1270716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    m.postTranslate(dx, dy);
1280716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    canvas->setMatrix(m);
1290716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com}
1300716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
13179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgSkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext(
13279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {}
13379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
13479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgbool SkLayerDrawLooper::LayerDrawLooperContext::next(SkCanvas* canvas,
13579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org                                                     SkPaint* paint) {
1364e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    canvas->restore();
1374e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    if (NULL == fCurrRec) {
1384e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com        return false;
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
141a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
1420716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
143091a594dbc4116ec2e54724432472bf37dae794acommit-bot@chromium.org    canvas->save();
1440716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    if (fCurrRec->fInfo.fPostTranslate) {
1450716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
1460716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com                      fCurrRec->fInfo.fOffset.fY);
1470716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    } else {
14879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        canvas->translate(fCurrRec->fInfo.fOffset.fX,
14979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org                          fCurrRec->fInfo.fOffset.fY);
1500716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    }
151e5d0def6d9864463565f9ca6a6e9ceedcb72712dmike@reedtribe.org    fCurrRec = fCurrRec->fNext;
152e5d0def6d9864463565f9ca6a6e9ceedcb72712dmike@reedtribe.org
1534e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    return true;
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
156daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.combool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const {
157daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (fCount != 2) {
158daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
159daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
160daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    const Rec* rec = fRecs;
161daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
162daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    // bottom layer needs to be just blur(maskfilter)
163daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) {
164daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
165daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
166daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (SkXfermode::kSrc_Mode != rec->fInfo.fColorMode) {
167daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
168daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
169daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    const SkMaskFilter* mf = rec->fPaint.getMaskFilter();
170daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (NULL == mf) {
171daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
172daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
173daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    SkMaskFilter::BlurRec maskBlur;
174daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (!mf->asABlur(&maskBlur)) {
175daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
176daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
177daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
178daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    rec = rec->fNext;
179daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    // top layer needs to be "plain"
180daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (rec->fInfo.fPaintBits) {
181daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
182daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
183daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (SkXfermode::kDst_Mode != rec->fInfo.fColorMode) {
184daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
185daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
186daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (!rec->fInfo.fOffset.equals(0, 0)) {
187daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
188daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
189daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
190daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (bsRec) {
191daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        bsRec->fSigma = maskBlur.fSigma;
192daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        bsRec->fOffset = fRecs->fInfo.fOffset;
193daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        bsRec->fColor = fRecs->fPaint.getColor();
194daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        bsRec->fStyle = maskBlur.fStyle;
195daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        bsRec->fQuality = maskBlur.fQuality;
196daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
197daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    return true;
198daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com}
199daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2028b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgvoid SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const {
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    buffer.writeInt(fCount);
204fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec* rec = fRecs;
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < fCount; i++) {
207842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com        // Legacy "flagsmask" field -- now ignored, remove when we bump version
208842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com        buffer.writeInt(0);
209842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com
2100716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        buffer.writeInt(rec->fInfo.fPaintBits);
2110716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        buffer.writeInt(rec->fInfo.fColorMode);
212c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com        buffer.writePoint(rec->fInfo.fOffset);
2130716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        buffer.writeBool(rec->fInfo.fPostTranslate);
214c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com        buffer.writePaint(rec->fPaint);
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        rec = rec->fNext;
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgSkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) {
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int count = buffer.readInt();
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    Builder builder;
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (int i = 0; i < count; i++) {
2240716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        LayerInfo info;
225842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com        // Legacy "flagsmask" field -- now ignored, remove when we bump version
22648f31bdbb3346e3003581229395022bf44828b13reed@google.com        (void)buffer.readInt();
227842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com
2280716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        info.fPaintBits = buffer.readInt();
2290716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        info.fColorMode = (SkXfermode::Mode)buffer.readInt();
230c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com        buffer.readPoint(&info.fOffset);
2310716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        info.fPostTranslate = buffer.readBool();
23274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        buffer.readPaint(builder.addLayerOnTop(info));
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2349fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed    return builder.detachLooper();
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2364991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
2370f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
2384991b8f23482afc1494fd17647421ce68de53331robertphillips@google.comvoid SkLayerDrawLooper::toString(SkString* str) const {
2394991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->appendf("SkLayerDrawLooper (%d): ", fCount);
2404991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
2414991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    Rec* rec = fRecs;
2424991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    for (int i = 0; i < fCount; i++) {
243842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com        str->appendf("%d: paintBits: (", i);
2444991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        if (0 == rec->fInfo.fPaintBits) {
2454991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            str->append("None");
2464991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        } else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) {
2474991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            str->append("EntirePaint");
2484991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        } else {
2494991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            bool needSeparator = false;
2504991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style",
2514991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
252cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com            SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX",
2534991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
2544991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect",
2554991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
2564991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter",
2574991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
258cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com            SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader",
2594991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
260cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com            SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter",
2614991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
262cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com            SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode",
2634991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                              &needSeparator);
2644991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        }
2654991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->append(") ");
2664991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
2674991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        static const char* gModeStrings[SkXfermode::kLastMode+1] = {
2684991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            "kClear", "kSrc", "kDst", "kSrcOver", "kDstOver", "kSrcIn", "kDstIn",
2694991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            "kSrcOut", "kDstOut", "kSrcATop", "kDstATop", "kXor", "kPlus",
2704991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            "kMultiply", "kScreen", "kOverlay", "kDarken", "kLighten", "kColorDodge",
2714991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            "kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion"
2724991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        };
2734991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
2744991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->appendf("mode: %s ", gModeStrings[rec->fInfo.fColorMode]);
2754991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
2764991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->append("offset: (");
2774991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->appendScalar(rec->fInfo.fOffset.fX);
2784991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->append(", ");
2794991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->appendScalar(rec->fInfo.fOffset.fY);
2804991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->append(") ");
2814991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
2824991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->append("postTranslate: ");
2834991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        if (rec->fInfo.fPostTranslate) {
2844991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            str->append("true ");
2854991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        } else {
2864991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com            str->append("false ");
2874991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        }
2884991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
289791f12e6cf558e0dd1689fbb0a6b5eddac9f42adrobertphillips@google.com        rec->fPaint.toString(str);
2904991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        rec = rec->fNext;
2914991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    }
2924991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com}
2934991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#endif
29474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
29574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgSkLayerDrawLooper::Builder::Builder()
29674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        : fRecs(NULL),
29774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org          fTopRec(NULL),
29874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org          fCount(0) {
29974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org}
30074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
30174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgSkLayerDrawLooper::Builder::~Builder() {
30274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    Rec* rec = fRecs;
30374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    while (rec) {
30474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        Rec* next = rec->fNext;
30574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        SkDELETE(rec);
30674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        rec = next;
30774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    }
30874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org}
30974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
31074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgSkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) {
31174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fCount += 1;
31274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
31374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    Rec* rec = SkNEW(Rec);
31474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    rec->fNext = fRecs;
31574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    rec->fInfo = info;
31674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fRecs = rec;
31774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    if (NULL == fTopRec) {
31874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        fTopRec = rec;
31974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    }
32074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
32174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    return &rec->fPaint;
32274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org}
32374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
32474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgvoid SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) {
32574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    LayerInfo info;
32674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
32774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    info.fOffset.set(dx, dy);
32874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    (void)this->addLayer(info);
32974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org}
33074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
33174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgSkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) {
33274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fCount += 1;
33374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
33474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    Rec* rec = SkNEW(Rec);
33574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    rec->fNext = NULL;
33674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    rec->fInfo = info;
33774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    if (NULL == fRecs) {
33874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        fRecs = rec;
33974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    } else {
34049f085dddff10473b6ebf832a974288300224e60bsalomon        SkASSERT(fTopRec);
34174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        fTopRec->fNext = rec;
34274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    }
34374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fTopRec = rec;
34474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
34574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    return &rec->fPaint;
34674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org}
34774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
34874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgSkLayerDrawLooper* SkLayerDrawLooper::Builder::detachLooper() {
34974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    SkLayerDrawLooper* looper = SkNEW(SkLayerDrawLooper);
35074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    looper->fCount = fCount;
35174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    looper->fRecs = fRecs;
35274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
35374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fCount = 0;
35474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fRecs = NULL;
35574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    fTopRec = NULL;
35674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
35774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    return looper;
35874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org}
359