Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: samplecode/SampleShadowing.cpp

Issue 2224163005: Made shadows blurry (thru implementing variance mapping) (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Trying different include path Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/private/SkShadowParams.h ('k') | src/core/SkCanvas.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkPictureRecorder.h" 9 #include "SkPictureRecorder.h"
10 #include "SkShadowPaintFilterCanvas.h" 10 #include "SkShadowPaintFilterCanvas.h"
(...skipping 20 matching lines...) Expand all
31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); 31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300);
32 32
33 fTestRects[1].fColor = 0xFF88EE88; 33 fTestRects[1].fColor = 0xFF88EE88;
34 fTestRects[1].fDepth = 160; 34 fTestRects[1].fDepth = 160;
35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); 35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350);
36 36
37 fTestRects[2].fColor = 0xFF8888EE; 37 fTestRects[2].fColor = 0xFF8888EE;
38 fTestRects[2].fDepth = 240; 38 fTestRects[2].fDepth = 240;
39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); 39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250);
40 40
41 fSliders[0].fGeometry = SkRect::MakeLTRB(20, 400, 30, 420);
42 fSliders[0].fOffset = 0.0f;
43 fSliders[0].fScale = 0.1f;
44
45 fSliders[1].fGeometry = SkRect::MakeLTRB(100, 420, 110, 440);
46 fSliders[1].fOffset = 0.0f;
47 fSliders[1].fScale = 10.0f;
48
49 fSliders[2].fGeometry = SkRect::MakeLTRB(0, 440, 10, 460);
50 fSliders[2].fOffset = 0.0f;
51 fSliders[2].fScale = 0.0025f;
52
41 fSceneChanged = true; 53 fSceneChanged = true;
42 fLightsChanged = true; 54 fLightsChanged = true;
43 55
44 fSelectedRect = -1; 56 fSelectedRect = -1;
57 fSelectedSlider = -1;
45 fMoveLight = false; 58 fMoveLight = false;
46 59
47 fClearShadowMaps = false; 60 fClearShadowMaps = false;
61
62 fShadowParams.fShadowRadius = 2.0f;
63 fShadowParams.fBiasingConstant = 0.3f;
64 fShadowParams.fMinVariance = 1024;
65 fShadowParams.fType = SkShadowParams::kVariance_ShadowType;
48 } 66 }
49 67
50 protected: 68 protected:
51 bool onQuery(SkEvent *evt) override { 69 bool onQuery(SkEvent *evt) override {
52 if (SampleCode::TitleQ(*evt)) { 70 if (SampleCode::TitleQ(*evt)) {
53 SampleCode::TitleR(evt, "shadowing"); 71 SampleCode::TitleR(evt, "shadowing");
54 return true; 72 return true;
55 } 73 }
56 74
57 SkUnichar uni; 75 SkUnichar uni;
58 if (SampleCode::CharQ(*evt, &uni)) { 76 if (SampleCode::CharQ(*evt, &uni)) {
59 switch (uni) { 77 switch (uni) {
60 case 'L': 78 case 'L':
61 fMoveLight = !fMoveLight; 79 fMoveLight = !fMoveLight;
62 break; 80 break;
63 case 'd': 81 case 'd':
64 // Raster generated shadow maps have their origin in the UL corner 82 // Raster generated shadow maps have their origin in the UL corner
65 // GPU shadow maps can have an arbitrary origin. 83 // GPU shadow maps can have an arbitrary origin.
66 // We override the 'd' keypress so that when the device is c ycled, 84 // We override the 'd' keypress so that when the device is c ycled,
67 // the shadow maps will be re-generated according to the new backend. 85 // the shadow maps will be re-generated according to the new backend.
68 fClearShadowMaps = true; 86 fClearShadowMaps = true;
69 break; 87 break;
88 case 'B':
89 if (SkShadowParams::kVariance_ShadowType == fShadowParams.fT ype) {
90 fShadowParams.fType = SkShadowParams::kNoBlur_ShadowType ;
91 } else if (SkShadowParams::kNoBlur_ShadowType ==
92 fShadowParams.fType) {
93 fShadowParams.fType = SkShadowParams::kVariance_ShadowTy pe;
94 }
95 fLightsChanged = true;
96 break;
70 default: 97 default:
71 break; 98 break;
72 } 99 }
73 } 100 }
74 return this->INHERITED::onQuery(evt); 101 return this->INHERITED::onQuery(evt);
75 } 102 }
76 103
77 sk_sp<SkPicture> makeTestPicture(int width, int height) { 104 sk_sp<SkPicture> makeTestPicture(int width, int height) {
78 SkPictureRecorder recorder; 105 SkPictureRecorder recorder;
79 106
(...skipping 26 matching lines...) Expand all
106 133
107 void onDrawContent(SkCanvas *canvas) override { 134 void onDrawContent(SkCanvas *canvas) override {
108 if (fSceneChanged) { 135 if (fSceneChanged) {
109 fPicture = this->makeTestPicture(kWidth, kHeight); 136 fPicture = this->makeTestPicture(kWidth, kHeight);
110 } 137 }
111 138
112 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { 139 if (fSceneChanged || fLightsChanged || fClearShadowMaps) {
113 for (int i = 0; i < fLights->numLights(); i++) { 140 for (int i = 0; i < fLights->numLights(); i++) {
114 fLights->light(i).setShadowMap(nullptr); 141 fLights->light(i).setShadowMap(nullptr);
115 } 142 }
143
116 fSceneChanged = false; 144 fSceneChanged = false;
117 fLightsChanged = false; 145 fLightsChanged = false;
118 fClearShadowMaps = false; 146 fClearShadowMaps = false;
119 } 147 }
120 148
121 canvas->setLights(fLights); 149 canvas->setLights(fLights);
122 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); 150 canvas->drawShadowedPicture(fPicture, nullptr, nullptr, fShadowParams);
151
152 for (int i = 0; i < kNumSliders; i++) {
153 SkPaint paint;
154 paint.setColor(SK_ColorBLACK);
155 canvas->drawRect(fSliders[i].fGeometry, paint);
156 }
123 } 157 }
124 158
125 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { 159 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
126 return new SkView::Click(this); 160 return new SkView::Click(this);
127 } 161 }
128 162
163 void updateFromSelectedSlider() {
164 SkScalar newValue = fSliders[fSelectedSlider].fGeometry.fLeft *
165 fSliders[fSelectedSlider].fScale +
166 fSliders[fSelectedSlider].fOffset;
167
168 switch (fSelectedSlider) {
169 case 0:
170 fShadowParams.fShadowRadius = newValue;
171 break;
172 case 1:
173 fShadowParams.fMinVariance = newValue;
174 break;
175 case 2:
176 fShadowParams.fBiasingConstant = newValue;
177 break;
178 default:
179 break;
180 }
181 }
182
129 bool onClick(Click *click) override { 183 bool onClick(Click *click) override {
130 SkScalar x = click->fCurr.fX; 184 SkScalar x = click->fCurr.fX;
131 SkScalar y = click->fCurr.fY; 185 SkScalar y = click->fCurr.fY;
132 186
133 SkScalar dx = x - click->fPrev.fX; 187 SkScalar dx = x - click->fPrev.fX;
134 SkScalar dy = y - click->fPrev.fY; 188 SkScalar dy = y - click->fPrev.fY;
135 189
136 if (fMoveLight) { 190 if (fMoveLight) {
137 if (dx != 0 || dy != 0) { 191 if (dx != 0 || dy != 0) {
138 float recipX = 1.0f / kWidth; 192 float recipX = 1.0f / kWidth;
(...skipping 15 matching lines...) Expand all
154 fLights = builder.finish(); 208 fLights = builder.finish();
155 209
156 fLightsChanged = true; 210 fLightsChanged = true;
157 this->inval(nullptr); 211 this->inval(nullptr);
158 } 212 }
159 return true; 213 return true;
160 } 214 }
161 215
162 if (click->fState == Click::State::kUp_State) { 216 if (click->fState == Click::State::kUp_State) {
163 fSelectedRect = -1; 217 fSelectedRect = -1;
218 fSelectedSlider = -1;
164 return true; 219 return true;
165 } 220 }
166 221
167 if (fSelectedRect > -1) { 222 if (fSelectedRect > -1) {
168 fTestRects[fSelectedRect].fGeometry.offset(dx, dy); 223 fTestRects[fSelectedRect].fGeometry.offset(dx, dy);
169 224
170 fSceneChanged = true; 225 fSceneChanged = true;
171 this->inval(nullptr); 226 this->inval(nullptr);
172 return true; 227 return true;
173 } 228 }
174 229
230 if (fSelectedSlider > -1) {
231 fSliders[fSelectedSlider].fGeometry.offset(dx, 0);
232
233 this->updateFromSelectedSlider();
234
235 fLightsChanged = true;
236 this->inval(nullptr);
237 return true;
238 }
239
175 // assume last elements are highest 240 // assume last elements are highest
176 for (int i = kNumTestRects - 1; i >= 0; i--) { 241 for (int i = kNumTestRects - 1; i >= 0; i--) {
177 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) { 242 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
178 fSelectedRect = i; 243 fSelectedRect = i;
179 fTestRects[i].fGeometry.offset(dx, dy); 244 fTestRects[i].fGeometry.offset(dx, dy);
180 245
181 fSceneChanged = true; 246 fSceneChanged = true;
182 this->inval(nullptr); 247 this->inval(nullptr);
183 break; 248 break;
184 } 249 }
185 } 250 }
186 251
252 for (int i = 0; i <= kNumSliders; i++) {
253 if (fSliders[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
254 fSelectedSlider = i;
255 fSliders[i].fGeometry.offset(dx, 0);
256
257 this->updateFromSelectedSlider();
258
259 fLightsChanged = true;
260
261 this->inval(nullptr);
262 break;
263 }
264 }
265
187 return true; 266 return true;
188 } 267 }
189 268
190 private: 269 private:
191 static constexpr int kNumTestRects = 3; 270 static constexpr int kNumTestRects = 3;
271 static constexpr int kNumSliders = 3;
192 272
193 static const int kWidth = 400; 273 static const int kWidth = 400;
194 static const int kHeight = 400; 274 static const int kHeight = 400;
195 bool fClearShadowMaps;
196 275
197 struct { 276 struct {
198 SkRect fGeometry; 277 SkRect fGeometry;
199 int fDepth; 278 int fDepth;
200 SkColor fColor; 279 SkColor fColor;
201 } fTestRects[kNumTestRects]; 280 } fTestRects[kNumTestRects];
281 int fSelectedRect;
202 282
203 int fSelectedRect; 283 struct {
284 SkRect fGeometry;
285 SkScalar fOffset;
286 SkScalar fScale;
287 } fSliders[kNumSliders];
288 int fSelectedSlider;
289
290 bool fClearShadowMaps;
204 bool fMoveLight; 291 bool fMoveLight;
205
206 sk_sp<SkPicture> fPicture;
207
208 bool fSceneChanged; 292 bool fSceneChanged;
209 bool fLightsChanged; 293 bool fLightsChanged;
210 294
295 sk_sp<SkPicture> fPicture;
296 SkShadowParams fShadowParams;
211 sk_sp<SkLights> fLights; 297 sk_sp<SkLights> fLights;
212 298
213 typedef SampleView INHERITED; 299 typedef SampleView INHERITED;
214 }; 300 };
215 301
216 ////////////////////////////////////////////////////////////////////////////// 302 //////////////////////////////////////////////////////////////////////////////
217 static SkView* MyFactory() { return new ShadowingView; } 303 static SkView* MyFactory() { return new ShadowingView; }
218 static SkViewRegister reg(MyFactory); 304 static SkViewRegister reg(MyFactory);
219 305
220 #endif 306 #endif
OLDNEW
« no previous file with comments | « include/private/SkShadowParams.h ('k') | src/core/SkCanvas.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698