2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <utils/JenkinsHash.h>
18 #include <utils/Trace.h>
21 #include "OpenGLRenderer.h"
22 #include "PathTessellator.h"
23 #include "ShadowTessellator.h"
24 #include "TessellationCache.h"
26 #include "thread/Signal.h"
27 #include "thread/Task.h"
28 #include "thread/TaskProcessor.h"
31 namespace uirenderer {
33 ///////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
37 TessellationCache::Description::Description()
42 , cap(SkPaint::kDefault_Cap)
43 , style(SkPaint::kFill_Style)
45 // Shape bits should be set to zeroes, because they are used for hash calculation.
46 memset(&shape, 0, sizeof(Shape));
49 TessellationCache::Description::Description(Type type, const Matrix4& transform, const SkPaint& paint)
51 , aa(paint.isAntiAlias())
52 , cap(paint.getStrokeCap())
53 , style(paint.getStyle())
54 , strokeWidth(paint.getStrokeWidth()) {
55 PathTessellator::extractTessellationScales(transform, &scaleX, &scaleY);
56 // Shape bits should be set to zeroes, because they are used for hash calculation.
57 memset(&shape, 0, sizeof(Shape));
60 bool 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;
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;
78 hash_t TessellationCache::Description::hash() const {
79 uint32_t hash = JenkinsHashMix(0, static_cast<int>(type));
80 hash = JenkinsHashMix(hash, aa);
81 hash = JenkinsHashMix(hash, cap);
82 hash = JenkinsHashMix(hash, style);
83 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
84 hash = JenkinsHashMix(hash, android::hash_type(scaleX));
85 hash = JenkinsHashMix(hash, android::hash_type(scaleY));
86 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
87 return JenkinsHashWhiten(hash);
90 void 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);
98 TessellationCache::ShadowDescription::ShadowDescription()
100 memset(&matrixData, 0, sizeof(matrixData));
103 TessellationCache::ShadowDescription::ShadowDescription(const SkPath* nodeKey, const Matrix4* drawTransform)
105 memcpy(&matrixData, drawTransform->data, sizeof(matrixData));
108 bool TessellationCache::ShadowDescription::operator==(
109 const TessellationCache::ShadowDescription& rhs) const {
110 return nodeKey == rhs.nodeKey
111 && memcmp(&matrixData, &rhs.matrixData, sizeof(matrixData)) == 0;
114 hash_t TessellationCache::ShadowDescription::hash() const {
115 uint32_t hash = JenkinsHashMixBytes(0, (uint8_t*) &nodeKey, sizeof(const void*));
116 hash = JenkinsHashMixBytes(hash, (uint8_t*) &matrixData, sizeof(matrixData));
117 return JenkinsHashWhiten(hash);
120 ///////////////////////////////////////////////////////////////////////////////
121 // General purpose tessellation task processing
122 ///////////////////////////////////////////////////////////////////////////////
124 class TessellationCache::TessellationTask : public Task<VertexBuffer*> {
126 TessellationTask(Tessellator tessellator, const Description& description)
127 : tessellator(tessellator)
128 , description(description) {
131 ~TessellationTask() {}
133 Tessellator tessellator;
134 Description description;
137 class TessellationCache::TessellationProcessor : public TaskProcessor<VertexBuffer*> {
139 TessellationProcessor(Caches& caches)
140 : TaskProcessor<VertexBuffer*>(&caches.tasks) {}
141 ~TessellationProcessor() {}
143 virtual void onProcess(const sp<Task<VertexBuffer*> >& task) override {
144 TessellationTask* t = static_cast<TessellationTask*>(task.get());
145 ATRACE_NAME("shape tessellation");
146 VertexBuffer* buffer = t->tessellator(t->description);
147 t->setResult(buffer);
151 class TessellationCache::Buffer {
153 Buffer(const sp<Task<VertexBuffer*> >& task)
163 unsigned int getSize() {
165 return mBuffer->getSize();
168 const VertexBuffer* getVertexBuffer() {
174 void blockOnPrecache() {
175 if (mTask != nullptr) {
176 mBuffer = mTask->getResult();
177 LOG_ALWAYS_FATAL_IF(mBuffer == nullptr, "Failed to precache");
181 sp<Task<VertexBuffer*> > mTask;
182 VertexBuffer* mBuffer;
185 ///////////////////////////////////////////////////////////////////////////////
186 // Shadow tessellation task processing
187 ///////////////////////////////////////////////////////////////////////////////
189 static void mapPointFakeZ(Vector3& point, const mat4* transformXY, const mat4* transformZ) {
190 // map z coordinate with true 3d matrix
191 point.z = transformZ->mapZ(point);
193 // map x,y coordinates with draw/Skia matrix
194 transformXY->mapPoint(point.x, point.y);
197 static void reverseVertexArray(Vertex* polygon, int len) {
199 for (int i = 0; i < n; i++) {
200 Vertex tmp = polygon[i];
202 polygon[i] = polygon[k];
207 void tessellateShadows(
208 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) {
214 // tessellate caster outline into a 2d polygon
215 std::vector<Vertex> casterVertices2d;
216 const float casterRefinementThreshold = 2.0f;
217 PathTessellator::approximatePathOutlineVertices(*casterPerimeter,
218 casterRefinementThreshold, casterVertices2d);
220 // Shadow requires CCW for now. TODO: remove potential double-reverse
221 reverseVertexArray(&casterVertices2d.front(), casterVertices2d.size());
223 if (casterVertices2d.size() == 0) return;
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];
232 casterPolygon[i] = (Vector3){point2d.x, point2d.y, 0};
233 mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
234 minZ = std::min(minZ, casterPolygon[i].z);
235 maxZ = std::max(maxZ, casterPolygon[i].z);
238 // map the centroid of the caster into 3d
239 Vector2 centroid = ShadowTessellator::centroid2d(
240 reinterpret_cast<const Vector2*>(&casterVertices2d.front()),
242 Vector3 centroid3d = {centroid.x, centroid.y, 0};
243 mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
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;
251 centroid3d.z += casterLift;
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);
260 // actual tessellation of both shadows
261 ShadowTessellator::tessellateAmbientShadow(
262 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
263 casterBounds, *localClip, maxZ, ambientBuffer);
265 ShadowTessellator::tessellateSpotShadow(
266 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
267 *drawTransform, lightCenter, lightRadius, casterBounds, *localClip,
271 class ShadowProcessor : public TaskProcessor<TessellationCache::vertexBuffer_pair_t> {
273 ShadowProcessor(Caches& caches)
274 : TaskProcessor<TessellationCache::vertexBuffer_pair_t>(&caches.tasks) {}
275 ~ShadowProcessor() {}
277 virtual void onProcess(const sp<Task<TessellationCache::vertexBuffer_pair_t> >& task) override {
278 TessellationCache::ShadowTask* t = static_cast<TessellationCache::ShadowTask*>(task.get());
279 ATRACE_NAME("shadow tessellation");
281 tessellateShadows(&t->drawTransform, &t->localClip, t->opaque, &t->casterPerimeter,
282 &t->transformXY, &t->transformZ, t->lightCenter, t->lightRadius,
283 t->ambientBuffer, t->spotBuffer);
285 t->setResult(TessellationCache::vertexBuffer_pair_t(&t->ambientBuffer, &t->spotBuffer));
289 ///////////////////////////////////////////////////////////////////////////////
290 // Cache constructor/destructor
291 ///////////////////////////////////////////////////////////////////////////////
293 TessellationCache::TessellationCache()
294 : mMaxSize(Properties::tessellationCacheSize)
295 , mCache(LruCache<Description, Buffer*>::kUnlimitedCapacity)
296 , mShadowCache(LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*>::kUnlimitedCapacity) {
297 mCache.setOnEntryRemovedListener(&mBufferRemovedListener);
298 mShadowCache.setOnEntryRemovedListener(&mBufferPairRemovedListener);
299 mDebugEnabled = Properties::debugLevel & kDebugCaches;
302 TessellationCache::~TessellationCache() {
306 ///////////////////////////////////////////////////////////////////////////////
308 ///////////////////////////////////////////////////////////////////////////////
310 uint32_t TessellationCache::getSize() {
311 LruCache<Description, Buffer*>::Iterator iter(mCache);
313 while (iter.next()) {
314 size += iter.value()->getSize();
319 uint32_t TessellationCache::getMaxSize() {
323 ///////////////////////////////////////////////////////////////////////////////
325 ///////////////////////////////////////////////////////////////////////////////
328 void TessellationCache::trim() {
329 uint32_t size = getSize();
330 while (size > mMaxSize) {
331 size -= mCache.peekOldestValue()->getSize();
332 mCache.removeOldest();
334 mShadowCache.clear();
337 void TessellationCache::clear() {
339 mShadowCache.clear();
342 ///////////////////////////////////////////////////////////////////////////////
344 ///////////////////////////////////////////////////////////////////////////////
346 void TessellationCache::BufferRemovedListener::operator()(Description& description,
351 ///////////////////////////////////////////////////////////////////////////////
353 ///////////////////////////////////////////////////////////////////////////////
355 void 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);
361 if (mShadowCache.get(key)) return;
362 sp<ShadowTask> task = new ShadowTask(drawTransform, localClip, opaque,
363 casterPerimeter, transformXY, transformZ, lightCenter, lightRadius);
364 if (mShadowProcessor == nullptr) {
365 mShadowProcessor = new ShadowProcessor(Caches::getInstance());
367 mShadowProcessor->add(task);
368 task->incStrong(nullptr); // not using sp<>s, so manually ref while in the cache
369 mShadowCache.put(key, task.get());
372 void 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));
379 precacheShadows(drawTransform, localClip, opaque, casterPerimeter,
380 transformXY, transformZ, lightCenter, lightRadius);
381 task = static_cast<ShadowTask*>(mShadowCache.get(key));
383 LOG_ALWAYS_FATAL_IF(task == nullptr, "shadow not precached");
384 outBuffers = task->getResult();
387 sp<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));
395 precacheShadows(drawTransform, localClip, opaque, casterPerimeter,
396 transformXY, transformZ, lightCenter, lightRadius);
397 task = static_cast<ShadowTask*>(mShadowCache.get(key));
399 LOG_ALWAYS_FATAL_IF(task == nullptr, "shadow not precached");
403 ///////////////////////////////////////////////////////////////////////////////
404 // Tessellation precaching
405 ///////////////////////////////////////////////////////////////////////////////
407 TessellationCache::Buffer* TessellationCache::getOrCreateBuffer(
408 const Description& entry, Tessellator tessellator) {
409 Buffer* buffer = mCache.get(entry);
411 // not cached, enqueue a task to fill the buffer
412 sp<TessellationTask> task = new TessellationTask(tessellator, entry);
413 buffer = new Buffer(task);
415 if (mProcessor == nullptr) {
416 mProcessor = new TessellationProcessor(Caches::getInstance());
418 mProcessor->add(task);
419 mCache.put(entry, buffer);
424 static VertexBuffer* tessellatePath(const TessellationCache::Description& description,
425 const SkPath& path) {
428 description.setupMatrixAndPaint(&matrix, &paint);
429 VertexBuffer* buffer = new VertexBuffer();
430 PathTessellator::tessellatePath(path, &paint, matrix, *buffer);
434 ///////////////////////////////////////////////////////////////////////////////
436 ///////////////////////////////////////////////////////////////////////////////
438 static 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;
445 rect.outset(outset, outset);
450 path.addRoundRect(rect, rx, ry);
451 return tessellatePath(description, path);
454 TessellationCache::Buffer* TessellationCache::getRoundRectBuffer(
455 const Matrix4& transform, const SkPaint& paint,
456 float width, float height, float rx, float ry) {
457 Description entry(Description::Type::RoundRect, transform, paint);
458 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);
464 const 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();
469 }; // namespace uirenderer
470 }; // namespace android