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