OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2016 Google Inc. | 3 * Copyright 2016 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkBlurMask.h" | 9 #include "SkBlurMask.h" |
10 #include "SkBlurMaskFilter.h" | 10 #include "SkBlurMaskFilter.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 #endif | 136 #endif |
137 } | 137 } |
138 | 138 |
139 void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zVa lue, | 139 void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zVa lue, |
140 SkScalar ambientAlpha) { | 140 SkScalar ambientAlpha) { |
141 | 141 |
142 if (ambientAlpha <= 0) { | 142 if (ambientAlpha <= 0) { |
143 return; | 143 return; |
144 } | 144 } |
145 | 145 |
146 SkRect pathRect; | |
147 SkRRect pathRRect; | |
148 if ((!path.isOval(&pathRect) || pathRect.width() != pathRect.height()) & & | |
149 (!path.isRRect(&pathRRect) || !pathRRect.allCornersCircular()) && | |
150 !path.isRect(&pathRect)) { | |
151 this->drawAmbientShadow(canvas, path, zValue, ambientAlpha); | |
152 return; | |
153 } | |
154 | |
155 const SkScalar kHeightFactor = 1.f / 128.f; | 146 const SkScalar kHeightFactor = 1.f / 128.f; |
156 const SkScalar kGeomFactor = 64; | 147 const SkScalar kGeomFactor = 64; |
157 | 148 |
158 SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); | 149 SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); |
159 SkScalar radius = zValue*kHeightFactor*kGeomFactor; | 150 SkScalar radius = zValue*kHeightFactor*kGeomFactor; |
160 | 151 |
161 // For all of these, we outset the rect by the radius to get our coverag e shape. | 152 SkRect pathRect; |
153 SkRRect pathRRect; | |
154 if (radius >= 64 || | |
155 (!path.isOval(&pathRect) || pathRect.width() != pathRect.height()) & & | |
156 (!path.isRRect(&pathRRect) || !pathRRect.allCornersCircular()) && | |
157 !path.isRect(&pathRect)) { | |
158 this->drawAmbientShadow(canvas, path, zValue, ambientAlpha); | |
159 return; | |
160 } | |
161 | |
162 // For all of these, we outset the rect by half the radius to get our st roke shape. | |
163 SkScalar halfRadius = SK_ScalarHalf*radius; | |
162 if (path.isOval(nullptr)) { | 164 if (path.isOval(nullptr)) { |
163 pathRect.outset(radius, radius); | 165 pathRect.outset(halfRadius, halfRadius); |
164 pathRRect = SkRRect::MakeOval(pathRect); | 166 pathRRect = SkRRect::MakeOval(pathRect); |
165 } else if (path.isRect(nullptr)) { | 167 } else if (path.isRect(nullptr)) { |
166 pathRect.outset(radius, radius); | 168 pathRect.outset(halfRadius, halfRadius); |
167 pathRRect = SkRRect::MakeRectXY(pathRect, radius, radius); | 169 pathRRect = SkRRect::MakeRectXY(pathRect, halfRadius, halfRadius); |
168 } else { | 170 } else { |
169 pathRRect.outset(radius, radius); | 171 pathRRect.outset(halfRadius, halfRadius); |
170 } | 172 } |
171 | 173 |
172 SkPaint paint; | 174 SkPaint paint; |
173 paint.setAntiAlias(true); | 175 paint.setAntiAlias(true); |
176 paint.setStyle(SkPaint::kStroke_Style); | |
177 // we outset the stroke a little to cover up AA on the interior edge | |
178 paint.setStrokeWidth(radius + 1); | |
174 // handle scale of radius due to CTM | 179 // handle scale of radius due to CTM |
175 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); | 180 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); |
176 radius *= maxScale; | 181 radius *= maxScale; |
177 unsigned char gray = (unsigned char)(ambientAlpha*umbraAlpha*255.999f); | 182 unsigned char gray = (unsigned char)(ambientAlpha*umbraAlpha*255.999f); |
robertphillips
2016/08/29 13:09:32
// Convert radius to 6.2 fixed point and place in
jvanverth1
2016/08/29 13:31:42
Done.
| |
178 SkASSERT(radius < 256); | 183 SkASSERT(radius < 64); |
179 // convert the radius to fixed point 8.8 and | 184 paint.setColor(SkColorSetARGB(1, gray, (unsigned char)(4.0f*radius), 0)) ; |
180 // place it in the G,B components of the color | |
181 unsigned char intPart = (unsigned char)radius; | |
182 SkScalar fracPart = radius - intPart; | |
183 paint.setColor(SkColorSetARGB(1, gray, intPart, (unsigned char)(fracPart *256.f))); | |
184 | 185 |
185 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); | 186 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); |
186 paint.setShader(gaussShader); | 187 paint.setShader(gaussShader); |
187 canvas->drawRRect(pathRRect, paint); | 188 canvas->drawRRect(pathRRect, paint); |
188 } | 189 } |
189 | 190 |
190 void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, | 191 void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
191 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp ha) { | 192 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp ha) { |
192 if (spotAlpha <= 0) { | 193 if (spotAlpha <= 0) { |
193 return; | 194 return; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 canvas->drawRect(occlRect, stroke) | 259 canvas->drawRect(occlRect, stroke) |
259 #endif | 260 #endif |
260 } | 261 } |
261 | 262 |
262 void drawSpotShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue , | 263 void drawSpotShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zValue , |
263 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp ha) { | 264 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp ha) { |
264 if (spotAlpha <= 0) { | 265 if (spotAlpha <= 0) { |
265 return; | 266 return; |
266 } | 267 } |
267 | 268 |
268 SkRect pathRect; | |
269 SkRRect pathRRect; | |
270 if ((!path.isOval(&pathRect) || pathRect.width() != pathRect.height()) & & | |
271 (!path.isRRect(&pathRRect) || !pathRRect.allCornersCircular()) && | |
272 !path.isRect(&pathRect)) { | |
273 this->drawSpotShadow(canvas, path, zValue, lightPos, lightWidth, spo tAlpha); | |
274 return; | |
275 } | |
276 | |
277 SkScalar zRatio = zValue / (lightPos.fZ - zValue); | 269 SkScalar zRatio = zValue / (lightPos.fZ - zValue); |
278 if (zRatio < 0.0f) { | 270 if (zRatio < 0.0f) { |
279 zRatio = 0.0f; | 271 zRatio = 0.0f; |
280 } else if (zRatio > 0.95f) { | 272 } else if (zRatio > 0.95f) { |
281 zRatio = 0.95f; | 273 zRatio = 0.95f; |
282 } | 274 } |
283 SkScalar radius = lightWidth*zRatio; | 275 SkScalar radius = lightWidth*zRatio; |
284 | 276 |
285 // For all of these, we outset the rect by the radius to get our coverag e shape. | 277 SkRect pathRect; |
278 SkRRect pathRRect; | |
279 if (radius >= 64 || | |
280 (!path.isOval(&pathRect) || pathRect.width() != pathRect.height()) & & | |
281 (!path.isRRect(&pathRRect) || !pathRRect.allCornersCircular()) && | |
282 !path.isRect(&pathRect)) { | |
283 this->drawSpotShadow(canvas, path, zValue, lightPos, lightWidth, spo tAlpha); | |
284 return; | |
285 } | |
286 | |
287 // For all of these, we outset the rect by half the radius to get our st roke shape. | |
288 SkScalar halfRadius = SK_ScalarHalf*radius; | |
286 if (path.isOval(nullptr)) { | 289 if (path.isOval(nullptr)) { |
287 pathRect.outset(radius, radius); | 290 pathRect.outset(halfRadius, halfRadius); |
288 pathRRect = SkRRect::MakeOval(pathRect); | 291 pathRRect = SkRRect::MakeOval(pathRect); |
289 } else if (path.isRect(nullptr)) { | 292 } else if (path.isRect(nullptr)) { |
290 pathRect.outset(radius, radius); | 293 pathRect.outset(halfRadius, halfRadius); |
291 pathRRect = SkRRect::MakeRectXY(pathRect, radius, radius); | 294 pathRRect = SkRRect::MakeRectXY(pathRect, halfRadius, halfRadius); |
292 } else { | 295 } else { |
293 pathRRect.outset(radius, radius); | 296 pathRRect.outset(halfRadius, halfRadius); |
294 } | 297 } |
295 | 298 |
296 // compute the transformation params | 299 // compute the transformation params |
297 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBound s().centerY()); | 300 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBound s().centerY()); |
298 canvas->getTotalMatrix().mapPoints(¢er, 1); | 301 canvas->getTotalMatrix().mapPoints(¢er, 1); |
299 SkPoint offset = SkPoint::Make(-zRatio*(lightPos.fX - center.fX), | 302 SkPoint offset = SkPoint::Make(-zRatio*(lightPos.fX - center.fX), |
300 -zRatio*(lightPos.fY - center.fY)); | 303 -zRatio*(lightPos.fY - center.fY)); |
301 SkAutoCanvasRestore acr(canvas, true); | 304 SkAutoCanvasRestore acr(canvas, true); |
302 | 305 |
303 SkPaint paint; | 306 SkPaint paint; |
304 paint.setAntiAlias(true); | 307 paint.setAntiAlias(true); |
308 // We outset the stroke by the length of the translation so the shadow e xtends to | |
309 // the edge of the shape. We also add one to cover up AA on the interior edge. | |
310 SkScalar pad = offset.length() + 1.0f; | |
311 // compute area | |
312 SkScalar strokeWidth = radius + 2.0f*pad; | |
313 SkScalar strokedArea = 2.0f*strokeWidth*(pathRRect.width() + pathRRect.h eight()); | |
314 SkScalar filledArea = (pathRRect.height() + radius)*(pathRRect.width() + radius); | |
315 // If the area of the stroked geometry is larger than the fill geometry, or | |
robertphillips
2016/08/29 13:09:33
add "6.2" before "fixed point" ?
jvanverth1
2016/08/29 13:31:42
Done.
| |
316 // if our pad is too big to convert to fixed point, just fill it. | |
317 if (strokedArea > filledArea || pad >= 64) { | |
318 pad = 0; | |
319 paint.setStyle(SkPaint::kStrokeAndFill_Style); | |
320 paint.setStrokeWidth(radius); | |
321 } else { | |
322 paint.setStyle(SkPaint::kStroke_Style); | |
323 paint.setStrokeWidth(strokeWidth); | |
324 } | |
305 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); | 325 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); |
306 paint.setShader(gaussShader); | 326 paint.setShader(gaussShader); |
307 // handle scale of radius due to CTM | 327 // handle scale of radius due to CTM |
308 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); | 328 SkScalar maxScale = canvas->getTotalMatrix().getMaxScale(); |
309 radius *= maxScale; | 329 radius *= maxScale; |
310 unsigned char gray = (unsigned char)(spotAlpha*255.999f); | 330 unsigned char gray = (unsigned char)(spotAlpha*255.999f); |
311 SkASSERT(radius < 256); | 331 SkASSERT(radius < 64); |
312 // convert the radius to fixed point 8.8 and | 332 SkASSERT(pad < 64); |
313 // place it in the G,B components of the color | 333 paint.setColor(SkColorSetARGB(1, gray, (unsigned char)(radius*4.0f), |
314 unsigned char intPart = (unsigned char)radius; | 334 (unsigned char)(pad*4.0f))); |
315 SkScalar fracPart = radius - intPart; | |
316 paint.setColor(SkColorSetARGB(1, gray, intPart, (unsigned char)(fracPart *256.f))); | |
317 | 335 |
318 // apply transformation to shadow | 336 // apply transformation to shadow |
319 canvas->translate(offset.fX, offset.fY); | 337 canvas->translate(offset.fX, offset.fY); |
320 #if 0 | 338 #if 0 |
321 // It looks like the scale may be invalid | 339 // It looks like the scale may be invalid |
322 SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); | 340 SkScalar scale = lightPos.fZ / (lightPos.fZ - zValue); |
323 if (scale < 1.0f) { | 341 if (scale < 1.0f) { |
324 scale = 1.0f; | 342 scale = 1.0f; |
325 } else if (scale > 1024.f) { | 343 } else if (scale > 1024.f) { |
326 scale = 1024.f; | 344 scale = 1024.f; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
412 } | 430 } |
413 | 431 |
414 private: | 432 private: |
415 typedef SkView INHERITED; | 433 typedef SkView INHERITED; |
416 }; | 434 }; |
417 | 435 |
418 ////////////////////////////////////////////////////////////////////////////// | 436 ////////////////////////////////////////////////////////////////////////////// |
419 | 437 |
420 static SkView* MyFactory() { return new ShadowsView; } | 438 static SkView* MyFactory() { return new ShadowsView; } |
421 static SkViewRegister reg(MyFactory); | 439 static SkViewRegister reg(MyFactory); |
OLD | NEW |