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