Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / graphics / SVGImage.cpp
1 /*
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.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27
28 #include "config.h"
29
30 #include "core/svg/graphics/SVGImage.h"
31
32 #include "core/dom/NodeTraversal.h"
33 #include "core/dom/shadow/ComposedTreeWalker.h"
34 #include "core/loader/FrameLoadRequest.h"
35 #include "core/page/Chrome.h"
36 #include "core/frame/Frame.h"
37 #include "core/frame/FrameView.h"
38 #include "core/frame/Settings.h"
39 #include "core/rendering/style/RenderStyle.h"
40 #include "core/rendering/svg/RenderSVGRoot.h"
41 #include "core/svg/SVGDocument.h"
42 #include "core/svg/SVGFEImageElement.h"
43 #include "core/svg/SVGImageElement.h"
44 #include "core/svg/SVGSVGElement.h"
45 #include "core/svg/graphics/SVGImageChromeClient.h"
46 #include "platform/LengthFunctions.h"
47 #include "platform/geometry/IntRect.h"
48 #include "platform/graphics/GraphicsContextStateSaver.h"
49 #include "platform/graphics/ImageBuffer.h"
50 #include "platform/graphics/ImageObserver.h"
51 #include "wtf/PassRefPtr.h"
52
53 namespace WebCore {
54
55 SVGImage::SVGImage(ImageObserver* observer)
56     : Image(observer)
57 {
58 }
59
60 SVGImage::~SVGImage()
61 {
62     if (m_page) {
63         // Store m_page in a local variable, clearing m_page, so that SVGImageChromeClient knows we're destructed.
64         OwnPtr<Page> currentPage = m_page.release();
65         currentPage->mainFrame()->loader().frameDetached(); // Break both the loader and view references to the frame
66     }
67
68     // Verify that page teardown destroyed the Chrome
69     ASSERT(!m_chromeClient || !m_chromeClient->image());
70 }
71
72 bool SVGImage::isInSVGImage(const Element* element)
73 {
74     ASSERT(element);
75
76     Page* page = element->document().page();
77     if (!page)
78         return false;
79
80     return page->chrome().client().isSVGImageChromeClient();
81 }
82
83 bool SVGImage::currentFrameHasSingleSecurityOrigin() const
84 {
85     if (!m_page)
86         return true;
87
88     Frame* frame = m_page->mainFrame();
89
90     RELEASE_ASSERT(frame->document()->loadEventFinished());
91
92     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
93     if (!rootElement)
94         return true;
95
96     // Don't allow foreignObject elements or images that are not known to be
97     // single-origin since these can leak cross-origin information.
98     ComposedTreeWalker walker(rootElement);
99     while (Node* node = walker.get()) {
100         if (node->hasTagName(SVGNames::foreignObjectTag))
101             return false;
102         if (node->hasTagName(SVGNames::imageTag))
103             return toSVGImageElement(node)->currentFrameHasSingleSecurityOrigin();
104         if (node->hasTagName(SVGNames::feImageTag))
105             return toSVGFEImageElement(node)->currentFrameHasSingleSecurityOrigin();
106         walker.next();
107     }
108
109     // Because SVG image rendering disallows external resources and links, these
110     // images effectively are restricted to a single security origin.
111     return true;
112 }
113
114 void SVGImage::setContainerSize(const IntSize& size)
115 {
116     if (!m_page || !usesContainerSize())
117         return;
118
119     Frame* frame = m_page->mainFrame();
120     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
121     if (!rootElement)
122         return;
123     RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
124     if (!renderer)
125         return;
126
127     FrameView* view = frameView();
128     view->resize(this->containerSize());
129
130     renderer->setContainerSize(size);
131 }
132
133 IntSize SVGImage::containerSize() const
134 {
135     if (!m_page)
136         return IntSize();
137     Frame* frame = m_page->mainFrame();
138     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
139     if (!rootElement)
140         return IntSize();
141
142     RenderSVGRoot* renderer = toRenderSVGRoot(rootElement->renderer());
143     if (!renderer)
144         return IntSize();
145
146     // If a container size is available it has precedence.
147     IntSize containerSize = renderer->containerSize();
148     if (!containerSize.isEmpty())
149         return containerSize;
150
151     // Assure that a container size is always given for a non-identity zoom level.
152     ASSERT(renderer->style()->effectiveZoom() == 1);
153
154     FloatSize currentSize;
155     if (rootElement->intrinsicWidth().isFixed() && rootElement->intrinsicHeight().isFixed())
156         currentSize = rootElement->currentViewportSize();
157     else
158         currentSize = rootElement->currentViewBoxRect().size();
159
160     if (!currentSize.isEmpty())
161         return IntSize(static_cast<int>(ceilf(currentSize.width())), static_cast<int>(ceilf(currentSize.height())));
162
163     // As last resort, use CSS default intrinsic size.
164     return IntSize(300, 150);
165 }
166
167 void SVGImage::drawForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& dstRect,
168     const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
169 {
170     if (!m_page)
171         return;
172
173     // Temporarily disable the image observer to prevent changeInRect() calls due re-laying out the image.
174     ImageObserverDisabler imageObserverDisabler(this);
175
176     IntSize roundedContainerSize = roundedIntSize(containerSize);
177     setContainerSize(roundedContainerSize);
178
179     FloatRect scaledSrc = srcRect;
180     scaledSrc.scale(1 / zoom);
181
182     // Compensate for the container size rounding by adjusting the source rect.
183     FloatSize adjustedSrcSize = scaledSrc.size();
184     adjustedSrcSize.scale(roundedContainerSize.width() / containerSize.width(), roundedContainerSize.height() / containerSize.height());
185     scaledSrc.setSize(adjustedSrcSize);
186
187     draw(context, dstRect, scaledSrc, compositeOp, blendMode);
188 }
189
190 PassRefPtr<NativeImageSkia> SVGImage::nativeImageForCurrentFrame()
191 {
192     if (!m_page)
193         return 0;
194
195     OwnPtr<ImageBuffer> buffer = ImageBuffer::create(size());
196     if (!buffer)
197         return 0;
198
199     drawForContainer(buffer->context(), size(), 1, rect(), rect(), CompositeSourceOver, blink::WebBlendModeNormal);
200
201     // FIXME: WK(Bug 113657): We should use DontCopyBackingStore here.
202     return buffer->copyImage(CopyBackingStore)->nativeImageForCurrentFrame();
203 }
204
205 void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize containerSize, float zoom, const FloatRect& srcRect,
206     const FloatSize& scale, const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& dstRect, blink::WebBlendMode blendMode, const IntSize& repeatSpacing)
207 {
208     FloatRect zoomedContainerRect = FloatRect(FloatPoint(), containerSize);
209     zoomedContainerRect.scale(zoom);
210
211     // The ImageBuffer size needs to be scaled to match the final resolution.
212     // FIXME: No need to get the full CTM here, we just need the scale.
213     AffineTransform transform = context->getCTM();
214     FloatSize imageBufferScale = FloatSize(transform.xScale(), transform.yScale());
215     ASSERT(imageBufferScale.width());
216     ASSERT(imageBufferScale.height());
217
218     FloatSize scaleWithoutCTM(scale.width() / imageBufferScale.width(), scale.height() / imageBufferScale.height());
219
220     FloatRect imageBufferSize = zoomedContainerRect;
221     imageBufferSize.scale(imageBufferScale.width(), imageBufferScale.height());
222
223     OwnPtr<ImageBuffer> buffer = ImageBuffer::create(expandedIntSize(imageBufferSize.size()));
224     if (!buffer) // Failed to allocate buffer.
225         return;
226
227     drawForContainer(buffer->context(), containerSize, zoom, imageBufferSize, zoomedContainerRect, CompositeSourceOver, blink::WebBlendModeNormal);
228     RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore, Unscaled);
229
230     // Adjust the source rect and transform due to the image buffer's scaling.
231     FloatRect scaledSrcRect = srcRect;
232     scaledSrcRect.scale(imageBufferScale.width(), imageBufferScale.height());
233
234     image->drawPattern(context, scaledSrcRect, scaleWithoutCTM, phase, compositeOp, dstRect, blendMode, repeatSpacing);
235 }
236
237 void SVGImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
238 {
239     if (!m_page)
240         return;
241
242     GraphicsContextStateSaver stateSaver(*context);
243     context->setCompositeOperation(compositeOp, blendMode);
244     context->clip(enclosingIntRect(dstRect));
245
246     bool compositingRequiresTransparencyLayer = compositeOp != CompositeSourceOver || blendMode != blink::WebBlendModeNormal;
247     if (compositingRequiresTransparencyLayer) {
248         context->beginTransparencyLayer(1);
249         context->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal);
250     }
251
252     FloatSize scale(dstRect.width() / srcRect.width(), dstRect.height() / srcRect.height());
253
254     // We can only draw the entire frame, clipped to the rect we want. So compute where the top left
255     // of the image would be if we were drawing without clipping, and translate accordingly.
256     FloatSize topLeftOffset(srcRect.location().x() * scale.width(), srcRect.location().y() * scale.height());
257     FloatPoint destOffset = dstRect.location() - topLeftOffset;
258
259     context->translate(destOffset.x(), destOffset.y());
260     context->scale(scale);
261
262     FrameView* view = frameView();
263     view->resize(containerSize());
264
265     if (view->needsLayout())
266         view->layout();
267
268     view->paint(context, enclosingIntRect(srcRect));
269
270     if (compositingRequiresTransparencyLayer)
271         context->endLayer();
272
273     stateSaver.restore();
274
275     if (imageObserver())
276         imageObserver()->didDraw(this);
277 }
278
279 RenderBox* SVGImage::embeddedContentBox() const
280 {
281     if (!m_page)
282         return 0;
283     Frame* frame = m_page->mainFrame();
284     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
285     if (!rootElement)
286         return 0;
287     return toRenderBox(rootElement->renderer());
288 }
289
290 FrameView* SVGImage::frameView() const
291 {
292     if (!m_page)
293         return 0;
294
295     return m_page->mainFrame()->view();
296 }
297
298 bool SVGImage::hasRelativeWidth() const
299 {
300     if (!m_page)
301         return false;
302     Frame* frame = m_page->mainFrame();
303     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
304     if (!rootElement)
305         return false;
306     return rootElement->intrinsicWidth().isPercent();
307 }
308
309 bool SVGImage::hasRelativeHeight() const
310 {
311     if (!m_page)
312         return false;
313     Frame* frame = m_page->mainFrame();
314     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
315     if (!rootElement)
316         return false;
317     return rootElement->intrinsicHeight().isPercent();
318 }
319
320 void SVGImage::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
321 {
322     if (!m_page)
323         return;
324     Frame* frame = m_page->mainFrame();
325     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
326     if (!rootElement)
327         return;
328
329     intrinsicWidth = rootElement->intrinsicWidth();
330     intrinsicHeight = rootElement->intrinsicHeight();
331     if (rootElement->preserveAspectRatio()->currentValue()->align() == SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_NONE)
332         return;
333
334     intrinsicRatio = rootElement->viewBox()->currentValue()->value().size();
335     if (intrinsicRatio.isEmpty() && intrinsicWidth.isFixed() && intrinsicHeight.isFixed())
336         intrinsicRatio = FloatSize(floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0));
337 }
338
339 // FIXME: support catchUpIfNecessary.
340 void SVGImage::startAnimation(bool /* catchUpIfNecessary */)
341 {
342     if (!m_page)
343         return;
344     Frame* frame = m_page->mainFrame();
345     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
346     if (!rootElement)
347         return;
348     rootElement->unpauseAnimations();
349     rootElement->setCurrentTime(0);
350 }
351
352 void SVGImage::stopAnimation()
353 {
354     if (!m_page)
355         return;
356     Frame* frame = m_page->mainFrame();
357     SVGSVGElement* rootElement = toSVGDocument(frame->document())->rootElement();
358     if (!rootElement)
359         return;
360     rootElement->pauseAnimations();
361 }
362
363 void SVGImage::resetAnimation()
364 {
365     stopAnimation();
366 }
367
368 bool SVGImage::dataChanged(bool allDataReceived)
369 {
370     TRACE_EVENT0("webkit", "SVGImage::dataChanged");
371
372     // Don't do anything if is an empty image.
373     if (!data()->size())
374         return true;
375
376     if (allDataReceived) {
377         static FrameLoaderClient* dummyFrameLoaderClient =  new EmptyFrameLoaderClient;
378
379         Page::PageClients pageClients;
380         fillWithEmptyClients(pageClients);
381         m_chromeClient = adoptPtr(new SVGImageChromeClient(this));
382         pageClients.chromeClient = m_chromeClient.get();
383
384         // FIXME: If this SVG ends up loading itself, we might leak the world.
385         // The Cache code does not know about ImageResources holding Frames and
386         // won't know to break the cycle.
387         // This will become an issue when SVGImage will be able to load other
388         // SVGImage objects, but we're safe now, because SVGImage can only be
389         // loaded by a top-level document.
390         m_page = adoptPtr(new Page(pageClients));
391         m_page->settings().setMediaEnabled(false);
392         m_page->settings().setScriptEnabled(false);
393         m_page->settings().setPluginsEnabled(false);
394         m_page->settings().setAcceleratedCompositingEnabled(false);
395
396         RefPtr<Frame> frame = Frame::create(FrameInit::create(0, &m_page->frameHost(), dummyFrameLoaderClient));
397         frame->setView(FrameView::create(frame.get()));
398         frame->init();
399         FrameLoader& loader = frame->loader();
400         loader.forceSandboxFlags(SandboxAll);
401
402         frame->view()->setScrollbarsSuppressed(true);
403         frame->view()->setCanHaveScrollbars(false); // SVG Images will always synthesize a viewBox, if it's not available, and thus never see scrollbars.
404         frame->view()->setTransparent(true); // SVG Images are transparent.
405
406         loader.load(FrameLoadRequest(0, blankURL(), SubstituteData(data(), "image/svg+xml", "UTF-8", KURL(), ForceSynchronousLoad)));
407         // Set the intrinsic size before a container size is available.
408         m_intrinsicSize = containerSize();
409     }
410
411     return m_page;
412 }
413
414 String SVGImage::filenameExtension() const
415 {
416     return "svg";
417 }
418
419 }