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

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: 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
« no previous file with comments | « samplecode/SampleShadowing.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 18 matching lines...) Expand all
29 #include "SkReadPixelsRec.h" 29 #include "SkReadPixelsRec.h"
30 #include "SkRRect.h" 30 #include "SkRRect.h"
31 #include "SkSmallAllocator.h" 31 #include "SkSmallAllocator.h"
32 #include "SkSpecialImage.h" 32 #include "SkSpecialImage.h"
33 #include "SkSurface_Base.h" 33 #include "SkSurface_Base.h"
34 #include "SkTextBlob.h" 34 #include "SkTextBlob.h"
35 #include "SkTextFormatParams.h" 35 #include "SkTextFormatParams.h"
36 #include "SkTLazy.h" 36 #include "SkTLazy.h"
37 #include "SkTraceEvent.h" 37 #include "SkTraceEvent.h"
38 38
39 #include <new> 39 #include <new>
robertphillips 2016/08/08 14:18:31 "" style - not <> style
40 #include <SkShadowPaintFilterCanvas.h>
40 41
41 #if SK_SUPPORT_GPU 42 #if SK_SUPPORT_GPU
42 #include "GrContext.h" 43 #include "GrContext.h"
43 #include "GrRenderTarget.h" 44 #include "GrRenderTarget.h"
44 #include "SkGrPriv.h" 45 #include "SkGrPriv.h"
46 #include "SkShadowShader.h"
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 for (int i = 0; i < fLights->numLights(); ++i) {
3076 // skip over ambient lights; they don't cast shadows
3077 // lights that have shadow maps do not need updating (because lights are immutable)
3078 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type() ||
3079 fLights->light(i).getShadowMap() != nullptr) {
3080 continue;
3081 }
3082
3083 // TODO: compute the correct size of the depth map from the light proper ties
3084 // TODO: maybe add a kDepth_8_SkColorType
3085 // TODO: find actual max depth of picture
3086 SkISize shMapSize = SkShadowPaintFilterCanvas::
3087 ComputeDepthMapSize(fLights->light(i), 255,
3088 picture->cullRect().width(),
3089 picture->cullRect().height());
3090
3091 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight ,
3092 kBGRA_8888_SkColorType,
3093 kOpaque_SkAlphaType);
3094
3095 // Create a new surface (that matches the backend of canvas)
3096 // for each shadow map
3097 sk_sp<SkSurface> surf(this->makeSurface(info));
3098
3099 // Wrap another SPFCanvas around the surface
3100 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3101 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3102
3103 // set the depth map canvas to have the light we're drawing.
3104 SkLights::Builder builder;
3105 builder.add(fLights->light(i));
3106 sk_sp<SkLights> curLight = builder.finish();
3107
3108 depthMapCanvas->setLights(std::move(curLight));
3109 depthMapCanvas->drawPicture(picture);
3110
3111 fLights->light(i).setShadowMap(surf->makeImageSnapshot());
3112 }
3113
3114 sk_sp<SkImage> povDepthMap;
3115 sk_sp<SkImage> diffuseMap;
3116
3117 // TODO: pass the depth to the shader in vertices, or uniforms
3118 // so we don't have to render depth and color separately
3119
3120 // povDepthMap
3121 {
3122 SkLights::Builder builder;
3123 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
3124 SkVector3::Make(0.0f, 0.0f, 1.0f)));
3125 sk_sp<SkLights> povLight = builder.finish();
3126
3127 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3128 picture->cullRect().height(),
3129 kBGRA_8888_SkColorType,
3130 kOpaque_SkAlphaType);
3131
3132 // Create a new surface (that matches the backend of canvas)
3133 // to create the povDepthMap
3134 sk_sp<SkSurface> surf(this->makeSurface(info));
3135
3136 // Wrap another SPFCanvas around the surface
3137 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
3138 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
3139
3140 // set the depth map canvas to have the light as the user's POV
3141 depthMapCanvas->setLights(std::move(povLight));
3142
3143 depthMapCanvas->drawPicture(picture);
3144
3145 povDepthMap = surf->makeImageSnapshot();
3146 }
3147
3148 // diffuseMap
3149 {
3150 SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(),
3151 picture->cullRect().height(),
3152 kBGRA_8888_SkColorType,
3153 kOpaque_SkAlphaType);
3154
3155 sk_sp<SkSurface> surf(this->makeSurface(info));
3156 surf->getCanvas()->drawPicture(picture);
3157
3158 diffuseMap = surf->makeImageSnapshot();
3159 }
3160
3161 SkPaint paint2 = *paint;
3162
3163 sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_Ti leMode,
3164 SkShader::kClamp_TileM ode);
3165
3166 sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_Tile Mode,
3167 SkShader::kClamp_TileMod e);
3168
3169 sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader ),
3170 std::move(diffuseShader) ,
3171 std::move(fLights),
3172 diffuseMap->width(),
3173 diffuseMap->height());
3174
3175 paint2.setShader(shadowShader);
3176
3177 this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), p aint2);
3178
3179
3180
3073 } 3181 }
3074 #endif 3182 #endif
3075 3183
3076 /////////////////////////////////////////////////////////////////////////////// 3184 ///////////////////////////////////////////////////////////////////////////////
3077 /////////////////////////////////////////////////////////////////////////////// 3185 ///////////////////////////////////////////////////////////////////////////////
3078 3186
3079 SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) { 3187 SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) {
3080 static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small"); 3188 static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small");
3081 3189
3082 SkASSERT(canvas); 3190 SkASSERT(canvas);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
3176 3284
3177 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { 3285 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
3178 fCanvas->restoreToCount(fSaveCount); 3286 fCanvas->restoreToCount(fSaveCount);
3179 } 3287 }
3180 3288
3181 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API 3289 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
3182 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) { 3290 SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* p rops) {
3183 return this->makeSurface(info, props).release(); 3291 return this->makeSurface(info, props).release();
3184 } 3292 }
3185 #endif 3293 #endif
OLDNEW
« no previous file with comments | « samplecode/SampleShadowing.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698