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