blob: 7915c331d0d03edf4da18876a35093d97bb113f6 [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
8#include "gm.h"
robertphillips651bb5f2016-04-08 13:35:14 -07009#include "SkAnimTimer.h"
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000010#include "SkLightingImageFilter.h"
senorblancod0d37ca2015-04-02 04:54:56 -070011#include "SkOffsetImageFilter.h"
robertphillips3d32d762015-07-13 13:16:44 -070012#include "SkPoint3.h"
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000013
caryclark0bccd872015-10-20 10:04:03 -070014#define WIDTH 330
senorblancod0d37ca2015-04-02 04:54:56 -070015#define HEIGHT 660
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000016
17namespace skiagm {
18
19class ImageLightingGM : public GM {
20public:
robertphillips651bb5f2016-04-08 13:35:14 -070021 ImageLightingGM()
22 : fAzimuth(SkIntToScalar(kStartAzimuth)) {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000023 this->setBGColor(0xFF000000);
24 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000026protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027
mtklein36352bf2015-03-25 18:17:31 -070028 SkString onShortName() override {
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000029 return SkString("lighting");
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000033 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000034 }
35
junov@chromium.orgff06af22013-01-14 16:27:50 +000036 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
37 canvas->save();
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +000038 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.orgff06af22013-01-14 16:27:50 +000042 canvas->restore();
43 }
44
robertphillips943a4622015-09-03 13:32:33 -070045 void onOnceBeforeDraw() override {
46 fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 void onDraw(SkCanvas* canvas) override {
caryclark12596012015-07-29 05:27:47 -070050 canvas->clear(sk_tool_utils::color_to_565(0xFF101010));
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000051 SkPaint checkPaint;
caryclark12596012015-07-29 05:27:47 -070052 checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020));
senorblanco@chromium.org7b734e02012-10-29 19:47:06 +000053 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 }
robertphillips651bb5f2016-04-08 13:35:14 -070062 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.orgf49b4292012-06-22 21:01:23 +000074 SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
robertphillips651bb5f2016-04-08 13:35:14 -070075
76 SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(cosAzimuth,
robertphillips3d32d762015-07-13 13:16:44 -070077 SkScalarCos(elevationRad)),
robertphillips651bb5f2016-04-08 13:35:14 -070078 SkScalarMul(sinAzimuth,
robertphillips3d32d762015-07-13 13:16:44 -070079 SkScalarCos(elevationRad)),
80 SkScalarSin(elevationRad));
caryclark0bccd872015-10-20 10:04:03 -070081 SkScalar cutoffAngle = SkIntToScalar(15);
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +000082 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.org4e16bb22013-07-26 00:10:07 +000088
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000089 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
senorblancod0d37ca2015-04-02 04:54:56 -070090 SkImageFilter::CropRect fullSizeCropRect(SkRect::MakeXYWH(0, 0, 100, 100));
robertphillips51a315e2016-03-31 09:05:49 -070091 sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +000092
93 int y = 0;
senorblancod0d37ca2015-04-02 04:54:56 -070094 for (int i = 0; i < 3; i++) {
halcanary96fcdcc2015-08-27 07:41:13 -070095 const SkImageFilter::CropRect* cr = (i == 1) ? &cropRect : (i == 2) ? &fullSizeCropRect : nullptr;
robertphillips12fa47d2016-04-08 16:28:09 -070096 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,
senorblancod0d37ca2015-04-02 04:54:56 -0700106 white,
107 surfaceScale,
108 kd,
109 input,
robertphillips12fa47d2016-04-08 16:28:09 -0700110 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000111 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000112
robertphillips12fa47d2016-04-08 16:28:09 -0700113 paint.setImageFilter(SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation,
114 spotTarget,
115 spotExponent,
116 cutoffAngle,
117 white,
118 surfaceScale,
119 kd,
120 input,
121 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000122 drawClippedBitmap(canvas, paint, 220, y);
123
124 y += 110;
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000125
robertphillips12fa47d2016-04-08 16:28:09 -0700126 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,
senorblancod0d37ca2015-04-02 04:54:56 -0700136 white,
137 surfaceScale,
138 ks,
139 shininess,
140 input,
robertphillips12fa47d2016-04-08 16:28:09 -0700141 cr));
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000142 drawClippedBitmap(canvas, paint, 110, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000143
robertphillips12fa47d2016-04-08 16:28:09 -0700144 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.org4e16bb22013-07-26 00:10:07 +0000154 drawClippedBitmap(canvas, paint, 220, y);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000155
senorblanco@chromium.org4e16bb22013-07-26 00:10:07 +0000156 y += 110;
157 }
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
robertphillips651bb5f2016-04-08 13:35:14 -0700160 bool onAnimate(const SkAnimTimer& timer) override {
161 static const SkScalar kDesiredDurationSecs = 15.0f;
162
163 fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
164 return true;
165 }
166
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000167private:
robertphillips651bb5f2016-04-08 13:35:14 -0700168 static const int kStartAzimuth = 225;
169
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000170 SkBitmap fBitmap;
robertphillips651bb5f2016-04-08 13:35:14 -0700171 SkScalar fAzimuth;
robertphillips943a4622015-09-03 13:32:33 -0700172
173 typedef GM INHERITED;
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000174};
175
176//////////////////////////////////////////////////////////////////////////////
177
halcanary385fe4d2015-08-26 13:07:48 -0700178DEF_GM(return new ImageLightingGM;)
senorblanco@chromium.orgf49b4292012-06-22 21:01:23 +0000179}