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 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 17 | #include <SkBitmap.h> |
| 18 | #include <SkCanvas.h> |
Chris Craik | 98d608d | 2014-07-17 12:25:11 -0700 | [diff] [blame] | 19 | #include <SkColor.h> |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 20 | #include <SkPaint.h> |
| 21 | #include <SkPath.h> |
| 22 | #include <SkRect.h> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 23 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 24 | #include <utils/JenkinsHash.h> |
| 25 | #include <utils/Trace.h> |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 26 | |
| 27 | #include "Caches.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 28 | #include "PathCache.h" |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 29 | |
| 30 | #include "thread/Signal.h" |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 31 | #include "thread/TaskProcessor.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 32 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 33 | #include <cutils/properties.h> |
| 34 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 38 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 39 | // Cache entries |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 42 | PathDescription::PathDescription() |
| 43 | : type(kShapeNone) |
| 44 | , join(SkPaint::kDefault_Join) |
| 45 | , cap(SkPaint::kDefault_Cap) |
| 46 | , style(SkPaint::kFill_Style) |
| 47 | , miter(4.0f) |
| 48 | , strokeWidth(1.0f) |
| 49 | , pathEffect(nullptr) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 50 | memset(&shape, 0, sizeof(Shape)); |
| 51 | } |
| 52 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 53 | PathDescription::PathDescription(ShapeType type, const SkPaint* paint) |
| 54 | : type(type) |
| 55 | , join(paint->getStrokeJoin()) |
| 56 | , cap(paint->getStrokeCap()) |
| 57 | , style(paint->getStyle()) |
| 58 | , miter(paint->getStrokeMiter()) |
| 59 | , strokeWidth(paint->getStrokeWidth()) |
| 60 | , pathEffect(paint->getPathEffect()) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 61 | memset(&shape, 0, sizeof(Shape)); |
| 62 | } |
| 63 | |
| 64 | hash_t PathDescription::hash() const { |
| 65 | uint32_t hash = JenkinsHashMix(0, type); |
| 66 | hash = JenkinsHashMix(hash, join); |
| 67 | hash = JenkinsHashMix(hash, cap); |
| 68 | hash = JenkinsHashMix(hash, style); |
| 69 | hash = JenkinsHashMix(hash, android::hash_type(miter)); |
| 70 | hash = JenkinsHashMix(hash, android::hash_type(strokeWidth)); |
| 71 | hash = JenkinsHashMix(hash, android::hash_type(pathEffect)); |
| 72 | hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape)); |
| 73 | return JenkinsHashWhiten(hash); |
| 74 | } |
| 75 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | // Utilities |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 80 | bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 81 | // NOTE: This should only be used after PathTessellator handles joins properly |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 82 | return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint, |
| 86 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
| 87 | const SkRect& bounds = path->getBounds(); |
| 88 | PathCache::computeBounds(bounds, paint, left, top, offset, width, height); |
| 89 | } |
| 90 | |
| 91 | void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint, |
| 92 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
Chris Craik | e6a15ee | 2015-07-07 18:42:17 -0700 | [diff] [blame] | 93 | const float pathWidth = std::max(bounds.width(), 1.0f); |
| 94 | const float pathHeight = std::max(bounds.height(), 1.0f); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 95 | |
| 96 | left = bounds.fLeft; |
| 97 | top = bounds.fTop; |
| 98 | |
Chris Craik | e6a15ee | 2015-07-07 18:42:17 -0700 | [diff] [blame] | 99 | offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 100 | |
| 101 | width = uint32_t(pathWidth + offset * 2.0 + 0.5); |
| 102 | height = uint32_t(pathHeight + offset * 2.0 + 0.5); |
| 103 | } |
| 104 | |
| 105 | static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) { |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 106 | bitmap.allocPixels(SkImageInfo::MakeA8(width, height)); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 107 | bitmap.eraseColor(0); |
| 108 | } |
| 109 | |
| 110 | static void initPaint(SkPaint& paint) { |
| 111 | // Make sure the paint is opaque, color, alpha, filter, etc. |
| 112 | // will be applied later when compositing the alpha8 texture |
Chris Craik | 98d608d | 2014-07-17 12:25:11 -0700 | [diff] [blame] | 113 | paint.setColor(SK_ColorBLACK); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 114 | paint.setAlpha(255); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 115 | paint.setColorFilter(nullptr); |
| 116 | paint.setMaskFilter(nullptr); |
| 117 | paint.setShader(nullptr); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 118 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode); |
| 119 | SkSafeUnref(paint.setXfermode(mode)); |
| 120 | } |
| 121 | |
| 122 | static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap, |
| 123 | float left, float top, float offset, uint32_t width, uint32_t height) { |
| 124 | initBitmap(bitmap, width, height); |
| 125 | |
| 126 | SkPaint pathPaint(*paint); |
| 127 | initPaint(pathPaint); |
| 128 | |
| 129 | SkCanvas canvas(bitmap); |
| 130 | canvas.translate(-left + offset, -top + offset); |
| 131 | canvas.drawPath(*path, pathPaint); |
| 132 | } |
| 133 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | // Cache constructor/destructor |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | PathCache::PathCache(): |
| 139 | mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity), |
| 140 | mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) { |
| 141 | char property[PROPERTY_VALUE_MAX]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 142 | if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) { |
Chris Craik | 11718bc | 2015-09-22 11:50:13 -0700 | [diff] [blame] | 143 | INIT_LOGD(" Setting path cache size to %sMB", property); |
Chris Craik | 42455fc | 2015-05-11 18:23:09 -0700 | [diff] [blame] | 144 | mMaxSize = MB(atof(property)); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 145 | } else { |
Chris Craik | 11718bc | 2015-09-22 11:50:13 -0700 | [diff] [blame] | 146 | INIT_LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 147 | } |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 148 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 149 | mCache.setOnEntryRemovedListener(this); |
| 150 | |
| 151 | GLint maxTextureSize; |
| 152 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 153 | mMaxTextureSize = maxTextureSize; |
| 154 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 155 | mDebugEnabled = Properties::debugLevel & kDebugCaches; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 158 | PathCache::~PathCache() { |
| 159 | mCache.clear(); |
| 160 | } |
| 161 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////// |
| 163 | // Size management |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
| 166 | uint32_t PathCache::getSize() { |
| 167 | return mSize; |
| 168 | } |
| 169 | |
| 170 | uint32_t PathCache::getMaxSize() { |
| 171 | return mMaxSize; |
| 172 | } |
| 173 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 174 | /////////////////////////////////////////////////////////////////////////////// |
| 175 | // Callbacks |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 178 | void PathCache::operator()(PathDescription& entry, PathTexture*& texture) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 179 | removeTexture(texture); |
| 180 | } |
| 181 | |
| 182 | /////////////////////////////////////////////////////////////////////////////// |
| 183 | // Caching |
| 184 | /////////////////////////////////////////////////////////////////////////////// |
| 185 | |
| 186 | void PathCache::removeTexture(PathTexture* texture) { |
| 187 | if (texture) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 188 | const uint32_t size = texture->width() * texture->height(); |
Romain Guy | 5d92320 | 2013-08-21 18:40:24 -0700 | [diff] [blame] | 189 | |
| 190 | // If there is a pending task we must wait for it to return |
| 191 | // before attempting our cleanup |
| 192 | const sp<Task<SkBitmap*> >& task = texture->task(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 193 | if (task != nullptr) { |
Andreas Gampe | 1e19674 | 2014-11-10 15:23:43 -0800 | [diff] [blame] | 194 | task->getResult(); |
Romain Guy | 5d92320 | 2013-08-21 18:40:24 -0700 | [diff] [blame] | 195 | texture->clearTask(); |
| 196 | } else { |
| 197 | // If there is a pending task, the path was not added |
| 198 | // to the cache and the size wasn't increased |
| 199 | if (size > mSize) { |
| 200 | ALOGE("Removing path texture of size %d will leave " |
| 201 | "the cache in an inconsistent state", size); |
| 202 | } |
| 203 | mSize -= size; |
| 204 | } |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 205 | |
| 206 | PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d", |
| 207 | texture->id, size, mSize); |
| 208 | if (mDebugEnabled) { |
| 209 | ALOGD("Shape deleted, size = %d", size); |
| 210 | } |
| 211 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 212 | texture->deleteTexture(); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 213 | delete texture; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void PathCache::purgeCache(uint32_t width, uint32_t height) { |
| 218 | const uint32_t size = width * height; |
| 219 | // Don't even try to cache a bitmap that's bigger than the cache |
| 220 | if (size < mMaxSize) { |
| 221 | while (mSize + size > mMaxSize) { |
| 222 | mCache.removeOldest(); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void PathCache::trim() { |
| 228 | while (mSize > mMaxSize) { |
| 229 | mCache.removeOldest(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path, |
| 234 | const SkPaint* paint) { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 235 | ATRACE_NAME("Generate Path Texture"); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 236 | |
| 237 | float left, top, offset; |
| 238 | uint32_t width, height; |
| 239 | computePathBounds(path, paint, left, top, offset, width, height); |
| 240 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 241 | if (!checkTextureSize(width, height)) return nullptr; |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 242 | |
| 243 | purgeCache(width, height); |
| 244 | |
| 245 | SkBitmap bitmap; |
| 246 | drawPath(path, paint, bitmap, left, top, offset, width, height); |
| 247 | |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 248 | PathTexture* texture = new PathTexture(Caches::getInstance(), |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 249 | left, top, offset, path->getGenerationID()); |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 250 | generateTexture(entry, &bitmap, texture); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 251 | |
| 252 | return texture; |
| 253 | } |
| 254 | |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 255 | void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap, |
| 256 | PathTexture* texture, bool addToCache) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 257 | generateTexture(*bitmap, texture); |
| 258 | |
Chris Craik | 42455fc | 2015-05-11 18:23:09 -0700 | [diff] [blame] | 259 | // Note here that we upload to a texture even if it's bigger than mMaxSize. |
| 260 | // Such an entry in mCache will only be temporary, since it will be evicted |
| 261 | // immediately on trim, or on any other Path entering the cache. |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 262 | uint32_t size = texture->width() * texture->height(); |
Chris Craik | 42455fc | 2015-05-11 18:23:09 -0700 | [diff] [blame] | 263 | mSize += size; |
| 264 | PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d", |
| 265 | texture->id, size, mSize); |
| 266 | if (mDebugEnabled) { |
| 267 | ALOGD("Shape created, size = %d", size); |
| 268 | } |
| 269 | if (addToCache) { |
| 270 | mCache.put(entry, texture); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | void PathCache::clear() { |
| 275 | mCache.clear(); |
| 276 | } |
| 277 | |
| 278 | void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) { |
Chris Craik | cf8426c | 2015-05-13 17:05:48 -0700 | [diff] [blame] | 279 | ATRACE_NAME("Upload Path Texture"); |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame^] | 280 | texture->upload(bitmap); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 281 | texture->setFilter(GL_LINEAR); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 285 | // Path precaching |
| 286 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 287 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 288 | PathCache::PathProcessor::PathProcessor(Caches& caches): |
| 289 | TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) { |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 290 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 291 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 292 | void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) { |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 293 | PathTask* t = static_cast<PathTask*>(task.get()); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 294 | ATRACE_NAME("pathPrecache"); |
| 295 | |
| 296 | float left, top, offset; |
| 297 | uint32_t width, height; |
Chris Craik | 906d47f | 2014-06-27 18:30:23 -0700 | [diff] [blame] | 298 | PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 299 | |
| 300 | PathTexture* texture = t->texture; |
| 301 | texture->left = left; |
| 302 | texture->top = top; |
| 303 | texture->offset = offset; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 304 | |
| 305 | if (width <= mMaxTextureSize && height <= mMaxTextureSize) { |
| 306 | SkBitmap* bitmap = new SkBitmap(); |
Chris Craik | 906d47f | 2014-06-27 18:30:23 -0700 | [diff] [blame] | 307 | drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 308 | t->setResult(bitmap); |
| 309 | } else { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 310 | t->setResult(nullptr); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 311 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 314 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 315 | // Paths |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 316 | /////////////////////////////////////////////////////////////////////////////// |
| 317 | |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 318 | void PathCache::removeDeferred(const SkPath* path) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 319 | Mutex::Autolock l(mLock); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 320 | mGarbage.push_back(path->getGenerationID()); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void PathCache::clearGarbage() { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 324 | Vector<PathDescription> pathsToRemove; |
| 325 | |
| 326 | { // scope for the mutex |
| 327 | Mutex::Autolock l(mLock); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 328 | for (const uint32_t generationID : mGarbage) { |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 329 | LruCache<PathDescription, PathTexture*>::Iterator iter(mCache); |
| 330 | while (iter.next()) { |
| 331 | const PathDescription& key = iter.key(); |
| 332 | if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) { |
| 333 | pathsToRemove.push(key); |
| 334 | } |
| 335 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 336 | } |
| 337 | mGarbage.clear(); |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 338 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 339 | |
| 340 | for (size_t i = 0; i < pathsToRemove.size(); i++) { |
| 341 | mCache.remove(pathsToRemove.itemAt(i)); |
| 342 | } |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 345 | PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 346 | PathDescription entry(kShapePath, paint); |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 347 | entry.shape.path.mGenerationID = path->getGenerationID(); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 348 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 349 | PathTexture* texture = mCache.get(entry); |
| 350 | |
| 351 | if (!texture) { |
| 352 | texture = addTexture(entry, path, paint); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 353 | } else { |
| 354 | // A bitmap is attached to the texture, this means we need to |
| 355 | // upload it as a GL texture |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 356 | const sp<Task<SkBitmap*> >& task = texture->task(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 357 | if (task != nullptr) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 358 | // But we must first wait for the worker thread to be done |
| 359 | // producing the bitmap, so let's wait |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 360 | SkBitmap* bitmap = task->getResult(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 361 | if (bitmap) { |
Romain Guy | 4500a8d | 2013-03-26 17:29:51 -0700 | [diff] [blame] | 362 | generateTexture(entry, bitmap, texture, false); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 363 | texture->clearTask(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 364 | } else { |
Romain Guy | 0f809f3 | 2013-03-13 14:31:46 -0700 | [diff] [blame] | 365 | ALOGW("Path too large to be rendered into a texture"); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 366 | texture->clearTask(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 367 | texture = nullptr; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 368 | mCache.remove(entry); |
| 369 | } |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 370 | } |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 373 | return texture; |
| 374 | } |
| 375 | |
Digish Pandya | 2e4f67c | 2015-11-04 11:00:28 +0530 | [diff] [blame] | 376 | void PathCache::remove(const SkPath* path, const SkPaint* paint) |
| 377 | { |
| 378 | PathDescription entry(kShapePath, paint); |
| 379 | entry.shape.path.mGenerationID = path->getGenerationID(); |
| 380 | mCache.remove(entry); |
| 381 | } |
| 382 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 383 | void PathCache::precache(const SkPath* path, const SkPaint* paint) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 384 | if (!Caches::getInstance().tasks.canRunTasks()) { |
| 385 | return; |
| 386 | } |
| 387 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 388 | PathDescription entry(kShapePath, paint); |
Derek Sollenberger | ee24859 | 2015-02-12 14:10:21 -0500 | [diff] [blame] | 389 | entry.shape.path.mGenerationID = path->getGenerationID(); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 390 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 391 | PathTexture* texture = mCache.get(entry); |
| 392 | |
| 393 | bool generate = false; |
| 394 | if (!texture) { |
| 395 | generate = true; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | if (generate) { |
| 399 | // It is important to specify the generation ID so we do not |
| 400 | // attempt to precache the same path several times |
Chris Craik | e2bb380 | 2015-03-13 15:07:52 -0700 | [diff] [blame] | 401 | texture = new PathTexture(Caches::getInstance(), path->getGenerationID()); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 402 | sp<PathTask> task = new PathTask(path, paint, texture); |
| 403 | texture->setTask(task); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 404 | |
| 405 | // During the precaching phase we insert path texture objects into |
| 406 | // the cache that do not point to any GL texture. They are instead |
| 407 | // treated as a task for the precaching worker thread. This is why |
| 408 | // we do not check the cache limit when inserting these objects. |
| 409 | // The conversion into GL texture will happen in get(), when a client |
| 410 | // asks for a path texture. This is also when the cache limit will |
| 411 | // be enforced. |
| 412 | mCache.put(entry, texture); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 413 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 414 | if (mProcessor == nullptr) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 415 | mProcessor = new PathProcessor(Caches::getInstance()); |
| 416 | } |
Chris Craik | dee66b6 | 2015-04-20 14:54:49 -0700 | [diff] [blame] | 417 | mProcessor->add(task); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 421 | /////////////////////////////////////////////////////////////////////////////// |
| 422 | // Rounded rects |
| 423 | /////////////////////////////////////////////////////////////////////////////// |
| 424 | |
| 425 | PathTexture* PathCache::getRoundRect(float width, float height, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 426 | float rx, float ry, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 427 | PathDescription entry(kShapeRoundRect, paint); |
| 428 | entry.shape.roundRect.mWidth = width; |
| 429 | entry.shape.roundRect.mHeight = height; |
| 430 | entry.shape.roundRect.mRx = rx; |
| 431 | entry.shape.roundRect.mRy = ry; |
| 432 | |
| 433 | PathTexture* texture = get(entry); |
| 434 | |
| 435 | if (!texture) { |
| 436 | SkPath path; |
| 437 | SkRect r; |
| 438 | r.set(0.0f, 0.0f, width, height); |
| 439 | path.addRoundRect(r, rx, ry, SkPath::kCW_Direction); |
| 440 | |
| 441 | texture = addTexture(entry, &path, paint); |
| 442 | } |
| 443 | |
| 444 | return texture; |
| 445 | } |
| 446 | |
| 447 | /////////////////////////////////////////////////////////////////////////////// |
| 448 | // Circles |
| 449 | /////////////////////////////////////////////////////////////////////////////// |
| 450 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 451 | PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 452 | PathDescription entry(kShapeCircle, paint); |
| 453 | entry.shape.circle.mRadius = radius; |
| 454 | |
| 455 | PathTexture* texture = get(entry); |
| 456 | |
| 457 | if (!texture) { |
| 458 | SkPath path; |
| 459 | path.addCircle(radius, radius, radius, SkPath::kCW_Direction); |
| 460 | |
| 461 | texture = addTexture(entry, &path, paint); |
| 462 | } |
| 463 | |
| 464 | return texture; |
| 465 | } |
| 466 | |
| 467 | /////////////////////////////////////////////////////////////////////////////// |
| 468 | // Ovals |
| 469 | /////////////////////////////////////////////////////////////////////////////// |
| 470 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 471 | PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 472 | PathDescription entry(kShapeOval, paint); |
| 473 | entry.shape.oval.mWidth = width; |
| 474 | entry.shape.oval.mHeight = height; |
| 475 | |
| 476 | PathTexture* texture = get(entry); |
| 477 | |
| 478 | if (!texture) { |
| 479 | SkPath path; |
| 480 | SkRect r; |
| 481 | r.set(0.0f, 0.0f, width, height); |
| 482 | path.addOval(r, SkPath::kCW_Direction); |
| 483 | |
| 484 | texture = addTexture(entry, &path, paint); |
| 485 | } |
| 486 | |
| 487 | return texture; |
| 488 | } |
| 489 | |
| 490 | /////////////////////////////////////////////////////////////////////////////// |
| 491 | // Rects |
| 492 | /////////////////////////////////////////////////////////////////////////////// |
| 493 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 494 | PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 495 | PathDescription entry(kShapeRect, paint); |
| 496 | entry.shape.rect.mWidth = width; |
| 497 | entry.shape.rect.mHeight = height; |
| 498 | |
| 499 | PathTexture* texture = get(entry); |
| 500 | |
| 501 | if (!texture) { |
| 502 | SkPath path; |
| 503 | SkRect r; |
| 504 | r.set(0.0f, 0.0f, width, height); |
| 505 | path.addRect(r, SkPath::kCW_Direction); |
| 506 | |
| 507 | texture = addTexture(entry, &path, paint); |
| 508 | } |
| 509 | |
| 510 | return texture; |
| 511 | } |
| 512 | |
| 513 | /////////////////////////////////////////////////////////////////////////////// |
| 514 | // Arcs |
| 515 | /////////////////////////////////////////////////////////////////////////////// |
| 516 | |
| 517 | PathTexture* PathCache::getArc(float width, float height, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 518 | float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 519 | PathDescription entry(kShapeArc, paint); |
| 520 | entry.shape.arc.mWidth = width; |
| 521 | entry.shape.arc.mHeight = height; |
| 522 | entry.shape.arc.mStartAngle = startAngle; |
| 523 | entry.shape.arc.mSweepAngle = sweepAngle; |
| 524 | entry.shape.arc.mUseCenter = useCenter; |
| 525 | |
| 526 | PathTexture* texture = get(entry); |
| 527 | |
| 528 | if (!texture) { |
| 529 | SkPath path; |
| 530 | SkRect r; |
| 531 | r.set(0.0f, 0.0f, width, height); |
| 532 | if (useCenter) { |
| 533 | path.moveTo(r.centerX(), r.centerY()); |
| 534 | } |
| 535 | path.arcTo(r, startAngle, sweepAngle, !useCenter); |
| 536 | if (useCenter) { |
| 537 | path.close(); |
| 538 | } |
| 539 | |
| 540 | texture = addTexture(entry, &path, paint); |
| 541 | } |
| 542 | |
| 543 | return texture; |
| 544 | } |
| 545 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 546 | }; // namespace uirenderer |
| 547 | }; // namespace android |