| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 , 100 , 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 |
} |