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

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/blackberry/TextureCacheCompositingThread.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) 2011, 2012 Research In Motion Limited. All rights reserved.
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
#include "TextureCacheCompositingThread.h"
 
21
 
 
22
#if USE(ACCELERATED_COMPOSITING)
 
23
 
 
24
#include "IntRect.h"
 
25
 
 
26
#include <GLES2/gl2.h>
 
27
#include <SkBitmap.h>
 
28
 
 
29
#define DEBUG_TEXTURE_MEMORY_USAGE 0
 
30
 
 
31
namespace WebCore {
 
32
 
 
33
static const int defaultMemoryLimit = 64 * 1024 * 1024; // Measured in bytes.
 
34
 
 
35
// Used to protect a newly created texture from being immediately evicted
 
36
// before someone has a chance to protect it for legitimate reasons.
 
37
class TextureProtector {
 
38
public:
 
39
    TextureProtector(Texture* texture)
 
40
        : m_texture(texture)
 
41
    {
 
42
        m_texture->protect();
 
43
    }
 
44
 
 
45
    ~TextureProtector()
 
46
    {
 
47
        m_texture->unprotect();
 
48
    }
 
49
 
 
50
private:
 
51
    Texture* m_texture;
 
52
};
 
53
 
 
54
TextureCacheCompositingThread::TextureCacheCompositingThread()
 
55
    : m_memoryUsage(0)
 
56
    , m_memoryLimit(defaultMemoryLimit)
 
57
{
 
58
}
 
59
 
 
60
unsigned TextureCacheCompositingThread::allocateTextureId()
 
61
{
 
62
    unsigned texid;
 
63
    glGenTextures(1, &texid);
 
64
    glBindTexture(GL_TEXTURE_2D, texid);
 
65
    if (!glIsTexture(texid))
 
66
        return 0;
 
67
 
 
68
    // Do basic linear filtering on resize.
 
69
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
70
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
71
    // NPOT textures in GL ES only work when the wrap mode is set to GL_CLAMP_TO_EDGE.
 
72
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
73
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
74
    return texid;
 
75
}
 
76
 
 
77
void TextureCacheCompositingThread::freeTextureId(unsigned id)
 
78
{
 
79
    if (id)
 
80
        glDeleteTextures(1, &id);
 
81
}
 
82
 
 
83
void TextureCacheCompositingThread::collectGarbage()
 
84
{
 
85
    int delta = 0;
 
86
 
 
87
    for (Garbage::iterator it = m_garbage.begin(); it != m_garbage.end(); ++it) {
 
88
        ZombieTexture& zombie = *it;
 
89
        freeTextureId(zombie.id);
 
90
        delta += zombie.size.width() * zombie.size.height() * Texture::bytesPerPixel();
 
91
    }
 
92
    m_garbage.clear();
 
93
 
 
94
    if (delta)
 
95
        decMemoryUsage(delta);
 
96
}
 
97
 
 
98
void TextureCacheCompositingThread::textureResized(Texture* texture, const IntSize& oldSize)
 
99
{
 
100
    int delta = (texture->width() * texture->height() - oldSize.width() * oldSize.height()) * Texture::bytesPerPixel();
 
101
    incMemoryUsage(delta);
 
102
    if (delta > 0)
 
103
        prune();
 
104
}
 
105
 
 
106
void TextureCacheCompositingThread::textureDestroyed(Texture* texture)
 
107
{
 
108
    if (texture->isColor()) {
 
109
        m_garbage.append(ZombieTexture(texture));
 
110
        return;
 
111
    }
 
112
 
 
113
    evict(m_textures.find(texture));
 
114
}
 
115
 
 
116
bool TextureCacheCompositingThread::install(Texture* texture)
 
117
{
 
118
    if (!texture)
 
119
        return true;
 
120
 
 
121
    if (!texture->hasTexture()) {
 
122
        unsigned textureId = allocateTextureId();
 
123
        if (!textureId)
 
124
            return false;
 
125
 
 
126
        texture->setTextureId(textureId);
 
127
    }
 
128
 
 
129
    if (!texture->isColor()) {
 
130
        if (!m_textures.contains(texture))
 
131
            m_textures.add(texture);
 
132
    }
 
133
 
 
134
    return true;
 
135
}
 
136
 
 
137
void TextureCacheCompositingThread::evict(const TextureSet::iterator& it)
 
138
{
 
139
    if (it == m_textures.end())
 
140
        return;
 
141
 
 
142
    Texture* texture = *it;
 
143
    if (texture->hasTexture())
 
144
        m_garbage.append(ZombieTexture(texture));
 
145
 
 
146
    texture->setTextureId(0);
 
147
 
 
148
    m_textures.remove(it);
 
149
}
 
150
 
 
151
void TextureCacheCompositingThread::textureAccessed(Texture* texture)
 
152
{
 
153
    if (texture->isColor())
 
154
        return;
 
155
 
 
156
    TextureSet::iterator it = m_textures.find(texture);
 
157
    if (it == m_textures.end())
 
158
        return;
 
159
 
 
160
    m_textures.remove(it);
 
161
    m_textures.add(texture);
 
162
}
 
163
 
 
164
TextureCacheCompositingThread* textureCacheCompositingThread()
 
165
{
 
166
    static TextureCacheCompositingThread* staticCache = new TextureCacheCompositingThread;
 
167
    return staticCache;
 
168
}
 
169
 
 
170
void TextureCacheCompositingThread::prune(size_t limit)
 
171
{
 
172
    while (m_memoryUsage > limit) {
 
173
        bool found = false;
 
174
        for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); ++it) {
 
175
            Texture* texture = *it;
 
176
            if (texture->isProtected())
 
177
                continue;
 
178
            evict(it);
 
179
            found = true;
 
180
            break;
 
181
        }
 
182
        if (!found)
 
183
            break;
 
184
    }
 
