blob: 5390fee88b0b2256f12d3699b66dbe97f7c8998a [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 Ahmed29a86dd2017-03-16 14:09:46 -0400214 private_handle_t * handle = const_cast<private_handle_t *>(hnd);
215 handle->fd = -1;
216 handle->fd_metadata = -1;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -0400217 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
218 delete handle;
219 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530220 return GRALLOC1_ERROR_NONE;
221}
222
Naseer Ahmed378d8582017-03-28 21:56:08 -0400223void BufferManager::RegisterHandleLocked(const private_handle_t *hnd,
224 int ion_handle,
225 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400226 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
227 handles_map_.emplace(std::make_pair(hnd, buffer));
228}
229
Naseer Ahmed378d8582017-03-28 21:56:08 -0400230gralloc1_error_t BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400231 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400232 int ion_handle = allocator_->ImportBuffer(hnd->fd);
233 if (ion_handle < 0) {
234 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
235 return GRALLOC1_ERROR_BAD_HANDLE;
236 }
237 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
238 if (ion_handle_meta < 0) {
239 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd,
240 hnd->fd, hnd->id);
241 return GRALLOC1_ERROR_BAD_HANDLE;
242 }
243 // Set base pointers to NULL since the data here was received over binder
244 hnd->base = 0;
245 hnd->base_metadata = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400246 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400247 return GRALLOC1_ERROR_NONE;
248}
249
250std::shared_ptr<BufferManager::Buffer>
Naseer Ahmed378d8582017-03-28 21:56:08 -0400251BufferManager::GetBufferFromHandleLocked(const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400252 auto it = handles_map_.find(hnd);
253 if (it != handles_map_.end()) {
254 return it->second;
255 } else {
256 return nullptr;
257 }
258}
259
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530260gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) {
261 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400262 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530263
264 hnd->base = 0;
265 hnd->base_metadata = 0;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400266
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530267 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
268 hnd->fd) != 0) {
269 return GRALLOC1_ERROR_BAD_HANDLE;
270 }
271
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400272 unsigned int size = ALIGN((unsigned int)sizeof(MetaData_t), getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530273 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base_metadata), size,
274 hnd->offset_metadata, hnd->fd_metadata) != 0) {
275 return GRALLOC1_ERROR_BAD_HANDLE;
276 }
277
278 return GRALLOC1_ERROR_NONE;
279}
280
281gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500282 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400283 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400284 std::lock_guard<std::mutex> lock(buffer_lock_);
285 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400286 if (buf != nullptr) {
287 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530288 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400289 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400290 err = ImportHandleLocked(handle);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400291 if (err == GRALLOC1_ERROR_NONE) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400292 // TODO(user): See bug 35955598
293 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
294 return GRALLOC1_ERROR_NONE; // Don't map secure buffer
295 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400296 err = MapBuffer(hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530297 }
298 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400299 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530300}
301
302gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500303 ALOGD_IF(DEBUG, "Release buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400304 std::lock_guard<std::mutex> lock(buffer_lock_);
305 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400306 if (buf == nullptr) {
307 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530308 return GRALLOC1_ERROR_BAD_HANDLE;
309 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400310 if (buf->DecRef()) {
311 handles_map_.erase(hnd);
312 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500313 FreeBuffer(buf);
314 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530315 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530316 return GRALLOC1_ERROR_NONE;
317}
318
319gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd,
320 gralloc1_producer_usage_t prod_usage,
321 gralloc1_consumer_usage_t cons_usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400322 std::lock_guard<std::mutex> lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530323 gralloc1_error_t err = GRALLOC1_ERROR_NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400324 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530325
326 // If buffer is not meant for CPU return err
327 if (!CpuCanAccess(prod_usage, cons_usage)) {
328 return GRALLOC1_ERROR_BAD_VALUE;
329 }
330
Naseer Ahmed378d8582017-03-28 21:56:08 -0400331 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400332 if (buf == nullptr) {
333 return GRALLOC1_ERROR_BAD_HANDLE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530334 }
335
Naseer Ahmed67330702017-05-02 15:00:26 -0400336 if (hnd->base == 0) {
337 // we need to map for real
338 err = MapBuffer(hnd);
339 }
340
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530341 // Invalidate if CPU reads in software and there are non-CPU
342 // writers. No need to do this for the metadata buffer as it is
343 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400344
345 // todo use handle here
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530346 if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
347 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
348 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400349 buf->ion_handle_main, CACHE_INVALIDATE)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530350 return GRALLOC1_ERROR_BAD_HANDLE;
351 }
352 }
353
354 // Mark the buffer to be flushed after CPU write.
355 if (!err && CpuCanWrite(prod_usage)) {
356 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
357 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
358 }
359
360 return err;
361}
362
363gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400364 std::lock_guard<std::mutex> lock(buffer_lock_);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530365 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
366
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530367 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400368 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400369 if (buf == nullptr) {
370 return GRALLOC1_ERROR_BAD_HANDLE;
371 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530372
373 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
374 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400375 buf->ion_handle_main, CACHE_CLEAN) != 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530376 status = GRALLOC1_ERROR_BAD_HANDLE;
377 }
378 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
379 }
380
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530381 return status;
382}
383
Naseer Ahmede69031e2016-11-22 20:05:16 -0500384uint32_t BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530385 gralloc1_consumer_usage_t cons_usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500386 uint32_t align = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530387 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
388 align = 8192;
389 }
390
391 if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700392 if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) ||
393 (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530394 // The alignment here reflects qsee mmu V7L/V8L requirement
395 align = SZ_2M;
396 } else {
397 align = SECURE_ALIGN;
398 }
399 }
400
401 return align;
402}
403
404int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
405 gralloc1_consumer_usage_t cons_usage) {
406 int flags = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530407 if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
408 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
409 }
410
411 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
412 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
413 }
414
415 if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
416 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
417 }
418
419 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
420 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
421 }
422
423 if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
424 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
425 }
426
427 if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
428 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
429 }
430
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700431 if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530432 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
433 }
434
435 if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
436 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
437 }
438
439 // TODO(user): is this correct???
440 if ((cons_usage &
441 (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
442 (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
443 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
444 }
445
446 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
447 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
448 }
449
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400450 if (!allocator_->UseUncached(prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530451 flags |= private_handle_t::PRIV_FLAGS_CACHED;
452 }
453
454 return flags;
455}
456
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400457int BufferManager::GetBufferType(int inputFormat) {
458 int buffer_type = BUFFER_TYPE_VIDEO;
459 if (IsUncompressedRGBFormat(inputFormat)) {
460 // RGB formats
461 buffer_type = BUFFER_TYPE_UI;
462 }
463
464 return buffer_type;
465}
466
467int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
468 unsigned int bufferSize) {
469 if (!handle)
470 return -EINVAL;
471
472 int format = descriptor.GetFormat();
473 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
474 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400475 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400476
477 // Get implementation defined format
478 int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
479
480 unsigned int size;
481 unsigned int alignedw, alignedh;
482 int buffer_type = GetBufferType(gralloc_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700483 BufferInfo info = GetBufferInfo(descriptor);
484 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400485 size = (bufferSize >= size) ? bufferSize : size;
486
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530487 int err = 0;
488 int flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400489 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530490 AllocData data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500491 data.align = GetDataAlignment(format, prod_usage, cons_usage);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400492 size = ALIGN(size, data.align) * layer_count;
493 data.size = size;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400494 data.handle = (uintptr_t) handle;
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400495 data.uncached = allocator_->UseUncached(prod_usage, cons_usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500496
497 // Allocate buffer memory
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530498 err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
499 if (err) {
500 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530501 return err;
502 }
503
Naseer Ahmede69031e2016-11-22 20:05:16 -0500504 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530505 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500506 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530507 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500508 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530509
510 err =
511 allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500512 if (err) {
513 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
514 return err;
515 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530516
517 flags = GetHandleFlags(format, prod_usage, cons_usage);
518 flags |= data.alloc_type;
519
520 // Create handle
Naseer Ahmede69031e2016-11-22 20:05:16 -0500521 private_handle_t *hnd = new private_handle_t(data.fd,
522 e_data.fd,
523 flags,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400524 INT(alignedw),
525 INT(alignedh),
526 descriptor.GetWidth(),
527 descriptor.GetHeight(),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500528 format,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400529 buffer_type,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500530 size,
531 prod_usage,
532 cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530533
Naseer Ahmede69031e2016-11-22 20:05:16 -0500534 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400535 hnd->base = 0;
536 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400537 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530538
Naseer Ahmede69031e2016-11-22 20:05:16 -0500539 ColorSpace_t colorSpace = ITU_R_601;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530540 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530541 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400542 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500543 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
544 if (DEBUG) {
545 private_handle_t::Dump(hnd);
546 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530547 return err;
548}
549
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530550gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
551 switch (operation) {
552 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
553 int fd = va_arg(args, int);
554 unsigned int size = va_arg(args, unsigned int);
555 unsigned int offset = va_arg(args, unsigned int);
556 void *base = va_arg(args, void *);
557 int width = va_arg(args, int);
558 int height = va_arg(args, int);
559 int format = va_arg(args, int);
560
561 native_handle_t **handle = va_arg(args, native_handle_t **);
562 private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
563 native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
564 if (hnd) {
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700565 unsigned int alignedw = 0, alignedh = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530566 hnd->magic = private_handle_t::kMagic;
567 hnd->fd = fd;
568 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
569 hnd->size = size;
570 hnd->offset = offset;
571 hnd->base = uint64_t(base) + offset;
572 hnd->gpuaddr = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700573 BufferInfo info(width, height, format);
574 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700575 hnd->unaligned_width = width;
576 hnd->unaligned_height = height;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500577 hnd->width = INT(alignedw);
578 hnd->height = INT(alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530579 hnd->format = format;
580 *handle = reinterpret_cast<native_handle_t *>(hnd);
581 }
582 } break;
583
584 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
585 int width = va_arg(args, int);
586 int format = va_arg(args, int);
587 int *stride = va_arg(args, int *);
588 unsigned int alignedw = 0, alignedh = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700589 BufferInfo info(width, width, format);
590 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530591 *stride = INT(alignedw);
592 } break;
593
594 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
595 private_handle_t *hnd = va_arg(args, private_handle_t *);
596 int *stride = va_arg(args, int *);
597 if (private_handle_t::validate(hnd) != 0) {
598 return GRALLOC1_ERROR_BAD_HANDLE;
599 }
600
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400601 BufferDim_t buffer_dim;
602 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
603 *stride = buffer_dim.sliceWidth;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530604 } else {
605 *stride = hnd->width;
606 }
607 } break;
608
609 // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
610 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
611 private_handle_t *hnd = va_arg(args, private_handle_t *);
612 int *stride = va_arg(args, int *);
613 int *height = va_arg(args, int *);
614 if (private_handle_t::validate(hnd) != 0) {
615 return GRALLOC1_ERROR_BAD_HANDLE;
616 }
617
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400618 BufferDim_t buffer_dim;
Rohit Kulkarni70537e02017-06-16 12:01:13 -0700619 int interlaced = 0;
Ramkumar Radhakrishnan05747532017-07-07 16:38:28 -0700620
621 *stride = hnd->width;
622 *height = hnd->height;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400623 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
624 *stride = buffer_dim.sliceWidth;
625 *height = buffer_dim.sliceHeight;
Rohit Kulkarni70537e02017-06-16 12:01:13 -0700626 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
627 if (interlaced && IsUBwcFormat(hnd->format)) {
628 unsigned int alignedw = 0, alignedh = 0;
629 // Get re-aligned height for single ubwc interlaced field and
630 // multiple by 2 to get frame height.
631 BufferInfo info(hnd->width, ((hnd->height+1)>>1), hnd->format);
632 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
633 *stride = static_cast<int>(alignedw);
634 *height = static_cast<int>(alignedh * 2);
635 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530636 }
637 } break;
638
639 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
640 // TODO(user): Usage is split now. take care of it from Gfx client.
641 // see if we can directly expect descriptor from gfx client.
642 int width = va_arg(args, int);
643 int height = va_arg(args, int);
644 int format = va_arg(args, int);
645 uint64_t producer_usage = va_arg(args, uint64_t);
646 uint64_t consumer_usage = va_arg(args, uint64_t);
647 gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
648 gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
649
650 int *aligned_width = va_arg(args, int *);
651 int *aligned_height = va_arg(args, int *);
652 int *tile_enabled = va_arg(args, int *);
653 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700654 BufferInfo info(width, height, format, prod_usage, cons_usage);
655 *tile_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
656 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530657 *aligned_width = INT(alignedw);
658 *aligned_height = INT(alignedh);
659 } break;
660
661 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
662 private_handle_t *hnd = va_arg(args, private_handle_t *);
663 int *color_space = va_arg(args, int *);
664 if (private_handle_t::validate(hnd) != 0) {
665 return GRALLOC1_ERROR_BAD_HANDLE;
666 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400667 *color_space = 0;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700668#ifdef USE_COLOR_METADATA
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400669 ColorMetaData color_metadata;
670 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
671 switch (color_metadata.colorPrimaries) {
672 case ColorPrimaries_BT709_5:
673 *color_space = HAL_CSC_ITU_R_709;
674 break;
675 case ColorPrimaries_BT601_6_525:
676 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
677 break;
678 case ColorPrimaries_BT2020:
679 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
680 break;
681 default:
682 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
683 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700684 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400685 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530686 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400687 if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) {
688 *color_space = 0;
689 }
690#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530691 } break;
692 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
693 private_handle_t *hnd = va_arg(args, private_handle_t *);
694 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
695 if (private_handle_t::validate(hnd) != 0) {
696 return GRALLOC1_ERROR_BAD_HANDLE;
697 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700698 if (GetYUVPlaneInfo(hnd, ycbcr)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530699 return GRALLOC1_ERROR_UNDEFINED;
700 }
701 } break;
702
703 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
704 private_handle_t *hnd = va_arg(args, private_handle_t *);
705 int *map_secure_buffer = va_arg(args, int *);
706 if (private_handle_t::validate(hnd) != 0) {
707 return GRALLOC1_ERROR_BAD_HANDLE;
708 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400709
710 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530711 *map_secure_buffer = 0;
712 }
713 } break;
714
715 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
716 private_handle_t *hnd = va_arg(args, private_handle_t *);
717 int *flag = va_arg(args, int *);
718 if (private_handle_t::validate(hnd) != 0) {
719 return GRALLOC1_ERROR_BAD_HANDLE;
720 }
721 *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
722 } break;
723
724 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
725 private_handle_t *hnd = va_arg(args, private_handle_t *);
726 void **rgb_data = va_arg(args, void **);
727 if (private_handle_t::validate(hnd) != 0) {
728 return GRALLOC1_ERROR_BAD_HANDLE;
729 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700730 if (GetRgbDataAddress(hnd, rgb_data)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530731 return GRALLOC1_ERROR_UNDEFINED;
732 }
733 } break;
734
Naseer Ahmede69031e2016-11-22 20:05:16 -0500735 case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: {
736 int width = va_arg(args, int);
737 int height = va_arg(args, int);
738 int format = va_arg(args, int);
739 uint64_t p_usage = va_arg(args, uint64_t);
740 uint64_t c_usage = va_arg(args, uint64_t);
741 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
742 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
743 uint32_t *aligned_width = va_arg(args, uint32_t *);
744 uint32_t *aligned_height = va_arg(args, uint32_t *);
745 uint32_t *size = va_arg(args, uint32_t *);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700746 auto info = BufferInfo(width, height, format, producer_usage, consumer_usage);
747 GetBufferSizeAndDimensions(info, size, aligned_width, aligned_height);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500748 // Align size
749 auto align = GetDataAlignment(format, producer_usage, consumer_usage);
750 *size = ALIGN(*size, align);
751 } break;
752
753 // TODO(user): Break out similar functionality, preferably moving to a common lib.
754
755 case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: {
756 int width = va_arg(args, int);
757 int height = va_arg(args, int);
758 int format = va_arg(args, int);
759 uint64_t p_usage = va_arg(args, uint64_t);
760 uint64_t c_usage = va_arg(args, uint64_t);
761 buffer_handle_t *hnd = va_arg(args, buffer_handle_t*);
762 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
763 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
764 BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage);
765 unsigned int size;
766 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700767 GetBufferSizeAndDimensions(GetBufferInfo(descriptor), &size, &alignedw, &alignedh);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500768 AllocateBuffer(descriptor, hnd, size);
769 } break;
770
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700771 case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
772 private_handle_t *hnd = va_arg(args, private_handle_t *);
773 int *flag = va_arg(args, int *);
774 if (private_handle_t::validate(hnd) != 0) {
775 return GRALLOC1_ERROR_BAD_HANDLE;
776 }
777 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
778 *flag = 0;
779 }
780 } break;
781
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530782 default:
783 break;
784 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530785 return GRALLOC1_ERROR_NONE;
786}
787
Naseer Ahmede69031e2016-11-22 20:05:16 -0500788static bool IsYuvFormat(const private_handle_t *hnd) {
789 switch (hnd->format) {
790 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
791 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
792 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
793 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
794 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
795 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
796 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
797 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
798 case HAL_PIXEL_FORMAT_NV21_ZSL:
799 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530800 case HAL_PIXEL_FORMAT_Y16:
Naseer Ahmed92998622017-03-20 12:39:17 -0400801 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500802 case HAL_PIXEL_FORMAT_RAW10:
803 case HAL_PIXEL_FORMAT_YV12:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530804 case HAL_PIXEL_FORMAT_Y8:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500805 return true;
806 default:
807 return false;
808 }
809}
810
811gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd,
812 uint32_t *out_num_planes) {
813 if (!IsYuvFormat(hnd)) {
814 return GRALLOC1_ERROR_UNSUPPORTED;
815 } else {
816 *out_num_planes = 3;
817 }
818 return GRALLOC1_ERROR_NONE;
819}
820
821gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd,
822 struct android_flex_layout *layout) {
823 if (!IsYuvFormat(hnd)) {
824 return GRALLOC1_ERROR_UNSUPPORTED;
825 }
826
827 android_ycbcr ycbcr;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700828 int err = GetYUVPlaneInfo(hnd, &ycbcr);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500829
830 if (err != 0) {
831 return GRALLOC1_ERROR_BAD_HANDLE;
832 }
833
834 layout->format = FLEX_FORMAT_YCbCr;
835 layout->num_planes = 3;
836
837 for (uint32_t i = 0; i < layout->num_planes; i++) {
838 layout->planes[i].bits_per_component = 8;
839 layout->planes[i].bits_used = 8;
840 layout->planes[i].h_increment = 1;
841 layout->planes[i].v_increment = 1;
842 layout->planes[i].h_subsampling = 2;
843 layout->planes[i].v_subsampling = 2;
844 }
845
846 layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
847 layout->planes[0].component = FLEX_COMPONENT_Y;
848 layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
849
850 layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
851 layout->planes[1].component = FLEX_COMPONENT_Cb;
852 layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
853 layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
854
855 layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
856 layout->planes[2].component = FLEX_COMPONENT_Cr;
857 layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
858 layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
859 return GRALLOC1_ERROR_NONE;
860}
Naseer Ahmeddc918132017-03-07 15:25:14 -0500861
862gralloc1_error_t BufferManager::Dump(std::ostringstream *os) {
863 for (auto it : handles_map_) {
864 auto buf = it.second;
865 auto hnd = buf->handle;
866 *os << "handle id: " << std::setw(4) << hnd->id;
867 *os << " fd: " << std::setw(3) << hnd->fd;
868 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
869 *os << " wxh: " << std::setw(4) << hnd->width <<" x " << std::setw(4) << hnd->height;
870 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
871 *os << std::setw(4) << hnd->unaligned_height;
872 *os << " size: " << std::setw(9) << hnd->size;
873 *os << std::hex << std::setfill('0');
874 *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags;
875 *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage;
876 *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage;
877 // TODO(user): get format string from qdutils
878 *os << " format: " << "0x" << std::setw(8) << hnd->format;
879 *os << std::dec << std::setfill(' ') << std::endl;
880 }
881 return GRALLOC1_ERROR_NONE;
882}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530883} // namespace gralloc1