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