1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
19 #include <limits.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdarg.h>
25
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30
31 #include <cutils/log.h>
32 #include <cutils/atomic.h>
33 #include <utils/Trace.h>
34
35 #include <hardware/hardware.h>
36 #include <hardware/gralloc.h>
37
38 #include "gralloc_priv.h"
39 #include "gr.h"
40 #include "alloc_controller.h"
41 #include "memalloc.h"
42 #include <qdMetaData.h>
43
44
45 using namespace gralloc;
46 /*****************************************************************************/
47
48 // Return the type of allocator -
49 // these are used for mapping/unmapping
getAllocator(int flags)50 static IMemAlloc* getAllocator(int flags)
51 {
52 IMemAlloc* memalloc;
53 IAllocController* alloc_ctrl = IAllocController::getInstance();
54 memalloc = alloc_ctrl->getAllocator(flags);
55 return memalloc;
56 }
57
gralloc_map(gralloc_module_t const * module,buffer_handle_t handle)58 static int gralloc_map(gralloc_module_t const* module,
59 buffer_handle_t handle)
60 {
61 ATRACE_CALL();
62 if(!module)
63 return -EINVAL;
64
65 private_handle_t* hnd = (private_handle_t*)handle;
66 unsigned int size = 0;
67 int err = 0;
68 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
69 void *mappedAddress = MAP_FAILED;
70 hnd->base = 0;
71 hnd->base_metadata = 0;
72
73 // Dont map framebuffer and secure buffers
74 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
75 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
76 size = hnd->size;
77 err = memalloc->map_buffer(&mappedAddress, size,
78 hnd->offset, hnd->fd);
79 if(err || mappedAddress == MAP_FAILED) {
80 ALOGE("Could not mmap handle %p, fd=%d (%s)",
81 handle, hnd->fd, strerror(errno));
82 return -errno;
83 }
84
85 hnd->base = uint64_t(mappedAddress) + hnd->offset;
86 }
87
88 //Allow mapping of metadata for all buffers including secure ones, but not
89 //of framebuffer
90 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
91 mappedAddress = MAP_FAILED;
92 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
93 err = memalloc->map_buffer(&mappedAddress, size,
94 hnd->offset_metadata, hnd->fd_metadata);
95 if(err || mappedAddress == MAP_FAILED) {
96 ALOGE("Could not mmap handle %p, fd=%d (%s)",
97 handle, hnd->fd_metadata, strerror(errno));
98 return -errno;
99 }
100 hnd->base_metadata = uint64_t(mappedAddress) + hnd->offset_metadata;
101 }
102 return 0;
103 }
104
gralloc_unmap(gralloc_module_t const * module,buffer_handle_t handle)105 static int gralloc_unmap(gralloc_module_t const* module,
106 buffer_handle_t handle)
107 {
108 ATRACE_CALL();
109 int err = -EINVAL;
110 if(!module)
111 return err;
112
113 private_handle_t* hnd = (private_handle_t*)handle;
114 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
115 if(!memalloc)
116 return err;
117
118 if(hnd->base) {
119 err = memalloc->unmap_buffer((void*)hnd->base, hnd->size, hnd->offset);
120 if (err) {
121 ALOGE("Could not unmap memory at address %p, %s", (void*) hnd->base,
122 strerror(errno));
123 return -errno;
124 }
125 hnd->base = 0;
126 }
127
128 if(hnd->base_metadata) {
129 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
130 err = memalloc->unmap_buffer((void*)hnd->base_metadata,
131 size, hnd->offset_metadata);
132 if (err) {
133 ALOGE("Could not unmap memory at address %p, %s",
134 (void*) hnd->base_metadata, strerror(errno));
135 return -errno;
136 }
137 hnd->base_metadata = 0;
138 }
139
140 return 0;
141 }
142
143 /*****************************************************************************/
144
145 static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
146
147 /*****************************************************************************/
148
gralloc_register_buffer(gralloc_module_t const * module,buffer_handle_t handle)149 int gralloc_register_buffer(gralloc_module_t const* module,
150 buffer_handle_t handle)
151 {
152 ATRACE_CALL();
153 if (!module || private_handle_t::validate(handle) < 0)
154 return -EINVAL;
155
156 /* NOTE: we need to initialize the buffer as not mapped/not locked
157 * because it shouldn't when this function is called the first time
158 * in a new process. Ideally these flags shouldn't be part of the
159 * handle, but instead maintained in the kernel or at least
160 * out-of-line
161 */
162
163 int err = gralloc_map(module, handle);
164 if (err) {
165 ALOGE("%s: gralloc_map failed", __FUNCTION__);
166 return err;
167 }
168
169 return 0;
170 }
171
gralloc_unregister_buffer(gralloc_module_t const * module,buffer_handle_t handle)172 int gralloc_unregister_buffer(gralloc_module_t const* module,
173 buffer_handle_t handle)
174 {
175 ATRACE_CALL();
176 if (!module || private_handle_t::validate(handle) < 0)
177 return -EINVAL;
178
179 /*
180 * If the buffer has been mapped during a lock operation, it's time
181 * to un-map it. It's an error to be here with a locked buffer.
182 * NOTE: the framebuffer is handled differently and is never unmapped.
183 * Also base and base_metadata are reset.
184 */
185 return gralloc_unmap(module, handle);
186 }
187
terminateBuffer(gralloc_module_t const * module,private_handle_t * hnd)188 int terminateBuffer(gralloc_module_t const* module,
189 private_handle_t* hnd)
190 {
191 ATRACE_CALL();
192 if(!module)
193 return -EINVAL;
194
195 /*
196 * If the buffer has been mapped during a lock operation, it's time
197 * to un-map it. It's an error to be here with a locked buffer.
198 * NOTE: the framebuffer is handled differently and is never unmapped.
199 * Also base and base_metadata are reset.
200 */
201 return gralloc_unmap(module, hnd);
202 }
203
gralloc_map_and_invalidate(gralloc_module_t const * module,buffer_handle_t handle,int usage)204 static int gralloc_map_and_invalidate (gralloc_module_t const* module,
205 buffer_handle_t handle, int usage)
206 {
207 ATRACE_CALL();
208 if (!module || private_handle_t::validate(handle) < 0)
209 return -EINVAL;
210
211 int err = 0;
212 private_handle_t* hnd = (private_handle_t*)handle;
213 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
214 if (hnd->base == 0) {
215 // we need to map for real
216 pthread_mutex_t* const lock = &sMapLock;
217 pthread_mutex_lock(lock);
218 err = gralloc_map(module, handle);
219 pthread_mutex_unlock(lock);
220 }
221 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and
222 hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) {
223 //Invalidate if CPU reads in software and there are non-CPU
224 //writers. No need to do this for the metadata buffer as it is
225 //only read/written in software.
226 if ((usage & GRALLOC_USAGE_SW_READ_MASK) and
227 (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER))
228 {
229 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
230 err = memalloc->clean_buffer((void*)hnd->base,
231 hnd->size, hnd->offset, hnd->fd,
232 CACHE_INVALIDATE);
233 }
234 //Mark the buffer to be flushed after CPU write.
235 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
236 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
237 }
238 }
239 }
240
241 return err;
242 }
243
gralloc_lock(gralloc_module_t const * module,buffer_handle_t handle,int usage,int,int,int,int,void ** vaddr)244 int gralloc_lock(gralloc_module_t const* module,
245 buffer_handle_t handle, int usage,
246 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
247 void** vaddr)
248 {
249 ATRACE_CALL();
250 private_handle_t* hnd = (private_handle_t*)handle;
251 int err = gralloc_map_and_invalidate(module, handle, usage);
252 if(!err)
253 *vaddr = (void*)hnd->base;
254 return err;
255 }
256
gralloc_lock_ycbcr(gralloc_module_t const * module,buffer_handle_t handle,int usage,int,int,int,int,struct android_ycbcr * ycbcr)257 int gralloc_lock_ycbcr(gralloc_module_t const* module,
258 buffer_handle_t handle, int usage,
259 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
260 struct android_ycbcr *ycbcr)
261 {
262 ATRACE_CALL();
263 private_handle_t* hnd = (private_handle_t*)handle;
264 int err = gralloc_map_and_invalidate(module, handle, usage);
265 if(!err)
266 err = getYUVPlaneInfo(hnd, ycbcr);
267 return err;
268 }
269
gralloc_unlock(gralloc_module_t const * module,buffer_handle_t handle)270 int gralloc_unlock(gralloc_module_t const* module,
271 buffer_handle_t handle)
272 {
273 ATRACE_CALL();
274 if (!module || private_handle_t::validate(handle) < 0)
275 return -EINVAL;
276
277 int err = 0;
278 private_handle_t* hnd = (private_handle_t*)handle;
279
280 IMemAlloc* memalloc = getAllocator(hnd->flags);
281 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
282 err = memalloc->clean_buffer((void*)hnd->base,
283 hnd->size, hnd->offset, hnd->fd,
284 CACHE_CLEAN);
285 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
286 }
287
288 return err;
289 }
290
291 /*****************************************************************************/
292
gralloc_perform(struct gralloc_module_t const * module,int operation,...)293 int gralloc_perform(struct gralloc_module_t const* module,
294 int operation, ... )
295 {
296 int res = -EINVAL;
297 va_list args;
298 if(!module)
299 return res;
300
301 va_start(args, operation);
302 switch (operation) {
303 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
304 {
305 int fd = va_arg(args, int);
306 unsigned int size = va_arg(args, unsigned int);
307 unsigned int offset = va_arg(args, unsigned int);
308 void* base = va_arg(args, void*);
309 int width = va_arg(args, int);
310 int height = va_arg(args, int);
311 int format = va_arg(args, int);
312
313 native_handle_t** handle = va_arg(args, native_handle_t**);
314 private_handle_t* hnd = (private_handle_t*)native_handle_create(
315 private_handle_t::sNumFds, private_handle_t::sNumInts());
316 if (hnd) {
317 hnd->magic = private_handle_t::sMagic;
318 hnd->fd = fd;
319 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
320 hnd->size = size;
321 hnd->offset = offset;
322 hnd->base = uint64_t(base) + offset;
323 hnd->gpuaddr = 0;
324 hnd->width = width;
325 hnd->height = height;
326 hnd->format = format;
327 *handle = (native_handle_t *)hnd;
328 res = 0;
329 }
330 break;
331
332 }
333 case GRALLOC_MODULE_PERFORM_GET_STRIDE:
334 {
335 int width = va_arg(args, int);
336 int format = va_arg(args, int);
337 int *stride = va_arg(args, int *);
338 int alignedw = 0, alignedh = 0;
339 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
340 0, format, 0, alignedw, alignedh);
341 *stride = alignedw;
342 res = 0;
343 } break;
344
345 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE:
346 {
347 const private_handle_t* hnd = va_arg(args, private_handle_t*);
348 int *stride = va_arg(args, int *);
349 if (private_handle_t::validate(hnd)) {
350 return res;
351 }
352
353 int alignedw = 0, alignedh = 0;
354 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh);
355 *stride = alignedw;
356
357 res = 0;
358 } break;
359
360 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE:
361 {
362 const private_handle_t* hnd = va_arg(args, private_handle_t*);
363 int *stride = va_arg(args, int *);
364 int *height = va_arg(args, int *);
365 if (private_handle_t::validate(hnd)) {
366 return res;
367 }
368
369 int alignedw = 0, alignedh = 0;
370 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh);
371 *stride = alignedw;
372 *height = alignedh;
373
374 res = 0;
375 } break;
376
377 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES:
378 {
379 int width = va_arg(args, int);
380 int height = va_arg(args, int);
381 int format = va_arg(args, int);
382 int usage = va_arg(args, int);
383 int *alignedWidth = va_arg(args, int *);
384 int *alignedHeight = va_arg(args, int *);
385 int *tileEnabled = va_arg(args,int *);
386 *tileEnabled = isUBwcEnabled(format, usage) ||
387 isMacroTileEnabled(format, usage);
388 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
389 height, format, usage, *alignedWidth, *alignedHeight);
390 res = 0;
391 } break;
392
393 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE:
394 {
395 private_handle_t* hnd = va_arg(args, private_handle_t*);
396 int *color_space = va_arg(args, int *);
397 if (private_handle_t::validate(hnd)) {
398 return res;
399 }
400 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
401 if(metadata && metadata->operation & UPDATE_COLOR_SPACE) {
402 *color_space = metadata->colorSpace;
403 res = 0;
404 }
405 } break;
406
407 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO:
408 {
409 private_handle_t* hnd = va_arg(args, private_handle_t*);
410 android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *);
411 if (!private_handle_t::validate(hnd)) {
412 res = getYUVPlaneInfo(hnd, ycbcr);
413 }
414 } break;
415
416 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO:
417 {
418 private_handle_t* hnd = va_arg(args, private_handle_t*);
419 int *map_secure_buffer = va_arg(args, int *);
420 if (private_handle_t::validate(hnd)) {
421 return res;
422 }
423 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
424 if(metadata && metadata->operation & MAP_SECURE_BUFFER) {
425 *map_secure_buffer = metadata->mapSecureBuffer;
426 res = 0;
427 } else {
428 *map_secure_buffer = 0;
429 }
430 } break;
431
432 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG:
433 {
434 private_handle_t* hnd = va_arg(args, private_handle_t*);
435 int *flag = va_arg(args, int *);
436 if (private_handle_t::validate(hnd)) {
437 return res;
438 }
439 *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
440 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
441 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
442 *flag = 0;
443 }
444 res = 0;
445 } break;
446
447 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS:
448 {
449 private_handle_t* hnd = va_arg(args, private_handle_t*);
450 void** rgb_data = va_arg(args, void**);
451 if (!private_handle_t::validate(hnd)) {
452 res = getRgbDataAddress(hnd, rgb_data);
453 }
454 } break;
455
456 case GRALLOC_MODULE_PERFORM_GET_IGC:
457 {
458 private_handle_t* hnd = va_arg(args, private_handle_t*);
459 uint32_t *igc = va_arg(args, uint32_t *);
460 if (!private_handle_t::validate(hnd) && igc) {
461 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
462 if (metadata && (metadata->operation & SET_IGC)) {
463 *igc = metadata->igc;
464 res = 0;
465 }
466 }
467 } break;
468
469 case GRALLOC_MODULE_PERFORM_SET_IGC:
470 res = 0;
471 break;
472
473 case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE:
474 {
475 private_handle_t* hnd = va_arg(args, private_handle_t*);
476 uint32_t *enable = va_arg(args, uint32_t*);
477 if (!private_handle_t::validate(hnd)) {
478 setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable);
479 res = 0;
480 }
481 } break;
482 default:
483 break;
484 }
485 va_end(args);
486 return res;
487 }
488