• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "hwc-platform-drm-generic"
18 
19 #include "DrmGenericImporter.h"
20 
21 #include <cutils/properties.h>
22 #include <gralloc_handle.h>
23 #include <hardware/gralloc.h>
24 #include <inttypes.h>
25 #include <log/log.h>
26 #include <xf86drm.h>
27 #include <xf86drmMode.h>
28 
29 namespace android {
30 
DrmGenericImporter(DrmDevice * drm)31 DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
32   uint64_t cap_value = 0;
33   if (drmGetCap(drm_->fd(), DRM_CAP_ADDFB2_MODIFIERS, &cap_value)) {
34     ALOGE("drmGetCap failed. Fallback to no modifier support.");
35     cap_value = 0;
36   }
37   has_modifier_support_ = cap_value;
38 }
39 
~DrmGenericImporter()40 DrmGenericImporter::~DrmGenericImporter() {
41 }
42 
ImportBuffer(hwc_drm_bo_t * bo)43 int DrmGenericImporter::ImportBuffer(hwc_drm_bo_t *bo) {
44   int ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0],
45                                &bo->gem_handles[0]);
46   if (ret) {
47     ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
48     return ret;
49   }
50 
51   for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
52     int fd = bo->prime_fds[i];
53     if (fd != 0) {
54       if (fd != bo->prime_fds[0]) {
55         ALOGE("Multiplanar FBs are not supported by this version of composer");
56         return -ENOTSUP;
57       }
58       bo->gem_handles[i] = bo->gem_handles[0];
59     }
60   }
61 
62   if (!has_modifier_support_ && bo->modifiers[0]) {
63     ALOGE("No ADDFB2 with modifier support. Can't import modifier %" PRIu64,
64           bo->modifiers[0]);
65     return -EINVAL;
66   }
67 
68   if (!bo->with_modifiers)
69     ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
70                         bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
71                         0);
72   else
73     ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
74                                      bo->format, bo->gem_handles, bo->pitches,
75                                      bo->offsets, bo->modifiers, &bo->fb_id,
76                                      bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
77                                                       : 0);
78 
79   if (ret) {
80     ALOGE("could not create drm fb %d", ret);
81     return ret;
82   }
83 
84   ImportHandle(bo->gem_handles[0]);
85 
86   return ret;
87 }
88 
ReleaseBuffer(hwc_drm_bo_t * bo)89 int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
90   if (bo->fb_id)
91     if (drmModeRmFB(drm_->fd(), bo->fb_id))
92       ALOGE("Failed to rm fb");
93 
94   for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
95     if (!bo->gem_handles[i])
96       continue;
97 
98     if (ReleaseHandle(bo->gem_handles[i])) {
99       ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
100     } else {
101       for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
102         if (bo->gem_handles[j] == bo->gem_handles[i])
103           bo->gem_handles[j] = 0;
104       bo->gem_handles[i] = 0;
105     }
106   }
107   return 0;
108 }
109 
ImportHandle(uint32_t gem_handle)110 int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
111   gem_refcount_[gem_handle]++;
112 
113   return 0;
114 }
115 
ReleaseHandle(uint32_t gem_handle)116 int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
117   if (--gem_refcount_[gem_handle])
118     return 0;
119 
120   gem_refcount_.erase(gem_handle);
121 
122   return CloseHandle(gem_handle);
123 }
124 
CloseHandle(uint32_t gem_handle)125 int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
126   struct drm_gem_close gem_close;
127 
128   memset(&gem_close, 0, sizeof(gem_close));
129 
130   gem_close.handle = gem_handle;
131   int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
132   if (ret)
133     ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
134 
135   return ret;
136 }
137 }  // namespace android
138