2 * Copyright (C) 2008 Alex Mathews <possessedpenguinbob@gmail.com>
3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
5 * Copyright (C) 2012 University of Szeged
6 * Copyright (C) 2013 Google Inc. All rights reserved.
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.
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.
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.
26 #include "platform/graphics/filters/FilterEffect.h"
28 #include "platform/graphics/ImageBuffer.h"
29 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
30 #include "platform/graphics/filters/Filter.h"
32 #if HAVE(ARM_NEON_INTRINSICS)
38 static const float kMaxFilterArea = 4096 * 4096;
40 FilterEffect::FilterEffect(Filter* filter)
47 , m_clipsToBounds(true)
48 , m_operatingColorSpace(ColorSpaceLinearRGB)
49 , m_resultColorSpace(ColorSpaceDeviceRGB)
54 FilterEffect::~FilterEffect()
58 float FilterEffect::maxFilterArea()
60 return kMaxFilterArea;
63 bool FilterEffect::isFilterSizeValid(const FloatRect& rect)
65 if (rect.width() < 0 || rect.height() < 0
66 || (rect.height() * rect.width() > kMaxFilterArea))
72 FloatRect FilterEffect::determineAbsolutePaintRect(const FloatRect& originalRequestedRect)
74 FloatRect requestedRect = originalRequestedRect;
75 // Filters in SVG clip to primitive subregion, while CSS doesn't.
77 requestedRect.intersect(maxEffectRect());
79 // We may be called multiple times if result is used more than once. Return
80 // quickly if if nothing new is required.
81 if (absolutePaintRect().contains(enclosingIntRect(requestedRect)))
84 FloatRect inputRect = mapPaintRect(requestedRect, false);
86 unsigned size = m_inputEffects.size();
88 for (unsigned i = 0; i < size; ++i)
89 inputUnion.unite(m_inputEffects.at(i)->determineAbsolutePaintRect(inputRect));
90 inputUnion = mapPaintRect(inputUnion, true);
92 if (affectsTransparentPixels() || !size) {
93 inputUnion = requestedRect;
95 // Rect may have inflated. Re-intersect with request.
96 inputUnion.intersect(requestedRect);
99 addAbsolutePaintRect(inputUnion);
103 FloatRect FilterEffect::mapRectRecursive(const FloatRect& rect)
106 if (m_inputEffects.size() > 0) {
107 result = m_inputEffects.at(0)->mapRectRecursive(rect);
108 for (unsigned i = 1; i < m_inputEffects.size(); ++i)
109 result.unite(m_inputEffects.at(i)->mapRectRecursive(rect));
112 return mapRect(result);
115 FloatRect FilterEffect::getSourceRect(const FloatRect& destRect, const FloatRect& destClipRect)
117 FloatRect sourceRect = mapRect(destRect, false);
118 FloatRect sourceClipRect = mapRect(destClipRect, false);
120 FloatRect boundaries = filter()->mapLocalRectToAbsoluteRect(effectBoundaries());
122 sourceClipRect.setX(boundaries.x());
124 sourceClipRect.setY(boundaries.y());
126 sourceClipRect.setWidth(boundaries.width());
128 sourceClipRect.setHeight(boundaries.height());
131 if (m_inputEffects.size() > 0) {
132 result = m_inputEffects.at(0)->getSourceRect(sourceRect, sourceClipRect);
133 for (unsigned i = 1; i < m_inputEffects.size(); ++i)
134 result.unite(m_inputEffects.at(i)->getSourceRect(sourceRect, sourceClipRect));
137 result.intersect(sourceClipRect);
142 IntRect FilterEffect::requestedRegionOfInputImageData(const IntRect& effectRect) const
145 IntPoint location = m_absolutePaintRect.location();
146 location.moveBy(-effectRect.location());
147 return IntRect(location, m_absolutePaintRect.size());
150 IntRect FilterEffect::drawingRegionOfInputImage(const IntRect& srcRect) const
152 return IntRect(IntPoint(srcRect.x() - m_absolutePaintRect.x(),
153 srcRect.y() - m_absolutePaintRect.y()), srcRect.size());
156 FilterEffect* FilterEffect::inputEffect(unsigned number) const
158 ASSERT_WITH_SECURITY_IMPLICATION(number < m_inputEffects.size());
159 return m_inputEffects.at(number).get();
162 void FilterEffect::addAbsolutePaintRect(const FloatRect& paintRect)
164 IntRect intPaintRect(enclosingIntRect(paintRect));
165 if (m_absolutePaintRect.contains(intPaintRect))
167 intPaintRect.unite(m_absolutePaintRect);
168 // Make sure we are not holding on to a smaller rendering.
170 m_absolutePaintRect = intPaintRect;
173 void FilterEffect::apply()
175 // Recursively determine paint rects first, so that we don't redraw images
176 // if a smaller section is requested first.
177 determineAbsolutePaintRect(maxEffectRect());
181 void FilterEffect::applyRecursive()
185 unsigned size = m_inputEffects.size();
186 for (unsigned i = 0; i < size; ++i) {
187 FilterEffect* in = m_inputEffects.at(i).get();
188 in->applyRecursive();
189 if (!in->hasResult())
192 // Convert input results to the current effect's color space.
193 transformResultColorSpace(in, i);
196 setResultColorSpace(m_operatingColorSpace);
198 if (!isFilterSizeValid(m_absolutePaintRect))
201 if (!mayProduceInvalidPreMultipliedPixels()) {
202 for (unsigned i = 0; i < size; ++i)
203 inputEffect(i)->correctFilterResultIfNeeded();
209 void FilterEffect::forceValidPreMultipliedPixels()
211 // Must operate on pre-multiplied results; other formats cannot have invalid pixels.
212 if (!m_premultipliedImageResult)
215 Uint8ClampedArray* imageArray = m_premultipliedImageResult.get();
216 unsigned char* pixelData = imageArray->data();
217 int pixelArrayLength = imageArray->length();
219 // We must have four bytes per pixel, and complete pixels
220 ASSERT(!(pixelArrayLength % 4));
222 #if HAVE(ARM_NEON_INTRINSICS)
223 if (pixelArrayLength >= 64) {
224 unsigned char* lastPixel = pixelData + (pixelArrayLength & ~0x3f);
226 // Increments pixelData by 64.
227 uint8x16x4_t sixteenPixels = vld4q_u8(pixelData);
228 sixteenPixels.val[0] = vminq_u8(sixteenPixels.val[0], sixteenPixels.val[3]);
229 sixteenPixels.val[1] = vminq_u8(sixteenPixels.val[1], sixteenPixels.val[3]);
230 sixteenPixels.val[2] = vminq_u8(sixteenPixels.val[2], sixteenPixels.val[3]);
231 vst4q_u8(pixelData, sixteenPixels);
233 } while (pixelData < lastPixel);
235 pixelArrayLength &= 0x3f;
236 if (!pixelArrayLength)
241 int numPixels = pixelArrayLength / 4;
243 // Iterate over each pixel, checking alpha and adjusting color components if necessary
244 while (--numPixels >= 0) {
245 // Alpha is the 4th byte in a pixel
246 unsigned char a = *(pixelData + 3);
247 // Clamp each component to alpha, and increment the pixel location
248 for (int i = 0; i < 3; ++i) {
253 // Increment for alpha
258 void FilterEffect::clearResult()
260 if (m_imageBufferResult)
261 m_imageBufferResult.clear();
262 if (m_unmultipliedImageResult)
263 m_unmultipliedImageResult.clear();
264 if (m_premultipliedImageResult)
265 m_premultipliedImageResult.clear();
267 m_absolutePaintRect = IntRect();
268 for (int i = 0; i < 4; i++) {
269 filter()->removeFromCache(m_imageFilters[i].get());
270 m_imageFilters[i] = nullptr;
274 void FilterEffect::clearResultsRecursive()
276 // Clear all results, regardless that the current effect has
277 // a result. Can be used if an effect is in an erroneous state.
281 unsigned size = m_inputEffects.size();
282 for (unsigned i = 0; i < size; ++i)
283 m_inputEffects.at(i).get()->clearResultsRecursive();
286 ImageBuffer* FilterEffect::asImageBuffer()
290 if (m_imageBufferResult)
291 return m_imageBufferResult.get();
292 OwnPtr<ImageBufferSurface> surface;
293 surface = adoptPtr(new UnacceleratedImageBufferSurface(m_absolutePaintRect.size()));
294 m_imageBufferResult = ImageBuffer::create(surface.release());
295 if (!m_imageBufferResult)
298 IntRect destinationRect(IntPoint(), m_absolutePaintRect.size());
299 if (m_premultipliedImageResult)
300 m_imageBufferResult->putByteArray(Premultiplied, m_premultipliedImageResult.get(), destinationRect.size(), destinationRect, IntPoint());
302 m_imageBufferResult->putByteArray(Unmultiplied, m_unmultipliedImageResult.get(), destinationRect.size(), destinationRect, IntPoint());
303 return m_imageBufferResult.get();
306 PassRefPtr<Uint8ClampedArray> FilterEffect::asUnmultipliedImage(const IntRect& rect)
308 ASSERT(isFilterSizeValid(rect));
309 RefPtr<Uint8ClampedArray> imageData = Uint8ClampedArray::createUninitialized(rect.width() * rect.height() * 4);
310 copyUnmultipliedImage(imageData.get(), rect);
311 return imageData.release();
314 PassRefPtr<Uint8ClampedArray> FilterEffect::asPremultipliedImage(const IntRect& rect)
316 ASSERT(isFilterSizeValid(rect));
317 RefPtr<Uint8ClampedArray> imageData = Uint8ClampedArray::createUninitialized(rect.width() * rect.height() * 4);
318 copyPremultipliedImage(imageData.get(), rect);
319 return imageData.release();
322 inline void FilterEffect::copyImageBytes(Uint8ClampedArray* source, Uint8ClampedArray* destination, const IntRect& rect)
324 // Initialize the destination to transparent black, if not entirely covered by the source.
325 if (rect.x() < 0 || rect.y() < 0 || rect.maxX() > m_absolutePaintRect.width() || rect.maxY() > m_absolutePaintRect.height())
326 memset(destination->data(), 0, destination->length());
328 // Early return if the rect does not intersect with the source.
329 if (rect.maxX() <= 0 || rect.maxY() <= 0 || rect.x() >= m_absolutePaintRect.width() || rect.y() >= m_absolutePaintRect.height())
332 int xOrigin = rect.x();
338 int xEnd = rect.maxX();
339 if (xEnd > m_absolutePaintRect.width())
340 xEnd = m_absolutePaintRect.width();
342 int yOrigin = rect.y();
348 int yEnd = rect.maxY();
349 if (yEnd > m_absolutePaintRect.height())
350 yEnd = m_absolutePaintRect.height();
352 int size = (xEnd - xOrigin) * 4;
353 int destinationScanline = rect.width() * 4;
354 int sourceScanline = m_absolutePaintRect.width() * 4;
355 unsigned char *destinationPixel = destination->data() + ((yDest * rect.width()) + xDest) * 4;
356 unsigned char *sourcePixel = source->data() + ((yOrigin * m_absolutePaintRect.width()) + xOrigin) * 4;
358 while (yOrigin < yEnd) {
359 memcpy(destinationPixel, sourcePixel, size);
360 destinationPixel += destinationScanline;
361 sourcePixel += sourceScanline;
366 void FilterEffect::copyUnmultipliedImage(Uint8ClampedArray* destination, const IntRect& rect)
370 if (!m_unmultipliedImageResult) {
371 // We prefer a conversion from the image buffer.
372 if (m_imageBufferResult)
373 m_unmultipliedImageResult = m_imageBufferResult->getUnmultipliedImageData(IntRect(IntPoint(), m_absolutePaintRect.size()));
375 ASSERT(isFilterSizeValid(m_absolutePaintRect));
376 m_unmultipliedImageResult = Uint8ClampedArray::createUninitialized(m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
377 unsigned char* sourceComponent = m_premultipliedImageResult->data();
378 unsigned char* destinationComponent = m_unmultipliedImageResult->data();
379 unsigned char* end = sourceComponent + (m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
380 while (sourceComponent < end) {
381 int alpha = sourceComponent[3];
383 destinationComponent[0] = static_cast<int>(sourceComponent[0]) * 255 / alpha;
384 destinationComponent[1] = static_cast<int>(sourceComponent[1]) * 255 / alpha;
385 destinationComponent[2] = static_cast<int>(sourceComponent[2]) * 255 / alpha;
387 destinationComponent[0] = 0;
388 destinationComponent[1] = 0;
389 destinationComponent[2] = 0;
391 destinationComponent[3] = alpha;
392 sourceComponent += 4;
393 destinationComponent += 4;
397 copyImageBytes(m_unmultipliedImageResult.get(), destination, rect);
400 void FilterEffect::copyPremultipliedImage(Uint8ClampedArray* destination, const IntRect& rect)
404 if (!m_premultipliedImageResult) {
405 // We prefer a conversion from the image buffer.
406 if (m_imageBufferResult)
407 m_premultipliedImageResult = m_imageBufferResult->getPremultipliedImageData(IntRect(IntPoint(), m_absolutePaintRect.size()));
409 ASSERT(isFilterSizeValid(m_absolutePaintRect));
410 m_premultipliedImageResult = Uint8ClampedArray::createUninitialized(m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
411 unsigned char* sourceComponent = m_unmultipliedImageResult->data();
412 unsigned char* destinationComponent = m_premultipliedImageResult->data();
413 unsigned char* end = sourceComponent + (m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
414 while (sourceComponent < end) {
415 int alpha = sourceComponent[3];
416 destinationComponent[0] = static_cast<int>(sourceComponent[0]) * alpha / 255;
417 destinationComponent[1] = static_cast<int>(sourceComponent[1]) * alpha / 255;
418 destinationComponent[2] = static_cast<int>(sourceComponent[2]) * alpha / 255;
419 destinationComponent[3] = alpha;
420 sourceComponent += 4;
421 destinationComponent += 4;
425 copyImageBytes(m_premultipliedImageResult.get(), destination, rect);
428 ImageBuffer* FilterEffect::createImageBufferResult()
430 // Only one result type is allowed.
431 ASSERT(!hasResult());
432 ASSERT(isFilterSizeValid(m_absolutePaintRect));
434 OwnPtr<ImageBufferSurface> surface;
435 surface = adoptPtr(new UnacceleratedImageBufferSurface(m_absolutePaintRect.size()));
436 m_imageBufferResult = ImageBuffer::create(surface.release());
437 return m_imageBufferResult.get();
440 Uint8ClampedArray* FilterEffect::createUnmultipliedImageResult()
442 // Only one result type is allowed.
443 ASSERT(!hasResult());
444 ASSERT(isFilterSizeValid(m_absolutePaintRect));
446 if (m_absolutePaintRect.isEmpty())
448 m_unmultipliedImageResult = Uint8ClampedArray::createUninitialized(m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
449 return m_unmultipliedImageResult.get();
452 Uint8ClampedArray* FilterEffect::createPremultipliedImageResult()
454 // Only one result type is allowed.
455 ASSERT(!hasResult());
456 ASSERT(isFilterSizeValid(m_absolutePaintRect));
458 if (m_absolutePaintRect.isEmpty())
460 m_premultipliedImageResult = Uint8ClampedArray::createUninitialized(m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
461 return m_premultipliedImageResult.get();
464 Color FilterEffect::adaptColorToOperatingColorSpace(const Color& deviceColor)
466 // |deviceColor| is assumed to be DeviceRGB.
467 return ColorSpaceUtilities::convertColor(deviceColor, operatingColorSpace());
470 void FilterEffect::transformResultColorSpace(ColorSpace dstColorSpace)
472 if (!hasResult() || dstColorSpace == m_resultColorSpace)
475 // FIXME: We can avoid this potentially unnecessary ImageBuffer conversion by adding
476 // color space transform support for the {pre,un}multiplied arrays.
477 asImageBuffer()->transformColorSpace(m_resultColorSpace, dstColorSpace);
479 m_resultColorSpace = dstColorSpace;
481 if (m_unmultipliedImageResult)
482 m_unmultipliedImageResult.clear();
483 if (m_premultipliedImageResult)
484 m_premultipliedImageResult.clear();
487 TextStream& FilterEffect::externalRepresentation(TextStream& ts, int) const
489 // FIXME: We should dump the subRegions of the filter primitives here later. This isn't
490 // possible at the moment, because we need more detailed informations from the target object.
494 FloatRect FilterEffect::determineFilterPrimitiveSubregion(DetermineSubregionFlags flags)
496 Filter* filter = this->filter();
499 // FETile, FETurbulence, FEFlood don't have input effects, take the filter region as unite rect.
501 if (unsigned numberOfInputEffects = inputEffects().size()) {
502 subregion = inputEffect(0)->determineFilterPrimitiveSubregion(flags);
503 for (unsigned i = 1; i < numberOfInputEffects; ++i)
504 subregion.unite(inputEffect(i)->determineFilterPrimitiveSubregion(flags));
506 subregion = filter->filterRegion();
509 // After calling determineFilterPrimitiveSubregion on the target effect, reset the subregion again for <feTile>.
510 if (filterEffectType() == FilterEffectTypeTile)
511 subregion = filter->filterRegion();
513 if (flags & MapRectForward) {
514 // mapRect works on absolute rectangles.
515 subregion = filter->mapAbsoluteRectToLocalRect(mapRect(
516 filter->mapLocalRectToAbsoluteRect(subregion)));
519 FloatRect boundaries = effectBoundaries();
521 subregion.setX(boundaries.x());
523 subregion.setY(boundaries.y());
525 subregion.setWidth(boundaries.width());
527 subregion.setHeight(boundaries.height());
529 setFilterPrimitiveSubregion(subregion);
531 FloatRect absoluteSubregion = filter->mapLocalRectToAbsoluteRect(subregion);
533 // Clip every filter effect to the filter region.
534 if (flags & ClipToFilterRegion) {
535 absoluteSubregion.intersect(filter->absoluteFilterRegion());
538 setMaxEffectRect(absoluteSubregion);
542 PassRefPtr<SkImageFilter> FilterEffect::createImageFilter(SkiaImageFilterBuilder* builder)
547 PassRefPtr<SkImageFilter> FilterEffect::createImageFilterWithoutValidation(SkiaImageFilterBuilder* builder)
549 return createImageFilter(builder);
552 SkImageFilter::CropRect FilterEffect::getCropRect(const FloatSize& cropOffset) const
554 FloatRect rect = filter()->filterRegion();
556 FloatRect boundaries = effectBoundaries();
557 boundaries.move(cropOffset);
559 rect.setX(boundaries.x());
560 flags |= SkImageFilter::CropRect::kHasLeft_CropEdge;
561 flags |= SkImageFilter::CropRect::kHasRight_CropEdge;
564 rect.setY(boundaries.y());
565 flags |= SkImageFilter::CropRect::kHasTop_CropEdge;
566 flags |= SkImageFilter::CropRect::kHasBottom_CropEdge;
569 rect.setWidth(boundaries.width());
570 flags |= SkImageFilter::CropRect::kHasRight_CropEdge;
573 rect.setHeight(boundaries.height());
574 flags |= SkImageFilter::CropRect::kHasBottom_CropEdge;
576 rect = filter()->mapLocalRectToAbsoluteRect(rect);
577 return SkImageFilter::CropRect(rect, flags);
580 static int getImageFilterIndex(ColorSpace colorSpace, bool requiresPMColorValidation)
582 // Map the (colorspace, bool) tuple to an integer index as follows:
583 // 0 == linear colorspace, no PM validation
584 // 1 == device colorspace, no PM validation
585 // 2 == linear colorspace, PM validation
586 // 3 == device colorspace, PM validation
587 return (colorSpace == ColorSpaceLinearRGB ? 0x1 : 0x0) | (requiresPMColorValidation ? 0x2 : 0x0);
590 SkImageFilter* FilterEffect::getImageFilter(ColorSpace colorSpace, bool requiresPMColorValidation) const
592 int index = getImageFilterIndex(colorSpace, requiresPMColorValidation);
593 return m_imageFilters[index].get();
596 void FilterEffect::setImageFilter(ColorSpace colorSpace, bool requiresPMColorValidation, PassRefPtr<SkImageFilter> imageFilter)
598 int index = getImageFilterIndex(colorSpace, requiresPMColorValidation);
599 filter()->removeFromCache(m_imageFilters[index].get());
600 m_imageFilters[index] = imageFilter;
603 } // namespace WebCore