 Chromium Code Reviews
 Chromium Code Reviews Issue 23102018:
  Refactoring DrawLooper so that it can apply shadow effects as skia image filters  (Closed) 
  Base URL: svn://svn.chromium.org/blink/trunk
    
  
    Issue 23102018:
  Refactoring DrawLooper so that it can apply shadow effects as skia image filters  (Closed) 
  Base URL: svn://svn.chromium.org/blink/trunk| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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() | 
| 49 { | |
| 50 m_cachedDrawLooper.reset(0); | |
| 51 } | |
| 47 | 52 | 
| 48 DrawLooper::~DrawLooper() { } | 53 DrawLooper::~DrawLooper() { } | 
| 49 | 54 | 
| 50 SkDrawLooper* DrawLooper::skDrawLooper() const | 55 SkDrawLooper* DrawLooper::skDrawLooper() | 
| 51 { | 56 { | 
| 52 return m_skDrawLooper.get(); | 57 if (!m_cachedDrawLooper.get()) | 
| 58 buildCachedDrawLooper(); | |
| 59 return m_cachedDrawLooper.get(); | |
| 60 } | |
| 61 | |
| 62 SkImageFilter* DrawLooper::imageFilter() | |
| 63 { | |
| 64 if (!m_cachedImageFilter.get()) | |
| 65 buildCachedImageFilter(); | |
| 66 return m_cachedImageFilter.get(); | |
| 67 } | |
| 68 | |
| 69 void DrawLooper::clearCached() | |
| 70 { | |
| 71 m_cachedDrawLooper.reset(0); | |
| 72 m_cachedImageFilter.reset(0); | |
| 53 } | 73 } | 
| 54 | 74 | 
| 55 void DrawLooper::addUnmodifiedContent() | 75 void DrawLooper::addUnmodifiedContent() | 
| 56 { | 76 { | 
| 57 SkLayerDrawLooper::LayerInfo info; | 77 DrawLooperLayerInfo info; | 
| 58 m_skDrawLooper->addLayerOnTop(info); | 78 info.m_layerType = UnmodifiedLayer; | 
| 79 m_layerInfo.append(info); | |
| 80 clearCached(); | |
| 59 } | 81 } | 
| 60 | 82 | 
| 83 | |
| 
jbroman
2013/08/23 19:59:38
Extra newline?
 | |
