blob: a8ace8c10edd9d0a0e7c92c25ad9dd3239a9857a [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>
Romain Guyc46d07a2013-03-15 19:06:39 -070020#include <SkPaint.h>
21#include <SkPath.h>
sergeyv7224e2b2016-04-07 18:06:53 -070022#include <SkPathEffect.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070023#include <SkRect.h>
Romain Guya2341a92010-09-08 18:04:33 -070024
Romain Guyc46d07a2013-03-15 19:06:39 -070025#include <utils/JenkinsHash.h>
26#include <utils/Trace.h>
Romain Guyca89e2a2013-03-08 17:44:20 -080027
28#include "Caches.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070029#include "PathCache.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070030
31#include "thread/Signal.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070032#include "thread/TaskProcessor.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070033
John Reck6b507802015-11-03 10:09:59 -080034#include <cutils/properties.h>
35
Romain Guy7fbcc042010-08-04 15:40:07 -070036namespace android {
37namespace uirenderer {
38
sergeyv7224e2b2016-04-07 18:06:53 -070039template <class T>
40static bool compareWidthHeight(const T& lhs, const T& rhs) {
41 return (lhs.mWidth == rhs.mWidth) && (lhs.mHeight == rhs.mHeight);
42}
43
44static bool compareRoundRects(const PathDescription::Shape::RoundRect& lhs,
45 const PathDescription::Shape::RoundRect& rhs) {
46 return compareWidthHeight(lhs, rhs) && lhs.mRx == rhs.mRx && lhs.mRy == rhs.mRy;
47}
48
49static bool compareArcs(const PathDescription::Shape::Arc& lhs, const PathDescription::Shape::Arc& rhs) {
50 return compareWidthHeight(lhs, rhs) && lhs.mStartAngle == rhs.mStartAngle &&
51 lhs.mSweepAngle == rhs.mSweepAngle && lhs.mUseCenter == rhs.mUseCenter;
52}
53
Romain Guyca89e2a2013-03-08 17:44:20 -080054///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070055// Cache entries
56///////////////////////////////////////////////////////////////////////////////
57
Chris Craike2bb3802015-03-13 15:07:52 -070058PathDescription::PathDescription()
sergeyv7224e2b2016-04-07 18:06:53 -070059 : type(ShapeType::None)
Chris Craike2bb3802015-03-13 15:07:52 -070060 , join(SkPaint::kDefault_Join)
61 , cap(SkPaint::kDefault_Cap)
62 , style(SkPaint::kFill_Style)
63 , miter(4.0f)
64 , strokeWidth(1.0f)
65 , pathEffect(nullptr) {
sergeyv7224e2b2016-04-07 18:06:53 -070066 // Shape bits should be set to zeroes, because they are used for hash calculation.
Romain Guyc46d07a2013-03-15 19:06:39 -070067 memset(&shape, 0, sizeof(Shape));
68}
69
Chris Craike2bb3802015-03-13 15:07:52 -070070PathDescription::PathDescription(ShapeType type, const SkPaint* paint)
71 : type(type)
72 , join(paint->getStrokeJoin())
73 , cap(paint->getStrokeCap())
74 , style(paint->getStyle())
75 , miter(paint->getStrokeMiter())
76 , strokeWidth(paint->getStrokeWidth())
77 , pathEffect(paint->getPathEffect()) {
sergeyv7224e2b2016-04-07 18:06:53 -070078 // Shape bits should be set to zeroes, because they are used for hash calculation.
Romain Guyc46d07a2013-03-15 19:06:39 -070079 memset(&shape, 0, sizeof(Shape));
80}
81
82hash_t PathDescription::hash() const {
sergeyv7224e2b2016-04-07 18:06:53 -070083 uint32_t hash = JenkinsHashMix(0, static_cast<int>(type));
Romain Guyc46d07a2013-03-15 19:06:39 -070084 hash = JenkinsHashMix(hash, join);
85 hash = JenkinsHashMix(hash, cap);
86 hash = JenkinsHashMix(hash, style);
87 hash = JenkinsHashMix(hash, android::hash_type(miter));
88 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
89 hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
90 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
91 return JenkinsHashWhiten(hash);
92}
93
sergeyv7224e2b2016-04-07 18:06:53 -070094bool PathDescription::operator==(const PathDescription& rhs) const {
95 if (type != rhs.type) return false;
96 if (join != rhs.join) return false;
97 if (cap != rhs.cap) return false;
98 if (style != rhs.style) return false;
99 if (miter != rhs.miter) return false;
100 if (strokeWidth != rhs.strokeWidth) return false;
101 if (pathEffect != rhs.pathEffect) return false;
102 switch (type) {
103 case ShapeType::None:
104 return 0;
105 case ShapeType::Rect:
106 return compareWidthHeight(shape.rect, rhs.shape.rect);
107 case ShapeType::RoundRect:
108 return compareRoundRects(shape.roundRect, rhs.shape.roundRect);
109 case ShapeType::Circle:
110 return shape.circle.mRadius == rhs.shape.circle.mRadius;
111 case ShapeType::Oval:
112 return compareWidthHeight(shape.oval, rhs.shape.oval);
113 case ShapeType::Arc:
114 return compareArcs(shape.arc, rhs.shape.arc);
115 case ShapeType::Path:
116 return shape.path.mGenerationID == rhs.shape.path.mGenerationID;
117 }
118}
119
Romain Guyc46d07a2013-03-15 19:06:39 -0700120///////////////////////////////////////////////////////////////////////////////
121// Utilities
122///////////////////////////////////////////////////////////////////////////////
123
Chris Craikd218a922014-01-02 17:13:34 -0800124bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700125 // NOTE: This should only be used after PathTessellator handles joins properly
Chris Craikd41c4d82015-01-05 15:51:13 -0800126 return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity;
Romain Guyc46d07a2013-03-15 19:06:39 -0700127}
128
129void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint,
130 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
131 const SkRect& bounds = path->getBounds();
132 PathCache::computeBounds(bounds, paint, left, top, offset, width, height);
133}
134
135void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint,
136 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
Chris Craike6a15ee2015-07-07 18:42:17 -0700137 const float pathWidth = std::max(bounds.width(), 1.0f);
138 const float pathHeight = std::max(bounds.height(), 1.0f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700139
140 left = bounds.fLeft;
141 top = bounds.fTop;
142
Chris Craike6a15ee2015-07-07 18:42:17 -0700143 offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700144
145 width = uint32_t(pathWidth + offset * 2.0 + 0.5);
146 height = uint32_t(pathHeight + offset * 2.0 + 0.5);
147}
148
149static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) {
Mike Reedb9330552014-06-16 17:31:48 -0400150 bitmap.allocPixels(SkImageInfo::MakeA8(width, height));
Romain Guyc46d07a2013-03-15 19:06:39 -0700151 bitmap.eraseColor(0);
152}
153
154static void initPaint(SkPaint& paint) {
155 // Make sure the paint is opaque, color, alpha, filter, etc.
156 // will be applied later when compositing the alpha8 texture
Chris Craik98d608d2014-07-17 12:25:11 -0700157 paint.setColor(SK_ColorBLACK);
Romain Guyc46d07a2013-03-15 19:06:39 -0700158 paint.setAlpha(255);
Chris Craikd41c4d82015-01-05 15:51:13 -0800159 paint.setColorFilter(nullptr);
160 paint.setMaskFilter(nullptr);
161 paint.setShader(nullptr);
Romain Guyc46d07a2013-03-15 19:06:39 -0700162 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
163 SkSafeUnref(paint.setXfermode(mode));
164}
165
166static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap,
167 float left, float top, float offset, uint32_t width, uint32_t height) {
168 initBitmap(bitmap, width, height);
169
170 SkPaint pathPaint(*paint);
171 initPaint(pathPaint);
172
173 SkCanvas canvas(bitmap);
174 canvas.translate(-left + offset, -top + offset);
175 canvas.drawPath(*path, pathPaint);
176}
177
Romain 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)
185 , mMaxSize(Properties::pathCacheSize) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700186 mCache.setOnEntryRemovedListener(this);
187
188 GLint maxTextureSize;
189 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
190 mMaxTextureSize = maxTextureSize;
191
Chris Craik2507c342015-05-04 14:36:49 -0700192 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700193}
194
Chris Craik05f3d6e2014-06-02 16:27:04 -0700195PathCache::~PathCache() {
196 mCache.clear();
197}
198
Romain Guyc46d07a2013-03-15 19:06:39 -0700199///////////////////////////////////////////////////////////////////////////////
200// Size management
201///////////////////////////////////////////////////////////////////////////////
202
203uint32_t PathCache::getSize() {
204 return mSize;
205}
206
207uint32_t PathCache::getMaxSize() {
208 return mMaxSize;
209}
210
Romain Guyc46d07a2013-03-15 19:06:39 -0700211///////////////////////////////////////////////////////////////////////////////
212// Callbacks
213///////////////////////////////////////////////////////////////////////////////
214
Andreas Gampe64bb4132014-11-22 00:35:09 +0000215void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700216 removeTexture(texture);
217}
218
219///////////////////////////////////////////////////////////////////////////////
220// Caching
221///////////////////////////////////////////////////////////////////////////////
222
223void PathCache::removeTexture(PathTexture* texture) {
224 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -0800225 const uint32_t size = texture->width() * texture->height();
Romain Guy5d923202013-08-21 18:40:24 -0700226
227 // If there is a pending task we must wait for it to return
228 // before attempting our cleanup
229 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800230 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800231 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700232 texture->clearTask();
233 } else {
234 // If there is a pending task, the path was not added
235 // to the cache and the size wasn't increased
236 if (size > mSize) {
237 ALOGE("Removing path texture of size %d will leave "
238 "the cache in an inconsistent state", size);
239 }
240 mSize -= size;
241 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700242
243 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
244 texture->id, size, mSize);
245 if (mDebugEnabled) {
246 ALOGD("Shape deleted, size = %d", size);
247 }
248
John Reck38e0c322015-11-10 12:19:17 -0800249 texture->deleteTexture();
Romain Guyc46d07a2013-03-15 19:06:39 -0700250 delete texture;
251 }
252}
253
254void PathCache::purgeCache(uint32_t width, uint32_t height) {
255 const uint32_t size = width * height;
256 // Don't even try to cache a bitmap that's bigger than the cache
257 if (size < mMaxSize) {
258 while (mSize + size > mMaxSize) {
259 mCache.removeOldest();
260 }
261 }
262}
263
264void PathCache::trim() {
265 while (mSize > mMaxSize) {
266 mCache.removeOldest();
267 }
268}
269
270PathTexture* 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
274 float left, top, offset;
275 uint32_t width, height;
276 computePathBounds(path, paint, left, top, offset, width, height);
277
Chris Craikd41c4d82015-01-05 15:51:13 -0800278 if (!checkTextureSize(width, height)) return nullptr;
Romain Guyc46d07a2013-03-15 19:06:39 -0700279
280 purgeCache(width, height);
281
282 SkBitmap bitmap;
283 drawPath(path, paint, bitmap, left, top, offset, width, height);
284
Chris Craike2bb3802015-03-13 15:07:52 -0700285 PathTexture* texture = new PathTexture(Caches::getInstance(),
John Reck38e0c322015-11-10 12:19:17 -0800286 left, top, offset, path->getGenerationID());
Romain Guy4500a8d2013-03-26 17:29:51 -0700287 generateTexture(entry, &bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700288
289 return texture;
290}
291
Romain Guy4500a8d2013-03-26 17:29:51 -0700292void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
293 PathTexture* texture, bool addToCache) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700294 generateTexture(*bitmap, texture);
295
Chris Craik42455fc2015-05-11 18:23:09 -0700296 // Note here that we upload to a texture even if it's bigger than mMaxSize.
297 // Such an entry in mCache will only be temporary, since it will be evicted
298 // immediately on trim, or on any other Path entering the cache.
John Reck38e0c322015-11-10 12:19:17 -0800299 uint32_t size = texture->width() * texture->height();
Chris Craik42455fc2015-05-11 18:23:09 -0700300 mSize += size;
301 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
302 texture->id, size, mSize);
303 if (mDebugEnabled) {
304 ALOGD("Shape created, size = %d", size);
305 }
306 if (addToCache) {
307 mCache.put(entry, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700308 }
309}
310
311void PathCache::clear() {
312 mCache.clear();
313}
314
315void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700316 ATRACE_NAME("Upload Path Texture");
John Reck38e0c322015-11-10 12:19:17 -0800317 texture->upload(bitmap);
Romain Guyc46d07a2013-03-15 19:06:39 -0700318 texture->setFilter(GL_LINEAR);
Romain Guyc46d07a2013-03-15 19:06:39 -0700319}
320
321///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800322// Path precaching
323///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700324
Romain Guy5dc7fa72013-03-11 20:48:31 -0700325PathCache::PathProcessor::PathProcessor(Caches& caches):
326 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700327}
Romain Guy33f6beb2012-02-16 19:24:51 -0800328
Romain Guy5dc7fa72013-03-11 20:48:31 -0700329void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700330 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700331 ATRACE_NAME("pathPrecache");
332
333 float left, top, offset;
334 uint32_t width, height;
Chris Craik906d47f2014-06-27 18:30:23 -0700335 PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700336
337 PathTexture* texture = t->texture;
338 texture->left = left;
339 texture->top = top;
340 texture->offset = offset;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700341
342 if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
343 SkBitmap* bitmap = new SkBitmap();
Chris Craik906d47f2014-06-27 18:30:23 -0700344 drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700345 t->setResult(bitmap);
346 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800347 t->setResult(nullptr);
Romain Guyca89e2a2013-03-08 17:44:20 -0800348 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800349}
350
Romain Guy7fbcc042010-08-04 15:40:07 -0700351///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700352// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700353///////////////////////////////////////////////////////////////////////////////
354
Derek Sollenbergeree248592015-02-12 14:10:21 -0500355void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800356 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700357 mGarbage.push_back(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800358}
359
360void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700361 Vector<PathDescription> pathsToRemove;
362
363 { // scope for the mutex
364 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700365 for (const uint32_t generationID : mGarbage) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500366 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
367 while (iter.next()) {
368 const PathDescription& key = iter.key();
sergeyv7224e2b2016-04-07 18:06:53 -0700369 if (key.type == ShapeType::Path && key.shape.path.mGenerationID == generationID) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500370 pathsToRemove.push(key);
371 }
372 }
Romain Guye3b0a012013-06-26 15:45:41 -0700373 }
374 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800375 }
Romain Guye3b0a012013-06-26 15:45:41 -0700376
377 for (size_t i = 0; i < pathsToRemove.size(); i++) {
378 mCache.remove(pathsToRemove.itemAt(i));
379 }
Romain Guya2341a92010-09-08 18:04:33 -0700380}
381
Chris Craikd218a922014-01-02 17:13:34 -0800382PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700383 PathDescription entry(ShapeType::Path, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500384 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700385
Romain Guy7fbcc042010-08-04 15:40:07 -0700386 PathTexture* texture = mCache.get(entry);
387
388 if (!texture) {
389 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800390 } else {
391 // A bitmap is attached to the texture, this means we need to
392 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700393 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800394 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800395 // But we must first wait for the worker thread to be done
396 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700397 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800398 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700399 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700400 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800401 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700402 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700403 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800404 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800405 mCache.remove(entry);
406 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800407 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700408 }
409
Romain Guy7fbcc042010-08-04 15:40:07 -0700410 return texture;
411}
412
sergeyv7224e2b2016-04-07 18:06:53 -0700413void PathCache::remove(const SkPath* path, const SkPaint* paint) {
414 PathDescription entry(ShapeType::Path, paint);
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530415 entry.shape.path.mGenerationID = path->getGenerationID();
416 mCache.remove(entry);
417}
418
Chris Craikd218a922014-01-02 17:13:34 -0800419void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700420 if (!Caches::getInstance().tasks.canRunTasks()) {
421 return;
422 }
423
sergeyv7224e2b2016-04-07 18:06:53 -0700424 PathDescription entry(ShapeType::Path, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500425 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700426
Romain Guyca89e2a2013-03-08 17:44:20 -0800427 PathTexture* texture = mCache.get(entry);
428
429 bool generate = false;
430 if (!texture) {
431 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800432 }
433
434 if (generate) {
435 // It is important to specify the generation ID so we do not
436 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700437 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700438 sp<PathTask> task = new PathTask(path, paint, texture);
439 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800440
441 // During the precaching phase we insert path texture objects into
442 // the cache that do not point to any GL texture. They are instead
443 // treated as a task for the precaching worker thread. This is why
444 // we do not check the cache limit when inserting these objects.
445 // The conversion into GL texture will happen in get(), when a client
446 // asks for a path texture. This is also when the cache limit will
447 // be enforced.
448 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700449
Chris Craikd41c4d82015-01-05 15:51:13 -0800450 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700451 mProcessor = new PathProcessor(Caches::getInstance());
452 }
Chris Craikdee66b62015-04-20 14:54:49 -0700453 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800454 }
455}
456
Romain Guyc46d07a2013-03-15 19:06:39 -0700457///////////////////////////////////////////////////////////////////////////////
458// Rounded rects
459///////////////////////////////////////////////////////////////////////////////
460
461PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800462 float rx, float ry, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700463 PathDescription entry(ShapeType::RoundRect, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700464 entry.shape.roundRect.mWidth = width;
465 entry.shape.roundRect.mHeight = height;
466 entry.shape.roundRect.mRx = rx;
467 entry.shape.roundRect.mRy = ry;
468
469 PathTexture* texture = get(entry);
470
471 if (!texture) {
472 SkPath path;
473 SkRect r;
474 r.set(0.0f, 0.0f, width, height);
475 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
476
477 texture = addTexture(entry, &path, paint);
478 }
479
480 return texture;
481}
482
483///////////////////////////////////////////////////////////////////////////////
484// Circles
485///////////////////////////////////////////////////////////////////////////////
486
Chris Craikd218a922014-01-02 17:13:34 -0800487PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700488 PathDescription entry(ShapeType::Circle, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700489 entry.shape.circle.mRadius = radius;
490
491 PathTexture* texture = get(entry);
492
493 if (!texture) {
494 SkPath path;
495 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
496
497 texture = addTexture(entry, &path, paint);
498 }
499
500 return texture;
501}
502
503///////////////////////////////////////////////////////////////////////////////
504// Ovals
505///////////////////////////////////////////////////////////////////////////////
506
Chris Craikd218a922014-01-02 17:13:34 -0800507PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700508 PathDescription entry(ShapeType::Oval, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700509 entry.shape.oval.mWidth = width;
510 entry.shape.oval.mHeight = height;
511
512 PathTexture* texture = get(entry);
513
514 if (!texture) {
515 SkPath path;
516 SkRect r;
517 r.set(0.0f, 0.0f, width, height);
518 path.addOval(r, SkPath::kCW_Direction);
519
520 texture = addTexture(entry, &path, paint);
521 }
522
523 return texture;
524}
525
526///////////////////////////////////////////////////////////////////////////////
527// Rects
528///////////////////////////////////////////////////////////////////////////////
529
Chris Craikd218a922014-01-02 17:13:34 -0800530PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700531 PathDescription entry(ShapeType::Rect, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700532 entry.shape.rect.mWidth = width;
533 entry.shape.rect.mHeight = height;
534
535 PathTexture* texture = get(entry);
536
537 if (!texture) {
538 SkPath path;
539 SkRect r;
540 r.set(0.0f, 0.0f, width, height);
541 path.addRect(r, SkPath::kCW_Direction);
542
543 texture = addTexture(entry, &path, paint);
544 }
545
546 return texture;
547}
548
549///////////////////////////////////////////////////////////////////////////////
550// Arcs
551///////////////////////////////////////////////////////////////////////////////
552
553PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800554 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
sergeyv7224e2b2016-04-07 18:06:53 -0700555 PathDescription entry(ShapeType::Arc, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -0700556 entry.shape.arc.mWidth = width;
557 entry.shape.arc.mHeight = height;
558 entry.shape.arc.mStartAngle = startAngle;
559 entry.shape.arc.mSweepAngle = sweepAngle;
560 entry.shape.arc.mUseCenter = useCenter;
561
562 PathTexture* texture = get(entry);
563
564 if (!texture) {
565 SkPath path;
566 SkRect r;
567 r.set(0.0f, 0.0f, width, height);
568 if (useCenter) {
569 path.moveTo(r.centerX(), r.centerY());
570 }
571 path.arcTo(r, startAngle, sweepAngle, !useCenter);
572 if (useCenter) {
573 path.close();
574 }
575
576 texture = addTexture(entry, &path, paint);
577 }
578
579 return texture;
580}
581
Romain Guy7fbcc042010-08-04 15:40:07 -0700582}; // namespace uirenderer
583}; // namespace android