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 <<<<<<< 21114d931c32f34c1d25c68b25f3c2bf4c1d0f07 |
| 26 #include "SkNormalSource.h" |
| 27 #include "SkPaintFilterCanvas.h" |
25 #include "SkNx.h" | 28 #include "SkNx.h" |
| 29 ======= |
| 30 >>>>>>> Made req changes; added some sliders |
26 #include "SkPaintPriv.h" | 31 #include "SkPaintPriv.h" |
27 #include "SkPatchUtils.h" | 32 #include "SkPatchUtils.h" |
28 #include "SkPicture.h" | 33 #include "SkPicture.h" |
29 #include "SkRasterClip.h" | 34 #include "SkRasterClip.h" |
30 #include "SkReadPixelsRec.h" | 35 #include "SkReadPixelsRec.h" |
31 #include "SkRRect.h" | 36 #include "SkRRect.h" |
32 #include "SkShadowPaintFilterCanvas.h" | 37 #include "SkShadowPaintFilterCanvas.h" |
33 #include "SkShadowShader.h" | 38 #include "SkShadowShader.h" |
34 #include "SkSmallAllocator.h" | 39 #include "SkSmallAllocator.h" |
35 #include "SkSpecialImage.h" | 40 #include "SkSpecialImage.h" |
36 #include "SkSurface_Base.h" | 41 #include "SkSurface_Base.h" |
37 #include "SkTextBlob.h" | 42 #include "SkTextBlob.h" |
38 #include "SkTextFormatParams.h" | 43 #include "SkTextFormatParams.h" |
39 #include "SkTLazy.h" | 44 #include "SkTLazy.h" |
40 #include "SkTraceEvent.h" | 45 #include "SkTraceEvent.h" |
41 | 46 #include "../../include/effects/SkBlurImageFilter.h" |
42 #include <new> | 47 #include <new> |
43 | 48 |
44 #if SK_SUPPORT_GPU | 49 #if SK_SUPPORT_GPU |
45 #include "GrContext.h" | 50 #include "GrContext.h" |
46 #include "GrRenderTarget.h" | 51 #include "GrRenderTarget.h" |
47 #include "SkGrPriv.h" | 52 #include "SkGrPriv.h" |
| 53 |
48 #endif | 54 #endif |
49 | 55 |
50 #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) | 56 #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) |
51 | 57 |
52 //#define SK_SUPPORT_PRECHECK_CLIPRECT | 58 //#define SK_SUPPORT_PRECHECK_CLIPRECT |
53 | 59 |
54 /* | 60 /* |
55 * Return true if the drawing this rect would hit every pixels in the canvas. | 61 * Return true if the drawing this rect would hit every pixels in the canvas. |
56 * | 62 * |
57 * Returns false if | 63 * Returns false if |
(...skipping 3091 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3149 } | 3155 } |
3150 } | 3156 } |
3151 | 3157 |
3152 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); | 3158 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
3153 picture->playback(this); | 3159 picture->playback(this); |
3154 } | 3160 } |
3155 | 3161 |
3156 #ifdef SK_EXPERIMENTAL_SHADOWING | 3162 #ifdef SK_EXPERIMENTAL_SHADOWING |
3157 void SkCanvas::drawShadowedPicture(const SkPicture* picture, | 3163 void SkCanvas::drawShadowedPicture(const SkPicture* picture, |
3158 const SkMatrix* matrix, | 3164 const SkMatrix* matrix, |
3159 const SkPaint* paint) { | 3165 const SkPaint* paint, |
| 3166 const SkShadowParams& sParams) { |
3160 RETURN_ON_NULL(picture); | 3167 RETURN_ON_NULL(picture); |
3161 | 3168 |
3162 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); | 3169 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); |
3163 | 3170 |
3164 this->onDrawShadowedPicture(picture, matrix, paint); | 3171 this->onDrawShadowedPicture(picture, matrix, paint, sParams); |
3165 } | 3172 } |
3166 | 3173 |
3167 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, | 3174 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, |
3168 const SkMatrix* matrix, | 3175 const SkMatrix* matrix, |
3169 const SkPaint* paint) { | 3176 const SkPaint* paint, |
| 3177 const SkShadowParams& sParams) { |
3170 if (!paint || paint->canComputeFastBounds()) { | 3178 if (!paint || paint->canComputeFastBounds()) { |
3171 SkRect bounds = picture->cullRect(); | 3179 SkRect bounds = picture->cullRect(); |
3172 if (paint) { | 3180 if (paint) { |
3173 paint->computeFastBounds(bounds, &bounds); | 3181 paint->computeFastBounds(bounds, &bounds); |
3174 } | 3182 } |
3175 if (matrix) { | 3183 if (matrix) { |
3176 matrix->mapRect(&bounds); | 3184 matrix->mapRect(&bounds); |
3177 } | 3185 } |
3178 if (this->quickReject(bounds)) { | 3186 if (this->quickReject(bounds)) { |
3179 return; | 3187 return; |
3180 } | 3188 } |
3181 } | 3189 } |
3182 | 3190 |
3183 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); | 3191 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
3184 | 3192 |
| 3193 sk_sp<SkImage> povDepthMap; |
| 3194 sk_sp<SkImage> diffuseMap; |
| 3195 |
| 3196 // TODO: pass the depth to the shader in vertices, or uniforms |
| 3197 // so we don't have to render depth and color separately |
3185 for (int i = 0; i < fLights->numLights(); ++i) { | 3198 for (int i = 0; i < fLights->numLights(); ++i) { |
3186 // skip over ambient lights; they don't cast shadows | 3199 // skip over ambient lights; they don't cast shadows |
3187 // lights that have shadow maps do not need updating (because lights are
immutable) | 3200 // lights that have shadow maps do not need updating (because lights are
immutable) |
3188 | 3201 |
3189 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() || | 3202 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() || |
3190 fLights->light(i).getShadowMap() != nullptr) { | 3203 fLights->light(i).getShadowMap() != nullptr) { |
3191 continue; | 3204 continue; |
3192 } | 3205 } |
3193 | 3206 |
3194 // TODO: compute the correct size of the depth map from the light proper
ties | 3207 // TODO: compute the correct size of the depth map from the light proper
ties |
3195 // TODO: maybe add a kDepth_8_SkColorType | 3208 // TODO: maybe add a kDepth_8_SkColorType |
3196 // TODO: find actual max depth of picture | 3209 // TODO: find actual max depth of picture |
3197 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( | 3210 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( |
3198 fLights->light(i), 255, | 3211 fLights->light(i), 255, |
3199 picture->cullRect().width(), | 3212 picture->cullRect().width(), |
3200 picture->cullRect().height()); | 3213 picture->cullRect().height()); |
3201 | 3214 |
3202 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight
, | 3215 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight
, |
3203 kBGRA_8888_SkColorType, | 3216 kBGRA_8888_SkColorType, |
3204 kOpaque_SkAlphaType); | 3217 kOpaque_SkAlphaType); |
3205 | 3218 |
3206 // Create a new surface (that matches the backend of canvas) | 3219 // Create a new surface (that matches the backend of canvas) |
3207 // for each shadow map | 3220 // for each shadow map |
3208 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3221 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3209 | 3222 |
3210 // Wrap another SPFCanvas around the surface | 3223 // Wrap another SPFCanvas around the surface |
3211 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | 3224 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
3212 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | 3225 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 3226 depthMapCanvas->setShadowType(sParams); |
3213 | 3227 |
3214 // set the depth map canvas to have the light we're drawing. | 3228 // set the depth map canvas to have the light we're drawing. |
3215 SkLights::Builder builder; | 3229 SkLights::Builder builder; |
3216 builder.add(fLights->light(i)); | 3230 builder.add(fLights->light(i)); |
3217 sk_sp<SkLights> curLight = builder.finish(); | 3231 sk_sp<SkLights> curLight = builder.finish(); |
| 3232 depthMapCanvas->setLights(std::move(curLight)); |
3218 | 3233 |
3219 depthMapCanvas->setLights(std::move(curLight)); | |
3220 depthMapCanvas->drawPicture(picture); | 3234 depthMapCanvas->drawPicture(picture); |
| 3235 sk_sp<SkImage> depthMap = surf->makeImageSnapshot(); |
3221 | 3236 |
3222 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); | 3237 <<<<<<< 21114d931c32f34c1d25c68b25f3c2bf4c1d0f07 |
3223 } | 3238 // curLight->light(0).setShadowMap(depthMap); |
3224 | 3239 |
3225 sk_sp<SkImage> povDepthMap; | 3240 // use another shader to perform the "shifting" |
3226 sk_sp<SkImage> diffuseMap; | 3241 // |
| 3242 // sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kCl
amp_TileMode, |
| 3243 // SkShader::kCl
amp_TileMode); |
| 3244 // |
| 3245 // |
| 3246 // sk_sp<SkShader> shadowMapShader = SkShadowMapShader::Make(povDepthShad
er, |
| 3247 // std::move(curLi
ght), |
| 3248 // diffuseMap->wid
th(), |
| 3249 // diffuseMap->hei
ght()); |
| 3250 // |
| 3251 // SkPaint paintSmall = *paint; |
| 3252 // paintSmall.setShader(shadowMapShader); |
3227 | 3253 |
3228 // TODO: pass the depth to the shader in vertices, or uniforms | 3254 // draw this into small canvas |
3229 // so we don't have to render depth and color separately | 3255 // blur radius 10 |
| 3256 // SkImageInfo infoSmall = SkImageInfo::Make(shMapSize.fWidth, shMapSize.
fHeight, |
| 3257 // kBGRA_8888_SkColorType, |
| 3258 // kOpaque_SkAlphaType); |
| 3259 // sk_sp<SkSurface> surfSmall(this->makeSurface(infoSmall)); |
| 3260 // sk_sp<SkCanvas> depthMapCanvasSmall = sk_ref_sp(surfSmall->getCanvas()
); |
| 3261 // depthMapCanvasSmall->scale(shMapSize.fWidth / picture->cullRect().widt
h(), |
| 3262 // shMapSize.fHeight / picture->cullRect().hei
ght()); |
3230 | 3263 |
3231 // povDepthMap | 3264 // povDepthMap |
3232 { | 3265 { |
3233 SkLights::Builder builder; | 3266 SkLights::Builder builder; |
3234 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f,
1.0f), | 3267 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f,
1.0f), |
3235 SkVector3::Make(0.0f, 0.0f,
1.0f))); | 3268 SkVector3::Make(0.0f, 0.0f,
1.0f))); |
| 3269 |
| 3270 // depthMapCanvasSmall->drawRect(SkRect::MakeIWH(infoSmall.width(), |
| 3271 // infoSmall.height()), pai
ntSmall); |
| 3272 // |
| 3273 // sk_sp<SkImage> finalDepthMap = surfSmall->makeImageSnapshot(); |
| 3274 // |
| 3275 // |
| 3276 |
| 3277 // blur depth map |
| 3278 // SkImageInfo info2 = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHei
ght, |
| 3279 // kBGRA_8888_SkColorType, |
| 3280 // kOpaque_SkAlphaType); |
| 3281 // sk_sp<SkSurface> surf2(this->makeSurface(info2)); |
| 3282 // sk_sp<SkCanvas> depthMapCanvas2 = sk_ref_sp(surf2->getCanvas()); |
| 3283 |
| 3284 // SkPaint paint2; |
| 3285 |
| 3286 // paint2.setFilterQuality(kHigh_SkFilterQuality); |
| 3287 // depthMapCanvas2->scale(10.0f, 10.0f); |
| 3288 |
| 3289 |
| 3290 |
| 3291 // paint2.setImageFilter(SkBlurImageFilter::Make(4.0f, 4.0f, nullptr)); |
| 3292 // SkRect asdf = SkRect::MakeIWH(400,400); |
| 3293 // SkCanvas::SaveLayerRec rec(&asdf, &paint); |
| 3294 // canvas->saveLayer(rec); |
| 3295 // paint.setImageFilter(nullptr); |
| 3296 |
| 3297 |
| 3298 |
| 3299 // depthMapCanvas2->drawImage(finalDepthMap, 0, 0, &paint2); |
| 3300 |
| 3301 |
| 3302 sk_sp<SkImage> depthMap2 = surf->makeImageSnapshot(); |
| 3303 |
| 3304 SkPaint blurPaint; |
| 3305 if (sType.fBlurAlgorithm != SkShadowType::kNoBlur_BlurAlgorithm) { |
| 3306 blurPaint.setImageFilter(SkBlurImageFilter::Make(sType.fShadowRadius
, |
| 3307 sType.fShadowRadius
, nullptr)); |
| 3308 } |
| 3309 ======= |
| 3310 if (sParams.fShadowType == SkShadowParams::kNoBlur_BlurAlgorithm) { |
| 3311 fLights->light(i).setShadowMap(std::move(depthMap)); |
| 3312 } else if (sParams.fShadowType == SkShadowParams::kVariance_BlurAlgorith
m) { |
| 3313 // we blur the variance map |
| 3314 SkPaint blurPaint; |
| 3315 blurPaint.setImageFilter(SkBlurImageFilter::Make(sParams.fShadowRadi
us, |
| 3316 sParams.fShadowRadi
us, nullptr)); |
| 3317 >>>>>>> Made req changes; added some sliders |
| 3318 |
| 3319 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize
.fHeight, |
| 3320 kBGRA_8888_SkColorType, |
| 3321 kOpaque_SkAlphaType); |
| 3322 |
| 3323 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo)); |
| 3324 sk_sp<SkCanvas> blurDepthMapCanvas = sk_ref_sp(blurSurf->getCanvas()
); |
| 3325 |
| 3326 blurDepthMapCanvas->drawImage(std::move(depthMap), 0, 0, &blurPaint)
; |
| 3327 |
| 3328 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot()); |
| 3329 } |
| 3330 } |
| 3331 |
| 3332 // povDepthMap |
| 3333 { |
| 3334 SkLights::Builder builder; |
| 3335 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 3336 SkVector3::Make(0.0f, 0.0f, 1.0f))); |
3236 sk_sp<SkLights> povLight = builder.finish(); | 3337 sk_sp<SkLights> povLight = builder.finish(); |
3237 | 3338 |
3238 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), | 3339 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
3239 picture->cullRect().height(), | 3340 picture->cullRect().height(), |
3240 kBGRA_8888_SkColorType, | 3341 kBGRA_8888_SkColorType, |
3241 kOpaque_SkAlphaType); | 3342 kOpaque_SkAlphaType); |
3242 | 3343 |
3243 // Create a new surface (that matches the backend of canvas) | 3344 // Create a new surface (that matches the backend of canvas) |
3244 // to create the povDepthMap | 3345 // to create the povDepthMap |
3245 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3346 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3246 | 3347 |
3247 // Wrap another SPFCanvas around the surface | 3348 // Wrap another SPFCanvas around the surface |
3248 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | 3349 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
3249 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | 3350 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
3250 | 3351 |
3251 // set the depth map canvas to have the light as the user's POV | 3352 // set the depth map canvas to have the light as the user's POV |
3252 depthMapCanvas->setLights(std::move(povLight)); | 3353 depthMapCanvas->setLights(std::move(povLight)); |
3253 | 3354 |
3254 depthMapCanvas->drawPicture(picture); | 3355 depthMapCanvas->drawPicture(picture); |
3255 | |
3256 povDepthMap = surf->makeImageSnapshot(); | 3356 povDepthMap = surf->makeImageSnapshot(); |
3257 } | 3357 } |
3258 | 3358 |
3259 // diffuseMap | 3359 // diffuseMap |
3260 { | 3360 { |
3261 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), | 3361 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
3262 picture->cullRect().height(), | 3362 picture->cullRect().height(), |
3263 kBGRA_8888_SkColorType, | 3363 kBGRA_8888_SkColorType, |
3264 kOpaque_SkAlphaType); | 3364 kOpaque_SkAlphaType); |
3265 | 3365 |
3266 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3366 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3267 surf->getCanvas()->drawPicture(picture); | 3367 surf->getCanvas()->drawPicture(picture); |
3268 | 3368 |
3269 diffuseMap = surf->makeImageSnapshot(); | 3369 diffuseMap = surf->makeImageSnapshot(); |
3270 } | 3370 } |
3271 | |
3272 SkPaint shadowPaint; | 3371 SkPaint shadowPaint; |
3273 | 3372 |
3274 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti
leMode, | 3373 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti
leMode, |
3275 SkShader::kClamp_Ti
leMode); | 3374 SkShader::kClamp_Ti
leMode); |
3276 | |
3277 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile
Mode, | 3375 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile
Mode, |
3278 SkShader::kClamp_Tile
Mode); | 3376 SkShader::kClamp_Tile
Mode); |
3279 | |
3280 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader
), | 3377 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader
), |
3281 std::move(diffuseShader)
, | 3378 std::move(diffuseShader)
, |
3282 std::move(fLights), | 3379 std::move(fLights), |
3283 diffuseMap->width(), | 3380 diffuseMap->width(), |
3284 diffuseMap->height()); | 3381 diffuseMap->height(), |
| 3382 sParams); |
3285 | 3383 |
3286 shadowPaint.setShader(shadowShader); | 3384 shadowPaint.setShader(shadowShader); |
3287 | 3385 |
3288 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s
hadowPaint); | 3386 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s
hadowPaint); |
3289 } | 3387 } |
3290 #endif | 3388 #endif |
3291 | 3389 |
3292 /////////////////////////////////////////////////////////////////////////////// | 3390 /////////////////////////////////////////////////////////////////////////////// |
3293 /////////////////////////////////////////////////////////////////////////////// | 3391 /////////////////////////////////////////////////////////////////////////////// |
3294 | 3392 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3392 | 3490 |
3393 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { | 3491 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
3394 fCanvas->restoreToCount(fSaveCount); | 3492 fCanvas->restoreToCount(fSaveCount); |
3395 } | 3493 } |
3396 | 3494 |
3397 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API | 3495 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API |
3398 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p
rops) { | 3496 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p
rops) { |
3399 return this->makeSurface(info, props).release(); | 3497 return this->makeSurface(info, props).release(); |
3400 } | 3498 } |
3401 #endif | 3499 #endif |
OLD | NEW |