Chromium Code Reviews| 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" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkGaussianEdgeShader.h" | |
| 12 #include "SkPath.h" | 13 #include "SkPath.h" |
| 13 #include "SkPoint3.h" | 14 #include "SkPoint3.h" |
| 14 #include "SkUtils.h" | 15 #include "SkUtils.h" |
| 15 #include "SkView.h" | 16 #include "SkView.h" |
| 16 #include "sk_tool_utils.h" | 17 #include "sk_tool_utils.h" |
| 17 | 18 |
| 19 //////////////////////////////////////////////////////////////////////////// | |
| 20 | |
| 18 class ShadowsView : public SampleView { | 21 class ShadowsView : public SampleView { |
| 19 SkPath fRectPath; | 22 SkPath fRectPath; |
| 20 SkPath fRRPath; | 23 SkPath fRRPath; |
| 21 SkPath fCirclePath; | 24 SkPath fCirclePath; |
| 22 SkPoint3 fLightPos; | 25 SkPoint3 fLightPos; |
| 23 | 26 |
| 24 bool fShowAmbient; | 27 bool fShowAmbient; |
| 25 bool fShowSpot; | 28 bool fShowSpot; |
| 26 bool fShowObject; | 29 bool fShowObject; |
| 27 | 30 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 occlRect, | 116 occlRect, |
| 114 SkBlurMaskFilter::kNone_ BlurFlag); | 117 SkBlurMaskFilter::kNone_ BlurFlag); |
| 115 | 118 |
| 116 SkPaint paint; | 119 SkPaint paint; |
| 117 paint.setAntiAlias(true); | 120 paint.setAntiAlias(true); |
| 118 paint.setMaskFilter(std::move(mf)); | 121 paint.setMaskFilter(std::move(mf)); |
| 119 paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlpha*25 5.999f), 0, 0, 0)); | 122 paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlpha*25 5.999f), 0, 0, 0)); |
| 120 canvas->drawPath(path, paint); | 123 canvas->drawPath(path, paint); |
| 121 | 124 |
| 122 // draw occlusion rect | 125 // draw occlusion rect |
| 126 #if DRAW_OCCL_RECT | |
| 123 SkPaint stroke; | 127 SkPaint stroke; |
| 124 stroke.setStyle(SkPaint::kStroke_Style); | 128 stroke.setStyle(SkPaint::kStroke_Style); |
| 125 stroke.setColor(SK_ColorBLUE); | 129 stroke.setColor(SK_ColorBLUE); |
| 126 canvas->drawRect(occlRect, stroke); | 130 canvas->drawRect(occlRect, stroke); |
| 131 #endif | |
| 132 } | |
| 133 | |
| 134 void drawAmbientShadowAlt(SkCanvas* canvas, const SkPath& path, SkScalar zVa lue, | |
| 135 SkScalar ambientAlpha) { | |
| 136 | |
| 137 if (ambientAlpha <= 0) { | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 const SkScalar kHeightFactor = 1.f / 128.f; | |
| 142 const SkScalar kGeomFactor = 64; | |
| 143 | |
| 144 SkScalar umbraAlpha = 1 / (1 + SkMaxScalar(zValue*kHeightFactor, 0)); | |
| 145 SkScalar radius = zValue*kHeightFactor*kGeomFactor; | |
| 146 | |
| 147 // fast path | |
| 148 SkRect pathRect; | |
| 149 SkRRect pathRRect; | |
| 150 if ((path.isOval(&pathRect) && pathRect.width() == pathRect.height()) || | |
| 151 (path.isRRect(&pathRRect) && pathRRect.allCornersCircular()) || | |
| 152 path.isRect(&pathRect)) { | |
| 153 | |
| 154 // For all of these, we outset the rect by half the radius to get ou r stroke shape. | |
| 155 if (path.isOval(nullptr)) { | |
| 156 pathRect.outset(0.5f*radius, 0.5f*radius); | |
| 157 pathRRect = SkRRect::MakeOval(pathRect); | |
| 158 } else if (path.isRect(nullptr)) { | |
| 159 pathRect.outset(0.5f*radius, 0.5f*radius); | |
| 160 pathRRect = SkRRect::MakeRectXY(pathRect, 0.5f*radius, 0.5f*radi us); | |
| 161 } else { | |
| 162 pathRRect.outset(0.5f*radius, 0.5f*radius); | |
| 163 } | |
| 164 | |
| 165 SkPaint paint; | |
| 166 paint.setAntiAlias(true); | |
| 167 paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlph a*255.999f), | |
| 168 0, 0, 0)); | |
| 169 paint.setStrokeWidth(radius); | |
| 170 paint.setStyle(SkPaint::kStroke_Style); | |
| 171 | |
| 172 sk_sp<SkShader> gaussShader = SkGaussianEdgeShader::Make(); | |
|
robertphillips
2016/08/16 17:56:07
std::move ?
jvanverth1
2016/08/16 19:14:11
Done.
| |
| 173 paint.setShader(gaussShader); | |
| 174 canvas->drawRRect(pathRRect, paint); | |
| 175 } else { | |
| 176 // occlude blur | |
| 177 SkRect occlRect; | |
| 178 GetOcclRect(path, &occlRect); | |
| 179 sk_sp<SkMaskFilter> f = SkBlurMaskFilter::Make(kNormal_SkBlurStyle, | |
| 180 SkBlurMask::ConvertRa diusToSigma(radius), | |
| 181 occlRect, | |
| 182 SkBlurMaskFilter::kNo ne_BlurFlag); | |
| 183 | |
| 184 SkPaint paint; | |
| 185 paint.setAntiAlias(true); | |
| 186 paint.setMaskFilter(std::move(f)); | |
| 187 paint.setColor(SkColorSetARGB((unsigned char)(ambientAlpha*umbraAlph a*255.999f), | |
| 188 0, 0, 0)); | |
| 189 canvas->drawPath(path, paint); | |
| 190 | |
| 191 // draw occlusion rect | |
| 192 #if DRAW_OCCL_RECT | |
| 193 SkPaint stroke; | |
| 194 stroke.setStyle(SkPaint::kStroke_Style); | |
| 195 stroke.setColor(SK_ColorBLUE); | |
| 196 canvas->drawRect(occlRect, stroke); | |
| 197 #endif | |
| 198 } | |
| 199 | |
| 127 } | 200 } |
| 128 | 201 |
| 129 void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, | 202 void drawSpotShadow(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 130 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp ha) { | 203 SkPoint3 lightPos, SkScalar lightWidth, SkScalar spotAlp ha) { |
| 131 if (spotAlpha <= 0) { | 204 if (spotAlpha <= 0) { |
| 132 return; | 205 return; |
| 133 } | 206 } |
| 134 | 207 |
| 135 SkScalar zRatio = zValue / (lightPos.fZ - zValue); | 208 SkScalar zRatio = zValue / (lightPos.fZ - zValue); |
| 136 if (zRatio < 0.0f) { | 209 if (zRatio < 0.0f) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 paint.setAntiAlias(true); | 244 paint.setAntiAlias(true); |
| 172 paint.setMaskFilter(std::move(mf)); | 245 paint.setMaskFilter(std::move(mf)); |
| 173 paint.setColor(SkColorSetARGB((unsigned char)(spotAlpha*255.999f), 0, 0, 0)); | 246 paint.setColor(SkColorSetARGB((unsigned char)(spotAlpha*255.999f), 0, 0, 0)); |
| 174 | 247 |
| 175 // apply transformation to shadow | 248 // apply transformation to shadow |
| 176 canvas->translate(offset.fX, offset.fY); | 249 canvas->translate(offset.fX, offset.fY); |
| 177 canvas->scale(scale, scale); | 250 canvas->scale(scale, scale); |
| 178 canvas->drawPath(path, paint); | 251 canvas->drawPath(path, paint); |
| 179 | 252 |
| 180 // draw occlusion rect | 253 // draw occlusion rect |
| 254 #if DRAW_OCCL_RECT | |
| 181 SkPaint stroke; | 255 SkPaint stroke; |
| 182 stroke.setStyle(SkPaint::kStroke_Style); | 256 stroke.setStyle(SkPaint::kStroke_Style); |
| 183 stroke.setColor(SK_ColorRED); | 257 stroke.setColor(SK_ColorRED); |
| 184 canvas->drawRect(occlRect, stroke); | 258 canvas->drawRect(occlRect, stroke) |
| 259 #endif | |
| 185 } | 260 } |
| 186 | 261 |
| 187 void drawShadowedPath(SkCanvas* canvas, const SkPath& path, SkScalar zValue, | 262 void drawShadowedPath(SkCanvas* canvas, const SkPath& path, SkScalar zValue, |
| 188 const SkPaint& paint) { | 263 const SkPaint& paint) { |
| 189 const SkScalar kLightWidth = 3; | 264 const SkScalar kLightWidth = 3; |
| 190 const SkScalar kAmbientAlpha = 0.25f; | 265 const SkScalar kAmbientAlpha = 0.25f; |
| 191 const SkScalar kSpotAlpha = 0.25f; | 266 const SkScalar kSpotAlpha = 0.25f; |
| 192 | 267 |
| 193 if (fShowAmbient) { | 268 if (fShowAmbient) { |
|
robertphillips
2016/08/16 17:56:07
Do we want to toggle between the two options ?
jvanverth1
2016/08/16 19:14:11
Done.
| |
| 194 this->drawAmbientShadow(canvas, path, zValue, kAmbientAlpha); | 269 this->drawAmbientShadowAlt(canvas, path, zValue, kAmbientAlpha); |
| 195 } | 270 } |
| 196 if (fShowSpot) { | 271 if (fShowSpot) { |
| 197 this->drawSpotShadow(canvas, path, zValue, fLightPos, kLightWidth, k SpotAlpha); | 272 this->drawSpotShadow(canvas, path, zValue, fLightPos, kLightWidth, k SpotAlpha); |
| 198 } | 273 } |
| 199 if (fShowObject) { | 274 if (fShowObject) { |
| 200 canvas->drawPath(path, paint); | 275 canvas->drawPath(path, paint); |
| 201 } | 276 } |
| 202 } | 277 } |
| 203 | 278 |
| 204 void onDrawContent(SkCanvas* canvas) override { | 279 void onDrawContent(SkCanvas* canvas) override { |
| 205 this->drawBG(canvas); | 280 this->drawBG(canvas); |
| 206 | 281 |
| 207 SkPaint paint; | 282 SkPaint paint; |
| 208 paint.setAntiAlias(true); | 283 paint.setAntiAlias(true); |
| 209 | 284 |
| 210 paint.setColor(SK_ColorWHITE); | 285 paint.setColor(SK_ColorWHITE); |
| 211 canvas->translate(200, 90); | 286 canvas->translate(200, 90); |
| 212 this->drawShadowedPath(canvas, fRectPath, 5, paint); | 287 this->drawShadowedPath(canvas, fRectPath, 5, paint); |
| 213 | 288 |
| 214 paint.setColor(SK_ColorRED); | 289 paint.setColor(SK_ColorRED); |
| 215 canvas->translate(250, 0); | 290 canvas->translate(250, 0); |
| 216 this->drawShadowedPath(canvas, fRRPath, 5, paint); | 291 this->drawShadowedPath(canvas, fRRPath, 5, paint); |
| 217 | 292 |
| 218 paint.setColor(SK_ColorBLUE); | 293 paint.setColor(SK_ColorBLUE); |
| 219 canvas->translate(-250, 110); | 294 canvas->translate(-250, 110); |
| 220 this->drawShadowedPath(canvas, fCirclePath, 5, paint); | 295 this->drawShadowedPath(canvas, fCirclePath, 5, paint); |
| 296 | |
| 297 paint.setColor(SK_ColorGREEN); | |
| 298 canvas->translate(250, 0); | |
| 299 this->drawShadowedPath(canvas, fRRPath, 5, paint); | |
| 221 } | 300 } |
| 222 | 301 |
| 223 protected: | 302 protected: |
| 224 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { | 303 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { |
| 225 return new SkView::Click(this); | 304 return new SkView::Click(this); |
| 226 } | 305 } |
| 227 | 306 |
| 228 bool onClick(Click *click) override { | 307 bool onClick(Click *click) override { |
| 229 SkScalar x = click->fCurr.fX; | 308 SkScalar x = click->fCurr.fX; |
| 230 SkScalar y = click->fCurr.fY; | 309 SkScalar y = click->fCurr.fY; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 242 } | 321 } |
| 243 | 322 |
| 244 private: | 323 private: |
| 245 typedef SkView INHERITED; | 324 typedef SkView INHERITED; |
| 246 }; | 325 }; |
| 247 | 326 |
| 248 ////////////////////////////////////////////////////////////////////////////// | 327 ////////////////////////////////////////////////////////////////////////////// |
| 249 | 328 |
| 250 static SkView* MyFactory() { return new ShadowsView; } | 329 static SkView* MyFactory() { return new ShadowsView; } |
| 251 static SkViewRegister reg(MyFactory); | 330 static SkViewRegister reg(MyFactory); |
| OLD | NEW |