1/*
2 * Copyright (C) 2015 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 <gtest/gtest.h>
18
19#include <DamageAccumulator.h>
20#include <Matrix.h>
21#include <RenderNode.h>
22#include <utils/LinearAllocator.h>
23
24#include <SkRect.h>
25
26using namespace android;
27using namespace android::uirenderer;
28
29// Test that push & pop are propegating the dirty rect
30// There is no transformation of the dirty rect, the input is the same
31// as the output.
32TEST(DamageAccumulator, identity) {
33    DamageAccumulator da;
34    SkRect curDirty;
35    da.pushTransform(&Matrix4::identity());
36    da.dirty(50, 50, 100, 100);
37    {
38        da.pushTransform(&Matrix4::identity());
39        da.peekAtDirty(&curDirty);
40        ASSERT_EQ(SkRect(), curDirty);
41        da.popTransform();
42    }
43    da.peekAtDirty(&curDirty);
44    ASSERT_EQ(SkRect::MakeLTRB(50, 50, 100, 100), curDirty);
45    da.popTransform();
46    da.finish(&curDirty);
47    ASSERT_EQ(SkRect::MakeLTRB(50, 50, 100, 100), curDirty);
48}
49
50// Test that transformation is happening at the correct levels via
51// peekAtDirty & popTransform. Just uses a simple translate to test this
52TEST(DamageAccumulator, translate) {
53    DamageAccumulator da;
54    Matrix4 translate;
55    SkRect curDirty;
56    translate.loadTranslate(25, 25, 0);
57    da.pushTransform(&translate);
58    da.dirty(50, 50, 100, 100);
59    da.peekAtDirty(&curDirty);
60    ASSERT_EQ(SkRect::MakeLTRB(50, 50, 100, 100), curDirty);
61    da.popTransform();
62    da.finish(&curDirty);
63    ASSERT_EQ(SkRect::MakeLTRB(75, 75, 125, 125), curDirty);
64}
65
66// Test that dirty rectangles are being unioned across "siblings
67TEST(DamageAccumulator, union) {
68    DamageAccumulator da;
69    SkRect curDirty;
70    da.pushTransform(&Matrix4::identity());
71    {
72        da.pushTransform(&Matrix4::identity());
73        da.dirty(50, 50, 100, 100);
74        da.popTransform();
75        da.pushTransform(&Matrix4::identity());
76        da.dirty(150, 50, 200, 125);
77        da.popTransform();
78    }
79    da.popTransform();
80    da.finish(&curDirty);
81    ASSERT_EQ(SkRect::MakeLTRB(50, 50, 200, 125), curDirty);
82}
83
84TEST(DamageAccumulator, basicRenderNode) {
85    DamageAccumulator da;
86    RenderNode node1;
87    node1.animatorProperties().setLeftTopRightBottom(50, 50, 500, 500);
88    node1.animatorProperties().updateMatrix();
89    da.pushTransform(&node1);
90    {
91        RenderNode node2;
92        node2.animatorProperties().setLeftTopRightBottom(50, 50, 100, 100);
93        node2.animatorProperties().updateMatrix();
94        da.pushTransform(&node2);
95        da.dirty(0, 0, 25, 25);
96        da.popTransform();
97    }
98    da.popTransform();
99    SkRect dirty;
100    da.finish(&dirty);
101    ASSERT_EQ(SkRect::MakeLTRB(100, 100, 125, 125), dirty);
102}
103
104TEST(DamageAccumulator, perspectiveTransform) {
105    DamageAccumulator da;
106    RenderNode node1;
107    node1.animatorProperties().setLeftTopRightBottom(50, 50, 500, 500);
108    node1.animatorProperties().setClipToBounds(true);
109    node1.animatorProperties().updateMatrix();
110    da.pushTransform(&node1);
111    {
112        RenderNode node2;
113        node2.animatorProperties().setLeftTopRightBottom(50, 50, 100, 100);
114        node2.animatorProperties().setClipToBounds(false);
115        node2.animatorProperties().setRotationX(1.0f);
116        node2.animatorProperties().setRotationY(1.0f);
117        node2.animatorProperties().setRotation(20.0f);
118        node2.animatorProperties().setCameraDistance(500.0f);
119        node2.animatorProperties().setTranslationZ(30.0f);
120        node2.animatorProperties().updateMatrix();
121        da.pushTransform(&node2);
122        da.dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
123        da.popTransform();
124    }
125    da.popTransform();
126    SkRect dirty;
127    da.finish(&dirty);
128    ASSERT_EQ(SkRect::MakeLTRB(50, 50, 500, 500), dirty);
129}
130