2013-12-30 15:32:54 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 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.
|
|
|
|
*/
|
|
|
|
|
2014-05-05 19:09:33 -07:00
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
|
|
|
|
2013-12-30 15:32:54 -08:00
|
|
|
#include <SkCanvas.h>
|
|
|
|
|
|
|
|
#include "StatefulBaseRenderer.h"
|
|
|
|
|
2014-07-29 12:50:14 -07:00
|
|
|
#include "utils/MathUtils.h"
|
|
|
|
|
2013-12-30 15:32:54 -08:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2014-07-23 18:19:28 -07:00
|
|
|
StatefulBaseRenderer::StatefulBaseRenderer()
|
|
|
|
: mDirtyClip(false)
|
|
|
|
, mWidth(-1)
|
|
|
|
, mHeight(-1)
|
|
|
|
, mSaveCount(1)
|
|
|
|
, mFirstSnapshot(new Snapshot)
|
|
|
|
, mSnapshot(mFirstSnapshot) {
|
2013-12-30 15:32:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::initializeSaveStack(float clipLeft, float clipTop,
|
2014-08-14 13:34:01 -07:00
|
|
|
float clipRight, float clipBottom, const Vector3& lightCenter) {
|
2013-12-30 15:32:54 -08:00
|
|
|
mSnapshot = new Snapshot(mFirstSnapshot,
|
|
|
|
SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
|
|
|
|
mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom);
|
|
|
|
mSnapshot->fbo = getTargetFbo();
|
2014-08-14 13:34:01 -07:00
|
|
|
mSnapshot->setRelativeLightCenter(lightCenter);
|
2013-12-30 15:32:54 -08:00
|
|
|
mSaveCount = 1;
|
|
|
|
}
|
|
|
|
|
2014-05-20 18:10:25 -07:00
|
|
|
void StatefulBaseRenderer::setViewport(int width, int height) {
|
2013-12-30 15:32:54 -08:00
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
2014-05-14 14:17:01 -07:00
|
|
|
mFirstSnapshot->initializeViewport(width, height);
|
2014-05-20 18:10:25 -07:00
|
|
|
onViewportInitialized();
|
2014-09-18 16:05:35 -07:00
|
|
|
|
|
|
|
// create a temporary 1st snapshot, so old snapshots are released,
|
|
|
|
// and viewport can be queried safely.
|
|
|
|
// TODO: remove, combine viewport + save stack initialization
|
|
|
|
mSnapshot = new Snapshot(mFirstSnapshot,
|
|
|
|
SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
|
|
|
|
mSaveCount = 1;
|
2014-05-20 18:10:25 -07:00
|
|
|
}
|
|
|
|
|
2013-12-30 15:32:54 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Save (layer)
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Non-virtual implementation of save, guaranteed to save without side-effects
|
|
|
|
*
|
|
|
|
* The approach here and in restoreSnapshot(), allows subclasses to directly manipulate the save
|
|
|
|
* stack, and ensures restoreToCount() doesn't call back into subclass overrides.
|
|
|
|
*/
|
|
|
|
int StatefulBaseRenderer::saveSnapshot(int flags) {
|
|
|
|
mSnapshot = new Snapshot(mSnapshot, flags);
|
|
|
|
return mSaveCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StatefulBaseRenderer::save(int flags) {
|
|
|
|
return saveSnapshot(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Non-virtual implementation of restore, guaranteed to restore without side-effects.
|
|
|
|
*/
|
|
|
|
void StatefulBaseRenderer::restoreSnapshot() {
|
|
|
|
sp<Snapshot> toRemove = mSnapshot;
|
|
|
|
sp<Snapshot> toRestore = mSnapshot->previous;
|
|
|
|
|
|
|
|
mSaveCount--;
|
|
|
|
mSnapshot = toRestore;
|
|
|
|
|
|
|
|
// subclass handles restore implementation
|
|
|
|
onSnapshotRestored(*toRemove, *toRestore);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::restore() {
|
|
|
|
if (mSaveCount > 1) {
|
|
|
|
restoreSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::restoreToCount(int saveCount) {
|
|
|
|
if (saveCount < 1) saveCount = 1;
|
|
|
|
|
|
|
|
while (mSaveCount > saveCount) {
|
|
|
|
restoreSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Matrix
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::getMatrix(SkMatrix* matrix) const {
|
|
|
|
mSnapshot->transform->copyTo(*matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::translate(float dx, float dy, float dz) {
|
|
|
|
mSnapshot->transform->translate(dx, dy, dz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::rotate(float degrees) {
|
|
|
|
mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::scale(float sx, float sy) {
|
|
|
|
mSnapshot->transform->scale(sx, sy, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::skew(float sx, float sy) {
|
|
|
|
mSnapshot->transform->skew(sx, sy);
|
|
|
|
}
|
|
|
|
|
2013-12-10 12:28:58 -05:00
|
|
|
void StatefulBaseRenderer::setMatrix(const SkMatrix& matrix) {
|
|
|
|
mSnapshot->transform->load(matrix);
|
2013-12-30 15:32:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::setMatrix(const Matrix4& matrix) {
|
|
|
|
mSnapshot->transform->load(matrix);
|
|
|
|
}
|
|
|
|
|
2013-12-10 12:28:58 -05:00
|
|
|
void StatefulBaseRenderer::concatMatrix(const SkMatrix& matrix) {
|
|
|
|
mat4 transform(matrix);
|
2013-12-30 15:32:54 -08:00
|
|
|
mSnapshot->transform->multiply(transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatefulBaseRenderer::concatMatrix(const Matrix4& matrix) {
|
|
|
|
mSnapshot->transform->multiply(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clip
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-01 14:45:21 -08:00
|
|
|
bool StatefulBaseRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
|
|
|
|
if (CC_LIKELY(currentTransform()->rectToRect())) {
|
|
|
|
mDirtyClip |= mSnapshot->clip(left, top, right, bottom, op);
|
|
|
|
return !mSnapshot->clipRect->isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.addRect(left, top, right, bottom);
|
|
|
|
|
|
|
|
return StatefulBaseRenderer::clipPath(&path, op);
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
bool StatefulBaseRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
|
2014-01-01 14:45:21 -08:00
|
|
|
SkMatrix transform;
|
|
|
|
currentTransform()->copyTo(transform);
|
|
|
|
|
|
|
|
SkPath transformed;
|
|
|
|
path->transform(transform, &transformed);
|
|
|
|
|
|
|
|
SkRegion clip;
|
|
|
|
if (!mSnapshot->previous->clipRegion->isEmpty()) {
|
|
|
|
clip.setRegion(*mSnapshot->previous->clipRegion);
|
|
|
|
} else {
|
|
|
|
if (mSnapshot->previous == firstSnapshot()) {
|
|
|
|
clip.setRect(0, 0, getWidth(), getHeight());
|
|
|
|
} else {
|
|
|
|
Rect* bounds = mSnapshot->previous->clipRect;
|
|
|
|
clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRegion region;
|
|
|
|
region.setPath(transformed, clip);
|
|
|
|
|
2014-07-29 10:35:13 -07:00
|
|
|
// region is the transformed input path, masked by the previous clip
|
2014-01-01 14:45:21 -08:00
|
|
|
mDirtyClip |= mSnapshot->clipRegionTransformed(region, op);
|
|
|
|
return !mSnapshot->clipRect->isEmpty();
|
|
|
|
}
|
|
|
|
|
2014-01-02 17:13:34 -08:00
|
|
|
bool StatefulBaseRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
|
2014-01-01 14:45:21 -08:00
|
|
|
mDirtyClip |= mSnapshot->clipRegionTransformed(*region, op);
|
|
|
|
return !mSnapshot->clipRect->isEmpty();
|
|
|
|
}
|
2013-12-30 15:32:54 -08:00
|
|
|
|
2014-05-05 19:09:33 -07:00
|
|
|
void StatefulBaseRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
|
2014-07-29 12:50:14 -07:00
|
|
|
Rect bounds;
|
|
|
|
float radius;
|
|
|
|
if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported
|
|
|
|
|
2014-08-21 12:26:16 -07:00
|
|
|
bool outlineIsRounded = MathUtils::isPositive(radius);
|
|
|
|
if (!outlineIsRounded || currentTransform()->isSimple()) {
|
2014-07-29 12:50:14 -07:00
|
|
|
// TODO: consider storing this rect separately, so that this can't be replaced with clip ops
|
|
|
|
clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkRegion::kIntersect_Op);
|
|
|
|
}
|
2014-08-21 12:26:16 -07:00
|
|
|
if (outlineIsRounded) {
|
2014-09-03 17:52:24 -07:00
|
|
|
setClippingRoundRect(allocator, bounds, radius, false);
|
2014-08-21 12:26:16 -07:00
|
|
|
}
|
2014-05-05 19:09:33 -07:00
|
|
|
}
|
|
|
|
|
2014-07-29 12:50:14 -07:00
|
|
|
void StatefulBaseRenderer::setClippingRoundRect(LinearAllocator& allocator,
|
2014-09-03 17:52:24 -07:00
|
|
|
const Rect& rect, float radius, bool highPriority) {
|
|
|
|
mSnapshot->setClippingRoundRect(allocator, rect, radius, highPriority);
|
2014-07-29 12:50:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 15:32:54 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Quick Rejection
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates whether content drawn within the passed bounds would be outside of, or intersect with
|
|
|
|
* the clipRect. Does not modify the scissor.
|
|
|
|
*
|
|
|
|
* @param clipRequired if not null, will be set to true if element intersects clip
|
|
|
|
* (and wasn't rejected)
|
|
|
|
*
|
|
|
|
* @param snapOut if set, the geometry will be treated as having an AA ramp.
|
|
|
|
* See Rect::snapGeometryToPixelBoundaries()
|
|
|
|
*/
|
|
|
|
bool StatefulBaseRenderer::calculateQuickRejectForScissor(float left, float top,
|
2014-05-05 19:09:33 -07:00
|
|
|
float right, float bottom,
|
|
|
|
bool* clipRequired, bool* roundRectClipRequired,
|
|
|
|
bool snapOut) const {
|
2013-12-30 15:32:54 -08:00
|
|
|
if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect r(left, top, right, bottom);
|
2014-01-01 14:45:21 -08:00
|
|
|
currentTransform()->mapRect(r);
|
2013-12-30 15:32:54 -08:00
|
|
|
r.snapGeometryToPixelBoundaries(snapOut);
|
|
|
|
|
2014-01-01 14:45:21 -08:00
|
|
|
Rect clipRect(*currentClipRect());
|
2013-12-30 15:32:54 -08:00
|
|
|
clipRect.snapToPixelBoundaries();
|
|
|
|
|
|
|
|
if (!clipRect.intersects(r)) return true;
|
|
|
|
|
|
|
|
// clip is required if geometry intersects clip rect
|
2014-05-05 19:09:33 -07:00
|
|
|
if (clipRequired) {
|
|
|
|
*clipRequired = !clipRect.contains(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// round rect clip is required if RR clip exists, and geometry intersects its corners
|
|
|
|
if (roundRectClipRequired) {
|
|
|
|
*roundRectClipRequired = mSnapshot->roundRectClipState != NULL
|
|
|
|
&& mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r);
|
|
|
|
}
|
2013-12-30 15:32:54 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns false if drawing won't be clipped out.
|
|
|
|
*
|
|
|
|
* Makes the decision conservatively, by rounding out the mapped rect before comparing with the
|
|
|
|
* clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
|
|
|
|
* rejection is still desired.
|
|
|
|
*
|
|
|
|
* This function, unlike quickRejectSetupScissor, should be used where precise geometry information
|
|
|
|
* isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
|
|
|
|
* rejection where precise rejection isn't important, or precise information isn't available.
|
|
|
|
*/
|
|
|
|
bool StatefulBaseRenderer::quickRejectConservative(float left, float top,
|
|
|
|
float right, float bottom) const {
|
|
|
|
if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect r(left, top, right, bottom);
|
2014-01-01 14:45:21 -08:00
|
|
|
currentTransform()->mapRect(r);
|
2013-12-30 15:32:54 -08:00
|
|
|
r.roundOut(); // rounded out to be conservative
|
|
|
|
|
2014-01-01 14:45:21 -08:00
|
|
|
Rect clipRect(*currentClipRect());
|
2013-12-30 15:32:54 -08:00
|
|
|
clipRect.snapToPixelBoundaries();
|
|
|
|
|
|
|
|
if (!clipRect.intersects(r)) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|