blob: dd06b410b23e168fbef305ce5af9433a4ba3ccb3 [file] [log] [blame]
msarettedf7fcd2016-04-25 06:40:26 -07001/*
2 * Copyright 2016 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 */
7
8#include "SkCanvas.h"
9#include "SkDrawable.h"
10#include "SkOnce.h"
11#include "SkPictureRecorder.h"
12#include "SkReadBuffer.h"
13#include "SkRect.h"
14#include "SkStream.h"
15#include "SkWriteBuffer.h"
16#include "Test.h"
17
18class IntDrawable : public SkDrawable {
19public:
20 IntDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
21 : fA(a)
22 , fB(b)
23 , fC(c)
24 , fD(d)
25 {}
26
27 void flatten(SkWriteBuffer& buffer) const override {
28 buffer.writeUInt(fA);
29 buffer.writeUInt(fB);
30 buffer.writeUInt(fC);
31 buffer.writeUInt(fD);
32 }
33
34 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
35 uint32_t a = buffer.readUInt();
36 uint32_t b = buffer.readUInt();
37 uint32_t c = buffer.readUInt();
38 uint32_t d = buffer.readUInt();
39 return sk_sp<IntDrawable>(new IntDrawable(a, b, c, d));
40 }
41
42 Factory getFactory() const override { return CreateProc; }
43
44 uint32_t a() const { return fA; }
45 uint32_t b() const { return fB; }
46 uint32_t c() const { return fC; }
47 uint32_t d() const { return fD; }
48
49 const char* getTypeName() const override { return "IntDrawable"; }
50
51protected:
52 SkRect onGetBounds() override { return SkRect::MakeEmpty(); }
53 void onDraw(SkCanvas*) override {}
54
55private:
56 uint32_t fA;
57 uint32_t fB;
58 uint32_t fC;
59 uint32_t fD;
60};
61
62class PaintDrawable : public SkDrawable {
63public:
64 PaintDrawable(const SkPaint& paint)
65 : fPaint(paint)
66 {}
67
68 void flatten(SkWriteBuffer& buffer) const override {
69 buffer.writePaint(fPaint);
70 }
71
72 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
73 SkPaint paint;
74 buffer.readPaint(&paint);
75 return sk_sp<PaintDrawable>(new PaintDrawable(paint));
76 }
77
78 Factory getFactory() const override { return CreateProc; }
79
80 const SkPaint& paint() const { return fPaint; }
81
82protected:
83 SkRect onGetBounds() override { return SkRect::MakeEmpty(); }
84 void onDraw(SkCanvas*) override {}
85
86private:
87 SkPaint fPaint;
88};
89
90class CompoundDrawable : public SkDrawable {
91public:
92 CompoundDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d, const SkPaint& paint)
93 : fIntDrawable(new IntDrawable(a, b, c, d))
94 , fPaintDrawable(new PaintDrawable(paint))
95 {}
96
97 CompoundDrawable(IntDrawable* intDrawable, PaintDrawable* paintDrawable)
98 : fIntDrawable(SkRef(intDrawable))
99 , fPaintDrawable(SkRef(paintDrawable))
100 {}
101
102 void flatten(SkWriteBuffer& buffer) const override {
103 buffer.writeFlattenable(fIntDrawable);
104 buffer.writeFlattenable(fPaintDrawable);
105 }
106
107 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
108 SkAutoTUnref<SkFlattenable> intDrawable(
109 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
110 SkASSERT(intDrawable);
111 SkASSERT(!strcmp("IntDrawable", intDrawable->getTypeName()));
112
113 SkAutoTUnref<SkFlattenable> paintDrawable(
114 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
115 SkASSERT(paintDrawable);
116 SkASSERT(!strcmp("PaintDrawable", paintDrawable->getTypeName()));
117
118 return sk_sp<CompoundDrawable>(new CompoundDrawable((IntDrawable*) intDrawable.get(),
119 (PaintDrawable*) paintDrawable.get()));
120 }
121
122 Factory getFactory() const override { return CreateProc; }
123
124 IntDrawable* intDrawable() const { return fIntDrawable; }
125 PaintDrawable* paintDrawable() const { return fPaintDrawable; }
126
127protected:
128 SkRect onGetBounds() override { return SkRect::MakeEmpty(); }
129 void onDraw(SkCanvas*) override {}
130
131private:
132 SkAutoTUnref<IntDrawable> fIntDrawable;
133 SkAutoTUnref<PaintDrawable> fPaintDrawable;
134};
135
136class RootDrawable : public SkDrawable {
137public:
138 RootDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d, const SkPaint& paint,
139 uint32_t e, uint32_t f, uint32_t g, uint32_t h, SkDrawable* drawable)
140 : fCompoundDrawable(new CompoundDrawable(a, b, c, d, paint))
141 , fIntDrawable(new IntDrawable(e, f, g, h))
142 , fDrawable(SkRef(drawable))
143 {}
144
145 RootDrawable(CompoundDrawable* compoundDrawable, IntDrawable* intDrawable,
146 SkDrawable* drawable)
147 : fCompoundDrawable(SkRef(compoundDrawable))
148 , fIntDrawable(SkRef(intDrawable))
149 , fDrawable(SkRef(drawable))
150 {}
151
152 void flatten(SkWriteBuffer& buffer) const override {
153 buffer.writeFlattenable(fCompoundDrawable);
154 buffer.writeFlattenable(fIntDrawable);
155 buffer.writeFlattenable(fDrawable);
156 }
157
158 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
159 SkAutoTUnref<SkFlattenable> compoundDrawable(
160 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
161 SkASSERT(compoundDrawable);
162 SkASSERT(!strcmp("CompoundDrawable", compoundDrawable->getTypeName()));
163
164 SkAutoTUnref<SkFlattenable> intDrawable(
165 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
166 SkASSERT(intDrawable);
167 SkASSERT(!strcmp("IntDrawable", intDrawable->getTypeName()));
168
169 SkAutoTUnref<SkFlattenable> drawable(
170 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
171 SkASSERT(drawable);
172
173 return sk_sp<RootDrawable>(new RootDrawable((CompoundDrawable*) compoundDrawable.get(),
174 (IntDrawable*) intDrawable.get(),
175 (SkDrawable*) drawable.get()));
176 }
177
178 Factory getFactory() const override { return CreateProc; }
179
180 CompoundDrawable* compoundDrawable() const { return fCompoundDrawable; }
181 IntDrawable* intDrawable() const { return fIntDrawable; }
182 SkDrawable* drawable() const { return fDrawable; }
183
184protected:
185 SkRect onGetBounds() override { return SkRect::MakeEmpty(); }
186 void onDraw(SkCanvas*) override {}
187
188private:
189 SkAutoTUnref<CompoundDrawable> fCompoundDrawable;
190 SkAutoTUnref<IntDrawable> fIntDrawable;
191 SkAutoTUnref<SkDrawable> fDrawable;
192};
193
194static void register_test_drawables() {
195 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(IntDrawable)
196 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(PaintDrawable)
197 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(CompoundDrawable)
198 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(RootDrawable)
199}
200
201static void register_test_drawables_once() {
202 static SkOnce once;
203 once(register_test_drawables);
204}
205
206DEF_TEST(FlattenDrawable, r) {
207 register_test_drawables_once();
208
209 // Create and serialize the test drawable
210 SkAutoTUnref<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4));
211 SkPaint paint;
212 paint.setColor(SK_ColorBLUE);
213 SkAutoTUnref<RootDrawable> root(new RootDrawable(5, 6, 7, 8, paint, 9, 10, 11, 12, drawable));
214 SkWriteBuffer writeBuffer;
215 writeBuffer.writeFlattenable(root);
216
217 // Copy the contents of the write buffer into a read buffer
218 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten());
219 writeBuffer.writeToMemory(data->writable_data());
220 SkReadBuffer readBuffer(data->data(), data->size());
221
222 // Deserialize and verify the drawable
223 SkAutoTUnref<SkDrawable> out((SkDrawable*)
224 readBuffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
225 REPORTER_ASSERT(r, out);
226 REPORTER_ASSERT(r, !strcmp("RootDrawable", out->getTypeName()));
227
228 RootDrawable* rootOut = (RootDrawable*) out.get();
229 REPORTER_ASSERT(r, 5 == rootOut->compoundDrawable()->intDrawable()->a());
230 REPORTER_ASSERT(r, 6 == rootOut->compoundDrawable()->intDrawable()->b());
231 REPORTER_ASSERT(r, 7 == rootOut->compoundDrawable()->intDrawable()->c());
232 REPORTER_ASSERT(r, 8 == rootOut->compoundDrawable()->intDrawable()->d());
233 REPORTER_ASSERT(r, SK_ColorBLUE ==
234 rootOut->compoundDrawable()->paintDrawable()->paint().getColor());
235 REPORTER_ASSERT(r, 9 == rootOut->intDrawable()->a());
236 REPORTER_ASSERT(r, 10 == rootOut->intDrawable()->b());
237 REPORTER_ASSERT(r, 11 == rootOut->intDrawable()->c());
238 REPORTER_ASSERT(r, 12 == rootOut->intDrawable()->d());
239
240 // Note that we can still recognize the generic drawable as an IntDrawable
241 SkDrawable* generic = rootOut->drawable();
242 REPORTER_ASSERT(r, !strcmp("IntDrawable", generic->getTypeName()));
243 IntDrawable* integer = (IntDrawable*) generic;
244 REPORTER_ASSERT(r, 1 == integer->a());
245 REPORTER_ASSERT(r, 2 == integer->b());
246 REPORTER_ASSERT(r, 3 == integer->c());
247 REPORTER_ASSERT(r, 4 == integer->d());
248}
249
250static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) {
251 sk_sp<SkFlattenable> drawable = IntDrawable::CreateProc(buffer);
252 IntDrawable* intDrawable = (IntDrawable*) drawable.get();
253 return sk_sp<IntDrawable>(new IntDrawable(intDrawable->a() + 1, intDrawable->b() + 1,
254 intDrawable->c() + 1, intDrawable->d() + 1));
255}
256
257DEF_TEST(FlattenCustomDrawable, r) {
258 // No need to register the drawables since we will be using a custom proc.
259
260 // Create and serialize the test drawable
261 SkAutoTUnref<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4));
262 SkWriteBuffer writeBuffer;
263 writeBuffer.writeFlattenable(drawable);
264
265 // Copy the contents of the write buffer into a read buffer
266 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten());
267 writeBuffer.writeToMemory(data->writable_data());
268 SkReadBuffer readBuffer(data->data(), data->size());
269
270 // Register a custom factory with the read buffer
271 readBuffer.setCustomFactory(SkString("IntDrawable"), &custom_create_proc);
272
273 // Deserialize and verify the drawable
274 SkAutoTUnref<IntDrawable> out((IntDrawable*)
275 readBuffer.readFlattenable(SkFlattenable::kSkDrawable_Type));
276 REPORTER_ASSERT(r, out);
277 REPORTER_ASSERT(r, 2 == out->a());
278 REPORTER_ASSERT(r, 3 == out->b());
279 REPORTER_ASSERT(r, 4 == out->c());
280 REPORTER_ASSERT(r, 5 == out->d());
281}