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

Side by Side Diff: Source/core/platform/graphics/DrawLooper.cpp

Issue 23102018: Refactoring DrawLooper so that it can apply shadow effects as skia image filters (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Adding missing adoptRefs Created 7 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 15 matching lines...) Expand all
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "core/platform/graphics/DrawLooper.h" 32 #include "core/platform/graphics/DrawLooper.h"
33 33
34 #include "core/platform/graphics/Color.h" 34 #include "core/platform/graphics/Color.h"
35 #include "core/platform/graphics/FloatSize.h" 35 #include "core/platform/graphics/FloatSize.h"
36 #include "third_party/skia/include/core/SkBitmap.h"
36 #include "third_party/skia/include/core/SkColor.h" 37 #include "third_party/skia/include/core/SkColor.h"
37 #include "third_party/skia/include/core/SkColorFilter.h" 38 #include "third_party/skia/include/core/SkColorFilter.h"
38 #include "third_party/skia/include/core/SkDrawLooper.h" 39 #include "third_party/skia/include/core/SkDrawLooper.h"
39 #include "third_party/skia/include/core/SkPaint.h" 40 #include "third_party/skia/include/core/SkPaint.h"
40 #include "third_party/skia/include/core/SkXfermode.h" 41 #include "third_party/skia/include/core/SkXfermode.h"
41 #include "third_party/skia/include/effects/SkBlurMaskFilter.h" 42 #include "third_party/skia/include/effects/SkBlurMaskFilter.h"
43 #include "third_party/skia/include/effects/SkDropShadowImageFilter.h"
42 #include "third_party/skia/include/effects/SkLayerDrawLooper.h" 44 #include "third_party/skia/include/effects/SkLayerDrawLooper.h"
43 45
44 namespace WebCore { 46 namespace WebCore {
45 47
46 DrawLooper::DrawLooper() : m_skDrawLooper(adoptRef(new SkLayerDrawLooper)) { } 48 DrawLooper::DrawLooper() { }
47 49
48 DrawLooper::~DrawLooper() { } 50 DrawLooper::~DrawLooper() { }
49 51
50 SkDrawLooper* DrawLooper::skDrawLooper() const 52 SkDrawLooper* DrawLooper::skDrawLooper() const
51 { 53 {
52 return m_skDrawLooper.get(); 54 if (!m_cachedDrawLooper)
55 buildCachedDrawLooper();
56 return m_cachedDrawLooper.get();
57 }
58
59 SkImageFilter* DrawLooper::imageFilter() const
60 {
61 if (!m_cachedImageFilter)
62 buildCachedImageFilter();
63 return m_cachedImageFilter.get();
64 }
65
66 void DrawLooper::clearCached()
67 {
68 m_cachedDrawLooper.clear();
69 m_cachedImageFilter.clear();
53 } 70 }
54 71
55 void DrawLooper::addUnmodifiedContent() 72 void DrawLooper::addUnmodifiedContent()
56 { 73 {
57 SkLayerDrawLooper::LayerInfo info; 74 DrawLooperLayerInfo info;
58 m_skDrawLooper->addLayerOnTop(info); 75 info.m_layerType = UnmodifiedLayer;
76 m_layerInfo.append(info);
77 clearCached();
59 } 78 }
60 79
61 void DrawLooper::addShadow(const FloatSize& offset, float blur, const Color& col or, 80 void DrawLooper::addShadow(const FloatSize& offset, float blur, const Color& col or,
62 ShadowTransformMode shadowTransformMode, ShadowAlphaMode shadowAlphaMode) 81 ShadowTransformMode shadowTransformMode, ShadowAlphaMode shadowAlphaMode)
63 { 82 {
64 // Detect when there's no effective shadow. 83 // Detect when there's no effective shadow.
65 if (!color.alpha()) 84 if (!color.alpha())
66 return; 85 return;
86 DrawLooperLayerInfo info;
87 info.m_layerType = ShadowLayer;
88 info.m_blur = blur;
89 info.m_color = color;
90 info.m_offset = offset;
91 info.m_shadowAlphaMode = shadowAlphaMode;
92 info.m_shadowTransformMode = shadowTransformMode;
93 m_layerInfo.append(info);
94 clearCached();
95 };
67 96
68 SkColor skColor = color.rgb(); 97 void DrawLooper::buildCachedDrawLooper() const
98 {
99 m_cachedDrawLooper = adoptRef(new SkLayerDrawLooper);
100 LayerVector::const_iterator info;
101 for (info = m_layerInfo.begin(); info < m_layerInfo.end(); ++info) {
102 if (info->m_layerType == ShadowLayer) {
103 SkColor skColor = info->m_color.rgb();
69 104
70 SkLayerDrawLooper::LayerInfo info; 105 SkLayerDrawLooper::LayerInfo skInfo;
71 106
72 switch (shadowAlphaMode) { 107 switch (info->m_shadowAlphaMode) {
73 case ShadowRespectsAlpha: 108 case ShadowRespectsAlpha:
74 info.fColorMode = SkXfermode::kDst_Mode; 109 skInfo.fColorMode = SkXfermode::kDst_Mode;
75 break; 110 break;
76 case ShadowIgnoresAlpha: 111 case ShadowIgnoresAlpha:
77 info.fColorMode = SkXfermode::kSrc_Mode; 112 skInfo.fColorMode = SkXfermode::kSrc_Mode;
78 break; 113 break;
79 default: 114 default:
80 ASSERT_NOT_REACHED(); 115 ASSERT_NOT_REACHED();
116 }
117
118 if (info->m_blur)
119 skInfo.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; // our blur
120 skInfo.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
121 skInfo.fOffset.set(info->m_offset.width(), info->m_offset.height());
122 skInfo.fPostTranslate = (info->m_shadowTransformMode == ShadowIgnore sTransforms);
123
124 SkPaint* paint = m_cachedDrawLooper->addLayerOnTop(skInfo);
125
126 if (info->m_blur) {
127 uint32_t mfFlags = SkBlurMaskFilter::kHighQuality_BlurFlag;
128 if (info->m_shadowTransformMode == ShadowIgnoresTransforms)
129 mfFlags |= SkBlurMaskFilter::kIgnoreTransform_BlurFlag;
130 RefPtr<SkMaskFilter> mf = adoptRef(SkBlurMaskFilter::Create(
131 (double)info->m_blur / 2.0, SkBlurMaskFilter::kNormal_BlurSt yle, mfFlags));
132 paint->setMaskFilter(mf.get());
133 }
134
135 RefPtr<SkColorFilter> cf = adoptRef(SkColorFilter::CreateModeFilter( skColor, SkXfermode::kSrcIn_Mode));
136 paint->setColorFilter(cf.get());
137 } else {
138 // Unmodified layer
139 SkLayerDrawLooper::LayerInfo skInfo;
140 m_cachedDrawLooper->addLayerOnTop(skInfo);
141 }
81 } 142 }
143 }
82 144
83 if (blur) 145 void DrawLooper::buildCachedImageFilter() const
84 info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; // our blur 146 {
85 info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit; 147 ASSERT(m_layerInfo.size() == 2);
86 info.fOffset.set(offset.width(), offset.height()); 148 ASSERT(m_layerInfo[0].m_layerType == ShadowLayer);
87 info.fPostTranslate = (shadowTransformMode == ShadowIgnoresTransforms); 149 ASSERT(m_layerInfo[1].m_layerType == UnmodifiedLayer);
150 ASSERT(m_layerInfo[0].m_shadowAlphaMode == ShadowRespectsAlpha);
151 ASSERT(m_layerInfo[0].m_shadowTransformMode == ShadowIgnoresTransforms);
152 const float blurToSigmaFactor = 0.25;
153 SkColor skColor = m_layerInfo[0].m_color.rgb();
154 m_cachedImageFilter = adoptRef(new SkDropShadowImageFilter(m_layerInfo[0].m_ offset.width(), m_layerInfo[0].m_offset.height(), m_layerInfo[0].m_blur * blurTo SigmaFactor, skColor));
155 }
88 156
89 SkPaint* paint = m_skDrawLooper->addLayerOnTop(info); 157 bool DrawLooper::shouldUseImageFilterToDrawBitmap(const SkBitmap& bitmap) const
158 {
159 if (bitmap.isOpaque() || !m_layerInfo.size())
160 return false;
90 161
91 if (blur) { 162 #if !ASSERT_DISABLED
92 uint32_t mfFlags = SkBlurMaskFilter::kHighQuality_BlurFlag; 163 // Verify that cases that require a mask filter to render correctly are of
93 if (shadowTransformMode == ShadowIgnoresTransforms) 164 // a form that can be handled by DropShadowImageFilter.
94 mfFlags |= SkBlurMaskFilter::kIgnoreTransform_BlurFlag; 165 LayerVector::const_iterator info;
95 RefPtr<SkMaskFilter> mf = adoptRef(SkBlurMaskFilter::Create( 166 int unmodifiedCount = 0;
96 (double)blur / 2.0, SkBlurMaskFilter::kNormal_BlurStyle, mfFlags)); 167 int shadowCount = 0;
97 paint->setMaskFilter(mf.get()); 168 bool needsFilter = false;
169 for (info = m_layerInfo.begin(); info < m_layerInfo.end(); ++info) {
170 if (info->m_layerType == ShadowLayer) {
171 needsFilter = needsFilter || (info->m_blur && info->m_shadowAlphaMod e == ShadowRespectsAlpha);
172 shadowCount++;
173 } else {
174 unmodifiedCount++;
175 }
176
98 } 177 }
178 if (needsFilter) {
179 // If any of the following assertions ever fire, it means that we are hi tting
180 // case that may not be rendered correclty by the DrawLooper and cannot be
181 // handled by DropShadowImageFilter.
182 ASSERT(shadowCount == 1);
183 ASSERT(unmodifiedCount == 1);
184 ASSERT(m_layerInfo[0].m_layerType == ShadowLayer);
185 ASSERT(m_layerInfo[0].m_shadowTransformMode == ShadowIgnoresTransforms);
186 }
187 #endif
99 188
100 RefPtr<SkColorFilter> cf = adoptRef(SkColorFilter::CreateModeFilter(skColor, SkXfermode::kSrcIn_Mode)); 189 return m_layerInfo.size() == 2
101 paint->setColorFilter(cf.get()); 190 && m_layerInfo[0].m_layerType == ShadowLayer
191 && m_layerInfo[0].m_shadowAlphaMode == ShadowRespectsAlpha
192 && m_layerInfo[0].m_shadowTransformMode == ShadowIgnoresTransforms
193 && m_layerInfo[0].m_blur
194 && m_layerInfo[1].m_layerType == UnmodifiedLayer;
102 } 195 }
103 196
104 } // namespace WebCore 197 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/platform/graphics/DrawLooper.h ('k') | Source/core/platform/graphics/GraphicsContext.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698