2
* Copyright (C) 2008 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
27
#include "CSSImageGeneratorValue.h"
29
#include "CSSCanvasValue.h"
30
#include "CSSCrossfadeValue.h"
31
#include "CSSGradientValue.h"
33
#include "RenderObject.h"
34
#include "WebCoreMemoryInstrumentation.h"
35
#include <wtf/MemoryInstrumentationHashCountedSet.h>
36
#include <wtf/MemoryInstrumentationHashMap.h>
37
#include <wtf/text/WTFString.h>
42
template<> struct SequenceMemoryInstrumentationTraits<const WebCore::RenderObject*> {
43
template <typename I> static void reportMemoryUsage(I, I, MemoryClassInfo&) { }
50
CSSImageGeneratorValue::CSSImageGeneratorValue(ClassType classType)
55
CSSImageGeneratorValue::~CSSImageGeneratorValue()
59
void CSSImageGeneratorValue::addClient(RenderObject* renderer, const IntSize& size)
67
RenderObjectSizeCountMap::iterator it = m_clients.find(renderer);
68
if (it == m_clients.end())
69
m_clients.add(renderer, SizeAndCount(size, 1));
71
SizeAndCount& sizeCount = it->value;
76
void CSSImageGeneratorValue::removeClient(RenderObject* renderer)
79
RenderObjectSizeCountMap::iterator it = m_clients.find(renderer);
80
ASSERT(it != m_clients.end());
82
IntSize removedImageSize;
83
SizeAndCount& sizeCount = it->value;
84
IntSize size = sizeCount.size;
85
if (!size.isEmpty()) {
87
if (!m_sizes.contains(size))
88
m_images.remove(size);
91
if (!--sizeCount.count)
92
m_clients.remove(renderer);
97
Image* CSSImageGeneratorValue::getImage(RenderObject* renderer, const IntSize& size)
99
RenderObjectSizeCountMap::iterator it = m_clients.find(renderer);
100
if (it != m_clients.end()) {
101
SizeAndCount& sizeCount = it->value;
102
IntSize oldSize = sizeCount.size;
103
if (oldSize != size) {
104
RefPtr<CSSImageGeneratorValue> protect(this);
105
removeClient(renderer);
106
addClient(renderer, size);
110
// Don't generate an image for empty sizes.
114
// Look up the image in our cache.
115
return m_images.get(size).get();
118
void CSSImageGeneratorValue::putImage(const IntSize& size, PassRefPtr<Image> image)
120
m_images.add(size, image);
123
void CSSImageGeneratorValue::reportBaseClassMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
125
MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS);
126
info.addMember(m_sizes);
127
info.addMember(m_clients);
128
info.addMember(m_images); // FIXME: instrument Image
131
PassRefPtr<Image> CSSImageGeneratorValue::image(RenderObject* renderer, const IntSize& size)
133
switch (classType()) {
135
return static_cast<CSSCanvasValue*>(this)->image(renderer, size);
137
return static_cast<CSSCrossfadeValue*>(this)->image(renderer, size);
138
case LinearGradientClass:
139
return static_cast<CSSLinearGradientValue*>(this)->image(renderer, size);
140
case RadialGradientClass:
141
return static_cast<CSSRadialGradientValue*>(this)->image(renderer, size);
143
ASSERT_NOT_REACHED();
148
bool CSSImageGeneratorValue::isFixedSize() const
150
switch (classType()) {
152
return static_cast<const CSSCanvasValue*>(this)->isFixedSize();
154
return static_cast<const CSSCrossfadeValue*>(this)->isFixedSize();
155
case LinearGradientClass:
156
return static_cast<const CSSLinearGradientValue*>(this)->isFixedSize();
157
case RadialGradientClass:
158
return static_cast<const CSSRadialGradientValue*>(this)->isFixedSize();
160
ASSERT_NOT_REACHED();
165
IntSize CSSImageGeneratorValue::fixedSize(const RenderObject* renderer)
167
switch (classType()) {
169
return static_cast<CSSCanvasValue*>(this)->fixedSize(renderer);
171
return static_cast<CSSCrossfadeValue*>(this)->fixedSize(renderer);
172
case LinearGradientClass:
173
return static_cast<CSSLinearGradientValue*>(this)->fixedSize(renderer);
174
case RadialGradientClass:
175
return static_cast<CSSRadialGradientValue*>(this)->fixedSize(renderer);
177
ASSERT_NOT_REACHED();
182
bool CSSImageGeneratorValue::isPending() const
184
switch (classType()) {
186
return static_cast<const CSSCrossfadeValue*>(this)->isPending();
188
return static_cast<const CSSCanvasValue*>(this)->isPending();
189
case LinearGradientClass:
190
return static_cast<const CSSLinearGradientValue*>(this)->isPending();
191
case RadialGradientClass:
192
return static_cast<const CSSRadialGradientValue*>(this)->isPending();
194
ASSERT_NOT_REACHED();
199
bool CSSImageGeneratorValue::hasAlpha(const RenderObject* renderer) const
201
switch (classType()) {
203
return static_cast<const CSSCrossfadeValue*>(this)->hasAlpha(renderer);
206
case LinearGradientClass:
207
return static_cast<const CSSLinearGradientValue*>(this)->hasAlpha(renderer);
208
case RadialGradientClass:
209
return static_cast<const CSSRadialGradientValue*>(this)->hasAlpha(renderer);
211
ASSERT_NOT_REACHED();
216
void CSSImageGeneratorValue::loadSubimages(CachedResourceLoader* cachedResourceLoader)
218
switch (classType()) {
220
static_cast<CSSCrossfadeValue*>(this)->loadSubimages(cachedResourceLoader);
223
static_cast<CSSCanvasValue*>(this)->loadSubimages(cachedResourceLoader);
225
case LinearGradientClass:
226
static_cast<CSSLinearGradientValue*>(this)->loadSubimages(cachedResourceLoader);
228
case RadialGradientClass:
229
static_cast<CSSRadialGradientValue*>(this)->loadSubimages(cachedResourceLoader);
232
ASSERT_NOT_REACHED();
236
} // namespace WebCore