blob: d475fb82e10a421291a41429b2c42d6cbff098eb [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Shuzhen Wangc502c872014-01-28 16:10:42 -08003 * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07004 *
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#include <limits.h>
19#include <errno.h>
20#include <pthread.h>
21#include <unistd.h>
22#include <string.h>
23#include <stdarg.h>
24
25#include <sys/mman.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/ioctl.h>
29#include <linux/ashmem.h>
30
31#include <cutils/log.h>
32#include <cutils/atomic.h>
33#include <cutils/ashmem.h>
34
35#include <hardware/hardware.h>
36#include <hardware/gralloc.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037#include <linux/android_pmem.h>
38
39#include "gralloc_priv.h"
40#include "gr.h"
41#include "alloc_controller.h"
42#include "memalloc.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080043#include <qdMetaData.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070044
45using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046/*****************************************************************************/
47
48// Return the type of allocator -
49// these are used for mapping/unmapping
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070050static IMemAlloc* getAllocator(int flags)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070051{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070052 IMemAlloc* memalloc;
53 IAllocController* alloc_ctrl = IAllocController::getInstance();
Iliyan Malchev202a77d2012-06-11 14:41:12 -070054 memalloc = alloc_ctrl->getAllocator(flags);
55 return memalloc;
56}
57
58static int gralloc_map(gralloc_module_t const* module,
Naseer Ahmed34d33bc2013-05-08 12:28:37 -040059 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070060{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080061 if(!module)
62 return -EINVAL;
63
Iliyan Malchev202a77d2012-06-11 14:41:12 -070064 private_handle_t* hnd = (private_handle_t*)handle;
65 void *mappedAddress;
66 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
67 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
68 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070069 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070070 int err = memalloc->map_buffer(&mappedAddress, size,
Naseer Ahmed29a26812012-06-14 00:56:20 -070071 hnd->offset, hnd->fd);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080072 if(err || mappedAddress == MAP_FAILED) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070073 ALOGE("Could not mmap handle %p, fd=%d (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070074 handle, hnd->fd, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070075 hnd->base = 0;
76 return -errno;
77 }
78
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079 hnd->base = intptr_t(mappedAddress) + hnd->offset;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080080 mappedAddress = MAP_FAILED;
81 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
82 err = memalloc->map_buffer(&mappedAddress, size,
83 hnd->offset_metadata, hnd->fd_metadata);
84 if(err || mappedAddress == MAP_FAILED) {
85 ALOGE("Could not mmap handle %p, fd=%d (%s)",
86 handle, hnd->fd_metadata, strerror(errno));
87 hnd->base_metadata = 0;
88 return -errno;
89 }
90 hnd->base_metadata = intptr_t(mappedAddress) + hnd->offset_metadata;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070091 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -070092 return 0;
93}
94
95static int gralloc_unmap(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -070096 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070097{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080098 if(!module)
99 return -EINVAL;
100
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101 private_handle_t* hnd = (private_handle_t*)handle;
102 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
103 int err = -EINVAL;
104 void* base = (void*)hnd->base;
105 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700106 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800107 if(memalloc != NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700108 err = memalloc->unmap_buffer(base, size, hnd->offset);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800109 if (err) {
110 ALOGE("Could not unmap memory at address %p", base);
111 }
112 base = (void*)hnd->base_metadata;
113 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
114 err = memalloc->unmap_buffer(base, size, hnd->offset_metadata);
115 if (err) {
116 ALOGE("Could not unmap memory at address %p", base);
117 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118 }
119 }
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800120 /* need to initialize the pointer to NULL otherwise unmapping for that
121 * buffer happens twice which leads to crash */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700122 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800123 hnd->base_metadata = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700124 return 0;
125}
126
127/*****************************************************************************/
128
129static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
130
131/*****************************************************************************/
132
133int gralloc_register_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700135{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800136 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700137 return -EINVAL;
138
139 // In this implementation, we don't need to do anything here
140
141 /* NOTE: we need to initialize the buffer as not mapped/not locked
142 * because it shouldn't when this function is called the first time
143 * in a new process. Ideally these flags shouldn't be part of the
144 * handle, but instead maintained in the kernel or at least
145 * out-of-line
146 */
147
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700148 private_handle_t* hnd = (private_handle_t*)handle;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700149 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800150 hnd->base_metadata = 0;
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400151 int err = gralloc_map(module, handle);
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700152 if (err) {
153 ALOGE("%s: gralloc_map failed", __FUNCTION__);
154 return err;
155 }
156
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700157 return 0;
158}
159
160int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700161 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800163 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164 return -EINVAL;
165
166 /*
167 * If the buffer has been mapped during a lock operation, it's time
168 * to un-map it. It's an error to be here with a locked buffer.
169 * NOTE: the framebuffer is handled differently and is never unmapped.
170 */
171
172 private_handle_t* hnd = (private_handle_t*)handle;
173
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700174 if (hnd->base != 0) {
175 gralloc_unmap(module, handle);
176 }
177 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800178 hnd->base_metadata = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700179 return 0;
180}
181
182int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800185 if(!module)
186 return -EINVAL;
187
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700188 /*
189 * If the buffer has been mapped during a lock operation, it's time
190 * to un-map it. It's an error to be here with a locked buffer.
191 */
192
193 if (hnd->base != 0) {
194 // this buffer was mapped, unmap it now
195 if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
196 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
197 private_handle_t::PRIV_FLAGS_USES_ASHMEM |
198 private_handle_t::PRIV_FLAGS_USES_ION)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700199 gralloc_unmap(module, hnd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700200 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700201 ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x",
202 hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700203 gralloc_unmap(module, hnd);
204 }
205 }
206
207 return 0;
208}
209
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400210static int gralloc_map_and_invalidate (gralloc_module_t const* module,
211 buffer_handle_t handle, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700212{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800213 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700214 return -EINVAL;
215
216 int err = 0;
217 private_handle_t* hnd = (private_handle_t*)handle;
218 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
219 if (hnd->base == 0) {
220 // we need to map for real
221 pthread_mutex_t* const lock = &sMapLock;
222 pthread_mutex_lock(lock);
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400223 err = gralloc_map(module, handle);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700224 pthread_mutex_unlock(lock);
225 }
Naseer Ahmedec147982013-06-14 17:06:46 -0400226 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
227 //Invalidate if reading in software. No need to do this for the
228 //metadata buffer as it is only read/written in software.
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 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
234 // Mark the buffer to be flushed after cpu read/write
235 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
236 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700237 }
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400238 } else {
239 hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700240 }
241 return err;
242}
243
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400244int 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 private_handle_t* hnd = (private_handle_t*)handle;
250 int err = gralloc_map_and_invalidate(module, handle, usage);
251 if(!err)
252 *vaddr = (void*)hnd->base;
253 return err;
254}
255
256int gralloc_lock_ycbcr(gralloc_module_t const* module,
257 buffer_handle_t handle, int usage,
258 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
259 struct android_ycbcr *ycbcr)
260{
261 private_handle_t* hnd = (private_handle_t*)handle;
262 int err = gralloc_map_and_invalidate(module, handle, usage);
263 size_t ystride, cstride;
264 if(!err) {
265 //hnd->format holds our implementation defined format
266 //HAL_PIXEL_FORMAT_YCrCb_420_SP is the only one set right now.
267 switch (hnd->format) {
268 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
269 ystride = ALIGN(hnd->width, 16);
270 cstride = ALIGN(hnd->width, 16)/2;
271 ycbcr->y = (void*)hnd->base;
272 ycbcr->cr = (void*)(hnd->base + ystride * hnd->height);
273 ycbcr->cb = (void*)(hnd->base + ystride * hnd->height + 1);
274 ycbcr->ystride = ystride;
275 ycbcr->cstride = cstride;
276 ycbcr->chroma_step = 2;
277 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved));
278 break;
279 default:
280 ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__,
281 hnd->format);
282 err = -EINVAL;
283 }
284 }
285 return err;
286}
287
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700288int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700289 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700290{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800291 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700292 return -EINVAL;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800293
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400294 int err = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700295 private_handle_t* hnd = (private_handle_t*)handle;
296
Naseer Ahmedec147982013-06-14 17:06:46 -0400297 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
Naseer Ahmed69662ac2013-08-28 13:16:48 -0400298 IMemAlloc* memalloc = getAllocator(hnd->flags);
Naseer Ahmedec147982013-06-14 17:06:46 -0400299 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
300 err = memalloc->clean_buffer((void*)hnd->base,
301 hnd->size, hnd->offset, hnd->fd,
302 CACHE_CLEAN_AND_INVALIDATE);
303 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
304 } else if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
305 hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
306 } else {
307 //Probably a round about way to do this, but this avoids adding new
308 //flags
309 err = memalloc->clean_buffer((void*)hnd->base,
310 hnd->size, hnd->offset, hnd->fd,
311 CACHE_INVALIDATE);
312 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700313 }
314
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400315 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700316}
317
318/*****************************************************************************/
319
320int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700321 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700322{
323 int res = -EINVAL;
324 va_list args;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800325 if(!module)
326 return res;
327
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700328 va_start(args, operation);
329 switch (operation) {
330 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
331 {
332 int fd = va_arg(args, int);
333 size_t size = va_arg(args, size_t);
334 size_t offset = va_arg(args, size_t);
335 void* base = va_arg(args, void*);
336 int width = va_arg(args, int);
337 int height = va_arg(args, int);
338 int format = va_arg(args, int);
339
340 native_handle_t** handle = va_arg(args, native_handle_t**);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700341 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700342 private_handle_t::sNumFds, private_handle_t::sNumInts);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343 hnd->magic = private_handle_t::sMagic;
344 hnd->fd = fd;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700345 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700346 hnd->size = size;
347 hnd->offset = offset;
348 hnd->base = intptr_t(base) + offset;
349 hnd->gpuaddr = 0;
350 hnd->width = width;
351 hnd->height = height;
352 hnd->format = format;
353 *handle = (native_handle_t *)hnd;
354 res = 0;
355 break;
356
357 }
Naseer Ahmed74214722013-02-09 08:11:36 -0500358#ifdef QCOM_BSP
Ramkumar Radhakrishnan935cb682012-11-07 16:43:38 -0800359 case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY:
360 {
361 int width = va_arg(args, int);
362 int height = va_arg(args, int);
363 int format = va_arg(args, int);
364 private_handle_t* hnd = va_arg(args, private_handle_t*);
365 if (private_handle_t::validate(hnd)) {
366 return res;
367 }
368 hnd->width = width;
369 hnd->height = height;
370 hnd->format = format;
371 res = 0;
372 }
373 break;
Naseer Ahmedc0593ea2013-03-18 11:46:24 -0400374#endif
Naomi Luisa44100c2013-02-08 12:42:03 -0800375 case GRALLOC_MODULE_PERFORM_GET_STRIDE:
376 {
377 int width = va_arg(args, int);
378 int format = va_arg(args, int);
379 int *stride = va_arg(args, int *);
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800380 int alignedw = 0, alignedh = 0;
381 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700382 0, format, false, alignedw, alignedh);
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800383 *stride = alignedw;
Naomi Luisa44100c2013-02-08 12:42:03 -0800384 res = 0;
385 } break;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700386
Naseer Ahmeda978f952013-09-26 14:36:28 -0400387 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE:
388 {
389 private_handle_t* hnd = va_arg(args, private_handle_t*);
390 int *stride = va_arg(args, int *);
391 if (private_handle_t::validate(hnd)) {
392 return res;
393 }
394 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
395 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
396 *stride = metadata->bufferDim.sliceWidth;
397 } else {
398 *stride = hnd->width;
399 }
400 res = 0;
401 } break;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700402
403 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES:
404 {
405 int width = va_arg(args, int);
406 int height = va_arg(args, int);
407 int format = va_arg(args, int);
408 int usage = va_arg(args, int);
409 int *alignedWidth = va_arg(args, int *);
410 int *alignedHeight = va_arg(args, int *);
411 int *tileEnabled = va_arg(args,int *);
412 *tileEnabled = isMacroTileEnabled(format, usage);
413 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
414 height, format, *tileEnabled, *alignedWidth,
415 *alignedHeight);
416 res = 0;
417 } break;
418
Shuzhen Wangc502c872014-01-28 16:10:42 -0800419 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE:
420 {
421 private_handle_t* hnd = va_arg(args, private_handle_t*);
422 int *color_space = va_arg(args, int *);
423 if (private_handle_t::validate(hnd)) {
424 return res;
425 }
426 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
427 if(metadata && metadata->operation & UPDATE_COLOR_SPACE) {
428 *color_space = metadata->colorSpace;
429 res = 0;
430 }
431 } break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700432 default:
433 break;
434 }
435 va_end(args);
436 return res;
437}