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 "core/svg/graphics/SVGImageCache.h"
23
24#include "core/fetch/ImageResource.h"
25#include "core/frame/FrameView.h"
26#include "core/page/Page.h"
27#include "core/rendering/svg/RenderSVGRoot.h"
28#include "core/svg/graphics/SVGImage.h"
29#include "core/svg/graphics/SVGImageForContainer.h"
30#include "platform/graphics/GraphicsContext.h"
31#include "platform/graphics/ImageBuffer.h"
32
33namespace WebCore {
34
35SVGImageCache::SVGImageCache(SVGImage* svgImage)
36    : m_svgImage(svgImage)
37{
38    ASSERT(m_svgImage);
39}
40
41SVGImageCache::~SVGImageCache()
42{
43    m_imageForContainerMap.clear();
44}
45
46void SVGImageCache::removeClientFromCache(const ImageResourceClient* client)
47{
48    ASSERT(client);
49    m_imageForContainerMap.remove(client);
50}
51
52void SVGImageCache::setContainerSizeForRenderer(const ImageResourceClient* client, const IntSize& containerSize, float containerZoom)
53{
54    ASSERT(client);
55    ASSERT(!containerSize.isEmpty());
56    ASSERT(containerZoom);
57
58    FloatSize containerSizeWithoutZoom(containerSize);
59    containerSizeWithoutZoom.scale(1 / containerZoom);
60
61    m_imageForContainerMap.set(client, SVGImageForContainer::create(m_svgImage, containerSizeWithoutZoom, containerZoom));
62}
63
64IntSize SVGImageCache::imageSizeForRenderer(const RenderObject* renderer) const
65{
66    IntSize imageSize = m_svgImage->size();
67    if (!renderer)
68        return imageSize;
69
70    ImageForContainerMap::const_iterator it = m_imageForContainerMap.find(renderer);
71    if (it == m_imageForContainerMap.end())
72        return imageSize;
73
74    RefPtr<SVGImageForContainer> imageForContainer = it->value;
75    ASSERT(!imageForContainer->size().isEmpty());
76    return imageForContainer->size();
77}
78
79// FIXME: This doesn't take into account the animation timeline so animations will not
80// restart on page load, nor will two animations in different pages have different timelines.
81Image* SVGImageCache::imageForRenderer(const RenderObject* renderer)
82{
83    if (!renderer)
84        return Image::nullImage();
85
86    ImageForContainerMap::iterator it = m_imageForContainerMap.find(renderer);
87    if (it == m_imageForContainerMap.end())
88        return Image::nullImage();
89
90    RefPtr<SVGImageForContainer> imageForContainer = it->value;
91    ASSERT(!imageForContainer->size().isEmpty());
92    return imageForContainer.get();
93}
94
95} // namespace WebCore
96