Flutter Engine
The Flutter Engine
displacement.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
15#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
21#include "tools/ToolUtils.h"
23
24#include <utility>
25
26namespace skiagm {
27
28class DisplacementMapGM : public GM {
29public:
31 this->setBGColor(0xFF000000);
32 }
33
34protected:
35 SkString getName() const override { return SkString("displacement"); }
36
37 void onOnceBeforeDraw() override {
38 fImage = ToolUtils::CreateStringImage(80, 80, 0xFF884422, 15, 55, 96, "g");
39
40 SkColor c1 = ToolUtils::color_to_565(0xFF244484);
41 SkColor c2 = ToolUtils::color_to_565(0xFF804020);
42
43 fCheckerboard = ToolUtils::create_checkerboard_image(80, 80, c1, c2, 8);
44 fSmall = ToolUtils::create_checkerboard_image(64, 64, c1, c2, 8);
45 fLarge = ToolUtils::create_checkerboard_image(96, 96, c1, c2, 8);
46 fLargeW = ToolUtils::create_checkerboard_image(96, 64, c1, c2, 8);
47 fLargeH = ToolUtils::create_checkerboard_image(64, 96, c1, c2, 8);
48 }
49
50 SkISize getISize() override { return SkISize::Make(600, 500); }
51
52 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
53 canvas->save();
55 canvas->clipIRect(fImage->bounds());
56 canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
57 canvas->restore();
58 }
59
60 void onDraw(SkCanvas* canvas) override {
61 canvas->clear(SK_ColorBLACK);
65 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr));
66 this->drawClippedBitmap(canvas, 0, 0, paint);
68 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr));
69 this->drawClippedBitmap(canvas, 100, 0, paint);
71 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr));
72 this->drawClippedBitmap(canvas, 200, 0, paint);
74 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr));
75 this->drawClippedBitmap(canvas, 300, 0, paint);
77 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr));
78 this->drawClippedBitmap(canvas, 400, 0, paint);
79
81 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr));
82 this->drawClippedBitmap(canvas, 0, 100, paint);
84 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr));
85 this->drawClippedBitmap(canvas, 100, 100, paint);
87 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr));
88 this->drawClippedBitmap(canvas, 200, 100, paint);
90 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr));
91 this->drawClippedBitmap(canvas, 300, 100, paint);
93 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr));
94 this->drawClippedBitmap(canvas, 400, 100, paint);
95
96 SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40);
98 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect));
99 this->drawClippedBitmap(canvas, 0, 200, paint);
101 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect));
102 this->drawClippedBitmap(canvas, 100, 200, paint);
104 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect));
105 this->drawClippedBitmap(canvas, 200, 200, paint);
107 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect));
108 this->drawClippedBitmap(canvas, 300, 200, paint);
110 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect));
111 this->drawClippedBitmap(canvas, 400, 200, paint);
112
114 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect));
115 this->drawClippedBitmap(canvas, 0, 300, paint);
117 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
118 this->drawClippedBitmap(canvas, 100, 300, paint);
120 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect));
121 this->drawClippedBitmap(canvas, 200, 300, paint);
123 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
124 this->drawClippedBitmap(canvas, 300, 300, paint);
126 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
127 this->drawClippedBitmap(canvas, 400, 300, paint);
128
129 // Test for negative scale.
131 SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr));
132 this->drawClippedBitmap(canvas, 500, 0, paint);
133
134 // Tests for images of different sizes
137 SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr));
138 this->drawClippedBitmap(canvas, 0, 400, paint);
141 SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
142 this->drawClippedBitmap(canvas, 100, 400, paint);
145 SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr));
146 this->drawClippedBitmap(canvas, 200, 400, paint);
149 SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
150 this->drawClippedBitmap(canvas, 300, 400, paint);
151
152 // Test for no given displacement input. In this case, both displacement
153 // and color should use the same bitmap, given to SkCanvas::drawBitmap()
154 // as an input argument.
156 SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr));
157 this->drawClippedBitmap(canvas, 400, 400, paint);
158 }
159
160private:
161 sk_sp<SkImage> fImage;
162 sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
163
164 using INHERITED = GM;
165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169DEF_GM(return new DisplacementMapGM;)
170} // namespace skiagm
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:991
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkImageFilter > DisplacementMap(SkColorChannel xChannelSelector, SkColorChannel yChannelSelector, SkScalar scale, sk_sp< SkImageFilter > displacement, sk_sp< SkImageFilter > color, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
SkIRect bounds() const
Definition: SkImage.h:303
SkISize getISize() override
void onOnceBeforeDraw() override
void onDraw(SkCanvas *canvas) override
void drawClippedBitmap(SkCanvas *canvas, int x, int y, const SkPaint &paint) const
SkString getName() const override
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
double y
double x
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:168
sk_sp< SkImage > CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, const char *str)
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkRect.h:32
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20