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