blob: 2663f40351b1eccbdbddc737b5cd7f24ee71dad4 [file] [log] [blame]
Brian Salomonbe3c1d22018-05-21 12:54:39 -04001/*
2 * Copyright 2018 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050017#include "include/private/base/SkTArray.h"
18#include "include/private/base/SkTDArray.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040019#include "tools/DecodeUtils.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040020#include "tools/GpuToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/Resources.h"
Robert Phillips0fb10ab2022-04-20 14:57:03 -040022#include "tools/ToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023
24#include <initializer_list>
Brian Salomonbe3c1d22018-05-21 12:54:39 -040025
Herb Derbyec96c212023-03-06 10:31:22 -050026using namespace skia_private;
27
Kevin Lubick8b741882023-10-06 11:41:38 -040028static sk_sp<SkImage> make_image1() {
29 return ToolUtils::GetResourceAsImage("images/mandrill_128.png");
30}
Brian Salomonbe3c1d22018-05-21 12:54:39 -040031
32static sk_sp<SkImage> make_image2() {
Kevin Lubick8b741882023-10-06 11:41:38 -040033 return ToolUtils::GetResourceAsImage("images/brickwork-texture.jpg")
34 ->makeSubset(nullptr, {0, 0, 128, 128});
Brian Salomonbe3c1d22018-05-21 12:54:39 -040035}
36
37namespace skiagm {
38
39class PerspImages : public GM {
40public:
41 PerspImages() = default;
42
43protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000044 SkString getName() const override { return SkString("persp_images"); }
Brian Salomonbe3c1d22018-05-21 12:54:39 -040045
Leandro Lovisolo8f023882023-08-15 21:13:52 +000046 SkISize getISize() override { return SkISize::Make(1150, 1280); }
Brian Salomonbe3c1d22018-05-21 12:54:39 -040047
48 void onOnceBeforeDraw() override {
49 fImages.push_back(make_image1());
50 fImages.push_back(make_image2());
51 }
52
53 void onDraw(SkCanvas* canvas) override {
54 SkTDArray<SkMatrix> matrices;
Herb Derby161a80f2022-09-22 15:50:42 -040055 matrices.append()->setAll(1.f, 0.f, 0.f,
Brian Salomonbe3c1d22018-05-21 12:54:39 -040056 0.f, 1.f, 0.f,
57 0.f, 0.005f, 1.f);
Herb Derby161a80f2022-09-22 15:50:42 -040058 matrices.append()->setAll(1.f, 0.f, 0.f,
Brian Salomonbe3c1d22018-05-21 12:54:39 -040059 0.f, 1.f, 0.f,
60 0.007f, -0.005f, 1.f);
61 matrices[1].preSkew(0.2f, -0.1f);
62 matrices[1].preRotate(-65.f);
63 matrices[1].preScale(1.2f, .8f);
64 matrices[1].postTranslate(0.f, 60.f);
65 SkPaint paint;
66 int n = 0;
67 SkRect bounds = SkRect::MakeEmpty();
68 for (const auto& img : fImages) {
69 SkRect imgB = SkRect::MakeWH(img->width(), img->height());
70 for (const auto& m : matrices) {
71 SkRect temp;
72 m.mapRect(&temp, imgB);
73 bounds.join(temp);
74 }
75 }
76 canvas->translate(-bounds.fLeft + 10.f, -bounds.fTop + 10.f);
77 canvas->save();
Brian Salomonb80ffee2018-05-23 16:39:39 -040078 enum class DrawType {
79 kDrawImage,
80 kDrawImageRectStrict,
81 kDrawImageRectFast,
82 };
83 for (auto type :
84 {DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) {
Brian Salomonbe3c1d22018-05-21 12:54:39 -040085 for (const auto& m : matrices) {
86 for (auto aa : {false, true}) {
87 paint.setAntiAlias(aa);
Mike Reed07c5f522021-01-23 12:23:23 -050088 for (auto sampling : {
89 SkSamplingOptions(SkFilterMode::kNearest),
90 SkSamplingOptions(SkFilterMode::kLinear),
91 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
Mike Reedf3ac2af2021-02-05 12:55:38 -050092 SkSamplingOptions(SkCubicResampler::Mitchell())}) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -040093 for (const auto& origImage : fImages) {
94 sk_sp<SkImage> img = ToolUtils::MakeTextureImage(canvas, origImage);
95 if (img) {
96 canvas->save();
97 canvas->concat(m);
98 SkRect src = { img->width() / 4.f, img->height() / 4.f,
99 3.f * img->width() / 4.f, 3.f * img->height() / 4 };
100 SkRect dst = { 0, 0,
101 3.f / 4.f * img->width(), 3.f / 4.f * img->height()};
102 switch (type) {
103 case DrawType::kDrawImage:
104 canvas->drawImage(img, 0, 0, sampling, &paint);
105 break;
106 case DrawType::kDrawImageRectStrict:
107 canvas->drawImageRect(img, src, dst, sampling, &paint,
108 SkCanvas::kStrict_SrcRectConstraint);
109 break;
110 case DrawType::kDrawImageRectFast:
111 canvas->drawImageRect(img, src, dst, sampling, &paint,
112 SkCanvas::kFast_SrcRectConstraint);
113 break;
114 }
115 canvas->restore();
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400116 }
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400117 ++n;
118 if (n < 8) {
119 canvas->translate(bounds.width() + 10.f, 0);
120 } else {
121 canvas->restore();
122 canvas->translate(0, bounds.height() + 10.f);
123 canvas->save();
124 n = 0;
125 }
126 }
127 }
128 }
129 }
130 }
131 canvas->restore();
132 }
133
134private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400135 inline static constexpr int kNumImages = 4;
Herb Derbyec96c212023-03-06 10:31:22 -0500136 TArray<sk_sp<SkImage>> fImages;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400137
John Stiles7571f9e2020-09-02 22:42:33 -0400138 using INHERITED = GM;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400139};
140
141//////////////////////////////////////////////////////////////////////////////
142
143DEF_GM(return new PerspImages();)
144
145} // namespace skiagm