blob: 59f1be9cc1603e14fed5b3e5a46b45378fe07680 [file] [log] [blame]
Roman Stratiienko946126c2020-10-02 19:01:10 +03001/*
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
17#define LOG_TAG "hwc-platform-libdrm"
18
19#include "platformlibdrm.h"
20
21#include <cutils/properties.h>
22#include <gralloc_handle.h>
23#include <hardware/gralloc.h>
24#include <log/log.h>
25#include <xf86drm.h>
26#include <xf86drmMode.h>
27
28namespace android {
29
30Importer *Importer::CreateInstance(DrmDevice *drm) {
31 DrmGenericImporter *importer = new LibdrmImporter(drm);
32 if (!importer)
33 return NULL;
34
35 int ret = importer->Init();
36 if (ret) {
37 ALOGE("Failed to initialize the libdrm importer %d", ret);
38 delete importer;
39 return NULL;
40 }
41 return importer;
42}
43
44enum chroma_order {
45 YCbCr,
46 YCrCb,
47};
48
49struct droid_yuv_format {
50 /* Lookup keys */
51 int native; /* HAL_PIXEL_FORMAT_ */
52 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
53 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
54
55 /* Result */
56 int fourcc; /* DRM_FORMAT_ */
57};
58
59/* The following table is used to look up a DRI image FourCC based
60 * on native format and information contained in android_ycbcr struct. */
61static const struct droid_yuv_format droid_yuv_formats[] = {
62 /* Native format, YCrCb, Chroma step, DRI image FourCC */
63 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12},
64 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420},
65 {HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420},
66 {HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420},
67 /* HACK: See droid_create_image_from_prime_fds() and
68 * https://issuetracker.google.com/32077885. */
69 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12},
70 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420},
71 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420},
72 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV},
73 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888},
74};
75
76#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77
78static int get_fourcc_yuv(int native, enum chroma_order chroma_order,
79 int chroma_step) {
80 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
81 if (droid_yuv_formats[i].native == native &&
82 droid_yuv_formats[i].chroma_order == chroma_order &&
83 droid_yuv_formats[i].chroma_step == chroma_step)
84 return droid_yuv_formats[i].fourcc;
85
86 return -1;
87}
88
89static bool is_yuv(int native) {
90 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
91 if (droid_yuv_formats[i].native == native)
92 return true;
93
94 return false;
95}
96
97bool LibdrmImporter::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
98 hwc_drm_bo_t *bo) {
99 struct android_ycbcr ycbcr;
100 enum chroma_order chroma_order;
101 int ret;
102
103 if (!gralloc_->lock_ycbcr) {
104 static std::once_flag once;
105 std::call_once(once,
106 []() { ALOGW("Gralloc does not support lock_ycbcr()"); });
107 return false;
108 }
109
110 memset(&ycbcr, 0, sizeof(ycbcr));
111 ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr);
112 if (ret) {
113 ALOGW("gralloc->lock_ycbcr failed: %d", ret);
114 return false;
115 }
116 gralloc_->unlock(gralloc_, handle);
117
118 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
119 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
120 * so they can be interpreted as offsets. */
121 bo->offsets[0] = (size_t)ycbcr.y;
122 /* We assume here that all the planes are located in one DMA-buf. */
123 if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
124 chroma_order = YCrCb;
125 bo->offsets[1] = (size_t)ycbcr.cr;
126 bo->offsets[2] = (size_t)ycbcr.cb;
127 } else {
128 chroma_order = YCbCr;
129 bo->offsets[1] = (size_t)ycbcr.cb;
130 bo->offsets[2] = (size_t)ycbcr.cr;
131 }
132
133 /* .ystride is the line length (in bytes) of the Y plane,
134 * .cstride is the line length (in bytes) of any of the remaining
135 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
136 * planar formats. */
137 bo->pitches[0] = ycbcr.ystride;
138 bo->pitches[1] = bo->pitches[2] = ycbcr.cstride;
139
140 /* .chroma_step is the byte distance between the same chroma channel
141 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
142 bo->format = get_fourcc_yuv(bo->hal_format, chroma_order, ycbcr.chroma_step);
143 if (bo->format == -1) {
144 ALOGW(
145 "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
146 "%d",
147 bo->hal_format, chroma_order == YCbCr ? "YCbCr" : "YCrCb",
148 (int)ycbcr.chroma_step);
149 return false;
150 }
151
152 /*
153 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
154 * the single-fd case cannot happen. So handle eithe single
155 * fd or fd-per-plane case:
156 */
157 if (num_fds == 1) {
158 bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0];
159 } else {
160 int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
161 if (num_fds != expected_planes)
162 return false;
163 }
164
165 return true;
166}
167
168int LibdrmImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
169 gralloc_handle_t *gr_handle = gralloc_handle(handle);
170 if (!gr_handle)
171 return -EINVAL;
172
173 bo->width = gr_handle->width;
174 bo->height = gr_handle->height;
175 bo->hal_format = gr_handle->format;
176
177#if GRALLOC_HANDLE_VERSION < 4
178 static std::once_flag once;
179 std::call_once(once, []() {
180 ALOGE(
181 "libdrm < v2.4.97 has broken gralloc_handle structure. Please update.");
182 });
183#endif
184#if GRALLOC_HANDLE_VERSION == 4
185 bo->modifiers[0] = gr_handle->modifier;
186 bo->with_modifiers = gr_handle->modifier != DRM_FORMAT_MOD_NONE &&
187 gr_handle->modifier != DRM_FORMAT_MOD_INVALID;
188#endif
189
190 bo->usage = gr_handle->usage;
191 bo->prime_fds[0] = gr_handle->prime_fd;
192
193 if (is_yuv(gr_handle->format)) {
194 if (!GetYuvPlaneInfo(handle->numFds, handle, bo))
195 return -EINVAL;
196 } else {
197 bo->pitches[0] = gr_handle->stride;
198 bo->offsets[0] = 0;
199 bo->format = ConvertHalFormatToDrm(gr_handle->format);
200 if (bo->format == DRM_FORMAT_INVALID)
201 return -EINVAL;
202 }
203
204 bo->pixel_stride = (gr_handle->stride * 8) /
205 DrmFormatToBitsPerPixel(bo->format);
206
207 return 0;
208}
209
210} // namespace android