blob: 61cbf85303ebffc19f4f5d51f614d895a1f36bd2 [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
17#define LOG_TAG "DamageAccumulator"
18
19#include "DamageAccumulator.h"
20
21#include <cutils/log.h>
22
23#include "RenderNode.h"
24#include "utils/MathUtils.h"
25
26namespace android {
27namespace uirenderer {
28
John Recka447d292014-06-11 18:39:44 -070029NullDamageAccumulator NullDamageAccumulator::sInstance;
30
31NullDamageAccumulator* NullDamageAccumulator::instance() {
32 return &sInstance;
33}
34
35enum TransformType {
John Reck2dc223d2014-06-17 10:46:09 -070036 TransformInvalid = 0,
John Recka447d292014-06-11 18:39:44 -070037 TransformRenderNode,
38 TransformMatrix4,
John Reck25fbb3f2014-06-12 13:46:45 -070039 TransformNone,
John Recka447d292014-06-11 18:39:44 -070040};
41
John Recke4267ea2014-06-03 15:53:15 -070042struct DirtyStack {
John Recka447d292014-06-11 18:39:44 -070043 TransformType type;
44 union {
45 const RenderNode* renderNode;
46 const Matrix4* matrix4;
47 };
John Recke4267ea2014-06-03 15:53:15 -070048 // When this frame is pop'd, this rect is mapped through the above transform
49 // and applied to the previous (aka parent) frame
50 SkRect pendingDirty;
51 DirtyStack* prev;
52 DirtyStack* next;
53};
54
55DamageAccumulator::DamageAccumulator() {
56 mHead = (DirtyStack*) mAllocator.alloc(sizeof(DirtyStack));
57 memset(mHead, 0, sizeof(DirtyStack));
58 // Create a root that we will not pop off
59 mHead->prev = mHead;
John Reck2dc223d2014-06-17 10:46:09 -070060 mHead->type = TransformNone;
John Recke4267ea2014-06-03 15:53:15 -070061}
62
John Recka447d292014-06-11 18:39:44 -070063void DamageAccumulator::pushCommon() {
John Recke4267ea2014-06-03 15:53:15 -070064 if (!mHead->next) {
65 DirtyStack* nextFrame = (DirtyStack*) mAllocator.alloc(sizeof(DirtyStack));
66 nextFrame->next = 0;
67 nextFrame->prev = mHead;
68 mHead->next = nextFrame;
69 }
70 mHead = mHead->next;
John Recke4267ea2014-06-03 15:53:15 -070071 mHead->pendingDirty.setEmpty();
72}
73
John Recka447d292014-06-11 18:39:44 -070074void DamageAccumulator::pushTransform(const RenderNode* transform) {
75 pushCommon();
76 mHead->type = TransformRenderNode;
77 mHead->renderNode = transform;
78}
79
80void DamageAccumulator::pushTransform(const Matrix4* transform) {
81 pushCommon();
82 mHead->type = TransformMatrix4;
83 mHead->matrix4 = transform;
84}
85
John Reck25fbb3f2014-06-12 13:46:45 -070086void DamageAccumulator::pushNullTransform() {
87 pushCommon();
88 mHead->type = TransformNone;
89}
90
John Recka447d292014-06-11 18:39:44 -070091void DamageAccumulator::popTransform() {
John Recke4267ea2014-06-03 15:53:15 -070092 LOG_ALWAYS_FATAL_IF(mHead->prev == mHead, "Cannot pop the root frame!");
93 DirtyStack* dirtyFrame = mHead;
94 mHead = mHead->prev;
John Reck25fbb3f2014-06-12 13:46:45 -070095 switch (dirtyFrame->type) {
96 case TransformRenderNode:
John Recka447d292014-06-11 18:39:44 -070097 applyRenderNodeTransform(dirtyFrame);
John Reck25fbb3f2014-06-12 13:46:45 -070098 break;
99 case TransformMatrix4:
John Recka447d292014-06-11 18:39:44 -0700100 applyMatrix4Transform(dirtyFrame);
John Reck25fbb3f2014-06-12 13:46:45 -0700101 break;
102 case TransformNone:
103 mHead->pendingDirty.join(dirtyFrame->pendingDirty);
104 break;
John Reck2dc223d2014-06-17 10:46:09 -0700105 default:
106 LOG_ALWAYS_FATAL("Tried to pop an invalid type: %d", dirtyFrame->type);
John Recka447d292014-06-11 18:39:44 -0700107 }
108}
109
110static inline void mapRect(const Matrix4* matrix, const SkRect& in, SkRect* out) {
111 if (in.isEmpty()) return;
112 Rect temp(in);
113 matrix->mapRect(temp);
114 out->join(RECT_ARGS(temp));
115}
116
117void DamageAccumulator::applyMatrix4Transform(DirtyStack* frame) {
118 mapRect(frame->matrix4, frame->pendingDirty, &mHead->pendingDirty);
119}
120
121static inline void mapRect(const RenderProperties& props, const SkRect& in, SkRect* out) {
122 if (in.isEmpty()) return;
123 const SkMatrix* transform = props.getTransformMatrix();
124 SkRect temp(in);
125 if (transform && !transform->isIdentity()) {
126 transform->mapRect(&temp);
127 }
128 temp.offset(props.getLeft(), props.getTop());
129 out->join(temp);
130}
131
132static DirtyStack* findParentRenderNode(DirtyStack* frame) {
133 while (frame->prev != frame) {
134 frame = frame->prev;
135 if (frame->type == TransformRenderNode) {
136 return frame;
John Recke4267ea2014-06-03 15:53:15 -0700137 }
John Recka447d292014-06-11 18:39:44 -0700138 }
139 return NULL;
140}
141
142static DirtyStack* findProjectionReceiver(DirtyStack* frame) {
143 if (frame) {
144 while (frame->prev != frame) {
145 frame = frame->prev;
146 if (frame->type == TransformRenderNode
147 && frame->renderNode->hasProjectionReceiver()) {
148 return frame;
John Recke4267ea2014-06-03 15:53:15 -0700149 }
John Recke4267ea2014-06-03 15:53:15 -0700150 }
John Recka447d292014-06-11 18:39:44 -0700151 }
152 return NULL;
153}
154
155static void applyTransforms(DirtyStack* frame, DirtyStack* end) {
156 SkRect* rect = &frame->pendingDirty;
157 while (frame != end) {
158 if (frame->type == TransformRenderNode) {
159 mapRect(frame->renderNode->properties(), *rect, rect);
160 } else {
161 mapRect(frame->matrix4, *rect, rect);
162 }
163 frame = frame->prev;
164 }
165}
166
167void DamageAccumulator::applyRenderNodeTransform(DirtyStack* frame) {
168 if (frame->pendingDirty.isEmpty()) {
169 return;
170 }
171
172 const RenderProperties& props = frame->renderNode->properties();
173
174 // Perform clipping
175 if (props.getClipToBounds() && !frame->pendingDirty.isEmpty()) {
176 if (!frame->pendingDirty.intersect(0, 0, props.getWidth(), props.getHeight())) {
177 frame->pendingDirty.setEmpty();
178 }
179 }
180
181 // apply all transforms
182 mapRect(props, frame->pendingDirty, &mHead->pendingDirty);
183
184 // project backwards if necessary
185 if (props.getProjectBackwards() && !frame->pendingDirty.isEmpty()) {
186 // First, find our parent RenderNode:
187 DirtyStack* parentNode = findParentRenderNode(frame);
188 // Find our parent's projection receiver, which is what we project onto
189 DirtyStack* projectionReceiver = findProjectionReceiver(parentNode);
190 if (projectionReceiver) {
191 applyTransforms(frame, projectionReceiver);
192 projectionReceiver->pendingDirty.join(frame->pendingDirty);
John Recka447d292014-06-11 18:39:44 -0700193 }
194
195 frame->pendingDirty.setEmpty();
John Recke4267ea2014-06-03 15:53:15 -0700196 }
197}
198
199void DamageAccumulator::dirty(float left, float top, float right, float bottom) {
200 mHead->pendingDirty.join(left, top, right, bottom);
201}
202
John Reck25fbb3f2014-06-12 13:46:45 -0700203void DamageAccumulator::peekAtDirty(SkRect* dest) {
204 *dest = mHead->pendingDirty;
205}
206
John Recke4267ea2014-06-03 15:53:15 -0700207void DamageAccumulator::finish(SkRect* totalDirty) {
208 LOG_ALWAYS_FATAL_IF(mHead->prev != mHead, "Cannot finish, mismatched push/pop calls! %p vs. %p", mHead->prev, mHead);
209 // Root node never has a transform, so this is the fully mapped dirty rect
210 *totalDirty = mHead->pendingDirty;
211 totalDirty->roundOut();
212 mHead->pendingDirty.setEmpty();
213}
214
215} /* namespace uirenderer */
216} /* namespace android */