Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 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 "SkReadBuffer.h" | |
| 11 #include "SkRect.h" | |
| 12 #include "SkSerializeDrawable.h" | |
| 13 #include "SkStream.h" | |
| 14 #include "SkWriteBuffer.h" | |
| 15 #include "Test.h" | |
| 16 | |
| 17 class SkIntDrawable : public SkDrawable { | |
| 18 public: | |
| 19 SkIntDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d) | |
| 20 : fA(a) | |
| 21 , fB(b) | |
| 22 , fC(c) | |
| 23 , fD(d) | |
| 24 {} | |
| 25 | |
| 26 void flatten(SkWriteBuffer& buffer) const override { | |
| 27 buffer.writeUInt(fA); | |
| 28 buffer.writeUInt(fB); | |
| 29 buffer.writeUInt(fC); | |
| 30 buffer.writeUInt(fD); | |
| 31 } | |
| 32 | |
| 33 static SkFlattenable* CreateProc(SkReadBuffer& buffer) { | |
| 34 uint32_t a = buffer.readUInt(); | |
| 35 uint32_t b = buffer.readUInt(); | |
| 36 uint32_t c = buffer.readUInt(); | |
| 37 uint32_t d = buffer.readUInt(); | |
| 38 return new SkIntDrawable(a, b, c, d); | |
| 39 } | |
| 40 | |
| 41 Factory getFactory() const override { return CreateProc; } | |
| 42 | |
| 43 uint32_t a() const { return fA; } | |
| 44 uint32_t b() const { return fB; } | |
| 45 uint32_t c() const { return fC; } | |
| 46 uint32_t d() const { return fD; } | |
| 47 | |
| 48 protected: | |
| 49 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | |
| 50 void onDraw(SkCanvas*) override {} | |
| 51 | |
| 52 private: | |
| 53 uint32_t fA; | |
| 54 uint32_t fB; | |
| 55 uint32_t fC; | |
| 56 uint32_t fD; | |
| 57 }; | |
| 58 | |
| 59 class SkPaintDrawable : public SkDrawable { | |
| 60 public: | |
| 61 SkPaintDrawable(const SkPaint& paint) | |
| 62 : fPaint(paint) | |
| 63 {} | |
| 64 | |
| 65 void flatten(SkWriteBuffer& buffer) const override { | |
| 66 buffer.writePaint(fPaint); | |
| 67 } | |
| 68 | |
| 69 static SkFlattenable* CreateProc(SkReadBuffer& buffer) { | |
| 70 SkPaint paint; | |
| 71 buffer.readPaint(&paint); | |
| 72 return new SkPaintDrawable(paint); | |
| 73 } | |
| 74 | |
| 75 Factory getFactory() const override { return CreateProc; } | |
| 76 | |
| 77 const SkPaint& paint() const { return fPaint; } | |
| 78 | |
| 79 protected: | |
| 80 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | |
| 81 void onDraw(SkCanvas*) override {} | |
| 82 | |
| 83 private: | |
| 84 SkPaint fPaint; | |
| 85 }; | |
| 86 | |
| 87 class SkCompoundDrawable : public SkDrawable { | |
| 88 public: | |
| 89 SkCompoundDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d, const SkP aint& paint) | |
| 90 : fIntDrawable(new SkIntDrawable(a, b, c, d)) | |
| 91 , fPaintDrawable(new SkPaintDrawable(paint)) | |
| 92 {} | |
| 93 | |
| 94 SkCompoundDrawable(SkIntDrawable* intDrawable, SkPaintDrawable* paintDrawabl e) | |
| 95 : fIntDrawable(SkRef(intDrawable)) | |
| 96 , fPaintDrawable(SkRef(paintDrawable)) | |
| 97 {} | |
| 98 | |
| 99 void flatten(SkWriteBuffer& buffer) const override { | |
| 100 buffer.writeFlattenable(fIntDrawable); | |
| 101 buffer.writeFlattenable(fPaintDrawable); | |
| 102 } | |
| 103 | |
| 104 static SkFlattenable* CreateProc(SkReadBuffer& buffer) { | |
| 105 SkAutoTUnref<SkFlattenable> intDrawable( | |
| 106 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | |
| 107 SkASSERT(intDrawable); | |
| 108 SkASSERT(!strcmp("SkIntDrawable", intDrawable->getTypeName())); | |
| 109 | |
| 110 SkAutoTUnref<SkFlattenable> paintDrawable( | |
| 111 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | |
| 112 SkASSERT(paintDrawable); | |
| 113 SkASSERT(!strcmp("SkPaintDrawable", paintDrawable->getTypeName())); | |
| 114 | |
| 115 return new SkCompoundDrawable((SkIntDrawable*) intDrawable.get(), | |
| 116 (SkPaintDrawable*) paintDrawable.get()); | |
| 117 } | |
| 118 | |
| 119 Factory getFactory() const override { return CreateProc; } | |
| 120 | |
| 121 SkIntDrawable* intDrawable() const { return fIntDrawable; } | |
| 122 SkPaintDrawable* paintDrawable() const { return fPaintDrawable; } | |
| 123 | |
| 124 protected: | |
| 125 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | |
| 126 void onDraw(SkCanvas*) override {} | |
| 127 | |
| 128 private: | |
| 129 SkAutoTUnref<SkIntDrawable> fIntDrawable; | |
| 130 SkAutoTUnref<SkPaintDrawable> fPaintDrawable; | |
| 131 }; | |
| 132 | |
| 133 class SkRootDrawable : public SkDrawable { | |
| 134 public: | |
| 135 SkRootDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d, const SkPaint & paint, | |
| 136 uint32_t e, uint32_t f, uint32_t g, uint32_t h, SkDrawable* d rawable) | |
| 137 : fCompoundDrawable(new SkCompoundDrawable(a, b, c, d, paint)) | |
| 138 , fIntDrawable(new SkIntDrawable(e, f, g, h)) | |
| 139 , fDrawable(SkRef(drawable)) | |
| 140 {} | |
| 141 | |
| 142 SkRootDrawable(SkCompoundDrawable* compoundDrawable, SkIntDrawable* intDrawa ble, | |
| 143 SkDrawable* drawable) | |
| 144 : fCompoundDrawable(SkRef(compoundDrawable)) | |
| 145 , fIntDrawable(SkRef(intDrawable)) | |
| 146 , fDrawable(SkRef(drawable)) | |
| 147 {} | |
| 148 | |
| 149 void flatten(SkWriteBuffer& buffer) const override { | |
| 150 buffer.writeFlattenable(fCompoundDrawable); | |
| 151 buffer.writeFlattenable(fIntDrawable); | |
| 152 buffer.writeFlattenable(fDrawable); | |
| 153 } | |
| 154 | |
| 155 static SkFlattenable* CreateProc(SkReadBuffer& buffer) { | |
| 156 SkAutoTUnref<SkFlattenable> compoundDrawable( | |
| 157 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | |
| 158 SkASSERT(compoundDrawable); | |
| 159 SkASSERT(!strcmp("SkCompoundDrawable", compoundDrawable->getTypeName())) ; | |
| 160 | |
| 161 SkAutoTUnref<SkFlattenable> intDrawable( | |
| 162 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | |
| 163 SkASSERT(intDrawable); | |
| 164 SkASSERT(!strcmp("SkIntDrawable", intDrawable->getTypeName())); | |
| 165 | |
| 166 SkAutoTUnref<SkFlattenable> drawable( | |
| 167 buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); | |
| 168 SkASSERT(drawable); | |
| 169 | |
| 170 return new SkRootDrawable((SkCompoundDrawable*) compoundDrawable.get(), | |
| 171 (SkIntDrawable*) intDrawable.get(), | |
| 172 (SkDrawable*) drawable.get()); | |
| 173 } | |
| 174 | |
| 175 Factory getFactory() const override { return CreateProc; } | |
| 176 | |
| 177 SkCompoundDrawable* compoundDrawable() const { return fCompoundDrawable; } | |
| 178 SkIntDrawable* intDrawable() const { return fIntDrawable; } | |
| 179 SkDrawable* drawable() const { return fDrawable; } | |
| 180 | |
| 181 protected: | |
| 182 SkRect onGetBounds() override { return SkRect::MakeEmpty(); } | |
| 183 void onDraw(SkCanvas*) override {} | |
| 184 | |
| 185 private: | |
| 186 SkAutoTUnref<SkCompoundDrawable> fCompoundDrawable; | |
| 187 SkAutoTUnref<SkIntDrawable> fIntDrawable; | |
| 188 SkAutoTUnref<SkDrawable> fDrawable; | |
| 189 }; | |
| 190 | |
| 191 DEF_TEST(SerializeDrawable, r) { | |
| 192 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkIntDrawable) | |
|
msarett
2016/03/29 15:29:18
Still need to think about how to register drawable
| |
| 193 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPaintDrawable) | |
| 194 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkCompoundDrawable) | |
| 195 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRootDrawable) | |
| 196 | |
| 197 // Create and serialize the test drawable | |
| 198 SkAutoTUnref<SkDrawable> drawable(new SkIntDrawable(1, 2, 3, 4)); | |
| 199 SkPaint paint; | |
| 200 paint.setColor(SK_ColorBLUE); | |
| 201 SkAutoTUnref<SkRootDrawable> rootDrawable(new SkRootDrawable(5, 6, 7, 8, pai nt, 9, 10, 11, 12, | |
| 202 drawable)); | |
| 203 SkAutoTDelete<SkWriteBuffer> writeBuffer(SkSerializeDrawable::Serialize(*roo tDrawable)); | |
| 204 | |
| 205 // Copy the contents of the write buffer into a read buffer | |
| 206 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer->bytesWritten()); | |
| 207 writeBuffer->writeToMemory(data->writable_data()); | |
| 208 SkReadBuffer readBuffer(data->data(), data->size()); | |
| 209 | |
| 210 // Deserialize and verify the drawable | |
| 211 SkAutoTUnref<SkDrawable> out(SkSerializeDrawable::NewFromSerialized(readBuff er)); | |
| 212 REPORTER_ASSERT(r, out); | |
| 213 REPORTER_ASSERT(r, !strcmp("SkRootDrawable", out->getTypeName())); | |
| 214 | |
| 215 SkRootDrawable* root = (SkRootDrawable*) out.get(); | |
| 216 REPORTER_ASSERT(r, 5 == root->compoundDrawable()->intDrawable()->a()); | |
| 217 REPORTER_ASSERT(r, 6 == root->compoundDrawable()->intDrawable()->b()); | |
| 218 REPORTER_ASSERT(r, 7 == root->compoundDrawable()->intDrawable()->c()); | |
| 219 REPORTER_ASSERT(r, 8 == root->compoundDrawable()->intDrawable()->d()); | |
| 220 REPORTER_ASSERT(r, SK_ColorBLUE == | |
| 221 root->compoundDrawable()->paintDrawable()->paint().getColor()); | |
| 222 REPORTER_ASSERT(r, 9 == root->intDrawable()->a()); | |
| 223 REPORTER_ASSERT(r, 10 == root->intDrawable()->b()); | |
| 224 REPORTER_ASSERT(r, 11 == root->intDrawable()->c()); | |
| 225 REPORTER_ASSERT(r, 12 == root->intDrawable()->d()); | |
| 226 | |
| 227 // Note that we can still recognize the generic drawable as int drawable | |
| 228 SkDrawable* generic = root->drawable(); | |
| 229 REPORTER_ASSERT(r, !strcmp("SkIntDrawable", generic->getTypeName())); | |
| 230 SkIntDrawable* integer = (SkIntDrawable*) generic; | |
| 231 REPORTER_ASSERT(r, 1 == integer->a()); | |
| 232 REPORTER_ASSERT(r, 2 == integer->b()); | |
| 233 REPORTER_ASSERT(r, 3 == integer->c()); | |
| 234 REPORTER_ASSERT(r, 4 == integer->d()); | |
| 235 } | |
| OLD | NEW |