1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderSVGResourceMasker.h"
#include "AffineTransform.h"
#include "CanvasPixelArray.h"
#include "Element.h"
#include "FloatPoint.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "Image.h"
#include "ImageBuffer.h"
#include "ImageData.h"
#include "IntRect.h"
#include "SVGElement.h"
#include "SVGMaskElement.h"
#include "SVGStyledElement.h"
#include "SVGUnitTypes.h"
#include <wtf/Vector.h>
namespace WebCore {
RenderSVGResourceType RenderSVGResourceMasker::s_resourceType = MaskerResourceType;
RenderSVGResourceMasker::RenderSVGResourceMasker(SVGStyledElement* node)
: RenderSVGResource(node)
{
}
RenderSVGResourceMasker::~RenderSVGResourceMasker()
{
deleteAllValues(m_masker);
m_masker.clear();
}
void RenderSVGResourceMasker::invalidateClients()
{
HashMap<RenderObject*, MaskerData*>::const_iterator end = m_masker.end();
for (HashMap<RenderObject*, MaskerData*>::const_iterator it = m_masker.begin(); it != end; ++it)
it->first->setNeedsLayout(true);
deleteAllValues(m_masker);
m_masker.clear();
}
void RenderSVGResourceMasker::invalidateClient(RenderObject* object)
{
ASSERT(object);
// FIXME: The HashMap should always contain the object on calling invalidateClient. A race condition
// during the parsing can causes a call of invalidateClient right before the call of applyResource.
// We return earlier for the moment. This bug should be fixed in:
// https://bugs.webkit.org/show_bug.cgi?id=35181
if (!m_masker.contains(object))
return;
delete m_masker.take(object);
}
bool RenderSVGResourceMasker::applyResource(RenderObject* object, GraphicsContext* context)
{
ASSERT(object);
ASSERT(context);
if (!m_masker.contains(object))
m_masker.set(object, new MaskerData);
MaskerData* maskerData = m_masker.get(object);
if (!maskerData->maskImage && !maskerData->emptyMask) {
SVGMaskElement* maskElement = static_cast<SVGMaskElement*>(node());
if (!maskElement)
return false;
createMaskImage(maskerData, maskElement, object);
}
if (!maskerData->maskImage)
return false;
context->clipToImageBuffer(maskerData->maskRect, maskerData->maskImage.get());
return true;
}
FloatRect RenderSVGResourceMasker::resourceBoundingBox(const FloatRect& objectBoundingBox) const
{
if (SVGMaskElement* element = static_cast<SVGMaskElement*>(node()))
return element->maskBoundingBox(objectBoundingBox);
return FloatRect();
}
void RenderSVGResourceMasker::createMaskImage(MaskerData* maskerData, const SVGMaskElement* maskElement, RenderObject* object)
{
FloatRect objectBoundingBox = object->objectBoundingBox();
// Mask rect clipped with clippingBoundingBox and filterBoundingBox as long as they are present.
maskerData->maskRect = object->repaintRectInLocalCoordinates();
if (maskerData->maskRect.isEmpty()) {
maskerData->emptyMask = true;
return;
}
// Calculate the smallest rect for the mask ImageBuffer.
FloatRect repaintRect;
Vector<RenderObject*> rendererList;
for (Node* node = maskElement->firstChild(); node; node = node->nextSibling()) {
RenderObject* renderer = node->renderer();
if (!node->isSVGElement() || !static_cast<SVGElement*>(node)->isStyled() || !renderer)
continue;
rendererList.append(renderer);
repaintRect.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
}
AffineTransform contextTransform;
// We need to scale repaintRect for objectBoundingBox to get the drawing area.
if (maskElement->maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
contextTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
FloatPoint contextAdjustment = repaintRect.location();
repaintRect = contextTransform.mapRect(repaintRect);
repaintRect.move(objectBoundingBox.x(), objectBoundingBox.y());
contextTransform.translate(-contextAdjustment.x(), -contextAdjustment.y());
}
repaintRect.intersect(maskerData->maskRect);
maskerData->maskRect = repaintRect;
IntRect maskImageRect = enclosingIntRect(maskerData->maskRect);
maskImageRect.setLocation(IntPoint());
// Don't create ImageBuffers with image size of 0
if (!maskImageRect.width() || !maskImageRect.height()) {
maskerData->emptyMask = true;
return;
}
// FIXME: This changes color space to linearRGB, the default color space
// for masking operations in SVG. We need a switch for the other color-space
// attribute values sRGB, inherit and auto.
maskerData->maskImage = ImageBuffer::create(maskImageRect.size(), LinearRGB);
if (!maskerData->maskImage)
return;
GraphicsContext* maskImageContext = maskerData->maskImage->context();
ASSERT(maskImageContext);
maskImageContext->save();
if (maskElement->maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE)
maskImageContext->translate(-maskerData->maskRect.x(), -maskerData->maskRect.y());
maskImageContext->concatCTM(contextTransform);
// draw the content into the ImageBuffer
Vector<RenderObject*>::iterator end = rendererList.end();
for (Vector<RenderObject*>::iterator it = rendererList.begin(); it != end; it++)
renderSubtreeToImage(maskerData->maskImage.get(), *it);
maskImageContext->restore();
// create the luminance mask
RefPtr<ImageData> imageData(maskerData->maskImage->getUnmultipliedImageData(maskImageRect));
CanvasPixelArray* srcPixelArray(imageData->data());
for (unsigned pixelOffset = 0; pixelOffset < srcPixelArray->length(); pixelOffset += 4) {
unsigned char a = srcPixelArray->get(pixelOffset + 3);
if (!a)
continue;
unsigned char r = srcPixelArray->get(pixelOffset);
unsigned char g = srcPixelArray->get(pixelOffset + 1);
unsigned char b = srcPixelArray->get(pixelOffset + 2);
double luma = (r * 0.2125 + g * 0.7154 + b * 0.0721) * ((double)a / 255.0);
srcPixelArray->set(pixelOffset + 3, luma);
}
maskerData->maskImage->putUnmultipliedImageData(imageData.get(), maskImageRect, IntPoint());
}
}
|