• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #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"
23 #include "include/utils/SkShadowUtils.h"
24 
25 #include <initializer_list>
26 
draw_shadow(SkCanvas * canvas,const SkPath & path,SkScalar height,SkColor color,SkPoint3 lightPos,SkScalar lightR,bool isAmbient,uint32_t flags)27 void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color,
28                  SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) {
29     SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
30     SkScalar spotAlpha = isAmbient ? 0.f : .5f;
31     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);
37 }
38 
39 static constexpr int kW = 800;
40 static constexpr int kH = 960;
41 
42 enum ShadowMode {
43     kDebugColorNoOccluders,
44     kDebugColorOccluders,
45     kGrayscale
46 };
47 
draw_paths(SkCanvas * canvas,ShadowMode mode)48 void draw_paths(SkCanvas* canvas, ShadowMode mode) {
49     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);
57     paths.push_back().addOval(SkRect::MakeWH(20, 60));
58 
59     // 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 
80     static constexpr SkScalar kPad = 15.f;
81     static constexpr SkScalar kLightR = 100.f;
82     static constexpr SkScalar kHeight = 50.f;
83 
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 
88     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) {
98         for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
99             int pathCounter = 0;
100             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);
115 
116                 // 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                 }
121                 if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
122                     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) {
127                     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);
131                 }
132 
133                 SkPaint paint;
134                 paint.setAntiAlias(true);
135                 if (kDebugColorNoOccluders == mode) {
136                     // Draw the path outline in green on top of the ambient and spot shadows.
137                     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 {
145                     paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
146                     if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
147                         paint.setAlphaf(0.5f);
148                     }
149                     paint.setStyle(SkPaint::kFill_Style);
150                 }
151                 canvas->drawPath(path, paint);
152                 if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
153                     canvas->restore();
154                 }
155                 canvas->restore();
156 
157                 canvas->translate(dx, 0);
158                 x += dx;
159                 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
160                 ++pathCounter;
161             }
162         }
163     }
164 
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;
210             dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
211         }
212     }
213 
214     // 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);
222         canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
223         canvas->restore();
224     }
225 }
226 
DEF_SIMPLE_GM(shadow_utils,canvas,kW,kH)227 DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
228     draw_paths(canvas, kDebugColorNoOccluders);
229 }
230 
DEF_SIMPLE_GM(shadow_utils_occl,canvas,kW,kH)231 DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
232     draw_paths(canvas, kDebugColorOccluders);
233 }
234 
DEF_SIMPLE_GM(shadow_utils_gray,canvas,kW,kH)235 DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
236     draw_paths(canvas, kGrayscale);
237 }
238