Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / filters / FEDropShadow.cpp
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
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
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
52 FloatRect FEDropShadow::mapRect(const FloatRect& rect, bool forward)
53 {
54     FloatRect result = rect;
55     Filter* filter = this->filter();
56     ASSERT(filter);
57
58     FloatRect offsetRect = rect;
59     if (forward)
60         offsetRect.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
61     else
62         offsetRect.move(-filter->applyHorizontalScale(m_dx), -filter->applyVerticalScale(m_dy));
63     result.unite(offsetRect);
64
65     IntSize kernelSize = FEGaussianBlur::calculateKernelSize(filter, FloatPoint(m_stdX, m_stdY));
66
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);
70     return result;
71 }
72
73 void FEDropShadow::applySoftware()
74 {
75     FilterEffect* in = inputEffect(0);
76
77     ImageBuffer* resultImage = createImageBufferResult();
78     if (!resultImage)
79         return;
80
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));
84
85     FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
86     GraphicsContext* resultContext = resultImage->context();
87     ASSERT(resultContext);
88
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));
92     SkPaint paint;
93     paint.setImageFilter(blurFilter.get());
94     paint.setColorFilter(colorFilter.get());
95     paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
96     RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore);
97
98     RefPtr<NativeImageSkia> nativeImage = image->nativeImageForCurrentFrame();
99
100     if (!nativeImage)
101         return;
102
103     resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x() + offset.width(), drawingRegion.y() + offset.height(), &paint);
104     resultContext->drawBitmap(nativeImage->bitmap(), drawingRegion.x(), drawingRegion.y());
105 }
106
107 PassRefPtr<SkImageFilter> FEDropShadow::createImageFilter(SkiaImageFilterBuilder* builder)
108 {
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));
117 }
118
119
120 TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const
121 {
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);
127     return ts;
128 }
129
130 } // namespace WebCore