2 * Copyright (C) 2008 Alex Mathews <possessedpenguinbob@gmail.com>
3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * Copyright (C) 2013 Google Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "platform/graphics/filters/FETile.h"
25 #include "SkFlattenableBuffers.h"
26 #include "SkTileImageFilter.h"
28 #include "platform/graphics/GraphicsContext.h"
29 #include "platform/graphics/Pattern.h"
30 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
31 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
32 #include "platform/text/TextStream.h"
33 #include "platform/transforms/AffineTransform.h"
34 #include "third_party/skia/include/core/SkDevice.h"
38 FETile::FETile(Filter* filter)
39 : FilterEffect(filter)
43 PassRefPtr<FETile> FETile::create(Filter* filter)
45 return adoptRef(new FETile(filter));
48 FloatRect FETile::mapPaintRect(const FloatRect& rect, bool forward)
50 return forward ? maxEffectRect() : inputEffect(0)->maxEffectRect();
53 void FETile::applySoftware()
55 FilterEffect* in = inputEffect(0);
57 ImageBuffer* resultImage = createImageBufferResult();
61 setIsAlphaImage(in->isAlphaImage());
63 // Source input needs more attention. It has the size of the filterRegion but gives the
64 // size of the cutted sourceImage back. This is part of the specification and optimization.
65 FloatRect tileRect = in->maxEffectRect();
66 FloatPoint inMaxEffectLocation = tileRect.location();
67 FloatPoint maxEffectLocation = maxEffectRect().location();
68 if (in->filterEffectType() == FilterEffectTypeSourceInput) {
69 Filter* filter = this->filter();
70 tileRect = filter->absoluteFilterRegion();
73 OwnPtr<ImageBufferSurface> surface;
74 IntSize intTileSize = roundedIntSize(tileRect.size());
75 surface = adoptPtr(new UnacceleratedImageBufferSurface(intTileSize));
76 OwnPtr<ImageBuffer> tileImage = ImageBuffer::create(surface.release());
80 GraphicsContext* tileImageContext = tileImage->context();
81 tileImageContext->scale(intTileSize.width() / tileRect.width(), intTileSize.height() / tileRect.height());
82 tileImageContext->translate(-inMaxEffectLocation.x(), -inMaxEffectLocation.y());
84 if (ImageBuffer* tileImageBuffer = in->asImageBuffer())
85 tileImageContext->drawImageBuffer(tileImageBuffer, IntRect(in->absolutePaintRect().location(), tileImageBuffer->size()));
87 RefPtr<Pattern> pattern = Pattern::createBitmapPattern(tileImage->copyImage(CopyBackingStore));
89 AffineTransform patternTransform;
90 patternTransform.translate(inMaxEffectLocation.x() - maxEffectLocation.x(), inMaxEffectLocation.y() - maxEffectLocation.y());
91 pattern->setPatternSpaceTransform(patternTransform);
92 GraphicsContext* filterContext = resultImage->context();
93 filterContext->setFillPattern(pattern);
94 filterContext->fillRect(FloatRect(FloatPoint(), absolutePaintRect().size()));
97 static FloatRect getRect(FilterEffect* effect)
99 FloatRect result = effect->filter()->filterRegion();
100 FloatRect boundaries = effect->effectBoundaries();
102 result.setX(boundaries.x());
104 result.setY(boundaries.y());
105 if (effect->hasWidth())
106 result.setWidth(boundaries.width());
107 if (effect->hasHeight())
108 result.setHeight(boundaries.height());
112 PassRefPtr<SkImageFilter> FETile::createImageFilter(SkiaImageFilterBuilder* builder)
114 RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
115 FloatRect srcRect = inputEffect(0) ? getRect(inputEffect(0)) : filter()->filterRegion();
116 FloatRect dstRect = getRect(this);
117 return adoptRef(SkTileImageFilter::Create(srcRect, dstRect, input.get()));
120 TextStream& FETile::externalRepresentation(TextStream& ts, int indent) const
122 writeIndent(ts, indent);
124 FilterEffect::externalRepresentation(ts);
126 inputEffect(0)->externalRepresentation(ts, indent + 1);