blob: f503e5d6e8ba8ef3e54d4912faf74909d8e729d1 [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);
John Reck272a6852015-07-29 16:48:58 -0700341 mGarbage.push_back(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);
John Reck272a6852015-07-29 16:48:58 -0700349 for (const uint32_t generationID : mGarbage) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500350 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
351 while (iter.next()) {
352 const PathDescription& key = iter.key();
353 if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
354 pathsToRemove.push(key);
355 }
356 }
Romain Guye3b0a012013-06-26 15:45:41 -0700357 }
358 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800359 }
Romain Guye3b0a012013-06-26 15:45:41 -0700360
361 for (size_t i = 0; i < pathsToRemove.size(); i++) {
362 mCache.remove(pathsToRemove.itemAt(i));
363 }
Romain Guya2341a92010-09-08 18:04:33 -0700364}
365
Chris Craikd218a922014-01-02 17:13:34 -0800366PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700367 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500368 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700369
Romain Guy7fbcc042010-08-04 15:40:07 -0700370 PathTexture* texture = mCache.get(entry);
371
372 if (!texture) {
373 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800374 } else {
375 // A bitmap is attached to the texture, this means we need to
376 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700377 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800378 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800379 // But we must first wait for the worker thread to be done
380 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700381 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800382 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700383 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700384 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800385 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700386 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700387 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800388 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800389 mCache.remove(entry);
390 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800391 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700392 }
393
Romain Guy7fbcc042010-08-04 15:40:07 -0700394 return texture;
395}
396
Chris Craikd218a922014-01-02 17:13:34 -0800397void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700398 if (!Caches::getInstance().tasks.canRunTasks()) {
399 return;
400 }
401
Romain Guyc46d07a2013-03-15 19:06:39 -0700402 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500403 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700404
Romain Guyca89e2a2013-03-08 17:44:20 -0800405 PathTexture* texture = mCache.get(entry);
406
407 bool generate = false;
408 if (!texture) {
409 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800410 }
411
412 if (generate) {
413 // It is important to specify the generation ID so we do not
414 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700415 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700416 sp<PathTask> task = new PathTask(path, paint, texture);
417 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800418
419 // During the precaching phase we insert path texture objects into
420 // the cache that do not point to any GL texture. They are instead
421 // treated as a task for the precaching worker thread. This is why
422 // we do not check the cache limit when inserting these objects.
423 // The conversion into GL texture will happen in get(), when a client
424 // asks for a path texture. This is also when the cache limit will
425 // be enforced.
426 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700427
Chris Craikd41c4d82015-01-05 15:51:13 -0800428 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700429 mProcessor = new PathProcessor(Caches::getInstance());
430 }
Chris Craikdee66b62015-04-20 14:54:49 -0700431 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800432 }
433}
434
Romain Guyc46d07a2013-03-15 19:06:39 -0700435///////////////////////////////////////////////////////////////////////////////
436// Rounded rects
437///////////////////////////////////////////////////////////////////////////////
438
439PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800440 float rx, float ry, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700441 PathDescription entry(kShapeRoundRect, paint);
442 entry.shape.roundRect.mWidth = width;
443 entry.shape.roundRect.mHeight = height;
444 entry.shape.roundRect.mRx = rx;
445 entry.shape.roundRect.mRy = ry;
446
447 PathTexture* texture = get(entry);
448
449 if (!texture) {
450 SkPath path;
451 SkRect r;
452 r.set(0.0f, 0.0f, width, height);
453 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
454
455 texture = addTexture(entry, &path, paint);
456 }
457
458 return texture;
459}
460
461///////////////////////////////////////////////////////////////////////////////
462// Circles
463///////////////////////////////////////////////////////////////////////////////
464
Chris Craikd218a922014-01-02 17:13:34 -0800465PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700466 PathDescription entry(kShapeCircle, paint);
467 entry.shape.circle.mRadius = radius;
468
469 PathTexture* texture = get(entry);
470
471 if (!texture) {
472 SkPath path;
473 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
474
475 texture = addTexture(entry, &path, paint);
476 }
477
478 return texture;
479}
480
481///////////////////////////////////////////////////////////////////////////////
482// Ovals
483///////////////////////////////////////////////////////////////////////////////
484
Chris Craikd218a922014-01-02 17:13:34 -0800485PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700486 PathDescription entry(kShapeOval, paint);
487 entry.shape.oval.mWidth = width;
488 entry.shape.oval.mHeight = height;
489
490 PathTexture* texture = get(entry);
491
492 if (!texture) {
493 SkPath path;
494 SkRect r;
495 r.set(0.0f, 0.0f, width, height);
496 path.addOval(r, SkPath::kCW_Direction);
497
498 texture = addTexture(entry, &path, paint);
499 }
500
501 return texture;
502}
503
504///////////////////////////////////////////////////////////////////////////////
505// Rects
506///////////////////////////////////////////////////////////////////////////////
507
Chris Craikd218a922014-01-02 17:13:34 -0800508PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700509 PathDescription entry(kShapeRect, paint);
510 entry.shape.rect.mWidth = width;
511 entry.shape.rect.mHeight = height;
512
513 PathTexture* texture = get(entry);
514
515 if (!texture) {
516 SkPath path;
517 SkRect r;
518 r.set(0.0f, 0.0f, width, height);
519 path.addRect(r, SkPath::kCW_Direction);
520
521 texture = addTexture(entry, &path, paint);
522 }
523
524 return texture;
525}
526
527///////////////////////////////////////////////////////////////////////////////
528// Arcs
529///////////////////////////////////////////////////////////////////////////////
530
531PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800532 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700533 PathDescription entry(kShapeArc, paint);
534 entry.shape.arc.mWidth = width;
535 entry.shape.arc.mHeight = height;
536 entry.shape.arc.mStartAngle = startAngle;
537 entry.shape.arc.mSweepAngle = sweepAngle;
538 entry.shape.arc.mUseCenter = useCenter;
539
540 PathTexture* texture = get(entry);
541
542 if (!texture) {
543 SkPath path;
544 SkRect r;
545 r.set(0.0f, 0.0f, width, height);
546 if (useCenter) {
547 path.moveTo(r.centerX(), r.centerY());
548 }
549 path.arcTo(r, startAngle, sweepAngle, !useCenter);
550 if (useCenter) {
551 path.close();
552 }
553
554 texture = addTexture(entry, &path, paint);
555 }
556
557 return texture;
558}
559
Romain Guy7fbcc042010-08-04 15:40:07 -0700560}; // namespace uirenderer
561}; // namespace android