Stop using PassRefPtr in platform/graphics
[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     : m_renderer(0)
41 {
42 }
43
44 RenderImageResource::~RenderImageResource()
45 {
46 }
47
48 void RenderImageResource::initialize(RenderElement* renderer)
49 {
50     ASSERT(!m_renderer);
51     ASSERT(renderer);
52     m_renderer = renderer;
53 }
54
55 void RenderImageResource::shutdown()
56 {
57     ASSERT(m_renderer);
58
59     if (m_cachedImage) {
60         image()->stopAnimation();
61         m_cachedImage->removeClient(*m_renderer);
62     }
63 }
64
65 void RenderImageResource::setCachedImage(CachedImage* newImage)
66 {
67     ASSERT(m_renderer);
68
69     if (m_cachedImage == newImage)
70         return;
71
72     if (m_cachedImage)
73         m_cachedImage->removeClient(*m_renderer);
74     m_cachedImage = newImage;
75     if (m_cachedImage) {
76         m_cachedImage->addClient(*m_renderer);
77         if (m_cachedImage->errorOccurred())
78             m_renderer->imageChanged(m_cachedImage.get());
79     }
80 }
81
82 void RenderImageResource::resetAnimation()
83 {
84     ASSERT(m_renderer);
85
86     if (!m_cachedImage)
87         return;
88
89     image()->resetAnimation();
90
91     if (!m_renderer->needsLayout())
92         m_renderer->repaint();
93 }
94
95 RefPtr<Image> RenderImageResource::image(const IntSize&) const
96 {
97     return m_cachedImage ? m_cachedImage->imageForRenderer(m_renderer) : &Image::nullImage();
98 }
99
100 bool RenderImageResource::errorOccurred() const
101 {
102     return m_cachedImage && m_cachedImage->errorOccurred();
103 }
104
105 void RenderImageResource::setContainerSizeForRenderer(const IntSize& imageContainerSize)
106 {
107     ASSERT(m_renderer);
108     if (m_cachedImage)
109         m_cachedImage->setContainerSizeForRenderer(m_renderer, imageContainerSize, m_renderer->style().effectiveZoom());
110 }
111
112 bool RenderImageResource::imageHasRelativeWidth() const
113 {
114     return m_cachedImage ? m_cachedImage->imageHasRelativeWidth() : false;
115 }
116
117 bool RenderImageResource::imageHasRelativeHeight() const
118 {
119     return m_cachedImage ? m_cachedImage->imageHasRelativeHeight() : false;
120 }
121
122 LayoutSize RenderImageResource::imageSize(float multiplier) const
123 {
124     return getImageSize(multiplier, CachedImage::UsedSize);
125 }
126
127 LayoutSize RenderImageResource::intrinsicSize(float multiplier) const
128 {
129     return getImageSize(multiplier, CachedImage::IntrinsicSize);
130 }
131
132 LayoutSize RenderImageResource::getImageSize(float multiplier, CachedImage::SizeType type) const
133 {
134     if (!m_cachedImage)
135         return LayoutSize();
136     LayoutSize size = m_cachedImage->imageSizeForRenderer(m_renderer, multiplier, type);
137     if (is<RenderImage>(m_renderer))
138         size.scale(downcast<RenderImage>(*m_renderer).imageDevicePixelRatio());
139     return size;
140 }
141
142 } // namespace WebCore