2012-02-02 17:31:16 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Snapshot.h"
|
|
|
|
|
2016-03-21 15:38:21 -07:00
|
|
|
#include "hwui/Canvas.h"
|
2012-02-02 17:31:16 -08:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-08 13:57:05 -07:00
|
|
|
Snapshot::Snapshot()
|
|
|
|
: flags(0)
|
2015-01-05 15:51:13 -08:00
|
|
|
, previous(nullptr)
|
|
|
|
, layer(nullptr)
|
2014-05-08 13:57:05 -07:00
|
|
|
, fbo(0)
|
2014-05-05 19:09:33 -07:00
|
|
|
, alpha(1.0f)
|
2015-01-06 13:22:54 -08:00
|
|
|
, roundRectClipState(nullptr)
|
2015-04-28 11:45:59 -07:00
|
|
|
, projectionPathMask(nullptr)
|
2015-01-06 13:22:54 -08:00
|
|
|
, mClipArea(&mClipAreaRoot) {
|
2012-02-02 17:31:16 -08:00
|
|
|
transform = &mTransformRoot;
|
2016-07-13 14:45:16 -07:00
|
|
|
mRelativeLightCenter.x = mRelativeLightCenter.y = mRelativeLightCenter.z = 0;
|
2012-02-02 17:31:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the specified snapshot/ The specified snapshot is stored as
|
|
|
|
* the previous snapshot.
|
|
|
|
*/
|
2015-10-06 10:06:37 -07:00
|
|
|
Snapshot::Snapshot(Snapshot* s, int saveFlags)
|
2014-05-08 13:57:05 -07:00
|
|
|
: flags(0)
|
|
|
|
, previous(s)
|
|
|
|
, layer(s->layer)
|
|
|
|
, fbo(s->fbo)
|
2014-05-14 14:17:01 -07:00
|
|
|
, alpha(s->alpha)
|
2014-05-05 19:09:33 -07:00
|
|
|
, roundRectClipState(s->roundRectClipState)
|
2015-04-28 11:45:59 -07:00
|
|
|
, projectionPathMask(s->projectionPathMask)
|
2015-01-06 13:22:54 -08:00
|
|
|
, mClipArea(nullptr)
|
2014-08-14 13:34:01 -07:00
|
|
|
, mViewportData(s->mViewportData)
|
|
|
|
, mRelativeLightCenter(s->mRelativeLightCenter) {
|
2015-12-21 10:43:01 -05:00
|
|
|
if (saveFlags & SaveFlags::Matrix) {
|
2015-08-19 15:10:24 -07:00
|
|
|
mTransformRoot = *s->transform;
|
2012-02-02 17:31:16 -08:00
|
|
|
transform = &mTransformRoot;
|
|
|
|
} else {
|
|
|
|
transform = s->transform;
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
if (saveFlags & SaveFlags::Clip) {
|
2015-01-06 13:22:54 -08:00
|
|
|
mClipAreaRoot = s->getClipArea();
|
|
|
|
mClipArea = &mClipAreaRoot;
|
2012-02-02 17:31:16 -08:00
|
|
|
} else {
|
2015-01-06 13:22:54 -08:00
|
|
|
mClipArea = s->mClipArea;
|
2012-02-02 17:31:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clipping
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-02 15:36:59 -05:00
|
|
|
void Snapshot::clip(const Rect& localClip, SkClipOp op) {
|
2015-01-06 13:22:54 -08:00
|
|
|
flags |= Snapshot::kFlagClipSet;
|
2016-12-02 15:36:59 -05:00
|
|
|
mClipArea->clipRectWithTransform(localClip, transform, static_cast<SkRegion::Op>(op));
|
2012-02-02 17:31:16 -08:00
|
|
|
}
|
|
|
|
|
2016-12-02 15:36:59 -05:00
|
|
|
void Snapshot::clipPath(const SkPath& path, SkClipOp op) {
|
2015-01-06 13:22:54 -08:00
|
|
|
flags |= Snapshot::kFlagClipSet;
|
2016-12-02 15:36:59 -05:00
|
|
|
mClipArea->clipPathWithTransform(path, transform, static_cast<SkRegion::Op>(op));
|
2012-02-02 17:31:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Snapshot::setClip(float left, float top, float right, float bottom) {
|
|
|
|
flags |= Snapshot::kFlagClipSet;
|
2015-08-20 12:54:25 -07:00
|
|
|
mClipArea->setClip(left, top, right, bottom);
|
2012-02-02 17:31:16 -08:00
|
|
|
}
|
|
|
|
|
2012-09-28 13:55:44 -07:00
|
|
|
bool Snapshot::hasPerspectiveTransform() const {
|
|
|
|
return transform->isPerspective();
|
|
|
|
}
|
|
|
|
|
2012-02-02 17:31:16 -08:00
|
|
|
const Rect& Snapshot::getLocalClip() {
|
|
|
|
mat4 inverse;
|
|
|
|
inverse.loadInverse(*transform);
|
|
|
|
|
2015-01-06 13:22:54 -08:00
|
|
|
mLocalClip.set(mClipArea->getClipRect());
|
2012-02-02 17:31:16 -08:00
|
|
|
inverse.mapRect(mLocalClip);
|
|
|
|
|
|
|
|
return mLocalClip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snapshot::resetClip(float left, float top, float right, float bottom) {
|
2013-02-06 16:51:04 -08:00
|
|
|
// TODO: This is incorrect, when we start rendering into a new layer,
|
|
|
|
// we may have to modify the previous snapshot's clip rect and clip
|
|
|
|
// region if the previous restore() call did not restore the clip
|
2015-01-06 13:22:54 -08:00
|
|
|
mClipArea = &mClipAreaRoot;
|
2012-02-07 17:04:34 -08:00
|
|
|
setClip(left, top, right, bottom);
|
2012-02-02 17:31:16 -08:00
|
|
|
}
|
|
|
|
|
2014-05-05 19:09:33 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-07-29 12:50:14 -07:00
|
|
|
// Clipping round rect
|
2014-05-05 19:09:33 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void Snapshot::setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds, float radius,
|
|
|
|
bool highPriority) {
|
2014-07-29 12:50:14 -07:00
|
|
|
if (bounds.isEmpty()) {
|
2015-01-06 13:22:54 -08:00
|
|
|
mClipArea->setEmpty();
|
2014-07-29 12:50:14 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-05-05 19:09:33 -07:00
|
|
|
|
2014-09-03 17:52:24 -07:00
|
|
|
if (roundRectClipState && roundRectClipState->highPriority) {
|
|
|
|
// ignore, don't replace, already have a high priority clip
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:09:33 -07:00
|
|
|
RoundRectClipState* state = new (allocator) RoundRectClipState;
|
|
|
|
|
2014-09-03 17:52:24 -07:00
|
|
|
state->highPriority = highPriority;
|
|
|
|
|
2014-05-05 19:09:33 -07:00
|
|
|
// store the inverse drawing matrix
|
2015-08-19 15:10:24 -07:00
|
|
|
Matrix4 roundRectDrawingMatrix = getOrthoMatrix();
|
2014-07-29 12:50:14 -07:00
|
|
|
roundRectDrawingMatrix.multiply(*transform);
|
|
|
|
state->matrix.loadInverse(roundRectDrawingMatrix);
|
2014-05-05 19:09:33 -07:00
|
|
|
|
|
|
|
// compute area under rounded corners - only draws overlapping these rects need to be clipped
|
2017-11-03 10:12:19 -07:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-05-05 19:09:33 -07:00
|
|
|
state->dangerRects[i] = bounds;
|
|
|
|
}
|
|
|
|
state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius;
|
|
|
|
state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius;
|
|
|
|
state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius;
|
|
|
|
state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
transform->mapRect(state->dangerRects[i]);
|
|
|
|
|
|
|
|
// round danger rects out as though they are AA geometry (since they essentially are)
|
|
|
|
state->dangerRects[i].snapGeometryToPixelBoundaries(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store RR area
|
2014-07-29 12:50:14 -07:00
|
|
|
state->innerRect = bounds;
|
|
|
|
state->innerRect.inset(radius);
|
|
|
|
state->radius = radius;
|
2014-05-05 19:09:33 -07:00
|
|
|
|
|
|
|
// store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info
|
|
|
|
roundRectClipState = state;
|
|
|
|
}
|
|
|
|
|
2016-07-06 16:10:09 -07:00
|
|
|
void Snapshot::setProjectionPathMask(const SkPath* path) {
|
2016-03-01 13:27:54 -08:00
|
|
|
projectionPathMask = path;
|
2015-04-28 11:45:59 -07:00
|
|
|
}
|
|
|
|
|
2016-04-07 13:51:07 -07:00
|
|
|
static Snapshot* getClipRoot(Snapshot* target) {
|
|
|
|
while (target->previous && target->previous->previous) {
|
|
|
|
target = target->previous;
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClipBase* Snapshot::serializeIntersectedClip(LinearAllocator& allocator,
|
2017-11-03 10:12:19 -07:00
|
|
|
const ClipBase* recordedClip,
|
|
|
|
const Matrix4& recordedClipTransform) {
|
2016-04-07 13:51:07 -07:00
|
|
|
auto target = this;
|
|
|
|
if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) {
|
|
|
|
// Clip must be intersected with root, instead of current clip.
|
|
|
|
target = getClipRoot(this);
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
return target->mClipArea->serializeIntersectedClip(allocator, recordedClip,
|
|
|
|
recordedClipTransform);
|
2016-04-07 13:51:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Snapshot::applyClip(const ClipBase* recordedClip, const Matrix4& transform) {
|
|
|
|
if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) {
|
|
|
|
// current clip is being replaced, but must intersect with clip root
|
|
|
|
*mClipArea = *(getClipRoot(this)->mClipArea);
|
|
|
|
}
|
|
|
|
mClipArea->applyClip(recordedClip, transform);
|
|
|
|
}
|
|
|
|
|
2012-02-02 17:31:16 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Queries
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-03-21 14:39:04 -07:00
|
|
|
void Snapshot::dump() const {
|
2017-11-03 10:12:19 -07:00
|
|
|
ALOGD("Snapshot %p, flags %x, prev %p, height %d, hasComplexClip %d", this, flags, previous,
|
|
|
|
getViewportHeight(), !mClipArea->isSimple());
|
2015-01-06 13:22:54 -08:00
|
|
|
const Rect& clipRect(mClipArea->getClipRect());
|
2017-11-03 10:12:19 -07:00
|
|
|
ALOGD(" ClipRect %.1f %.1f %.1f %.1f, clip simple %d", clipRect.left, clipRect.top,
|
|
|
|
clipRect.right, clipRect.bottom, mClipArea->isSimple());
|
2015-04-06 10:47:23 -07:00
|
|
|
|
2013-03-21 14:39:04 -07:00
|
|
|
ALOGD(" Transform (at %p):", transform);
|
|
|
|
transform->dump();
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|