blob: 06ea55ad2eacc3a364f759ddd9e7080453875b2a [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>
22#include <SkRect.h>
Romain Guya2341a92010-09-08 18:04:33 -070023
Romain Guyc46d07a2013-03-15 19:06:39 -070024#include <utils/JenkinsHash.h>
25#include <utils/Trace.h>
Romain Guyca89e2a2013-03-08 17:44:20 -080026
27#include "Caches.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070028#include "PathCache.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070029
30#include "thread/Signal.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070031#include "thread/TaskProcessor.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070032
John Reck6b507802015-11-03 10:09:59 -080033#include <cutils/properties.h>
34
Romain Guy7fbcc042010-08-04 15:40:07 -070035namespace android {
36namespace uirenderer {
37
Romain Guyca89e2a2013-03-08 17:44:20 -080038///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070039// Cache entries
40///////////////////////////////////////////////////////////////////////////////
41
Chris Craike2bb3802015-03-13 15:07:52 -070042PathDescription::PathDescription()
43 : type(kShapeNone)
44 , join(SkPaint::kDefault_Join)
45 , cap(SkPaint::kDefault_Cap)
46 , style(SkPaint::kFill_Style)
47 , miter(4.0f)
48 , strokeWidth(1.0f)
49 , pathEffect(nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070050 memset(&shape, 0, sizeof(Shape));
51}
52
Chris Craike2bb3802015-03-13 15:07:52 -070053PathDescription::PathDescription(ShapeType type, const SkPaint* paint)
54 : type(type)
55 , join(paint->getStrokeJoin())
56 , cap(paint->getStrokeCap())
57 , style(paint->getStyle())
58 , miter(paint->getStrokeMiter())
59 , strokeWidth(paint->getStrokeWidth())
60 , pathEffect(paint->getPathEffect()) {
Romain Guyc46d07a2013-03-15 19:06:39 -070061 memset(&shape, 0, sizeof(Shape));
62}
63
64hash_t PathDescription::hash() const {
65 uint32_t hash = JenkinsHashMix(0, type);
66 hash = JenkinsHashMix(hash, join);
67 hash = JenkinsHashMix(hash, cap);
68 hash = JenkinsHashMix(hash, style);
69 hash = JenkinsHashMix(hash, android::hash_type(miter));
70 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
71 hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
72 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
73 return JenkinsHashWhiten(hash);
74}
75
Romain Guyc46d07a2013-03-15 19:06:39 -070076///////////////////////////////////////////////////////////////////////////////
77// Utilities
78///////////////////////////////////////////////////////////////////////////////
79
Chris Craikd218a922014-01-02 17:13:34 -080080bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -070081 // NOTE: This should only be used after PathTessellator handles joins properly
Chris Craikd41c4d82015-01-05 15:51:13 -080082 return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity;
Romain Guyc46d07a2013-03-15 19:06:39 -070083}
84
85void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint,
86 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
87 const SkRect& bounds = path->getBounds();
88 PathCache::computeBounds(bounds, paint, left, top, offset, width, height);
89}
90
91void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint,
92 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
Chris Craike6a15ee2015-07-07 18:42:17 -070093 const float pathWidth = std::max(bounds.width(), 1.0f);
94 const float pathHeight = std::max(bounds.height(), 1.0f);
Romain Guyc46d07a2013-03-15 19:06:39 -070095
96 left = bounds.fLeft;
97 top = bounds.fTop;
98
Chris Craike6a15ee2015-07-07 18:42:17 -070099 offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700100
101 width = uint32_t(pathWidth + offset * 2.0 + 0.5);
102 height = uint32_t(pathHeight + offset * 2.0 + 0.5);
103}
104
105static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) {
Mike Reedb9330552014-06-16 17:31:48 -0400106 bitmap.allocPixels(SkImageInfo::MakeA8(width, height));
Romain Guyc46d07a2013-03-15 19:06:39 -0700107 bitmap.eraseColor(0);
108}
109
110static void initPaint(SkPaint& paint) {
111 // Make sure the paint is opaque, color, alpha, filter, etc.
112 // will be applied later when compositing the alpha8 texture
Chris Craik98d608d2014-07-17 12:25:11 -0700113 paint.setColor(SK_ColorBLACK);
Romain Guyc46d07a2013-03-15 19:06:39 -0700114 paint.setAlpha(255);
Chris Craikd41c4d82015-01-05 15:51:13 -0800115 paint.setColorFilter(nullptr);
116 paint.setMaskFilter(nullptr);
117 paint.setShader(nullptr);
Romain Guyc46d07a2013-03-15 19:06:39 -0700118 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
119 SkSafeUnref(paint.setXfermode(mode));
120}
121
122static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap,
123 float left, float top, float offset, uint32_t width, uint32_t height) {
124 initBitmap(bitmap, width, height);
125
126 SkPaint pathPaint(*paint);
127 initPaint(pathPaint);
128
129 SkCanvas canvas(bitmap);
130 canvas.translate(-left + offset, -top + offset);
131 canvas.drawPath(*path, pathPaint);
132}
133
Romain Guyc46d07a2013-03-15 19:06:39 -0700134///////////////////////////////////////////////////////////////////////////////
135// Cache constructor/destructor
136///////////////////////////////////////////////////////////////////////////////
137
138PathCache::PathCache():
139 mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity),
140 mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
141 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -0800142 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) {
Chris Craik11718bc2015-09-22 11:50:13 -0700143 INIT_LOGD(" Setting path cache size to %sMB", property);
Chris Craik42455fc2015-05-11 18:23:09 -0700144 mMaxSize = MB(atof(property));
Romain Guyc46d07a2013-03-15 19:06:39 -0700145 } else {
Chris Craik11718bc2015-09-22 11:50:13 -0700146 INIT_LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE);
Romain Guyc46d07a2013-03-15 19:06:39 -0700147 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700148
Romain Guyc46d07a2013-03-15 19:06:39 -0700149 mCache.setOnEntryRemovedListener(this);
150
151 GLint maxTextureSize;
152 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
153 mMaxTextureSize = maxTextureSize;
154
Chris Craik2507c342015-05-04 14:36:49 -0700155 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700156}
157
Chris Craik05f3d6e2014-06-02 16:27:04 -0700158PathCache::~PathCache() {
159 mCache.clear();
160}
161
Romain Guyc46d07a2013-03-15 19:06:39 -0700162///////////////////////////////////////////////////////////////////////////////
163// Size management
164///////////////////////////////////////////////////////////////////////////////
165
166uint32_t PathCache::getSize() {
167 return mSize;
168}
169
170uint32_t PathCache::getMaxSize() {
171 return mMaxSize;
172}
173
Romain Guyc46d07a2013-03-15 19:06:39 -0700174///////////////////////////////////////////////////////////////////////////////
175// Callbacks
176///////////////////////////////////////////////////////////////////////////////
177
Andreas Gampe64bb4132014-11-22 00:35:09 +0000178void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700179 removeTexture(texture);
180}
181
182///////////////////////////////////////////////////////////////////////////////
183// Caching
184///////////////////////////////////////////////////////////////////////////////
185
186void PathCache::removeTexture(PathTexture* texture) {
187 if (texture) {
188 const uint32_t size = texture->width * texture->height;
Romain Guy5d923202013-08-21 18:40:24 -0700189
190 // If there is a pending task we must wait for it to return
191 // before attempting our cleanup
192 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800193 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800194 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700195 texture->clearTask();
196 } else {
197 // If there is a pending task, the path was not added
198 // to the cache and the size wasn't increased
199 if (size > mSize) {
200 ALOGE("Removing path texture of size %d will leave "
201 "the cache in an inconsistent state", size);
202 }
203 mSize -= size;
204 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700205
206 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
207 texture->id, size, mSize);
208 if (mDebugEnabled) {
209 ALOGD("Shape deleted, size = %d", size);
210 }
211
212 if (texture->id) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800213 Caches::getInstance().textureState().deleteTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700214 }
215 delete texture;
216 }
217}
218
219void PathCache::purgeCache(uint32_t width, uint32_t height) {
220 const uint32_t size = width * height;
221 // Don't even try to cache a bitmap that's bigger than the cache
222 if (size < mMaxSize) {
223 while (mSize + size > mMaxSize) {
224 mCache.removeOldest();
225 }
226 }
227}
228
229void PathCache::trim() {
230 while (mSize > mMaxSize) {
231 mCache.removeOldest();
232 }
233}
234
235PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
236 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800237 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700238
239 float left, top, offset;
240 uint32_t width, height;
241 computePathBounds(path, paint, left, top, offset, width, height);
242
Chris Craikd41c4d82015-01-05 15:51:13 -0800243 if (!checkTextureSize(width, height)) return nullptr;
Romain Guyc46d07a2013-03-15 19:06:39 -0700244
245 purgeCache(width, height);
246
247 SkBitmap bitmap;
248 drawPath(path, paint, bitmap, left, top, offset, width, height);
249
Chris Craike2bb3802015-03-13 15:07:52 -0700250 PathTexture* texture = new PathTexture(Caches::getInstance(),
251 left, top, offset, width, height,
Romain Guyc46d07a2013-03-15 19:06:39 -0700252 path->getGenerationID());
Romain Guy4500a8d2013-03-26 17:29:51 -0700253 generateTexture(entry, &bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700254
255 return texture;
256}
257
Romain Guy4500a8d2013-03-26 17:29:51 -0700258void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
259 PathTexture* texture, bool addToCache) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700260 generateTexture(*bitmap, texture);
261
Chris Craik42455fc2015-05-11 18:23:09 -0700262 // Note here that we upload to a texture even if it's bigger than mMaxSize.
263 // Such an entry in mCache will only be temporary, since it will be evicted
264 // immediately on trim, or on any other Path entering the cache.
Romain Guyc46d07a2013-03-15 19:06:39 -0700265 uint32_t size = texture->width * texture->height;
Chris Craik42455fc2015-05-11 18:23:09 -0700266 mSize += size;
267 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
268 texture->id, size, mSize);
269 if (mDebugEnabled) {
270 ALOGD("Shape created, size = %d", size);
271 }
272 if (addToCache) {
273 mCache.put(entry, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700274 }
275}
276
277void PathCache::clear() {
278 mCache.clear();
279}
280
281void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700282 ATRACE_NAME("Upload Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700283 SkAutoLockPixels alp(bitmap);
284 if (!bitmap.readyToDraw()) {
285 ALOGE("Cannot generate texture from bitmap");
286 return;
287 }
288
289 glGenTextures(1, &texture->id);
290
Chris Craik44eb2c02015-01-29 09:45:09 -0800291 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700292 // Textures are Alpha8
293 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
294
295 texture->blend = true;
296 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
297 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
298
299 texture->setFilter(GL_LINEAR);
300 texture->setWrap(GL_CLAMP_TO_EDGE);
301}
302
303///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800304// Path precaching
305///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700306
Romain Guy5dc7fa72013-03-11 20:48:31 -0700307PathCache::PathProcessor::PathProcessor(Caches& caches):
308 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700309}
Romain Guy33f6beb2012-02-16 19:24:51 -0800310
Romain Guy5dc7fa72013-03-11 20:48:31 -0700311void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700312 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700313 ATRACE_NAME("pathPrecache");
314
315 float left, top, offset;
316 uint32_t width, height;
Chris Craik906d47f2014-06-27 18:30:23 -0700317 PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700318
319 PathTexture* texture = t->texture;
320 texture->left = left;
321 texture->top = top;
322 texture->offset = offset;
323 texture->width = width;
324 texture->height = height;
325
326 if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
327 SkBitmap* bitmap = new SkBitmap();
Chris Craik906d47f2014-06-27 18:30:23 -0700328 drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700329 t->setResult(bitmap);
330 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700331 texture->width = 0;
332 texture->height = 0;
Chris Craikd41c4d82015-01-05 15:51:13 -0800333 t->setResult(nullptr);
Romain Guyca89e2a2013-03-08 17:44:20 -0800334 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800335}
336
Romain Guy7fbcc042010-08-04 15:40:07 -0700337///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700338// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700339///////////////////////////////////////////////////////////////////////////////
340
Derek Sollenbergeree248592015-02-12 14:10:21 -0500341void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800342 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700343 mGarbage.push_back(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800344}
345
346void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700347 Vector<PathDescription> pathsToRemove;
348
349 { // scope for the mutex
350 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700351 for (const uint32_t generationID : mGarbage) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500352 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
353 while (iter.next()) {
354 const PathDescription& key = iter.key();
355 if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
356 pathsToRemove.push(key);
357 }
358 }
Romain Guye3b0a012013-06-26 15:45:41 -0700359 }
360 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800361 }
Romain Guye3b0a012013-06-26 15:45:41 -0700362
363 for (size_t i = 0; i < pathsToRemove.size(); i++) {
364 mCache.remove(pathsToRemove.itemAt(i));
365 }
Romain Guya2341a92010-09-08 18:04:33 -0700366}
367
Chris Craikd218a922014-01-02 17:13:34 -0800368PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700369 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500370 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700371
Romain Guy7fbcc042010-08-04 15:40:07 -0700372 PathTexture* texture = mCache.get(entry);
373
374 if (!texture) {
375 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800376 } else {
377 // A bitmap is attached to the texture, this means we need to
378 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700379 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800380 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800381 // But we must first wait for the worker thread to be done
382 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700383 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800384 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700385 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700386 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800387 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700388 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700389 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800390 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800391 mCache.remove(entry);
392 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800393 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700394 }
395
Romain Guy7fbcc042010-08-04 15:40:07 -0700396 return texture;
397}
398
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530399void PathCache::remove(const SkPath* path, const SkPaint* paint)
400{
401 PathDescription entry(kShapePath, paint);
402 entry.shape.path.mGenerationID = path->getGenerationID();
403 mCache.remove(entry);
404}
405
Chris Craikd218a922014-01-02 17:13:34 -0800406void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700407 if (!Caches::getInstance().tasks.canRunTasks()) {
408 return;
409 }
410
Romain Guyc46d07a2013-03-15 19:06:39 -0700411 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500412 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700413
Romain Guyca89e2a2013-03-08 17:44:20 -0800414 PathTexture* texture = mCache.get(entry);
415
416 bool generate = false;
417 if (!texture) {
418 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800419 }
420
421 if (generate) {
422 // It is important to specify the generation ID so we do not
423 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700424 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700425 sp<PathTask> task = new PathTask(path, paint, texture);
426 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800427
428 // During the precaching phase we insert path texture objects into
429 // the cache that do not point to any GL texture. They are instead
430 // treated as a task for the precaching worker thread. This is why
431 // we do not check the cache limit when inserting these objects.
432 // The conversion into GL texture will happen in get(), when a client
433 // asks for a path texture. This is also when the cache limit will
434 // be enforced.
435 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700436
Chris Craikd41c4d82015-01-05 15:51:13 -0800437 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700438 mProcessor = new PathProcessor(Caches::getInstance());
439 }
Chris Craikdee66b62015-04-20 14:54:49 -0700440 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800441 }
442}
443
Romain Guyc46d07a2013-03-15 19:06:39 -0700444///////////////////////////////////////////////////////////////////////////////
445// Rounded rects
446///////////////////////////////////////////////////////////////////////////////
447
448PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800449 float rx, float ry, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700450 PathDescription entry(kShapeRoundRect, paint);
451 entry.shape.roundRect.mWidth = width;
452 entry.shape.roundRect.mHeight = height;
453 entry.shape.roundRect.mRx = rx;
454 entry.shape.roundRect.mRy = ry;
455
456 PathTexture* texture = get(entry);
457
458 if (!texture) {
459 SkPath path;
460 SkRect r;
461 r.set(0.0f, 0.0f, width, height);
462 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
463
464 texture = addTexture(entry, &path, paint);
465 }
466
467 return texture;
468}
469
470///////////////////////////////////////////////////////////////////////////////
471// Circles
472///////////////////////////////////////////////////////////////////////////////
473
Chris Craikd218a922014-01-02 17:13:34 -0800474PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700475 PathDescription entry(kShapeCircle, paint);
476 entry.shape.circle.mRadius = radius;
477
478 PathTexture* texture = get(entry);
479
480 if (!texture) {
481 SkPath path;
482 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
483
484 texture = addTexture(entry, &path, paint);
485 }
486
487 return texture;
488}
489
490///////////////////////////////////////////////////////////////////////////////
491// Ovals
492///////////////////////////////////////////////////////////////////////////////
493
Chris Craikd218a922014-01-02 17:13:34 -0800494PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700495 PathDescription entry(kShapeOval, paint);
496 entry.shape.oval.mWidth = width;
497 entry.shape.oval.mHeight = height;
498
499 PathTexture* texture = get(entry);
500
501 if (!texture) {
502 SkPath path;
503 SkRect r;
504 r.set(0.0f, 0.0f, width, height);
505 path.addOval(r, SkPath::kCW_Direction);
506
507 texture = addTexture(entry, &path, paint);
508 }
509
510 return texture;
511}
512
513///////////////////////////////////////////////////////////////////////////////
514// Rects
515///////////////////////////////////////////////////////////////////////////////
516
Chris Craikd218a922014-01-02 17:13:34 -0800517PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700518 PathDescription entry(kShapeRect, paint);
519 entry.shape.rect.mWidth = width;
520 entry.shape.rect.mHeight = height;
521
522 PathTexture* texture = get(entry);
523
524 if (!texture) {
525 SkPath path;
526 SkRect r;
527 r.set(0.0f, 0.0f, width, height);
528 path.addRect(r, SkPath::kCW_Direction);
529
530 texture = addTexture(entry, &path, paint);
531 }
532
533 return texture;
534}
535
536///////////////////////////////////////////////////////////////////////////////
537// Arcs
538///////////////////////////////////////////////////////////////////////////////
539
540PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800541 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700542 PathDescription entry(kShapeArc, paint);
543 entry.shape.arc.mWidth = width;
544 entry.shape.arc.mHeight = height;
545 entry.shape.arc.mStartAngle = startAngle;
546 entry.shape.arc.mSweepAngle = sweepAngle;
547 entry.shape.arc.mUseCenter = useCenter;
548
549 PathTexture* texture = get(entry);
550
551 if (!texture) {
552 SkPath path;
553 SkRect r;
554 r.set(0.0f, 0.0f, width, height);
555 if (useCenter) {
556 path.moveTo(r.centerX(), r.centerY());
557 }
558 path.arcTo(r, startAngle, sweepAngle, !useCenter);
559 if (useCenter) {
560 path.close();
561 }
562
563 texture = addTexture(entry, &path, paint);
564 }
565
566 return texture;
567}
568
Romain Guy7fbcc042010-08-04 15:40:07 -0700569}; // namespace uirenderer
570}; // namespace android