1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <android-base/macros.h>
16 #include <android/hardware_buffer.h>
17 #include <benchmark/benchmark.h>
18 
19 constexpr AHardwareBuffer_Desc k720pDesc = {.width = 1280,
20                                             .height = 720,
21                                             .layers = 1,
22                                             .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
23                                             .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
24                                             .stride = 0};
25 
BM_BufferAllocationDeallocation(benchmark::State & state)26 static void BM_BufferAllocationDeallocation(benchmark::State& state) {
27     AHardwareBuffer* buffer = nullptr;
28     for (auto _ : state) {
29         int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
30         if (UNLIKELY(status != 0)) {
31             state.SkipWithError("Unable to allocate buffer.");
32         }
33         AHardwareBuffer_release(buffer);
34         buffer = nullptr;
35     }
36 }
37 BENCHMARK(BM_BufferAllocationDeallocation);
38 
BM_AHardwareBuffer_Id(benchmark::State & state)39 static void BM_AHardwareBuffer_Id(benchmark::State& state) {
40     AHardwareBuffer* buffer = nullptr;
41     int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
42     if (UNLIKELY(status != 0)) {
43         state.SkipWithError("Unable to allocate buffer.");
44     }
45 
46     for (auto _ : state) {
47         uint64_t id = 0;
48         int status = AHardwareBuffer_getId(buffer, &id);
49         if (UNLIKELY(status != 0)) {
50             state.SkipWithError("Unable to get ID.");
51         }
52     }
53 
54     AHardwareBuffer_release(buffer);
55 }
56 BENCHMARK(BM_AHardwareBuffer_Id);
57 
BM_AHardwareBuffer_Desc(benchmark::State & state)58 static void BM_AHardwareBuffer_Desc(benchmark::State& state) {
59     AHardwareBuffer* buffer = nullptr;
60     int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
61     if (UNLIKELY(status != 0)) {
62         state.SkipWithError("Unable to allocate buffer.");
63     }
64 
65     for (auto _ : state) {
66         AHardwareBuffer_Desc desc = {};
67         AHardwareBuffer_describe(buffer, &desc);
68     }
69 
70     AHardwareBuffer_release(buffer);
71 }
72 BENCHMARK(BM_AHardwareBuffer_Desc);
73 
74 BENCHMARK_MAIN();
75