Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 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 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 24 | #include <utils/threads.h> |
| 25 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 26 | #include "PathCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 27 | #include "Properties.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | // Constructors/destructor |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 36 | PathCache::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 Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 49 | PathCache::PathCache(uint32_t maxByteSize): |
| 50 | mCache(GenerationCache<PathCacheEntry, PathTexture*>::kUnlimitedCapacity), |
| 51 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 52 | init(); |
| 53 | } |
| 54 | |
| 55 | PathCache::~PathCache() { |
| 56 | mCache.clear(); |
| 57 | } |
| 58 | |
| 59 | void PathCache::init() { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 60 | mCache.setOnEntryRemovedListener(this); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 61 | |
| 62 | GLint maxTextureSize; |
| 63 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 64 | mMaxTextureSize = maxTextureSize; |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 65 | |
| 66 | mDebugEnabled = readDebugLevel() & kDebugCaches; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // Size management |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | uint32_t PathCache::getSize() { |
| 74 | return mSize; |
| 75 | } |
| 76 | |
| 77 | uint32_t PathCache::getMaxSize() { |
| 78 | return mMaxSize; |
| 79 | } |
| 80 | |
| 81 | void PathCache::setMaxSize(uint32_t maxSize) { |
| 82 | mMaxSize = maxSize; |
| 83 | while (mSize > mMaxSize) { |
| 84 | mCache.removeOldest(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | // Callbacks |
| 90 | /////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame^] | 93 | removeTexture(texture); |
| 94 | } |
| 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////////// |
| 97 | // Caching |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | void PathCache::removeTexture(PathTexture* texture) { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 101 | if (texture) { |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 102 | const uint32_t size = texture->width * texture->height; |
| 103 | mSize -= size; |
| 104 | |
| 105 | PATH_LOGD("PathCache::callback: delete path: name, size, mSize = %d, %d, %d", |
| 106 | texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 107 | if (mDebugEnabled) { |
| 108 | LOGD("Path deleted, size = %d", size); |
| 109 | } |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 110 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 111 | glDeleteTextures(1, &texture->id); |
| 112 | delete texture; |
| 113 | } |
| 114 | } |
| 115 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 116 | void PathCache::remove(SkPath* path) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 117 | // TODO: Linear search... |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 118 | Vector<uint32_t> pathsToRemove; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 119 | for (uint32_t i = 0; i < mCache.size(); i++) { |
| 120 | if (mCache.getKeyAt(i).path == path) { |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 121 | pathsToRemove.push(i); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame^] | 122 | removeTexture(mCache.getValueAt(i)); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 125 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame^] | 126 | mCache.setOnEntryRemovedListener(NULL); |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 127 | for (size_t i = 0; i < pathsToRemove.size(); i++) { |
| 128 | mCache.removeAt(pathsToRemove.itemAt(i)); |
| 129 | } |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame^] | 130 | mCache.setOnEntryRemovedListener(this); |
| 131 | } |
| 132 | |
| 133 | void PathCache::removeDeferred(SkPath* path) { |
| 134 | Mutex::Autolock _l(mLock); |
| 135 | mGarbage.push(path); |
| 136 | } |
| 137 | |
| 138 | void PathCache::clearGarbage() { |
| 139 | Mutex::Autolock _l(mLock); |
| 140 | size_t count = mGarbage.size(); |
| 141 | for (size_t i = 0; i < count; i++) { |
| 142 | remove(mGarbage.itemAt(i)); |
| 143 | } |
| 144 | mGarbage.clear(); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 147 | PathTexture* PathCache::get(SkPath* path, SkPaint* paint) { |
| 148 | PathCacheEntry entry(path, paint); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 149 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 150 | PathTexture* texture = mCache.get(entry); |
| 151 | |
| 152 | if (!texture) { |
| 153 | texture = addTexture(entry, path, paint); |
| 154 | } else if (path->getGenerationID() != texture->generation) { |
| 155 | mCache.remove(entry); |
| 156 | texture = addTexture(entry, path, paint); |
| 157 | } |
| 158 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 159 | return texture; |
| 160 | } |
| 161 | |
| 162 | PathTexture* PathCache::addTexture(const PathCacheEntry& entry, |
| 163 | const SkPath *path, const SkPaint* paint) { |
| 164 | const SkRect& bounds = path->getBounds(); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 165 | |
Romain Guy | d799912 | 2010-09-30 16:56:56 -0700 | [diff] [blame] | 166 | const float pathWidth = fmax(bounds.width(), 1.0f); |
| 167 | const float pathHeight = fmax(bounds.height(), 1.0f); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 168 | |
| 169 | if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) { |
| 170 | LOGW("Path too large to be rendered into a texture"); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 174 | const float offset = entry.strokeWidth * 1.5f; |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 175 | const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5); |
| 176 | const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 177 | |
| 178 | const uint32_t size = width * height; |
| 179 | // Don't even try to cache a bitmap that's bigger than the cache |
| 180 | if (size < mMaxSize) { |
| 181 | while (mSize + size > mMaxSize) { |
| 182 | mCache.removeOldest(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | PathTexture* texture = new PathTexture; |
| 187 | texture->left = bounds.fLeft; |
| 188 | texture->top = bounds.fTop; |
| 189 | texture->offset = offset; |
| 190 | texture->width = width; |
| 191 | texture->height = height; |
| 192 | texture->generation = path->getGenerationID(); |
| 193 | |
| 194 | SkBitmap bitmap; |
| 195 | bitmap.setConfig(SkBitmap::kA8_Config, width, height); |
| 196 | bitmap.allocPixels(); |
| 197 | bitmap.eraseColor(0); |
| 198 | |
Romain Guy | 1041ade | 2010-11-04 12:10:40 -0700 | [diff] [blame] | 199 | SkPaint pathPaint(*paint); |
| 200 | if (!pathPaint.getXfermode()) { |
| 201 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode); |
| 202 | pathPaint.setXfermode(mode)->safeUnref(); |
| 203 | } |
| 204 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 205 | SkCanvas canvas(bitmap); |
| 206 | canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset); |
Romain Guy | 1041ade | 2010-11-04 12:10:40 -0700 | [diff] [blame] | 207 | canvas.drawPath(*path, pathPaint); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 208 | |
| 209 | generateTexture(bitmap, texture); |
| 210 | |
| 211 | if (size < mMaxSize) { |
| 212 | mSize += size; |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 213 | PATH_LOGD("PathCache::get: create path: name, size, mSize = %d, %d, %d", |
| 214 | texture->id, size, mSize); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 215 | if (mDebugEnabled) { |
| 216 | LOGD("Path created, size = %d", size); |
| 217 | } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 218 | mCache.put(entry, texture); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 219 | } else { |
| 220 | texture->cleanup = true; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return texture; |
| 224 | } |
| 225 | |
| 226 | void PathCache::clear() { |
| 227 | mCache.clear(); |
| 228 | } |
| 229 | |
| 230 | void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) { |
| 231 | SkAutoLockPixels alp(bitmap); |
| 232 | if (!bitmap.readyToDraw()) { |
| 233 | LOGE("Cannot generate texture from bitmap"); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | glGenTextures(1, &texture->id); |
| 238 | |
| 239 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 240 | // Textures are Alpha8 |
| 241 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 242 | |
| 243 | texture->blend = true; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 244 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0, |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 245 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels()); |
| 246 | |
| 247 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 248 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 249 | |
| 250 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 251 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 252 | } |
| 253 | |
| 254 | }; // namespace uirenderer |
| 255 | }; // namespace android |