Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ba294d7d authored by Jim Shargo's avatar Jim Shargo
Browse files

nativewindow: Add more benchmarks to evaluate FFI costs

This CL adds two new benchmarks, one for getting an AHardwareBuffer's ID
and another for getting its description.

These should (and do) ultimately take the same amount of time.

Test: just adding new tests
Merged-In: I5e09a2736ab829ca465aaa4073b439a605d49b5a
Change-Id: I7ee6dd4e989c85fe18221f51a998ae1c25221800
parent e4680d75
Loading
Loading
Loading
Loading
+44 −8
Original line number Diff line number Diff line
@@ -16,16 +16,17 @@
#include <android/hardware_buffer.h>
#include <benchmark/benchmark.h>

static void BM_BufferAllocationDeallocation(benchmark::State& state) {
    AHardwareBuffer_Desc buffer_desc = {.width = 1280,
constexpr AHardwareBuffer_Desc k720pDesc = {.width = 1280,
                                            .height = 720,
                                            .layers = 1,
                                            .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
                                            .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
                                            .stride = 0};

static void BM_BufferAllocationDeallocation(benchmark::State& state) {
    AHardwareBuffer* buffer = nullptr;
    for (auto _ : state) {
        int status = AHardwareBuffer_allocate(&buffer_desc, &buffer);
        int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
        if (UNLIKELY(status != 0)) {
            state.SkipWithError("Unable to allocate buffer.");
        }
@@ -35,4 +36,39 @@ static void BM_BufferAllocationDeallocation(benchmark::State& state) {
}
BENCHMARK(BM_BufferAllocationDeallocation);

static void BM_AHardwareBuffer_Id(benchmark::State& state) {
    AHardwareBuffer* buffer = nullptr;
    int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
    if (UNLIKELY(status != 0)) {
        state.SkipWithError("Unable to allocate buffer.");
    }

    for (auto _ : state) {
        uint64_t id = 0;
        int status = AHardwareBuffer_getId(buffer, &id);
        if (UNLIKELY(status != 0)) {
            state.SkipWithError("Unable to get ID.");
        }
    }

    AHardwareBuffer_release(buffer);
}
BENCHMARK(BM_AHardwareBuffer_Id);

static void BM_AHardwareBuffer_Desc(benchmark::State& state) {
    AHardwareBuffer* buffer = nullptr;
    int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
    if (UNLIKELY(status != 0)) {
        state.SkipWithError("Unable to allocate buffer.");
    }

    for (auto _ : state) {
        AHardwareBuffer_Desc desc = {};
        AHardwareBuffer_describe(buffer, &desc);
    }

    AHardwareBuffer_release(buffer);
}
BENCHMARK(BM_AHardwareBuffer_Desc);

BENCHMARK_MAIN();
+25 −5
Original line number Diff line number Diff line
@@ -20,20 +20,40 @@
use criterion::*;
use nativewindow::*;

fn allocate_deallocate() {
    let buffer = AHardwareBuffer::new(
#[inline]
fn create_720p_buffer() -> AHardwareBuffer {
    AHardwareBuffer::new(
        1280,
        720,
        1,
        AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
        AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
    )
    .unwrap();
    drop(buffer);
    .unwrap()
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("allocate_deallocate", |b| b.iter(allocate_deallocate));
    c.bench_function("allocate_deallocate", |b| {
        b.iter(|| {
            let buffer = create_720p_buffer();
            drop(buffer);
        })
    });

    let buffer = create_720p_buffer();
    c.bench_with_input(BenchmarkId::new("id", "buffer"), &buffer, |b, buffer| {
        b.iter(|| {
            buffer.id();
        })
    });

    // This benchmark exercises getters that need to fetch data via an
    // underlying call to AHardwareBuffer_describe.
    c.bench_with_input(BenchmarkId::new("desc", "buffer"), &buffer, |b, buffer| {
        b.iter(|| {
            buffer.width();
        })
    });
}

criterion_group!(benches, criterion_benchmark);