blob: 232ecc12ba4449dfd0613a360f1c7a14946c2cac [file] [log] [blame]
John Stultz16100f62017-05-03 11:12:18 -07001/*
2 * Copyright (C) 2010 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 <cutils/log.h>
24#include <cutils/atomic.h>
25#include <hardware/hardware.h>
26#include <hardware/gralloc.h>
27
28#include "gralloc_priv.h"
29#include "gralloc_helper.h"
30#include "alloc_device.h"
31#include "framebuffer_device.h"
32
33#include "gralloc_module_allocator_specific.h"
34#include "gralloc_buffer_priv.h"
35
36#include "mali_gralloc_formats.h"
37
38static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER;
39
40static int gralloc_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
41{
42 int status = -EINVAL;
43
44 if (!strncmp(name, GRALLOC_HARDWARE_GPU0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
45 {
46 status = alloc_device_open(module, name, device);
47 }
48 else if (!strncmp(name, GRALLOC_HARDWARE_FB0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
49 {
50 status = framebuffer_device_open(module, name, device);
51 }
52
53 return status;
54}
55
56static int gralloc_register_buffer(gralloc_module_t const* module, buffer_handle_t handle)
57{
58 GRALLOC_UNUSED(module);
59
60 if (private_handle_t::validate(handle) < 0)
61 {
62 AERR("Registering invalid buffer %p, returning error", handle);
63 return -EINVAL;
64 }
65
66 // if this handle was created in this process, then we keep it as is.
67 private_handle_t* hnd = (private_handle_t*)handle;
68
69 if (hnd->pid == getpid())
70 {
71 // If the handle is created and registered in the same process this is valid,
72 // but it could also be that application is registering twice which is illegal.
73 AWAR("Registering handle %p coming from the same process: %d.", hnd, hnd->pid);
74 }
75
76 int retval = -EINVAL;
77
78 pthread_mutex_lock(&s_map_lock);
79
80 hnd->pid = getpid();
81
82 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
83 {
84 AERR( "Can't register buffer %p as it is a framebuffer", handle );
85 }
86 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
87 {
88 retval = gralloc_backend_register(hnd);
89 }
90 else
91 {
92 AERR("unknown buffer flags not supported. flags = %d", hnd->flags );
93 }
94
95 pthread_mutex_unlock(&s_map_lock);
96 return retval;
97}
98
99static int gralloc_unregister_buffer(gralloc_module_t const* module, buffer_handle_t handle)
100{
101 GRALLOC_UNUSED(module);
102
103 if (private_handle_t::validate(handle) < 0)
104 {
105 AERR("unregistering invalid buffer %p, returning error", handle);
106 return -EINVAL;
107 }
108
109 private_handle_t* hnd = (private_handle_t*)handle;
110
111 AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState);
112
113 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
114 {
115 AERR( "Can't unregister buffer %p as it is a framebuffer", handle );
116 }
117 else if (hnd->pid == getpid()) // never unmap buffers that were not created in this process
118 {
119 pthread_mutex_lock(&s_map_lock);
120
121 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
122 {
123 gralloc_backend_unregister(hnd);
124 }
125 else
126 {
127 AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
128 }
129
130 /*
131 * Close shared attribute region file descriptor. It might seem strange to "free"
132 * this here since this can happen in a client process, but free here is nothing
133 * but unmapping and closing the duplicated file descriptor. The original ashmem
134 * fd instance is still open until alloc_device_free() is called. Even sharing
135 * of gralloc buffers within the same process should have fds dup:ed.
136 */
137 gralloc_buffer_attr_free( hnd );
138
139 hnd->base = 0;
140 hnd->lockState = 0;
141 hnd->writeOwner = 0;
142
143 pthread_mutex_unlock(&s_map_lock);
144 }
145 else
146 {
147 AERR( "Trying to unregister buffer %p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
148 }
149
150 return 0;
151}
152
153static int gralloc_lock(gralloc_module_t const* module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void** vaddr)
154{
155 GRALLOC_UNUSED(module);
156 GRALLOC_UNUSED(l);
157 GRALLOC_UNUSED(t);
158 GRALLOC_UNUSED(w);
159 GRALLOC_UNUSED(h);
160
161 if (private_handle_t::validate(handle) < 0)
162 {
163 AERR("Locking invalid buffer %p, returning error", handle );
164 return -EINVAL;
165 }
166
167 private_handle_t* hnd = (private_handle_t*)handle;
168
169 if (hnd->req_format == HAL_PIXEL_FORMAT_YCbCr_420_888)
170 {
171 AERR("Buffers with format YCbCr_420_888 must be locked using (*lock_ycbcr)" );
172 return -EINVAL;
173 }
174
175 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
176 {
177 hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
178 }
179 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
180 {
181 *vaddr = (void*)hnd->base;
182 }
183 return 0;
184}
185
186static int gralloc_lock_ycbcr(gralloc_module_t const* module, buffer_handle_t handle, int usage,
187 int l, int t, int w, int h,
188 android_ycbcr *ycbcr)
189{
190 GRALLOC_UNUSED(module);
191 GRALLOC_UNUSED(l);
192 GRALLOC_UNUSED(t);
193 GRALLOC_UNUSED(w);
194 GRALLOC_UNUSED(h);
195
196 if (private_handle_t::validate(handle) < 0)
197 {
198 AERR("Locking invalid buffer %p, returning error", handle );
199 return -EINVAL;
200 }
201
202 private_handle_t* hnd = (private_handle_t*)handle;
203
204 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
205 {
206 hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
207 }
208 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK) &&
209 !(hnd->internal_format & MALI_GRALLOC_INTFMT_EXT_MASK))
210 {
211 char* base = (char*)hnd->base;
212 int y_stride = hnd->byte_stride;
213 /* Ensure height is aligned for subsampled chroma before calculating buffer parameters */
214 int adjusted_height = GRALLOC_ALIGN(hnd->height, 2);
215 int y_size = y_stride * adjusted_height;
216
217 int u_offset = 0;
218 int v_offset = 0;
219 int c_stride = 0;
220 int step = 0;
221
222 uint64_t base_format = hnd->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK;
223
224 switch (base_format)
225 {
226 case MALI_GRALLOC_FORMAT_INTERNAL_NV12:
227 c_stride = y_stride;
228 /* Y plane, UV plane */
229 u_offset = y_size;
230 v_offset = y_size + 1;
231 step = 2;
232 break;
233
234 case MALI_GRALLOC_FORMAT_INTERNAL_NV21:
235 c_stride = y_stride;
236 /* Y plane, UV plane */
237 v_offset = y_size;
238 u_offset = y_size + 1;
239 step = 2;
240 break;
241
242 case MALI_GRALLOC_FORMAT_INTERNAL_YV12:
243 {
244 int c_size;
245
246 /* Stride alignment set to 16 as the SW access flags were set */
247 c_stride = GRALLOC_ALIGN(hnd->byte_stride / 2, 16);
248 c_size = c_stride * (adjusted_height / 2);
249 /* Y plane, V plane, U plane */
250 v_offset = y_size;
251 u_offset = y_size + c_size;
252 step = 1;
253 break;
254 }
255
256 default:
257 AERR("Can't lock buffer %p: wrong format %" PRIx64, hnd, hnd->internal_format);
258 return -EINVAL;
259 }
260
261 ycbcr->y = base;
262 ycbcr->cb = base + u_offset;
263 ycbcr->cr = base + v_offset;
264 ycbcr->ystride = y_stride;
265 ycbcr->cstride = c_stride;
266 ycbcr->chroma_step = step;
267 }
268 return 0;
269}
270
271static int gralloc_unlock(gralloc_module_t const* module, buffer_handle_t handle)
272{
273 GRALLOC_UNUSED(module);
274
275 if (private_handle_t::validate(handle) < 0)
276 {
277 AERR( "Unlocking invalid buffer %p, returning error", handle );
278 return -EINVAL;
279 }
280
281 private_handle_t* hnd = (private_handle_t*)handle;
282
283 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner)
284 {
285 gralloc_backend_sync(hnd);
286 }
287
288 return 0;
289}
290
291// There is one global instance of the module
292
293static struct hw_module_methods_t gralloc_module_methods =
294{
295 gralloc_device_open
296};
297
298private_module_t::private_module_t()
299{
300#define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj))))
301
302 base.common.tag = HARDWARE_MODULE_TAG;
303 base.common.version_major = 1;
304 base.common.version_minor = 0;
305 base.common.id = GRALLOC_HARDWARE_MODULE_ID;
306 base.common.name = "Graphics Memory Allocator Module";
307 base.common.author = "ARM Ltd.";
308 base.common.methods = &gralloc_module_methods;
309 base.common.dso = NULL;
310 INIT_ZERO(base.common.reserved);
311
312 base.registerBuffer = gralloc_register_buffer;
313 base.unregisterBuffer = gralloc_unregister_buffer;
314 base.lock = gralloc_lock;
315 base.lock_ycbcr = gralloc_lock_ycbcr;
316 base.unlock = gralloc_unlock;
317 base.perform = NULL;
318 INIT_ZERO(base.reserved_proc);
319
320 framebuffer = NULL;
321 flags = 0;
322 numBuffers = 0;
323 bufferMask = 0;
324 pthread_mutex_init(&(lock), NULL);
325 currentBuffer = NULL;
326 INIT_ZERO(info);
327 INIT_ZERO(finfo);
328 xdpi = 0.0f;
329 ydpi = 0.0f;
330 fps = 0.0f;
331 swapInterval = 1;
332
333#undef INIT_ZERO
334};
335
336/*
337 * HAL_MODULE_INFO_SYM will be initialized using the default constructor
338 * implemented above
339 */
340struct private_module_t HAL_MODULE_INFO_SYM;
341