blob: 5131c371f4f90908bfd9afc6010b20214b1180ca [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
Roman Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030018
19#include <drm/drm_fourcc.h>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020020#include <hardware/gralloc.h>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030021
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020022#include <optional>
23
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020024#include "BufferInfo.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030025#include "drm/DrmDevice.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030026
27#ifndef DRM_FORMAT_INVALID
28#define DRM_FORMAT_INVALID 0
29#endif
30
31namespace android {
32
Roman Stratiienko74d2c4a2021-12-17 18:10:57 +020033using BufferUniqueId = uint64_t;
34
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030035class BufferInfoGetter {
36 public:
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020037 virtual ~BufferInfoGetter() = default;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030038
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020039 virtual auto GetBoInfo(buffer_handle_t handle)
40 -> std::optional<BufferInfo> = 0;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030041
Roman Stratiienko74d2c4a2021-12-17 18:10:57 +020042 virtual std::optional<BufferUniqueId> GetUniqueId(buffer_handle_t handle);
43
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030044 static BufferInfoGetter *GetInstance();
45
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030046 static bool IsDrmFormatRgb(uint32_t drm_format);
47};
48
49class LegacyBufferInfoGetter : public BufferInfoGetter {
50 public:
51 using BufferInfoGetter::BufferInfoGetter;
52
53 int Init();
54
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020055 virtual int ValidateGralloc() {
56 return 0;
57 }
58
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020059 static std::unique_ptr<LegacyBufferInfoGetter> CreateInstance();
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030060
61 static uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020062
63 // NOLINTNEXTLINE:(readability-identifier-naming)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030064 const gralloc_module_t *gralloc_;
65};
66
Roman Stratiienkoe3983342021-02-15 15:41:53 +020067#ifdef DISABLE_LEGACY_GETTERS
68#define LEGACY_BUFFER_INFO_GETTER(getter_)
69#else
Roman Stratiienkofc014f52021-12-23 19:04:29 +020070// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020071#define LEGACY_BUFFER_INFO_GETTER(getter_) \
72 std::unique_ptr<LegacyBufferInfoGetter> \
73 LegacyBufferInfoGetter::CreateInstance() { \
74 auto instance = std::make_unique<getter_>(); \
75 if (instance) { \
Roman Stratiienko39d8f7e2021-11-30 17:06:44 +020076 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) { \
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020083 instance.reset(); \
84 } \
85 } \
86 return std::move(instance); \
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030087 }
Roman Stratiienkoe3983342021-02-15 15:41:53 +020088#endif
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030089
90} // namespace android