blob: 26521e2f7f7d9424db0e1c9c2a8d2b547129175e [file] [log] [blame]
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkPoint3.h"
10#include "include/effects/SkLightingImageFilter.h"
11#include "include/effects/SkOffsetImageFilter.h"
12#include "tools/ToolUtils.h"
13#include "tools/timer/AnimTimer.h"
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000014
caryclark0bccd872015-10-20 10:04:03 -070015#define WIDTH 330
senorblancod0d37ca2015-04-02 04:54:56 -070016#define HEIGHT 660
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000017
18namespace skiagm {
19
20class ImageLightingGM : public GM {
21public:
robertphillips651bb5f2016-04-08 13:35:14 -070022 ImageLightingGM()
23 : fAzimuth(SkIntToScalar(kStartAzimuth)) {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000024 this->setBGColor(0xFF000000);
25 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000027protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000030 return SkString("lighting");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000034 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000035 }
36
junov@chromium.orgff06af22013-01-14 16:27:50 +000037 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
38 canvas->save();
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000039 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
40 canvas->clipRect(SkRect::MakeWH(
41 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
42 canvas->drawBitmap(fBitmap, 0, 0, &paint);
junov@chromium.orgff06af22013-01-14 16:27:50 +000043 canvas->restore();
44 }
45
robertphillips943a4622015-09-03 13:32:33 -070046 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050047 fBitmap = ToolUtils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
robertphillips943a4622015-09-03 13:32:33 -070048 }
49
mtklein36352bf2015-03-25 18:17:31 -070050 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040051 canvas->clear(0xFF101010);
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000052 SkPaint checkPaint;
Mike Kleind46dce32018-08-16 10:17:03 -040053 checkPaint.setColor(0xFF202020);
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000054 for (int y = 0; y < HEIGHT; y += 16) {
55 for (int x = 0; x < WIDTH; x += 16) {
56 canvas->save();
57 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
58 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
59 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
60 canvas->restore();
61 }
62 }
Brian Osman4428f2c2019-04-02 10:59:28 -040063 SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(fAzimuth)),
64 cosAzimuth = SkScalarCos(SkDegreesToRadians(fAzimuth));
robertphillips651bb5f2016-04-08 13:35:14 -070065
66 SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
67 SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth,
68 spotTarget.fY + 70.7214f * sinAzimuth,
69 spotTarget.fZ + SkIntToScalar(20));
70 SkScalar spotExponent = SK_Scalar1;
71
72 SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
73 spotTarget.fY + 50 * sinAzimuth,
74 SkIntToScalar(10));
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000075 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
robertphillips651bb5f2016-04-08 13:35:14 -070076
Mike Reeddf85c382017-02-14 10:59:19 -050077 SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad),
78 sinAzimuth * SkScalarCos(elevationRad),
robertphillips3d32d762015-07-13 13:16:44 -070079 SkScalarSin(elevationRad));
caryclark0bccd872015-10-20 10:04:03 -070080 SkScalar cutoffAngle = SkIntToScalar(15);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000081 SkScalar kd = SkIntToScalar(2);
82 SkScalar ks = SkIntToScalar(1);
83 SkScalar shininess = SkIntToScalar(8);
84 SkScalar surfaceScale = SkIntToScalar(1);
85 SkColor white(0xFFFFFFFF);
86 SkPaint paint;
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000087
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000088 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
senorblancod0d37ca2015-04-02 04:54:56 -070089 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
robertphillips51a315e2016-03-31 09:05:49 -070090 sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000091
92 int y = 0;
senorblancod0d37ca2015-04-02 04:54:56 -070093 for (int i = 0; i < 3; i++) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr;
robertphillips12fa47d2016-04-08 16:28:09 -070095 sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr;
96 paint.setImageFilter(SkLightingImageFilter::MakePointLitDiffuse(pointLocation,
97 white,
98 surfaceScale,
99 kd,
100 input,
101 cr));
102 drawClippedBitmap(canvas, paint, 0, y);
103
104 paint.setImageFilter(SkLightingImageFilter::MakeDistantLitDiffuse(distantDirection,
senorblancod0d37ca2015-04-02 04:54:56 -0700105 white,
106 surfaceScale,
107 kd,
108 input,
robertphillips12fa47d2016-04-08 16:28:09 -0700109 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000110 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000111
robertphillips12fa47d2016-04-08 16:28:09 -0700112 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation,
113 spotTarget,
114 spotExponent,
115 cutoffAngle,
116 white,
117 surfaceScale,
118 kd,
119 input,
120 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000121 drawClippedBitmap(canvas, paint, 220, y);
122
123 y += 110;
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000124
robertphillips12fa47d2016-04-08 16:28:09 -0700125 paint.setImageFilter(SkLightingImageFilter::MakePointLitSpecular(pointLocation,
126 white,
127 surfaceScale,
128 ks,
129 shininess,
130 input,
131 cr));
132 drawClippedBitmap(canvas, paint, 0, y);
133
134 paint.setImageFilter(SkLightingImageFilter::MakeDistantLitSpecular(distantDirection,
senorblancod0d37ca2015-04-02 04:54:56 -0700135 white,
136 surfaceScale,
137 ks,
138 shininess,
139 input,
robertphillips12fa47d2016-04-08 16:28:09 -0700140 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000141 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000142
robertphillips12fa47d2016-04-08 16:28:09 -0700143 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitSpecular(spotLocation,
144 spotTarget,
145 spotExponent,
146 cutoffAngle,
147 white,
148 surfaceScale,
149 ks,
150 shininess,
151 input,
152 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000153 drawClippedBitmap(canvas, paint, 220, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000154
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000155 y += 110;
156 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
Mike Kleincd5104e2019-03-20 11:55:08 -0500159 bool onAnimate(const AnimTimer& timer) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700160 constexpr SkScalar kDesiredDurationSecs = 15.0f;
robertphillips651bb5f2016-04-08 13:35:14 -0700161
162 fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
163 return true;
164 }
165
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000166private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700167 static constexpr int kStartAzimuth = 225;
robertphillips651bb5f2016-04-08 13:35:14 -0700168
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000169 SkBitmap fBitmap;
robertphillips651bb5f2016-04-08 13:35:14 -0700170 SkScalar fAzimuth;
robertphillips943a4622015-09-03 13:32:33 -0700171
172 typedef GM INHERITED;
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000173};
174
175//////////////////////////////////////////////////////////////////////////////
176
halcanary385fe4d2015-08-26 13:07:48 -0700177DEF_GM(return new ImageLightingGM;)
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000178}