blob: 9f7b22e6807c8722cd95e36d8523a31d2ae0d38f [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"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053030#include "qdMetaData.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050031#include "qd_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053032
Naseer Ahmede36f2242017-12-01 15:33:56 -050033namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053034
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070035static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
36 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
Naseer Ahmede36f2242017-12-01 15:33:56 -050037 descriptor.GetUsage());
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070038}
39
Naseer Ahmede69031e2016-11-22 20:05:16 -050040BufferManager::BufferManager() : next_id_(0) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053041 char property[PROPERTY_VALUE_MAX];
42
43 // Map framebuffer memory
44 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
45 (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
46 (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
47 map_fb_mem_ = true;
48 }
49
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053050 handles_map_.clear();
Naseer Ahmede69031e2016-11-22 20:05:16 -050051 allocator_ = new Allocator();
52 allocator_->Init();
53}
54
Naseer Ahmede36f2242017-12-01 15:33:56 -050055BufferManager *BufferManager::GetInstance() {
56 static BufferManager *instance = new BufferManager();
57 return instance;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053058}
59
60BufferManager::~BufferManager() {
61 if (allocator_) {
62 delete allocator_;
63 }
64}
65
Naseer Ahmede36f2242017-12-01 15:33:56 -050066Error BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
Naseer Ahmede69031e2016-11-22 20:05:16 -050067 auto hnd = buf->handle;
Naseer Ahmeda186b472017-07-07 18:50:49 -040068 ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd);
69
70 if (private_handle_t::validate(hnd) != 0) {
71 ALOGE("FreeBuffer: Invalid handle: %p", hnd);
Naseer Ahmede36f2242017-12-01 15:33:56 -050072 return Error::BAD_BUFFER;
Naseer Ahmeda186b472017-07-07 18:50:49 -040073 }
74
Naseer Ahmede36f2242017-12-01 15:33:56 -050075 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, hnd->fd,
76 buf->ion_handle_main) != 0) {
77 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053078 }
79
80 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
81 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -050082 hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050083 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053084 }
85
Naseer Ahmede36f2242017-12-01 15:33:56 -050086 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed29a86dd2017-03-16 14:09:46 -040087 handle->fd = -1;
88 handle->fd_metadata = -1;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040089 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050090 delete handle;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040091 }
Naseer Ahmede36f2242017-12-01 15:33:56 -050092 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053093}
94
Naseer Ahmede36f2242017-12-01 15:33:56 -050095void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, int ion_handle,
Naseer Ahmed378d8582017-03-28 21:56:08 -040096 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040097 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
98 handles_map_.emplace(std::make_pair(hnd, buffer));
99}
100
Naseer Ahmede36f2242017-12-01 15:33:56 -0500101Error BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400102 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400103 int ion_handle = allocator_->ImportBuffer(hnd->fd);
104 if (ion_handle < 0) {
105 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500106 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400107 }
108 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
109 if (ion_handle_meta < 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500110 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd,
111 hnd->id);
112 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400113 }
114 // Set base pointers to NULL since the data here was received over binder
115 hnd->base = 0;
116 hnd->base_metadata = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400117 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500118 return Error::NONE;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400119}
120
Naseer Ahmede36f2242017-12-01 15:33:56 -0500121std::shared_ptr<BufferManager::Buffer> BufferManager::GetBufferFromHandleLocked(
122 const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400123 auto it = handles_map_.find(hnd);
124 if (it != handles_map_.end()) {
125 return it->second;
126 } else {
127 return nullptr;
128 }
129}
130
Naseer Ahmede36f2242017-12-01 15:33:56 -0500131Error BufferManager::MapBuffer(private_handle_t const *handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530132 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400133 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530134
135 hnd->base = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530136 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
137 hnd->fd) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500138 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530139 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500140 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530141}
142
Naseer Ahmede36f2242017-12-01 15:33:56 -0500143Error BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500144 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500145 auto err = Error::NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400146 std::lock_guard<std::mutex> lock(buffer_lock_);
147 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400148 if (buf != nullptr) {
149 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530150 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400151 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400152 err = ImportHandleLocked(handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530153 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400154 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530155}
156
Naseer Ahmede36f2242017-12-01 15:33:56 -0500157Error BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmeda186b472017-07-07 18:50:49 -0400158 ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400159 std::lock_guard<std::mutex> lock(buffer_lock_);
160 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400161 if (buf == nullptr) {
162 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500163 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530164 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400165 if (buf->DecRef()) {
166 handles_map_.erase(hnd);
167 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500168 FreeBuffer(buf);
169 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530170 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500171 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530172}
173
Naseer Ahmede36f2242017-12-01 15:33:56 -0500174Error BufferManager::LockBuffer(const private_handle_t *hnd, uint64_t usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400175 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500176 auto err = Error::NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400177 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530178
179 // If buffer is not meant for CPU return err
Naseer Ahmede36f2242017-12-01 15:33:56 -0500180 if (!CpuCanAccess(usage)) {
181 return Error::BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530182 }
183
Naseer Ahmed378d8582017-03-28 21:56:08 -0400184 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400185 if (buf == nullptr) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500186 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530187 }
188
Naseer Ahmed67330702017-05-02 15:00:26 -0400189 if (hnd->base == 0) {
190 // we need to map for real
191 err = MapBuffer(hnd);
192 }
193
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530194 // Invalidate if CPU reads in software and there are non-CPU
195 // writers. No need to do this for the metadata buffer as it is
196 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400197
198 // todo use handle here
Naseer Ahmede36f2242017-12-01 15:33:56 -0500199 if (err == Error::NONE && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530200 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
201 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700202 buf->ion_handle_main, CACHE_INVALIDATE, hnd->fd)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500203 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530204 }
205 }
206
207 // Mark the buffer to be flushed after CPU write.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500208 if (err == Error::NONE && CpuCanWrite(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530209 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
210 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
211 }
212
213 return err;
214}
215
Naseer Ahmede36f2242017-12-01 15:33:56 -0500216Error BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400217 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500218 auto status = Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530219
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530220 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400221 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400222 if (buf == nullptr) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500223 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400224 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530225
226 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
227 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700228 buf->ion_handle_main, CACHE_CLEAN, hnd->fd) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500229 status = Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530230 }
231 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
232 }
233
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530234 return status;
235}
236
Naseer Ahmede36f2242017-12-01 15:33:56 -0500237int BufferManager::GetHandleFlags(int format, uint64_t usage) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530238 int flags = 0;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500239 if (usage & BufferUsage::VIDEO_ENCODER) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530240 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
241 }
242
Naseer Ahmede36f2242017-12-01 15:33:56 -0500243 if (usage & BufferUsage::CAMERA_OUTPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530244 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
245 }
246
Naseer Ahmede36f2242017-12-01 15:33:56 -0500247 if (usage & BufferUsage::CAMERA_INPUT) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530248 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
249 }
250
Naseer Ahmede36f2242017-12-01 15:33:56 -0500251 if (usage & BufferUsage::COMPOSER_OVERLAY) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530252 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
253 }
254
Naseer Ahmede36f2242017-12-01 15:33:56 -0500255 if (usage & BufferUsage::GPU_TEXTURE) {
256 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
257 }
258
259 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
260 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
261 }
262
263 if (IsUBwcEnabled(format, usage)) {
264 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
265 }
266
267 if (usage & (BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK)) {
268 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
269 }
270
271 if ((usage & (BufferUsage::VIDEO_ENCODER | BufferUsage::VIDEO_DECODER |
272 BufferUsage::CAMERA_OUTPUT | BufferUsage::GPU_RENDER_TARGET))) {
273 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
274 }
275
276 if (!allocator_->UseUncached(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530277 flags |= private_handle_t::PRIV_FLAGS_CACHED;
278 }
279
280 return flags;
281}
282
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400283int BufferManager::GetBufferType(int inputFormat) {
284 int buffer_type = BUFFER_TYPE_VIDEO;
285 if (IsUncompressedRGBFormat(inputFormat)) {
286 // RGB formats
287 buffer_type = BUFFER_TYPE_UI;
288 }
289
290 return buffer_type;
291}
292
Naseer Ahmede36f2242017-12-01 15:33:56 -0500293Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
294 unsigned int bufferSize) {
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400295 if (!handle)
Naseer Ahmede36f2242017-12-01 15:33:56 -0500296 return Error::BAD_BUFFER;
297 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400298
Naseer Ahmede36f2242017-12-01 15:33:56 -0500299 uint64_t usage = descriptor.GetUsage();
300 int format = allocator_->GetImplDefinedFormat(usage, descriptor.GetFormat());
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400301 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400302
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400303 unsigned int size;
304 unsigned int alignedw, alignedh;
Naseer Ahmed95dc2882017-07-26 18:12:12 -0400305
306 int buffer_type = GetBufferType(format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700307 BufferInfo info = GetBufferInfo(descriptor);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500308 info.format = format;
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700309 GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400310 size = (bufferSize >= size) ? bufferSize : size;
311
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530312 int err = 0;
313 int flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400314 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530315 AllocData data;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500316 data.align = GetDataAlignment(format, usage);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400317 data.size = size;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500318 data.handle = (uintptr_t)handle;
319 data.uncached = allocator_->UseUncached(usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500320
321 // Allocate buffer memory
Naseer Ahmede36f2242017-12-01 15:33:56 -0500322 err = allocator_->AllocateMem(&data, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530323 if (err) {
324 ALOGE("gralloc failed to allocate err=%s", strerror(-err));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500325 return Error::NO_RESOURCES;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530326 }
327
Naseer Ahmede69031e2016-11-22 20:05:16 -0500328 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530329 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500330 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530331 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500332 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530333
Naseer Ahmede36f2242017-12-01 15:33:56 -0500334 err = allocator_->AllocateMem(&e_data, 0);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500335 if (err) {
336 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500337 return Error::NO_RESOURCES;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500338 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530339
Naseer Ahmede36f2242017-12-01 15:33:56 -0500340 flags = GetHandleFlags(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530341 flags |= data.alloc_type;
342
343 // Create handle
Naseer Ahmede36f2242017-12-01 15:33:56 -0500344 private_handle_t *hnd = new private_handle_t(
345 data.fd, e_data.fd, flags, INT(alignedw), INT(alignedh), descriptor.GetWidth(),
346 descriptor.GetHeight(), format, buffer_type, data.size, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530347
Naseer Ahmede69031e2016-11-22 20:05:16 -0500348 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400349 hnd->base = 0;
350 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400351 hnd->layer_count = layer_count;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530352
Arun Kumar K.Raf6a6052017-12-04 14:10:11 +0530353 if (buffer_type == BUFFER_TYPE_VIDEO) {
354 // set default csc to 601L for only video buffers
355 ColorSpace_t colorSpace = ITU_R_601;
356 setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
357 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530358 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400359 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500360 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
361 if (DEBUG) {
362 private_handle_t::Dump(hnd);
363 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500364 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530365}
366
Naseer Ahmede36f2242017-12-01 15:33:56 -0500367Error BufferManager::Dump(std::ostringstream *os) {
Naseer Ahmeddc918132017-03-07 15:25:14 -0500368 for (auto it : handles_map_) {
369 auto buf = it.second;
370 auto hnd = buf->handle;
371 *os << "handle id: " << std::setw(4) << hnd->id;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500372 *os << " fd: " << std::setw(3) << hnd->fd;
373 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
374 *os << " wxh: " << std::setw(4) << hnd->width << " x " << std::setw(4) << hnd->height;
375 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
376 *os << std::setw(4) << hnd->unaligned_height;
377 *os << " size: " << std::setw(9) << hnd->size;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500378 *os << std::hex << std::setfill('0');
Naseer Ahmede36f2242017-12-01 15:33:56 -0500379 *os << " priv_flags: "
380 << "0x" << std::setw(8) << hnd->flags;
381 *os << " usage: "
382 << "0x" << std::setw(8) << hnd->usage;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500383 // TODO(user): get format string from qdutils
Naseer Ahmede36f2242017-12-01 15:33:56 -0500384 *os << " format: "
385 << "0x" << std::setw(8) << hnd->format;
386 *os << std::dec << std::setfill(' ') << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500387 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500388 return Error::NONE;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500389}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500390} // namespace gralloc