blob: 2b4fe17d424dc6954008d92653a15eece0deb7d2 [file] [log] [blame]
John Recke4267ea2014-06-03 15:53:15 -07001/*
2 * Copyright (C) 2014 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
John Recke4267ea2014-06-03 15:53:15 -070017#include "DamageAccumulator.h"
18
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070019#include <log/log.h>
John Recke4267ea2014-06-03 15:53:15 -070020
21#include "RenderNode.h"
22#include "utils/MathUtils.h"
23
24namespace android {
25namespace uirenderer {
26
John Recka447d292014-06-11 18:39:44 -070027enum TransformType {
John Reck2dc223d2014-06-17 10:46:09 -070028 TransformInvalid = 0,
John Recka447d292014-06-11 18:39:44 -070029 TransformRenderNode,
30 TransformMatrix4,
John Reck25fbb3f2014-06-12 13:46:45 -070031 TransformNone,
John Recka447d292014-06-11 18:39:44 -070032};
33
John Recke4267ea2014-06-03 15:53:15 -070034struct DirtyStack {
John Recka447d292014-06-11 18:39:44 -070035 TransformType type;
36 union {
37 const RenderNode* renderNode;
38 const Matrix4* matrix4;
39 };
John Recke4267ea2014-06-03 15:53:15 -070040 // When this frame is pop'd, this rect is mapped through the above transform
41 // and applied to the previous (aka parent) frame
42 SkRect pendingDirty;
43 DirtyStack* prev;
44 DirtyStack* next;
45};
46
47DamageAccumulator::DamageAccumulator() {
John Reck7df9ff22016-02-10 16:08:08 -080048 mHead = mAllocator.create_trivial<DirtyStack>();
John Recke4267ea2014-06-03 15:53:15 -070049 memset(mHead, 0, sizeof(DirtyStack));
50 // Create a root that we will not pop off
51 mHead->prev = mHead;
John Reck2dc223d2014-06-17 10:46:09 -070052 mHead->type = TransformNone;
John Recke4267ea2014-06-03 15:53:15 -070053}
54
Chris Craik69e5adf2014-08-14 13:34:01 -070055static void computeTransformImpl(const DirtyStack* currentFrame, Matrix4* outMatrix) {
56 if (currentFrame->prev != currentFrame) {
57 computeTransformImpl(currentFrame->prev, outMatrix);
58 }
59 switch (currentFrame->type) {
60 case TransformRenderNode:
61 currentFrame->renderNode->applyViewPropertyTransforms(*outMatrix);
62 break;
63 case TransformMatrix4:
64 outMatrix->multiply(*currentFrame->matrix4);
65 break;
66 case TransformNone:
67 // nothing to be done
68 break;
69 default:
70 LOG_ALWAYS_FATAL("Tried to compute transform with an invalid type: %d", currentFrame->type);
71 }
72}
73
74void DamageAccumulator::computeCurrentTransform(Matrix4* outMatrix) const {
75 outMatrix->loadIdentity();
76 computeTransformImpl(mHead, outMatrix);
77}
78
John Recka447d292014-06-11 18:39:44 -070079void DamageAccumulator::pushCommon() {
John Recke4267ea2014-06-03 15:53:15 -070080 if (!mHead->next) {
John Reck7df9ff22016-02-10 16:08:08 -080081 DirtyStack* nextFrame = mAllocator.create_trivial<DirtyStack>();
Chris Craikd41c4d82015-01-05 15:51:13 -080082 nextFrame->next = nullptr;
John Recke4267ea2014-06-03 15:53:15 -070083 nextFrame->prev = mHead;
84 mHead->next = nextFrame;
85 }
86 mHead = mHead->next;
John Recke4267ea2014-06-03 15:53:15 -070087 mHead->pendingDirty.setEmpty();
88}
89
John Recka447d292014-06-11 18:39:44 -070090void DamageAccumulator::pushTransform(const RenderNode* transform) {
91 pushCommon();
92 mHead->type = TransformRenderNode;
93 mHead->renderNode = transform;
94}
95
96void DamageAccumulator::pushTransform(const Matrix4* transform) {
97 pushCommon();
98 mHead->type = TransformMatrix4;
99 mHead->matrix4 = transform;
100}
101
102void DamageAccumulator::popTransform() {
John Recke4267ea2014-06-03 15:53:15 -0700103 LOG_ALWAYS_FATAL_IF(mHead->prev == mHead, "Cannot pop the root frame!");
104 DirtyStack* dirtyFrame = mHead;
105 mHead = mHead->prev;
John Reck25fbb3f2014-06-12 13:46:45 -0700106 switch (dirtyFrame->type) {
107 case TransformRenderNode:
John Recka447d292014-06-11 18:39:44 -0700108 applyRenderNodeTransform(dirtyFrame);
John Reck25fbb3f2014-06-12 13:46:45 -0700109 break;
110 case TransformMatrix4:
John Recka447d292014-06-11 18:39:44 -0700111 applyMatrix4Transform(dirtyFrame);
John Reck25fbb3f2014-06-12 13:46:45 -0700112 break;
113 case TransformNone:
114 mHead->pendingDirty.join(dirtyFrame->pendingDirty);
115 break;
John Reck2dc223d2014-06-17 10:46:09 -0700116 default:
117 LOG_ALWAYS_FATAL("Tried to pop an invalid type: %d", dirtyFrame->type);
John Recka447d292014-06-11 18:39:44 -0700118 }
119}
120
121static inline void mapRect(const Matrix4* matrix, const SkRect& in, SkRect* out) {
122 if (in.isEmpty()) return;
123 Rect temp(in);
John Reckc1288232015-08-12 13:39:11 -0700124 if (CC_LIKELY(!matrix->isPerspective())) {
125 matrix->mapRect(temp);
126 } else {
127 // Don't attempt to calculate damage for a perspective transform
128 // as the numbers this works with can break the perspective
129 // calculations. Just give up and expand to DIRTY_MIN/DIRTY_MAX
130 temp.set(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
131 }
John Recka447d292014-06-11 18:39:44 -0700132 out->join(RECT_ARGS(temp));
133}
134
135void DamageAccumulator::applyMatrix4Transform(DirtyStack* frame) {
136 mapRect(frame->matrix4, frame->pendingDirty, &mHead->pendingDirty);
137}
138
139static inline void mapRect(const RenderProperties& props, const SkRect& in, SkRect* out) {
140 if (in.isEmpty()) return;
141 const SkMatrix* transform = props.getTransformMatrix();
142 SkRect temp(in);
143 if (transform && !transform->isIdentity()) {
John Reckc1288232015-08-12 13:39:11 -0700144 if (CC_LIKELY(!transform->hasPerspective())) {
145 transform->mapRect(&temp);
146 } else {
147 // Don't attempt to calculate damage for a perspective transform
148 // as the numbers this works with can break the perspective
149 // calculations. Just give up and expand to DIRTY_MIN/DIRTY_MAX
150 temp.set(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
151 }
John Recka447d292014-06-11 18:39:44 -0700152 }
153 temp.offset(props.getLeft(), props.getTop());
154 out->join(temp);
155}
156
157static DirtyStack* findParentRenderNode(DirtyStack* frame) {
158 while (frame->prev != frame) {
159 frame = frame->prev;
160 if (frame->type == TransformRenderNode) {
161 return frame;
John Recke4267ea2014-06-03 15:53:15 -0700162 }
John Recka447d292014-06-11 18:39:44 -0700163 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800164 return nullptr;
John Recka447d292014-06-11 18:39:44 -0700165}
166
167static DirtyStack* findProjectionReceiver(DirtyStack* frame) {
168 if (frame) {
169 while (frame->prev != frame) {
170 frame = frame->prev;
171 if (frame->type == TransformRenderNode
172 && frame->renderNode->hasProjectionReceiver()) {
173 return frame;
John Recke4267ea2014-06-03 15:53:15 -0700174 }
John Recke4267ea2014-06-03 15:53:15 -0700175 }
John Recka447d292014-06-11 18:39:44 -0700176 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800177 return nullptr;
John Recka447d292014-06-11 18:39:44 -0700178}
179
180static void applyTransforms(DirtyStack* frame, DirtyStack* end) {
181 SkRect* rect = &frame->pendingDirty;
182 while (frame != end) {
183 if (frame->type == TransformRenderNode) {
184 mapRect(frame->renderNode->properties(), *rect, rect);
185 } else {
186 mapRect(frame->matrix4, *rect, rect);
187 }
188 frame = frame->prev;
189 }
190}
191
192void DamageAccumulator::applyRenderNodeTransform(DirtyStack* frame) {
193 if (frame->pendingDirty.isEmpty()) {
194 return;
195 }
196
197 const RenderProperties& props = frame->renderNode->properties();
John Reckce9f3082014-06-17 16:18:09 -0700198 if (props.getAlpha() <= 0) {
199 return;
200 }
John Recka447d292014-06-11 18:39:44 -0700201
202 // Perform clipping
John Reck293e8682014-06-17 10:34:02 -0700203 if (props.getClipDamageToBounds() && !frame->pendingDirty.isEmpty()) {
John Recka447d292014-06-11 18:39:44 -0700204 if (!frame->pendingDirty.intersect(0, 0, props.getWidth(), props.getHeight())) {
205 frame->pendingDirty.setEmpty();
206 }
207 }
208
209 // apply all transforms
210 mapRect(props, frame->pendingDirty, &mHead->pendingDirty);
211
212 // project backwards if necessary
213 if (props.getProjectBackwards() && !frame->pendingDirty.isEmpty()) {
214 // First, find our parent RenderNode:
215 DirtyStack* parentNode = findParentRenderNode(frame);
216 // Find our parent's projection receiver, which is what we project onto
217 DirtyStack* projectionReceiver = findProjectionReceiver(parentNode);
218 if (projectionReceiver) {
219 applyTransforms(frame, projectionReceiver);
220 projectionReceiver->pendingDirty.join(frame->pendingDirty);
John Recka447d292014-06-11 18:39:44 -0700221 }
222
223 frame->pendingDirty.setEmpty();
John Recke4267ea2014-06-03 15:53:15 -0700224 }
225}
226
227void DamageAccumulator::dirty(float left, float top, float right, float bottom) {
228 mHead->pendingDirty.join(left, top, right, bottom);
229}
230
Chris Craikc71bfca2014-08-21 10:18:58 -0700231void DamageAccumulator::peekAtDirty(SkRect* dest) const {
John Reck25fbb3f2014-06-12 13:46:45 -0700232 *dest = mHead->pendingDirty;
233}
234
John Recke4267ea2014-06-03 15:53:15 -0700235void DamageAccumulator::finish(SkRect* totalDirty) {
236 LOG_ALWAYS_FATAL_IF(mHead->prev != mHead, "Cannot finish, mismatched push/pop calls! %p vs. %p", mHead->prev, mHead);
237 // Root node never has a transform, so this is the fully mapped dirty rect
238 *totalDirty = mHead->pendingDirty;
Mike Reed71487eb2014-11-19 16:13:20 -0500239 totalDirty->roundOut(totalDirty);
John Recke4267ea2014-06-03 15:53:15 -0700240 mHead->pendingDirty.setEmpty();
241}
242
243} /* namespace uirenderer */
244} /* namespace android */