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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 39 | fBitmap = ToolUtils::create_string_bitmap(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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 44 | 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.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 50 | } |
| 51 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 52 | SkISize onISize() override { |
senorblanco | 0050237 | 2016-01-21 09:55:47 -0800 | [diff] [blame] | 53 | return SkISize::Make(600, 500); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 54 | } |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 55 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 56 | 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] | 57 | canvas->save(); |
senorblanco@chromium.org | bc386ce | 2013-10-15 19:30:58 +0000 | [diff] [blame] | 58 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 59 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 60 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 61 | canvas->restore(); |
| 62 | } |
| 63 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 64 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 65 | canvas->clear(SK_ColorBLACK); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 66 | SkPaint paint; |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 67 | sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard)); |
| 68 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 69 | SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 70 | this->drawClippedBitmap(canvas, 0, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 71 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 72 | SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 73 | this->drawClippedBitmap(canvas, 100, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 74 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 75 | SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 76 | this->drawClippedBitmap(canvas, 200, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 77 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 78 | SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 79 | this->drawClippedBitmap(canvas, 300, 0, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 80 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 81 | SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 82 | this->drawClippedBitmap(canvas, 400, 0, paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 83 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 84 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 85 | SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 86 | this->drawClippedBitmap(canvas, 0, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 87 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 88 | SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 89 | this->drawClippedBitmap(canvas, 100, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 90 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 91 | SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 92 | this->drawClippedBitmap(canvas, 200, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 93 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 94 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 95 | this->drawClippedBitmap(canvas, 300, 100, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 96 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 97 | SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 98 | this->drawClippedBitmap(canvas, 400, 100, paint); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 99 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 100 | SkIRect cropRect = SkIRect::MakeXYWH(30, 30, 40, 40); |
| 101 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 102 | SkColorChannel::kR, SkColorChannel::kG, 0.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 103 | this->drawClippedBitmap(canvas, 0, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 104 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 105 | SkColorChannel::kB, SkColorChannel::kA, 16.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 106 | this->drawClippedBitmap(canvas, 100, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 107 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 108 | SkColorChannel::kR, SkColorChannel::kB, 32.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 109 | this->drawClippedBitmap(canvas, 200, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 110 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 111 | SkColorChannel::kG, SkColorChannel::kA, 48.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 112 | this->drawClippedBitmap(canvas, 300, 200, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 113 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 114 | SkColorChannel::kR, SkColorChannel::kA, 64.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 115 | this->drawClippedBitmap(canvas, 400, 200, paint); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 116 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 117 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 118 | SkColorChannel::kR, SkColorChannel::kG, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 119 | this->drawClippedBitmap(canvas, 0, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 120 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 121 | SkColorChannel::kB, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 122 | this->drawClippedBitmap(canvas, 100, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 123 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 124 | SkColorChannel::kR, SkColorChannel::kB, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 125 | this->drawClippedBitmap(canvas, 200, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 126 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 127 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 128 | this->drawClippedBitmap(canvas, 300, 300, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 129 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 130 | SkColorChannel::kR, SkColorChannel::kA, 40.0f, displ, nullptr, &cropRect)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 131 | this->drawClippedBitmap(canvas, 400, 300, paint); |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 132 | |
senorblanco | 0050237 | 2016-01-21 09:55:47 -0800 | [diff] [blame] | 133 | // Test for negative scale. |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 134 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 135 | SkColorChannel::kG, SkColorChannel::kA, -40.0f, displ, nullptr)); |
senorblanco | 0050237 | 2016-01-21 09:55:47 -0800 | [diff] [blame] | 136 | this->drawClippedBitmap(canvas, 500, 0, paint); |
| 137 | |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 138 | // Tests for images of different sizes |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 139 | displ = SkImageFilters::Image(fSmall); |
| 140 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 141 | SkColorChannel::kR, SkColorChannel::kG, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 142 | this->drawClippedBitmap(canvas, 0, 400, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 143 | displ = SkImageFilters::Image(fLarge); |
| 144 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 145 | SkColorChannel::kB, SkColorChannel::kA, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 146 | this->drawClippedBitmap(canvas, 100, 400, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 147 | displ = SkImageFilters::Image(fLargeW); |
| 148 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 149 | SkColorChannel::kR, SkColorChannel::kB, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 150 | this->drawClippedBitmap(canvas, 200, 400, paint); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 151 | displ = SkImageFilters::Image(fLargeH); |
| 152 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 153 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, std::move(displ), nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 154 | this->drawClippedBitmap(canvas, 300, 400, paint); |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 155 | |
| 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 Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 159 | paint.setImageFilter(SkImageFilters::DisplacementMap( |
| 160 | SkColorChannel::kG, SkColorChannel::kA, 40.0f, nullptr, nullptr)); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 161 | this->drawClippedBitmap(canvas, 400, 400, paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | private: |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 165 | SkBitmap fBitmap; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 166 | sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 167 | |
| 168 | typedef GM INHERITED; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | ////////////////////////////////////////////////////////////////////////////// |
| 172 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 173 | DEF_GM(return new DisplacementMapGM;) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 174 | } |