1 #pragma once
2 
3 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
4 #include <log/log.h>
5 
6 #include "format.h"
7 #include "format_type.h"
8 #include "metadata.h"
9 #include "utils.h"
10 
11 namespace pixel::graphics::mapper {
12 
13 namespace {
14 
15 using ::aidl::android::hardware::graphics::common::PlaneLayout;
16 using android::hardware::graphics::mapper::V4_0::Error;
17 using android::hardware::graphics::mapper::V4_0::IMapper;
18 using HidlPixelFormat = ::android::hardware::graphics::common::V1_2::PixelFormat;
19 using namespace ::pixel::graphics;
20 
get_mapper()21 android::sp<IMapper> get_mapper() {
22     static android::sp<IMapper> mapper = []() {
23         auto mapper = IMapper::getService();
24         if (!mapper) {
25             ALOGE("Failed to get mapper service");
26         }
27         return mapper;
28     }();
29 
30     return mapper;
31 }
32 
33 } // namespace
34 
35 template <MetadataType T>
36 struct always_false : std::false_type {};
37 
38 template <MetadataType T>
39 struct ReturnType {
40     static_assert(always_false<T>::value, "Unspecialized ReturnType is not supported");
41     using type = void;
42 };
43 
44 template <MetadataType T>
get(buffer_handle_t)45 static std::optional<typename ReturnType<T>::type> get(buffer_handle_t /*handle*/) {
46     static_assert(always_false<T>::value, "Unspecialized get is not supported");
47     return {};
48 }
49 
50 // TODO: Add support for stable-c mapper
51 #define GET(metadata, return_type)                                                       \
52     template <>                                                                          \
53     struct ReturnType<MetadataType::metadata> {                                          \
54         using type = return_type;                                                        \
55     };                                                                                   \
56                                                                                          \
57     template <>                                                                          \
58     [[maybe_unused]] std::optional<typename ReturnType<MetadataType::metadata>::type>    \
59     get<MetadataType::metadata>(buffer_handle_t handle) {                                \
60         auto mapper = get_mapper();                                                      \
61         IMapper::MetadataType type = {                                                   \
62                 .name = kPixelMetadataTypeName,                                          \
63                 .value = static_cast<int64_t>(MetadataType::metadata),                   \
64         };                                                                               \
65                                                                                          \
66         android::hardware::hidl_vec<uint8_t> vec;                                        \
67         Error error;                                                                     \
68         auto ret = mapper->get(const_cast<native_handle_t*>(handle), type,               \
69                                [&](const auto& tmpError,                                 \
70                                    const android::hardware::hidl_vec<uint8_t>& tmpVec) { \
71                                    error = tmpError;                                     \
72                                    vec = tmpVec;                                         \
73                                });                                                       \
74         if (!ret.isOk()) {                                                               \
75             return {};                                                                   \
76         }                                                                                \
77                                                                                          \
78         return utils::decode<return_type>(vec);                                          \
79     }
80 
81 #pragma clang diagnostic push
82 #pragma clang diagnostic ignored "-Wunused-function"
83 
84 GET(PLANE_DMA_BUFS, std::vector<int>);
85 GET(VIDEO_HDR, void*);
86 GET(VIDEO_ROI, void*);
87 GET(VIDEO_GMV, VideoGMV);
88 
89 GET(COMPRESSED_PLANE_LAYOUTS, std::vector<CompressedPlaneLayout>);
90 GET(PIXEL_FORMAT_ALLOCATED, Format);
91 GET(FORMAT_TYPE, FormatType);
92 
93 #pragma clang diagnostic pop
94 
95 #undef GET
96 
97 template <MetadataType T>
set(buffer_handle_t,typename ReturnType<T>::type)98 static Error set(buffer_handle_t /*handle*/, typename ReturnType<T>::type /*data*/) {
99     static_assert(always_false<T>::value, "Unspecialized set is not supported");
100     return {};
101 }
102 
103 #define SET(metadata, metadata_typename)                                                  \
104     template <>                                                                           \
105     [[maybe_unused]] Error                                                                \
106     set<MetadataType::metadata>(buffer_handle_t handle,                                   \
107                                 typename ReturnType<MetadataType::metadata>::type data) { \
108         auto mapper = get_mapper();                                                       \
109         auto encoded_data = utils::encode<metadata_typename>(data);                       \
110         IMapper::MetadataType type = {                                                    \
111                 .name = kPixelMetadataTypeName,                                           \
112                 .value = static_cast<int64_t>(MetadataType::metadata),                    \
113         };                                                                                \
114                                                                                           \
115         auto ret = mapper->set(const_cast<native_handle_t*>(handle), type, encoded_data); \
116                                                                                           \
117         return ret;                                                                       \
118     }
119 
120 SET(VIDEO_GMV, VideoGMV);
121 #undef SET
122 
123 } // namespace pixel::graphics::mapper
124