blob: c7763b7da320f0cdaccdeaadfb503392fd953486 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.org6806fe82012-10-12 14:41:39 +00007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkLayerDrawLooper_DEFINED
9#define SkLayerDrawLooper_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBlendMode.h"
12#include "include/core/SkDrawLooper.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000016class SK_API SkLayerDrawLooper : public SkDrawLooper {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017public:
Brian Salomond3b65972017-03-22 12:05:03 -040018 ~SkLayerDrawLooper() override;
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000019
reed@google.com0716c632011-04-12 18:32:06 +000020 /**
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.com842292f2014-02-28 15:38:22 +000024 * 0 means ignore the layer's paint... except for fColorMode, which is
25 * always applied.
reed@google.com0716c632011-04-12 18:32:06 +000026 */
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000027 enum Bits {
reed@google.com0716c632011-04-12 18:32:06 +000028 kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings
reed@google.com0716c632011-04-12 18:32:06 +000029 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
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034
Mike Reedd5cdc2c2019-01-04 15:57:33 -050035 // unsupported kTextSkewX_Bit = 1 << 1,
36
reed@google.com84260582011-11-21 16:42:10 +000037 /**
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.
reed@google.com842292f2014-02-28 15:38:22 +000041 * - Color is always computed using the LayerInfo's fColorMode.
reed@google.com84260582011-11-21 16:42:10 +000042 */
tomhudson@google.com1f902872012-06-01 13:15:47 +000043 kEntirePaint_Bits = -1
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000045 };
46 typedef int32_t BitFlags;
reed@google.com0716c632011-04-12 18:32:06 +000047
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);
Mike Reed71fecc32016-11-18 17:19:54 -050055 * 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
reed@google.com0716c632011-04-12 18:32:06 +000058 */
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000059 struct SK_API LayerInfo {
Mike Reedfaba3712016-11-03 14:45:31 -040060 BitFlags fPaintBits;
61 SkBlendMode fColorMode;
62 SkVector fOffset;
63 bool fPostTranslate; //!< applies to fOffset
reed@google.com0716c632011-04-12 18:32:06 +000064
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
Herb Derby73fe7b02017-02-08 15:12:19 -050075 SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc*) const override;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
mtklein36352bf2015-03-25 18:17:31 -070077 bool asABlurShadow(BlurShadowRec* rec) const override;
reed@google.comdaaafa62014-04-29 15:20:16 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079protected:
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000080 SkLayerDrawLooper();
81
mtklein36352bf2015-03-25 18:17:31 -070082 void flatten(SkWriteBuffer&) const override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084private:
Mike Klein4fee3232018-10-18 17:27:16 -040085 SK_FLATTENABLE_HOOKS(SkLayerDrawLooper)
86
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 struct Rec {
88 Rec* fNext;
89 SkPaint fPaint;
reed@google.com0716c632011-04-12 18:32:06 +000090 LayerInfo fInfo;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 };
92 Rec* fRecs;
93 int fCount;
reed@google.com4e2b3d32011-04-07 14:18:59 +000094
95 // state-machine during the init/next cycle
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000096 class LayerDrawLooperContext : public SkDrawLooper::Context {
97 public:
98 explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000099
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000100 protected:
mtklein36352bf2015-03-25 18:17:31 -0700101 bool next(SkCanvas*, SkPaint* paint) override;
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000102
103 private:
104 Rec* fCurrRec;
105
106 static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
107 };
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 typedef SkDrawLooper INHERITED;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000110
111public:
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 */
reed7b380d02016-03-21 13:25:16 -0700141 sk_sp<SkDrawLooper> detach();
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000142
143 private:
144 Rec* fRecs;
145 Rec* fTopRec;
146 int fCount;
147 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148};
149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150#endif