1295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck/*
2295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * Copyright (C) 2016 The Android Open Source Project
3295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck *
4295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * you may not use this file except in compliance with the License.
6295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * You may obtain a copy of the License at
7295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck *
8295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck *
10295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * Unless required by applicable law or agreed to in writing, software
11295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * See the License for the specific language governing permissions and
14295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck * limitations under the License.
15295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck */
16295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
17295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckpackage android.graphics.perftests;
18295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
19295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.graphics.Bitmap;
20295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.graphics.Color;
21295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.graphics.Bitmap.Config;
22295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.graphics.Paint;
23295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.perftests.utils.BenchmarkState;
24295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.perftests.utils.PerfStatusReporter;
25295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.support.test.filters.LargeTest;
26295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.view.DisplayListCanvas;
27295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport android.view.RenderNode;
28295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
29295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport org.junit.Rule;
30295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckimport org.junit.Test;
31295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
32295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck@LargeTest
33295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reckpublic class CanvasPerfTest {
34295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    @Rule
35295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
36295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
37295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    @Test
38295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    public void testBasicViewGroupDraw() {
39295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        // This test is a clone of BM_DisplayListCanvas_basicViewGroupDraw
40295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
41295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
42295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        RenderNode node = RenderNode.create("benchmark", null);
43295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        RenderNode child = RenderNode.create("child", null);
44295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        child.setLeftTopRightBottom(50, 50, 100, 100);
45295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
46295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        DisplayListCanvas canvas = node.start(100, 100);
47295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        node.end(canvas);
48295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        canvas = child.start(50, 50);
49295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        canvas.drawColor(Color.WHITE);
50295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        child.end(canvas);
51295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
52295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        while (state.keepRunning()) {
53295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            canvas = node.start(200, 200);
54295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            int save = canvas.save();
55295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            canvas.clipRect(1, 1, 199, 199);
56295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            canvas.insertReorderBarrier();
57295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            for (int i = 0; i < 5; i++) {
58295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.drawRenderNode(child);
59295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            }
60295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            canvas.insertInorderBarrier();
61295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            canvas.restoreToCount(save);
62295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            node.end(canvas);
63295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        }
64295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    }
65295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
66295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    @Test
67295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    public void testRecordSimpleBitmapView() {
68295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        // This test is a clone of BM_DisplayListCanvas_record_simpleBitmapView
69295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
70295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
71295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        RenderNode node = RenderNode.create("benchmark", null);
72295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
73295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        DisplayListCanvas canvas = node.start(100, 100);
74295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        node.end(canvas);
75295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        Bitmap bitmap = Bitmap.createBitmap(80, 80, Config.ARGB_8888);
76295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        Paint paint = new Paint();
77295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        paint.setColor(Color.BLACK);
78295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck
79295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        while (state.keepRunning()) {
80295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            canvas = node.start(100, 100);
81295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            {
82295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.save();
83295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.drawRect(0, 0, 100, 100, paint);
84295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.restore();
85295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            }
86295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            {
87295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.save();
88295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.translate(10, 10);
89295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.drawBitmap(bitmap, 0, 0, null);
90295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck                canvas.restore();
91295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            }
92295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck            node.end(canvas);
93295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck        }
94295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck    }
95295f9af9a87d1a201f6771ac5bc73ecf4939d753John Reck}
96