blob: e36f5f17d29c0d632faf35ac7cdf63159cfd8b23 [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
33namespace android {
34namespace uirenderer {
35
Romain Guyca89e2a2013-03-08 17:44:20 -080036///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070037// Cache entries
38///////////////////////////////////////////////////////////////////////////////
39
Chris Craike2bb3802015-03-13 15:07:52 -070040PathDescription::PathDescription()
41 : type(kShapeNone)
42 , join(SkPaint::kDefault_Join)
43 , cap(SkPaint::kDefault_Cap)
44 , style(SkPaint::kFill_Style)
45 , miter(4.0f)
46 , strokeWidth(1.0f)
47 , pathEffect(nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070048 memset(&shape, 0, sizeof(Shape));
49}
50
Chris Craike2bb3802015-03-13 15:07:52 -070051PathDescription::PathDescription(ShapeType type, const SkPaint* paint)
52 : type(type)
53 , join(paint->getStrokeJoin())
54 , cap(paint->getStrokeCap())
55 , style(paint->getStyle())
56 , miter(paint->getStrokeMiter())
57 , strokeWidth(paint->getStrokeWidth())
58 , pathEffect(paint->getPathEffect()) {
Romain Guyc46d07a2013-03-15 19:06:39 -070059 memset(&shape, 0, sizeof(Shape));
60}
61
62hash_t PathDescription::hash() const {
63 uint32_t hash = JenkinsHashMix(0, type);
64 hash = JenkinsHashMix(hash, join);
65 hash = JenkinsHashMix(hash, cap);
66 hash = JenkinsHashMix(hash, style);
67 hash = JenkinsHashMix(hash, android::hash_type(miter));
68 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
69 hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
70 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
71 return JenkinsHashWhiten(hash);
72}
73
Romain Guyc46d07a2013-03-15 19:06:39 -070074///////////////////////////////////////////////////////////////////////////////
75// Utilities
76///////////////////////////////////////////////////////////////////////////////
77
Chris Craikd218a922014-01-02 17:13:34 -080078bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -070079 // NOTE: This should only be used after PathTessellator handles joins properly
Chris Craikd41c4d82015-01-05 15:51:13 -080080 return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity;
Romain Guyc46d07a2013-03-15 19:06:39 -070081}
82
83void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint,
84 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
85 const SkRect& bounds = path->getBounds();
86 PathCache::computeBounds(bounds, paint, left, top, offset, width, height);
87}
88
89void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint,
90 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
Chris Craike6a15ee2015-07-07 18:42:17 -070091 const float pathWidth = std::max(bounds.width(), 1.0f);
92 const float pathHeight = std::max(bounds.height(), 1.0f);
Romain Guyc46d07a2013-03-15 19:06:39 -070093
94 left = bounds.fLeft;
95 top = bounds.fTop;
96
Chris Craike6a15ee2015-07-07 18:42:17 -070097 offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
Romain Guyc46d07a2013-03-15 19:06:39 -070098
99 width = uint32_t(pathWidth + offset * 2.0 + 0.5);
100 height = uint32_t(pathHeight + offset * 2.0 + 0.5);
101}
102
103static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) {
Mike Reedb9330552014-06-16 17:31:48 -0400104 bitmap.allocPixels(SkImageInfo::MakeA8(width, height));
Romain Guyc46d07a2013-03-15 19:06:39 -0700105 bitmap.eraseColor(0);
106}
107
108static void initPaint(SkPaint& paint) {
109 // Make sure the paint is opaque, color, alpha, filter, etc.
110 // will be applied later when compositing the alpha8 texture
Chris Craik98d608d2014-07-17 12:25:11 -0700111 paint.setColor(SK_ColorBLACK);
Romain Guyc46d07a2013-03-15 19:06:39 -0700112 paint.setAlpha(255);
Chris Craikd41c4d82015-01-05 15:51:13 -0800113 paint.setColorFilter(nullptr);
114 paint.setMaskFilter(nullptr);
115 paint.setShader(nullptr);
Romain Guyc46d07a2013-03-15 19:06:39 -0700116 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
117 SkSafeUnref(paint.setXfermode(mode));
118}
119
120static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap,
121 float left, float top, float offset, uint32_t width, uint32_t height) {
122 initBitmap(bitmap, width, height);
123
124 SkPaint pathPaint(*paint);
125 initPaint(pathPaint);
126
127 SkCanvas canvas(bitmap);
128 canvas.translate(-left + offset, -top + offset);
129 canvas.drawPath(*path, pathPaint);
130}
131
Romain Guyc46d07a2013-03-15 19:06:39 -0700132///////////////////////////////////////////////////////////////////////////////
133// Cache constructor/destructor
134///////////////////////////////////////////////////////////////////////////////
135
136PathCache::PathCache():
137 mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity),
138 mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
139 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -0800140 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700141 INIT_LOGD(" Setting %s cache size to %sMB", name, property);
Chris Craik42455fc2015-05-11 18:23:09 -0700142 mMaxSize = MB(atof(property));
Romain Guyc46d07a2013-03-15 19:06:39 -0700143 } else {
144 INIT_LOGD(" Using default %s cache size of %.2fMB", name, DEFAULT_PATH_CACHE_SIZE);
145 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700146
Romain Guyc46d07a2013-03-15 19:06:39 -0700147 mCache.setOnEntryRemovedListener(this);
148
149 GLint maxTextureSize;
150 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
151 mMaxTextureSize = maxTextureSize;
152
Chris Craik2507c342015-05-04 14:36:49 -0700153 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700154}
155
Chris Craik05f3d6e2014-06-02 16:27:04 -0700156PathCache::~PathCache() {
157 mCache.clear();
158}
159
Romain Guyc46d07a2013-03-15 19:06:39 -0700160///////////////////////////////////////////////////////////////////////////////
161// Size management
162///////////////////////////////////////////////////////////////////////////////
163
164uint32_t PathCache::getSize() {
165 return mSize;
166}
167
168uint32_t PathCache::getMaxSize() {
169 return mMaxSize;
170}
171
Romain Guyc46d07a2013-03-15 19:06:39 -0700172///////////////////////////////////////////////////////////////////////////////
173// Callbacks
174///////////////////////////////////////////////////////////////////////////////
175
Andreas Gampe64bb4132014-11-22 00:35:09 +0000176void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700177 removeTexture(texture);
178}
179
180///////////////////////////////////////////////////////////////////////////////
181// Caching
182///////////////////////////////////////////////////////////////////////////////
183
184void PathCache::removeTexture(PathTexture* texture) {
185 if (texture) {
186 const uint32_t size = texture->width * texture->height;
Romain Guy5d923202013-08-21 18:40:24 -0700187
188 // If there is a pending task we must wait for it to return
189 // before attempting our cleanup
190 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800191 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800192 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700193 texture->clearTask();
194 } else {
195 // If there is a pending task, the path was not added
196 // to the cache and the size wasn't increased
197 if (size > mSize) {
198 ALOGE("Removing path texture of size %d will leave "
199 "the cache in an inconsistent state", size);
200 }
201 mSize -= size;
202 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700203
204 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
205 texture->id, size, mSize);
206 if (mDebugEnabled) {
207 ALOGD("Shape deleted, size = %d", size);
208 }
209
210 if (texture->id) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800211 Caches::getInstance().textureState().deleteTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700212 }
213 delete texture;
214 }
215}
216
217void PathCache::purgeCache(uint32_t width, uint32_t height) {
218 const uint32_t size = width * height;
219 // Don't even try to cache a bitmap that's bigger than the cache
220 if (size < mMaxSize) {
221 while (mSize + size > mMaxSize) {
222 mCache.removeOldest();
223 }
224 }
225}
226
227void PathCache::trim() {
228 while (mSize > mMaxSize) {
229 mCache.removeOldest();
230 }
231}
232
233PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
234 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800235 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700236
237 float left, top, offset;
238 uint32_t width, height;
239 computePathBounds(path, paint, left, top, offset, width, height);
240
Chris Craikd41c4d82015-01-05 15:51:13 -0800241 if (!checkTextureSize(width, height)) return nullptr;
Romain Guyc46d07a2013-03-15 19:06:39 -0700242
243 purgeCache(width, height);
244
245 SkBitmap bitmap;
246 drawPath(path, paint, bitmap, left, top, offset, width, height);
247
Chris Craike2bb3802015-03-13 15:07:52 -0700248 PathTexture* texture = new PathTexture(Caches::getInstance(),
249 left, top, offset, width, height,
Romain Guyc46d07a2013-03-15 19:06:39 -0700250 path->getGenerationID());
Romain Guy4500a8d2013-03-26 17:29:51 -0700251 generateTexture(entry, &bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700252
253 return texture;
254}
255
Romain Guy4500a8d2013-03-26 17:29:51 -0700256void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
257 PathTexture* texture, bool addToCache) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700258 generateTexture(*bitmap, texture);
259
Chris Craik42455fc2015-05-11 18:23:09 -0700260 // Note here that we upload to a texture even if it's bigger than mMaxSize.
261 // Such an entry in mCache will only be temporary, since it will be evicted
262 // immediately on trim, or on any other Path entering the cache.
Romain Guyc46d07a2013-03-15 19:06:39 -0700263 uint32_t size = texture->width * texture->height;
Chris Craik42455fc2015-05-11 18:23:09 -0700264 mSize += size;
265 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
266 texture->id, size, mSize);
267 if (mDebugEnabled) {
268 ALOGD("Shape created, size = %d", size);
269 }
270 if (addToCache) {
271 mCache.put(entry, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700272 }
273}
274
275void PathCache::clear() {
276 mCache.clear();
277}
278
279void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700280 ATRACE_NAME("Upload Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700281 SkAutoLockPixels alp(bitmap);
282 if (!bitmap.readyToDraw()) {
283 ALOGE("Cannot generate texture from bitmap");
284 return;
285 }
286
287 glGenTextures(1, &texture->id);
288
Chris Craik44eb2c02015-01-29 09:45:09 -0800289 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700290 // Textures are Alpha8
291 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
292
293 texture->blend = true;
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
295 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
296
297 texture->setFilter(GL_LINEAR);
298 texture->setWrap(GL_CLAMP_TO_EDGE);
299}
300
301///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800302// Path precaching
303///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700304
Romain Guy5dc7fa72013-03-11 20:48:31 -0700305PathCache::PathProcessor::PathProcessor(Caches& caches):
306 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700307}
Romain Guy33f6beb2012-02-16 19:24:51 -0800308
Romain Guy5dc7fa72013-03-11 20:48:31 -0700309void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700310 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700311 ATRACE_NAME("pathPrecache");
312
313 float left, top, offset;
314 uint32_t width, height;
Chris Craik906d47f2014-06-27 18:30:23 -0700315 PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700316
317 PathTexture* texture = t->texture;
318 texture->left = left;
319 texture->top = top;
320 texture->offset = offset;
321 texture->width = width;
322 texture->height = height;
323
324 if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
325 SkBitmap* bitmap = new SkBitmap();
Chris Craik906d47f2014-06-27 18:30:23 -0700326 drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700327 t->setResult(bitmap);
328 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700329 texture->width = 0;
330 texture->height = 0;
Chris Craikd41c4d82015-01-05 15:51:13 -0800331 t->setResult(nullptr);
Romain Guyca89e2a2013-03-08 17:44:20 -0800332 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800333}
334
Romain Guy7fbcc042010-08-04 15:40:07 -0700335///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700336// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700337///////////////////////////////////////////////////////////////////////////////
338
Derek Sollenbergeree248592015-02-12 14:10:21 -0500339void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800340 Mutex::Autolock l(mLock);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500341 mGarbage.push(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800342}
343
344void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700345 Vector<PathDescription> pathsToRemove;
346
347 { // scope for the mutex
348 Mutex::Autolock l(mLock);
349 size_t count = mGarbage.size();
350 for (size_t i = 0; i < count; i++) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500351 const uint32_t generationID = mGarbage.itemAt(i);
352
353 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
354 while (iter.next()) {
355 const PathDescription& key = iter.key();
356 if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
357 pathsToRemove.push(key);
358 }
359 }
Romain Guye3b0a012013-06-26 15:45:41 -0700360 }
361 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800362 }
Romain Guye3b0a012013-06-26 15:45:41 -0700363
364 for (size_t i = 0; i < pathsToRemove.size(); i++) {
365 mCache.remove(pathsToRemove.itemAt(i));
366 }
Romain Guya2341a92010-09-08 18:04:33 -0700367}
368
Chris Craikd218a922014-01-02 17:13:34 -0800369PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700370 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500371 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700372
Romain Guy7fbcc042010-08-04 15:40:07 -0700373 PathTexture* texture = mCache.get(entry);
374
375 if (!texture) {
376 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800377 } else {
378 // A bitmap is attached to the texture, this means we need to
379 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700380 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800381 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800382 // But we must first wait for the worker thread to be done
383 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700384 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800385 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700386 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700387 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800388 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700389 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700390 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800391 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800392 mCache.remove(entry);
393 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800394 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700395 }
396
Romain Guy7fbcc042010-08-04 15:40:07 -0700397 return texture;
398}
399
Chris Craikd218a922014-01-02 17:13:34 -0800400void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700401 if (!Caches::getInstance().tasks.canRunTasks()) {
402 return;
403 }
404
Romain Guyc46d07a2013-03-15 19:06:39 -0700405 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500406 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700407
Romain Guyca89e2a2013-03-08 17:44:20 -0800408 PathTexture* texture = mCache.get(entry);
409
410 bool generate = false;
411 if (!texture) {
412 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800413 }
414
415 if (generate) {
416 // It is important to specify the generation ID so we do not
417 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700418 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700419 sp<PathTask> task = new PathTask(path, paint, texture);
420 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800421
422 // During the precaching phase we insert path texture objects into
423 // the cache that do not point to any GL texture. They are instead
424 // treated as a task for the precaching worker thread. This is why
425 // we do not check the cache limit when inserting these objects.
426 // The conversion into GL texture will happen in get(), when a client
427 // asks for a path texture. This is also when the cache limit will
428 // be enforced.
429 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700430
Chris Craikd41c4d82015-01-05 15:51:13 -0800431 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700432 mProcessor = new PathProcessor(Caches::getInstance());
433 }
Chris Craikdee66b62015-04-20 14:54:49 -0700434 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800435 }
436}
437
Romain Guyc46d07a2013-03-15 19:06:39 -0700438///////////////////////////////////////////////////////////////////////////////
439// Rounded rects
440///////////////////////////////////////////////////////////////////////////////
441
442PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800443 float rx, float ry, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700444 PathDescription entry(kShapeRoundRect, paint);
445 entry.shape.roundRect.mWidth = width;
446 entry.shape.roundRect.mHeight = height;
447 entry.shape.roundRect.mRx = rx;
448 entry.shape.roundRect.mRy = ry;
449
450 PathTexture* texture = get(entry);
451
452 if (!texture) {
453 SkPath path;
454 SkRect r;
455 r.set(0.0f, 0.0f, width, height);
456 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
457
458 texture = addTexture(entry, &path, paint);
459 }
460
461 return texture;
462}
463
464///////////////////////////////////////////////////////////////////////////////
465// Circles
466///////////////////////////////////////////////////////////////////////////////
467
Chris Craikd218a922014-01-02 17:13:34 -0800468PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700469 PathDescription entry(kShapeCircle, paint);
470 entry.shape.circle.mRadius = radius;
471
472 PathTexture* texture = get(entry);
473
474 if (!texture) {
475 SkPath path;
476 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
477
478 texture = addTexture(entry, &path, paint);
479 }
480
481 return texture;
482}
483
484///////////////////////////////////////////////////////////////////////////////
485// Ovals
486///////////////////////////////////////////////////////////////////////////////
487
Chris Craikd218a922014-01-02 17:13:34 -0800488PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700489 PathDescription entry(kShapeOval, paint);
490 entry.shape.oval.mWidth = width;
491 entry.shape.oval.mHeight = height;
492
493 PathTexture* texture = get(entry);
494
495 if (!texture) {
496 SkPath path;
497 SkRect r;
498 r.set(0.0f, 0.0f, width, height);
499 path.addOval(r, SkPath::kCW_Direction);
500
501 texture = addTexture(entry, &path, paint);
502 }
503
504 return texture;
505}
506
507///////////////////////////////////////////////////////////////////////////////
508// Rects
509///////////////////////////////////////////////////////////////////////////////
510
Chris Craikd218a922014-01-02 17:13:34 -0800511PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700512 PathDescription entry(kShapeRect, paint);
513 entry.shape.rect.mWidth = width;
514 entry.shape.rect.mHeight = height;
515
516 PathTexture* texture = get(entry);
517
518 if (!texture) {
519 SkPath path;
520 SkRect r;
521 r.set(0.0f, 0.0f, width, height);
522 path.addRect(r, SkPath::kCW_Direction);
523
524 texture = addTexture(entry, &path, paint);
525 }
526
527 return texture;
528}
529
530///////////////////////////////////////////////////////////////////////////////
531// Arcs
532///////////////////////////////////////////////////////////////////////////////
533
534PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800535 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700536 PathDescription entry(kShapeArc, paint);
537 entry.shape.arc.mWidth = width;
538 entry.shape.arc.mHeight = height;
539 entry.shape.arc.mStartAngle = startAngle;
540 entry.shape.arc.mSweepAngle = sweepAngle;
541 entry.shape.arc.mUseCenter = useCenter;
542
543 PathTexture* texture = get(entry);
544
545 if (!texture) {
546 SkPath path;
547 SkRect r;
548 r.set(0.0f, 0.0f, width, height);
549 if (useCenter) {
550 path.moveTo(r.centerX(), r.centerY());
551 }
552 path.arcTo(r, startAngle, sweepAngle, !useCenter);
553 if (useCenter) {
554 path.close();
555 }
556
557 texture = addTexture(entry, &path, paint);
558 }
559
560 return texture;
561}
562
Romain Guy7fbcc042010-08-04 15:40:07 -0700563}; // namespace uirenderer
564}; // namespace android