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 "SkFlattenableBuffers.h"
29#include "SkOffsetImageFilter.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
35namespace WebCore {
36
37FEOffset::FEOffset(Filter* filter, float dx, float dy)
38    : FilterEffect(filter)
39    , m_dx(dx)
40    , m_dy(dy)
41{
42}
43
44PassRefPtr<FEOffset> FEOffset::create(Filter* filter, float dx, float dy)
45{
46    return adoptRef(new FEOffset(filter, dx, dy));
47}
48
49float FEOffset::dx() const
50{
51    return m_dx;
52}
53
54void FEOffset::setDx(float dx)
55{
56    m_dx = dx;
57}
58
59float FEOffset::dy() const
60{
61    return m_dy;
62}
63
64void FEOffset::setDy(float dy)
65{
66    m_dy = dy;
67}
68
69FloatRect FEOffset::mapRect(const FloatRect& rect, bool forward)
70{
71    FloatRect result = rect;
72    if (forward)
73        result.move(filter()->applyHorizontalScale(m_dx), filter()->applyVerticalScale(m_dy));
74    else
75        result.move(-filter()->applyHorizontalScale(m_dx), -filter()->applyVerticalScale(m_dy));
76    return result;
77}
78
79void FEOffset::applySoftware()
80{
81    FilterEffect* in = inputEffect(0);
82
83    ImageBuffer* resultImage = createImageBufferResult();
84    if (!resultImage)
85        return;
86
87    setIsAlphaImage(in->isAlphaImage());
88
89    FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
90    Filter* filter = this->filter();
91    drawingRegion.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
92    resultImage->context()->drawImageBuffer(in->asImageBuffer(), drawingRegion);
93}
94
95PassRefPtr<SkImageFilter> FEOffset::createImageFilter(SkiaImageFilterBuilder* builder)
96{
97    RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
98    Filter* filter = this->filter();
99    SkImageFilter::CropRect cropRect = getCropRect(builder->cropOffset());
100    return adoptRef(SkOffsetImageFilter::Create(SkFloatToScalar(filter->applyHorizontalScale(m_dx)), SkFloatToScalar(filter->applyVerticalScale(m_dy)), input.get(), &cropRect));
101}
102
103TextStream& FEOffset::externalRepresentation(TextStream& ts, int indent) const
104{
105    writeIndent(ts, indent);
106    ts << "[feOffset";
107    FilterEffect::externalRepresentation(ts);
108    ts << " dx=\"" << dx() << "\" dy=\"" << dy() << "\"]\n";
109    inputEffect(0)->externalRepresentation(ts, indent + 1);
110    return ts;
111}
112
113} // namespace WebCore
114