OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkDrawable.h" | 9 #include "SkDrawable.h" |
10 #include "SkOnce.h" | 10 #include "SkOnce.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) { | 72 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) { |
73 SkPaint paint; | 73 SkPaint paint; |
74 buffer.readPaint(&paint); | 74 buffer.readPaint(&paint); |
75 return sk_sp<PaintDrawable>(new PaintDrawable(paint)); | 75 return sk_sp<PaintDrawable>(new PaintDrawable(paint)); |
76 } | 76 } |
77 | 77 |
78 Factory getFactory() const override { return CreateProc; } | 78 Factory getFactory() const override { return CreateProc; } |
79 | 79 |
80 const SkPaint& paint() const { return fPaint; } | 80 const SkPaint& paint() const { return fPaint; } |
81 | 81 |
82 const char* getTypeName() const override { return "PaintDrawable"; } | |
83 | |
82 protected: | 84 protected: |
83 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | 85 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
84 void onDraw(SkCanvas*) override {} | 86 void onDraw(SkCanvas*) override {} |
85 | 87 |
86 private: | 88 private: |
87 SkPaint fPaint; | 89 SkPaint fPaint; |
88 }; | 90 }; |
89 | 91 |
90 class CompoundDrawable : public SkDrawable { | 92 class CompoundDrawable : public SkDrawable { |
91 public: | 93 public: |
(...skipping 25 matching lines...) Expand all Loading... | |
117 | 119 |
118 return sk_sp<CompoundDrawable>(new CompoundDrawable((IntDrawable*) intDr awable.get(), | 120 return sk_sp<CompoundDrawable>(new CompoundDrawable((IntDrawable*) intDr awable.get(), |
119 (PaintDrawable*) pai ntDrawable.get())); | 121 (PaintDrawable*) pai ntDrawable.get())); |
120 } | 122 } |
121 | 123 |
122 Factory getFactory() const override { return CreateProc; } | 124 Factory getFactory() const override { return CreateProc; } |
123 | 125 |
124 IntDrawable* intDrawable() const { return fIntDrawable; } | 126 IntDrawable* intDrawable() const { return fIntDrawable; } |
125 PaintDrawable* paintDrawable() const { return fPaintDrawable; } | 127 PaintDrawable* paintDrawable() const { return fPaintDrawable; } |
126 | 128 |
129 const char* getTypeName() const override { return "CompoundDrawable"; } | |
130 | |
127 protected: | 131 protected: |
128 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | 132 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
129 void onDraw(SkCanvas*) override {} | 133 void onDraw(SkCanvas*) override {} |
130 | 134 |
131 private: | 135 private: |
132 SkAutoTUnref<IntDrawable> fIntDrawable; | 136 SkAutoTUnref<IntDrawable> fIntDrawable; |
133 SkAutoTUnref<PaintDrawable> fPaintDrawable; | 137 SkAutoTUnref<PaintDrawable> fPaintDrawable; |
134 }; | 138 }; |
135 | 139 |
136 class RootDrawable : public SkDrawable { | 140 class RootDrawable : public SkDrawable { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 (IntDrawable*) intDrawable.g et(), | 178 (IntDrawable*) intDrawable.g et(), |
175 (SkDrawable*) drawable.get() )); | 179 (SkDrawable*) drawable.get() )); |
176 } | 180 } |
177 | 181 |
178 Factory getFactory() const override { return CreateProc; } | 182 Factory getFactory() const override { return CreateProc; } |
179 | 183 |
180 CompoundDrawable* compoundDrawable() const { return fCompoundDrawable; } | 184 CompoundDrawable* compoundDrawable() const { return fCompoundDrawable; } |
181 IntDrawable* intDrawable() const { return fIntDrawable; } | 185 IntDrawable* intDrawable() const { return fIntDrawable; } |
182 SkDrawable* drawable() const { return fDrawable; } | 186 SkDrawable* drawable() const { return fDrawable; } |
183 | 187 |
188 const char* getTypeName() const override { return "RootDrawable"; } | |
189 | |
184 protected: | 190 protected: |
185 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | 191 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
186 void onDraw(SkCanvas*) override {} | 192 void onDraw(SkCanvas*) override {} |
187 | 193 |
188 private: | 194 private: |
189 SkAutoTUnref<CompoundDrawable> fCompoundDrawable; | 195 SkAutoTUnref<CompoundDrawable> fCompoundDrawable; |
190 SkAutoTUnref<IntDrawable> fIntDrawable; | 196 SkAutoTUnref<IntDrawable> fIntDrawable; |
191 SkAutoTUnref<SkDrawable> fDrawable; | 197 SkAutoTUnref<SkDrawable> fDrawable; |
192 }; | 198 }; |
193 | 199 |
194 static void register_test_drawables() { | 200 static void register_test_drawables(SkReadBuffer& buffer) { |
195 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(IntDrawable) | 201 buffer.setCustomFactory(SkString("IntDrawable"), IntDrawable::CreateProc); |
196 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(PaintDrawable) | 202 buffer.setCustomFactory(SkString("PaintDrawable"), PaintDrawable::CreateProc ); |
197 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(CompoundDrawable) | 203 buffer.setCustomFactory(SkString("CompoundDrawable"), CompoundDrawable::Crea teProc); |
198 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(RootDrawable) | 204 buffer.setCustomFactory(SkString("RootDrawable"), RootDrawable::CreateProc); |
199 } | |
200 | |
201 static void register_test_drawables_once() { | |
202 static SkOnce once; | |
203 once(register_test_drawables); | |
204 } | 205 } |
205 | 206 |
206 DEF_TEST(FlattenDrawable, r) { | 207 DEF_TEST(FlattenDrawable, r) { |
207 register_test_drawables_once(); | |
208 | |
209 // Create and serialize the test drawable | 208 // Create and serialize the test drawable |
210 SkAutoTUnref<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4)); | 209 SkAutoTUnref<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4)); |
211 SkPaint paint; | 210 SkPaint paint; |
212 paint.setColor(SK_ColorBLUE); | 211 paint.setColor(SK_ColorBLUE); |
213 SkAutoTUnref<RootDrawable> root(new RootDrawable(5, 6, 7, 8, paint, 9, 10, 1 1, 12, drawable)); | 212 SkAutoTUnref<RootDrawable> root(new RootDrawable(5, 6, 7, 8, paint, 9, 10, 1 1, 12, drawable)); |
214 SkWriteBuffer writeBuffer; | 213 SkWriteBuffer writeBuffer; |
215 writeBuffer.writeFlattenable(root); | 214 writeBuffer.writeFlattenable(root); |
216 | 215 |
217 // Copy the contents of the write buffer into a read buffer | 216 // Copy the contents of the write buffer into a read buffer |
218 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); | 217 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); |
219 writeBuffer.writeToMemory(data->writable_data()); | 218 writeBuffer.writeToMemory(data->writable_data()); |
220 SkReadBuffer readBuffer(data->data(), data->size()); | 219 SkReadBuffer readBuffer(data->data(), data->size()); |
220 register_test_drawables(readBuffer); | |
221 | 221 |
222 // Deserialize and verify the drawable | 222 // Deserialize and verify the drawable |
223 SkAutoTUnref<SkDrawable> out((SkDrawable*) | 223 SkAutoTUnref<SkDrawable> out((SkDrawable*) |
224 readBuffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | 224 readBuffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
225 REPORTER_ASSERT(r, out); | 225 REPORTER_ASSERT(r, out); |
226 REPORTER_ASSERT(r, !strcmp("RootDrawable", out->getTypeName())); | 226 REPORTER_ASSERT(r, !strcmp("RootDrawable", out->getTypeName())); |
227 | 227 |
228 RootDrawable* rootOut = (RootDrawable*) out.get(); | 228 RootDrawable* rootOut = (RootDrawable*) out.get(); |
229 REPORTER_ASSERT(r, 5 == rootOut->compoundDrawable()->intDrawable()->a()); | 229 REPORTER_ASSERT(r, 5 == rootOut->compoundDrawable()->intDrawable()->a()); |
230 REPORTER_ASSERT(r, 6 == rootOut->compoundDrawable()->intDrawable()->b()); | 230 REPORTER_ASSERT(r, 6 == rootOut->compoundDrawable()->intDrawable()->b()); |
(...skipping 16 matching lines...) Expand all Loading... | |
247 REPORTER_ASSERT(r, 4 == integer->d()); | 247 REPORTER_ASSERT(r, 4 == integer->d()); |
248 } | 248 } |
249 | 249 |
250 static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) { | 250 static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) { |
251 sk_sp<SkFlattenable> drawable = IntDrawable::CreateProc(buffer); | 251 sk_sp<SkFlattenable> drawable = IntDrawable::CreateProc(buffer); |
252 IntDrawable* intDrawable = (IntDrawable*) drawable.get(); | 252 IntDrawable* intDrawable = (IntDrawable*) drawable.get(); |
253 return sk_sp<IntDrawable>(new IntDrawable(intDrawable->a() + 1, intDrawable- >b() + 1, | 253 return sk_sp<IntDrawable>(new IntDrawable(intDrawable->a() + 1, intDrawable- >b() + 1, |
254 intDrawable->c() + 1, intDrawable- >d() + 1)); | 254 intDrawable->c() + 1, intDrawable- >d() + 1)); |
255 } | 255 } |
256 | 256 |
257 DEF_TEST(FlattenCustomDrawable, r) { | 257 DEF_TEST(FlattenCustomDrawable, r) { |
mtklein
2016/04/26 15:17:24
Does this make this test moot?
msarett
2016/04/26 15:18:54
Yes... I'll delete this.
| |
258 // No need to register the drawables since we will be using a custom proc. | |
259 | |
260 // Create and serialize the test drawable | 258 // Create and serialize the test drawable |
261 SkAutoTUnref<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4)); | 259 SkAutoTUnref<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4)); |
262 SkWriteBuffer writeBuffer; | 260 SkWriteBuffer writeBuffer; |
263 writeBuffer.writeFlattenable(drawable); | 261 writeBuffer.writeFlattenable(drawable); |
264 | 262 |
265 // Copy the contents of the write buffer into a read buffer | 263 // Copy the contents of the write buffer into a read buffer |
266 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); | 264 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); |
267 writeBuffer.writeToMemory(data->writable_data()); | 265 writeBuffer.writeToMemory(data->writable_data()); |
268 SkReadBuffer readBuffer(data->data(), data->size()); | 266 SkReadBuffer readBuffer(data->data(), data->size()); |
269 | 267 |
270 // Register a custom factory with the read buffer | 268 // Register a custom factory with the read buffer |
271 readBuffer.setCustomFactory(SkString("IntDrawable"), &custom_create_proc); | 269 readBuffer.setCustomFactory(SkString("IntDrawable"), &custom_create_proc); |
272 | 270 |
273 // Deserialize and verify the drawable | 271 // Deserialize and verify the drawable |
274 SkAutoTUnref<IntDrawable> out((IntDrawable*) | 272 SkAutoTUnref<IntDrawable> out((IntDrawable*) |
275 readBuffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | 273 readBuffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
276 REPORTER_ASSERT(r, out); | 274 REPORTER_ASSERT(r, out); |
277 REPORTER_ASSERT(r, 2 == out->a()); | 275 REPORTER_ASSERT(r, 2 == out->a()); |
278 REPORTER_ASSERT(r, 3 == out->b()); | 276 REPORTER_ASSERT(r, 3 == out->b()); |
279 REPORTER_ASSERT(r, 4 == out->c()); | 277 REPORTER_ASSERT(r, 4 == out->c()); |
280 REPORTER_ASSERT(r, 5 == out->d()); | 278 REPORTER_ASSERT(r, 5 == out->d()); |
281 } | 279 } |
OLD | NEW |