skia2/modules/skottie/tests/Image.cpp
Florin Malita 38921cafe1 Reland "[skottie] Add image sampling and transform options"
This reverts commit b81842aa28.

Reason for revert: reland with fixes

Original change's description:
> Revert "[skottie] Add image sampling and transform options"
>
> This reverts commit 2f24405250.
>
> Reason for revert: broke Win/shared 
>
> Original change's description:
> > [skottie] Add image sampling and transform options
> >
> > Expand the SkImageAsset API to support controlling sampling options and
> > pass an additional transform.
> >
> > Bug: skia:10944, skia:10942
> > Change-Id: I7bad0b2ab58ed40fe4b425de0eb6970a4c7d7117
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340097
> > Commit-Queue: Florin Malita <fmalita@chromium.org>
> > Commit-Queue: Florin Malita <fmalita@google.com>
> > Reviewed-by: Mike Reed <reed@google.com>
>
> TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com,aparchur@google.com
>
> Change-Id: I59d4161356ffdc20588f1bd3beb33c54e44807a2
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:10944
> Bug: skia:10942
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340619
> Reviewed-by: Florin Malita <fmalita@google.com>
> Commit-Queue: Florin Malita <fmalita@google.com>

TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com,aparchur@google.com

# Not skipping CQ checks because this is a reland.

Bug: skia:10944
Bug: skia:10942
Change-Id: I91892f4db6366ceb07d1a49a7bc54da17cea5399
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340657
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@google.com>
2020-12-03 19:21:57 +00:00

134 lines
4.2 KiB
C++

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkSurface.h"
#include "modules/skottie/include/Skottie.h"
#include "tests/Test.h"
using namespace skottie;
DEF_TEST(Skottie_Image_CustomTransform, r) {
static constexpr char json[] =
R"({
"v": "5.2.1",
"w": 100,
"h": 100,
"fr": 10,
"ip": 0,
"op": 100,
"assets": [{
"id": "img_0",
"p" : "img_0.png",
"u" : "images/",
"w" : 100,
"h" : 50
}],
"layers": [
{
"ip": 0,
"op": 100,
"ty": 2,
"refId": "img_0",
"ks": {
"p": { "a": 0, "k": [0,25] }
}
}
]
})";
SkMemoryStream stream(json, strlen(json));
static const struct TestData {
float t;
SkMatrix m;
SkColor c[5]; // expected color samples at center/L/T/R/B
} tests[] {
{ 0, SkMatrix::I(),
{0xffff0000, 0xffff0000, 0xff00ff00, 0xffff0000, 0xff00ff00}},
{ 1, SkMatrix::Translate(50,25) * SkMatrix::Scale(.5f,.5f) * SkMatrix::Translate(-50,-25),
{0xffff0000, 0xff00ff00, 0xff00ff00, 0xff00ff00, 0xff00ff00}},
{ 2, SkMatrix::Translate(-50, 0),
{0xff00ff00, 0xffff0000, 0xff00ff00, 0xff00ff00, 0xff00ff00}},
{ 3, SkMatrix::Translate(0, -25),
{0xff00ff00, 0xff00ff00, 0xffff0000, 0xff00ff00, 0xff00ff00}},
{ 4, SkMatrix::Translate(50, 0),
{0xffff0000, 0xff00ff00, 0xff00ff00, 0xffff0000, 0xff00ff00}},
{ 5, SkMatrix::Translate(0, 25),
{0xffff0000, 0xffff0000, 0xff00ff00, 0xffff0000, 0xffff0000}},
};
class TestImageAsset final : public ImageAsset {
public:
TestImageAsset(const TestData* tst, skiatest::Reporter* r)
: fTest(tst)
, fReporter(r) {
auto surf = SkSurface::MakeRasterN32Premul(200, 100);
surf->getCanvas()->drawColor(0xffff0000);
fImage = surf->makeImageSnapshot();
}
private:
bool isMultiFrame() override { return true; }
FrameData getFrameData(float t) override {
REPORTER_ASSERT(fReporter, t == fTest->t);
return { fImage, SkSamplingOptions(), fTest++->m };
}
sk_sp<SkImage> fImage;
const TestData* fTest;
skiatest::Reporter* fReporter;
};
class TestResourceProvider final : public ResourceProvider {
public:
TestResourceProvider(const TestData* tst, skiatest::Reporter* r)
: fTest(tst)
, fReporter(r) {}
private:
sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override {
return sk_make_sp<TestImageAsset>(fTest, fReporter);
}
const TestData* fTest;
skiatest::Reporter* fReporter;
};
auto anim = Animation::Builder()
.setResourceProvider(sk_make_sp<TestResourceProvider>(tests, r))
.make(&stream);
REPORTER_ASSERT(r, anim);
static constexpr SkSize render_size{100, 100};
auto surf = SkSurface::MakeRasterN32Premul(render_size.width(), render_size.height());
auto rect = SkRect::MakeSize(render_size);
SkPixmap pmap;
surf->peekPixels(&pmap);
for (const auto& tst : tests) {
surf->getCanvas()->clear(0xff00ff00);
anim->seekFrameTime(tst.t);
anim->render(surf->getCanvas(), &rect);
REPORTER_ASSERT(r,
tst.c[0] == pmap.getColor(render_size.width() / 2, render_size.height() / 2));
REPORTER_ASSERT(r,
tst.c[1] == pmap.getColor(1 , render_size.height() / 2));
REPORTER_ASSERT(r,
tst.c[2] == pmap.getColor(render_size.width() / 2, 1));
REPORTER_ASSERT(r,
tst.c[3] == pmap.getColor(render_size.width() - 1, render_size.height() / 2));
REPORTER_ASSERT(r,
tst.c[4] == pmap.getColor(render_size.width() /2 , render_size.height() - 1));
}
}