Debian Codegraph @

apct-tests/perftests/core/src/android/text/StaticLayoutCreateDrawPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16 package android.text;
17
18 import static android.text.Layout.Alignment.ALIGN_NORMAL;
19
20 import android.graphics.Canvas;
21 import android.graphics.RecordingCanvas;
22 import android.graphics.RenderNode;
23 import android.perftests.utils.BenchmarkState;
24 import android.perftests.utils.PerfStatusReporter;
25 import android.text.NonEditableTextGenerator.TextType;
26
27 import androidx.test.filters.LargeTest;
28
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.Random;
38
39 /**
40 * Performance test for multi line, single style {@link StaticLayout} creation/draw.
41 */
42 @LargeTest
43 @RunWith(Parameterized.class)
44 public class StaticLayoutCreateDrawPerfTest {
45
46 private static final boolean[] BOOLEANS = new boolean[]{false, true};
47
48 private static final float SPACING_ADD = 10f;
49 private static final float SPACING_MULT = 1.5f;
50
51 @Rule
52 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
53
54 @Parameterized.Parameters(name = "cached {3} {1}chars {0}")
55 public static Collection cases() {
56 final List<Object[]> params = new ArrayList<>();
57 for (int length : new int[]{128}) {
58 for (boolean cached : BOOLEANS) {
59 for (TextType textType : new TextType[]{TextType.STRING,
60 TextType.SPANNABLE_BUILDER}) {
61 params.add(new Object[]{textType.name(), length, textType, cached});
62 }
63 }
64 }
65 return params;
66 }
67
68 private final int mLineWidth;
69 private final int mLength;
70 private final TextType mTextType;
71 private final boolean mCached;
72 private final TextPaint mTextPaint;
73
74 public StaticLayoutCreateDrawPerfTest(String label, int length, TextType textType,
75 boolean cached) {
76 mLength = length;
77 mTextType = textType;
78 mCached = cached;
79 mTextPaint = new TextPaint();
80 mTextPaint.setTextSize(10);
81 mLineWidth = Integer.MAX_VALUE;
82 }
83
84 /**
85 * Measures the creation time for a multi line {@link StaticLayout}.
86 */
87 @Test
88 public void timeCreate() throws Exception {
89 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
90
91 state.pauseTiming();
92 Canvas.freeTextLayoutCaches();
93 final CharSequence text = createRandomText(mLength);
94 createLayout(text);
95 state.resumeTiming();
96
97 while (state.keepRunning()) {
98 state.pauseTiming();
99 if (!mCached) Canvas.freeTextLayoutCaches();
100 state.resumeTiming();
101
102 createLayout(text);
103 }
104 }
105
106 /**
107 * Measures the draw time for a multi line {@link StaticLayout}.
108 */
109 @Test
110 public void timeDraw() throws Exception {
111 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
112
113 state.pauseTiming();
114 Canvas.freeTextLayoutCaches();
115 final RenderNode node = RenderNode.create("benchmark", null);
116 final CharSequence text = createRandomText(mLength);
117 final Layout layout = createLayout(text);
118 state.resumeTiming();
119
120 while (state.keepRunning()) {
121
122 state.pauseTiming();
123 final RecordingCanvas canvas = node.start(1200, 200);
124 int save = canvas.save();
125 if (!mCached) Canvas.freeTextLayoutCaches();
126 state.resumeTiming();
127
128 layout.draw(canvas);
129
130 state.pauseTiming();
131 canvas.restoreToCount(save);
132 node.end(canvas);
133 state.resumeTiming();
134 }
135 }
136
137 private Layout createLayout(CharSequence text) {
138 return StaticLayout.Builder.obtain(text, 0 /*start*/, text.length() /*end*/, mTextPaint,
139 mLineWidth)
140 .setAlignment(ALIGN_NORMAL)
141 .setIncludePad(true)
142 .setLineSpacing(SPACING_ADD, SPACING_MULT)
143 .build();
144 }
145
146 private CharSequence createRandomText(int length) {
147 return new NonEditableTextGenerator(new Random(0))
148 .setSequenceLength(length)
149 .setTextType(mTextType)
150 .build();
151 }
152 }