OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2008 The Android Open Source Project | 2 * Copyright 2008 The Android Open Source Project |
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 "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkCanvasPriv.h" | 10 #include "SkCanvasPriv.h" |
11 #include "SkClipStack.h" | 11 #include "SkClipStack.h" |
12 #include "SkColorFilter.h" | 12 #include "SkColorFilter.h" |
13 #include "SkDraw.h" | 13 #include "SkDraw.h" |
14 #include "SkDrawable.h" | 14 #include "SkDrawable.h" |
15 #include "SkDrawFilter.h" | 15 #include "SkDrawFilter.h" |
16 #include "SkDrawLooper.h" | 16 #include "SkDrawLooper.h" |
17 #include "SkErrorInternals.h" | 17 #include "SkErrorInternals.h" |
18 #include "SkImage.h" | 18 #include "SkImage.h" |
19 #include "SkImage_Base.h" | 19 #include "SkImage_Base.h" |
20 #include "SkImageFilter.h" | 20 #include "SkImageFilter.h" |
21 #include "SkImageFilterCache.h" | 21 #include "SkImageFilterCache.h" |
22 #include "SkLatticeIter.h" | 22 #include "SkLatticeIter.h" |
23 #include "SkMatrixUtils.h" | 23 #include "SkMatrixUtils.h" |
24 #include "SkMetaData.h" | 24 #include "SkMetaData.h" |
25 #include "SkNx.h" | 25 #include "SkNx.h" |
26 #include "SkPaintPriv.h" | 26 #include "SkPaintPriv.h" |
27 #include "SkPatchUtils.h" | 27 #include "SkPatchUtils.h" |
28 #include "SkPicture.h" | 28 #include "SkPicture.h" |
29 #include "SkRadialShadowMapShader.h" | |
29 #include "SkRasterClip.h" | 30 #include "SkRasterClip.h" |
30 #include "SkReadPixelsRec.h" | 31 #include "SkReadPixelsRec.h" |
31 #include "SkRRect.h" | 32 #include "SkRRect.h" |
32 #include "SkShadowPaintFilterCanvas.h" | 33 #include "SkShadowPaintFilterCanvas.h" |
33 #include "SkShadowShader.h" | 34 #include "SkShadowShader.h" |
34 #include "SkSmallAllocator.h" | 35 #include "SkSmallAllocator.h" |
35 #include "SkSpecialImage.h" | 36 #include "SkSpecialImage.h" |
36 #include "SkSurface_Base.h" | 37 #include "SkSurface_Base.h" |
37 #include "SkTextBlob.h" | 38 #include "SkTextBlob.h" |
38 #include "SkTextFormatParams.h" | 39 #include "SkTextFormatParams.h" |
(...skipping 3193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3232 if (this->quickReject(bounds)) { | 3233 if (this->quickReject(bounds)) { |
3233 return; | 3234 return; |
3234 } | 3235 } |
3235 } | 3236 } |
3236 | 3237 |
3237 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); | 3238 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
3238 | 3239 |
3239 sk_sp<SkImage> povDepthMap; | 3240 sk_sp<SkImage> povDepthMap; |
3240 sk_sp<SkImage> diffuseMap; | 3241 sk_sp<SkImage> diffuseMap; |
3241 | 3242 |
3242 // TODO: pass the depth to the shader in vertices, or uniforms | |
3243 // so we don't have to render depth and color separately | |
3244 for (int i = 0; i < fLights->numLights(); ++i) { | |
3245 // skip over ambient lights; they don't cast shadows | |
3246 // lights that have shadow maps do not need updating (because lights are immutable) | |
3247 | |
3248 if (fLights->light(i).getShadowMap() != nullptr) { | |
3249 continue; | |
3250 } | |
3251 | |
3252 // TODO: compute the correct size of the depth map from the light proper ties | |
3253 // TODO: maybe add a kDepth_8_SkColorType | |
3254 // TODO: find actual max depth of picture | |
3255 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( | |
3256 fLights->light(i), 255, | |
3257 picture->cullRect().width(), | |
3258 picture->cullRect().height()); | |
3259 | |
3260 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight , | |
3261 kBGRA_8888_SkColorType, | |
3262 kOpaque_SkAlphaType); | |
3263 | |
3264 // Create a new surface (that matches the backend of canvas) | |
3265 // for each shadow map | |
3266 sk_sp<SkSurface> surf(this->makeSurface(info)); | |
3267 | |
3268 // Wrap another SPFCanvas around the surface | |
3269 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
3270 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
3271 depthMapCanvas->setShadowParams(params); | |
3272 | |
3273 // set the depth map canvas to have the light we're drawing. | |
3274 SkLights::Builder builder; | |
3275 builder.add(fLights->light(i)); | |
3276 sk_sp<SkLights> curLight = builder.finish(); | |
3277 depthMapCanvas->setLights(std::move(curLight)); | |
3278 | |
3279 depthMapCanvas->drawPicture(picture); | |
3280 sk_sp<SkImage> depthMap = surf->makeImageSnapshot(); | |
3281 | |
3282 if (params.fType == SkShadowParams::kNoBlur_ShadowType) { | |
3283 fLights->light(i).setShadowMap(std::move(depthMap)); | |
3284 } else if (params.fType == SkShadowParams::kVariance_ShadowType) { | |
3285 // we blur the variance map | |
3286 SkPaint blurPaint; | |
3287 blurPaint.setImageFilter(SkImageFilter::MakeBlur(params.fShadowRadiu s, | |
3288 params.fShadowRadiu s, nullptr)); | |
3289 | |
3290 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize .fHeight, | |
3291 kBGRA_8888_SkColorType, | |
3292 kOpaque_SkAlphaType); | |
3293 | |
3294 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo)); | |
3295 | |
3296 blurSurf->getCanvas()->drawImage(std::move(depthMap), 0, 0, &blurPai nt); | |
3297 | |
3298 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot()); | |
3299 } | |
3300 } | |
3301 | |
3302 // povDepthMap | 3243 // povDepthMap |
3303 { | 3244 { |
3304 SkLights::Builder builder; | 3245 SkLights::Builder builder; |
3305 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f), | 3246 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
3306 SkVector3::Make(0.0f, 0.0f, 1.0f))); | 3247 SkVector3::Make(0.0f, 0.0f, 1.0f))); |
3307 sk_sp<SkLights> povLight = builder.finish(); | 3248 sk_sp<SkLights> povLight = builder.finish(); |
3308 | 3249 |
3309 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), | 3250 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
3310 picture->cullRect().height(), | 3251 picture->cullRect().height(), |
3311 kBGRA_8888_SkColorType, | 3252 kBGRA_8888_SkColorType, |
(...skipping 19 matching lines...) Expand all Loading... | |
3331 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), | 3272 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
3332 picture->cullRect().height(), | 3273 picture->cullRect().height(), |
3333 kBGRA_8888_SkColorType, | 3274 kBGRA_8888_SkColorType, |
3334 kOpaque_SkAlphaType); | 3275 kOpaque_SkAlphaType); |
3335 | 3276 |
3336 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3277 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3337 surf->getCanvas()->drawPicture(picture); | 3278 surf->getCanvas()->drawPicture(picture); |
3338 | 3279 |
3339 diffuseMap = surf->makeImageSnapshot(); | 3280 diffuseMap = surf->makeImageSnapshot(); |
3340 } | 3281 } |
3341 SkPaint shadowPaint; | |
3342 | 3282 |
3343 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode, | 3283 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode, |
3344 SkShader::kClamp_Ti leMode); | 3284 SkShader::kClamp_Ti leMode); |
3345 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode, | 3285 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode, |
3346 SkShader::kClamp_Tile Mode); | 3286 SkShader::kClamp_Tile Mode); |
3287 | |
3288 // TODO: pass the depth to the shader in vertices, or uniforms | |
3289 // so we don't have to render depth and color separately | |
3290 for (int i = 0; i < fLights->numLights(); ++i) { | |
3291 // skip over ambient lights; they don't cast shadows | |
3292 // lights that have shadow maps do not need updating (because lights are immutable) | |
3293 sk_sp<SkImage> depthMap; | |
3294 SkISize shMapSize; | |
3295 | |
3296 if (fLights->light(i).getShadowMap() != nullptr) { | |
3297 continue; | |
3298 } | |
3299 | |
3300 if (fLights->light(i).isRadial()) { | |
3301 shMapSize.fHeight = 1; | |
3302 shMapSize.fWidth = (int) picture->cullRect().width(); | |
3303 | |
3304 SkImageInfo info = SkImageInfo::Make(diffuseMap->width(), 1, | |
3305 kBGRA_8888_SkColorType, | |
3306 kOpaque_SkAlphaType); | |
3307 | |
3308 // Create new surface (that matches the backend of canvas) | |
3309 // for each shadow map | |
3310 sk_sp<SkSurface> surf(this->makeSurface(info)); | |
3311 | |
3312 // Wrap another SPFCanvas around the surface | |
3313 SkCanvas* depthMapCanvas = surf->getCanvas(); | |
3314 | |
3315 sk_sp<SkShader> shadowMapShader; | |
3316 shadowMapShader = SkRadialShadowMapShader::Make( | |
3317 povDepthShader, fLights, | |
jvanverth1
2016/09/08 19:57:27
Shouldn't this be just the current light instead o
vjiaoblack
2016/09/08 20:25:08
Done.
| |
3318 (int) picture->cullRect().width(), | |
3319 (int) picture->cullRect().height()); | |
3320 | |
3321 SkPaint shadowMapPaint; | |
3322 shadowMapPaint.setShader(std::move(shadowMapShader)); | |
3323 | |
3324 depthMapCanvas->setLights(fLights); | |
jvanverth1
2016/09/08 19:57:27
Same here: just the current light?
vjiaoblack
2016/09/08 20:25:08
Done.
| |
3325 | |
3326 depthMapCanvas->drawRect(SkRect::MakeIWH(diffuseMap->width(), | |
3327 diffuseMap->height()), | |
3328 shadowMapPaint); | |
3329 | |
3330 depthMap = surf->makeImageSnapshot(); | |
3331 | |
3332 } else { | |
3333 // TODO: compute the correct size of the depth map from the light pr operties | |
3334 // TODO: maybe add a kDepth_8_SkColorType | |
3335 // TODO: find actual max depth of picture | |
3336 shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( | |
3337 fLights->light(i), 255, | |
3338 (int) picture->cullRect().width(), | |
3339 (int) picture->cullRect().height()); | |
3340 | |
3341 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe ight, | |
3342 kBGRA_8888_SkColorType, | |
3343 kOpaque_SkAlphaType); | |
3344 | |
3345 // Create a new surface (that matches the backend of canvas) | |
3346 // for each shadow map | |
3347 sk_sp<SkSurface> surf(this->makeSurface(info)); | |
3348 | |
3349 // Wrap another SPFCanvas around the surface | |
3350 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
3351 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
3352 depthMapCanvas->setShadowParams(params); | |
3353 | |
3354 // set the depth map canvas to have the light we're drawing. | |
3355 SkLights::Builder builder; | |
3356 builder.add(fLights->light(i)); | |
3357 sk_sp<SkLights> curLight = builder.finish(); | |
3358 depthMapCanvas->setLights(std::move(curLight)); | |
3359 | |
3360 depthMapCanvas->drawPicture(picture); | |
3361 depthMap = surf->makeImageSnapshot(); | |
3362 } | |
3363 | |
3364 if (params.fType == SkShadowParams::kNoBlur_ShadowType) { | |
3365 fLights->light(i).setShadowMap(std::move(depthMap)); | |
3366 } else if (params.fType == SkShadowParams::kVariance_ShadowType) { | |
3367 // we blur the variance map | |
3368 SkPaint blurPaint; | |
3369 blurPaint.setImageFilter(SkImageFilter::MakeBlur(params.fShadowRadiu s, | |
3370 params.fShadowRadiu s, nullptr)); | |
3371 | |
3372 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize .fHeight, | |
3373 kBGRA_8888_SkColorType, | |
3374 kOpaque_SkAlphaType); | |
3375 | |
3376 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo)); | |
3377 | |
3378 blurSurf->getCanvas()->drawImage(std::move(depthMap), 0, 0, &blurPai nt); | |
3379 | |
3380 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot()); | |
3381 } | |
3382 } | |
3383 | |
3384 SkPaint shadowPaint; | |
3347 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ), | 3385 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ), |
3348 std::move(diffuseShader) , | 3386 std::move(diffuseShader) , |
3349 std::move(fLights), | 3387 fLights, |
3350 diffuseMap->width(), | 3388 diffuseMap->width(), |
3351 diffuseMap->height(), | 3389 diffuseMap->height(), |
3352 params); | 3390 params); |
3353 | 3391 |
3354 shadowPaint.setShader(shadowShader); | 3392 shadowPaint.setShader(shadowShader); |
3355 | 3393 |
3356 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint); | 3394 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint); |
3357 } | 3395 } |
3358 #endif | 3396 #endif |
3359 | 3397 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3460 | 3498 |
3461 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { | 3499 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
3462 fCanvas->restoreToCount(fSaveCount); | 3500 fCanvas->restoreToCount(fSaveCount); |
3463 } | 3501 } |
3464 | 3502 |
3465 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API | 3503 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API |
3466 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) { | 3504 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) { |
3467 return this->makeSurface(info, props).release(); | 3505 return this->makeSurface(info, props).release(); |
3468 } | 3506 } |
3469 #endif | 3507 #endif |
OLD | NEW |