1 /* 2 * Copyright (C) Research In Motion Limited 2011. All rights reserved. 3 * Copyright (C) 2013 Google Inc. All rights reserved. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #include "config.h" 22 23 #include "core/platform/graphics/filters/FEDropShadow.h" 24 25 #include "core/platform/graphics/GraphicsContext.h" 26 #include "core/platform/graphics/filters/FEGaussianBlur.h" 27 #include "core/platform/graphics/filters/Filter.h" 28 #include "core/platform/text/TextStream.h" 29 #include "core/rendering/RenderTreeAsText.h" 30 #include "third_party/skia/include/core/SkColorFilter.h" 31 #include "third_party/skia/include/effects/SkBlurImageFilter.h" 32 33 using namespace std; 34 35 namespace WebCore { 36 37 FEDropShadow::FEDropShadow(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity) 38 : FilterEffect(filter) 39 , m_stdX(stdX) 40 , m_stdY(stdY) 41 , m_dx(dx) 42 , m_dy(dy) 43 , m_shadowColor(shadowColor) 44 , m_shadowOpacity(shadowOpacity) 45 { 46 } 47 48 PassRefPtr<FEDropShadow> FEDropShadow::create(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity) 49 { 50 return adoptRef(new FEDropShadow(filter, stdX, stdY, dx, dy, shadowColor, shadowOpacity)); 51 } 52 53 void FEDropShadow::determineAbsolutePaintRect() 54 { 55 Filter* filter = this->filter(); 56 ASSERT(filter); 57 58 FloatRect absolutePaintRect = mapRect(inputEffect(0)->absolutePaintRect()); 59 60 if (clipsToBounds()) 61 absolutePaintRect.intersect(maxEffectRect()); 62 else 63 absolutePaintRect.unite(maxEffectRect()); 64 65 setAbsolutePaintRect(enclosingIntRect(absolutePaintRect)); 66 } 67 68 FloatRect FEDropShadow::mapRect(const FloatRect& rect, bool forward) 69 { 70 FloatRect result = rect; 71 Filter* filter = this->filter(); 72 ASSERT(filter); 73 74 FloatRect offsetRect = rect; 75 if (forward) 76 offsetRect.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy)); 77 else 78 offsetRect.move(-filter->applyHorizontalScale(m_dx), -filter->applyVerticalScale(m_dy)); 79 result.unite(offsetRect); 80 81 unsigned kernelSizeX = 0; 82 unsigned kernelSizeY = 0; 83 FEGaussianBlur::calculateKernelSize(filter, kernelSizeX, kernelSizeY, m_stdX, m_stdY); 84 85 // We take the half kernel size and multiply it with three, because we run box blur three times. 86 result.inflateX(3 * kernelSizeX * 0.5f); 87 result.inflateY(3 * kernelSizeY * 0.5f); 88 return result; 89 } 90 91 void FEDropShadow::applySoftware() 92 { 93 FilterEffect* in = inputEffect(0); 94 95 ImageBuffer* resultImage = createImageBufferResult(); 96 if (!resultImage) 97 return; 98 99 Filter* filter = this->filter(); 100 FloatSize blurRadius(filter->applyHorizontalScale(m_stdX), filter->applyVerticalScale(m_stdY)); 101 FloatSize offset(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy)); 102 103 FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect()); 104 GraphicsContext* resultContext = resultImage->context(); 105 ASSERT(resultContext); 106 107 SkAutoTUnref<SkImageFilter> blurFilter(new SkBlurImageFilter(blurRadius.width(), blurRadius.height())); 108 SkAutoTUnref<SkColorFilter> colorFilter(SkColorFilter::CreateModeFilter(m_shadowColor.rgb(), SkXfermode::kSrcIn_Mode)); 109 SkPaint paint; 110 paint.setImageFilter(blurFilter.get()); 111 paint.setColorFilter(colorFilter.get()); 112 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); 113 RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore); 114 115 RefPtr<NativeImageSkia> nativeImage = image->nativeImageForCurrentFrame(); 116 117 if (!nativeImage) 118 return; 119 120 resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x() + offset.width(), drawingRegion.y() + offset.height(), &paint); 121 resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x(), drawingRegion.y()); 122 } 123 124 TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const 125 { 126 writeIndent(ts, indent); 127 ts << "[feDropShadow"; 128 FilterEffect::externalRepresentation(ts); 129 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\" dx=\"" << m_dx << "\" dy=\"" << m_dy << "\" flood-color=\"" << m_shadowColor.nameForRenderTreeAsText() <<"\" flood-opacity=\"" << m_shadowOpacity << "]\n"; 130 inputEffect(0)->externalRepresentation(ts, indent + 1); 131 return ts; 132 } 133 134 } // namespace WebCore 135