Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
perspimages.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkRect.h"
15#include "include/core/SkSize.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
26using namespace skia_private;
27
29 return ToolUtils::GetResourceAsImage("images/mandrill_128.png");
30}
31
33 return ToolUtils::GetResourceAsImage("images/brickwork-texture.jpg")
34 ->makeSubset(nullptr, {0, 0, 128, 128});
35}
36
37namespace skiagm {
38
39class PerspImages : public GM {
40public:
41 PerspImages() = default;
42
43protected:
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);
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 {
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 : {
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,
109 break;
110 case DrawType::kDrawImageRectFast:
111 canvas->drawImageRect(img, src, dst, sampling, &paint,
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
134private:
135 inline static constexpr int kNumImages = 4;
136 TArray<sk_sp<SkImage>> fImages;
137
138 using INHERITED = GM;
139};
140
141//////////////////////////////////////////////////////////////////////////////
142
143DEF_GM(return new PerspImages();)
144
145} // namespace skiagm
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition SkCanvas.h:1542
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition SkCanvas.h:1543
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
int save()
Definition SkCanvas.cpp:451
void concat(const SkMatrix &matrix)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
virtual sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const =0
T * append()
Definition SkTDArray.h:191
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
void onOnceBeforeDraw() override
const Paint & paint
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkImage > MakeTextureImage(SkCanvas *canvas, sk_sp< SkImage > orig)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
static sk_sp< SkImage > make_image2()
static sk_sp< SkImage > make_image1()
static constexpr SkCubicResampler Mitchell()
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeEmpty()
Definition SkRect.h:595
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609