blob: 72ff9dc81260cac30c80f5b643b1149a1af69457 [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
52 // Enable UBWC for framebuffer
53 if ((property_get("debug.gralloc.enable_fb_ubwc", property, NULL) > 0) &&
54 (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
55 (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
56 ubwc_for_fb_ = true;
57 }
58
59 handles_map_.clear();
Naseer Ahmede69031e2016-11-22 20:05:16 -050060 allocator_ = new Allocator();
61 allocator_->Init();
62}
63
64
65gralloc1_error_t BufferManager::CreateBufferDescriptor(
66 gralloc1_buffer_descriptor_t *descriptor_id) {
Naseer Ahmed378d8582017-03-28 21:56:08 -040067 std::lock_guard<std::mutex> lock(descriptor_lock_);
Naseer Ahmede69031e2016-11-22 20:05:16 -050068 auto descriptor = std::make_shared<BufferDescriptor>();
69 descriptors_map_.emplace(descriptor->GetId(), descriptor);
70 *descriptor_id = descriptor->GetId();
71 return GRALLOC1_ERROR_NONE;
72}
73
74gralloc1_error_t BufferManager::DestroyBufferDescriptor(
75 gralloc1_buffer_descriptor_t descriptor_id) {
Naseer Ahmed378d8582017-03-28 21:56:08 -040076 std::lock_guard<std::mutex> lock(descriptor_lock_);
Naseer Ahmede69031e2016-11-22 20:05:16 -050077 const auto descriptor = descriptors_map_.find(descriptor_id);
78 if (descriptor == descriptors_map_.end()) {
79 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
80 }
81 descriptors_map_.erase(descriptor);
82 return GRALLOC1_ERROR_NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053083}
84
85BufferManager::~BufferManager() {
86 if (allocator_) {
87 delete allocator_;
88 }
89}
90
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053091gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -050092 const gralloc1_buffer_descriptor_t *descriptor_ids,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053093 buffer_handle_t *out_buffers) {
94 bool shared = true;
95 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
96
97 // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported
98 // client can ask to test the allocation by passing NULL out_buffers
99 bool test_allocate = !out_buffers;
100
Naseer Ahmede69031e2016-11-22 20:05:16 -0500101 // Validate descriptors
Naseer Ahmed378d8582017-03-28 21:56:08 -0400102 std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500103 std::vector<std::shared_ptr<BufferDescriptor>> descriptors;
104 for (uint32_t i = 0; i < num_descriptors; i++) {
105 const auto map_descriptor = descriptors_map_.find(descriptor_ids[i]);
106 if (map_descriptor == descriptors_map_.end()) {
107 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
108 } else {
109 descriptors.push_back(map_descriptor->second);
110 }
111 }
112
113 // Resolve implementation defined formats
114 for (auto &descriptor : descriptors) {
115 descriptor->SetColorFormat(allocator_->GetImplDefinedFormat(descriptor->GetProducerUsage(),
116 descriptor->GetConsumerUsage(),
117 descriptor->GetFormat()));
118 }
119
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530120 // Check if input descriptors can be supported AND
121 // Find out if a single buffer can be shared for all the given input descriptors
122 uint32_t i = 0;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500123 ssize_t max_buf_index = -1;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530124 shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index);
125
126 if (test_allocate) {
127 status = shared ? GRALLOC1_ERROR_NOT_SHARED : status;
128 return status;
129 }
130
Naseer Ahmed378d8582017-03-28 21:56:08 -0400131 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530132 if (shared && (max_buf_index >= 0)) {
133 // Allocate one and duplicate/copy the handles for each descriptor
Naseer Ahmede69031e2016-11-22 20:05:16 -0500134 if (AllocateBuffer(*descriptors[UINT(max_buf_index)], &out_buffers[max_buf_index])) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530135 return GRALLOC1_ERROR_NO_RESOURCES;
136 }
137
138 for (i = 0; i < num_descriptors; i++) {
139 // Create new handle for a given descriptor.
140 // Current assumption is even MetaData memory would be same
141 // Need to revisit if there is a need for own metadata memory
142 if (i != UINT(max_buf_index)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500143 CreateSharedHandle(out_buffers[max_buf_index], *descriptors[i], &out_buffers[i]);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530144 }
145 }
146 } else {
147 // Buffer sharing is not feasible.
Naseer Ahmede69031e2016-11-22 20:05:16 -0500148 // Allocate separate buffer for each descriptor
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530149 for (i = 0; i < num_descriptors; i++) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500150 if (AllocateBuffer(*descriptors[i], &out_buffers[i])) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530151 return GRALLOC1_ERROR_NO_RESOURCES;
152 }
153 }
154 }
155
156 // Allocation is successful. If backstore is not shared inform the client.
157 if (!shared) {
158 return GRALLOC1_ERROR_NOT_SHARED;
159 }
160
161 return status;
162}
163
164void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
165 buffer_handle_t *outbuffer) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400166 // TODO(user): This path is not verified
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530167 private_handle_t const *input = reinterpret_cast<private_handle_t const *>(inbuffer);
168
169 // Get Buffer attributes or dimension
170 unsigned int alignedw = 0, alignedh = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700171 BufferInfo info = GetBufferInfo(descriptor);
172
173 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530174
175 // create new handle from input reference handle and given descriptor
176 int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(),
177 descriptor.GetConsumerUsage());
178 int buffer_type = GetBufferType(descriptor.GetFormat());
179
180 // Duplicate the fds
Saurabh Shah7d476ed2016-06-27 16:40:58 -0700181 // TODO(user): Not sure what to do for fb_id. Use duped fd and new dimensions?
Naseer Ahmede69031e2016-11-22 20:05:16 -0500182 private_handle_t *out_hnd = new private_handle_t(dup(input->fd),
183 dup(input->fd_metadata),
184 flags,
185 INT(alignedw),
186 INT(alignedh),
187 descriptor.GetWidth(),
188 descriptor.GetHeight(),
189 descriptor.GetFormat(),
190 buffer_type,
191 input->size,
192 descriptor.GetProducerUsage(),
193 descriptor.GetConsumerUsage());
194 out_hnd->id = ++next_id_;
195 // TODO(user): Base address of shared handle and ion handles
Naseer Ahmed378d8582017-03-28 21:56:08 -0400196 RegisterHandleLocked(out_hnd, -1, -1);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530197 *outbuffer = out_hnd;
198}
199
Naseer Ahmede69031e2016-11-22 20:05:16 -0500200gralloc1_error_t BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
201 auto hnd = buf->handle;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400202 ALOGD_IF(DEBUG, "FreeBuffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530203 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500204 hnd->fd, buf->ion_handle_main) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530205 return GRALLOC1_ERROR_BAD_HANDLE;
206 }
207
208 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
209 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500210 hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530211 return GRALLOC1_ERROR_BAD_HANDLE;
212 }
213
Naseer Ahmede69031e2016-11-22 20:05:16 -0500214 // TODO(user): delete handle once framework bug around this is confirmed
Naseer Ahmed29a86dd2017-03-16 14:09:46 -0400215 // to be resolved. This is tracked in bug 36355756
216 private_handle_t * handle = const_cast<private_handle_t *>(hnd);
217 handle->fd = -1;
218 handle->fd_metadata = -1;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530219 return GRALLOC1_ERROR_NONE;
220}
221
Naseer Ahmed378d8582017-03-28 21:56:08 -0400222void BufferManager::RegisterHandleLocked(const private_handle_t *hnd,
223 int ion_handle,
224 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400225 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
226 handles_map_.emplace(std::make_pair(hnd, buffer));
227}
228
Naseer Ahmed378d8582017-03-28 21:56:08 -0400229gralloc1_error_t BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400230 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400231 int ion_handle = allocator_->ImportBuffer(hnd->fd);
232 if (ion_handle < 0) {
233 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
234 return GRALLOC1_ERROR_BAD_HANDLE;
235 }
236 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
237 if (ion_handle_meta < 0) {
238 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd,
239 hnd->fd, hnd->id);
240 return GRALLOC1_ERROR_BAD_HANDLE;
241 }
242 // Set base pointers to NULL since the data here was received over binder
243 hnd->base = 0;
244 hnd->base_metadata = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400245 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400246 return GRALLOC1_ERROR_NONE;
247}
248
249std::shared_ptr<BufferManager::Buffer>
Naseer Ahmed378d8582017-03-28 21:56:08 -0400250BufferManager::GetBufferFromHandleLocked(const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400251 auto it = handles_map_.find(hnd);
252 if (it != handles_map_.end()) {
253 return it->second;
254 } else {
255 return nullptr;
256 }
257}
258
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530259gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
260 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400261 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530262
263 hnd->base = 0;
264 hnd->base_metadata = 0;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400265
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530266 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
267 hnd->fd) != 0) {
268 return GRALLOC1_ERROR_BAD_HANDLE;
269 }
270
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400271 unsigned int size = ALIGN((unsigned int)sizeof(MetaData_t), getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530272 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base_metadata), size,
273 hnd->offset_metadata, hnd->fd_metadata) != 0) {
274 return GRALLOC1_ERROR_BAD_HANDLE;
275 }
276
277 return GRALLOC1_ERROR_NONE;
278}
279
280gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500281 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400282 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400283 std::lock_guard<std::mutex> lock(buffer_lock_);
284 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400285 if (buf != nullptr) {
286 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530287 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400288 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400289 err = ImportHandleLocked(handle);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400290 if (err == GRALLOC1_ERROR_NONE) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400291 // TODO(user): See bug 35955598
292 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
293 return GRALLOC1_ERROR_NONE; // Don't map secure buffer
294 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400295 err = MapBuffer(hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530296 }
297 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400298 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530299}
300
301gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500302 ALOGD_IF(DEBUG, "Release buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400303 std::lock_guard<std::mutex> lock(buffer_lock_);
304 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400305 if (buf == nullptr) {
306 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530307 return GRALLOC1_ERROR_BAD_HANDLE;
308 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400309 if (buf->DecRef()) {
310 handles_map_.erase(hnd);
311 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500312 FreeBuffer(buf);
313 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530314 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530315 return GRALLOC1_ERROR_NONE;
316}
317
318gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd,
319 gralloc1_producer_usage_t prod_usage,
320 gralloc1_consumer_usage_t cons_usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400321 std::lock_guard<std::mutex> lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530322 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400323 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530324
325 // If buffer is not meant for CPU return err
326 if (!CpuCanAccess(prod_usage, cons_usage)) {
327 return GRALLOC1_ERROR_BAD_VALUE;
328 }
329
330 if (hnd->base == 0) {
331 // we need to map for real
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530332 err = MapBuffer(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400333 }
334
Naseer Ahmed378d8582017-03-28 21:56:08 -0400335 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400336 if (buf == nullptr) {
337 return GRALLOC1_ERROR_BAD_HANDLE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530338 }
339
340 // Invalidate if CPU reads in software and there are non-CPU
341 // writers. No need to do this for the metadata buffer as it is
342 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400343
344 // todo use handle here
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530345 if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
346 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
347 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400348 buf->ion_handle_main, CACHE_INVALIDATE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530349 return GRALLOC1_ERROR_BAD_HANDLE;
350 }
351 }
352
353 // Mark the buffer to be flushed after CPU write.
354 if (!err && CpuCanWrite(prod_usage)) {
355 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
356 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
357 }
358
359 return err;
360}
361
362gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400363 std::lock_guard<std::mutex> lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530364 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
365
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530366 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400367 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400368 if (buf == nullptr) {
369 return GRALLOC1_ERROR_BAD_HANDLE;
370 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530371
372 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
373 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400374 buf->ion_handle_main, CACHE_CLEAN) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530375 status = GRALLOC1_ERROR_BAD_HANDLE;
376 }
377 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
378 }
379
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530380 return status;
381}
382
Naseer Ahmede69031e2016-11-22 20:05:16 -0500383uint32_t BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530384 gralloc1_consumer_usage_t cons_usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500385 uint32_t align = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530386 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
387 align = 8192;
388 }
389
390 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700391 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) ||
392 (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530393 // The alignment here reflects qsee mmu V7L/V8L requirement
394 align = SZ_2M;
395 } else {
396 align = SECURE_ALIGN;
397 }
398 }
399
400 return align;
401}
402
403int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
404 gralloc1_consumer_usage_t cons_usage) {
405 int flags = 0;
406 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY) {
407 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
408 }
409
410 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY) {
411 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
412 }
413
414 if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
415 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
416 }
417
418 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
419 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
420 }
421
422 if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
423 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
424 }
425
426 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
427 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
428 }
429
430 if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
431 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
432 }
433
434 if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
435 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
436 }
437
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700438 if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530439 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
440 }
441
442 if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
443 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
444 }
445
446 // TODO(user): is this correct???
447 if ((cons_usage &
448 (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
449 (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
450 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
451 }
452
453 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
454 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
455 }
456
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400457 if (!allocator_->UseUncached(prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530458 flags |= private_handle_t::PRIV_FLAGS_CACHED;
459 }
460
461 return flags;
462}
463
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400464int BufferManager::GetBufferType(int inputFormat) {
465 int buffer_type = BUFFER_TYPE_VIDEO;
466 if (IsUncompressedRGBFormat(inputFormat)) {
467 // RGB formats
468 buffer_type = BUFFER_TYPE_UI;
469 }
470
471 return buffer_type;
472}
473
474int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
475 unsigned int bufferSize) {
476 if (!handle)
477 return -EINVAL;
478
479 int format = descriptor.GetFormat();
480 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
481 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400482 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400483
484 // Get implementation defined format
485 int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
486
487 unsigned int size;
488 unsigned int alignedw, alignedh;
489 int buffer_type = GetBufferType(gralloc_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700490 BufferInfo info = GetBufferInfo(descriptor);
491 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400492 size = (bufferSize >= size) ? bufferSize : size;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400493 size = size * layer_count;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400494
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530495 int err = 0;
496 int flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400497 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530498 AllocData data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500499 data.align = GetDataAlignment(format, prod_usage, cons_usage);
500 data.size = ALIGN(size, data.align);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400501 data.handle = (uintptr_t) handle;
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400502 data.uncached = allocator_->UseUncached(prod_usage, cons_usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500503
504 // Allocate buffer memory
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530505 err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
506 if (err) {
507 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530508 return err;
509 }
510
Naseer Ahmede69031e2016-11-22 20:05:16 -0500511 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530512 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500513 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530514 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500515 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530516
517 err =
518 allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500519 if (err) {
520 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
521 return err;
522 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530523
524 flags = GetHandleFlags(format, prod_usage, cons_usage);
525 flags |= data.alloc_type;
526
527 // Create handle
Naseer Ahmede69031e2016-11-22 20:05:16 -0500528 private_handle_t *hnd = new private_handle_t(data.fd,
529 e_data.fd,
530 flags,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400531 INT(alignedw),
532 INT(alignedh),
533 descriptor.GetWidth(),
534 descriptor.GetHeight(),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500535 format,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400536 buffer_type,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500537 size,
538 prod_usage,
539 cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530540
Naseer Ahmede69031e2016-11-22 20:05:16 -0500541 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400542 hnd->base = 0;
543 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400544 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530545
Naseer Ahmede69031e2016-11-22 20:05:16 -0500546 ColorSpace_t colorSpace = ITU_R_601;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530547 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530548 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400549 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500550 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
551 if (DEBUG) {
552 private_handle_t::Dump(hnd);
553 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530554 return err;
555}
556
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530557gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
558 switch (operation) {
559 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
560 int fd = va_arg(args, int);
561 unsigned int size = va_arg(args, unsigned int);
562 unsigned int offset = va_arg(args, unsigned int);
563 void *base = va_arg(args, void *);
564 int width = va_arg(args, int);
565 int height = va_arg(args, int);
566 int format = va_arg(args, int);
567
568 native_handle_t **handle = va_arg(args, native_handle_t **);
569 private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
570 native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
571 if (hnd) {
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700572 unsigned int alignedw = 0, alignedh = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530573 hnd->magic = private_handle_t::kMagic;
574 hnd->fd = fd;
575 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
576 hnd->size = size;
577 hnd->offset = offset;
578 hnd->base = uint64_t(base) + offset;
579 hnd->gpuaddr = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700580 BufferInfo info(width, height, format);
581 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700582 hnd->unaligned_width = width;
583 hnd->unaligned_height = height;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500584 hnd->width = INT(alignedw);
585 hnd->height = INT(alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530586 hnd->format = format;
587 *handle = reinterpret_cast<native_handle_t *>(hnd);
588 }
589 } break;
590
591 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
592 int width = va_arg(args, int);
593 int format = va_arg(args, int);
594 int *stride = va_arg(args, int *);
595 unsigned int alignedw = 0, alignedh = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700596 BufferInfo info(width, width, format);
597 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530598 *stride = INT(alignedw);
599 } break;
600
601 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
602 private_handle_t *hnd = va_arg(args, private_handle_t *);
603 int *stride = va_arg(args, int *);
604 if (private_handle_t::validate(hnd) != 0) {
605 return GRALLOC1_ERROR_BAD_HANDLE;
606 }
607
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400608 BufferDim_t buffer_dim;
609 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
610 *stride = buffer_dim.sliceWidth;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530611 } else {
612 *stride = hnd->width;
613 }
614 } break;
615
616 // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
617 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
618 private_handle_t *hnd = va_arg(args, private_handle_t *);
619 int *stride = va_arg(args, int *);
620 int *height = va_arg(args, int *);
621 if (private_handle_t::validate(hnd) != 0) {
622 return GRALLOC1_ERROR_BAD_HANDLE;
623 }
624
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400625 BufferDim_t buffer_dim;
626 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
627 *stride = buffer_dim.sliceWidth;
628 *height = buffer_dim.sliceHeight;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530629 } else {
630 *stride = hnd->width;
631 *height = hnd->height;
632 }
633 } break;
634
635 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
636 // TODO(user): Usage is split now. take care of it from Gfx client.
637 // see if we can directly expect descriptor from gfx client.
638 int width = va_arg(args, int);
639 int height = va_arg(args, int);
640 int format = va_arg(args, int);
641 uint64_t producer_usage = va_arg(args, uint64_t);
642 uint64_t consumer_usage = va_arg(args, uint64_t);
643 gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
644 gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
645
646 int *aligned_width = va_arg(args, int *);
647 int *aligned_height = va_arg(args, int *);
648 int *tile_enabled = va_arg(args, int *);
649 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700650 BufferInfo info(width, height, format, prod_usage, cons_usage);
651 *tile_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
652 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530653 *aligned_width = INT(alignedw);
654 *aligned_height = INT(alignedh);
655 } break;
656
657 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
658 private_handle_t *hnd = va_arg(args, private_handle_t *);
659 int *color_space = va_arg(args, int *);
660 if (private_handle_t::validate(hnd) != 0) {
661 return GRALLOC1_ERROR_BAD_HANDLE;
662 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400663 *color_space = 0;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700664#ifdef USE_COLOR_METADATA
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400665 ColorMetaData color_metadata;
666 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
667 switch (color_metadata.colorPrimaries) {
668 case ColorPrimaries_BT709_5:
669 *color_space = HAL_CSC_ITU_R_709;
670 break;
671 case ColorPrimaries_BT601_6_525:
672 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
673 break;
674 case ColorPrimaries_BT2020:
675 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
676 break;
677 default:
678 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
679 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700680 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400681 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530682 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400683 if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) {
684 *color_space = 0;
685 }
686#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530687 } break;
688 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
689 private_handle_t *hnd = va_arg(args, private_handle_t *);
690 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
691 if (private_handle_t::validate(hnd) != 0) {
692 return GRALLOC1_ERROR_BAD_HANDLE;
693 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700694 if (GetYUVPlaneInfo(hnd, ycbcr)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530695 return GRALLOC1_ERROR_UNDEFINED;
696 }
697 } break;
698
699 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
700 private_handle_t *hnd = va_arg(args, private_handle_t *);
701 int *map_secure_buffer = va_arg(args, int *);
702 if (private_handle_t::validate(hnd) != 0) {
703 return GRALLOC1_ERROR_BAD_HANDLE;
704 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400705
706 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530707 *map_secure_buffer = 0;
708 }
709 } break;
710
711 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
712 private_handle_t *hnd = va_arg(args, private_handle_t *);
713 int *flag = va_arg(args, int *);
714 if (private_handle_t::validate(hnd) != 0) {
715 return GRALLOC1_ERROR_BAD_HANDLE;
716 }
717 *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
718 } break;
719
720 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
721 private_handle_t *hnd = va_arg(args, private_handle_t *);
722 void **rgb_data = va_arg(args, void **);
723 if (private_handle_t::validate(hnd) != 0) {
724 return GRALLOC1_ERROR_BAD_HANDLE;
725 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700726 if (GetRgbDataAddress(hnd, rgb_data)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530727 return GRALLOC1_ERROR_UNDEFINED;
728 }
729 } break;
730
Naseer Ahmede69031e2016-11-22 20:05:16 -0500731 case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: {
732 int width = va_arg(args, int);
733 int height = va_arg(args, int);
734 int format = va_arg(args, int);
735 uint64_t p_usage = va_arg(args, uint64_t);
736 uint64_t c_usage = va_arg(args, uint64_t);
737 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
738 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
739 uint32_t *aligned_width = va_arg(args, uint32_t *);
740 uint32_t *aligned_height = va_arg(args, uint32_t *);
741 uint32_t *size = va_arg(args, uint32_t *);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700742 auto info = BufferInfo(width, height, format, producer_usage, consumer_usage);
743 GetBufferSizeAndDimensions(info, size, aligned_width, aligned_height);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500744 // Align size
745 auto align = GetDataAlignment(format, producer_usage, consumer_usage);
746 *size = ALIGN(*size, align);
747 } break;
748
749 // TODO(user): Break out similar functionality, preferably moving to a common lib.
750
751 case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: {
752 int width = va_arg(args, int);
753 int height = va_arg(args, int);
754 int format = va_arg(args, int);
755 uint64_t p_usage = va_arg(args, uint64_t);
756 uint64_t c_usage = va_arg(args, uint64_t);
757 buffer_handle_t *hnd = va_arg(args, buffer_handle_t*);
758 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
759 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
760 BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage);
761 unsigned int size;
762 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700763 GetBufferSizeAndDimensions(GetBufferInfo(descriptor), &size, &alignedw, &alignedh);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500764 AllocateBuffer(descriptor, hnd, size);
765 } break;
766
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700767 case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
768 private_handle_t *hnd = va_arg(args, private_handle_t *);
769 int *flag = va_arg(args, int *);
770 if (private_handle_t::validate(hnd) != 0) {
771 return GRALLOC1_ERROR_BAD_HANDLE;
772 }
773 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
774 *flag = 0;
775 }
776 } break;
777
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530778 default:
779 break;
780 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530781 return GRALLOC1_ERROR_NONE;
782}
783
Naseer Ahmede69031e2016-11-22 20:05:16 -0500784static bool IsYuvFormat(const private_handle_t *hnd) {
785 switch (hnd->format) {
786 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
787 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
788 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
789 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
790 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
791 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
792 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
793 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
794 case HAL_PIXEL_FORMAT_NV21_ZSL:
795 case HAL_PIXEL_FORMAT_RAW16:
Naseer Ahmed92998622017-03-20 12:39:17 -0400796 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500797 case HAL_PIXEL_FORMAT_RAW10:
798 case HAL_PIXEL_FORMAT_YV12:
799 return true;
800 default:
801 return false;
802 }
803}
804
805gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd,
806 uint32_t *out_num_planes) {
807 if (!IsYuvFormat(hnd)) {
808 return GRALLOC1_ERROR_UNSUPPORTED;
809 } else {
810 *out_num_planes = 3;
811 }
812 return GRALLOC1_ERROR_NONE;
813}
814
815gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd,
816 struct android_flex_layout *layout) {
817 if (!IsYuvFormat(hnd)) {
818 return GRALLOC1_ERROR_UNSUPPORTED;
819 }
820
821 android_ycbcr ycbcr;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700822 int err = GetYUVPlaneInfo(hnd, &ycbcr);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500823
824 if (err != 0) {
825 return GRALLOC1_ERROR_BAD_HANDLE;
826 }
827
828 layout->format = FLEX_FORMAT_YCbCr;
829 layout->num_planes = 3;
830
831 for (uint32_t i = 0; i < layout->num_planes; i++) {
832 layout->planes[i].bits_per_component = 8;
833 layout->planes[i].bits_used = 8;
834 layout->planes[i].h_increment = 1;
835 layout->planes[i].v_increment = 1;
836 layout->planes[i].h_subsampling = 2;
837 layout->planes[i].v_subsampling = 2;
838 }
839
840 layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
841 layout->planes[0].component = FLEX_COMPONENT_Y;
842 layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
843
844 layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
845 layout->planes[1].component = FLEX_COMPONENT_Cb;
846 layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
847 layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
848
849 layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
850 layout->planes[2].component = FLEX_COMPONENT_Cr;
851 layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
852 layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
853 return GRALLOC1_ERROR_NONE;
854}
Naseer Ahmeddc918132017-03-07 15:25:14 -0500855
856gralloc1_error_t BufferManager::Dump(std::ostringstream *os) {
857 for (auto it : handles_map_) {
858 auto buf = it.second;
859 auto hnd = buf->handle;
860 *os << "handle id: " << std::setw(4) << hnd->id;
861 *os << " fd: " << std::setw(3) << hnd->fd;
862 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
863 *os << " wxh: " << std::setw(4) << hnd->width <<" x " << std::setw(4) << hnd->height;
864 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
865 *os << std::setw(4) << hnd->unaligned_height;
866 *os << " size: " << std::setw(9) << hnd->size;
867 *os << std::hex << std::setfill('0');
868 *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags;
869 *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage;
870 *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage;
871 // TODO(user): get format string from qdutils
872 *os << " format: " << "0x" << std::setw(8) << hnd->format;
873 *os << std::dec << std::setfill(' ') << std::endl;
874 }
875 return GRALLOC1_ERROR_NONE;
876}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530877} // namespace gralloc1