1 /*
2  * Copyright (C) 2016-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 <errno.h>
20 #include <pthread.h>
21 #include <inttypes.h>
22 
23 #include <log/log.h>
24 #include <cutils/atomic.h>
25 #include <hardware/hardware.h>
26 #include <hardware/fb.h>
27 
28 #if GRALLOC_USE_GRALLOC1_API == 1
29 #include <hardware/gralloc1.h>
30 #else
31 #include <hardware/gralloc.h>
32 #endif
33 
34 #include "mali_gralloc_module.h"
35 #include "mali_gralloc_private_interface_types.h"
36 #include "mali_gralloc_buffer.h"
37 #include "gralloc_helper.h"
38 #include "framebuffer_device.h"
39 #include "gralloc_buffer_priv.h"
40 #include "mali_gralloc_formats.h"
41 #include "mali_gralloc_usages.h"
42 #include "mali_gralloc_bufferaccess.h"
43 #include "mali_gralloc_reference.h"
44 
45 #if GRALLOC_USE_GRALLOC1_API == 1
46 #include "mali_gralloc_public_interface.h"
47 #else
48 #include "legacy/alloc_device.h"
49 #endif
50 
mali_gralloc_module_device_open(const hw_module_t * module,const char * name,hw_device_t ** device)51 static int mali_gralloc_module_device_open(const hw_module_t *module, const char *name, hw_device_t **device)
52 {
53 	int status = -EINVAL;
54 
55 #if GRALLOC_USE_GRALLOC1_API == 1
56 
57 	if (!strncmp(name, GRALLOC_HARDWARE_MODULE_ID, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
58 	{
59 		status = mali_gralloc_device_open(module, name, device);
60 	}
61 
62 #else
63 
64 	if (!strncmp(name, GRALLOC_HARDWARE_GPU0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
65 	{
66 		status = alloc_device_open(module, name, device);
67 	}
68 
69 #endif
70 	else if (!strncmp(name, GRALLOC_HARDWARE_FB0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
71 	{
72 		status = framebuffer_device_open(module, name, device);
73 	}
74 
75 	return status;
76 }
77 
gralloc_register_buffer(gralloc_module_t const * module,buffer_handle_t handle)78 static int gralloc_register_buffer(gralloc_module_t const *module, buffer_handle_t handle)
79 {
80 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
81 
82 	return mali_gralloc_reference_retain(m, handle);
83 }
84 
gralloc_unregister_buffer(gralloc_module_t const * module,buffer_handle_t handle)85 static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle)
86 {
87 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
88 
89 	return mali_gralloc_reference_release(m, handle, false);
90 }
91 
gralloc_lock(gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr)92 static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h,
93                         void **vaddr)
94 {
95 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
96 
97 	return mali_gralloc_lock(m, handle, usage, l, t, w, h, vaddr);
98 }
99 
gralloc_lock_ycbcr(gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,android_ycbcr * ycbcr)100 static int gralloc_lock_ycbcr(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w,
101                               int h, android_ycbcr *ycbcr)
102 {
103 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
104 
105 	return mali_gralloc_lock_ycbcr(m, handle, usage, l, t, w, h, ycbcr);
106 }
107 
gralloc_unlock(gralloc_module_t const * module,buffer_handle_t handle)108 static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle)
109 {
110 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
111 
112 	return mali_gralloc_unlock(m, handle);
113 }
114 
gralloc_lock_async(gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr,int32_t fence_fd)115 static int gralloc_lock_async(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w,
116                               int h, void **vaddr, int32_t fence_fd)
117 {
118 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
119 
120 	return mali_gralloc_lock_async(m, handle, usage, l, t, w, h, vaddr, fence_fd);
121 }
122 
gralloc_lock_ycbcr_async(gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,android_ycbcr * ycbcr,int32_t fence_fd)123 static int gralloc_lock_ycbcr_async(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t,
124                                     int w, int h, android_ycbcr *ycbcr, int32_t fence_fd)
125 {
126 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
127 
128 	return mali_gralloc_lock_ycbcr_async(m, handle, usage, l, t, w, h, ycbcr, fence_fd);
129 }
130 
gralloc_unlock_async(gralloc_module_t const * module,buffer_handle_t handle,int32_t * fence_fd)131 static int gralloc_unlock_async(gralloc_module_t const *module, buffer_handle_t handle, int32_t *fence_fd)
132 {
133 	const mali_gralloc_module *m = reinterpret_cast<const mali_gralloc_module *>(module);
134 
135 	return mali_gralloc_unlock_async(m, handle, fence_fd);
136 }
137 
138 // There is one global instance of the module
139 
140 static struct hw_module_methods_t mali_gralloc_module_methods = { mali_gralloc_module_device_open };
141 
private_module_t()142 private_module_t::private_module_t()
143 {
144 #define INIT_ZERO(obj) (memset(&(obj), 0, sizeof((obj))))
145 	base.common.tag = HARDWARE_MODULE_TAG;
146 #if GRALLOC_USE_GRALLOC1_API == 1
147 	base.common.version_major = GRALLOC_MODULE_API_VERSION_1_0;
148 #else
149 	base.common.version_major = GRALLOC_MODULE_API_VERSION_0_3;
150 #endif
151 	base.common.version_minor = 0;
152 	base.common.id = GRALLOC_HARDWARE_MODULE_ID;
153 	base.common.name = "Graphics Memory Allocator Module";
154 	base.common.author = "ARM Ltd.";
155 	base.common.methods = &mali_gralloc_module_methods;
156 	base.common.dso = NULL;
157 	INIT_ZERO(base.common.reserved);
158 
159 #if GRALLOC_USE_GRALLOC1_API == 0
160 	base.registerBuffer = gralloc_register_buffer;
161 	base.unregisterBuffer = gralloc_unregister_buffer;
162 	base.lock = gralloc_lock;
163 	base.lock_ycbcr = gralloc_lock_ycbcr;
164 	base.unlock = gralloc_unlock;
165 	base.lockAsync = gralloc_lock_async;
166 	base.lockAsync_ycbcr = gralloc_lock_ycbcr_async;
167 	base.unlockAsync = gralloc_unlock_async;
168 	base.perform = NULL;
169 	INIT_ZERO(base.reserved_proc);
170 #endif
171 
172 	framebuffer = NULL;
173 	flags = 0;
174 	numBuffers = 0;
175 	bufferMask = 0;
176 	pthread_mutex_init(&(lock), NULL);
177 	currentBuffer = NULL;
178 	INIT_ZERO(info);
179 	INIT_ZERO(finfo);
180 	xdpi = 0.0f;
181 	ydpi = 0.0f;
182 	fps = 0.0f;
183 	swapInterval = 1;
184 	ion_client = -1;
185 
186 #undef INIT_ZERO
187 };
188 
189 /*
190  * HAL_MODULE_INFO_SYM will be initialized using the default constructor
191  * implemented above
192  */
193 struct private_module_t HAL_MODULE_INFO_SYM;
194