blob: 687f3c1fe0f1b6daae7eaa6ab8c880fcdbfe65ce [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 Ahmeda422f352017-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 Ahmeda422f352017-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 Ahmeda422f352017-12-01 15:33:56 -050032#include "qd_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053033
Naseer Ahmeda422f352017-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 Ahmeda422f352017-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 handles_map_.clear();
Naseer Ahmede69031e2016-11-22 20:05:16 -050043 allocator_ = new Allocator();
44 allocator_->Init();
45}
46
Naseer Ahmeda422f352017-12-01 15:33:56 -050047BufferManager *BufferManager::GetInstance() {
48 static BufferManager *instance = new BufferManager();
49 return instance;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053050}
51
52BufferManager::~BufferManager() {
53 if (allocator_) {
54 delete allocator_;
55 }
56}
57
Naseer Ahmeda422f352017-12-01 15:33:56 -050058Error BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
Naseer Ahmede69031e2016-11-22 20:05:16 -050059 auto hnd = buf->handle;
Naseer Ahmeda186b472017-07-07 18:50:49 -040060 ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd);
61
62 if (private_handle_t::validate(hnd) != 0) {
63 ALOGE("FreeBuffer: Invalid handle: %p", hnd);
Naseer Ahmeda422f352017-12-01 15:33:56 -050064 return Error::BAD_BUFFER;
Naseer Ahmeda186b472017-07-07 18:50:49 -040065 }
66
Naseer Ahmeda422f352017-12-01 15:33:56 -050067 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, hnd->fd,
68 buf->ion_handle_main) != 0) {
69 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053070 }
71
72 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
73 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -050074 hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
Naseer Ahmeda422f352017-12-01 15:33:56 -050075 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053076 }
77
Naseer Ahmeda422f352017-12-01 15:33:56 -050078 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed29a86dd2017-03-16 14:09:46 -040079 handle->fd = -1;
80 handle->fd_metadata = -1;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040081 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
Naseer Ahmeda422f352017-12-01 15:33:56 -050082 delete handle;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040083 }
Naseer Ahmeda422f352017-12-01 15:33:56 -050084 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053085}
86
Naseer Ahmeda422f352017-12-01 15:33:56 -050087void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, int ion_handle,
Naseer Ahmed378d8582017-03-28 21:56:08 -040088 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040089 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
90 handles_map_.emplace(std::make_pair(hnd, buffer));
91}
92
Naseer Ahmeda422f352017-12-01 15:33:56 -050093Error BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -040094 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040095 int ion_handle = allocator_->ImportBuffer(hnd->fd);
96 if (ion_handle < 0) {
97 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
Naseer Ahmeda422f352017-12-01 15:33:56 -050098 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040099 }
100 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
101 if (ion_handle_meta < 0) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500102 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd,
103 hnd->id);
104 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400105 }
106 // Set base pointers to NULL since the data here was received over binder
107 hnd->base = 0;
108 hnd->base_metadata = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400109 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500110 return Error::NONE;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400111}
112
Naseer Ahmeda422f352017-12-01 15:33:56 -0500113std::shared_ptr<BufferManager::Buffer> BufferManager::GetBufferFromHandleLocked(
114 const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400115 auto it = handles_map_.find(hnd);
116 if (it != handles_map_.end()) {
117 return it->second;
118 } else {
119 return nullptr;
120 }
121}
122
Naseer Ahmeda422f352017-12-01 15:33:56 -0500123Error BufferManager::MapBuffer(private_handle_t const *handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530124 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400125 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530126
127 hnd->base = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530128 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
129 hnd->fd) != 0) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500130 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530131 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500132 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530133}
134
Naseer Ahmeda422f352017-12-01 15:33:56 -0500135Error BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500136 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500137 auto err = Error::NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400138 std::lock_guard<std::mutex> lock(buffer_lock_);
139 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400140 if (buf != nullptr) {
141 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530142 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400143 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400144 err = ImportHandleLocked(handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530145 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400146 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530147}
148
Naseer Ahmeda422f352017-12-01 15:33:56 -0500149Error BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmeda186b472017-07-07 18:50:49 -0400150 ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400151 std::lock_guard<std::mutex> lock(buffer_lock_);
152 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400153 if (buf == nullptr) {
154 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500155 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530156 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400157 if (buf->DecRef()) {
158 handles_map_.erase(hnd);
159 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500160 FreeBuffer(buf);
161 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530162 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500163 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530164}
165
Naseer Ahmeda422f352017-12-01 15:33:56 -0500166Error BufferManager::LockBuffer(const private_handle_t *hnd, uint64_t usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400167 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500168 auto err = Error::NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400169 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530170
171 // If buffer is not meant for CPU return err
Naseer Ahmeda422f352017-12-01 15:33:56 -0500172 if (!CpuCanAccess(usage)) {
173 return Error::BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530174 }
175
Naseer Ahmed378d8582017-03-28 21:56:08 -0400176 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400177 if (buf == nullptr) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500178 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530179 }
180
Naseer Ahmed67330702017-05-02 15:00:26 -0400181 if (hnd->base == 0) {
182 // we need to map for real
183 err = MapBuffer(hnd);
184 }
185
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530186 // Invalidate if CPU reads in software and there are non-CPU
187 // writers. No need to do this for the metadata buffer as it is
188 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400189
190 // todo use handle here
Naseer Ahmeda422f352017-12-01 15:33:56 -0500191 if (err == Error::NONE && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530192 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
193 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400194 buf->ion_handle_main, CACHE_INVALIDATE)) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500195 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530196 }
197 }
198
199 // Mark the buffer to be flushed after CPU write.
Naseer Ahmeda422f352017-12-01 15:33:56 -0500200 if (err == Error::NONE && CpuCanWrite(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530201 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
202 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
203 }
204
205 return err;
206}
207
Naseer Ahmeda422f352017-12-01 15:33:56 -0500208Error BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400209 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmeda422f352017-12-01 15:33:56 -0500210 auto status = Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530211
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530212 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400213 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400214 if (buf == nullptr) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500215 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400216 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530217
218 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
219 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400220 buf->ion_handle_main, CACHE_CLEAN) != 0) {
Naseer Ahmeda422f352017-12-01 15:33:56 -0500221 status = Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530222 }
223 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
224 }
225
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530226 return status;
227}
228
Naseer Ahmeda422f352017-12-01 15:33:56 -0500229uint32_t BufferManager::GetDataAlignment(int format, uint64_t usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500230 uint32_t align = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530231 if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) {
232 align = 8192;
233 }
234
Naseer Ahmeda422f352017-12-01 15:33:56 -0500235 if (usage & BufferUsage::PROTECTED) {
236 if ((usage & BufferUsage::CAMERA_OUTPUT) || (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530237 // The alignment here reflects qsee mmu V7L/V8L requirement
238 align = SZ_2M;
239 } else {
240 align = SECURE_ALIGN;
241 }
242 }
243
244 return align;
245}
246
Naseer Ahmeda422f352017-12-01 15:33:56 -0500247int BufferManager::GetHandleFlags(int format, uint64_t usage) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530248 int flags = 0;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500249 if (usage & BufferUsage::VIDEO_ENCODER) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530250 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
251 }
252
Naseer Ahmeda422f352017-12-01 15:33:56 -0500253 if (usage & BufferUsage::CAMERA_OUTPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530254 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
255 }
256
Naseer Ahmeda422f352017-12-01 15:33:56 -0500257 if (usage & BufferUsage::CAMERA_INPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530258 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
259 }
260
Naseer Ahmeda422f352017-12-01 15:33:56 -0500261 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530262 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
263 }
264
Naseer Ahmeda422f352017-12-01 15:33:56 -0500265 if (usage & BufferUsage::GPU_TEXTURE) {
266 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
267 }
268
269 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
270 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
271 }
272
273 if (IsUBwcEnabled(format, usage)) {
274 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
275 }
276
277 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
278 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
279 }
280
281 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
282 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
283 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
284 }
285
286 if (!allocator_->UseUncached(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530287 flags |= private_handle_t::PRIV_FLAGS_CACHED;
288 }
289
290 return flags;
291}
292
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400293int BufferManager::GetBufferType(int inputFormat) {
294 int buffer_type = BUFFER_TYPE_VIDEO;
295 if (IsUncompressedRGBFormat(inputFormat)) {
296 // RGB formats
297 buffer_type = BUFFER_TYPE_UI;
298 }
299
300 return buffer_type;
301}
302
Naseer Ahmeda422f352017-12-01 15:33:56 -0500303Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
304 unsigned int bufferSize) {
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400305 if (!handle)
Naseer Ahmeda422f352017-12-01 15:33:56 -0500306 return Error::BAD_BUFFER;
307 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400308
Naseer Ahmeda422f352017-12-01 15:33:56 -0500309 uint64_t usage = descriptor.GetUsage();
310 int format = allocator_->GetImplDefinedFormat(usage, descriptor.GetFormat());
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400311 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400312
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400313 unsigned int size;
314 unsigned int alignedw, alignedh;
Naseer Ahmed95dc2882017-07-26 18:12:12 -0400315
316 int buffer_type = GetBufferType(format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700317 BufferInfo info = GetBufferInfo(descriptor);
Naseer Ahmedcf5dd892018-02-26 12:59:22 -0500318 info.format = format;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700319 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400320 size = (bufferSize >= size) ? bufferSize : size;
321
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530322 int err = 0;
323 int flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400324 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530325 AllocData data;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500326 data.align = GetDataAlignment(format, usage);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400327 size = ALIGN(size, data.align) * layer_count;
328 data.size = size;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500329 data.handle = (uintptr_t)handle;
330 data.uncached = allocator_->UseUncached(usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500331
332 // Allocate buffer memory
Naseer Ahmeda422f352017-12-01 15:33:56 -0500333 err = allocator_->AllocateMem(&data, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530334 if (err) {
335 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Naseer Ahmeda422f352017-12-01 15:33:56 -0500336 return Error::NO_RESOURCES;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530337 }
338
Naseer Ahmede69031e2016-11-22 20:05:16 -0500339 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530340 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500341 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530342 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500343 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530344
Naseer Ahmeda422f352017-12-01 15:33:56 -0500345 err = allocator_->AllocateMem(&e_data, 0);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500346 if (err) {
347 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
Naseer Ahmeda422f352017-12-01 15:33:56 -0500348 return Error::NO_RESOURCES;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500349 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530350
Naseer Ahmeda422f352017-12-01 15:33:56 -0500351 flags = GetHandleFlags(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530352 flags |= data.alloc_type;
353
354 // Create handle
Naseer Ahmeda422f352017-12-01 15:33:56 -0500355 private_handle_t *hnd = new private_handle_t(
356 data.fd, e_data.fd, flags, INT(alignedw), INT(alignedh), descriptor.GetWidth(),
357 descriptor.GetHeight(), format, buffer_type, data.size, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530358
Naseer Ahmede69031e2016-11-22 20:05:16 -0500359 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400360 hnd->base = 0;
361 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400362 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530363
Naseer Ahmede69031e2016-11-22 20:05:16 -0500364 ColorSpace_t colorSpace = ITU_R_601;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530365 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530366 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400367 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500368 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
369 if (DEBUG) {
370 private_handle_t::Dump(hnd);
371 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500372 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530373}
374
Naseer Ahmeda422f352017-12-01 15:33:56 -0500375Error BufferManager::Dump(std::ostringstream *os) {
Uday Kiran Pichika76109432018-04-10 15:37:35 +0530376 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Naseer Ahmeddc918132017-03-07 15:25:14 -0500377 for (auto it : handles_map_) {
378 auto buf = it.second;
379 auto hnd = buf->handle;
380 *os << "handle id: " << std::setw(4) << hnd->id;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500381 *os << " fd: " << std::setw(3) << hnd->fd;
382 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
383 *os << " wxh: " << std::setw(4) << hnd->width << " x " << std::setw(4) << hnd->height;
384 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
385 *os << std::setw(4) << hnd->unaligned_height;
386 *os << " size: " << std::setw(9) << hnd->size;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500387 *os << std::hex << std::setfill('0');
Naseer Ahmeda422f352017-12-01 15:33:56 -0500388 *os << " priv_flags: "
389 << "0x" << std::setw(8) << hnd->flags;
390 *os << " usage: "
391 << "0x" << std::setw(8) << hnd->usage;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500392 // TODO(user): get format string from qdutils
Naseer Ahmeda422f352017-12-01 15:33:56 -0500393 *os << " format: "
394 << "0x" << std::setw(8) << hnd->format;
395 *os << std::dec << std::setfill(' ') << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500396 }
Naseer Ahmeda422f352017-12-01 15:33:56 -0500397 return Error::NONE;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500398}
Naseer Ahmeda422f352017-12-01 15:33:56 -0500399} // namespace gralloc