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