Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1 | /* |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 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" |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_VIEW |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 19 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 20 | #include <SkBitmap.h> |
| 21 | #include <SkCanvas.h> |
| 22 | #include <SkPaint.h> |
| 23 | #include <SkPath.h> |
| 24 | #include <SkRect.h> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 25 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 26 | #include <utils/JenkinsHash.h> |
| 27 | #include <utils/Trace.h> |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 28 | |
| 29 | #include "Caches.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 30 | #include "PathCache.h" |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 31 | |
| 32 | #include "thread/Signal.h" |
| 33 | #include "thread/Task.h" |
| 34 | #include "thread/TaskProcessor.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 39 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 40 | // Cache entries |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | PathDescription::PathDescription(): |
| 44 | type(kShapeNone), |
| 45 | join(SkPaint::kDefault_Join), |
| 46 | cap(SkPaint::kDefault_Cap), |
| 47 | style(SkPaint::kFill_Style), |
| 48 | miter(4.0f), |
| 49 | strokeWidth(1.0f), |
| 50 | pathEffect(NULL) { |
| 51 | memset(&shape, 0, sizeof(Shape)); |
| 52 | } |
| 53 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 54 | PathDescription::PathDescription(ShapeType type, const SkPaint* paint): |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 55 | type(type), |
| 56 | join(paint->getStrokeJoin()), |
| 57 | cap(paint->getStrokeCap()), |
| 58 | style(paint->getStyle()), |
| 59 | miter(paint->getStrokeMiter()), |
| 60 | strokeWidth(paint->getStrokeWidth()), |
| 61 | pathEffect(paint->getPathEffect()) { |
| 62 | memset(&shape, 0, sizeof(Shape)); |
| 63 | } |
| 64 | |
| 65 | hash_t PathDescription::hash() const { |
| 66 | uint32_t hash = JenkinsHashMix(0, type); |
| 67 | hash = JenkinsHashMix(hash, join); |
| 68 | hash = JenkinsHashMix(hash, cap); |
| 69 | hash = JenkinsHashMix(hash, style); |
| 70 | hash = JenkinsHashMix(hash, android::hash_type(miter)); |
| 71 | hash = JenkinsHashMix(hash, android::hash_type(strokeWidth)); |
| 72 | hash = JenkinsHashMix(hash, android::hash_type(pathEffect)); |
| 73 | hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape)); |
| 74 | return JenkinsHashWhiten(hash); |
| 75 | } |
| 76 | |
| 77 | int PathDescription::compare(const PathDescription& rhs) const { |
| 78 | return memcmp(this, &rhs, sizeof(PathDescription)); |
| 79 | } |
| 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | // Utilities |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 85 | bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 86 | // NOTE: This should only be used after PathTessellator handles joins properly |
| 87 | return paint->getPathEffect() == NULL && path->getConvexity() == SkPath::kConvex_Convexity; |
| 88 | } |
| 89 | |
| 90 | void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint, |
| 91 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
| 92 | const SkRect& bounds = path->getBounds(); |
| 93 | PathCache::computeBounds(bounds, paint, left, top, offset, width, height); |
| 94 | } |
| 95 | |
| 96 | void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint, |
| 97 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
| 98 | const float pathWidth = fmax(bounds.width(), 1.0f); |
| 99 | const float pathHeight = fmax(bounds.height(), 1.0f); |
| 100 | |
| 101 | left = bounds.fLeft; |
| 102 | top = bounds.fTop; |
| 103 | |
| 104 | offset = (int) floorf(fmax(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f); |
| 105 | |
| 106 | width = uint32_t(pathWidth + offset * 2.0 + 0.5); |
| 107 | height = uint32_t(pathHeight + offset * 2.0 + 0.5); |
| 108 | } |
| 109 | |
| 110 | static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) { |
| 111 | bitmap.setConfig(SkBitmap::kA8_Config, width, height); |
| 112 | bitmap.allocPixels(); |
| 113 | bitmap.eraseColor(0); |
| 114 | } |
| 115 | |
| 116 | static void initPaint(SkPaint& paint) { |
| 117 | // Make sure the paint is opaque, color, alpha, filter, etc. |
| 118 | // will be applied later when compositing the alpha8 texture |
| 119 | paint.setColor(0xff000000); |
| 120 | paint.setAlpha(255); |
| 121 | paint.setColorFilter(NULL); |
| 122 | paint.setMaskFilter(NULL); |
| 123 | paint.setShader(NULL); |
| 124 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode); |
| 125 | SkSafeUnref(paint.setXfermode(mode)); |
| 126 | } |
| 127 | |
| 128 | static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap, |
| 129 | float left, float top, float offset, uint32_t width, uint32_t height) { |
| 130 | initBitmap(bitmap, width, height); |
| 131 | |
| 132 | SkPaint pathPaint(*paint); |
| 133 | initPaint(pathPaint); |
| 134 | |
| 135 | SkCanvas canvas(bitmap); |
| 136 | canvas.translate(-left + offset, -top + offset); |
| 137 | canvas.drawPath(*path, pathPaint); |
| 138 | } |
| 139 | |
| 140 | static PathTexture* createTexture(float left, float top, float offset, |
| 141 | uint32_t width, uint32_t height, uint32_t id) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 142 | PathTexture* texture = new PathTexture(Caches::getInstance()); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 143 | texture->left = left; |
| 144 | texture->top = top; |
| 145 | texture->offset = offset; |
| 146 | texture->width = width; |
| 147 | texture->height = height; |
| 148 | texture->generation = id; |
| 149 | return texture; |
| 150 | } |
| 151 | |
| 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | // Cache constructor/destructor |
| 154 | /////////////////////////////////////////////////////////////////////////////// |
| 155 | |
| 156 | PathCache::PathCache(): |
| 157 | mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity), |
| 158 | mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) { |
| 159 | char property[PROPERTY_VALUE_MAX]; |
| 160 | if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) { |
| 161 | INIT_LOGD(" Setting %s cache size to %sMB", name, property); |
| 162 | setMaxSize(MB(atof(property))); |
| 163 | } else { |
| 164 | INIT_LOGD(" Using default %s cache size of %.2fMB", name, DEFAULT_PATH_CACHE_SIZE); |
| 165 | } |
| 166 | init(); |
| 167 | } |
| 168 | |
| 169 | PathCache::~PathCache() { |
| 170 | mCache.clear(); |
| 171 | } |
| 172 | |
| 173 | void PathCache::init() { |
| 174 | mCache.setOnEntryRemovedListener(this); |
| 175 | |
| 176 | GLint maxTextureSize; |
| 177 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 178 | mMaxTextureSize = maxTextureSize; |
| 179 | |
| 180 | mDebugEnabled = readDebugLevel() & kDebugCaches; |
| 181 | } |
| 182 | |
| 183 | /////////////////////////////////////////////////////////////////////////////// |
| 184 | // Size management |
| 185 | /////////////////////////////////////////////////////////////////////////////// |
| 186 | |
| 187 | uint32_t PathCache::getSize() { |
| 188 | return mSize; |
| 189 | } |
| 190 | |
| 191 | uint32_t PathCache::getMaxSize() { |
| 192 | return mMaxSize; |
| 193 | } |
| 194 | |
| 195 | void PathCache::setMaxSize(uint32_t maxSize) { |
| 196 | mMaxSize = maxSize; |
| 197 | while (mSize > mMaxSize) { |
| 198 | mCache.removeOldest(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | // Callbacks |
| 204 | /////////////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | void PathCache::operator()(PathDescription& entry, PathTexture*& texture) { |
| 207 | removeTexture(texture); |
| 208 | } |
| 209 | |
| 210 | /////////////////////////////////////////////////////////////////////////////// |
| 211 | // Caching |
| 212 | /////////////////////////////////////////////////////////////////////////////// |
| 213 | |
| 214 | void PathCache::removeTexture(PathTexture* texture) { |
| 215 | if (texture) { |
| 216 | const uint32_t size = texture->width * texture->height; |
Romain Guy | 5d92320 | 2013-08-21 18:40:24 -0700 | [diff] [blame] | 217 | |
| 218 | // If there is a pending task we must wait for it to return |
| 219 | // before attempting our cleanup |
| 220 | const sp<Task<SkBitmap*> >& task = texture->task(); |
| 221 | if (task != NULL) { |
| 222 | SkBitmap* bitmap = task->getResult(); |
| 223 | texture->clearTask(); |
| 224 | } else { |
| 225 | // If there is a pending task, the path was not added |
| 226 | // to the cache and the size wasn't increased |
| 227 | if (size > mSize) { |
| 228 | ALOGE("Removing path texture of size %d will leave " |
| 229 | "the cache in an inconsistent state", size); |
| 230 | } |
| 231 | mSize -= size; |
| 232 | } |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 233 | |
| 234 | PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d", |
| 235 | texture->id, size, mSize); |
| 236 | if (mDebugEnabled) { |
| 237 | ALOGD("Shape deleted, size = %d", size); |
| 238 | } |
| 239 | |
| 240 | if (texture->id) { |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 241 | Caches::getInstance().deleteTexture(texture->id); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 242 | } |
| 243 | delete texture; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void PathCache::purgeCache(uint32_t width, uint32_t height) { |
| 248 | const uint32_t size = width * height; |
| 249 | // Don't even try to cache a bitmap that's bigger than the cache |
| 250 | if (size < mMaxSize) { |
| 251 | while (mSize + size > mMaxSize) { |
| 252 | mCache.removeOldest(); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void PathCache::trim() { |
| 258 | while (mSize > mMaxSize) { |
| 259 | mCache.removeOldest(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path, |
| 264 | const SkPaint* paint) { |
| 265 | ATRACE_CALL(); |
| 266 | |
| 267 | float left, top, offset; |
| 268 | uint32_t width, height; |
| 269 | computePathBounds(path, paint, left, top, offset, width, height); |
| 270 | |
| 271 | if (!checkTextureSize(width, height)) return NULL; |
| 272 | |
| 273 | purgeCache(width, height); |
| 274 | |
| 275 | SkBitmap bitmap; |
| 276 | drawPath(path, paint, bitmap, left, top, offset, width, height); |
| 277 | |
| 278 | PathTexture* texture = createTexture(left, top, offset, width, height, |
| 279 | path->getGenerationID()); |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 280 | generateTexture(entry, &bitmap, texture); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 281 | |
| 282 | return texture; |
| 283 | } |
| 284 | |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 285 | void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap, |
| 286 | PathTexture* texture, bool addToCache) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 287 | generateTexture(*bitmap, texture); |
| 288 | |
| 289 | uint32_t size = texture->width * texture->height; |
| 290 | if (size < mMaxSize) { |
| 291 | mSize += size; |
| 292 | PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d", |
| 293 | texture->id, size, mSize); |
| 294 | if (mDebugEnabled) { |
| 295 | ALOGD("Shape created, size = %d", size); |
| 296 | } |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 297 | if (addToCache) { |
| 298 | mCache.put(entry, texture); |
| 299 | } |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 300 | } else { |
Romain Guy | 0a8c51b | 2013-08-21 17:35:38 -0700 | [diff] [blame] | 301 | // It's okay to add a texture that's bigger than the cache since |
| 302 | // we'll trim the cache later when addToCache is set to false |
| 303 | if (!addToCache) { |
| 304 | mSize += size; |
| 305 | } |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 306 | texture->cleanup = true; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void PathCache::clear() { |
| 311 | mCache.clear(); |
| 312 | } |
| 313 | |
| 314 | void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) { |
| 315 | SkAutoLockPixels alp(bitmap); |
| 316 | if (!bitmap.readyToDraw()) { |
| 317 | ALOGE("Cannot generate texture from bitmap"); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | glGenTextures(1, &texture->id); |
| 322 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 323 | Caches::getInstance().bindTexture(texture->id); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 324 | // Textures are Alpha8 |
| 325 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 326 | |
| 327 | texture->blend = true; |
| 328 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0, |
| 329 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels()); |
| 330 | |
| 331 | texture->setFilter(GL_LINEAR); |
| 332 | texture->setWrap(GL_CLAMP_TO_EDGE); |
| 333 | } |
| 334 | |
| 335 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 336 | // Path precaching |
| 337 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 338 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 339 | PathCache::PathProcessor::PathProcessor(Caches& caches): |
| 340 | TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) { |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 341 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 342 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 343 | void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) { |
| 344 | sp<PathTask> t = static_cast<PathTask* >(task.get()); |
| 345 | ATRACE_NAME("pathPrecache"); |
| 346 | |
| 347 | float left, top, offset; |
| 348 | uint32_t width, height; |
| 349 | PathCache::computePathBounds(t->path, t->paint, left, top, offset, width, height); |
| 350 | |
| 351 | PathTexture* texture = t->texture; |
| 352 | texture->left = left; |
| 353 | texture->top = top; |
| 354 | texture->offset = offset; |
| 355 | texture->width = width; |
| 356 | texture->height = height; |
| 357 | |
| 358 | if (width <= mMaxTextureSize && height <= mMaxTextureSize) { |
| 359 | SkBitmap* bitmap = new SkBitmap(); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 360 | drawPath(t->path, t->paint, *bitmap, left, top, offset, width, height); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 361 | t->setResult(bitmap); |
| 362 | } else { |
Romain Guy | 0f809f3 | 2013-03-13 14:31:46 -0700 | [diff] [blame] | 363 | texture->width = 0; |
| 364 | texture->height = 0; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 365 | t->setResult(NULL); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 366 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 369 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 370 | // Paths |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 371 | /////////////////////////////////////////////////////////////////////////////// |
| 372 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 373 | void PathCache::remove(Vector<PathDescription>& pathsToRemove, const path_pair_t& pair) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 374 | LruCache<PathDescription, PathTexture*>::Iterator i(mCache); |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 375 | |
| 376 | while (i.next()) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 377 | const PathDescription& key = i.key(); |
| 378 | if (key.type == kShapePath && |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 379 | (key.shape.path.mPath == pair.getFirst() || |
| 380 | key.shape.path.mPath == pair.getSecond())) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 381 | pathsToRemove.push(key); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void PathCache::removeDeferred(SkPath* path) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 387 | Mutex::Autolock l(mLock); |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 388 | mGarbage.push(path_pair_t(path, const_cast<SkPath*>(path->getSourcePath()))); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void PathCache::clearGarbage() { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 392 | Vector<PathDescription> pathsToRemove; |
| 393 | |
| 394 | { // scope for the mutex |
| 395 | Mutex::Autolock l(mLock); |
| 396 | size_t count = mGarbage.size(); |
| 397 | for (size_t i = 0; i < count; i++) { |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 398 | const path_pair_t& pair = mGarbage.itemAt(i); |
| 399 | remove(pathsToRemove, pair); |
| 400 | delete pair.getFirst(); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 401 | } |
| 402 | mGarbage.clear(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 403 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 404 | |
| 405 | for (size_t i = 0; i < pathsToRemove.size(); i++) { |
| 406 | mCache.remove(pathsToRemove.itemAt(i)); |
| 407 | } |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 410 | /** |
| 411 | * To properly handle path mutations at draw time we always make a copy |
| 412 | * of paths objects when recording display lists. The source path points |
| 413 | * to the path we originally copied the path from. This ensures we use |
| 414 | * the original path as a cache key the first time a path is inserted |
| 415 | * in the cache. The source path is also used to reclaim garbage when a |
| 416 | * Dalvik Path object is collected. |
| 417 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 418 | static const SkPath* getSourcePath(const SkPath* path) { |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 419 | const SkPath* sourcePath = path->getSourcePath(); |
| 420 | if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 421 | return const_cast<SkPath*>(sourcePath); |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 422 | } |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 423 | return path; |
| 424 | } |
| 425 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 426 | PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 427 | path = getSourcePath(path); |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 428 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 429 | PathDescription entry(kShapePath, paint); |
| 430 | entry.shape.path.mPath = path; |
| 431 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 432 | PathTexture* texture = mCache.get(entry); |
| 433 | |
| 434 | if (!texture) { |
| 435 | texture = addTexture(entry, path, paint); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 436 | } else { |
| 437 | // A bitmap is attached to the texture, this means we need to |
| 438 | // upload it as a GL texture |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 439 | const sp<Task<SkBitmap*> >& task = texture->task(); |
| 440 | if (task != NULL) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 441 | // But we must first wait for the worker thread to be done |
| 442 | // producing the bitmap, so let's wait |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 443 | SkBitmap* bitmap = task->getResult(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 444 | if (bitmap) { |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 445 | generateTexture(entry, bitmap, texture, false); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 446 | texture->clearTask(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 447 | } else { |
Romain Guy | 0f809f3 | 2013-03-13 14:31:46 -0700 | [diff] [blame] | 448 | ALOGW("Path too large to be rendered into a texture"); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 449 | texture->clearTask(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 450 | texture = NULL; |
| 451 | mCache.remove(entry); |
| 452 | } |
| 453 | } else if (path->getGenerationID() != texture->generation) { |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 454 | // The size of the path might have changed so we first |
| 455 | // remove the entry from the cache |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 456 | mCache.remove(entry); |
| 457 | texture = addTexture(entry, path, paint); |
| 458 | } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 461 | return texture; |
| 462 | } |
| 463 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 464 | void PathCache::precache(const SkPath* path, const SkPaint* paint) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 465 | if (!Caches::getInstance().tasks.canRunTasks()) { |
| 466 | return; |
| 467 | } |
| 468 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 469 | path = getSourcePath(path); |
| 470 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 471 | PathDescription entry(kShapePath, paint); |
| 472 | entry.shape.path.mPath = path; |
| 473 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 474 | PathTexture* texture = mCache.get(entry); |
| 475 | |
| 476 | bool generate = false; |
| 477 | if (!texture) { |
| 478 | generate = true; |
| 479 | } else if (path->getGenerationID() != texture->generation) { |
| 480 | mCache.remove(entry); |
| 481 | generate = true; |
| 482 | } |
| 483 | |
| 484 | if (generate) { |
| 485 | // It is important to specify the generation ID so we do not |
| 486 | // attempt to precache the same path several times |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 487 | texture = createTexture(0.0f, 0.0f, 0.0f, 0, 0, path->getGenerationID()); |
| 488 | sp<PathTask> task = new PathTask(path, paint, texture); |
| 489 | texture->setTask(task); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 490 | |
| 491 | // During the precaching phase we insert path texture objects into |
| 492 | // the cache that do not point to any GL texture. They are instead |
| 493 | // treated as a task for the precaching worker thread. This is why |
| 494 | // we do not check the cache limit when inserting these objects. |
| 495 | // The conversion into GL texture will happen in get(), when a client |
| 496 | // asks for a path texture. This is also when the cache limit will |
| 497 | // be enforced. |
| 498 | mCache.put(entry, texture); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 499 | |
| 500 | if (mProcessor == NULL) { |
| 501 | mProcessor = new PathProcessor(Caches::getInstance()); |
| 502 | } |
| 503 | mProcessor->add(task); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 507 | /////////////////////////////////////////////////////////////////////////////// |
| 508 | // Rounded rects |
| 509 | /////////////////////////////////////////////////////////////////////////////// |
| 510 | |
| 511 | PathTexture* PathCache::getRoundRect(float width, float height, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 512 | float rx, float ry, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 513 | PathDescription entry(kShapeRoundRect, paint); |
| 514 | entry.shape.roundRect.mWidth = width; |
| 515 | entry.shape.roundRect.mHeight = height; |
| 516 | entry.shape.roundRect.mRx = rx; |
| 517 | entry.shape.roundRect.mRy = ry; |
| 518 | |
| 519 | PathTexture* texture = get(entry); |
| 520 | |
| 521 | if (!texture) { |
| 522 | SkPath path; |
| 523 | SkRect r; |
| 524 | r.set(0.0f, 0.0f, width, height); |
| 525 | path.addRoundRect(r, rx, ry, SkPath::kCW_Direction); |
| 526 | |
| 527 | texture = addTexture(entry, &path, paint); |
| 528 | } |
| 529 | |
| 530 | return texture; |
| 531 | } |
| 532 | |
| 533 | /////////////////////////////////////////////////////////////////////////////// |
| 534 | // Circles |
| 535 | /////////////////////////////////////////////////////////////////////////////// |
| 536 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 537 | PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 538 | PathDescription entry(kShapeCircle, paint); |
| 539 | entry.shape.circle.mRadius = radius; |
| 540 | |
| 541 | PathTexture* texture = get(entry); |
| 542 | |
| 543 | if (!texture) { |
| 544 | SkPath path; |
| 545 | path.addCircle(radius, radius, radius, SkPath::kCW_Direction); |
| 546 | |
| 547 | texture = addTexture(entry, &path, paint); |
| 548 | } |
| 549 | |
| 550 | return texture; |
| 551 | } |
| 552 | |
| 553 | /////////////////////////////////////////////////////////////////////////////// |
| 554 | // Ovals |
| 555 | /////////////////////////////////////////////////////////////////////////////// |
| 556 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 557 | PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 558 | PathDescription entry(kShapeOval, paint); |
| 559 | entry.shape.oval.mWidth = width; |
| 560 | entry.shape.oval.mHeight = height; |
| 561 | |
| 562 | PathTexture* texture = get(entry); |
| 563 | |
| 564 | if (!texture) { |
| 565 | SkPath path; |
| 566 | SkRect r; |
| 567 | r.set(0.0f, 0.0f, width, height); |
| 568 | path.addOval(r, SkPath::kCW_Direction); |
| 569 | |
| 570 | texture = addTexture(entry, &path, paint); |
| 571 | } |
| 572 | |
| 573 | return texture; |
| 574 | } |
| 575 | |
| 576 | /////////////////////////////////////////////////////////////////////////////// |
| 577 | // Rects |
| 578 | /////////////////////////////////////////////////////////////////////////////// |
| 579 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 580 | PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 581 | PathDescription entry(kShapeRect, paint); |
| 582 | entry.shape.rect.mWidth = width; |
| 583 | entry.shape.rect.mHeight = height; |
| 584 | |
| 585 | PathTexture* texture = get(entry); |
| 586 | |
| 587 | if (!texture) { |
| 588 | SkPath path; |
| 589 | SkRect r; |
| 590 | r.set(0.0f, 0.0f, width, height); |
| 591 | path.addRect(r, SkPath::kCW_Direction); |
| 592 | |
| 593 | texture = addTexture(entry, &path, paint); |
| 594 | } |
| 595 | |
| 596 | return texture; |
| 597 | } |
| 598 | |
| 599 | /////////////////////////////////////////////////////////////////////////////// |
| 600 | // Arcs |
| 601 | /////////////////////////////////////////////////////////////////////////////// |
| 602 | |
| 603 | PathTexture* PathCache::getArc(float width, float height, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 604 | float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 605 | PathDescription entry(kShapeArc, paint); |
| 606 | entry.shape.arc.mWidth = width; |
| 607 | entry.shape.arc.mHeight = height; |
| 608 | entry.shape.arc.mStartAngle = startAngle; |
| 609 | entry.shape.arc.mSweepAngle = sweepAngle; |
| 610 | entry.shape.arc.mUseCenter = useCenter; |
| 611 | |
| 612 | PathTexture* texture = get(entry); |
| 613 | |
| 614 | if (!texture) { |
| 615 | SkPath path; |
| 616 | SkRect r; |
| 617 | r.set(0.0f, 0.0f, width, height); |
| 618 | if (useCenter) { |
| 619 | path.moveTo(r.centerX(), r.centerY()); |
| 620 | } |
| 621 | path.arcTo(r, startAngle, sweepAngle, !useCenter); |
| 622 | if (useCenter) { |
| 623 | path.close(); |
| 624 | } |
| 625 | |
| 626 | texture = addTexture(entry, &path, paint); |
| 627 | } |
| 628 | |
| 629 | return texture; |
| 630 | } |
| 631 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 632 | }; // namespace uirenderer |
| 633 | }; // namespace android |