2 * Copyright (C) 2006 Eric Seidel <eric@webkit.org>
3 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/svg/graphics/SVGImage.h"
32 #include "core/animation/AnimationTimeline.h"
33 #include "core/dom/NodeTraversal.h"
34 #include "core/dom/shadow/ComposedTreeWalker.h"
35 #include "core/frame/FrameView.h"
36 #include "core/frame/LocalFrame.h"
37 #include "core/frame/Settings.h"
38 #include "core/loader/FrameLoadRequest.h"
39 #include "core/page/Chrome.h"
40 #include "core/rendering/style/RenderStyle.h"
41 #include "core/rendering/svg/RenderSVGRoot.h"
42 #include "core/svg/SVGDocumentExtensions.h"
43 #include "core/svg/SVGFEImageElement.h"
44 #include "core/svg/SVGImageElement.h"
45 #include "core/svg/SVGSVGElement.h"
46 #include "core/svg/animation/SMILTimeContainer.h"
47 #include "core/svg/graphics/SVGImageChromeClient.h"
48 #include "platform/EventDispatchForbiddenScope.h"
49 #include "platform/LengthFunctions.h"
50 #include "platform/TraceEvent.h"
51 #include "platform/geometry/IntRect.h"
52 #include "platform/graphics/DisplayList.h"
53 #include "platform/graphics/GraphicsContextStateSaver.h"
54 #include "platform/graphics/ImageBuffer.h"
55 #include "platform/graphics/ImageObserver.h"
56 #include "wtf/PassRefPtr.h"
60 SVGImage::SVGImage(ImageObserver* observer)
68 // Store m_page in a local variable, clearing m_page, so that SVGImageChromeClient knows we're destructed.
69 OwnPtrWillBeRawPtr<Page> currentPage = m_page.release();
70 // Break both the loader and view references to the frame
71 currentPage->willBeDestroyed();
74 // Verify that page teardown destroyed the Chrome
75 ASSERT(!m_chromeClient || !m_chromeClient->image());
78 bool SVGImage::isInSVGImage(const Node* node)
82 Page* page = node->document().page();
86 return page->chrome().client().isSVGImageChromeClient();
89 bool SVGImage::currentFrameHasSingleSecurityOrigin() const
94 LocalFrame* frame = toLocalFrame(m_page->mainFrame());
96 RELEASE_ASSERT(frame->document()->loadEventFinished());
98 SVGSVGElement* rootElement = frame->document()->accessSVGExtensions().rootElement();
102 // Don't allow foreignObject elements or images that are not known to be
103 // single-origin since these can leak cross-origin information.
104 ComposedTreeWalker walker(rootElement);
105 while (Node* node = walker.get()) {
106 if (isSVGForeignObjectElement(*node))
108 if (isSVGImageElement(*node)) {
109 if (!toSVGImageElement(*node).currentFrameHasSingleSecurityOrigin())
111 } else if (isSVGFEImageElement(*node)) {
112 if (!toSVGFEImageElement(*node).currentFrameHasSingleSecurityOrigin())
118 // Because SVG image rendering disallows external resources and links, these
119 // images effectively are restricted to a single security origin.
123 static SVGSVGElement* svgRootElement(Page* page)
127 LocalFrame* frame = toLocalFrame(page->mainFrame());
128 return frame->document()->accessSVGExtensions().rootElement();
131 void SVGImage::setContainerSize(const IntSize& size)
133 if (!usesContainerSize())
136 SVGSVGElement* rootElement = svgRootElement(m_page.get());
140 FrameView* view = frameView();
141 view->resize(this->containerSize());
143 RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
146 renderer->setContainerSize(size);
149 IntSize SVGImage::containerSize() const
151 SVGSVGElement* rootElement = svgRootElement(m_page.get());
155 RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
159 // If a container size is available it has precedence.
160 IntSize containerSize = renderer->containerSize();
161 if (!containerSize.isEmpty())
162 return containerSize;
164 // Assure that a container size is always given for a non-identity zoom level.
165 ASSERT(renderer->style()->effectiveZoom() == 1);
167 FloatSize intrinsicSize;
168 double intrinsicRatio = 0;
169 renderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
171 if (intrinsicSize.isEmpty() && intrinsicRatio) {
172 if (!intrinsicSize.width() && intrinsicSize.height())
173 intrinsicSize.setWidth(intrinsicSize.height() * intrinsicRatio);
174 else if (intrinsicSize.width() && !intrinsicSize.height())
175 intrinsicSize.setHeight(intrinsicSize.width() / intrinsicRatio);
178 if (!intrinsicSize.isEmpty())
179 return expandedIntSize(intrinsicSize);
181 // As last resort, use CSS replaced element fallback size.
182 return IntSize(300, 150);
185 void SVGImage::drawForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& dstRect,
186 const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
191 // Temporarily disable the image observer to prevent changeInRect() calls due re-laying out the image.
192 ImageObserverDisabler imageObserverDisabler(this);
194 IntSize roundedContainerSize = roundedIntSize(containerSize);
195 setContainerSize(roundedContainerSize);
197 FloatRect scaledSrc = srcRect;
198 scaledSrc.scale(1 / zoom);
200 // Compensate for the container size rounding by adjusting the source rect.
201 FloatSize adjustedSrcSize = scaledSrc.size();
202 adjustedSrcSize.scale(roundedContainerSize.width() / containerSize.width(), roundedContainerSize.height() / containerSize.height());
203 scaledSrc.setSize(adjustedSrcSize);
205 draw(context, dstRect, scaledSrc, compositeOp, blendMode);
208 PassRefPtr<NativeImageSkia> SVGImage::nativeImageForCurrentFrame()
213 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(size());
217 drawForContainer(buffer->context(), size(), 1, rect(), rect(), CompositeSourceOver, blink::WebBlendModeNormal);
219 // FIXME: WK(Bug 113657): We should use DontCopyBackingStore here.
220 return buffer->copyImage(CopyBackingStore)->nativeImageForCurrentFrame();
223 void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize containerSize,
224 float zoom, const FloatRect& srcRect, const FloatSize& tileScale, const FloatPoint& phase,
225 CompositeOperator compositeOp, const FloatRect& dstRect, blink::WebBlendMode blendMode,
226 const IntSize& repeatSpacing)
228 // Tile adjusted for scaling/stretch.
229 FloatRect tile(srcRect);
230 tile.scale(tileScale.width(), tileScale.height());
232 // Expand the tile to account for repeat spacing.
233 FloatRect spacedTile(tile);
234 spacedTile.expand(repeatSpacing);
236 // Record using a dedicated GC, to avoid inheriting unwanted state (pending color filters
237 // for example must be applied atomically during the final fill/composite phase).
238 GraphicsContext recordingContext(0);
239 recordingContext.beginRecording(spacedTile);
240 // When generating an expanded tile, make sure we don't draw into the spacing area.
241 if (tile != spacedTile)
242 recordingContext.clipRect(tile);
243 drawForContainer(&recordingContext, containerSize, zoom, tile, srcRect, CompositeSourceOver,
244 blink::WebBlendModeNormal);
245 RefPtr<DisplayList> tileDisplayList = recordingContext.endRecording();
247 SkMatrix patternTransform;
248 patternTransform.setTranslate(phase.x() + tile.x(), phase.y() + tile.y());
249 SkRect tileRect = SkRect::MakeWH(spacedTile.width(), spacedTile.height());
250 RefPtr<SkShader> patternShader = adoptRef(SkShader::CreatePictureShader(
251 tileDisplayList->picture().get(), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
252 &patternTransform, &tileRect));
255 paint.setShader(patternShader.get());
256 paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp, blendMode));
257 paint.setColorFilter(context->colorFilter());
258 context->drawRect(dstRect, paint);
261 void SVGImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
266 GraphicsContextStateSaver stateSaver(*context);
267 context->setCompositeOperation(compositeOp, blendMode);
268 context->clip(enclosingIntRect(dstRect));
270 bool compositingRequiresTransparencyLayer = compositeOp != CompositeSourceOver || blendMode != blink::WebBlendModeNormal;
271 float opacity = context->getNormalizedAlpha() / 255.f;
272 bool requiresTransparencyLayer = compositingRequiresTransparencyLayer || opacity < 1;
273 if (requiresTransparencyLayer) {
274 context->beginTransparencyLayer(opacity);
275 if (compositingRequiresTransparencyLayer)
276 context->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal);
279 FloatSize scale(dstRect.width() / srcRect.width(), dstRect.height() / srcRect.height());
281 // We can only draw the entire frame, clipped to the rect we want. So compute where the top left
282 // of the image would be if we were drawing without clipping, and translate accordingly.
283 FloatSize topLeftOffset(srcRect.location().x() * scale.width(), srcRect.location().y() * scale.height());
284 FloatPoint destOffset = dstRect.location() - topLeftOffset;
286 context->translate(destOffset.x(), destOffset.y());
287 context->scale(scale.width(), scale.height());
289 FrameView* view = frameView();
290 view->resize(containerSize());
292 if (!m_url.isEmpty())
293 view->scrollToFragment(m_url);
295 view->updateLayoutAndStyleForPainting();
296 view->paint(context, enclosingIntRect(srcRect));
297 ASSERT(!view->needsLayout());
299 if (requiresTransparencyLayer)
302 stateSaver.restore();
305 imageObserver()->didDraw(this);
307 // Start any (SMIL) animations if needed. This will restart or continue
308 // animations if preceded by calls to resetAnimation or stopAnimation
313 RenderBox* SVGImage::embeddedContentBox() const
315 SVGSVGElement* rootElement = svgRootElement(m_page.get());
318 return toRenderBox(rootElement->renderer());
321 FrameView* SVGImage::frameView() const
326 return toLocalFrame(m_page->mainFrame())->view();
329 void SVGImage::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
331 SVGSVGElement* rootElement = svgRootElement(m_page.get());
335 intrinsicWidth = rootElement->intrinsicWidth();
336 intrinsicHeight = rootElement->intrinsicHeight();
337 if (rootElement->preserveAspectRatio()->currentValue()->align() == SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_NONE)
340 intrinsicRatio = rootElement->viewBox()->currentValue()->value().size();
341 if (intrinsicRatio.isEmpty() && intrinsicWidth.isFixed() && intrinsicHeight.isFixed())
342 intrinsicRatio = FloatSize(floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0));
345 // FIXME: support CatchUpAnimation = CatchUp.
346 void SVGImage::startAnimation(CatchUpAnimation)
348 SVGSVGElement* rootElement = svgRootElement(m_page.get());
349 if (!rootElement || !rootElement->animationsPaused())
351 rootElement->unpauseAnimations();
354 void SVGImage::stopAnimation()
356 SVGSVGElement* rootElement = svgRootElement(m_page.get());
359 rootElement->pauseAnimations();
362 void SVGImage::resetAnimation()
364 SVGSVGElement* rootElement = svgRootElement(m_page.get());
367 rootElement->pauseAnimations();
368 rootElement->setCurrentTime(0);
371 bool SVGImage::hasAnimations() const
373 SVGSVGElement* rootElement = svgRootElement(m_page.get());
376 return rootElement->timeContainer()->hasAnimations() || toLocalFrame(m_page->mainFrame())->document()->timeline().hasPendingUpdates();
379 bool SVGImage::dataChanged(bool allDataReceived)
381 TRACE_EVENT0("blink", "SVGImage::dataChanged");
383 // Don't do anything if is an empty image.
387 if (allDataReceived) {
388 // SVGImage will fire events (and the default C++ handlers run) but doesn't
389 // actually allow script to run so it's fine to call into it. We allow this
390 // since it means an SVG data url can synchronously load like other image
392 EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents;
394 static FrameLoaderClient* dummyFrameLoaderClient = new EmptyFrameLoaderClient;
396 Page::PageClients pageClients;
397 fillWithEmptyClients(pageClients);
398 m_chromeClient = adoptPtr(new SVGImageChromeClient(this));
399 pageClients.chromeClient = m_chromeClient.get();
401 // FIXME: If this SVG ends up loading itself, we might leak the world.
402 // The Cache code does not know about ImageResources holding Frames and
403 // won't know to break the cycle.
404 // This will become an issue when SVGImage will be able to load other
405 // SVGImage objects, but we're safe now, because SVGImage can only be
406 // loaded by a top-level document.
407 OwnPtrWillBeRawPtr<Page> page;
409 TRACE_EVENT0("blink", "SVGImage::dataChanged::createPage");
410 page = adoptPtrWillBeNoop(new Page(pageClients));
411 page->settings().setScriptEnabled(false);
412 page->settings().setPluginsEnabled(false);
413 page->settings().setAcceleratedCompositingEnabled(false);
416 RefPtrWillBeRawPtr<LocalFrame> frame = nullptr;
418 TRACE_EVENT0("blink", "SVGImage::dataChanged::createFrame");
419 frame = LocalFrame::create(dummyFrameLoaderClient, &page->frameHost(), 0);
420 frame->setView(FrameView::create(frame.get()));
424 FrameLoader& loader = frame->loader();
425 loader.forceSandboxFlags(SandboxAll);
427 frame->view()->setScrollbarsSuppressed(true);
428 frame->view()->setCanHaveScrollbars(false); // SVG Images will always synthesize a viewBox, if it's not available, and thus never see scrollbars.
429 frame->view()->setTransparent(true); // SVG Images are transparent.
431 m_page = page.release();
433 TRACE_EVENT0("blink", "SVGImage::dataChanged::load");
434 loader.load(FrameLoadRequest(0, blankURL(), SubstituteData(data(), AtomicString("image/svg+xml", AtomicString::ConstructFromLiteral),
435 AtomicString("UTF-8", AtomicString::ConstructFromLiteral), KURL(), ForceSynchronousLoad)));
436 // Set the intrinsic size before a container size is available.
437 m_intrinsicSize = containerSize();
443 String SVGImage::filenameExtension() const