1/*
2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
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/SourceAlpha.h"
23
24#include "platform/graphics/Color.h"
25#include "platform/graphics/GraphicsContext.h"
26#include "platform/graphics/filters/Filter.h"
27#include "platform/graphics/filters/SkiaImageFilterBuilder.h"
28#include "platform/graphics/filters/SourceGraphic.h"
29#include "platform/text/TextStream.h"
30#include "third_party/skia/include/effects/SkColorFilterImageFilter.h"
31#include "third_party/skia/include/effects/SkColorMatrixFilter.h"
32#include "wtf/StdLibExtras.h"
33#include "wtf/text/WTFString.h"
34
35namespace blink {
36
37PassRefPtr<SourceAlpha> SourceAlpha::create(Filter* filter)
38{
39    return adoptRef(new SourceAlpha(filter));
40}
41
42const AtomicString& SourceAlpha::effectName()
43{
44    DEFINE_STATIC_LOCAL(const AtomicString, s_effectName, ("SourceAlpha", AtomicString::ConstructFromLiteral));
45    return s_effectName;
46}
47
48FloatRect SourceAlpha::determineAbsolutePaintRect(const FloatRect& requestedRect)
49{
50    FloatRect srcRect = filter()->sourceImageRect();
51    srcRect.intersect(requestedRect);
52    addAbsolutePaintRect(srcRect);
53    return srcRect;
54}
55
56void SourceAlpha::applySoftware()
57{
58    ImageBuffer* resultImage = createImageBufferResult();
59    Filter* filter = this->filter();
60    if (!resultImage || !filter->sourceImage())
61        return;
62
63    setIsAlphaImage(true);
64
65    FloatRect imageRect(FloatPoint(), absolutePaintRect().size());
66    GraphicsContext* filterContext = resultImage->context();
67    filterContext->fillRect(imageRect, Color::black);
68
69    IntRect srcRect = filter->sourceImageRect();
70    if (ImageBuffer* sourceImageBuffer = filter->sourceImage()) {
71        filterContext->drawImageBuffer(sourceImageBuffer,
72            FloatRect(IntPoint(srcRect.location() - absolutePaintRect().location()), sourceImageBuffer->size()),
73            0, CompositeDestinationIn);
74    }
75}
76
77PassRefPtr<SkImageFilter> SourceAlpha::createImageFilter(SkiaImageFilterBuilder* builder)
78{
79    RefPtr<SkImageFilter> sourceGraphic(builder->build(builder->sourceGraphic(), operatingColorSpace()));
80    SkScalar matrix[20] = {
81        0, 0, 0, 0, 0,
82        0, 0, 0, 0, 0,
83        0, 0, 0, 0, 0,
84        0, 0, 0, SK_Scalar1, 0
85    };
86    RefPtr<SkColorFilter> colorFilter(adoptRef(SkColorMatrixFilter::Create(matrix)));
87    return adoptRef(SkColorFilterImageFilter::Create(colorFilter.get(), sourceGraphic.get()));
88}
89
90TextStream& SourceAlpha::externalRepresentation(TextStream& ts, int indent) const
91{
92    writeIndent(ts, indent);
93    ts << "[SourceAlpha]\n";
94    return ts;
95}
96
97} // namespace blink
98