1 /*
2  * Copyright (C) 2010-2019 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <string.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <sys/ioctl.h>
24 #include <linux/fb.h>
25 
26 #include <system/window.h>
27 #include <log/log.h>
28 #include <cutils/atomic.h>
29 #include <hardware/hardware.h>
30 #include <hardware/fb.h>
31 
32 #if GRALLOC_VERSION_MAJOR == 1
33 #include <hardware/gralloc1.h>
34 #include <GLES/gl.h>
35 #elif GRALLOC_VERSION_MAJOR == 0
36 #include <hardware/gralloc.h>
37 #include <GLES/gl.h>
38 #endif
39 
40 #include "mali_gralloc_module.h"
41 #include "mali_gralloc_private_interface_types.h"
42 #include "mali_gralloc_buffer.h"
43 #include "gralloc_helper.h"
44 #include "gralloc_vsync.h"
45 #include "mali_gralloc_bufferaccess.h"
46 #include "mali_gralloc_ion.h"
47 #include "gralloc_buffer_priv.h"
48 
49 /* Number of buffers for page flipping */
50 #define NUM_BUFFERS NUM_FB_BUFFERS
51 
52 enum
53 {
54 	PAGE_FLIP = 0x00000001,
55 };
56 
57 #if GRALLOC_VERSION_MAJOR <= 1
fb_set_swap_interval(struct framebuffer_device_t * dev,int interval)58 static int fb_set_swap_interval(struct framebuffer_device_t *dev, int interval)
59 {
60 	if (interval < dev->minSwapInterval)
61 	{
62 		interval = dev->minSwapInterval;
63 	}
64 	else if (interval > dev->maxSwapInterval)
65 	{
66 		interval = dev->maxSwapInterval;
67 	}
68 
69 	private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
70 	m->swapInterval = interval;
71 
72 	if (0 == interval)
73 	{
74 		gralloc_vsync_disable(dev);
75 	}
76 	else
77 	{
78 		gralloc_vsync_enable(dev);
79 	}
80 
81 	return 0;
82 }
83 #endif
84 
85 #if GRALLOC_VERSION_MAJOR <= 1
fb_post(struct framebuffer_device_t * dev,buffer_handle_t buffer)86 static int fb_post(struct framebuffer_device_t *dev, buffer_handle_t buffer)
87 {
88 	if (private_handle_t::validate(buffer) < 0)
89 	{
90 		return -EINVAL;
91 	}
92 
93 	private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(buffer);
94 	private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
95 
96 	if (m->currentBuffer)
97 	{
98 		mali_gralloc_unlock(m, m->currentBuffer);
99 		m->currentBuffer = 0;
100 	}
101 
102 	if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
103 	{
104 		/* Buffer is being locked for non-cpu usage, hence no need to populate a valid cpu address parameter */
105 		mali_gralloc_lock(m, buffer, private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
106 		                             0, 0, 0, 0, NULL);
107 		int interrupt;
108 		m->info.activate = FB_ACTIVATE_VBL;
109 		m->info.yoffset = hnd->offset / m->finfo.line_length;
110 
111 		if (ioctl(m->framebuffer->fb_fd, FBIOPUT_VSCREENINFO, &m->info) == -1)
112 		{
113 			AERR("FBIOPUT_VSCREENINFO failed for fd: %d", m->framebuffer->fb_fd);
114 			mali_gralloc_unlock(m, buffer);
115 			return -errno;
116 		}
117 
118 		if (0 != gralloc_wait_for_vsync(dev))
119 		{
120 			AERR("Gralloc wait for vsync failed for fd: %d", m->framebuffer->fb_fd);
121 			mali_gralloc_unlock(m, buffer);
122 			return -errno;
123 		}
124 
125 		m->currentBuffer = buffer;
126 	}
127 	else
128 	{
129 		void *fb_vaddr;
130 		void *buffer_vaddr;
131 
132 		mali_gralloc_lock(m, m->framebuffer, GRALLOC_USAGE_SW_WRITE_RARELY, 0, 0, 0, 0, &fb_vaddr);
133 		mali_gralloc_lock(m, buffer, GRALLOC_USAGE_SW_READ_RARELY, 0, 0, 0, 0, &buffer_vaddr);
134 		// If buffer's alignment match framebuffer alignment we can do a direct copy.
135 		// If not we must fallback to do an aligned copy of each line.
136 		if (hnd->plane_info[0].byte_stride == (int)m->finfo.line_length)
137 		{
138 			memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
139 		}
140 		else
141 		{
142 			uintptr_t fb_offset = 0;
143 			uintptr_t buffer_offset = 0;
144 			unsigned int i;
145 
146 			for (i = 0; i < m->info.yres; i++)
147 			{
148 				memcpy((void *)((uintptr_t)fb_vaddr + fb_offset), (void *)((uintptr_t)buffer_vaddr + buffer_offset),
149 				       m->finfo.line_length);
150 
151 				fb_offset += m->finfo.line_length;
152 				buffer_offset += hnd->plane_info[0].byte_stride;
153 			}
154 		}
155 
156 		mali_gralloc_unlock(m, buffer);
157 		mali_gralloc_unlock(m, m->framebuffer);
158 	}
159 
160 	return 0;
161 }
162 #endif
163 
init_frame_buffer_locked(struct private_module_t * module)164 static int init_frame_buffer_locked(struct private_module_t *module)
165 {
166 	if (module->framebuffer)
167 	{
168 		return 0; // Nothing to do, already initialized
169 	}
170 
171 	char const *const device_template[] = { "/dev/graphics/fb%u", "/dev/fb%u", NULL };
172 
173 	int fd = -1;
174 	int i = 0;
175 	char name[64];
176 
177 	while ((fd == -1) && device_template[i])
178 	{
179 		snprintf(name, 64, device_template[i], 0);
180 		fd = open(name, O_RDWR, 0);
181 		i++;
182 	}
183 
184 	if (fd < 0)
185 	{
186 		return -errno;
187 	}
188 
189 	struct fb_fix_screeninfo finfo;
190 
191 	if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
192 	{
193 		close(fd);
194 		return -errno;
195 	}
196 
197 	struct fb_var_screeninfo info;
198 
199 	if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
200 	{
201 		close(fd);
202 		return -errno;
203 	}
204 
205 	info.reserved[0] = 0;
206 	info.reserved[1] = 0;
207 	info.reserved[2] = 0;
208 	info.xoffset = 0;
209 	info.yoffset = 0;
210 	info.activate = FB_ACTIVATE_NOW;
211 
212 #if GRALLOC_FB_BPP == 16
213 	/*
214 	 * Explicitly request 5/6/5
215 	 */
216 	info.bits_per_pixel = 16;
217 	info.red.offset = 11;
218 	info.red.length = 5;
219 	info.green.offset = 5;
220 	info.green.length = 6;
221 	info.blue.offset = 0;
222 	info.blue.length = 5;
223 	info.transp.offset = 0;
224 	info.transp.length = 0;
225 #elif GRALLOC_FB_BPP == 32
226 	/*
227 	 * Explicitly request 8/8/8
228 	 */
229 	info.bits_per_pixel = 32;
230 	info.red.offset = 16;
231 	info.red.length = 8;
232 	info.green.offset = 8;
233 	info.green.length = 8;
234 	info.blue.offset = 0;
235 	info.blue.length = 8;
236 	info.transp.offset = 0;
237 	info.transp.length = 0;
238 #else
239 #error "Invalid framebuffer bit depth"
240 #endif
241 
242 	/*
243 	 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
244 	 */
245 	info.yres_virtual = info.yres * NUM_BUFFERS;
246 
247 	uint32_t flags = PAGE_FLIP;
248 
249 	if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1)
250 	{
251 		info.yres_virtual = info.yres;
252 		flags &= ~PAGE_FLIP;
253 		AWAR("FBIOPUT_VSCREENINFO failed, page flipping not supported fd: %d", fd);
254 	}
255 
256 	if (info.yres_virtual < info.yres * 2)
257 	{
258 		// we need at least 2 for page-flipping
259 		info.yres_virtual = info.yres;
260 		flags &= ~PAGE_FLIP;
261 		AWAR("page flipping not supported (yres_virtual=%d, requested=%d)", info.yres_virtual, info.yres * 2);
262 	}
263 
264 	if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
265 	{
266 		close(fd);
267 		return -errno;
268 	}
269 
270 	int refreshRate = 0;
271 
272 	if (info.pixclock > 0)
273 	{
274 		refreshRate =
275 		    1000000000000000LLU / (uint64_t(info.upper_margin + info.lower_margin + info.yres + info.hsync_len) *
276 		                           (info.left_margin + info.right_margin + info.xres + info.vsync_len) * info.pixclock);
277 	}
278 	else
279 	{
280 		AWAR("fbdev pixclock is zero for fd: %d", fd);
281 	}
282 
283 	if (refreshRate == 0)
284 	{
285 		refreshRate = 60 * 1000; // 60 Hz
286 	}
287 
288 	if (int(info.width) <= 0 || int(info.height) <= 0)
289 	{
290 		// the driver doesn't return that information
291 		// default to 160 dpi
292 		info.width = ((info.xres * 25.4f) / 160.0f + 0.5f);
293 		info.height = ((info.yres * 25.4f) / 160.0f + 0.5f);
294 	}
295 
296 	float xdpi = (info.xres * 25.4f) / info.width;
297 	float ydpi = (info.yres * 25.4f) / info.height;
298 	float fps = refreshRate / 1000.0f;
299 
300 	AINF("using (fd=%d)\n"
301 	     "id           = %s\n"
302 	     "xres         = %d px\n"
303 	     "yres         = %d px\n"
304 	     "xres_virtual = %d px\n"
305 	     "yres_virtual = %d px\n"
306 	     "bpp          = %d\n"
307 	     "r            = %2u:%u\n"
308 	     "g            = %2u:%u\n"
309 	     "b            = %2u:%u\n",
310 	     fd, finfo.id, info.xres, info.yres, info.xres_virtual, info.yres_virtual, info.bits_per_pixel, info.red.offset,
311 	     info.red.length, info.green.offset, info.green.length, info.blue.offset, info.blue.length);
312 
313 	AINF("width        = %d mm (%f dpi)\n"
314 	     "height       = %d mm (%f dpi)\n"
315 	     "refresh rate = %.2f Hz\n",
316 	     info.width, xdpi, info.height, ydpi, fps);
317 
318 	if (0 == strncmp(finfo.id, "CLCD FB", 7))
319 	{
320 		module->dpy_type = MALI_DPY_TYPE_CLCD;
321 	}
322 	else if (0 == strncmp(finfo.id, "ARM Mali HDLCD", 14))
323 	{
324 		module->dpy_type = MALI_DPY_TYPE_HDLCD;
325 	}
326 	else if (0 == strncmp(finfo.id, "ARM HDLCD Control", 16))
327 	{
328 		module->dpy_type = MALI_DPY_TYPE_HDLCD;
329 	}
330 	else
331 	{
332 		module->dpy_type = MALI_DPY_TYPE_UNKNOWN;
333 	}
334 
335 	if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
336 	{
337 		close(fd);
338 		return -errno;
339 	}
340 
341 	if (finfo.smem_len <= 0)
342 	{
343 		close(fd);
344 		return -errno;
345 	}
346 
347 	module->flags = flags;
348 	module->info = info;
349 	module->finfo = finfo;
350 	module->xdpi = xdpi;
351 	module->ydpi = ydpi;
352 	module->fps = fps;
353 	module->swapInterval = 1;
354 
355 	/*
356 	 * map the framebuffer
357 	 */
358 	size_t fbSize = round_up_to_page_size(finfo.line_length * info.yres_virtual);
359 	void *vaddr = mmap(0, fbSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
360 
361 	if (vaddr == MAP_FAILED)
362 	{
363 		close(fd);
364 		AERR("Error mapping the framebuffer (%s)", strerror(errno));
365 		return -errno;
366 	}
367 
368 	memset(vaddr, 0, fbSize);
369 	// Create a "fake" buffer object for the entire frame buffer memory, and store it in the module
370 	module->framebuffer = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, fbSize, vaddr,
371 	                                           static_cast<uint64_t>(GRALLOC_USAGE_HW_FB),
372 	                                           static_cast<uint64_t>(GRALLOC_USAGE_HW_FB), dup(fd), 0,
373 	                                           finfo.line_length, info.xres_virtual, info.yres_virtual,
374 	                                           module->fbdev_format);
375 
376 	module->numBuffers = info.yres_virtual / info.yres;
377 	module->bufferMask = 0;
378 
379 	return 0;
380 }
381 
init_frame_buffer(struct private_module_t * module)382 static int init_frame_buffer(struct private_module_t *module)
383 {
384 	pthread_mutex_lock(&module->lock);
385 	int err = init_frame_buffer_locked(module);
386 	pthread_mutex_unlock(&module->lock);
387 	return err;
388 }
389 
390 #if GRALLOC_VERSION_MAJOR <= 1
fb_close(struct hw_device_t * device)391 static int fb_close(struct hw_device_t *device)
392 {
393 	framebuffer_device_t *dev = reinterpret_cast<framebuffer_device_t *>(device);
394 
395 	if (dev)
396 	{
397 		free(dev);
398 	}
399 
400 	return 0;
401 }
402 #endif
403 
fb_alloc_framebuffer_dmabuf(private_module_t * m,private_handle_t * hnd)404 static int fb_alloc_framebuffer_dmabuf(private_module_t *m, private_handle_t *hnd)
405 {
406 	struct fb_dmabuf_export fb_dma_buf;
407 	int res;
408 	res = ioctl(m->framebuffer->fb_fd, FBIOGET_DMABUF, &fb_dma_buf);
409 
410 	if (res == 0)
411 	{
412 		hnd->share_fd = fb_dma_buf.fd;
413 		return 0;
414 	}
415 	else
416 	{
417 		AINF("FBIOGET_DMABUF ioctl failed(%d). See gralloc_priv.h and the integration manual for vendor framebuffer "
418 		     "integration",
419 		     res);
420 		return -1;
421 	}
422 }
423 
fb_alloc_from_ion_module(mali_gralloc_module * m,int width,int height,int byte_stride,size_t buffer_size,uint64_t consumer_usage,uint64_t producer_usage,buffer_handle_t * pHandle)424 static int fb_alloc_from_ion_module(mali_gralloc_module *m, int width, int height, int byte_stride, size_t buffer_size,
425                                     uint64_t consumer_usage, uint64_t producer_usage, buffer_handle_t *pHandle)
426 {
427 	buffer_descriptor_t fb_buffer_descriptor;
428 	gralloc_buffer_descriptor_t gralloc_buffer_descriptor[1];
429 	bool shared = false;
430 	int err = 0;
431 
432 	fb_buffer_descriptor.width = width;
433 	fb_buffer_descriptor.height = height;
434 	fb_buffer_descriptor.size = buffer_size;
435 	fb_buffer_descriptor.pixel_stride = width;
436 
437 	memset(fb_buffer_descriptor.plane_info, 0, sizeof(fb_buffer_descriptor.plane_info));
438 	fb_buffer_descriptor.plane_info[0].alloc_width = width;
439 	fb_buffer_descriptor.plane_info[0].alloc_height = height;
440 	fb_buffer_descriptor.plane_info[0].byte_stride = byte_stride;
441 	fb_buffer_descriptor.plane_info[0].offset = 0;
442 
443 	fb_buffer_descriptor.internal_format = m->fbdev_format;
444 	fb_buffer_descriptor.alloc_format = m->fbdev_format;
445 	fb_buffer_descriptor.consumer_usage = consumer_usage;
446 	fb_buffer_descriptor.producer_usage = producer_usage;
447 	fb_buffer_descriptor.layer_count = 1;
448 
449 	gralloc_buffer_descriptor[0] = (gralloc_buffer_descriptor_t)(&fb_buffer_descriptor);
450 
451 	err = mali_gralloc_ion_allocate(gralloc_buffer_descriptor, 1, pHandle, &shared);
452 
453 	return err;
454 }
455 
fb_alloc_framebuffer_locked(mali_gralloc_module * m,uint64_t consumer_usage,uint64_t producer_usage,buffer_handle_t * pHandle,int * stride,int * byte_stride)456 static int fb_alloc_framebuffer_locked(mali_gralloc_module *m, uint64_t consumer_usage, uint64_t producer_usage,
457                                        buffer_handle_t *pHandle, int *stride, int *byte_stride)
458 {
459 	// allocate the framebuffer
460 	if (m->framebuffer == NULL)
461 	{
462 		// initialize the framebuffer, the framebuffer is mapped once and forever.
463 		int err = init_frame_buffer_locked(m);
464 
465 		if (err < 0)
466 		{
467 			return err;
468 		}
469 
470 		if (m->framebuffer == NULL)
471 		{
472 			return -1;
473 		}
474 	}
475 
476 	uint32_t bufferMask = m->bufferMask;
477 	const uint32_t numBuffers = m->numBuffers;
478 	/* framebufferSize is used for allocating the handle to the framebuffer and refers
479 	 *                 to the size of the actual framebuffer.
480 	 * alignedFramebufferSize is used for allocating a possible internal buffer and
481 	 *                        thus need to consider internal alignment requirements. */
482 	const size_t framebufferSize = m->finfo.line_length * m->info.yres;
483 	const size_t alignedFramebufferSize = GRALLOC_ALIGN(m->finfo.line_length, 64) * m->info.yres;
484 
485 	*stride = m->info.xres;
486 
487 	if (numBuffers == 1)
488 	{
489 		// If we have only one buffer, we never use page-flipping. Instead,
490 		// we return a regular buffer which will be memcpy'ed to the main
491 		// screen when post is called.
492 		uint64_t newConsumerUsage = (consumer_usage & ~(static_cast<uint64_t>(GRALLOC_USAGE_HW_FB)));
493 		uint64_t newProducerUsage = (producer_usage & ~(static_cast<uint64_t>(GRALLOC_USAGE_HW_FB))) |
494 		                                               static_cast<uint64_t>(GRALLOC_USAGE_HW_2D);
495 		AWAR("fallback to single buffering. Virtual Y-res too small %d", m->info.yres);
496 		*byte_stride = GRALLOC_ALIGN(m->finfo.line_length, 64);
497 		return fb_alloc_from_ion_module(m, m->info.xres, m->info.yres, *byte_stride,
498 		                                alignedFramebufferSize, newConsumerUsage, newProducerUsage, pHandle);
499 	}
500 
501 	if (bufferMask >= ((1LU << numBuffers) - 1))
502 	{
503 		// We ran out of buffers, reset bufferMask
504 		bufferMask = 0;
505 		m->bufferMask = 0;
506 	}
507 
508 	uintptr_t framebufferVaddr = (uintptr_t)m->framebuffer->base;
509 
510 	// find a free slot
511 	for (uint32_t i = 0; i < numBuffers; i++)
512 	{
513 		if ((bufferMask & (1LU << i)) == 0)
514 		{
515 			m->bufferMask |= (1LU << i);
516 			break;
517 		}
518 
519 		framebufferVaddr += framebufferSize;
520 	}
521 
522 	// The entire framebuffer memory is already mapped, now create a buffer object for parts of this memory
523 	private_handle_t *hnd = new private_handle_t(
524 	    private_handle_t::PRIV_FLAGS_FRAMEBUFFER, framebufferSize, (void *)framebufferVaddr, consumer_usage,
525 	    producer_usage, dup(m->framebuffer->fb_fd), (framebufferVaddr - (uintptr_t)m->framebuffer->base),
526 	    m->finfo.line_length, m->info.xres, m->info.yres, m->fbdev_format);
527 
528 	/*
529 	 * Perform allocator specific actions. If these fail we fall back to a regular buffer
530 	 * which will be memcpy'ed to the main screen when fb_post is called.
531 	 */
532 	if (fb_alloc_framebuffer_dmabuf(m, hnd) == -1)
533 	{
534 		delete hnd;
535 		uint64_t newConsumerUsage = (consumer_usage & ~(static_cast<uint64_t>(GRALLOC_USAGE_HW_FB)));
536 		uint64_t newProducerUsage = (producer_usage & ~(static_cast<uint64_t>(GRALLOC_USAGE_HW_FB))) |
537 		                                               static_cast<uint64_t>(GRALLOC_USAGE_HW_2D);
538 		AERR("Fallback to single buffering. Unable to map framebuffer memory to handle:%p", hnd);
539 		*byte_stride = GRALLOC_ALIGN(m->finfo.line_length, 64);
540 		return fb_alloc_from_ion_module(m, m->info.xres, m->info.yres, *byte_stride,
541 		                                alignedFramebufferSize, newConsumerUsage, newProducerUsage, pHandle);
542 	}
543 
544 	*pHandle = hnd;
545 	*byte_stride = m->finfo.line_length;
546 
547 	return 0;
548 }
549 
550 #if GRALLOC_VERSION_MAJOR <= 1
framebuffer_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)551 int framebuffer_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
552 {
553 	int status = -EINVAL;
554 
555 	GRALLOC_UNUSED(name);
556 	GRALLOC_UNUSED(module);
557 	GRALLOC_UNUSED(device);
558 
559 #if GRALLOC_VERSION_MAJOR == 1
560 	gralloc1_device_t *gralloc_device;
561 #else
562 	alloc_device_t *gralloc_device;
563 #endif
564 
565 #if DISABLE_FRAMEBUFFER_HAL == 1
566 	AERR("Framebuffer HAL not support/disabled");
567 	return -ENODEV;
568 #else
569 
570 #if GRALLOC_VERSION_MAJOR == 1
571 	status = gralloc1_open(module, &gralloc_device);
572 #else
573 	status = gralloc_open(module, &gralloc_device);
574 #endif
575 
576 	if (status < 0)
577 	{
578 		return status;
579 	}
580 
581 	private_module_t *m = (private_module_t *)module;
582 
583 	/* Populate frame buffer pixel format */
584 #if GRALLOC_FB_BPP == 16
585 	m->fbdev_format = MALI_GRALLOC_FORMAT_INTERNAL_RGB_565;
586 #elif GRALLOC_FB_BPP == 32
587 	m->fbdev_format = MALI_GRALLOC_FORMAT_INTERNAL_BGRA_8888;
588 #else
589 #error "Invalid framebuffer bit depth"
590 #endif
591 
592 	status = init_frame_buffer(m);
593 
594 	/* malloc is used instead of 'new' to instantiate the struct framebuffer_device_t
595 	 * C++11 spec specifies that if a class/struct has a const member,default constructor
596 	 * is deleted. So, if 'new' is used to instantiate the class/struct, it will throw
597 	 * error complaining about deleted constructor. Even if the struct is wrapped in a class
598 	 * it will still try to use the base class constructor to initialize the members, resulting
599 	 * in error 'deleted constructor'.
600 	 * This leaves two options
601 	 * Option 1: initialize the const members at the instantiation time. With {value1, value2 ..}
602 	 * Which relies on the order of the members, and if members are reordered or a new member is introduced
603 	 * it will end up assiging wrong value to members. Designated assignment as well has been removed in C++11
604 	 * Option 2: use malloc instead of 'new' to allocate the class/struct and initialize the members in code.
605 	 * This is the only maintainable option available.
606 	 */
607 
608 	framebuffer_device_t *dev = reinterpret_cast<framebuffer_device_t *>(malloc(sizeof(framebuffer_device_t)));
609 
610 	/* if either or both of init_frame_buffer() and malloc failed */
611 	if ((status < 0) || (!dev))
612 	{
613 #if GRALLOC_VERSION_MAJOR == 1
614 		gralloc1_close(gralloc_device);
615 #else
616 		gralloc_close(gralloc_device);
617 #endif
618 		(!dev) ? (void)(status = -ENOMEM) : free(dev);
619 		return status;
620 	}
621 
622 	memset(dev, 0, sizeof(*dev));
623 
624 	/* initialize the procs */
625 	dev->common.tag = HARDWARE_DEVICE_TAG;
626 	dev->common.version = 0;
627 	dev->common.module = const_cast<hw_module_t *>(module);
628 	dev->common.close = fb_close;
629 	dev->setSwapInterval = fb_set_swap_interval;
630 	dev->post = fb_post;
631 	dev->setUpdateRect = 0;
632 
633 	int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
634 	const_cast<uint32_t &>(dev->flags) = 0;
635 	const_cast<uint32_t &>(dev->width) = m->info.xres;
636 	const_cast<uint32_t &>(dev->height) = m->info.yres;
637 	const_cast<int &>(dev->stride) = stride;
638 	const_cast<int &>(dev->format) = m->fbdev_format;
639 	const_cast<float &>(dev->xdpi) = m->xdpi;
640 	const_cast<float &>(dev->ydpi) = m->ydpi;
641 	const_cast<float &>(dev->fps) = m->fps;
642 	const_cast<int &>(dev->minSwapInterval) = 0;
643 	const_cast<int &>(dev->maxSwapInterval) = 1;
644 	*device = &dev->common;
645 
646 	gralloc_vsync_enable(dev);
647 
648 	return status;
649 #endif
650 }
651 #endif
652 
653 #if DISABLE_FRAMEBUFFER_HAL != 1
mali_gralloc_fb_allocate(private_module_t * const module,const buffer_descriptor_t * const bufDescriptor,buffer_handle_t * const outBuffers)654 int32_t mali_gralloc_fb_allocate(private_module_t * const module,
655                                  const buffer_descriptor_t * const bufDescriptor,
656                                  buffer_handle_t * const outBuffers)
657 {
658 	uint64_t format = bufDescriptor->hal_format;
659 
660 #if GRALLOC_FB_SWAP_RED_BLUE == 1
661 #if GRALLOC_FB_BPP == 16
662 	format = HAL_PIXEL_FORMAT_RGB_565;
663 #elif GRALLOC_FB_BPP == 32
664 	format = HAL_PIXEL_FORMAT_BGRA_8888;
665 #else
666 #error "Invalid framebuffer bit depth"
667 #endif
668 #endif
669 
670 	int byte_stride, pixel_stride;
671 	pthread_mutex_lock(&module->lock);
672 	const int status = fb_alloc_framebuffer_locked(module, bufDescriptor->consumer_usage,
673 	                                               bufDescriptor->producer_usage,
674 	                                               outBuffers, &pixel_stride, &byte_stride);
675 	pthread_mutex_unlock(&module->lock);
676 	if (status < 0)
677 	{
678 		return status;
679 	}
680 	else
681 	{
682 		private_handle_t *hnd = (private_handle_t *)*outBuffers;
683 
684 		/* Allocate a meta-data buffer for framebuffer too. fbhal
685 		 * ones wont need it but for hwc they will.
686 		 */
687 		(void)gralloc_buffer_attr_allocate(hnd);
688 
689 		hnd->req_format = format;
690 		hnd->yuv_info = MALI_YUV_BT601_NARROW;
691 		hnd->internal_format = format;
692 		hnd->alloc_format = format;
693 		hnd->plane_info[0].byte_stride = byte_stride;
694 		hnd->width = bufDescriptor->width;
695 		hnd->height = bufDescriptor->height;
696 		hnd->stride = pixel_stride;
697 		hnd->plane_info[0].alloc_width = bufDescriptor->width;;
698 		hnd->plane_info[0].alloc_heightt = bufDescriptor->height;
699 		hnd->layer_count = 1;
700 		return 0;
701 	}
702 }
703 #endif
704