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 SkLights::Builder builder; |
| 3316 builder.add(fLights->light(i)); |
| 3317 sk_sp<SkLights> curLight = builder.finish(); |
| 3318 |
| 3319 sk_sp<SkShader> shadowMapShader; |
| 3320 shadowMapShader = SkRadialShadowMapShader::Make( |
| 3321 povDepthShader, curLight, |
| 3322 (int) picture->cullRect().width(), |
| 3323 (int) picture->cullRect().height()); |
| 3324 |
| 3325 SkPaint shadowMapPaint; |
| 3326 shadowMapPaint.setShader(std::move(shadowMapShader)); |
| 3327 |
| 3328 depthMapCanvas->setLights(curLight); |
| 3329 |
| 3330 depthMapCanvas->drawRect(SkRect::MakeIWH(diffuseMap->width(), |
| 3331 diffuseMap->height()), |
| 3332 shadowMapPaint); |
| 3333 |
| 3334 depthMap = surf->makeImageSnapshot(); |
| 3335 |
| 3336 } else { |
| 3337 // TODO: compute the correct size of the depth map from the light pr
operties |
| 3338 // TODO: maybe add a kDepth_8_SkColorType |
| 3339 // TODO: find actual max depth of picture |
| 3340 shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( |
| 3341 fLights->light(i), 255, |
| 3342 (int) picture->cullRect().width(), |
| 3343 (int) picture->cullRect().height()); |
| 3344 |
| 3345 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe
ight, |
| 3346 kBGRA_8888_SkColorType, |
| 3347 kOpaque_SkAlphaType); |
| 3348 |
| 3349 // Create a new surface (that matches the backend of canvas) |
| 3350 // for each shadow map |
| 3351 sk_sp<SkSurface> surf(this->makeSurface(info)); |
| 3352 |
| 3353 // Wrap another SPFCanvas around the surface |
| 3354 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 3355 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 3356 depthMapCanvas->setShadowParams(params); |
| 3357 |
| 3358 // set the depth map canvas to have the light we're drawing. |
| 3359 SkLights::Builder builder; |
| 3360 builder.add(fLights->light(i)); |
| 3361 sk_sp<SkLights> curLight = builder.finish(); |
| 3362 depthMapCanvas->setLights(std::move(curLight)); |
| 3363 |
| 3364 depthMapCanvas->drawPicture(picture); |
| 3365 depthMap = surf->makeImageSnapshot(); |
| 3366 } |
| 3367 |
| 3368 if (params.fType == SkShadowParams::kNoBlur_ShadowType) { |
| 3369 fLights->light(i).setShadowMap(std::move(depthMap)); |
| 3370 } else if (params.fType == SkShadowParams::kVariance_ShadowType) { |
| 3371 // we blur the variance map |
| 3372 SkPaint blurPaint; |
| 3373 blurPaint.setImageFilter(SkImageFilter::MakeBlur(params.fShadowRadiu
s, |
| 3374 params.fShadowRadiu
s, nullptr)); |
| 3375 |
| 3376 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize
.fHeight, |
| 3377 kBGRA_8888_SkColorType, |
| 3378 kOpaque_SkAlphaType); |
| 3379 |
| 3380 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo)); |
| 3381 |
| 3382 blurSurf->getCanvas()->drawImage(std::move(depthMap), 0, 0, &blurPai
nt); |
| 3383 |
| 3384 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot()); |
| 3385 } |
| 3386 } |
| 3387 |
| 3388 SkPaint shadowPaint; |
3347 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader
), | 3389 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader
), |
3348 std::move(diffuseShader)
, | 3390 std::move(diffuseShader)
, |
3349 std::move(fLights), | 3391 fLights, |
3350 diffuseMap->width(), | 3392 diffuseMap->width(), |
3351 diffuseMap->height(), | 3393 diffuseMap->height(), |
3352 params); | 3394 params); |
3353 | 3395 |
3354 shadowPaint.setShader(shadowShader); | 3396 shadowPaint.setShader(shadowShader); |
3355 | 3397 |
3356 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s
hadowPaint); | 3398 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s
hadowPaint); |
3357 } | 3399 } |
3358 #endif | 3400 #endif |
3359 | 3401 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3460 | 3502 |
3461 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { | 3503 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
3462 fCanvas->restoreToCount(fSaveCount); | 3504 fCanvas->restoreToCount(fSaveCount); |
3463 } | 3505 } |
3464 | 3506 |
3465 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API | 3507 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API |
3466 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p
rops) { | 3508 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p
rops) { |
3467 return this->makeSurface(info, props).release(); | 3509 return this->makeSurface(info, props).release(); |
3468 } | 3510 } |
3469 #endif | 3511 #endif |
OLD | NEW |