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 | |
| 8 | #include "gm.h" |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 10 | #include "SkLightingImageFilter.h" |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 11 | #include "SkOffsetImageFilter.h" |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 12 | #include "SkPoint3.h" |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 13 | |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 14 | #define WIDTH 330 |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 15 | #define HEIGHT 660 |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class ImageLightingGM : public GM { |
| 20 | public: |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 21 | ImageLightingGM() |
| 22 | : fAzimuth(SkIntToScalar(kStartAzimuth)) { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 23 | this->setBGColor(0xFF000000); |
| 24 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 26 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 27 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | SkString onShortName() override { |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 29 | return SkString("lighting"); |
| 30 | } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkISize onISize() override { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 33 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 34 | } |
| 35 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 36 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) { |
| 37 | canvas->save(); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 38 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 39 | canvas->clipRect(SkRect::MakeWH( |
| 40 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 41 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 42 | canvas->restore(); |
| 43 | } |
| 44 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 45 | void onOnceBeforeDraw() override { |
| 46 | fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
| 47 | } |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | void onDraw(SkCanvas* canvas) override { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 50 | canvas->clear(sk_tool_utils::color_to_565(0xFF101010)); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 51 | SkPaint checkPaint; |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 52 | checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020)); |
senorblanco@chromium.org | 7b734e0 | 2012-10-29 19:47:06 +0000 | [diff] [blame] | 53 | for (int y = 0; y < HEIGHT; y += 16) { |
| 54 | for (int x = 0; x < WIDTH; x += 16) { |
| 55 | canvas->save(); |
| 56 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 57 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint); |
| 58 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint); |
| 59 | canvas->restore(); |
| 60 | } |
| 61 | } |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 62 | SkScalar cosAzimuth; |
| 63 | SkScalar sinAzimuth = SkScalarSinCos(SkDegreesToRadians(fAzimuth), &cosAzimuth); |
| 64 | |
| 65 | SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0); |
| 66 | SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth, |
| 67 | spotTarget.fY + 70.7214f * sinAzimuth, |
| 68 | spotTarget.fZ + SkIntToScalar(20)); |
| 69 | SkScalar spotExponent = SK_Scalar1; |
| 70 | |
| 71 | SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth, |
| 72 | spotTarget.fY + 50 * sinAzimuth, |
| 73 | SkIntToScalar(10)); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 74 | SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 75 | |
| 76 | SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(cosAzimuth, |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 77 | SkScalarCos(elevationRad)), |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 78 | SkScalarMul(sinAzimuth, |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 79 | SkScalarCos(elevationRad)), |
| 80 | SkScalarSin(elevationRad)); |
caryclark | 0bccd87 | 2015-10-20 10:04:03 -0700 | [diff] [blame] | 81 | SkScalar cutoffAngle = SkIntToScalar(15); |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 82 | SkScalar kd = SkIntToScalar(2); |
| 83 | SkScalar ks = SkIntToScalar(1); |
| 84 | SkScalar shininess = SkIntToScalar(8); |
| 85 | SkScalar surfaceScale = SkIntToScalar(1); |
| 86 | SkColor white(0xFFFFFFFF); |
| 87 | SkPaint paint; |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 88 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 89 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65)); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 90 | SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 91 | sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 92 | |
| 93 | int y = 0; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 94 | for (int i = 0; i < 3; i++) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr; |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 96 | sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr; |
| 97 | paint.setImageFilter(SkLightingImageFilter::MakePointLitDiffuse(pointLocation, |
| 98 | white, |
| 99 | surfaceScale, |
| 100 | kd, |
| 101 | input, |
| 102 | cr)); |
| 103 | drawClippedBitmap(canvas, paint, 0, y); |
| 104 | |
| 105 | paint.setImageFilter(SkLightingImageFilter::MakeDistantLitDiffuse(distantDirection, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 106 | white, |
| 107 | surfaceScale, |
| 108 | kd, |
| 109 | input, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 110 | 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 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 113 | paint.setImageFilter(SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation, |
| 114 | spotTarget, |
| 115 | spotExponent, |
| 116 | cutoffAngle, |
| 117 | white, |
| 118 | surfaceScale, |
| 119 | kd, |
| 120 | input, |
| 121 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 122 | drawClippedBitmap(canvas, paint, 220, y); |
| 123 | |
| 124 | y += 110; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 125 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 126 | paint.setImageFilter(SkLightingImageFilter::MakePointLitSpecular(pointLocation, |
| 127 | white, |
| 128 | surfaceScale, |
| 129 | ks, |
| 130 | shininess, |
| 131 | input, |
| 132 | cr)); |
| 133 | drawClippedBitmap(canvas, paint, 0, y); |
| 134 | |
| 135 | paint.setImageFilter(SkLightingImageFilter::MakeDistantLitSpecular(distantDirection, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 136 | white, |
| 137 | surfaceScale, |
| 138 | ks, |
| 139 | shininess, |
| 140 | input, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 141 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 142 | drawClippedBitmap(canvas, paint, 110, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 143 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 144 | paint.setImageFilter(SkLightingImageFilter::MakeSpotLitSpecular(spotLocation, |
| 145 | spotTarget, |
| 146 | spotExponent, |
| 147 | cutoffAngle, |
| 148 | white, |
| 149 | surfaceScale, |
| 150 | ks, |
| 151 | shininess, |
| 152 | input, |
| 153 | cr)); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 154 | drawClippedBitmap(canvas, paint, 220, y); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 155 | |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 156 | y += 110; |
| 157 | } |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 158 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 160 | bool onAnimate(const SkAnimTimer& timer) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 161 | constexpr SkScalar kDesiredDurationSecs = 15.0f; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 162 | |
| 163 | fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f); |
| 164 | return true; |
| 165 | } |
| 166 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 167 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 168 | static constexpr int kStartAzimuth = 225; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 169 | |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 170 | SkBitmap fBitmap; |
robertphillips | 651bb5f | 2016-04-08 13:35:14 -0700 | [diff] [blame] | 171 | SkScalar fAzimuth; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 172 | |
| 173 | typedef GM INHERITED; |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | ////////////////////////////////////////////////////////////////////////////// |
| 177 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 178 | DEF_GM(return new ImageLightingGM;) |
senorblanco@chromium.org | f49b429 | 2012-06-22 21:01:23 +0000 | [diff] [blame] | 179 | } |