Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / filters / FEFlood.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6  * Copyright (C) 2013 Google Inc. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "platform/graphics/filters/FEFlood.h"
26
27 #include "SkColorFilter.h"
28 #include "SkColorFilterImageFilter.h"
29 #include "SkFlattenableBuffers.h"
30 #include "platform/graphics/GraphicsContext.h"
31 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
32 #include "platform/text/TextStream.h"
33 #include "third_party/skia/include/core/SkDevice.h"
34
35 namespace blink {
36
37 FEFlood::FEFlood(Filter* filter, const Color& floodColor, float floodOpacity)
38     : FilterEffect(filter)
39     , m_floodColor(floodColor)
40     , m_floodOpacity(floodOpacity)
41 {
42     FilterEffect::setOperatingColorSpace(ColorSpaceDeviceRGB);
43 }
44
45 PassRefPtr<FEFlood> FEFlood::create(Filter* filter, const Color& floodColor, float floodOpacity)
46 {
47     return adoptRef(new FEFlood(filter, floodColor, floodOpacity));
48 }
49
50 Color FEFlood::floodColor() const
51 {
52     return m_floodColor;
53 }
54
55 bool FEFlood::setFloodColor(const Color& color)
56 {
57     if (m_floodColor == color)
58         return false;
59     m_floodColor = color;
60     return true;
61 }
62
63 float FEFlood::floodOpacity() const
64 {
65     return m_floodOpacity;
66 }
67
68 bool FEFlood::setFloodOpacity(float floodOpacity)
69 {
70     if (m_floodOpacity == floodOpacity)
71         return false;
72     m_floodOpacity = floodOpacity;
73     return true;
74 }
75
76 void FEFlood::applySoftware()
77 {
78     ImageBuffer* resultImage = createImageBufferResult();
79     if (!resultImage)
80         return;
81
82     Color color = floodColor().combineWithAlpha(floodOpacity());
83     resultImage->context()->fillRect(FloatRect(FloatPoint(), absolutePaintRect().size()), color);
84     FilterEffect::setResultColorSpace(ColorSpaceDeviceRGB);
85 }
86
87 PassRefPtr<SkImageFilter> FEFlood::createImageFilter(SkiaImageFilterBuilder* builder)
88 {
89     Color color = floodColor().combineWithAlpha(floodOpacity());
90
91     SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
92     SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(color.rgb(), SkXfermode::kSrc_Mode));
93     return adoptRef(SkColorFilterImageFilter::Create(cf, 0, &rect));
94 }
95
96 TextStream& FEFlood::externalRepresentation(TextStream& ts, int indent) const
97 {
98     writeIndent(ts, indent);
99     ts << "[feFlood";
100     FilterEffect::externalRepresentation(ts);
101     ts << " flood-color=\"" << floodColor().nameForRenderTreeAsText() << "\" "
102        << "flood-opacity=\"" << floodOpacity() << "\"]\n";
103     return ts;
104 }
105
106 } // namespace blink