1 /*
2  * Copyright (C) 2015 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 
17 #include <benchmark/benchmark.h>
18 
19 #include "DisplayList.h"
20 #include "hwui/Canvas.h"
21 #include "pipeline/skia/SkiaDisplayList.h"
22 #include "tests/common/TestUtils.h"
23 
24 using namespace android;
25 using namespace android::uirenderer;
26 
BM_DisplayList_alloc(benchmark::State & benchState)27 void BM_DisplayList_alloc(benchmark::State& benchState) {
28     while (benchState.KeepRunning()) {
29         auto displayList = new skiapipeline::SkiaDisplayList();
30         benchmark::DoNotOptimize(displayList);
31         delete displayList;
32     }
33 }
34 BENCHMARK(BM_DisplayList_alloc);
35 
BM_DisplayList_alloc_theoretical(benchmark::State & benchState)36 void BM_DisplayList_alloc_theoretical(benchmark::State& benchState) {
37     while (benchState.KeepRunning()) {
38         auto displayList = new char[sizeof(skiapipeline::SkiaDisplayList)];
39         benchmark::DoNotOptimize(displayList);
40         delete[] displayList;
41     }
42 }
43 BENCHMARK(BM_DisplayList_alloc_theoretical);
44 
BM_DisplayListCanvas_record_empty(benchmark::State & benchState)45 void BM_DisplayListCanvas_record_empty(benchmark::State& benchState) {
46     std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(100, 100));
47     delete canvas->finishRecording();
48 
49     while (benchState.KeepRunning()) {
50         canvas->resetRecording(100, 100);
51         benchmark::DoNotOptimize(canvas.get());
52         delete canvas->finishRecording();
53     }
54 }
55 BENCHMARK(BM_DisplayListCanvas_record_empty);
56 
BM_DisplayListCanvas_record_saverestore(benchmark::State & benchState)57 void BM_DisplayListCanvas_record_saverestore(benchmark::State& benchState) {
58     std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(100, 100));
59     delete canvas->finishRecording();
60 
61     while (benchState.KeepRunning()) {
62         canvas->resetRecording(100, 100);
63         canvas->save(SaveFlags::MatrixClip);
64         canvas->save(SaveFlags::MatrixClip);
65         benchmark::DoNotOptimize(canvas.get());
66         canvas->restore();
67         canvas->restore();
68         delete canvas->finishRecording();
69     }
70 }
71 BENCHMARK(BM_DisplayListCanvas_record_saverestore);
72 
BM_DisplayListCanvas_record_translate(benchmark::State & benchState)73 void BM_DisplayListCanvas_record_translate(benchmark::State& benchState) {
74     std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(100, 100));
75     delete canvas->finishRecording();
76 
77     while (benchState.KeepRunning()) {
78         canvas->resetRecording(100, 100);
79         canvas->scale(10, 10);
80         benchmark::DoNotOptimize(canvas.get());
81         delete canvas->finishRecording();
82     }
83 }
84 BENCHMARK(BM_DisplayListCanvas_record_translate);
85 
86 /**
87  * Simulate a simple view drawing a background, overlapped by an image.
88  *
89  * Note that the recording commands are intentionally not perfectly efficient, as the
90  * View system frequently produces unneeded save/restores.
91  */
BM_DisplayListCanvas_record_simpleBitmapView(benchmark::State & benchState)92 void BM_DisplayListCanvas_record_simpleBitmapView(benchmark::State& benchState) {
93     std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(100, 100));
94     delete canvas->finishRecording();
95 
96     SkPaint rectPaint;
97     sk_sp<Bitmap> iconBitmap(TestUtils::createBitmap(80, 80));
98 
99     while (benchState.KeepRunning()) {
100         canvas->resetRecording(100, 100);
101         {
102             canvas->save(SaveFlags::MatrixClip);
103             canvas->drawRect(0, 0, 100, 100, rectPaint);
104             canvas->restore();
105         }
106         {
107             canvas->save(SaveFlags::MatrixClip);
108             canvas->translate(10, 10);
109             canvas->drawBitmap(*iconBitmap, 0, 0, nullptr);
110             canvas->restore();
111         }
112         benchmark::DoNotOptimize(canvas.get());
113         delete canvas->finishRecording();
114     }
115 }
116 BENCHMARK(BM_DisplayListCanvas_record_simpleBitmapView);
117 
BM_DisplayListCanvas_basicViewGroupDraw(benchmark::State & benchState)118 void BM_DisplayListCanvas_basicViewGroupDraw(benchmark::State& benchState) {
119     sp<RenderNode> child = TestUtils::createNode(50, 50, 100, 100, [](auto& props, auto& canvas) {
120         canvas.drawColor(0xFFFFFFFF, SkBlendMode::kSrcOver);
121     });
122 
123     std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(100, 100));
124     delete canvas->finishRecording();
125 
126     while (benchState.KeepRunning()) {
127         canvas->resetRecording(200, 200);
128         canvas->translate(0, 0);  // mScrollX, mScrollY
129 
130         // Clip to padding
131         // Can expect ~25% of views to have clip to padding with a non-null padding
132         int clipRestoreCount = canvas->save(SaveFlags::MatrixClip);
133         canvas->clipRect(1, 1, 199, 199, SkClipOp::kIntersect);
134 
135         canvas->insertReorderBarrier(true);
136 
137         // Draw child loop
138         for (int i = 0; i < benchState.range(0); i++) {
139             canvas->drawRenderNode(child.get());
140         }
141 
142         canvas->insertReorderBarrier(false);
143         canvas->restoreToCount(clipRestoreCount);
144 
145         delete canvas->finishRecording();
146     }
147 }
148 BENCHMARK(BM_DisplayListCanvas_basicViewGroupDraw)->Arg(1)->Arg(5)->Arg(10);
149