PathCache.cpp revision d79991277043d6bdbd90bb63fd8aff73ef9e06a5
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
19#include <GLES2/gl2.h>
20
21#include <SkCanvas.h>
22#include <SkRect.h>
23
24#include <utils/threads.h>
25
26#include "PathCache.h"
27#include "Properties.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Constructors/destructor
34///////////////////////////////////////////////////////////////////////////////
35
36PathCache::PathCache():
37        mCache(GenerationCache<PathCacheEntry, PathTexture*>::kUnlimitedCapacity),
38        mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
39    char property[PROPERTY_VALUE_MAX];
40    if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) {
41        LOGD("  Setting path cache size to %sMB", property);
42        setMaxSize(MB(atof(property)));
43    } else {
44        LOGD("  Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE);
45    }
46    init();
47}
48
49PathCache::PathCache(uint32_t maxByteSize):
50        mCache(GenerationCache<PathCacheEntry, PathTexture*>::kUnlimitedCapacity),
51        mSize(0), mMaxSize(maxByteSize) {
52    init();
53}
54
55PathCache::~PathCache() {
56    Mutex::Autolock _l(mLock);
57    mCache.clear();
58}
59
60void PathCache::init() {
61    mCache.setOnEntryRemovedListener(this);
62
63    GLint maxTextureSize;
64    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
65    mMaxTextureSize = maxTextureSize;
66}
67
68///////////////////////////////////////////////////////////////////////////////
69// Size management
70///////////////////////////////////////////////////////////////////////////////
71
72uint32_t PathCache::getSize() {
73    Mutex::Autolock _l(mLock);
74    return mSize;
75}
76
77uint32_t PathCache::getMaxSize() {
78    Mutex::Autolock _l(mLock);
79    return mMaxSize;
80}
81
82void PathCache::setMaxSize(uint32_t maxSize) {
83    Mutex::Autolock _l(mLock);
84    mMaxSize = maxSize;
85    while (mSize > mMaxSize) {
86        mCache.removeOldest();
87    }
88}
89
90///////////////////////////////////////////////////////////////////////////////
91// Callbacks
92///////////////////////////////////////////////////////////////////////////////
93
94void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) {
95    const uint32_t size = texture->width * texture->height;
96    mSize -= size;
97
98    if (texture) {
99        glDeleteTextures(1, &texture->id);
100        delete texture;
101    }
102}
103
104///////////////////////////////////////////////////////////////////////////////
105// Caching
106///////////////////////////////////////////////////////////////////////////////
107
108void PathCache::remove(SkPath* path) {
109    Mutex::Autolock _l(mLock);
110    // TODO: Linear search...
111    for (uint32_t i = 0; i < mCache.size(); i++) {
112        if (mCache.getKeyAt(i).path == path) {
113            mCache.removeAt(i);
114        }
115    }
116}
117
118PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
119    PathCacheEntry entry(path, paint);
120
121    mLock.lock();
122    PathTexture* texture = mCache.get(entry);
123    mLock.unlock();
124
125    if (!texture) {
126        texture = addTexture(entry, path, paint);
127    } else if (path->getGenerationID() != texture->generation) {
128        mLock.lock();
129        mCache.remove(entry);
130        mLock.unlock();
131        texture = addTexture(entry, path, paint);
132    }
133
134    return texture;
135}
136
137PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
138        const SkPath *path, const SkPaint* paint) {
139    const SkRect& bounds = path->getBounds();
140
141    const float pathWidth = fmax(bounds.width(), 1.0f);
142    const float pathHeight = fmax(bounds.height(), 1.0f);
143
144    if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) {
145        LOGW("Path too large to be rendered into a texture");
146        return NULL;
147    }
148
149    const float offset = entry.strokeWidth * 1.5f;
150    const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5);
151    const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5);
152
153    const uint32_t size = width * height;
154    // Don't even try to cache a bitmap that's bigger than the cache
155    if (size < mMaxSize) {
156        mLock.lock();
157        while (mSize + size > mMaxSize) {
158            mCache.removeOldest();
159        }
160        mLock.unlock();
161    }
162
163    PathTexture* texture = new PathTexture;
164    texture->left = bounds.fLeft;
165    texture->top = bounds.fTop;
166    texture->offset = offset;
167    texture->width = width;
168    texture->height = height;
169    texture->generation = path->getGenerationID();
170
171    SkBitmap bitmap;
172    bitmap.setConfig(SkBitmap::kA8_Config, width, height);
173    bitmap.allocPixels();
174    bitmap.eraseColor(0);
175
176    SkCanvas canvas(bitmap);
177    canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
178    canvas.drawPath(*path, *paint);
179
180    generateTexture(bitmap, texture);
181
182    if (size < mMaxSize) {
183        mLock.lock();
184        mSize += size;
185        mCache.put(entry, texture);
186        mLock.unlock();
187    } else {
188        texture->cleanup = true;
189    }
190
191    return texture;
192}
193
194void PathCache::clear() {
195    Mutex::Autolock _l(mLock);
196    mCache.clear();
197}
198
199void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
200    SkAutoLockPixels alp(bitmap);
201    if (!bitmap.readyToDraw()) {
202        LOGE("Cannot generate texture from bitmap");
203        return;
204    }
205
206    glGenTextures(1, &texture->id);
207
208    glBindTexture(GL_TEXTURE_2D, texture->id);
209    // Textures are Alpha8
210    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
211
212    texture->blend = true;
213    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
214            GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
215
216    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
217    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
218
219    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
220    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
221}
222
223}; // namespace uirenderer
224}; // namespace android
225