blob: 667b5c16e6f50c5360167f623b3cad99c0b77e89 [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
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030041 static BufferInfoGetter *GetInstance();
42
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030043 static bool IsDrmFormatRgb(uint32_t drm_format);
44};
45
46class LegacyBufferInfoGetter : public BufferInfoGetter {
47 public:
48 using BufferInfoGetter::BufferInfoGetter;
49
50 int Init();
51
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020052 virtual int ValidateGralloc() {
53 return 0;
54 }
55
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020056 static std::unique_ptr<LegacyBufferInfoGetter> CreateInstance();
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030057
58 static uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020059
60 // NOLINTNEXTLINE:(readability-identifier-naming)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030061 const gralloc_module_t *gralloc_;
62};
63
Roman Stratiienkoe3983342021-02-15 15:41:53 +020064#ifdef DISABLE_LEGACY_GETTERS
65#define LEGACY_BUFFER_INFO_GETTER(getter_)
66#else
Roman Stratiienkofc014f52021-12-23 19:04:29 +020067// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020068#define LEGACY_BUFFER_INFO_GETTER(getter_) \
69 std::unique_ptr<LegacyBufferInfoGetter> \
70 LegacyBufferInfoGetter::CreateInstance() { \
71 auto instance = std::make_unique<getter_>(); \
72 if (instance) { \
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020073 int err = instance->Init(); \
74 if (err) { \
75 ALOGE("Failed to initialize the " #getter_ " getter %d", err); \
76 instance.reset(); \
77 } \
78 err = instance->ValidateGralloc(); \
79 if (err) { \
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020080 instance.reset(); \
81 } \
82 } \
83 return std::move(instance); \
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030084 }
Roman Stratiienkoe3983342021-02-15 15:41:53 +020085#endif
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030086
87} // namespace android
88#endif