epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | 6806fe8 | 2012-10-12 14:41:39 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkLayerDrawLooper_DEFINED |
| 9 | #define SkLayerDrawLooper_DEFINED |
| 10 | |
| 11 | #include "SkDrawLooper.h" |
commit-bot@chromium.org | 5da3f22 | 2013-05-14 20:11:23 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
| 13 | #include "SkPoint.h" |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 14 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 16 | class SK_API SkLayerDrawLooper : public SkDrawLooper { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | public: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | virtual ~SkLayerDrawLooper(); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 19 | |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 20 | /** |
| 21 | * Bits specifies which aspects of the layer's paint should replace the |
| 22 | * corresponding aspects on the draw's paint. |
| 23 | * kEntirePaint_Bits means use the layer's paint completely. |
reed@google.com | 842292f | 2014-02-28 15:38:22 +0000 | [diff] [blame] | 24 | * 0 means ignore the layer's paint... except for fColorMode, which is |
| 25 | * always applied. |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 26 | */ |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 27 | enum Bits { |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 28 | kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings |
| 29 | kTextSkewX_Bit = 1 << 1, //!< use this layer's textskewx |
| 30 | kPathEffect_Bit = 1 << 2, //!< use this layer's patheffect |
| 31 | kMaskFilter_Bit = 1 << 3, //!< use this layer's maskfilter |
| 32 | kShader_Bit = 1 << 4, //!< use this layer's shader |
| 33 | kColorFilter_Bit = 1 << 5, //!< use this layer's colorfilter |
| 34 | kXfermode_Bit = 1 << 6, //!< use this layer's xfermode |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | |
reed@google.com | 8426058 | 2011-11-21 16:42:10 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Use the layer's paint entirely, with these exceptions: |
| 38 | * - We never override the draw's paint's text_encoding, since that is |
| 39 | * used to interpret the text/len parameters in draw[Pos]Text. |
reed@google.com | 842292f | 2014-02-28 15:38:22 +0000 | [diff] [blame] | 40 | * - Color is always computed using the LayerInfo's fColorMode. |
reed@google.com | 8426058 | 2011-11-21 16:42:10 +0000 | [diff] [blame] | 41 | */ |
tomhudson@google.com | 1f90287 | 2012-06-01 13:15:47 +0000 | [diff] [blame] | 42 | kEntirePaint_Bits = -1 |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 44 | }; |
| 45 | typedef int32_t BitFlags; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Info for how to apply the layer's paint and offset. |
| 49 | * |
| 50 | * fColorMode controls how we compute the final color for the layer: |
| 51 | * The layer's paint's color is treated as the SRC |
| 52 | * The draw's paint's color is treated as the DST |
| 53 | * final-color = Mode(layers-color, draws-color); |
| 54 | * Any SkXfermode::Mode will work. Two common choices are: |
| 55 | * kSrc_Mode: to use the layer's color, ignoring the draw's |
| 56 | * kDst_Mode: to just keep the draw's color, ignoring the layer's |
| 57 | */ |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 58 | struct SK_API LayerInfo { |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 59 | BitFlags fPaintBits; |
| 60 | SkXfermode::Mode fColorMode; |
| 61 | SkVector fOffset; |
| 62 | bool fPostTranslate; //!< applies to fOffset |
| 63 | |
| 64 | /** |
| 65 | * Initial the LayerInfo. Defaults to settings that will draw the |
| 66 | * layer with no changes: e.g. |
| 67 | * fPaintBits == 0 |
| 68 | * fColorMode == kDst_Mode |
| 69 | * fOffset == (0, 0) |
| 70 | */ |
| 71 | LayerInfo(); |
| 72 | }; |
| 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const override; |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | size_t contextSize() const override { return sizeof(LayerDrawLooperContext); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | bool asABlurShadow(BlurShadowRec* rec) const override; |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 80 | SK_TO_STRING_OVERRIDE() |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 81 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | Factory getFactory() const override { return CreateProc; } |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 83 | static SkFlattenable* CreateProc(SkReadBuffer& buffer); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | protected: |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 86 | SkLayerDrawLooper(); |
| 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void flatten(SkWriteBuffer&) const override; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | private: |
| 91 | struct Rec { |
| 92 | Rec* fNext; |
| 93 | SkPaint fPaint; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 94 | LayerInfo fInfo; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | }; |
| 96 | Rec* fRecs; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 97 | Rec* fTopRec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | int fCount; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 99 | |
| 100 | // state-machine during the init/next cycle |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 101 | class LayerDrawLooperContext : public SkDrawLooper::Context { |
| 102 | public: |
| 103 | explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 105 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 106 | bool next(SkCanvas*, SkPaint* paint) override; |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | Rec* fCurrRec; |
| 110 | |
| 111 | static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&); |
| 112 | }; |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | class MyRegistrar : public SkFlattenable::Registrar { |
| 115 | public: |
| 116 | MyRegistrar(); |
| 117 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 118 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | typedef SkDrawLooper INHERITED; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 120 | |
| 121 | public: |
| 122 | class SK_API Builder { |
| 123 | public: |
| 124 | Builder(); |
| 125 | ~Builder(); |
| 126 | |
| 127 | /** |
| 128 | * Call for each layer you want to add (from top to bottom). |
| 129 | * This returns a paint you can modify, but that ptr is only valid until |
| 130 | * the next call made to addLayer(). |
| 131 | */ |
| 132 | SkPaint* addLayer(const LayerInfo&); |
| 133 | |
| 134 | /** |
| 135 | * This layer will draw with the original paint, at the specified offset |
| 136 | */ |
| 137 | void addLayer(SkScalar dx, SkScalar dy); |
| 138 | |
| 139 | /** |
| 140 | * This layer will with the original paint and no offset. |
| 141 | */ |
| 142 | void addLayer() { this->addLayer(0, 0); } |
| 143 | |
| 144 | /// Similar to addLayer, but adds a layer to the top. |
| 145 | SkPaint* addLayerOnTop(const LayerInfo&); |
| 146 | |
| 147 | /** |
| 148 | * Pass list of layers on to newly built looper and return it. This will |
| 149 | * also reset the builder, so it can be used to build another looper. |
| 150 | */ |
| 151 | SkLayerDrawLooper* detachLooper(); |
| 152 | |
| 153 | private: |
| 154 | Rec* fRecs; |
| 155 | Rec* fTopRec; |
| 156 | int fCount; |
| 157 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | #endif |