1 /*
2  * Copyright (C) 2010-2017 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 <pthread.h>
22 #include <stdlib.h>
23 
24 #include <log/log.h>
25 #include <cutils/atomic.h>
26 #include <hardware/hardware.h>
27 #include <hardware/gralloc.h>
28 
29 #include <sys/ioctl.h>
30 
31 #include "mali_gralloc_module.h"
32 #include "alloc_device.h"
33 #include "gralloc_priv.h"
34 #include "gralloc_helper.h"
35 #include "framebuffer_device.h"
36 #include "mali_gralloc_ion.h"
37 #include "gralloc_buffer_priv.h"
38 #include "mali_gralloc_bufferdescriptor.h"
39 #include "mali_gralloc_bufferallocation.h"
40 #include "mali_gralloc_formats.h"
41 #include "mali_gralloc_usages.h"
42 
alloc_device_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride)43 static int alloc_device_alloc(alloc_device_t *dev, int w, int h, int format, int usage, buffer_handle_t *pHandle,
44                               int *pStride)
45 {
46 	mali_gralloc_module *m;
47 	int err = -EINVAL;
48 
49 	if (!dev || !pHandle || !pStride)
50 	{
51 		return err;
52 	}
53 
54 	m = reinterpret_cast<private_module_t *>(dev->common.module);
55 
56 #if GRALLOC_FB_SWAP_RED_BLUE == 1
57 
58 	/* match the framebuffer format */
59 	if (usage & GRALLOC_USAGE_HW_FB)
60 	{
61 #ifdef GRALLOC_16_BITS
62 		format = HAL_PIXEL_FORMAT_RGB_565;
63 #else
64 		format = HAL_PIXEL_FORMAT_BGRA_8888;
65 #endif
66 	}
67 
68 #endif
69 
70 #if DISABLE_FRAMEBUFFER_HAL != 1
71 
72 	if (usage & GRALLOC_USAGE_HW_FB)
73 	{
74 		int byte_stride;
75 		int pixel_stride;
76 
77 		err = fb_alloc_framebuffer(m, usage, usage, pHandle, &pixel_stride, &byte_stride);
78 
79 		if (err >= 0)
80 		{
81 			private_handle_t *hnd = (private_handle_t *)*pHandle;
82 
83 			/* Allocate a meta-data buffer for framebuffer too. fbhal
84 			 * ones wont need it but it will lead to binder IPC fail
85 			 * without a valid share_attr_fd.
86 			 *
87 			 * Explicitly ignore allocation errors since it is not critical to have
88 			 */
89 			(void)gralloc_buffer_attr_allocate(hnd);
90 
91 			hnd->req_format = format;
92 			hnd->yuv_info = MALI_YUV_BT601_NARROW;
93 			hnd->internal_format = format;
94 			hnd->byte_stride = byte_stride;
95 			hnd->width = w;
96 			hnd->height = h;
97 			hnd->stride = pixel_stride;
98 			hnd->internalWidth = w;
99 			hnd->internalHeight = h;
100 		}
101 	}
102 	else
103 #endif
104 	{
105 		/* share the same allocation interface with gralloc1.*/
106 		buffer_descriptor_t buffer_descriptor;
107 		gralloc_buffer_descriptor_t gralloc_buffer_descriptor[1];
108 
109 		buffer_descriptor.hal_format = format;
110 		buffer_descriptor.consumer_usage = usage;
111 		buffer_descriptor.producer_usage = usage;
112 		buffer_descriptor.width = w;
113 		buffer_descriptor.height = h;
114 		buffer_descriptor.layer_count = 1;
115 		buffer_descriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
116 		gralloc_buffer_descriptor[0] = (gralloc_buffer_descriptor_t)(&buffer_descriptor);
117 
118 		if (mali_gralloc_buffer_allocate(m, gralloc_buffer_descriptor, 1, pHandle, NULL) < 0)
119 		{
120 			ALOGE("Failed to allocate buffer.");
121 			err = -ENOMEM;
122 		}
123 		else
124 		{
125 			mali_gralloc_query_getstride(*pHandle, pStride);
126 			err = 0;
127 		}
128 	}
129 
130 	return err;
131 }
132 
alloc_device_free(alloc_device_t * dev,buffer_handle_t handle)133 static int alloc_device_free(alloc_device_t *dev, buffer_handle_t handle)
134 {
135 	if (private_handle_t::validate(handle) < 0)
136 	{
137 		return -EINVAL;
138 	}
139 
140 	private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(handle);
141 	private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
142 
143 	if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
144 	{
145 		// free this buffer
146 		close(hnd->fd);
147 	}
148 	else
149 	{
150 		mali_gralloc_buffer_free(handle);
151 	}
152 
153 	delete hnd;
154 
155 	return 0;
156 }
157 
alloc_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)158 int alloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
159 {
160 	alloc_device_t *dev;
161 
162 	GRALLOC_UNUSED(name);
163 
164 	dev = new alloc_device_t;
165 
166 	if (NULL == dev)
167 	{
168 		return -1;
169 	}
170 
171 	/* initialize our state here */
172 	memset(dev, 0, sizeof(*dev));
173 
174 	/* initialize the procs */
175 	dev->common.tag = HARDWARE_DEVICE_TAG;
176 	dev->common.version = 0;
177 	dev->common.module = const_cast<hw_module_t *>(module);
178 	dev->common.close = mali_gralloc_ion_device_close;
179 	dev->alloc = alloc_device_alloc;
180 	dev->free = alloc_device_free;
181 
182 	*device = &dev->common;
183 
184 	return 0;
185 }
186