xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/BufferInfoGetter.h (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <drm/drm_fourcc.h>
20 #include <hardware/gralloc.h>
21 
22 #include <optional>
23 
24 #include "BufferInfo.h"
25 #include "drm/DrmDevice.h"
26 
27 #ifndef DRM_FORMAT_INVALID
28 #define DRM_FORMAT_INVALID 0
29 #endif
30 
31 namespace android {
32 
33 using BufferUniqueId = uint64_t;
34 
35 class BufferInfoGetter {
36  public:
37   virtual ~BufferInfoGetter() = default;
38 
39   virtual auto GetBoInfo(buffer_handle_t handle)
40       -> std::optional<BufferInfo> = 0;
41 
42   virtual std::optional<BufferUniqueId> GetUniqueId(buffer_handle_t handle);
43 
44   static BufferInfoGetter *GetInstance();
45 
46   static bool IsDrmFormatRgb(uint32_t drm_format);
47 };
48 
49 class LegacyBufferInfoGetter : public BufferInfoGetter {
50  public:
51   using BufferInfoGetter::BufferInfoGetter;
52 
53   int Init();
54 
ValidateGralloc()55   virtual int ValidateGralloc() {
56     return 0;
57   }
58 
59   static std::unique_ptr<LegacyBufferInfoGetter> CreateInstance();
60 
61   static uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
62 
63   // NOLINTNEXTLINE:(readability-identifier-naming)
64   const gralloc_module_t *gralloc_;
65 };
66 
67 #ifdef DISABLE_LEGACY_GETTERS
68 #define LEGACY_BUFFER_INFO_GETTER(getter_)
69 #else
70 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
71 #define LEGACY_BUFFER_INFO_GETTER(getter_)                             \
72   std::unique_ptr<LegacyBufferInfoGetter>                              \
73   LegacyBufferInfoGetter::CreateInstance() {                           \
74     auto instance = std::make_unique<getter_>();                       \
75     if (instance) {                                                    \
76       int err = instance->Init();                                      \
77       if (err) {                                                       \
78         ALOGE("Failed to initialize the " #getter_ " getter %d", err); \
79         instance.reset();                                              \
80       }                                                                \
81       err = instance->ValidateGralloc();                               \
82       if (err) {                                                       \
83         instance.reset();                                              \
84       }                                                                \
85     }                                                                  \
86     return std::move(instance);                                        \
87   }
88 #endif
89 
90 }  // namespace android
91