blob: 5b2e5e2b8e1eb01fee1dae0f16f019679bf225d3 [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
17#define LOG_TAG "OpenGLRenderer"
Romain Guyc46d07a2013-03-15 19:06:39 -070018#define ATRACE_TAG ATRACE_TAG_VIEW
Romain Guy7fbcc042010-08-04 15:40:07 -070019
Romain Guyc46d07a2013-03-15 19:06:39 -070020#include <SkBitmap.h>
21#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070022#include <SkColor.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070023#include <SkPaint.h>
24#include <SkPath.h>
25#include <SkRect.h>
Romain Guya2341a92010-09-08 18:04:33 -070026
Romain Guyc46d07a2013-03-15 19:06:39 -070027#include <utils/JenkinsHash.h>
28#include <utils/Trace.h>
Romain Guyca89e2a2013-03-08 17:44:20 -080029
30#include "Caches.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070031#include "PathCache.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070032
33#include "thread/Signal.h"
Romain Guyc46d07a2013-03-15 19:06:39 -070034#include "thread/TaskProcessor.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070035
36namespace android {
37namespace uirenderer {
38
Romain Guyca89e2a2013-03-08 17:44:20 -080039///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -070040// Cache entries
41///////////////////////////////////////////////////////////////////////////////
42
43PathDescription::PathDescription():
44 type(kShapeNone),
45 join(SkPaint::kDefault_Join),
46 cap(SkPaint::kDefault_Cap),
47 style(SkPaint::kFill_Style),
48 miter(4.0f),
49 strokeWidth(1.0f),
Chris Craikd41c4d82015-01-05 15:51:13 -080050 pathEffect(nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070051 memset(&shape, 0, sizeof(Shape));
52}
53
Chris Craikd218a922014-01-02 17:13:34 -080054PathDescription::PathDescription(ShapeType type, const SkPaint* paint):
Romain Guyc46d07a2013-03-15 19:06:39 -070055 type(type),
56 join(paint->getStrokeJoin()),
57 cap(paint->getStrokeCap()),
58 style(paint->getStyle()),
59 miter(paint->getStrokeMiter()),
60 strokeWidth(paint->getStrokeWidth()),
61 pathEffect(paint->getPathEffect()) {
62 memset(&shape, 0, sizeof(Shape));
63}
64
65hash_t PathDescription::hash() const {
66 uint32_t hash = JenkinsHashMix(0, type);
67 hash = JenkinsHashMix(hash, join);
68 hash = JenkinsHashMix(hash, cap);
69 hash = JenkinsHashMix(hash, style);
70 hash = JenkinsHashMix(hash, android::hash_type(miter));
71 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
72 hash = JenkinsHashMix(hash, android::hash_type(pathEffect));
73 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
74 return JenkinsHashWhiten(hash);
75}
76
Romain Guyc46d07a2013-03-15 19:06:39 -070077///////////////////////////////////////////////////////////////////////////////
78// Utilities
79///////////////////////////////////////////////////////////////////////////////
80
Chris Craikd218a922014-01-02 17:13:34 -080081bool PathCache::canDrawAsConvexPath(SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -070082 // NOTE: This should only be used after PathTessellator handles joins properly
Chris Craikd41c4d82015-01-05 15:51:13 -080083 return paint->getPathEffect() == nullptr && path->getConvexity() == SkPath::kConvex_Convexity;
Romain Guyc46d07a2013-03-15 19:06:39 -070084}
85
86void PathCache::computePathBounds(const SkPath* path, const SkPaint* paint,
87 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
88 const SkRect& bounds = path->getBounds();
89 PathCache::computeBounds(bounds, paint, left, top, offset, width, height);
90}
91
92void PathCache::computeBounds(const SkRect& bounds, const SkPaint* paint,
93 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
94 const float pathWidth = fmax(bounds.width(), 1.0f);
95 const float pathHeight = fmax(bounds.height(), 1.0f);
96
97 left = bounds.fLeft;
98 top = bounds.fTop;
99
100 offset = (int) floorf(fmax(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
101
102 width = uint32_t(pathWidth + offset * 2.0 + 0.5);
103 height = uint32_t(pathHeight + offset * 2.0 + 0.5);
104}
105
106static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) {
Mike Reedb9330552014-06-16 17:31:48 -0400107 bitmap.allocPixels(SkImageInfo::MakeA8(width, height));
Romain Guyc46d07a2013-03-15 19:06:39 -0700108 bitmap.eraseColor(0);
109}
110
111static void initPaint(SkPaint& paint) {
112 // Make sure the paint is opaque, color, alpha, filter, etc.
113 // will be applied later when compositing the alpha8 texture
Chris Craik98d608d2014-07-17 12:25:11 -0700114 paint.setColor(SK_ColorBLACK);
Romain Guyc46d07a2013-03-15 19:06:39 -0700115 paint.setAlpha(255);
Chris Craikd41c4d82015-01-05 15:51:13 -0800116 paint.setColorFilter(nullptr);
117 paint.setMaskFilter(nullptr);
118 paint.setShader(nullptr);
Romain Guyc46d07a2013-03-15 19:06:39 -0700119 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
120 SkSafeUnref(paint.setXfermode(mode));
121}
122
123static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap,
124 float left, float top, float offset, uint32_t width, uint32_t height) {
125 initBitmap(bitmap, width, height);
126
127 SkPaint pathPaint(*paint);
128 initPaint(pathPaint);
129
130 SkCanvas canvas(bitmap);
131 canvas.translate(-left + offset, -top + offset);
132 canvas.drawPath(*path, pathPaint);
133}
134
135static PathTexture* createTexture(float left, float top, float offset,
136 uint32_t width, uint32_t height, uint32_t id) {
Romain Guy8aa195d2013-06-04 18:00:09 -0700137 PathTexture* texture = new PathTexture(Caches::getInstance());
Romain Guyc46d07a2013-03-15 19:06:39 -0700138 texture->left = left;
139 texture->top = top;
140 texture->offset = offset;
141 texture->width = width;
142 texture->height = height;
143 texture->generation = id;
144 return texture;
145}
146
147///////////////////////////////////////////////////////////////////////////////
148// Cache constructor/destructor
149///////////////////////////////////////////////////////////////////////////////
150
151PathCache::PathCache():
152 mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity),
153 mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
154 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -0800155 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700156 INIT_LOGD(" Setting %s cache size to %sMB", name, property);
157 setMaxSize(MB(atof(property)));
158 } else {
159 INIT_LOGD(" Using default %s cache size of %.2fMB", name, DEFAULT_PATH_CACHE_SIZE);
160 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700161
Romain Guyc46d07a2013-03-15 19:06:39 -0700162 mCache.setOnEntryRemovedListener(this);
163
164 GLint maxTextureSize;
165 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
166 mMaxTextureSize = maxTextureSize;
167
168 mDebugEnabled = readDebugLevel() & kDebugCaches;
169}
170
Chris Craik05f3d6e2014-06-02 16:27:04 -0700171PathCache::~PathCache() {
172 mCache.clear();
173}
174
Romain Guyc46d07a2013-03-15 19:06:39 -0700175///////////////////////////////////////////////////////////////////////////////
176// Size management
177///////////////////////////////////////////////////////////////////////////////
178
179uint32_t PathCache::getSize() {
180 return mSize;
181}
182
183uint32_t PathCache::getMaxSize() {
184 return mMaxSize;
185}
186
187void PathCache::setMaxSize(uint32_t maxSize) {
188 mMaxSize = maxSize;
189 while (mSize > mMaxSize) {
190 mCache.removeOldest();
191 }
192}
193
194///////////////////////////////////////////////////////////////////////////////
195// Callbacks
196///////////////////////////////////////////////////////////////////////////////
197
Andreas Gampe64bb4132014-11-22 00:35:09 +0000198void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700199 removeTexture(texture);
200}
201
202///////////////////////////////////////////////////////////////////////////////
203// Caching
204///////////////////////////////////////////////////////////////////////////////
205
206void PathCache::removeTexture(PathTexture* texture) {
207 if (texture) {
208 const uint32_t size = texture->width * texture->height;
Romain Guy5d923202013-08-21 18:40:24 -0700209
210 // If there is a pending task we must wait for it to return
211 // before attempting our cleanup
212 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800213 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800214 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700215 texture->clearTask();
216 } else {
217 // If there is a pending task, the path was not added
218 // to the cache and the size wasn't increased
219 if (size > mSize) {
220 ALOGE("Removing path texture of size %d will leave "
221 "the cache in an inconsistent state", size);
222 }
223 mSize -= size;
224 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700225
226 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
227 texture->id, size, mSize);
228 if (mDebugEnabled) {
229 ALOGD("Shape deleted, size = %d", size);
230 }
231
232 if (texture->id) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800233 Caches::getInstance().textureState().deleteTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700234 }
235 delete texture;
236 }
237}
238
239void PathCache::purgeCache(uint32_t width, uint32_t height) {
240 const uint32_t size = width * height;
241 // Don't even try to cache a bitmap that's bigger than the cache
242 if (size < mMaxSize) {
243 while (mSize + size > mMaxSize) {
244 mCache.removeOldest();
245 }
246 }
247}
248
249void PathCache::trim() {
250 while (mSize > mMaxSize) {
251 mCache.removeOldest();
252 }
253}
254
255PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
256 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800257 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700258
259 float left, top, offset;
260 uint32_t width, height;
261 computePathBounds(path, paint, left, top, offset, width, height);
262
Chris Craikd41c4d82015-01-05 15:51:13 -0800263 if (!checkTextureSize(width, height)) return nullptr;
Romain Guyc46d07a2013-03-15 19:06:39 -0700264
265 purgeCache(width, height);
266
267 SkBitmap bitmap;
268 drawPath(path, paint, bitmap, left, top, offset, width, height);
269
270 PathTexture* texture = createTexture(left, top, offset, width, height,
271 path->getGenerationID());
Romain Guy4500a8d2013-03-26 17:29:51 -0700272 generateTexture(entry, &bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700273
274 return texture;
275}
276
Romain Guy4500a8d2013-03-26 17:29:51 -0700277void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
278 PathTexture* texture, bool addToCache) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700279 generateTexture(*bitmap, texture);
280
281 uint32_t size = texture->width * texture->height;
282 if (size < mMaxSize) {
283 mSize += size;
284 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
285 texture->id, size, mSize);
286 if (mDebugEnabled) {
287 ALOGD("Shape created, size = %d", size);
288 }
Romain Guy4500a8d2013-03-26 17:29:51 -0700289 if (addToCache) {
290 mCache.put(entry, texture);
291 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700292 } else {
Romain Guy0a8c51b2013-08-21 17:35:38 -0700293 // It's okay to add a texture that's bigger than the cache since
294 // we'll trim the cache later when addToCache is set to false
295 if (!addToCache) {
296 mSize += size;
297 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700298 texture->cleanup = true;
299 }
300}
301
302void PathCache::clear() {
303 mCache.clear();
304}
305
306void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
307 SkAutoLockPixels alp(bitmap);
308 if (!bitmap.readyToDraw()) {
309 ALOGE("Cannot generate texture from bitmap");
310 return;
311 }
312
313 glGenTextures(1, &texture->id);
314
Chris Craik44eb2c02015-01-29 09:45:09 -0800315 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700316 // Textures are Alpha8
317 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
318
319 texture->blend = true;
320 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
321 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
322
323 texture->setFilter(GL_LINEAR);
324 texture->setWrap(GL_CLAMP_TO_EDGE);
325}
326
327///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800328// Path precaching
329///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700330
Romain Guy5dc7fa72013-03-11 20:48:31 -0700331PathCache::PathProcessor::PathProcessor(Caches& caches):
332 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700333}
Romain Guy33f6beb2012-02-16 19:24:51 -0800334
Romain Guy5dc7fa72013-03-11 20:48:31 -0700335void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700336 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700337 ATRACE_NAME("pathPrecache");
338
339 float left, top, offset;
340 uint32_t width, height;
Chris Craik906d47f2014-06-27 18:30:23 -0700341 PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700342
343 PathTexture* texture = t->texture;
344 texture->left = left;
345 texture->top = top;
346 texture->offset = offset;
347 texture->width = width;
348 texture->height = height;
349
350 if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
351 SkBitmap* bitmap = new SkBitmap();
Chris Craik906d47f2014-06-27 18:30:23 -0700352 drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700353 t->setResult(bitmap);
354 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700355 texture->width = 0;
356 texture->height = 0;
Chris Craikd41c4d82015-01-05 15:51:13 -0800357 t->setResult(nullptr);
Romain Guyca89e2a2013-03-08 17:44:20 -0800358 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800359}
360
Romain Guy7fbcc042010-08-04 15:40:07 -0700361///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700362// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700363///////////////////////////////////////////////////////////////////////////////
364
Derek Sollenbergeree248592015-02-12 14:10:21 -0500365void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800366 Mutex::Autolock l(mLock);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500367 mGarbage.push(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800368}
369
370void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700371 Vector<PathDescription> pathsToRemove;
372
373 { // scope for the mutex
374 Mutex::Autolock l(mLock);
375 size_t count = mGarbage.size();
376 for (size_t i = 0; i < count; i++) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500377 const uint32_t generationID = mGarbage.itemAt(i);
378
379 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
380 while (iter.next()) {
381 const PathDescription& key = iter.key();
382 if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
383 pathsToRemove.push(key);
384 }
385 }
Romain Guye3b0a012013-06-26 15:45:41 -0700386 }
387 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800388 }
Romain Guye3b0a012013-06-26 15:45:41 -0700389
390 for (size_t i = 0; i < pathsToRemove.size(); i++) {
391 mCache.remove(pathsToRemove.itemAt(i));
392 }
Romain Guya2341a92010-09-08 18:04:33 -0700393}
394
Chris Craikd218a922014-01-02 17:13:34 -0800395PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700396 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500397 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700398
Romain Guy7fbcc042010-08-04 15:40:07 -0700399 PathTexture* texture = mCache.get(entry);
400
401 if (!texture) {
402 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800403 } else {
404 // A bitmap is attached to the texture, this means we need to
405 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700406 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800407 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800408 // But we must first wait for the worker thread to be done
409 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700410 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800411 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700412 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700413 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800414 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700415 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700416 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800417 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800418 mCache.remove(entry);
419 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800420 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700421 }
422
Romain Guy7fbcc042010-08-04 15:40:07 -0700423 return texture;
424}
425
Chris Craikd218a922014-01-02 17:13:34 -0800426void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700427 if (!Caches::getInstance().tasks.canRunTasks()) {
428 return;
429 }
430
Romain Guyc46d07a2013-03-15 19:06:39 -0700431 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500432 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700433
Romain Guyca89e2a2013-03-08 17:44:20 -0800434 PathTexture* texture = mCache.get(entry);
435
436 bool generate = false;
437 if (!texture) {
438 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800439 }
440
441 if (generate) {
442 // It is important to specify the generation ID so we do not
443 // attempt to precache the same path several times
Romain Guy5dc7fa72013-03-11 20:48:31 -0700444 texture = createTexture(0.0f, 0.0f, 0.0f, 0, 0, path->getGenerationID());
445 sp<PathTask> task = new PathTask(path, paint, texture);
446 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800447
448 // During the precaching phase we insert path texture objects into
449 // the cache that do not point to any GL texture. They are instead
450 // treated as a task for the precaching worker thread. This is why
451 // we do not check the cache limit when inserting these objects.
452 // The conversion into GL texture will happen in get(), when a client
453 // asks for a path texture. This is also when the cache limit will
454 // be enforced.
455 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700456
Chris Craikd41c4d82015-01-05 15:51:13 -0800457 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700458 mProcessor = new PathProcessor(Caches::getInstance());
459 }
Sangkyu Leec3c58e02015-01-12 09:51:53 +0900460 if (!mProcessor->add(task)) {
461 mProcessor->process(task);
462 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800463 }
464}
465
Romain Guyc46d07a2013-03-15 19:06:39 -0700466///////////////////////////////////////////////////////////////////////////////
467// Rounded rects
468///////////////////////////////////////////////////////////////////////////////
469
470PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800471 float rx, float ry, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700472 PathDescription entry(kShapeRoundRect, paint);
473 entry.shape.roundRect.mWidth = width;
474 entry.shape.roundRect.mHeight = height;
475 entry.shape.roundRect.mRx = rx;
476 entry.shape.roundRect.mRy = ry;
477
478 PathTexture* texture = get(entry);
479
480 if (!texture) {
481 SkPath path;
482 SkRect r;
483 r.set(0.0f, 0.0f, width, height);
484 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
485
486 texture = addTexture(entry, &path, paint);
487 }
488
489 return texture;
490}
491
492///////////////////////////////////////////////////////////////////////////////
493// Circles
494///////////////////////////////////////////////////////////////////////////////
495
Chris Craikd218a922014-01-02 17:13:34 -0800496PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700497 PathDescription entry(kShapeCircle, paint);
498 entry.shape.circle.mRadius = radius;
499
500 PathTexture* texture = get(entry);
501
502 if (!texture) {
503 SkPath path;
504 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
505
506 texture = addTexture(entry, &path, paint);
507 }
508
509 return texture;
510}
511
512///////////////////////////////////////////////////////////////////////////////
513// Ovals
514///////////////////////////////////////////////////////////////////////////////
515
Chris Craikd218a922014-01-02 17:13:34 -0800516PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700517 PathDescription entry(kShapeOval, paint);
518 entry.shape.oval.mWidth = width;
519 entry.shape.oval.mHeight = height;
520
521 PathTexture* texture = get(entry);
522
523 if (!texture) {
524 SkPath path;
525 SkRect r;
526 r.set(0.0f, 0.0f, width, height);
527 path.addOval(r, SkPath::kCW_Direction);
528
529 texture = addTexture(entry, &path, paint);
530 }
531
532 return texture;
533}
534
535///////////////////////////////////////////////////////////////////////////////
536// Rects
537///////////////////////////////////////////////////////////////////////////////
538
Chris Craikd218a922014-01-02 17:13:34 -0800539PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700540 PathDescription entry(kShapeRect, paint);
541 entry.shape.rect.mWidth = width;
542 entry.shape.rect.mHeight = height;
543
544 PathTexture* texture = get(entry);
545
546 if (!texture) {
547 SkPath path;
548 SkRect r;
549 r.set(0.0f, 0.0f, width, height);
550 path.addRect(r, SkPath::kCW_Direction);
551
552 texture = addTexture(entry, &path, paint);
553 }
554
555 return texture;
556}
557
558///////////////////////////////////////////////////////////////////////////////
559// Arcs
560///////////////////////////////////////////////////////////////////////////////
561
562PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800563 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700564 PathDescription entry(kShapeArc, paint);
565 entry.shape.arc.mWidth = width;
566 entry.shape.arc.mHeight = height;
567 entry.shape.arc.mStartAngle = startAngle;
568 entry.shape.arc.mSweepAngle = sweepAngle;
569 entry.shape.arc.mUseCenter = useCenter;
570
571 PathTexture* texture = get(entry);
572
573 if (!texture) {
574 SkPath path;
575 SkRect r;
576 r.set(0.0f, 0.0f, width, height);
577 if (useCenter) {
578 path.moveTo(r.centerX(), r.centerY());
579 }
580 path.arcTo(r, startAngle, sweepAngle, !useCenter);
581 if (useCenter) {
582 path.close();
583 }
584
585 texture = addTexture(entry, &path, paint);
586 }
587
588 return texture;
589}
590
Romain Guy7fbcc042010-08-04 15:40:07 -0700591}; // namespace uirenderer
592}; // namespace android