• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
115 		gralloc_buffer_descriptor[0] = (gralloc_buffer_descriptor_t)(&buffer_descriptor);
116 
117 		if (mali_gralloc_buffer_allocate(m, gralloc_buffer_descriptor, 1, pHandle, NULL) < 0)
118 		{
119 			ALOGE("Failed to allocate buffer.");
120 			err = -ENOMEM;
121 		}
122 		else
123 		{
124 			mali_gralloc_query_getstride(*pHandle, pStride);
125 			err = 0;
126 		}
127 	}
128 
129 	return err;
130 }
131 
alloc_device_free(alloc_device_t * dev,buffer_handle_t handle)132 static int alloc_device_free(alloc_device_t *dev, buffer_handle_t handle)
133 {
134 	if (private_handle_t::validate(handle) < 0)
135 	{
136 		return -EINVAL;
137 	}
138 
139 	private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(handle);
140 	private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
141 
142 	if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
143 	{
144 		// free this buffer
145 		close(hnd->fd);
146 	}
147 	else
148 	{
149 		mali_gralloc_buffer_free(handle);
150 	}
151 
152 	delete hnd;
153 
154 	return 0;
155 }
156 
alloc_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)157 int alloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
158 {
159 	alloc_device_t *dev;
160 
161 	GRALLOC_UNUSED(name);
162 
163 	dev = new alloc_device_t;
164 
165 	if (NULL == dev)
166 	{
167 		return -1;
168 	}
169 
170 	/* initialize our state here */
171 	memset(dev, 0, sizeof(*dev));
172 
173 	/* initialize the procs */
174 	dev->common.tag = HARDWARE_DEVICE_TAG;
175 	dev->common.version = 0;
176 	dev->common.module = const_cast<hw_module_t *>(module);
177 	dev->common.close = mali_gralloc_ion_device_close;
178 	dev->alloc = alloc_device_alloc;
179 	dev->free = alloc_device_free;
180 
181 	*device = &dev->common;
182 
183 	return 0;
184 }
185