185
}
 
186
 
 
187
void TextureCacheCompositingThread::clear()
 
188
{
 
189
    m_cache.clear();
 
190
    m_colors.clear();
 
191
    collectGarbage();
 
192
}
 
193
 
 
194
void TextureCacheCompositingThread::setMemoryUsage(size_t memoryUsage)
 
195
{
 
196
    m_memoryUsage = memoryUsage;
 
197
#if DEBUG_TEXTURE_MEMORY_USAGE
 
198
    fprintf(stderr, "Texture memory usage %u kB\n", m_memoryUsage / 1024);
 
199
#endif
 
200
}
 
201
 
 
202
PassRefPtr<Texture> TextureCacheCompositingThread::textureForTiledContents(const SkBitmap& contents, const IntRect& tileRect, const TileIndex& index, bool isOpaque)
 
203
{
 
204
    HashMap<ContentsKey, TextureMap>::iterator it = m_cache.add(key(contents), TextureMap()).iterator;
 
205
    TextureMap& map = (*it).value;
 
206
 
 
207
    TextureMap::iterator jt = map.add(index, RefPtr<Texture>()).iterator;
 
208
    RefPtr<Texture> texture = (*jt).value;
 
209
    if (!texture) {
 
210
        texture = createTexture();
 
211
#if DEBUG_TEXTURE_MEMORY_USAGE
 
212
        fprintf(stderr, "Creating texture 0x%x for 0x%x+%d @ (%d, %d)\n", texture.get(), contents.pixelRef(), contents.pixelRefOffset(), index.i(), index.j());
 
213
#endif
 
214
        map.set(index, texture);
 
215
    }
 
216
 
 
217
    // Protect newly created texture from being evicted.
 
218
    TextureProtector protector(texture.get());
 
219
 
 
220
    IntSize contentsSize(contents.width(), contents.height());
 
221
    IntRect dirtyRect(IntPoint(), contentsSize);
 
222
    if (tileRect.size() != texture->size()) {
 
223
#if DEBUG_TEXTURE_MEMORY_USAGE
 
224
        fprintf(stderr, "Updating texture 0x%x for 0x%x+%d @ (%d, %d)\n", texture.get(), contents.pixelRef(), contents.pixelRefOffset(), index.i(), index.j());
 
225
#endif
 
226
        texture->updateContents(contents, dirtyRect, tileRect, isOpaque);
 
227
    }
 
228
    return texture.release();
 
229
}
 
