blob: 16f563579cbf8fdfb8bacf4653182af3e3d2954f [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;
407 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY) {
408 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
409 }
410
411 if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY) {
412 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
413 }
414
415 if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) {
416 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
417 }
418
419 if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) {
420 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
421 }
422
423 if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) {
424 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
425 }
426
427 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
428 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
429 }
430
431 if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) {
432 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
433 }
434
435 if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) {
436 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
437 }
438
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700439 if (IsUBwcEnabled(format, prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530440 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
441 }
442
443 if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) {
444 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
445 }
446
447 // TODO(user): is this correct???
448 if ((cons_usage &
449 (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) ||
450 (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) {
451 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
452 }
453
454 if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) {
455 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
456 }
457
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400458 if (!allocator_->UseUncached(prod_usage, cons_usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530459 flags |= private_handle_t::PRIV_FLAGS_CACHED;
460 }
461
462 return flags;
463}
464
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400465int BufferManager::GetBufferType(int inputFormat) {
466 int buffer_type = BUFFER_TYPE_VIDEO;
467 if (IsUncompressedRGBFormat(inputFormat)) {
468 // RGB formats
469 buffer_type = BUFFER_TYPE_UI;
470 }
471
472 return buffer_type;
473}
474
475int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
476 unsigned int bufferSize) {
477 if (!handle)
478 return -EINVAL;
479
480 int format = descriptor.GetFormat();
481 gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
482 gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400483 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400484
485 // Get implementation defined format
486 int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format);
487
488 unsigned int size;
489 unsigned int alignedw, alignedh;
490 int buffer_type = GetBufferType(gralloc_format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700491 BufferInfo info = GetBufferInfo(descriptor);
492 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400493 size = (bufferSize >= size) ? bufferSize : size;
494
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);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400500 size = ALIGN(size, data.align) * layer_count;
501 data.size = size;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400502 data.handle = (uintptr_t) handle;
Naseer Ahmedfe6342e2017-04-20 16:52:40 -0400503 data.uncached = allocator_->UseUncached(prod_usage, cons_usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500504
505 // Allocate buffer memory
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530506 err = allocator_->AllocateMem(&data, prod_usage, cons_usage);
507 if (err) {
508 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530509 return err;
510 }
511
Naseer Ahmede69031e2016-11-22 20:05:16 -0500512 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530513 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500514 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530515 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500516 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530517
518 err =
519 allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500520 if (err) {
521 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
522 return err;
523 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530524
525 flags = GetHandleFlags(format, prod_usage, cons_usage);
526 flags |= data.alloc_type;
527
528 // Create handle
Naseer Ahmede69031e2016-11-22 20:05:16 -0500529 private_handle_t *hnd = new private_handle_t(data.fd,
530 e_data.fd,
531 flags,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400532 INT(alignedw),
533 INT(alignedh),
534 descriptor.GetWidth(),
535 descriptor.GetHeight(),
Naseer Ahmede69031e2016-11-22 20:05:16 -0500536 format,
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400537 buffer_type,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500538 size,
539 prod_usage,
540 cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530541
Naseer Ahmede69031e2016-11-22 20:05:16 -0500542 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400543 hnd->base = 0;
544 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400545 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530546
Naseer Ahmede69031e2016-11-22 20:05:16 -0500547 ColorSpace_t colorSpace = ITU_R_601;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530548 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530549 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400550 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500551 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
552 if (DEBUG) {
553 private_handle_t::Dump(hnd);
554 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530555 return err;
556}
557
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530558gralloc1_error_t BufferManager::Perform(int operation, va_list args) {
559 switch (operation) {
560 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: {
561 int fd = va_arg(args, int);
562 unsigned int size = va_arg(args, unsigned int);
563 unsigned int offset = va_arg(args, unsigned int);
564 void *base = va_arg(args, void *);
565 int width = va_arg(args, int);
566 int height = va_arg(args, int);
567 int format = va_arg(args, int);
568
569 native_handle_t **handle = va_arg(args, native_handle_t **);
570 private_handle_t *hnd = reinterpret_cast<private_handle_t *>(
571 native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts()));
572 if (hnd) {
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700573 unsigned int alignedw = 0, alignedh = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530574 hnd->magic = private_handle_t::kMagic;
575 hnd->fd = fd;
576 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION;
577 hnd->size = size;
578 hnd->offset = offset;
579 hnd->base = uint64_t(base) + offset;
580 hnd->gpuaddr = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700581 BufferInfo info(width, height, format);
582 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700583 hnd->unaligned_width = width;
584 hnd->unaligned_height = height;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500585 hnd->width = INT(alignedw);
586 hnd->height = INT(alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530587 hnd->format = format;
588 *handle = reinterpret_cast<native_handle_t *>(hnd);
589 }
590 } break;
591
592 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
593 int width = va_arg(args, int);
594 int format = va_arg(args, int);
595 int *stride = va_arg(args, int *);
596 unsigned int alignedw = 0, alignedh = 0;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700597 BufferInfo info(width, width, format);
598 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530599 *stride = INT(alignedw);
600 } break;
601
602 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
603 private_handle_t *hnd = va_arg(args, private_handle_t *);
604 int *stride = va_arg(args, int *);
605 if (private_handle_t::validate(hnd) != 0) {
606 return GRALLOC1_ERROR_BAD_HANDLE;
607 }
608
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400609 BufferDim_t buffer_dim;
610 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
611 *stride = buffer_dim.sliceWidth;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530612 } else {
613 *stride = hnd->width;
614 }
615 } break;
616
617 // TODO(user) : this alone should be sufficient, ask gfx to get rid of above
618 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
619 private_handle_t *hnd = va_arg(args, private_handle_t *);
620 int *stride = va_arg(args, int *);
621 int *height = va_arg(args, int *);
622 if (private_handle_t::validate(hnd) != 0) {
623 return GRALLOC1_ERROR_BAD_HANDLE;
624 }
625
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400626 BufferDim_t buffer_dim;
Rohit Kulkarni70537e02017-06-16 12:01:13 -0700627 int interlaced = 0;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400628 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
629 *stride = buffer_dim.sliceWidth;
630 *height = buffer_dim.sliceHeight;
Rohit Kulkarni70537e02017-06-16 12:01:13 -0700631 } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) {
632 if (interlaced && IsUBwcFormat(hnd->format)) {
633 unsigned int alignedw = 0, alignedh = 0;
634 // Get re-aligned height for single ubwc interlaced field and
635 // multiple by 2 to get frame height.
636 BufferInfo info(hnd->width, ((hnd->height+1)>>1), hnd->format);
637 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
638 *stride = static_cast<int>(alignedw);
639 *height = static_cast<int>(alignedh * 2);
640 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530641 } else {
642 *stride = hnd->width;
643 *height = hnd->height;
644 }
645 } break;
646
647 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
648 // TODO(user): Usage is split now. take care of it from Gfx client.
649 // see if we can directly expect descriptor from gfx client.
650 int width = va_arg(args, int);
651 int height = va_arg(args, int);
652 int format = va_arg(args, int);
653 uint64_t producer_usage = va_arg(args, uint64_t);
654 uint64_t consumer_usage = va_arg(args, uint64_t);
655 gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage);
656 gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage);
657
658 int *aligned_width = va_arg(args, int *);
659 int *aligned_height = va_arg(args, int *);
660 int *tile_enabled = va_arg(args, int *);
661 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700662 BufferInfo info(width, height, format, prod_usage, cons_usage);
663 *tile_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
664 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530665 *aligned_width = INT(alignedw);
666 *aligned_height = INT(alignedh);
667 } break;
668
669 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
670 private_handle_t *hnd = va_arg(args, private_handle_t *);
671 int *color_space = va_arg(args, int *);
672 if (private_handle_t::validate(hnd) != 0) {
673 return GRALLOC1_ERROR_BAD_HANDLE;
674 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400675 *color_space = 0;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700676#ifdef USE_COLOR_METADATA
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400677 ColorMetaData color_metadata;
678 if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) {
679 switch (color_metadata.colorPrimaries) {
680 case ColorPrimaries_BT709_5:
681 *color_space = HAL_CSC_ITU_R_709;
682 break;
683 case ColorPrimaries_BT601_6_525:
684 *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601);
685 break;
686 case ColorPrimaries_BT2020:
687 *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020;
688 break;
689 default:
690 ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries);
691 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700692 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400693 break;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530694 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400695 if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) {
696 *color_space = 0;
697 }
698#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530699 } break;
700 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
701 private_handle_t *hnd = va_arg(args, private_handle_t *);
702 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
703 if (private_handle_t::validate(hnd) != 0) {
704 return GRALLOC1_ERROR_BAD_HANDLE;
705 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700706 if (GetYUVPlaneInfo(hnd, ycbcr)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530707 return GRALLOC1_ERROR_UNDEFINED;
708 }
709 } break;
710
711 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
712 private_handle_t *hnd = va_arg(args, private_handle_t *);
713 int *map_secure_buffer = va_arg(args, int *);
714 if (private_handle_t::validate(hnd) != 0) {
715 return GRALLOC1_ERROR_BAD_HANDLE;
716 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400717
718 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530719 *map_secure_buffer = 0;
720 }
721 } break;
722
723 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
724 private_handle_t *hnd = va_arg(args, private_handle_t *);
725 int *flag = va_arg(args, int *);
726 if (private_handle_t::validate(hnd) != 0) {
727 return GRALLOC1_ERROR_BAD_HANDLE;
728 }
729 *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
730 } break;
731
732 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
733 private_handle_t *hnd = va_arg(args, private_handle_t *);
734 void **rgb_data = va_arg(args, void **);
735 if (private_handle_t::validate(hnd) != 0) {
736 return GRALLOC1_ERROR_BAD_HANDLE;
737 }
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700738 if (GetRgbDataAddress(hnd, rgb_data)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530739 return GRALLOC1_ERROR_UNDEFINED;
740 }
741 } break;
742
Naseer Ahmede69031e2016-11-22 20:05:16 -0500743 case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: {
744 int width = va_arg(args, int);
745 int height = va_arg(args, int);
746 int format = va_arg(args, int);
747 uint64_t p_usage = va_arg(args, uint64_t);
748 uint64_t c_usage = va_arg(args, uint64_t);
749 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
750 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
751 uint32_t *aligned_width = va_arg(args, uint32_t *);
752 uint32_t *aligned_height = va_arg(args, uint32_t *);
753 uint32_t *size = va_arg(args, uint32_t *);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700754 auto info = BufferInfo(width, height, format, producer_usage, consumer_usage);
755 GetBufferSizeAndDimensions(info, size, aligned_width, aligned_height);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500756 // Align size
757 auto align = GetDataAlignment(format, producer_usage, consumer_usage);
758 *size = ALIGN(*size, align);
759 } break;
760
761 // TODO(user): Break out similar functionality, preferably moving to a common lib.
762
763 case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: {
764 int width = va_arg(args, int);
765 int height = va_arg(args, int);
766 int format = va_arg(args, int);
767 uint64_t p_usage = va_arg(args, uint64_t);
768 uint64_t c_usage = va_arg(args, uint64_t);
769 buffer_handle_t *hnd = va_arg(args, buffer_handle_t*);
770 gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage);
771 gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage);
772 BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage);
773 unsigned int size;
774 unsigned int alignedw, alignedh;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700775 GetBufferSizeAndDimensions(GetBufferInfo(descriptor), &size, &alignedw, &alignedh);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500776 AllocateBuffer(descriptor, hnd, size);
777 } break;
778
Rohit Kulkarnia152c312017-06-02 14:22:35 -0700779 case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
780 private_handle_t *hnd = va_arg(args, private_handle_t *);
781 int *flag = va_arg(args, int *);
782 if (private_handle_t::validate(hnd) != 0) {
783 return GRALLOC1_ERROR_BAD_HANDLE;
784 }
785 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
786 *flag = 0;
787 }
788 } break;
789
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530790 default:
791 break;
792 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530793 return GRALLOC1_ERROR_NONE;
794}
795
Naseer Ahmede69031e2016-11-22 20:05:16 -0500796static bool IsYuvFormat(const private_handle_t *hnd) {
797 switch (hnd->format) {
798 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
799 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
800 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
801 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS
802 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
803 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
804 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
805 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO:
806 case HAL_PIXEL_FORMAT_NV21_ZSL:
807 case HAL_PIXEL_FORMAT_RAW16:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530808 case HAL_PIXEL_FORMAT_Y16:
Naseer Ahmed92998622017-03-20 12:39:17 -0400809 case HAL_PIXEL_FORMAT_RAW12:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500810 case HAL_PIXEL_FORMAT_RAW10:
811 case HAL_PIXEL_FORMAT_YV12:
Anjaneya Prasad Musunurib1654142017-04-18 10:32:42 +0530812 case HAL_PIXEL_FORMAT_Y8:
Naseer Ahmede69031e2016-11-22 20:05:16 -0500813 return true;
814 default:
815 return false;
816 }
817}
818
819gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd,
820 uint32_t *out_num_planes) {
821 if (!IsYuvFormat(hnd)) {
822 return GRALLOC1_ERROR_UNSUPPORTED;
823 } else {
824 *out_num_planes = 3;
825 }
826 return GRALLOC1_ERROR_NONE;
827}
828
829gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd,
830 struct android_flex_layout *layout) {
831 if (!IsYuvFormat(hnd)) {
832 return GRALLOC1_ERROR_UNSUPPORTED;
833 }
834
835 android_ycbcr ycbcr;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700836 int err = GetYUVPlaneInfo(hnd, &ycbcr);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500837
838 if (err != 0) {
839 return GRALLOC1_ERROR_BAD_HANDLE;
840 }
841
842 layout->format = FLEX_FORMAT_YCbCr;
843 layout->num_planes = 3;
844
845 for (uint32_t i = 0; i < layout->num_planes; i++) {
846 layout->planes[i].bits_per_component = 8;
847 layout->planes[i].bits_used = 8;
848 layout->planes[i].h_increment = 1;
849 layout->planes[i].v_increment = 1;
850 layout->planes[i].h_subsampling = 2;
851 layout->planes[i].v_subsampling = 2;
852 }
853
854 layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
855 layout->planes[0].component = FLEX_COMPONENT_Y;
856 layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
857
858 layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
859 layout->planes[1].component = FLEX_COMPONENT_Cb;
860 layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
861 layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
862
863 layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
864 layout->planes[2].component = FLEX_COMPONENT_Cr;
865 layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
866 layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
867 return GRALLOC1_ERROR_NONE;
868}
Naseer Ahmeddc918132017-03-07 15:25:14 -0500869
870gralloc1_error_t BufferManager::Dump(std::ostringstream *os) {
871 for (auto it : handles_map_) {
872 auto buf = it.second;
873 auto hnd = buf->handle;
874 *os << "handle id: " << std::setw(4) << hnd->id;
875 *os << " fd: " << std::setw(3) << hnd->fd;
876 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
877 *os << " wxh: " << std::setw(4) << hnd->width <<" x " << std::setw(4) << hnd->height;
878 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
879 *os << std::setw(4) << hnd->unaligned_height;
880 *os << " size: " << std::setw(9) << hnd->size;
881 *os << std::hex << std::setfill('0');
882 *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags;
883 *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage;
884 *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage;
885 // TODO(user): get format string from qdutils
886 *os << " format: " << "0x" << std::setw(8) << hnd->format;
887 *os << std::dec << std::setfill(' ') << std::endl;
888 }
889 return GRALLOC1_ERROR_NONE;
890}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530891} // namespace gralloc1