blob: a91a52b48671fd459ad2f81091743a0ef0ac9f0c [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// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Roman Stratiienko8b926a52022-05-16 18:15:27 +030018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
Roman Stratiienko8666dc92021-02-09 17:49:55 +020020#define LOG_TAG "hwc-platform-drm-generic"
21
22#include "DrmFbImporter.h"
23
Roman Stratiienko8666dc92021-02-09 17:49:55 +020024#include <hardware/gralloc.h>
Roman Stratiienko8b926a52022-05-16 18:15:27 +030025#include <utils/Trace.h>
Roman Stratiienko8666dc92021-02-09 17:49:55 +020026#include <xf86drm.h>
27#include <xf86drmMode.h>
28
29#include <cinttypes>
30#include <system_error>
31
32#include "utils/log.h"
33#include "utils/properties.h"
34
35namespace android {
36
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020037auto DrmFbIdHandle::CreateInstance(BufferInfo *bo, GemHandle first_gem_handle,
Roman Stratiienko7d899112022-01-31 11:30:27 +020038 DrmDevice &drm)
Roman Stratiienko8666dc92021-02-09 17:49:55 +020039 -> std::shared_ptr<DrmFbIdHandle> {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030040 // NOLINTNEXTLINE(misc-const-correctness)
Roman Stratiienko8b926a52022-05-16 18:15:27 +030041 ATRACE_NAME("Import dmabufs and register FB");
42
Roman Stratiienko8666dc92021-02-09 17:49:55 +020043 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory): priv. constructor usage
44 std::shared_ptr<DrmFbIdHandle> local(new DrmFbIdHandle(drm));
45
46 local->gem_handles_[0] = first_gem_handle;
47 int32_t err = 0;
48
49 /* Framebuffer object creation require gem handle for every used plane */
Roman Stratiienkodeb77352021-12-06 12:59:26 +020050 for (size_t i = 1; i < local->gem_handles_.size(); i++) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020051 if (bo->prime_fds[i] > 0) {
52 if (bo->prime_fds[i] != bo->prime_fds[0]) {
Roman Stratiienko76892782023-01-16 17:15:53 +020053 err = drmPrimeFDToHandle(*drm.GetFd(), bo->prime_fds[i],
Roman Stratiienko8666dc92021-02-09 17:49:55 +020054 &local->gem_handles_.at(i));
55 if (err != 0) {
56 ALOGE("failed to import prime fd %d errno=%d", bo->prime_fds[i],
57 errno);
58 }
59 } else {
60 local->gem_handles_.at(i) = local->gem_handles_[0];
61 }
62 }
63 }
64
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030065 auto has_modifiers = bo->modifiers[0] != DRM_FORMAT_MOD_NONE &&
Roman Stratiienko8666dc92021-02-09 17:49:55 +020066 bo->modifiers[0] != DRM_FORMAT_MOD_INVALID;
67
Roman Stratiienko7d899112022-01-31 11:30:27 +020068 if (!drm.HasAddFb2ModifiersSupport() && has_modifiers) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020069 ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
70 bo->modifiers[0]);
71 local.reset();
72 return local;
73 }
74
75 /* Create framebuffer object */
76 if (!has_modifiers) {
Roman Stratiienko76892782023-01-16 17:15:53 +020077 err = drmModeAddFB2(*drm.GetFd(), bo->width, bo->height, bo->format,
Roman Stratiienkocf80b9b2022-04-28 15:42:36 +030078 local->gem_handles_.data(), &bo->pitches[0],
Roman Stratiienko8666dc92021-02-09 17:49:55 +020079 &bo->offsets[0], &local->fb_id_, 0);
80 } else {
Roman Stratiienko76892782023-01-16 17:15:53 +020081 err = drmModeAddFB2WithModifiers(*drm.GetFd(), bo->width, bo->height,
Roman Stratiienkocf80b9b2022-04-28 15:42:36 +030082 bo->format, local->gem_handles_.data(),
Roman Stratiienko8666dc92021-02-09 17:49:55 +020083 &bo->pitches[0], &bo->offsets[0],
84 &bo->modifiers[0], &local->fb_id_,
85 DRM_MODE_FB_MODIFIERS);
86 }
87 if (err != 0) {
88 ALOGE("could not create drm fb %d", err);
89 local.reset();
90 }
91
92 return local;
93}
94
95DrmFbIdHandle::~DrmFbIdHandle() {
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030096 // NOLINTNEXTLINE(misc-const-correctness)
Roman Stratiienko8b926a52022-05-16 18:15:27 +030097 ATRACE_NAME("Close FB and dmabufs");
98
Roman Stratiienko8666dc92021-02-09 17:49:55 +020099 /* Destroy framebuffer object */
Roman Stratiienko76892782023-01-16 17:15:53 +0200100 if (drmModeRmFB(*drm_->GetFd(), fb_id_) != 0) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200101 ALOGE("Failed to rm fb");
102 }
103
104 /* Close GEM handles.
105 *
106 * WARNING: TODO(nobody):
107 * From Linux side libweston relies on libgbm to get KMS handle and never
108 * closes it (handle is closed by libgbm on buffer destruction)
109 * Probably we should offer similar approach to users (at least on user
110 * request via system properties)
111 */
112 struct drm_gem_close gem_close {};
Roman Stratiienkodeb77352021-12-06 12:59:26 +0200113 for (size_t i = 0; i < gem_handles_.size(); i++) {
Roman Stratiienko85143292021-08-25 06:15:42 +0300114 /* Don't close invalid handle. Close handle only once in cases
115 * where several YUV planes located in the single buffer. */
116 if (gem_handles_[i] == 0 ||
117 (i != 0 && gem_handles_[i] == gem_handles_[0])) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200118 continue;
119 }
Roman Stratiienko85143292021-08-25 06:15:42 +0300120 gem_close.handle = gem_handles_[i];
Roman Stratiienko76892782023-01-16 17:15:53 +0200121 auto err = drmIoctl(*drm_->GetFd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200122 if (err != 0) {
Roman Stratiienko85143292021-08-25 06:15:42 +0300123 ALOGE("Failed to close gem handle %d, errno: %d", gem_handles_[i], errno);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200124 }
125 }
126}
127
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +0200128auto DrmFbImporter::GetOrCreateFbId(BufferInfo *bo)
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200129 -> std::shared_ptr<DrmFbIdHandle> {
130 /* Lookup DrmFbIdHandle in cache first. First handle serves as a cache key. */
131 GemHandle first_handle = 0;
Roman Stratiienko76892782023-01-16 17:15:53 +0200132 auto err = drmPrimeFDToHandle(*drm_->GetFd(), bo->prime_fds[0],
133 &first_handle);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200134
135 if (err != 0) {
136 ALOGE("Failed to import prime fd %d ret=%d", bo->prime_fds[0], err);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200137 return {};
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200138 }
139
140 auto drm_fb_id_cached = drm_fb_id_handle_cache_.find(first_handle);
141
142 if (drm_fb_id_cached != drm_fb_id_handle_cache_.end()) {
143 if (auto drm_fb_id_handle_shared = drm_fb_id_cached->second.lock()) {
144 return drm_fb_id_handle_shared;
145 }
146 drm_fb_id_handle_cache_.erase(drm_fb_id_cached);
147 }
148
149 /* Cleanup cached empty weak pointers */
150 const int minimal_cleanup_size = 128;
151 if (drm_fb_id_handle_cache_.size() > minimal_cleanup_size) {
152 CleanupEmptyCacheElements();
153 }
154
155 /* No DrmFbIdHandle found in cache, create framebuffer object */
Roman Stratiienko7d899112022-01-31 11:30:27 +0200156 auto fb_id_handle = DrmFbIdHandle::CreateInstance(bo, first_handle, *drm_);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200157 if (fb_id_handle) {
158 drm_fb_id_handle_cache_[first_handle] = fb_id_handle;
159 }
160
161 return fb_id_handle;
162}
163
164} // namespace android