Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: src/core/SkCanvas.cpp

Issue 2224163005: Made shadows blurry (thru implementing variance mapping) (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: rebased off master again! Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 19 matching lines...) Expand all
30 #include "SkReadPixelsRec.h" 30 #include "SkReadPixelsRec.h"
31 #include "SkRRect.h" 31 #include "SkRRect.h"
32 #include "SkShadowPaintFilterCanvas.h" 32 #include "SkShadowPaintFilterCanvas.h"
33 #include "SkShadowShader.h" 33 #include "SkShadowShader.h"
34 #include "SkSmallAllocator.h" 34 #include "SkSmallAllocator.h"
35 #include "SkSpecialImage.h" 35 #include "SkSpecialImage.h"
36 #include "SkSurface_Base.h" 36 #include "SkSurface_Base.h"
37 #include "SkTextBlob.h" 37 #include "SkTextBlob.h"
38 #include "SkTextFormatParams.h" 38 #include "SkTextFormatParams.h"
39 #include "SkTLazy.h" 39 #include "SkTLazy.h"
40 #include "SkTraceEvent.h" 40 #include "SkTraceEvent.h"
robertphillips 2016/08/23 17:19:40 Can we fix this now ?
vjiaoblack 2016/08/23 18:01:21 Done.
41 41 #include "../../include/effects/SkBlurImageFilter.h"
42 #include <new> 42 #include <new>
43 43
44 #if SK_SUPPORT_GPU 44 #if SK_SUPPORT_GPU
45 #include "GrContext.h" 45 #include "GrContext.h"
46 #include "GrRenderTarget.h" 46 #include "GrRenderTarget.h"
47 #include "SkGrPriv.h" 47 #include "SkGrPriv.h"
48
48 #endif 49 #endif
49 50
50 #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) 51 #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0)
51 52
52 //#define SK_SUPPORT_PRECHECK_CLIPRECT 53 //#define SK_SUPPORT_PRECHECK_CLIPRECT
53 54
54 /* 55 /*
55 * Return true if the drawing this rect would hit every pixels in the canvas. 56 * Return true if the drawing this rect would hit every pixels in the canvas.
56 * 57 *
57 * Returns false if 58 * Returns false if
(...skipping 3098 matching lines...) Expand 10 before | Expand all | Expand 10 after
3156 } 3157 }
3157 } 3158 }
3158 3159
3159 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); 3160 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
3160 picture->playback(this); 3161 picture->playback(this);
3161 } 3162 }
3162 3163
3163 #ifdef SK_EXPERIMENTAL_SHADOWING 3164 #ifdef SK_EXPERIMENTAL_SHADOWING
3164 void SkCanvas::drawShadowedPicture(const SkPicture* picture, 3165 void SkCanvas::drawShadowedPicture(const SkPicture* picture,
3165 const SkMatrix* matrix, 3166 const SkMatrix* matrix,
3166 const SkPaint* paint) { 3167 const SkPaint* paint,
3168 const SkShadowParams& params) {
3167 RETURN_ON_NULL(picture); 3169 RETURN_ON_NULL(picture);
3168 3170
3169 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); 3171 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()");
3170 3172
3171 this->onDrawShadowedPicture(picture, matrix, paint); 3173 this->onDrawShadowedPicture(picture, matrix, paint, params);
3172 } 3174 }
3173 3175
3174 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, 3176 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture,
3175 const SkMatrix* matrix, 3177 const SkMatrix* matrix,
3176 const SkPaint* paint) { 3178 const SkPaint* paint,
3179 const SkShadowParams& params) {
3177 if (!paint || paint->canComputeFastBounds()) { 3180 if (!paint || paint->canComputeFastBounds()) {
3178 SkRect bounds = picture->cullRect(); 3181 SkRect bounds = picture->cullRect();
3179 if (paint) { 3182 if (paint) {
3180 paint->computeFastBounds(bounds, &bounds); 3183 paint->computeFastBounds(bounds, &bounds);
3181 } 3184 }
3182 if (matrix) { 3185 if (matrix) {
3183 matrix->mapRect(&bounds); 3186 matrix->mapRect(&bounds);
3184 } 3187 }
3185 if (this->quickReject(bounds)) { 3188 if (this->quickReject(bounds)) {
3186 return; 3189 return;
3187 } 3190 }
3188 } 3191 }
3189 3192
3190 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); 3193 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
3191 3194
3195 sk_sp<SkImage> povDepthMap;
3196 sk_sp<SkImage> diffuseMap;
3197
3198 // TODO: pass the depth to the shader in vertices, or uniforms
3199 // so we don't have to render depth and color separately
3192 for (int i = 0; i < fLights->numLights(); ++i) { 3200 for (int i = 0; i < fLights->numLights(); ++i) {
3193 // skip over ambient lights; they don't cast shadows 3201 // skip over ambient lights; they don't cast shadows
3194 // lights that have shadow maps do not need updating (because lights are immutable) 3202 // lights that have shadow maps do not need updating (because lights are immutable)
3195 3203
3196 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() || 3204 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() ||
3197 fLights->light(i).getShadowMap() != nullptr) { 3205 fLights->light(i).getShadowMap() != nullptr) {
3198 continue; 3206 continue;
3199 } 3207 }
3200 3208
3201 // TODO: compute the correct size of the depth map from the light proper ties 3209 // TODO: compute the correct size of the depth map from the light proper ties
3202 // TODO: maybe add a kDepth_8_SkColorType 3210 // TODO: maybe add a kDepth_8_SkColorType
3203 // TODO: find actual max depth of picture 3211 // TODO: find actual max depth of picture
3204 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( 3212 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize(
3205 fLights->light(i), 255, 3213 fLights->light(i), 255,
3206 picture->cullRect().width(), 3214 picture->cullRect().width(),
3207 picture->cullRect().height()); 3215 picture->cullRect().height());
3208 3216
3209 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight , 3217 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight ,
3210 kBGRA_8888_SkColorType, 3218 kBGRA_8888_SkColorType,
3211 kOpaque_SkAlphaType); 3219 kOpaque_SkAlphaType);
3212 3220
3213 // Create a new surface (that matches the backend of canvas) 3221 // Create a new surface (that matches the backend of canvas)
3214 // for each shadow map 3222 // for each shadow map
3215 sk_sp<SkSurface> surf(this->makeSurface(info)); 3223 sk_sp<SkSurface> surf(this->makeSurface(info));
3216 3224
3217 // Wrap another SPFCanvas around the surface 3225 // Wrap another SPFCanvas around the surface
3218 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = 3226 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3219 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); 3227 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3228 depthMapCanvas->setShadowParams(params);
3220 3229
3221 // set the depth map canvas to have the light we're drawing. 3230 // set the depth map canvas to have the light we're drawing.
3222 SkLights::Builder builder; 3231 SkLights::Builder builder;
3223 builder.add(fLights->light(i)); 3232 builder.add(fLights->light(i));
3224 sk_sp<SkLights> curLight = builder.finish(); 3233 sk_sp<SkLights> curLight = builder.finish();
3234 depthMapCanvas->setLights(std::move(curLight));
3225 3235
3226 depthMapCanvas->setLights(std::move(curLight));
3227 depthMapCanvas->drawPicture(picture); 3236 depthMapCanvas->drawPicture(picture);
3237 sk_sp<SkImage> depthMap = surf->makeImageSnapshot();
3228 3238
3229 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); 3239 if (params.fType == SkShadowParams::kNoBlur_BlurAlgorithm) {
3240 fLights->light(i).setShadowMap(std::move(depthMap));
3241 } else if (params.fType == SkShadowParams::kVariance_BlurAlgorithm) {
3242 // we blur the variance map
3243 SkPaint blurPaint;
3244 blurPaint.setImageFilter(SkBlurImageFilter::Make(params.fShadowRadiu s,
3245 params.fShadowRadiu s, nullptr));
3246
robertphillips 2016/08/23 17:19:40 rm commented out code
vjiaoblack 2016/08/23 18:01:21 Done.
3247 // depthMapCanvas2->drawImage(finalDepthMap, 0, 0, &paint2);
3248
3249
3250 sk_sp<SkImage> depthMap2 = surf->makeImageSnapshot();
3251
3252
3253 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize .fHeight,
3254 kBGRA_8888_SkColorType,
3255 kOpaque_SkAlphaType);
3256
3257 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo));
3258
3259 blurSurf->getCanvas()->drawImage(std::move(depthMap), 0, 0, &blurPai nt);
3260
3261 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot());
3262 }
3230 } 3263 }
3231 3264
3232 sk_sp<SkImage> povDepthMap;
3233 sk_sp<SkImage> diffuseMap;
3234
3235 // TODO: pass the depth to the shader in vertices, or uniforms
3236 // so we don't have to render depth and color separately
3237
3238 // povDepthMap 3265 // povDepthMap
3239 { 3266 {
3240 SkLights::Builder builder; 3267 SkLights::Builder builder;
3241 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f), 3268 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f),
3242 SkVector3::Make(0.0f, 0.0f, 1.0f))); 3269 SkVector3::Make(0.0f, 0.0f, 1.0f)));
3243 sk_sp<SkLights> povLight = builder.finish(); 3270 sk_sp<SkLights> povLight = builder.finish();
3244 3271
3245 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), 3272 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3246 picture->cullRect().height(), 3273 picture->cullRect().height(),
3247 kBGRA_8888_SkColorType, 3274 kBGRA_8888_SkColorType,
3248 kOpaque_SkAlphaType); 3275 kOpaque_SkAlphaType);
3249 3276
3250 // Create a new surface (that matches the backend of canvas) 3277 // Create a new surface (that matches the backend of canvas)
3251 // to create the povDepthMap 3278 // to create the povDepthMap
3252 sk_sp<SkSurface> surf(this->makeSurface(info)); 3279 sk_sp<SkSurface> surf(this->makeSurface(info));
3253 3280
3254 // Wrap another SPFCanvas around the surface 3281 // Wrap another SPFCanvas around the surface
3255 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = 3282 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3256 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); 3283 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3257 3284
3258 // set the depth map canvas to have the light as the user's POV 3285 // set the depth map canvas to have the light as the user's POV
3259 depthMapCanvas->setLights(std::move(povLight)); 3286 depthMapCanvas->setLights(std::move(povLight));
3260 3287
3261 depthMapCanvas->drawPicture(picture); 3288 depthMapCanvas->drawPicture(picture);
3262
3263 povDepthMap = surf->makeImageSnapshot(); 3289 povDepthMap = surf->makeImageSnapshot();
3264 } 3290 }
3265 3291
3266 // diffuseMap 3292 // diffuseMap
3267 { 3293 {
3268 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), 3294 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3269 picture->cullRect().height(), 3295 picture->cullRect().height(),
3270 kBGRA_8888_SkColorType, 3296 kBGRA_8888_SkColorType,
3271 kOpaque_SkAlphaType); 3297 kOpaque_SkAlphaType);
3272 3298
3273 sk_sp<SkSurface> surf(this->makeSurface(info)); 3299 sk_sp<SkSurface> surf(this->makeSurface(info));
3274 surf->getCanvas()->drawPicture(picture); 3300 surf->getCanvas()->drawPicture(picture);
3275 3301
3276 diffuseMap = surf->makeImageSnapshot(); 3302 diffuseMap = surf->makeImageSnapshot();
3277 } 3303 }
3278
3279 SkPaint shadowPaint; 3304 SkPaint shadowPaint;
3280 3305
3281 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode, 3306 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode,
3282 SkShader::kClamp_Ti leMode); 3307 SkShader::kClamp_Ti leMode);
3283
3284 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode, 3308 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode,
3285 SkShader::kClamp_Tile Mode); 3309 SkShader::kClamp_Tile Mode);
3286
3287 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ), 3310 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ),
3288 std::move(diffuseShader) , 3311 std::move(diffuseShader) ,
3289 std::move(fLights), 3312 std::move(fLights),
3290 diffuseMap->width(), 3313 diffuseMap->width(),
3291 diffuseMap->height()); 3314 diffuseMap->height(),
3315 params);
3292 3316
3293 shadowPaint.setShader(shadowShader); 3317 shadowPaint.setShader(shadowShader);
3294 3318
3295 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint); 3319 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint);
3296 } 3320 }
3297 #endif 3321 #endif
3298 3322
3299 /////////////////////////////////////////////////////////////////////////////// 3323 ///////////////////////////////////////////////////////////////////////////////
3300 /////////////////////////////////////////////////////////////////////////////// 3324 ///////////////////////////////////////////////////////////////////////////////
3301 3325
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
3399 3423
3400 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { 3424 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
3401 fCanvas->restoreToCount(fSaveCount); 3425 fCanvas->restoreToCount(fSaveCount);
3402 } 3426 }
3403 3427
3404 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API 3428 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
3405 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) { 3429 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) {
3406 return this->makeSurface(info, props).release(); 3430 return this->makeSurface(info, props).release();
3407 } 3431 }
3408 #endif 3432 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698