blob: f5b66358865aa5e2b1ca3bbf51db72b93de58800 [file] [log] [blame]
Jim Van Verthe7705782017-05-04 14:00:59 -04001
2/*
3 * Copyright 2017 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkAnimTimer.h"
10#include "SkBlurMask.h"
11#include "SkBlurMaskFilter.h"
12#include "SkColorFilter.h"
13#include "SkCamera.h"
14#include "SkCanvas.h"
15#include "SkGaussianEdgeShader.h"
16#include "SkPath.h"
17#include "SkPathOps.h"
18#include "SkPoint3.h"
19#include "SkShadowUtils.h"
20#include "SkUtils.h"
21#include "SkView.h"
22#include "sk_tool_utils.h"
23
24////////////////////////////////////////////////////////////////////////////
25
26class ShadowUtilsView : public SampleView {
27 SkTArray<SkPath> fPaths;
28 SkScalar fZDelta;
29
30 bool fShowAmbient;
31 bool fShowSpot;
32 bool fUseAlt;
33 bool fShowObject;
34 bool fIgnoreShadowAlpha;
35
36public:
37 ShadowUtilsView()
38 : fZDelta(0)
39 , fShowAmbient(true)
40 , fShowSpot(true)
41 , fUseAlt(false)
42 , fShowObject(false)
43 , fIgnoreShadowAlpha(false) {}
44
45protected:
46 void onOnceBeforeDraw() override {
47 fPaths.push_back().addRoundRect(SkRect::MakeWH(50, 50), 10, 10);
48 SkRRect oddRRect;
49 oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
50 fPaths.push_back().addRRect(oddRRect);
51 fPaths.push_back().addRect(SkRect::MakeWH(50, 50));
52 fPaths.push_back().addCircle(25, 25, 25);
53 fPaths.push_back().cubicTo(100, 50, 20, 100, 0, 0);
54 fPaths.push_back().addOval(SkRect::MakeWH(20, 60));
55 }
56
57 // overrides from SkEventSink
58 bool onQuery(SkEvent* evt) override {
59 if (SampleCode::TitleQ(*evt)) {
60 SampleCode::TitleR(evt, "ShadowUtils");
61 return true;
62 }
63
64 SkUnichar uni;
65 if (SampleCode::CharQ(*evt, &uni)) {
66 bool handled = false;
67 switch (uni) {
68 case 'W':
69 fShowAmbient = !fShowAmbient;
70 handled = true;
71 break;
72 case 'S':
73 fShowSpot = !fShowSpot;
74 handled = true;
75 break;
76 case 'T':
77 fUseAlt = !fUseAlt;
78 handled = true;
79 break;
80 case 'O':
81 fShowObject = !fShowObject;
82 handled = true;
83 break;
84 case '>':
85 fZDelta += 0.5f;
86 handled = true;
87 break;
88 case '<':
89 fZDelta -= 0.5f;
90 handled = true;
91 break;
92 case '?':
93 fIgnoreShadowAlpha = !fIgnoreShadowAlpha;
94 handled = true;
95 break;
96 default:
97 break;
98 }
99 if (handled) {
100 this->inval(nullptr);
101 return true;
102 }
103 }
104 return this->INHERITED::onQuery(evt);
105 }
106
107 void drawBG(SkCanvas* canvas) {
Jim Van Verth8d1e0ac2017-05-05 15:53:23 -0400108 canvas->drawColor(0xFFFFFFFF);
Jim Van Verthe7705782017-05-04 14:00:59 -0400109 }
110
111 void drawShadowedPath(SkCanvas* canvas, const SkPath& path,
112 std::function<SkScalar(SkScalar, SkScalar)> zFunc,
113 const SkPaint& paint, SkScalar ambientAlpha,
114 const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha,
115 uint32_t flags) {
116 if (fIgnoreShadowAlpha) {
117 ambientAlpha = 255;
118 spotAlpha = 255;
119 }
120 if (!fShowAmbient) {
121 ambientAlpha = 0;
122 }
123 if (!fShowSpot) {
124 spotAlpha = 0;
125 }
126 if (fUseAlt) {
127 flags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
128 }
129 //SkShadowUtils::DrawShadow(canvas, path,
130 // zValue,
131 // lightPos, lightWidth,
132 // ambientAlpha, spotAlpha, SK_ColorBLACK, flags);
133 SkShadowUtils::DrawUncachedShadow(canvas, path, zFunc,
134 lightPos, lightWidth,
135 ambientAlpha, 0, SK_ColorRED, flags);
136 SkShadowUtils::DrawUncachedShadow(canvas, path, zFunc,
137 lightPos, lightWidth,
138 0, spotAlpha, SK_ColorBLUE, flags);
139
140 if (fShowObject) {
141 canvas->drawPath(path, paint);
142 } else {
143 SkPaint strokePaint;
144
145 strokePaint.setColor(paint.getColor());
146 strokePaint.setStyle(SkPaint::kStroke_Style);
147
148 canvas->drawPath(path, strokePaint);
149 }
150 }
151
152 void onDrawContent(SkCanvas* canvas) override {
153 this->drawBG(canvas);
154
155 static constexpr int kW = 800;
156 static constexpr SkScalar kPad = 15.f;
157 static constexpr SkPoint3 kLightPos = { 250, 400, 500 };
158 static constexpr SkScalar kLightR = 100.f;
159 static constexpr SkScalar kHeight = 50.f;
160 static constexpr SkScalar kAmbientAlpha = 0.5f;
161 static constexpr SkScalar kSpotAlpha = 0.5f;
162
163 canvas->translate(3 * kPad, 3 * kPad);
164 canvas->save();
165 SkScalar x = 0;
166 SkScalar dy = 0;
167 SkTDArray<SkMatrix> matrices;
168 matrices.push()->reset();
169 SkMatrix* m = matrices.push();
170 m->setRotate(33.f, 25.f, 25.f);
171 m->postScale(1.2f, 0.8f, 25.f, 25.f);
172 SkPaint paint;
173 paint.setColor(SK_ColorGREEN);
174 paint.setAntiAlias(true);
175 SkScalar zValue = SkTMax(1.0f, kHeight + fZDelta);
176 std::function<SkScalar(SkScalar, SkScalar)> zFunc =
177 [zValue](SkScalar, SkScalar) { return zValue; };
178 for (auto& m : matrices) {
179 for (auto flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
180 for (const auto& path : fPaths) {
181 SkRect postMBounds = path.getBounds();
182 m.mapRect(&postMBounds);
183 SkScalar w = postMBounds.width() + kHeight;
184 SkScalar dx = w + kPad;
185 if (x + dx > kW - 3 * kPad) {
186 canvas->restore();
187 canvas->translate(0, dy);
188 canvas->save();
189 x = 0;
190 dy = 0;
191 }
192
193 canvas->save();
194 canvas->concat(m);
195 drawShadowedPath(canvas, path, zFunc, paint, kAmbientAlpha, kLightPos, kLightR,
196 kSpotAlpha, flags);
197 canvas->restore();
198
199 canvas->translate(dx, 0);
200 x += dx;
201 dy = SkTMax(dy, postMBounds.height() + kPad + kHeight);
202 }
203 }
204 }
205 // Show where the light is in x,y as a circle (specified in device space).
206 SkMatrix invCanvasM = canvas->getTotalMatrix();
207 if (invCanvasM.invert(&invCanvasM)) {
208 canvas->save();
209 canvas->concat(invCanvasM);
210 SkPaint paint;
211 paint.setColor(SK_ColorBLACK);
212 paint.setAntiAlias(true);
213 canvas->drawCircle(kLightPos.fX, kLightPos.fY, kLightR / 10.f, paint);
214 canvas->restore();
215 }
216 }
217
218private:
219 typedef SampleView INHERITED;
220};
221
222//////////////////////////////////////////////////////////////////////////////
223
224static SkView* MyFactory() { return new ShadowUtilsView; }
225static SkViewRegister reg(MyFactory);