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