blob: 04d07dbbee8ab44e4c67e35ebe929898ed99118d [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 Guy7fbcc042010-08-04 15:40:07 -070066}
67
Romain Guy7fbcc042010-08-04 15:40:07 -070068///////////////////////////////////////////////////////////////////////////////
69// Size management
70///////////////////////////////////////////////////////////////////////////////
71
72uint32_t PathCache::getSize() {
Romain Guya2341a92010-09-08 18:04:33 -070073 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -070074 return mSize;
75}
76
77uint32_t PathCache::getMaxSize() {
Romain Guya2341a92010-09-08 18:04:33 -070078 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -070079 return mMaxSize;
80}
81
82void PathCache::setMaxSize(uint32_t maxSize) {
Romain Guya2341a92010-09-08 18:04:33 -070083 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -070084 mMaxSize = maxSize;
85 while (mSize > mMaxSize) {
86 mCache.removeOldest();
87 }
88}
89
90///////////////////////////////////////////////////////////////////////////////
91// Callbacks
92///////////////////////////////////////////////////////////////////////////////
93
94void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) {
Romain Guy7fbcc042010-08-04 15:40:07 -070095 if (texture) {
Romain Guy9e108412010-11-09 14:35:20 -080096 const uint32_t size = texture->width * texture->height;
97 mSize -= size;
98
99 PATH_LOGD("PathCache::callback: delete path: name, size, mSize = %d, %d, %d",
100 texture->id, size, mSize);
101
Romain Guy7fbcc042010-08-04 15:40:07 -0700102 glDeleteTextures(1, &texture->id);
103 delete texture;
104 }
105}
106
107///////////////////////////////////////////////////////////////////////////////
108// Caching
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guya2341a92010-09-08 18:04:33 -0700111void PathCache::remove(SkPath* path) {
112 Mutex::Autolock _l(mLock);
Romain Guy9e108412010-11-09 14:35:20 -0800113
Romain Guya2341a92010-09-08 18:04:33 -0700114 // TODO: Linear search...
Romain Guy9e108412010-11-09 14:35:20 -0800115 Vector<uint32_t> pathsToRemove;
Romain Guya2341a92010-09-08 18:04:33 -0700116 for (uint32_t i = 0; i < mCache.size(); i++) {
117 if (mCache.getKeyAt(i).path == path) {
Romain Guy9e108412010-11-09 14:35:20 -0800118 pathsToRemove.push(i);
Romain Guya2341a92010-09-08 18:04:33 -0700119 }
120 }
Romain Guy9e108412010-11-09 14:35:20 -0800121
122 for (size_t i = 0; i < pathsToRemove.size(); i++) {
123 mCache.removeAt(pathsToRemove.itemAt(i));
124 }
Romain Guya2341a92010-09-08 18:04:33 -0700125}
126
Romain Guy7fbcc042010-08-04 15:40:07 -0700127PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
128 PathCacheEntry entry(path, paint);
Romain Guya2341a92010-09-08 18:04:33 -0700129
130 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700131 PathTexture* texture = mCache.get(entry);
Romain Guya2341a92010-09-08 18:04:33 -0700132 mLock.unlock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700133
134 if (!texture) {
135 texture = addTexture(entry, path, paint);
136 } else if (path->getGenerationID() != texture->generation) {
Romain Guya2341a92010-09-08 18:04:33 -0700137 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700138 mCache.remove(entry);
Romain Guya2341a92010-09-08 18:04:33 -0700139 mLock.unlock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700140 texture = addTexture(entry, path, paint);
141 }
142
Romain Guy7fbcc042010-08-04 15:40:07 -0700143 return texture;
144}
145
146PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
147 const SkPath *path, const SkPaint* paint) {
148 const SkRect& bounds = path->getBounds();
Romain Guy9cccc2b92010-08-07 23:46:15 -0700149
Romain Guyd7999122010-09-30 16:56:56 -0700150 const float pathWidth = fmax(bounds.width(), 1.0f);
151 const float pathHeight = fmax(bounds.height(), 1.0f);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700152
153 if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) {
154 LOGW("Path too large to be rendered into a texture");
155 return NULL;
156 }
157
Romain Guy7fbcc042010-08-04 15:40:07 -0700158 const float offset = entry.strokeWidth * 1.5f;
Romain Guy9cccc2b92010-08-07 23:46:15 -0700159 const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5);
160 const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5);
Romain Guy7fbcc042010-08-04 15:40:07 -0700161
162 const uint32_t size = width * height;
163 // Don't even try to cache a bitmap that's bigger than the cache
164 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700165 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700166 while (mSize + size > mMaxSize) {
167 mCache.removeOldest();
168 }
Romain Guya2341a92010-09-08 18:04:33 -0700169 mLock.unlock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700170 }
171
172 PathTexture* texture = new PathTexture;
173 texture->left = bounds.fLeft;
174 texture->top = bounds.fTop;
175 texture->offset = offset;
176 texture->width = width;
177 texture->height = height;
178 texture->generation = path->getGenerationID();
179
180 SkBitmap bitmap;
181 bitmap.setConfig(SkBitmap::kA8_Config, width, height);
182 bitmap.allocPixels();
183 bitmap.eraseColor(0);
184
Romain Guy1041ade2010-11-04 12:10:40 -0700185 SkPaint pathPaint(*paint);
186 if (!pathPaint.getXfermode()) {
187 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
188 pathPaint.setXfermode(mode)->safeUnref();
189 }
190
Romain Guy7fbcc042010-08-04 15:40:07 -0700191 SkCanvas canvas(bitmap);
192 canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
Romain Guy1041ade2010-11-04 12:10:40 -0700193 canvas.drawPath(*path, pathPaint);
Romain Guy7fbcc042010-08-04 15:40:07 -0700194
195 generateTexture(bitmap, texture);
196
197 if (size < mMaxSize) {
Romain Guya2341a92010-09-08 18:04:33 -0700198 mLock.lock();
Romain Guy7fbcc042010-08-04 15:40:07 -0700199 mSize += size;
Romain Guy9e108412010-11-09 14:35:20 -0800200 PATH_LOGD("PathCache::get: create path: name, size, mSize = %d, %d, %d",
201 texture->id, size, mSize);
Romain Guy7fbcc042010-08-04 15:40:07 -0700202 mCache.put(entry, texture);
Romain Guya2341a92010-09-08 18:04:33 -0700203 mLock.unlock();
Romain Guy22158e12010-08-06 11:18:34 -0700204 } else {
205 texture->cleanup = true;
Romain Guy7fbcc042010-08-04 15:40:07 -0700206 }
207
208 return texture;
209}
210
211void PathCache::clear() {
Romain Guya2341a92010-09-08 18:04:33 -0700212 Mutex::Autolock _l(mLock);
Romain Guy7fbcc042010-08-04 15:40:07 -0700213 mCache.clear();
214}
215
216void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
217 SkAutoLockPixels alp(bitmap);
218 if (!bitmap.readyToDraw()) {
219 LOGE("Cannot generate texture from bitmap");
220 return;
221 }
222
223 glGenTextures(1, &texture->id);
224
225 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guy61c8c9c2010-08-09 20:48:09 -0700226 // Textures are Alpha8
227 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy7fbcc042010-08-04 15:40:07 -0700228
229 texture->blend = true;
Romain Guy1e45aae2010-08-13 19:39:53 -0700230 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
Romain Guy7fbcc042010-08-04 15:40:07 -0700231 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
232
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
235
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
238}
239
240}; // namespace uirenderer
241}; // namespace android