blob: 584737dcf40efa643701b43088774dcc78f83db1 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Saurabh Shahc5b2b702016-10-24 17:16:01 -07002 * Copyright (c) 2011-2017 The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303 * 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
Naseer Ahmed699b4572017-03-09 12:28:45 -050020#define DEBUG 0
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070021
Naseer Ahmeddc918132017-03-07 15:25:14 -050022#include <iomanip>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053023#include <utility>
Naseer Ahmede69031e2016-11-22 20:05:16 -050024#include <vector>
Naseer Ahmeddc918132017-03-07 15:25:14 -050025#include <sstream>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053026
27#include "qd_utils.h"
28#include "gr_priv_handle.h"
29#include "gr_buf_descriptor.h"
30#include "gr_utils.h"
31#include "gr_buf_mgr.h"
32#include "qdMetaData.h"
33
34namespace gralloc1 {
Naseer Ahmede69031e2016-11-22 20:05:16 -050035std::atomic<gralloc1_buffer_descriptor_t> BufferDescriptor::next_id_(1);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053036
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070037static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
38 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
39 descriptor.GetProducerUsage(), descriptor.GetConsumerUsage());
40}
41
Naseer Ahmede69031e2016-11-22 20:05:16 -050042BufferManager::BufferManager() : next_id_(0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053043 char property[PROPERTY_VALUE_MAX];
44
45 // Map framebuffer memory
46 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
47 (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
48 (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
49 map_fb_mem_ = true;
50 }
51
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053052 handles_map_.clear();
Naseer Ahmede69031e2016-11-22 20:05:16 -050053 allocator_ = new Allocator();
54 allocator_->Init();
55}
56
57
58gralloc1_error_t BufferManager::CreateBufferDescriptor(
59 gralloc1_buffer_descriptor_t *descriptor_id) {
Naseer Ahmed378d8582017-03-28 21:56:08 -040060 std::lock_guard<std::mutex> lock(descriptor_lock_);
Naseer Ahmede69031e2016-11-22 20:05:16 -050061 auto descriptor = std::make_shared<BufferDescriptor>();
62 descriptors_map_.emplace(descriptor->GetId(), descriptor);
63 *descriptor_id = descriptor->GetId();
64 return GRALLOC1_ERROR_NONE;
65}
66
67gralloc1_error_t BufferManager::DestroyBufferDescriptor(
68 gralloc1_buffer_descriptor_t descriptor_id) {
Naseer Ahmed378d8582017-03-28 21:56:08 -040069 std::lock_guard<std::mutex> lock(descriptor_lock_);
Naseer Ahmede69031e2016-11-22 20:05:16 -050070 const auto descriptor = descriptors_map_.find(descriptor_id);
71 if (descriptor == descriptors_map_.end()) {
72 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
73 }
74 descriptors_map_.erase(descriptor);
75 return GRALLOC1_ERROR_NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053076}
77
78BufferManager::~BufferManager() {
79 if (allocator_) {
80 delete allocator_;
81 }
82}
83
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053084gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -050085 const gralloc1_buffer_descriptor_t *descriptor_ids,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053086 buffer_handle_t *out_buffers) {
87 bool shared = true;
88 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
89
90 // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported
91 // client can ask to test the allocation by passing NULL out_buffers
92 bool test_allocate = !out_buffers;
93
Naseer Ahmede69031e2016-11-22 20:05:16 -050094 // Validate descriptors
Naseer Ahmed378d8582017-03-28 21:56:08 -040095 std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
Naseer Ahmede69031e2016-11-22 20:05:16 -050096 std::vector<std::shared_ptr<BufferDescriptor>> descriptors;
97 for (uint32_t i = 0; i < num_descriptors; i++) {
98 const auto map_descriptor = descriptors_map_.find(descriptor_ids[i]);
99 if (map_descriptor == descriptors_map_.end()) {
100 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
101 } else {
102 descriptors.push_back(map_descriptor->second);
103 }
104 }
105
106 // Resolve implementation defined formats
107 for (auto &descriptor : descriptors) {
108 descriptor->SetColorFormat(allocator_->GetImplDefinedFormat(descriptor->GetProducerUsage(),
109 descriptor->GetConsumerUsage(),
110 descriptor->GetFormat()));
111 }
112
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530113 // Check if input descriptors can be supported AND
114 // Find out if a single buffer can be shared for all the given input descriptors
115 uint32_t i = 0;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500116 ssize_t max_buf_index = -1;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530117 shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index);
118
119 if (test_allocate) {
120 status = shared ? GRALLOC1_ERROR_NOT_SHARED : status;
121 return status;
122 }
123
Naseer Ahmed378d8582017-03-28 21:56:08 -0400124 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530125 if (shared && (max_buf_index >= 0)) {
126 // Allocate one and duplicate/copy the handles for each descriptor
Naseer Ahmede69031e2016-11-22 20:05:16 -0500127 if (AllocateBuffer(*descriptors[UINT(max_buf_index)], &out_buffers[max_buf_index])) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530128 return GRALLOC1_ERROR_NO_RESOURCES;
129 }
130
131 for (i = 0; i < num_descriptors; i++) {
132 // Create new handle for a given descriptor.
133 // Current assumption is even MetaData memory would be same
134 // Need to revisit if there is a need for own metadata memory
135 if (i != UINT(max_buf_index)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500136 CreateSharedHandle(out_buffers[max_buf_index], *descriptors[i], &out_buffers[i]);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530137 }
138 }
139 } else {
140 // Buffer sharing is not feasible.
Naseer Ahmede69031e2016-11-22 20:05:16 -0500141 // Allocate separate buffer for each descriptor
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530142 for (i = 0; i < num_descriptors; i++) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500143 if (AllocateBuffer(*descriptors[i], &out_buffers[i])) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530144 return GRALLOC1_ERROR_NO_RESOURCES;
145 }
146 }
147 }
148
149 // Allocation is successful. If backstore is not shared inform the client.
150 if (!shared) {
151 return GRALLOC1_ERROR_NOT_SHARED;
152 }
153
154 return status;
155}
156
157void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
158 buffer_handle_t *outbuffer) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400159 // TODO(user): This path is not verified
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530160 private_handle_t const *input = reinterpret_cast<private_handle_t const *>(inbuffer);
161
162 // Get Buffer attributes or dimension
163 unsigned int alignedw = 0, alignedh = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700164 BufferInfo info = GetBufferInfo(descriptor);
165
166 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530167
168 // create new handle from input reference handle and given descriptor
169 int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(),
170 descriptor.GetConsumerUsage());
171 int buffer_type = GetBufferType(descriptor.GetFormat());
172
173 // Duplicate the fds
Saurabh Shah7d476ed2016-06-27 16:40:58 -0700174 // TODO(user): Not sure what to do for fb_id. Use duped fd and new dimensions?
Naseer Ahmede69031e2016-11-22 20:05:16 -0500175 private_handle_t *out_hnd = new private_handle_t(dup(input->fd),
176 dup(input->fd_metadata),
177 flags,
178 INT(alignedw),
179 INT(alignedh),
180 descriptor.GetWidth(),
181 descriptor.GetHeight(),
182 descriptor.GetFormat(),
183 buffer_type,
184 input->size,
185 descriptor.GetProducerUsage(),
186 descriptor.GetConsumerUsage());
187 out_hnd->id = ++next_id_;
188 // TODO(user): Base address of shared handle and ion handles
Naseer Ahmed378d8582017-03-28 21:56:08 -0400189 RegisterHandleLocked(out_hnd, -1, -1);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530190 *outbuffer = out_hnd;
191}
192
Naseer Ahmede69031e2016-11-22 20:05:16 -0500193gralloc1_error_t BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
194 auto hnd = buf->handle;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400195 ALOGD_IF(DEBUG, "FreeBuffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530196 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500197 hnd->fd, buf->ion_handle_main) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530198 return GRALLOC1_ERROR_BAD_HANDLE;
199 }
200
201 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
202 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500203 hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530204 return GRALLOC1_ERROR_BAD_HANDLE;
205 }
206
Naseer Ahmed29a86dd2017-03-16 14:09:46 -0400207 private_handle_t * handle = const_cast<private_handle_t *>(hnd);
208 handle->fd = -1;
209 handle->fd_metadata = -1;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -0400210 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
211 delete handle;
212 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530213 return GRALLOC1_ERROR_NONE;
214}
215
Naseer Ahmed378d8582017-03-28 21:56:08 -0400216void BufferManager::RegisterHandleLocked(const private_handle_t *hnd,
217 int ion_handle,
218 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400219 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
220 handles_map_.emplace(std::make_pair(hnd, buffer));
221}
222
Naseer Ahmed378d8582017-03-28 21:56:08 -0400223gralloc1_error_t BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400224 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400225 int ion_handle = allocator_->ImportBuffer(hnd->fd);
226 if (ion_handle < 0) {
227 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
228 return GRALLOC1_ERROR_BAD_HANDLE;
229 }
230 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
231 if (ion_handle_meta < 0) {
232 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd,
233 hnd->fd, hnd->id);
234 return GRALLOC1_ERROR_BAD_HANDLE;
235 }
236 // Set base pointers to NULL since the data here was received over binder
237 hnd->base = 0;
238 hnd->base_metadata = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400239 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400240 return GRALLOC1_ERROR_NONE;
241}
242
243std::shared_ptr<BufferManager::Buffer>
Naseer Ahmed378d8582017-03-28 21:56:08 -0400244BufferManager::GetBufferFromHandleLocked(const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400245 auto it = handles_map_.find(hnd);
246 if (it != handles_map_.end()) {
247 return it->second;
248 } else {
249 return nullptr;
250 }
251}
252
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530253gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
254 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400255 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530256
257 hnd->base = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530258 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
259 hnd->fd) != 0) {
260 return GRALLOC1_ERROR_BAD_HANDLE;
261 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530262 return GRALLOC1_ERROR_NONE;
263}
264
265gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500266 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400267 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400268 std::lock_guard<std::mutex> lock(buffer_lock_);
269 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400270 if (buf != nullptr) {
271 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530272 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400273 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400274 err = ImportHandleLocked(handle);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400275 if (err == GRALLOC1_ERROR_NONE) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400276 // TODO(user): See bug 35955598
277 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
278 return GRALLOC1_ERROR_NONE; // Don't map secure buffer
279 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400280 err = MapBuffer(hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530281 }
282 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400283 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530284}
285
286gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500287 ALOGD_IF(DEBUG, "Release buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400288 std::lock_guard<std::mutex> lock(buffer_lock_);
289 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400290 if (buf == nullptr) {
291 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530292 return GRALLOC1_ERROR_BAD_HANDLE;
293 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400294 if (buf->DecRef()) {
295 handles_map_.erase(hnd);
296 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500297 FreeBuffer(buf);
298 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530299 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530300 return GRALLOC1_ERROR_NONE;
301}
302
303gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd,
304 gralloc1_producer_usage_t prod_usage,
305 gralloc1_consumer_usage_t cons_usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400306 std::lock_guard<std::mutex> lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530307 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400308 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530309
310 // If buffer is not meant for CPU return err
311 if (!CpuCanAccess(prod_usage, cons_usage)) {
312 return GRALLOC1_ERROR_BAD_VALUE;
313 }
314
Naseer Ahmed378d8582017-03-28 21:56:08 -0400315 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400316 if (buf == nullptr) {
317 return GRALLOC1_ERROR_BAD_HANDLE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530318 }
319
Naseer Ahmed67330702017-05-02 15:00:26 -0400320 if (hnd->base == 0) {
321 // we need to map for real
322 err = MapBuffer(hnd);
323 }
324
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530325 // Invalidate if CPU reads in software and there are non-CPU
326 // writers. No need to do this for the metadata buffer as it is
327 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400328
329 // todo use handle here
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530330 if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
331 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
332 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400333 buf->ion_handle_main, CACHE_INVALIDATE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530334 return GRALLOC1_ERROR_BAD_HANDLE;
335 }
336 }
337
338 // Mark the buffer to be flushed after CPU write.
339 if (!err && CpuCanWrite(prod_usage)) {
340 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
341 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
342 }
343
344 return err;
345}
346
347gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400348 std::lock_guard<std::mutex> lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530349 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
350
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530351 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400352 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400353 if (buf == nullptr) {
354 return GRALLOC1_ERROR_BAD_HANDLE;
355 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530356
357 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
358 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400359 buf->ion_handle_main, CACHE_CLEAN) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530360 status = GRALLOC1_ERROR_BAD_HANDLE;
361 }
362 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
363 }
364
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530365 return status;
366}
367
Naseer Ahmede69031e2016-11-22 20:05:16 -0500368uint32_t BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530369 gralloc1_consumer_usage_t cons_usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500370 uint32_t align = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530371 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
372 align = 8192;
373 }
374
375 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700376 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) ||
377 (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530378 // The alignment here reflects qsee mmu V7L/V8L requirement
379 align = SZ_2M;
380 } else {
381 align = SECURE_ALIGN;
382 }
383 }
384
385 return align;
386}
387
388int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
389 gralloc1_consumer_usage_t cons_usage) {
390 int flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530391 if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
392 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
393 }
394
395 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
396 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
397 }
398
399 if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
400 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
401 }
402
403 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
404 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
405 }
406
407 if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
408 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
409 }
410
411 if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
412 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
413 }
414
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700415 if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530416 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
417 }
418
419 if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
420 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
421 }
422
423 // TODO(user): is this correct???
424 if ((cons_usage &
425 (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
426 (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
427 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
428 }
429
430 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
431 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
432 }
433
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400434 if (!allocator_->UseUncached(prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530435 flags |= private_handle_t::PRIV_FLAGS_CACHED;
436 }
437
438 return flags;
439}
440
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400441int BufferManager::GetBufferType(int inputFormat) {
442 int buffer_type = BUFFER_TYPE_VIDEO;
443 if (IsUncompressedRGBFormat(inputFormat)) {
444 // RGB formats
445 buffer_type = BUFFER_TYPE_UI;
446 }
447
448 return buffer_type;
449}
450
451int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
452 unsigned int bufferSize) {
453 if (!handle)
454 return -EINVAL;
455
456 int format = descriptor.GetFormat();
457 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
458 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400459 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400460
461 // Get implementation defined format
462 int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
463
464 unsigned int size;
465 unsigned int alignedw, alignedh;
466 int buffer_type = GetBufferType(gralloc_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700467 BufferInfo info = GetBufferInfo(descriptor);
468 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400469 size = (bufferSize >= size) ? bufferSize : size;
470
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530471 int err = 0;
472 int flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400473 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530474 AllocData data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500475 data.align = GetDataAlignment(format, prod_usage, cons_usage);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400476 size = ALIGN(size, data.align) * layer_count;
477 data.size = size;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400478 data.handle = (uintptr_t) handle;
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400479 data.uncached = allocator_->UseUncached(prod_usage, cons_usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500480
481 // Allocate buffer memory
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530482 err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
483 if (err) {
484 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530485 return err;
486 }
487
Naseer Ahmede69031e2016-11-22 20:05:16 -0500488 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530489 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500490 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530491 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500492 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530493
494 err =
495 allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500496 if (err) {
497 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
498 return err;
499 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530500
501 flags = GetHandleFlags(format, prod_usage, cons_usage);
502 flags |= data.alloc_type;
503
504 // Create handle
Naseer Ahmede69031e2016-11-22 20:05:16 -0500505 private_handle_t *hnd = new private_handle_t(data.fd,
506 e_data.fd,
507 flags,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400508 INT(alignedw),
509 INT(alignedh),
510 descriptor.GetWidth(),
511 descriptor.GetHeight(),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500512 format,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400513 buffer_type,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500514 size,
515 prod_usage,
516 cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530517
Naseer Ahmede69031e2016-11-22 20:05:16 -0500518 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400519 hnd->base = 0;
520 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400521 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530522
Naseer Ahmede69031e2016-11-22 20:05:16 -0500523 ColorSpace_t colorSpace = ITU_R_601;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530524 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530525 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400526 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500527 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
528 if (DEBUG) {
529 private_handle_t::Dump(hnd);
530 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530531 return err;
532}
533
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530534gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
535 switch (operation) {
536 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
537 int fd = va_arg(args, int);
538 unsigned int size = va_arg(args, unsigned int);
539 unsigned int offset = va_arg(args, unsigned int);
540 void *base = va_arg(args, void *);
541 int width = va_arg(args, int);
542 int height = va_arg(args, int);
543 int format = va_arg(args, int);
544
545 native_handle_t **handle = va_arg(args, native_handle_t **);
546 private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
547 native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
548 if (hnd) {
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700549 unsigned int alignedw = 0, alignedh = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530550 hnd->magic = private_handle_t::kMagic;
551 hnd->fd = fd;
552 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
553 hnd->size = size;
554 hnd->offset = offset;
555 hnd->base = uint64_t(base) + offset;
556 hnd->gpuaddr = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700557 BufferInfo info(width, height, format);
558 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700559 hnd->unaligned_width = width;
560 hnd->unaligned_height = height;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500561 hnd->width = INT(alignedw);
562 hnd->height = INT(alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530563 hnd->format = format;
564 *handle = reinterpret_cast<native_handle_t *>(hnd);
565 }
566 } break;
567
568 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
569 int width = va_arg(args, int);
570 int format = va_arg(args, int);
571 int *stride = va_arg(args, int *);
572 unsigned int alignedw = 0, alignedh = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700573 BufferInfo info(width, width, format);
574 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530575 *stride = INT(alignedw);
576 } break;
577
578 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
579 private_handle_t *hnd = va_arg(args, private_handle_t *);
580 int *stride = va_arg(args, int *);
581 if (private_handle_t::validate(hnd) != 0) {
582 return GRALLOC1_ERROR_BAD_HANDLE;
583 }
584
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400585 BufferDim_t buffer_dim;
586 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
587 *stride = buffer_dim.sliceWidth;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530588 } else {
589 *stride = hnd->width;
590 }
591 } break;
592
593 // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
594 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
595 private_handle_t *hnd = va_arg(args, private_handle_t *);
596 int *stride = va_arg(args, int *);
597 int *height = va_arg(args, int *);
598 if (private_handle_t::validate(hnd) != 0) {
599 return GRALLOC1_ERROR_BAD_HANDLE;
600 }
601
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400602 BufferDim_t buffer_dim;
Rohit Kulkarni70537e02017-06-16 12:01:13 -0700603 int interlaced = 0;
Ramkumar Radhakrishnan05747532017-07-07 16:38:28 -0700604
605 *stride = hnd->width;
606 *height = hnd->height;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400607 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
608 *stride = buffer_dim.sliceWidth;
609 *height = buffer_dim.sliceHeight;
Rohit Kulkarni70537e02017-06-16 12:01:13 -0700610 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
611 if (interlaced && IsUBwcFormat(hnd->format)) {
612 unsigned int alignedw = 0, alignedh = 0;
613 // Get re-aligned height for single ubwc interlaced field and
614 // multiple by 2 to get frame height.
615 BufferInfo info(hnd->width, ((hnd->height+1)>>1), hnd->format);
616 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
617 *stride = static_cast<int>(alignedw);
618 *height = static_cast<int>(alignedh * 2);
619 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530620 }
621 } break;
622
623 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
624 // TODO(user): Usage is split now. take care of it from Gfx client.
625 // see if we can directly expect descriptor from gfx client.
626 int width = va_arg(args, int);
627 int height = va_arg(args, int);
628 int format = va_arg(args, int);
629 uint64_t producer_usage = va_arg(args, uint64_t);
630 uint64_t consumer_usage = va_arg(args, uint64_t);
631 gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
632 gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
633
634 int *aligned_width = va_arg(args, int *);
635 int *aligned_height = va_arg(args, int *);
636 int *tile_enabled = va_arg(args, int *);
637 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700638 BufferInfo info(width, height, format, prod_usage, cons_usage);
639 *tile_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
640 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530641 *aligned_width = INT(alignedw);
642 *aligned_height = INT(alignedh);
643 } break;
644
645 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
646 private_handle_t *hnd = va_arg(args, private_handle_t *);
647 int *color_space = va_arg(args, int *);
648 if (private_handle_t::validate(hnd) != 0) {
649 return GRALLOC1_ERROR_BAD_HANDLE;
650 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400651 *color_space = 0;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700652#ifdef USE_COLOR_METADATA
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400653 ColorMetaData color_metadata;
654 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
655 switch (color_metadata.colorPrimaries) {
656 case ColorPrimaries_BT709_5:
657 *color_space = HAL_CSC_ITU_R_709;
658 break;
659 case ColorPrimaries_BT601_6_525:
660 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
661 break;
662 case ColorPrimaries_BT2020:
663 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
664 break;
665 default:
666 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
667 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700668 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400669 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530670 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400671 if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) {
672 *color_space = 0;
673 }
674#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530675 } break;
676 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
677 private_handle_t *hnd = va_arg(args, private_handle_t *);
678 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
679 if (private_handle_t::validate(hnd) != 0) {
680 return GRALLOC1_ERROR_BAD_HANDLE;
681 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700682 if (GetYUVPlaneInfo(hnd, ycbcr)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530683 return GRALLOC1_ERROR_UNDEFINED;
684 }
685 } break;
686
687 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
688 private_handle_t *hnd = va_arg(args, private_handle_t *);
689 int *map_secure_buffer = va_arg(args, int *);
690 if (private_handle_t::validate(hnd) != 0) {
691 return GRALLOC1_ERROR_BAD_HANDLE;
692 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400693
694 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530695 *map_secure_buffer = 0;
696 }
697 } break;
698
699 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
700 private_handle_t *hnd = va_arg(args, private_handle_t *);
701 int *flag = va_arg(args, int *);
702 if (private_handle_t::validate(hnd) != 0) {
703 return GRALLOC1_ERROR_BAD_HANDLE;
704 }
705 *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
Ramakant Singhc7d07792017-07-26 15:36:33 +0530706 int linear_format = 0;
707 if (getMetaData(hnd, GET_LINEAR_FORMAT, &linear_format) == 0) {
708 if (!linear_format) {
709 *flag = 0;
710 }
711 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530712 } break;
713
714 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
715 private_handle_t *hnd = va_arg(args, private_handle_t *);
716 void **rgb_data = va_arg(args, void **);
717 if (private_handle_t::validate(hnd) != 0) {
718 return GRALLOC1_ERROR_BAD_HANDLE;
719 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700720 if (GetRgbDataAddress(hnd, rgb_data)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530721 return GRALLOC1_ERROR_UNDEFINED;
722 }
723 } break;
724
Naseer Ahmede69031e2016-11-22 20:05:16 -0500725 case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: {
726 int width = va_arg(args, int);
727 int height = va_arg(args, int);
728 int format = va_arg(args, int);
729 uint64_t p_usage = va_arg(args, uint64_t);
730 uint64_t c_usage = va_arg(args, uint64_t);
731 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
732 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
733 uint32_t *aligned_width = va_arg(args, uint32_t *);
734 uint32_t *aligned_height = va_arg(args, uint32_t *);
735 uint32_t *size = va_arg(args, uint32_t *);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700736 auto info = BufferInfo(width, height, format, producer_usage, consumer_usage);
737 GetBufferSizeAndDimensions(info, size, aligned_width, aligned_height);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500738 // Align size
739 auto align = GetDataAlignment(format, producer_usage, consumer_usage);
740 *size = ALIGN(*size, align);
741 } break;
742
743 // TODO(user): Break out similar functionality, preferably moving to a common lib.
744
745 case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: {
746 int width = va_arg(args, int);
747 int height = va_arg(args, int);
748 int format = va_arg(args, int);
749 uint64_t p_usage = va_arg(args, uint64_t);
750 uint64_t c_usage = va_arg(args, uint64_t);
751 buffer_handle_t *hnd = va_arg(args, buffer_handle_t*);
752 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
753 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
754 BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage);
755 unsigned int size;
756 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700757 GetBufferSizeAndDimensions(GetBufferInfo(descriptor), &size, &alignedw, &alignedh);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500758 AllocateBuffer(descriptor, hnd, size);
759 } break;
760
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700761 case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
762 private_handle_t *hnd = va_arg(args, private_handle_t *);
763 int *flag = va_arg(args, int *);
764 if (private_handle_t::validate(hnd) != 0) {
765 return GRALLOC1_ERROR_BAD_HANDLE;
766 }
767 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
768 *flag = 0;
769 }
770 } break;
771
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530772 default:
773 break;
774 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530775 return GRALLOC1_ERROR_NONE;
776}
777
Naseer Ahmede69031e2016-11-22 20:05:16 -0500778static bool IsYuvFormat(const private_handle_t *hnd) {
779 switch (hnd->format) {
780 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
781 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
782 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
783 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
784 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
785 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
786 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
787 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
788 case HAL_PIXEL_FORMAT_NV21_ZSL:
789 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530790 case HAL_PIXEL_FORMAT_Y16:
Naseer Ahmed92998622017-03-20 12:39:17 -0400791 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500792 case HAL_PIXEL_FORMAT_RAW10:
793 case HAL_PIXEL_FORMAT_YV12:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530794 case HAL_PIXEL_FORMAT_Y8:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500795 return true;
796 default:
797 return false;
798 }
799}
800
801gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd,
802 uint32_t *out_num_planes) {
803 if (!IsYuvFormat(hnd)) {
804 return GRALLOC1_ERROR_UNSUPPORTED;
805 } else {
806 *out_num_planes = 3;
807 }
808 return GRALLOC1_ERROR_NONE;
809}
810
811gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd,
812 struct android_flex_layout *layout) {
813 if (!IsYuvFormat(hnd)) {
814 return GRALLOC1_ERROR_UNSUPPORTED;
815 }
816
817 android_ycbcr ycbcr;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700818 int err = GetYUVPlaneInfo(hnd, &ycbcr);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500819
820 if (err != 0) {
821 return GRALLOC1_ERROR_BAD_HANDLE;
822 }
823
824 layout->format = FLEX_FORMAT_YCbCr;
825 layout->num_planes = 3;
826
827 for (uint32_t i = 0; i < layout->num_planes; i++) {
828 layout->planes[i].bits_per_component = 8;
829 layout->planes[i].bits_used = 8;
830 layout->planes[i].h_increment = 1;
831 layout->planes[i].v_increment = 1;
832 layout->planes[i].h_subsampling = 2;
833 layout->planes[i].v_subsampling = 2;
834 }
835
836 layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
837 layout->planes[0].component = FLEX_COMPONENT_Y;
838 layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
839
840 layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
841 layout->planes[1].component = FLEX_COMPONENT_Cb;
842 layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
843 layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
844
845 layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
846 layout->planes[2].component = FLEX_COMPONENT_Cr;
847 layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
848 layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
849 return GRALLOC1_ERROR_NONE;
850}
Naseer Ahmeddc918132017-03-07 15:25:14 -0500851
852gralloc1_error_t BufferManager::Dump(std::ostringstream *os) {
853 for (auto it : handles_map_) {
854 auto buf = it.second;
855 auto hnd = buf->handle;
856 *os << "handle id: " << std::setw(4) << hnd->id;
857 *os << " fd: " << std::setw(3) << hnd->fd;
858 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
859 *os << " wxh: " << std::setw(4) << hnd->width <<" x " << std::setw(4) << hnd->height;
860 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
861 *os << std::setw(4) << hnd->unaligned_height;
862 *os << " size: " << std::setw(9) << hnd->size;
863 *os << std::hex << std::setfill('0');
864 *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags;
865 *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage;
866 *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage;
867 // TODO(user): get format string from qdutils
868 *os << " format: " << "0x" << std::setw(8) << hnd->format;
869 *os << std::dec << std::setfill(' ') << std::endl;
870 }
871 return GRALLOC1_ERROR_NONE;
872}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530873} // namespace gralloc1