blob: 7ff26dcaf080f8193f416a1ece9093f47d769775 [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -07001/*
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
Romain Guya2341a92010-09-08 18:04:33 -070024#include <utils/threads.h>
25
Romain Guy7fbcc042010-08-04 15:40:07 -070026#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070027#include "Properties.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070028
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Constructors/destructor
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guyfb8b7632010-08-23 21:05:08 -070036PathCache::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
Romain Guy7fbcc042010-08-04 15:40:07 -070049PathCache::PathCache(uint32_t maxByteSize):
50 mCache(GenerationCache<PathCacheEntry, PathTexture*>::kUnlimitedCapacity),
51 mSize(0), mMaxSize(maxByteSize) {
Romain Guyfb8b7632010-08-23 21:05:08 -070052 init();
53}
54
55PathCache::~PathCache() {
Romain Guya2341a92010-09-08 18:04:33 -070056 Mutex::Autolock _l(mLock);
Romain Guyfb8b7632010-08-23 21:05:08 -070057 mCache.clear();
58}
59
60void PathCache::init() {
Romain Guy7fbcc042010-08-04 15:40:07 -070061 mCache.setOnEntryRemovedListener(this);
Romain Guy9cccc2b92010-08-07 23:46:15 -070062
63 GLint maxTextureSize;
64 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
65 mMaxTextureSize = maxTextureSize;
Romain Guye190aa62010-11-10 19:01:29 -080066
67 mDebugEnabled = readDebugLevel() & kDebugCaches;
Romain Guy7fbcc042010-08-04 15:40:07 -070068}
69
Romain Guy7fbcc042010-08-04 15:40:07 -070070///////////////////////////////////////////////////////////////////////////////
71// Size management
72///////////////////////////////////////////////////////////////////////////////
73
74uint32_t PathCache::getSize() {
Romain Guya2341a92010-09-08 18:04:33 -070075 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -070076 return mSize;
77}
78
79uint32_t PathCache::getMaxSize() {
Romain Guya2341a92010-09-08 18:04:33 -070080 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -070081 return mMaxSize;
82}
83
84void PathCache::setMaxSize(uint32_t maxSize) {
Romain Guya2341a92010-09-08 18:04:33 -070085 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -070086 mMaxSize = maxSize;
87 while (mSize > mMaxSize) {
88 mCache.removeOldest();
89 }
90}
91
92///////////////////////////////////////////////////////////////////////////////
93// Callbacks
94///////////////////////////////////////////////////////////////////////////////
95
96void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) {
Romain Guy7fbcc042010-08-04 15:40:07 -070097 if (texture) {
Romain Guy9e108412010-11-09 14:35:20 -080098 const uint32_t size = texture->width * texture->height;
99 mSize -= size;
100
101 PATH_LOGD("PathCache::callback: delete path: name, size, mSize = %d, %d, %d",
102 texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800103 if (mDebugEnabled) {
104 LOGD("Path deleted, size = %d", size);
105 }
Romain Guy9e108412010-11-09 14:35:20 -0800106
Romain Guy7fbcc042010-08-04 15:40:07 -0700107 glDeleteTextures(1, &texture->id);
108 delete texture;
109 }
110}
111
112///////////////////////////////////////////////////////////////////////////////
113// Caching
114///////////////////////////////////////////////////////////////////////////////
115
Romain Guya2341a92010-09-08 18:04:33 -0700116void PathCache::remove(SkPath* path) {
117 Mutex::Autolock _l(mLock);
Romain Guy9e108412010-11-09 14:35:20 -0800118
Romain Guya2341a92010-09-08 18:04:33 -0700119 // TODO: Linear search...
Romain Guy9e108412010-11-09 14:35:20 -0800120 Vector<uint32_t> pathsToRemove;
Romain Guya2341a92010-09-08 18:04:33 -0700121 for (uint32_t i = 0; i < mCache.size(); i++) {
122 if (mCache.getKeyAt(i).path == path) {
Romain Guy9e108412010-11-09 14:35:20 -0800123 pathsToRemove.push(i);
Romain Guya2341a92010-09-08 18:04:33 -0700124 }
125 }
Romain Guy9e108412010-11-09 14:35:20 -0800126
127 for (size_t i = 0; i < pathsToRemove.size(); i++) {
128 mCache.removeAt(pathsToRemove.itemAt(i));
129 }
Romain Guya2341a92010-09-08 18:04:33 -0700130}
131
Romain Guy7fbcc042010-08-04 15:40:07 -0700132PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
133 PathCacheEntry entry(path, paint);
Romain Guya2341a92010-09-08 18:04:33 -0700134
135 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700136 PathTexture* texture = mCache.get(entry);
Romain Guya2341a92010-09-08 18:04:33 -0700137 mLock.unlock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700138
139 if (!texture) {
140 texture = addTexture(entry, path, paint);
141 } else if (path->getGenerationID() != texture->generation) {
Romain Guya2341a92010-09-08 18:04:33 -0700142 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700143 mCache.remove(entry);
Romain Guya2341a92010-09-08 18:04:33 -0700144 mLock.unlock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700145 texture = addTexture(entry, path, paint);
146 }
147
Romain Guy7fbcc042010-08-04 15:40:07 -0700148 return texture;
149}
150
151PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
152 const SkPath *path, const SkPaint* paint) {
153 const SkRect& bounds = path->getBounds();
Romain Guy9cccc2b92010-08-07 23:46:15 -0700154
Romain Guyd7999122010-09-30 16:56:56 -0700155 const float pathWidth = fmax(bounds.width(), 1.0f);
156 const float pathHeight = fmax(bounds.height(), 1.0f);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700157
158 if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) {
159 LOGW("Path too large to be rendered into a texture");
160 return NULL;
161 }
162
Romain Guy7fbcc042010-08-04 15:40:07 -0700163 const float offset = entry.strokeWidth * 1.5f;
Romain Guy9cccc2b92010-08-07 23:46:15 -0700164 const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5);
165 const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5);
Romain Guy7fbcc042010-08-04 15:40:07 -0700166
167 const uint32_t size = width * height;
168 // Don't even try to cache a bitmap that's bigger than the cache
169 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700170 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700171 while (mSize + size > mMaxSize) {
172 mCache.removeOldest();
173 }
Romain Guya2341a92010-09-08 18:04:33 -0700174 mLock.unlock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700175 }
176
177 PathTexture* texture = new PathTexture;
178 texture->left = bounds.fLeft;
179 texture->top = bounds.fTop;
180 texture->offset = offset;
181 texture->width = width;
182 texture->height = height;
183 texture->generation = path->getGenerationID();
184
185 SkBitmap bitmap;
186 bitmap.setConfig(SkBitmap::kA8_Config, width, height);
187 bitmap.allocPixels();
188 bitmap.eraseColor(0);
189
Romain Guy1041ade2010-11-04 12:10:40 -0700190 SkPaint pathPaint(*paint);
191 if (!pathPaint.getXfermode()) {
192 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
193 pathPaint.setXfermode(mode)->safeUnref();
194 }
195
Romain Guy7fbcc042010-08-04 15:40:07 -0700196 SkCanvas canvas(bitmap);
197 canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
Romain Guy1041ade2010-11-04 12:10:40 -0700198 canvas.drawPath(*path, pathPaint);
Romain Guy7fbcc042010-08-04 15:40:07 -0700199
200 generateTexture(bitmap, texture);
201
202 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700203 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700204 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800205 PATH_LOGD("PathCache::get: create path: name, size, mSize = %d, %d, %d",
206 texture->id, size, mSize);
Romain Guye190aa62010-11-10 19:01:29 -0800207 if (mDebugEnabled) {
208 LOGD("Path created, size = %d", size);
209 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700210 mCache.put(entry, texture);
Romain Guya2341a92010-09-08 18:04:33 -0700211 mLock.unlock();
Romain Guy22158e12010-08-06 11:18:34 -0700212 } else {
213 texture->cleanup = true;
Romain Guy7fbcc042010-08-04 15:40:07 -0700214 }
215
216 return texture;
217}
218
219void PathCache::clear() {
Romain Guya2341a92010-09-08 18:04:33 -0700220 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -0700221 mCache.clear();
222}
223
224void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
225 SkAutoLockPixels alp(bitmap);
226 if (!bitmap.readyToDraw()) {
227 LOGE("Cannot generate texture from bitmap");
228 return;
229 }
230
231 glGenTextures(1, &texture->id);
232
233 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guy61c8c9c2010-08-09 20:48:09 -0700234 // Textures are Alpha8
235 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy7fbcc042010-08-04 15:40:07 -0700236
237 texture->blend = true;
Romain Guy1e45aae2010-08-13 19:39:53 -0700238 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
Romain Guy7fbcc042010-08-04 15:40:07 -0700239 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
240
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
243
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
246}
247
248}; // namespace uirenderer
249}; // namespace android