blob: d93d08ad7e563462427bfed4f431ea3259359697 [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"
reed@google.com0716c632011-04-12 18:32:06 +000014#include "SkXfermode.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:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000018 SK_DECLARE_INST_COUNT(SkLayerDrawLooper)
19
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 virtual ~SkLayerDrawLooper();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000021
reed@google.com0716c632011-04-12 18:32:06 +000022 /**
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.com842292f2014-02-28 15:38:22 +000026 * 0 means ignore the layer's paint... except for fColorMode, which is
27 * always applied.
reed@google.com0716c632011-04-12 18:32:06 +000028 */
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000029 enum Bits {
reed@google.com0716c632011-04-12 18:32:06 +000030 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.comfbfcd562012-08-23 18:09:54 +000037
reed@google.com84260582011-11-21 16:42:10 +000038 /**
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.com842292f2014-02-28 15:38:22 +000042 * - Color is always computed using the LayerInfo's fColorMode.
reed@google.com84260582011-11-21 16:42:10 +000043 */
tomhudson@google.com1f902872012-06-01 13:15:47 +000044 kEntirePaint_Bits = -1
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000046 };
47 typedef int32_t BitFlags;
reed@google.com0716c632011-04-12 18:32:06 +000048
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.com8c3ff172011-04-15 15:42:24 +000060 struct SK_API LayerInfo {
reed@google.com0716c632011-04-12 18:32:06 +000061 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
mtklein36352bf2015-03-25 18:17:31 -070076 SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const override;
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000077
mtklein36352bf2015-03-25 18:17:31 -070078 size_t contextSize() const override { return sizeof(LayerDrawLooperContext); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
mtklein36352bf2015-03-25 18:17:31 -070080 bool asABlurShadow(BlurShadowRec* rec) const override;
reed@google.comdaaafa62014-04-29 15:20:16 +000081
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000082 SK_TO_STRING_OVERRIDE()
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000083
mtklein36352bf2015-03-25 18:17:31 -070084 Factory getFactory() const override { return CreateProc; }
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +000085 static SkFlattenable* CreateProc(SkReadBuffer& buffer);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087protected:
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000088 SkLayerDrawLooper();
89
mtklein36352bf2015-03-25 18:17:31 -070090 void flatten(SkWriteBuffer&) const override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092private:
93 struct Rec {
94 Rec* fNext;
95 SkPaint fPaint;
reed@google.com0716c632011-04-12 18:32:06 +000096 LayerInfo fInfo;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 };
98 Rec* fRecs;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000099 Rec* fTopRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 int fCount;
reed@google.com4e2b3d32011-04-07 14:18:59 +0000101
102 // state-machine during the init/next cycle
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000103 class LayerDrawLooperContext : public SkDrawLooper::Context {
104 public:
105 explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000106
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000107 protected:
mtklein36352bf2015-03-25 18:17:31 -0700108 bool next(SkCanvas*, SkPaint* paint) override;
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000109
110 private:
111 Rec* fCurrRec;
112
113 static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
114 };
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 class MyRegistrar : public SkFlattenable::Registrar {
117 public:
118 MyRegistrar();
119 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 typedef SkDrawLooper INHERITED;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000122
123public:
124 class SK_API Builder {
125 public:
126 Builder();
127 ~Builder();
128
129 /**
130 * Call for each layer you want to add (from top to bottom).
131 * This returns a paint you can modify, but that ptr is only valid until
132 * the next call made to addLayer().
133 */
134 SkPaint* addLayer(const LayerInfo&);
135
136 /**
137 * This layer will draw with the original paint, at the specified offset
138 */
139 void addLayer(SkScalar dx, SkScalar dy);
140
141 /**
142 * This layer will with the original paint and no offset.
143 */
144 void addLayer() { this->addLayer(0, 0); }
145
146 /// Similar to addLayer, but adds a layer to the top.
147 SkPaint* addLayerOnTop(const LayerInfo&);
148
149 /**
150 * Pass list of layers on to newly built looper and return it. This will
151 * also reset the builder, so it can be used to build another looper.
152 */
153 SkLayerDrawLooper* detachLooper();
154
155 private:
156 Rec* fRecs;
157 Rec* fTopRec;
158 int fCount;
159 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160};
161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162#endif