blob: b9dafb37d95760997d1f87a076db439ef132aa5d [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];
Jason Macnakf1af9572020-08-20 11:49:51 -070069 property_get("vendor.hwc.drm.exclude_non_hwfb_imports", exclude_non_hwfb_prop,
70 "0");
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +030071 exclude_non_hwfb_ = static_cast<bool>(strncmp(exclude_non_hwfb_prop, "0", 1));
72
Sean Paulda6270d2015-06-01 14:11:52 -040073 return 0;
74}
75
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +030076enum chroma_order {
77 YCbCr,
78 YCrCb,
79};
80
81struct droid_yuv_format {
82 /* Lookup keys */
83 int native; /* HAL_PIXEL_FORMAT_ */
84 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
85 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
86
87 /* Result */
88 int fourcc; /* DRM_FORMAT_ */
89};
90
91/* The following table is used to look up a DRI image FourCC based
92 * on native format and information contained in android_ycbcr struct. */
93static const struct droid_yuv_format droid_yuv_formats[] = {
94 /* Native format, YCrCb, Chroma step, DRI image FourCC */
95 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12},
96 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420},
97 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420},
98 {HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420},
99 /* HACK: See droid_create_image_from_prime_fds() and
100 * https://issuetracker.google.com/32077885. */
101 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12},
102 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420},
103 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420},
104 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV},
105 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888},
106};
107
108#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
109
110static int get_fourcc_yuv(int native, enum chroma_order chroma_order,
111 int chroma_step) {
112 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
113 if (droid_yuv_formats[i].native == native &&
114 droid_yuv_formats[i].chroma_order == chroma_order &&
115 droid_yuv_formats[i].chroma_step == chroma_step)
116 return droid_yuv_formats[i].fourcc;
117
118 return -1;
119}
120
121static bool is_yuv(int native) {
122 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
123 if (droid_yuv_formats[i].native == native)
124 return true;
125
126 return false;
127}
128
129bool DrmGenericImporter::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
130 hwc_drm_bo_t *bo) {
131 struct android_ycbcr ycbcr;
132 enum chroma_order chroma_order;
133 int ret;
134
135 if (!gralloc_->lock_ycbcr) {
136 static std::once_flag once;
137 std::call_once(once,
138 []() { ALOGW("Gralloc does not support lock_ycbcr()"); });
139 return false;
140 }
141
142 memset(&ycbcr, 0, sizeof(ycbcr));
143 ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr);
144 if (ret) {
145 ALOGW("gralloc->lock_ycbcr failed: %d", ret);
146 return false;
147 }
148 gralloc_->unlock(gralloc_, handle);
149
150 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
151 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
152 * so they can be interpreted as offsets. */
153 bo->offsets[0] = (size_t)ycbcr.y;
154 /* We assume here that all the planes are located in one DMA-buf. */
155 if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
156 chroma_order = YCrCb;
157 bo->offsets[1] = (size_t)ycbcr.cr;
158 bo->offsets[2] = (size_t)ycbcr.cb;
159 } else {
160 chroma_order = YCbCr;
161 bo->offsets[1] = (size_t)ycbcr.cb;
162 bo->offsets[2] = (size_t)ycbcr.cr;
163 }
164
165 /* .ystride is the line length (in bytes) of the Y plane,
166 * .cstride is the line length (in bytes) of any of the remaining
167 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
168 * planar formats. */
169 bo->pitches[0] = ycbcr.ystride;
170 bo->pitches[1] = bo->pitches[2] = ycbcr.cstride;
171
172 /* .chroma_step is the byte distance between the same chroma channel
173 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
174 bo->format = get_fourcc_yuv(bo->hal_format, chroma_order, ycbcr.chroma_step);
175 if (bo->format == -1) {
176 ALOGW(
177 "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
178 "%d",
179 bo->hal_format, chroma_order == YCbCr ? "YCbCr" : "YCrCb",
180 (int)ycbcr.chroma_step);
181 return false;
182 }
183
184 /*
185 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
186 * the single-fd case cannot happen. So handle eithe single
187 * fd or fd-per-plane case:
188 */
189 if (num_fds == 1) {
190 bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0];
191 } else {
192 int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
193 if (num_fds != expected_planes)
194 return false;
195 }
196
197 return true;
198}
199
Sean Paulda6270d2015-06-01 14:11:52 -0400200uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
201 switch (hal_format) {
202 case HAL_PIXEL_FORMAT_RGB_888:
203 return DRM_FORMAT_BGR888;
204 case HAL_PIXEL_FORMAT_BGRA_8888:
205 return DRM_FORMAT_ARGB8888;
206 case HAL_PIXEL_FORMAT_RGBX_8888:
207 return DRM_FORMAT_XBGR8888;
208 case HAL_PIXEL_FORMAT_RGBA_8888:
209 return DRM_FORMAT_ABGR8888;
210 case HAL_PIXEL_FORMAT_RGB_565:
211 return DRM_FORMAT_BGR565;
212 case HAL_PIXEL_FORMAT_YV12:
213 return DRM_FORMAT_YVU420;
214 default:
215 ALOGE("Cannot convert hal format to drm format %u", hal_format);
Roman Stratiienkof63726c2019-11-06 15:03:12 +0200216 return DRM_FORMAT_INVALID;
Sean Paulda6270d2015-06-01 14:11:52 -0400217 }
218}
219
John Stultza4514832018-08-24 16:27:36 -0700220uint32_t DrmGenericImporter::DrmFormatToBitsPerPixel(uint32_t drm_format) {
221 switch (drm_format) {
222 case DRM_FORMAT_ARGB8888:
223 case DRM_FORMAT_XBGR8888:
224 case DRM_FORMAT_ABGR8888:
225 return 32;
226 case DRM_FORMAT_BGR888:
227 return 24;
228 case DRM_FORMAT_BGR565:
229 return 16;
230 case DRM_FORMAT_YVU420:
231 return 12;
232 default:
233 ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format);
234 return 32;
235 }
236}
237
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200238int DrmGenericImporter::ConvertBoInfo(buffer_handle_t handle,
239 hwc_drm_bo_t *bo) {
John Stultzd12274d2018-04-02 20:57:21 -0700240 gralloc_handle_t *gr_handle = gralloc_handle(handle);
Sean Paulda6270d2015-06-01 14:11:52 -0400241 if (!gr_handle)
242 return -EINVAL;
243
Sean Paulda6270d2015-06-01 14:11:52 -0400244 bo->width = gr_handle->width;
245 bo->height = gr_handle->height;
John Stultz616fb222018-08-24 16:08:57 -0700246 bo->hal_format = gr_handle->format;
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +0300247
248#if GRALLOC_HANDLE_VERSION < 4
249#warning libdrm >= v2.4.97 is required to support modifiers
250#endif
251#if GRALLOC_HANDLE_VERSION == 4
252 bo->modifiers[0] = gr_handle->modifier;
253 bo->with_modifiers = gr_handle->modifier != DRM_FORMAT_MOD_NONE &&
254 gr_handle->modifier != DRM_FORMAT_MOD_INVALID;
255#endif
256
Rob Herringaeccd892017-10-06 17:20:05 -0500257 bo->usage = gr_handle->usage;
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +0300258 bo->prime_fds[0] = gr_handle->prime_fd;
259
260 if (is_yuv(gr_handle->format)) {
261 if (!GetYuvPlaneInfo(handle->numFds, handle, bo))
262 return -EINVAL;
263 } else {
264 bo->pitches[0] = gr_handle->stride;
265 bo->offsets[0] = 0;
266 bo->format = ConvertHalFormatToDrm(gr_handle->format);
267 if (bo->format == DRM_FORMAT_INVALID)
268 return -EINVAL;
269 }
270
John Stultza4514832018-08-24 16:27:36 -0700271 bo->pixel_stride = (gr_handle->stride * 8) /
272 DrmFormatToBitsPerPixel(bo->format);
Sean Paulda6270d2015-06-01 14:11:52 -0400273
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200274 return 0;
275}
276
277int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
278 memset(bo, 0, sizeof(hwc_drm_bo_t));
279
280 int ret = ConvertBoInfo(handle, bo);
281 if (ret)
282 return ret;
283
284 ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &bo->gem_handles[0]);
285 if (ret) {
286 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
287 return ret;
288 }
289
290 for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
291 int fd = bo->prime_fds[i];
292 if (fd != 0) {
293 if (fd != bo->prime_fds[0]) {
294 ALOGE("Multiplanar FBs are not supported by this version of composer");
295 return -ENOTSUP;
296 }
297 bo->gem_handles[i] = bo->gem_handles[0];
298 }
299 }
300
301 if (!bo->with_modifiers)
302 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
303 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
304 0);
305 else
306 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
307 bo->format, bo->gem_handles, bo->pitches,
308 bo->offsets, bo->modifiers, &bo->fb_id,
309 bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
310 : 0);
311
Sean Paulda6270d2015-06-01 14:11:52 -0400312 if (ret) {
313 ALOGE("could not create drm fb %d", ret);
314 return ret;
315 }
316
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200317 ImportHandle(bo->gem_handles[0]);
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100318
Sean Paulda6270d2015-06-01 14:11:52 -0400319 return ret;
320}
321
322int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
323 if (bo->fb_id)
324 if (drmModeRmFB(drm_->fd(), bo->fb_id))
325 ALOGE("Failed to rm fb");
326
John Stultzee18b0e2018-08-27 10:53:07 -0700327 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
Sean Paulda6270d2015-06-01 14:11:52 -0400328 if (!bo->gem_handles[i])
329 continue;
330
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100331 if (ReleaseHandle(bo->gem_handles[i])) {
332 ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
John Stultz53cb4ef2018-05-31 17:46:50 -0700333 } else {
John Stultzee18b0e2018-08-27 10:53:07 -0700334 for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
John Stultz53cb4ef2018-05-31 17:46:50 -0700335 if (bo->gem_handles[j] == bo->gem_handles[i])
336 bo->gem_handles[j] = 0;
Sean Paulda6270d2015-06-01 14:11:52 -0400337 bo->gem_handles[i] = 0;
John Stultz53cb4ef2018-05-31 17:46:50 -0700338 }
Sean Paulda6270d2015-06-01 14:11:52 -0400339 }
340 return 0;
341}
Sean Paul4c4646e2016-05-10 04:19:24 -0400342
Alexey Firago18ec6882018-11-21 23:47:05 +0300343bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200344 hwc_drm_bo_t bo;
345
346 int ret = ConvertBoInfo(handle, &bo);
347 if (ret)
Alexey Firago18ec6882018-11-21 23:47:05 +0300348 return false;
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +0300349
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200350 if (bo.prime_fds[0] == 0)
351 return false;
352
353 if (exclude_non_hwfb_ && !(bo.usage & GRALLOC_USAGE_HW_FB))
354 return false;
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +0300355
Alexey Firago18ec6882018-11-21 23:47:05 +0300356 return true;
357}
358
Sean Paul4c4646e2016-05-10 04:19:24 -0400359#ifdef USE_DRM_GENERIC_IMPORTER
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100360std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
Sean Paul4c4646e2016-05-10 04:19:24 -0400361 std::unique_ptr<Planner> planner(new Planner);
362 planner->AddStage<PlanStageGreedy>();
363 return planner;
364}
365#endif
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100366
367int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
368 gem_refcount_[gem_handle]++;
369
370 return 0;
371}
372
373int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
374 if (--gem_refcount_[gem_handle])
375 return 0;
376
377 gem_refcount_.erase(gem_handle);
378
379 return CloseHandle(gem_handle);
380}
381
382int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
383 struct drm_gem_close gem_close;
384
385 memset(&gem_close, 0, sizeof(gem_close));
386
387 gem_close.handle = gem_handle;
388 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
389 if (ret)
390 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
391
392 return ret;
393}
Sean Paulda6270d2015-06-01 14:11:52 -0400394}