Skip to content

Commit 6bbbbb4

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Add benchmarks for libgui" into main
2 parents bdd2039 + 1c80627 commit 6bbbbb4

3 files changed

Lines changed: 184 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package {
2+
// See: http://go/android-license-faq
3+
// A large-scale-change added 'default_applicable_licenses' to import
4+
// all of the 'license_kinds' from "frameworks_native_license"
5+
// to get the below license kinds:
6+
// SPDX-license-identifier-Apache-2.0
7+
default_applicable_licenses: ["frameworks_native_license"],
8+
}
9+
10+
cc_benchmark {
11+
name: "libgui_benchmarks",
12+
srcs: [
13+
"*.cpp",
14+
],
15+
defaults: ["libgui-defaults"],
16+
static_libs: [
17+
"libgmock",
18+
"libgtest",
19+
],
20+
shared_libs: [
21+
"libgui",
22+
],
23+
header_libs: [
24+
"libsurfaceflinger_mocks_headers",
25+
"surfaceflinger_tests_common_headers",
26+
],
27+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (C) 2024 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+
#include <cstddef>
19+
#include <optional>
20+
#include <vector>
21+
#include "binder/Parcel.h"
22+
#include "gui/SurfaceComposerClient.h"
23+
#include "gui/SurfaceControl.h"
24+
#include "log/log_main.h"
25+
26+
namespace android {
27+
namespace {
28+
using android::hardware::graphics::common::V1_1::BufferUsage;
29+
30+
std::vector<sp<SurfaceControl>> createSurfaceControl(const char* name, size_t num) {
31+
sp<SurfaceComposerClient> client = sp<SurfaceComposerClient>::make();
32+
LOG_FATAL_IF(client->initCheck() != OK, "Could not init SurfaceComposerClient");
33+
std::vector<sp<SurfaceControl>> surfaceControls;
34+
for (size_t i = 0; i < num; i++) {
35+
surfaceControls.push_back(
36+
client->createSurface(String8(name), 0, 0, PIXEL_FORMAT_RGBA_8888,
37+
ISurfaceComposerClient::eFXSurfaceBufferState));
38+
}
39+
return surfaceControls;
40+
}
41+
42+
void applyTransaction(benchmark::State& state) {
43+
std::vector<sp<SurfaceControl>> surfaceControls = createSurfaceControl(__func__, 5 /* num */);
44+
for (auto _ : state) {
45+
SurfaceComposerClient::Transaction t;
46+
for (auto& sc : surfaceControls) {
47+
t.setCrop(sc, FloatRect{1, 2, 3, 4});
48+
t.setAutoRefresh(sc, true);
49+
t.hide(sc);
50+
t.setAlpha(sc, 0.5);
51+
t.setCornerRadius(sc, 0.8);
52+
}
53+
Parcel p;
54+
t.writeToParcel(&p);
55+
t.clear();
56+
benchmark::DoNotOptimize(t);
57+
}
58+
}
59+
BENCHMARK(applyTransaction);
60+
61+
// Mimic a buffer transaction with callbacks
62+
void applyBufferTransaction(benchmark::State& state) {
63+
std::vector<sp<SurfaceControl>> surfaceControls = createSurfaceControl(__func__, 5 /* num */);
64+
std::vector<sp<GraphicBuffer>> buffers;
65+
for (size_t i = 0; i < surfaceControls.size(); i++) {
66+
int64_t usageFlags = BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
67+
BufferUsage::COMPOSER_OVERLAY | BufferUsage::GPU_TEXTURE;
68+
buffers.emplace_back(
69+
sp<GraphicBuffer>::make(5, 5, PIXEL_FORMAT_RGBA_8888, 1, usageFlags, "test"));
70+
}
71+
72+
for (auto _ : state) {
73+
SurfaceComposerClient::Transaction t;
74+
int i = 0;
75+
for (auto& sc : surfaceControls) {
76+
std::function<void(const ReleaseCallbackId&, const sp<Fence>& /*releaseFence*/,
77+
std::optional<uint32_t> currentMaxAcquiredBufferCount)>
78+
releaseBufferCallback;
79+
t.setBuffer(sc, buffers[i], std::nullopt, std::nullopt, 5, releaseBufferCallback);
80+
}
81+
Parcel p;
82+
// proxy for applying the transaction
83+
t.writeToParcel(&p);
84+
t.clear();
85+
benchmark::DoNotOptimize(t);
86+
}
87+
}
88+
BENCHMARK(applyBufferTransaction);
89+
90+
void mergeTransaction(benchmark::State& state) {
91+
std::vector<sp<SurfaceControl>> surfaceControls = createSurfaceControl(__func__, 5 /* num */);
92+
for (auto _ : state) {
93+
SurfaceComposerClient::Transaction t1;
94+
for (auto& sc : surfaceControls) {
95+
t1.setCrop(sc, FloatRect{1, 2, 3, 4});
96+
t1.setAutoRefresh(sc, true);
97+
t1.hide(sc);
98+
t1.setAlpha(sc, 0.5);
99+
t1.setCornerRadius(sc, 0.8);
100+
}
101+
102+
SurfaceComposerClient::Transaction t2;
103+
for (auto& sc : surfaceControls) {
104+
t2.hide(sc);
105+
t2.setAlpha(sc, 0.5);
106+
t2.setCornerRadius(sc, 0.8);
107+
t2.setBackgroundBlurRadius(sc, 5);
108+
}
109+
t1.merge(std::move(t2));
110+
benchmark::DoNotOptimize(t1);
111+
}
112+
}
113+
BENCHMARK(mergeTransaction);
114+
115+
void readTransactionFromParcel(benchmark::State& state) {
116+
std::vector<sp<SurfaceControl>> surfaceControls = createSurfaceControl(__func__, 5 /* num */);
117+
SurfaceComposerClient::Transaction t;
118+
for (auto& sc : surfaceControls) {
119+
t.setCrop(sc, FloatRect{1, 2, 3, 4});
120+
t.setAutoRefresh(sc, true);
121+
t.hide(sc);
122+
t.setAlpha(sc, 0.5);
123+
t.setCornerRadius(sc, 0.8);
124+
}
125+
Parcel p;
126+
t.writeToParcel(&p);
127+
t.clear();
128+
129+
for (auto _ : state) {
130+
SurfaceComposerClient::Transaction t2;
131+
t2.readFromParcel(&p);
132+
p.setDataPosition(0);
133+
benchmark::DoNotOptimize(t2);
134+
}
135+
}
136+
BENCHMARK(readTransactionFromParcel);
137+
138+
} // namespace
139+
} // namespace android

libs/gui/tests/benchmarks/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (C) 2024 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+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)