Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkMatrix.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkSize.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/private/SkTArray.h" |
| 19 | #include "include/private/SkTDArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "tools/Resources.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | |
| 22 | #include <initializer_list> |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 23 | |
| 24 | static sk_sp<SkImage> make_image1() { return GetResourceAsImage("images/mandrill_128.png"); } |
| 25 | |
| 26 | static sk_sp<SkImage> make_image2() { |
| 27 | return GetResourceAsImage("images/brickwork-texture.jpg")->makeSubset({0, 0, 128, 128}); |
| 28 | } |
| 29 | |
| 30 | namespace skiagm { |
| 31 | |
| 32 | class PerspImages : public GM { |
| 33 | public: |
| 34 | PerspImages() = default; |
| 35 | |
| 36 | protected: |
| 37 | SkString onShortName() override { return SkString("persp_images"); } |
| 38 | |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 39 | SkISize onISize() override { return SkISize::Make(1150, 1280); } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 40 | |
| 41 | void onOnceBeforeDraw() override { |
| 42 | fImages.push_back(make_image1()); |
| 43 | fImages.push_back(make_image2()); |
| 44 | } |
| 45 | |
| 46 | void onDraw(SkCanvas* canvas) override { |
| 47 | SkTDArray<SkMatrix> matrices; |
| 48 | matrices.push()->setAll(1.f, 0.f, 0.f, |
| 49 | 0.f, 1.f, 0.f, |
| 50 | 0.f, 0.005f, 1.f); |
| 51 | matrices.push()->setAll(1.f, 0.f, 0.f, |
| 52 | 0.f, 1.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); |
| 58 | SkPaint paint; |
| 59 | int n = 0; |
| 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) { |
| 64 | SkRect temp; |
| 65 | m.mapRect(&temp, imgB); |
| 66 | bounds.join(temp); |
| 67 | } |
| 68 | } |
| 69 | canvas->translate(-bounds.fLeft + 10.f, -bounds.fTop + 10.f); |
| 70 | canvas->save(); |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 71 | enum class DrawType { |
| 72 | kDrawImage, |
| 73 | kDrawImageRectStrict, |
| 74 | kDrawImageRectFast, |
| 75 | }; |
| 76 | for (auto type : |
| 77 | {DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) { |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 78 | for (const auto& m : matrices) { |
| 79 | for (auto aa : {false, true}) { |
| 80 | paint.setAntiAlias(aa); |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 81 | for (auto sampling : { |
| 82 | SkSamplingOptions(SkFilterMode::kNearest), |
| 83 | SkSamplingOptions(SkFilterMode::kLinear), |
| 84 | SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear), |
Mike Reed | f3ac2af | 2021-02-05 12:55:38 -0500 | [diff] [blame] | 85 | SkSamplingOptions(SkCubicResampler::Mitchell())}) { |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 86 | for (const auto& img : fImages) { |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 87 | canvas->save(); |
| 88 | canvas->concat(m); |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 89 | SkRect src = {img->width() / 4.f, img->height() / 4.f, |
| 90 | 3.f * img->width() / 4.f, 3.f * img->height() / 4}; |
| 91 | SkRect dst = {0, 0, |
| 92 | 3.f / 4.f * img->width(), 3.f / 4.f * img->height()}; |
| 93 | switch (type) { |
| 94 | case DrawType::kDrawImage: |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 95 | canvas->drawImage(img, 0, 0, sampling, &paint); |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 96 | break; |
| 97 | case DrawType::kDrawImageRectStrict: |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 98 | canvas->drawImageRect(img, src, dst, sampling, &paint, |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 99 | SkCanvas::kStrict_SrcRectConstraint); |
| 100 | break; |
| 101 | case DrawType::kDrawImageRectFast: |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 102 | canvas->drawImageRect(img, src, dst, sampling, &paint, |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 103 | SkCanvas::kFast_SrcRectConstraint); |
| 104 | break; |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 105 | } |
| 106 | canvas->restore(); |
| 107 | ++n; |
| 108 | if (n < 8) { |
| 109 | canvas->translate(bounds.width() + 10.f, 0); |
| 110 | } else { |
| 111 | canvas->restore(); |
| 112 | canvas->translate(0, bounds.height() + 10.f); |
| 113 | canvas->save(); |
| 114 | n = 0; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | canvas->restore(); |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | static constexpr int kNumImages = 4; |
| 126 | SkTArray<sk_sp<SkImage>> fImages; |
| 127 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 128 | using INHERITED = GM; |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | ////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | DEF_GM(return new PerspImages();) |
| 134 | |
| 135 | } // namespace skiagm |