Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: src/core/SkRemote.h

Issue 1409113005: SkRemote: more refactoring (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: forward declares Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dm/DMSrcSink.cpp ('k') | src/core/SkRemote.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkRemote_DEFINED 8 #ifndef SkRemote_DEFINED
9 #define SkRemote_DEFINED 9 #define SkRemote_DEFINED
10 10
11 #include "SkCanvas.h"
12 #include "SkPaint.h" 11 #include "SkPaint.h"
12 #include "SkRegion.h"
13 #include "SkRemote_protocol.h" 13 #include "SkRemote_protocol.h"
14 #include "SkShader.h"
15 #include "SkTHash.h"
16 #include "SkTypes.h" 14 #include "SkTypes.h"
17 15
16 class SkCanvas;
17 class SkMatrix;
18 class SkPath;
19 class SkShader;
20 class SkXfermode;
21
18 // TODO: document 22 // TODO: document
19 23
20 namespace SkRemote { 24 namespace SkRemote {
21 25
22 // General purpose identifier. Holds a Type and a 56-bit value. 26 // General purpose identifier. Holds a Type and a 56-bit value.
23 class ID { 27 class ID {
24 public: 28 public:
25 ID() {} 29 ID() {}
26 ID(Type type, uint64_t val) { 30 ID(Type type, uint64_t val) {
27 fVal = (uint64_t)type << 56 | val; 31 fVal = (uint64_t)type << 56 | val;
(...skipping 26 matching lines...) Expand all
54 SkPaint::Join fJoin; 58 SkPaint::Join fJoin;
55 59
56 static Stroke CreateFrom(const SkPaint&); 60 static Stroke CreateFrom(const SkPaint&);
57 void applyTo(SkPaint*) const; 61 void applyTo(SkPaint*) const;
58 }; 62 };
59 63
60 // TODO: document 64 // TODO: document
61 struct Encoder { 65 struct Encoder {
62 virtual ~Encoder() {} 66 virtual ~Encoder() {}
63 67
64 static Encoder* CreateCachingEncoder(Encoder*);
65
66 virtual ID define(const SkMatrix&) = 0; 68 virtual ID define(const SkMatrix&) = 0;
67 virtual ID define(const Misc&) = 0; 69 virtual ID define(const Misc&) = 0;
68 virtual ID define(const SkPath&) = 0; 70 virtual ID define(const SkPath&) = 0;
69 virtual ID define(const Stroke&) = 0; 71 virtual ID define(const Stroke&) = 0;
70 virtual ID define(SkShader*) = 0; 72 virtual ID define(SkShader*) = 0;
71 virtual ID define(SkXfermode*) = 0; 73 virtual ID define(SkXfermode*) = 0;
72 74
73 virtual void undefine(ID) = 0; 75 virtual void undefine(ID) = 0;
74 76
75 virtual void save() = 0; 77 virtual void save() = 0;
76 virtual void restore() = 0; 78 virtual void restore() = 0;
77 79
78 virtual void setMatrix(ID matrix) = 0; 80 virtual void setMatrix(ID matrix) = 0;
79 81
80 // TODO: struct CommonIDs { ID misc, shader, xfermode; ... } 82 // TODO: struct CommonIDs { ID misc, shader, xfermode; ... }
81 // for IDs that affect both fill + stroke? 83 // for IDs that affect both fill + stroke?
82 84
83 virtual void clipPath(ID path, SkRegion::Op, bool aa) = 0; 85 virtual void clipPath(ID path, SkRegion::Op, bool aa) = 0;
84 virtual void fillPath(ID path, ID misc, ID shader, ID xfermode) = 0; 86 virtual void fillPath(ID path, ID misc, ID shader, ID xfermode) = 0;
85 virtual void strokePath(ID path, ID misc, ID shader, ID xfermode, ID str oke) = 0; 87 virtual void strokePath(ID path, ID misc, ID shader, ID xfermode, ID str oke) = 0;
86 }; 88 };
87 89
88 // An SkCanvas that translates to Encoder calls. 90 // None of these factories take ownership of their arguments.
89 class Client final : public SkCanvas {
90 public:
91 explicit Client(Encoder*);
92 91
93 private: 92 // Returns a new SkCanvas that translates to the Encoder API.
94 class AutoID; 93 SkCanvas* NewCanvas(Encoder*);
95 94 // Returns an Encoder that translates back to the SkCanvas API.
96 template <typename T> 95 Encoder* NewDecoder(SkCanvas*);
97 AutoID id(const T&); 96 // Wraps another Encoder with a cache. TODO: parameterize
98 97 Encoder* NewCachingEncoder(Encoder*);
99 void willSave() override;
100 void didRestore() override;
101
102 void didConcat(const SkMatrix&) override;
103 void didSetMatrix(const SkMatrix&) override;
104
105 void onClipPath (const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
106 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
107 void onClipRect (const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
108
109 void onDrawOval(const SkRect&, const SkPaint&) override;
110 void onDrawRRect(const SkRRect&, const SkPaint&) override;
111 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) overri de;
112 void onDrawPath(const SkPath&, const SkPaint&) override;
113 void onDrawRect(const SkRect&, const SkPaint&) override;
114 void onDrawPaint(const SkPaint&) override;
115
116 void onDrawText(const void*, size_t, SkScalar,
117 SkScalar, const SkPaint&) override;
118 void onDrawPosText(const void*, size_t, const SkPoint[],
119 const SkPaint&) override;
120 void onDrawPosTextH(const void*, size_t, const SkScalar[], SkScalar,
121 const SkPaint&) override;
122
123 Encoder* fEncoder;
124 };
125
126 // An Encoder that translates back to SkCanvas calls.
127 class Server final : public Encoder {
128 public:
129 explicit Server(SkCanvas*);
130
131 private:
132 ID define(const SkMatrix&) override;
133 ID define(const Misc&) override;
134 ID define(const SkPath&) override;
135 ID define(const Stroke&) override;
136 ID define(SkShader*) override;
137 ID define(SkXfermode*) override;
138
139 void undefine(ID) override;
140
141 void save() override;
142 void restore() override;
143
144 void setMatrix(ID matrix) override;
145
146 void clipPath(ID path, SkRegion::Op, bool aa) ove rride;
147 void fillPath(ID path, ID misc, ID shader, ID xfermode) ove rride;
148 void strokePath(ID path, ID misc, ID shader, ID xfermode, ID stroke) ove rride;
149
150 // Maps ID -> T.
151 template <typename T, Type kType>
152 class IDMap {
153 public:
154 ~IDMap() {
155 // A well-behaved client always cleans up its definitions.
156 SkASSERT(fMap.count() == 0);
157 }
158
159 void set(const ID& id, const T& val) {
160 SkASSERT(id.type() == kType);
161 fMap.set(id, val);
162 }
163
164 void remove(const ID& id) {
165 SkASSERT(id.type() == kType);
166 fMap.remove(id);
167 }
168
169 const T& find(const ID& id) const {
170 SkASSERT(id.type() == kType);
171 T* val = fMap.find(id);
172 SkASSERT(val != nullptr);
173 return *val;
174 }
175
176 private:
177 SkTHashMap<ID, T> fMap;
178 };
179
180 // Maps ID -> T*, and keeps the T alive by reffing it.
181 template <typename T, Type kType>
182 class ReffedIDMap {
183 public:
184 ReffedIDMap() {}
185 ~ReffedIDMap() {
186 // A well-behaved client always cleans up its definitions.
187 SkASSERT(fMap.count() == 0);
188 }
189
190 void set(const ID& id, T* val) {
191 SkASSERT(id.type() == kType);
192 fMap.set(id, SkSafeRef(val));
193 }
194
195 void remove(const ID& id) {
196 SkASSERT(id.type() == kType);
197 T** val = fMap.find(id);
198 SkASSERT(val);
199 SkSafeUnref(*val);
200 fMap.remove(id);
201 }
202
203 T* find(const ID& id) const {
204 SkASSERT(id.type() == kType);
205 T** val = fMap.find(id);
206 SkASSERT(val);
207 return *val;
208 }
209
210 private:
211 SkTHashMap<ID, T*> fMap;
212 };
213
214 template <typename Map, typename T>
215 ID define(Type, Map*, const T&);
216
217 IDMap<SkMatrix, Type::kMatrix> fMatrix;
218 IDMap<Misc , Type::kMisc > fMisc;
219 IDMap<SkPath , Type::kPath > fPath;
220 IDMap<Stroke , Type::kStroke> fStroke;
221 ReffedIDMap<SkShader, Type::kShader> fShader;
222 ReffedIDMap<SkXfermode, Type::kXfermode> fXfermode;
223
224 SkCanvas* fCanvas;
225 uint64_t fNextID = 0;
226 };
227 98
228 } // namespace SkRemote 99 } // namespace SkRemote
229 100
230 #endif//SkRemote_DEFINED 101 #endif//SkRemote_DEFINED
OLDNEW
« no previous file with comments | « dm/DMSrcSink.cpp ('k') | src/core/SkRemote.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698