blob: 8d4ae1b6622afa1769b0600dda93a46743518d98 [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,
49 const PathDescription::Shape::RoundRect& rhs) {
50 return compareWidthHeight(lhs, rhs) && lhs.mRx == rhs.mRx && lhs.mRy == rhs.mRy;
51}
52
53static 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 Guyca89e2a2013-03-08 17:44:20 -080058///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070059// Cache entries
60///////////////////////////////////////////////////////////////////////////////
61
Chris Craike2bb3802015-03-13 15:07:52 -070062PathDescription::PathDescription()
sergeyv7224e2b2016-04-07 18:06:53 -070063 : type(ShapeType::None)
Chris Craike2bb3802015-03-13 15:07:52 -070064 , 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) {
sergeyv7224e2b2016-04-07 18:06:53 -070070 // Shape bits should be set to zeroes, because they are used for hash calculation.
Romain Guyc46d07a2013-03-15 19:06:39 -070071 memset(&shape, 0, sizeof(Shape));
72}
73
Chris Craike2bb3802015-03-13 15:07:52 -070074PathDescription::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()) {
sergeyv7224e2b2016-04-07 18:06:53 -070082 // Shape bits should be set to zeroes, because they are used for hash calculation.
Romain Guyc46d07a2013-03-15 19:06:39 -070083 memset(&shape, 0, sizeof(Shape));
84}
85
86hash_t PathDescription::hash() const {
sergeyv7224e2b2016-04-07 18:06:53 -070087 uint32_t hash = JenkinsHashMix(0, static_cast<int>(type));
Romain Guyc46d07a2013-03-15 19:06:39 -070088 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
sergeyv7224e2b2016-04-07 18:06:53 -070098bool 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 Guyc46d07a2013-03-15 19:06:39 -0700124///////////////////////////////////////////////////////////////////////////////
125// Utilities
126///////////////////////////////////////////////////////////////////////////////
127
sergeyvd93b9bd2016-08-04 16:18:22 -0700128static void computePathBounds(const SkPath* path, const SkPaint* paint, PathTexture* texture,
129 uint32_t& width, uint32_t& height) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700130 const SkRect& bounds = path->getBounds();
Chris Craike6a15ee2015-07-07 18:42:17 -0700131 const float pathWidth = std::max(bounds.width(), 1.0f);
132 const float pathHeight = std::max(bounds.height(), 1.0f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700133
sergeyv89561e62016-08-04 16:21:07 -0700134 texture->left = floorf(bounds.fLeft);
135 texture->top = floorf(bounds.fTop);
Romain Guyc46d07a2013-03-15 19:06:39 -0700136
sergeyvd93b9bd2016-08-04 16:18:22 -0700137 texture->offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700138
sergeyvd93b9bd2016-08-04 16:18:22 -0700139 width = uint32_t(pathWidth + texture->offset * 2.0 + 0.5);
140 height = uint32_t(pathHeight + texture->offset * 2.0 + 0.5);
Romain Guyc46d07a2013-03-15 19:06:39 -0700141}
142
Romain Guyc46d07a2013-03-15 19:06:39 -0700143static 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 Craik98d608d2014-07-17 12:25:11 -0700146 paint.setColor(SK_ColorBLACK);
Romain Guyc46d07a2013-03-15 19:06:39 -0700147 paint.setAlpha(255);
Chris Craikd41c4d82015-01-05 15:51:13 -0800148 paint.setColorFilter(nullptr);
149 paint.setMaskFilter(nullptr);
150 paint.setShader(nullptr);
Mike Reed260ab722016-10-07 15:59:20 -0400151 paint.setBlendMode(SkBlendMode::kSrc);
Romain Guyc46d07a2013-03-15 19:06:39 -0700152}
153
sergeyv98fa4f92016-10-24 15:35:21 -0700154static sk_sp<Bitmap> drawPath(const SkPath* path, const SkPaint* paint, PathTexture* texture,
sergeyvd93b9bd2016-08-04 16:18:22 -0700155 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
sergeyv98fa4f92016-10-24 15:35:21 -0700164 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height));
Romain Guyc46d07a2013-03-15 19:06:39 -0700165 SkPaint pathPaint(*paint);
166 initPaint(pathPaint);
167
sergeyv98fa4f92016-10-24 15:35:21 -0700168 SkBitmap skBitmap;
169 bitmap->getSkBitmap(&skBitmap);
170 skBitmap.eraseColor(0);
171 SkCanvas canvas(skBitmap);
sergeyvd93b9bd2016-08-04 16:18:22 -0700172 canvas.translate(-texture->left + texture->offset, -texture->top + texture->offset);
Romain Guyc46d07a2013-03-15 19:06:39 -0700173 canvas.drawPath(*path, pathPaint);
sergeyvd93b9bd2016-08-04 16:18:22 -0700174 return bitmap;
Romain Guyc46d07a2013-03-15 19:06:39 -0700175}
176
Romain Guyc46d07a2013-03-15 19:06:39 -0700177///////////////////////////////////////////////////////////////////////////////
178// Cache constructor/destructor
179///////////////////////////////////////////////////////////////////////////////
180
Chris Craik48a8f432016-02-05 15:59:29 -0800181PathCache::PathCache()
182 : mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity)
183 , mSize(0)
John Reck8dc02f92017-07-17 09:55:02 -0700184 , mMaxSize(DeviceInfo::multiplyByResolution(4)) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700185 mCache.setOnEntryRemovedListener(this);
John Reck8dc02f92017-07-17 09:55:02 -0700186 mMaxTextureSize = DeviceInfo::get()->maxTextureSize();
Chris Craik2507c342015-05-04 14:36:49 -0700187 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700188}
189
Chris Craik05f3d6e2014-06-02 16:27:04 -0700190PathCache::~PathCache() {
191 mCache.clear();
192}
193
Romain Guyc46d07a2013-03-15 19:06:39 -0700194///////////////////////////////////////////////////////////////////////////////
195// Size management
196///////////////////////////////////////////////////////////////////////////////
197
198uint32_t PathCache::getSize() {
199 return mSize;
200}
201
202uint32_t PathCache::getMaxSize() {
203 return mMaxSize;
204}
205
Romain Guyc46d07a2013-03-15 19:06:39 -0700206///////////////////////////////////////////////////////////////////////////////
207// Callbacks
208///////////////////////////////////////////////////////////////////////////////
209
Andreas Gampe64bb4132014-11-22 00:35:09 +0000210void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700211 removeTexture(texture);
212}
213
214///////////////////////////////////////////////////////////////////////////////
215// Caching
216///////////////////////////////////////////////////////////////////////////////
217
218void PathCache::removeTexture(PathTexture* texture) {
219 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -0800220 const uint32_t size = texture->width() * texture->height();
Romain Guy5d923202013-08-21 18:40:24 -0700221
222 // If there is a pending task we must wait for it to return
223 // before attempting our cleanup
sergeyv98fa4f92016-10-24 15:35:21 -0700224 const sp<PathTask>& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800225 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800226 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700227 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 Guyc46d07a2013-03-15 19:06:39 -0700237
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 Reck38e0c322015-11-10 12:19:17 -0800244 texture->deleteTexture();
Romain Guyc46d07a2013-03-15 19:06:39 -0700245 delete texture;
246 }
247}
248
249void 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
259void PathCache::trim() {
John Reck8dc02f92017-07-17 09:55:02 -0700260 while (mSize > mMaxSize || mCache.size() > PATH_CACHE_COUNT_LIMIT) {
John Reckcecec702016-10-12 14:08:49 -0700261 LOG_ALWAYS_FATAL_IF(!mCache.size(), "Inconsistent mSize! Ran out of items to remove!"
262 " mSize = %u, mMaxSize = %u", mSize, mMaxSize);
Romain Guyc46d07a2013-03-15 19:06:39 -0700263 mCache.removeOldest();
264 }
265}
266
267PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
268 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800269 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700270
sergeyvd93b9bd2016-08-04 16:18:22 -0700271 PathTexture* texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
sergeyv98fa4f92016-10-24 15:35:21 -0700272 sk_sp<Bitmap> bitmap(drawPath(path, paint, texture, mMaxTextureSize));
273 if (!bitmap) {
sergeyvd93b9bd2016-08-04 16:18:22 -0700274 delete texture;
275 return nullptr;
276 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700277
sergeyvd93b9bd2016-08-04 16:18:22 -0700278 purgeCache(bitmap->width(), bitmap->height());
sergeyv98fa4f92016-10-24 15:35:21 -0700279 generateTexture(entry, *bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700280 return texture;
281}
282
sergeyv98fa4f92016-10-24 15:35:21 -0700283void PathCache::generateTexture(const PathDescription& entry, Bitmap& bitmap,
Romain Guy4500a8d2013-03-26 17:29:51 -0700284 PathTexture* texture, bool addToCache) {
sergeyv98fa4f92016-10-24 15:35:21 -0700285 generateTexture(bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700286
Chris Craik42455fc2015-05-11 18:23:09 -0700287 // 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 Reck38e0c322015-11-10 12:19:17 -0800290 uint32_t size = texture->width() * texture->height();
Chris Craik42455fc2015-05-11 18:23:09 -0700291 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 Guyc46d07a2013-03-15 19:06:39 -0700299 }
300}
301
302void PathCache::clear() {
303 mCache.clear();
304}
305
sergeyv98fa4f92016-10-24 15:35:21 -0700306void PathCache::generateTexture(Bitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700307 ATRACE_NAME("Upload Path Texture");
John Reck38e0c322015-11-10 12:19:17 -0800308 texture->upload(bitmap);
Romain Guyc46d07a2013-03-15 19:06:39 -0700309 texture->setFilter(GL_LINEAR);
Romain Guyc46d07a2013-03-15 19:06:39 -0700310}
311
312///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800313// Path precaching
314///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700315
Romain Guy5dc7fa72013-03-11 20:48:31 -0700316PathCache::PathProcessor::PathProcessor(Caches& caches):
sergeyv98fa4f92016-10-24 15:35:21 -0700317 TaskProcessor<sk_sp<Bitmap> >(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700318}
Romain Guy33f6beb2012-02-16 19:24:51 -0800319
sergeyv98fa4f92016-10-24 15:35:21 -0700320void PathCache::PathProcessor::onProcess(const sp<Task<sk_sp<Bitmap> > >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700321 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700322 ATRACE_NAME("pathPrecache");
323
sergeyvd93b9bd2016-08-04 16:18:22 -0700324 t->setResult(drawPath(&t->path, &t->paint, t->texture, mMaxTextureSize));
Romain Guy33f6beb2012-02-16 19:24:51 -0800325}
326
Romain Guy7fbcc042010-08-04 15:40:07 -0700327///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700328// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700329///////////////////////////////////////////////////////////////////////////////
330
Derek Sollenbergeree248592015-02-12 14:10:21 -0500331void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800332 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700333 mGarbage.push_back(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800334}
335
336void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700337 Vector<PathDescription> pathsToRemove;
338
339 { // scope for the mutex
340 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700341 for (const uint32_t generationID : mGarbage) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500342 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
343 while (iter.next()) {
344 const PathDescription& key = iter.key();
sergeyv7224e2b2016-04-07 18:06:53 -0700345 if (key.type == ShapeType::Path && key.shape.path.mGenerationID == generationID) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500346 pathsToRemove.push(key);
347 }
348 }
Romain Guye3b0a012013-06-26 15:45:41 -0700349 }
350 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800351 }
Romain Guye3b0a012013-06-26 15:45:41 -0700352
353 for (size_t i = 0; i < pathsToRemove.size(); i++) {
354 mCache.remove(pathsToRemove.itemAt(i));
355 }
Romain Guya2341a92010-09-08 18:04:33 -0700356}
357
Chris Craikd218a922014-01-02 17:13:34 -0800358PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700359 PathDescription entry(ShapeType::Path, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500360 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700361
Romain Guy7fbcc042010-08-04 15:40:07 -0700362 PathTexture* texture = mCache.get(entry);
363
364 if (!texture) {
365 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800366 } else {
367 // A bitmap is attached to the texture, this means we need to
368 // upload it as a GL texture
sergeyv98fa4f92016-10-24 15:35:21 -0700369 const sp<PathTask>& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800370 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800371 // But we must first wait for the worker thread to be done
372 // producing the bitmap, so let's wait
sergeyv98fa4f92016-10-24 15:35:21 -0700373 sk_sp<Bitmap> bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800374 if (bitmap) {
sergeyv98fa4f92016-10-24 15:35:21 -0700375 generateTexture(entry, *bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700376 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800377 } else {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700378 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800379 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800380 mCache.remove(entry);
381 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800382 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700383 }
384
Romain Guy7fbcc042010-08-04 15:40:07 -0700385 return texture;
386}
387
sergeyv7224e2b2016-04-07 18:06:53 -0700388void PathCache::remove(const SkPath* path, const SkPaint* paint) {
389 PathDescription entry(ShapeType::Path, paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530390 entry.shape.path.mGenerationID = path->getGenerationID();
391 mCache.remove(entry);
392}
393
Chris Craikd218a922014-01-02 17:13:34 -0800394void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700395 if (!Caches::getInstance().tasks.canRunTasks()) {
396 return;
397 }
398
sergeyv7224e2b2016-04-07 18:06:53 -0700399 PathDescription entry(ShapeType::Path, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500400 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700401
Romain Guyca89e2a2013-03-08 17:44:20 -0800402 PathTexture* texture = mCache.get(entry);
403
404 bool generate = false;
405 if (!texture) {
406 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800407 }
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 Craike2bb3802015-03-13 15:07:52 -0700412 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700413 sp<PathTask> task = new PathTask(path, paint, texture);
414 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800415
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 Guy5dc7fa72013-03-11 20:48:31 -0700424
Chris Craikd41c4d82015-01-05 15:51:13 -0800425 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700426 mProcessor = new PathProcessor(Caches::getInstance());
427 }
Chris Craikdee66b62015-04-20 14:54:49 -0700428 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800429 }
430}
431
Romain Guyc46d07a2013-03-15 19:06:39 -0700432///////////////////////////////////////////////////////////////////////////////
433// Rounded rects
434///////////////////////////////////////////////////////////////////////////////
435
436PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800437 float rx, float ry, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700438 PathDescription entry(ShapeType::RoundRect, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700439 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 Craikd218a922014-01-02 17:13:34 -0800462PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700463 PathDescription entry(ShapeType::Circle, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700464 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 Craikd218a922014-01-02 17:13:34 -0800482PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700483 PathDescription entry(ShapeType::Oval, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700484 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 Craikd218a922014-01-02 17:13:34 -0800505PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700506 PathDescription entry(ShapeType::Rect, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700507 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
528PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800529 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700530 PathDescription entry(ShapeType::Arc, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700531 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 Guy7fbcc042010-08-04 15:40:07 -0700557}; // namespace uirenderer
558}; // namespace android