blob: 167aa60b0b53687ef21571d1cbdcdc25f318a7e6 [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
26#include "drm/DrmDevice.h"
27#include "drmhwcgralloc.h"
28
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:
39 static auto CreateInstance(hwc_drm_bo_t *bo, GemHandle first_gem_handle,
40 const std::shared_ptr<DrmDevice> &drm)
41 -> std::shared_ptr<DrmFbIdHandle>;
42
43 ~DrmFbIdHandle();
44 DrmFbIdHandle(DrmFbIdHandle &&) = delete;
45 DrmFbIdHandle(const DrmFbIdHandle &) = delete;
46 auto operator=(const DrmFbIdHandle &) = delete;
47 auto operator=(DrmFbIdHandle &&) = delete;
48
49 auto GetFbId [[nodiscard]] () const -> uint32_t {
50 return fb_id_;
51 }
52
53 private:
54 explicit DrmFbIdHandle(std::shared_ptr<DrmDevice> drm)
55 : drm_(std::move(drm)){};
56
57 const std::shared_ptr<DrmDevice> drm_;
58
59 uint32_t fb_id_{};
60 std::array<GemHandle, HWC_DRM_BO_MAX_PLANES> gem_handles_{};
61};
62
63class DrmFbImporter {
64 public:
65 explicit DrmFbImporter(std::shared_ptr<DrmDevice> drm)
66 : drm_(std::move(drm)){};
67 ~DrmFbImporter() = default;
68 DrmFbImporter(const DrmFbImporter &) = delete;
69 DrmFbImporter(DrmFbImporter &&) = delete;
70 auto operator=(const DrmFbImporter &) = delete;
71 auto operator=(DrmFbImporter &&) = delete;
72
73 auto GetOrCreateFbId(hwc_drm_bo_t *bo) -> std::shared_ptr<DrmFbIdHandle>;
74
75 private:
76 void CleanupEmptyCacheElements() {
77 for (auto it = drm_fb_id_handle_cache_.begin();
78 it != drm_fb_id_handle_cache_.end();) {
79 if (it->second.expired()) {
80 it = drm_fb_id_handle_cache_.erase(it);
81 } else {
82 ++it;
83 }
84 }
85 }
86
87 const std::shared_ptr<DrmDevice> drm_;
88
89 std::map<GemHandle, std::weak_ptr<DrmFbIdHandle>> drm_fb_id_handle_cache_;
90};
91
92} // namespace android
93
94#endif