blob: 8f914acc5c764c303b15f6bd86217d63e5d864ae [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
Chris Craik48a8f432016-02-05 15:59:29 -0800138PathCache::PathCache()
139 : mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity)
140 , mSize(0)
141 , mMaxSize(Properties::pathCacheSize) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700142 mCache.setOnEntryRemovedListener(this);
143
144 GLint maxTextureSize;
145 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
146 mMaxTextureSize = maxTextureSize;
147
Chris Craik2507c342015-05-04 14:36:49 -0700148 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700149}
150
Chris Craik05f3d6e2014-06-02 16:27:04 -0700151PathCache::~PathCache() {
152 mCache.clear();
153}
154
Romain Guyc46d07a2013-03-15 19:06:39 -0700155///////////////////////////////////////////////////////////////////////////////
156// Size management
157///////////////////////////////////////////////////////////////////////////////
158
159uint32_t PathCache::getSize() {
160 return mSize;
161}
162
163uint32_t PathCache::getMaxSize() {
164 return mMaxSize;
165}
166
Romain Guyc46d07a2013-03-15 19:06:39 -0700167///////////////////////////////////////////////////////////////////////////////
168// Callbacks
169///////////////////////////////////////////////////////////////////////////////
170
Andreas Gampe64bb4132014-11-22 00:35:09 +0000171void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700172 removeTexture(texture);
173}
174
175///////////////////////////////////////////////////////////////////////////////
176// Caching
177///////////////////////////////////////////////////////////////////////////////
178
179void PathCache::removeTexture(PathTexture* texture) {
180 if (texture) {
John Reck38e0c322015-11-10 12:19:17 -0800181 const uint32_t size = texture->width() * texture->height();
Romain Guy5d923202013-08-21 18:40:24 -0700182
183 // If there is a pending task we must wait for it to return
184 // before attempting our cleanup
185 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800186 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800187 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700188 texture->clearTask();
189 } else {
190 // If there is a pending task, the path was not added
191 // to the cache and the size wasn't increased
192 if (size > mSize) {
193 ALOGE("Removing path texture of size %d will leave "
194 "the cache in an inconsistent state", size);
195 }
196 mSize -= size;
197 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700198
199 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
200 texture->id, size, mSize);
201 if (mDebugEnabled) {
202 ALOGD("Shape deleted, size = %d", size);
203 }
204
John Reck38e0c322015-11-10 12:19:17 -0800205 texture->deleteTexture();
Romain Guyc46d07a2013-03-15 19:06:39 -0700206 delete texture;
207 }
208}
209
210void PathCache::purgeCache(uint32_t width, uint32_t height) {
211 const uint32_t size = width * height;
212 // Don't even try to cache a bitmap that's bigger than the cache
213 if (size < mMaxSize) {
214 while (mSize + size > mMaxSize) {
215 mCache.removeOldest();
216 }
217 }
218}
219
220void PathCache::trim() {
221 while (mSize > mMaxSize) {
222 mCache.removeOldest();
223 }
224}
225
226PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
227 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800228 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700229
230 float left, top, offset;
231 uint32_t width, height;
232 computePathBounds(path, paint, left, top, offset, width, height);
233
Chris Craikd41c4d82015-01-05 15:51:13 -0800234 if (!checkTextureSize(width, height)) return nullptr;
Romain Guyc46d07a2013-03-15 19:06:39 -0700235
236 purgeCache(width, height);
237
238 SkBitmap bitmap;
239 drawPath(path, paint, bitmap, left, top, offset, width, height);
240
Chris Craike2bb3802015-03-13 15:07:52 -0700241 PathTexture* texture = new PathTexture(Caches::getInstance(),
John Reck38e0c322015-11-10 12:19:17 -0800242 left, top, offset, path->getGenerationID());
Romain Guy4500a8d2013-03-26 17:29:51 -0700243 generateTexture(entry, &bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700244
245 return texture;
246}
247
Romain Guy4500a8d2013-03-26 17:29:51 -0700248void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
249 PathTexture* texture, bool addToCache) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700250 generateTexture(*bitmap, texture);
251
Chris Craik42455fc2015-05-11 18:23:09 -0700252 // Note here that we upload to a texture even if it's bigger than mMaxSize.
253 // Such an entry in mCache will only be temporary, since it will be evicted
254 // immediately on trim, or on any other Path entering the cache.
John Reck38e0c322015-11-10 12:19:17 -0800255 uint32_t size = texture->width() * texture->height();
Chris Craik42455fc2015-05-11 18:23:09 -0700256 mSize += size;
257 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
258 texture->id, size, mSize);
259 if (mDebugEnabled) {
260 ALOGD("Shape created, size = %d", size);
261 }
262 if (addToCache) {
263 mCache.put(entry, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700264 }
265}
266
267void PathCache::clear() {
268 mCache.clear();
269}
270
271void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700272 ATRACE_NAME("Upload Path Texture");
John Reck38e0c322015-11-10 12:19:17 -0800273 texture->upload(bitmap);
Romain Guyc46d07a2013-03-15 19:06:39 -0700274 texture->setFilter(GL_LINEAR);
Romain Guyc46d07a2013-03-15 19:06:39 -0700275}
276
277///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800278// Path precaching
279///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700280
Romain Guy5dc7fa72013-03-11 20:48:31 -0700281PathCache::PathProcessor::PathProcessor(Caches& caches):
282 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700283}
Romain Guy33f6beb2012-02-16 19:24:51 -0800284
Romain Guy5dc7fa72013-03-11 20:48:31 -0700285void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700286 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700287 ATRACE_NAME("pathPrecache");
288
289 float left, top, offset;
290 uint32_t width, height;
Chris Craik906d47f2014-06-27 18:30:23 -0700291 PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700292
293 PathTexture* texture = t->texture;
294 texture->left = left;
295 texture->top = top;
296 texture->offset = offset;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700297
298 if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
299 SkBitmap* bitmap = new SkBitmap();
Chris Craik906d47f2014-06-27 18:30:23 -0700300 drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700301 t->setResult(bitmap);
302 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800303 t->setResult(nullptr);
Romain Guyca89e2a2013-03-08 17:44:20 -0800304 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800305}
306
Romain Guy7fbcc042010-08-04 15:40:07 -0700307///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700308// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700309///////////////////////////////////////////////////////////////////////////////
310
Derek Sollenbergeree248592015-02-12 14:10:21 -0500311void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700313 mGarbage.push_back(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800314}
315
316void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700317 Vector<PathDescription> pathsToRemove;
318
319 { // scope for the mutex
320 Mutex::Autolock l(mLock);
John Reck272a6852015-07-29 16:48:58 -0700321 for (const uint32_t generationID : mGarbage) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500322 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
323 while (iter.next()) {
324 const PathDescription& key = iter.key();
325 if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
326 pathsToRemove.push(key);
327 }
328 }
Romain Guye3b0a012013-06-26 15:45:41 -0700329 }
330 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800331 }
Romain Guye3b0a012013-06-26 15:45:41 -0700332
333 for (size_t i = 0; i < pathsToRemove.size(); i++) {
334 mCache.remove(pathsToRemove.itemAt(i));
335 }
Romain Guya2341a92010-09-08 18:04:33 -0700336}
337
Chris Craikd218a922014-01-02 17:13:34 -0800338PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700339 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500340 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700341
Romain Guy7fbcc042010-08-04 15:40:07 -0700342 PathTexture* texture = mCache.get(entry);
343
344 if (!texture) {
345 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800346 } else {
347 // A bitmap is attached to the texture, this means we need to
348 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700349 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800350 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800351 // But we must first wait for the worker thread to be done
352 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700353 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800354 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700355 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700356 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800357 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700358 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700359 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800360 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800361 mCache.remove(entry);
362 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800363 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700364 }
365
Romain Guy7fbcc042010-08-04 15:40:07 -0700366 return texture;
367}
368
Digish Pandya2e4f67c2015-11-04 11:00:28 +0530369void PathCache::remove(const SkPath* path, const SkPaint* paint)
370{
371 PathDescription entry(kShapePath, paint);
372 entry.shape.path.mGenerationID = path->getGenerationID();
373 mCache.remove(entry);
374}
375
Chris Craikd218a922014-01-02 17:13:34 -0800376void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700377 if (!Caches::getInstance().tasks.canRunTasks()) {
378 return;
379 }
380
Romain Guyc46d07a2013-03-15 19:06:39 -0700381 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500382 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700383
Romain Guyca89e2a2013-03-08 17:44:20 -0800384 PathTexture* texture = mCache.get(entry);
385
386 bool generate = false;
387 if (!texture) {
388 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800389 }
390
391 if (generate) {
392 // It is important to specify the generation ID so we do not
393 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700394 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700395 sp<PathTask> task = new PathTask(path, paint, texture);
396 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800397
398 // During the precaching phase we insert path texture objects into
399 // the cache that do not point to any GL texture. They are instead
400 // treated as a task for the precaching worker thread. This is why
401 // we do not check the cache limit when inserting these objects.
402 // The conversion into GL texture will happen in get(), when a client
403 // asks for a path texture. This is also when the cache limit will
404 // be enforced.
405 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700406
Chris Craikd41c4d82015-01-05 15:51:13 -0800407 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700408 mProcessor = new PathProcessor(Caches::getInstance());
409 }
Chris Craikdee66b62015-04-20 14:54:49 -0700410 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800411 }
412}
413
Romain Guyc46d07a2013-03-15 19:06:39 -0700414///////////////////////////////////////////////////////////////////////////////
415// Rounded rects
416///////////////////////////////////////////////////////////////////////////////
417
418PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800419 float rx, float ry, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700420 PathDescription entry(kShapeRoundRect, paint);
421 entry.shape.roundRect.mWidth = width;
422 entry.shape.roundRect.mHeight = height;
423 entry.shape.roundRect.mRx = rx;
424 entry.shape.roundRect.mRy = ry;
425
426 PathTexture* texture = get(entry);
427
428 if (!texture) {
429 SkPath path;
430 SkRect r;
431 r.set(0.0f, 0.0f, width, height);
432 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
433
434 texture = addTexture(entry, &path, paint);
435 }
436
437 return texture;
438}
439
440///////////////////////////////////////////////////////////////////////////////
441// Circles
442///////////////////////////////////////////////////////////////////////////////
443
Chris Craikd218a922014-01-02 17:13:34 -0800444PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700445 PathDescription entry(kShapeCircle, paint);
446 entry.shape.circle.mRadius = radius;
447
448 PathTexture* texture = get(entry);
449
450 if (!texture) {
451 SkPath path;
452 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
453
454 texture = addTexture(entry, &path, paint);
455 }
456
457 return texture;
458}
459
460///////////////////////////////////////////////////////////////////////////////
461// Ovals
462///////////////////////////////////////////////////////////////////////////////
463
Chris Craikd218a922014-01-02 17:13:34 -0800464PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700465 PathDescription entry(kShapeOval, paint);
466 entry.shape.oval.mWidth = width;
467 entry.shape.oval.mHeight = height;
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.addOval(r, SkPath::kCW_Direction);
476
477 texture = addTexture(entry, &path, paint);
478 }
479
480 return texture;
481}
482
483///////////////////////////////////////////////////////////////////////////////
484// Rects
485///////////////////////////////////////////////////////////////////////////////
486
Chris Craikd218a922014-01-02 17:13:34 -0800487PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700488 PathDescription entry(kShapeRect, paint);
489 entry.shape.rect.mWidth = width;
490 entry.shape.rect.mHeight = height;
491
492 PathTexture* texture = get(entry);
493
494 if (!texture) {
495 SkPath path;
496 SkRect r;
497 r.set(0.0f, 0.0f, width, height);
498 path.addRect(r, SkPath::kCW_Direction);
499
500 texture = addTexture(entry, &path, paint);
501 }
502
503 return texture;
504}
505
506///////////////////////////////////////////////////////////////////////////////
507// Arcs
508///////////////////////////////////////////////////////////////////////////////
509
510PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800511 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700512 PathDescription entry(kShapeArc, paint);
513 entry.shape.arc.mWidth = width;
514 entry.shape.arc.mHeight = height;
515 entry.shape.arc.mStartAngle = startAngle;
516 entry.shape.arc.mSweepAngle = sweepAngle;
517 entry.shape.arc.mUseCenter = useCenter;
518
519 PathTexture* texture = get(entry);
520
521 if (!texture) {
522 SkPath path;
523 SkRect r;
524 r.set(0.0f, 0.0f, width, height);
525 if (useCenter) {
526 path.moveTo(r.centerX(), r.centerY());
527 }
528 path.arcTo(r, startAngle, sweepAngle, !useCenter);
529 if (useCenter) {
530 path.close();
531 }
532
533 texture = addTexture(entry, &path, paint);
534 }
535
536 return texture;
537}
538
Romain Guy7fbcc042010-08-04 15:40:07 -0700539}; // namespace uirenderer
540}; // namespace android