| 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 | */ |
| 7 | |
| 8 | #ifndef SkLayerDrawLooper_DEFINED |
| 9 | #define SkLayerDrawLooper_DEFINED |
| 10 | |
| 11 | #include "include/core/SkBlendMode.h" |
| 12 | #include "include/core/SkDrawLooper.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | |
| 16 | class SK_API SkLayerDrawLooper : public SkDrawLooper { |
| 17 | public: |
| 18 | ~SkLayerDrawLooper() override; |
| 19 | |
| 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. |
| 24 | * 0 means ignore the layer's paint... except for fColorMode, which is |
| 25 | * always applied. |
| 26 | */ |
| 27 | enum Bits { |
| 28 | kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings |
| 29 | kPathEffect_Bit = 1 << 2, //!< use this layer's patheffect |
| 30 | kMaskFilter_Bit = 1 << 3, //!< use this layer's maskfilter |
| 31 | kShader_Bit = 1 << 4, //!< use this layer's shader |
| 32 | kColorFilter_Bit = 1 << 5, //!< use this layer's colorfilter |
| 33 | kXfermode_Bit = 1 << 6, //!< use this layer's xfermode |
| 34 | |
| 35 | // unsupported kTextSkewX_Bit = 1 << 1, |
| 36 | |
| 37 | /** |
| 38 | * Use the layer's paint entirely, with these exceptions: |
| 39 | * - We never override the draw's paint's text_encoding, since that is |
| 40 | * used to interpret the text/len parameters in draw[Pos]Text. |
| 41 | * - Color is always computed using the LayerInfo's fColorMode. |
| 42 | */ |
| 43 | kEntirePaint_Bits = -1 |
| 44 | |
| 45 | }; |
| 46 | typedef int32_t BitFlags; |
| 47 | |
| 48 | /** |
| 49 | * Info for how to apply the layer's paint and offset. |
| 50 | * |
| 51 | * fColorMode controls how we compute the final color for the layer: |
| 52 | * The layer's paint's color is treated as the SRC |
| 53 | * The draw's paint's color is treated as the DST |
| 54 | * final-color = Mode(layers-color, draws-color); |
| 55 | * Any SkBlendMode will work. Two common choices are: |
| 56 | * kSrc: to use the layer's color, ignoring the draw's |
| 57 | * kDst: to just keep the draw's color, ignoring the layer's |
| 58 | */ |
| 59 | struct SK_API LayerInfo { |
| 60 | BitFlags fPaintBits; |
| 61 | SkBlendMode fColorMode; |
| 62 | SkVector fOffset; |
| 63 | bool fPostTranslate; //!< applies to fOffset |
| 64 | |
| 65 | /** |
| 66 | * Initial the LayerInfo. Defaults to settings that will draw the |
| 67 | * layer with no changes: e.g. |
| 68 | * fPaintBits == 0 |
| 69 | * fColorMode == kDst_Mode |
| 70 | * fOffset == (0, 0) |
| 71 | */ |
| 72 | LayerInfo(); |
| 73 | }; |
| 74 | |
| 75 | SkDrawLooper::Context* makeContext(SkArenaAlloc*) const override; |
| 76 | |
| 77 | bool asABlurShadow(BlurShadowRec* rec) const override; |
| 78 | |
| 79 | protected: |
| 80 | SkLayerDrawLooper(); |
| 81 | |
| 82 | void flatten(SkWriteBuffer&) const override; |
| 83 | |
| 84 | private: |
| 85 | SK_FLATTENABLE_HOOKS(SkLayerDrawLooper) |
| 86 | |
| 87 | struct Rec { |
| 88 | Rec* fNext; |
| 89 | SkPaint fPaint; |
| 90 | LayerInfo fInfo; |
| 91 | }; |
| 92 | Rec* fRecs; |
| 93 | int fCount; |
| 94 | |
| 95 | // state-machine during the init/next cycle |
| 96 | class LayerDrawLooperContext : public SkDrawLooper::Context { |
| 97 | public: |
| 98 | explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper); |
| 99 | |
| 100 | protected: |
| 101 | bool next(Info*, SkPaint* paint) override; |
| 102 | |
| 103 | private: |
| 104 | Rec* fCurrRec; |
| 105 | |
| 106 | static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&); |
| 107 | }; |
| 108 | |
| 109 | typedef SkDrawLooper INHERITED; |
| 110 | |
| 111 | public: |
| 112 | class SK_API Builder { |
| 113 | public: |
| 114 | Builder(); |
| 115 | ~Builder(); |
| 116 | |
| 117 | /** |
| 118 | * Call for each layer you want to add (from top to bottom). |
| 119 | * This returns a paint you can modify, but that ptr is only valid until |
| 120 | * the next call made to addLayer(). |
| 121 | */ |
| 122 | SkPaint* addLayer(const LayerInfo&); |
| 123 | |
| 124 | /** |
| 125 | * This layer will draw with the original paint, at the specified offset |
| 126 | */ |
| 127 | void addLayer(SkScalar dx, SkScalar dy); |
| 128 | |
| 129 | /** |
| 130 | * This layer will with the original paint and no offset. |
| 131 | */ |
| 132 | void addLayer() { this->addLayer(0, 0); } |
| 133 | |
| 134 | /// Similar to addLayer, but adds a layer to the top. |
| 135 | SkPaint* addLayerOnTop(const LayerInfo&); |
| 136 | |
| 137 | /** |
| 138 | * Pass list of layers on to newly built looper and return it. This will |
| 139 | * also reset the builder, so it can be used to build another looper. |
| 140 | */ |
| 141 | sk_sp<SkDrawLooper> detach(); |
| 142 | |
| 143 | private: |
| 144 | Rec* fRecs; |
| 145 | Rec* fTopRec; |
| 146 | int fCount; |
| 147 | }; |
| 148 | }; |
| 149 | |
| 150 | #endif |
| 151 | |