blob: 95a1eac52c16b5cc1906b7dce76e2635b0f3b126 [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
Roman Stratiienkoacdea8c2020-09-23 11:51:12 +0300241 static std::once_flag once;
242 std::call_once(once, []() {
243 ALOGE(
244 "libdrm < v2.4.97 has broken gralloc_handle structure. Please update.");
245 });
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +0300246#endif
247#if GRALLOC_HANDLE_VERSION == 4
248 bo->modifiers[0] = gr_handle->modifier;
249 bo->with_modifiers = gr_handle->modifier != DRM_FORMAT_MOD_NONE &&
250 gr_handle->modifier != DRM_FORMAT_MOD_INVALID;
251#endif
252
Rob Herringaeccd892017-10-06 17:20:05 -0500253 bo->usage = gr_handle->usage;
Roman Stratiienkofbf5c0c2020-08-24 11:27:48 +0300254 bo->prime_fds[0] = gr_handle->prime_fd;
255
256 if (is_yuv(gr_handle->format)) {
257 if (!GetYuvPlaneInfo(handle->numFds, handle, bo))
258 return -EINVAL;
259 } else {
260 bo->pitches[0] = gr_handle->stride;
261 bo->offsets[0] = 0;
262 bo->format = ConvertHalFormatToDrm(gr_handle->format);
263 if (bo->format == DRM_FORMAT_INVALID)
264 return -EINVAL;
265 }
266
John Stultza4514832018-08-24 16:27:36 -0700267 bo->pixel_stride = (gr_handle->stride * 8) /
268 DrmFormatToBitsPerPixel(bo->format);
Sean Paulda6270d2015-06-01 14:11:52 -0400269
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200270 return 0;
271}
272
273int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
274 memset(bo, 0, sizeof(hwc_drm_bo_t));
275
276 int ret = ConvertBoInfo(handle, bo);
277 if (ret)
278 return ret;
279
280 ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &bo->gem_handles[0]);
281 if (ret) {
282 ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
283 return ret;
284 }
285
286 for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
287 int fd = bo->prime_fds[i];
288 if (fd != 0) {
289 if (fd != bo->prime_fds[0]) {
290 ALOGE("Multiplanar FBs are not supported by this version of composer");
291 return -ENOTSUP;
292 }
293 bo->gem_handles[i] = bo->gem_handles[0];
294 }
295 }
296
297 if (!bo->with_modifiers)
298 ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
299 bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
300 0);
301 else
302 ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
303 bo->format, bo->gem_handles, bo->pitches,
304 bo->offsets, bo->modifiers, &bo->fb_id,
305 bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
306 : 0);
307
Sean Paulda6270d2015-06-01 14:11:52 -0400308 if (ret) {
309 ALOGE("could not create drm fb %d", ret);
310 return ret;
311 }
312
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200313 ImportHandle(bo->gem_handles[0]);
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100314
Sean Paulda6270d2015-06-01 14:11:52 -0400315 return ret;
316}
317
318int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
319 if (bo->fb_id)
320 if (drmModeRmFB(drm_->fd(), bo->fb_id))
321 ALOGE("Failed to rm fb");
322
John Stultzee18b0e2018-08-27 10:53:07 -0700323 for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
Sean Paulda6270d2015-06-01 14:11:52 -0400324 if (!bo->gem_handles[i])
325 continue;
326
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100327 if (ReleaseHandle(bo->gem_handles[i])) {
328 ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
John Stultz53cb4ef2018-05-31 17:46:50 -0700329 } else {
John Stultzee18b0e2018-08-27 10:53:07 -0700330 for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
John Stultz53cb4ef2018-05-31 17:46:50 -0700331 if (bo->gem_handles[j] == bo->gem_handles[i])
332 bo->gem_handles[j] = 0;
Sean Paulda6270d2015-06-01 14:11:52 -0400333 bo->gem_handles[i] = 0;
John Stultz53cb4ef2018-05-31 17:46:50 -0700334 }
Sean Paulda6270d2015-06-01 14:11:52 -0400335 }
336 return 0;
337}
Sean Paul4c4646e2016-05-10 04:19:24 -0400338
Alexey Firago18ec6882018-11-21 23:47:05 +0300339bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200340 hwc_drm_bo_t bo;
341
342 int ret = ConvertBoInfo(handle, &bo);
343 if (ret)
Alexey Firago18ec6882018-11-21 23:47:05 +0300344 return false;
Mykhailo Sopiha1693bc32019-07-08 18:28:56 +0300345
Roman Stratiienko4163efc2019-12-06 12:30:28 +0200346 if (bo.prime_fds[0] == 0)
347 return false;
348
Alexey Firago18ec6882018-11-21 23:47:05 +0300349 return true;
350}
351
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +0100352std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
Sean Paul4c4646e2016-05-10 04:19:24 -0400353 std::unique_ptr<Planner> planner(new Planner);
354 planner->AddStage<PlanStageGreedy>();
355 return planner;
356}
Vincent Donnefortf67c3652019-08-02 11:18:35 +0100357
358int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
359 gem_refcount_[gem_handle]++;
360
361 return 0;
362}
363
364int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
365 if (--gem_refcount_[gem_handle])
366 return 0;
367
368 gem_refcount_.erase(gem_handle);
369
370 return CloseHandle(gem_handle);
371}
372
373int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
374 struct drm_gem_close gem_close;
375
376 memset(&gem_close, 0, sizeof(gem_close));
377
378 gem_close.handle = gem_handle;
379 int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
380 if (ret)
381 ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
382
383 return ret;
384}
Sean Paulda6270d2015-06-01 14:11:52 -0400385}