• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 ATRACE_TAG ATRACE_TAG_GRAPHICS
19 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
20 #define LOG_TAG "hwc-platform-drm-generic"
21 
22 #include "DrmFbImporter.h"
23 
24 #include <hardware/gralloc.h>
25 #include <utils/Trace.h>
26 #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 
35 namespace android {
36 
CreateInstance(BufferInfo * bo,GemHandle first_gem_handle,DrmDevice & drm)37 auto DrmFbIdHandle::CreateInstance(BufferInfo *bo, GemHandle first_gem_handle,
38                                    DrmDevice &drm)
39     -> std::shared_ptr<DrmFbIdHandle> {
40   ATRACE_NAME("Import dmabufs and register FB");
41 
42   // 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 */
49   for (size_t i = 1; i < local->gem_handles_.size(); i++) {
50     if (bo->prime_fds[i] > 0) {
51       if (bo->prime_fds[i] != bo->prime_fds[0]) {
52         err = drmPrimeFDToHandle(drm.GetFd(), bo->prime_fds[i],
53                                  &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 
67   if (!drm.HasAddFb2ModifiersSupport() && has_modifiers) {
68     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) {
76     err = drmModeAddFB2(drm.GetFd(), bo->width, bo->height, bo->format,
77                         local->gem_handles_.data(), &bo->pitches[0],
78                         &bo->offsets[0], &local->fb_id_, 0);
79   } else {
80     err = drmModeAddFB2WithModifiers(drm.GetFd(), bo->width, bo->height,
81                                      bo->format, local->gem_handles_.data(),
82                                      &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 
~DrmFbIdHandle()94 DrmFbIdHandle::~DrmFbIdHandle() {
95   ATRACE_NAME("Close FB and dmabufs");
96 
97   /* Destroy framebuffer object */
98   if (drmModeRmFB(drm_->GetFd(), fb_id_) != 0) {
99     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 {};
111   for (size_t i = 0; i < gem_handles_.size(); i++) {
112     /* 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])) {
116       continue;
117     }
118     gem_close.handle = gem_handles_[i];
119     int32_t err = drmIoctl(drm_->GetFd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
120     if (err != 0) {
121       ALOGE("Failed to close gem handle %d, errno: %d", gem_handles_[i], errno);
122     }
123   }
124 }
125 
GetOrCreateFbId(BufferInfo * bo)126 auto DrmFbImporter::GetOrCreateFbId(BufferInfo *bo)
127     -> std::shared_ptr<DrmFbIdHandle> {
128   /* Lookup DrmFbIdHandle in cache first. First handle serves as a cache key. */
129   GemHandle first_handle = 0;
130   int32_t err = drmPrimeFDToHandle(drm_->GetFd(), bo->prime_fds[0],
131                                    &first_handle);
132 
133   if (err != 0) {
134     ALOGE("Failed to import prime fd %d ret=%d", bo->prime_fds[0], err);
135     return {};
136   }
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 */
154   auto fb_id_handle = DrmFbIdHandle::CreateInstance(bo, first_handle, *drm_);
155   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
163