xref: /aosp_15_r20/external/minigbm/cros_gralloc/cros_gralloc_buffer_metadata.h (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
1 /*
2  * Copyright 2022 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #ifndef CROSGRALLOC4METADATA_H
8 #define CROSGRALLOC4METADATA_H
9 
10 #include <optional>
11 #include <type_traits>
12 
13 #include <aidl/android/hardware/graphics/common/BlendMode.h>
14 #include <aidl/android/hardware/graphics/common/Cta861_3.h>
15 #include <aidl/android/hardware/graphics/common/Dataspace.h>
16 #include <aidl/android/hardware/graphics/common/Smpte2086.h>
17 
18 #include "cros_gralloc_helpers.h"
19 
20 // Simple replacement for std::optional which is not guarenteed to be memory layout
21 // stable across ABIs.
22 template <typename T> struct cros_buffer_optional {
23 
24 	enum class state : uint32_t { VACANT, OCCUPIED };
25 
26 	cros_buffer_optional() = default;
27 
cros_buffer_optionalcros_buffer_optional28 	cros_buffer_optional(std::optional<T> v) : occupancy(v ? state::OCCUPIED : state::VACANT)
29 	{
30 		if (v) {
31 			value = *v;
32 		}
33 	}
34 
to_std_optionalcros_buffer_optional35 	std::optional<T> to_std_optional() const
36 	{
37 		return occupancy == state::VACANT ? std::nullopt : std::make_optional(value);
38 	}
39 
40 	state occupancy = state::VACANT;
41 	T value;
42 };
43 
44 /*
45  * The metadata for cros_gralloc_buffer-s that should reside in a shared memory region
46  * instead of directly in cros_gralloc_handle-s.
47  *
48  * Any metadata that is mutable must be stored in this shared memory region as
49  * cros_gralloc_handle-s can not be tracked and updated across processes.
50  */
51 struct cros_gralloc_buffer_metadata {
52 	/*
53 	 * Name is stored in the shared memory metadata to simplify cros_gralloc_handle
54 	 * creation. This allows us to keep handles small while avoiding variable sized
55 	 * handles.
56 	 */
57 	char name[CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE];
58 	aidl::android::hardware::graphics::common::BlendMode blend_mode;
59 	aidl::android::hardware::graphics::common::Dataspace dataspace;
60 	cros_buffer_optional<aidl::android::hardware::graphics::common::Cta861_3> cta861_3;
61 	cros_buffer_optional<aidl::android::hardware::graphics::common::Smpte2086> smpte2086;
62 };
63 
64 static_assert(std::is_standard_layout_v<cros_gralloc_buffer_metadata>);
65 static_assert(std::is_trivially_copyable_v<cros_gralloc_buffer_metadata>);
66 
67 #endif
68