blob: ed3a68c9e3320b4d2fced80f60312e83232f0f16 [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"
Sean Paulda6270d2015-06-01 14:11:52 -040020
Sean Paulda6270d2015-06-01 14:11:52 -040021#include <xf86drm.h>
22#include <xf86drmMode.h>
23
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +030024#include <cutils/properties.h>
John Stultzd12274d2018-04-02 20:57:21 -070025#include <gralloc_handle.h>
Sean Paulda6270d2015-06-01 14:11:52 -040026#include <hardware/gralloc.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040027#include <log/log.h>
Sean Paulda6270d2015-06-01 14:11:52 -040028
29namespace android {
30
31#ifdef USE_DRM_GENERIC_IMPORTER
32// static
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010033Importer *Importer::CreateInstance(DrmDevice *drm) {
Sean Paulda6270d2015-06-01 14:11:52 -040034 DrmGenericImporter *importer = new DrmGenericImporter(drm);
35 if (!importer)
36 return NULL;
37
38 int ret = importer->Init();
39 if (ret) {
40 ALOGE("Failed to initialize the nv importer %d", ret);
41 delete importer;
42 return NULL;
43 }
44 return importer;
45}
46#endif
47
Roman Stratiienko42c7f0c2020-08-29 22:28:07 +030048DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
Sean Paulda6270d2015-06-01 14:11:52 -040049}
50
51DrmGenericImporter::~DrmGenericImporter() {
52}
53
54int DrmGenericImporter::Init() {
55 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
56 (const hw_module_t **)&gralloc_);
57 if (ret) {
58 ALOGE("Failed to open gralloc module");
59 return ret;
60 }
Mykhailo Sopihaad438862019-06-06 14:45:27 +030061
62 ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
63 gralloc_->common.author);
64
Sean Paulda6270d2015-06-01 14:11:52 -040065 return 0;
66}
67
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +030068enum chroma_order {
69 YCbCr,
70 YCrCb,
71};
72
73struct droid_yuv_format {
74 /* Lookup keys */
75 int native; /* HAL_PIXEL_FORMAT_ */
76 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
77 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
78
79 /* Result */
80 int fourcc; /* DRM_FORMAT_ */
81};
82
83/* The following table is used to look up a DRI image FourCC based
84 * on native format and information contained in android_ycbcr struct. */
85static const struct droid_yuv_format droid_yuv_formats[] = {
86 /* Native format, YCrCb, Chroma step, DRI image FourCC */
87 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12},
88 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420},
89 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420},
90 {HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420},
91 /* HACK: See droid_create_image_from_prime_fds() and
92 * https://issuetracker.google.com/32077885. */
93 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12},
94 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420},
95 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420},
96 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV},
97 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888},
98};
99
100#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
101
102static int get_fourcc_yuv(int native, enum chroma_order chroma_order,
103 int chroma_step) {
104 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
105 if (droid_yuv_formats[i].native == native &&
106 droid_yuv_formats[i].chroma_order == chroma_order &&
107 droid_yuv_formats[i].chroma_step == chroma_step)
108 return droid_yuv_formats[i].fourcc;
109
110 return -1;
111}
112
113static bool is_yuv(int native) {
114 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
115 if (droid_yuv_formats[i].native == native)
116 return true;
117
118 return false;
119}
120
121bool DrmGenericImporter::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
122 hwc_drm_bo_t *bo) {
123 struct android_ycbcr ycbcr;
124 enum chroma_order chroma_order;
125 int ret;
126
127 if (!gralloc_->lock_ycbcr) {
128 static std::once_flag once;
129 std::call_once(once,
130 []() { ALOGW("Gralloc does not support lock_ycbcr()"); });
131 return false;
132 }
133
134 memset(&ycbcr, 0, sizeof(ycbcr));
135 ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr);
136 if (ret) {
137 ALOGW("gralloc->lock_ycbcr failed: %d", ret);
138 return false;
139 }
140 gralloc_->unlock(gralloc_, handle);
141
142 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
143 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
144 * so they can be interpreted as offsets. */
145 bo->offsets[0] = (size_t)ycbcr.y;
146 /* We assume here that all the planes are located in one DMA-buf. */
147 if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
148 chroma_order = YCrCb;
149 bo->offsets[1] = (size_t)ycbcr.cr;
150 bo->offsets[2] = (size_t)ycbcr.cb;
151 } else {
152 chroma_order = YCbCr;
153 bo->offsets[1] = (size_t)ycbcr.cb;
154 bo->offsets[2] = (size_t)ycbcr.cr;
155 }
156
157 /* .ystride is the line length (in bytes) of the Y plane,
158 * .cstride is the line length (in bytes) of any of the remaining
159 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
160 * planar formats. */
161 bo->pitches[0] = ycbcr.ystride;
162 bo->pitches[1] = bo->pitches[2] = ycbcr.cstride;
163
164 /* .chroma_step is the byte distance between the same chroma channel
165 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
166 bo->format = get_fourcc_yuv(bo->hal_format, chroma_order, ycbcr.chroma_step);
167 if (bo->format == -1) {
168 ALOGW(
169 "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
170 "%d",
171 bo->hal_format, chroma_order == YCbCr ? "YCbCr" : "YCrCb",
172 (int)ycbcr.chroma_step);
173 return false;
174 }
175
176 /*
177 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
178 * the single-fd case cannot happen. So handle eithe single
179 * fd or fd-per-plane case:
180 */
181 if (num_fds == 1) {
182 bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0];
183 } else {
184 int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
185 if (num_fds != expected_planes)
186 return false;
187 }
188
189 return true;
190}
191
Sean Paulda6270d2015-06-01 14:11:52 -0400192uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
193 switch (hal_format) {
194 case HAL_PIXEL_FORMAT_RGB_888:
195 return DRM_FORMAT_BGR888;
196 case HAL_PIXEL_FORMAT_BGRA_8888:
197 return DRM_FORMAT_ARGB8888;
198 case HAL_PIXEL_FORMAT_RGBX_8888:
199 return DRM_FORMAT_XBGR8888;
200 case HAL_PIXEL_FORMAT_RGBA_8888:
201 return DRM_FORMAT_ABGR8888;
202 case HAL_PIXEL_FORMAT_RGB_565:
203 return DRM_FORMAT_BGR565;
204 case HAL_PIXEL_FORMAT_YV12:
205 return DRM_FORMAT_YVU420;
206 default:
207 ALOGE("Cannot convert hal format to drm format %u", hal_format);
Roman Stratiienkof63726c2019-11-06 15:03:12 +0200208 return DRM_FORMAT_INVALID;
Sean Paulda6270d2015-06-01 14:11:52 -0400209 }
210}
211
John Stultza4514832018-08-24 16:27:36 -0700212uint32_t DrmGenericImporter::DrmFormatToBitsPerPixel(uint32_t drm_format) {
213 switch (drm_format) {
214 case DRM_FORMAT_ARGB8888:
215 case DRM_FORMAT_XBGR8888:
216 case DRM_FORMAT_ABGR8888:
217 return 32;
218 case DRM_FORMAT_BGR888:
219 return 24;
220 case DRM_FORMAT_BGR565:
221 return 16;
222 case DRM_FORMAT_YVU420:
223 return 12;
224 default:
225 ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format);
226 return 32;
227 }
228}
229
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200230int DrmGenericImporter::ConvertBoInfo(buffer_handle_t handle,
231 hwc_drm_bo_t *bo) {
John Stultzd12274d2018-04-02 20:57:21 -0700232 gralloc_handle_t *gr_handle = gralloc_handle(handle);
Sean Paulda6270d2015-06-01 14:11:52 -0400233 if (!gr_handle)
234 return -EINVAL;
235
Sean Paulda6270d2015-06-01 14:11:52 -0400236 bo->width = gr_handle->width;
237 bo->height = gr_handle->height;
John Stultz616fb222018-08-24 16:08:57 -0700238 bo->hal_format = gr_handle->format;
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +0300239
240#if GRALLOC_HANDLE_VERSION < 4
241#warning libdrm >= v2.4.97 is required to support modifiers
242#endif
243#if GRALLOC_HANDLE_VERSION == 4
244 bo->modifiers[0] = gr_handle->modifier;
245 bo->with_modifiers = gr_handle->modifier != DRM_FORMAT_MOD_NONE &&
246 gr_handle->modifier != DRM_FORMAT_MOD_INVALID;
247#endif
248
Rob Herringaeccd892017-10-06 17:20:05 -0500249 bo->usage = gr_handle->usage;
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +0300250 bo->prime_fds[0] = gr_handle->prime_fd;
251
252 if (is_yuv(gr_handle->format)) {
253 if (!GetYuvPlaneInfo(handle->numFds, handle, bo))
254 return -EINVAL;
255 } else {
256 bo->pitches[0] = gr_handle->stride;
257 bo->offsets[0] = 0;
258 bo->format = ConvertHalFormatToDrm(gr_handle->format);
259 if (bo->format == DRM_FORMAT_INVALID)
260 return -EINVAL;
261 }
262
John Stultza4514832018-08-24 16:27:36 -0700263 bo->pixel_stride = (gr_handle->stride * 8) /
264 DrmFormatToBitsPerPixel(bo->format);
Sean Paulda6270d2015-06-01 14:11:52 -0400265
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200266 return 0;
267}
268
269int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
270 memset(bo, 0, sizeof(hwc_drm_bo_t));
271
272 int ret = ConvertBoInfo(handle, bo);
273 if (ret)
274 return ret;
275
276 ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &bo->gem_handles[0]);
277 if (ret) {
278 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
279 return ret;
280 }
281
282 for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
283 int fd = bo->prime_fds[i];
284 if (fd != 0) {
285 if (fd != bo->prime_fds[0]) {
286 ALOGE("Multiplanar FBs are not supported by this version of composer");
287 return -ENOTSUP;
288 }
289 bo->gem_handles[i] = bo->gem_handles[0];
290 }
291 }
292
293 if (!bo->with_modifiers)
294 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
295 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
296 0);
297 else
298 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
299 bo->format, bo->gem_handles, bo->pitches,
300 bo->offsets, bo->modifiers, &bo->fb_id,
301 bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
302 : 0);
303
Sean Paulda6270d2015-06-01 14:11:52 -0400304 if (ret) {
305 ALOGE("could not create drm fb %d", ret);
306 return ret;
307 }
308
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200309 ImportHandle(bo->gem_handles[0]);
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100310
Sean Paulda6270d2015-06-01 14:11:52 -0400311 return ret;
312}
313
314int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
315 if (bo->fb_id)
316 if (drmModeRmFB(drm_->fd(), bo->fb_id))
317 ALOGE("Failed to rm fb");
318
John Stultzee18b0e2018-08-27 10:53:07 -0700319 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
Sean Paulda6270d2015-06-01 14:11:52 -0400320 if (!bo->gem_handles[i])
321 continue;
322
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100323 if (ReleaseHandle(bo->gem_handles[i])) {
324 ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
John Stultz53cb4ef2018-05-31 17:46:50 -0700325 } else {
John Stultzee18b0e2018-08-27 10:53:07 -0700326 for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
John Stultz53cb4ef2018-05-31 17:46:50 -0700327 if (bo->gem_handles[j] == bo->gem_handles[i])
328 bo->gem_handles[j] = 0;
Sean Paulda6270d2015-06-01 14:11:52 -0400329 bo->gem_handles[i] = 0;
John Stultz53cb4ef2018-05-31 17:46:50 -0700330 }
Sean Paulda6270d2015-06-01 14:11:52 -0400331 }
332 return 0;
333}
Sean Paul4c4646e2016-05-10 04:19:24 -0400334
Alexey Firago18ec6882018-11-21 23:47:05 +0300335bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200336 hwc_drm_bo_t bo;
337
338 int ret = ConvertBoInfo(handle, &bo);
339 if (ret)
Alexey Firago18ec6882018-11-21 23:47:05 +0300340 return false;
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +0300341
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200342 if (bo.prime_fds[0] == 0)
343 return false;
344
Alexey Firago18ec6882018-11-21 23:47:05 +0300345 return true;
346}
347
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100348std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
Sean Paul4c4646e2016-05-10 04:19:24 -0400349 std::unique_ptr<Planner> planner(new Planner);
350 planner->AddStage<PlanStageGreedy>();
351 return planner;
352}
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100353
354int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
355 gem_refcount_[gem_handle]++;
356
357 return 0;
358}
359
360int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
361 if (--gem_refcount_[gem_handle])
362 return 0;
363
364 gem_refcount_.erase(gem_handle);
365
366 return CloseHandle(gem_handle);
367}
368
369int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
370 struct drm_gem_close gem_close;
371
372 memset(&gem_close, 0, sizeof(gem_close));
373
374 gem_close.handle = gem_handle;
375 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
376 if (ret)
377 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
378
379 return ret;
380}
Sean Paulda6270d2015-06-01 14:11:52 -0400381}