blob: 08b54ff0f619f40e7a78861bbac750781a51fd65 [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
17#define LOG_TAG "OpenGLRenderer"
18#define ATRACE_TAG ATRACE_TAG_VIEW
19
20#include <utils/JenkinsHash.h>
21#include <utils/Trace.h>
22
23#include "Caches.h"
24#include "OpenGLRenderer.h"
25#include "PathTessellator.h"
26#include "ShadowTessellator.h"
27#include "TessellationCache.h"
28
29#include "thread/Signal.h"
30#include "thread/Task.h"
31#include "thread/TaskProcessor.h"
32
33namespace android {
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Cache entries
38///////////////////////////////////////////////////////////////////////////////
39
40TessellationCache::Description::Description()
41 : type(kNone)
Chris Craik6ac174b2014-06-17 13:47:05 -070042 , scaleX(1.0f)
43 , scaleY(1.0f)
Chris Craiked4ef0b2014-06-12 13:27:30 -070044 , aa(false)
Chris Craik05f3d6e2014-06-02 16:27:04 -070045 , cap(SkPaint::kDefault_Cap)
46 , style(SkPaint::kFill_Style)
47 , strokeWidth(1.0f) {
48 memset(&shape, 0, sizeof(Shape));
49}
50
Chris Craik6ac174b2014-06-17 13:47:05 -070051TessellationCache::Description::Description(Type type, const Matrix4& transform, const SkPaint& paint)
Chris Craik05f3d6e2014-06-02 16:27:04 -070052 : type(type)
Chris Craik6ac174b2014-06-17 13:47:05 -070053 , aa(paint.isAntiAlias())
54 , cap(paint.getStrokeCap())
55 , style(paint.getStyle())
56 , strokeWidth(paint.getStrokeWidth()) {
57 PathTessellator::extractTessellationScales(transform, &scaleX, &scaleY);
Chris Craik05f3d6e2014-06-02 16:27:04 -070058 memset(&shape, 0, sizeof(Shape));
59}
60
61hash_t TessellationCache::Description::hash() const {
62 uint32_t hash = JenkinsHashMix(0, type);
Chris Craiked4ef0b2014-06-12 13:27:30 -070063 hash = JenkinsHashMix(hash, aa);
Chris Craik05f3d6e2014-06-02 16:27:04 -070064 hash = JenkinsHashMix(hash, cap);
65 hash = JenkinsHashMix(hash, style);
66 hash = JenkinsHashMix(hash, android::hash_type(strokeWidth));
Chris Craik6ac174b2014-06-17 13:47:05 -070067 hash = JenkinsHashMix(hash, android::hash_type(scaleX));
68 hash = JenkinsHashMix(hash, android::hash_type(scaleY));
Chris Craik05f3d6e2014-06-02 16:27:04 -070069 hash = JenkinsHashMixBytes(hash, (uint8_t*) &shape, sizeof(Shape));
70 return JenkinsHashWhiten(hash);
71}
72
Chris Craik6ac174b2014-06-17 13:47:05 -070073void TessellationCache::Description::setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const {
74 matrix->loadScale(scaleX, scaleY, 1.0f);
75 paint->setAntiAlias(aa);
76 paint->setStrokeCap(cap);
77 paint->setStyle(style);
78 paint->setStrokeWidth(strokeWidth);
79}
80
Chris Craik05f3d6e2014-06-02 16:27:04 -070081TessellationCache::ShadowDescription::ShadowDescription()
82 : nodeKey(NULL) {
83 memset(&matrixData, 0, 16 * sizeof(float));
84}
85
86TessellationCache::ShadowDescription::ShadowDescription(const void* nodeKey, const Matrix4* drawTransform)
87 : nodeKey(nodeKey) {
88 memcpy(&matrixData, drawTransform->data, 16 * sizeof(float));
89}
90
91hash_t TessellationCache::ShadowDescription::hash() const {
92 uint32_t hash = JenkinsHashMixBytes(0, (uint8_t*) &nodeKey, sizeof(const void*));
93 hash = JenkinsHashMixBytes(hash, (uint8_t*) &matrixData, 16 * sizeof(float));
94 return JenkinsHashWhiten(hash);
95}
96
97///////////////////////////////////////////////////////////////////////////////
98// General purpose tessellation task processing
99///////////////////////////////////////////////////////////////////////////////
100
101class TessellationCache::TessellationTask : public Task<VertexBuffer*> {
102public:
Chris Craik6ac174b2014-06-17 13:47:05 -0700103 TessellationTask(Tessellator tessellator, const Description& description)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700104 : tessellator(tessellator)
Chris Craik6ac174b2014-06-17 13:47:05 -0700105 , description(description) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700106 }
107
108 ~TessellationTask() {}
109
110 Tessellator tessellator;
111 Description description;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700112};
113
114class TessellationCache::TessellationProcessor : public TaskProcessor<VertexBuffer*> {
115public:
116 TessellationProcessor(Caches& caches)
117 : TaskProcessor<VertexBuffer*>(&caches.tasks) {}
118 ~TessellationProcessor() {}
119
120 virtual void onProcess(const sp<Task<VertexBuffer*> >& task) {
121 TessellationTask* t = static_cast<TessellationTask*>(task.get());
122 ATRACE_NAME("shape tessellation");
Chris Craik6ac174b2014-06-17 13:47:05 -0700123 VertexBuffer* buffer = t->tessellator(t->description);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700124 t->setResult(buffer);
125 }
126};
127
128struct TessellationCache::Buffer {
129public:
130 Buffer(const sp<Task<VertexBuffer*> >& task)
131 : mTask(task)
132 , mBuffer(NULL) {
133 }
134
135 ~Buffer() {
136 mTask.clear();
137 delete mBuffer;
138 }
139
140 unsigned int getSize() {
141 blockOnPrecache();
142 return mBuffer->getSize();
143 }
144
145 const VertexBuffer* getVertexBuffer() {
146 blockOnPrecache();
147 return mBuffer;
148 }
149
150private:
151 void blockOnPrecache() {
152 if (mTask != NULL) {
153 mBuffer = mTask->getResult();
154 LOG_ALWAYS_FATAL_IF(mBuffer == NULL, "Failed to precache");
155 mTask.clear();
156 }
157 }
158 sp<Task<VertexBuffer*> > mTask;
159 VertexBuffer* mBuffer;
160};
161
162///////////////////////////////////////////////////////////////////////////////
163// Shadow tessellation task processing
164///////////////////////////////////////////////////////////////////////////////
165
166class ShadowTask : public Task<TessellationCache::vertexBuffer_pair_t*> {
167public:
168 ShadowTask(const Matrix4* drawTransform, const Rect& localClip, bool opaque,
169 const SkPath* casterPerimeter, const Matrix4* transformXY, const Matrix4* transformZ,
170 const Vector3& lightCenter, float lightRadius)
Chris Craik1b3be082014-06-11 13:44:19 -0700171 : drawTransform(*drawTransform)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700172 , localClip(localClip)
173 , opaque(opaque)
Chris Craik1b3be082014-06-11 13:44:19 -0700174 , casterPerimeter(*casterPerimeter)
175 , transformXY(*transformXY)
176 , transformZ(*transformZ)
Chris Craik05f3d6e2014-06-02 16:27:04 -0700177 , lightCenter(lightCenter)
178 , lightRadius(lightRadius) {
179 }
180
181 ~ShadowTask() {
182 TessellationCache::vertexBuffer_pair_t* bufferPair = getResult();
183 delete bufferPair->getFirst();
184 delete bufferPair->getSecond();
185 delete bufferPair;
186 }
187
Chris Craik1b3be082014-06-11 13:44:19 -0700188 /* Note - we deep copy all task parameters, because *even though* pointers into Allocator
189 * controlled objects (like the SkPath and Matrix4s) should be safe for the entire frame,
190 * certain Allocators are destroyed before trim() is called to flush incomplete tasks.
191 *
192 * These deep copies could be avoided, long term, by cancelling or flushing outstanding tasks
193 * before tearning down single-frame LinearAllocators.
194 */
195 const Matrix4 drawTransform;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700196 const Rect localClip;
197 bool opaque;
Chris Craik1b3be082014-06-11 13:44:19 -0700198 const SkPath casterPerimeter;
199 const Matrix4 transformXY;
200 const Matrix4 transformZ;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700201 const Vector3 lightCenter;
202 const float lightRadius;
203};
204
205static void mapPointFakeZ(Vector3& point, const mat4* transformXY, const mat4* transformZ) {
206 // map z coordinate with true 3d matrix
207 point.z = transformZ->mapZ(point);
208
209 // map x,y coordinates with draw/Skia matrix
210 transformXY->mapPoint(point.x, point.y);
211}
212
213static void tessellateShadows(
214 const Matrix4* drawTransform, const Rect* localClip,
215 bool isCasterOpaque, const SkPath* casterPerimeter,
216 const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
217 const Vector3& lightCenter, float lightRadius,
218 VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer) {
219
220 // tessellate caster outline into a 2d polygon
221 Vector<Vertex> casterVertices2d;
222 const float casterRefinementThresholdSquared = 20.0f; // TODO: experiment with this value
223 PathTessellator::approximatePathOutlineVertices(*casterPerimeter,
224 casterRefinementThresholdSquared, casterVertices2d);
225 if (!ShadowTessellator::isClockwisePath(*casterPerimeter)) {
226 ShadowTessellator::reverseVertexArray(casterVertices2d.editArray(),
227 casterVertices2d.size());
228 }
229
230 if (casterVertices2d.size() == 0) return;
231
232 // map 2d caster poly into 3d
233 const int casterVertexCount = casterVertices2d.size();
234 Vector3 casterPolygon[casterVertexCount];
235 float minZ = FLT_MAX;
236 float maxZ = -FLT_MAX;
237 for (int i = 0; i < casterVertexCount; i++) {
238 const Vertex& point2d = casterVertices2d[i];
239 casterPolygon[i] = Vector3(point2d.x, point2d.y, 0);
240 mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
241 minZ = fmin(minZ, casterPolygon[i].z);
242 maxZ = fmax(maxZ, casterPolygon[i].z);
243 }
244
245 // map the centroid of the caster into 3d
246 Vector2 centroid = ShadowTessellator::centroid2d(
247 reinterpret_cast<const Vector2*>(casterVertices2d.array()),
248 casterVertexCount);
249 Vector3 centroid3d(centroid.x, centroid.y, 0);
250 mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
251
252 // if the caster intersects the z=0 plane, lift it in Z so it doesn't
253 if (minZ < SHADOW_MIN_CASTER_Z) {
254 float casterLift = SHADOW_MIN_CASTER_Z - minZ;
255 for (int i = 0; i < casterVertexCount; i++) {
256 casterPolygon[i].z += casterLift;
257 }
258 centroid3d.z += casterLift;
259 }
260
261 // Check whether we want to draw the shadow at all by checking the caster's bounds against clip.
262 // We only have ortho projection, so we can just ignore the Z in caster for
263 // simple rejection calculation.
264 Rect casterBounds(casterPerimeter->getBounds());
265 casterTransformXY->mapRect(casterBounds);
266
267 // actual tessellation of both shadows
268 ShadowTessellator::tessellateAmbientShadow(
269 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
270 casterBounds, *localClip, maxZ, ambientBuffer);
271
272 ShadowTessellator::tessellateSpotShadow(
273 isCasterOpaque, casterPolygon, casterVertexCount,
274 *drawTransform, lightCenter, lightRadius, casterBounds, *localClip,
275 spotBuffer);
276
277 // TODO: set ambientBuffer & spotBuffer's bounds for correct layer damage
278}
279
280class ShadowProcessor : public TaskProcessor<TessellationCache::vertexBuffer_pair_t*> {
281public:
282 ShadowProcessor(Caches& caches)
283 : TaskProcessor<TessellationCache::vertexBuffer_pair_t*>(&caches.tasks) {}
284 ~ShadowProcessor() {}
285
286 virtual void onProcess(const sp<Task<TessellationCache::vertexBuffer_pair_t*> >& task) {
287 ShadowTask* t = static_cast<ShadowTask*>(task.get());
288 ATRACE_NAME("shadow tessellation");
289
290 VertexBuffer* ambientBuffer = new VertexBuffer;
291 VertexBuffer* spotBuffer = new VertexBuffer;
Chris Craik1b3be082014-06-11 13:44:19 -0700292 tessellateShadows(&t->drawTransform, &t->localClip, t->opaque, &t->casterPerimeter,
293 &t->transformXY, &t->transformZ, t->lightCenter, t->lightRadius,
Chris Craik05f3d6e2014-06-02 16:27:04 -0700294 *ambientBuffer, *spotBuffer);
295
296 t->setResult(new TessellationCache::vertexBuffer_pair_t(ambientBuffer, spotBuffer));
297 }
298};
299
300///////////////////////////////////////////////////////////////////////////////
301// Cache constructor/destructor
302///////////////////////////////////////////////////////////////////////////////
303
304TessellationCache::TessellationCache()
305 : mSize(0)
306 , mMaxSize(MB(DEFAULT_VERTEX_CACHE_SIZE))
307 , mCache(LruCache<Description, Buffer*>::kUnlimitedCapacity)
308 , mShadowCache(LruCache<ShadowDescription, Task<vertexBuffer_pair_t*>*>::kUnlimitedCapacity) {
309 char property[PROPERTY_VALUE_MAX];
310 if (property_get(PROPERTY_VERTEX_CACHE_SIZE, property, NULL) > 0) {
311 INIT_LOGD(" Setting %s cache size to %sMB", name, property);
312 setMaxSize(MB(atof(property)));
313 } else {
314 INIT_LOGD(" Using default %s cache size of %.2fMB", name, DEFAULT_VERTEX_CACHE_SIZE);
315 }
316
317 mCache.setOnEntryRemovedListener(&mBufferRemovedListener);
318 mShadowCache.setOnEntryRemovedListener(&mBufferPairRemovedListener);
319 mDebugEnabled = readDebugLevel() & kDebugCaches;
320}
321
322TessellationCache::~TessellationCache() {
323 mCache.clear();
324}
325
326///////////////////////////////////////////////////////////////////////////////
327// Size management
328///////////////////////////////////////////////////////////////////////////////
329
330uint32_t TessellationCache::getSize() {
331 LruCache<Description, Buffer*>::Iterator iter(mCache);
332 uint32_t size = 0;
333 while (iter.next()) {
334 size += iter.value()->getSize();
335 }
336 return size;
337}
338
339uint32_t TessellationCache::getMaxSize() {
340 return mMaxSize;
341}
342
343void TessellationCache::setMaxSize(uint32_t maxSize) {
344 mMaxSize = maxSize;
345 while (mSize > mMaxSize) {
346 mCache.removeOldest();
347 }
348}
349
350///////////////////////////////////////////////////////////////////////////////
351// Caching
352///////////////////////////////////////////////////////////////////////////////
353
354
355void TessellationCache::trim() {
356 uint32_t size = getSize();
357 while (size > mMaxSize) {
358 size -= mCache.peekOldestValue()->getSize();
359 mCache.removeOldest();
360 }
361 mShadowCache.clear();
362}
363
364void TessellationCache::clear() {
365 mCache.clear();
366 mShadowCache.clear();
367}
368
369///////////////////////////////////////////////////////////////////////////////
370// Callbacks
371///////////////////////////////////////////////////////////////////////////////
372
373void TessellationCache::BufferRemovedListener::operator()(Description& description,
374 Buffer*& buffer) {
375 delete buffer;
376}
377
378///////////////////////////////////////////////////////////////////////////////
379// Shadows
380///////////////////////////////////////////////////////////////////////////////
381
382void TessellationCache::precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
383 bool opaque, const SkPath* casterPerimeter,
384 const Matrix4* transformXY, const Matrix4* transformZ,
385 const Vector3& lightCenter, float lightRadius) {
386 ShadowDescription key(casterPerimeter, drawTransform);
387
388 sp<ShadowTask> task = new ShadowTask(drawTransform, localClip, opaque,
389 casterPerimeter, transformXY, transformZ, lightCenter, lightRadius);
390 if (mShadowProcessor == NULL) {
391 mShadowProcessor = new ShadowProcessor(Caches::getInstance());
392 }
393 mShadowProcessor->add(task);
394
395 task->incStrong(NULL); // not using sp<>s, so manually ref while in the cache
396 mShadowCache.put(key, task.get());
397}
398
399void TessellationCache::getShadowBuffers(const Matrix4* drawTransform, const Rect& localClip,
400 bool opaque, const SkPath* casterPerimeter,
401 const Matrix4* transformXY, const Matrix4* transformZ,
402 const Vector3& lightCenter, float lightRadius, vertexBuffer_pair_t& outBuffers) {
403 ShadowDescription key(casterPerimeter, drawTransform);
404 ShadowTask* task = static_cast<ShadowTask*>(mShadowCache.get(key));
405 if (!task) {
406 precacheShadows(drawTransform, localClip, opaque, casterPerimeter,
407 transformXY, transformZ, lightCenter, lightRadius);
408 task = static_cast<ShadowTask*>(mShadowCache.get(key));
409 }
410 LOG_ALWAYS_FATAL_IF(task == NULL, "shadow not precached");
411 outBuffers = *(task->getResult());
412}
413
414///////////////////////////////////////////////////////////////////////////////
415// Tessellation precaching
416///////////////////////////////////////////////////////////////////////////////
417
Chris Craik05f3d6e2014-06-02 16:27:04 -0700418TessellationCache::Buffer* TessellationCache::getOrCreateBuffer(
Chris Craik6ac174b2014-06-17 13:47:05 -0700419 const Description& entry, Tessellator tessellator) {
Chris Craik05f3d6e2014-06-02 16:27:04 -0700420 Buffer* buffer = mCache.get(entry);
421 if (!buffer) {
422 // not cached, enqueue a task to fill the buffer
Chris Craik6ac174b2014-06-17 13:47:05 -0700423 sp<TessellationTask> task = new TessellationTask(tessellator, entry);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700424 buffer = new Buffer(task);
425
426 if (mProcessor == NULL) {
427 mProcessor = new TessellationProcessor(Caches::getInstance());
428 }
429 mProcessor->add(task);
430 mCache.put(entry, buffer);
431 }
432 return buffer;
433}
434
Chris Craik6ac174b2014-06-17 13:47:05 -0700435static VertexBuffer* tessellatePath(const TessellationCache::Description& description,
436 const SkPath& path) {
437 Matrix4 matrix;
438 SkPaint paint;
439 description.setupMatrixAndPaint(&matrix, &paint);
440 VertexBuffer* buffer = new VertexBuffer();
441 PathTessellator::tessellatePath(path, &paint, matrix, *buffer);
442 return buffer;
443}
444
Chris Craik05f3d6e2014-06-02 16:27:04 -0700445///////////////////////////////////////////////////////////////////////////////
Chris Craik6ac174b2014-06-17 13:47:05 -0700446// RoundRect
Chris Craik05f3d6e2014-06-02 16:27:04 -0700447///////////////////////////////////////////////////////////////////////////////
448
Chris Craik6ac174b2014-06-17 13:47:05 -0700449static VertexBuffer* tessellateRoundRect(const TessellationCache::Description& description) {
450 SkRect rect = SkRect::MakeWH(description.shape.roundRect.width,
451 description.shape.roundRect.height);
452 float rx = description.shape.roundRect.rx;
453 float ry = description.shape.roundRect.ry;
454 if (description.style == SkPaint::kStrokeAndFill_Style) {
455 float outset = description.strokeWidth / 2;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700456 rect.outset(outset, outset);
457 rx += outset;
458 ry += outset;
459 }
460 SkPath path;
461 path.addRoundRect(rect, rx, ry);
Chris Craik6ac174b2014-06-17 13:47:05 -0700462 return tessellatePath(description, path);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700463}
464
Chris Craik6ac174b2014-06-17 13:47:05 -0700465TessellationCache::Buffer* TessellationCache::getRoundRectBuffer(
466 const Matrix4& transform, const SkPaint& paint,
467 float width, float height, float rx, float ry) {
468 Description entry(Description::kRoundRect, transform, paint);
469 entry.shape.roundRect.width = width;
470 entry.shape.roundRect.height = height;
471 entry.shape.roundRect.rx = rx;
472 entry.shape.roundRect.ry = ry;
473 return getOrCreateBuffer(entry, &tessellateRoundRect);
Chris Craik05f3d6e2014-06-02 16:27:04 -0700474}
Chris Craik6ac174b2014-06-17 13:47:05 -0700475const VertexBuffer* TessellationCache::getRoundRect(const Matrix4& transform, const SkPaint& paint,
476 float width, float height, float rx, float ry) {
477 return getRoundRectBuffer(transform, paint, width, height, rx, ry)->getVertexBuffer();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700478}
479
480}; // namespace uirenderer
481}; // namespace android