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) Research In Motion Limited 2010. All rights reserved.
7 * Copyright (C) 2013 Google Inc. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#include "config.h"
26#include "platform/graphics/filters/FEOffset.h"
27
28#include "SkOffsetImageFilter.h"
29#include "platform/graphics/GraphicsContext.h"
30#include "platform/graphics/filters/SkiaImageFilterBuilder.h"
31#include "platform/text/TextStream.h"
32#include "third_party/skia/include/core/SkDevice.h"
33
34namespace blink {
35
36FEOffset::FEOffset(Filter* filter, float dx, float dy)
37    : FilterEffect(filter)
38    , m_dx(dx)
39    , m_dy(dy)
40{
41}
42
43PassRefPtr<FEOffset> FEOffset::create(Filter* filter, float dx, float dy)
44{
45    return adoptRef(new FEOffset(filter, dx, dy));
46}
47
48float FEOffset::dx() const
49{
50    return m_dx;
51}
52
53void FEOffset::setDx(float dx)
54{
55    m_dx = dx;
56}
57
58float FEOffset::dy() const
59{
60    return m_dy;
61}
62
63void FEOffset::setDy(float dy)
64{
65    m_dy = dy;
66}
67
68FloatRect FEOffset::mapRect(const FloatRect& rect, bool forward)
69{
70    FloatRect result = rect;
71    if (forward)
72        result.move(filter()->applyHorizontalScale(m_dx), filter()->applyVerticalScale(m_dy));
73    else
74        result.move(-filter()->applyHorizontalScale(m_dx), -filter()->applyVerticalScale(m_dy));
75    return result;
76}
77
78void FEOffset::applySoftware()
79{
80    FilterEffect* in = inputEffect(0);
81
82    ImageBuffer* resultImage = createImageBufferResult();
83    if (!resultImage)
84        return;
85
86    setIsAlphaImage(in->isAlphaImage());
87
88    FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
89    Filter* filter = this->filter();
90    drawingRegion.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
91    resultImage->context()->drawImageBuffer(in->asImageBuffer(), drawingRegion);
92}
93
94PassRefPtr<SkImageFilter> FEOffset::createImageFilter(SkiaImageFilterBuilder* builder)
95{
96    RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
97    Filter* filter = this->filter();
98    SkImageFilter::CropRect cropRect = getCropRect(builder->cropOffset());
99    return adoptRef(SkOffsetImageFilter::Create(SkFloatToScalar(filter->applyHorizontalScale(m_dx)), SkFloatToScalar(filter->applyVerticalScale(m_dy)), input.get(), &cropRect));
100}
101
102TextStream& FEOffset::externalRepresentation(TextStream& ts, int indent) const
103{
104    writeIndent(ts, indent);
105    ts << "[feOffset";
106    FilterEffect::externalRepresentation(ts);
107    ts << " dx=\"" << dx() << "\" dy=\"" << dy() << "\"]\n";
108    inputEffect(0)->externalRepresentation(ts, indent + 1);
109    return ts;
110}
111
112} // namespace blink
113