Flutter Engine
The Flutter Engine
Classes | Macros | Functions | Variables
FlattenDrawableTest.cpp File Reference
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkData.h"
#include "include/core/SkDrawable.h"
#include "include/core/SkFlattenable.h"
#include "include/core/SkFont.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkMaskFilter.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkTypes.h"
#include "src/core/SkPathEffectBase.h"
#include "src/core/SkReadBuffer.h"
#include "src/core/SkWriteBuffer.h"
#include "src/effects/colorfilters/SkColorFilterBase.h"
#include "src/shaders/SkShaderBase.h"
#include "tests/Test.h"
#include "tools/fonts/FontToolUtils.h"
#include <cstdint>
#include <cstring>

Go to the source code of this file.

Classes

class  IntDrawable
 
class  PaintDrawable
 
class  CompoundDrawable
 
class  RootDrawable
 
struct  Initializer
 

Macros

#define test(name)   REPORTER_ASSERT(reporter, !name::Deserialize(data->data(), data->size()))
 

Functions

 DEF_TEST (FlattenDrawable, r)
 
 DEF_TEST (FlattenRecordedDrawable, r)
 
 DEF_TEST (Flattenable_EmptyDeserialze, reporter)
 

Variables

static struct Initializer initializer
 

Macro Definition Documentation

◆ test

#define test (   name)    REPORTER_ASSERT(reporter, !name::Deserialize(data->data(), data->size()))

Function Documentation

◆ DEF_TEST() [1/3]

DEF_TEST ( Flattenable_EmptyDeserialze  ,
reporter   
)

Definition at line 302 of file FlattenDrawableTest.cpp.

302 {
303 auto data = SkData::MakeEmpty();
304
305 #define test(name) REPORTER_ASSERT(reporter, !name::Deserialize(data->data(), data->size()))
308 test(SkShaderBase); // todo: make this just be shader!
311 #undef test
312}
#define test(name)
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ DEF_TEST() [2/3]

DEF_TEST ( FlattenDrawable  ,
 
)

Definition at line 222 of file FlattenDrawableTest.cpp.

222 {
223 // Create and serialize the test drawable
224 sk_sp<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4));
226 paint.setColor(SK_ColorBLUE);
227 sk_sp<RootDrawable> root(new RootDrawable(5, 6, 7, 8, paint, 9, 10, 11, 12, drawable.get()));
228 SkBinaryWriteBuffer writeBuffer({});
229 writeBuffer.writeFlattenable(root.get());
230
231 // Copy the contents of the write buffer into a read buffer
232 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten());
233 writeBuffer.writeToMemory(data->writable_data());
234 SkReadBuffer readBuffer(data->data(), data->size());
235
236 // Deserialize and verify the drawable
239 REPORTER_ASSERT(r, !strcmp("RootDrawable", out->getTypeName()));
240
241 RootDrawable* rootOut = (RootDrawable*) out.get();
242 REPORTER_ASSERT(r, 5 == rootOut->compoundDrawable()->intDrawable()->a());
243 REPORTER_ASSERT(r, 6 == rootOut->compoundDrawable()->intDrawable()->b());
244 REPORTER_ASSERT(r, 7 == rootOut->compoundDrawable()->intDrawable()->c());
245 REPORTER_ASSERT(r, 8 == rootOut->compoundDrawable()->intDrawable()->d());
247 rootOut->compoundDrawable()->paintDrawable()->paint().getColor());
248 REPORTER_ASSERT(r, 9 == rootOut->intDrawable()->a());
249 REPORTER_ASSERT(r, 10 == rootOut->intDrawable()->b());
250 REPORTER_ASSERT(r, 11 == rootOut->intDrawable()->c());
251 REPORTER_ASSERT(r, 12 == rootOut->intDrawable()->d());
252
253 // Note that we can still recognize the generic drawable as an IntDrawable
254 SkDrawable* generic = rootOut->drawable();
255 REPORTER_ASSERT(r, !strcmp("IntDrawable", generic->getTypeName()));
256 IntDrawable* integer = (IntDrawable*) generic;
257 REPORTER_ASSERT(r, 1 == integer->a());
258 REPORTER_ASSERT(r, 2 == integer->b());
259 REPORTER_ASSERT(r, 3 == integer->c());
260 REPORTER_ASSERT(r, 4 == integer->d());
261}
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
PaintDrawable * paintDrawable() const
IntDrawable * intDrawable() const
uint32_t b() const
uint32_t d() const
uint32_t a() const
uint32_t c() const
const SkPaint & paint() const
const sk_sp< Effect > & get() const
Definition: RefCntTest.cpp:125
SkDrawable * drawable() const
CompoundDrawable * compoundDrawable() const
IntDrawable * intDrawable() const
void writeFlattenable(const SkFlattenable *flattenable) override
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116
SkColor getColor() const
Definition: SkPaint.h:225
const Paint & paint
Definition: color_source.cc:38
string root
Definition: scale_cpu.py:20

◆ DEF_TEST() [3/3]

DEF_TEST ( FlattenRecordedDrawable  ,
 
)

Definition at line 263 of file FlattenDrawableTest.cpp.

263 {
264 // Record a set of canvas draw commands
265 SkPictureRecorder recorder;
266 SkCanvas* canvas = recorder.beginRecording(1000.0f, 1000.0f);
268 paint.setColor(SK_ColorGREEN);
269 canvas->drawPoint(42.0f, 17.0f, paint);
270 paint.setColor(SK_ColorRED);
271 canvas->drawPaint(paint);
272 SkPaint textPaint;
273 textPaint.setColor(SK_ColorBLUE);
274 canvas->drawString("TEXT", 467.0f, 100.0f, ToolUtils::DefaultFont(), textPaint);
275
276 // Draw some drawables as well
277 sk_sp<SkDrawable> drawable(new IntDrawable(1, 2, 3, 4));
278 sk_sp<RootDrawable> root(new RootDrawable(5, 6, 7, 8, paint, 9, 10, 11, 12, drawable.get()));
279 canvas->drawDrawable(root.get(), 747.0f, 242.0f);
280 sk_sp<PaintDrawable> paintDrawable(new PaintDrawable(paint));
281 canvas->drawDrawable(paintDrawable.get(), 500.0, 500.0f);
282 sk_sp<CompoundDrawable> comDrawable(new CompoundDrawable(13, 14, 15, 16, textPaint));
283 canvas->drawDrawable(comDrawable.get(), 10.0f, 10.0f);
284
285 // Serialize the recorded drawable
286 sk_sp<SkDrawable> recordedDrawable = recorder.finishRecordingAsDrawable();
287 SkBinaryWriteBuffer writeBuffer({});
288 writeBuffer.writeFlattenable(recordedDrawable.get());
289
290 // Copy the contents of the write buffer into a read buffer
291 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten());
292 writeBuffer.writeToMemory(data->writable_data());
293 SkReadBuffer readBuffer(data->data(), data->size());
294
295 // Deserialize and verify the drawable
298 REPORTER_ASSERT(r, !strcmp("SkRecordedDrawable", out->getTypeName()));
299}
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2695
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
Definition: SkCanvas.cpp:2574
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
void setColor(SkColor color)
Definition: SkPaint.cpp:119
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkDrawable > finishRecordingAsDrawable()
T * get() const
Definition: SkRefCnt.h:303
SkFont DefaultFont()

Variable Documentation

◆ initializer

struct Initializer initializer
static