blob: 91e7ac39af90c2a9d83c68a6e6c8139e3d179f87 [file] [log] [blame]
Chris Craik05f3d6e2014-06-02 16:27:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
Chris Craik05f3d6e2014-06-02 16:27:04 -070017#include <utils/JenkinsHash.h>
18#include <utils/Trace.h>
19
20#include "Caches.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070021#include "PathTessellator.h"
22#include "ShadowTessellator.h"
23#include "TessellationCache.h"
24
25#include "thread/Signal.h"
26#include "thread/Task.h"
27#include "thread/TaskProcessor.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Cache entries
34///////////////////////////////////////////////////////////////////////////////
35
36TessellationCache::Description::Description()
sergeyv7224e2b2016-04-07 18:06:53 -070037 : type(Type::None)
Chris Craik6ac174b2014-06-17 13:47:05 -070038 , scaleX(1.0f)
39 , scaleY(1.0f)
Chris Craiked4ef0b2014-06-12 13:27:30 -070040 , aa(false)
Chris Craik05f3d6e2014-06-02 16:27:04 -070041 , cap(SkPaint::kDefault_Cap)
42 , style(SkPaint::kFill_Style)
43 , strokeWidth(1.0f) {
sergeyv7224e2b2016-04-07 18:06:53 -070044 // Shape bits should be set to zeroes, because they are used for hash calculation.
Chris Craik05f3d6e2014-06-02 16:27:04 -070045 memset(&shape, 0, sizeof(Shape));
46}
47
Chris Craik6ac174b2014-06-17 13:47:05 -070048TessellationCache::Description::Description(Type type, const Matrix4& transform, const SkPaint& paint)
Chris Craik05f3d6e2014-06-02 16:27:04 -070049 : type(type)
Chris Craik6ac174b2014-06-17 13:47:05 -070050 , aa(paint.isAntiAlias())
51 , cap(paint.getStrokeCap())
52 , style(paint.getStyle())
53 , strokeWidth(paint.getStrokeWidth()) {
54 PathTessellator::extractTessellationScales(transform, &scaleX, &scaleY);
sergeyv7224e2b2016-04-07 18:06:53 -070055 // Shape bits should be set to zeroes, because they are used for hash calculation.
Chris Craik05f3d6e2014-06-02 16:27:04 -070056 memset(&shape, 0, sizeof(Shape));
57}
58
sergeyv7224e2b2016-04-07 18:06:53 -070059bool TessellationCache::Description::operator==(const TessellationCache::Description& rhs) const {
60 if (type != rhs.type) return false;
61 if (scaleX != rhs.scaleX) return false;
62 if (scaleY != rhs.scaleY) return false;
63 if (aa != rhs.aa) return false;
64 if (cap != rhs.cap) return false;
65 if (style != rhs.style) return false;
66 if (strokeWidth != rhs.strokeWidth) return false;
67 if (type == Type::None) return true;
68 const Shape::RoundRect& lRect = shape.roundRect;
69 const Shape::RoundRect& rRect = rhs.shape.roundRect;
70
71 if (lRect.width != rRect.width) return false;
72 if (lRect.height != rRect.height) return false;
73 if (lRect.rx != rRect.rx) return false;
74 return lRect.ry == rRect.ry;
75}
76
Chris Craik05f3d6e2014-06-02 16:27:04 -070077hash_t TessellationCache::Description::hash() const {
sergeyv7224e2b2016-04-07 18:06:53 -070078 uint32_t hash = JenkinsHashMix(0, static_cast<int>(type));
Chris Craiked4ef0b2014-06-12 13:27:30 -070079 hash = JenkinsHashMix(hash, aa);
Chris Craik05f3d6e2014-06-02 16:27:04 -070080 hash = JenkinsHashMix(hash, cap);
81 hash = JenkinsHashMix(hash, style);
82 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
Chris Craik6ac174b2014-06-17 13:47:05 -070083 hash = JenkinsHashMix(hash, android::hash_type(scaleX));
84 hash = JenkinsHashMix(hash, android::hash_type(scaleY));
Chris Craik05f3d6e2014-06-02 16:27:04 -070085 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
86 return JenkinsHashWhiten(hash);
87}
88
Chris Craik6ac174b2014-06-17 13:47:05 -070089void TessellationCache::Description::setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const {
90 matrix->loadScale(scaleX, scaleY, 1.0f);
91 paint->setAntiAlias(aa);
92 paint->setStrokeCap(cap);
93 paint->setStyle(style);
94 paint->setStrokeWidth(strokeWidth);
95}
96
Chris Craik05f3d6e2014-06-02 16:27:04 -070097TessellationCache::ShadowDescription::ShadowDescription()
Chris Craikd41c4d82015-01-05 15:51:13 -080098 : nodeKey(nullptr) {
sergeyv7224e2b2016-04-07 18:06:53 -070099 memset(&matrixData, 0, sizeof(matrixData));
Chris Craik05f3d6e2014-06-02 16:27:04 -0700100}
101
sergeyv7224e2b2016-04-07 18:06:53 -0700102TessellationCache::ShadowDescription::ShadowDescription(const SkPath* nodeKey, const Matrix4* drawTransform)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700103 : nodeKey(nodeKey) {
sergeyv7224e2b2016-04-07 18:06:53 -0700104 memcpy(&matrixData, drawTransform->data, sizeof(matrixData));
105}
106
107bool TessellationCache::ShadowDescription::operator==(
108 const TessellationCache::ShadowDescription& rhs) const {
109 return nodeKey == rhs.nodeKey
110 && memcmp(&matrixData, &rhs.matrixData, sizeof(matrixData)) == 0;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700111}
112
113hash_t TessellationCache::ShadowDescription::hash() const {
114 uint32_t hash = JenkinsHashMixBytes(0, (uint8_t*) &nodeKey, sizeof(const void*));
sergeyv7224e2b2016-04-07 18:06:53 -0700115 hash = JenkinsHashMixBytes(hash, (uint8_t*) &matrixData, sizeof(matrixData));
Chris Craik05f3d6e2014-06-02 16:27:04 -0700116 return JenkinsHashWhiten(hash);
117}
118
119///////////////////////////////////////////////////////////////////////////////
120// General purpose tessellation task processing
121///////////////////////////////////////////////////////////////////////////////
122
123class TessellationCache::TessellationTask : public Task<VertexBuffer*> {
124public:
Chris Craik6ac174b2014-06-17 13:47:05 -0700125 TessellationTask(Tessellator tessellator, const Description& description)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700126 : tessellator(tessellator)
Chris Craik6ac174b2014-06-17 13:47:05 -0700127 , description(description) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700128 }
129
130 ~TessellationTask() {}
131
132 Tessellator tessellator;
133 Description description;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700134};
135
136class TessellationCache::TessellationProcessor : public TaskProcessor<VertexBuffer*> {
137public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700138 explicit TessellationProcessor(Caches& caches)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700139 : TaskProcessor<VertexBuffer*>(&caches.tasks) {}
140 ~TessellationProcessor() {}
141
Chris Craikd41c4d82015-01-05 15:51:13 -0800142 virtual void onProcess(const sp<Task<VertexBuffer*> >& task) override {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700143 TessellationTask* t = static_cast<TessellationTask*>(task.get());
144 ATRACE_NAME("shape tessellation");
Chris Craik6ac174b2014-06-17 13:47:05 -0700145 VertexBuffer* buffer = t->tessellator(t->description);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700146 t->setResult(buffer);
147 }
148};
149
John Reck1aa5d2d2014-07-24 13:38:28 -0700150class TessellationCache::Buffer {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700151public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700152 explicit Buffer(const sp<Task<VertexBuffer*> >& task)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700153 : mTask(task)
Chris Craikd41c4d82015-01-05 15:51:13 -0800154 , mBuffer(nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700155 }
156
157 ~Buffer() {
158 mTask.clear();
159 delete mBuffer;
160 }
161
162 unsigned int getSize() {
163 blockOnPrecache();
164 return mBuffer->getSize();
165 }
166
167 const VertexBuffer* getVertexBuffer() {
168 blockOnPrecache();
169 return mBuffer;
170 }
171
172private:
173 void blockOnPrecache() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800174 if (mTask != nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700175 mBuffer = mTask->getResult();
Chris Craikd41c4d82015-01-05 15:51:13 -0800176 LOG_ALWAYS_FATAL_IF(mBuffer == nullptr, "Failed to precache");
Chris Craik05f3d6e2014-06-02 16:27:04 -0700177 mTask.clear();
178 }
179 }
180 sp<Task<VertexBuffer*> > mTask;
181 VertexBuffer* mBuffer;
182};
183
184///////////////////////////////////////////////////////////////////////////////
185// Shadow tessellation task processing
186///////////////////////////////////////////////////////////////////////////////
187
Chris Craik05f3d6e2014-06-02 16:27:04 -0700188static void mapPointFakeZ(Vector3& point, const mat4* transformXY, const mat4* transformZ) {
189 // map z coordinate with true 3d matrix
190 point.z = transformZ->mapZ(point);
191
192 // map x,y coordinates with draw/Skia matrix
193 transformXY->mapPoint(point.x, point.y);
194}
195
Chris Craikfca52b752015-04-28 11:45:59 -0700196static void reverseVertexArray(Vertex* polygon, int len) {
197 int n = len / 2;
198 for (int i = 0; i < n; i++) {
199 Vertex tmp = polygon[i];
200 int k = len - 1 - i;
201 polygon[i] = polygon[k];
202 polygon[k] = tmp;
203 }
204}
205
John Reck82f5e0c2015-10-22 17:07:45 -0700206void tessellateShadows(
Chris Craik05f3d6e2014-06-02 16:27:04 -0700207 const Matrix4* drawTransform, const Rect* localClip,
208 bool isCasterOpaque, const SkPath* casterPerimeter,
209 const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
210 const Vector3& lightCenter, float lightRadius,
211 VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer) {
212
213 // tessellate caster outline into a 2d polygon
John Reck272a6852015-07-29 16:48:58 -0700214 std::vector<Vertex> casterVertices2d;
ztenghui21ef8202015-05-28 16:06:28 -0700215 const float casterRefinementThreshold = 2.0f;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700216 PathTessellator::approximatePathOutlineVertices(*casterPerimeter,
ztenghui21ef8202015-05-28 16:06:28 -0700217 casterRefinementThreshold, casterVertices2d);
Chris Craikfca52b752015-04-28 11:45:59 -0700218
219 // Shadow requires CCW for now. TODO: remove potential double-reverse
John Reck272a6852015-07-29 16:48:58 -0700220 reverseVertexArray(&casterVertices2d.front(), casterVertices2d.size());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700221
222 if (casterVertices2d.size() == 0) return;
223
224 // map 2d caster poly into 3d
225 const int casterVertexCount = casterVertices2d.size();
226 Vector3 casterPolygon[casterVertexCount];
227 float minZ = FLT_MAX;
228 float maxZ = -FLT_MAX;
229 for (int i = 0; i < casterVertexCount; i++) {
230 const Vertex& point2d = casterVertices2d[i];
John Reck1aa5d2d2014-07-24 13:38:28 -0700231 casterPolygon[i] = (Vector3){point2d.x, point2d.y, 0};
Chris Craik05f3d6e2014-06-02 16:27:04 -0700232 mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
Chris Craike6a15ee2015-07-07 18:42:17 -0700233 minZ = std::min(minZ, casterPolygon[i].z);
234 maxZ = std::max(maxZ, casterPolygon[i].z);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700235 }
236
237 // map the centroid of the caster into 3d
238 Vector2 centroid = ShadowTessellator::centroid2d(
John Reck272a6852015-07-29 16:48:58 -0700239 reinterpret_cast<const Vector2*>(&casterVertices2d.front()),
Chris Craik05f3d6e2014-06-02 16:27:04 -0700240 casterVertexCount);
John Reck1aa5d2d2014-07-24 13:38:28 -0700241 Vector3 centroid3d = {centroid.x, centroid.y, 0};
Chris Craik05f3d6e2014-06-02 16:27:04 -0700242 mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
243
244 // if the caster intersects the z=0 plane, lift it in Z so it doesn't
245 if (minZ < SHADOW_MIN_CASTER_Z) {
246 float casterLift = SHADOW_MIN_CASTER_Z - minZ;
247 for (int i = 0; i < casterVertexCount; i++) {
248 casterPolygon[i].z += casterLift;
249 }
250 centroid3d.z += casterLift;
251 }
252
253 // Check whether we want to draw the shadow at all by checking the caster's bounds against clip.
254 // We only have ortho projection, so we can just ignore the Z in caster for
255 // simple rejection calculation.
256 Rect casterBounds(casterPerimeter->getBounds());
257 casterTransformXY->mapRect(casterBounds);
258
259 // actual tessellation of both shadows
260 ShadowTessellator::tessellateAmbientShadow(
261 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
262 casterBounds, *localClip, maxZ, ambientBuffer);
263
264 ShadowTessellator::tessellateSpotShadow(
ztenghuic50a03d2014-08-21 13:47:54 -0700265 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
Chris Craik05f3d6e2014-06-02 16:27:04 -0700266 *drawTransform, lightCenter, lightRadius, casterBounds, *localClip,
267 spotBuffer);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700268}
269
Chris Craikd8165e82016-02-03 15:52:25 -0800270class ShadowProcessor : public TaskProcessor<TessellationCache::vertexBuffer_pair_t> {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700271public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700272 explicit ShadowProcessor(Caches& caches)
Chris Craikd8165e82016-02-03 15:52:25 -0800273 : TaskProcessor<TessellationCache::vertexBuffer_pair_t>(&caches.tasks) {}
Chris Craik05f3d6e2014-06-02 16:27:04 -0700274 ~ShadowProcessor() {}
275
Chris Craikd8165e82016-02-03 15:52:25 -0800276 virtual void onProcess(const sp<Task<TessellationCache::vertexBuffer_pair_t> >& task) override {
Chris Craik6e068c012016-01-15 16:15:30 -0800277 TessellationCache::ShadowTask* t = static_cast<TessellationCache::ShadowTask*>(task.get());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700278 ATRACE_NAME("shadow tessellation");
279
Chris Craik1b3be082014-06-11 13:44:19 -0700280 tessellateShadows(&t->drawTransform, &t->localClip, t->opaque, &t->casterPerimeter,
281 &t->transformXY, &t->transformZ, t->lightCenter, t->lightRadius,
Chris Craikd8165e82016-02-03 15:52:25 -0800282 t->ambientBuffer, t->spotBuffer);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700283
Chris Craikd8165e82016-02-03 15:52:25 -0800284 t->setResult(TessellationCache::vertexBuffer_pair_t(&t->ambientBuffer, &t->spotBuffer));
Chris Craik05f3d6e2014-06-02 16:27:04 -0700285 }
286};
287
288///////////////////////////////////////////////////////////////////////////////
289// Cache constructor/destructor
290///////////////////////////////////////////////////////////////////////////////
291
292TessellationCache::TessellationCache()
Chris Craik48a8f432016-02-05 15:59:29 -0800293 : mMaxSize(Properties::tessellationCacheSize)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700294 , mCache(LruCache<Description, Buffer*>::kUnlimitedCapacity)
295 , mShadowCache(LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*>::kUnlimitedCapacity) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700296 mCache.setOnEntryRemovedListener(&mBufferRemovedListener);
297 mShadowCache.setOnEntryRemovedListener(&mBufferPairRemovedListener);
Chris Craik2507c342015-05-04 14:36:49 -0700298 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700299}
300
301TessellationCache::~TessellationCache() {
302 mCache.clear();
303}
304
305///////////////////////////////////////////////////////////////////////////////
306// Size management
307///////////////////////////////////////////////////////////////////////////////
308
309uint32_t TessellationCache::getSize() {
310 LruCache<Description, Buffer*>::Iterator iter(mCache);
311 uint32_t size = 0;
312 while (iter.next()) {
313 size += iter.value()->getSize();
314 }
315 return size;
316}
317
318uint32_t TessellationCache::getMaxSize() {
319 return mMaxSize;
320}
321
Chris Craik05f3d6e2014-06-02 16:27:04 -0700322///////////////////////////////////////////////////////////////////////////////
323// Caching
324///////////////////////////////////////////////////////////////////////////////
325
326
327void TessellationCache::trim() {
328 uint32_t size = getSize();
329 while (size > mMaxSize) {
330 size -= mCache.peekOldestValue()->getSize();
331 mCache.removeOldest();
332 }
333 mShadowCache.clear();
334}
335
336void TessellationCache::clear() {
337 mCache.clear();
338 mShadowCache.clear();
339}
340
341///////////////////////////////////////////////////////////////////////////////
342// Callbacks
343///////////////////////////////////////////////////////////////////////////////
344
Andreas Gampe64bb4132014-11-22 00:35:09 +0000345void TessellationCache::BufferRemovedListener::operator()(Description& description,
Chris Craik05f3d6e2014-06-02 16:27:04 -0700346 Buffer*& buffer) {
347 delete buffer;
348}
349
350///////////////////////////////////////////////////////////////////////////////
351// Shadows
352///////////////////////////////////////////////////////////////////////////////
353
354void TessellationCache::precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
355 bool opaque, const SkPath* casterPerimeter,
356 const Matrix4* transformXY, const Matrix4* transformZ,
357 const Vector3& lightCenter, float lightRadius) {
358 ShadowDescription key(casterPerimeter, drawTransform);
359
Mykola Kondratenkob1596332015-03-12 15:20:38 +0100360 if (mShadowCache.get(key)) return;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700361 sp<ShadowTask> task = new ShadowTask(drawTransform, localClip, opaque,
362 casterPerimeter, transformXY, transformZ, lightCenter, lightRadius);
Chris Craikd41c4d82015-01-05 15:51:13 -0800363 if (mShadowProcessor == nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700364 mShadowProcessor = new ShadowProcessor(Caches::getInstance());
365 }
Chris Craikdee66b62015-04-20 14:54:49 -0700366 mShadowProcessor->add(task);
Chris Craikd41c4d82015-01-05 15:51:13 -0800367 task->incStrong(nullptr); // not using sp<>s, so manually ref while in the cache
Chris Craik05f3d6e2014-06-02 16:27:04 -0700368 mShadowCache.put(key, task.get());
369}
370
Chris Craik6e068c012016-01-15 16:15:30 -0800371sp<TessellationCache::ShadowTask> TessellationCache::getShadowTask(
372 const Matrix4* drawTransform, const Rect& localClip,
373 bool opaque, const SkPath* casterPerimeter,
374 const Matrix4* transformXY, const Matrix4* transformZ,
375 const Vector3& lightCenter, float lightRadius) {
376 ShadowDescription key(casterPerimeter, drawTransform);
377 ShadowTask* task = static_cast<ShadowTask*>(mShadowCache.get(key));
378 if (!task) {
379 precacheShadows(drawTransform, localClip, opaque, casterPerimeter,
380 transformXY, transformZ, lightCenter, lightRadius);
381 task = static_cast<ShadowTask*>(mShadowCache.get(key));
382 }
383 LOG_ALWAYS_FATAL_IF(task == nullptr, "shadow not precached");
384 return task;
385}
386
Chris Craik05f3d6e2014-06-02 16:27:04 -0700387///////////////////////////////////////////////////////////////////////////////
388// Tessellation precaching
389///////////////////////////////////////////////////////////////////////////////
390
Chris Craik05f3d6e2014-06-02 16:27:04 -0700391TessellationCache::Buffer* TessellationCache::getOrCreateBuffer(
Chris Craik6ac174b2014-06-17 13:47:05 -0700392 const Description& entry, Tessellator tessellator) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700393 Buffer* buffer = mCache.get(entry);
394 if (!buffer) {
395 // not cached, enqueue a task to fill the buffer
Chris Craik6ac174b2014-06-17 13:47:05 -0700396 sp<TessellationTask> task = new TessellationTask(tessellator, entry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700397 buffer = new Buffer(task);
398
Chris Craikd41c4d82015-01-05 15:51:13 -0800399 if (mProcessor == nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700400 mProcessor = new TessellationProcessor(Caches::getInstance());
401 }
Chris Craikdee66b62015-04-20 14:54:49 -0700402 mProcessor->add(task);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700403 mCache.put(entry, buffer);
404 }
405 return buffer;
406}
407
Chris Craik6ac174b2014-06-17 13:47:05 -0700408static VertexBuffer* tessellatePath(const TessellationCache::Description& description,
409 const SkPath& path) {
410 Matrix4 matrix;
411 SkPaint paint;
412 description.setupMatrixAndPaint(&matrix, &paint);
413 VertexBuffer* buffer = new VertexBuffer();
414 PathTessellator::tessellatePath(path, &paint, matrix, *buffer);
415 return buffer;
416}
417
Chris Craik05f3d6e2014-06-02 16:27:04 -0700418///////////////////////////////////////////////////////////////////////////////
Chris Craik6ac174b2014-06-17 13:47:05 -0700419// RoundRect
Chris Craik05f3d6e2014-06-02 16:27:04 -0700420///////////////////////////////////////////////////////////////////////////////
421
Chris Craik6ac174b2014-06-17 13:47:05 -0700422static VertexBuffer* tessellateRoundRect(const TessellationCache::Description& description) {
423 SkRect rect = SkRect::MakeWH(description.shape.roundRect.width,
424 description.shape.roundRect.height);
425 float rx = description.shape.roundRect.rx;
426 float ry = description.shape.roundRect.ry;
427 if (description.style == SkPaint::kStrokeAndFill_Style) {
428 float outset = description.strokeWidth / 2;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700429 rect.outset(outset, outset);
430 rx += outset;
431 ry += outset;
432 }
433 SkPath path;
434 path.addRoundRect(rect, rx, ry);
Chris Craik6ac174b2014-06-17 13:47:05 -0700435 return tessellatePath(description, path);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700436}
437
Chris Craik6ac174b2014-06-17 13:47:05 -0700438TessellationCache::Buffer* TessellationCache::getRoundRectBuffer(
439 const Matrix4& transform, const SkPaint& paint,
440 float width, float height, float rx, float ry) {
sergeyv7224e2b2016-04-07 18:06:53 -0700441 Description entry(Description::Type::RoundRect, transform, paint);
Chris Craik6ac174b2014-06-17 13:47:05 -0700442 entry.shape.roundRect.width = width;
443 entry.shape.roundRect.height = height;
444 entry.shape.roundRect.rx = rx;
445 entry.shape.roundRect.ry = ry;
446 return getOrCreateBuffer(entry, &tessellateRoundRect);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700447}
Chris Craik6ac174b2014-06-17 13:47:05 -0700448const VertexBuffer* TessellationCache::getRoundRect(const Matrix4& transform, const SkPaint& paint,
449 float width, float height, float rx, float ry) {
450 return getRoundRectBuffer(transform, paint, width, height, rx, ry)->getVertexBuffer();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700451}
452
453}; // namespace uirenderer
454}; // namespace android