Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2004, 2005 Rob Buis <buis (at) kde.org>
      4  * Copyright (C) 2005 Eric Seidel <eric (at) webkit.org>
      5  * Copyright (C) 2009 Dirk Schulze <krit (at) 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 
     26 #include "core/platform/graphics/filters/FEFlood.h"
     27 
     28 #include "SkFlattenableBuffers.h"
     29 #include "SkImageFilter.h"
     30 #include "core/platform/graphics/GraphicsContext.h"
     31 #include "core/platform/graphics/filters/Filter.h"
     32 #include "core/platform/text/TextStream.h"
     33 #include "core/rendering/RenderTreeAsText.h"
     34 #include "third_party/skia/include/core/SkDevice.h"
     35 
     36 namespace {
     37 
     38 class FloodImageFilter : public SkImageFilter {
     39 public:
     40     FloodImageFilter(const SkColor& color)
     41         : SkImageFilter(0, 0)
     42         , m_color(color)
     43     {
     44     }
     45     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FloodImageFilter)
     46 
     47     FloodImageFilter(SkFlattenableReadBuffer& buffer) : SkImageFilter(buffer)
     48     {
     49         m_color = buffer.readColor();
     50     }
     51 
     52     virtual void flatten(SkFlattenableWriteBuffer& buffer) const
     53     {
     54         buffer.writeColor(m_color);
     55     }
     56 
     57     virtual bool onFilterImage(Proxy* proxy, const SkBitmap& src, const SkMatrix&, SkBitmap* result, SkIPoint*)
     58     {
     59         if (!src.width() || !src.height())
     60             return false;
     61 
     62         SkAutoTUnref<SkDevice> device(proxy->createDevice(src.width(), src.height()));
     63         SkCanvas canvas(device.get());
     64         SkPaint paint;
     65         paint.setColor(m_color);
     66         canvas.drawRect(SkRect::MakeWH(src.width(), src.height()), paint);
     67         *result = device->accessBitmap(false);
     68         return true;
     69     }
     70 private:
     71     SkColor m_color;
     72 };
     73 
     74 }; // unnamed namespace
     75 
     76 namespace WebCore {
     77 
     78 FEFlood::FEFlood(Filter* filter, const Color& floodColor, float floodOpacity)
     79     : FilterEffect(filter)
     80     , m_floodColor(floodColor)
     81     , m_floodOpacity(floodOpacity)
     82 {
     83     FilterEffect::setOperatingColorSpace(ColorSpaceDeviceRGB);
     84     FilterEffect::setResultColorSpace(ColorSpaceDeviceRGB);
     85 }
     86 
     87 PassRefPtr<FEFlood> FEFlood::create(Filter* filter, const Color& floodColor, float floodOpacity)
     88 {
     89     return adoptRef(new FEFlood(filter, floodColor, floodOpacity));
     90 }
     91 
     92 Color FEFlood::floodColor() const
     93 {
     94     return m_floodColor;
     95 }
     96 
     97 bool FEFlood::setFloodColor(const Color& color)
     98 {
     99     if (m_floodColor == color)
    100         return false;
    101     m_floodColor = color;
    102     return true;
    103 }
    104 
    105 float FEFlood::floodOpacity() const
    106 {
    107     return m_floodOpacity;
    108 }
    109 
    110 bool FEFlood::setFloodOpacity(float floodOpacity)
    111 {
    112     if (m_floodOpacity == floodOpacity)
    113         return false;
    114     m_floodOpacity = floodOpacity;
    115     return true;
    116 }
    117 
    118 void FEFlood::applySoftware()
    119 {
    120     ImageBuffer* resultImage = createImageBufferResult();
    121     if (!resultImage)
    122         return;
    123 
    124     Color color = colorWithOverrideAlpha(floodColor().rgb(), floodOpacity());
    125     resultImage->context()->fillRect(FloatRect(FloatPoint(), absolutePaintRect().size()), color);
    126 }
    127 
    128 PassRefPtr<SkImageFilter> FEFlood::createImageFilter(SkiaImageFilterBuilder* builder)
    129 {
    130     Color color = colorWithOverrideAlpha(floodColor().rgb(), floodOpacity());
    131     return adoptRef(new FloodImageFilter(color.rgb()));
    132 }
    133 
    134 TextStream& FEFlood::externalRepresentation(TextStream& ts, int indent) const
    135 {
    136     writeIndent(ts, indent);
    137     ts << "[feFlood";
    138     FilterEffect::externalRepresentation(ts);
    139     ts << " flood-color=\"" << floodColor().nameForRenderTreeAsText() << "\" "
    140        << "flood-opacity=\"" << floodOpacity() << "\"]\n";
    141     return ts;
    142 }
    143 
    144 } // namespace WebCore
    145