blob: 9650583280ee84215dc7d08ddbf045d6faec35bf [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
2 * Copyright (c) 2011-2016 The Linux Foundation. All rights reserved.
3 * Not a Contribution
4 *
5 * Copyright (C) 2010 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include <utility>
21
22#include "qd_utils.h"
23#include "gr_priv_handle.h"
24#include "gr_buf_descriptor.h"
25#include "gr_utils.h"
26#include "gr_buf_mgr.h"
27#include "qdMetaData.h"
28
29namespace gralloc1 {
30
31BufferManager::BufferManager() {
32 char property[PROPERTY_VALUE_MAX];
33
34 // Map framebuffer memory
35 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
36 (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
37 (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
38 map_fb_mem_ = true;
39 }
40
41 // Enable UBWC for framebuffer
42 if ((property_get("debug.gralloc.enable_fb_ubwc", property, NULL) > 0) &&
43 (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
44 (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
45 ubwc_for_fb_ = true;
46 }
47
48 handles_map_.clear();
49}
50
51BufferManager::~BufferManager() {
52 if (allocator_) {
53 delete allocator_;
54 }
55}
56
57bool BufferManager::Init() {
58 allocator_ = new Allocator();
59
60 return allocator_->Init();
61}
62
63gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors,
64 const BufferDescriptor *descriptors,
65 buffer_handle_t *out_buffers) {
66 bool shared = true;
67 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
68
69 // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported
70 // client can ask to test the allocation by passing NULL out_buffers
71 bool test_allocate = !out_buffers;
72
73 // Check if input descriptors can be supported AND
74 // Find out if a single buffer can be shared for all the given input descriptors
75 uint32_t i = 0;
76 int max_buf_index = -1;
77 shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index);
78
79 if (test_allocate) {
80 status = shared ? GRALLOC1_ERROR_NOT_SHARED : status;
81 return status;
82 }
83
84 if (shared && (max_buf_index >= 0)) {
85 // Allocate one and duplicate/copy the handles for each descriptor
86 if (AllocateBuffer(descriptors[max_buf_index], &out_buffers[max_buf_index])) {
87 return GRALLOC1_ERROR_NO_RESOURCES;
88 }
89
90 for (i = 0; i < num_descriptors; i++) {
91 // Create new handle for a given descriptor.
92 // Current assumption is even MetaData memory would be same
93 // Need to revisit if there is a need for own metadata memory
94 if (i != UINT(max_buf_index)) {
95 CreateSharedHandle(out_buffers[max_buf_index], descriptors[i], &out_buffers[i]);
96
97 // since we just created handle out of existing handle add it to map
98 locker_.lock();
99 handles_map_.insert(std::pair<private_handle_t const *, int>(
100 reinterpret_cast<private_handle_t const *>(out_buffers[i]), 1));
101 locker_.unlock();
102 }
103 }
104 } else {
105 // Buffer sharing is not feasible.
106 // Allocate seperate buffer for each descriptor
107 for (i = 0; i < num_descriptors; i++) {
108 if (AllocateBuffer(descriptors[i], &out_buffers[i])) {
109 return GRALLOC1_ERROR_NO_RESOURCES;
110 }
111 }
112 }
113
114 // Allocation is successful. If backstore is not shared inform the client.
115 if (!shared) {
116 return GRALLOC1_ERROR_NOT_SHARED;
117 }
118
119 return status;
120}
121
122void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
123 buffer_handle_t *outbuffer) {
124 private_handle_t const *input = reinterpret_cast<private_handle_t const *>(inbuffer);
125
126 // Get Buffer attributes or dimension
127 unsigned int alignedw = 0, alignedh = 0;
128 allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
129
130 // create new handle from input reference handle and given descriptor
131 int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(),
132 descriptor.GetConsumerUsage());
133 int buffer_type = GetBufferType(descriptor.GetFormat());
134
135 // Duplicate the fds
136 private_handle_t *out_hnd = new private_handle_t(
137 dup(input->fd), input->size, flags, buffer_type, descriptor.GetFormat(), INT(alignedw),
138 INT(alignedh), dup(input->fd_metadata), input->offset_metadata, input->base_metadata,
139 descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetProducerUsage(),
140 descriptor.GetConsumerUsage());
141
142 *outbuffer = out_hnd;
143}
144
145gralloc1_error_t BufferManager::FreeBuffer(private_handle_t const *hnd) {
146 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
147 hnd->fd) != 0) {
148 return GRALLOC1_ERROR_BAD_HANDLE;
149 }
150
151 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
152 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
153 hnd->offset_metadata, hnd->fd_metadata) != 0) {
154 return GRALLOC1_ERROR_BAD_HANDLE;
155 }
156
157 // delete handle also
158 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
159 delete handle;
160
161 return GRALLOC1_ERROR_NONE;
162}
163
164gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
165 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
166
167 hnd->base = 0;
168 hnd->base_metadata = 0;
169 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
170 hnd->fd) != 0) {
171 return GRALLOC1_ERROR_BAD_HANDLE;
172 }
173
174 unsigned int size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
175 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base_metadata), size,
176 hnd->offset_metadata, hnd->fd_metadata) != 0) {
177 return GRALLOC1_ERROR_BAD_HANDLE;
178 }
179
180 return GRALLOC1_ERROR_NONE;
181}
182
183gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
184 locker_.lock();
185
186 // find if this handle is already in map
187 auto it = handles_map_.find(hnd);
188 if (it != handles_map_.end()) {
189 // It's already in map, Just increment refcnt
190 // No need to mmap the memory.
191 it->second = it->second + 1;
192 } else {
193 // not present in the map. mmap and then add entry to map
194 if (MapBuffer(hnd) == GRALLOC1_ERROR_NONE) {
195 handles_map_.insert(std::pair<private_handle_t const *, int>(hnd, 1));
196 }
197 }
198
199 locker_.unlock();
200 return GRALLOC1_ERROR_NONE;
201}
202
203gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
204 locker_.lock();
205
206 // find if this handle is already in map
207 auto it = handles_map_.find(hnd);
208 if (it == handles_map_.end()) {
209 // Corrupt handle or map.
210 locker_.unlock();
211 return GRALLOC1_ERROR_BAD_HANDLE;
212 } else {
213 it->second = it->second - 1;
214 }
215
216 if (!it->second) {
217 handles_map_.erase(it);
218 FreeBuffer(hnd);
219 }
220
221 locker_.unlock();
222 return GRALLOC1_ERROR_NONE;
223}
224
225gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd,
226 gralloc1_producer_usage_t prod_usage,
227 gralloc1_consumer_usage_t cons_usage) {
228 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
229
230 // If buffer is not meant for CPU return err
231 if (!CpuCanAccess(prod_usage, cons_usage)) {
232 return GRALLOC1_ERROR_BAD_VALUE;
233 }
234
235 if (hnd->base == 0) {
236 // we need to map for real
237 locker_.lock();
238 err = MapBuffer(hnd);
239 locker_.unlock();
240 }
241
242 // Invalidate if CPU reads in software and there are non-CPU
243 // writers. No need to do this for the metadata buffer as it is
244 // only read/written in software.
245 if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
246 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
247 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
248 hnd->fd, CACHE_INVALIDATE)) {
249 return GRALLOC1_ERROR_BAD_HANDLE;
250 }
251 }
252
253 // Mark the buffer to be flushed after CPU write.
254 if (!err && CpuCanWrite(prod_usage)) {
255 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
256 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
257 }
258
259 return err;
260}
261
262gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) {
263 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
264
265 locker_.lock();
266 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
267
268 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
269 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
270 hnd->fd, CACHE_CLEAN) != 0) {
271 status = GRALLOC1_ERROR_BAD_HANDLE;
272 }
273 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
274 }
275
276 locker_.unlock();
277 return status;
278}
279
280int BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
281 gralloc1_consumer_usage_t cons_usage) {
282 int align = getpagesize();
283 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
284 align = 8192;
285 }
286
287 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700288 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) ||
289 (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530290 // The alignment here reflects qsee mmu V7L/V8L requirement
291 align = SZ_2M;
292 } else {
293 align = SECURE_ALIGN;
294 }
295 }
296
297 return align;
298}
299
300int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
301 gralloc1_consumer_usage_t cons_usage) {
302 int flags = 0;
303 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY) {
304 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
305 }
306
307 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY) {
308 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
309 }
310
311 if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
312 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
313 }
314
315 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
316 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
317 }
318
319 if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
320 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
321 }
322
323 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
324 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
325 }
326
327 if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
328 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
329 }
330
331 if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
332 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
333 }
334
335 if (allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage)) {
336 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
337 }
338
339 if (allocator_->IsUBwcEnabled(format, prod_usage, cons_usage)) {
340 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
341 }
342
343 if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
344 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
345 }
346
347 // TODO(user): is this correct???
348 if ((cons_usage &
349 (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
350 (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
351 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
352 }
353
354 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
355 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
356 }
357
358 if (!allocator_->UseUncached(prod_usage)) {
359 flags |= private_handle_t::PRIV_FLAGS_CACHED;
360 }
361
362 return flags;
363}
364
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700365int BufferManager::AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
366 int unaligned_h, int format, int bufferType,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530367 gralloc1_producer_usage_t prod_usage,
368 gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle) {
369 int err = 0;
370 int flags = 0;
371 size = ALIGN(size, PAGE_SIZE);
372 AllocData data;
373 data.align = (unsigned int)GetDataAlignment(format, prod_usage, cons_usage);
374 size = ALIGN(size, data.align);
375 data.size = size;
376 data.handle = (uintptr_t)handle;
377
378 // Allocate memory
379 data.uncached = allocator_->UseUncached(prod_usage);
380 err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
381 if (err) {
382 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
383 *handle = 0;
384 return err;
385 }
386
387 // allocate memory for MetaData
388 AllocData e_data;
389 e_data.size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
390 e_data.handle = data.handle;
391 e_data.align = (unsigned int)getpagesize();
392
393 ColorSpace_t colorSpace = ITU_R_601;
394 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
395 colorSpace = ITU_R_601_FR;
396 }
397
398 err =
399 allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
400 ALOGE_IF(err, "gralloc failed for e_daata error=%s", strerror(-err));
401
402 flags = GetHandleFlags(format, prod_usage, cons_usage);
403 flags |= data.alloc_type;
404
405 // Create handle
406 uint64_t eBaseAddr = (uint64_t)(e_data.base) + e_data.offset;
407 private_handle_t *hnd = new private_handle_t(data.fd, size, flags, bufferType, format, aligned_w,
408 aligned_h, e_data.fd, e_data.offset, eBaseAddr,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700409 unaligned_w, unaligned_h, prod_usage, cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530410
411 hnd->offset = data.offset;
412 hnd->base = (uint64_t)(data.base) + data.offset;
413 hnd->gpuaddr = 0;
414
415 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
416
417 *handle = hnd;
418
419 // we have just allocated the buffer & mmapped. Add to map
420 locker_.lock();
421 handles_map_.insert(std::pair<private_handle_t const *, int>(hnd, 1));
422 locker_.unlock();
423
424 return err;
425}
426
427int BufferManager::GetBufferType(int inputFormat) {
428 int buffer_type = BUFFER_TYPE_VIDEO;
429 if (IsUncompressedRGBFormat(inputFormat)) {
430 // RGB formats
431 buffer_type = BUFFER_TYPE_UI;
432 }
433
434 return buffer_type;
435}
436
437int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
438 unsigned int bufferSize) {
439 if (!handle)
440 return -EINVAL;
441
442 int format = descriptor.GetFormat();
443 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
444 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
445
446 // Get implementation defined format
447 int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
448
449 bool use_fb_mem = false;
450 if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && map_fb_mem_) {
451 use_fb_mem = true;
452 }
453
454 if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && ubwc_for_fb_) {
455 prod_usage =
456 (gralloc1_producer_usage_t)(prod_usage | GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC);
457 }
458
459 unsigned int size;
460 unsigned int alignedw, alignedh;
461 int buffer_type = GetBufferType(gralloc_format);
462 allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh);
463
464 size = (bufferSize >= size) ? bufferSize : size;
465
466 int err = 0;
467 if (use_fb_mem) {
468 // TODO(user): TBD Framebuffer specific implementation in a seperate file/class
469 } else {
470 err = AllocateBuffer(size, INT(alignedw), INT(alignedh), descriptor.GetWidth(),
471 descriptor.GetHeight(), format, buffer_type, descriptor.GetProducerUsage(),
472 descriptor.GetConsumerUsage(), handle);
473 }
474
475 if (err < 0) {
476 return err;
477 }
478
479 return 0;
480}
481
482gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
483 switch (operation) {
484 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
485 int fd = va_arg(args, int);
486 unsigned int size = va_arg(args, unsigned int);
487 unsigned int offset = va_arg(args, unsigned int);
488 void *base = va_arg(args, void *);
489 int width = va_arg(args, int);
490 int height = va_arg(args, int);
491 int format = va_arg(args, int);
492
493 native_handle_t **handle = va_arg(args, native_handle_t **);
494 private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
495 native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
496 if (hnd) {
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700497 unsigned int alignedw = 0, alignedh = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530498 hnd->magic = private_handle_t::kMagic;
499 hnd->fd = fd;
500 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
501 hnd->size = size;
502 hnd->offset = offset;
503 hnd->base = uint64_t(base) + offset;
504 hnd->gpuaddr = 0;
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700505 BufferDescriptor descriptor(width, height, format);
506 allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
507 hnd->unaligned_width = width;
508 hnd->unaligned_height = height;
509 hnd->width = alignedw;
510 hnd->height = alignedh;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530511 hnd->format = format;
512 *handle = reinterpret_cast<native_handle_t *>(hnd);
513 }
514 } break;
515
516 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
517 int width = va_arg(args, int);
518 int format = va_arg(args, int);
519 int *stride = va_arg(args, int *);
520 unsigned int alignedw = 0, alignedh = 0;
521 BufferDescriptor descriptor(width, width, format);
522 allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
523 *stride = INT(alignedw);
524 } break;
525
526 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
527 private_handle_t *hnd = va_arg(args, private_handle_t *);
528 int *stride = va_arg(args, int *);
529 if (private_handle_t::validate(hnd) != 0) {
530 return GRALLOC1_ERROR_BAD_HANDLE;
531 }
532
533 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
534 if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
535 *stride = metadata->bufferDim.sliceWidth;
536 } else {
537 *stride = hnd->width;
538 }
539 } break;
540
541 // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
542 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
543 private_handle_t *hnd = va_arg(args, private_handle_t *);
544 int *stride = va_arg(args, int *);
545 int *height = va_arg(args, int *);
546 if (private_handle_t::validate(hnd) != 0) {
547 return GRALLOC1_ERROR_BAD_HANDLE;
548 }
549
550 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
551 if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) {
552 *stride = metadata->bufferDim.sliceWidth;
553 *height = metadata->bufferDim.sliceHeight;
554 } else {
555 *stride = hnd->width;
556 *height = hnd->height;
557 }
558 } break;
559
560 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
561 // TODO(user): Usage is split now. take care of it from Gfx client.
562 // see if we can directly expect descriptor from gfx client.
563 int width = va_arg(args, int);
564 int height = va_arg(args, int);
565 int format = va_arg(args, int);
566 uint64_t producer_usage = va_arg(args, uint64_t);
567 uint64_t consumer_usage = va_arg(args, uint64_t);
568 gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
569 gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
570
571 int *aligned_width = va_arg(args, int *);
572 int *aligned_height = va_arg(args, int *);
573 int *tile_enabled = va_arg(args, int *);
574 unsigned int alignedw, alignedh;
575 BufferDescriptor descriptor(width, height, format, prod_usage, cons_usage);
576 *tile_enabled = allocator_->IsUBwcEnabled(format, prod_usage, cons_usage) ||
577 allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage);
578
579 allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
580 *aligned_width = INT(alignedw);
581 *aligned_height = INT(alignedh);
582 } break;
583
584 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
585 private_handle_t *hnd = va_arg(args, private_handle_t *);
586 int *color_space = va_arg(args, int *);
587 if (private_handle_t::validate(hnd) != 0) {
588 return GRALLOC1_ERROR_BAD_HANDLE;
589 }
590 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700591 if (!metadata) {
592 return GRALLOC1_ERROR_BAD_HANDLE;
593#ifdef USE_COLOR_METADATA
594 } else if (metadata->operation & COLOR_METADATA) {
595 ColorMetaData *colorMetadata = &metadata->color;
596 switch (colorMetadata->colorPrimaries) {
597 case ColorPrimaries_BT709_5:
598 *color_space = HAL_CSC_ITU_R_709;
599 break;
600 case ColorPrimaries_BT601_6_525:
601 *color_space = ((colorMetadata->range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
602 break;
603 case ColorPrimaries_BT2020:
604 *color_space = (colorMetadata->range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
605 break;
606 default:
607 ALOGE("Unknown Color Space = %d", colorMetadata->colorPrimaries);
608 break;
609 }
610#endif
611 } else if (metadata->operation & UPDATE_COLOR_SPACE) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530612 *color_space = metadata->colorSpace;
613 }
614 } break;
615 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
616 private_handle_t *hnd = va_arg(args, private_handle_t *);
617 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
618 if (private_handle_t::validate(hnd) != 0) {
619 return GRALLOC1_ERROR_BAD_HANDLE;
620 }
621 if (allocator_->GetYUVPlaneInfo(hnd, ycbcr)) {
622 return GRALLOC1_ERROR_UNDEFINED;
623 }
624 } break;
625
626 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
627 private_handle_t *hnd = va_arg(args, private_handle_t *);
628 int *map_secure_buffer = va_arg(args, int *);
629 if (private_handle_t::validate(hnd) != 0) {
630 return GRALLOC1_ERROR_BAD_HANDLE;
631 }
632 MetaData_t *metadata = reinterpret_cast<MetaData_t *>(hnd->base_metadata);
633 if (metadata && metadata->operation & MAP_SECURE_BUFFER) {
634 *map_secure_buffer = metadata->mapSecureBuffer;
635 } else {
636 *map_secure_buffer = 0;
637 }
638 } break;
639
640 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
641 private_handle_t *hnd = va_arg(args, private_handle_t *);
642 int *flag = va_arg(args, int *);
643 if (private_handle_t::validate(hnd) != 0) {
644 return GRALLOC1_ERROR_BAD_HANDLE;
645 }
646 *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
647 } break;
648
649 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
650 private_handle_t *hnd = va_arg(args, private_handle_t *);
651 void **rgb_data = va_arg(args, void **);
652 if (private_handle_t::validate(hnd) != 0) {
653 return GRALLOC1_ERROR_BAD_HANDLE;
654 }
655 if (allocator_->GetRgbDataAddress(hnd, rgb_data)) {
656 return GRALLOC1_ERROR_UNDEFINED;
657 }
658 } break;
659
660 default:
661 break;
662 }
663
664 return GRALLOC1_ERROR_NONE;
665}
666
667} // namespace gralloc1