~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/css/CSSImageGeneratorValue.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 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 "CSSImageGeneratorValue.h"
 
28
 
 
29
#include "CSSCanvasValue.h"
 
30
#include "CSSCrossfadeValue.h"
 
31
#include "CSSGradientValue.h"
 
32
#include "Image.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>
 
38
 
 
39
 
 
40
namespace WTF {
 
41
 
 
42
template<> struct SequenceMemoryInstrumentationTraits<const WebCore::RenderObject*> {
 
43
    template <typename I> static void reportMemoryUsage(I, I, MemoryClassInfo&) { }
 
44
};
 
45
 
 
46
}
 
47
 
 
48
namespace WebCore {
 
49
 
 
50
CSSImageGeneratorValue::CSSImageGeneratorValue(ClassType classType)
 
51
    : CSSValue(classType)
 
52
{
 
53
}
 
54
 
 
55
CSSImageGeneratorValue::~CSSImageGeneratorValue()
 
56
{
 
57
}
 
58
 
 
59
void CSSImageGeneratorValue::addClient(RenderObject* renderer, const IntSize& size)
 
60
{
 
61
    ref();
 
62
 
 
63
    ASSERT(renderer);
 
64
    if (!size.isEmpty())
 
65
        m_sizes.add(size);
 
66
 
 
67
    RenderObjectSizeCountMap::iterator it = m_clients.find(renderer);
 
68
    if (it == m_clients.end())
 
69
        m_clients.add(renderer, SizeAndCount(size, 1));
 
70
    else {
 
71
        SizeAndCount& sizeCount = it->value;
 
72
        ++sizeCount.count;
 
73
    }
 
74
}
 
75
 
 
76
void CSSImageGeneratorValue::removeClient(RenderObject* renderer)
 
77
{
 
78
    ASSERT(renderer);
 
79
    RenderObjectSizeCountMap::iterator it = m_clients.find(renderer);
 
80
    ASSERT(it != m_clients.end());
 
81
 
 
82
    IntSize removedImageSize;
 
83
    SizeAndCount& sizeCount = it->value;
 
84
    IntSize size = sizeCount.size;
 
85
    if (!size.isEmpty()) {
 
86
        m_sizes.remove(size);
 
87
        if (!m_sizes.contains(size))
 
88
            m_images.remove(size);
 
89
    }
 
90
 
 
91
    if (!--sizeCount.count)
 
92
        m_clients.remove(renderer);
 
93
 
 
94
    deref();
 
95
}
 
96
 
 
97
Image* CSSImageGeneratorValue::getImage(RenderObject* renderer, const IntSize& size)
 
98
{
 
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);
 
107
        }
 
108
    }
 
109
 
 
110
    // Don't generate an image for empty sizes.
 
111
    if (size.isEmpty())
 
112
        return 0;
 
113
 
 
114
    // Look up the image in our cache.
 
115
    return m_images.get(size).get();
 
116
}
 
117
 
 
118
void CSSImageGeneratorValue::putImage(const IntSize& size, PassRefPtr<Image> image)
 
119
{
 
120
    m_images.add(size, image);
 
121
}
 
122
 
 
123
void CSSImageGeneratorValue::reportBaseClassMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
 
124
{
 
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
 
129
}
 
130
 
 
131
PassRefPtr<Image> CSSImageGeneratorValue::image(RenderObject* renderer, const IntSize& size)
 
132
{
 
133
    switch (classType()) {
 
134
    case CanvasClass:
 
135
        return static_cast<CSSCanvasValue*>(this)->image(renderer, size);
 
136
    case CrossfadeClass:
 
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);
 
142
    default:
 
143
        ASSERT_NOT_REACHED();
 
144
    }
 
145
    return 0;
 
146
}
 
147
 
 
148
bool CSSImageGeneratorValue::isFixedSize() const
 
149
{
 
150
    switch (classType()) {
 
151
    case CanvasClass:
 
152
        return static_cast<const CSSCanvasValue*>(this)->isFixedSize();
 
153
    case CrossfadeClass:
 
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();
 
159
    default:
 
160
        ASSERT_NOT_REACHED();
 
161
    }
 
162
    return false;
 
163
}
 
164
 
 
165
IntSize CSSImageGeneratorValue::fixedSize(const RenderObject* renderer)
 
166
{
 
167
    switch (classType()) {
 
168
    case CanvasClass:
 
169
        return static_cast<CSSCanvasValue*>(this)->fixedSize(renderer);
 
170
    case CrossfadeClass:
 
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);
 
176
    default:
 
177
        ASSERT_NOT_REACHED();
 
178
    }
 
179
    return IntSize();
 
180
}
 
181
 
 
182
bool CSSImageGeneratorValue::isPending() const
 
183
{
 
184
    switch (classType()) {
 
185
    case CrossfadeClass:
 
186
        return static_cast<const CSSCrossfadeValue*>(this)->isPending();
 
187
    case CanvasClass:
 
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();
 
193
    default:
 
194
        ASSERT_NOT_REACHED();
 
195
    }
 
196
    return false;
 
197
}
 
198
 
 
199
bool CSSImageGeneratorValue::hasAlpha(const RenderObject* renderer) const
 
200
{
 
201
    switch (classType()) {
 
202
    case CrossfadeClass:
 
203
        return static_cast<const CSSCrossfadeValue*>(this)->hasAlpha(renderer);
 
204
    case CanvasClass:
 
205
        return true;
 
206
    case LinearGradientClass:
 
207
        return static_cast<const CSSLinearGradientValue*>(this)->hasAlpha(renderer);
 
208
    case RadialGradientClass:
 
209
        return static_cast<const CSSRadialGradientValue*>(this)->hasAlpha(renderer);
 
210
    default:
 
211
        ASSERT_NOT_REACHED();
 
212
    }
 
213
    return true;
 
214
}
 
215
 
 
216
void CSSImageGeneratorValue::loadSubimages(CachedResourceLoader* cachedResourceLoader)
 
217
{
 
218
    switch (classType()) {
 
219
    case CrossfadeClass:
 
220
        static_cast<CSSCrossfadeValue*>(this)->loadSubimages(cachedResourceLoader);
 
221
        break;
 
222
    case CanvasClass:
 
223
        static_cast<CSSCanvasValue*>(this)->loadSubimages(cachedResourceLoader);
 
224
        break;
 
225
    case LinearGradientClass:
 
226
        static_cast<CSSLinearGradientValue*>(this)->loadSubimages(cachedResourceLoader);
 
227
        break;
 
228
    case RadialGradientClass:
 
229
        static_cast<CSSRadialGradientValue*>(this)->loadSubimages(cachedResourceLoader);
 
230
        break;
 
231
    default:
 
232
        ASSERT_NOT_REACHED();
 
233
    }
 
234
}
 
235
 
 
236
} // namespace WebCore