2 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
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.
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.
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.
22 #include "platform/graphics/filters/FEDropShadow.h"
24 #include "platform/graphics/GraphicsContext.h"
25 #include "platform/graphics/filters/FEGaussianBlur.h"
26 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
27 #include "platform/text/TextStream.h"
28 #include "third_party/skia/include/core/SkColorFilter.h"
29 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
30 #include "third_party/skia/include/effects/SkDropShadowImageFilter.h"
36 FEDropShadow::FEDropShadow(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
37 : FilterEffect(filter)
42 , m_shadowColor(shadowColor)
43 , m_shadowOpacity(shadowOpacity)
47 PassRefPtr<FEDropShadow> FEDropShadow::create(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
49 return adoptRef(new FEDropShadow(filter, stdX, stdY, dx, dy, shadowColor, shadowOpacity));
52 FloatRect FEDropShadow::mapRect(const FloatRect& rect, bool forward)
54 FloatRect result = rect;
55 Filter* filter = this->filter();
58 FloatRect offsetRect = rect;
60 offsetRect.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
62 offsetRect.move(-filter->applyHorizontalScale(m_dx), -filter->applyVerticalScale(m_dy));
63 result.unite(offsetRect);
65 IntSize kernelSize = FEGaussianBlur::calculateKernelSize(filter, FloatPoint(m_stdX, m_stdY));
67 // We take the half kernel size and multiply it with three, because we run box blur three times.
68 result.inflateX(3 * kernelSize.width() * 0.5f);
69 result.inflateY(3 * kernelSize.height() * 0.5f);
73 void FEDropShadow::applySoftware()
75 FilterEffect* in = inputEffect(0);
77 ImageBuffer* resultImage = createImageBufferResult();
81 Filter* filter = this->filter();
82 FloatSize blurRadius(filter->applyHorizontalScale(m_stdX), filter->applyVerticalScale(m_stdY));
83 FloatSize offset(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
85 FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
86 GraphicsContext* resultContext = resultImage->context();
87 ASSERT(resultContext);
89 Color color = adaptColorToOperatingColorSpace(m_shadowColor.combineWithAlpha(m_shadowOpacity));
90 SkAutoTUnref<SkImageFilter> blurFilter(SkBlurImageFilter::Create(blurRadius.width(), blurRadius.height()));
91 SkAutoTUnref<SkColorFilter> colorFilter(SkColorFilter::CreateModeFilter(color.rgb(), SkXfermode::kSrcIn_Mode));
93 paint.setImageFilter(blurFilter.get());
94 paint.setColorFilter(colorFilter.get());
95 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
96 RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore);
98 RefPtr<NativeImageSkia> nativeImage = image->nativeImageForCurrentFrame();
103 resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x() + offset.width(), drawingRegion.y() + offset.height(), &paint);
104 resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x(), drawingRegion.y());
107 PassRefPtr<SkImageFilter> FEDropShadow::createImageFilter(SkiaImageFilterBuilder* builder)
109 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
110 float dx = filter()->applyHorizontalScale(m_dx);
111 float dy = filter()->applyVerticalScale(m_dy);
112 float stdX = filter()->applyHorizontalScale(m_stdX);
113 float stdY = filter()->applyVerticalScale(m_stdY);
114 Color color = adaptColorToOperatingColorSpace(m_shadowColor.combineWithAlpha(m_shadowOpacity));
115 SkImageFilter::CropRect cropRect = getCropRect(builder->cropOffset());
116 return adoptRef(SkDropShadowImageFilter::Create(SkFloatToScalar(dx), SkFloatToScalar(dy), SkFloatToScalar(stdX), SkFloatToScalar(stdY), color.rgb(), input.get(), &cropRect));
120 TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const
122 writeIndent(ts, indent);
123 ts << "[feDropShadow";
124 FilterEffect::externalRepresentation(ts);
125 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\" dx=\"" << m_dx << "\" dy=\"" << m_dy << "\" flood-color=\"" << m_shadowColor.nameForRenderTreeAsText() <<"\" flood-opacity=\"" << m_shadowOpacity << "]\n";
126 inputEffect(0)->externalRepresentation(ts, indent + 1);
130 } // namespace WebCore