2014-06-02 16:27:04 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-07-06 16:10:09 -07:00
|
|
|
#pragma once
|
2014-06-02 16:27:04 -07:00
|
|
|
|
|
|
|
#include "Debug.h"
|
2015-10-22 17:07:45 -07:00
|
|
|
#include "Matrix.h"
|
|
|
|
#include "Rect.h"
|
|
|
|
#include "Vector.h"
|
2016-02-03 15:52:25 -08:00
|
|
|
#include "VertexBuffer.h"
|
2015-10-22 17:07:45 -07:00
|
|
|
#include "thread/TaskProcessor.h"
|
2014-06-02 16:27:04 -07:00
|
|
|
#include "utils/Macros.h"
|
|
|
|
#include "utils/Pair.h"
|
|
|
|
|
2015-10-22 17:07:45 -07:00
|
|
|
#include <SkPaint.h>
|
2016-01-15 16:15:30 -08:00
|
|
|
#include <SkPath.h>
|
2015-10-22 17:07:45 -07:00
|
|
|
|
|
|
|
#include <utils/LruCache.h>
|
|
|
|
#include <utils/Mutex.h>
|
|
|
|
#include <utils/StrongPointer.h>
|
|
|
|
|
2014-06-02 16:27:04 -07:00
|
|
|
class SkBitmap;
|
|
|
|
class SkCanvas;
|
|
|
|
struct SkRect;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
class Caches;
|
2014-10-15 15:46:42 -04:00
|
|
|
class VertexBuffer;
|
2014-06-02 16:27:04 -07:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Classes
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class TessellationCache {
|
|
|
|
public:
|
|
|
|
typedef Pair<VertexBuffer*, VertexBuffer*> vertexBuffer_pair_t;
|
|
|
|
|
|
|
|
struct Description {
|
2016-04-07 18:06:53 -07:00
|
|
|
HASHABLE_TYPE(Description);
|
|
|
|
enum class Type {
|
|
|
|
None,
|
|
|
|
RoundRect,
|
2014-06-02 16:27:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Type type;
|
2014-06-17 13:47:05 -07:00
|
|
|
float scaleX;
|
|
|
|
float scaleY;
|
2014-06-12 13:27:30 -07:00
|
|
|
bool aa;
|
2014-06-02 16:27:04 -07:00
|
|
|
SkPaint::Cap cap;
|
|
|
|
SkPaint::Style style;
|
|
|
|
float strokeWidth;
|
|
|
|
union Shape {
|
|
|
|
struct RoundRect {
|
2014-06-17 13:47:05 -07:00
|
|
|
float width;
|
|
|
|
float height;
|
|
|
|
float rx;
|
|
|
|
float ry;
|
2014-06-02 16:27:04 -07:00
|
|
|
} roundRect;
|
|
|
|
} shape;
|
|
|
|
|
|
|
|
Description();
|
2014-06-17 13:47:05 -07:00
|
|
|
Description(Type type, const Matrix4& transform, const SkPaint& paint);
|
|
|
|
void setupMatrixAndPaint(Matrix4* matrix, SkPaint* paint) const;
|
2014-06-02 16:27:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ShadowDescription {
|
2016-04-07 18:06:53 -07:00
|
|
|
HASHABLE_TYPE(ShadowDescription);
|
|
|
|
const SkPath* nodeKey;
|
2014-06-02 16:27:04 -07:00
|
|
|
float matrixData[16];
|
|
|
|
|
|
|
|
ShadowDescription();
|
2016-04-07 18:06:53 -07:00
|
|
|
ShadowDescription(const SkPath* nodeKey, const Matrix4* drawTransform);
|
2014-06-02 16:27:04 -07:00
|
|
|
};
|
|
|
|
|
2016-02-03 15:52:25 -08:00
|
|
|
class ShadowTask : public Task<vertexBuffer_pair_t> {
|
2016-01-15 16:15:30 -08:00
|
|
|
public:
|
|
|
|
ShadowTask(const Matrix4* drawTransform, const Rect& localClip, bool opaque,
|
|
|
|
const SkPath* casterPerimeter, const Matrix4* transformXY, const Matrix4* transformZ,
|
|
|
|
const Vector3& lightCenter, float lightRadius)
|
|
|
|
: drawTransform(*drawTransform)
|
|
|
|
, localClip(localClip)
|
|
|
|
, opaque(opaque)
|
|
|
|
, casterPerimeter(*casterPerimeter)
|
|
|
|
, transformXY(*transformXY)
|
|
|
|
, transformZ(*transformZ)
|
|
|
|
, lightCenter(lightCenter)
|
|
|
|
, lightRadius(lightRadius) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note - we deep copy all task parameters, because *even though* pointers into Allocator
|
|
|
|
* controlled objects (like the SkPath and Matrix4s) should be safe for the entire frame,
|
|
|
|
* certain Allocators are destroyed before trim() is called to flush incomplete tasks.
|
|
|
|
*
|
2016-02-03 15:52:25 -08:00
|
|
|
* These deep copies could be avoided, long term, by canceling or flushing outstanding
|
2016-01-15 16:15:30 -08:00
|
|
|
* tasks before tearing down single-frame LinearAllocators.
|
|
|
|
*/
|
|
|
|
const Matrix4 drawTransform;
|
|
|
|
const Rect localClip;
|
|
|
|
bool opaque;
|
|
|
|
const SkPath casterPerimeter;
|
|
|
|
const Matrix4 transformXY;
|
|
|
|
const Matrix4 transformZ;
|
|
|
|
const Vector3 lightCenter;
|
|
|
|
const float lightRadius;
|
2016-02-03 15:52:25 -08:00
|
|
|
VertexBuffer ambientBuffer;
|
|
|
|
VertexBuffer spotBuffer;
|
2016-01-15 16:15:30 -08:00
|
|
|
};
|
|
|
|
|
2014-06-02 16:27:04 -07:00
|
|
|
TessellationCache();
|
|
|
|
~TessellationCache();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the cache. This causes all TessellationBuffers to be deleted.
|
|
|
|
*/
|
|
|
|
void clear();
|
|
|
|
/**
|
|
|
|
* Returns the maximum size of the cache in bytes.
|
|
|
|
*/
|
|
|
|
uint32_t getMaxSize();
|
|
|
|
/**
|
|
|
|
* Returns the current size of the cache in bytes.
|
|
|
|
*/
|
|
|
|
uint32_t getSize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trims the contents of the cache, removing items until it's under its
|
|
|
|
* specified limit.
|
|
|
|
*
|
|
|
|
* Trimming is used for caches that support pre-caching from a worker
|
|
|
|
* thread. During pre-caching the maximum limit of the cache can be
|
|
|
|
* exceeded for the duration of the frame. It is therefore required to
|
|
|
|
* trim the cache at the end of the frame to keep the total amount of
|
|
|
|
* memory used under control.
|
|
|
|
*
|
|
|
|
* Also removes transient Shadow VertexBuffers, which aren't cached between frames.
|
|
|
|
*/
|
|
|
|
void trim();
|
|
|
|
|
|
|
|
// TODO: precache/get for Oval, Lines, Points, etc.
|
|
|
|
|
2014-06-17 13:47:05 -07:00
|
|
|
void precacheRoundRect(const Matrix4& transform, const SkPaint& paint,
|
|
|
|
float width, float height, float rx, float ry) {
|
|
|
|
getRoundRectBuffer(transform, paint, width, height, rx, ry);
|
2014-06-02 16:27:04 -07:00
|
|
|
}
|
2014-06-17 13:47:05 -07:00
|
|
|
const VertexBuffer* getRoundRect(const Matrix4& transform, const SkPaint& paint,
|
|
|
|
float width, float height, float rx, float ry);
|
2014-06-02 16:27:04 -07:00
|
|
|
|
2016-01-15 16:15:30 -08:00
|
|
|
sp<ShadowTask> getShadowTask(const Matrix4* drawTransform, const Rect& localClip,
|
|
|
|
bool opaque, const SkPath* casterPerimeter,
|
|
|
|
const Matrix4* transformXY, const Matrix4* transformZ,
|
|
|
|
const Vector3& lightCenter, float lightRadius);
|
|
|
|
|
2014-06-02 16:27:04 -07:00
|
|
|
private:
|
|
|
|
class Buffer;
|
|
|
|
class TessellationTask;
|
|
|
|
class TessellationProcessor;
|
|
|
|
|
2014-06-17 13:47:05 -07:00
|
|
|
typedef VertexBuffer* (*Tessellator)(const Description&);
|
2014-06-02 16:27:04 -07:00
|
|
|
|
2016-07-06 16:10:09 -07:00
|
|
|
void precacheShadows(const Matrix4* drawTransform, const Rect& localClip,
|
|
|
|
bool opaque, const SkPath* casterPerimeter,
|
|
|
|
const Matrix4* transformXY, const Matrix4* transformZ,
|
|
|
|
const Vector3& lightCenter, float lightRadius);
|
|
|
|
|
2014-06-17 13:47:05 -07:00
|
|
|
Buffer* getRectBuffer(const Matrix4& transform, const SkPaint& paint,
|
|
|
|
float width, float height);
|
|
|
|
Buffer* getRoundRectBuffer(const Matrix4& transform, const SkPaint& paint,
|
|
|
|
float width, float height, float rx, float ry);
|
2014-06-02 16:27:04 -07:00
|
|
|
|
2014-06-17 13:47:05 -07:00
|
|
|
Buffer* getOrCreateBuffer(const Description& entry, Tessellator tessellator);
|
2014-06-02 16:27:04 -07:00
|
|
|
|
2016-02-05 15:59:29 -08:00
|
|
|
const uint32_t mMaxSize;
|
2014-06-02 16:27:04 -07:00
|
|
|
|
|
|
|
bool mDebugEnabled;
|
|
|
|
|
|
|
|
mutable Mutex mLock;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// General tessellation caching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
sp<TaskProcessor<VertexBuffer*> > mProcessor;
|
|
|
|
LruCache<Description, Buffer*> mCache;
|
|
|
|
class BufferRemovedListener : public OnEntryRemoved<Description, Buffer*> {
|
2014-12-22 14:28:49 -08:00
|
|
|
void operator()(Description& description, Buffer*& buffer) override;
|
2014-06-02 16:27:04 -07:00
|
|
|
};
|
|
|
|
BufferRemovedListener mBufferRemovedListener;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Shadow tessellation caching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-03 15:52:25 -08:00
|
|
|
sp<TaskProcessor<vertexBuffer_pair_t> > mShadowProcessor;
|
2014-06-02 16:27:04 -07:00
|
|
|
|
|
|
|
// holds a pointer, and implicit strong ref to each shadow task of the frame
|
2016-02-03 15:52:25 -08:00
|
|
|
LruCache<ShadowDescription, Task<vertexBuffer_pair_t>*> mShadowCache;
|
|
|
|
class BufferPairRemovedListener : public OnEntryRemoved<ShadowDescription, Task<vertexBuffer_pair_t>*> {
|
|
|
|
void operator()(ShadowDescription& description, Task<vertexBuffer_pair_t>*& bufferPairTask) override {
|
2014-12-22 14:28:49 -08:00
|
|
|
bufferPairTask->decStrong(nullptr);
|
2014-06-02 16:27:04 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
BufferPairRemovedListener mBufferPairRemovedListener;
|
|
|
|
|
|
|
|
}; // class TessellationCache
|
|
|
|
|
2015-10-22 17:07:45 -07:00
|
|
|
void tessellateShadows(
|
|
|
|
const Matrix4* drawTransform, const Rect* localClip,
|
|
|
|
bool isCasterOpaque, const SkPath* casterPerimeter,
|
|
|
|
const Matrix4* casterTransformXY, const Matrix4* casterTransformZ,
|
|
|
|
const Vector3& lightCenter, float lightRadius,
|
|
|
|
VertexBuffer& ambientBuffer, VertexBuffer& spotBuffer);
|
|
|
|
|
2014-06-02 16:27:04 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|