1 /* 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005 Rob Buis <buis (at) kde.org> 4 * Copyright (C) 2005 Eric Seidel <eric (at) webkit.org> 5 * Copyright (C) 2009 Dirk Schulze <krit (at) webkit.org> 6 * Copyright (C) 2010 Igalia, S.L. 7 * Copyright (C) Research In Motion Limited 2010. All rights reserved. 8 * Copyright (C) 2013 Google Inc. All rights reserved. 9 * 10 * This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Library General Public 12 * License as published by the Free Software Foundation; either 13 * version 2 of the License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Library General Public License for more details. 19 * 20 * You should have received a copy of the GNU Library General Public License 21 * along with this library; see the file COPYING.LIB. If not, write to 22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 23 * Boston, MA 02110-1301, USA. 24 */ 25 26 #include "config.h" 27 28 #include "platform/graphics/filters/FEGaussianBlur.h" 29 30 #include "platform/graphics/GraphicsContext.h" 31 #include "platform/graphics/cpu/arm/filters/FEGaussianBlurNEON.h" 32 #include "platform/graphics/filters/ParallelJobs.h" 33 #include "platform/graphics/filters/SkiaImageFilterBuilder.h" 34 #include "platform/text/TextStream.h" 35 #include "wtf/MathExtras.h" 36 #include "wtf/Uint8ClampedArray.h" 37 38 #include "SkBlurImageFilter.h" 39 40 static inline float gaussianKernelFactor() 41 { 42 return 3 / 4.f * sqrtf(twoPiFloat); 43 } 44 45 static const int gMaxKernelSize = 1000; 46 47 namespace blink { 48 49 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y) 50 : FilterEffect(filter) 51 , m_stdX(x) 52 , m_stdY(y) 53 { 54 } 55 56 PassRefPtr<FEGaussianBlur> FEGaussianBlur::create(Filter* filter, float x, float y) 57 { 58 return adoptRef(new FEGaussianBlur(filter, x, y)); 59 } 60 61 float FEGaussianBlur::stdDeviationX() const 62 { 63 return m_stdX; 64 } 65 66 void FEGaussianBlur::setStdDeviationX(float x) 67 { 68 m_stdX = x; 69 } 70 71 float FEGaussianBlur::stdDeviationY() const 72 { 73 return m_stdY; 74 } 75 76 void FEGaussianBlur::setStdDeviationY(float y) 77 { 78 m_stdY = y; 79 } 80 81 IntSize FEGaussianBlur::calculateUnscaledKernelSize(const FloatPoint& std) 82 { 83 ASSERT(std.x() >= 0 && std.y() >= 0); 84 85 IntSize kernelSize; 86 // Limit the kernel size to 1000. A bigger radius won't make a big difference for the result image but 87 // inflates the absolute paint rect to much. This is compatible with Firefox' behavior. 88 if (std.x()) { 89 int size = std::max<unsigned>(2, static_cast<unsigned>(floorf(std.x() * gaussianKernelFactor() + 0.5f))); 90 kernelSize.setWidth(std::min(size, gMaxKernelSize)); 91 } 92 93 if (std.y()) { 94 int size = std::max<unsigned>(2, static_cast<unsigned>(floorf(std.y() * gaussianKernelFactor() + 0.5f))); 95 kernelSize.setHeight(std::min(size, gMaxKernelSize)); 96 } 97 98 return kernelSize; 99 } 100 101 IntSize FEGaussianBlur::calculateKernelSize(Filter* filter, const FloatPoint& std) 102 { 103 FloatPoint stdError(filter->applyHorizontalScale(std.x()), filter->applyVerticalScale(std.y())); 104 105 return calculateUnscaledKernelSize(stdError); 106 } 107 108 FloatRect FEGaussianBlur::mapRect(const FloatRect& rect, bool) 109 { 110 FloatRect result = rect; 111 IntSize kernelSize = calculateKernelSize(filter(), FloatPoint(m_stdX, m_stdY)); 112 113 // We take the half kernel size and multiply it with three, because we run box blur three times. 114 result.inflateX(3 * kernelSize.width() * 0.5f); 115 result.inflateY(3 * kernelSize.height() * 0.5f); 116 return result; 117 } 118 119 FloatRect FEGaussianBlur::determineAbsolutePaintRect(const FloatRect& originalRequestedRect) 120 { 121 FloatRect requestedRect = originalRequestedRect; 122 if (clipsToBounds()) 123 requestedRect.intersect(maxEffectRect()); 124 125 FilterEffect* input = inputEffect(0); 126 FloatRect inputRect = input->determineAbsolutePaintRect(mapRect(requestedRect, false)); 127 FloatRect outputRect = mapRect(inputRect, true); 128 outputRect.intersect(requestedRect); 129 addAbsolutePaintRect(outputRect); 130 131 // Blur needs space for both input and output pixels in the paint area. 132 // Input is also clipped to subregion. 133 if (clipsToBounds()) 134 inputRect.intersect(maxEffectRect()); 135 addAbsolutePaintRect(inputRect); 136 return outputRect; 137 } 138 139 void FEGaussianBlur::applySoftware() 140 { 141 ImageBuffer* resultImage = createImageBufferResult(); 142 if (!resultImage) 143 return; 144 145 FilterEffect* in = inputEffect(0); 146 147 IntRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect()); 148 149 setIsAlphaImage(in->isAlphaImage()); 150 151 float stdX = filter()->applyHorizontalScale(m_stdX); 152 float stdY = filter()->applyVerticalScale(m_stdY); 153 154 RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore); 155 156 SkPaint paint; 157 GraphicsContext* dstContext = resultImage->context(); 158 paint.setImageFilter(SkBlurImageFilter::Create(stdX, stdY))->unref(); 159 160 SkRect bounds = SkRect::MakeWH(absolutePaintRect().width(), absolutePaintRect().height()); 161 dstContext->saveLayer(&bounds, &paint); 162 paint.setColor(0xFFFFFFFF); 163 dstContext->drawImage(image.get(), drawingRegion.location(), CompositeCopy); 164 dstContext->restoreLayer(); 165 } 166 167 PassRefPtr<SkImageFilter> FEGaussianBlur::createImageFilter(SkiaImageFilterBuilder* builder) 168 { 169 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace())); 170 float stdX = filter()->applyHorizontalScale(m_stdX); 171 float stdY = filter()->applyVerticalScale(m_stdY); 172 SkImageFilter::CropRect rect = getCropRect(builder->cropOffset()); 173 return adoptRef(SkBlurImageFilter::Create(SkFloatToScalar(stdX), SkFloatToScalar(stdY), input.get(), &rect)); 174 } 175 176 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, int indent) const 177 { 178 writeIndent(ts, indent); 179 ts << "[feGaussianBlur"; 180 FilterEffect::externalRepresentation(ts); 181 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n"; 182 inputEffect(0)->externalRepresentation(ts, indent + 1); 183 return ts; 184 } 185 186 } // namespace blink 187