1/*
2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "SVGImageCache.h"
23
24#include "FrameView.h"
25#include "GraphicsContext.h"
26#include "ImageBuffer.h"
27#include "LayoutSize.h"
28#include "RenderSVGRoot.h"
29#include "SVGImage.h"
30#include "SVGImageForContainer.h"
31
32namespace WebCore {
33
34SVGImageCache::SVGImageCache(SVGImage* svgImage)
35 : m_svgImage(svgImage)
36{
37 ASSERT(m_svgImage);
38}
39
40SVGImageCache::~SVGImageCache()
41{
42 m_imageForContainerMap.clear();
43}
44
45void SVGImageCache::removeClientFromCache(const CachedImageClient* client)
46{
47 ASSERT(client);
48
49 m_imageForContainerMap.remove(client);
50}
51
52void SVGImageCache::setContainerContextForClient(const CachedImageClient& client, const LayoutSize& containerSize, float containerZoom, const URL& imageURL)
53{
54 ASSERT(!containerSize.isEmpty());
55 ASSERT(containerZoom);
56
57 FloatSize containerSizeWithoutZoom(containerSize);
58 containerSizeWithoutZoom.scale(1 / containerZoom);
59
60 m_imageForContainerMap.set(&client, SVGImageForContainer::create(m_svgImage, containerSizeWithoutZoom, containerZoom, imageURL));
61}
62
63Image* SVGImageCache::findImageForRenderer(const RenderObject* renderer) const
64{
65 return renderer ? m_imageForContainerMap.get(renderer) : nullptr;
66}
67
68FloatSize SVGImageCache::imageSizeForRenderer(const RenderObject* renderer) const
69{
70 auto* image = findImageForRenderer(renderer);
71 return image ? image->size() : m_svgImage->size();
72}
73
74// FIXME: This doesn't take into account the animation timeline so animations will not
75// restart on page load, nor will two animations in different pages have different timelines.
76Image* SVGImageCache::imageForRenderer(const RenderObject* renderer) const
77{
78 auto* image = findImageForRenderer(renderer);
79 if (!image)
80 return &Image::nullImage();
81 ASSERT(!image->size().isEmpty());
82 return image;
83}
84
85} // namespace WebCore
86