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