blob: bfa5605351d7d7eedc52b2ff3511c936cba26026 [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"
sugoi@google.com781cc762013-01-15 15:40:19 +000022
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include <utility>
24
sugoi@google.com781cc762013-01-15 15:40:19 +000025namespace skiagm {
26
27class DisplacementMapGM : public GM {
28public:
robertphillips943a4622015-09-03 13:32:33 -070029 DisplacementMapGM() {
sugoi@google.com781cc762013-01-15 15:40:19 +000030 this->setBGColor(0xFF000000);
31 }
32
33protected:
robertphillips943a4622015-09-03 13:32:33 -070034 SkString onShortName() override {
sugoi@google.com781cc762013-01-15 15:40:19 +000035 return SkString("displacement");
36 }
37
robertphillips943a4622015-09-03 13:32:33 -070038 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050039 fBitmap = ToolUtils::create_string_bitmap(80, 80, 0xFF884422, 15, 55, 96, "g");
robertphillips943a4622015-09-03 13:32:33 -070040
Mike Kleinea3f0142019-03-20 11:12:10 -050041 SkColor c1 = ToolUtils::color_to_565(0xFF244484);
42 SkColor c2 = ToolUtils::color_to_565(0xFF804020);
robertphillips943a4622015-09-03 13:32:33 -070043
Mike Reeddb873dd2020-12-04 12:22:10 -050044 fCheckerboard = ToolUtils::create_checkerboard_image(80, 80, c1, c2, 8);
45 fSmall = ToolUtils::create_checkerboard_image(64, 64, c1, c2, 8);
46 fLarge = ToolUtils::create_checkerboard_image(96, 96, c1, c2, 8);
47 fLargeW = ToolUtils::create_checkerboard_image(96, 64, c1, c2, 8);
48 fLargeH = ToolUtils::create_checkerboard_image(64, 96, c1, c2, 8);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000049 }
50
robertphillips943a4622015-09-03 13:32:33 -070051 SkISize onISize() override {
senorblanco00502372016-01-21 09:55:47 -080052 return SkISize::Make(600, 500);
sugoi@google.com781cc762013-01-15 15:40:19 +000053 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000054
robertphillips943a4622015-09-03 13:32:33 -070055 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
sugoi@google.com781cc762013-01-15 15:40:19 +000056 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000057 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000058 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
59 canvas->drawBitmap(fBitmap, 0, 0, &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000060 canvas->restore();
61 }
62
robertphillips943a4622015-09-03 13:32:33 -070063 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070064 canvas->clear(SK_ColorBLACK);
sugoi@google.com781cc762013-01-15 15:40:19 +000065 SkPaint paint;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040066 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
67 paint.setImageFilter(SkImageFilters::DisplacementMap(
68 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070069 this->drawClippedBitmap(canvas, 0, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040070 paint.setImageFilter(SkImageFilters::DisplacementMap(
71 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070072 this->drawClippedBitmap(canvas, 100, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040073 paint.setImageFilter(SkImageFilters::DisplacementMap(
74 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070075 this->drawClippedBitmap(canvas, 200, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040076 paint.setImageFilter(SkImageFilters::DisplacementMap(
77 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070078 this->drawClippedBitmap(canvas, 300, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040079 paint.setImageFilter(SkImageFilters::DisplacementMap(
80 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070081 this->drawClippedBitmap(canvas, 400, 0, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000082
Michael Ludwig898bbfa2019-08-02 15:21:23 -040083 paint.setImageFilter(SkImageFilters::DisplacementMap(
84 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070085 this->drawClippedBitmap(canvas, 0, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040086 paint.setImageFilter(SkImageFilters::DisplacementMap(
87 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070088 this->drawClippedBitmap(canvas, 100, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040089 paint.setImageFilter(SkImageFilters::DisplacementMap(
90 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070091 this->drawClippedBitmap(canvas, 200, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040092 paint.setImageFilter(SkImageFilters::DisplacementMap(
93 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070094 this->drawClippedBitmap(canvas, 300, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040095 paint.setImageFilter(SkImageFilters::DisplacementMap(
96 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070097 this->drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000098
Michael Ludwig898bbfa2019-08-02 15:21:23 -040099 SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40);
100 paint.setImageFilter(SkImageFilters::DisplacementMap(
101 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700102 this->drawClippedBitmap(canvas, 0, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400103 paint.setImageFilter(SkImageFilters::DisplacementMap(
104 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700105 this->drawClippedBitmap(canvas, 100, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400106 paint.setImageFilter(SkImageFilters::DisplacementMap(
107 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700108 this->drawClippedBitmap(canvas, 200, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400109 paint.setImageFilter(SkImageFilters::DisplacementMap(
110 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700111 this->drawClippedBitmap(canvas, 300, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400112 paint.setImageFilter(SkImageFilters::DisplacementMap(
113 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700114 this->drawClippedBitmap(canvas, 400, 200, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000115
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400116 paint.setImageFilter(SkImageFilters::DisplacementMap(
117 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700118 this->drawClippedBitmap(canvas, 0, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400119 paint.setImageFilter(SkImageFilters::DisplacementMap(
120 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700121 this->drawClippedBitmap(canvas, 100, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400122 paint.setImageFilter(SkImageFilters::DisplacementMap(
123 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700124 this->drawClippedBitmap(canvas, 200, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400125 paint.setImageFilter(SkImageFilters::DisplacementMap(
126 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700127 this->drawClippedBitmap(canvas, 300, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400128 paint.setImageFilter(SkImageFilters::DisplacementMap(
129 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700130 this->drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000131
senorblanco00502372016-01-21 09:55:47 -0800132 // Test for negative scale.
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400133 paint.setImageFilter(SkImageFilters::DisplacementMap(
134 SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr));
senorblanco00502372016-01-21 09:55:47 -0800135 this->drawClippedBitmap(canvas, 500, 0, paint);
136
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000137 // Tests for images of different sizes
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400138 displ = SkImageFilters::Image(fSmall);
139 paint.setImageFilter(SkImageFilters::DisplacementMap(
140 SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700141 this->drawClippedBitmap(canvas, 0, 400, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400142 displ = SkImageFilters::Image(fLarge);
143 paint.setImageFilter(SkImageFilters::DisplacementMap(
144 SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700145 this->drawClippedBitmap(canvas, 100, 400, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400146 displ = SkImageFilters::Image(fLargeW);
147 paint.setImageFilter(SkImageFilters::DisplacementMap(
148 SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700149 this->drawClippedBitmap(canvas, 200, 400, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400150 displ = SkImageFilters::Image(fLargeH);
151 paint.setImageFilter(SkImageFilters::DisplacementMap(
152 SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700153 this->drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000154
155 // Test for no given displacement input. In this case, both displacement
156 // and color should use the same bitmap, given to SkCanvas::drawBitmap()
157 // as an input argument.
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400158 paint.setImageFilter(SkImageFilters::DisplacementMap(
159 SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700160 this->drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000161 }
162
163private:
fmalita5598b632015-09-15 11:26:13 -0700164 SkBitmap fBitmap;
reed9ce9d672016-03-17 10:51:11 -0700165 sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
robertphillips943a4622015-09-03 13:32:33 -0700166
John Stiles7571f9e2020-09-02 22:42:33 -0400167 using INHERITED = GM;
sugoi@google.com781cc762013-01-15 15:40:19 +0000168};
169
170//////////////////////////////////////////////////////////////////////////////
171
robertphillips943a4622015-09-03 13:32:33 -0700172DEF_GM(return new DisplacementMapGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400173} // namespace skiagm