1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlurDrawLooper.h"
97ce661d19c5cf4484305a1b20c44bd111f129847robertphillips@google.com#include "SkBlurMask.h"     // just for SkBlurMask::ConvertRadiusToSigma
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBlurMaskFilter.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
124991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#include "SkColorFilter.h"
138b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkReadBuffer.h"
148b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkWriteBuffer.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMaskFilter.h"
164991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#include "SkPaint.h"
174991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#include "SkString.h"
184991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#include "SkStringUtils.h"
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
207bd141dce43ea3405bc60c9c84e6f910b851b079skia.committer@gmail.comSkBlurDrawLooper::SkBlurDrawLooper(SkColor color, SkScalar sigma,
217ce661d19c5cf4484305a1b20c44bd111f129847robertphillips@google.com                                   SkScalar dx, SkScalar dy, uint32_t flags) {
227ce661d19c5cf4484305a1b20c44bd111f129847robertphillips@google.com    this->init(sigma, dx, dy, color, flags);
237ce661d19c5cf4484305a1b20c44bd111f129847robertphillips@google.com}
247ce661d19c5cf4484305a1b20c44bd111f129847robertphillips@google.com
25daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com// only call from constructor
26daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.comvoid SkBlurDrawLooper::initEffects() {
27daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    SkASSERT(fBlurFlags <= kAll_BlurFlag);
28daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (fSigma > 0) {
29daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        uint32_t flags = fBlurFlags & kIgnoreTransform_BlurFlag ?
30daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com                            SkBlurMaskFilter::kIgnoreTransform_BlurFlag :
31daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com                            SkBlurMaskFilter::kNone_BlurFlag;
32eeaeafebdeec2c546134b741ab8b3c6b7c5190abskia.committer@gmail.com
33daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        flags |= fBlurFlags & kHighQuality_BlurFlag ?
34daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com                    SkBlurMaskFilter::kHighQuality_BlurFlag :
35daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com                    SkBlurMaskFilter::kNone_BlurFlag;
36eeaeafebdeec2c546134b741ab8b3c6b7c5190abskia.committer@gmail.com
37daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        fBlur = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, fSigma, flags);
384e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    } else {
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fBlur = NULL;
404868e6b221a4a98e40f977851af5fcf09631ea15senorblanco@chromium.org    }
41eeaeafebdeec2c546134b741ab8b3c6b7c5190abskia.committer@gmail.com
42daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (fBlurFlags & kOverrideColor_BlurFlag) {
43a6398911174d5445456ecb2f5f4f0565db2f100bjunov@google.com        // Set alpha to 1 for the override since transparency will already
44a6398911174d5445456ecb2f5f4f0565db2f100bjunov@google.com        // be baked into the blurred mask.
45daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkColor opaqueColor = SkColorSetA(fBlurColor, 255);
464868e6b221a4a98e40f977851af5fcf09631ea15senorblanco@chromium.org        //The SrcIn xfer mode will multiply 'color' by the incoming alpha
474e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com        fColorFilter = SkColorFilter::CreateModeFilter(opaqueColor,
484e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com                                                       SkXfermode::kSrcIn_Mode);
494e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    } else {
504868e6b221a4a98e40f977851af5fcf09631ea15senorblanco@chromium.org        fColorFilter = NULL;
514868e6b221a4a98e40f977851af5fcf09631ea15senorblanco@chromium.org    }
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
54daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.comvoid SkBlurDrawLooper::init(SkScalar sigma, SkScalar dx, SkScalar dy,
55daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com                            SkColor color, uint32_t flags) {
56daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    fSigma = sigma;
57daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    fDx = dx;
58daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    fDy = dy;
59daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    fBlurColor = color;
60daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    fBlurFlags = flags;
61daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
62daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    this->initEffects();
63daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com}
64daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
65daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.comSkBlurDrawLooper::SkBlurDrawLooper(SkReadBuffer& buffer) : INHERITED(buffer) {
666bac947cd5bc460dd9166ada6310d678fd2e39f8reed@google.com
67daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    fSigma = buffer.readScalar();
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDx = buffer.readScalar();
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDy = buffer.readScalar();
70c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    fBlurColor = buffer.readColor();
71c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    fBlurFlags = buffer.readUInt() & kAll_BlurFlag;
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
73daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    this->initEffects();
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
768b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgvoid SkBlurDrawLooper::flatten(SkWriteBuffer& buffer) const {
7754924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com    this->INHERITED::flatten(buffer);
78daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    buffer.writeScalar(fSigma);
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    buffer.writeScalar(fDx);
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    buffer.writeScalar(fDy);
81c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    buffer.writeColor(fBlurColor);
82daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    buffer.write32(fBlurFlags);
83daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com}
84daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
85daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.comSkBlurDrawLooper::~SkBlurDrawLooper() {
86daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    SkSafeUnref(fBlur);
87daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    SkSafeUnref(fColorFilter);
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
90daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.combool SkBlurDrawLooper::asABlurShadow(BlurShadowRec* rec) const {
91daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (fSigma <= 0 || (fBlurFlags & fBlurFlags & kIgnoreTransform_BlurFlag)) {
92daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        return false;
93daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
94daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
95daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    if (rec) {
96daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        rec->fSigma = fSigma;
97daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        rec->fColor = fBlurColor;
98daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        rec->fOffset.set(fDx, fDy);
99daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        rec->fStyle = kNormal_SkBlurStyle;
100daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        rec->fQuality = (fBlurFlags & kHighQuality_BlurFlag) ?
101daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com                        kHigh_SkBlurQuality : kLow_SkBlurQuality;
102daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    }
103daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    return true;
104daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com}
105daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
106daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com////////////////////////////////////////////////////////////////////////////////////////
107daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
10879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgSkDrawLooper::Context* SkBlurDrawLooper::createContext(SkCanvas*, void* storage) const {
10979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    return SkNEW_PLACEMENT_ARGS(storage, BlurDrawLooperContext, (this));
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgSkBlurDrawLooper::BlurDrawLooperContext::BlurDrawLooperContext(
11379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        const SkBlurDrawLooper* looper)
11479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    : fLooper(looper), fState(SkBlurDrawLooper::kBeforeEdge) {}
11579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
11679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgbool SkBlurDrawLooper::BlurDrawLooperContext::next(SkCanvas* canvas,
11779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org                                                   SkPaint* paint) {
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (fState) {
1194e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com        case kBeforeEdge:
1204e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            // we do nothing if a maskfilter is already installed
1214e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            if (paint->getMaskFilter()) {
1224e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com                fState = kDone;
1234e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com                return false;
1244e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            }
12556c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
126f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com            SkColor blurColor;
12779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org            blurColor = fLooper->fBlurColor;
128f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com            if (SkColorGetA(blurColor) == 255) {
129f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com                blurColor = SkColorSetA(blurColor, paint->getAlpha());
130f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com            }
131f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com            paint->setColor(blurColor);
132f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com#else
13379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org            paint->setColor(fLooper->fBlurColor);
134f5dbe2f00f853c6a1719924bdd0c33335a53423adjsollen@google.com#endif
13579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org            paint->setMaskFilter(fLooper->fBlur);
13679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org            paint->setColorFilter(fLooper->fColorFilter);
13774e608cdc1e13593b54d54f11e65c1c309d19370commit-bot@chromium.org            canvas->save();
13879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org            if (fLooper->fBlurFlags & kIgnoreTransform_BlurFlag) {
1394e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com                SkMatrix transform(canvas->getTotalMatrix());
14079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org                transform.postTranslate(fLooper->fDx, fLooper->fDy);
1414e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com                canvas->setMatrix(transform);
1424e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            } else {
14379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org                canvas->translate(fLooper->fDx, fLooper->fDy);
1444e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            }
1454e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            fState = kAfterEdge;
1464e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            return true;
1474e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com        case kAfterEdge:
1484e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            canvas->restore();
1494e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            fState = kDone;
1504e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            return true;
1514e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com        default:
1524e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            SkASSERT(kDone == fState);
1534e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com            return false;
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1564991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1570f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
1584991b8f23482afc1494fd17647421ce68de53331robertphillips@google.comvoid SkBlurDrawLooper::toString(SkString* str) const {
1594991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->append("SkBlurDrawLooper: ");
1604991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1614991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->append("dx: ");
1624991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->appendScalar(fDx);
1634991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1644991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->append(" dy: ");
1654991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->appendScalar(fDy);
1664991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1674991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->append(" color: ");
1684991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->appendHex(fBlurColor);
1694991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1704991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->append(" flags: (");
1714991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    if (kNone_BlurFlag == fBlurFlags) {
1724991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        str->append("None");
1734991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    } else {
1744991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        bool needsSeparator = false;
175cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com        SkAddFlagToString(str, SkToBool(kIgnoreTransform_BlurFlag & fBlurFlags), "IgnoreTransform",
1764991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                          &needsSeparator);
1774991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com        SkAddFlagToString(str, SkToBool(kOverrideColor_BlurFlag & fBlurFlags), "OverrideColor",
1784991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                          &needsSeparator);
179cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com        SkAddFlagToString(str, SkToBool(kHighQuality_BlurFlag & fBlurFlags), "HighQuality",
1804991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com                          &needsSeparator);
1814991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    }
1824991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    str->append(")");
1834991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1844991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    // TODO: add optional "fBlurFilter->toString(str);" when SkMaskFilter::toString is added
1854991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com    // alternatively we could cache the radius in SkBlurDrawLooper and just add it here
1864991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com}
1874991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com#endif
188