| 61 void DrawLooper::addShadow(const FloatSize& offset, float blur, const Color& col or, | 84 void DrawLooper::addShadow(const FloatSize& offset, float blur, const Color& col or, | 
| 62 ShadowTransformMode shadowTransformMode, ShadowAlphaMode shadowAlphaMode) | 85 ShadowTransformMode shadowTransformMode, ShadowAlphaMode shadowAlphaMode) | 
| 63 { | 86 { | 
| 64 // Detect when there's no effective shadow. | 87 // Detect when there's no effective shadow. | 
| 65 if (!color.alpha()) | 88 if (!color.alpha()) | 
| 66 return; | 89 return; | 
| 90 DrawLooperLayerInfo info; | |
| 91 info.m_layerType = ShadowLayer; | |
| 92 info.m_blur = blur; | |
| 93 info.m_color = color; | |
| 94 info.m_offset = offset; | |
| 95 info.m_shadowAlphaMode = shadowAlphaMode; | |
| 96 info.m_shadowTransformMode = shadowTransformMode; | |
| 97 m_layerInfo.append(info); | |
| 98 clearCached(); | |
| 99 }; | |
| 67 | 100 | 
| 68 SkColor skColor = color.rgb(); | 101 void DrawLooper::buildCachedDrawLooper() | 
| 102 { | |
| 103 m_cachedDrawLooper.reset(new SkLayerDrawLooper); | |
| 104 LayerVector::const_iterator info; | |
| 105 for (info = m_layerInfo.begin(); info < m_layerInfo.end(); ++info) { | |
| 106 if (info->m_layerType == ShadowLayer) { | |
| 107 SkColor skColor = info->m_color.rgb(); | |
| 69 | 108 | 
| 70 SkLayerDrawLooper::LayerInfo info; | 109 SkLayerDrawLooper::LayerInfo skInfo; | 
| 71 | 110 | 
| 72 switch (shadowAlphaMode) { | 111 switch (info->m_shadowAlphaMode) { | 
| 73 case ShadowRespectsAlpha: | 112 case ShadowRespectsAlpha: | 
| 74 info.fColorMode = SkXfermode::kDst_Mode; | 113 skInfo.fColorMode = SkXfermode::kDst_Mode; | 
| 75 break; | 114 break; | 
| 76 case ShadowIgnoresAlpha: | 115 case ShadowIgnoresAlpha: | 
| 77 info.fColorMode = SkXfermode::kSrc_Mode; | 116 skInfo.fColorMode = SkXfermode::kSrc_Mode; | 
| 78 break; | 117 break; | 
| 79 default: | 118 default: | 
| 80 ASSERT_NOT_REACHED(); | 119 ASSERT_NOT_REACHED(); | 
| 120 } | |
| 121 | |
| 122 if (info->m_blur) | |
| 123 skInfo.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; // our blur | |
| 124 skInfo.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit; | |
| 125 skInfo.fOffset.set(info->m_offset.width(), info->m_offset.height()); | |
| 126 skInfo.fPostTranslate = (info->m_shadowTransformMode == ShadowIgnore sTransforms); | |
| 127 | |
| 128 SkPaint* paint = m_cachedDrawLooper->addLayerOnTop(skInfo); | |
| 129 | |
| 130 if (info->m_blur) { | |
| 131 uint32_t mfFlags = SkBlurMaskFilter::kHighQuality_BlurFlag; | |
| 132 if (info->m_shadowTransformMode == ShadowIgnoresTransforms) | |
| 133 mfFlags |= SkBlurMaskFilter::kIgnoreTransform_BlurFlag; | |
| 134 RefPtr<SkMaskFilter> mf = adoptRef(SkBlurMaskFilter::Create( | |
| 135 (double)info->m_blur / 2.0, SkBlurMaskFilter::kNormal_BlurSt yle, mfFlags)); | |
| 136 paint->setMaskFilter(mf.get()); | |
| 137 } | |
| 138 | |
| 139 RefPtr<SkColorFilter> cf = adoptRef(SkColorFilter::CreateModeFilter( skColor, SkXfermode::kSrcIn_Mode)); | |
| 140 paint->setColorFilter(cf.get()); | |
| 141 } else { | |
| 142 // Unmodified layer | |
| 143 SkLayerDrawLooper::LayerInfo skInfo; | |
| 144 m_cachedDrawLooper->addLayerOnTop(skInfo); | |
| 145 } | |
| 81 } | 146 } | 
| 147 } | |
| 82 | 148 | 
| 83 if (blur) | 149 void DrawLooper::buildCachedImageFilter() | 
| 84 info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; // our blur | 150 { | 
| 85 info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit; | 151 ASSERT(m_layerInfo.size() == 2); | 
| 86 info.fOffset.set(offset.width(), offset.height()); | 152 ASSERT(m_layerInfo[0].m_layerType == ShadowLayer); | 
| 87 info.fPostTranslate = (shadowTransformMode == ShadowIgnoresTransforms); | 153 ASSERT(m_layerInfo[1].m_layerType == UnmodifiedLayer); | 
| 154 ASSERT(m_layerInfo[0].m_shadowAlphaMode == ShadowRespectsAlpha); | |
| 155 ASSERT(m_layerInfo[0].m_shadowTransformMode == ShadowIgnoresTransforms); | |
| 156 const float blurToSigmaFactor = 0.25; | |
| 157 SkColor skColor = m_layerInfo[0].m_color.rgb(); | |
| 158 m_cachedImageFilter.reset(new SkDropShadowImageFilter(m_layerInfo[0].m_offse t.width(), m_layerInfo[0].m_offset.height(), m_layerInfo[0].m_blur * blurToSigma Factor, skColor)); | |
| 159 } | |
| 88 | 160 | 
| 89 SkPaint* paint = m_skDrawLooper->addLayerOnTop(info); | 161 bool DrawLooper::shouldUseImageFilterToDrawBitmap(const SkBitmap& bitmap) | 
| 162 { | |
| 163 if (bitmap.isOpaque() || !m_layerInfo.size()) | |
| 164 return false; | |
| 90 | 165 | 
| 91 if (blur) { | 166 #if !ASSERT_DISABLED | 
| 92 uint32_t mfFlags = SkBlurMaskFilter::kHighQuality_BlurFlag; | 167 // Verify that cases that require a mask filter to render correctly are of | 
| 93 if (shadowTransformMode == ShadowIgnoresTransforms) | 168 // a form that can be handled by DropShadowImageFilter. | 
| 94 mfFlags |= SkBlurMaskFilter::kIgnoreTransform_BlurFlag; | 169 LayerVector::const_iterator info; | 
| 95 RefPtr<SkMaskFilter> mf = adoptRef(SkBlurMaskFilter::Create( | 170 int unmodifiedCount = 0; | 
| 96 (double)blur / 2.0, SkBlurMaskFilter::kNormal_BlurStyle, mfFlags)); | 171 int shadowCount = 0; | 
| 97 paint->setMaskFilter(mf.get()); | 172 bool needsFilter = false; | 
| 173 for (info = m_layerInfo.begin(); info < m_layerInfo.end(); ++info) { | |
| 174 if (info->m_layerType == ShadowLayer) { | |
| 175 needsFilter = needsFilter || (info->m_blur && info->m_shadowAlphaMod e == ShadowRespectsAlpha); | |
| 176 shadowCount++; | |
| 177 } else { | |
| 178 unmodifiedCount++; | |
| 179 } | |
| 180 | |
| 98 } | 181 } | 
| 182 if (needsFilter) { | |
| 183 // If any of the following assertions ever fire, it means that we are hi tting | |
| 184 // case that may not be rendered correclty by the DrawLooper and cannot be | |
| 185 // handled by DropShadowImageFilter. | |
| 186 ASSERT(shadowCount == 1); | |
| 187 ASSERT(unmodifiedCount == 1); | |
| 188 ASSERT(m_layerInfo[0].m_layerType == ShadowLayer); | |
| 189 ASSERT(m_layerInfo[0].m_shadowTransformMode == ShadowIgnoresTransforms); | |
| 190 } | |
| 191 #endif | |
| 99 | 192 | 
| 100 RefPtr<SkColorFilter> cf = adoptRef(SkColorFilter::CreateModeFilter(skColor, SkXfermode::kSrcIn_Mode)); | 193 return m_layerInfo.size() == 2 | 
| 101 paint->setColorFilter(cf.get()); | 194 && m_layerInfo[0].m_layerType == ShadowLayer | 
| 195 && m_layerInfo[0].m_shadowAlphaMode == ShadowRespectsAlpha | |
| 196 && m_layerInfo[0].m_shadowTransformMode == ShadowIgnoresTransforms | |
| 197 && m_layerInfo[0].m_blur | |
| 198 && m_layerInfo[1].m_layerType == UnmodifiedLayer; | |
| 102 } | 199 } | 
| 103 | 200 | 
| 104 } // namespace WebCore | 201 } // namespace WebCore | 
| OLD | NEW |