• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "platform/graphics/filters/FEDropShadow.h"
23 
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"
31 
32 using namespace std;
33 
34 namespace WebCore {
35 
FEDropShadow(Filter * filter,float stdX,float stdY,float dx,float dy,const Color & shadowColor,float shadowOpacity)36 FEDropShadow::FEDropShadow(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
37     : FilterEffect(filter)
38     , m_stdX(stdX)
39     , m_stdY(stdY)
40     , m_dx(dx)
41     , m_dy(dy)
42     , m_shadowColor(shadowColor)
43     , m_shadowOpacity(shadowOpacity)
44 {
45 }
46 
create(Filter * filter,float stdX,float stdY,float dx,float dy,const Color & shadowColor,float shadowOpacity)47 PassRefPtr<FEDropShadow> FEDropShadow::create(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
48 {
49     return adoptRef(new FEDropShadow(filter, stdX, stdY, dx, dy, shadowColor, shadowOpacity));
50 }
51 
determineAbsolutePaintRect()52 void FEDropShadow::determineAbsolutePaintRect()
53 {
54     Filter* filter = this->filter();
55     ASSERT_UNUSED(filter, filter);
56 
57     FloatRect absolutePaintRect = mapRect(inputEffect(0)->absolutePaintRect());
58 
59     if (clipsToBounds())
60         absolutePaintRect.intersect(maxEffectRect());
61     else
62         absolutePaintRect.unite(maxEffectRect());
63 
64     setAbsolutePaintRect(enclosingIntRect(absolutePaintRect));
65 }
66 
mapRect(const FloatRect & rect,bool forward)67 FloatRect FEDropShadow::mapRect(const FloatRect& rect, bool forward)
68 {
69     FloatRect result = rect;
70     Filter* filter = this->filter();
71     ASSERT(filter);
72 
73     FloatRect offsetRect = rect;
74     if (forward)
75         offsetRect.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
76     else
77         offsetRect.move(-filter->applyHorizontalScale(m_dx), -filter->applyVerticalScale(m_dy));
78     result.unite(offsetRect);
79 
80     unsigned kernelSizeX = 0;
81     unsigned kernelSizeY = 0;
82     FEGaussianBlur::calculateKernelSize(filter, kernelSizeX, kernelSizeY, m_stdX, m_stdY);
83 
84     // We take the half kernel size and multiply it with three, because we run box blur three times.
85     result.inflateX(3 * kernelSizeX * 0.5f);
86     result.inflateY(3 * kernelSizeY * 0.5f);
87     return result;
88 }
89 
applySoftware()90 void FEDropShadow::applySoftware()
91 {
92     FilterEffect* in = inputEffect(0);
93 
94     ImageBuffer* resultImage = createImageBufferResult();
95     if (!resultImage)
96         return;
97 
98     Filter* filter = this->filter();
99     FloatSize blurRadius(filter->applyHorizontalScale(m_stdX), filter->applyVerticalScale(m_stdY));
100     FloatSize offset(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
101 
102     FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
103     GraphicsContext* resultContext = resultImage->context();
104     ASSERT(resultContext);
105 
106     SkAutoTUnref<SkImageFilter> blurFilter(new SkBlurImageFilter(blurRadius.width(), blurRadius.height()));
107     SkAutoTUnref<SkColorFilter> colorFilter(SkColorFilter::CreateModeFilter(m_shadowColor.rgb(), SkXfermode::kSrcIn_Mode));
108     SkPaint paint;
109     paint.setImageFilter(blurFilter.get());
110     paint.setColorFilter(colorFilter.get());
111     paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
112     RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore);
113 
114     RefPtr<NativeImageSkia> nativeImage = image->nativeImageForCurrentFrame();
115 
116     if (!nativeImage)
117         return;
118 
119     resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x() + offset.width(), drawingRegion.y() + offset.height(), &paint);
120     resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x(), drawingRegion.y());
121 }
122 
createImageFilter(SkiaImageFilterBuilder * builder)123 PassRefPtr<SkImageFilter> FEDropShadow::createImageFilter(SkiaImageFilterBuilder* builder)
124 {
125     RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
126     float dx = filter()->applyHorizontalScale(m_dx);
127     float dy = filter()->applyVerticalScale(m_dy);
128     float stdX = filter()->applyHorizontalScale(m_stdX);
129     float stdY = filter()->applyHorizontalScale(m_stdY);
130     SkImageFilter::CropRect cropRect = getCropRect(builder->cropOffset());
131     return adoptRef(new SkDropShadowImageFilter(SkFloatToScalar(dx), SkFloatToScalar(dy), SkFloatToScalar(stdX), SkFloatToScalar(stdY), m_shadowColor.rgb(), input.get(), &cropRect));
132 }
133 
134 
externalRepresentation(TextStream & ts,int indent) const135 TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const
136 {
137     writeIndent(ts, indent);
138     ts << "[feDropShadow";
139     FilterEffect::externalRepresentation(ts);
140     ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\" dx=\"" << m_dx << "\" dy=\"" << m_dy << "\" flood-color=\"" << m_shadowColor.nameForRenderTreeAsText() <<"\" flood-opacity=\"" << m_shadowOpacity << "]\n";
141     inputEffect(0)->externalRepresentation(ts, indent + 1);
142     return ts;
143 }
144 
145 } // namespace WebCore
146