1 /*
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 "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkData.h"
11 #include "include/core/SkDataTable.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPicture.h"
17 #include "include/core/SkPictureRecorder.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkSamplingOptions.h"
21 #include "include/core/SkSerialProcs.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/encode/SkPngEncoder.h"
27 #include "include/private/base/SkTDArray.h"
28 #include "tests/Test.h"
29 #include "tools/DecodeUtils.h"
30 #include "tools/Resources.h"
31 #include "tools/ToolUtils.h"
32 #include "tools/fonts/FontToolUtils.h"
33
34 #include <algorithm>
35 #include <cstring>
36 #include <functional>
37 #include <iterator>
38
picture_to_image(const sk_sp<SkPicture> & pic)39 static sk_sp<SkImage> picture_to_image(const sk_sp<SkPicture>& pic) {
40 SkIRect r = pic->cullRect().round();
41 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(r.width(), r.height()));
42 surf->getCanvas()->drawPicture(pic);
43 return surf->makeImageSnapshot();
44 }
45
46 struct State {
47 const char* fStr;
48 SkImage* fImg;
49 };
50
DEF_TEST(serial_procs_image,reporter)51 DEF_TEST(serial_procs_image, reporter) {
52 auto src_img = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
53 const char magic_str[] = "magic signature";
54
55 const SkSerialImageProc sprocs[] = {
56 [](SkImage* img, void* ctx) -> sk_sp<SkData> { return nullptr; },
57 [](SkImage* img, void* ctx) { return SkPngEncoder::Encode(nullptr, img, {}); },
58 [](SkImage* img, void* ctx) { return SkData::MakeWithCString(((State*)ctx)->fStr); },
59 };
60 const SkDeserialImageProc dprocs[] = {
61 [](const void* data, size_t length, void*) -> sk_sp<SkImage> { return nullptr; },
62 [](const void* data, size_t length, void*) {
63 return SkImages::DeferredFromEncodedData(SkData::MakeWithCopy(data, length));
64 },
65 [](const void* data, size_t length, void* ctx) -> sk_sp<SkImage> {
66 State* state = (State*)ctx;
67 if (length != strlen(state->fStr) + 1 || 0 != memcmp(data, state->fStr, length)) {
68 return nullptr;
69 }
70 return sk_ref_sp(state->fImg);
71 },
72 };
73
74 sk_sp<SkPicture> pic;
75 {
76 SkPictureRecorder rec;
77 SkCanvas* canvas = rec.beginRecording(128, 128);
78 canvas->drawImage(src_img, 0, 0);
79 pic = rec.finishRecordingAsPicture();
80 }
81
82 State state = { magic_str, src_img.get() };
83
84 SkSerialProcs sproc;
85 sproc.fImageCtx = &state;
86 SkDeserialProcs dproc;
87 dproc.fImageCtx = &state;
88
89 for (size_t i = 0; i < std::size(sprocs); ++i) {
90 sproc.fImageProc = sprocs[i];
91 auto data = pic->serialize(&sproc);
92 REPORTER_ASSERT(reporter, data);
93
94 dproc.fImageProc = dprocs[i];
95 auto new_pic = SkPicture::MakeFromData(data.get(), &dproc);
96 REPORTER_ASSERT(reporter, data);
97
98 auto dst_img = picture_to_image(new_pic);
99 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(src_img.get(), dst_img.get()));
100 }
101 }
102
103 ///////////////////////////////////////////////////////////////////////////////////////////////////
104
make_pic(const std::function<void (SkCanvas *)> & drawer)105 static sk_sp<SkPicture> make_pic(const std::function<void(SkCanvas*)>& drawer) {
106 SkPictureRecorder rec;
107 drawer(rec.beginRecording(128, 128));
108 return rec.finishRecordingAsPicture();
109 }
110
makes(SkSerialPictureProc proc,void * ctx=nullptr)111 static SkSerialProcs makes(SkSerialPictureProc proc, void* ctx = nullptr) {
112 SkSerialProcs procs;
113 procs.fPictureProc = proc;
114 procs.fPictureCtx = ctx;
115 return procs;
116 }
117
maked(SkDeserialPictureProc proc,const void * ctx=nullptr)118 static SkDeserialProcs maked(SkDeserialPictureProc proc, const void* ctx = nullptr) {
119 SkDeserialProcs procs;
120 procs.fPictureProc = proc;
121 procs.fPictureCtx = const_cast<void*>(ctx);
122 return procs;
123 }
124
125 // packages the picture's point in the skdata, and records it in the ctx as an array
126 struct Context {
127 SkTDArray<SkPicture*> fArray;
128 SkPicture* fSkipMe = nullptr;
129 };
130
array_serial_proc(SkPicture * pic,void * ctx)131 static sk_sp<SkData> array_serial_proc(SkPicture* pic, void* ctx) {
132 Context* c = (Context*)ctx;
133 if (c->fSkipMe == pic) {
134 return nullptr;
135 }
136 *c->fArray.append() = pic;
137 return SkData::MakeWithCopy(&pic, sizeof(pic));
138 }
139
array_deserial_proc(const void * data,size_t size,void * ctx)140 static sk_sp<SkPicture> array_deserial_proc(const void* data, size_t size, void* ctx) {
141 SkASSERT(sizeof(SkPicture*) == size);
142
143 Context* c = (Context*)ctx;
144 SkPicture* pic;
145 memcpy(&pic, data, size);
146
147 auto found = std::find(c->fArray.begin(), c->fArray.end(), pic);
148 SkASSERT(found != c->fArray.end());
149 if (found != c->fArray.end()) {
150 c->fArray.removeShuffle(std::distance(c->fArray.begin(), found));
151 }
152
153 return sk_ref_sp(pic);
154 }
155
test_pictures(skiatest::Reporter * reporter,sk_sp<SkPicture> p0,int count,bool skipRoot)156 static void test_pictures(skiatest::Reporter* reporter, sk_sp<SkPicture> p0, int count,
157 bool skipRoot) {
158 Context ctx;
159 if (skipRoot) {
160 ctx.fSkipMe = p0.get();
161 }
162
163 SkSerialProcs sprocs = makes(array_serial_proc, &ctx);
164 auto d0 = p0->serialize(&sprocs);
165 REPORTER_ASSERT(reporter, ctx.fArray.size() == count);
166 SkDeserialProcs dprocs = maked(array_deserial_proc, &ctx);
167 p0 = SkPicture::MakeFromData(d0.get(), &dprocs);
168 REPORTER_ASSERT(reporter, ctx.fArray.size() == 0);
169 }
170
DEF_TEST(serial_procs_picture,reporter)171 DEF_TEST(serial_procs_picture, reporter) {
172
173 auto p1 = make_pic([](SkCanvas* c) {
174 // need to be large enough that drawPictures doesn't "unroll" us
175 for (int i = 0; i < 20; ++i) {
176 c->drawColor(SK_ColorRED);
177 }
178 });
179
180 // now use custom serialization
181 auto p0 = make_pic([](SkCanvas* c) { c->drawColor(SK_ColorBLUE); });
182 test_pictures(reporter, p0, 1, false);
183
184 // test inside effect
185 p0 = make_pic([p1](SkCanvas* c) {
186 SkPaint paint;
187 SkTileMode tm = SkTileMode::kClamp;
188 paint.setShader(p1->makeShader(tm, tm, SkFilterMode::kNearest));
189 c->drawPaint(paint);
190 });
191 test_pictures(reporter, p0, 1, true);
192
193 // test nested picture
194 p0 = make_pic([p1](SkCanvas* c) {
195 c->drawColor(SK_ColorRED);
196 c->drawPicture(p1);
197 c->drawColor(SK_ColorBLUE);
198 });
199 test_pictures(reporter, p0, 1, true);
200 }
201
make_picture(const sk_sp<SkTypeface> & tf0,const sk_sp<SkTypeface> & tf1)202 static sk_sp<SkPicture> make_picture(const sk_sp<SkTypeface>& tf0, const sk_sp<SkTypeface>& tf1) {
203 SkPictureRecorder rec;
204 SkCanvas* canvas = rec.beginRecording(100, 100);
205 SkPaint paint;
206 SkFont font;
207 font.setTypeface(tf0); canvas->drawString("hello", 0, 0, font, paint);
208 font.setTypeface(tf1); canvas->drawString("hello", 0, 0, font, paint);
209 font.setTypeface(tf0); canvas->drawString("hello", 0, 0, font, paint);
210 font.setTypeface(tf1); canvas->drawString("hello", 0, 0, font, paint);
211 return rec.finishRecordingAsPicture();
212 }
213
DEF_TEST(serial_typeface,reporter)214 DEF_TEST(serial_typeface, reporter) {
215 auto tf0 = ToolUtils::CreateTypefaceFromResource("fonts/hintgasp.ttf");
216 auto tf1 = ToolUtils::CreateTypefaceFromResource("fonts/Roboto2-Regular_NoEmbed.ttf");
217 if (!tf0 || !tf1 || tf0.get() == tf1.get()) {
218 return; // need two different typefaces for this test to make sense.
219 }
220
221 auto pic = make_picture(tf0, tf1);
222
223 int counter = 0;
224 SkSerialProcs procs;
225 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> {
226 *(int*)ctx += 1;
227 return nullptr;
228 };
229 procs.fTypefaceCtx = &counter;
230 auto data = pic->serialize(&procs);
231
232 // The picture has 2 references to each typeface, but we want the serialized picture to
233 // only have written the data 1 time per typeface.
234 REPORTER_ASSERT(reporter, counter == 2);
235 }
236
237