1/*
2 * Copyright (C) 2016 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
17package android.graphics.perftests;
18
19import android.graphics.Outline;
20import android.perftests.utils.BenchmarkState;
21import android.perftests.utils.PerfStatusReporter;
22import android.support.test.filters.LargeTest;
23import android.view.DisplayListCanvas;
24import android.view.RenderNode;
25
26import org.junit.Rule;
27import org.junit.Test;
28
29import java.util.ArrayList;
30
31@LargeTest
32public class RenderNodePerfTest {
33    @Rule
34    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
35
36    @Test
37    public void testMeasureRenderNodeJniOverhead() {
38        final RenderNode node = RenderNode.create("benchmark", null);
39        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
40
41        while (state.keepRunning()) {
42            node.setTranslationX(1.0f);
43        }
44    }
45
46    @Test
47    public void testCreateRenderNodeNoName() {
48        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
49        while (state.keepRunning()) {
50            RenderNode node = RenderNode.create(null, null);
51            node.destroy();
52        }
53    }
54
55    @Test
56    public void testCreateRenderNode() {
57        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
58        while (state.keepRunning()) {
59            RenderNode node = RenderNode.create("LinearLayout", null);
60            node.destroy();
61        }
62    }
63
64    @Test
65    public void testIsValid() {
66        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
67        RenderNode node = RenderNode.create("LinearLayout", null);
68        while (state.keepRunning()) {
69            node.isValid();
70        }
71    }
72
73    @Test
74    public void testStartEnd() {
75        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
76        RenderNode node = RenderNode.create("LinearLayout", null);
77        while (state.keepRunning()) {
78            DisplayListCanvas canvas = node.start(100, 100);
79            node.end(canvas);
80        }
81    }
82
83    @Test
84    public void testStartEndDeepHierarchy() {
85        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
86        RenderNode[] nodes = new RenderNode[30];
87        DisplayListCanvas[] canvases = new DisplayListCanvas[nodes.length];
88        for (int i = 0; i < nodes.length; i++) {
89            nodes[i] = RenderNode.create("LinearLayout", null);
90        }
91
92        while (state.keepRunning()) {
93            for (int i = 0; i < nodes.length; i++) {
94                canvases[i] = nodes[i].start(100, 100);
95            }
96            for (int i = nodes.length - 1; i >= 0; i--) {
97                nodes[i].end(canvases[i]);
98            }
99        }
100    }
101
102    @Test
103    public void testHasIdentityMatrix() {
104        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
105        RenderNode node = RenderNode.create("LinearLayout", null);
106        while (state.keepRunning()) {
107            node.hasIdentityMatrix();
108        }
109    }
110
111    @Test
112    public void testSetOutline() {
113        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
114        RenderNode node = RenderNode.create("LinearLayout", null);
115        Outline a = new Outline();
116        a.setRoundRect(0, 0, 100, 100, 10);
117        Outline b = new Outline();
118        b.setRect(50, 50, 150, 150);
119        b.setAlpha(0.5f);
120
121        while (state.keepRunning()) {
122            node.setOutline(a);
123            node.setOutline(b);
124        }
125    }
126}
127