bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkImage.h" |
| 14 | #include "include/core/SkImageFilter.h" |
| 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkPixelRef.h" |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 18 | #include "include/core/SkRRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/core/SkRect.h" |
| 20 | #include "include/core/SkRefCnt.h" |
| 21 | #include "include/core/SkScalar.h" |
| 22 | #include "include/core/SkTypeface.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 23 | #include "include/effects/SkImageFilters.h" |
Kevin Lubick | 0d4d114 | 2023-02-13 09:13:10 -0500 | [diff] [blame] | 24 | #include "src/base/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 26 | #include "tools/fonts/FontToolUtils.h" |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 27 | #include "tools/timer/TimeUtils.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 28 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 29 | #include <utility> |
| 30 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 31 | #define WIDTH 500 |
| 32 | #define HEIGHT 500 |
| 33 | |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 34 | static void draw_content(SkCanvas* canvas, float maxTextSize, int count) { |
| 35 | const char* str = "The quick brown fox jumped over the lazy dog."; |
| 36 | SkRandom rand; |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 37 | SkFont font = ToolUtils::DefaultPortableFont(); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 38 | for (int i = 0; i < count; ++i) { |
| 39 | int x = rand.nextULessThan(WIDTH); |
| 40 | int y = rand.nextULessThan(HEIGHT); |
| 41 | SkPaint paint; |
| 42 | paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000)); |
| 43 | font.setSize(rand.nextRangeScalar(0, maxTextSize)); |
| 44 | canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint); |
| 45 | } |
| 46 | } |
| 47 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 48 | DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) { |
senorblanco | cbf6b6e | 2014-10-23 15:00:10 -0700 | [diff] [blame] | 49 | SkPaint filterPaint; |
| 50 | filterPaint.setImageFilter( |
Michael Ludwig | 0dc9e8d | 2023-05-02 16:23:56 -0400 | [diff] [blame] | 51 | SkImageFilters::Magnifier(SkRect::MakeWH(WIDTH, HEIGHT), 2.f, 100.f, |
Michael Ludwig | 2c49e60 | 2023-05-30 13:12:19 -0400 | [diff] [blame] | 52 | SkFilterMode::kLinear, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | canvas->saveLayer(nullptr, &filterPaint); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 54 | draw_content(canvas, 300.f, 25); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 55 | canvas->restore(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 56 | } |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 57 | |
| 58 | //////////////////////////////////////////////////////////////////////////////// |
| 59 | #define WIDTH_HEIGHT 256 |
| 60 | |
| 61 | static sk_sp<SkImage> make_img() { |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 62 | SkBitmap bitmap; |
| 63 | bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT); |
| 64 | SkCanvas canvas(bitmap); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 65 | |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 66 | canvas.clear(0x0); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 67 | |
| 68 | SkPaint paint; |
| 69 | paint.setColor(SK_ColorBLUE); |
| 70 | |
| 71 | for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) { |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 72 | canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint); |
| 73 | canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Osman | b44bb31 | 2016-12-20 15:54:11 -0500 | [diff] [blame] | 76 | SkBitmap result; |
| 77 | result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType)); |
| 78 | result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
| 79 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 80 | return result.asImage(); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) { |
Michael Ludwig | 642f218 | 2023-05-30 20:57:39 -0400 | [diff] [blame] | 84 | sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(make_img(), SkFilterMode::kNearest)); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 85 | |
| 86 | // Crop out a 16 pixel ring around the result |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 87 | const SkIRect cropRect = SkIRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 88 | |
| 89 | SkPaint filterPaint; |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 90 | filterPaint.setImageFilter(SkImageFilters::Magnifier( |
Michael Ludwig | 0dc9e8d | 2023-05-02 16:23:56 -0400 | [diff] [blame] | 91 | SkRect::MakeWH(WIDTH_HEIGHT, WIDTH_HEIGHT), |
| 92 | WIDTH_HEIGHT / (WIDTH_HEIGHT - 96.f), 64.f, {}, |
| 93 | std::move(imageSource), &cropRect)); |
robertphillips | b2a4dc6 | 2016-04-14 07:54:04 -0700 | [diff] [blame] | 94 | |
| 95 | canvas->saveLayer(nullptr, &filterPaint); |
| 96 | canvas->restore(); |
| 97 | } |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 98 | |
| 99 | class ImageMagnifierBounds : public skiagm::GM { |
| 100 | public: |
| 101 | ImageMagnifierBounds() : fX(0.f), fY(0.f) {} |
| 102 | |
| 103 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 104 | SkString getName() const override { return SkString("imagemagnifier_bounds"); } |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 105 | SkISize getISize() override { return SkISize::Make(768, 512); } |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 106 | |
| 107 | bool onAnimate(double nanos) override { |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 108 | fX = TimeUtils::SineWave(nanos, 10.f, 0.f, -200.f, 200.f); |
| 109 | fY = TimeUtils::SineWave(nanos, 10.f, 3.f, -200.f, 200.f); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
| 113 | void onDraw(SkCanvas* canvas) override { |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 114 | this->drawRow(canvas, 16.f); // fish eye distortion |
| 115 | canvas->translate(0.f, 256.f); |
| 116 | this->drawRow(canvas, 0.f); // no distortion, just zoom |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | |
| 121 | void drawRow(SkCanvas* canvas, float inset) { |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 122 | // Draw the magnifier two ways: backdrop filtered and then through a saveLayer with a |
| 123 | // regular filter. Lastly draw the un-filtered input. Relevant bounds are displayed on |
| 124 | // top of the rendering: |
| 125 | // - black = the lens bounding box |
| 126 | // - red = the clipped inset lens bounds |
| 127 | // - blue = the source of the undistorted magnified content |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 128 | auto drawBorder = [canvas](SkRect rect, SkColor color, |
| 129 | float width, float borderInset = 0.f) { |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 130 | SkPaint paint; |
| 131 | paint.setStyle(SkPaint::kStroke_Style); |
| 132 | paint.setStrokeWidth(width); |
| 133 | paint.setColor(color); |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 134 | paint.setAntiAlias(true); |
| 135 | |
| 136 | // This draws the original rect (unrounded) when borderInset = 0 |
| 137 | rect.inset(borderInset, borderInset); |
| 138 | canvas->drawRRect(SkRRect::MakeRectXY(rect, borderInset, borderInset), paint); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | // Logically there is a 'widgetBounds' that is the region of pixels to |
| 142 | // be filled with magnified content. Pixels inside widgetBounds are |
| 143 | // scaled up by a factor of 'zoomAmount', with a non linear distortion |
| 144 | // applied to pixels up to 'inset' inside 'widgetBounds'. The specific |
| 145 | // linearly scaled region is termed the 'srcRect' and is adjusted |
| 146 | // dynamically if parts of 'widgetBounds' are offscreen. |
| 147 | SkRect widgetBounds = {16.f, 24.f, 220.f, 248.f}; |
| 148 | widgetBounds.offset(fX, fY); // animating helps highlight magnifier behavior |
| 149 | |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 150 | constexpr float kZoomAmount = 2.5f; |
| 151 | |
| 152 | // The available content for backdrops, which clips the widgetBounds as it animates. |
| 153 | constexpr SkRect kOutBounds = {0.f, 0.f, 256.f, 256.f}; |
| 154 | |
Michael Ludwig | 094e348 | 2023-05-03 13:58:13 -0400 | [diff] [blame] | 155 | // The filter responds to any crop (explicit or from missing backdrop content). Compute |
| 156 | // the corresponding clipped bounds and source bounds for visualization purposes. |
| 157 | SkPoint zoomCenter = widgetBounds.center(); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 158 | SkRect clippedWidget = widgetBounds; |
| 159 | SkAssertResult(clippedWidget.intersect(kOutBounds)); |
Michael Ludwig | 094e348 | 2023-05-03 13:58:13 -0400 | [diff] [blame] | 160 | zoomCenter = {SkTPin(zoomCenter.fX, clippedWidget.fLeft, clippedWidget.fRight), |
| 161 | SkTPin(zoomCenter.fY, clippedWidget.fTop, clippedWidget.fBottom)}; |
| 162 | zoomCenter = zoomCenter * (1.f - 1.f / kZoomAmount); |
| 163 | SkRect srcRect = {clippedWidget.fLeft / kZoomAmount + zoomCenter.fX, |
| 164 | clippedWidget.fTop / kZoomAmount + zoomCenter.fY, |
| 165 | clippedWidget.fRight / kZoomAmount + zoomCenter.fX, |
| 166 | clippedWidget.fBottom / kZoomAmount + zoomCenter.fY}; |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 167 | |
Michael Ludwig | 094e348 | 2023-05-03 13:58:13 -0400 | [diff] [blame] | 168 | // Internally, the magnifier filter performs equivalent calculations but responds to the |
| 169 | // canvas matrix and available input automatically. |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 170 | sk_sp<SkImageFilter> magnifier = |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 171 | SkImageFilters::Magnifier(widgetBounds, kZoomAmount, inset, |
Michael Ludwig | 2c49e60 | 2023-05-30 13:12:19 -0400 | [diff] [blame] | 172 | SkFilterMode::kLinear, nullptr, kOutBounds); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 173 | |
| 174 | // Draw once as a backdrop filter |
| 175 | canvas->save(); |
| 176 | canvas->clipRect(kOutBounds); |
| 177 | draw_content(canvas, 32.f, 350); |
Michael Ludwig | 094e348 | 2023-05-03 13:58:13 -0400 | [diff] [blame] | 178 | canvas->saveLayer({nullptr, nullptr, magnifier.get(), 0}); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 179 | canvas->restore(); |
| 180 | |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 181 | drawBorder(widgetBounds, SK_ColorBLACK, 2.f); |
| 182 | if (inset > 0.f) { |
| 183 | drawBorder(clippedWidget, SK_ColorRED, 2.f, inset); |
| 184 | } |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 185 | canvas->restore(); |
| 186 | |
| 187 | // Draw once as a regular filter |
| 188 | canvas->save(); |
| 189 | canvas->translate(256.f, 0.f); |
| 190 | canvas->clipRect(kOutBounds); |
| 191 | |
Michael Ludwig | 094e348 | 2023-05-03 13:58:13 -0400 | [diff] [blame] | 192 | SkPaint paint; |
| 193 | paint.setImageFilter(magnifier); |
| 194 | canvas->saveLayer(nullptr, &paint); |
| 195 | draw_content(canvas, 32.f, 350); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 196 | canvas->restore(); |
| 197 | |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 198 | drawBorder(widgetBounds, SK_ColorBLACK, 2.f); |
| 199 | if (inset > 0.f) { |
| 200 | drawBorder(clippedWidget, SK_ColorRED, 2.f, inset); |
| 201 | } |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 202 | canvas->restore(); |
| 203 | |
| 204 | // Draw once unfiltered |
| 205 | canvas->save(); |
| 206 | canvas->translate(512.f, 0.f); |
| 207 | canvas->clipRect(kOutBounds); |
| 208 | draw_content(canvas, 32.f, 350); |
| 209 | |
Michael Ludwig | 872787e | 2023-05-12 10:31:07 -0400 | [diff] [blame] | 210 | drawBorder(widgetBounds, SK_ColorBLACK, 2.f); |
| 211 | drawBorder(srcRect, SK_ColorBLUE, 2.f, inset / kZoomAmount); |
Michael Ludwig | 8d1f921 | 2023-05-03 11:44:20 -0400 | [diff] [blame] | 212 | canvas->restore(); |
| 213 | } |
| 214 | |
| 215 | private: |
| 216 | SkScalar fX; |
| 217 | SkScalar fY; |
| 218 | }; |
| 219 | |
| 220 | DEF_GM(return new ImageMagnifierBounds(); ) |