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.Bitmap;
20import android.graphics.Color;
21import android.graphics.Bitmap.Config;
22import android.graphics.Paint;
23import android.perftests.utils.BenchmarkState;
24import android.perftests.utils.PerfStatusReporter;
25import android.support.test.filters.LargeTest;
26import android.view.DisplayListCanvas;
27import android.view.RenderNode;
28
29import org.junit.Rule;
30import org.junit.Test;
31
32@LargeTest
33public class CanvasPerfTest {
34    @Rule
35    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
36
37    @Test
38    public void testBasicViewGroupDraw() {
39        // This test is a clone of BM_DisplayListCanvas_basicViewGroupDraw
40
41        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
42        RenderNode node = RenderNode.create("benchmark", null);
43        RenderNode child = RenderNode.create("child", null);
44        child.setLeftTopRightBottom(50, 50, 100, 100);
45
46        DisplayListCanvas canvas = node.start(100, 100);
47        node.end(canvas);
48        canvas = child.start(50, 50);
49        canvas.drawColor(Color.WHITE);
50        child.end(canvas);
51
52        while (state.keepRunning()) {
53            canvas = node.start(200, 200);
54            int save = canvas.save();
55            canvas.clipRect(1, 1, 199, 199);
56            canvas.insertReorderBarrier();
57            for (int i = 0; i < 5; i++) {
58                canvas.drawRenderNode(child);
59            }
60            canvas.insertInorderBarrier();
61            canvas.restoreToCount(save);
62            node.end(canvas);
63        }
64    }
65
66    @Test
67    public void testRecordSimpleBitmapView() {
68        // This test is a clone of BM_DisplayListCanvas_record_simpleBitmapView
69
70        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
71        RenderNode node = RenderNode.create("benchmark", null);
72
73        DisplayListCanvas canvas = node.start(100, 100);
74        node.end(canvas);
75        Bitmap bitmap = Bitmap.createBitmap(80, 80, Config.ARGB_8888);
76        Paint paint = new Paint();
77        paint.setColor(Color.BLACK);
78
79        while (state.keepRunning()) {
80            canvas = node.start(100, 100);
81            {
82                canvas.save();
83                canvas.drawRect(0, 0, 100, 100, paint);
84                canvas.restore();
85            }
86            {
87                canvas.save();
88                canvas.translate(10, 10);
89                canvas.drawBitmap(bitmap, 0, 0, null);
90                canvas.restore();
91            }
92            node.end(canvas);
93        }
94    }
95}
96