blob: 4d35faac658614aba837b223157c1ce80d0e1927 [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
34class BufferInfoGetter {
35 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020036 virtual ~BufferInfoGetter() = default;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030037
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020038 virtual auto GetBoInfo(buffer_handle_t handle)
39 -> std::optional<BufferInfo> = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030040
41 bool IsHandleUsable(buffer_handle_t handle);
42
43 static BufferInfoGetter *GetInstance();
44
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030045 static bool IsDrmFormatRgb(uint32_t drm_format);
46};
47
48class LegacyBufferInfoGetter : public BufferInfoGetter {
49 public:
50 using BufferInfoGetter::BufferInfoGetter;
51
52 int Init();
53
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020054 virtual int ValidateGralloc() {
55 return 0;
56 }
57
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020058 static std::unique_ptr<LegacyBufferInfoGetter> CreateInstance();
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030059
60 static uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020061
62 // NOLINTNEXTLINE:(readability-identifier-naming)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030063 const gralloc_module_t *gralloc_;
64};
65
Roman Stratiienkoe3983342021-02-15 15:41:53 +020066#ifdef DISABLE_LEGACY_GETTERS
67#define LEGACY_BUFFER_INFO_GETTER(getter_)
68#else
Roman Stratiienkofc014f52021-12-23 19:04:29 +020069// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020070#define LEGACY_BUFFER_INFO_GETTER(getter_) \
71 std::unique_ptr<LegacyBufferInfoGetter> \
72 LegacyBufferInfoGetter::CreateInstance() { \
73 auto instance = std::make_unique<getter_>(); \
74 if (instance) { \
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020075 int err = instance->Init(); \
76 if (err) { \
77 ALOGE("Failed to initialize the " #getter_ " getter %d", err); \
78 instance.reset(); \
79 } \
80 err = instance->ValidateGralloc(); \
81 if (err) { \
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020082 instance.reset(); \
83 } \
84 } \
85 return std::move(instance); \
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030086 }
Roman Stratiienkoe3983342021-02-15 15:41:53 +020087#endif
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030088
89} // namespace android
90#endif