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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
/*
* Copyright (C) 2006 Eric Seidel <eric@webkit.org>
* Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "core/svg/graphics/SVGImage.h"
#include "core/animation/AnimationTimeline.h"
#include "core/dom/NoEventDispatchAssertion.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/shadow/ComposedTreeWalker.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/loader/FrameLoadRequest.h"
#include "core/page/Chrome.h"
#include "core/rendering/style/RenderStyle.h"
#include "core/rendering/svg/RenderSVGRoot.h"
#include "core/svg/SVGDocumentExtensions.h"
#include "core/svg/SVGFEImageElement.h"
#include "core/svg/SVGImageElement.h"
#include "core/svg/SVGSVGElement.h"
#include "core/svg/animation/SMILTimeContainer.h"
#include "core/svg/graphics/SVGImageChromeClient.h"
#include "platform/LengthFunctions.h"
#include "platform/TraceEvent.h"
#include "platform/geometry/IntRect.h"
#include "platform/graphics/GraphicsContextStateSaver.h"
#include "platform/graphics/ImageBuffer.h"
#include "platform/graphics/ImageObserver.h"
#include "wtf/PassRefPtr.h"
namespace WebCore {
SVGImage::SVGImage(ImageObserver* observer)
: Image(observer)
{
}
SVGImage::~SVGImage()
{
if (m_page) {
// Store m_page in a local variable, clearing m_page, so that SVGImageChromeClient knows we're destructed.
OwnPtrWillBeRawPtr<Page> currentPage = m_page.release();
// Break both the loader and view references to the frame
currentPage->willBeDestroyed();
}
// Verify that page teardown destroyed the Chrome
ASSERT(!m_chromeClient || !m_chromeClient->image());
}
bool SVGImage::isInSVGImage(const Node* node)
{
ASSERT(node);
Page* page = node->document().page();
if (!page)
return false;
return page->chrome().client().isSVGImageChromeClient();
}
bool SVGImage::currentFrameHasSingleSecurityOrigin() const
{
if (!m_page)
return true;
LocalFrame* frame = toLocalFrame(m_page->mainFrame());
RELEASE_ASSERT(frame->document()->loadEventFinished());
SVGSVGElement* rootElement = frame->document()->accessSVGExtensions().rootElement();
if (!rootElement)
return true;
// Don't allow foreignObject elements or images that are not known to be
// single-origin since these can leak cross-origin information.
ComposedTreeWalker walker(rootElement);
while (Node* node = walker.get()) {
if (isSVGForeignObjectElement(*node))
return false;
if (isSVGImageElement(*node)) {
if (!toSVGImageElement(*node).currentFrameHasSingleSecurityOrigin())
return false;
} else if (isSVGFEImageElement(*node)) {
if (!toSVGFEImageElement(*node).currentFrameHasSingleSecurityOrigin())
return false;
}
walker.next();
}
// Because SVG image rendering disallows external resources and links, these
// images effectively are restricted to a single security origin.
return true;
}
static SVGSVGElement* svgRootElement(Page* page)
{
if (!page)
return 0;
LocalFrame* frame = toLocalFrame(page->mainFrame());
return frame->document()->accessSVGExtensions().rootElement();
}
void SVGImage::setContainerSize(const IntSize& size)
{
if (!usesContainerSize())
return;
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
FrameView* view = frameView();
view->resize(this->containerSize());
RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
if (!renderer)
return;
renderer->setContainerSize(size);
}
IntSize SVGImage::containerSize() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return IntSize();
RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
if (!renderer)
return IntSize();
// If a container size is available it has precedence.
IntSize containerSize = renderer->containerSize();
if (!containerSize.isEmpty())
return containerSize;
// Assure that a container size is always given for a non-identity zoom level.
ASSERT(renderer->style()->effectiveZoom() == 1);
FloatSize intrinsicSize;
double intrinsicRatio = 0;
renderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
if (intrinsicSize.isEmpty() && intrinsicRatio) {
if (!intrinsicSize.width() && intrinsicSize.height())
intrinsicSize.setWidth(intrinsicSize.height() * intrinsicRatio);
else if (intrinsicSize.width() && !intrinsicSize.height())
intrinsicSize.setHeight(intrinsicSize.width() / intrinsicRatio);
}
if (!intrinsicSize.isEmpty())
return expandedIntSize(intrinsicSize);
// As last resort, use CSS replaced element fallback size.
return IntSize(300, 150);
}
void SVGImage::drawForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& dstRect,
const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
{
if (!m_page)
return;
// Temporarily disable the image observer to prevent changeInRect() calls due re-laying out the image.
ImageObserverDisabler imageObserverDisabler(this);
IntSize roundedContainerSize = roundedIntSize(containerSize);
setContainerSize(roundedContainerSize);
FloatRect scaledSrc = srcRect;
scaledSrc.scale(1 / zoom);
// Compensate for the container size rounding by adjusting the source rect.
FloatSize adjustedSrcSize = scaledSrc.size();
adjustedSrcSize.scale(roundedContainerSize.width() / containerSize.width(), roundedContainerSize.height() / containerSize.height());
scaledSrc.setSize(adjustedSrcSize);
draw(context, dstRect, scaledSrc, compositeOp, blendMode);
}
PassRefPtr<NativeImageSkia> SVGImage::nativeImageForCurrentFrame()
{
if (!m_page)
return nullptr;
OwnPtr<ImageBuffer> buffer = ImageBuffer::create(size());
if (!buffer)
return nullptr;
drawForContainer(buffer->context(), size(), 1, rect(), rect(), CompositeSourceOver, blink::WebBlendModeNormal);
// FIXME: WK(Bug 113657): We should use DontCopyBackingStore here.
return buffer->copyImage(CopyBackingStore)->nativeImageForCurrentFrame();
}
void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& srcRect,
const FloatSize& scale, const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& dstRect, blink::WebBlendMode blendMode, const IntSize& repeatSpacing)
{
FloatRect zoomedContainerRect = FloatRect(FloatPoint(), containerSize);
zoomedContainerRect.scale(zoom);
// The ImageBuffer size needs to be scaled to match the final resolution.
// FIXME: No need to get the full CTM here, we just need the scale.
// FIXME: See crbug.com/382491. This scale does not reflect compositor applied
// scale factors, such a High DPI or device zoom.
AffineTransform transform = context->getCTM();
FloatSize imageBufferScale = FloatSize(transform.xScale(), transform.yScale());
ASSERT(imageBufferScale.width());
ASSERT(imageBufferScale.height());
FloatSize scaleWithoutCTM(scale.width() / imageBufferScale.width(), scale.height() / imageBufferScale.height());
FloatRect imageBufferSize = zoomedContainerRect;
imageBufferSize.scale(imageBufferScale.width(), imageBufferScale.height());
OwnPtr<ImageBuffer> buffer = ImageBuffer::create(expandedIntSize(imageBufferSize.size()));
if (!buffer) // Failed to allocate buffer.
return;
drawForContainer(buffer->context(), containerSize, zoom, imageBufferSize, zoomedContainerRect, CompositeSourceOver, blink::WebBlendModeNormal);
RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore, Unscaled);
// Adjust the source rect and transform due to the image buffer's scaling.
FloatRect scaledSrcRect = srcRect;
scaledSrcRect.scale(imageBufferScale.width(), imageBufferScale.height());
image->drawPattern(context, scaledSrcRect, scaleWithoutCTM, phase, compositeOp, dstRect, blendMode, repeatSpacing);
}
void SVGImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
{
if (!m_page)
return;
GraphicsContextStateSaver stateSaver(*context);
context->setCompositeOperation(compositeOp, blendMode);
context->clip(enclosingIntRect(dstRect));
bool compositingRequiresTransparencyLayer = compositeOp != CompositeSourceOver || blendMode != blink::WebBlendModeNormal;
float opacity = context->getNormalizedAlpha() / 255.f;
bool requiresTransparencyLayer = compositingRequiresTransparencyLayer || opacity < 1;
if (requiresTransparencyLayer) {
context->beginTransparencyLayer(opacity);
if (compositingRequiresTransparencyLayer)
context->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal);
}
FloatSize scale(dstRect.width() / srcRect.width(), dstRect.height() / srcRect.height());
// We can only draw the entire frame, clipped to the rect we want. So compute where the top left
// of the image would be if we were drawing without clipping, and translate accordingly.
FloatSize topLeftOffset(srcRect.location().x() * scale.width(), srcRect.location().y() * scale.height());
FloatPoint destOffset = dstRect.location() - topLeftOffset;
context->translate(destOffset.x(), destOffset.y());
context->scale(scale.width(), scale.height());
FrameView* view = frameView();
view->resize(containerSize());
if (!m_url.isEmpty())
view->scrollToFragment(m_url);
if (view->needsLayout())
view->layout();
view->paint(context, enclosingIntRect(srcRect));
ASSERT(!view->needsLayout());
if (requiresTransparencyLayer)
context->endLayer();
stateSaver.restore();
if (imageObserver())
imageObserver()->didDraw(this);
// Start any (SMIL) animations if needed. This will restart or continue
// animations if preceded by calls to resetAnimation or stopAnimation
// respectively.
startAnimation();
}
RenderBox* SVGImage::embeddedContentBox() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return 0;
return toRenderBox(rootElement->renderer());
}
FrameView* SVGImage::frameView() const
{
if (!m_page)
return 0;
return toLocalFrame(m_page->mainFrame())->view();
}
void SVGImage::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
intrinsicWidth = rootElement->intrinsicWidth();
intrinsicHeight = rootElement->intrinsicHeight();
if (rootElement->preserveAspectRatio()->currentValue()->align() == SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_NONE)
return;
intrinsicRatio = rootElement->viewBox()->currentValue()->value().size();
if (intrinsicRatio.isEmpty() && intrinsicWidth.isFixed() && intrinsicHeight.isFixed())
intrinsicRatio = FloatSize(floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0));
}
// FIXME: support CatchUpAnimation = CatchUp.
void SVGImage::startAnimation(CatchUpAnimation)
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement || !rootElement->animationsPaused())
return;
rootElement->unpauseAnimations();
}
void SVGImage::stopAnimation()
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
rootElement->pauseAnimations();
}
void SVGImage::resetAnimation()
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return;
rootElement->pauseAnimations();
rootElement->setCurrentTime(0);
}
bool SVGImage::hasAnimations() const
{
SVGSVGElement* rootElement = svgRootElement(m_page.get());
if (!rootElement)
return false;
return rootElement->timeContainer()->hasAnimations() || toLocalFrame(m_page->mainFrame())->document()->timeline().hasPendingUpdates();
}
bool SVGImage::dataChanged(bool allDataReceived)
{
TRACE_EVENT0("webkit", "SVGImage::dataChanged");
// Don't do anything if is an empty image.
if (!data()->size())
return true;
if (allDataReceived) {
// SVGImage will fire events (and the default C++ handlers run) but doesn't
// actually allow script to run so it's fine to call into it. We allow this
// since it means an SVG data url can synchronously load like other image
// types.
NoEventDispatchAssertion::AllowSVGImageEvents allowSVGImageEvents;
static FrameLoaderClient* dummyFrameLoaderClient = new EmptyFrameLoaderClient;
Page::PageClients pageClients;
fillWithEmptyClients(pageClients);
m_chromeClient = adoptPtr(new SVGImageChromeClient(this));
pageClients.chromeClient = m_chromeClient.get();
// FIXME: If this SVG ends up loading itself, we might leak the world.
// The Cache code does not know about ImageResources holding Frames and
// won't know to break the cycle.
// This will become an issue when SVGImage will be able to load other
// SVGImage objects, but we're safe now, because SVGImage can only be
// loaded by a top-level document.
OwnPtrWillBeRawPtr<Page> page = adoptPtrWillBeNoop(new Page(pageClients));
page->settings().setScriptEnabled(false);
page->settings().setPluginsEnabled(false);
page->settings().setAcceleratedCompositingEnabled(false);
RefPtr<LocalFrame> frame = LocalFrame::create(dummyFrameLoaderClient, &page->frameHost(), 0);
frame->setView(FrameView::create(frame.get()));
frame->init();
FrameLoader& loader = frame->loader();
loader.forceSandboxFlags(SandboxAll);
frame->view()->setScrollbarsSuppressed(true);
frame->view()->setCanHaveScrollbars(false); // SVG Images will always synthesize a viewBox, if it's not available, and thus never see scrollbars.
frame->view()->setTransparent(true); // SVG Images are transparent.
m_page = page.release();
loader.load(FrameLoadRequest(0, blankURL(), SubstituteData(data(), "image/svg+xml", "UTF-8", KURL(), ForceSynchronousLoad)));
// Set the intrinsic size before a container size is available.
m_intrinsicSize = containerSize();
}
return m_page;
}
String SVGImage::filenameExtension() const
{
return "svg";
}
}
|