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