Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / css / CSSCrossfadeValue.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc.  All rights reserved.
3  * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "CSSCrossfadeValue.h"
29
30 #include "AnimationUtilities.h"
31 #include "CSSImageValue.h"
32 #include "CachedImage.h"
33 #include "CachedResourceLoader.h"
34 #include "CrossfadeGeneratedImage.h"
35 #include "ImageBuffer.h"
36 #include "RenderElement.h"
37 #include "StyleCachedImage.h"
38 #include "StyleGeneratedImage.h"
39 #include <wtf/text/StringBuilder.h>
40
41 namespace WebCore {
42
43 static inline double blendFunc(double from, double to, double progress)
44 {
45     return blend(from, to, progress);
46 }
47
48 static bool subimageKnownToBeOpaque(CSSValue& value, const RenderElement* renderer)
49 {
50     if (is<CSSImageValue>(value))
51         return downcast<CSSImageValue>(value).knownToBeOpaque(renderer);
52
53     if (is<CSSImageGeneratorValue>(value))
54         return downcast<CSSImageGeneratorValue>(value).knownToBeOpaque(renderer);
55
56     ASSERT_NOT_REACHED();
57
58     return false;
59 }
60
61 CSSCrossfadeValue::~CSSCrossfadeValue()
62 {
63     if (m_cachedFromImage)
64         m_cachedFromImage->removeClient(&m_crossfadeSubimageObserver);
65     if (m_cachedToImage)
66         m_cachedToImage->removeClient(&m_crossfadeSubimageObserver);
67 }
68
69 String CSSCrossfadeValue::customCSSText() const
70 {
71     StringBuilder result;
72     result.appendLiteral("-webkit-cross-fade(");
73     result.append(m_fromValue->cssText());
74     result.appendLiteral(", ");
75     result.append(m_toValue->cssText());
76     result.appendLiteral(", ");
77     result.append(m_percentageValue->cssText());
78     result.append(')');
79     return result.toString();
80 }
81
82 FloatSize CSSCrossfadeValue::fixedSize(const RenderElement* renderer)
83 {
84     float percentage = m_percentageValue->getFloatValue();
85     float inversePercentage = 1 - percentage;
86
87     // FIXME: Skip Content Security Policy check when cross fade is applied to an element in a user agent shadow tree.
88     // See <https://bugs.webkit.org/show_bug.cgi?id=146663>.
89     ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions();
90
91     CachedResourceLoader& cachedResourceLoader = renderer->document().cachedResourceLoader();
92     CachedImage* cachedFromImage = cachedImageForCSSValue(m_fromValue.get(), cachedResourceLoader, options);
93     CachedImage* cachedToImage = cachedImageForCSSValue(m_toValue.get(), cachedResourceLoader, options);
94
95     if (!cachedFromImage || !cachedToImage)
96         return FloatSize();
97
98     FloatSize fromImageSize = cachedFromImage->imageForRenderer(renderer)->size();
99     FloatSize toImageSize = cachedToImage->imageForRenderer(renderer)->size();
100
101     // Rounding issues can cause transitions between images of equal size to return
102     // a different fixed size; avoid performing the interpolation if the images are the same size.
103     if (fromImageSize == toImageSize)
104         return fromImageSize;
105
106     return FloatSize(fromImageSize.width() * inversePercentage + toImageSize.width() * percentage,
107         fromImageSize.height() * inversePercentage + toImageSize.height() * percentage);
108 }
109
110 bool CSSCrossfadeValue::isPending() const
111 {
112     return CSSImageGeneratorValue::subimageIsPending(m_fromValue.get())
113         || CSSImageGeneratorValue::subimageIsPending(m_toValue.get());
114 }
115
116 bool CSSCrossfadeValue::knownToBeOpaque(const RenderElement* renderer) const
117 {
118     return subimageKnownToBeOpaque(*m_fromValue, renderer) && subimageKnownToBeOpaque(*m_toValue, renderer);
119 }
120
121 void CSSCrossfadeValue::loadSubimages(CachedResourceLoader& cachedResourceLoader, const ResourceLoaderOptions& options)
122 {
123     CachedResourceHandle<CachedImage> oldCachedFromImage = m_cachedFromImage;
124     CachedResourceHandle<CachedImage> oldCachedToImage = m_cachedToImage;
125
126     m_cachedFromImage = CSSImageGeneratorValue::cachedImageForCSSValue(m_fromValue.get(), cachedResourceLoader, options);
127     m_cachedToImage = CSSImageGeneratorValue::cachedImageForCSSValue(m_toValue.get(), cachedResourceLoader, options);
128
129     if (m_cachedFromImage != oldCachedFromImage) {
130         if (oldCachedFromImage)
131             oldCachedFromImage->removeClient(&m_crossfadeSubimageObserver);
132         if (m_cachedFromImage)
133             m_cachedFromImage->addClient(&m_crossfadeSubimageObserver);
134     }
135
136     if (m_cachedToImage != oldCachedToImage) {
137         if (oldCachedToImage)
138             oldCachedToImage->removeClient(&m_crossfadeSubimageObserver);
139         if (m_cachedToImage)
140             m_cachedToImage->addClient(&m_crossfadeSubimageObserver);
141     }
142
143     m_crossfadeSubimageObserver.setReady(true);
144 }
145
146 RefPtr<Image> CSSCrossfadeValue::image(RenderElement* renderer, const FloatSize& size)
147 {
148     if (size.isEmpty())
149         return nullptr;
150
151     // FIXME: Skip Content Security Policy check when cross fade is applied to an element in a user agent shadow tree.
152     // See <https://bugs.webkit.org/show_bug.cgi?id=146663>.
153     ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions();
154
155     CachedResourceLoader& cachedResourceLoader = renderer->document().cachedResourceLoader();
156     CachedImage* cachedFromImage = cachedImageForCSSValue(m_fromValue.get(), cachedResourceLoader, options);
157     CachedImage* cachedToImage = cachedImageForCSSValue(m_toValue.get(), cachedResourceLoader, options);
158
159     if (!cachedFromImage || !cachedToImage)
160         return Image::nullImage();
161
162     Image* fromImage = cachedFromImage->imageForRenderer(renderer);
163     Image* toImage = cachedToImage->imageForRenderer(renderer);
164
165     if (!fromImage || !toImage)
166         return Image::nullImage();
167
168     m_generatedImage = CrossfadeGeneratedImage::create(*fromImage, *toImage, m_percentageValue->getFloatValue(), fixedSize(renderer), size);
169
170     return m_generatedImage;
171 }
172
173 void CSSCrossfadeValue::crossfadeChanged(const IntRect&)
174 {
175     for (auto it = clients().begin(), end = clients().end(); it != end; ++it)
176         it->key->imageChanged(static_cast<WrappedImagePtr>(this));
177 }
178
179 void CSSCrossfadeValue::CrossfadeSubimageObserverProxy::imageChanged(CachedImage*, const IntRect* rect)
180 {
181     if (m_ready)
182         m_ownerValue->crossfadeChanged(*rect);
183 }
184
185 bool CSSCrossfadeValue::traverseSubresources(const std::function<bool (const CachedResource&)>& handler) const
186 {
187     if (m_cachedFromImage && handler(*m_cachedFromImage))
188         return true;
189     if (m_cachedToImage && handler(*m_cachedToImage))
190         return true;
191     return false;
192 }
193
194 RefPtr<CSSCrossfadeValue> CSSCrossfadeValue::blend(const CSSCrossfadeValue& from, double progress) const
195 {
196     ASSERT(equalInputImages(from));
197     RefPtr<StyleCachedImage> toStyledImage = StyleCachedImage::create(m_cachedToImage.get());
198     RefPtr<StyleCachedImage> fromStyledImage = StyleCachedImage::create(m_cachedFromImage.get());
199
200     auto fromImageValue = CSSImageValue::create(m_cachedFromImage->url(), fromStyledImage.get());
201     auto toImageValue = CSSImageValue::create(m_cachedToImage->url(), toStyledImage.get());
202
203     RefPtr<CSSCrossfadeValue> crossfadeValue = CSSCrossfadeValue::create(WTFMove(fromImageValue), WTFMove(toImageValue));
204
205     double fromPercentage = from.m_percentageValue->getDoubleValue();
206     if (from.m_percentageValue->isPercentage())
207         fromPercentage /= 100.0;
208     double toPercentage = m_percentageValue->getDoubleValue();
209     if (m_percentageValue->isPercentage())
210         toPercentage /= 100.0;
211     crossfadeValue->setPercentage(CSSPrimitiveValue::create(blendFunc(fromPercentage, toPercentage, progress), CSSPrimitiveValue::CSS_NUMBER));
212     return crossfadeValue;
213 }
214
215 bool CSSCrossfadeValue::equals(const CSSCrossfadeValue& other) const
216 {
217     return equalInputImages(other)
218         && compareCSSValuePtr(m_percentageValue, other.m_percentageValue);
219 }
220
221
222 bool CSSCrossfadeValue::equalInputImages(const CSSCrossfadeValue& other) const
223 {
224     return compareCSSValuePtr(m_fromValue, other.m_fromValue)
225         && compareCSSValuePtr(m_toValue, other.m_toValue);
226 }
227
228 } // namespace WebCore