Commit | Line | Data |
---|---|---|
2480eccc RSZ |
1 | /* |
2 | * Copyright (C) 2008 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 | ||
2480eccc RSZ |
17 | #include <limits.h> |
18 | #include <unistd.h> | |
19 | #include <fcntl.h> | |
20 | #include <errno.h> | |
21 | #include <pthread.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | ||
25 | #include <sys/mman.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/ioctl.h> | |
29 | ||
30 | #include <ion/ion.h> | |
31 | #include <linux/ion.h> | |
32 | #include <cutils/log.h> | |
33 | #include <cutils/atomic.h> | |
34 | ||
35 | #include <hardware/hardware.h> | |
36 | #include <hardware/gralloc.h> | |
37 | ||
38 | #include "gralloc_priv.h" | |
39 | #include "exynos_format.h" | |
2480eccc | 40 | |
88c9cff4 | 41 | #define ION_HEAP_EXYNOS_CONTIG_MASK (1 << 4) |
ce73ba18 JK |
42 | #define ION_EXYNOS_FIMD_VIDEO_MASK (1 << 28) |
43 | #define ION_EXYNOS_MFC_OUTPUT_MASK (1 << 26) | |
44 | #define ION_EXYNOS_MFC_INPUT_MASK (1 << 25) | |
88c9cff4 RSZ |
45 | #define ION_HEAP_SYSTEM_ID 0 |
46 | #define ION_HEAP_EXYNOS_CONTIG_ID 4 | |
47 | #define ION_HEAP_CHUNK_ID 6 | |
ab73a889 | 48 | #define MB_1 (1024*1024) |
ce73ba18 | 49 | |
788e20df | 50 | |
2480eccc RSZ |
51 | /*****************************************************************************/ |
52 | ||
53 | struct gralloc_context_t { | |
54 | alloc_device_t device; | |
55 | /* our private data here */ | |
56 | }; | |
57 | ||
58 | static int gralloc_alloc_buffer(alloc_device_t* dev, | |
59 | size_t size, int usage, buffer_handle_t* pHandle); | |
60 | ||
61 | /*****************************************************************************/ | |
62 | ||
63 | int fb_device_open(const hw_module_t* module, const char* name, | |
64 | hw_device_t** device); | |
65 | ||
66 | static int gralloc_device_open(const hw_module_t* module, const char* name, | |
67 | hw_device_t** device); | |
68 | ||
69 | extern int gralloc_lock(gralloc_module_t const* module, | |
70 | buffer_handle_t handle, int usage, | |
71 | int l, int t, int w, int h, | |
72 | void** vaddr); | |
73 | ||
74 | extern int gralloc_unlock(gralloc_module_t const* module, | |
75 | buffer_handle_t handle); | |
76 | ||
77 | extern int gralloc_register_buffer(gralloc_module_t const* module, | |
78 | buffer_handle_t handle); | |
79 | ||
80 | extern int gralloc_unregister_buffer(gralloc_module_t const* module, | |
81 | buffer_handle_t handle); | |
82 | ||
83 | /*****************************************************************************/ | |
84 | ||
85 | static struct hw_module_methods_t gralloc_module_methods = { | |
86 | open: gralloc_device_open | |
87 | }; | |
88 | ||
89 | struct private_module_t HAL_MODULE_INFO_SYM = { | |
90 | base: { | |
a776338e RSZ |
91 | common: { |
92 | tag: HARDWARE_MODULE_TAG, | |
93 | version_major: 1, | |
94 | version_minor: 0, | |
95 | id: GRALLOC_HARDWARE_MODULE_ID, | |
96 | name: "Graphics Memory Allocator Module", | |
97 | author: "The Android Open Source Project", | |
98 | methods: &gralloc_module_methods | |
99 | }, | |
ec68ab21 RSZ |
100 | registerBuffer: gralloc_register_buffer, |
101 | unregisterBuffer: gralloc_unregister_buffer, | |
102 | lock: gralloc_lock, | |
103 | unlock: gralloc_unlock, | |
a776338e RSZ |
104 | }, |
105 | framebuffer: 0, | |
106 | flags: 0, | |
107 | numBuffers: 0, | |
108 | bufferMask: 0, | |
109 | lock: PTHREAD_MUTEX_INITIALIZER, | |
4d9f47b6 | 110 | refcount: 0, |
a776338e RSZ |
111 | currentBuffer: 0, |
112 | ionfd: -1, | |
2480eccc RSZ |
113 | }; |
114 | ||
115 | /*****************************************************************************/ | |
116 | ||
788e20df SK |
117 | static unsigned int _select_heap(int usage) |
118 | { | |
119 | unsigned int heap_mask; | |
120 | ||
121 | if (usage & GRALLOC_USAGE_PROTECTED) | |
88c9cff4 | 122 | heap_mask = (1 << ION_HEAP_EXYNOS_CONTIG_ID); |
788e20df | 123 | else |
88c9cff4 | 124 | heap_mask = (1 << ION_HEAP_SYSTEM_ID) | (1 << ION_HEAP_CHUNK_ID); |
788e20df SK |
125 | |
126 | return heap_mask; | |
127 | } | |
128 | ||
da3d8712 RSZ |
129 | static int gralloc_alloc_rgb(int ionfd, int w, int h, int format, int usage, |
130 | unsigned int ion_flags, private_handle_t **hnd, int *stride) | |
2480eccc | 131 | { |
ab73a889 | 132 | size_t size, bpr, alignment = 0; |
70212e56 | 133 | int bpp = 0, vstride, fd, err; |
788e20df SK |
134 | unsigned int heap_mask = _select_heap(usage); |
135 | ||
2207ac2e GH |
136 | if (format == HAL_PIXEL_FORMAT_RGBA_8888) { |
137 | bool sw_usage = !!(usage & (GRALLOC_USAGE_SW_READ_MASK | | |
138 | GRALLOC_USAGE_SW_WRITE_MASK)); | |
139 | ||
140 | if (usage & GRALLOC_USAGE_HW_FB) { | |
141 | ALOGW_IF(sw_usage, | |
142 | "framebuffer target should not have SW usage bits; ignoring"); | |
143 | format = HAL_PIXEL_FORMAT_BGRA_8888; | |
144 | } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { | |
145 | if (sw_usage) | |
146 | return -EINVAL; | |
147 | format = HAL_PIXEL_FORMAT_BGRA_8888; | |
148 | } | |
efe80f0e GH |
149 | } |
150 | ||
2480eccc RSZ |
151 | switch (format) { |
152 | case HAL_PIXEL_FORMAT_RGBA_8888: | |
153 | case HAL_PIXEL_FORMAT_RGBX_8888: | |
154 | case HAL_PIXEL_FORMAT_BGRA_8888: | |
99e0c3cd JH |
155 | case HAL_PIXEL_FORMAT_sRGB_A_8888: |
156 | case HAL_PIXEL_FORMAT_sRGB_X_8888: | |
2480eccc RSZ |
157 | bpp = 4; |
158 | break; | |
159 | case HAL_PIXEL_FORMAT_RGB_888: | |
160 | bpp = 3; | |
161 | break; | |
162 | case HAL_PIXEL_FORMAT_RGB_565: | |
2480eccc RSZ |
163 | case HAL_PIXEL_FORMAT_RAW_SENSOR: |
164 | bpp = 2; | |
165 | break; | |
166 | case HAL_PIXEL_FORMAT_BLOB: | |
d15fb8b7 RSZ |
167 | *stride = w; |
168 | vstride = h; | |
39433170 | 169 | size = w * h; |
2480eccc RSZ |
170 | break; |
171 | default: | |
172 | return -EINVAL; | |
173 | } | |
d15fb8b7 RSZ |
174 | |
175 | if (format != HAL_PIXEL_FORMAT_BLOB) { | |
472449a8 | 176 | bpr = ALIGN(w*bpp, 64); |
d15fb8b7 | 177 | vstride = ALIGN(h, 16); |
4cc86ec1 GH |
178 | if (vstride < h + 2) |
179 | size = bpr * (h + 2); | |
d15fb8b7 RSZ |
180 | else |
181 | size = bpr * vstride; | |
182 | *stride = bpr / bpp; | |
183 | size = ALIGN(size, PAGE_SIZE); | |
184 | } | |
2480eccc | 185 | |
ab73a889 RSZ |
186 | if (usage & GRALLOC_USAGE_PROTECTED) { |
187 | alignment = MB_1; | |
ce73ba18 | 188 | ion_flags |= ION_EXYNOS_FIMD_VIDEO_MASK; |
ab73a889 | 189 | } |
ce73ba18 | 190 | |
ab73a889 | 191 | err = ion_alloc_fd(ionfd, size, alignment, heap_mask, ion_flags, |
da3d8712 RSZ |
192 | &fd); |
193 | *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride, | |
194 | vstride); | |
2480eccc RSZ |
195 | |
196 | return err; | |
197 | } | |
198 | ||
55a3039b RSZ |
199 | static int gralloc_alloc_framework_yuv(int ionfd, int w, int h, int format, |
200 | int usage, unsigned int ion_flags, | |
201 | private_handle_t **hnd, int *stride) | |
202 | { | |
203 | size_t size; | |
204 | int err, fd; | |
88c9cff4 | 205 | unsigned int heap_mask = _select_heap(usage); |
55a3039b RSZ |
206 | |
207 | switch (format) { | |
208 | case HAL_PIXEL_FORMAT_YV12: | |
209 | *stride = ALIGN(w, 16); | |
d881a0ce | 210 | size = (*stride * h) + (ALIGN(*stride / 2, 16) * h); |
55a3039b RSZ |
211 | break; |
212 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: | |
213 | *stride = w; | |
d881a0ce | 214 | size = *stride * h * 3 / 2; |
55a3039b RSZ |
215 | break; |
216 | default: | |
bcf29611 RSZ |
217 | ALOGE("invalid yuv format %d\n", format); |
218 | return -EINVAL; | |
55a3039b RSZ |
219 | } |
220 | ||
88c9cff4 | 221 | err = ion_alloc_fd(ionfd, size, 0, heap_mask, ion_flags, &fd); |
55a3039b RSZ |
222 | if (err) |
223 | return err; | |
224 | ||
225 | *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride, h); | |
226 | return err; | |
227 | } | |
228 | ||
229 | static int gralloc_alloc_yuv(int ionfd, int w, int h, int format, | |
230 | int usage, unsigned int ion_flags, | |
231 | private_handle_t **hnd, int *stride) | |
2480eccc RSZ |
232 | { |
233 | size_t luma_size, chroma_size; | |
234 | int err, planes, fd, fd1, fd2 = 0; | |
70212e56 | 235 | size_t luma_vstride; |
788e20df SK |
236 | unsigned int heap_mask = _select_heap(usage); |
237 | ||
2480eccc RSZ |
238 | *stride = ALIGN(w, 16); |
239 | ||
7d5efeeb SK |
240 | if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
241 | ALOGV("HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED : usage(%x), flags(%x)\n", usage, ion_flags); | |
242 | if ((usage & GRALLOC_USAGE_HW_CAMERA_ZSL) == GRALLOC_USAGE_HW_CAMERA_ZSL) { | |
243 | format = HAL_PIXEL_FORMAT_YCbCr_422_I; // YUYV | |
244 | } else if (usage & GRALLOC_USAGE_HW_TEXTURE) { | |
245 | format = HAL_PIXEL_FORMAT_EXYNOS_YV12; | |
246 | } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { | |
247 | format = HAL_PIXEL_FORMAT_YCbCr_420_SP; // NV12M | |
248 | } | |
249 | } | |
2480eccc | 250 | switch (format) { |
c853be7b | 251 | case HAL_PIXEL_FORMAT_EXYNOS_YV12: |
bcf29611 | 252 | { |
6cc676b4 | 253 | *stride = ALIGN(w, 32); |
bcf29611 RSZ |
254 | luma_vstride = ALIGN(h, 16); |
255 | luma_size = luma_vstride * *stride; | |
256 | chroma_size = (luma_vstride / 2) * ALIGN(*stride / 2, 16); | |
257 | planes = 3; | |
258 | break; | |
259 | } | |
c853be7b | 260 | case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP: |
2480eccc RSZ |
261 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
262 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: | |
bcf29611 RSZ |
263 | { |
264 | size_t chroma_vstride = ALIGN(h / 2, 32); | |
265 | luma_vstride = ALIGN(h, 32); | |
266 | luma_size = luma_vstride * *stride; | |
267 | chroma_size = chroma_vstride * *stride; | |
268 | planes = 2; | |
269 | break; | |
270 | } | |
55a3039b RSZ |
271 | case HAL_PIXEL_FORMAT_YV12: |
272 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: | |
273 | return gralloc_alloc_framework_yuv(ionfd, w, h, format, usage, | |
274 | ion_flags, hnd, stride); | |
7d5efeeb SK |
275 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
276 | { | |
277 | luma_vstride = h; | |
278 | luma_size = luma_vstride * *stride * 2; | |
279 | chroma_size = 0; | |
280 | planes = 1; | |
281 | break; | |
282 | } | |
2480eccc | 283 | default: |
bcf29611 RSZ |
284 | ALOGE("invalid yuv format %d\n", format); |
285 | return -EINVAL; | |
2480eccc RSZ |
286 | } |
287 | ||
ce73ba18 JK |
288 | if (usage & GRALLOC_USAGE_PROTECTED) |
289 | ion_flags |= ION_EXYNOS_MFC_OUTPUT_MASK; | |
290 | ||
788e20df | 291 | err = ion_alloc_fd(ionfd, luma_size, 0, heap_mask, ion_flags, &fd); |
2480eccc RSZ |
292 | if (err) |
293 | return err; | |
7d5efeeb SK |
294 | if (planes == 1) { |
295 | *hnd = new private_handle_t(fd, luma_size, usage, w, h, | |
ec68ab21 RSZ |
296 | format, *stride, luma_vstride); |
297 | } else { | |
7d5efeeb SK |
298 | err = ion_alloc_fd(ionfd, chroma_size, 0, heap_mask, ion_flags, &fd1); |
299 | if (err) | |
300 | goto err1; | |
301 | if (planes == 3) { | |
302 | err = ion_alloc_fd(ionfd, chroma_size, 0, heap_mask, ion_flags, &fd2); | |
303 | if (err) | |
304 | goto err2; | |
305 | ||
306 | *hnd = new private_handle_t(fd, fd1, fd2, luma_size, usage, w, h, | |
307 | format, *stride, luma_vstride); | |
308 | } else { | |
309 | *hnd = new private_handle_t(fd, fd1, luma_size, usage, w, h, format, | |
310 | *stride, luma_vstride); | |
311 | } | |
ec68ab21 | 312 | } |
06f1fa77 AR |
313 | // Set chroma & gamut fields |
314 | if (!err && *hnd) { | |
315 | if (usage & GRALLOC_USAGE_PRIVATE_CHROMA) { | |
316 | (*hnd)->chroma = HAL_PIXEL_CHROMA_BT601_8; | |
317 | (*hnd)->gamut = HAL_PIXEL_GAMUT_NARROW_8; | |
318 | } else { | |
319 | (*hnd)->chroma = HAL_PIXEL_CHROMA_BT709_8; | |
320 | (*hnd)->gamut = HAL_PIXEL_GAMUT_WIDE_8; | |
321 | } | |
322 | } | |
2480eccc RSZ |
323 | return err; |
324 | ||
325 | err2: | |
326 | close(fd1); | |
327 | err1: | |
328 | close(fd); | |
329 | return err; | |
330 | } | |
331 | ||
332 | static int gralloc_alloc(alloc_device_t* dev, | |
333 | int w, int h, int format, int usage, | |
334 | buffer_handle_t* pHandle, int* pStride) | |
335 | { | |
336 | int stride; | |
337 | int err; | |
da3d8712 | 338 | unsigned int ion_flags = 0; |
a776338e | 339 | private_handle_t *hnd = NULL; |
2480eccc RSZ |
340 | |
341 | if (!pHandle || !pStride) | |
342 | return -EINVAL; | |
343 | ||
344 | if( (usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN ) | |
f8552390 | 345 | ion_flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC; |
2480eccc RSZ |
346 | |
347 | private_module_t* m = reinterpret_cast<private_module_t*> | |
348 | (dev->common.module); | |
349 | gralloc_module_t* module = reinterpret_cast<gralloc_module_t*> | |
350 | (dev->common.module); | |
351 | ||
da3d8712 RSZ |
352 | err = gralloc_alloc_rgb(m->ionfd, w, h, format, usage, ion_flags, &hnd, |
353 | &stride); | |
2480eccc | 354 | if (err) |
da3d8712 RSZ |
355 | err = gralloc_alloc_yuv(m->ionfd, w, h, format, usage, ion_flags, |
356 | &hnd, &stride); | |
2480eccc | 357 | if (err) |
8a4849e1 | 358 | goto err; |
2480eccc | 359 | |
8a4849e1 CC |
360 | err = gralloc_register_buffer(module, hnd); |
361 | if (err) | |
2480eccc RSZ |
362 | goto err; |
363 | ||
364 | *pHandle = hnd; | |
365 | *pStride = stride; | |
366 | return 0; | |
367 | err: | |
a776338e RSZ |
368 | if (!hnd) |
369 | return err; | |
2480eccc | 370 | close(hnd->fd); |
ec68ab21 | 371 | if (hnd->fd1 >= 0) |
2480eccc | 372 | close(hnd->fd1); |
ec68ab21 | 373 | if (hnd->fd2 >= 0) |
2480eccc RSZ |
374 | close(hnd->fd2); |
375 | return err; | |
376 | } | |
377 | ||
378 | static int gralloc_free(alloc_device_t* dev, | |
379 | buffer_handle_t handle) | |
380 | { | |
381 | if (private_handle_t::validate(handle) < 0) | |
382 | return -EINVAL; | |
383 | ||
384 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); | |
385 | gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>( | |
386 | dev->common.module); | |
8a4849e1 CC |
387 | |
388 | gralloc_unregister_buffer(module, hnd); | |
2480eccc | 389 | |
2480eccc | 390 | close(hnd->fd); |
ec68ab21 | 391 | if (hnd->fd1 >= 0) |
2480eccc | 392 | close(hnd->fd1); |
ec68ab21 | 393 | if (hnd->fd2 >= 0) |
2480eccc RSZ |
394 | close(hnd->fd2); |
395 | ||
396 | delete hnd; | |
397 | return 0; | |
398 | } | |
399 | ||
400 | /*****************************************************************************/ | |
401 | ||
402 | static int gralloc_close(struct hw_device_t *dev) | |
403 | { | |
404 | gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev); | |
405 | if (ctx) { | |
4d9f47b6 GH |
406 | private_module_t *p = reinterpret_cast<private_module_t*>(ctx->device.common.module); |
407 | pthread_mutex_lock(&p->lock); | |
408 | LOG_ALWAYS_FATAL_IF(!p->refcount); | |
409 | p->refcount--; | |
410 | if (!p->refcount) | |
411 | close(p->ionfd); | |
412 | pthread_mutex_unlock(&p->lock); | |
413 | ||
2480eccc RSZ |
414 | /* TODO: keep a list of all buffer_handle_t created, and free them |
415 | * all here. | |
416 | */ | |
417 | free(ctx); | |
418 | } | |
419 | return 0; | |
420 | } | |
421 | ||
422 | int gralloc_device_open(const hw_module_t* module, const char* name, | |
423 | hw_device_t** device) | |
424 | { | |
425 | int status = -EINVAL; | |
426 | if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { | |
427 | gralloc_context_t *dev; | |
428 | dev = (gralloc_context_t*)malloc(sizeof(*dev)); | |
429 | ||
430 | /* initialize our state here */ | |
431 | memset(dev, 0, sizeof(*dev)); | |
432 | ||
433 | /* initialize the procs */ | |
434 | dev->device.common.tag = HARDWARE_DEVICE_TAG; | |
435 | dev->device.common.version = 0; | |
436 | dev->device.common.module = const_cast<hw_module_t*>(module); | |
437 | dev->device.common.close = gralloc_close; | |
438 | ||
439 | dev->device.alloc = gralloc_alloc; | |
440 | dev->device.free = gralloc_free; | |
441 | ||
442 | private_module_t *p = reinterpret_cast<private_module_t*>(dev->device.common.module); | |
4d9f47b6 GH |
443 | pthread_mutex_lock(&p->lock); |
444 | if (!p->refcount) | |
445 | p->ionfd = ion_open(); | |
446 | p->refcount++; | |
447 | pthread_mutex_unlock(&p->lock); | |
2480eccc RSZ |
448 | |
449 | *device = &dev->device.common; | |
450 | status = 0; | |
451 | } else { | |
452 | status = fb_device_open(module, name, device); | |
453 | } | |
454 | return status; | |
455 | } |