| 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 |
|
| 8 |
#include "gm/gm.h" |
| 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/base/SkTArray.h" |
| 18 |
#include "include/private/base/SkTDArray.h" |
| 19 |
#include "tools/DecodeUtils.h" |
| 20 |
#include "tools/GpuToolUtils.h" |
| 21 |
#include "tools/Resources.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
|
| 24 |
#include <initializer_list> |
| 25 |
|
| 26 |
using namespace skia_private; |
| 27 |
|
| 28 |
static sk_sp<SkImage> make_image1() { |
| 29 |
return ToolUtils::GetResourceAsImage("images/mandrill_128.png"); |
| 30 |
} |
| 31 |
|
| 32 |
static sk_sp<SkImage> make_image2() { |
| 33 |
return ToolUtils::GetResourceAsImage("images/brickwork-texture.jpg") |
| 34 |
->makeSubset(nullptr, {0, 0, 128, 128}, {}); |
| 35 |
} |
| 36 |
|
| 37 |
namespace skiagm { |
| 38 |
|
| 39 |
class PerspImages : public GM { |
| 40 |
public: |
| 41 |
PerspImages() = default; |
| 42 |
|
| 43 |
protected: |
| 44 |
SkString getName() const override { return SkString("persp_images"); } |
| 45 |
|
| 46 |
SkISize getISize() override { return SkISize::Make(1150, 1280); } |
| 47 |
|
| 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; |
| 55 |
matrices.append()->setAll(1.f, 0.f, 0.f, |
| 56 |
0.f, 1.f, 0.f, |
| 57 |
0.f, 0.005f, 1.f); |
| 58 |
matrices.append()->setAll(1.f, 0.f, 0.f, |
| 59 |
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(); |
| 78 |
enum class DrawType { |
| 79 |
kDrawImage, |
| 80 |
kDrawImageRectStrict, |
| 81 |
kDrawImageRectFast, |
| 82 |
}; |
| 83 |
for (auto type : |
| 84 |
{DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) { |
| 85 |
for (const auto& m : matrices) { |
| 86 |
for (auto aa : {false, true}) { |
| 87 |
paint.setAntiAlias(aa); |
| 88 |
for (auto sampling : { |
| 89 |
SkSamplingOptions(SkFilterMode::kNearest), |
| 90 |
SkSamplingOptions(SkFilterMode::kLinear), |
| 91 |
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear), |
| 92 |
SkSamplingOptions(SkCubicResampler::Mitchell())}) { |
| 93 |
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(); |
| 116 |
} |
| 117 |
++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 |
|
| 134 |
private: |
| 135 |
inline static constexpr int kNumImages = 4; |
| 136 |
TArray<sk_sp<SkImage>> fImages; |
| 137 |
|
| 138 |
using INHERITED = GM; |
| 139 |
}; |
| 140 |
|
| 141 |
////////////////////////////////////////////////////////////////////////////// |
| 142 |
|
| 143 |
DEF_GM(return new PerspImages();) |
| 144 |
|
| 145 |
} // namespace skiagm |