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" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 22 | #include "tools/timer/TimeUtils.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 23 | |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 24 | #define WIDTH 330 |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 25 | #define HEIGHT 660 |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 26 | |
| 27 | namespace skiagm { |
| 28 | |
| 29 | class ImageLightingGM : public GM { |
| 30 | public: |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 31 | ImageLightingGM() |
| 32 | : fAzimuth(SkIntToScalar(kStartAzimuth)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 33 | this->setBGColor(0xFF000000); |
| 34 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 35 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 36 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkString onShortName() override { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 39 | return SkString("lighting"); |
| 40 | } |
| 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | SkISize onISize() override { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 43 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 44 | } |
| 45 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 46 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 47 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 48 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 49 | canvas->clipRect(SkRect::MakeWH( |
| 50 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 51 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 52 | canvas->restore(); |
| 53 | } |
| 54 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 55 | void onOnceBeforeDraw() override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 56 | fBitmap = ToolUtils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 57 | } |
| 58 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 60 | canvas->clear(0xFF101010); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 61 | SkPaint checkPaint; |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 62 | checkPaint.setColor(0xFF202020); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 63 | for (int y = 0; y < HEIGHT; y += 16) { |
| 64 | for (int x = 0; x < WIDTH; x += 16) { |
| 65 | canvas->save(); |
| 66 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 67 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 68 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 69 | canvas->restore(); |
| 70 | } |
| 71 | } |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 72 | SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(fAzimuth)), |
| 73 | cosAzimuth = SkScalarCos(SkDegreesToRadians(fAzimuth)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 74 | |
| 75 | SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 76 | SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth, |
| 77 | spotTarget.fY + 70.7214f * sinAzimuth, |
| 78 | spotTarget.fZ + SkIntToScalar(20)); |
| 79 | SkScalar spotExponent = SK_Scalar1; |
| 80 | |
| 81 | SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth, |
| 82 | spotTarget.fY + 50 * sinAzimuth, |
| 83 | SkIntToScalar(10)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 84 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 85 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 86 | SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad), |
| 87 | sinAzimuth * SkScalarCos(elevationRad), |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 88 | SkScalarSin(elevationRad)); |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 89 | SkScalar cutoffAngle = SkIntToScalar(15); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 90 | SkScalar kd = SkIntToScalar(2); |
| 91 | SkScalar ks = SkIntToScalar(1); |
| 92 | SkScalar shininess = SkIntToScalar(8); |
| 93 | SkScalar surfaceScale = SkIntToScalar(1); |
| 94 | SkColor white(0xFFFFFFFF); |
| 95 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 96 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 97 | SkIRect cropRect = SkIRect::MakeXYWH(20, 10, 60, 65); |
| 98 | SkIRect fullSizeCropRect = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 99 | sk_sp<SkImageFilter> noopCropped(SkImageFilters::Offset(0, 0, nullptr, &cropRect)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 100 | |
| 101 | int y = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 102 | for (int i = 0; i < 3; i++) { |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 103 | const SkIRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr; |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 104 | sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr; |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 105 | paint.setImageFilter(SkImageFilters::PointLitDiffuse( |
| 106 | pointLocation, white, surfaceScale, kd, input, cr)); |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 107 | drawClippedBitmap(canvas, paint, 0, y); |
| 108 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 109 | paint.setImageFilter(SkImageFilters::DistantLitDiffuse( |
| 110 | distantDirection, white, surfaceScale, kd, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 111 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 112 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 113 | paint.setImageFilter(SkImageFilters::SpotLitDiffuse( |
| 114 | spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, |
| 115 | input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 116 | drawClippedBitmap(canvas, paint, 220, y); |
| 117 | |
| 118 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 119 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 120 | paint.setImageFilter(SkImageFilters::PointLitSpecular( |
| 121 | pointLocation, white, surfaceScale, ks, shininess, input, cr)); |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 122 | drawClippedBitmap(canvas, paint, 0, y); |
| 123 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 124 | paint.setImageFilter(SkImageFilters::DistantLitSpecular( |
| 125 | distantDirection, white, surfaceScale, ks, shininess, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 126 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 127 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 128 | paint.setImageFilter(SkImageFilters::SpotLitSpecular( |
| 129 | spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, |
| 130 | shininess, input, cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 131 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 132 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 133 | y += 110; |
| 134 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 135 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 137 | bool onAnimate(double nanos) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 138 | constexpr SkScalar kDesiredDurationSecs = 15.0f; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 139 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 140 | fAzimuth = kStartAzimuth + TimeUtils::Scaled(1e-9 * nanos, 360.0f/kDesiredDurationSecs, 360.0f); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 144 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 145 | static constexpr int kStartAzimuth = 225; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 146 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 147 | SkBitmap fBitmap; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 148 | SkScalar fAzimuth; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 149 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 150 | using INHERITED = GM; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 155 | DEF_GM(return new ImageLightingGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 156 | } // namespace skiagm |