blob: 55912967da20f07a5211bb40cd38d51fe82be2bd [file] [log] [blame]
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +03001/*
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#ifndef ANDROID_BUFFERINFOGETTER_H_
18#define ANDROID_BUFFERINFOGETTER_H_
19
20#include <drm/drm_fourcc.h>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020021#include <hardware/gralloc.h>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030022
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020023#include <optional>
24
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020025#include "BufferInfo.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030026#include "drm/DrmDevice.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030027
28#ifndef DRM_FORMAT_INVALID
29#define DRM_FORMAT_INVALID 0
30#endif
31
32namespace android {
33
Roman Stratiienko74d2c4a2021-12-17 18:10:57 +020034using BufferUniqueId = uint64_t;
35
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030036class BufferInfoGetter {
37 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020038 virtual ~BufferInfoGetter() = default;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030039
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020040 virtual auto GetBoInfo(buffer_handle_t handle)
41 -> std::optional<BufferInfo> = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030042
Roman Stratiienko74d2c4a2021-12-17 18:10:57 +020043 virtual std::optional<BufferUniqueId> GetUniqueId(buffer_handle_t handle);
44
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030045 static BufferInfoGetter *GetInstance();
46
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030047 static bool IsDrmFormatRgb(uint32_t drm_format);
48};
49
50class LegacyBufferInfoGetter : public BufferInfoGetter {
51 public:
52 using BufferInfoGetter::BufferInfoGetter;
53
54 int Init();
55
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020056 virtual int ValidateGralloc() {
57 return 0;
58 }
59
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020060 static std::unique_ptr<LegacyBufferInfoGetter> CreateInstance();
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030061
62 static uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020063
64 // NOLINTNEXTLINE:(readability-identifier-naming)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030065 const gralloc_module_t *gralloc_;
66};
67
Roman Stratiienkoe3983342021-02-15 15:41:53 +020068#ifdef DISABLE_LEGACY_GETTERS
69#define LEGACY_BUFFER_INFO_GETTER(getter_)
70#else
Roman Stratiienkofc014f52021-12-23 19:04:29 +020071// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020072#define LEGACY_BUFFER_INFO_GETTER(getter_) \
73 std::unique_ptr<LegacyBufferInfoGetter> \
74 LegacyBufferInfoGetter::CreateInstance() { \
75 auto instance = std::make_unique<getter_>(); \
76 if (instance) { \
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020077 int err = instance->Init(); \
78 if (err) { \
79 ALOGE("Failed to initialize the " #getter_ " getter %d", err); \
80 instance.reset(); \
81 } \
82 err = instance->ValidateGralloc(); \
83 if (err) { \
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020084 instance.reset(); \
85 } \
86 } \
87 return std::move(instance); \
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030088 }
Roman Stratiienkoe3983342021-02-15 15:41:53 +020089#endif
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030090
91} // namespace android
92#endif