initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / filters / FEDropShadow.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #if ENABLE(FILTERS)
23 #include "FEDropShadow.h"
24
25 #include "ColorSpace.h"
26 #include "FEGaussianBlur.h"
27 #include "Filter.h"
28 #include "GraphicsContext.h"
29 #include "RenderTreeAsText.h"
30 #include "ShadowBlur.h"
31 #include "TextStream.h"
32 #include <wtf/ByteArray.h>
33 #include <wtf/MathExtras.h>
34
35 using namespace std;
36
37 namespace WebCore {
38     
39 FEDropShadow::FEDropShadow(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
40     : FilterEffect(filter)
41     , m_stdX(stdX)
42     , m_stdY(stdY)
43     , m_dx(dx)
44     , m_dy(dy)
45     , m_shadowColor(shadowColor)
46     , m_shadowOpacity(shadowOpacity)
47 {
48 }
49
50 PassRefPtr<FEDropShadow> FEDropShadow::create(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
51 {
52     return adoptRef(new FEDropShadow(filter, stdX, stdY, dx, dy, shadowColor, shadowOpacity));
53 }
54
55 void FEDropShadow::determineAbsolutePaintRect()
56 {
57     Filter* filter = this->filter();
58     ASSERT(filter);
59
60     FloatRect absolutePaintRect = inputEffect(0)->absolutePaintRect();
61     FloatRect absoluteOffsetPaintRect(absolutePaintRect);
62     absoluteOffsetPaintRect.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
63     absolutePaintRect.unite(absoluteOffsetPaintRect);
64     absolutePaintRect.intersect(maxEffectRect());
65     
66     unsigned kernelSizeX = 0;
67     unsigned kernelSizeY = 0;
68     FEGaussianBlur::calculateKernelSize(filter, kernelSizeX, kernelSizeY, m_stdX, m_stdY);
69     
70     // We take the half kernel size and multiply it with three, because we run box blur three times.
71     absolutePaintRect.inflateX(3 * kernelSizeX * 0.5f);
72     absolutePaintRect.inflateY(3 * kernelSizeY * 0.5f);
73     setAbsolutePaintRect(enclosingIntRect(absolutePaintRect));
74 }
75
76 void FEDropShadow::apply()
77 {
78     if (hasResult())
79         return;
80
81     FilterEffect* in = inputEffect(0);
82     in->apply();
83     if (!in->hasResult())
84         return;
85
86     ImageBuffer* resultImage = createImageBufferResult();
87     if (!resultImage)
88         return;
89
90     Filter* filter = this->filter();
91     FloatSize blurRadius(filter->applyHorizontalScale(m_stdX), filter->applyVerticalScale(m_stdY));
92     FloatSize offset(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
93
94     FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
95     FloatRect drawingRegionWithOffset(drawingRegion);
96     drawingRegionWithOffset.move(offset);
97
98     ImageBuffer* sourceImage = in->asImageBuffer();
99     ASSERT(sourceImage);
100     GraphicsContext* resultContext = resultImage->context();
101     ASSERT(resultContext);
102     resultContext->setAlpha(m_shadowOpacity);
103     resultContext->drawImageBuffer(sourceImage, ColorSpaceDeviceRGB, drawingRegionWithOffset);
104     resultContext->setAlpha(1);
105
106     ShadowBlur contextShadow(blurRadius, offset, m_shadowColor, ColorSpaceDeviceRGB);
107
108     // TODO: Direct pixel access to ImageBuffer would avoid copying the ImageData.
109     IntRect shadowArea(IntPoint(), resultImage->size());
110     RefPtr<ByteArray> srcPixelArray = resultImage->getPremultipliedImageData(shadowArea);
111
112     contextShadow.blurLayerImage(srcPixelArray->data(), shadowArea.size(), 4 * shadowArea.size().width());
113
114     resultImage->putPremultipliedImageData(srcPixelArray.get(), shadowArea.size(), shadowArea, IntPoint());
115
116     resultContext->setCompositeOperation(CompositeSourceIn);
117     resultContext->fillRect(FloatRect(FloatPoint(), absolutePaintRect().size()), m_shadowColor, ColorSpaceDeviceRGB);
118     resultContext->setCompositeOperation(CompositeDestinationOver);
119
120     resultImage->context()->drawImageBuffer(sourceImage, ColorSpaceDeviceRGB, drawingRegion);
121 }
122
123 void FEDropShadow::dump()
124 {
125 }
126
127 TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const
128 {
129     writeIndent(ts, indent);
130     ts << "[feDropShadow";
131     FilterEffect::externalRepresentation(ts);
132     ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\" dx=\"" << m_dx << "\" dy=\"" << m_dy << "\" flood-color=\"" << m_shadowColor.nameForRenderTreeAsText() <<"\" flood-opacity=\"" << m_shadowOpacity << "]\n";
133     inputEffect(0)->externalRepresentation(ts, indent + 1);
134     return ts;
135 }
136     
137 } // namespace WebCore
138
139 #endif // ENABLE(FILTERS)