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 */
76806fe87e0b39e283291c1a1c7d1d864230aa2aatfarina@chromium.org
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkLayerDrawLooper_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkLayerDrawLooper_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawLooper.h"
125da3f220034f89f3234b16b98bd86e7246941ffecommit-bot@chromium.org#include "SkPaint.h"
135da3f220034f89f3234b16b98bd86e7246941ffecommit-bot@chromium.org#include "SkPoint.h"
140716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com#include "SkXfermode.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168c3ff17e2cab6f7c798b9f8ff4515c4a3d3fd9d1bsalomon@google.comclass SK_API SkLayerDrawLooper : public SkDrawLooper {
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
180456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com    SK_DECLARE_INST_COUNT(SkLayerDrawLooper)
190456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual ~SkLayerDrawLooper();
210e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
220716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    /**
230716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  Bits specifies which aspects of the layer's paint should replace the
240716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  corresponding aspects on the draw's paint.
250716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  kEntirePaint_Bits means use the layer's paint completely.
26842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com     *  0 means ignore the layer's paint... except for fColorMode, which is
27842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com     *  always applied.
280716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     */
290e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    enum Bits {
300716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kStyle_Bit      = 1 << 0,   //!< use this layer's Style/stroke settings
310716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kTextSkewX_Bit  = 1 << 1,   //!< use this layer's textskewx
320716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kPathEffect_Bit = 1 << 2,   //!< use this layer's patheffect
330716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kMaskFilter_Bit = 1 << 3,   //!< use this layer's maskfilter
340716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kShader_Bit     = 1 << 4,   //!< use this layer's shader
350716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kColorFilter_Bit = 1 << 5,  //!< use this layer's colorfilter
360716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kXfermode_Bit   = 1 << 6,   //!< use this layer's xfermode
37fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
388426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com        /**
398426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com         *  Use the layer's paint entirely, with these exceptions:
408426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com         *  - We never override the draw's paint's text_encoding, since that is
418426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com         *    used to interpret the text/len parameters in draw[Pos]Text.
42842292f10fc08bc8701114f7eec00944b6ea26d2reed@google.com         *  - Color is always computed using the LayerInfo's fColorMode.
438426058dee0faf75a18b81cfcde47ee8ab8c31d9reed@google.com         */
441f90287df3129cb267422e482c52ebeca6a8990ftomhudson@google.com        kEntirePaint_Bits = -1
45fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
460e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    };
470e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    typedef int32_t BitFlags;
480716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
490716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    /**
500716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  Info for how to apply the layer's paint and offset.
510716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *
520716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  fColorMode controls how we compute the final color for the layer:
530716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      The layer's paint's color is treated as the SRC
540716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      The draw's paint's color is treated as the DST
550716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      final-color = Mode(layers-color, draws-color);
560716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  Any SkXfermode::Mode will work. Two common choices are:
570716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      kSrc_Mode: to use the layer's color, ignoring the draw's
580716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      kDst_Mode: to just keep the draw's color, ignoring the layer's
590716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     */
608c3ff17e2cab6f7c798b9f8ff4515c4a3d3fd9d1bsalomon@google.com    struct SK_API LayerInfo {
610716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        BitFlags            fPaintBits;
620716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        SkXfermode::Mode    fColorMode;
630716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        SkVector            fOffset;
640716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        bool                fPostTranslate; //!< applies to fOffset
650716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
660716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        /**
670716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *  Initial the LayerInfo. Defaults to settings that will draw the
680716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *  layer with no changes: e.g.
690716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *      fPaintBits == 0
700716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *      fColorMode == kDst_Mode
710716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *      fOffset == (0, 0)
720716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         */
730716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        LayerInfo();
740716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    };
750716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
7679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE;
7779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
7879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    virtual size_t contextSize() const SK_OVERRIDE { return sizeof(LayerDrawLooperContext); }
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
80daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    virtual bool asABlurShadow(BlurShadowRec* rec) const SK_OVERRIDE;
81daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
820f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_OVERRIDE()
8374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
8474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    /// Implements Flattenable.
8574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; }
8674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    static SkFlattenable* CreateProc(SkReadBuffer& buffer);
87fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
8973cb15351f33459e0c861a96135c634dec77ef9dcommit-bot@chromium.org    SkLayerDrawLooper();
9073cb15351f33459e0c861a96135c634dec77ef9dcommit-bot@chromium.org
918b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
92fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    struct Rec {
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Rec*    fNext;
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint fPaint;
970716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        LayerInfo fInfo;
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec*    fRecs;
1008f838259ab65e44562902679fa88cb00575b99cecommit-bot@chromium.org    Rec*    fTopRec;
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int     fCount;
1024e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com
1034e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    // state-machine during the init/next cycle
10479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    class LayerDrawLooperContext : public SkDrawLooper::Context {
10579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    public:
10679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper);
10779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
10879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    protected:
10979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual bool next(SkCanvas*, SkPaint* paint) SK_OVERRIDE;
1100e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
11179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    private:
11279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        Rec* fCurrRec;
11379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
11479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
11579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    };
1160e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class MyRegistrar : public SkFlattenable::Registrar {
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        MyRegistrar();
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
121fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkDrawLooper INHERITED;
12374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
12474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.orgpublic:
12574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    class SK_API Builder {
12674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    public:
12774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        Builder();
12874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        ~Builder();
12974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
13074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        /**
13174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         *  Call for each layer you want to add (from top to bottom).
13274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         *  This returns a paint you can modify, but that ptr is only valid until
13374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         *  the next call made to addLayer().
13474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         */
13574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        SkPaint* addLayer(const LayerInfo&);
13674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
13774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        /**
13874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         *  This layer will draw with the original paint, at the specified offset
13974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         */
14074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        void addLayer(SkScalar dx, SkScalar dy);
14174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
14274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        /**
14374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         *  This layer will with the original paint and no offset.
14474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org         */
14574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        void addLayer() { this->addLayer(0, 0); }
14674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
14774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        /// Similar to addLayer, but adds a layer to the top.
14874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        SkPaint* addLayerOnTop(const LayerInfo&);
14974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
15074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        /**
15174ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org          * Pass list of layers on to newly built looper and return it. This will
15274ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org          * also reset the builder, so it can be used to build another looper.
15374ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org          */
15474ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        SkLayerDrawLooper* detachLooper();
15574ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org
15674ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    private:
15774ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        Rec* fRecs;
15874ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        Rec* fTopRec;
15974ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org        int  fCount;
16074ba2f62dce1998bd6555291ab0a5330c276301dcommit-bot@chromium.org    };
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
164