blob: 1aa81601be09baa4971044e42cf2df4dce843034 [file] [log] [blame]
Sean Paulda6270d2015-06-01 14:11:52 -04001/*
2 * Copyright (C) 2015 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
Sean Paul0aee6b22016-05-10 04:08:10 -040017#define LOG_TAG "hwc-platform-drm-generic"
Sean Paulda6270d2015-06-01 14:11:52 -040018
Sean Paulf72cccd2018-08-27 13:59:08 -040019#include "platformdrmgeneric.h"
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010020#include "drmdevice.h"
Sean Paul63769962016-04-21 16:25:06 -040021#include "platform.h"
Sean Paulda6270d2015-06-01 14:11:52 -040022
Sean Paulda6270d2015-06-01 14:11:52 -040023#include <xf86drm.h>
24#include <xf86drmMode.h>
25
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +030026#include <cutils/properties.h>
John Stultzd12274d2018-04-02 20:57:21 -070027#include <gralloc_handle.h>
Sean Paulda6270d2015-06-01 14:11:52 -040028#include <hardware/gralloc.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040029#include <log/log.h>
Sean Paulda6270d2015-06-01 14:11:52 -040030
31namespace android {
32
33#ifdef USE_DRM_GENERIC_IMPORTER
34// static
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010035Importer *Importer::CreateInstance(DrmDevice *drm) {
Sean Paulda6270d2015-06-01 14:11:52 -040036 DrmGenericImporter *importer = new DrmGenericImporter(drm);
37 if (!importer)
38 return NULL;
39
40 int ret = importer->Init();
41 if (ret) {
42 ALOGE("Failed to initialize the nv importer %d", ret);
43 delete importer;
44 return NULL;
45 }
46 return importer;
47}
48#endif
49
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +030050DrmGenericImporter::DrmGenericImporter(DrmDevice *drm)
51 : drm_(drm), exclude_non_hwfb_(false) {
Sean Paulda6270d2015-06-01 14:11:52 -040052}
53
54DrmGenericImporter::~DrmGenericImporter() {
55}
56
57int DrmGenericImporter::Init() {
58 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
59 (const hw_module_t **)&gralloc_);
60 if (ret) {
61 ALOGE("Failed to open gralloc module");
62 return ret;
63 }
Mykhailo Sopihaad438862019-06-06 14:45:27 +030064
65 ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
66 gralloc_->common.author);
67
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +030068 char exclude_non_hwfb_prop[PROPERTY_VALUE_MAX];
69 property_get("hwc.drm.exclude_non_hwfb_imports", exclude_non_hwfb_prop, "0");
70 exclude_non_hwfb_ = static_cast<bool>(strncmp(exclude_non_hwfb_prop, "0", 1));
71
Sean Paulda6270d2015-06-01 14:11:52 -040072 return 0;
73}
74
75uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
76 switch (hal_format) {
77 case HAL_PIXEL_FORMAT_RGB_888:
78 return DRM_FORMAT_BGR888;
79 case HAL_PIXEL_FORMAT_BGRA_8888:
80 return DRM_FORMAT_ARGB8888;
81 case HAL_PIXEL_FORMAT_RGBX_8888:
82 return DRM_FORMAT_XBGR8888;
83 case HAL_PIXEL_FORMAT_RGBA_8888:
84 return DRM_FORMAT_ABGR8888;
85 case HAL_PIXEL_FORMAT_RGB_565:
86 return DRM_FORMAT_BGR565;
87 case HAL_PIXEL_FORMAT_YV12:
88 return DRM_FORMAT_YVU420;
89 default:
90 ALOGE("Cannot convert hal format to drm format %u", hal_format);
Roman Stratiienkof63726c2019-11-06 15:03:12 +020091 return DRM_FORMAT_INVALID;
Sean Paulda6270d2015-06-01 14:11:52 -040092 }
93}
94
John Stultza4514832018-08-24 16:27:36 -070095uint32_t DrmGenericImporter::DrmFormatToBitsPerPixel(uint32_t drm_format) {
96 switch (drm_format) {
97 case DRM_FORMAT_ARGB8888:
98 case DRM_FORMAT_XBGR8888:
99 case DRM_FORMAT_ABGR8888:
100 return 32;
101 case DRM_FORMAT_BGR888:
102 return 24;
103 case DRM_FORMAT_BGR565:
104 return 16;
105 case DRM_FORMAT_YVU420:
106 return 12;
107 default:
108 ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format);
109 return 32;
110 }
111}
112
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200113int DrmGenericImporter::ConvertBoInfo(buffer_handle_t handle,
114 hwc_drm_bo_t *bo) {
John Stultzd12274d2018-04-02 20:57:21 -0700115 gralloc_handle_t *gr_handle = gralloc_handle(handle);
Sean Paulda6270d2015-06-01 14:11:52 -0400116 if (!gr_handle)
117 return -EINVAL;
118
Sean Paulda6270d2015-06-01 14:11:52 -0400119 bo->width = gr_handle->width;
120 bo->height = gr_handle->height;
John Stultz616fb222018-08-24 16:08:57 -0700121 bo->hal_format = gr_handle->format;
Sean Paulda6270d2015-06-01 14:11:52 -0400122 bo->format = ConvertHalFormatToDrm(gr_handle->format);
Roman Stratiienkof63726c2019-11-06 15:03:12 +0200123 if (bo->format == DRM_FORMAT_INVALID)
124 return -EINVAL;
Rob Herringaeccd892017-10-06 17:20:05 -0500125 bo->usage = gr_handle->usage;
John Stultza4514832018-08-24 16:27:36 -0700126 bo->pixel_stride = (gr_handle->stride * 8) /
127 DrmFormatToBitsPerPixel(bo->format);
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200128 bo->prime_fds[0] = gr_handle->prime_fd;
Sean Paulda6270d2015-06-01 14:11:52 -0400129 bo->pitches[0] = gr_handle->stride;
Sean Paulda6270d2015-06-01 14:11:52 -0400130 bo->offsets[0] = 0;
131
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200132 return 0;
133}
134
135int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
136 memset(bo, 0, sizeof(hwc_drm_bo_t));
137
138 int ret = ConvertBoInfo(handle, bo);
139 if (ret)
140 return ret;
141
142 ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &bo->gem_handles[0]);
143 if (ret) {
144 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
145 return ret;
146 }
147
148 for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
149 int fd = bo->prime_fds[i];
150 if (fd != 0) {
151 if (fd != bo->prime_fds[0]) {
152 ALOGE("Multiplanar FBs are not supported by this version of composer");
153 return -ENOTSUP;
154 }
155 bo->gem_handles[i] = bo->gem_handles[0];
156 }
157 }
158
159 if (!bo->with_modifiers)
160 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
161 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
162 0);
163 else
164 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
165 bo->format, bo->gem_handles, bo->pitches,
166 bo->offsets, bo->modifiers, &bo->fb_id,
167 bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
168 : 0);
169
Sean Paulda6270d2015-06-01 14:11:52 -0400170 if (ret) {
171 ALOGE("could not create drm fb %d", ret);
172 return ret;
173 }
174
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200175 ImportHandle(bo->gem_handles[0]);
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100176
Sean Paulda6270d2015-06-01 14:11:52 -0400177 return ret;
178}
179
180int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
181 if (bo->fb_id)
182 if (drmModeRmFB(drm_->fd(), bo->fb_id))
183 ALOGE("Failed to rm fb");
184
John Stultzee18b0e2018-08-27 10:53:07 -0700185 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
Sean Paulda6270d2015-06-01 14:11:52 -0400186 if (!bo->gem_handles[i])
187 continue;
188
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100189 if (ReleaseHandle(bo->gem_handles[i])) {
190 ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
John Stultz53cb4ef2018-05-31 17:46:50 -0700191 } else {
John Stultzee18b0e2018-08-27 10:53:07 -0700192 for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
John Stultz53cb4ef2018-05-31 17:46:50 -0700193 if (bo->gem_handles[j] == bo->gem_handles[i])
194 bo->gem_handles[j] = 0;
Sean Paulda6270d2015-06-01 14:11:52 -0400195 bo->gem_handles[i] = 0;
John Stultz53cb4ef2018-05-31 17:46:50 -0700196 }
Sean Paulda6270d2015-06-01 14:11:52 -0400197 }
198 return 0;
199}
Sean Paul4c4646e2016-05-10 04:19:24 -0400200
Alexey Firago18ec6882018-11-21 23:47:05 +0300201bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200202 hwc_drm_bo_t bo;
203
204 int ret = ConvertBoInfo(handle, &bo);
205 if (ret)
Alexey Firago18ec6882018-11-21 23:47:05 +0300206 return false;
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +0300207
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200208 if (bo.prime_fds[0] == 0)
209 return false;
210
211 if (exclude_non_hwfb_ && !(bo.usage & GRALLOC_USAGE_HW_FB))
212 return false;
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +0300213
Alexey Firago18ec6882018-11-21 23:47:05 +0300214 return true;
215}
216
Sean Paul4c4646e2016-05-10 04:19:24 -0400217#ifdef USE_DRM_GENERIC_IMPORTER
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100218std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
Sean Paul4c4646e2016-05-10 04:19:24 -0400219 std::unique_ptr<Planner> planner(new Planner);
220 planner->AddStage<PlanStageGreedy>();
221 return planner;
222}
223#endif
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100224
225int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
226 gem_refcount_[gem_handle]++;
227
228 return 0;
229}
230
231int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
232 if (--gem_refcount_[gem_handle])
233 return 0;
234
235 gem_refcount_.erase(gem_handle);
236
237 return CloseHandle(gem_handle);
238}
239
240int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
241 struct drm_gem_close gem_close;
242
243 memset(&gem_close, 0, sizeof(gem_close));
244
245 gem_close.handle = gem_handle;
246 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
247 if (ret)
248 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
249
250 return ret;
251}
Sean Paulda6270d2015-06-01 14:11:52 -0400252}