blob: c3ae070dc8aec7f90d5f4bf367863554ca983541 [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 Ahmed29a26812012-06-14 00:56:20 -070059 buffer_handle_t handle,
60 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070061{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080062 if(!module)
63 return -EINVAL;
64
Iliyan Malchev202a77d2012-06-11 14:41:12 -070065 private_handle_t* hnd = (private_handle_t*)handle;
66 void *mappedAddress;
67 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
68 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
69 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070070 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 int err = memalloc->map_buffer(&mappedAddress, size,
Naseer Ahmed29a26812012-06-14 00:56:20 -070072 hnd->offset, hnd->fd);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080073 if(err || mappedAddress == MAP_FAILED) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070074 ALOGE("Could not mmap handle %p, fd=%d (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -070075 handle, hnd->fd, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076 hnd->base = 0;
77 return -errno;
78 }
79
Iliyan Malchev202a77d2012-06-11 14:41:12 -070080 hnd->base = intptr_t(mappedAddress) + hnd->offset;
Naseer Ahmed29a26812012-06-14 00:56:20 -070081 //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
Iliyan Malchev202a77d2012-06-11 14:41:12 -070082 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080083 mappedAddress = MAP_FAILED;
84 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
85 err = memalloc->map_buffer(&mappedAddress, size,
86 hnd->offset_metadata, hnd->fd_metadata);
87 if(err || mappedAddress == MAP_FAILED) {
88 ALOGE("Could not mmap handle %p, fd=%d (%s)",
89 handle, hnd->fd_metadata, strerror(errno));
90 hnd->base_metadata = 0;
91 return -errno;
92 }
93 hnd->base_metadata = intptr_t(mappedAddress) + hnd->offset_metadata;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094 }
95 *vaddr = (void*)hnd->base;
96 return 0;
97}
98
99static int gralloc_unmap(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700100 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800102 if(!module)
103 return -EINVAL;
104
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700105 private_handle_t* hnd = (private_handle_t*)handle;
106 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
107 int err = -EINVAL;
108 void* base = (void*)hnd->base;
109 size_t size = hnd->size;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700110 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800111 if(memalloc != NULL) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700112 err = memalloc->unmap_buffer(base, size, hnd->offset);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800113 if (err) {
114 ALOGE("Could not unmap memory at address %p", base);
115 }
116 base = (void*)hnd->base_metadata;
117 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
118 err = memalloc->unmap_buffer(base, size, hnd->offset_metadata);
119 if (err) {
120 ALOGE("Could not unmap memory at address %p", base);
121 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700122 }
123 }
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800124 /* need to initialize the pointer to NULL otherwise unmapping for that
125 * buffer happens twice which leads to crash */
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700126 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800127 hnd->base_metadata = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700128 return 0;
129}
130
131/*****************************************************************************/
132
133static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
134
135/*****************************************************************************/
136
137int gralloc_register_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700139{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800140 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700141 return -EINVAL;
142
143 // In this implementation, we don't need to do anything here
144
145 /* NOTE: we need to initialize the buffer as not mapped/not locked
146 * because it shouldn't when this function is called the first time
147 * in a new process. Ideally these flags shouldn't be part of the
148 * handle, but instead maintained in the kernel or at least
149 * out-of-line
150 */
151
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700152 private_handle_t* hnd = (private_handle_t*)handle;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700153 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800154 hnd->base_metadata = 0;
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700155 void *vaddr;
156 int err = gralloc_map(module, handle, &vaddr);
157 if (err) {
158 ALOGE("%s: gralloc_map failed", __FUNCTION__);
159 return err;
160 }
161
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700162 return 0;
163}
164
165int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700166 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700167{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800168 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700169 return -EINVAL;
170
171 /*
172 * If the buffer has been mapped during a lock operation, it's time
173 * to un-map it. It's an error to be here with a locked buffer.
174 * NOTE: the framebuffer is handled differently and is never unmapped.
175 */
176
177 private_handle_t* hnd = (private_handle_t*)handle;
178
Kinjal Bhavsar139e1622012-09-10 12:35:11 -0700179 if (hnd->base != 0) {
180 gralloc_unmap(module, handle);
181 }
182 hnd->base = 0;
Ramkumar Radhakrishnan7bf31c32013-01-28 22:51:51 -0800183 hnd->base_metadata = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184 return 0;
185}
186
187int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700188 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700189{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800190 if(!module)
191 return -EINVAL;
192
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 /*
194 * If the buffer has been mapped during a lock operation, it's time
195 * to un-map it. It's an error to be here with a locked buffer.
196 */
197
198 if (hnd->base != 0) {
199 // this buffer was mapped, unmap it now
200 if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM |
201 private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP |
202 private_handle_t::PRIV_FLAGS_USES_ASHMEM |
203 private_handle_t::PRIV_FLAGS_USES_ION)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 gralloc_unmap(module, hnd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700205 } else {
Naseer Ahmed29a26812012-06-14 00:56:20 -0700206 ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x",
207 hnd->flags);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700208 gralloc_unmap(module, hnd);
209 }
210 }
211
212 return 0;
213}
214
215int gralloc_lock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216 buffer_handle_t handle, int usage,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800217 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700218 void** vaddr)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700219{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800220 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700221 return -EINVAL;
222
223 int err = 0;
224 private_handle_t* hnd = (private_handle_t*)handle;
225 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
226 if (hnd->base == 0) {
227 // we need to map for real
228 pthread_mutex_t* const lock = &sMapLock;
229 pthread_mutex_lock(lock);
230 err = gralloc_map(module, handle, vaddr);
231 pthread_mutex_unlock(lock);
232 }
233 *vaddr = (void*)hnd->base;
Naseer Ahmedec147982013-06-14 17:06:46 -0400234 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
235 //Invalidate if reading in software. No need to do this for the
236 //metadata buffer as it is only read/written in software.
237 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
238 err = memalloc->clean_buffer((void*)hnd->base,
239 hnd->size, hnd->offset, hnd->fd,
240 CACHE_INVALIDATE);
241 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
242 // Mark the buffer to be flushed after cpu read/write
243 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
244 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700245 }
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400246 } else {
247 hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700248 }
249 return err;
250}
251
252int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700253 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700254{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800255 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700256 return -EINVAL;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800257
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400258 int err = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700259 private_handle_t* hnd = (private_handle_t*)handle;
260
Naseer Ahmedec147982013-06-14 17:06:46 -0400261 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
Naseer Ahmed69662ac2013-08-28 13:16:48 -0400262 IMemAlloc* memalloc = getAllocator(hnd->flags);
Naseer Ahmedec147982013-06-14 17:06:46 -0400263 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
264 err = memalloc->clean_buffer((void*)hnd->base,
265 hnd->size, hnd->offset, hnd->fd,
266 CACHE_CLEAN_AND_INVALIDATE);
267 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
268 } else if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
269 hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
270 } else {
271 //Probably a round about way to do this, but this avoids adding new
272 //flags
273 err = memalloc->clean_buffer((void*)hnd->base,
274 hnd->size, hnd->offset, hnd->fd,
275 CACHE_INVALIDATE);
276 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700277 }
278
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400279 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280}
281
282/*****************************************************************************/
283
284int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700286{
287 int res = -EINVAL;
288 va_list args;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800289 if(!module)
290 return res;
291
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700292 va_start(args, operation);
293 switch (operation) {
294 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
295 {
296 int fd = va_arg(args, int);
297 size_t size = va_arg(args, size_t);
298 size_t offset = va_arg(args, size_t);
299 void* base = va_arg(args, void*);
300 int width = va_arg(args, int);
301 int height = va_arg(args, int);
302 int format = va_arg(args, int);
303
304 native_handle_t** handle = va_arg(args, native_handle_t**);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700305 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Naseer Ahmed29a26812012-06-14 00:56:20 -0700306 private_handle_t::sNumFds, private_handle_t::sNumInts);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700307 hnd->magic = private_handle_t::sMagic;
308 hnd->fd = fd;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700309 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700310 hnd->size = size;
311 hnd->offset = offset;
312 hnd->base = intptr_t(base) + offset;
313 hnd->gpuaddr = 0;
314 hnd->width = width;
315 hnd->height = height;
316 hnd->format = format;
317 *handle = (native_handle_t *)hnd;
318 res = 0;
319 break;
320
321 }
Naseer Ahmed74214722013-02-09 08:11:36 -0500322#ifdef QCOM_BSP
Ramkumar Radhakrishnan935cb682012-11-07 16:43:38 -0800323 case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY:
324 {
325 int width = va_arg(args, int);
326 int height = va_arg(args, int);
327 int format = va_arg(args, int);
328 private_handle_t* hnd = va_arg(args, private_handle_t*);
329 if (private_handle_t::validate(hnd)) {
330 return res;
331 }
332 hnd->width = width;
333 hnd->height = height;
334 hnd->format = format;
335 res = 0;
336 }
337 break;
Naseer Ahmedc0593ea2013-03-18 11:46:24 -0400338#endif
Naomi Luisa44100c2013-02-08 12:42:03 -0800339 case GRALLOC_MODULE_PERFORM_GET_STRIDE:
340 {
341 int width = va_arg(args, int);
342 int format = va_arg(args, int);
343 int *stride = va_arg(args, int *);
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800344 int alignedw = 0, alignedh = 0;
345 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700346 0, format, false, alignedw, alignedh);
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800347 *stride = alignedw;
Naomi Luisa44100c2013-02-08 12:42:03 -0800348 res = 0;
349 } break;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700350
Naseer Ahmeda978f952013-09-26 14:36:28 -0400351 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE:
352 {
353 private_handle_t* hnd = va_arg(args, private_handle_t*);
354 int *stride = va_arg(args, int *);
355 if (private_handle_t::validate(hnd)) {
356 return res;
357 }
358 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
359 if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
360 *stride = metadata->bufferDim.sliceWidth;
361 } else {
362 *stride = hnd->width;
363 }
364 res = 0;
365 } break;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700366
367 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES:
368 {
369 int width = va_arg(args, int);
370 int height = va_arg(args, int);
371 int format = va_arg(args, int);
372 int usage = va_arg(args, int);
373 int *alignedWidth = va_arg(args, int *);
374 int *alignedHeight = va_arg(args, int *);
375 int *tileEnabled = va_arg(args,int *);
376 *tileEnabled = isMacroTileEnabled(format, usage);
377 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
378 height, format, *tileEnabled, *alignedWidth,
379 *alignedHeight);
380 res = 0;
381 } break;
382
Shuzhen Wangc502c872014-01-28 16:10:42 -0800383 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE:
384 {
385 private_handle_t* hnd = va_arg(args, private_handle_t*);
386 int *color_space = va_arg(args, int *);
387 if (private_handle_t::validate(hnd)) {
388 return res;
389 }
390 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
391 if(metadata && metadata->operation & UPDATE_COLOR_SPACE) {
392 *color_space = metadata->colorSpace;
393 res = 0;
394 }
395 } break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700396 default:
397 break;
398 }
399 va_end(args);
400 return res;
401}