2018-05-21 16:54:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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 "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
|
|
|
|
#include <initializer_list>
|
2018-05-21 16:54:39 +00:00
|
|
|
|
|
|
|
static sk_sp<SkImage> make_image1() { return GetResourceAsImage("images/mandrill_128.png"); }
|
|
|
|
|
|
|
|
static sk_sp<SkImage> make_image2() {
|
|
|
|
return GetResourceAsImage("images/brickwork-texture.jpg")->makeSubset({0, 0, 128, 128});
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class PerspImages : public GM {
|
|
|
|
public:
|
|
|
|
PerspImages() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return SkString("persp_images"); }
|
|
|
|
|
2018-05-23 20:39:39 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(1150, 1280); }
|
2018-05-21 16:54:39 +00:00
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fImages.push_back(make_image1());
|
|
|
|
fImages.push_back(make_image2());
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkTDArray<SkMatrix> matrices;
|
|
|
|
matrices.push()->setAll(1.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.005f, 1.f);
|
|
|
|
matrices.push()->setAll(1.f, 0.f, 0.f,
|
|
|
|
0.f, 1.f, 0.f,
|
|
|
|
0.007f, -0.005f, 1.f);
|
|
|
|
matrices[1].preSkew(0.2f, -0.1f);
|
|
|
|
matrices[1].preRotate(-65.f);
|
|
|
|
matrices[1].preScale(1.2f, .8f);
|
|
|
|
matrices[1].postTranslate(0.f, 60.f);
|
|
|
|
SkPaint paint;
|
|
|
|
int n = 0;
|
|
|
|
SkRect bounds = SkRect::MakeEmpty();
|
|
|
|
for (const auto& img : fImages) {
|
|
|
|
SkRect imgB = SkRect::MakeWH(img->width(), img->height());
|
|
|
|
for (const auto& m : matrices) {
|
|
|
|
SkRect temp;
|
|
|
|
m.mapRect(&temp, imgB);
|
|
|
|
bounds.join(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->translate(-bounds.fLeft + 10.f, -bounds.fTop + 10.f);
|
|
|
|
canvas->save();
|
2018-05-23 20:39:39 +00:00
|
|
|
enum class DrawType {
|
|
|
|
kDrawImage,
|
|
|
|
kDrawImageRectStrict,
|
|
|
|
kDrawImageRectFast,
|
|
|
|
};
|
|
|
|
for (auto type :
|
|
|
|
{DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) {
|
2018-05-21 16:54:39 +00:00
|
|
|
for (const auto& m : matrices) {
|
|
|
|
for (auto aa : {false, true}) {
|
|
|
|
paint.setAntiAlias(aa);
|
2021-01-23 17:23:23 +00:00
|
|
|
for (auto sampling : {
|
|
|
|
SkSamplingOptions(SkFilterMode::kNearest),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
|
2021-02-05 17:55:38 +00:00
|
|
|
SkSamplingOptions(SkCubicResampler::Mitchell())}) {
|
2018-05-21 16:54:39 +00:00
|
|
|
for (const auto& img : fImages) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->concat(m);
|
2018-05-23 20:39:39 +00:00
|
|
|
SkRect src = {img->width() / 4.f, img->height() / 4.f,
|
|
|
|
3.f * img->width() / 4.f, 3.f * img->height() / 4};
|
|
|
|
SkRect dst = {0, 0,
|
|
|
|
3.f / 4.f * img->width(), 3.f / 4.f * img->height()};
|
|
|
|
switch (type) {
|
|
|
|
case DrawType::kDrawImage:
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImage(img, 0, 0, sampling, &paint);
|
2018-05-23 20:39:39 +00:00
|
|
|
break;
|
|
|
|
case DrawType::kDrawImageRectStrict:
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImageRect(img, src, dst, sampling, &paint,
|
2018-05-23 20:39:39 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
|
|
|
break;
|
|
|
|
case DrawType::kDrawImageRectFast:
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImageRect(img, src, dst, sampling, &paint,
|
2018-05-23 20:39:39 +00:00
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
|
|
|
break;
|
2018-05-21 16:54:39 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
++n;
|
|
|
|
if (n < 8) {
|
|
|
|
canvas->translate(bounds.width() + 10.f, 0);
|
|
|
|
} else {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, bounds.height() + 10.f);
|
|
|
|
canvas->save();
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kNumImages = 4;
|
2018-05-21 16:54:39 +00:00
|
|
|
SkTArray<sk_sp<SkImage>> fImages;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2018-05-21 16:54:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new PerspImages();)
|
|
|
|
|
|
|
|
} // namespace skiagm
|