blob: 21c26f7a1d44c10837fe77082ee67446a8a83363 [file] [log] [blame]
Romain Guyada4d532012-02-02 17:31:16 -08001/*
2 * Copyright (C) 2012 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#include "Snapshot.h"
18
sergeyvdccca442016-03-21 15:38:21 -070019#include "hwui/Canvas.h"
Romain Guyada4d532012-02-02 17:31:16 -080020
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Constructors
26///////////////////////////////////////////////////////////////////////////////
27
Chris Craike4aa95e2014-05-08 13:57:05 -070028Snapshot::Snapshot()
29 : flags(0)
Chris Craikd41c4d82015-01-05 15:51:13 -080030 , previous(nullptr)
31 , layer(nullptr)
Chris Craike4aa95e2014-05-08 13:57:05 -070032 , fbo(0)
Chris Craikdeeda3d2014-05-05 19:09:33 -070033 , alpha(1.0f)
Rob Tsuk487a92c2015-01-06 13:22:54 -080034 , roundRectClipState(nullptr)
Chris Craikfca52b752015-04-28 11:45:59 -070035 , projectionPathMask(nullptr)
Rob Tsuk487a92c2015-01-06 13:22:54 -080036 , mClipArea(&mClipAreaRoot) {
Romain Guyada4d532012-02-02 17:31:16 -080037 transform = &mTransformRoot;
Romain Guyada4d532012-02-02 17:31:16 -080038}
39
40/**
41 * Copies the specified snapshot/ The specified snapshot is stored as
42 * the previous snapshot.
43 */
John Reckd9ee5502015-10-06 10:06:37 -070044Snapshot::Snapshot(Snapshot* s, int saveFlags)
Chris Craike4aa95e2014-05-08 13:57:05 -070045 : flags(0)
46 , previous(s)
47 , layer(s->layer)
48 , fbo(s->fbo)
Chris Craika64a2be2014-05-14 14:17:01 -070049 , alpha(s->alpha)
Chris Craikdeeda3d2014-05-05 19:09:33 -070050 , roundRectClipState(s->roundRectClipState)
Chris Craikfca52b752015-04-28 11:45:59 -070051 , projectionPathMask(s->projectionPathMask)
Rob Tsuk487a92c2015-01-06 13:22:54 -080052 , mClipArea(nullptr)
Chris Craik69e5adf2014-08-14 13:34:01 -070053 , mViewportData(s->mViewportData)
54 , mRelativeLightCenter(s->mRelativeLightCenter) {
Florin Malitaeecff562015-12-21 10:43:01 -050055 if (saveFlags & SaveFlags::Matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -070056 mTransformRoot = *s->transform;
Romain Guyada4d532012-02-02 17:31:16 -080057 transform = &mTransformRoot;
58 } else {
59 transform = s->transform;
60 }
61
Florin Malitaeecff562015-12-21 10:43:01 -050062 if (saveFlags & SaveFlags::Clip) {
Rob Tsuk487a92c2015-01-06 13:22:54 -080063 mClipAreaRoot = s->getClipArea();
64 mClipArea = &mClipAreaRoot;
Romain Guyada4d532012-02-02 17:31:16 -080065 } else {
Rob Tsuk487a92c2015-01-06 13:22:54 -080066 mClipArea = s->mClipArea;
Romain Guyada4d532012-02-02 17:31:16 -080067 }
Romain Guyada4d532012-02-02 17:31:16 -080068}
69
70///////////////////////////////////////////////////////////////////////////////
71// Clipping
72///////////////////////////////////////////////////////////////////////////////
73
Chris Craik4d3e7042015-08-20 12:54:25 -070074void Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -080075 flags |= Snapshot::kFlagClipSet;
Chris Craik4d3e7042015-08-20 12:54:25 -070076 mClipArea->clipRegion(region, op);
Romain Guy967e2bf2012-02-07 17:04:34 -080077}
78
Chris Craika2a70722015-12-17 12:58:24 -080079void Snapshot::clip(const Rect& localClip, SkRegion::Op op) {
Rob Tsuk487a92c2015-01-06 13:22:54 -080080 flags |= Snapshot::kFlagClipSet;
Chris Craika2a70722015-12-17 12:58:24 -080081 mClipArea->clipRectWithTransform(localClip, transform, op);
Romain Guyada4d532012-02-02 17:31:16 -080082}
83
Chris Craik4d3e7042015-08-20 12:54:25 -070084void Snapshot::clipPath(const SkPath& path, SkRegion::Op op) {
Rob Tsuk487a92c2015-01-06 13:22:54 -080085 flags |= Snapshot::kFlagClipSet;
Chris Craik4d3e7042015-08-20 12:54:25 -070086 mClipArea->clipPathWithTransform(path, transform, op);
Romain Guyada4d532012-02-02 17:31:16 -080087}
88
89void Snapshot::setClip(float left, float top, float right, float bottom) {
Romain Guyada4d532012-02-02 17:31:16 -080090 flags |= Snapshot::kFlagClipSet;
Chris Craik4d3e7042015-08-20 12:54:25 -070091 mClipArea->setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -080092}
93
Romain Guya3dc55f2012-09-28 13:55:44 -070094bool Snapshot::hasPerspectiveTransform() const {
95 return transform->isPerspective();
96}
97
Romain Guyada4d532012-02-02 17:31:16 -080098const Rect& Snapshot::getLocalClip() {
99 mat4 inverse;
100 inverse.loadInverse(*transform);
101
Rob Tsuk487a92c2015-01-06 13:22:54 -0800102 mLocalClip.set(mClipArea->getClipRect());
Romain Guyada4d532012-02-02 17:31:16 -0800103 inverse.mapRect(mLocalClip);
104
105 return mLocalClip;
106}
107
108void Snapshot::resetClip(float left, float top, float right, float bottom) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800109 // TODO: This is incorrect, when we start rendering into a new layer,
110 // we may have to modify the previous snapshot's clip rect and clip
111 // region if the previous restore() call did not restore the clip
Rob Tsuk487a92c2015-01-06 13:22:54 -0800112 mClipArea = &mClipAreaRoot;
Romain Guy967e2bf2012-02-07 17:04:34 -0800113 setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800114}
115
116///////////////////////////////////////////////////////////////////////////////
Chris Craikaf4d04c2014-07-29 12:50:14 -0700117// Clipping round rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700118///////////////////////////////////////////////////////////////////////////////
119
Chris Craike83cbd42014-09-03 17:52:24 -0700120void Snapshot::setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
121 float radius, bool highPriority) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700122 if (bounds.isEmpty()) {
Rob Tsuk487a92c2015-01-06 13:22:54 -0800123 mClipArea->setEmpty();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700124 return;
125 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700126
Chris Craike83cbd42014-09-03 17:52:24 -0700127 if (roundRectClipState && roundRectClipState->highPriority) {
128 // ignore, don't replace, already have a high priority clip
129 return;
130 }
131
Chris Craikdeeda3d2014-05-05 19:09:33 -0700132 RoundRectClipState* state = new (allocator) RoundRectClipState;
133
Chris Craike83cbd42014-09-03 17:52:24 -0700134 state->highPriority = highPriority;
135
Chris Craikdeeda3d2014-05-05 19:09:33 -0700136 // store the inverse drawing matrix
Chris Craik7c85c542015-08-19 15:10:24 -0700137 Matrix4 roundRectDrawingMatrix = getOrthoMatrix();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700138 roundRectDrawingMatrix.multiply(*transform);
139 state->matrix.loadInverse(roundRectDrawingMatrix);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700140
141 // compute area under rounded corners - only draws overlapping these rects need to be clipped
142 for (int i = 0 ; i < 4; i++) {
143 state->dangerRects[i] = bounds;
144 }
145 state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius;
146 state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius;
147 state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius;
148 state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius;
149 for (int i = 0; i < 4; i++) {
150 transform->mapRect(state->dangerRects[i]);
151
152 // round danger rects out as though they are AA geometry (since they essentially are)
153 state->dangerRects[i].snapGeometryToPixelBoundaries(true);
154 }
155
156 // store RR area
Chris Craikaf4d04c2014-07-29 12:50:14 -0700157 state->innerRect = bounds;
158 state->innerRect.inset(radius);
159 state->radius = radius;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700160
161 // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info
162 roundRectClipState = state;
163}
164
Chris Craik5e00c7c2016-07-06 16:10:09 -0700165void Snapshot::setProjectionPathMask(const SkPath* path) {
Chris Craik678ff812016-03-01 13:27:54 -0800166 projectionPathMask = path;
Chris Craikfca52b752015-04-28 11:45:59 -0700167}
168
Chris Craik04d46eb2016-04-07 13:51:07 -0700169static Snapshot* getClipRoot(Snapshot* target) {
170 while (target->previous && target->previous->previous) {
171 target = target->previous;
172 }
173 return target;
174}
175
176const ClipBase* Snapshot::serializeIntersectedClip(LinearAllocator& allocator,
177 const ClipBase* recordedClip, const Matrix4& recordedClipTransform) {
178 auto target = this;
179 if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) {
180 // Clip must be intersected with root, instead of current clip.
181 target = getClipRoot(this);
182 }
183
184 return target->mClipArea->serializeIntersectedClip(allocator,
185 recordedClip, recordedClipTransform);
186}
187
188void Snapshot::applyClip(const ClipBase* recordedClip, const Matrix4& transform) {
189 if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) {
190 // current clip is being replaced, but must intersect with clip root
191 *mClipArea = *(getClipRoot(this)->mClipArea);
192 }
193 mClipArea->applyClip(recordedClip, transform);
194}
195
Chris Craikdeeda3d2014-05-05 19:09:33 -0700196///////////////////////////////////////////////////////////////////////////////
Romain Guyada4d532012-02-02 17:31:16 -0800197// Queries
198///////////////////////////////////////////////////////////////////////////////
199
Chris Craik5f803622013-03-21 14:39:04 -0700200void Snapshot::dump() const {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700201 ALOGD("Snapshot %p, flags %x, prev %p, height %d, hasComplexClip %d",
202 this, flags, previous, getViewportHeight(), !mClipArea->isSimple());
Rob Tsuk487a92c2015-01-06 13:22:54 -0800203 const Rect& clipRect(mClipArea->getClipRect());
Chris Craike9c01a42015-04-06 10:47:23 -0700204 ALOGD(" ClipRect %.1f %.1f %.1f %.1f, clip simple %d",
205 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom, mClipArea->isSimple());
206
Chris Craik5f803622013-03-21 14:39:04 -0700207 ALOGD(" Transform (at %p):", transform);
208 transform->dump();
209}
210
Romain Guyada4d532012-02-02 17:31:16 -0800211}; // namespace uirenderer
212}; // namespace android