blob: a67fcbc9d593d1f032aca4f43c515611eecf9d2e [file] [log] [blame]
sugoi@google.com781cc762013-01-15 15:40:19 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkImageFilter.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040020#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040022#include "tools/fonts/FontToolUtils.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <utility>
25
sugoi@google.com781cc762013-01-15 15:40:19 +000026namespace skiagm {
27
28class DisplacementMapGM : public GM {
29public:
robertphillips943a4622015-09-03 13:32:33 -070030 DisplacementMapGM() {
sugoi@google.com781cc762013-01-15 15:40:19 +000031 this->setBGColor(0xFF000000);
32 }
33
34protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000035 SkString getName() const override { return SkString("displacement"); }
sugoi@google.com781cc762013-01-15 15:40:19 +000036
robertphillips943a4622015-09-03 13:32:33 -070037 void onOnceBeforeDraw() override {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040038 fImage = ToolUtils::CreateStringImage(80, 80, 0xFF884422, 15, 55, 96, "g");
robertphillips943a4622015-09-03 13:32:33 -070039
Mike Kleinea3f0142019-03-20 11:12:10 -050040 SkColor c1 = ToolUtils::color_to_565(0xFF244484);
41 SkColor c2 = ToolUtils::color_to_565(0xFF804020);
robertphillips943a4622015-09-03 13:32:33 -070042
Mike Reeddb873dd2020-12-04 12:22:10 -050043 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);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000048 }
49
Leandro Lovisolo8f023882023-08-15 21:13:52 +000050 SkISize getISize() override { return SkISize::Make(600, 500); }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000051
robertphillips943a4622015-09-03 13:32:33 -070052 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
sugoi@google.com781cc762013-01-15 15:40:19 +000053 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000054 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
Mike Reed568f0ae2021-01-24 08:57:23 -050055 canvas->clipIRect(fImage->bounds());
56 canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000057 canvas->restore();
58 }
59
robertphillips943a4622015-09-03 13:32:33 -070060 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070061 canvas->clear(SK_ColorBLACK);
sugoi@google.com781cc762013-01-15 15:40:19 +000062 SkPaint paint;
Michael Ludwig642f2182023-05-30 20:57:39 -040063 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard, SkFilterMode::kLinear));
Michael Ludwig898bbfa2019-08-02 15:21:23 -040064 paint.setImageFilter(SkImageFilters::DisplacementMap(
65 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070066 this->drawClippedBitmap(canvas, 0, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040067 paint.setImageFilter(SkImageFilters::DisplacementMap(
68 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070069 this->drawClippedBitmap(canvas, 100, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040070 paint.setImageFilter(SkImageFilters::DisplacementMap(
71 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070072 this->drawClippedBitmap(canvas, 200, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040073 paint.setImageFilter(SkImageFilters::DisplacementMap(
74 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070075 this->drawClippedBitmap(canvas, 300, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040076 paint.setImageFilter(SkImageFilters::DisplacementMap(
77 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070078 this->drawClippedBitmap(canvas, 400, 0, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000079
Michael Ludwig898bbfa2019-08-02 15:21:23 -040080 paint.setImageFilter(SkImageFilters::DisplacementMap(
81 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070082 this->drawClippedBitmap(canvas, 0, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040083 paint.setImageFilter(SkImageFilters::DisplacementMap(
84 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070085 this->drawClippedBitmap(canvas, 100, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040086 paint.setImageFilter(SkImageFilters::DisplacementMap(
87 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070088 this->drawClippedBitmap(canvas, 200, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040089 paint.setImageFilter(SkImageFilters::DisplacementMap(
90 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070091 this->drawClippedBitmap(canvas, 300, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040092 paint.setImageFilter(SkImageFilters::DisplacementMap(
93 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070094 this->drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000095
Michael Ludwig898bbfa2019-08-02 15:21:23 -040096 SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40);
97 paint.setImageFilter(SkImageFilters::DisplacementMap(
98 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -070099 this->drawClippedBitmap(canvas, 0, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400100 paint.setImageFilter(SkImageFilters::DisplacementMap(
101 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700102 this->drawClippedBitmap(canvas, 100, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400103 paint.setImageFilter(SkImageFilters::DisplacementMap(
104 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700105 this->drawClippedBitmap(canvas, 200, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400106 paint.setImageFilter(SkImageFilters::DisplacementMap(
107 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700108 this->drawClippedBitmap(canvas, 300, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400109 paint.setImageFilter(SkImageFilters::DisplacementMap(
110 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700111 this->drawClippedBitmap(canvas, 400, 200, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000112
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400113 paint.setImageFilter(SkImageFilters::DisplacementMap(
114 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700115 this->drawClippedBitmap(canvas, 0, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400116 paint.setImageFilter(SkImageFilters::DisplacementMap(
117 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700118 this->drawClippedBitmap(canvas, 100, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400119 paint.setImageFilter(SkImageFilters::DisplacementMap(
120 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700121 this->drawClippedBitmap(canvas, 200, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400122 paint.setImageFilter(SkImageFilters::DisplacementMap(
123 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700124 this->drawClippedBitmap(canvas, 300, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400125 paint.setImageFilter(SkImageFilters::DisplacementMap(
126 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700127 this->drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000128
senorblanco00502372016-01-21 09:55:47 -0800129 // Test for negative scale.
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400130 paint.setImageFilter(SkImageFilters::DisplacementMap(
131 SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr));
senorblanco00502372016-01-21 09:55:47 -0800132 this->drawClippedBitmap(canvas, 500, 0, paint);
133
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000134 // Tests for images of different sizes
Michael Ludwig642f2182023-05-30 20:57:39 -0400135 displ = SkImageFilters::Image(fSmall, SkFilterMode::kLinear);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400136 paint.setImageFilter(SkImageFilters::DisplacementMap(
137 SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700138 this->drawClippedBitmap(canvas, 0, 400, paint);
Michael Ludwig642f2182023-05-30 20:57:39 -0400139 displ = SkImageFilters::Image(fLarge, SkFilterMode::kLinear);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400140 paint.setImageFilter(SkImageFilters::DisplacementMap(
141 SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700142 this->drawClippedBitmap(canvas, 100, 400, paint);
Michael Ludwig642f2182023-05-30 20:57:39 -0400143 displ = SkImageFilters::Image(fLargeW, SkFilterMode::kLinear);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400144 paint.setImageFilter(SkImageFilters::DisplacementMap(
145 SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700146 this->drawClippedBitmap(canvas, 200, 400, paint);
Michael Ludwig642f2182023-05-30 20:57:39 -0400147 displ = SkImageFilters::Image(fLargeH, SkFilterMode::kLinear);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400148 paint.setImageFilter(SkImageFilters::DisplacementMap(
149 SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700150 this->drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000151
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.
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400155 paint.setImageFilter(SkImageFilters::DisplacementMap(
156 SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700157 this->drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000158 }
159
160private:
Mike Reed568f0ae2021-01-24 08:57:23 -0500161 sk_sp<SkImage> fImage;
reed9ce9d672016-03-17 10:51:11 -0700162 sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
robertphillips943a4622015-09-03 13:32:33 -0700163
John Stiles7571f9e2020-09-02 22:42:33 -0400164 using INHERITED = GM;
sugoi@google.com781cc762013-01-15 15:40:19 +0000165};
166
167//////////////////////////////////////////////////////////////////////////////
168
robertphillips943a4622015-09-03 13:32:33 -0700169DEF_GM(return new DisplacementMapGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400170} // namespace skiagm