| 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 | #include "RenderView.h" |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | StyleCachedImage::StyleCachedImage(CSSValue& cssValue) |
| 37 | : m_cssValue(cssValue) |
| 38 | { |
| 39 | ASSERT(is<CSSImageValue>(m_cssValue) || is<CSSImageSetValue>(m_cssValue) || is<CSSCursorImageValue>(m_cssValue)); |
| 40 | |
| 41 | m_isCachedImage = true; |
| 42 | |
| 43 | // CSSImageValue doesn't get invalidated so we can grab the CachedImage immediately if it exists. |
| 44 | if (is<CSSImageValue>(m_cssValue)) { |
| 45 | m_cachedImage = downcast<CSSImageValue>(m_cssValue.get()).cachedImage(); |
| 46 | if (m_cachedImage) |
| 47 | m_isPending = false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | StyleCachedImage::~StyleCachedImage() = default; |
| 52 | |
| 53 | bool StyleCachedImage::operator==(const StyleImage& other) const |
| 54 | { |
| 55 | if (!is<StyleCachedImage>(other)) |
| 56 | return false; |
| 57 | auto& otherCached = downcast<StyleCachedImage>(other); |
| 58 | if (&otherCached == this) |
| 59 | return true; |
| 60 | if (m_scaleFactor != otherCached.m_scaleFactor) |
| 61 | return false; |
| 62 | if (m_cssValue.ptr() == otherCached.m_cssValue.ptr()) |
| 63 | return true; |
| 64 | if (m_cachedImage && m_cachedImage == otherCached.m_cachedImage) |
| 65 | return true; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | URL StyleCachedImage::imageURL() |
| 70 | { |
| 71 | if (is<CSSImageValue>(m_cssValue)) |
| 72 | return downcast<CSSImageValue>(m_cssValue.get()).url(); |
| 73 | |
| 74 | if (is<CSSImageSetValue>(m_cssValue)) |
| 75 | return downcast<CSSImageSetValue>(m_cssValue.get()).bestImageForScaleFactorURL(); |
| 76 | |
| 77 | if (is<CSSCursorImageValue>(m_cssValue.get())) |
| 78 | return downcast<CSSCursorImageValue>(m_cssValue.get()).imageURL(); |
| 79 | |
| 80 | ASSERT_NOT_REACHED(); |
| 81 | return { }; |
| 82 | } |
| 83 | |
| 84 | void StyleCachedImage::load(CachedResourceLoader& loader, const ResourceLoaderOptions& options) |
| 85 | { |
| 86 | ASSERT(m_isPending); |
| 87 | m_isPending = false; |
| 88 | |
| 89 | if (is<CSSImageValue>(m_cssValue)) { |
| 90 | auto& imageValue = downcast<CSSImageValue>(m_cssValue.get()); |
| 91 | m_cachedImage = imageValue.loadImage(loader, options); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (is<CSSImageSetValue>(m_cssValue)) { |
| 96 | auto& imageSetValue = downcast<CSSImageSetValue>(m_cssValue.get()); |
| 97 | std::tie(m_cachedImage, m_scaleFactor) = imageSetValue.loadBestFitImage(loader, options); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (is<CSSCursorImageValue>(m_cssValue.get())) { |
| 102 | auto& cursorValue = downcast<CSSCursorImageValue>(m_cssValue.get()); |
| 103 | std::tie(m_cachedImage, m_scaleFactor) = cursorValue.loadImage(loader, options); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | CachedImage* StyleCachedImage::cachedImage() const |
| 109 | { |
| 110 | return m_cachedImage.get(); |
| 111 | } |
| 112 | |
| 113 | Ref<CSSValue> StyleCachedImage::cssValue() const |
| 114 | { |
| 115 | return m_cssValue.copyRef(); |
| 116 | } |
| 117 | |
| 118 | bool StyleCachedImage::canRender(const RenderElement* renderer, float multiplier) const |
| 119 | { |
| 120 | if (!m_cachedImage) |
| 121 | return false; |
| 122 | return m_cachedImage->canRender(renderer, multiplier); |
| 123 | } |
| 124 | |
| 125 | bool StyleCachedImage::isPending() const |
| 126 | { |
| 127 | return m_isPending; |
| 128 | } |
| 129 | |
| 130 | bool StyleCachedImage::isLoaded() const |
| 131 | { |
| 132 | if (!m_cachedImage) |
| 133 | return false; |
| 134 | return m_cachedImage->isLoaded(); |
| 135 | } |
| 136 | |
| 137 | bool StyleCachedImage::errorOccurred() const |
| 138 | { |
| 139 | if (!m_cachedImage) |
| 140 | return false; |
| 141 | return m_cachedImage->errorOccurred(); |
| 142 | } |
| 143 | |
| 144 | FloatSize StyleCachedImage::imageSize(const RenderElement* renderer, float multiplier) const |
| 145 | { |
| 146 | if (!m_cachedImage) |
| 147 | return { }; |
| 148 | FloatSize size = m_cachedImage->imageSizeForRenderer(renderer, multiplier); |
| 149 | size.scale(1 / m_scaleFactor); |
| 150 | return size; |
| 151 | } |
| 152 | |
| 153 | bool StyleCachedImage::imageHasRelativeWidth() const |
| 154 | { |
| 155 | if (!m_cachedImage) |
| 156 | return false; |
| 157 | return m_cachedImage->imageHasRelativeWidth(); |
| 158 | } |
| 159 | |
| 160 | bool StyleCachedImage::imageHasRelativeHeight() const |
| 161 | { |
| 162 | if (!m_cachedImage) |
| 163 | return false; |
| 164 | return m_cachedImage->imageHasRelativeHeight(); |
| 165 | } |
| 166 | |
| 167 | void StyleCachedImage::computeIntrinsicDimensions(const RenderElement*, Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) |
| 168 | { |
| 169 | if (!m_cachedImage) |
| 170 | return; |
| 171 | m_cachedImage->computeIntrinsicDimensions(intrinsicWidth, intrinsicHeight, intrinsicRatio); |
| 172 | } |
| 173 | |
| 174 | bool StyleCachedImage::usesImageContainerSize() const |
| 175 | { |
| 176 | if (!m_cachedImage) |
| 177 | return false; |
| 178 | return m_cachedImage->usesImageContainerSize(); |
| 179 | } |
| 180 | |
| 181 | void StyleCachedImage::setContainerContextForRenderer(const RenderElement& renderer, const FloatSize& containerSize, float containerZoom) |
| 182 | { |
| 183 | if (!m_cachedImage) |
| 184 | return; |
| 185 | m_cachedImage->setContainerContextForClient(renderer, LayoutSize(containerSize), containerZoom, imageURL()); |
| 186 | } |
| 187 | |
| 188 | void StyleCachedImage::addClient(RenderElement* renderer) |
| 189 | { |
| 190 | ASSERT(!m_isPending); |
| 191 | if (!m_cachedImage) |
| 192 | return; |
| 193 | ASSERT(renderer); |
| 194 | m_cachedImage->addClient(*renderer); |
| 195 | } |
| 196 | |
| 197 | void StyleCachedImage::removeClient(RenderElement* renderer) |
| 198 | { |
| 199 | ASSERT(!m_isPending); |
| 200 | if (!m_cachedImage) |
| 201 | return; |
| 202 | ASSERT(renderer); |
| 203 | |
| 204 | m_cachedImage->removeClient(*renderer); |
| 205 | } |
| 206 | |
| 207 | RefPtr<Image> StyleCachedImage::image(RenderElement* renderer, const FloatSize&) const |
| 208 | { |
| 209 | ASSERT(!m_isPending); |
| 210 | if (!m_cachedImage) |
| 211 | return nullptr; |
| 212 | return m_cachedImage->imageForRenderer(renderer); |
| 213 | } |
| 214 | |
| 215 | float StyleCachedImage::imageScaleFactor() const |
| 216 | { |
| 217 | return m_scaleFactor; |
| 218 | } |
| 219 | |
| 220 | bool StyleCachedImage::knownToBeOpaque(const RenderElement* renderer) const |
| 221 | { |
| 222 | if (!m_cachedImage) |
| 223 | return false; |
| 224 | return m_cachedImage->currentFrameKnownToBeOpaque(renderer); |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | |