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