blob: 95cbc759d6366c469eb829bd22a6de94c5c6b13d [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Anjaneya Prasad Musunurib6fe9702018-01-11 13:56:23 +05302 * Copyright (c) 2011-2018 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>
Naseer Ahmede36f2242017-12-01 15:33:56 -050023#include <sstream>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053024#include <utility>
Naseer Ahmede69031e2016-11-22 20:05:16 -050025#include <vector>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053026
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053027#include "gr_buf_descriptor.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053028#include "gr_buf_mgr.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050029#include "gr_priv_handle.h"
30#include "gr_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053031#include "qdMetaData.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050032#include "qd_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053033
Naseer Ahmede36f2242017-12-01 15:33:56 -050034namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053035
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070036static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
37 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
Naseer Ahmede36f2242017-12-01 15:33:56 -050038 descriptor.GetUsage());
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070039}
40
Naseer Ahmede69031e2016-11-22 20:05:16 -050041BufferManager::BufferManager() : next_id_(0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053042 char property[PROPERTY_VALUE_MAX];
43
44 // Map framebuffer memory
45 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
46 (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
47 (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
48 map_fb_mem_ = true;
49 }
50
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053051 handles_map_.clear();
Naseer Ahmede69031e2016-11-22 20:05:16 -050052 allocator_ = new Allocator();
53 allocator_->Init();
54}
55
Naseer Ahmede36f2242017-12-01 15:33:56 -050056BufferManager *BufferManager::GetInstance() {
57 static BufferManager *instance = new BufferManager();
58 return instance;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053059}
60
61BufferManager::~BufferManager() {
62 if (allocator_) {
63 delete allocator_;
64 }
65}
66
Naseer Ahmede36f2242017-12-01 15:33:56 -050067Error BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
Naseer Ahmede69031e2016-11-22 20:05:16 -050068 auto hnd = buf->handle;
Naseer Ahmeda186b472017-07-07 18:50:49 -040069 ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd);
70
71 if (private_handle_t::validate(hnd) != 0) {
72 ALOGE("FreeBuffer: Invalid handle: %p", hnd);
Naseer Ahmede36f2242017-12-01 15:33:56 -050073 return Error::BAD_BUFFER;
Naseer Ahmeda186b472017-07-07 18:50:49 -040074 }
75
Naseer Ahmede36f2242017-12-01 15:33:56 -050076 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, hnd->fd,
77 buf->ion_handle_main) != 0) {
78 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053079 }
80
81 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
82 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -050083 hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050084 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053085 }
86
Naseer Ahmede36f2242017-12-01 15:33:56 -050087 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed29a86dd2017-03-16 14:09:46 -040088 handle->fd = -1;
89 handle->fd_metadata = -1;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040090 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050091 delete handle;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040092 }
Naseer Ahmede36f2242017-12-01 15:33:56 -050093 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053094}
95
Naseer Ahmede36f2242017-12-01 15:33:56 -050096void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, int ion_handle,
Naseer Ahmed378d8582017-03-28 21:56:08 -040097 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040098 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
99 handles_map_.emplace(std::make_pair(hnd, buffer));
100}
101
Naseer Ahmede36f2242017-12-01 15:33:56 -0500102Error BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400103 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400104 int ion_handle = allocator_->ImportBuffer(hnd->fd);
105 if (ion_handle < 0) {
106 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500107 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400108 }
109 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
110 if (ion_handle_meta < 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500111 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd,
112 hnd->id);
113 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400114 }
115 // Set base pointers to NULL since the data here was received over binder
116 hnd->base = 0;
117 hnd->base_metadata = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400118 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500119 return Error::NONE;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400120}
121
Naseer Ahmede36f2242017-12-01 15:33:56 -0500122std::shared_ptr<BufferManager::Buffer> BufferManager::GetBufferFromHandleLocked(
123 const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400124 auto it = handles_map_.find(hnd);
125 if (it != handles_map_.end()) {
126 return it->second;
127 } else {
128 return nullptr;
129 }
130}
131
Naseer Ahmede36f2242017-12-01 15:33:56 -0500132Error BufferManager::MapBuffer(private_handle_t const *handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530133 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400134 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530135
136 hnd->base = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530137 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
138 hnd->fd) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500139 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530140 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500141 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530142}
143
Naseer Ahmede36f2242017-12-01 15:33:56 -0500144Error BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500145 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500146 auto err = Error::NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400147 std::lock_guard<std::mutex> lock(buffer_lock_);
148 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400149 if (buf != nullptr) {
150 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530151 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400152 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400153 err = ImportHandleLocked(handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530154 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400155 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530156}
157
Naseer Ahmede36f2242017-12-01 15:33:56 -0500158Error BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmeda186b472017-07-07 18:50:49 -0400159 ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400160 std::lock_guard<std::mutex> lock(buffer_lock_);
161 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400162 if (buf == nullptr) {
163 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500164 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530165 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400166 if (buf->DecRef()) {
167 handles_map_.erase(hnd);
168 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500169 FreeBuffer(buf);
170 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530171 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500172 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530173}
174
Naseer Ahmede36f2242017-12-01 15:33:56 -0500175Error BufferManager::LockBuffer(const private_handle_t *hnd, uint64_t usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400176 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500177 auto err = Error::NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400178 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530179
180 // If buffer is not meant for CPU return err
Naseer Ahmede36f2242017-12-01 15:33:56 -0500181 if (!CpuCanAccess(usage)) {
182 return Error::BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530183 }
184
Naseer Ahmed378d8582017-03-28 21:56:08 -0400185 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400186 if (buf == nullptr) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500187 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530188 }
189
Naseer Ahmed67330702017-05-02 15:00:26 -0400190 if (hnd->base == 0) {
191 // we need to map for real
192 err = MapBuffer(hnd);
193 }
194
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530195 // Invalidate if CPU reads in software and there are non-CPU
196 // writers. No need to do this for the metadata buffer as it is
197 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400198
199 // todo use handle here
Naseer Ahmede36f2242017-12-01 15:33:56 -0500200 if (err == Error::NONE && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530201 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
202 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700203 buf->ion_handle_main, CACHE_INVALIDATE, hnd->fd)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500204 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530205 }
206 }
207
208 // Mark the buffer to be flushed after CPU write.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500209 if (err == Error::NONE && CpuCanWrite(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530210 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
211 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
212 }
213
214 return err;
215}
216
Naseer Ahmede36f2242017-12-01 15:33:56 -0500217Error BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400218 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500219 auto status = Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530220
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530221 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400222 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400223 if (buf == nullptr) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500224 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400225 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530226
227 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
228 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700229 buf->ion_handle_main, CACHE_CLEAN, hnd->fd) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500230 status = Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231 }
232 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
233 }
234
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530235 return status;
236}
237
Naseer Ahmede36f2242017-12-01 15:33:56 -0500238uint32_t BufferManager::GetDataAlignment(int format, uint64_t usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500239 uint32_t align = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530240 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
241 align = 8192;
242 }
243
Naseer Ahmede36f2242017-12-01 15:33:56 -0500244 if (usage & BufferUsage::PROTECTED) {
245 if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530246 // The alignment here reflects qsee mmu V7L/V8L requirement
247 align = SZ_2M;
248 } else {
249 align = SECURE_ALIGN;
250 }
251 }
252
253 return align;
254}
255
Naseer Ahmede36f2242017-12-01 15:33:56 -0500256int BufferManager::GetHandleFlags(int format, uint64_t usage) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530257 int flags = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500258 if (usage & BufferUsage::VIDEO_ENCODER) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530259 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
260 }
261
Naseer Ahmede36f2242017-12-01 15:33:56 -0500262 if (usage & BufferUsage::CAMERA_OUTPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530263 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
264 }
265
Naseer Ahmede36f2242017-12-01 15:33:56 -0500266 if (usage & BufferUsage::CAMERA_INPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530267 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
268 }
269
Naseer Ahmede36f2242017-12-01 15:33:56 -0500270 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530271 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
272 }
273
Naseer Ahmede36f2242017-12-01 15:33:56 -0500274 if (usage & BufferUsage::GPU_TEXTURE) {
275 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
276 }
277
278 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
279 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
280 }
281
282 if (IsUBwcEnabled(format, usage)) {
283 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
284 }
285
286 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
287 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
288 }
289
290 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
291 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
292 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
293 }
294
295 if (!allocator_->UseUncached(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530296 flags |= private_handle_t::PRIV_FLAGS_CACHED;
297 }
298
299 return flags;
300}
301
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400302int BufferManager::GetBufferType(int inputFormat) {
303 int buffer_type = BUFFER_TYPE_VIDEO;
304 if (IsUncompressedRGBFormat(inputFormat)) {
305 // RGB formats
306 buffer_type = BUFFER_TYPE_UI;
307 }
308
309 return buffer_type;
310}
311
Naseer Ahmede36f2242017-12-01 15:33:56 -0500312Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
313 unsigned int bufferSize) {
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400314 if (!handle)
Naseer Ahmede36f2242017-12-01 15:33:56 -0500315 return Error::BAD_BUFFER;
316 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400317
Naseer Ahmede36f2242017-12-01 15:33:56 -0500318 uint64_t usage = descriptor.GetUsage();
319 int format = allocator_->GetImplDefinedFormat(usage, descriptor.GetFormat());
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400320 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400321
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400322 unsigned int size;
323 unsigned int alignedw, alignedh;
Naseer Ahmed95dc2882017-07-26 18:12:12 -0400324
325 int buffer_type = GetBufferType(format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700326 BufferInfo info = GetBufferInfo(descriptor);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500327 info.format = format;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700328 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400329 size = (bufferSize >= size) ? bufferSize : size;
330
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530331 int err = 0;
332 int flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400333 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530334 AllocData data;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500335 data.align = GetDataAlignment(format, usage);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400336 size = ALIGN(size, data.align) * layer_count;
337 data.size = size;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500338 data.handle = (uintptr_t)handle;
339 data.uncached = allocator_->UseUncached(usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500340
341 // Allocate buffer memory
Naseer Ahmede36f2242017-12-01 15:33:56 -0500342 err = allocator_->AllocateMem(&data, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530343 if (err) {
344 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500345 return Error::NO_RESOURCES;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530346 }
347
Naseer Ahmede69031e2016-11-22 20:05:16 -0500348 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530349 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500350 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530351 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500352 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530353
Naseer Ahmede36f2242017-12-01 15:33:56 -0500354 err = allocator_->AllocateMem(&e_data, 0);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500355 if (err) {
356 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500357 return Error::NO_RESOURCES;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500358 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530359
Naseer Ahmede36f2242017-12-01 15:33:56 -0500360 flags = GetHandleFlags(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530361 flags |= data.alloc_type;
362
363 // Create handle
Naseer Ahmede36f2242017-12-01 15:33:56 -0500364 private_handle_t *hnd = new private_handle_t(
365 data.fd, e_data.fd, flags, INT(alignedw), INT(alignedh), descriptor.GetWidth(),
366 descriptor.GetHeight(), format, buffer_type, data.size, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530367
Naseer Ahmede69031e2016-11-22 20:05:16 -0500368 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400369 hnd->base = 0;
370 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400371 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530372
Arun Kumar K.Raf6a6052017-12-04 14:10:11 +0530373 if (buffer_type == BUFFER_TYPE_VIDEO) {
374 // set default csc to 601L for only video buffers
375 ColorSpace_t colorSpace = ITU_R_601;
376 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
377 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530378 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400379 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500380 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
381 if (DEBUG) {
382 private_handle_t::Dump(hnd);
383 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500384 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530385}
386
Naseer Ahmede36f2242017-12-01 15:33:56 -0500387Error BufferManager::Dump(std::ostringstream *os) {
Naseer Ahmeddc918132017-03-07 15:25:14 -0500388 for (auto it : handles_map_) {
389 auto buf = it.second;
390 auto hnd = buf->handle;
391 *os << "handle id: " << std::setw(4) << hnd->id;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500392 *os << " fd: " << std::setw(3) << hnd->fd;
393 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
394 *os << " wxh: " << std::setw(4) << hnd->width << " x " << std::setw(4) << hnd->height;
395 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
396 *os << std::setw(4) << hnd->unaligned_height;
397 *os << " size: " << std::setw(9) << hnd->size;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500398 *os << std::hex << std::setfill('0');
Naseer Ahmede36f2242017-12-01 15:33:56 -0500399 *os << " priv_flags: "
400 << "0x" << std::setw(8) << hnd->flags;
401 *os << " usage: "
402 << "0x" << std::setw(8) << hnd->usage;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500403 // TODO(user): get format string from qdutils
Naseer Ahmede36f2242017-12-01 15:33:56 -0500404 *os << " format: "
405 << "0x" << std::setw(8) << hnd->format;
406 *os << std::dec << std::setfill(' ') << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500407 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500408 return Error::NONE;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500409}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500410} // namespace gralloc