blob: 451a9210753ab21d8d69b646aa20c2b14823579f [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPoint3.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/effects/SkLightingImageFilter.h"
21#include "include/effects/SkOffsetImageFilter.h"
22#include "tools/ToolUtils.h"
23#include "tools/timer/AnimTimer.h"
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000024
caryclark0bccd872015-10-20 10:04:03 -070025#define WIDTH 330
senorblancod0d37ca2015-04-02 04:54:56 -070026#define HEIGHT 660
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000027
28namespace skiagm {
29
30class ImageLightingGM : public GM {
31public:
robertphillips651bb5f2016-04-08 13:35:14 -070032 ImageLightingGM()
33 : fAzimuth(SkIntToScalar(kStartAzimuth)) {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000034 this->setBGColor(0xFF000000);
35 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000037protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000038
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000040 return SkString("lighting");
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 SkISize onISize() override {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000044 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000045 }
46
junov@chromium.orgff06af22013-01-14 16:27:50 +000047 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
48 canvas->save();
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000049 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
50 canvas->clipRect(SkRect::MakeWH(
51 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
52 canvas->drawBitmap(fBitmap, 0, 0, &paint);
junov@chromium.orgff06af22013-01-14 16:27:50 +000053 canvas->restore();
54 }
55
robertphillips943a4622015-09-03 13:32:33 -070056 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050057 fBitmap = ToolUtils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
robertphillips943a4622015-09-03 13:32:33 -070058 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040061 canvas->clear(0xFF101010);
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000062 SkPaint checkPaint;
Mike Kleind46dce32018-08-16 10:17:03 -040063 checkPaint.setColor(0xFF202020);
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000064 for (int y = 0; y < HEIGHT; y += 16) {
65 for (int x = 0; x < WIDTH; x += 16) {
66 canvas->save();
67 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
68 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
69 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
70 canvas->restore();
71 }
72 }
Brian Osman4428f2c2019-04-02 10:59:28 -040073 SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(fAzimuth)),
74 cosAzimuth = SkScalarCos(SkDegreesToRadians(fAzimuth));
robertphillips651bb5f2016-04-08 13:35:14 -070075
76 SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
77 SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth,
78 spotTarget.fY + 70.7214f * sinAzimuth,
79 spotTarget.fZ + SkIntToScalar(20));
80 SkScalar spotExponent = SK_Scalar1;
81
82 SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
83 spotTarget.fY + 50 * sinAzimuth,
84 SkIntToScalar(10));
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000085 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
robertphillips651bb5f2016-04-08 13:35:14 -070086
Mike Reeddf85c382017-02-14 10:59:19 -050087 SkPoint3 distantDirection = SkPoint3::Make(cosAzimuth * SkScalarCos(elevationRad),
88 sinAzimuth * SkScalarCos(elevationRad),
robertphillips3d32d762015-07-13 13:16:44 -070089 SkScalarSin(elevationRad));
caryclark0bccd872015-10-20 10:04:03 -070090 SkScalar cutoffAngle = SkIntToScalar(15);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000091 SkScalar kd = SkIntToScalar(2);
92 SkScalar ks = SkIntToScalar(1);
93 SkScalar shininess = SkIntToScalar(8);
94 SkScalar surfaceScale = SkIntToScalar(1);
95 SkColor white(0xFFFFFFFF);
96 SkPaint paint;
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000097
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000098 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
senorblancod0d37ca2015-04-02 04:54:56 -070099 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
robertphillips51a315e2016-03-31 09:05:49 -0700100 sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000101
102 int y = 0;
senorblancod0d37ca2015-04-02 04:54:56 -0700103 for (int i = 0; i < 3; i++) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr;
robertphillips12fa47d2016-04-08 16:28:09 -0700105 sk_sp<SkImageFilter> input = (i == 2) ? noopCropped : nullptr;
106 paint.setImageFilter(SkLightingImageFilter::MakePointLitDiffuse(pointLocation,
107 white,
108 surfaceScale,
109 kd,
110 input,
111 cr));
112 drawClippedBitmap(canvas, paint, 0, y);
113
114 paint.setImageFilter(SkLightingImageFilter::MakeDistantLitDiffuse(distantDirection,
senorblancod0d37ca2015-04-02 04:54:56 -0700115 white,
116 surfaceScale,
117 kd,
118 input,
robertphillips12fa47d2016-04-08 16:28:09 -0700119 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000120 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000121
robertphillips12fa47d2016-04-08 16:28:09 -0700122 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation,
123 spotTarget,
124 spotExponent,
125 cutoffAngle,
126 white,
127 surfaceScale,
128 kd,
129 input,
130 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000131 drawClippedBitmap(canvas, paint, 220, y);
132
133 y += 110;
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000134
robertphillips12fa47d2016-04-08 16:28:09 -0700135 paint.setImageFilter(SkLightingImageFilter::MakePointLitSpecular(pointLocation,
136 white,
137 surfaceScale,
138 ks,
139 shininess,
140 input,
141 cr));
142 drawClippedBitmap(canvas, paint, 0, y);
143
144 paint.setImageFilter(SkLightingImageFilter::MakeDistantLitSpecular(distantDirection,
senorblancod0d37ca2015-04-02 04:54:56 -0700145 white,
146 surfaceScale,
147 ks,
148 shininess,
149 input,
robertphillips12fa47d2016-04-08 16:28:09 -0700150 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000151 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000152
robertphillips12fa47d2016-04-08 16:28:09 -0700153 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitSpecular(spotLocation,
154 spotTarget,
155 spotExponent,
156 cutoffAngle,
157 white,
158 surfaceScale,
159 ks,
160 shininess,
161 input,
162 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000163 drawClippedBitmap(canvas, paint, 220, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000164
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000165 y += 110;
166 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000167 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
Mike Kleincd5104e2019-03-20 11:55:08 -0500169 bool onAnimate(const AnimTimer& timer) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700170 constexpr SkScalar kDesiredDurationSecs = 15.0f;
robertphillips651bb5f2016-04-08 13:35:14 -0700171
172 fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
173 return true;
174 }
175
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000176private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700177 static constexpr int kStartAzimuth = 225;
robertphillips651bb5f2016-04-08 13:35:14 -0700178
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000179 SkBitmap fBitmap;
robertphillips651bb5f2016-04-08 13:35:14 -0700180 SkScalar fAzimuth;
robertphillips943a4622015-09-03 13:32:33 -0700181
182 typedef GM INHERITED;
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000183};
184
185//////////////////////////////////////////////////////////////////////////////
186
halcanary385fe4d2015-08-26 13:07:48 -0700187DEF_GM(return new ImageLightingGM;)
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000188}