blob: 2b5ddd4cba0802c3649076cce5cf7af6159dec66 [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)
18#define LOG_TAG "hwc-platform-drm-generic"
19
20#include "DrmFbImporter.h"
21
22#include <gralloc_handle.h>
23#include <hardware/gralloc.h>
24#include <xf86drm.h>
25#include <xf86drmMode.h>
26
27#include <cinttypes>
28#include <system_error>
29
30#include "utils/log.h"
31#include "utils/properties.h"
32
33namespace android {
34
35auto DrmFbIdHandle::CreateInstance(hwc_drm_bo_t *bo, GemHandle first_gem_handle,
36 const std::shared_ptr<DrmDevice> &drm)
37 -> std::shared_ptr<DrmFbIdHandle> {
38 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory): priv. constructor usage
39 std::shared_ptr<DrmFbIdHandle> local(new DrmFbIdHandle(drm));
40
41 local->gem_handles_[0] = first_gem_handle;
42 int32_t err = 0;
43
44 /* Framebuffer object creation require gem handle for every used plane */
45 for (int i = 1; i < local->gem_handles_.size(); i++) {
46 if (bo->prime_fds[i] > 0) {
47 if (bo->prime_fds[i] != bo->prime_fds[0]) {
48 err = drmPrimeFDToHandle(drm->fd(), bo->prime_fds[i],
49 &local->gem_handles_.at(i));
50 if (err != 0) {
51 ALOGE("failed to import prime fd %d errno=%d", bo->prime_fds[i],
52 errno);
53 }
54 } else {
55 local->gem_handles_.at(i) = local->gem_handles_[0];
56 }
57 }
58 }
59
60 bool has_modifiers = bo->modifiers[0] != DRM_FORMAT_MOD_NONE &&
61 bo->modifiers[0] != DRM_FORMAT_MOD_INVALID;
62
63 if (!drm->HasAddFb2ModifiersSupport() && has_modifiers) {
64 ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
65 bo->modifiers[0]);
66 local.reset();
67 return local;
68 }
69
70 /* Create framebuffer object */
71 if (!has_modifiers) {
72 err = drmModeAddFB2(drm->fd(), bo->width, bo->height, bo->format,
73 &local->gem_handles_[0], &bo->pitches[0],
74 &bo->offsets[0], &local->fb_id_, 0);
75 } else {
76 err = drmModeAddFB2WithModifiers(drm->fd(), bo->width, bo->height,
77 bo->format, &local->gem_handles_[0],
78 &bo->pitches[0], &bo->offsets[0],
79 &bo->modifiers[0], &local->fb_id_,
80 DRM_MODE_FB_MODIFIERS);
81 }
82 if (err != 0) {
83 ALOGE("could not create drm fb %d", err);
84 local.reset();
85 }
86
87 return local;
88}
89
90DrmFbIdHandle::~DrmFbIdHandle() {
91 /* Destroy framebuffer object */
92 if (drmModeRmFB(drm_->fd(), fb_id_) != 0) {
93 ALOGE("Failed to rm fb");
94 }
95
96 /* Close GEM handles.
97 *
98 * WARNING: TODO(nobody):
99 * From Linux side libweston relies on libgbm to get KMS handle and never
100 * closes it (handle is closed by libgbm on buffer destruction)
101 * Probably we should offer similar approach to users (at least on user
102 * request via system properties)
103 */
104 struct drm_gem_close gem_close {};
105 for (unsigned int gem_handle : gem_handles_) {
106 if (gem_handle == 0) {
107 continue;
108 }
109 gem_close.handle = gem_handle;
110 int32_t err = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
111 if (err != 0) {
112 ALOGE("Failed to close gem handle %d, errno: %d", gem_handle, errno);
113 }
114 }
115}
116
117auto DrmFbImporter::GetOrCreateFbId(hwc_drm_bo_t *bo)
118 -> std::shared_ptr<DrmFbIdHandle> {
119 /* Lookup DrmFbIdHandle in cache first. First handle serves as a cache key. */
120 GemHandle first_handle = 0;
121 int32_t err = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &first_handle);
122
123 if (err != 0) {
124 ALOGE("Failed to import prime fd %d ret=%d", bo->prime_fds[0], err);
125 return std::shared_ptr<DrmFbIdHandle>();
126 }
127
128 auto drm_fb_id_cached = drm_fb_id_handle_cache_.find(first_handle);
129
130 if (drm_fb_id_cached != drm_fb_id_handle_cache_.end()) {
131 if (auto drm_fb_id_handle_shared = drm_fb_id_cached->second.lock()) {
132 return drm_fb_id_handle_shared;
133 }
134 drm_fb_id_handle_cache_.erase(drm_fb_id_cached);
135 }
136
137 /* Cleanup cached empty weak pointers */
138 const int minimal_cleanup_size = 128;
139 if (drm_fb_id_handle_cache_.size() > minimal_cleanup_size) {
140 CleanupEmptyCacheElements();
141 }
142
143 /* No DrmFbIdHandle found in cache, create framebuffer object */
144 auto fb_id_handle = DrmFbIdHandle::CreateInstance(bo, first_handle, drm_);
145 if (fb_id_handle) {
146 drm_fb_id_handle_cache_[first_handle] = fb_id_handle;
147 }
148
149 return fb_id_handle;
150}
151
152} // namespace android