Debian Codegraph @

apct-tests/perftests/core/src/android/text/PaintMeasureDrawPerfTest.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 android.graphics.Canvas;
19 import android.graphics.Paint;
20 import android.graphics.RecordingCanvas;
21 import android.graphics.RenderNode;
22 import android.perftests.utils.BenchmarkState;
23 import android.perftests.utils.PerfStatusReporter;
24
25 import androidx.test.filters.LargeTest;
26
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.Parameterized;
31
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Random;
36
37 /**
38 * Performance test for single line measure and draw using {@link Paint} and {@link Canvas}.
39 */
40 @LargeTest
41 @RunWith(Parameterized.class)
42 public class PaintMeasureDrawPerfTest {
43
44 private static final boolean[] BOOLEANS = new boolean[]{false, true};
45
46 @Parameterized.Parameters(name = "cached {1} {0}chars")
47 public static Collection cases() {
48 final List<Object[]> params = new ArrayList<>();
49 for (int length : new int[]{128}) {
50 for (boolean cached : BOOLEANS) {
51 params.add(new Object[]{length, cached});
52 }
53 }
54 return params;
55 }
56
57 @Rule
58 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
59
60 private final int mLength;
61 private final boolean mCached;
62 private final TextPaint mTextPaint;
63
64
65 public PaintMeasureDrawPerfTest(int length, boolean cached) {
66 mLength = length;
67 mCached = cached;
68 mTextPaint = new TextPaint();
69 mTextPaint.setTextSize(10);
70 }
71
72 /**
73 * Measure the time for {@link Paint#measureText(String)}
74 */
75 @Test
76 public void timeMeasure() throws Exception {
77 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
78
79 state.pauseTiming();
80 Canvas.freeTextLayoutCaches();
81 final String text = createRandomText();
82 if (mCached) mTextPaint.measureText(text);
83 state.resumeTiming();
84
85 while (state.keepRunning()) {
86 state.pauseTiming();
87 if (!mCached) Canvas.freeTextLayoutCaches();
88 state.resumeTiming();
89
90 mTextPaint.measureText(text);
91 }
92 }
93
94 /**
95 * Measures the time for {@link Canvas#drawText(String, float, float, Paint)}
96 */
97 @Test
98 public void timeDraw() throws Throwable {
99 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
100
101 state.pauseTiming();
102 Canvas.freeTextLayoutCaches();
103 final RenderNode node = RenderNode.create("benchmark", null);
104 final String text = createRandomText();
105 if (mCached) mTextPaint.measureText(text);
106 state.resumeTiming();
107
108 while (state.keepRunning()) {
109
110 state.pauseTiming();
111 final RecordingCanvas canvas = node.start(1200, 200);
112 final int save = canvas.save();
113 if (!mCached) Canvas.freeTextLayoutCaches();
114 state.resumeTiming();
115
116 canvas.drawText(text, 0 /*x*/, 100 /*y*/, mTextPaint);
117
118 state.pauseTiming();
119 canvas.restoreToCount(save);
120 node.end(canvas);
121 state.resumeTiming();
122 }
123 }
124
125 private String createRandomText() {
126 return (String) new NonEditableTextGenerator(new Random(0))
127 .setSequenceLength(mLength)
128 .setCreateBoring(true)
129 .setTextType(NonEditableTextGenerator.TextType.STRING)
130 .build();
131 }
132 }