summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/chromium/TextureManager.cpp
blob: 485c72228ec037fcce5f3fec40092485237c06bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (C) 2010, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if USE(ACCELERATED_COMPOSITING)

#include "TextureManager.h"

#include "ManagedTexture.h"
#include "TraceEvent.h"

using namespace std;

namespace WebCore {


namespace {
size_t memoryLimitBytes(size_t viewportMultiplier, const IntSize& viewportSize, size_t minMegabytes, size_t maxMegabytes)
{
    if (!viewportMultiplier)
        return maxMegabytes * 1024 * 1024;
    if (viewportSize.isEmpty())
        return minMegabytes * 1024 * 1024;
    return max(minMegabytes * 1024 * 1024, min(maxMegabytes * 1024 * 1024, viewportMultiplier * TextureManager::memoryUseBytes(viewportSize, GraphicsContext3D::RGBA)));
}
}

size_t TextureManager::highLimitBytes(const IntSize& viewportSize)
{
    size_t viewportMultiplier, minMegabytes, maxMegabytes;
#if OS(ANDROID)
    viewportMultiplier = 16;
    minMegabytes = 32;
    maxMegabytes = 64;
#else
    viewportMultiplier = 0;
    minMegabytes = 0;
    maxMegabytes = 128;
#endif
    return memoryLimitBytes(viewportMultiplier, viewportSize, minMegabytes, maxMegabytes);
}

size_t TextureManager::reclaimLimitBytes(const IntSize& viewportSize)
{
    size_t viewportMultiplier, minMegabytes, maxMegabytes;
#if OS(ANDROID)
    viewportMultiplier = 8;
    minMegabytes = 16;
    maxMegabytes = 32;
#else
    viewportMultiplier = 0;
    minMegabytes = 0;
    maxMegabytes = 64;
#endif
    return memoryLimitBytes(viewportMultiplier, viewportSize, minMegabytes, maxMegabytes);
}

size_t TextureManager::memoryUseBytes(const IntSize& size, GC3Denum textureFormat)
{
    // FIXME: This assumes all textures are 1 byte/component.
    const GC3Denum type = GraphicsContext3D::UNSIGNED_BYTE;
    unsigned int componentsPerPixel = 4;
    unsigned int bytesPerComponent = 1;
    if (!GraphicsContext3D::computeFormatAndTypeParameters(textureFormat, type, &componentsPerPixel, &bytesPerComponent))
        ASSERT_NOT_REACHED();

    return size.width() * size.height() * componentsPerPixel * bytesPerComponent;
}


TextureManager::TextureManager(size_t maxMemoryLimitBytes, size_t preferredMemoryLimitBytes, int maxTextureSize)
    : m_maxMemoryLimitBytes(maxMemoryLimitBytes)
    , m_preferredMemoryLimitBytes(preferredMemoryLimitBytes)
    , m_memoryUseBytes(0)
    , m_maxTextureSize(maxTextureSize)
    , m_nextToken(1)
{
}

TextureManager::~TextureManager()
{
    for (HashSet<ManagedTexture*>::iterator it = m_registeredTextures.begin(); it != m_registeredTextures.end(); ++it)
        (*it)->clearManager();
}

void TextureManager::setMemoryAllocationLimitBytes(size_t memoryLimitBytes)
{
    setMaxMemoryLimitBytes(memoryLimitBytes);
#if defined(OS_ANDROID)
    // On android, we are setting the preferred memory limit to half of our
    // maximum allocation, because we would like to stay significantly below
    // the absolute memory limit whenever we can. Specifically, by limitting
    // prepainting only to the halfway memory mark.
    setPreferredMemoryLimitBytes(memoryLimitBytes / 2);
#else
    setPreferredMemoryLimitBytes(memoryLimitBytes);
#endif
}

void TextureManager::setMaxMemoryLimitBytes(size_t memoryLimitBytes)
{
    reduceMemoryToLimit(memoryLimitBytes);
    ASSERT(currentMemoryUseBytes() <= memoryLimitBytes);
    m_maxMemoryLimitBytes = memoryLimitBytes;
}

void TextureManager::setPreferredMemoryLimitBytes(size_t memoryLimitBytes)
{
    m_preferredMemoryLimitBytes = memoryLimitBytes;
}

void TextureManager::registerTexture(ManagedTexture* texture)
{
    ASSERT(texture);
    ASSERT(!m_registeredTextures.contains(texture));

    m_registeredTextures.add(texture);
}

void TextureManager::unregisterTexture(ManagedTexture* texture)
{
    ASSERT(texture);
    ASSERT(m_registeredTextures.contains(texture));

    m_registeredTextures.remove(texture);
}

TextureToken TextureManager::getToken()
{
    return m_nextToken++;
}

void TextureManager::releaseToken(TextureToken token)
{
    TextureMap::iterator it = m_textures.find(token);
    if (it != m_textures.end())
        removeTexture(token, it->second);
}

bool TextureManager::hasTexture(TextureToken token)
{
    return m_textures.contains(token);
}

bool TextureManager::isProtected(TextureToken token)
{
    return token && hasTexture(token) && m_textures.get(token).isProtected;
}

void TextureManager::protectTexture(TextureToken token)
{
    ASSERT(hasTexture(token));
    TextureInfo info = m_textures.take(token);
    info.isProtected = true;
    m_textures.add(token, info);
    // If someone protects a texture, put it at the end of the LRU list.
    m_textureLRUSet.remove(token);
    m_textureLRUSet.add(token);
}

void TextureManager::unprotectTexture(TextureToken token)
{
    TextureMap::iterator it = m_textures.find(token);
    if (it != m_textures.end())
        it->second.isProtected = false;
}

void TextureManager::unprotectAllTextures()
{
    for (TextureMap::iterator it = m_textures.begin(); it != m_textures.end(); ++it)
        it->second.isProtected = false;
}

void TextureManager::evictTexture(TextureToken token, TextureInfo info)
{
    TRACE_EVENT0("cc", "TextureManager::evictTexture");
    removeTexture(token, info);
}

void TextureManager::reduceMemoryToLimit(size_t limit)
{
    while (m_memoryUseBytes > limit) {
        ASSERT(!m_textureLRUSet.isEmpty());
        bool foundCandidate = false;
        for (ListHashSet<TextureToken>::iterator lruIt = m_textureLRUSet.begin(); lruIt != m_textureLRUSet.end(); ++lruIt) {
            TextureToken token = *lruIt;
            TextureInfo info = m_textures.get(token);
            if (info.isProtected)
                continue;
            evictTexture(token, info);
            foundCandidate = true;
            break;
        }
        if (!foundCandidate)
            return;
    }
}

void TextureManager::addTexture(TextureToken token, TextureInfo info)
{
    ASSERT(!m_textureLRUSet.contains(token));
    ASSERT(!m_textures.contains(token));
    m_memoryUseBytes += memoryUseBytes(info.size, info.format);
    m_textures.set(token, info);
    m_textureLRUSet.add(token);
}

void TextureManager::deleteEvictedTextures(TextureAllocator* allocator)
{
    if (allocator) {
        for (size_t i = 0; i < m_evictedTextures.size(); ++i) {
            if (m_evictedTextures[i].textureId) {
#ifndef NDEBUG
                ASSERT(m_evictedTextures[i].allocator == allocator);
#endif
                allocator->deleteTexture(m_evictedTextures[i].textureId, m_evictedTextures[i].size, m_evictedTextures[i].format);
            }
        }
    }
    m_evictedTextures.clear();
}

void TextureManager::evictAndRemoveAllDeletedTextures()
{
    unprotectAllTextures();
    reduceMemoryToLimit(0);
    m_evictedTextures.clear();
}

void TextureManager::evictAndDeleteAllTextures(TextureAllocator* allocator)
{
    unprotectAllTextures();
    reduceMemoryToLimit(0);
    deleteEvictedTextures(allocator);
}

void TextureManager::removeTexture(TextureToken token, TextureInfo info)
{
    ASSERT(m_textureLRUSet.contains(token));
    ASSERT(m_textures.contains(token));
    m_memoryUseBytes -= memoryUseBytes(info.size, info.format);
    m_textures.remove(token);
    ASSERT(m_textureLRUSet.contains(token));
    m_textureLRUSet.remove(token);
    EvictionEntry entry;
    entry.textureId = info.textureId;
    entry.size = info.size;
    entry.format = info.format;
#ifndef NDEBUG
    entry.allocator = info.allocator;
#endif
    m_evictedTextures.append(entry);
}

unsigned TextureManager::allocateTexture(TextureAllocator* allocator, TextureToken token)
{
    TextureMap::iterator it = m_textures.find(token);
    ASSERT(it != m_textures.end());
    TextureInfo* info = &it.get()->second;
    ASSERT(info->isProtected);

    unsigned textureId = allocator->createTexture(info->size, info->format);
    info->textureId = textureId;
#ifndef NDEBUG
    info->allocator = allocator;
#endif
    return textureId;
}

bool TextureManager::requestTexture(TextureToken token, IntSize size, unsigned format)
{
    if (size.width() > m_maxTextureSize || size.height() > m_maxTextureSize)
        return false;

    TextureMap::iterator it = m_textures.find(token);
    if (it != m_textures.end()) {
        ASSERT(it->second.size != size || it->second.format != format);
        removeTexture(token, it->second);
    }

    size_t memoryRequiredBytes = memoryUseBytes(size, format);
    if (memoryRequiredBytes > m_maxMemoryLimitBytes)
        return false;

    reduceMemoryToLimit(m_maxMemoryLimitBytes - memoryRequiredBytes);
    if (m_memoryUseBytes + memoryRequiredBytes > m_maxMemoryLimitBytes)
        return false;

    TextureInfo info;
    info.size = size;
    info.format = format;
    info.textureId = 0;
    info.isProtected = true;
#ifndef NDEBUG
    info.allocator = 0;
#endif
    addTexture(token, info);
    return true;
}

}

#endif // USE(ACCELERATED_COMPOSITING)