blob: b936539b76c405e98fad59ce2806ef5c9cd7bfcd [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Tharaga Balachandran821fe072020-02-18 17:02:26 -05002 * Copyright (c) 2011-2018, 2020 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
Tharaga Balachandran821fe072020-02-18 17:02:26 -050027#include "gr_adreno_info.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053028#include "gr_buf_descriptor.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053029#include "gr_buf_mgr.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050030#include "gr_priv_handle.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 handles_map_.clear();
Naseer Ahmede69031e2016-11-22 20:05:16 -050043 allocator_ = new Allocator();
44 allocator_->Init();
45}
46
Naseer Ahmede36f2242017-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
Tharaga Balachandran821fe072020-02-18 17:02:26 -050058void BufferManager::SetGrallocDebugProperties(gralloc::GrallocProperties props) {
59 allocator_->SetProperties(props);
60 AdrenoMemInfo::GetInstance()->AdrenoSetProperties(props);
61}
62
Naseer Ahmede36f2242017-12-01 15:33:56 -050063Error BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) {
Naseer Ahmede69031e2016-11-22 20:05:16 -050064 auto hnd = buf->handle;
Naseer Ahmeda186b472017-07-07 18:50:49 -040065 ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd);
66
67 if (private_handle_t::validate(hnd) != 0) {
68 ALOGE("FreeBuffer: Invalid handle: %p", hnd);
Naseer Ahmede36f2242017-12-01 15:33:56 -050069 return Error::BAD_BUFFER;
Naseer Ahmeda186b472017-07-07 18:50:49 -040070 }
71
Naseer Ahmede36f2242017-12-01 15:33:56 -050072 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, hnd->fd,
73 buf->ion_handle_main) != 0) {
74 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053075 }
76
77 unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE);
78 if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -050079 hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050080 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053081 }
82
Naseer Ahmede36f2242017-12-01 15:33:56 -050083 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed29a86dd2017-03-16 14:09:46 -040084 handle->fd = -1;
85 handle->fd_metadata = -1;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040086 if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050087 delete handle;
Naseer Ahmed2f8f8d42017-06-09 18:17:26 -040088 }
Naseer Ahmede36f2242017-12-01 15:33:56 -050089 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053090}
91
Naseer Ahmed920d71b2018-03-08 16:54:28 -050092Error BufferManager::ValidateBufferSize(private_handle_t const *hnd, BufferInfo info) {
93 unsigned int size, alignedw, alignedh;
Saurabh Dubey48b757d2018-05-24 19:13:53 +053094 info.format = GetImplDefinedFormat(info.usage, info.format);
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -070095 int ret = GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh);
96 if (ret < 0) {
97 return Error::BAD_BUFFER;
98 }
Naseer Ahmed920d71b2018-03-08 16:54:28 -050099 auto ion_fd_size = static_cast<unsigned int>(lseek(hnd->fd, 0, SEEK_END));
100 if (size != ion_fd_size) {
101 return Error::BAD_VALUE;
102 }
103 return Error::NONE;
104}
105
Naseer Ahmede36f2242017-12-01 15:33:56 -0500106void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, int ion_handle,
Naseer Ahmed378d8582017-03-28 21:56:08 -0400107 int ion_handle_meta) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400108 auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta);
109 handles_map_.emplace(std::make_pair(hnd, buffer));
110}
111
Naseer Ahmede36f2242017-12-01 15:33:56 -0500112Error BufferManager::ImportHandleLocked(private_handle_t *hnd) {
Ramkumar Radhakrishnan372b5ee2018-09-20 13:17:36 -0700113 if (private_handle_t::validate(hnd) != 0) {
114 ALOGE("ImportHandleLocked: Invalid handle: %p", hnd);
115 return Error::BAD_BUFFER;
116 }
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400117 ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400118 int ion_handle = allocator_->ImportBuffer(hnd->fd);
119 if (ion_handle < 0) {
120 ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500121 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400122 }
123 int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata);
124 if (ion_handle_meta < 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500125 ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd,
126 hnd->id);
127 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400128 }
Naseer Ahmed920d71b2018-03-08 16:54:28 -0500129 // Initialize members that aren't transported
130 hnd->size = static_cast<unsigned int>(lseek(hnd->fd, 0, SEEK_END));
131 hnd->offset = 0;
132 hnd->offset_metadata = 0;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400133 hnd->base = 0;
134 hnd->base_metadata = 0;
Naseer Ahmed920d71b2018-03-08 16:54:28 -0500135 hnd->gpuaddr = 0;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400136 RegisterHandleLocked(hnd, ion_handle, ion_handle_meta);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500137 return Error::NONE;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400138}
139
Naseer Ahmede36f2242017-12-01 15:33:56 -0500140std::shared_ptr<BufferManager::Buffer> BufferManager::GetBufferFromHandleLocked(
141 const private_handle_t *hnd) {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400142 auto it = handles_map_.find(hnd);
143 if (it != handles_map_.end()) {
144 return it->second;
145 } else {
146 return nullptr;
147 }
148}
149
Naseer Ahmede36f2242017-12-01 15:33:56 -0500150Error BufferManager::MapBuffer(private_handle_t const *handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530151 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400152 ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530153
154 hnd->base = 0;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530155 if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset,
156 hnd->fd) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500157 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530158 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500159 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530160}
161
Naseer Ahmed920d71b2018-03-08 16:54:28 -0500162Error BufferManager::IsBufferImported(const private_handle_t *hnd) {
163 std::lock_guard<std::mutex> lock(buffer_lock_);
164 auto buf = GetBufferFromHandleLocked(hnd);
165 if (buf != nullptr) {
166 return Error::NONE;
167 }
168 return Error::BAD_BUFFER;
169}
170
Naseer Ahmede36f2242017-12-01 15:33:56 -0500171Error BufferManager::RetainBuffer(private_handle_t const *hnd) {
Naseer Ahmed699b4572017-03-09 12:28:45 -0500172 ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500173 auto err = Error::NONE;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400174 std::lock_guard<std::mutex> lock(buffer_lock_);
175 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400176 if (buf != nullptr) {
177 buf->IncRef();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530178 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400179 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400180 err = ImportHandleLocked(handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530181 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400182 return err;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530183}
184
Naseer Ahmede36f2242017-12-01 15:33:56 -0500185Error BufferManager::ReleaseBuffer(private_handle_t const *hnd) {
Naseer Ahmeda186b472017-07-07 18:50:49 -0400186 ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400187 std::lock_guard<std::mutex> lock(buffer_lock_);
188 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400189 if (buf == nullptr) {
190 ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500191 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530192 } else {
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400193 if (buf->DecRef()) {
194 handles_map_.erase(hnd);
195 // Unmap, close ion handle and close fd
Naseer Ahmede69031e2016-11-22 20:05:16 -0500196 FreeBuffer(buf);
197 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530198 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500199 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530200}
201
Naseer Ahmede36f2242017-12-01 15:33:56 -0500202Error BufferManager::LockBuffer(const private_handle_t *hnd, uint64_t usage) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400203 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500204 auto err = Error::NONE;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400205 ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530206
207 // If buffer is not meant for CPU return err
Naseer Ahmede36f2242017-12-01 15:33:56 -0500208 if (!CpuCanAccess(usage)) {
209 return Error::BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530210 }
211
Naseer Ahmed378d8582017-03-28 21:56:08 -0400212 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400213 if (buf == nullptr) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500214 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530215 }
216
Naseer Ahmed67330702017-05-02 15:00:26 -0400217 if (hnd->base == 0) {
218 // we need to map for real
219 err = MapBuffer(hnd);
220 }
221
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530222 // Invalidate if CPU reads in software and there are non-CPU
223 // writers. No need to do this for the metadata buffer as it is
224 // only read/written in software.
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400225
226 // todo use handle here
Naseer Ahmede36f2242017-12-01 15:33:56 -0500227 if (err == Error::NONE && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) &&
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530228 (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) {
229 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700230 buf->ion_handle_main, CACHE_INVALIDATE, hnd->fd)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500231 return Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530232 }
233 }
234
235 // Mark the buffer to be flushed after CPU write.
Naseer Ahmede36f2242017-12-01 15:33:56 -0500236 if (err == Error::NONE && CpuCanWrite(usage)) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530237 private_handle_t *handle = const_cast<private_handle_t *>(hnd);
238 handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
239 }
240
241 return err;
242}
243
Naseer Ahmede36f2242017-12-01 15:33:56 -0500244Error BufferManager::UnlockBuffer(const private_handle_t *handle) {
Naseer Ahmed378d8582017-03-28 21:56:08 -0400245 std::lock_guard<std::mutex> lock(buffer_lock_);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500246 auto status = Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530247
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530248 private_handle_t *hnd = const_cast<private_handle_t *>(handle);
Naseer Ahmed378d8582017-03-28 21:56:08 -0400249 auto buf = GetBufferFromHandleLocked(hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400250 if (buf == nullptr) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500251 return Error::BAD_BUFFER;
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400252 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530253
254 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
255 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700256 buf->ion_handle_main, CACHE_CLEAN, hnd->fd) != 0) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500257 status = Error::BAD_BUFFER;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530258 }
259 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700260 } else {
261 if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
262 buf->ion_handle_main, CACHE_READ_DONE, hnd->fd) != 0) {
263 status = Error::BAD_BUFFER;
264 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530265 }
266
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530267 return status;
268}
269
Naseer Ahmede36f2242017-12-01 15:33:56 -0500270Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
Tharaga Balachandran887bed42019-09-26 19:17:58 -0400271 unsigned int bufferSize, bool testAlloc) {
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400272 if (!handle)
Naseer Ahmede36f2242017-12-01 15:33:56 -0500273 return Error::BAD_BUFFER;
274 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400275
Naseer Ahmede36f2242017-12-01 15:33:56 -0500276 uint64_t usage = descriptor.GetUsage();
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530277 int format = GetImplDefinedFormat(usage, descriptor.GetFormat());
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400278 uint32_t layer_count = descriptor.GetLayerCount();
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400279
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400280 unsigned int size;
281 unsigned int alignedw, alignedh;
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700282 int err = 0;
Naseer Ahmed95dc2882017-07-26 18:12:12 -0400283
284 int buffer_type = GetBufferType(format);
Saurabh Shah14c8e5b2017-04-07 10:37:23 -0700285 BufferInfo info = GetBufferInfo(descriptor);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500286 info.format = format;
Saurabh Dubey70957382018-05-15 16:05:32 +0530287 info.layer_count = layer_count;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400288
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530289 GraphicsMetadata graphics_metadata = {};
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700290 err = GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh, &graphics_metadata);
291 if (err < 0) {
292 return Error::BAD_DESCRIPTOR;
293 }
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530294
Tharaga Balachandran887bed42019-09-26 19:17:58 -0400295 if (testAlloc) {
296 return Error::NONE;
297 }
298
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530299 size = (bufferSize >= size) ? bufferSize : size;
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530300 uint64_t flags = 0;
Naseer Ahmed4c0eec92017-03-27 16:51:48 -0400301 auto page_size = UINT(getpagesize());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530302 AllocData data;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500303 data.align = GetDataAlignment(format, usage);
Naseer Ahmedf8e9c432017-06-13 17:45:12 -0400304 data.size = size;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500305 data.handle = (uintptr_t)handle;
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530306 data.uncached = UseUncached(format, usage);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500307
308 // Allocate buffer memory
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700309 err = allocator_->AllocateMem(&data, usage, format);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530310 if (err) {
Ramkumar Radhakrishnan30ea0f02018-10-11 19:25:10 -0700311 ALOGE("gralloc failed to allocate err=%s format %d size %d WxH %dx%d usage %" PRIu64,
312 strerror(-err), format, size, alignedw, alignedh, usage);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500313 return Error::NO_RESOURCES;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530314 }
315
Naseer Ahmede69031e2016-11-22 20:05:16 -0500316 // Allocate memory for MetaData
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530317 AllocData e_data;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500318 e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530319 e_data.handle = data.handle;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500320 e_data.align = page_size;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530321
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700322 err = allocator_->AllocateMem(&e_data, 0, 0);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500323 if (err) {
324 ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500325 return Error::NO_RESOURCES;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500326 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530327
Naseer Ahmede36f2242017-12-01 15:33:56 -0500328 flags = GetHandleFlags(format, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530329 flags |= data.alloc_type;
330
331 // Create handle
Naseer Ahmede36f2242017-12-01 15:33:56 -0500332 private_handle_t *hnd = new private_handle_t(
Saurabh Dubey48b757d2018-05-24 19:13:53 +0530333 data.fd, e_data.fd, INT(flags), INT(alignedw), INT(alignedh), descriptor.GetWidth(),
Naseer Ahmede36f2242017-12-01 15:33:56 -0500334 descriptor.GetHeight(), format, buffer_type, data.size, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530335
Naseer Ahmede69031e2016-11-22 20:05:16 -0500336 hnd->id = ++next_id_;
Naseer Ahmed49f2e9c2017-03-23 22:13:17 -0400337 hnd->base = 0;
338 hnd->base_metadata = 0;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400339 hnd->layer_count = layer_count;
Arun Kumar K.R05746222018-04-04 10:45:58 +0530340 // set default csc as 709, but for video(yuv) its 601L
341 ColorSpace_t colorSpace = (buffer_type == BUFFER_TYPE_VIDEO) ? ITU_R_601 : ITU_R_709;
Praveen Chavan9a1704b2018-07-06 18:19:46 -0700342 setMetaDataAndUnmap(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530343
Saurabh Dubeyefd7f9e2018-06-15 18:00:02 +0530344 bool use_adreno_for_size = CanUseAdrenoForSize(buffer_type, usage);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530345 if (use_adreno_for_size) {
Praveen Chavan9a1704b2018-07-06 18:19:46 -0700346 setMetaDataAndUnmap(hnd, SET_GRAPHICS_METADATA, reinterpret_cast<void *>(&graphics_metadata));
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530347 }
348
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530349 *handle = hnd;
Naseer Ahmed378d8582017-03-28 21:56:08 -0400350 RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle);
Naseer Ahmed699b4572017-03-09 12:28:45 -0500351 ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id);
352 if (DEBUG) {
353 private_handle_t::Dump(hnd);
354 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500355 return Error::NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530356}
357
Naseer Ahmede36f2242017-12-01 15:33:56 -0500358Error BufferManager::Dump(std::ostringstream *os) {
Uday Kiran Pichikab58d8842018-04-10 15:37:35 +0530359 std::lock_guard<std::mutex> buffer_lock(buffer_lock_);
Naseer Ahmeddc918132017-03-07 15:25:14 -0500360 for (auto it : handles_map_) {
361 auto buf = it.second;
362 auto hnd = buf->handle;
363 *os << "handle id: " << std::setw(4) << hnd->id;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500364 *os << " fd: " << std::setw(3) << hnd->fd;
365 *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata;
366 *os << " wxh: " << std::setw(4) << hnd->width << " x " << std::setw(4) << hnd->height;
367 *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x ";
368 *os << std::setw(4) << hnd->unaligned_height;
369 *os << " size: " << std::setw(9) << hnd->size;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500370 *os << std::hex << std::setfill('0');
Naseer Ahmede36f2242017-12-01 15:33:56 -0500371 *os << " priv_flags: "
372 << "0x" << std::setw(8) << hnd->flags;
373 *os << " usage: "
374 << "0x" << std::setw(8) << hnd->usage;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500375 // TODO(user): get format string from qdutils
Naseer Ahmede36f2242017-12-01 15:33:56 -0500376 *os << " format: "
377 << "0x" << std::setw(8) << hnd->format;
378 *os << std::dec << std::setfill(' ') << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500379 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500380 return Error::NONE;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500381}
Naseer Ahmede36f2242017-12-01 15:33:56 -0500382} // namespace gralloc