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() { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 56 | Mutex::Autolock _l(mLock); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 57 | mCache.clear(); |
| 58 | } |
| 59 | |
| 60 | void PathCache::init() { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 61 | mCache.setOnEntryRemovedListener(this); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 62 | |
| 63 | GLint maxTextureSize; |
| 64 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 65 | mMaxTextureSize = maxTextureSize; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | // Size management |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | uint32_t PathCache::getSize() { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 73 | Mutex::Autolock _l(mLock); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 74 | return mSize; |
| 75 | } |
| 76 | |
| 77 | uint32_t PathCache::getMaxSize() { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 78 | Mutex::Autolock _l(mLock); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 79 | return mMaxSize; |
| 80 | } |
| 81 | |
| 82 | void PathCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 83 | Mutex::Autolock _l(mLock); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 84 | mMaxSize = maxSize; |
| 85 | while (mSize > mMaxSize) { |
| 86 | mCache.removeOldest(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////////// |
| 91 | // Callbacks |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | void 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 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 108 | void 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 | return; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 119 | PathTexture* PathCache::get(SkPath* path, SkPaint* paint) { |
| 120 | PathCacheEntry entry(path, paint); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 121 | |
| 122 | mLock.lock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 123 | PathTexture* texture = mCache.get(entry); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 124 | mLock.unlock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 125 | |
| 126 | if (!texture) { |
| 127 | texture = addTexture(entry, path, paint); |
| 128 | } else if (path->getGenerationID() != texture->generation) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 129 | mLock.lock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 130 | mCache.remove(entry); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 131 | mLock.unlock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 132 | texture = addTexture(entry, path, paint); |
| 133 | } |
| 134 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 135 | return texture; |
| 136 | } |
| 137 | |
| 138 | PathTexture* PathCache::addTexture(const PathCacheEntry& entry, |
| 139 | const SkPath *path, const SkPaint* paint) { |
| 140 | const SkRect& bounds = path->getBounds(); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 141 | |
| 142 | const float pathWidth = bounds.width(); |
| 143 | const float pathHeight = bounds.height(); |
| 144 | |
| 145 | if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) { |
| 146 | LOGW("Path too large to be rendered into a texture"); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 150 | const float offset = entry.strokeWidth * 1.5f; |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 151 | const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5); |
| 152 | const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 153 | |
| 154 | const uint32_t size = width * height; |
| 155 | // Don't even try to cache a bitmap that's bigger than the cache |
| 156 | if (size < mMaxSize) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 157 | mLock.lock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 158 | while (mSize + size > mMaxSize) { |
| 159 | mCache.removeOldest(); |
| 160 | } |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 161 | mLock.unlock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | PathTexture* texture = new PathTexture; |
| 165 | texture->left = bounds.fLeft; |
| 166 | texture->top = bounds.fTop; |
| 167 | texture->offset = offset; |
| 168 | texture->width = width; |
| 169 | texture->height = height; |
| 170 | texture->generation = path->getGenerationID(); |
| 171 | |
| 172 | SkBitmap bitmap; |
| 173 | bitmap.setConfig(SkBitmap::kA8_Config, width, height); |
| 174 | bitmap.allocPixels(); |
| 175 | bitmap.eraseColor(0); |
| 176 | |
| 177 | SkCanvas canvas(bitmap); |
| 178 | canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset); |
| 179 | canvas.drawPath(*path, *paint); |
| 180 | |
| 181 | generateTexture(bitmap, texture); |
| 182 | |
| 183 | if (size < mMaxSize) { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 184 | mLock.lock(); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 185 | mSize += size; |
| 186 | mCache.put(entry, texture); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 187 | mLock.unlock(); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 188 | } else { |
| 189 | texture->cleanup = true; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return texture; |
| 193 | } |
| 194 | |
| 195 | void PathCache::clear() { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 196 | Mutex::Autolock _l(mLock); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 197 | mCache.clear(); |
| 198 | } |
| 199 | |
| 200 | void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) { |
| 201 | SkAutoLockPixels alp(bitmap); |
| 202 | if (!bitmap.readyToDraw()) { |
| 203 | LOGE("Cannot generate texture from bitmap"); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | glGenTextures(1, &texture->id); |
| 208 | |
| 209 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 210 | // Textures are Alpha8 |
| 211 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 212 | |
| 213 | texture->blend = true; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 214 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0, |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 215 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels()); |
| 216 | |
| 217 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 218 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 219 | |
| 220 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 221 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 222 | } |
| 223 | |
| 224 | }; // namespace uirenderer |
| 225 | }; // namespace android |