blob: 3a3514091feefec9780141d7599edcd95ceda417 [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
11#include "SkDrawLooper.h"
commit-bot@chromium.org5da3f222013-05-14 20:11:23 +000012#include "SkPaint.h"
13#include "SkPoint.h"
Mike Reed71fecc32016-11-18 17:19:54 -050014#include "SkBlendMode.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
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.comfbfcd562012-08-23 18:09:54 +000035
reed@google.com84260582011-11-21 16:42:10 +000036 /**
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.com842292f2014-02-28 15:38:22 +000040 * - Color is always computed using the LayerInfo's fColorMode.
reed@google.com84260582011-11-21 16:42:10 +000041 */
tomhudson@google.com1f902872012-06-01 13:15:47 +000042 kEntirePaint_Bits = -1
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000044 };
45 typedef int32_t BitFlags;
reed@google.com0716c632011-04-12 18:32:06 +000046
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);
Mike Reed71fecc32016-11-18 17:19:54 -050054 * Any SkBlendMode will work. Two common choices are:
55 * kSrc: to use the layer's color, ignoring the draw's
56 * kDst: to just keep the draw's color, ignoring the layer's
reed@google.com0716c632011-04-12 18:32:06 +000057 */
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000058 struct SK_API LayerInfo {
Mike Reedfaba3712016-11-03 14:45:31 -040059 BitFlags fPaintBits;
60 SkBlendMode fColorMode;
61 SkVector fOffset;
62 bool fPostTranslate; //!< applies to fOffset
reed@google.com0716c632011-04-12 18:32:06 +000063
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
Herb Derby73fe7b02017-02-08 15:12:19 -050074 SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc*) const override;
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
mtklein36352bf2015-03-25 18:17:31 -070076 bool asABlurShadow(BlurShadowRec* rec) const override;
reed@google.comdaaafa62014-04-29 15:20:16 +000077
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000078 SK_TO_STRING_OVERRIDE()
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000079
mtklein36352bf2015-03-25 18:17:31 -070080 Factory getFactory() const override { return CreateProc; }
reed60c9b582016-04-03 09:11:13 -070081 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083protected:
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000084 SkLayerDrawLooper();
85
mtklein36352bf2015-03-25 18:17:31 -070086 void flatten(SkWriteBuffer&) const override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088private:
89 struct Rec {
90 Rec* fNext;
91 SkPaint fPaint;
reed@google.com0716c632011-04-12 18:32:06 +000092 LayerInfo fInfo;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 };
94 Rec* fRecs;
95 int fCount;
reed@google.com4e2b3d32011-04-07 14:18:59 +000096
97 // state-machine during the init/next cycle
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000098 class LayerDrawLooperContext : public SkDrawLooper::Context {
99 public:
100 explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000101
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000102 protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 bool next(SkCanvas*, SkPaint* paint) override;
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000104
105 private:
106 Rec* fCurrRec;
107
108 static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
109 };
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 typedef SkDrawLooper INHERITED;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000112
113public:
114 class SK_API Builder {
115 public:
116 Builder();
117 ~Builder();
118
119 /**
120 * Call for each layer you want to add (from top to bottom).
121 * This returns a paint you can modify, but that ptr is only valid until
122 * the next call made to addLayer().
123 */
124 SkPaint* addLayer(const LayerInfo&);
125
126 /**
127 * This layer will draw with the original paint, at the specified offset
128 */
129 void addLayer(SkScalar dx, SkScalar dy);
130
131 /**
132 * This layer will with the original paint and no offset.
133 */
134 void addLayer() { this->addLayer(0, 0); }
135
136 /// Similar to addLayer, but adds a layer to the top.
137 SkPaint* addLayerOnTop(const LayerInfo&);
138
139 /**
140 * Pass list of layers on to newly built looper and return it. This will
141 * also reset the builder, so it can be used to build another looper.
142 */
reed7b380d02016-03-21 13:25:16 -0700143 sk_sp<SkDrawLooper> detach();
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000144
145 private:
146 Rec* fRecs;
147 Rec* fTopRec;
148 int fCount;
149 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150};
151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152#endif