1 #pragma once
2 /*
3 * Copyright (C) 2016 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #include <stdint.h>
18 #include <limits.h>
19 #include <string.h>
20 #include <sys/cdefs.h>
21 #include <sys/mman.h>
22 #include <hardware/gralloc.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include <cutils/native_handle.h>
28 #include <log/log.h>
29
30 #include <linux/fb.h>
31
32 #ifndef GRALLOC_MODULE_API_VERSION_0_2
33 // This structure will be defined in later releases of Android. Declare it
34 // here to allow us to structure the code well.
35 struct android_ycbcr {
36 void* y;
37 void* cb;
38 void* cr;
39 size_t ystride;
40 size_t cstride;
41 size_t chroma_step;
42 uint32_t reserved[8];
43 };
44 #endif
45
46 namespace cuttlefish {
47 namespace screen {
48
49 struct ScreenRegionView {
alignScreenRegionView50 static int align(int input) {
51 auto constexpr alignment = 16;
52 return (input + alignment - 1) & -alignment;
53 }
54 static constexpr int kSwiftShaderPadding = 4;
55 };
56
57 }
58 }
59
60 /*****************************************************************************/
61
62 struct private_handle_t;
63
64 struct private_module_t {
65 gralloc_module_t base;
66 };
67
68 /*****************************************************************************/
69
70 struct priv_alloc_device_t {
71 alloc_device_t device;
72 };
73
74 /*****************************************************************************/
75
76 struct private_handle_t : public native_handle {
77 // file-descriptors
78 int fd;
79 // ints
80 int magic;
81 int flags;
82 int format;
83 int x_res;
84 int y_res;
85 int stride_in_pixels;
86 // Use to indicate which frame we're using.
87 int frame_offset;
88 int total_size;
89 int lock_level;
90
sNumIntsprivate_handle_t91 static inline int sNumInts() {
92 return (((sizeof(private_handle_t) - sizeof(native_handle_t))/sizeof(int)) - sNumFds);
93 }
94 static const int sNumFds = 1;
95 static const int sMagic = 0x3141592;
96
97 private_handle_t(int fd, int size, int format, int x_res, int y_res,
98 int stride_in_pixels, int flags, int frame_offset = 0)
fdprivate_handle_t99 : fd(fd),
100 magic(sMagic),
101 flags(flags),
102 format(format),
103 x_res(x_res),
104 y_res(y_res),
105 stride_in_pixels(stride_in_pixels),
106 frame_offset(frame_offset),
107 total_size(size),
108 lock_level(0) {
109 version = sizeof(native_handle);
110 numInts = sNumInts();
111 numFds = sNumFds;
112 }
113
~private_handle_tprivate_handle_t114 ~private_handle_t() {
115 magic = 0;
116 }
117
validateprivate_handle_t118 static int validate(const native_handle* h) {
119 const private_handle_t* hnd = (const private_handle_t*)h;
120 if (!h) {
121 ALOGE("invalid gralloc handle (at %p): NULL pointer", h);
122 return -EINVAL;
123 }
124 if (h->version != sizeof(native_handle)) {
125 ALOGE(
126 "invalid gralloc handle (at %p): Wrong version(observed: %d, "
127 "expected: %zu)",
128 h,
129 h->version,
130 sizeof(native_handle));
131 return -EINVAL;
132 }
133 if (h->numInts != sNumInts()) {
134 ALOGE(
135 "invalid gralloc handle (at %p): Wrong number of ints(observed: %d, "
136 "expected: %d)",
137 h,
138 h->numInts,
139 sNumInts());
140 return -EINVAL;
141 }
142 if (h->numFds != sNumFds) {
143 ALOGE(
144 "invalid gralloc handle (at %p): Wrong number of file "
145 "descriptors(observed: %d, expected: %d)",
146 h,
147 h->numFds,
148 sNumFds);
149 return -EINVAL;
150 }
151 if (hnd->magic != sMagic) {
152 ALOGE(
153 "invalid gralloc handle (at %p): Wrong magic number(observed: %d, "
154 "expected: %d)",
155 h,
156 hnd->magic,
157 sMagic);
158 return -EINVAL;
159 }
160 return 0;
161 }
162 };
163
164
formatToBytesPerPixel(int format)165 static inline int formatToBytesPerPixel(int format) {
166 switch (format) {
167 case HAL_PIXEL_FORMAT_RGBA_FP16:
168 return 8;
169 case HAL_PIXEL_FORMAT_RGBA_8888:
170 case HAL_PIXEL_FORMAT_RGBX_8888:
171 case HAL_PIXEL_FORMAT_BGRA_8888:
172 // The camera 3.0 implementation assumes that IMPLEMENTATION_DEFINED
173 // means HAL_PIXEL_FORMAT_RGBA_8888
174 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
175 return 4;
176 case HAL_PIXEL_FORMAT_RGB_888:
177 return 3;
178 case HAL_PIXEL_FORMAT_RGB_565:
179 case HAL_PIXEL_FORMAT_YV12:
180 #ifdef GRALLOC_MODULE_API_VERSION_0_2
181 case HAL_PIXEL_FORMAT_YCbCr_420_888:
182 #endif
183 return 2;
184 case HAL_PIXEL_FORMAT_BLOB:
185 return 1;
186 default:
187 ALOGE("%s: unknown format=%d", __FUNCTION__, format);
188 return 8;
189 }
190 }
191
pixel_format_to_string(int format)192 inline const char* pixel_format_to_string(int format) {
193 switch (format) {
194 // Formats that are universal across versions
195 case HAL_PIXEL_FORMAT_RGBA_8888:
196 return "RGBA_8888";
197 case HAL_PIXEL_FORMAT_RGBX_8888:
198 return "RGBX_8888";
199 case HAL_PIXEL_FORMAT_BGRA_8888:
200 return "BGRA_8888";
201 case HAL_PIXEL_FORMAT_RGB_888:
202 return "RGB_888";
203 case HAL_PIXEL_FORMAT_RGB_565:
204 return "RGB_565";
205 case HAL_PIXEL_FORMAT_YV12:
206 return "YV12";
207 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
208 return "YCrCb_420_SP";
209 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
210 return "YCbCr_422_SP";
211 case HAL_PIXEL_FORMAT_YCbCr_422_I:
212 return "YCbCr_422_I";
213
214 // First supported on JBMR1 (API 17)
215 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
216 return "IMPLEMENTATION_DEFINED";
217 case HAL_PIXEL_FORMAT_BLOB:
218 return "BLOB";
219 // First supported on JBMR2 (API 18)
220 case HAL_PIXEL_FORMAT_YCbCr_420_888:
221 return "YCbCr_420_888";
222 case HAL_PIXEL_FORMAT_Y8:
223 return "Y8";
224 case HAL_PIXEL_FORMAT_Y16:
225 return "Y16";
226 // Support was added in L (API 21)
227 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
228 return "RAW_OPAQUE";
229 // This is an alias for RAW_SENSOR in L and replaces it in M.
230 case HAL_PIXEL_FORMAT_RAW16:
231 return "RAW16";
232 case HAL_PIXEL_FORMAT_RAW10:
233 return "RAW10";
234 case HAL_PIXEL_FORMAT_YCbCr_444_888:
235 return "YCbCr_444_888";
236 case HAL_PIXEL_FORMAT_YCbCr_422_888:
237 return "YCbCr_422_888";
238 case HAL_PIXEL_FORMAT_RAW12:
239 return "RAW12";
240 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
241 return "FLEX_RGBA_8888";
242 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
243 return "FLEX_RGB_888";
244 case HAL_PIXEL_FORMAT_RGBA_FP16:
245 return "RGBA_FP16";
246 }
247 return "UNKNOWN";
248 }
249
250
formatToYcbcr(int format,int width,int height,void * base_v,android_ycbcr * out)251 static inline void formatToYcbcr(
252 int format, int width, int height, void* base_v, android_ycbcr* out) {
253 char* it = static_cast<char*>(base_v);
254 // Clear reserved fields;
255 memset(out, 0, sizeof(*out));
256 switch (format) {
257 case HAL_PIXEL_FORMAT_YV12:
258 #ifdef GRALLOC_MODULE_API_VERSION_0_2
259 case HAL_PIXEL_FORMAT_YCbCr_420_888:
260 #endif
261 out->ystride = cuttlefish::screen::ScreenRegionView::align(width);
262 out->cstride =
263 cuttlefish::screen::ScreenRegionView::align(out->ystride / 2);
264 out->chroma_step = 1;
265 out->y = it;
266 it += out->ystride * height;
267 out->cr = it;
268 it += out->cstride * height / 2;
269 out->cb = it;
270 break;
271 default:
272 ALOGE("%s: can't deal with format=0x%x (%s)",
273 __FUNCTION__, format, pixel_format_to_string(format));
274 }
275 }
276
formatToBytesPerFrame(int format,int w,int h)277 static inline int formatToBytesPerFrame(int format, int w, int h) {
278 int bytes_per_pixel = formatToBytesPerPixel(format);
279 int w16, h16;
280 int y_size, c_size;
281
282 switch (format) {
283 // BLOB is used to allocate buffers for JPEG formatted data. Bytes per pixel
284 // is 1, the desired buffer size is in w, and h should be 1. We refrain from
285 // adding additional padding, although the caller is likely to round
286 // up to a page size.
287 case HAL_PIXEL_FORMAT_BLOB:
288 return bytes_per_pixel * w * h;
289 case HAL_PIXEL_FORMAT_YV12:
290 #ifdef GRALLOC_MODULE_API_VERSION_0_2
291 case HAL_PIXEL_FORMAT_YCbCr_420_888:
292 #endif
293 android_ycbcr strides;
294 formatToYcbcr(format, w, h, NULL, &strides);
295 y_size = strides.ystride * h;
296 c_size = strides.cstride * h / 2;
297 return (y_size + 2 * c_size +
298 cuttlefish::screen::ScreenRegionView::kSwiftShaderPadding);
299 /*case HAL_PIXEL_FORMAT_RGBA_8888:
300 case HAL_PIXEL_FORMAT_RGBX_8888:
301 case HAL_PIXEL_FORMAT_BGRA_8888:
302 case HAL_PIXEL_FORMAT_RGB_888:
303 case HAL_PIXEL_FORMAT_RGB_565:*/
304 default:
305 w16 = cuttlefish::screen::ScreenRegionView::align(w);
306 h16 = cuttlefish::screen::ScreenRegionView::align(h);
307 return bytes_per_pixel * w16 * h16 +
308 cuttlefish::screen::ScreenRegionView::kSwiftShaderPadding;
309 }
310 }
311
312 int gralloc_lock(
313 gralloc_module_t const* module,
314 buffer_handle_t handle, int usage,
315 int l, int t, int w, int h,
316 void** vaddr);
317
318 int gralloc_unlock(
319 gralloc_module_t const* module, buffer_handle_t handle);
320
321 int gralloc_register_buffer(
322 gralloc_module_t const* module, buffer_handle_t handle);
323
324 int gralloc_unregister_buffer(
325 gralloc_module_t const* module, buffer_handle_t handle);
326
327 int gralloc_lock_ycbcr(
328 struct gralloc_module_t const* module,
329 buffer_handle_t handle, int usage,
330 int l, int t, int w, int h,
331 struct android_ycbcr *ycbcr);
332
333 int32_t gralloc_get_transport_size(
334 struct gralloc_module_t const* module, buffer_handle_t handle,
335 uint32_t *outNumFds, uint32_t *outNumInts);
336
337 int32_t gralloc_validate_buffer_size(
338 struct gralloc_module_t const* device, buffer_handle_t handle,
339 uint32_t w, uint32_t h, int32_t format, int usage,
340 uint32_t stride);
341