blob: 3af640f76365d6a2ebae2d1c4455dcd656b2423c [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
Chris Craike2bb3802015-03-13 15:07:52 -070043PathDescription::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)
50 , pathEffect(nullptr) {
Romain Guyc46d07a2013-03-15 19:06:39 -070051 memset(&shape, 0, sizeof(Shape));
52}
53
Chris Craike2bb3802015-03-13 15:07:52 -070054PathDescription::PathDescription(ShapeType type, const SkPaint* paint)
55 : 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()) {
Romain Guyc46d07a2013-03-15 19:06:39 -070062 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) {
Chris Craike6a15ee2015-07-07 18:42:17 -070094 const float pathWidth = std::max(bounds.width(), 1.0f);
95 const float pathHeight = std::max(bounds.height(), 1.0f);
Romain Guyc46d07a2013-03-15 19:06:39 -070096
97 left = bounds.fLeft;
98 top = bounds.fTop;
99
Chris Craike6a15ee2015-07-07 18:42:17 -0700100 offset = (int) floorf(std::max(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
Romain Guyc46d07a2013-03-15 19:06:39 -0700101
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
Romain Guyc46d07a2013-03-15 19:06:39 -0700135///////////////////////////////////////////////////////////////////////////////
136// Cache constructor/destructor
137///////////////////////////////////////////////////////////////////////////////
138
139PathCache::PathCache():
140 mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity),
141 mSize(0), mMaxSize(MB(DEFAULT_PATH_CACHE_SIZE)) {
142 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -0800143 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, nullptr) > 0) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700144 INIT_LOGD(" Setting %s cache size to %sMB", name, property);
Chris Craik42455fc2015-05-11 18:23:09 -0700145 mMaxSize = MB(atof(property));
Romain Guyc46d07a2013-03-15 19:06:39 -0700146 } else {
147 INIT_LOGD(" Using default %s cache size of %.2fMB", name, DEFAULT_PATH_CACHE_SIZE);
148 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700149
Romain Guyc46d07a2013-03-15 19:06:39 -0700150 mCache.setOnEntryRemovedListener(this);
151
152 GLint maxTextureSize;
153 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
154 mMaxTextureSize = maxTextureSize;
155
Chris Craik2507c342015-05-04 14:36:49 -0700156 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Romain Guyc46d07a2013-03-15 19:06:39 -0700157}
158
Chris Craik05f3d6e2014-06-02 16:27:04 -0700159PathCache::~PathCache() {
160 mCache.clear();
161}
162
Romain Guyc46d07a2013-03-15 19:06:39 -0700163///////////////////////////////////////////////////////////////////////////////
164// Size management
165///////////////////////////////////////////////////////////////////////////////
166
167uint32_t PathCache::getSize() {
168 return mSize;
169}
170
171uint32_t PathCache::getMaxSize() {
172 return mMaxSize;
173}
174
Romain Guyc46d07a2013-03-15 19:06:39 -0700175///////////////////////////////////////////////////////////////////////////////
176// Callbacks
177///////////////////////////////////////////////////////////////////////////////
178
Andreas Gampe64bb4132014-11-22 00:35:09 +0000179void PathCache::operator()(PathDescription& entry, PathTexture*& texture) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700180 removeTexture(texture);
181}
182
183///////////////////////////////////////////////////////////////////////////////
184// Caching
185///////////////////////////////////////////////////////////////////////////////
186
187void PathCache::removeTexture(PathTexture* texture) {
188 if (texture) {
189 const uint32_t size = texture->width * texture->height;
Romain Guy5d923202013-08-21 18:40:24 -0700190
191 // If there is a pending task we must wait for it to return
192 // before attempting our cleanup
193 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800194 if (task != nullptr) {
Andreas Gampe1e196742014-11-10 15:23:43 -0800195 task->getResult();
Romain Guy5d923202013-08-21 18:40:24 -0700196 texture->clearTask();
197 } else {
198 // If there is a pending task, the path was not added
199 // to the cache and the size wasn't increased
200 if (size > mSize) {
201 ALOGE("Removing path texture of size %d will leave "
202 "the cache in an inconsistent state", size);
203 }
204 mSize -= size;
205 }
Romain Guyc46d07a2013-03-15 19:06:39 -0700206
207 PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d",
208 texture->id, size, mSize);
209 if (mDebugEnabled) {
210 ALOGD("Shape deleted, size = %d", size);
211 }
212
213 if (texture->id) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800214 Caches::getInstance().textureState().deleteTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700215 }
216 delete texture;
217 }
218}
219
220void PathCache::purgeCache(uint32_t width, uint32_t height) {
221 const uint32_t size = width * height;
222 // Don't even try to cache a bitmap that's bigger than the cache
223 if (size < mMaxSize) {
224 while (mSize + size > mMaxSize) {
225 mCache.removeOldest();
226 }
227 }
228}
229
230void PathCache::trim() {
231 while (mSize > mMaxSize) {
232 mCache.removeOldest();
233 }
234}
235
236PathTexture* PathCache::addTexture(const PathDescription& entry, const SkPath *path,
237 const SkPaint* paint) {
Chris Craik70850ea2014-11-18 10:49:23 -0800238 ATRACE_NAME("Generate Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700239
240 float left, top, offset;
241 uint32_t width, height;
242 computePathBounds(path, paint, left, top, offset, width, height);
243
Chris Craikd41c4d82015-01-05 15:51:13 -0800244 if (!checkTextureSize(width, height)) return nullptr;
Romain Guyc46d07a2013-03-15 19:06:39 -0700245
246 purgeCache(width, height);
247
248 SkBitmap bitmap;
249 drawPath(path, paint, bitmap, left, top, offset, width, height);
250
Chris Craike2bb3802015-03-13 15:07:52 -0700251 PathTexture* texture = new PathTexture(Caches::getInstance(),
252 left, top, offset, width, height,
Romain Guyc46d07a2013-03-15 19:06:39 -0700253 path->getGenerationID());
Romain Guy4500a8d2013-03-26 17:29:51 -0700254 generateTexture(entry, &bitmap, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700255
256 return texture;
257}
258
Romain Guy4500a8d2013-03-26 17:29:51 -0700259void PathCache::generateTexture(const PathDescription& entry, SkBitmap* bitmap,
260 PathTexture* texture, bool addToCache) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700261 generateTexture(*bitmap, texture);
262
Chris Craik42455fc2015-05-11 18:23:09 -0700263 // Note here that we upload to a texture even if it's bigger than mMaxSize.
264 // Such an entry in mCache will only be temporary, since it will be evicted
265 // immediately on trim, or on any other Path entering the cache.
Romain Guyc46d07a2013-03-15 19:06:39 -0700266 uint32_t size = texture->width * texture->height;
Chris Craik42455fc2015-05-11 18:23:09 -0700267 mSize += size;
268 PATH_LOGD("PathCache::get/create: name, size, mSize = %d, %d, %d",
269 texture->id, size, mSize);
270 if (mDebugEnabled) {
271 ALOGD("Shape created, size = %d", size);
272 }
273 if (addToCache) {
274 mCache.put(entry, texture);
Romain Guyc46d07a2013-03-15 19:06:39 -0700275 }
276}
277
278void PathCache::clear() {
279 mCache.clear();
280}
281
282void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
Chris Craikcf8426c2015-05-13 17:05:48 -0700283 ATRACE_NAME("Upload Path Texture");
Romain Guyc46d07a2013-03-15 19:06:39 -0700284 SkAutoLockPixels alp(bitmap);
285 if (!bitmap.readyToDraw()) {
286 ALOGE("Cannot generate texture from bitmap");
287 return;
288 }
289
290 glGenTextures(1, &texture->id);
291
Chris Craik44eb2c02015-01-29 09:45:09 -0800292 Caches::getInstance().textureState().bindTexture(texture->id);
Romain Guyc46d07a2013-03-15 19:06:39 -0700293 // Textures are Alpha8
294 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
295
296 texture->blend = true;
297 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
298 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
299
300 texture->setFilter(GL_LINEAR);
301 texture->setWrap(GL_CLAMP_TO_EDGE);
302}
303
304///////////////////////////////////////////////////////////////////////////////
Romain Guyca89e2a2013-03-08 17:44:20 -0800305// Path precaching
306///////////////////////////////////////////////////////////////////////////////
Romain Guyfdd6fc12012-04-27 11:47:13 -0700307
Romain Guy5dc7fa72013-03-11 20:48:31 -0700308PathCache::PathProcessor::PathProcessor(Caches& caches):
309 TaskProcessor<SkBitmap*>(&caches.tasks), mMaxTextureSize(caches.maxTextureSize) {
Romain Guyfdd6fc12012-04-27 11:47:13 -0700310}
Romain Guy33f6beb2012-02-16 19:24:51 -0800311
Romain Guy5dc7fa72013-03-11 20:48:31 -0700312void PathCache::PathProcessor::onProcess(const sp<Task<SkBitmap*> >& task) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700313 PathTask* t = static_cast<PathTask*>(task.get());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700314 ATRACE_NAME("pathPrecache");
315
316 float left, top, offset;
317 uint32_t width, height;
Chris Craik906d47f2014-06-27 18:30:23 -0700318 PathCache::computePathBounds(&t->path, &t->paint, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700319
320 PathTexture* texture = t->texture;
321 texture->left = left;
322 texture->top = top;
323 texture->offset = offset;
324 texture->width = width;
325 texture->height = height;
326
327 if (width <= mMaxTextureSize && height <= mMaxTextureSize) {
328 SkBitmap* bitmap = new SkBitmap();
Chris Craik906d47f2014-06-27 18:30:23 -0700329 drawPath(&t->path, &t->paint, *bitmap, left, top, offset, width, height);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700330 t->setResult(bitmap);
331 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700332 texture->width = 0;
333 texture->height = 0;
Chris Craikd41c4d82015-01-05 15:51:13 -0800334 t->setResult(nullptr);
Romain Guyca89e2a2013-03-08 17:44:20 -0800335 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800336}
337
Romain Guy7fbcc042010-08-04 15:40:07 -0700338///////////////////////////////////////////////////////////////////////////////
Romain Guyc46d07a2013-03-15 19:06:39 -0700339// Paths
Romain Guy7fbcc042010-08-04 15:40:07 -0700340///////////////////////////////////////////////////////////////////////////////
341
Derek Sollenbergeree248592015-02-12 14:10:21 -0500342void PathCache::removeDeferred(const SkPath* path) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800343 Mutex::Autolock l(mLock);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500344 mGarbage.push(path->getGenerationID());
Romain Guyfe48f652010-11-11 15:36:56 -0800345}
346
347void PathCache::clearGarbage() {
Romain Guye3b0a012013-06-26 15:45:41 -0700348 Vector<PathDescription> pathsToRemove;
349
350 { // scope for the mutex
351 Mutex::Autolock l(mLock);
352 size_t count = mGarbage.size();
353 for (size_t i = 0; i < count; i++) {
Derek Sollenbergeree248592015-02-12 14:10:21 -0500354 const uint32_t generationID = mGarbage.itemAt(i);
355
356 LruCache<PathDescription, PathTexture*>::Iterator iter(mCache);
357 while (iter.next()) {
358 const PathDescription& key = iter.key();
359 if (key.type == kShapePath && key.shape.path.mGenerationID == generationID) {
360 pathsToRemove.push(key);
361 }
362 }
Romain Guye3b0a012013-06-26 15:45:41 -0700363 }
364 mGarbage.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800365 }
Romain Guye3b0a012013-06-26 15:45:41 -0700366
367 for (size_t i = 0; i < pathsToRemove.size(); i++) {
368 mCache.remove(pathsToRemove.itemAt(i));
369 }
Romain Guya2341a92010-09-08 18:04:33 -0700370}
371
Chris Craikd218a922014-01-02 17:13:34 -0800372PathTexture* PathCache::get(const SkPath* path, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700373 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500374 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700375
Romain Guy7fbcc042010-08-04 15:40:07 -0700376 PathTexture* texture = mCache.get(entry);
377
378 if (!texture) {
379 texture = addTexture(entry, path, paint);
Romain Guyca89e2a2013-03-08 17:44:20 -0800380 } else {
381 // A bitmap is attached to the texture, this means we need to
382 // upload it as a GL texture
Romain Guy5dc7fa72013-03-11 20:48:31 -0700383 const sp<Task<SkBitmap*> >& task = texture->task();
Chris Craikd41c4d82015-01-05 15:51:13 -0800384 if (task != nullptr) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800385 // But we must first wait for the worker thread to be done
386 // producing the bitmap, so let's wait
Romain Guy5dc7fa72013-03-11 20:48:31 -0700387 SkBitmap* bitmap = task->getResult();
Romain Guyca89e2a2013-03-08 17:44:20 -0800388 if (bitmap) {
Romain Guy4500a8d2013-03-26 17:29:51 -0700389 generateTexture(entry, bitmap, texture, false);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700390 texture->clearTask();
Romain Guyca89e2a2013-03-08 17:44:20 -0800391 } else {
Romain Guy0f809f32013-03-13 14:31:46 -0700392 ALOGW("Path too large to be rendered into a texture");
Romain Guy5dc7fa72013-03-11 20:48:31 -0700393 texture->clearTask();
Chris Craikd41c4d82015-01-05 15:51:13 -0800394 texture = nullptr;
Romain Guyca89e2a2013-03-08 17:44:20 -0800395 mCache.remove(entry);
396 }
Romain Guyca89e2a2013-03-08 17:44:20 -0800397 }
Romain Guy7fbcc042010-08-04 15:40:07 -0700398 }
399
Romain Guy7fbcc042010-08-04 15:40:07 -0700400 return texture;
401}
402
Chris Craikd218a922014-01-02 17:13:34 -0800403void PathCache::precache(const SkPath* path, const SkPaint* paint) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700404 if (!Caches::getInstance().tasks.canRunTasks()) {
405 return;
406 }
407
Romain Guyc46d07a2013-03-15 19:06:39 -0700408 PathDescription entry(kShapePath, paint);
Derek Sollenbergeree248592015-02-12 14:10:21 -0500409 entry.shape.path.mGenerationID = path->getGenerationID();
Romain Guyc46d07a2013-03-15 19:06:39 -0700410
Romain Guyca89e2a2013-03-08 17:44:20 -0800411 PathTexture* texture = mCache.get(entry);
412
413 bool generate = false;
414 if (!texture) {
415 generate = true;
Romain Guyca89e2a2013-03-08 17:44:20 -0800416 }
417
418 if (generate) {
419 // It is important to specify the generation ID so we do not
420 // attempt to precache the same path several times
Chris Craike2bb3802015-03-13 15:07:52 -0700421 texture = new PathTexture(Caches::getInstance(), path->getGenerationID());
Romain Guy5dc7fa72013-03-11 20:48:31 -0700422 sp<PathTask> task = new PathTask(path, paint, texture);
423 texture->setTask(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800424
425 // During the precaching phase we insert path texture objects into
426 // the cache that do not point to any GL texture. They are instead
427 // treated as a task for the precaching worker thread. This is why
428 // we do not check the cache limit when inserting these objects.
429 // The conversion into GL texture will happen in get(), when a client
430 // asks for a path texture. This is also when the cache limit will
431 // be enforced.
432 mCache.put(entry, texture);
Romain Guy5dc7fa72013-03-11 20:48:31 -0700433
Chris Craikd41c4d82015-01-05 15:51:13 -0800434 if (mProcessor == nullptr) {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700435 mProcessor = new PathProcessor(Caches::getInstance());
436 }
Chris Craikdee66b62015-04-20 14:54:49 -0700437 mProcessor->add(task);
Romain Guyca89e2a2013-03-08 17:44:20 -0800438 }
439}
440
Romain Guyc46d07a2013-03-15 19:06:39 -0700441///////////////////////////////////////////////////////////////////////////////
442// Rounded rects
443///////////////////////////////////////////////////////////////////////////////
444
445PathTexture* PathCache::getRoundRect(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800446 float rx, float ry, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700447 PathDescription entry(kShapeRoundRect, paint);
448 entry.shape.roundRect.mWidth = width;
449 entry.shape.roundRect.mHeight = height;
450 entry.shape.roundRect.mRx = rx;
451 entry.shape.roundRect.mRy = ry;
452
453 PathTexture* texture = get(entry);
454
455 if (!texture) {
456 SkPath path;
457 SkRect r;
458 r.set(0.0f, 0.0f, width, height);
459 path.addRoundRect(r, rx, ry, SkPath::kCW_Direction);
460
461 texture = addTexture(entry, &path, paint);
462 }
463
464 return texture;
465}
466
467///////////////////////////////////////////////////////////////////////////////
468// Circles
469///////////////////////////////////////////////////////////////////////////////
470
Chris Craikd218a922014-01-02 17:13:34 -0800471PathTexture* PathCache::getCircle(float radius, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700472 PathDescription entry(kShapeCircle, paint);
473 entry.shape.circle.mRadius = radius;
474
475 PathTexture* texture = get(entry);
476
477 if (!texture) {
478 SkPath path;
479 path.addCircle(radius, radius, radius, SkPath::kCW_Direction);
480
481 texture = addTexture(entry, &path, paint);
482 }
483
484 return texture;
485}
486
487///////////////////////////////////////////////////////////////////////////////
488// Ovals
489///////////////////////////////////////////////////////////////////////////////
490
Chris Craikd218a922014-01-02 17:13:34 -0800491PathTexture* PathCache::getOval(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700492 PathDescription entry(kShapeOval, paint);
493 entry.shape.oval.mWidth = width;
494 entry.shape.oval.mHeight = height;
495
496 PathTexture* texture = get(entry);
497
498 if (!texture) {
499 SkPath path;
500 SkRect r;
501 r.set(0.0f, 0.0f, width, height);
502 path.addOval(r, SkPath::kCW_Direction);
503
504 texture = addTexture(entry, &path, paint);
505 }
506
507 return texture;
508}
509
510///////////////////////////////////////////////////////////////////////////////
511// Rects
512///////////////////////////////////////////////////////////////////////////////
513
Chris Craikd218a922014-01-02 17:13:34 -0800514PathTexture* PathCache::getRect(float width, float height, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700515 PathDescription entry(kShapeRect, paint);
516 entry.shape.rect.mWidth = width;
517 entry.shape.rect.mHeight = height;
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 path.addRect(r, SkPath::kCW_Direction);
526
527 texture = addTexture(entry, &path, paint);
528 }
529
530 return texture;
531}
532
533///////////////////////////////////////////////////////////////////////////////
534// Arcs
535///////////////////////////////////////////////////////////////////////////////
536
537PathTexture* PathCache::getArc(float width, float height,
Chris Craikd218a922014-01-02 17:13:34 -0800538 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Romain Guyc46d07a2013-03-15 19:06:39 -0700539 PathDescription entry(kShapeArc, paint);
540 entry.shape.arc.mWidth = width;
541 entry.shape.arc.mHeight = height;
542 entry.shape.arc.mStartAngle = startAngle;
543 entry.shape.arc.mSweepAngle = sweepAngle;
544 entry.shape.arc.mUseCenter = useCenter;
545
546 PathTexture* texture = get(entry);
547
548 if (!texture) {
549 SkPath path;
550 SkRect r;
551 r.set(0.0f, 0.0f, width, height);
552 if (useCenter) {
553 path.moveTo(r.centerX(), r.centerY());
554 }
555 path.arcTo(r, startAngle, sweepAngle, !useCenter);
556 if (useCenter) {
557 path.close();
558 }
559
560 texture = addTexture(entry, &path, paint);
561 }
562
563 return texture;
564}
565
Romain Guy7fbcc042010-08-04 15:40:07 -0700566}; // namespace uirenderer
567}; // namespace android