Move RenderObject::shouldRespectImageOrientation to RenderElement.
[WebKit-https.git] / Source / WebCore / rendering / style / StyleCachedImage.cpp
1 /*
2  * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3  *           (C) 2000 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2003, 2005-2008, 2016 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include "config.h"
25 #include "StyleCachedImage.h"
26
27 #include "CSSCursorImageValue.h"
28 #include "CSSImageSetValue.h"
29 #include "CSSImageValue.h"
30 #include "CachedImage.h"
31 #include "RenderElement.h"
32
33 namespace WebCore {
34
35 StyleCachedImage::StyleCachedImage(CSSValue& cssValue)
36     : m_cssValue(cssValue)
37 {
38     ASSERT(is<CSSImageValue>(m_cssValue) || is<CSSImageSetValue>(m_cssValue) || is<CSSCursorImageValue>(m_cssValue));
39
40     m_isCachedImage = true;
41
42     // CSSImageValue doesn't get invalidated so we can grab the CachedImage immediately if it exists.
43     if (is<CSSImageValue>(m_cssValue)) {
44         m_cachedImage = downcast<CSSImageValue>(m_cssValue.get()).cachedImage();
45         if (m_cachedImage)
46             m_isPending = false;
47     }
48 }
49
50 StyleCachedImage::~StyleCachedImage()
51 {
52 }
53
54 bool StyleCachedImage::operator==(const StyleImage& other) const
55 {
56     if (!is<StyleCachedImage>(other))
57         return false;
58     auto& otherCached = downcast<StyleCachedImage>(other);
59     if (&otherCached == this)
60         return true;
61     if (m_scaleFactor != otherCached.m_scaleFactor)
62         return false;
63     if (m_cssValue.ptr() == otherCached.m_cssValue.ptr())
64         return true;
65     if (m_cachedImage && m_cachedImage == otherCached.m_cachedImage)
66         return true;
67     return false;
68 }
69
70 void StyleCachedImage::load(CachedResourceLoader& loader, const ResourceLoaderOptions& options)
71 {
72     ASSERT(m_isPending);
73     m_isPending = false;
74
75     if (is<CSSImageValue>(m_cssValue)) {
76         auto& imageValue = downcast<CSSImageValue>(m_cssValue.get());
77         m_cachedImage = imageValue.loadImage(loader, options);
78         return;
79     }
80
81     if (is<CSSImageSetValue>(m_cssValue)) {
82         auto& imageSetValue = downcast<CSSImageSetValue>(m_cssValue.get());
83         std::tie(m_cachedImage, m_scaleFactor) = imageSetValue.loadBestFitImage(loader, options);
84         return;
85     }
86
87     if (is<CSSCursorImageValue>(m_cssValue.get())) {
88         auto& cursorValue = downcast<CSSCursorImageValue>(m_cssValue.get());
89         std::tie(m_cachedImage, m_scaleFactor) = cursorValue.loadImage(loader, options);
90         return;
91     }
92 }
93
94 CachedImage* StyleCachedImage::cachedImage() const
95 {
96     return m_cachedImage.get();
97 }
98
99 PassRefPtr<CSSValue> StyleCachedImage::cssValue() const
100 {
101     return const_cast<CSSValue*>(m_cssValue.ptr());
102 }
103
104 bool StyleCachedImage::canRender(const RenderElement* renderer, float multiplier) const
105 {
106     if (!m_cachedImage)
107         return false;
108     return m_cachedImage->canRender(renderer, multiplier);
109 }
110
111 bool StyleCachedImage::isPending() const
112 {
113     return m_isPending;
114 }
115
116 bool StyleCachedImage::isLoaded() const
117 {
118     if (!m_cachedImage)
119         return false;
120     return m_cachedImage->isLoaded();
121 }
122
123 bool StyleCachedImage::errorOccurred() const
124 {
125     if (!m_cachedImage)
126         return false;
127     return m_cachedImage->errorOccurred();
128 }
129
130 FloatSize StyleCachedImage::imageSize(const RenderElement* renderer, float multiplier) const
131 {
132     if (!m_cachedImage)
133         return { };
134     FloatSize size = m_cachedImage->imageSizeForRenderer(renderer, multiplier);
135     size.scale(1 / m_scaleFactor);
136     return size;
137 }
138
139 bool StyleCachedImage::imageHasRelativeWidth() const
140 {
141     if (!m_cachedImage)
142         return false;
143     return m_cachedImage->imageHasRelativeWidth();
144 }
145
146 bool StyleCachedImage::imageHasRelativeHeight() const
147 {
148     if (!m_cachedImage)
149         return false;
150     return m_cachedImage->imageHasRelativeHeight();
151 }
152
153 void StyleCachedImage::computeIntrinsicDimensions(const RenderElement*, Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
154 {
155     if (!m_cachedImage)
156         return;
157     m_cachedImage->computeIntrinsicDimensions(intrinsicWidth, intrinsicHeight, intrinsicRatio);
158 }
159
160 bool StyleCachedImage::usesImageContainerSize() const
161 {
162     if (!m_cachedImage)
163         return false;
164     return m_cachedImage->usesImageContainerSize();
165 }
166
167 void StyleCachedImage::setContainerSizeForRenderer(const RenderElement* renderer, const FloatSize& imageContainerSize, float imageContainerZoomFactor)
168 {
169     if (!m_cachedImage)
170         return;
171     m_cachedImage->setContainerSizeForRenderer(renderer, LayoutSize(imageContainerSize), imageContainerZoomFactor);
172 }
173
174 void StyleCachedImage::addClient(RenderElement* renderer)
175 {
176     ASSERT(!m_isPending);
177     if (!m_cachedImage)
178         return;
179     m_cachedImage->addClient(renderer);
180 }
181
182 void StyleCachedImage::removeClient(RenderElement* renderer)
183 {
184     ASSERT(!m_isPending);
185     if (!m_cachedImage)
186         return;
187     m_cachedImage->removeClient(renderer);
188 }
189
190 RefPtr<Image> StyleCachedImage::image(RenderElement* renderer, const FloatSize&) const
191 {
192     ASSERT(!m_isPending);
193     if (!m_cachedImage)
194         return nullptr;
195     return m_cachedImage->imageForRenderer(renderer);
196 }
197
198 float StyleCachedImage::imageScaleFactor() const
199 {
200     return m_scaleFactor;
201 }
202
203 bool StyleCachedImage::knownToBeOpaque(const RenderElement* renderer) const
204 {
205     if (!m_cachedImage)
206         return false;
207     return m_cachedImage->currentFrameKnownToBeOpaque(renderer);
208 }
209
210 }