d8de5f9a1deb8f9e6d4ca1a1272dc0d41cd803a9
[WebKit-https.git] / Source / WebCore / rendering / RenderImageResource.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll <knoll@kde.org>
3  * Copyright (C) 1999 Antti Koivisto <koivisto@kde.org>
4  * Copyright (C) 2000 Dirk Mueller <mueller@kde.org>
5  * Copyright (C) 2006 Allan Sandfeld Jensen <kde@carewolf.com>
6  * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
7  * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
8  * Copyright (C) 2010 Google Inc. All rights reserved.
9  * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public License
22  * along with this library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  *
26  */
27
28 #include "config.h"
29 #include "RenderImageResource.h"
30
31 #include "CachedImage.h"
32 #include "Image.h"
33 #include "RenderElement.h"
34 #include "RenderImage.h"
35 #include "RenderImageResourceStyleImage.h"
36
37 namespace WebCore {
38
39 RenderImageResource::RenderImageResource()
40 {
41 }
42
43 RenderImageResource::~RenderImageResource()
44 {
45 }
46
47 void RenderImageResource::initialize(RenderElement* renderer)
48 {
49     ASSERT(!m_renderer);
50     ASSERT(renderer);
51     m_renderer = renderer;
52 }
53
54 void RenderImageResource::shutdown()
55 {
56     if (!m_cachedImage)
57         return;
58
59     ASSERT(m_renderer);
60     image()->stopAnimation();
61     m_cachedImage->removeClient(*m_renderer);
62 }
63
64 void RenderImageResource::setCachedImage(CachedImage* newImage)
65 {
66     ASSERT(m_renderer);
67
68     if (m_cachedImage == newImage)
69         return;
70
71     if (m_cachedImage)
72         m_cachedImage->removeClient(*m_renderer);
73     m_cachedImage = newImage;
74     if (m_cachedImage) {
75         m_cachedImage->addClient(*m_renderer);
76         if (m_cachedImage->errorOccurred())
77             m_renderer->imageChanged(m_cachedImage.get());
78     }
79 }
80
81 void RenderImageResource::resetAnimation()
82 {
83     ASSERT(m_renderer);
84
85     if (!m_cachedImage)
86         return;
87
88     image()->resetAnimation();
89
90     if (!m_renderer->needsLayout())
91         m_renderer->repaint();
92 }
93
94 RefPtr<Image> RenderImageResource::image(const IntSize&) const
95 {
96     return m_cachedImage ? m_cachedImage->imageForRenderer(m_renderer) : &Image::nullImage();
97 }
98
99 bool RenderImageResource::errorOccurred() const
100 {
101     return m_cachedImage && m_cachedImage->errorOccurred();
102 }
103
104 void RenderImageResource::setContainerSizeForRenderer(const IntSize& imageContainerSize)
105 {
106     ASSERT(m_renderer);
107     if (m_cachedImage)
108         m_cachedImage->setContainerSizeForRenderer(m_renderer, imageContainerSize, m_renderer->style().effectiveZoom());
109 }
110
111 bool RenderImageResource::imageHasRelativeWidth() const
112 {
113     return m_cachedImage ? m_cachedImage->imageHasRelativeWidth() : false;
114 }
115
116 bool RenderImageResource::imageHasRelativeHeight() const
117 {
118     return m_cachedImage ? m_cachedImage->imageHasRelativeHeight() : false;
119 }
120
121 LayoutSize RenderImageResource::imageSize(float multiplier) const
122 {
123     return getImageSize(multiplier, CachedImage::UsedSize);
124 }
125
126 LayoutSize RenderImageResource::intrinsicSize(float multiplier) const
127 {
128     return getImageSize(multiplier, CachedImage::IntrinsicSize);
129 }
130
131 LayoutSize RenderImageResource::getImageSize(float multiplier, CachedImage::SizeType type) const
132 {
133     if (!m_cachedImage)
134         return LayoutSize();
135     LayoutSize size = m_cachedImage->imageSizeForRenderer(m_renderer, multiplier, type);
136     if (is<RenderImage>(m_renderer))
137         size.scale(downcast<RenderImage>(*m_renderer).imageDevicePixelRatio());
138     return size;
139 }
140
141 } // namespace WebCore