blob: 3dc2b091ec69455611047913894306e7e7fdd014 [file] [log] [blame]
Brian Salomon0bd699e2017-02-01 12:23:25 -05001/*
2 * Copyright 2017 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/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkMatrix.h"
12#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPoint.h"
15#include "include/core/SkPoint3.h"
16#include "include/core/SkRRect.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkTypes.h"
20#include "include/private/SkShadowFlags.h"
21#include "include/private/SkTArray.h"
22#include "include/private/SkTDArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/utils/SkShadowUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024
25#include <initializer_list>
Brian Salomon0bd699e2017-02-01 12:23:25 -050026
Jim Van Vertha5566842018-02-22 10:58:34 -050027void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color,
28 SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) {
Brian Salomon0bd699e2017-02-01 12:23:25 -050029 SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
30 SkScalar spotAlpha = isAmbient ? 0.f : .5f;
Jim Van Vertha5566842018-02-22 10:58:34 -050031 SkColor ambientColor = SkColorSetARGB(ambientAlpha*SkColorGetA(color), SkColorGetR(color),
32 SkColorGetG(color), SkColorGetB(color));
33 SkColor spotColor = SkColorSetARGB(spotAlpha*SkColorGetA(color), SkColorGetR(color),
34 SkColorGetG(color), SkColorGetB(color));
35 SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR,
36 ambientColor, spotColor, flags);
Brian Salomon0bd699e2017-02-01 12:23:25 -050037}
38
Brian Salomonab664fa2017-03-24 16:07:20 +000039static constexpr int kW = 800;
Jim Van Verth22526362018-02-28 14:51:19 -050040static constexpr int kH = 960;
Brian Salomon0bd699e2017-02-01 12:23:25 -050041
Jim Van Verth744cbb32017-12-19 13:02:38 -050042enum ShadowMode {
43 kDebugColorNoOccluders,
44 kDebugColorOccluders,
45 kGrayscale
46};
47
48void draw_paths(SkCanvas* canvas, ShadowMode mode) {
Brian Salomon0bd699e2017-02-01 12:23:25 -050049 SkTArray<SkPath> paths;
50 paths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
51 SkRRect oddRRect;
52 oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
53 paths.push_back().addRRect(oddRRect);
54 paths.push_back().addRect(SkRect::MakeWH(50, 50));
55 paths.push_back().addCircle(25, 25, 25);
56 paths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
Brian Salomonab664fa2017-03-24 16:07:20 +000057 paths.push_back().addOval(SkRect::MakeWH(20, 60));
Brian Salomon0bd699e2017-02-01 12:23:25 -050058
Jim Van Verth22526362018-02-28 14:51:19 -050059 // star
60 SkTArray<SkPath> concavePaths;
61 concavePaths.push_back().moveTo(0.0f, -33.3333f);
62 concavePaths.back().lineTo(9.62f, -16.6667f);
63 concavePaths.back().lineTo(28.867f, -16.6667f);
64 concavePaths.back().lineTo(19.24f, 0.0f);
65 concavePaths.back().lineTo(28.867f, 16.6667f);
66 concavePaths.back().lineTo(9.62f, 16.6667f);
67 concavePaths.back().lineTo(0.0f, 33.3333f);
68 concavePaths.back().lineTo(-9.62f, 16.6667f);
69 concavePaths.back().lineTo(-28.867f, 16.6667f);
70 concavePaths.back().lineTo(-19.24f, 0.0f);
71 concavePaths.back().lineTo(-28.867f, -16.6667f);
72 concavePaths.back().lineTo(-9.62f, -16.6667f);
73 concavePaths.back().close();
74
75 // dumbbell
76 concavePaths.push_back().moveTo(50, 0);
77 concavePaths.back().cubicTo(100, 25, 60, 50, 50, 0);
78 concavePaths.back().cubicTo(0, -25, 40, -50, 50, 0);
79
Brian Salomon0bd699e2017-02-01 12:23:25 -050080 static constexpr SkScalar kPad = 15.f;
Brian Salomon0bd699e2017-02-01 12:23:25 -050081 static constexpr SkScalar kLightR = 100.f;
82 static constexpr SkScalar kHeight = 50.f;
Jim Van Verth9392f569c2017-05-16 15:01:43 -040083
84 // transform light position relative to canvas to handle tiling
85 SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400);
86 SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 };
87
Brian Salomon0bd699e2017-02-01 12:23:25 -050088 canvas->translate(3 * kPad, 3 * kPad);
89 canvas->save();
90 SkScalar x = 0;
91 SkScalar dy = 0;
92 SkTDArray<SkMatrix> matrices;
93 matrices.push()->reset();
94 SkMatrix* m = matrices.push();
95 m->setRotate(33.f, 25.f, 25.f);
96 m->postScale(1.2f, 0.8f, 25.f, 25.f);
97 for (auto& m : matrices) {
Jim Van Verth744cbb32017-12-19 13:02:38 -050098 for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
Jim Van Vertheda9a552019-07-24 14:46:53 -040099 int pathCounter = 0;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500100 for (const auto& path : paths) {
101 SkRect postMBounds = path.getBounds();
102 m.mapRect(&postMBounds);
103 SkScalar w = postMBounds.width() + kHeight;
104 SkScalar dx = w + kPad;
105 if (x + dx > kW - 3 * kPad) {
106 canvas->restore();
107 canvas->translate(0, dy);
108 canvas->save();
109 x = 0;
110 dy = 0;
111 }
112
113 canvas->save();
114 canvas->concat(m);
Brian Salomon0bd699e2017-02-01 12:23:25 -0500115
Jim Van Vertheda9a552019-07-24 14:46:53 -0400116 // flip a couple of paths to test 180° rotation
117 if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
118 canvas->save();
119 canvas->rotate(180, 25, 25);
120 }
Jim Van Verth744cbb32017-12-19 13:02:38 -0500121 if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500122 draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
123 true, flags);
124 draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
125 false, flags);
126 } else if (kGrayscale == mode) {
Jim Van Vertha5566842018-02-22 10:58:34 -0500127 SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
128 SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
129 SkShadowUtils::DrawShadow(canvas, path, SkPoint3{0, 0, kHeight}, lightPos,
130 kLightR, ambientColor, spotColor, flags);
Jim Van Verth744cbb32017-12-19 13:02:38 -0500131 }
132
Brian Salomon0bd699e2017-02-01 12:23:25 -0500133 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500134 paint.setAntiAlias(true);
Jim Van Verth744cbb32017-12-19 13:02:38 -0500135 if (kDebugColorNoOccluders == mode) {
136 // Draw the path outline in green on top of the ambient and spot shadows.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400137 if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
138 paint.setColor(SK_ColorCYAN);
139 } else {
140 paint.setColor(SK_ColorGREEN);
141 }
142 paint.setStyle(SkPaint::kStroke_Style);
143 paint.setStrokeWidth(0);
144 } else {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500145 paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400146 if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
Mike Reed9407e242019-02-15 16:13:57 -0500147 paint.setAlphaf(0.5f);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400148 }
149 paint.setStyle(SkPaint::kFill_Style);
150 }
Brian Salomon0bd699e2017-02-01 12:23:25 -0500151 canvas->drawPath(path, paint);
Jim Van Vertheda9a552019-07-24 14:46:53 -0400152 if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
153 canvas->restore();
154 }
Brian Salomon0bd699e2017-02-01 12:23:25 -0500155 canvas->restore();
156
157 canvas->translate(dx, 0);
158 x += dx;
Brian Osman788b9162020-02-07 10:36:46 -0500159 dy = std::max(dy, postMBounds.height() + kPad + kHeight);
Jim Van Vertheda9a552019-07-24 14:46:53 -0400160 ++pathCounter;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500161 }
162 }
163 }
Jim Van Verth22526362018-02-28 14:51:19 -0500164
165 // concave paths
166 canvas->restore();
167 canvas->translate(kPad, dy);
168 canvas->save();
169 x = kPad;
170 dy = 0;
171 for (auto& m : matrices) {
172 // for the concave paths we are not clipping, so transparent and opaque are the same
173 for (const auto& path : concavePaths) {
174 SkRect postMBounds = path.getBounds();
175 m.mapRect(&postMBounds);
176 SkScalar w = postMBounds.width() + kHeight;
177 SkScalar dx = w + kPad;
178
179 canvas->save();
180 canvas->concat(m);
181
182 if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
183 draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
184 true, kNone_ShadowFlag);
185 draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
186 false, kNone_ShadowFlag);
187 } else if (kGrayscale == mode) {
188 SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
189 SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
190 SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, kHeight }, lightPos,
191 kLightR, ambientColor, spotColor, kNone_ShadowFlag);
192 }
193
194 SkPaint paint;
195 paint.setAntiAlias(true);
196 if (kDebugColorNoOccluders == mode) {
197 // Draw the path outline in green on top of the ambient and spot shadows.
198 paint.setColor(SK_ColorGREEN);
199 paint.setStyle(SkPaint::kStroke_Style);
200 paint.setStrokeWidth(0);
201 } else {
202 paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
203 paint.setStyle(SkPaint::kFill_Style);
204 }
205 canvas->drawPath(path, paint);
206 canvas->restore();
207
208 canvas->translate(dx, 0);
209 x += dx;
Brian Osman788b9162020-02-07 10:36:46 -0500210 dy = std::max(dy, postMBounds.height() + kPad + kHeight);
Jim Van Verth22526362018-02-28 14:51:19 -0500211 }
212 }
213
Brian Salomon0bd699e2017-02-01 12:23:25 -0500214 // Show where the light is in x,y as a circle (specified in device space).
215 SkMatrix invCanvasM = canvas->getTotalMatrix();
216 if (invCanvasM.invert(&invCanvasM)) {
217 canvas->save();
218 canvas->concat(invCanvasM);
219 SkPaint paint;
220 paint.setColor(SK_ColorBLACK);
221 paint.setAntiAlias(true);
Jim Van Verth9392f569c2017-05-16 15:01:43 -0400222 canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
Brian Salomon0bd699e2017-02-01 12:23:25 -0500223 canvas->restore();
224 }
225}
Jim Van Verth78c8f302017-05-15 10:44:22 -0400226
227DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500228 draw_paths(canvas, kDebugColorNoOccluders);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400229}
230
231DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
Jim Van Verth744cbb32017-12-19 13:02:38 -0500232 draw_paths(canvas, kDebugColorOccluders);
233}
234
235DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
236 draw_paths(canvas, kGrayscale);
Jim Van Verth78c8f302017-05-15 10:44:22 -0400237}
Mike Reedf36b37f2020-03-27 15:11:10 -0400238
239#include "include/effects/SkGradientShader.h"
240#include "src/core/SkColorFilterPriv.h"
241
242DEF_SIMPLE_GM(shadow_utils_gaussian_colorfilter, canvas, 512, 256) {
243 const SkRect r = SkRect::MakeWH(256, 256);
244
245 const SkColor colors[] = { 0, 0xFF000000 };
246 auto sh = SkGradientShader::MakeRadial({r.centerX(), r.centerY()}, r.width(),
247 colors, nullptr, SK_ARRAY_COUNT(colors),
248 SkTileMode::kClamp);
249
250 SkPaint redPaint;
251 redPaint.setColor(SK_ColorRED);
252
253 SkPaint paint;
254 paint.setShader(sh);
255 canvas->drawRect(r, redPaint);
256 canvas->drawRect(r, paint);
257
258 canvas->translate(256, 0);
259 paint.setColorFilter(SkColorFilterPriv::MakeGaussian());
260 canvas->drawRect(r, redPaint);
261 canvas->drawRect(r, paint);
262}