blob: f82d6743f88d2da0e10cc6f46859dae5ce26dca9 [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
37auto DrmFbIdHandle::CreateInstance(hwc_drm_bo_t *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 Stratiienko8b926a52022-05-16 18:15:27 +030040 ATRACE_NAME("Import dmabufs and register FB");
41
Roman Stratiienko8666dc92021-02-09 17:49:55 +020042 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory): priv. constructor usage
43 std::shared_ptr<DrmFbIdHandle> local(new DrmFbIdHandle(drm));
44
45 local->gem_handles_[0] = first_gem_handle;
46 int32_t err = 0;
47
48 /* Framebuffer object creation require gem handle for every used plane */
Roman Stratiienkodeb77352021-12-06 12:59:26 +020049 for (size_t i = 1; i < local->gem_handles_.size(); i++) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020050 if (bo->prime_fds[i] > 0) {
51 if (bo->prime_fds[i] != bo->prime_fds[0]) {
Roman Stratiienko7d899112022-01-31 11:30:27 +020052 err = drmPrimeFDToHandle(drm.GetFd(), bo->prime_fds[i],
Roman Stratiienko8666dc92021-02-09 17:49:55 +020053 &local->gem_handles_.at(i));
54 if (err != 0) {
55 ALOGE("failed to import prime fd %d errno=%d", bo->prime_fds[i],
56 errno);
57 }
58 } else {
59 local->gem_handles_.at(i) = local->gem_handles_[0];
60 }
61 }
62 }
63
64 bool has_modifiers = bo->modifiers[0] != DRM_FORMAT_MOD_NONE &&
65 bo->modifiers[0] != DRM_FORMAT_MOD_INVALID;
66
Roman Stratiienko7d899112022-01-31 11:30:27 +020067 if (!drm.HasAddFb2ModifiersSupport() && has_modifiers) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020068 ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
69 bo->modifiers[0]);
70 local.reset();
71 return local;
72 }
73
74 /* Create framebuffer object */
75 if (!has_modifiers) {
Roman Stratiienko7d899112022-01-31 11:30:27 +020076 err = drmModeAddFB2(drm.GetFd(), bo->width, bo->height, bo->format,
Roman Stratiienkocf80b9b2022-04-28 15:42:36 +030077 local->gem_handles_.data(), &bo->pitches[0],
Roman Stratiienko8666dc92021-02-09 17:49:55 +020078 &bo->offsets[0], &local->fb_id_, 0);
79 } else {
Roman Stratiienko7d899112022-01-31 11:30:27 +020080 err = drmModeAddFB2WithModifiers(drm.GetFd(), bo->width, bo->height,
Roman Stratiienkocf80b9b2022-04-28 15:42:36 +030081 bo->format, local->gem_handles_.data(),
Roman Stratiienko8666dc92021-02-09 17:49:55 +020082 &bo->pitches[0], &bo->offsets[0],
83 &bo->modifiers[0], &local->fb_id_,
84 DRM_MODE_FB_MODIFIERS);
85 }
86 if (err != 0) {
87 ALOGE("could not create drm fb %d", err);
88 local.reset();
89 }
90
91 return local;
92}
93
94DrmFbIdHandle::~DrmFbIdHandle() {
Roman Stratiienko8b926a52022-05-16 18:15:27 +030095 ATRACE_NAME("Close FB and dmabufs");
96
Roman Stratiienko8666dc92021-02-09 17:49:55 +020097 /* Destroy framebuffer object */
Roman Stratiienko7d899112022-01-31 11:30:27 +020098 if (drmModeRmFB(drm_->GetFd(), fb_id_) != 0) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +020099 ALOGE("Failed to rm fb");
100 }
101
102 /* Close GEM handles.
103 *
104 * WARNING: TODO(nobody):
105 * From Linux side libweston relies on libgbm to get KMS handle and never
106 * closes it (handle is closed by libgbm on buffer destruction)
107 * Probably we should offer similar approach to users (at least on user
108 * request via system properties)
109 */
110 struct drm_gem_close gem_close {};
Roman Stratiienkodeb77352021-12-06 12:59:26 +0200111 for (size_t i = 0; i < gem_handles_.size(); i++) {
Roman Stratiienko85143292021-08-25 06:15:42 +0300112 /* Don't close invalid handle. Close handle only once in cases
113 * where several YUV planes located in the single buffer. */
114 if (gem_handles_[i] == 0 ||
115 (i != 0 && gem_handles_[i] == gem_handles_[0])) {
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200116 continue;
117 }
Roman Stratiienko85143292021-08-25 06:15:42 +0300118 gem_close.handle = gem_handles_[i];
Roman Stratiienko7d899112022-01-31 11:30:27 +0200119 int32_t err = drmIoctl(drm_->GetFd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200120 if (err != 0) {
Roman Stratiienko85143292021-08-25 06:15:42 +0300121 ALOGE("Failed to close gem handle %d, errno: %d", gem_handles_[i], errno);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200122 }
123 }
124}
125
126auto DrmFbImporter::GetOrCreateFbId(hwc_drm_bo_t *bo)
127 -> std::shared_ptr<DrmFbIdHandle> {
128 /* Lookup DrmFbIdHandle in cache first. First handle serves as a cache key. */
129 GemHandle first_handle = 0;
Roman Stratiienko7d899112022-01-31 11:30:27 +0200130 int32_t err = drmPrimeFDToHandle(drm_->GetFd(), bo->prime_fds[0],
131 &first_handle);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200132
133 if (err != 0) {
134 ALOGE("Failed to import prime fd %d ret=%d", bo->prime_fds[0], err);
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200135 return {};
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200136 }
137
138 auto drm_fb_id_cached = drm_fb_id_handle_cache_.find(first_handle);
139
140 if (drm_fb_id_cached != drm_fb_id_handle_cache_.end()) {
141 if (auto drm_fb_id_handle_shared = drm_fb_id_cached->second.lock()) {
142 return drm_fb_id_handle_shared;
143 }
144 drm_fb_id_handle_cache_.erase(drm_fb_id_cached);
145 }
146
147 /* Cleanup cached empty weak pointers */
148 const int minimal_cleanup_size = 128;
149 if (drm_fb_id_handle_cache_.size() > minimal_cleanup_size) {
150 CleanupEmptyCacheElements();
151 }
152
153 /* No DrmFbIdHandle found in cache, create framebuffer object */
Roman Stratiienko7d899112022-01-31 11:30:27 +0200154 auto fb_id_handle = DrmFbIdHandle::CreateInstance(bo, first_handle, *drm_);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200155 if (fb_id_handle) {
156 drm_fb_id_handle_cache_[first_handle] = fb_id_handle;
157 }
158
159 return fb_id_handle;
160}
161
162} // namespace android