blob: fd82079147eb602758720ce33fa638c58a7e3de2 [file] [log] [blame]
Mike Reed60691a52017-12-05 15:11:24 -05001/*
2 * Copyright 2017 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 "Test.h"
9#include "Resources.h"
10#include "sk_tool_utils.h"
11#include "SkCanvas.h"
Mike Reed45ab6302017-12-13 11:38:57 -050012#include "SkImageSource.h"
Mike Reed60691a52017-12-05 15:11:24 -050013#include "SkPicture.h"
14#include "SkPictureRecorder.h"
15#include "SkSerialProcs.h"
16#include "SkSurface.h"
17
18static sk_sp<SkImage> picture_to_image(sk_sp<SkPicture> pic) {
19 SkIRect r = pic->cullRect().round();
20 auto surf = SkSurface::MakeRasterN32Premul(r.width(), r.height());
21 surf->getCanvas()->drawPicture(pic);
22 return surf->makeImageSnapshot();
23}
24
25struct State {
26 const char* fStr;
27 SkImage* fImg;
28};
29
30DEF_TEST(serial_procs_image, reporter) {
Hal Canaryc465d132017-12-08 10:21:31 -050031 auto src_img = GetResourceAsImage("images/mandrill_128.png");
Mike Reed60691a52017-12-05 15:11:24 -050032 const char magic_str[] = "magic signature";
33
34 const SkSerialImageProc sprocs[] = {
Mike Reed64f73762017-12-08 10:31:52 -050035 [](SkImage* img, void* ctx) -> sk_sp<SkData> { return nullptr; },
36 [](SkImage* img, void* ctx) { return img->encodeToData(); },
37 [](SkImage* img, void* ctx) { return SkData::MakeWithCString(((State*)ctx)->fStr); },
Mike Reed60691a52017-12-05 15:11:24 -050038 };
39 const SkDeserialImageProc dprocs[] = {
40 [](const void* data, size_t length, void*) -> sk_sp<SkImage> {
Mike Reed60691a52017-12-05 15:11:24 -050041 return nullptr;
42 },
Mike Reed64f73762017-12-08 10:31:52 -050043 [](const void* data, size_t length, void*) {
Mike Reed60691a52017-12-05 15:11:24 -050044 return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length));
45 },
46 [](const void* data, size_t length, void* ctx) -> sk_sp<SkImage> {
47 State* state = (State*)ctx;
Mike Reed64f73762017-12-08 10:31:52 -050048 if (length != strlen(state->fStr)+1 || memcmp(data, state->fStr, length)) {
Mike Reed60691a52017-12-05 15:11:24 -050049 return nullptr;
50 }
51 return sk_ref_sp(state->fImg);
52 },
53 };
54
55 sk_sp<SkPicture> pic;
56 {
57 SkPictureRecorder rec;
58 SkCanvas* canvas = rec.beginRecording(128, 128);
59 canvas->drawImage(src_img, 0, 0, nullptr);
60 pic = rec.finishRecordingAsPicture();
61 }
62
63 State state = { magic_str, src_img.get() };
64
65 SkSerialProcs sproc;
66 sproc.fImageCtx = &state;
67 SkDeserialProcs dproc;
68 dproc.fImageCtx = &state;
69
70 for (size_t i = 0; i < SK_ARRAY_COUNT(sprocs); ++i) {
71 sproc.fImageProc = sprocs[i];
Mike Reed47fdf6c2017-12-20 14:12:07 -050072 auto data = pic->serialize(&sproc);
Mike Reed60691a52017-12-05 15:11:24 -050073 REPORTER_ASSERT(reporter, data);
74
75 dproc.fImageProc = dprocs[i];
Mike Reed47fdf6c2017-12-20 14:12:07 -050076 auto new_pic = SkPicture::MakeFromData(data.get(), &dproc);
Mike Reed60691a52017-12-05 15:11:24 -050077 REPORTER_ASSERT(reporter, data);
78
79 auto dst_img = picture_to_image(new_pic);
80 REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(src_img.get(), dst_img.get()));
81 }
82}
83
Mike Reed45ab6302017-12-13 11:38:57 -050084///////////////////////////////////////////////////////////////////////////////////////////////////
85
86static sk_sp<SkPicture> make_pic(const std::function<void(SkCanvas*)>& drawer) {
87 SkPictureRecorder rec;
88 drawer(rec.beginRecording(128, 128));
89 return rec.finishRecordingAsPicture();
90}
91
92static SkSerialProcs makes(SkSerialPictureProc proc, void* ctx = nullptr) {
93 SkSerialProcs procs;
94 procs.fPictureProc = proc;
95 procs.fPictureCtx = ctx;
96 return procs;
97}
98
99static SkDeserialProcs maked(SkDeserialPictureProc proc, const void* ctx = nullptr) {
100 SkDeserialProcs procs;
101 procs.fPictureProc = proc;
102 procs.fPictureCtx = const_cast<void*>(ctx);
103 return procs;
104}
105
106// packages the picture's point in the skdata, and records it in the ctx as an array
107struct Context {
108 SkTDArray<SkPicture*> fArray;
109 SkPicture* fSkipMe = nullptr;
110};
111
112static sk_sp<SkData> array_serial_proc(SkPicture* pic, void* ctx) {
113 Context* c = (Context*)ctx;
114 if (c->fSkipMe == pic) {
115 return nullptr;
116 }
117 *c->fArray.append() = pic;
118 return SkData::MakeWithCopy(&pic, sizeof(pic));
119}
120
121static sk_sp<SkPicture> array_deserial_proc(const void* data, size_t size, void* ctx) {
122 SkASSERT(sizeof(SkPicture*) == size);
123
124 Context* c = (Context*)ctx;
125 SkPicture* pic;
126 memcpy(&pic, data, size);
127
128 int index = c->fArray.find(pic);
129 SkASSERT(index >= 0);
130 c->fArray.removeShuffle(index);
131
132 return sk_ref_sp(pic);
133}
134
135static void test_pictures(skiatest::Reporter* reporter, sk_sp<SkPicture> p0, int count,
136 bool skipRoot) {
137 Context ctx;
138 if (skipRoot) {
139 ctx.fSkipMe = p0.get();
140 }
141
Mike Reed47fdf6c2017-12-20 14:12:07 -0500142 SkSerialProcs sprocs = makes(array_serial_proc, &ctx);
143 auto d0 = p0->serialize(&sprocs);
Mike Reed45ab6302017-12-13 11:38:57 -0500144 REPORTER_ASSERT(reporter, ctx.fArray.count() == count);
Mike Reed47fdf6c2017-12-20 14:12:07 -0500145 SkDeserialProcs dprocs = maked(array_deserial_proc, &ctx);
146 p0 = SkPicture::MakeFromData(d0.get(), &dprocs);
Mike Reed45ab6302017-12-13 11:38:57 -0500147 REPORTER_ASSERT(reporter, ctx.fArray.count() == 0);
148}
149
150DEF_TEST(serial_procs_picture, reporter) {
151
152 auto p1 = make_pic([](SkCanvas* c) {
153 // need to be large enough that drawPictures doesn't "unroll" us
154 for (int i = 0; i < 20; ++i) {
155 c->drawColor(SK_ColorRED);
156 }
157 });
158
159 // now use custom serialization
160 auto p0 = make_pic([](SkCanvas* c) { c->drawColor(SK_ColorBLUE); });
161 test_pictures(reporter, p0, 1, false);
162
163 // test inside effect
164 p0 = make_pic([p1](SkCanvas* c) {
165 SkPaint paint;
166 SkShader::TileMode tm = SkShader::kClamp_TileMode;
167 paint.setShader(SkShader::MakePictureShader(p1, tm, tm, nullptr, nullptr));
168 c->drawPaint(paint);
169 });
170 test_pictures(reporter, p0, 1, true);
171
172 // test nested picture
173 p0 = make_pic([p1](SkCanvas* c) {
174 c->drawColor(SK_ColorRED);
175 c->drawPicture(p1);
176 c->drawColor(SK_ColorBLUE);
177 });
178 test_pictures(reporter, p0, 1, true);
179}
180