230
 
 
231
PassRefPtr<Texture> TextureCacheCompositingThread::textureForColor(const Color& color)
 
232
{
 
233
    // Just to make sure we don't get fooled by some malicious web page
 
234
    // into caching millions of color textures.
 
235
    if (m_colors.size() > 100)
 
236
        m_colors.clear();
 
237
 
 
238
    ColorTextureMap::iterator it = m_colors.find(color);
 
239
    RefPtr<Texture> texture;
 
240
    if (it == m_colors.end()) {
 
241
        texture = Texture::create(true /* isColor */);
 
242
#if DEBUG_TEXTURE_MEMORY_USAGE
 
243
        fprintf(stderr, "Creating texture 0x%x for color 0x%x\n", texture.get(), color.rgb());
 
244
#endif
 
245
        m_colors.set(color, texture);
 
246
    } else
 
247
        texture = (*it).value;
 
248
 
 
249
    // Color textures can't be evicted, so no need for TextureProtector.
 
250
 
 
251
    if (texture->size() != IntSize(1, 1))
 
252
        texture->setContentsToColor(color);
 
253
 
 
254
    return texture.release();
 
255
}
 
256
 
 
257
PassRefPtr<Texture> TextureCacheCompositingThread::updateContents(const RefPtr<Texture>& textureIn, const SkBitmap& contents, const IntRect& dirtyRect, const IntRect& tileRect, bool isOpaque)
 
258
{
 
259
    RefPtr<Texture> texture(textureIn);
 
260
 
 
261
    // If the texture was 0, or needs to transition from a solid color texture to a contents texture,
 
262
    // create a new texture.
 
263
    if (!texture || texture->isColor())
 
264
        texture = createTexture();
 
265
 
 
266
    // Protect newly created texture from being evicted.
 
267
    TextureProtector protector(texture.get());
 
268
 
 
269
    texture->updateContents(contents, dirtyRect, tileRect, isOpaque);
 
270
 
 
271
    return texture.release();
 
272
}
 
273
 
 
274
TextureCacheCompositingThread::ContentsKey TextureCacheCompositingThread::key(const SkBitmap& contents)
 
275
{
 
276
    // The pixelRef is an address, and addresses can be reused by the allocator
 
277
    // so it's unsuitable to use as a key. Instead, grab the generation, which
 
278
    // is globally unique according to the current implementation in
 
279
    // SkPixelRef.cpp.
 
280
    uint32_t generation = contents.getGenerationID();
 
281
 
 
282
    // If the generation is equal to the deleted value, use something else that
 
283
    // is unlikely to correspond to a generation currently in use or soon to be
 
284
    // in use.
 
285
    uint32_t deletedValue = 0;
 
286
    HashTraits<uint32_t>::constructDeletedValue(deletedValue);
 
287
    if (generation == deletedValue) {
 
288
        // This strategy works as long as the deleted value is -1.
 
289
        ASSERT(deletedValue == static_cast<uint32_t>(-1));
 
290
        generation = deletedValue / 2;
 
291
    }
 
292
 
 
293
    // Now the generation alone does not uniquely identify the texture contents.
 
294
    // The same pixelref can be reused in another bitmap but with a different
 
295
    // offset (somewhat contrived, but possible).
 
296
    return std::make_pair(generation, contents.pixelRefOffset());
 
297
}
 
298
 
 
299
} // namespace WebCore
 
300
 
 
301
#endif