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

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: added shadowParams info to debug string Created 4 years, 4 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 "SkRRect.h" 30 #include "SkRRect.h"
31 #include "SkShadowPaintFilterCanvas.h" 31 #include "SkShadowPaintFilterCanvas.h"
32 #include "SkShadowShader.h" 32 #include "SkShadowShader.h"
33 #include "SkSmallAllocator.h" 33 #include "SkSmallAllocator.h"
34 #include "SkSpecialImage.h" 34 #include "SkSpecialImage.h"
35 #include "SkSurface_Base.h" 35 #include "SkSurface_Base.h"
36 #include "SkTextBlob.h" 36 #include "SkTextBlob.h"
37 #include "SkTextFormatParams.h" 37 #include "SkTextFormatParams.h"
38 #include "SkTLazy.h" 38 #include "SkTLazy.h"
39 #include "SkTraceEvent.h" 39 #include "SkTraceEvent.h"
40 40 #include "../../include/effects/SkBlurImageFilter.h"
41 #include <new> 41 #include <new>
42 42
43 #if SK_SUPPORT_GPU 43 #if SK_SUPPORT_GPU
44 #include "GrContext.h" 44 #include "GrContext.h"
45 #include "GrRenderTarget.h" 45 #include "GrRenderTarget.h"
46 #include "SkGrPriv.h" 46 #include "SkGrPriv.h"
47
47 #endif 48 #endif
48 49
49 #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) 50 #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0)
50 51
51 //#define SK_SUPPORT_PRECHECK_CLIPRECT 52 //#define SK_SUPPORT_PRECHECK_CLIPRECT
52 53
53 /* 54 /*
54 * Return true if the drawing this rect would hit every pixels in the canvas. 55 * Return true if the drawing this rect would hit every pixels in the canvas.
55 * 56 *
56 * Returns false if 57 * Returns false if
(...skipping 2996 matching lines...) Expand 10 before | Expand all | Expand 10 after
3053 } 3054 }
3054 } 3055 }
3055 3056
3056 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); 3057 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
3057 picture->playback(this); 3058 picture->playback(this);
3058 } 3059 }
3059 3060
3060 #ifdef SK_EXPERIMENTAL_SHADOWING 3061 #ifdef SK_EXPERIMENTAL_SHADOWING
3061 void SkCanvas::drawShadowedPicture(const SkPicture* picture, 3062 void SkCanvas::drawShadowedPicture(const SkPicture* picture,
3062 const SkMatrix* matrix, 3063 const SkMatrix* matrix,
3063 const SkPaint* paint) { 3064 const SkPaint* paint,
3065 const SkShadowParams& sParams) {
3064 RETURN_ON_NULL(picture); 3066 RETURN_ON_NULL(picture);
3065 3067
3066 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); 3068 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()");
3067 3069
3068 this->onDrawShadowedPicture(picture, matrix, paint); 3070 this->onDrawShadowedPicture(picture, matrix, paint, sParams);
3069 } 3071 }
3070 3072
3071 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, 3073 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture,
3072 const SkMatrix* matrix, 3074 const SkMatrix* matrix,
3073 const SkPaint* paint) { 3075 const SkPaint* paint,
3076 const SkShadowParams& sParams) {
3074 if (!paint || paint->canComputeFastBounds()) { 3077 if (!paint || paint->canComputeFastBounds()) {
3075 SkRect bounds = picture->cullRect(); 3078 SkRect bounds = picture->cullRect();
3076 if (paint) { 3079 if (paint) {
3077 paint->computeFastBounds(bounds, &bounds); 3080 paint->computeFastBounds(bounds, &bounds);
3078 } 3081 }
3079 if (matrix) { 3082 if (matrix) {
3080 matrix->mapRect(&bounds); 3083 matrix->mapRect(&bounds);
3081 } 3084 }
3082 if (this->quickReject(bounds)) { 3085 if (this->quickReject(bounds)) {
3083 return; 3086 return;
3084 } 3087 }
3085 } 3088 }
3086 3089
3087 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); 3090 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
3088 3091
3092 sk_sp<SkImage> povDepthMap;
3093 sk_sp<SkImage> diffuseMap;
3094
3095 // TODO: pass the depth to the shader in vertices, or uniforms
3096 // so we don't have to render depth and color separately
3089 for (int i = 0; i < fLights->numLights(); ++i) { 3097 for (int i = 0; i < fLights->numLights(); ++i) {
3090 // skip over ambient lights; they don't cast shadows 3098 // skip over ambient lights; they don't cast shadows
3091 // lights that have shadow maps do not need updating (because lights are immutable) 3099 // lights that have shadow maps do not need updating (because lights are immutable)
3092 3100
3093 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() || 3101 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() ||
3094 fLights->light(i).getShadowMap() != nullptr) { 3102 fLights->light(i).getShadowMap() != nullptr) {
3095 continue; 3103 continue;
3096 } 3104 }
3097 3105
3098 // TODO: compute the correct size of the depth map from the light proper ties 3106 // TODO: compute the correct size of the depth map from the light proper ties
3099 // TODO: maybe add a kDepth_8_SkColorType 3107 // TODO: maybe add a kDepth_8_SkColorType
3100 // TODO: find actual max depth of picture 3108 // TODO: find actual max depth of picture
3101 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( 3109 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize(
3102 fLights->light(i), 255, 3110 fLights->light(i), 255,
3103 picture->cullRect().width(), 3111 picture->cullRect().width(),
3104 picture->cullRect().height()); 3112 picture->cullRect().height());
3105 3113
3106 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight , 3114 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight ,
3107 kBGRA_8888_SkColorType, 3115 kBGRA_8888_SkColorType,
3108 kOpaque_SkAlphaType); 3116 kOpaque_SkAlphaType);
3109 3117
3110 // Create a new surface (that matches the backend of canvas) 3118 // Create a new surface (that matches the backend of canvas)
3111 // for each shadow map 3119 // for each shadow map
3112 sk_sp<SkSurface> surf(this->makeSurface(info)); 3120 sk_sp<SkSurface> surf(this->makeSurface(info));
3113 3121
3114 // Wrap another SPFCanvas around the surface 3122 // Wrap another SPFCanvas around the surface
3115 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = 3123 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3116 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); 3124 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
robertphillips 2016/08/16 15:58:16 setShadowParams ?
3125 depthMapCanvas->setShadowType(sParams);
3117 3126
3118 // set the depth map canvas to have the light we're drawing. 3127 // set the depth map canvas to have the light we're drawing.
3119 SkLights::Builder builder; 3128 SkLights::Builder builder;
3120 builder.add(fLights->light(i)); 3129 builder.add(fLights->light(i));
3121 sk_sp<SkLights> curLight = builder.finish(); 3130 sk_sp<SkLights> curLight = builder.finish();
3131 depthMapCanvas->setLights(std::move(curLight));
3122 3132
3123 depthMapCanvas->setLights(std::move(curLight));
3124 depthMapCanvas->drawPicture(picture); 3133 depthMapCanvas->drawPicture(picture);
3134 sk_sp<SkImage> depthMap = surf->makeImageSnapshot();
3125 3135
3126 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); 3136 if (sParams.fShadowType == SkShadowParams::kNoBlur_BlurAlgorithm) {
3137 fLights->light(i).setShadowMap(std::move(depthMap));
3138 } else if (sParams.fShadowType == SkShadowParams::kVariance_BlurAlgorith m) {
3139 // we blur the variance map
3140 SkPaint blurPaint;
3141 blurPaint.setImageFilter(SkBlurImageFilter::Make(sParams.fShadowRadi us,
3142 sParams.fShadowRadi us, nullptr));
3143
3144 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize .fHeight,
3145 kBGRA_8888_SkColorType,
3146 kOpaque_SkAlphaType);
3147
3148 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo));
robertphillips 2016/08/16 15:58:16 I don't think you want/need to ref count the surfa
3149 sk_sp<SkCanvas> blurDepthMapCanvas = sk_ref_sp(blurSurf->getCanvas() );
3150
3151 blurDepthMapCanvas->drawImage(std::move(depthMap), 0, 0, &blurPaint) ;
3152
3153 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot());
3154 }
3127 } 3155 }
3128 3156
3129 sk_sp<SkImage> povDepthMap;
3130 sk_sp<SkImage> diffuseMap;
3131
3132 // TODO: pass the depth to the shader in vertices, or uniforms
3133 // so we don't have to render depth and color separately
3134
3135 // povDepthMap 3157 // povDepthMap
3136 { 3158 {
3137 SkLights::Builder builder; 3159 SkLights::Builder builder;
3138 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), 3160 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
3139 SkVector3::Make(0.0f, 0.0f, 1.0f))); 3161 SkVector3::Make(0.0f, 0.0f, 1.0f)));
3140 sk_sp<SkLights> povLight = builder.finish(); 3162 sk_sp<SkLights> povLight = builder.finish();
3141 3163
3142 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), 3164 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3143 picture->cullRect().height(), 3165 picture->cullRect().height(),
3144 kBGRA_8888_SkColorType, 3166 kBGRA_8888_SkColorType,
3145 kOpaque_SkAlphaType); 3167 kOpaque_SkAlphaType);
3146 3168
3147 // Create a new surface (that matches the backend of canvas) 3169 // Create a new surface (that matches the backend of canvas)
3148 // to create the povDepthMap 3170 // to create the povDepthMap
3149 sk_sp<SkSurface> surf(this->makeSurface(info)); 3171 sk_sp<SkSurface> surf(this->makeSurface(info));
3150 3172
3151 // Wrap another SPFCanvas around the surface 3173 // Wrap another SPFCanvas around the surface
3152 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = 3174 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3153 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); 3175 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3154 3176
3155 // set the depth map canvas to have the light as the user's POV 3177 // set the depth map canvas to have the light as the user's POV
3156 depthMapCanvas->setLights(std::move(povLight)); 3178 depthMapCanvas->setLights(std::move(povLight));
3157 3179
3158 depthMapCanvas->drawPicture(picture); 3180 depthMapCanvas->drawPicture(picture);
3159
3160 povDepthMap = surf->makeImageSnapshot(); 3181 povDepthMap = surf->makeImageSnapshot();
3161 } 3182 }
3162 3183
3163 // diffuseMap 3184 // diffuseMap
3164 { 3185 {
3165 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), 3186 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3166 picture->cullRect().height(), 3187 picture->cullRect().height(),
3167 kBGRA_8888_SkColorType, 3188 kBGRA_8888_SkColorType,
3168 kOpaque_SkAlphaType); 3189 kOpaque_SkAlphaType);
3169 3190
3170 sk_sp<SkSurface> surf(this->makeSurface(info)); 3191 sk_sp<SkSurface> surf(this->makeSurface(info));
3171 surf->getCanvas()->drawPicture(picture); 3192 surf->getCanvas()->drawPicture(picture);
3172 3193
3173 diffuseMap = surf->makeImageSnapshot(); 3194 diffuseMap = surf->makeImageSnapshot();
3174 } 3195 }
3175
3176 SkPaint shadowPaint; 3196 SkPaint shadowPaint;
3177 3197
3178 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode, 3198 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode,
3179 SkShader::kClamp_Ti leMode); 3199 SkShader::kClamp_Ti leMode);
3180
3181 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode, 3200 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode,
3182 SkShader::kClamp_Tile Mode); 3201 SkShader::kClamp_Tile Mode);
3183
3184 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ), 3202 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ),
3185 std::move(diffuseShader) , 3203 std::move(diffuseShader) ,
3186 std::move(fLights), 3204 std::move(fLights),
3187 diffuseMap->width(), 3205 diffuseMap->width(),
3188 diffuseMap->height()); 3206 diffuseMap->height(),
3207 sParams);
3189 3208
3190 shadowPaint.setShader(shadowShader); 3209 shadowPaint.setShader(shadowShader);
3191 3210
3192 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint); 3211 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint);
3193 } 3212 }
3194 #endif 3213 #endif
3195 3214
3196 /////////////////////////////////////////////////////////////////////////////// 3215 ///////////////////////////////////////////////////////////////////////////////
3197 /////////////////////////////////////////////////////////////////////////////// 3216 ///////////////////////////////////////////////////////////////////////////////
3198 3217
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
3296 3315
3297 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { 3316 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
3298 fCanvas->restoreToCount(fSaveCount); 3317 fCanvas->restoreToCount(fSaveCount);
3299 } 3318 }
3300 3319
3301 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API 3320 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
3302 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) { 3321 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) {
3303 return this->makeSurface(info, props).release(); 3322 return this->makeSurface(info, props).release();
3304 } 3323 }
3305 #endif 3324 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698