blob: cfdc0848c8b0be9b5fae572db10542882142de83 [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"
21#include "OpenGLRenderer.h"
22#include "PathTessellator.h"
23#include "ShadowTessellator.h"
24#include "TessellationCache.h"
25
26#include "thread/Signal.h"
27#include "thread/Task.h"
28#include "thread/TaskProcessor.h"
29
30namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Cache entries
35///////////////////////////////////////////////////////////////////////////////
36
37TessellationCache::Description::Description()
sergeyv7224e2b2016-04-07 18:06:53 -070038 : type(Type::None)
Chris Craik6ac174b2014-06-17 13:47:05 -070039 , scaleX(1.0f)
40 , scaleY(1.0f)
Chris Craiked4ef0b2014-06-12 13:27:30 -070041 , aa(false)
Chris Craik05f3d6e2014-06-02 16:27:04 -070042 , cap(SkPaint::kDefault_Cap)
43 , style(SkPaint::kFill_Style)
44 , strokeWidth(1.0f) {
sergeyv7224e2b2016-04-07 18:06:53 -070045 // Shape bits should be set to zeroes, because they are used for hash calculation.
Chris Craik05f3d6e2014-06-02 16:27:04 -070046 memset(&shape, 0, sizeof(Shape));
47}
48
Chris Craik6ac174b2014-06-17 13:47:05 -070049TessellationCache::Description::Description(Type type, const Matrix4& transform, const SkPaint& paint)
Chris Craik05f3d6e2014-06-02 16:27:04 -070050 : type(type)
Chris Craik6ac174b2014-06-17 13:47:05 -070051 , aa(paint.isAntiAlias())
52 , cap(paint.getStrokeCap())
53 , style(paint.getStyle())
54 , strokeWidth(paint.getStrokeWidth()) {
55 PathTessellator::extractTessellationScales(transform, &scaleX, &scaleY);
sergeyv7224e2b2016-04-07 18:06:53 -070056 // Shape bits should be set to zeroes, because they are used for hash calculation.
Chris Craik05f3d6e2014-06-02 16:27:04 -070057 memset(&shape, 0, sizeof(Shape));
58}
59
sergeyv7224e2b2016-04-07 18:06:53 -070060bool TessellationCache::Description::operator==(const TessellationCache::Description& rhs) const {
61 if (type != rhs.type) return false;
62 if (scaleX != rhs.scaleX) return false;
63 if (scaleY != rhs.scaleY) return false;
64 if (aa != rhs.aa) return false;
65 if (cap != rhs.cap) return false;
66 if (style != rhs.style) return false;
67 if (strokeWidth != rhs.strokeWidth) return false;
68 if (type == Type::None) return true;
69 const Shape::RoundRect& lRect = shape.roundRect;
70 const Shape::RoundRect& rRect = rhs.shape.roundRect;
71
72 if (lRect.width != rRect.width) return false;
73 if (lRect.height != rRect.height) return false;
74 if (lRect.rx != rRect.rx) return false;
75 return lRect.ry == rRect.ry;
76}
77
Chris Craik05f3d6e2014-06-02 16:27:04 -070078hash_t TessellationCache::Description::hash() const {
sergeyv7224e2b2016-04-07 18:06:53 -070079 uint32_t hash = JenkinsHashMix(0, static_cast<int>(type));
Chris Craiked4ef0b2014-06-12 13:27:30 -070080 hash = JenkinsHashMix(hash, aa);
Chris Craik05f3d6e2014-06-02 16:27:04 -070081 hash = JenkinsHashMix(hash, cap);
82 hash = JenkinsHashMix(hash, style);
83 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
Chris Craik6ac174b2014-06-17 13:47:05 -070084 hash = JenkinsHashMix(hash, android::hash_type(scaleX));
85 hash = JenkinsHashMix(hash, android::hash_type(scaleY));
Chris Craik05f3d6e2014-06-02 16:27:04 -070086 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
87 return JenkinsHashWhiten(hash);
88}
89
Chris Craik6ac174b2014-06-17 13:47:05 -070090void TessellationCache::Description::setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const {
91 matrix->loadScale(scaleX, scaleY, 1.0f);
92 paint->setAntiAlias(aa);
93 paint->setStrokeCap(cap);
94 paint->setStyle(style);
95 paint->setStrokeWidth(strokeWidth);
96}
97
Chris Craik05f3d6e2014-06-02 16:27:04 -070098TessellationCache::ShadowDescription::ShadowDescription()
Chris Craikd41c4d82015-01-05 15:51:13 -080099 : nodeKey(nullptr) {
sergeyv7224e2b2016-04-07 18:06:53 -0700100 memset(&matrixData, 0, sizeof(matrixData));
Chris Craik05f3d6e2014-06-02 16:27:04 -0700101}
102
sergeyv7224e2b2016-04-07 18:06:53 -0700103TessellationCache::ShadowDescription::ShadowDescription(const SkPath* nodeKey, const Matrix4* drawTransform)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700104 : nodeKey(nodeKey) {
sergeyv7224e2b2016-04-07 18:06:53 -0700105 memcpy(&matrixData, drawTransform->data, sizeof(matrixData));
106}
107
108bool TessellationCache::ShadowDescription::operator==(
109 const TessellationCache::ShadowDescription& rhs) const {
110 return nodeKey == rhs.nodeKey
111 && memcmp(&matrixData, &rhs.matrixData, sizeof(matrixData)) == 0;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700112}
113
114hash_t TessellationCache::ShadowDescription::hash() const {
115 uint32_t hash = JenkinsHashMixBytes(0, (uint8_t*) &nodeKey, sizeof(const void*));
sergeyv7224e2b2016-04-07 18:06:53 -0700116 hash = JenkinsHashMixBytes(hash, (uint8_t*) &matrixData, sizeof(matrixData));
Chris Craik05f3d6e2014-06-02 16:27:04 -0700117 return JenkinsHashWhiten(hash);
118}
119
120///////////////////////////////////////////////////////////////////////////////
121// General purpose tessellation task processing
122///////////////////////////////////////////////////////////////////////////////
123
124class TessellationCache::TessellationTask : public Task<VertexBuffer*> {
125public:
Chris Craik6ac174b2014-06-17 13:47:05 -0700126 TessellationTask(Tessellator tessellator, const Description& description)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700127 : tessellator(tessellator)
Chris Craik6ac174b2014-06-17 13:47:05 -0700128 , description(description) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700129 }
130
131 ~TessellationTask() {}
132
133 Tessellator tessellator;
134 Description description;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700135};
136
137class TessellationCache::TessellationProcessor : public TaskProcessor<VertexBuffer*> {
138public:
139 TessellationProcessor(Caches& caches)
140 : TaskProcessor<VertexBuffer*>(&caches.tasks) {}
141 ~TessellationProcessor() {}
142
Chris Craikd41c4d82015-01-05 15:51:13 -0800143 virtual void onProcess(const sp<Task<VertexBuffer*> >& task) override {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700144 TessellationTask* t = static_cast<TessellationTask*>(task.get());
145 ATRACE_NAME("shape tessellation");
Chris Craik6ac174b2014-06-17 13:47:05 -0700146 VertexBuffer* buffer = t->tessellator(t->description);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700147 t->setResult(buffer);
148 }
149};
150
John Reck1aa5d2d2014-07-24 13:38:28 -0700151class TessellationCache::Buffer {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700152public:
153 Buffer(const sp<Task<VertexBuffer*> >& task)
154 : mTask(task)
Chris Craikd41c4d82015-01-05 15:51:13 -0800155 , mBuffer(nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700156 }
157
158 ~Buffer() {
159 mTask.clear();
160 delete mBuffer;
161 }
162
163 unsigned int getSize() {
164 blockOnPrecache();
165 return mBuffer->getSize();
166 }
167
168 const VertexBuffer* getVertexBuffer() {
169 blockOnPrecache();
170 return mBuffer;
171 }
172
173private:
174 void blockOnPrecache() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800175 if (mTask != nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700176 mBuffer = mTask->getResult();
Chris Craikd41c4d82015-01-05 15:51:13 -0800177 LOG_ALWAYS_FATAL_IF(mBuffer == nullptr, "Failed to precache");
Chris Craik05f3d6e2014-06-02 16:27:04 -0700178 mTask.clear();
179 }
180 }
181 sp<Task<VertexBuffer*> > mTask;
182 VertexBuffer* mBuffer;
183};
184
185///////////////////////////////////////////////////////////////////////////////
186// Shadow tessellation task processing
187///////////////////////////////////////////////////////////////////////////////
188
Chris Craik05f3d6e2014-06-02 16:27:04 -0700189static void mapPointFakeZ(Vector3& point, const mat4* transformXY, const mat4* transformZ) {
190 // map z coordinate with true 3d matrix
191 point.z = transformZ->mapZ(point);
192
193 // map x,y coordinates with draw/Skia matrix
194 transformXY->mapPoint(point.x, point.y);
195}
196
Chris Craikfca52b752015-04-28 11:45:59 -0700197static void reverseVertexArray(Vertex* polygon, int len) {
198 int n = len / 2;
199 for (int i = 0; i < n; i++) {
200 Vertex tmp = polygon[i];
201 int k = len - 1 - i;
202 polygon[i] = polygon[k];
203 polygon[k] = tmp;
204 }
205}
206
John Reck82f5e0c2015-10-22 17:07:45 -0700207void tessellateShadows(
Chris Craik05f3d6e2014-06-02 16:27:04 -0700208 const Matrix4* drawTransform, const Rect* localClip,
209 bool isCasterOpaque, const SkPath* casterPerimeter,
210 const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
211 const Vector3& lightCenter, float lightRadius,
212 VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer) {
213
214 // tessellate caster outline into a 2d polygon
John Reck272a6852015-07-29 16:48:58 -0700215 std::vector<Vertex> casterVertices2d;
ztenghui21ef8202015-05-28 16:06:28 -0700216 const float casterRefinementThreshold = 2.0f;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700217 PathTessellator::approximatePathOutlineVertices(*casterPerimeter,
ztenghui21ef8202015-05-28 16:06:28 -0700218 casterRefinementThreshold, casterVertices2d);
Chris Craikfca52b752015-04-28 11:45:59 -0700219
220 // Shadow requires CCW for now. TODO: remove potential double-reverse
John Reck272a6852015-07-29 16:48:58 -0700221 reverseVertexArray(&casterVertices2d.front(), casterVertices2d.size());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700222
223 if (casterVertices2d.size() == 0) return;
224
225 // map 2d caster poly into 3d
226 const int casterVertexCount = casterVertices2d.size();
227 Vector3 casterPolygon[casterVertexCount];
228 float minZ = FLT_MAX;
229 float maxZ = -FLT_MAX;
230 for (int i = 0; i < casterVertexCount; i++) {
231 const Vertex& point2d = casterVertices2d[i];
John Reck1aa5d2d2014-07-24 13:38:28 -0700232 casterPolygon[i] = (Vector3){point2d.x, point2d.y, 0};
Chris Craik05f3d6e2014-06-02 16:27:04 -0700233 mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
Chris Craike6a15ee2015-07-07 18:42:17 -0700234 minZ = std::min(minZ, casterPolygon[i].z);
235 maxZ = std::max(maxZ, casterPolygon[i].z);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700236 }
237
238 // map the centroid of the caster into 3d
239 Vector2 centroid = ShadowTessellator::centroid2d(
John Reck272a6852015-07-29 16:48:58 -0700240 reinterpret_cast<const Vector2*>(&casterVertices2d.front()),
Chris Craik05f3d6e2014-06-02 16:27:04 -0700241 casterVertexCount);
John Reck1aa5d2d2014-07-24 13:38:28 -0700242 Vector3 centroid3d = {centroid.x, centroid.y, 0};
Chris Craik05f3d6e2014-06-02 16:27:04 -0700243 mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
244
245 // if the caster intersects the z=0 plane, lift it in Z so it doesn't
246 if (minZ < SHADOW_MIN_CASTER_Z) {
247 float casterLift = SHADOW_MIN_CASTER_Z - minZ;
248 for (int i = 0; i < casterVertexCount; i++) {
249 casterPolygon[i].z += casterLift;
250 }
251 centroid3d.z += casterLift;
252 }
253
254 // Check whether we want to draw the shadow at all by checking the caster's bounds against clip.
255 // We only have ortho projection, so we can just ignore the Z in caster for
256 // simple rejection calculation.
257 Rect casterBounds(casterPerimeter->getBounds());
258 casterTransformXY->mapRect(casterBounds);
259
260 // actual tessellation of both shadows
261 ShadowTessellator::tessellateAmbientShadow(
262 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
263 casterBounds, *localClip, maxZ, ambientBuffer);
264
265 ShadowTessellator::tessellateSpotShadow(
ztenghuic50a03d2014-08-21 13:47:54 -0700266 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
Chris Craik05f3d6e2014-06-02 16:27:04 -0700267 *drawTransform, lightCenter, lightRadius, casterBounds, *localClip,
268 spotBuffer);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700269}
270
Chris Craikd8165e82016-02-03 15:52:25 -0800271class ShadowProcessor : public TaskProcessor<TessellationCache::vertexBuffer_pair_t> {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700272public:
273 ShadowProcessor(Caches& caches)
Chris Craikd8165e82016-02-03 15:52:25 -0800274 : TaskProcessor<TessellationCache::vertexBuffer_pair_t>(&caches.tasks) {}
Chris Craik05f3d6e2014-06-02 16:27:04 -0700275 ~ShadowProcessor() {}
276
Chris Craikd8165e82016-02-03 15:52:25 -0800277 virtual void onProcess(const sp<Task<TessellationCache::vertexBuffer_pair_t> >& task) override {
Chris Craik6e068c012016-01-15 16:15:30 -0800278 TessellationCache::ShadowTask* t = static_cast<TessellationCache::ShadowTask*>(task.get());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700279 ATRACE_NAME("shadow tessellation");
280
Chris Craik1b3be082014-06-11 13:44:19 -0700281 tessellateShadows(&t->drawTransform, &t->localClip, t->opaque, &t->casterPerimeter,
282 &t->transformXY, &t->transformZ, t->lightCenter, t->lightRadius,
Chris Craikd8165e82016-02-03 15:52:25 -0800283 t->ambientBuffer, t->spotBuffer);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700284
Chris Craikd8165e82016-02-03 15:52:25 -0800285 t->setResult(TessellationCache::vertexBuffer_pair_t(&t->ambientBuffer, &t->spotBuffer));
Chris Craik05f3d6e2014-06-02 16:27:04 -0700286 }
287};
288
289///////////////////////////////////////////////////////////////////////////////
290// Cache constructor/destructor
291///////////////////////////////////////////////////////////////////////////////
292
293TessellationCache::TessellationCache()
Chris Craik48a8f432016-02-05 15:59:29 -0800294 : mMaxSize(Properties::tessellationCacheSize)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700295 , mCache(LruCache<Description, Buffer*>::kUnlimitedCapacity)
296 , mShadowCache(LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*>::kUnlimitedCapacity) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700297 mCache.setOnEntryRemovedListener(&mBufferRemovedListener);
298 mShadowCache.setOnEntryRemovedListener(&mBufferPairRemovedListener);
Chris Craik2507c342015-05-04 14:36:49 -0700299 mDebugEnabled = Properties::debugLevel & kDebugCaches;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700300}
301
302TessellationCache::~TessellationCache() {
303 mCache.clear();
304}
305
306///////////////////////////////////////////////////////////////////////////////
307// Size management
308///////////////////////////////////////////////////////////////////////////////
309
310uint32_t TessellationCache::getSize() {
311 LruCache<Description, Buffer*>::Iterator iter(mCache);
312 uint32_t size = 0;
313 while (iter.next()) {
314 size += iter.value()->getSize();
315 }
316 return size;
317}
318
319uint32_t TessellationCache::getMaxSize() {
320 return mMaxSize;
321}
322
Chris Craik05f3d6e2014-06-02 16:27:04 -0700323///////////////////////////////////////////////////////////////////////////////
324// Caching
325///////////////////////////////////////////////////////////////////////////////
326
327
328void TessellationCache::trim() {
329 uint32_t size = getSize();
330 while (size > mMaxSize) {
331 size -= mCache.peekOldestValue()->getSize();
332 mCache.removeOldest();
333 }
334 mShadowCache.clear();
335}
336
337void TessellationCache::clear() {
338 mCache.clear();
339 mShadowCache.clear();
340}
341
342///////////////////////////////////////////////////////////////////////////////
343// Callbacks
344///////////////////////////////////////////////////////////////////////////////
345
Andreas Gampe64bb4132014-11-22 00:35:09 +0000346void TessellationCache::BufferRemovedListener::operator()(Description& description,
Chris Craik05f3d6e2014-06-02 16:27:04 -0700347 Buffer*& buffer) {
348 delete buffer;
349}
350
351///////////////////////////////////////////////////////////////////////////////
352// Shadows
353///////////////////////////////////////////////////////////////////////////////
354
355void TessellationCache::precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
356 bool opaque, const SkPath* casterPerimeter,
357 const Matrix4* transformXY, const Matrix4* transformZ,
358 const Vector3& lightCenter, float lightRadius) {
359 ShadowDescription key(casterPerimeter, drawTransform);
360
Mykola Kondratenkob1596332015-03-12 15:20:38 +0100361 if (mShadowCache.get(key)) return;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700362 sp<ShadowTask> task = new ShadowTask(drawTransform, localClip, opaque,
363 casterPerimeter, transformXY, transformZ, lightCenter, lightRadius);
Chris Craikd41c4d82015-01-05 15:51:13 -0800364 if (mShadowProcessor == nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700365 mShadowProcessor = new ShadowProcessor(Caches::getInstance());
366 }
Chris Craikdee66b62015-04-20 14:54:49 -0700367 mShadowProcessor->add(task);
Chris Craikd41c4d82015-01-05 15:51:13 -0800368 task->incStrong(nullptr); // not using sp<>s, so manually ref while in the cache
Chris Craik05f3d6e2014-06-02 16:27:04 -0700369 mShadowCache.put(key, task.get());
370}
371
372void TessellationCache::getShadowBuffers(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, vertexBuffer_pair_t& outBuffers) {
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 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800383 LOG_ALWAYS_FATAL_IF(task == nullptr, "shadow not precached");
Chris Craikd8165e82016-02-03 15:52:25 -0800384 outBuffers = task->getResult();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700385}
386
Chris Craik6e068c012016-01-15 16:15:30 -0800387sp<TessellationCache::ShadowTask> TessellationCache::getShadowTask(
388 const Matrix4* drawTransform, const Rect& localClip,
389 bool opaque, const SkPath* casterPerimeter,
390 const Matrix4* transformXY, const Matrix4* transformZ,
391 const Vector3& lightCenter, float lightRadius) {
392 ShadowDescription key(casterPerimeter, drawTransform);
393 ShadowTask* task = static_cast<ShadowTask*>(mShadowCache.get(key));
394 if (!task) {
395 precacheShadows(drawTransform, localClip, opaque, casterPerimeter,
396 transformXY, transformZ, lightCenter, lightRadius);
397 task = static_cast<ShadowTask*>(mShadowCache.get(key));
398 }
399 LOG_ALWAYS_FATAL_IF(task == nullptr, "shadow not precached");
400 return task;
401}
402
Chris Craik05f3d6e2014-06-02 16:27:04 -0700403///////////////////////////////////////////////////////////////////////////////
404// Tessellation precaching
405///////////////////////////////////////////////////////////////////////////////
406
Chris Craik05f3d6e2014-06-02 16:27:04 -0700407TessellationCache::Buffer* TessellationCache::getOrCreateBuffer(
Chris Craik6ac174b2014-06-17 13:47:05 -0700408 const Description& entry, Tessellator tessellator) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700409 Buffer* buffer = mCache.get(entry);
410 if (!buffer) {
411 // not cached, enqueue a task to fill the buffer
Chris Craik6ac174b2014-06-17 13:47:05 -0700412 sp<TessellationTask> task = new TessellationTask(tessellator, entry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700413 buffer = new Buffer(task);
414
Chris Craikd41c4d82015-01-05 15:51:13 -0800415 if (mProcessor == nullptr) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700416 mProcessor = new TessellationProcessor(Caches::getInstance());
417 }
Chris Craikdee66b62015-04-20 14:54:49 -0700418 mProcessor->add(task);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700419 mCache.put(entry, buffer);
420 }
421 return buffer;
422}
423
Chris Craik6ac174b2014-06-17 13:47:05 -0700424static VertexBuffer* tessellatePath(const TessellationCache::Description& description,
425 const SkPath& path) {
426 Matrix4 matrix;
427 SkPaint paint;
428 description.setupMatrixAndPaint(&matrix, &paint);
429 VertexBuffer* buffer = new VertexBuffer();
430 PathTessellator::tessellatePath(path, &paint, matrix, *buffer);
431 return buffer;
432}
433
Chris Craik05f3d6e2014-06-02 16:27:04 -0700434///////////////////////////////////////////////////////////////////////////////
Chris Craik6ac174b2014-06-17 13:47:05 -0700435// RoundRect
Chris Craik05f3d6e2014-06-02 16:27:04 -0700436///////////////////////////////////////////////////////////////////////////////
437
Chris Craik6ac174b2014-06-17 13:47:05 -0700438static VertexBuffer* tessellateRoundRect(const TessellationCache::Description& description) {
439 SkRect rect = SkRect::MakeWH(description.shape.roundRect.width,
440 description.shape.roundRect.height);
441 float rx = description.shape.roundRect.rx;
442 float ry = description.shape.roundRect.ry;
443 if (description.style == SkPaint::kStrokeAndFill_Style) {
444 float outset = description.strokeWidth / 2;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700445 rect.outset(outset, outset);
446 rx += outset;
447 ry += outset;
448 }
449 SkPath path;
450 path.addRoundRect(rect, rx, ry);
Chris Craik6ac174b2014-06-17 13:47:05 -0700451 return tessellatePath(description, path);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700452}
453
Chris Craik6ac174b2014-06-17 13:47:05 -0700454TessellationCache::Buffer* TessellationCache::getRoundRectBuffer(
455 const Matrix4& transform, const SkPaint& paint,
456 float width, float height, float rx, float ry) {
sergeyv7224e2b2016-04-07 18:06:53 -0700457 Description entry(Description::Type::RoundRect, transform, paint);
Chris Craik6ac174b2014-06-17 13:47:05 -0700458 entry.shape.roundRect.width = width;
459 entry.shape.roundRect.height = height;
460 entry.shape.roundRect.rx = rx;
461 entry.shape.roundRect.ry = ry;
462 return getOrCreateBuffer(entry, &tessellateRoundRect);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700463}
Chris Craik6ac174b2014-06-17 13:47:05 -0700464const VertexBuffer* TessellationCache::getRoundRect(const Matrix4& transform, const SkPaint& paint,
465 float width, float height, float rx, float ry) {
466 return getRoundRectBuffer(transform, paint, width, height, rx, ry)->getVertexBuffer();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700467}
468
469}; // namespace uirenderer
470}; // namespace android