| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text; |
| 18 |
|
| 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 |
import androidx.test.runner.AndroidJUnit4; |
| 27 |
|
| 28 |
import org.junit.Before; |
| 29 |
import org.junit.Rule; |
| 30 |
import org.junit.Test; |
| 31 |
import org.junit.runner.RunWith; |
| 32 |
|
| 33 |
import java.util.Random; |
| 34 |
|
| 35 |
@LargeTest |
| 36 |
@RunWith(AndroidJUnit4.class) |
| 37 |
public class VariableFontPerfTest { |
| 38 |
private static final int WORD_LENGTH = 9; |
| 39 |
private static final boolean NO_STYLE_TEXT = false; |
| 40 |
|
| 41 |
private static final TextPaint PAINT = new TextPaint(); |
| 42 |
|
| 43 |
public VariableFontPerfTest() {} |
| 44 |
|
| 45 |
@Rule |
| 46 |
public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 47 |
|
| 48 |
private final TextPerfUtils mTextUtil = new TextPerfUtils(); |
| 49 |
|
| 50 |
@Before |
| 51 |
public void setUp() { |
| 52 |
mTextUtil.resetRandom(0 ); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void testDraw_SetVariationOnce() { |
| 57 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 58 |
final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); |
| 59 |
final Paint paint = new Paint(PAINT); |
| 60 |
paint.setFontVariationSettings("'wght' 700"); |
| 61 |
final RenderNode node = RenderNode.create("benchmark", null); |
| 62 |
while (state.keepRunning()) { |
| 63 |
state.pauseTiming(); |
| 64 |
final RecordingCanvas c = node.beginRecording(1200, 200); |
| 65 |
state.resumeTiming(); |
| 66 |
|
| 67 |
c.drawText(text, 0, text.length(), 0, 100, paint); |
| 68 |
|
| 69 |
state.pauseTiming(); |
| 70 |
node.endRecording(); |
| 71 |
state.resumeTiming(); |
| 72 |
|
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void testDraw_SetVariationEachDraw() { |
| 78 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 79 |
final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); |
| 80 |
final Paint paint = new Paint(PAINT); |
| 81 |
final RenderNode node = RenderNode.create("benchmark", null); |
| 82 |
while (state.keepRunning()) { |
| 83 |
state.pauseTiming(); |
| 84 |
final RecordingCanvas c = node.beginRecording(1200, 200); |
| 85 |
paint.setFontVariationSettings("'wght' 700"); |
| 86 |
state.resumeTiming(); |
| 87 |
|
| 88 |
c.drawText(text, 0, text.length(), 0, 100, paint); |
| 89 |
|
| 90 |
state.pauseTiming(); |
| 91 |
node.endRecording(); |
| 92 |
state.resumeTiming(); |
| 93 |
|
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Test |
| 98 |
public void testDraw_SetDifferentVariationEachDraw() { |
| 99 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 100 |
final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); |
| 101 |
final Paint paint = new Paint(PAINT); |
| 102 |
final RenderNode node = RenderNode.create("benchmark", null); |
| 103 |
final Random random = new Random(0); |
| 104 |
while (state.keepRunning()) { |
| 105 |
state.pauseTiming(); |
| 106 |
final RecordingCanvas c = node.beginRecording(1200, 200); |
| 107 |
int weight = random.nextInt(1000); |
| 108 |
paint.setFontVariationSettings("'wght' " + weight); |
| 109 |
state.resumeTiming(); |
| 110 |
|
| 111 |
c.drawText(text, 0, text.length(), 0, 100, paint); |
| 112 |
|
| 113 |
state.pauseTiming(); |
| 114 |
node.endRecording(); |
| 115 |
state.resumeTiming(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void testSetFontVariationSettings() { |
| 121 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 122 |
final Paint paint = new Paint(PAINT); |
| 123 |
final Random random = new Random(0); |
| 124 |
while (state.keepRunning()) { |
| 125 |
state.pauseTiming(); |
| 126 |
int weight = random.nextInt(1000); |
| 127 |
state.resumeTiming(); |
| 128 |
|
| 129 |
paint.setFontVariationSettings("'wght' " + weight); |
| 130 |
} |
| 131 |
} |
| 132 |
} |