2017-12-05 20:11:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkSerialProcs.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2017-12-05 20:11:24 +00:00
|
|
|
|
|
|
|
static sk_sp<SkImage> picture_to_image(sk_sp<SkPicture> pic) {
|
|
|
|
SkIRect r = pic->cullRect().round();
|
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(r.width(), r.height());
|
|
|
|
surf->getCanvas()->drawPicture(pic);
|
|
|
|
return surf->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
const char* fStr;
|
|
|
|
SkImage* fImg;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(serial_procs_image, reporter) {
|
2017-12-08 15:21:31 +00:00
|
|
|
auto src_img = GetResourceAsImage("images/mandrill_128.png");
|
2017-12-05 20:11:24 +00:00
|
|
|
const char magic_str[] = "magic signature";
|
|
|
|
|
|
|
|
const SkSerialImageProc sprocs[] = {
|
2017-12-08 15:31:52 +00:00
|
|
|
[](SkImage* img, void* ctx) -> sk_sp<SkData> { return nullptr; },
|
|
|
|
[](SkImage* img, void* ctx) { return img->encodeToData(); },
|
|
|
|
[](SkImage* img, void* ctx) { return SkData::MakeWithCString(((State*)ctx)->fStr); },
|
2017-12-05 20:11:24 +00:00
|
|
|
};
|
|
|
|
const SkDeserialImageProc dprocs[] = {
|
|
|
|
[](const void* data, size_t length, void*) -> sk_sp<SkImage> {
|
|
|
|
return nullptr;
|
|
|
|
},
|
2017-12-08 15:31:52 +00:00
|
|
|
[](const void* data, size_t length, void*) {
|
2017-12-05 20:11:24 +00:00
|
|
|
return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length));
|
|
|
|
},
|
|
|
|
[](const void* data, size_t length, void* ctx) -> sk_sp<SkImage> {
|
|
|
|
State* state = (State*)ctx;
|
2020-08-16 03:22:53 +00:00
|
|
|
if (length != strlen(state->fStr)+1 || 0 != memcmp(data, state->fStr, length)) {
|
2017-12-05 20:11:24 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return sk_ref_sp(state->fImg);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
sk_sp<SkPicture> pic;
|
|
|
|
{
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
SkCanvas* canvas = rec.beginRecording(128, 128);
|
|
|
|
canvas->drawImage(src_img, 0, 0, nullptr);
|
|
|
|
pic = rec.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
|
|
|
State state = { magic_str, src_img.get() };
|
|
|
|
|
|
|
|
SkSerialProcs sproc;
|
|
|
|
sproc.fImageCtx = &state;
|
|
|
|
SkDeserialProcs dproc;
|
|
|
|
dproc.fImageCtx = &state;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(sprocs); ++i) {
|
|
|
|
sproc.fImageProc = sprocs[i];
|
2017-12-20 19:12:07 +00:00
|
|
|
auto data = pic->serialize(&sproc);
|
2017-12-05 20:11:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, data);
|
|
|
|
|
|
|
|
dproc.fImageProc = dprocs[i];
|
2017-12-20 19:12:07 +00:00
|
|
|
auto new_pic = SkPicture::MakeFromData(data.get(), &dproc);
|
2017-12-05 20:11:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, data);
|
|
|
|
|
|
|
|
auto dst_img = picture_to_image(new_pic);
|
2019-03-20 16:12:10 +00:00
|
|
|
REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(src_img.get(), dst_img.get()));
|
2017-12-05 20:11:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:38:57 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static sk_sp<SkPicture> make_pic(const std::function<void(SkCanvas*)>& drawer) {
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
drawer(rec.beginRecording(128, 128));
|
|
|
|
return rec.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkSerialProcs makes(SkSerialPictureProc proc, void* ctx = nullptr) {
|
|
|
|
SkSerialProcs procs;
|
|
|
|
procs.fPictureProc = proc;
|
|
|
|
procs.fPictureCtx = ctx;
|
|
|
|
return procs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkDeserialProcs maked(SkDeserialPictureProc proc, const void* ctx = nullptr) {
|
|
|
|
SkDeserialProcs procs;
|
|
|
|
procs.fPictureProc = proc;
|
|
|
|
procs.fPictureCtx = const_cast<void*>(ctx);
|
|
|
|
return procs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// packages the picture's point in the skdata, and records it in the ctx as an array
|
|
|
|
struct Context {
|
|
|
|
SkTDArray<SkPicture*> fArray;
|
|
|
|
SkPicture* fSkipMe = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static sk_sp<SkData> array_serial_proc(SkPicture* pic, void* ctx) {
|
|
|
|
Context* c = (Context*)ctx;
|
|
|
|
if (c->fSkipMe == pic) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
*c->fArray.append() = pic;
|
|
|
|
return SkData::MakeWithCopy(&pic, sizeof(pic));
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkPicture> array_deserial_proc(const void* data, size_t size, void* ctx) {
|
|
|
|
SkASSERT(sizeof(SkPicture*) == size);
|
|
|
|
|
|
|
|
Context* c = (Context*)ctx;
|
|
|
|
SkPicture* pic;
|
|
|
|
memcpy(&pic, data, size);
|
|
|
|
|
|
|
|
int index = c->fArray.find(pic);
|
|
|
|
SkASSERT(index >= 0);
|
|
|
|
c->fArray.removeShuffle(index);
|
|
|
|
|
|
|
|
return sk_ref_sp(pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_pictures(skiatest::Reporter* reporter, sk_sp<SkPicture> p0, int count,
|
|
|
|
bool skipRoot) {
|
|
|
|
Context ctx;
|
|
|
|
if (skipRoot) {
|
|
|
|
ctx.fSkipMe = p0.get();
|
|
|
|
}
|
|
|
|
|
2017-12-20 19:12:07 +00:00
|
|
|
SkSerialProcs sprocs = makes(array_serial_proc, &ctx);
|
|
|
|
auto d0 = p0->serialize(&sprocs);
|
2017-12-13 16:38:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, ctx.fArray.count() == count);
|
2017-12-20 19:12:07 +00:00
|
|
|
SkDeserialProcs dprocs = maked(array_deserial_proc, &ctx);
|
|
|
|
p0 = SkPicture::MakeFromData(d0.get(), &dprocs);
|
2017-12-13 16:38:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, ctx.fArray.count() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(serial_procs_picture, reporter) {
|
|
|
|
|
|
|
|
auto p1 = make_pic([](SkCanvas* c) {
|
|
|
|
// need to be large enough that drawPictures doesn't "unroll" us
|
|
|
|
for (int i = 0; i < 20; ++i) {
|
|
|
|
c->drawColor(SK_ColorRED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// now use custom serialization
|
|
|
|
auto p0 = make_pic([](SkCanvas* c) { c->drawColor(SK_ColorBLUE); });
|
|
|
|
test_pictures(reporter, p0, 1, false);
|
|
|
|
|
|
|
|
// test inside effect
|
|
|
|
p0 = make_pic([p1](SkCanvas* c) {
|
|
|
|
SkPaint paint;
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode tm = SkTileMode::kClamp;
|
|
|
|
paint.setShader(p1->makeShader(tm, tm));
|
2017-12-13 16:38:57 +00:00
|
|
|
c->drawPaint(paint);
|
|
|
|
});
|
|
|
|
test_pictures(reporter, p0, 1, true);
|
|
|
|
|
|
|
|
// test nested picture
|
|
|
|
p0 = make_pic([p1](SkCanvas* c) {
|
|
|
|
c->drawColor(SK_ColorRED);
|
|
|
|
c->drawPicture(p1);
|
|
|
|
c->drawColor(SK_ColorBLUE);
|
|
|
|
});
|
|
|
|
test_pictures(reporter, p0, 1, true);
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:51:46 +00:00
|
|
|
static sk_sp<SkPicture> make_picture(sk_sp<SkTypeface> tf0, sk_sp<SkTypeface> tf1) {
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
SkCanvas* canvas = rec.beginRecording(100, 100);
|
|
|
|
SkPaint paint;
|
2019-01-08 18:01:58 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setTypeface(tf0); canvas->drawString("hello", 0, 0, font, paint);
|
|
|
|
font.setTypeface(tf1); canvas->drawString("hello", 0, 0, font, paint);
|
|
|
|
font.setTypeface(tf0); canvas->drawString("hello", 0, 0, font, paint);
|
|
|
|
font.setTypeface(tf1); canvas->drawString("hello", 0, 0, font, paint);
|
2018-09-05 14:51:46 +00:00
|
|
|
return rec.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(serial_typeface, reporter) {
|
|
|
|
auto tf0 = MakeResourceAsTypeface("fonts/hintgasp.ttf");
|
|
|
|
auto tf1 = MakeResourceAsTypeface("fonts/Roboto2-Regular_NoEmbed.ttf");
|
|
|
|
if (!tf0 || !tf1 || tf0.get() == tf1.get()) {
|
|
|
|
return; // need two different typefaces for this test to make sense.
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pic = make_picture(tf0, tf1);
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
SkSerialProcs procs;
|
|
|
|
procs.fTypefaceProc = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> {
|
|
|
|
*(int*)ctx += 1;
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
procs.fTypefaceCtx = &counter;
|
|
|
|
auto data = pic->serialize(&procs);
|
|
|
|
|
|
|
|
// The picture has 2 references to each typeface, but we want the serialized picture to
|
|
|
|
// only have written the data 1 time per typeface.
|
|
|
|
REPORTER_ASSERT(reporter, counter == 2);
|
|
|
|
}
|
|
|
|
|