sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #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 Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 20 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "tools/ToolUtils.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 22 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include <utility> |
| 24 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
| 27 | class DisplacementMapGM : public GM { |
| 28 | public: |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 29 | DisplacementMapGM() { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 30 | this->setBGColor(0xFF000000); |
| 31 | } |
| 32 | |
| 33 | protected: |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 35 | return SkString("displacement"); |
| 36 | } |
| 37 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 38 | void onOnceBeforeDraw() override { |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 39 | fImage = ToolUtils::create_string_image(80, 80, 0xFF884422, 15, 55, 96, "g"); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 40 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 41 | SkColor c1 = ToolUtils::color_to_565(0xFF244484); |
| 42 | SkColor c2 = ToolUtils::color_to_565(0xFF804020); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 43 | |
Mike Reed | db873dd | 2020-12-04 12:22:10 -0500 | [diff] [blame] | 44 | 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.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 49 | } |
| 50 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 51 | SkISize onISize() override { |
senorblanco | 0050237 | 2016-01-21 09:55:47 -0800 | [diff] [blame] | 52 | return SkISize::Make(600, 500); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 53 | } |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 54 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 55 | void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 56 | canvas->save(); |
senorblanco@chromium.org | bc386ce | 2013-10-15 19:30:58 +0000 | [diff] [blame] | 57 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 58 | canvas->clipIRect(fImage->bounds()); |
| 59 | canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 60 | canvas->restore(); |
| 61 | } |
| 62 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 63 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 64 | canvas->clear(SK_ColorBLACK); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 65 | SkPaint paint; |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 66 | sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard)); |
| 67 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 68 | SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 69 | this->drawClippedBitmap(canvas, 0, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 70 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 71 | SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 72 | this->drawClippedBitmap(canvas, 100, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 73 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 74 | SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 75 | this->drawClippedBitmap(canvas, 200, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 76 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 77 | SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 78 | this->drawClippedBitmap(canvas, 300, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 79 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 80 | SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 81 | this->drawClippedBitmap(canvas, 400, 0, paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 82 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 83 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 84 | SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 85 | this->drawClippedBitmap(canvas, 0, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 86 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 87 | SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 88 | this->drawClippedBitmap(canvas, 100, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 89 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 90 | SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 91 | this->drawClippedBitmap(canvas, 200, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 92 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 93 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 94 | this->drawClippedBitmap(canvas, 300, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 95 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 96 | SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 97 | this->drawClippedBitmap(canvas, 400, 100, paint); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 98 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 99 | SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40); |
| 100 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 101 | SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 102 | this->drawClippedBitmap(canvas, 0, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 103 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 104 | SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 105 | this->drawClippedBitmap(canvas, 100, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 106 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 107 | SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 108 | this->drawClippedBitmap(canvas, 200, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 109 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 110 | SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 111 | this->drawClippedBitmap(canvas, 300, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 112 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 113 | SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 114 | this->drawClippedBitmap(canvas, 400, 200, paint); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 115 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 116 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 117 | SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 118 | this->drawClippedBitmap(canvas, 0, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 119 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 120 | SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 121 | this->drawClippedBitmap(canvas, 100, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 122 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 123 | SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 124 | this->drawClippedBitmap(canvas, 200, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 125 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 126 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 127 | this->drawClippedBitmap(canvas, 300, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 128 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 129 | SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 130 | this->drawClippedBitmap(canvas, 400, 300, paint); |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 131 | |
senorblanco | 0050237 | 2016-01-21 09:55:47 -0800 | [diff] [blame] | 132 | // Test for negative scale. |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 133 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 134 | SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr)); |
senorblanco | 0050237 | 2016-01-21 09:55:47 -0800 | [diff] [blame] | 135 | this->drawClippedBitmap(canvas, 500, 0, paint); |
| 136 | |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 137 | // Tests for images of different sizes |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 138 | displ = SkImageFilters::Image(fSmall); |
| 139 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 140 | SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 141 | this->drawClippedBitmap(canvas, 0, 400, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 142 | displ = SkImageFilters::Image(fLarge); |
| 143 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 144 | SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 145 | this->drawClippedBitmap(canvas, 100, 400, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 146 | displ = SkImageFilters::Image(fLargeW); |
| 147 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 148 | SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 149 | this->drawClippedBitmap(canvas, 200, 400, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 150 | displ = SkImageFilters::Image(fLargeH); |
| 151 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 152 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 153 | this->drawClippedBitmap(canvas, 300, 400, paint); |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 154 | |
| 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 Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 158 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 159 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 160 | this->drawClippedBitmap(canvas, 400, 400, paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 164 | sk_sp<SkImage> fImage; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 165 | sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 166 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 167 | using INHERITED = GM; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | ////////////////////////////////////////////////////////////////////////////// |
| 171 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 172 | DEF_GM(return new DisplacementMapGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 173 | } // namespace skiagm |