blob: e67c5bddd35499b2cf8dbc865937a411fe99c344 [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -07001/*
Romain Guyc46d07a2013-03-15 19:06:39 -07002 * Copyright (C) 2013 The Android Open Source Project
Romain Guy7fbcc042010-08-04 15:40:07 -07003 *
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 Guyc46d07a2013-03-15 19:06:39 -070017#include <SkBitmap.h>
18#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070019#include <SkColor.h>
Mike Reed260ab722016-10-07 15:59:20 -040020#include <SkColorFilter.h>
21#include <SkMaskFilter.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070022#include <SkPaint.h>
23#include <SkPath.h>
sergeyv7224e2b2016-04-07 18:06:53 -070024#include <SkPathEffect.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070025#include <SkRect.h>
Romain Guya2341a92010-09-08 18:04:33 -070026
Romain Guyc46d07a2013-03-15 19:06:39 -070027#include <utils/JenkinsHash.h>
28#include <utils/Trace.h>
Romain Guyca89e2a2013-03-08 17:44:20 -080029
30#include "Caches.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070031#include "PathCache.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070032
33#include "thread/Signal.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070034#include "thread/TaskProcessor.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070035
John Reck6b507802015-11-03 10:09:59 -080036#include <cutils/properties.h>
37
Romain Guy7fbcc042010-08-04 15:40:07 -070038namespace android {
39namespace uirenderer {
40
John Reck8dc02f92017-07-17 09:55:02 -070041static constexpr size_t PATH_CACHE_COUNT_LIMIT = 256;
42
sergeyv7224e2b2016-04-07 18:06:53 -070043template <class T>
44static bool compareWidthHeight(const T& lhs, const T& rhs) {
45 return (lhs.mWidth == rhs.mWidth) && (lhs.mHeight == rhs.mHeight);
46}
47
48static bool compareRoundRects(const PathDescription::Shape::RoundRect& lhs,
John Reck1bcacfd2017-11-03 10:12:19 -070049 const PathDescription::Shape::RoundRect& rhs) {
sergeyv7224e2b2016-04-07 18:06:53 -070050 return compareWidthHeight(lhs, rhs) && lhs.mRx == rhs.mRx && lhs.mRy == rhs.mRy;
51}
52
John Reck1bcacfd2017-11-03 10:12:19 -070053static bool compareArcs(const PathDescription::Shape::Arc& lhs,
54 const PathDescription::Shape::Arc& rhs) {
sergeyv7224e2b2016-04-07 18:06:53 -070055 return compareWidthHeight(lhs, rhs) && lhs.mStartAngle == rhs.mStartAngle &&
John Reck1bcacfd2017-11-03 10:12:19 -070056 lhs.mSweepAngle == rhs.mSweepAngle && lhs.mUseCenter == rhs.mUseCenter;
sergeyv7224e2b2016-04-07 18:06:53 -070057}
58
Romain Guyca89e2a2013-03-08 17:44:20 -080059///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070060// Cache entries
61///////////////////////////////////////////////////////////////////////////////
62
Chris Craike2bb3802015-03-13 15:07:52 -070063PathDescription::PathDescription()
sergeyv7224e2b2016-04-07 18:06:53 -070064 : type(ShapeType::None)
Chris Craike2bb3802015-03-13 15:07:52 -070065 , join(SkPaint::kDefault_Join)
66 , cap(SkPaint::kDefault_Cap)
67 , style(SkPaint::kFill_Style)
68 , miter(4.0f)
69 , strokeWidth(1.0f)
70 , pathEffect(nullptr) {
sergeyv7224e2b2016-04-07 18:06:53 -070071 // Shape bits should be set to zeroes, because they are used for hash calculation.
Romain Guyc46d07a2013-03-15 19:06:39 -070072 memset(&shape, 0, sizeof(Shape));
73}
74
Chris Craike2bb3802015-03-13 15:07:52 -070075PathDescription::PathDescription(ShapeType type, const SkPaint* paint)
76 : type(type)
77 , join(paint->getStrokeJoin())
78 , cap(paint->getStrokeCap())
79 , style(paint->getStyle())
80 , miter(paint->getStrokeMiter())
81 , strokeWidth(paint->getStrokeWidth())
82 , pathEffect(paint->getPathEffect()) {
sergeyv7224e2b2016-04-07 18:06:53 -070083 // Shape bits should be set to zeroes, because they are used for hash calculation.
Romain Guyc46d07a2013-03-15 19:06:39 -070084 memset(&shape, 0, sizeof(Shape));
85}
86
87hash_t PathDescription::hash() const {
sergeyv7224e2b2016-04-07 18:06:53 -070088 uint32_t hash = JenkinsHashMix(0, static_cast<int>(type));
Romain Guyc46d07a2013-03-15 19:06:39 -070089 hash = JenkinsHashMix(hash, join);
90 hash = JenkinsHashMix(hash, cap);
91 hash = JenkinsHashMix(hash, style);
92 hash = JenkinsHashMix(hash, android::hash_type(miter));
93 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
94 hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
John Reck1bcacfd2017-11-03 10:12:19 -070095 hash = JenkinsHashMixBytes(hash, (uint8_t*)&shape, sizeof(Shape));
Romain Guyc46d07a2013-03-15 19:06:39 -070096 return JenkinsHashWhiten(hash);
97}
98
sergeyv7224e2b2016-04-07 18:06:53 -070099bool PathDescription::operator==(const PathDescription& rhs) const {
100 if (type != rhs.type) return false;
101 if (join != rhs.join) return false;
102 if (cap != rhs.cap) return false;
103 if (style != rhs.style) return false;
104 if (miter != rhs.miter) return false;
105 if (strokeWidth != rhs.strokeWidth) return false;
106 if (pathEffect != rhs.pathEffect) return false;
107 switch (type) {
108 case ShapeType::None:
109 return 0;
110 case ShapeType::Rect:
111 return compareWidthHeight(shape.rect, rhs.shape.rect);
112 case ShapeType::RoundRect:
113 return compareRoundRects(shape.roundRect, rhs.shape.roundRect);
114 case ShapeType::Circle:
115 return shape.circle.mRadius == rhs.shape.circle.mRadius;
116 case ShapeType::Oval:
117 return compareWidthHeight(shape.oval, rhs.shape.oval);
118 case ShapeType::Arc:
119 return compareArcs(shape.arc, rhs.shape.arc);
120 case ShapeType::Path:
121 return shape.path.mGenerationID == rhs.shape.path.mGenerationID;
122 }
123}
124
Romain Guyc46d07a2013-03-15 19:06:39 -0700125///////////////////////////////////////////////////////////////////////////////
126// Utilities
127///////////////////////////////////////////////////////////////////////////////
128
sergeyvd93b9bd2016-08-04 16:18:22 -0700129static void computePathBounds(const SkPath* path, const SkPaint* paint, PathTexture* texture,
John Reck1bcacfd2017-11-03 10:12:19 -0700130 uint32_t& width, uint32_t& height) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700131 const SkRect& bounds = path->getBounds();
Chris Craike6a15ee2015-07-07 18:42:17 -0700132 const float pathWidth = std::max(bounds.width(), 1.0f);
133 const float pathHeight = std::max(bounds.height(), 1.0f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700134
sergeyv89561e62016-08-04 16:21:07 -0700135 texture->left = floorf(bounds.fLeft);
136 texture->top = floorf(bounds.fTop);
Romain Guyc46d07a2013-03-15 19:06:39 -0700137
John Reck1bcacfd2017-11-03 10:12:19 -0700138 texture->offset = (int)floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700139
sergeyvd93b9bd2016-08-04 16:18:22 -0700140 width = uint32_t(pathWidth + texture->offset * 2.0 + 0.5);
141 height = uint32_t(pathHeight + texture->offset * 2.0 + 0.5);
Romain Guyc46d07a2013-03-15 19:06:39 -0700142}
143
Romain Guyc46d07a2013-03-15 19:06:39 -0700144static void initPaint(SkPaint& paint) {
145 // Make sure the paint is opaque, color, alpha, filter, etc.
146 // will be applied later when compositing the alpha8 texture
Chris Craik98d608d2014-07-17 12:25:11 -0700147 paint.setColor(SK_ColorBLACK);
Romain Guyc46d07a2013-03-15 19:06:39 -0700148 paint.setAlpha(255);
Chris Craikd41c4d82015-01-05 15:51:13 -0800149 paint.setColorFilter(nullptr);
150 paint.setMaskFilter(nullptr);
151 paint.setShader(nullptr);
Mike Reed260ab722016-10-07 15:59:20 -0400152 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guyc46d07a2013-03-15 19:06:39 -0700153}
154
sergeyv98fa4f92016-10-24 15:35:21 -0700155static sk_sp<Bitmap> drawPath(const SkPath* path, const SkPaint* paint, PathTexture* texture,
John Reck1bcacfd2017-11-03 10:12:19 -0700156 uint32_t maxTextureSize) {
sergeyvd93b9bd2016-08-04 16:18:22 -0700157 uint32_t width, height;
158 computePathBounds(path, paint, texture, width, height);
159 if (width > maxTextureSize || height > maxTextureSize) {
John Reck1bcacfd2017-11-03 10:12:19 -0700160 ALOGW("Shape too large to be rendered into a texture (%dx%d, max=%dx%d)", width, height,
161 maxTextureSize, maxTextureSize);
sergeyvd93b9bd2016-08-04 16:18:22 -0700162 return nullptr;
163 }
164
sergeyv98fa4f92016-10-24 15:35:21 -0700165 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height));
Romain Guyc46d07a2013-03-15 19:06:39 -0700166 SkPaint pathPaint(*paint);
167 initPaint(pathPaint);
168
sergeyv98fa4f92016-10-24 15:35:21 -0700169 SkBitmap skBitmap;
170 bitmap->getSkBitmap(&skBitmap);
171 skBitmap.eraseColor(0);
172 SkCanvas canvas(skBitmap);
sergeyvd93b9bd2016-08-04 16:18:22 -0700173 canvas.translate(-texture->left + texture->offset, -texture->top + texture->offset);
Romain Guyc46d07a2013-03-15 19:06:39 -0700174 canvas.drawPath(*path, pathPaint);
sergeyvd93b9bd2016-08-04 16:18:22 -0700175 return bitmap;
Romain Guyc46d07a2013-03-15 19:06:39 -0700176}
177
Romain Guyc46d07a2013-03-15 19:06:39 -0700178///////////////////////////////////////////////////////////////////////////////
179// Cache constructor/destructor
180///////////////////////////////////////////////////////////////////////////////
181
Chris Craik48a8f432016-02-05 15:59:29 -0800182PathCache::PathCache()
183 : mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity)
184 , mSize(0)
John Reck8dc02f92017-07-17 09:55:02 -0700185 , mMaxSize(DeviceInfo::multiplyByResolution(4)) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700186 mCache.setOnEntryRemovedListener(this);
John Reck8dc02f92017-07-17 09:55:02 -0700187 mMaxTextureSize = DeviceInfo::get()->maxTextureSize();
Chris Craik2507c342015-05-04 14:36:49 -0700188 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700189}
190
Chris Craik05f3d6e2014-06-02 16:27:04 -0700191PathCache::~PathCache() {
192 mCache.clear();
193}
194
Romain Guyc46d07a2013-03-15 19:06:39 -0700195///////////////////////////////////////////////////////////////////////////////
196// Size management
197///////////////////////////////////////////////////////////////////////////////
198
199uint32_t PathCache::getSize() {
200 return mSize;
201}
202
203uint32_t PathCache::getMaxSize() {
204 return mMaxSize;
205}
206
Romain Guyc46d07a2013-03-15 19:06:39 -0700207///////////////////////////////////////////////////////////////////////////////
208// Callbacks
209///////////////////////////////////////////////////////////////////////////////
210
Andreas Gampe64bb4132014-11-22 00:35:09 +0000211void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700212 removeTexture(texture);
213}
214
215///////////////////////////////////////////////////////////////////////////////
216// Caching
217///////////////////////////////////////////////////////////////////////////////
218
219void PathCache::removeTexture(PathTexture* texture) {
220 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -0800221 const uint32_t size = texture->width() * texture->height();
Romain Guy5d923202013-08-21 18:40:24 -0700222
223 // If there is a pending task we must wait for it to return
224 // before attempting our cleanup
sergeyv98fa4f92016-10-24 15:35:21 -0700225 const sp<PathTask>& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800226 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800227 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700228 texture->clearTask();
229 } else {
230 // If there is a pending task, the path was not added
231 // to the cache and the size wasn't increased
232 if (size > mSize) {
233 ALOGE("Removing path texture of size %d will leave "
John Reck1bcacfd2017-11-03 10:12:19 -0700234 "the cache in an inconsistent state",
235 size);
Romain Guy5d923202013-08-21 18:40:24 -0700236 }
237 mSize -= size;
238 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700239
John Reck1bcacfd2017-11-03 10:12:19 -0700240 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d", texture->id, size, mSize);
Romain Guyc46d07a2013-03-15 19:06:39 -0700241 if (mDebugEnabled) {
242 ALOGD("Shape deleted, size = %d", size);
243 }
244
John Reck38e0c322015-11-10 12:19:17 -0800245 texture->deleteTexture();
Romain Guyc46d07a2013-03-15 19:06:39 -0700246 delete texture;
247 }
248}
249
250void PathCache::purgeCache(uint32_t width, uint32_t height) {
251 const uint32_t size = width * height;
252 // Don't even try to cache a bitmap that's bigger than the cache
253 if (size < mMaxSize) {
254 while (mSize + size > mMaxSize) {
255 mCache.removeOldest();
256 }
257 }
258}
259
260void PathCache::trim() {
John Reck8dc02f92017-07-17 09:55:02 -0700261 while (mSize > mMaxSize || mCache.size() > PATH_CACHE_COUNT_LIMIT) {
John Reck1bcacfd2017-11-03 10:12:19 -0700262 LOG_ALWAYS_FATAL_IF(!mCache.size(),
263 "Inconsistent mSize! Ran out of items to remove!"
264 " mSize = %u, mMaxSize = %u",
265 mSize, mMaxSize);
Romain Guyc46d07a2013-03-15 19:06:39 -0700266 mCache.removeOldest();
267 }
268}
269
John Reck1bcacfd2017-11-03 10:12:19 -0700270PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath* path,
271 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800272 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700273
sergeyvd93b9bd2016-08-04 16:18:22 -0700274 PathTexture* texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
sergeyv98fa4f92016-10-24 15:35:21 -0700275 sk_sp<Bitmap> bitmap(drawPath(path, paint, texture, mMaxTextureSize));
276 if (!bitmap) {
sergeyvd93b9bd2016-08-04 16:18:22 -0700277 delete texture;
278 return nullptr;
279 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700280
sergeyvd93b9bd2016-08-04 16:18:22 -0700281 purgeCache(bitmap->width(), bitmap->height());
sergeyv98fa4f92016-10-24 15:35:21 -0700282 generateTexture(entry, *bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700283 return texture;
284}
285
John Reck1bcacfd2017-11-03 10:12:19 -0700286void PathCache::generateTexture(const PathDescription& entry, Bitmap& bitmap, PathTexture* texture,
287 bool addToCache) {
sergeyv98fa4f92016-10-24 15:35:21 -0700288 generateTexture(bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700289
Chris Craik42455fc2015-05-11 18:23:09 -0700290 // Note here that we upload to a texture even if it's bigger than mMaxSize.
291 // Such an entry in mCache will only be temporary, since it will be evicted
292 // immediately on trim, or on any other Path entering the cache.
John Reck38e0c322015-11-10 12:19:17 -0800293 uint32_t size = texture->width() * texture->height();
Chris Craik42455fc2015-05-11 18:23:09 -0700294 mSize += size;
John Reck1bcacfd2017-11-03 10:12:19 -0700295 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d", texture->id, size, mSize);
Chris Craik42455fc2015-05-11 18:23:09 -0700296 if (mDebugEnabled) {
297 ALOGD("Shape created, size = %d", size);
298 }
299 if (addToCache) {
300 mCache.put(entry, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700301 }
302}
303
304void PathCache::clear() {
305 mCache.clear();
306}
307
sergeyv98fa4f92016-10-24 15:35:21 -0700308void PathCache::generateTexture(Bitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700309 ATRACE_NAME("Upload Path Texture");
John Reck38e0c322015-11-10 12:19:17 -0800310 texture->upload(bitmap);
Romain Guyc46d07a2013-03-15 19:06:39 -0700311 texture->setFilter(GL_LINEAR);
Romain Guyc46d07a2013-03-15 19:06:39 -0700312}
313
314///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800315// Path precaching
316///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700317
John Reck1bcacfd2017-11-03 10:12:19 -0700318PathCache::PathProcessor::PathProcessor(Caches& caches)
319 : TaskProcessor<sk_sp<Bitmap> >(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {}
Romain Guy33f6beb2012-02-16 19:24:51 -0800320
sergeyv98fa4f92016-10-24 15:35:21 -0700321void PathCache::PathProcessor::onProcess(const sp<Task<sk_sp<Bitmap> > >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700322 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700323 ATRACE_NAME("pathPrecache");
324
sergeyvd93b9bd2016-08-04 16:18:22 -0700325 t->setResult(drawPath(&t->path, &t->paint, t->texture, mMaxTextureSize));
Romain Guy33f6beb2012-02-16 19:24:51 -0800326}
327
Romain Guy7fbcc042010-08-04 15:40:07 -0700328///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700329// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700330///////////////////////////////////////////////////////////////////////////////
331
Derek Sollenbergeree248592015-02-12 14:10:21 -0500332void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800333 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700334 mGarbage.push_back(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800335}
336
337void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700338 Vector<PathDescription> pathsToRemove;
339
John Reck1bcacfd2017-11-03 10:12:19 -0700340 { // scope for the mutex
Romain Guye3b0a012013-06-26 15:45:41 -0700341 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700342 for (const uint32_t generationID : mGarbage) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500343 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
344 while (iter.next()) {
345 const PathDescription& key = iter.key();
sergeyv7224e2b2016-04-07 18:06:53 -0700346 if (key.type == ShapeType::Path && key.shape.path.mGenerationID == generationID) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500347 pathsToRemove.push(key);
348 }
349 }
Romain Guye3b0a012013-06-26 15:45:41 -0700350 }
351 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800352 }
Romain Guye3b0a012013-06-26 15:45:41 -0700353
354 for (size_t i = 0; i < pathsToRemove.size(); i++) {
355 mCache.remove(pathsToRemove.itemAt(i));
356 }
Romain Guya2341a92010-09-08 18:04:33 -0700357}
358
Chris Craikd218a922014-01-02 17:13:34 -0800359PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700360 PathDescription entry(ShapeType::Path, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500361 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700362
Romain Guy7fbcc042010-08-04 15:40:07 -0700363 PathTexture* texture = mCache.get(entry);
364
365 if (!texture) {
366 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800367 } else {
368 // A bitmap is attached to the texture, this means we need to
369 // upload it as a GL texture
sergeyv98fa4f92016-10-24 15:35:21 -0700370 const sp<PathTask>& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800371 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800372 // But we must first wait for the worker thread to be done
373 // producing the bitmap, so let's wait
sergeyv98fa4f92016-10-24 15:35:21 -0700374 sk_sp<Bitmap> bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800375 if (bitmap) {
sergeyv98fa4f92016-10-24 15:35:21 -0700376 generateTexture(entry, *bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700377 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800378 } else {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700379 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800380 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800381 mCache.remove(entry);
382 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800383 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700384 }
385
Romain Guy7fbcc042010-08-04 15:40:07 -0700386 return texture;
387}
388
sergeyv7224e2b2016-04-07 18:06:53 -0700389void PathCache::remove(const SkPath* path, const SkPaint* paint) {
390 PathDescription entry(ShapeType::Path, paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530391 entry.shape.path.mGenerationID = path->getGenerationID();
392 mCache.remove(entry);
393}
394
Chris Craikd218a922014-01-02 17:13:34 -0800395void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700396 if (!Caches::getInstance().tasks.canRunTasks()) {
397 return;
398 }
399
sergeyv7224e2b2016-04-07 18:06:53 -0700400 PathDescription entry(ShapeType::Path, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500401 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700402
Romain Guyca89e2a2013-03-08 17:44:20 -0800403 PathTexture* texture = mCache.get(entry);
404
405 bool generate = false;
406 if (!texture) {
407 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800408 }
409
410 if (generate) {
411 // It is important to specify the generation ID so we do not
412 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700413 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700414 sp<PathTask> task = new PathTask(path, paint, texture);
415 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800416
417 // During the precaching phase we insert path texture objects into
418 // the cache that do not point to any GL texture. They are instead
419 // treated as a task for the precaching worker thread. This is why
420 // we do not check the cache limit when inserting these objects.
421 // The conversion into GL texture will happen in get(), when a client
422 // asks for a path texture. This is also when the cache limit will
423 // be enforced.
424 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700425
Chris Craikd41c4d82015-01-05 15:51:13 -0800426 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700427 mProcessor = new PathProcessor(Caches::getInstance());
428 }
Chris Craikdee66b62015-04-20 14:54:49 -0700429 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800430 }
431}
432
Romain Guyc46d07a2013-03-15 19:06:39 -0700433///////////////////////////////////////////////////////////////////////////////
434// Rounded rects
435///////////////////////////////////////////////////////////////////////////////
436
John Reck1bcacfd2017-11-03 10:12:19 -0700437PathTexture* PathCache::getRoundRect(float width, float height, float rx, float ry,
438 const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700439 PathDescription entry(ShapeType::RoundRect, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700440 entry.shape.roundRect.mWidth = width;
441 entry.shape.roundRect.mHeight = height;
442 entry.shape.roundRect.mRx = rx;
443 entry.shape.roundRect.mRy = ry;
444
445 PathTexture* texture = get(entry);
446
447 if (!texture) {
448 SkPath path;
449 SkRect r;
450 r.set(0.0f, 0.0f, width, height);
451 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
452
453 texture = addTexture(entry, &path, paint);
454 }
455
456 return texture;
457}
458
459///////////////////////////////////////////////////////////////////////////////
460// Circles
461///////////////////////////////////////////////////////////////////////////////
462
Chris Craikd218a922014-01-02 17:13:34 -0800463PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700464 PathDescription entry(ShapeType::Circle, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700465 entry.shape.circle.mRadius = radius;
466
467 PathTexture* texture = get(entry);
468
469 if (!texture) {
470 SkPath path;
471 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
472
473 texture = addTexture(entry, &path, paint);
474 }
475
476 return texture;
477}
478
479///////////////////////////////////////////////////////////////////////////////
480// Ovals
481///////////////////////////////////////////////////////////////////////////////
482
Chris Craikd218a922014-01-02 17:13:34 -0800483PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700484 PathDescription entry(ShapeType::Oval, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700485 entry.shape.oval.mWidth = width;
486 entry.shape.oval.mHeight = height;
487
488 PathTexture* texture = get(entry);
489
490 if (!texture) {
491 SkPath path;
492 SkRect r;
493 r.set(0.0f, 0.0f, width, height);
494 path.addOval(r, SkPath::kCW_Direction);
495
496 texture = addTexture(entry, &path, paint);
497 }
498
499 return texture;
500}
501
502///////////////////////////////////////////////////////////////////////////////
503// Rects
504///////////////////////////////////////////////////////////////////////////////
505
Chris Craikd218a922014-01-02 17:13:34 -0800506PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700507 PathDescription entry(ShapeType::Rect, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700508 entry.shape.rect.mWidth = width;
509 entry.shape.rect.mHeight = height;
510
511 PathTexture* texture = get(entry);
512
513 if (!texture) {
514 SkPath path;
515 SkRect r;
516 r.set(0.0f, 0.0f, width, height);
517 path.addRect(r, SkPath::kCW_Direction);
518
519 texture = addTexture(entry, &path, paint);
520 }
521
522 return texture;
523}
524
525///////////////////////////////////////////////////////////////////////////////
526// Arcs
527///////////////////////////////////////////////////////////////////////////////
528
John Reck1bcacfd2017-11-03 10:12:19 -0700529PathTexture* PathCache::getArc(float width, float height, float startAngle, float sweepAngle,
530 bool useCenter, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700531 PathDescription entry(ShapeType::Arc, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700532 entry.shape.arc.mWidth = width;
533 entry.shape.arc.mHeight = height;
534 entry.shape.arc.mStartAngle = startAngle;
535 entry.shape.arc.mSweepAngle = sweepAngle;
536 entry.shape.arc.mUseCenter = useCenter;
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 if (useCenter) {
545 path.moveTo(r.centerX(), r.centerY());
546 }
547 path.arcTo(r, startAngle, sweepAngle, !useCenter);
548 if (useCenter) {
549 path.close();
550 }
551
552 texture = addTexture(entry, &path, paint);
553 }
554
555 return texture;
556}
557
John Reck1bcacfd2017-11-03 10:12:19 -0700558}; // namespace uirenderer
559}; // namespace android