Don't reuse cached stylesheet with failed or canceled resource loads
[WebKit-https.git] / Source / WebCore / css / CSSCrossfadeValue.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "CSSCrossfadeValue.h"
28
29 #include "CSSImageValue.h"
30 #include "CachedImage.h"
31 #include "CachedResourceLoader.h"
32 #include "CrossfadeGeneratedImage.h"
33 #include "ImageBuffer.h"
34 #include "RenderObject.h"
35 #include "StyleCachedImage.h"
36 #include "StyleGeneratedImage.h"
37
38 namespace WebCore {
39
40 static bool subimageIsPending(CSSValue* value)
41 {
42     if (value->isImageValue())
43         return static_cast<CSSImageValue*>(value)->cachedOrPendingImage()->isPendingImage();
44     
45     if (value->isImageGeneratorValue())
46         return static_cast<CSSImageGeneratorValue*>(value)->isPending();
47     
48     ASSERT_NOT_REACHED();
49     
50     return false;
51 }
52
53 static CachedImage* cachedImageForCSSValue(CSSValue* value, CachedResourceLoader* cachedResourceLoader)
54 {
55     if (!value)
56         return 0;
57
58     if (value->isImageValue()) {
59         StyleCachedImage* styleCachedImage = static_cast<CSSImageValue*>(value)->cachedImage(cachedResourceLoader);
60         if (!styleCachedImage)
61             return 0;
62
63         return styleCachedImage->cachedImage();
64     }
65     
66     if (value->isImageGeneratorValue()) {
67         static_cast<CSSImageGeneratorValue*>(value)->loadSubimages(cachedResourceLoader);
68         // FIXME: Handle CSSImageGeneratorValue (and thus cross-fades with gradients and canvas).
69         return 0;
70     }
71     
72     ASSERT_NOT_REACHED();
73     
74     return 0;
75 }
76
77 CSSCrossfadeValue::~CSSCrossfadeValue()
78 {
79     if (m_cachedFromImage)
80         m_cachedFromImage->removeClient(&m_crossfadeSubimageObserver);
81     if (m_cachedToImage)
82         m_cachedToImage->removeClient(&m_crossfadeSubimageObserver);
83 }
84
85 String CSSCrossfadeValue::customCssText() const
86 {
87     String result = "-webkit-cross-fade(";
88     result += m_fromValue->cssText() + ", ";
89     result += m_toValue->cssText() + ", ";
90     result += m_percentageValue->cssText();
91     result += ")";
92     return result;
93 }
94
95 IntSize CSSCrossfadeValue::fixedSize(const RenderObject* renderer)
96 {
97     float percentage = m_percentageValue->getFloatValue();
98     float inversePercentage = 1 - percentage;
99
100     CachedResourceLoader* cachedResourceLoader = renderer->document()->cachedResourceLoader();
101     CachedImage* cachedFromImage = cachedImageForCSSValue(m_fromValue.get(), cachedResourceLoader);
102     CachedImage* cachedToImage = cachedImageForCSSValue(m_toValue.get(), cachedResourceLoader);
103
104     if (!cachedFromImage || !cachedToImage)
105         return IntSize();
106
107     IntSize fromImageSize = cachedFromImage->imageForRenderer(renderer)->size();
108     IntSize toImageSize = cachedToImage->imageForRenderer(renderer)->size();
109
110     // Rounding issues can cause transitions between images of equal size to return
111     // a different fixed size; avoid performing the interpolation if the images are the same size.
112     if (fromImageSize == toImageSize)
113         return fromImageSize;
114
115     return IntSize(fromImageSize.width() * inversePercentage + toImageSize.width() * percentage,
116         fromImageSize.height() * inversePercentage + toImageSize.height() * percentage);
117 }
118
119 bool CSSCrossfadeValue::isPending() const
120 {
121     return subimageIsPending(m_fromValue.get()) || subimageIsPending(m_toValue.get());
122 }
123
124 void CSSCrossfadeValue::loadSubimages(CachedResourceLoader* cachedResourceLoader)
125 {
126     m_cachedFromImage = cachedImageForCSSValue(m_fromValue.get(), cachedResourceLoader);
127     m_cachedToImage = cachedImageForCSSValue(m_toValue.get(), cachedResourceLoader);
128
129     if (m_cachedFromImage)
130         m_cachedFromImage->addClient(&m_crossfadeSubimageObserver);
131     if (m_cachedToImage)
132         m_cachedToImage->addClient(&m_crossfadeSubimageObserver);
133
134     m_crossfadeSubimageObserver.setReady(true);
135 }
136
137 PassRefPtr<Image> CSSCrossfadeValue::image(RenderObject* renderer, const IntSize& size)
138 {
139     if (size.isEmpty())
140         return 0;
141
142     CachedResourceLoader* cachedResourceLoader = renderer->document()->cachedResourceLoader();
143     CachedImage* cachedFromImage = cachedImageForCSSValue(m_fromValue.get(), cachedResourceLoader);
144     CachedImage* cachedToImage = cachedImageForCSSValue(m_toValue.get(), cachedResourceLoader);
145
146     if (!cachedFromImage || !cachedToImage)
147         return Image::nullImage();
148
149     Image* fromImage = cachedFromImage->imageForRenderer(renderer);
150     Image* toImage = cachedToImage->imageForRenderer(renderer);
151
152     if (!fromImage || !toImage)
153         return Image::nullImage();
154
155     m_generatedImage = CrossfadeGeneratedImage::create(fromImage, toImage, m_percentageValue->getFloatValue(), fixedSize(renderer), size);
156
157     return m_generatedImage.release();
158 }
159
160 void CSSCrossfadeValue::crossfadeChanged(const IntRect&)
161 {
162     RenderObjectSizeCountMap::const_iterator end = clients().end();
163     for (RenderObjectSizeCountMap::const_iterator curr = clients().begin(); curr != end; ++curr) {
164         RenderObject* client = const_cast<RenderObject*>(curr->first);
165         client->imageChanged(static_cast<WrappedImagePtr>(this));
166     }
167 }
168
169 void CSSCrossfadeValue::CrossfadeSubimageObserverProxy::imageChanged(CachedImage*, const IntRect* rect)
170 {
171     if (m_ready)
172         m_ownerValue->crossfadeChanged(*rect);
173 }
174
175 bool CSSCrossfadeValue::hasFailedOrCanceledSubresources() const
176 {
177     if (m_cachedFromImage && m_cachedFromImage->loadFailedOrCanceled())
178         return true;
179     if (m_cachedToImage && m_cachedToImage->loadFailedOrCanceled())
180         return true;
181     return false;
182 }
183
184 } // namespace WebCore