blob: 9e9423888d8ace6b12c0b225db16a7936346f57d [file] [log] [blame]
Roman Stratiienko8666dc92021-02-09 17:49:55 +02001/*
2 * Copyright (C) 2021 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 DRM_DRMFBIMPORTER_H_
18#define DRM_DRMFBIMPORTER_H_
19
20#include <drm/drm_fourcc.h>
21#include <hardware/gralloc.h>
22
23#include <array>
24#include <map>
25
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020026#include "bufferinfo/BufferInfo.h"
Roman Stratiienko8666dc92021-02-09 17:49:55 +020027#include "drm/DrmDevice.h"
Roman Stratiienko8666dc92021-02-09 17:49:55 +020028
29#ifndef DRM_FORMAT_INVALID
30#define DRM_FORMAT_INVALID 0
31#endif
32
33using GemHandle = uint32_t;
34
35namespace android {
36
37class DrmFbIdHandle {
38 public:
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020039 static auto CreateInstance(BufferInfo *bo, GemHandle first_gem_handle,
Roman Stratiienko7d899112022-01-31 11:30:27 +020040 DrmDevice &drm) -> std::shared_ptr<DrmFbIdHandle>;
Roman Stratiienko8666dc92021-02-09 17:49:55 +020041
42 ~DrmFbIdHandle();
43 DrmFbIdHandle(DrmFbIdHandle &&) = delete;
44 DrmFbIdHandle(const DrmFbIdHandle &) = delete;
45 auto operator=(const DrmFbIdHandle &) = delete;
46 auto operator=(DrmFbIdHandle &&) = delete;
47
48 auto GetFbId [[nodiscard]] () const -> uint32_t {
49 return fb_id_;
50 }
51
52 private:
Roman Stratiienko7d899112022-01-31 11:30:27 +020053 explicit DrmFbIdHandle(DrmDevice &drm) : drm_(&drm){};
Roman Stratiienko8666dc92021-02-09 17:49:55 +020054
Roman Stratiienko7d899112022-01-31 11:30:27 +020055 DrmDevice *const drm_;
Roman Stratiienko8666dc92021-02-09 17:49:55 +020056
57 uint32_t fb_id_{};
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020058 std::array<GemHandle, kBufferMaxPlanes> gem_handles_{};
Roman Stratiienko8666dc92021-02-09 17:49:55 +020059};
60
61class DrmFbImporter {
62 public:
Roman Stratiienko7d899112022-01-31 11:30:27 +020063 explicit DrmFbImporter(DrmDevice &drm) : drm_(&drm){};
Roman Stratiienko8666dc92021-02-09 17:49:55 +020064 ~DrmFbImporter() = default;
65 DrmFbImporter(const DrmFbImporter &) = delete;
66 DrmFbImporter(DrmFbImporter &&) = delete;
67 auto operator=(const DrmFbImporter &) = delete;
68 auto operator=(DrmFbImporter &&) = delete;
69
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020070 auto GetOrCreateFbId(BufferInfo *bo) -> std::shared_ptr<DrmFbIdHandle>;
Roman Stratiienko8666dc92021-02-09 17:49:55 +020071
72 private:
73 void CleanupEmptyCacheElements() {
74 for (auto it = drm_fb_id_handle_cache_.begin();
75 it != drm_fb_id_handle_cache_.end();) {
76 if (it->second.expired()) {
77 it = drm_fb_id_handle_cache_.erase(it);
78 } else {
79 ++it;
80 }
81 }
82 }
83
Roman Stratiienko7d899112022-01-31 11:30:27 +020084 DrmDevice *const drm_;
Roman Stratiienko8666dc92021-02-09 17:49:55 +020085
86 std::map<GemHandle, std::weak_ptr<DrmFbIdHandle>> drm_fb_id_handle_cache_;
87};
88
89} // namespace android
90
91#endif