blob: 3f08009b6be9865960301164c128e711b6f17f59 [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;
Keith Mokde89c2f2016-07-13 14:45:16 -070038 mRelativeLightCenter.x = mRelativeLightCenter.y = mRelativeLightCenter.z = 0;
Romain Guyada4d532012-02-02 17:31:16 -080039}
40
41/**
42 * Copies the specified snapshot/ The specified snapshot is stored as
43 * the previous snapshot.
44 */
John Reckd9ee5502015-10-06 10:06:37 -070045Snapshot::Snapshot(Snapshot* s, int saveFlags)
Chris Craike4aa95e2014-05-08 13:57:05 -070046 : flags(0)
47 , previous(s)
48 , layer(s->layer)
49 , fbo(s->fbo)
Chris Craika64a2be2014-05-14 14:17:01 -070050 , alpha(s->alpha)
Chris Craikdeeda3d2014-05-05 19:09:33 -070051 , roundRectClipState(s->roundRectClipState)
Chris Craikfca52b752015-04-28 11:45:59 -070052 , projectionPathMask(s->projectionPathMask)
Rob Tsuk487a92c2015-01-06 13:22:54 -080053 , mClipArea(nullptr)
Chris Craik69e5adf2014-08-14 13:34:01 -070054 , mViewportData(s->mViewportData)
55 , mRelativeLightCenter(s->mRelativeLightCenter) {
Florin Malitaeecff562015-12-21 10:43:01 -050056 if (saveFlags & SaveFlags::Matrix) {
Chris Craik7c85c542015-08-19 15:10:24 -070057 mTransformRoot = *s->transform;
Romain Guyada4d532012-02-02 17:31:16 -080058 transform = &mTransformRoot;
59 } else {
60 transform = s->transform;
61 }
62
Florin Malitaeecff562015-12-21 10:43:01 -050063 if (saveFlags & SaveFlags::Clip) {
Rob Tsuk487a92c2015-01-06 13:22:54 -080064 mClipAreaRoot = s->getClipArea();
65 mClipArea = &mClipAreaRoot;
Romain Guyada4d532012-02-02 17:31:16 -080066 } else {
Rob Tsuk487a92c2015-01-06 13:22:54 -080067 mClipArea = s->mClipArea;
Romain Guyada4d532012-02-02 17:31:16 -080068 }
Romain Guyada4d532012-02-02 17:31:16 -080069}
70
71///////////////////////////////////////////////////////////////////////////////
72// Clipping
73///////////////////////////////////////////////////////////////////////////////
74
Mike Reed6e49c9f2016-12-02 15:36:59 -050075void Snapshot::clipRegionTransformed(const SkRegion& region, SkClipOp op) {
Romain Guy8ce00302013-01-15 18:51:42 -080076 flags |= Snapshot::kFlagClipSet;
Mike Reed6e49c9f2016-12-02 15:36:59 -050077 mClipArea->clipRegion(region, static_cast<SkRegion::Op>(op));
Romain Guy967e2bf2012-02-07 17:04:34 -080078}
79
Mike Reed6e49c9f2016-12-02 15:36:59 -050080void Snapshot::clip(const Rect& localClip, SkClipOp op) {
Rob Tsuk487a92c2015-01-06 13:22:54 -080081 flags |= Snapshot::kFlagClipSet;
Mike Reed6e49c9f2016-12-02 15:36:59 -050082 mClipArea->clipRectWithTransform(localClip, transform, static_cast<SkRegion::Op>(op));
Romain Guyada4d532012-02-02 17:31:16 -080083}
84
Mike Reed6e49c9f2016-12-02 15:36:59 -050085void Snapshot::clipPath(const SkPath& path, SkClipOp op) {
Rob Tsuk487a92c2015-01-06 13:22:54 -080086 flags |= Snapshot::kFlagClipSet;
Mike Reed6e49c9f2016-12-02 15:36:59 -050087 mClipArea->clipPathWithTransform(path, transform, static_cast<SkRegion::Op>(op));
Romain Guyada4d532012-02-02 17:31:16 -080088}
89
90void Snapshot::setClip(float left, float top, float right, float bottom) {
Romain Guyada4d532012-02-02 17:31:16 -080091 flags |= Snapshot::kFlagClipSet;
Chris Craik4d3e7042015-08-20 12:54:25 -070092 mClipArea->setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -080093}
94
Romain Guya3dc55f2012-09-28 13:55:44 -070095bool Snapshot::hasPerspectiveTransform() const {
96 return transform->isPerspective();
97}
98
Romain Guyada4d532012-02-02 17:31:16 -080099const Rect& Snapshot::getLocalClip() {
100 mat4 inverse;
101 inverse.loadInverse(*transform);
102
Rob Tsuk487a92c2015-01-06 13:22:54 -0800103 mLocalClip.set(mClipArea->getClipRect());
Romain Guyada4d532012-02-02 17:31:16 -0800104 inverse.mapRect(mLocalClip);
105
106 return mLocalClip;
107}
108
109void Snapshot::resetClip(float left, float top, float right, float bottom) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800110 // TODO: This is incorrect, when we start rendering into a new layer,
111 // we may have to modify the previous snapshot's clip rect and clip
112 // region if the previous restore() call did not restore the clip
Rob Tsuk487a92c2015-01-06 13:22:54 -0800113 mClipArea = &mClipAreaRoot;
Romain Guy967e2bf2012-02-07 17:04:34 -0800114 setClip(left, top, right, bottom);
Romain Guyada4d532012-02-02 17:31:16 -0800115}
116
117///////////////////////////////////////////////////////////////////////////////
Chris Craikaf4d04c2014-07-29 12:50:14 -0700118// Clipping round rect
Chris Craikdeeda3d2014-05-05 19:09:33 -0700119///////////////////////////////////////////////////////////////////////////////
120
Chris Craike83cbd42014-09-03 17:52:24 -0700121void Snapshot::setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds,
122 float radius, bool highPriority) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700123 if (bounds.isEmpty()) {
Rob Tsuk487a92c2015-01-06 13:22:54 -0800124 mClipArea->setEmpty();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700125 return;
126 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700127
Chris Craike83cbd42014-09-03 17:52:24 -0700128 if (roundRectClipState && roundRectClipState->highPriority) {
129 // ignore, don't replace, already have a high priority clip
130 return;
131 }
132
Chris Craikdeeda3d2014-05-05 19:09:33 -0700133 RoundRectClipState* state = new (allocator) RoundRectClipState;
134
Chris Craike83cbd42014-09-03 17:52:24 -0700135 state->highPriority = highPriority;
136
Chris Craikdeeda3d2014-05-05 19:09:33 -0700137 // store the inverse drawing matrix
Chris Craik7c85c542015-08-19 15:10:24 -0700138 Matrix4 roundRectDrawingMatrix = getOrthoMatrix();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700139 roundRectDrawingMatrix.multiply(*transform);
140 state->matrix.loadInverse(roundRectDrawingMatrix);
Chris Craikdeeda3d2014-05-05 19:09:33 -0700141
142 // compute area under rounded corners - only draws overlapping these rects need to be clipped
143 for (int i = 0 ; i < 4; i++) {
144 state->dangerRects[i] = bounds;
145 }
146 state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius;
147 state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius;
148 state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius;
149 state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius;
150 for (int i = 0; i < 4; i++) {
151 transform->mapRect(state->dangerRects[i]);
152
153 // round danger rects out as though they are AA geometry (since they essentially are)
154 state->dangerRects[i].snapGeometryToPixelBoundaries(true);
155 }
156
157 // store RR area
Chris Craikaf4d04c2014-07-29 12:50:14 -0700158 state->innerRect = bounds;
159 state->innerRect.inset(radius);
160 state->radius = radius;
Chris Craikdeeda3d2014-05-05 19:09:33 -0700161
162 // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info
163 roundRectClipState = state;
164}
165
Chris Craik5e00c7c2016-07-06 16:10:09 -0700166void Snapshot::setProjectionPathMask(const SkPath* path) {
Chris Craik678ff812016-03-01 13:27:54 -0800167 projectionPathMask = path;
Chris Craikfca52b752015-04-28 11:45:59 -0700168}
169
Chris Craik04d46eb2016-04-07 13:51:07 -0700170static Snapshot* getClipRoot(Snapshot* target) {
171 while (target->previous && target->previous->previous) {
172 target = target->previous;
173 }
174 return target;
175}
176
177const ClipBase* Snapshot::serializeIntersectedClip(LinearAllocator& allocator,
178 const ClipBase* recordedClip, const Matrix4& recordedClipTransform) {
179 auto target = this;
180 if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) {
181 // Clip must be intersected with root, instead of current clip.
182 target = getClipRoot(this);
183 }
184
185 return target->mClipArea->serializeIntersectedClip(allocator,
186 recordedClip, recordedClipTransform);
187}
188
189void Snapshot::applyClip(const ClipBase* recordedClip, const Matrix4& transform) {
190 if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) {
191 // current clip is being replaced, but must intersect with clip root
192 *mClipArea = *(getClipRoot(this)->mClipArea);
193 }
194 mClipArea->applyClip(recordedClip, transform);
195}
196
Chris Craikdeeda3d2014-05-05 19:09:33 -0700197///////////////////////////////////////////////////////////////////////////////
Romain Guyada4d532012-02-02 17:31:16 -0800198// Queries
199///////////////////////////////////////////////////////////////////////////////
200
Chris Craik5f803622013-03-21 14:39:04 -0700201void Snapshot::dump() const {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700202 ALOGD("Snapshot %p, flags %x, prev %p, height %d, hasComplexClip %d",
203 this, flags, previous, getViewportHeight(), !mClipArea->isSimple());
Rob Tsuk487a92c2015-01-06 13:22:54 -0800204 const Rect& clipRect(mClipArea->getClipRect());
Chris Craike9c01a42015-04-06 10:47:23 -0700205 ALOGD(" ClipRect %.1f %.1f %.1f %.1f, clip simple %d",
206 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom, mClipArea->isSimple());
207
Chris Craik5f803622013-03-21 14:39:04 -0700208 ALOGD(" Transform (at %p):", transform);
209 transform->dump();
210}
211
Romain Guyada4d532012-02-02 17:31:16 -0800212}; // namespace uirenderer
213}; // namespace android