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