blob: b5f269bf242be4d31fa40cd1bb914956cb801aac [file] [log] [blame]
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -07001/*
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
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -070017#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"
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -070040
Rebecca Schultz Zavin88c9cff2013-01-09 15:37:04 -080041#define ION_HEAP_EXYNOS_CONTIG_MASK (1 << 4)
Jihyun Kimce73ba12012-09-12 16:51:53 +090042#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)
Rebecca Schultz Zavin88c9cff2013-01-09 15:37:04 -080045#define ION_HEAP_SYSTEM_ID 0
46#define ION_HEAP_EXYNOS_CONTIG_ID 4
47#define ION_HEAP_CHUNK_ID 6
Rebecca Schultz Zavinab73a882012-10-06 09:20:31 -070048#define MB_1 (1024*1024)
Jihyun Kimce73ba12012-09-12 16:51:53 +090049
Sanghee Kim788e20d2012-08-08 22:59:55 -070050
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -070051/*****************************************************************************/
52
53struct gralloc_context_t {
54 alloc_device_t device;
55 /* our private data here */
56};
57
58static int gralloc_alloc_buffer(alloc_device_t* dev,
59 size_t size, int usage, buffer_handle_t* pHandle);
60
61/*****************************************************************************/
62
63int fb_device_open(const hw_module_t* module, const char* name,
64 hw_device_t** device);
65
66static int gralloc_device_open(const hw_module_t* module, const char* name,
67 hw_device_t** device);
68
69extern 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
74extern int gralloc_unlock(gralloc_module_t const* module,
75 buffer_handle_t handle);
76
77extern int gralloc_register_buffer(gralloc_module_t const* module,
78 buffer_handle_t handle);
79
80extern int gralloc_unregister_buffer(gralloc_module_t const* module,
81 buffer_handle_t handle);
82
83/*****************************************************************************/
84
85static struct hw_module_methods_t gralloc_module_methods = {
86open: gralloc_device_open
87};
88
89struct private_module_t HAL_MODULE_INFO_SYM = {
90base: {
Rebecca Schultz Zavina7763382012-09-11 11:15:48 -070091 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 },
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700100 registerBuffer: gralloc_register_buffer,
101 unregisterBuffer: gralloc_unregister_buffer,
102 lock: gralloc_lock,
103 unlock: gralloc_unlock,
Rebecca Schultz Zavina7763382012-09-11 11:15:48 -0700104},
105framebuffer: 0,
106flags: 0,
107numBuffers: 0,
108bufferMask: 0,
109lock: PTHREAD_MUTEX_INITIALIZER,
Greg Hackmann4d9f47b2014-04-02 11:52:10 -0700110refcount: 0,
Rebecca Schultz Zavina7763382012-09-11 11:15:48 -0700111currentBuffer: 0,
112ionfd: -1,
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700113};
114
115/*****************************************************************************/
116
Sanghee Kim788e20d2012-08-08 22:59:55 -0700117static unsigned int _select_heap(int usage)
118{
119 unsigned int heap_mask;
120
121 if (usage & GRALLOC_USAGE_PROTECTED)
Rebecca Schultz Zavin88c9cff2013-01-09 15:37:04 -0800122 heap_mask = (1 << ION_HEAP_EXYNOS_CONTIG_ID);
Sanghee Kim788e20d2012-08-08 22:59:55 -0700123 else
Rebecca Schultz Zavin88c9cff2013-01-09 15:37:04 -0800124 heap_mask = (1 << ION_HEAP_SYSTEM_ID) | (1 << ION_HEAP_CHUNK_ID);
Sanghee Kim788e20d2012-08-08 22:59:55 -0700125
126 return heap_mask;
127}
128
Rebecca Schultz Zavinda3d8712012-08-15 17:03:47 -0700129static 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)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700131{
Rebecca Schultz Zavinab73a882012-10-06 09:20:31 -0700132 size_t size, bpr, alignment = 0;
Rebecca Schultz Zavin70212e52012-08-15 11:17:04 -0700133 int bpp = 0, vstride, fd, err;
Sanghee Kim788e20d2012-08-08 22:59:55 -0700134 unsigned int heap_mask = _select_heap(usage);
135
Greg Hackmann2207ac22013-08-29 10:24:35 -0700136 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 }
Greg Hackmannefe80f02013-08-27 17:14:27 -0700149 }
150
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700151 switch (format) {
152 case HAL_PIXEL_FORMAT_RGBA_8888:
153 case HAL_PIXEL_FORMAT_RGBX_8888:
154 case HAL_PIXEL_FORMAT_BGRA_8888:
Jesse Hall99e0c3c2014-08-17 21:41:25 -0700155 case HAL_PIXEL_FORMAT_sRGB_A_8888:
156 case HAL_PIXEL_FORMAT_sRGB_X_8888:
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700157 bpp = 4;
158 break;
159 case HAL_PIXEL_FORMAT_RGB_888:
160 bpp = 3;
161 break;
162 case HAL_PIXEL_FORMAT_RGB_565:
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700163 case HAL_PIXEL_FORMAT_RAW_SENSOR:
164 bpp = 2;
165 break;
166 case HAL_PIXEL_FORMAT_BLOB:
Rebecca Schultz Zavind15fb8b2012-09-25 16:49:28 -0700167 *stride = w;
168 vstride = h;
Rebecca Schultz Zavin39433172012-09-27 16:45:04 -0700169 size = w * h;
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700170 break;
171 default:
172 return -EINVAL;
173 }
Rebecca Schultz Zavind15fb8b2012-09-25 16:49:28 -0700174
175 if (format != HAL_PIXEL_FORMAT_BLOB) {
Bartosz Szatkowski472449a2013-02-19 12:49:30 +0000176 bpr = ALIGN(w*bpp, 64);
Rebecca Schultz Zavind15fb8b2012-09-25 16:49:28 -0700177 vstride = ALIGN(h, 16);
Greg Hackmann4cc86ec2012-10-08 10:14:53 -0700178 if (vstride < h + 2)
179 size = bpr * (h + 2);
Rebecca Schultz Zavind15fb8b2012-09-25 16:49:28 -0700180 else
181 size = bpr * vstride;
182 *stride = bpr / bpp;
183 size = ALIGN(size, PAGE_SIZE);
184 }
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700185
Rebecca Schultz Zavinab73a882012-10-06 09:20:31 -0700186 if (usage & GRALLOC_USAGE_PROTECTED) {
187 alignment = MB_1;
Jihyun Kimce73ba12012-09-12 16:51:53 +0900188 ion_flags |= ION_EXYNOS_FIMD_VIDEO_MASK;
Rebecca Schultz Zavinab73a882012-10-06 09:20:31 -0700189 }
Jihyun Kimce73ba12012-09-12 16:51:53 +0900190
Rebecca Schultz Zavinab73a882012-10-06 09:20:31 -0700191 err = ion_alloc_fd(ionfd, size, alignment, heap_mask, ion_flags,
Rebecca Schultz Zavinda3d8712012-08-15 17:03:47 -0700192 &fd);
193 *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride,
194 vstride);
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700195
196 return err;
197}
198
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700199static 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;
Rebecca Schultz Zavin88c9cff2013-01-09 15:37:04 -0800205 unsigned int heap_mask = _select_heap(usage);
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700206
207 switch (format) {
208 case HAL_PIXEL_FORMAT_YV12:
209 *stride = ALIGN(w, 16);
Rebecca Schultz Zavind881a0c2012-10-31 17:05:48 -0700210 size = (*stride * h) + (ALIGN(*stride / 2, 16) * h);
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700211 break;
212 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
213 *stride = w;
Rebecca Schultz Zavind881a0c2012-10-31 17:05:48 -0700214 size = *stride * h * 3 / 2;
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700215 break;
216 default:
Rebecca Schultz Zavinbcf29612012-08-27 21:31:22 -0700217 ALOGE("invalid yuv format %d\n", format);
218 return -EINVAL;
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700219 }
220
Rebecca Schultz Zavin88c9cff2013-01-09 15:37:04 -0800221 err = ion_alloc_fd(ionfd, size, 0, heap_mask, ion_flags, &fd);
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700222 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
229static 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)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700232{
233 size_t luma_size, chroma_size;
234 int err, planes, fd, fd1, fd2 = 0;
Rebecca Schultz Zavin70212e52012-08-15 11:17:04 -0700235 size_t luma_vstride;
Sanghee Kim788e20d2012-08-08 22:59:55 -0700236 unsigned int heap_mask = _select_heap(usage);
237
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700238 *stride = ALIGN(w, 16);
239
Sungjoong Kang7d5efee2012-09-06 17:11:16 -0700240 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 }
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700250 switch (format) {
Rebecca Schultz Zavinc853be72012-08-23 00:03:05 -0700251 case HAL_PIXEL_FORMAT_EXYNOS_YV12:
Rebecca Schultz Zavinbcf29612012-08-27 21:31:22 -0700252 {
Seho Kim6cc676b2012-09-30 13:35:01 -0700253 *stride = ALIGN(w, 32);
Rebecca Schultz Zavinbcf29612012-08-27 21:31:22 -0700254 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 }
Rebecca Schultz Zavinc853be72012-08-23 00:03:05 -0700260 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP:
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700261 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
262 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Rebecca Schultz Zavinbcf29612012-08-27 21:31:22 -0700263 {
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 }
Rebecca Schultz Zavin55a30392012-08-27 15:23:32 -0700271 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);
Sungjoong Kang7d5efee2012-09-06 17:11:16 -0700275 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 }
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700283 default:
Rebecca Schultz Zavinbcf29612012-08-27 21:31:22 -0700284 ALOGE("invalid yuv format %d\n", format);
285 return -EINVAL;
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700286 }
287
Jihyun Kimce73ba12012-09-12 16:51:53 +0900288 if (usage & GRALLOC_USAGE_PROTECTED)
289 ion_flags |= ION_EXYNOS_MFC_OUTPUT_MASK;
290
Sanghee Kim788e20d2012-08-08 22:59:55 -0700291 err = ion_alloc_fd(ionfd, luma_size, 0, heap_mask, ion_flags, &fd);
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700292 if (err)
293 return err;
Sungjoong Kang7d5efee2012-09-06 17:11:16 -0700294 if (planes == 1) {
295 *hnd = new private_handle_t(fd, luma_size, usage, w, h,
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700296 format, *stride, luma_vstride);
297 } else {
Sungjoong Kang7d5efee2012-09-06 17:11:16 -0700298 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 }
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700312 }
Alex Ray06f1fa72013-04-15 11:24:13 -0700313 // 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 }
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700323 return err;
324
325err2:
326 close(fd1);
327err1:
328 close(fd);
329 return err;
330}
331
332static 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;
Rebecca Schultz Zavinda3d8712012-08-15 17:03:47 -0700338 unsigned int ion_flags = 0;
Rebecca Schultz Zavina7763382012-09-11 11:15:48 -0700339 private_handle_t *hnd = NULL;
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700340
341 if (!pHandle || !pStride)
342 return -EINVAL;
343
344 if( (usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN )
Rebecca Schultz Zavinf8552392012-09-20 00:21:01 -0700345 ion_flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700346
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
Rebecca Schultz Zavinda3d8712012-08-15 17:03:47 -0700352 err = gralloc_alloc_rgb(m->ionfd, w, h, format, usage, ion_flags, &hnd,
353 &stride);
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700354 if (err)
Rebecca Schultz Zavinda3d8712012-08-15 17:03:47 -0700355 err = gralloc_alloc_yuv(m->ionfd, w, h, format, usage, ion_flags,
356 &hnd, &stride);
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700357 if (err)
Colin Cross8a4849e2013-08-22 19:44:28 -0700358 goto err;
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700359
Colin Cross8a4849e2013-08-22 19:44:28 -0700360 err = gralloc_register_buffer(module, hnd);
361 if (err)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700362 goto err;
363
364 *pHandle = hnd;
365 *pStride = stride;
366 return 0;
367err:
Rebecca Schultz Zavina7763382012-09-11 11:15:48 -0700368 if (!hnd)
369 return err;
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700370 close(hnd->fd);
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700371 if (hnd->fd1 >= 0)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700372 close(hnd->fd1);
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700373 if (hnd->fd2 >= 0)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700374 close(hnd->fd2);
375 return err;
376}
377
378static 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);
Colin Cross8a4849e2013-08-22 19:44:28 -0700387
388 gralloc_unregister_buffer(module, hnd);
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700389
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700390 close(hnd->fd);
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700391 if (hnd->fd1 >= 0)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700392 close(hnd->fd1);
Rebecca Schultz Zavinec68ab22012-08-27 10:58:52 -0700393 if (hnd->fd2 >= 0)
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700394 close(hnd->fd2);
395
396 delete hnd;
397 return 0;
398}
399
400/*****************************************************************************/
401
402static int gralloc_close(struct hw_device_t *dev)
403{
404 gralloc_context_t* ctx = reinterpret_cast<gralloc_context_t*>(dev);
405 if (ctx) {
Greg Hackmann4d9f47b2014-04-02 11:52:10 -0700406 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
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700414 /* 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
422int 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);
Greg Hackmann4d9f47b2014-04-02 11:52:10 -0700443 pthread_mutex_lock(&p->lock);
444 if (!p->refcount)
445 p->ionfd = ion_open();
446 p->refcount++;
447 pthread_mutex_unlock(&p->lock);
Rebecca Schultz Zavin2480ecc2012-08-14 16:08:04 -0700448
449 *device = &dev->device.common;
450 status = 0;
451 } else {
452 status = fb_device_open(module, name, device);
453 }
454 return status;
455}