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" |
(...skipping 20 matching lines...) Expand all Loading... |
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" |
41 | |
42 #include <new> | 41 #include <new> |
43 | 42 |
44 #if SK_SUPPORT_GPU | 43 #if SK_SUPPORT_GPU |
45 #include "GrContext.h" | 44 #include "GrContext.h" |
46 #include "GrRenderTarget.h" | 45 #include "GrRenderTarget.h" |
47 #include "SkGrPriv.h" | 46 #include "SkGrPriv.h" |
| 47 |
48 #endif | 48 #endif |
49 | 49 |
50 #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) |
51 | 51 |
52 //#define SK_SUPPORT_PRECHECK_CLIPRECT | 52 //#define SK_SUPPORT_PRECHECK_CLIPRECT |
53 | 53 |
54 /* | 54 /* |
55 * 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. |
56 * | 56 * |
57 * Returns false if | 57 * Returns false if |
(...skipping 3098 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3156 } | 3156 } |
3157 } | 3157 } |
3158 | 3158 |
3159 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); | 3159 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
3160 picture->playback(this); | 3160 picture->playback(this); |
3161 } | 3161 } |
3162 | 3162 |
3163 #ifdef SK_EXPERIMENTAL_SHADOWING | 3163 #ifdef SK_EXPERIMENTAL_SHADOWING |
3164 void SkCanvas::drawShadowedPicture(const SkPicture* picture, | 3164 void SkCanvas::drawShadowedPicture(const SkPicture* picture, |
3165 const SkMatrix* matrix, | 3165 const SkMatrix* matrix, |
3166 const SkPaint* paint) { | 3166 const SkPaint* paint, |
| 3167 const SkShadowParams& params) { |
3167 RETURN_ON_NULL(picture); | 3168 RETURN_ON_NULL(picture); |
3168 | 3169 |
3169 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); | 3170 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); |
3170 | 3171 |
3171 this->onDrawShadowedPicture(picture, matrix, paint); | 3172 this->onDrawShadowedPicture(picture, matrix, paint, params); |
3172 } | 3173 } |
3173 | 3174 |
3174 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, | 3175 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, |
3175 const SkMatrix* matrix, | 3176 const SkMatrix* matrix, |
3176 const SkPaint* paint) { | 3177 const SkPaint* paint, |
| 3178 const SkShadowParams& params) { |
3177 if (!paint || paint->canComputeFastBounds()) { | 3179 if (!paint || paint->canComputeFastBounds()) { |
3178 SkRect bounds = picture->cullRect(); | 3180 SkRect bounds = picture->cullRect(); |
3179 if (paint) { | 3181 if (paint) { |
3180 paint->computeFastBounds(bounds, &bounds); | 3182 paint->computeFastBounds(bounds, &bounds); |
3181 } | 3183 } |
3182 if (matrix) { | 3184 if (matrix) { |
3183 matrix->mapRect(&bounds); | 3185 matrix->mapRect(&bounds); |
3184 } | 3186 } |
3185 if (this->quickReject(bounds)) { | 3187 if (this->quickReject(bounds)) { |
3186 return; | 3188 return; |
3187 } | 3189 } |
3188 } | 3190 } |
3189 | 3191 |
3190 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); | 3192 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
3191 | 3193 |
| 3194 sk_sp<SkImage> povDepthMap; |
| 3195 sk_sp<SkImage> diffuseMap; |
| 3196 |
| 3197 // TODO: pass the depth to the shader in vertices, or uniforms |
| 3198 // so we don't have to render depth and color separately |
3192 for (int i = 0; i < fLights->numLights(); ++i) { | 3199 for (int i = 0; i < fLights->numLights(); ++i) { |
3193 // skip over ambient lights; they don't cast shadows | 3200 // skip over ambient lights; they don't cast shadows |
3194 // lights that have shadow maps do not need updating (because lights are
immutable) | 3201 // lights that have shadow maps do not need updating (because lights are
immutable) |
3195 | 3202 |
3196 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() || | 3203 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() || |
3197 fLights->light(i).getShadowMap() != nullptr) { | 3204 fLights->light(i).getShadowMap() != nullptr) { |
3198 continue; | 3205 continue; |
3199 } | 3206 } |
3200 | 3207 |
3201 // TODO: compute the correct size of the depth map from the light proper
ties | 3208 // TODO: compute the correct size of the depth map from the light proper
ties |
3202 // TODO: maybe add a kDepth_8_SkColorType | 3209 // TODO: maybe add a kDepth_8_SkColorType |
3203 // TODO: find actual max depth of picture | 3210 // TODO: find actual max depth of picture |
3204 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( | 3211 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( |
3205 fLights->light(i), 255, | 3212 fLights->light(i), 255, |
3206 picture->cullRect().width(), | 3213 picture->cullRect().width(), |
3207 picture->cullRect().height()); | 3214 picture->cullRect().height()); |
3208 | 3215 |
3209 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight
, | 3216 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight
, |
3210 kBGRA_8888_SkColorType, | 3217 kBGRA_8888_SkColorType, |
3211 kOpaque_SkAlphaType); | 3218 kOpaque_SkAlphaType); |
3212 | 3219 |
3213 // Create a new surface (that matches the backend of canvas) | 3220 // Create a new surface (that matches the backend of canvas) |
3214 // for each shadow map | 3221 // for each shadow map |
3215 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3222 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3216 | 3223 |
3217 // Wrap another SPFCanvas around the surface | 3224 // Wrap another SPFCanvas around the surface |
3218 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | 3225 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
3219 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | 3226 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 3227 depthMapCanvas->setShadowParams(params); |
3220 | 3228 |
3221 // set the depth map canvas to have the light we're drawing. | 3229 // set the depth map canvas to have the light we're drawing. |
3222 SkLights::Builder builder; | 3230 SkLights::Builder builder; |
3223 builder.add(fLights->light(i)); | 3231 builder.add(fLights->light(i)); |
3224 sk_sp<SkLights> curLight = builder.finish(); | 3232 sk_sp<SkLights> curLight = builder.finish(); |
| 3233 depthMapCanvas->setLights(std::move(curLight)); |
3225 | 3234 |
3226 depthMapCanvas->setLights(std::move(curLight)); | |
3227 depthMapCanvas->drawPicture(picture); | 3235 depthMapCanvas->drawPicture(picture); |
| 3236 sk_sp<SkImage> depthMap = surf->makeImageSnapshot(); |
3228 | 3237 |
3229 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); | 3238 if (params.fType == SkShadowParams::kNoBlur_ShadowType) { |
| 3239 fLights->light(i).setShadowMap(std::move(depthMap)); |
| 3240 } else if (params.fType == SkShadowParams::kVariance_ShadowType) { |
| 3241 // we blur the variance map |
| 3242 SkPaint blurPaint; |
| 3243 blurPaint.setImageFilter(SkImageFilter::MakeBlur(params.fShadowRadiu
s, |
| 3244 params.fShadowRadiu
s, nullptr)); |
| 3245 |
| 3246 SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize
.fHeight, |
| 3247 kBGRA_8888_SkColorType, |
| 3248 kOpaque_SkAlphaType); |
| 3249 |
| 3250 sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo)); |
| 3251 |
| 3252 blurSurf->getCanvas()->drawImage(std::move(depthMap), 0, 0, &blurPai
nt); |
| 3253 |
| 3254 fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot()); |
| 3255 } |
3230 } | 3256 } |
3231 | 3257 |
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 | 3258 // povDepthMap |
3239 { | 3259 { |
3240 SkLights::Builder builder; | 3260 SkLights::Builder builder; |
3241 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f,
1.0f), | 3261 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f,
1.0f), |
3242 SkVector3::Make(0.0f, 0.0f,
1.0f))); | 3262 SkVector3::Make(0.0f, 0.0f,
1.0f))); |
3243 sk_sp<SkLights> povLight = builder.finish(); | 3263 sk_sp<SkLights> povLight = builder.finish(); |
3244 | 3264 |
3245 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), | 3265 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
3246 picture->cullRect().height(), | 3266 picture->cullRect().height(), |
3247 kBGRA_8888_SkColorType, | 3267 kBGRA_8888_SkColorType, |
3248 kOpaque_SkAlphaType); | 3268 kOpaque_SkAlphaType); |
3249 | 3269 |
3250 // Create a new surface (that matches the backend of canvas) | 3270 // Create a new surface (that matches the backend of canvas) |
3251 // to create the povDepthMap | 3271 // to create the povDepthMap |
3252 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3272 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3253 | 3273 |
3254 // Wrap another SPFCanvas around the surface | 3274 // Wrap another SPFCanvas around the surface |
3255 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | 3275 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
3256 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | 3276 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
3257 | 3277 |
3258 // set the depth map canvas to have the light as the user's POV | 3278 // set the depth map canvas to have the light as the user's POV |
3259 depthMapCanvas->setLights(std::move(povLight)); | 3279 depthMapCanvas->setLights(std::move(povLight)); |
3260 | 3280 |
3261 depthMapCanvas->drawPicture(picture); | 3281 depthMapCanvas->drawPicture(picture); |
3262 | |
3263 povDepthMap = surf->makeImageSnapshot(); | 3282 povDepthMap = surf->makeImageSnapshot(); |
3264 } | 3283 } |
3265 | 3284 |
3266 // diffuseMap | 3285 // diffuseMap |
3267 { | 3286 { |
3268 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), | 3287 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
3269 picture->cullRect().height(), | 3288 picture->cullRect().height(), |
3270 kBGRA_8888_SkColorType, | 3289 kBGRA_8888_SkColorType, |
3271 kOpaque_SkAlphaType); | 3290 kOpaque_SkAlphaType); |
3272 | 3291 |
3273 sk_sp<SkSurface> surf(this->makeSurface(info)); | 3292 sk_sp<SkSurface> surf(this->makeSurface(info)); |
3274 surf->getCanvas()->drawPicture(picture); | 3293 surf->getCanvas()->drawPicture(picture); |
3275 | 3294 |
3276 diffuseMap = surf->makeImageSnapshot(); | 3295 diffuseMap = surf->makeImageSnapshot(); |
3277 } | 3296 } |
3278 | |
3279 SkPaint shadowPaint; | 3297 SkPaint shadowPaint; |
3280 | 3298 |
3281 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti
leMode, | 3299 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti
leMode, |
3282 SkShader::kClamp_Ti
leMode); | 3300 SkShader::kClamp_Ti
leMode); |
3283 | |
3284 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile
Mode, | 3301 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile
Mode, |
3285 SkShader::kClamp_Tile
Mode); | 3302 SkShader::kClamp_Tile
Mode); |
3286 | |
3287 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader
), | 3303 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader
), |
3288 std::move(diffuseShader)
, | 3304 std::move(diffuseShader)
, |
3289 std::move(fLights), | 3305 std::move(fLights), |
3290 diffuseMap->width(), | 3306 diffuseMap->width(), |
3291 diffuseMap->height()); | 3307 diffuseMap->height(), |
| 3308 params); |
3292 | 3309 |
3293 shadowPaint.setShader(shadowShader); | 3310 shadowPaint.setShader(shadowShader); |
3294 | 3311 |
3295 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s
hadowPaint); | 3312 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s
hadowPaint); |
3296 } | 3313 } |
3297 #endif | 3314 #endif |
3298 | 3315 |
3299 /////////////////////////////////////////////////////////////////////////////// | 3316 /////////////////////////////////////////////////////////////////////////////// |
3300 /////////////////////////////////////////////////////////////////////////////// | 3317 /////////////////////////////////////////////////////////////////////////////// |
3301 | 3318 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3399 | 3416 |
3400 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { | 3417 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
3401 fCanvas->restoreToCount(fSaveCount); | 3418 fCanvas->restoreToCount(fSaveCount); |
3402 } | 3419 } |
3403 | 3420 |
3404 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API | 3421 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API |
3405 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p
rops) { | 3422 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p
rops) { |
3406 return this->makeSurface(info, props).release(); | 3423 return this->makeSurface(info, props).release(); |
3407 } | 3424 } |
3408 #endif | 3425 #endif |
OLD | NEW |