2 * Copyright 2018 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/private/SkTArray.h"
18 #include "include/private/SkTDArray.h"
19 #include "tools/Resources.h"
20 #include "tools/ToolUtils.h"
22 #include <initializer_list>
24 static sk_sp<SkImage> make_image1() { return GetResourceAsImage("images/mandrill_128.png"); }
26 static sk_sp<SkImage> make_image2() {
27 return GetResourceAsImage("images/brickwork-texture.jpg")->makeSubset({0, 0, 128, 128});
32 class PerspImages : public GM {
34 PerspImages() = default;
37 SkString onShortName() override { return SkString("persp_images"); }
39 SkISize onISize() override { return SkISize::Make(1150, 1280); }
41 void onOnceBeforeDraw() override {
42 fImages.push_back(make_image1());
43 fImages.push_back(make_image2());
46 void onDraw(SkCanvas* canvas) override {
47 SkTDArray<SkMatrix> matrices;
48 matrices.push()->setAll(1.f, 0.f, 0.f,
51 matrices.push()->setAll(1.f, 0.f, 0.f,
53 0.007f, -0.005f, 1.f);
54 matrices[1].preSkew(0.2f, -0.1f);
55 matrices[1].preRotate(-65.f);
56 matrices[1].preScale(1.2f, .8f);
57 matrices[1].postTranslate(0.f, 60.f);
60 SkRect bounds = SkRect::MakeEmpty();
61 for (const auto& img : fImages) {
62 SkRect imgB = SkRect::MakeWH(img->width(), img->height());
63 for (const auto& m : matrices) {
65 m.mapRect(&temp, imgB);
69 canvas->translate(-bounds.fLeft + 10.f, -bounds.fTop + 10.f);
77 {DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) {
78 for (const auto& m : matrices) {
79 for (auto aa : {false, true}) {
80 paint.setAntiAlias(aa);
81 for (auto sampling : {
82 SkSamplingOptions(SkFilterMode::kNearest),
83 SkSamplingOptions(SkFilterMode::kLinear),
84 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
85 SkSamplingOptions(SkCubicResampler::Mitchell())}) {
86 for (const auto& origImage : fImages) {
87 sk_sp<SkImage> img = ToolUtils::MakeTextureImage(canvas, origImage);
91 SkRect src = { img->width() / 4.f, img->height() / 4.f,
92 3.f * img->width() / 4.f, 3.f * img->height() / 4 };
94 3.f / 4.f * img->width(), 3.f / 4.f * img->height()};
96 case DrawType::kDrawImage:
97 canvas->drawImage(img, 0, 0, sampling, &paint);
99 case DrawType::kDrawImageRectStrict:
100 canvas->drawImageRect(img, src, dst, sampling, &paint,
101 SkCanvas::kStrict_SrcRectConstraint);
103 case DrawType::kDrawImageRectFast:
104 canvas->drawImageRect(img, src, dst, sampling, &paint,
105 SkCanvas::kFast_SrcRectConstraint);
112 canvas->translate(bounds.width() + 10.f, 0);
115 canvas->translate(0, bounds.height() + 10.f);
128 inline static constexpr int kNumImages = 4;
129 SkTArray<sk_sp<SkImage>> fImages;
131 using INHERITED = GM;
134 //////////////////////////////////////////////////////////////////////////////
136 DEF_GM(return new PerspImages();)
138 } // namespace skiagm