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

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

Issue 2220633002: moved code into onDrawShadowedPic, only renders into shadow maps if needed (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: made req changes 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 10 matching lines...) Expand all
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 "SkPaintPriv.h" 25 #include "SkPaintPriv.h"
26 #include "SkPatchUtils.h" 26 #include "SkPatchUtils.h"
27 #include "SkPicture.h" 27 #include "SkPicture.h"
28 #include "SkRasterClip.h" 28 #include "SkRasterClip.h"
29 #include "SkReadPixelsRec.h" 29 #include "SkReadPixelsRec.h"
30 #include "SkRRect.h" 30 #include "SkRRect.h"
31 #include "SkShadowPaintFilterCanvas.h"
32 #include "SkShadowShader.h"
31 #include "SkSmallAllocator.h" 33 #include "SkSmallAllocator.h"
32 #include "SkSpecialImage.h" 34 #include "SkSpecialImage.h"
33 #include "SkSurface_Base.h" 35 #include "SkSurface_Base.h"
34 #include "SkTextBlob.h" 36 #include "SkTextBlob.h"
35 #include "SkTextFormatParams.h" 37 #include "SkTextFormatParams.h"
36 #include "SkTLazy.h" 38 #include "SkTLazy.h"
37 #include "SkTraceEvent.h" 39 #include "SkTraceEvent.h"
38 40
39 #include <new> 41 #include <new>
40 42
41 #if SK_SUPPORT_GPU 43 #if SK_SUPPORT_GPU
42 #include "GrContext.h" 44 #include "GrContext.h"
43 #include "GrRenderTarget.h" 45 #include "GrRenderTarget.h"
44 #include "SkGrPriv.h" 46 #include "SkGrPriv.h"
robertphillips 2016/08/08 16:28:25 stray '\n'
vjiaoblack 2016/08/09 13:28:03 Done.
47
45 #endif 48 #endif
46 49
47 #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)
48 51
49 //#define SK_SUPPORT_PRECHECK_CLIPRECT 52 //#define SK_SUPPORT_PRECHECK_CLIPRECT
50 53
51 /* 54 /*
52 * 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.
53 * 56 *
54 * Returns false if 57 * Returns false if
(...skipping 3007 matching lines...) Expand 10 before | Expand all | Expand 10 after
3062 RETURN_ON_NULL(picture); 3065 RETURN_ON_NULL(picture);
3063 3066
3064 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); 3067 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()");
3065 3068
3066 this->onDrawShadowedPicture(picture, matrix, paint); 3069 this->onDrawShadowedPicture(picture, matrix, paint);
3067 } 3070 }
3068 3071
3069 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, 3072 void SkCanvas::onDrawShadowedPicture(const SkPicture* picture,
3070 const SkMatrix* matrix, 3073 const SkMatrix* matrix,
3071 const SkPaint* paint) { 3074 const SkPaint* paint) {
3072 this->onDrawPicture(picture, matrix, paint); 3075 if (!paint || paint->canComputeFastBounds()) {
3076 SkRect bounds = picture->cullRect();
3077 if (paint) {
3078 paint->computeFastBounds(bounds, &bounds);
3079 }
3080 if (matrix) {
3081 matrix->mapRect(&bounds);
3082 }
3083 if (this->quickReject(bounds)) {
3084 return;
3085 }
3086 }
3087
robertphillips 2016/08/08 16:28:25 Use the SkAutoCanvasMatrixPaint object. You can me
vjiaoblack 2016/08/09 13:28:03 Done.
3088 if (matrix) {
3089 this->setMatrix(*matrix);
3090 }
3091 for (int i = 0; i < fLights->numLights(); ++i) {
3092 // skip over ambient lights; they don't cast shadows
3093 // lights that have shadow maps do not need updating (because lights are immutable)
3094
3095 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() ||
3096 fLights->light(i).getShadowMap() != nullptr) {
3097 continue;
3098 }
3099
3100 // TODO: compute the correct size of the depth map from the light proper ties
3101 // TODO: maybe add a kDepth_8_SkColorType
3102 // TODO: find actual max depth of picture
3103 SkISize shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize(
3104 fLights->light(i), 255,
3105 picture->cullRect().width(),
3106 picture->cullRect().height());
3107
3108 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight ,
3109 kBGRA_8888_SkColorType,
3110 kOpaque_SkAlphaType);
3111
3112 // Create a new surface (that matches the backend of canvas)
3113 // for each shadow map
3114 sk_sp<SkSurface> surf(this->makeSurface(info));
3115
3116 // Wrap another SPFCanvas around the surface
3117 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3118 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3119
3120 // set the depth map canvas to have the light we're drawing.
3121 SkLights::Builder builder;
3122 builder.add(fLights->light(i));
3123 sk_sp<SkLights> curLight = builder.finish();
3124
3125 depthMapCanvas->setLights(std::move(curLight));
3126 depthMapCanvas->drawPicture(picture);
3127
3128 fLights->light(i).setShadowMap(surf->makeImageSnapshot());
3129 }
3130
3131 sk_sp<SkImage> povDepthMap;
3132 sk_sp<SkImage> diffuseMap;
3133
3134 // TODO: pass the depth to the shader in vertices, or uniforms
3135 // so we don't have to render depth and color separately
3136
3137 // povDepthMap
3138 {
3139 SkLights::Builder builder;
3140 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
3141 SkVector3::Make(0.0f, 0.0f, 1.0f)));
3142 sk_sp<SkLights> povLight = builder.finish();
3143
3144 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3145 picture->cullRect().height(),
3146 kBGRA_8888_SkColorType,
3147 kOpaque_SkAlphaType);
3148
3149 // Create a new surface (that matches the backend of canvas)
3150 // to create the povDepthMap
3151 sk_sp<SkSurface> surf(this->makeSurface(info));
3152
3153 // Wrap another SPFCanvas around the surface
3154 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3155 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3156
3157 // set the depth map canvas to have the light as the user's POV
3158 depthMapCanvas->setLights(std::move(povLight));
3159
3160 depthMapCanvas->drawPicture(picture);
3161
3162 povDepthMap = surf->makeImageSnapshot();
3163 }
3164
3165 // diffuseMap
3166 {
3167 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3168 picture->cullRect().height(),
3169 kBGRA_8888_SkColorType,
3170 kOpaque_SkAlphaType);
3171
3172 sk_sp<SkSurface> surf(this->makeSurface(info));
3173 surf->getCanvas()->drawPicture(picture);
3174
3175 diffuseMap = surf->makeImageSnapshot();
3176 }
3177
robertphillips 2016/08/08 16:28:25 Look at the documentation for drawPicture for what
vjiaoblack 2016/08/09 13:28:03 seems like SkAutoCanvasMatrixPaint doesn't handle
3178 SkPaint shadowPaint;
3179 if (paint) {
3180 shadowPaint = *paint;
3181 }
3182 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode,
3183 SkShader::kClamp_Ti leMode);
3184
3185 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode,
3186 SkShader::kClamp_Tile Mode);
3187
3188 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ),
3189 std::move(diffuseShader) ,
3190 std::move(fLights),
3191 diffuseMap->width(),
3192 diffuseMap->height());
3193
3194 shadowPaint.setShader(shadowShader);
3195
3196 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), s hadowPaint);
3073 } 3197 }
3074 #endif 3198 #endif
3075 3199
3076 /////////////////////////////////////////////////////////////////////////////// 3200 ///////////////////////////////////////////////////////////////////////////////
3077 /////////////////////////////////////////////////////////////////////////////// 3201 ///////////////////////////////////////////////////////////////////////////////
3078 3202
3079 SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) { 3203 SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) {
3080 static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small"); 3204 static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small");
3081 3205
3082 SkASSERT(canvas); 3206 SkASSERT(canvas);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
3176 3300
3177 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { 3301 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
3178 fCanvas->restoreToCount(fSaveCount); 3302 fCanvas->restoreToCount(fSaveCount);
3179 } 3303 }
3180 3304
3181 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API 3305 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
3182 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) { 3306 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) {
3183 return this->makeSurface(info, props).release(); 3307 return this->makeSurface(info, props).release();
3184 } 3308 }
3185 #endif 3309 #endif
OLDNEW
« samplecode/SampleShadowing.cpp ('K') | « samplecode/SampleShadowing.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698