senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +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/SkImageFilter.h" |
| 13 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkPoint3.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 22 | #include "tools/fonts/FontToolUtils.h" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 23 | #include "tools/timer/TimeUtils.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 24 | |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 25 | #define WIDTH 660 |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 26 | #define HEIGHT 660 |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 27 | |
| 28 | namespace skiagm { |
| 29 | |
| 30 | class ImageLightingGM : public GM { |
| 31 | public: |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 32 | ImageLightingGM() |
| 33 | : fAzimuth(SkIntToScalar(kStartAzimuth)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 34 | this->setBGColor(0xFF000000); |
| 35 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 37 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 38 | SkString getName() const override { return SkString("lighting"); } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 39 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 40 | SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 41 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 42 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 43 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 44 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame] | 45 | canvas->clipIRect(fBitmap.bounds()); |
| 46 | canvas->drawImage(fBitmap.asImage(), 0, 0, SkSamplingOptions(), &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 47 | canvas->restore(); |
| 48 | } |
| 49 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 50 | void onOnceBeforeDraw() override { |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 51 | fBitmap = ToolUtils::CreateStringBitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 55 | canvas->clear(0xFF101010); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 56 | SkPaint checkPaint; |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 57 | checkPaint.setColor(0xFF202020); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 58 | for (int y = 0; y < HEIGHT; y += 16) { |
| 59 | for (int x = 0; x < WIDTH; x += 16) { |
| 60 | canvas->save(); |
| 61 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 62 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 63 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 64 | canvas->restore(); |
| 65 | } |
| 66 | } |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 67 | SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(fAzimuth)), |
| 68 | cosAzimuth = SkScalarCos(SkDegreesToRadians(fAzimuth)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 69 | |
| 70 | SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 71 | SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth, |
| 72 | spotTarget.fY + 70.7214f * sinAzimuth, |
| 73 | spotTarget.fZ + SkIntToScalar(20)); |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 74 | SkScalar spotExponent1 = SK_Scalar1; |
| 75 | SkScalar spotExponent10 = SkIntToScalar(10); |
| 76 | SkScalar cutoffAngleSmall = SkIntToScalar(15); |
| 77 | SkScalar cutoffAngleNone = SkIntToScalar(180); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 78 | |
| 79 | SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth, |
| 80 | spotTarget.fY + 50 * sinAzimuth, |
| 81 | SkIntToScalar(10)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 82 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 83 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 84 | SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad), |
| 85 | sinAzimuth * SkScalarCos(elevationRad), |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 86 | SkScalarSin(elevationRad)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 87 | SkScalar kd = SkIntToScalar(2); |
| 88 | SkScalar ks = SkIntToScalar(1); |
| 89 | SkScalar shininess = SkIntToScalar(8); |
| 90 | SkScalar surfaceScale = SkIntToScalar(1); |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 91 | SkScalar surfaceScaleSmall = 0.1f; |
| 92 | SkColor greenYellow = SkColorSetARGB(255, 173, 255, 47); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 93 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 94 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 95 | SkIRect cropRect = SkIRect::MakeXYWH(20, 10, 60, 65); |
| 96 | SkIRect fullSizeCropRect = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 97 | sk_sp<SkImageFilter> noopCropped(SkImageFilters::Offset(0, 0, nullptr, &cropRect)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 98 | |
| 99 | int y = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 100 | for (int i = 0; i < 3; i++) { |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 101 | const SkIRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr; |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 102 | sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr; |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 103 | // Basic point, distant and spot lights with diffuse lighting |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 104 | paint.setImageFilter(SkImageFilters::PointLitDiffuse( |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 105 | pointLocation, SK_ColorWHITE, surfaceScale, kd, input, cr)); |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 106 | drawClippedBitmap(canvas, paint, 0, y); |
| 107 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 108 | paint.setImageFilter(SkImageFilters::DistantLitDiffuse( |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 109 | distantDirection, SK_ColorWHITE, surfaceScale, kd, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 110 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 111 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 112 | paint.setImageFilter(SkImageFilters::SpotLitDiffuse( |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 113 | spotLocation, spotTarget, spotExponent1, cutoffAngleSmall, SK_ColorWHITE, |
| 114 | surfaceScale, kd, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 115 | drawClippedBitmap(canvas, paint, 220, y); |
| 116 | |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 117 | // Spot light with no angle cutoff |
| 118 | paint.setImageFilter(SkImageFilters::SpotLitDiffuse( |
| 119 | spotLocation, spotTarget, spotExponent10, cutoffAngleNone, SK_ColorWHITE, |
| 120 | surfaceScale, kd, input, cr)); |
| 121 | drawClippedBitmap(canvas, paint, 330, y); |
| 122 | |
| 123 | // Spot light with falloff exponent |
| 124 | paint.setImageFilter(SkImageFilters::SpotLitDiffuse( |
| 125 | spotLocation, spotTarget, spotExponent1, cutoffAngleNone, SK_ColorWHITE, |
| 126 | surfaceScaleSmall, kd, input, cr)); |
| 127 | drawClippedBitmap(canvas, paint, 440, y); |
| 128 | |
| 129 | // Large constant to show oversaturation |
| 130 | paint.setImageFilter(SkImageFilters::DistantLitDiffuse( |
| 131 | distantDirection, greenYellow, surfaceScale, 4.f * kd, input, cr)); |
| 132 | drawClippedBitmap(canvas, paint, 550, y); |
| 133 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 134 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 135 | |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 136 | // Basic point, distant and spot lights with specular lighting |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 137 | paint.setImageFilter(SkImageFilters::PointLitSpecular( |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 138 | pointLocation, SK_ColorWHITE, surfaceScale, ks, shininess, input, cr)); |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 139 | drawClippedBitmap(canvas, paint, 0, y); |
| 140 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 141 | paint.setImageFilter(SkImageFilters::DistantLitSpecular( |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 142 | distantDirection, SK_ColorWHITE, surfaceScale, ks, shininess, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 143 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 144 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 145 | paint.setImageFilter(SkImageFilters::SpotLitSpecular( |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 146 | spotLocation, spotTarget, spotExponent1, cutoffAngleSmall, SK_ColorWHITE, |
| 147 | surfaceScale, ks, shininess, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 148 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 149 | |
Michael Ludwig | f577427 | 2021-01-21 18:43:58 -0500 | [diff] [blame] | 150 | // Spot light with no angle cutoff |
| 151 | paint.setImageFilter(SkImageFilters::SpotLitSpecular( |
| 152 | spotLocation, spotTarget, spotExponent10, cutoffAngleNone, SK_ColorWHITE, |
| 153 | surfaceScale, ks, shininess, input, cr)); |
| 154 | drawClippedBitmap(canvas, paint, 330, y); |
| 155 | |
| 156 | // Spot light with falloff exponent |
| 157 | paint.setImageFilter(SkImageFilters::SpotLitSpecular( |
| 158 | spotLocation, spotTarget, spotExponent1, cutoffAngleNone, SK_ColorWHITE, |
| 159 | surfaceScaleSmall, ks, shininess, input, cr)); |
| 160 | drawClippedBitmap(canvas, paint, 440, y); |
| 161 | |
| 162 | // Large constant to show oversaturation |
| 163 | paint.setImageFilter(SkImageFilters::DistantLitSpecular( |
| 164 | distantDirection, greenYellow, surfaceScale, 4.f * ks, shininess, input, cr)); |
| 165 | drawClippedBitmap(canvas, paint, 550, y); |
| 166 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 167 | y += 110; |
| 168 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 169 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 170 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 171 | bool onAnimate(double nanos) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 172 | constexpr SkScalar kDesiredDurationSecs = 15.0f; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 173 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 174 | fAzimuth = kStartAzimuth + TimeUtils::Scaled(1e-9 * nanos, 360.0f/kDesiredDurationSecs, 360.0f); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 178 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 179 | inline static constexpr int kStartAzimuth = 225; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 180 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 181 | SkBitmap fBitmap; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 182 | SkScalar fAzimuth; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 183 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 184 | using INHERITED = GM; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 189 | DEF_GM(return new ImageLightingGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 190 | } // namespace skiagm |