blob: 132c768a14875567b752eec11de66b3bc3db006b [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Naseer Ahmeddebd5ce2016-05-03 15:25:02 -04003 * Copyright (c) 2011-2016, 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
Naseer Ahmed9eb5e092014-09-25 13:24:44 -040018#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070019#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>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070030
31#include <cutils/log.h>
32#include <cutils/atomic.h>
Saurabh Shahd4749de2014-09-10 18:04:31 -070033#include <utils/Trace.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070034
35#include <hardware/hardware.h>
36#include <hardware/gralloc.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070037
38#include "gralloc_priv.h"
39#include "gr.h"
40#include "alloc_controller.h"
41#include "memalloc.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080042#include <qdMetaData.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043
Saurabh Shahd4749de2014-09-10 18:04:31 -070044
Iliyan Malchev202a77d2012-06-11 14:41:12 -070045using 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
Naseer Ahmeddebd5ce2016-05-03 15:25:02 -040058static int gralloc_map_metadata(buffer_handle_t handle) {
59 private_handle_t* hnd = (private_handle_t*)handle;
60 hnd->base_metadata = 0;
61 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
62 void *mappedAddress = MAP_FAILED;
63 unsigned int size = 0;
64 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
65 mappedAddress = MAP_FAILED;
66 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
67 int ret = memalloc->map_buffer(&mappedAddress, size,
68 hnd->offset_metadata, hnd->fd_metadata);
69 if(ret || mappedAddress == MAP_FAILED) {
70 ALOGE("Could not mmap metadata for handle %p, fd=%d (%s)",
71 hnd, hnd->fd_metadata, strerror(errno));
72 return -errno;
73 }
74 hnd->base_metadata = uint64_t(mappedAddress) + hnd->offset_metadata;
75 }
76 return 0;
77}
78
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079static int gralloc_map(gralloc_module_t const* module,
Naseer Ahmed34d33bc2013-05-08 12:28:37 -040080 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -070081{
Saurabh Shahd4749de2014-09-10 18:04:31 -070082 ATRACE_CALL();
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080083 if(!module)
84 return -EINVAL;
85
Iliyan Malchev202a77d2012-06-11 14:41:12 -070086 private_handle_t* hnd = (private_handle_t*)handle;
Arun Kumar K.Rda2f69b2014-09-30 15:45:37 -070087 unsigned int size = 0;
88 int err = 0;
89 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
Saurabh Shahdbe41c52015-04-23 11:50:50 -070090 void *mappedAddress = MAP_FAILED;
91 hnd->base = 0;
Saurabh Shahdbe41c52015-04-23 11:50:50 -070092
93 // Dont map framebuffer and secure buffers
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) &&
95 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) {
Arun Kumar K.Rda2f69b2014-09-30 15:45:37 -070096 size = hnd->size;
97 err = memalloc->map_buffer(&mappedAddress, size,
Naseer Ahmed29a26812012-06-14 00:56:20 -070098 hnd->offset, hnd->fd);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080099 if(err || mappedAddress == MAP_FAILED) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700100 ALOGE("Could not mmap handle %p, fd=%d (%s)",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700101 handle, hnd->fd, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700102 return -errno;
103 }
104
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700105 hnd->base = uint64_t(mappedAddress) + hnd->offset;
Naseer Ahmeddebd5ce2016-05-03 15:25:02 -0400106 } else {
107 // Cannot map secure buffers or framebuffers, but still need to map
108 // metadata for secure buffers.
109 // If mapping a secure buffers fails, the framework needs to get
110 // an error code.
111 err = -EACCES;
Arun Kumar K.Rda2f69b2014-09-30 15:45:37 -0700112 }
113
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700114 //Allow mapping of metadata for all buffers including secure ones, but not
115 //of framebuffer
Naseer Ahmeddebd5ce2016-05-03 15:25:02 -0400116 int metadata_err = gralloc_map_metadata(handle);
117 if (!err) {
118 err = metadata_err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700119 }
Naseer Ahmeddebd5ce2016-05-03 15:25:02 -0400120 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700121}
122
123static int gralloc_unmap(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700124 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700125{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700126 ATRACE_CALL();
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700127 int err = -EINVAL;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800128 if(!module)
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700129 return err;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800130
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700131 private_handle_t* hnd = (private_handle_t*)handle;
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700132 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
133 if(!memalloc)
134 return err;
135
136 if(hnd->base) {
137 err = memalloc->unmap_buffer((void*)hnd->base, hnd->size, hnd->offset);
138 if (err) {
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500139 ALOGE("Could not unmap memory at address %p, %s", (void*) hnd->base,
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700140 strerror(errno));
141 return -errno;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700142 }
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700143 hnd->base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700144 }
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700145
146 if(hnd->base_metadata) {
147 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
148 err = memalloc->unmap_buffer((void*)hnd->base_metadata,
149 size, hnd->offset_metadata);
150 if (err) {
151 ALOGE("Could not unmap memory at address %p, %s",
Naseer Ahmedb8ecfbf2015-11-02 20:34:29 -0500152 (void*) hnd->base_metadata, strerror(errno));
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700153 return -errno;
154 }
155 hnd->base_metadata = 0;
156 }
157
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700158 return 0;
159}
160
161/*****************************************************************************/
162
163static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
164
165/*****************************************************************************/
166
167int gralloc_register_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700168 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700169{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700170 ATRACE_CALL();
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800171 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700172 return -EINVAL;
Naseer Ahmedf8d5f822016-08-02 11:28:13 -0400173
Naseer Ahmeddebd5ce2016-05-03 15:25:02 -0400174 int err = gralloc_map(module, handle);
175 /* Do not fail register_buffer for secure buffers*/
176 if (err == -EACCES)
177 err = 0;
178 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700179}
180
181int gralloc_unregister_buffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700182 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700183{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700184 ATRACE_CALL();
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800185 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700186 return -EINVAL;
187
188 /*
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 * NOTE: the framebuffer is handled differently and is never unmapped.
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700192 * Also base and base_metadata are reset.
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 */
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700194 return gralloc_unmap(module, handle);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195}
196
197int terminateBuffer(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700198 private_handle_t* hnd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700199{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700200 ATRACE_CALL();
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800201 if(!module)
202 return -EINVAL;
203
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 /*
205 * If the buffer has been mapped during a lock operation, it's time
206 * to un-map it. It's an error to be here with a locked buffer.
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700207 * NOTE: the framebuffer is handled differently and is never unmapped.
208 * Also base and base_metadata are reset.
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700209 */
Saurabh Shahdbe41c52015-04-23 11:50:50 -0700210 return gralloc_unmap(module, hnd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700211}
212
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400213static int gralloc_map_and_invalidate (gralloc_module_t const* module,
214 buffer_handle_t handle, int usage)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700215{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700216 ATRACE_CALL();
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800217 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700218 return -EINVAL;
219
220 int err = 0;
221 private_handle_t* hnd = (private_handle_t*)handle;
222 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
223 if (hnd->base == 0) {
224 // we need to map for real
225 pthread_mutex_t* const lock = &sMapLock;
226 pthread_mutex_lock(lock);
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400227 err = gralloc_map(module, handle);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700228 pthread_mutex_unlock(lock);
229 }
Saurabh Shah9ff53a92014-09-17 16:40:44 -0700230 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800231 hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) {
Saurabh Shah9ff53a92014-09-17 16:40:44 -0700232 //Invalidate if CPU reads in software and there are non-CPU
233 //writers. No need to do this for the metadata buffer as it is
234 //only read/written in software.
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800235 if ((usage & GRALLOC_USAGE_SW_READ_MASK) and
236 (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER))
237 {
Saurabh Shah9ff53a92014-09-17 16:40:44 -0700238 IMemAlloc* memalloc = getAllocator(hnd->flags) ;
239 err = memalloc->clean_buffer((void*)hnd->base,
240 hnd->size, hnd->offset, hnd->fd,
241 CACHE_INVALIDATE);
242 }
243 //Mark the buffer to be flushed after CPU write.
Naseer Ahmedec147982013-06-14 17:06:46 -0400244 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
Naseer Ahmedec147982013-06-14 17:06:46 -0400245 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
246 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700247 }
248 }
Saurabh Shah9ff53a92014-09-17 16:40:44 -0700249
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700250 return err;
251}
252
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400253int gralloc_lock(gralloc_module_t const* module,
254 buffer_handle_t handle, int usage,
255 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
256 void** vaddr)
257{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700258 ATRACE_CALL();
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400259 private_handle_t* hnd = (private_handle_t*)handle;
260 int err = gralloc_map_and_invalidate(module, handle, usage);
261 if(!err)
262 *vaddr = (void*)hnd->base;
263 return err;
264}
265
266int gralloc_lock_ycbcr(gralloc_module_t const* module,
267 buffer_handle_t handle, int usage,
268 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
269 struct android_ycbcr *ycbcr)
270{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700271 ATRACE_CALL();
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400272 private_handle_t* hnd = (private_handle_t*)handle;
273 int err = gralloc_map_and_invalidate(module, handle, usage);
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400274 if(!err)
275 err = getYUVPlaneInfo(hnd, ycbcr);
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400276 return err;
277}
278
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700279int gralloc_unlock(gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700280 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700281{
Saurabh Shahd4749de2014-09-10 18:04:31 -0700282 ATRACE_CALL();
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800283 if (!module || private_handle_t::validate(handle) < 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700284 return -EINVAL;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800285
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400286 int err = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287 private_handle_t* hnd = (private_handle_t*)handle;
288
Saurabh Shah9ff53a92014-09-17 16:40:44 -0700289 IMemAlloc* memalloc = getAllocator(hnd->flags);
290 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
291 err = memalloc->clean_buffer((void*)hnd->base,
292 hnd->size, hnd->offset, hnd->fd,
293 CACHE_CLEAN);
294 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700295 }
296
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400297 return err;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700298}
299
300/*****************************************************************************/
301
302int gralloc_perform(struct gralloc_module_t const* module,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700303 int operation, ... )
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304{
305 int res = -EINVAL;
306 va_list args;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800307 if(!module)
308 return res;
309
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700310 va_start(args, operation);
311 switch (operation) {
312 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER:
313 {
314 int fd = va_arg(args, int);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700315 unsigned int size = va_arg(args, unsigned int);
316 unsigned int offset = va_arg(args, unsigned int);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700317 void* base = va_arg(args, void*);
318 int width = va_arg(args, int);
319 int height = va_arg(args, int);
320 int format = va_arg(args, int);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700321 int alignedw = 0, alignedh = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700322
323 native_handle_t** handle = va_arg(args, native_handle_t**);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700324 private_handle_t* hnd = (private_handle_t*)native_handle_create(
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700325 private_handle_t::sNumFds, private_handle_t::sNumInts());
Ramakant Singh04b6b242015-03-23 15:01:12 +0530326 if (hnd) {
327 hnd->magic = private_handle_t::sMagic;
328 hnd->fd = fd;
329 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
330 hnd->size = size;
331 hnd->offset = offset;
332 hnd->base = uint64_t(base) + offset;
333 hnd->gpuaddr = 0;
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700334 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
335 height, format, 0, alignedw, alignedh);
336 hnd->width = alignedw;
337 hnd->height = alignedh;
338 hnd->unaligned_width = width;
339 hnd->unaligned_height = height;
Ramakant Singh04b6b242015-03-23 15:01:12 +0530340 hnd->format = format;
341 *handle = (native_handle_t *)hnd;
342 res = 0;
343 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700344 break;
345
346 }
Naomi Luisa44100c2013-02-08 12:42:03 -0800347 case GRALLOC_MODULE_PERFORM_GET_STRIDE:
348 {
349 int width = va_arg(args, int);
350 int format = va_arg(args, int);
351 int *stride = va_arg(args, int *);
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800352 int alignedw = 0, alignedh = 0;
353 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800354 0, format, 0, alignedw, alignedh);
Ramkumar Radhakrishnan473f4082013-11-04 14:29:18 -0800355 *stride = alignedw;
Naomi Luisa44100c2013-02-08 12:42:03 -0800356 res = 0;
357 } break;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700358
Naseer Ahmeda978f952013-09-26 14:36:28 -0400359 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE:
360 {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700361 const private_handle_t* hnd = va_arg(args, private_handle_t*);
Naseer Ahmeda978f952013-09-26 14:36:28 -0400362 int *stride = va_arg(args, int *);
363 if (private_handle_t::validate(hnd)) {
364 return res;
365 }
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700366
367 int alignedw = 0, alignedh = 0;
368 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh);
369 *stride = alignedw;
370
Naseer Ahmeda978f952013-09-26 14:36:28 -0400371 res = 0;
372 } break;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700373
Raj Kamal3d01d8d2014-03-04 05:25:33 +0530374 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE:
375 {
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700376 const private_handle_t* hnd = va_arg(args, private_handle_t*);
Raj Kamal3d01d8d2014-03-04 05:25:33 +0530377 int *stride = va_arg(args, int *);
378 int *height = va_arg(args, int *);
379 if (private_handle_t::validate(hnd)) {
380 return res;
381 }
Manoj Kumar AVM8e1aa182015-08-05 19:45:16 -0700382
383 int alignedw = 0, alignedh = 0;
384 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh);
385 *stride = alignedw;
386 *height = alignedh;
387
Raj Kamal3d01d8d2014-03-04 05:25:33 +0530388 res = 0;
389 } break;
390
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700391 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES:
392 {
393 int width = va_arg(args, int);
394 int height = va_arg(args, int);
395 int format = va_arg(args, int);
396 int usage = va_arg(args, int);
397 int *alignedWidth = va_arg(args, int *);
398 int *alignedHeight = va_arg(args, int *);
399 int *tileEnabled = va_arg(args,int *);
Sushil Chauhane61fac52015-04-30 17:14:37 -0700400 *tileEnabled = isUBwcEnabled(format, usage) ||
401 isMacroTileEnabled(format, usage);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700402 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
Sushil Chauhan65e26302015-01-14 10:48:57 -0800403 height, format, usage, *alignedWidth, *alignedHeight);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700404 res = 0;
405 } break;
406
Shuzhen Wangc502c872014-01-28 16:10:42 -0800407 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE:
408 {
409 private_handle_t* hnd = va_arg(args, private_handle_t*);
410 int *color_space = va_arg(args, int *);
411 if (private_handle_t::validate(hnd)) {
412 return res;
413 }
414 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700415 if (!metadata) {
416 break;
417#ifdef USE_COLOR_METADATA
418 } else if (metadata->operation & COLOR_METADATA) {
419 ColorMetaData *colorMetadata = &metadata->color;
420 res = 0;
421 switch (colorMetadata->colorPrimaries) {
422 case ColorPrimaries_BT709_5:
423 *color_space = HAL_CSC_ITU_R_709;
424 break;
425 case ColorPrimaries_BT601_6_525:
426 *color_space = ((colorMetadata->range) ?
427 HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
428 break;
429 case ColorPrimaries_BT2020:
430 *color_space = (colorMetadata->range) ?
431 HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
432 break;
433 default:
434 res = -EINVAL;
435 break;
436 }
437#endif
438 } else if(metadata->operation & UPDATE_COLOR_SPACE) {
Shuzhen Wangc502c872014-01-28 16:10:42 -0800439 *color_space = metadata->colorSpace;
440 res = 0;
441 }
442 } break;
Arun Kumar K.R563fbde2014-11-24 08:51:17 -0800443
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400444 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO:
445 {
446 private_handle_t* hnd = va_arg(args, private_handle_t*);
447 android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *);
Naseer Ahmedf5ff33a2014-04-30 15:30:39 -0400448 if (!private_handle_t::validate(hnd)) {
Naseer Ahmedb29fdfd2014-04-08 20:23:47 -0400449 res = getYUVPlaneInfo(hnd, ycbcr);
450 }
451 } break;
452
Arun Kumar K.R563fbde2014-11-24 08:51:17 -0800453 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO:
454 {
455 private_handle_t* hnd = va_arg(args, private_handle_t*);
456 int *map_secure_buffer = va_arg(args, int *);
457 if (private_handle_t::validate(hnd)) {
458 return res;
459 }
460 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
461 if(metadata && metadata->operation & MAP_SECURE_BUFFER) {
462 *map_secure_buffer = metadata->mapSecureBuffer;
463 res = 0;
464 } else {
465 *map_secure_buffer = 0;
466 }
467 } break;
468
Sushil Chauhana9d47002015-02-18 14:55:03 -0800469 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG:
470 {
471 private_handle_t* hnd = va_arg(args, private_handle_t*);
472 int *flag = va_arg(args, int *);
473 if (private_handle_t::validate(hnd)) {
474 return res;
475 }
476 *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700477 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
478 if (metadata && (metadata->operation & LINEAR_FORMAT)) {
479 *flag = 0;
480 }
Sushil Chauhana9d47002015-02-18 14:55:03 -0800481 res = 0;
482 } break;
483
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700484 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS:
485 {
486 private_handle_t* hnd = va_arg(args, private_handle_t*);
Sushil Chauhanc85b65b2015-04-30 11:05:36 -0700487 void** rgb_data = va_arg(args, void**);
Sushil Chauhan7dd3a432015-04-08 15:54:42 -0700488 if (!private_handle_t::validate(hnd)) {
489 res = getRgbDataAddress(hnd, rgb_data);
490 }
491 } break;
492
Dileep Marchya1a7e1f12015-09-25 19:11:57 -0700493 case GRALLOC_MODULE_PERFORM_GET_IGC:
494 {
495 private_handle_t* hnd = va_arg(args, private_handle_t*);
496 uint32_t *igc = va_arg(args, uint32_t *);
497 if (!private_handle_t::validate(hnd) && igc) {
498 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
499 if (metadata && (metadata->operation & SET_IGC)) {
500 *igc = metadata->igc;
501 res = 0;
502 }
503 }
504 } break;
505
506 case GRALLOC_MODULE_PERFORM_SET_IGC:
Dileep Marchyaa2129342016-01-05 18:15:57 -0800507 res = 0;
508 break;
Dileep Marchya1a7e1f12015-09-25 19:11:57 -0700509
Saurabh Shah95f83682015-10-16 10:30:04 -0700510 case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE:
511 {
512 private_handle_t* hnd = va_arg(args, private_handle_t*);
Saurabh Shahb8067a42015-11-06 16:52:02 -0800513 uint32_t *enable = va_arg(args, uint32_t*);
Sushil Chauhan76466212016-01-05 18:09:22 -0800514 if (!private_handle_t::validate(hnd)) {
515 setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable);
516 res = 0;
Saurabh Shah95f83682015-10-16 10:30:04 -0700517 }
Saurabh Shah95f83682015-10-16 10:30:04 -0700518 } break;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700519 default:
520 break;
521 }
522 va_end(args);
523 return res;
524}