blob: 0261199eb1410c1ffd1e92b90b7adeb56a409a88 [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 Kleinea3f0142019-03-20 11:12:10 -050044 fCheckerboard =
45 SkImage::MakeFromBitmap(ToolUtils::create_checkerboard_bitmap(80, 80, c1, c2, 8));
46 fSmall = SkImage::MakeFromBitmap(ToolUtils::create_checkerboard_bitmap(64, 64, c1, c2, 8));
47 fLarge = SkImage::MakeFromBitmap(ToolUtils::create_checkerboard_bitmap(96, 96, c1, c2, 8));
48 fLargeW = SkImage::MakeFromBitmap(ToolUtils::create_checkerboard_bitmap(96, 64, c1, c2, 8));
49 fLargeH = SkImage::MakeFromBitmap(ToolUtils::create_checkerboard_bitmap(64, 96, c1, c2, 8));
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000050 }
51
robertphillips943a4622015-09-03 13:32:33 -070052 SkISize onISize() override {
senorblanco00502372016-01-21 09:55:47 -080053 return SkISize::Make(600, 500);
sugoi@google.com781cc762013-01-15 15:40:19 +000054 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000055
robertphillips943a4622015-09-03 13:32:33 -070056 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
sugoi@google.com781cc762013-01-15 15:40:19 +000057 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000058 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000059 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
60 canvas->drawBitmap(fBitmap, 0, 0, &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000061 canvas->restore();
62 }
63
robertphillips943a4622015-09-03 13:32:33 -070064 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070065 canvas->clear(SK_ColorBLACK);
sugoi@google.com781cc762013-01-15 15:40:19 +000066 SkPaint paint;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040067 sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard));
68 paint.setImageFilter(SkImageFilters::DisplacementMap(
69 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070070 this->drawClippedBitmap(canvas, 0, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040071 paint.setImageFilter(SkImageFilters::DisplacementMap(
72 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070073 this->drawClippedBitmap(canvas, 100, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040074 paint.setImageFilter(SkImageFilters::DisplacementMap(
75 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070076 this->drawClippedBitmap(canvas, 200, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040077 paint.setImageFilter(SkImageFilters::DisplacementMap(
78 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070079 this->drawClippedBitmap(canvas, 300, 0, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040080 paint.setImageFilter(SkImageFilters::DisplacementMap(
81 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070082 this->drawClippedBitmap(canvas, 400, 0, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000083
Michael Ludwig898bbfa2019-08-02 15:21:23 -040084 paint.setImageFilter(SkImageFilters::DisplacementMap(
85 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070086 this->drawClippedBitmap(canvas, 0, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040087 paint.setImageFilter(SkImageFilters::DisplacementMap(
88 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070089 this->drawClippedBitmap(canvas, 100, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040090 paint.setImageFilter(SkImageFilters::DisplacementMap(
91 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070092 this->drawClippedBitmap(canvas, 200, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040093 paint.setImageFilter(SkImageFilters::DisplacementMap(
94 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070095 this->drawClippedBitmap(canvas, 300, 100, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040096 paint.setImageFilter(SkImageFilters::DisplacementMap(
97 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -070098 this->drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000099
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400100 SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40);
101 paint.setImageFilter(SkImageFilters::DisplacementMap(
102 SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700103 this->drawClippedBitmap(canvas, 0, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400104 paint.setImageFilter(SkImageFilters::DisplacementMap(
105 SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700106 this->drawClippedBitmap(canvas, 100, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400107 paint.setImageFilter(SkImageFilters::DisplacementMap(
108 SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700109 this->drawClippedBitmap(canvas, 200, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400110 paint.setImageFilter(SkImageFilters::DisplacementMap(
111 SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700112 this->drawClippedBitmap(canvas, 300, 200, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400113 paint.setImageFilter(SkImageFilters::DisplacementMap(
114 SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700115 this->drawClippedBitmap(canvas, 400, 200, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000116
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400117 paint.setImageFilter(SkImageFilters::DisplacementMap(
118 SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700119 this->drawClippedBitmap(canvas, 0, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400120 paint.setImageFilter(SkImageFilters::DisplacementMap(
121 SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700122 this->drawClippedBitmap(canvas, 100, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400123 paint.setImageFilter(SkImageFilters::DisplacementMap(
124 SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700125 this->drawClippedBitmap(canvas, 200, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400126 paint.setImageFilter(SkImageFilters::DisplacementMap(
127 SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700128 this->drawClippedBitmap(canvas, 300, 300, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400129 paint.setImageFilter(SkImageFilters::DisplacementMap(
130 SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700131 this->drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000132
senorblanco00502372016-01-21 09:55:47 -0800133 // Test for negative scale.
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400134 paint.setImageFilter(SkImageFilters::DisplacementMap(
135 SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr));
senorblanco00502372016-01-21 09:55:47 -0800136 this->drawClippedBitmap(canvas, 500, 0, paint);
137
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000138 // Tests for images of different sizes
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400139 displ = SkImageFilters::Image(fSmall);
140 paint.setImageFilter(SkImageFilters::DisplacementMap(
141 SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700142 this->drawClippedBitmap(canvas, 0, 400, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400143 displ = SkImageFilters::Image(fLarge);
144 paint.setImageFilter(SkImageFilters::DisplacementMap(
145 SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700146 this->drawClippedBitmap(canvas, 100, 400, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400147 displ = SkImageFilters::Image(fLargeW);
148 paint.setImageFilter(SkImageFilters::DisplacementMap(
149 SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700150 this->drawClippedBitmap(canvas, 200, 400, paint);
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400151 displ = SkImageFilters::Image(fLargeH);
152 paint.setImageFilter(SkImageFilters::DisplacementMap(
153 SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700154 this->drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000155
156 // Test for no given displacement input. In this case, both displacement
157 // and color should use the same bitmap, given to SkCanvas::drawBitmap()
158 // as an input argument.
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400159 paint.setImageFilter(SkImageFilters::DisplacementMap(
160 SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700161 this->drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000162 }
163
164private:
fmalita5598b632015-09-15 11:26:13 -0700165 SkBitmap fBitmap;
reed9ce9d672016-03-17 10:51:11 -0700166 sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
robertphillips943a4622015-09-03 13:32:33 -0700167
168 typedef GM INHERITED;
sugoi@google.com781cc762013-01-15 15:40:19 +0000169};
170
171//////////////////////////////////////////////////////////////////////////////
172
robertphillips943a4622015-09-03 13:32:33 -0700173DEF_GM(return new DisplacementMapGM;)
sugoi@google.com781cc762013-01-15 15:40:19 +0000174}