Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Tharaga Balachandran | 821fe07 | 2020-02-18 17:02:26 -0500 | [diff] [blame] | 2 | * Copyright (c) 2011-2018, 2020 The Linux Foundation. All rights reserved. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 3 | * 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 Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 20 | #define DEBUG 0 |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 21 | |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 22 | #include <iomanip> |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 23 | #include <sstream> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 24 | #include <utility> |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 25 | #include <vector> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 26 | |
Tharaga Balachandran | 821fe07 | 2020-02-18 17:02:26 -0500 | [diff] [blame] | 27 | #include "gr_adreno_info.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 28 | #include "gr_buf_descriptor.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 29 | #include "gr_buf_mgr.h" |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 30 | #include "gr_priv_handle.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 31 | #include "qdMetaData.h" |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 32 | #include "qd_utils.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 33 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 34 | namespace gralloc { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 35 | |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 36 | static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) { |
| 37 | return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(), |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 38 | descriptor.GetUsage()); |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 41 | BufferManager::BufferManager() : next_id_(0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 42 | handles_map_.clear(); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 43 | allocator_ = new Allocator(); |
| 44 | allocator_->Init(); |
| 45 | } |
| 46 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 47 | BufferManager *BufferManager::GetInstance() { |
| 48 | static BufferManager *instance = new BufferManager(); |
| 49 | return instance; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | BufferManager::~BufferManager() { |
| 53 | if (allocator_) { |
| 54 | delete allocator_; |
| 55 | } |
| 56 | } |
| 57 | |
Tharaga Balachandran | 821fe07 | 2020-02-18 17:02:26 -0500 | [diff] [blame] | 58 | void BufferManager::SetGrallocDebugProperties(gralloc::GrallocProperties props) { |
| 59 | allocator_->SetProperties(props); |
| 60 | AdrenoMemInfo::GetInstance()->AdrenoSetProperties(props); |
| 61 | } |
| 62 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 63 | Error BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 64 | auto hnd = buf->handle; |
Naseer Ahmed | a186b47 | 2017-07-07 18:50:49 -0400 | [diff] [blame] | 65 | ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd); |
| 66 | |
| 67 | if (private_handle_t::validate(hnd) != 0) { |
| 68 | ALOGE("FreeBuffer: Invalid handle: %p", hnd); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 69 | return Error::BAD_BUFFER; |
Naseer Ahmed | a186b47 | 2017-07-07 18:50:49 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 72 | 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 Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 75 | } |
| 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 Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 79 | hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 80 | return Error::BAD_BUFFER; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 81 | } |
| 82 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 83 | private_handle_t *handle = const_cast<private_handle_t *>(hnd); |
Naseer Ahmed | 29a86dd | 2017-03-16 14:09:46 -0400 | [diff] [blame] | 84 | handle->fd = -1; |
| 85 | handle->fd_metadata = -1; |
Naseer Ahmed | 2f8f8d4 | 2017-06-09 18:17:26 -0400 | [diff] [blame] | 86 | if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 87 | delete handle; |
Naseer Ahmed | 2f8f8d4 | 2017-06-09 18:17:26 -0400 | [diff] [blame] | 88 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 89 | return Error::NONE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 90 | } |
| 91 | |
Naseer Ahmed | 920d71b | 2018-03-08 16:54:28 -0500 | [diff] [blame] | 92 | Error BufferManager::ValidateBufferSize(private_handle_t const *hnd, BufferInfo info) { |
| 93 | unsigned int size, alignedw, alignedh; |
Saurabh Dubey | 48b757d | 2018-05-24 19:13:53 +0530 | [diff] [blame] | 94 | info.format = GetImplDefinedFormat(info.usage, info.format); |
Ramkumar Radhakrishnan | 30ea0f0 | 2018-10-11 19:25:10 -0700 | [diff] [blame] | 95 | int ret = GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh); |
| 96 | if (ret < 0) { |
| 97 | return Error::BAD_BUFFER; |
| 98 | } |
Naseer Ahmed | 920d71b | 2018-03-08 16:54:28 -0500 | [diff] [blame] | 99 | 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 106 | void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, int ion_handle, |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 107 | int ion_handle_meta) { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 108 | auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta); |
| 109 | handles_map_.emplace(std::make_pair(hnd, buffer)); |
| 110 | } |
| 111 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 112 | Error BufferManager::ImportHandleLocked(private_handle_t *hnd) { |
Ramkumar Radhakrishnan | 372b5ee | 2018-09-20 13:17:36 -0700 | [diff] [blame] | 113 | if (private_handle_t::validate(hnd) != 0) { |
| 114 | ALOGE("ImportHandleLocked: Invalid handle: %p", hnd); |
| 115 | return Error::BAD_BUFFER; |
| 116 | } |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 117 | ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 118 | 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 121 | return Error::BAD_BUFFER; |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 122 | } |
| 123 | int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata); |
| 124 | if (ion_handle_meta < 0) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 125 | 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 Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 128 | } |
Naseer Ahmed | 920d71b | 2018-03-08 16:54:28 -0500 | [diff] [blame] | 129 | // 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 Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 133 | hnd->base = 0; |
| 134 | hnd->base_metadata = 0; |
Naseer Ahmed | 920d71b | 2018-03-08 16:54:28 -0500 | [diff] [blame] | 135 | hnd->gpuaddr = 0; |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 136 | RegisterHandleLocked(hnd, ion_handle, ion_handle_meta); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 137 | return Error::NONE; |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 140 | std::shared_ptr<BufferManager::Buffer> BufferManager::GetBufferFromHandleLocked( |
| 141 | const private_handle_t *hnd) { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 142 | 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 150 | Error BufferManager::MapBuffer(private_handle_t const *handle) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 151 | private_handle_t *hnd = const_cast<private_handle_t *>(handle); |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 152 | ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 153 | |
| 154 | hnd->base = 0; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 155 | if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset, |
| 156 | hnd->fd) != 0) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 157 | return Error::BAD_BUFFER; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 158 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 159 | return Error::NONE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 160 | } |
| 161 | |
Naseer Ahmed | 920d71b | 2018-03-08 16:54:28 -0500 | [diff] [blame] | 162 | Error 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 171 | Error BufferManager::RetainBuffer(private_handle_t const *hnd) { |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 172 | ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 173 | auto err = Error::NONE; |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 174 | std::lock_guard<std::mutex> lock(buffer_lock_); |
| 175 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 176 | if (buf != nullptr) { |
| 177 | buf->IncRef(); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 178 | } else { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 179 | private_handle_t *handle = const_cast<private_handle_t *>(hnd); |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 180 | err = ImportHandleLocked(handle); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 181 | } |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 182 | return err; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 183 | } |
| 184 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 185 | Error BufferManager::ReleaseBuffer(private_handle_t const *hnd) { |
Naseer Ahmed | a186b47 | 2017-07-07 18:50:49 -0400 | [diff] [blame] | 186 | ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd); |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 187 | std::lock_guard<std::mutex> lock(buffer_lock_); |
| 188 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 189 | if (buf == nullptr) { |
| 190 | ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 191 | return Error::BAD_BUFFER; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 192 | } else { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 193 | if (buf->DecRef()) { |
| 194 | handles_map_.erase(hnd); |
| 195 | // Unmap, close ion handle and close fd |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 196 | FreeBuffer(buf); |
| 197 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 198 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 199 | return Error::NONE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 200 | } |
| 201 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 202 | Error BufferManager::LockBuffer(const private_handle_t *hnd, uint64_t usage) { |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 203 | std::lock_guard<std::mutex> lock(buffer_lock_); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 204 | auto err = Error::NONE; |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 205 | ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 206 | |
| 207 | // If buffer is not meant for CPU return err |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 208 | if (!CpuCanAccess(usage)) { |
| 209 | return Error::BAD_VALUE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 210 | } |
| 211 | |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 212 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 213 | if (buf == nullptr) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 214 | return Error::BAD_BUFFER; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 215 | } |
| 216 | |
Naseer Ahmed | 6733070 | 2017-05-02 15:00:26 -0400 | [diff] [blame] | 217 | if (hnd->base == 0) { |
| 218 | // we need to map for real |
| 219 | err = MapBuffer(hnd); |
| 220 | } |
| 221 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 222 | // 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 Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 225 | |
| 226 | // todo use handle here |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 227 | if (err == Error::NONE && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) && |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 228 | (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) { |
| 229 | if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 230 | buf->ion_handle_main, CACHE_INVALIDATE, hnd->fd)) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 231 | return Error::BAD_BUFFER; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | // Mark the buffer to be flushed after CPU write. |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 236 | if (err == Error::NONE && CpuCanWrite(usage)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 237 | 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 244 | Error BufferManager::UnlockBuffer(const private_handle_t *handle) { |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 245 | std::lock_guard<std::mutex> lock(buffer_lock_); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 246 | auto status = Error::NONE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 247 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 248 | private_handle_t *hnd = const_cast<private_handle_t *>(handle); |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 249 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 250 | if (buf == nullptr) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 251 | return Error::BAD_BUFFER; |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 252 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 253 | |
| 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 Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 256 | buf->ion_handle_main, CACHE_CLEAN, hnd->fd) != 0) { |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 257 | status = Error::BAD_BUFFER; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 258 | } |
| 259 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 260 | } 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 Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 267 | return status; |
| 268 | } |
| 269 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 270 | Error BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle, |
Tharaga Balachandran | 887bed4 | 2019-09-26 19:17:58 -0400 | [diff] [blame] | 271 | unsigned int bufferSize, bool testAlloc) { |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 272 | if (!handle) |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 273 | return Error::BAD_BUFFER; |
| 274 | std::lock_guard<std::mutex> buffer_lock(buffer_lock_); |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 275 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 276 | uint64_t usage = descriptor.GetUsage(); |
Saurabh Dubey | 48b757d | 2018-05-24 19:13:53 +0530 | [diff] [blame] | 277 | int format = GetImplDefinedFormat(usage, descriptor.GetFormat()); |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 278 | uint32_t layer_count = descriptor.GetLayerCount(); |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 279 | |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 280 | unsigned int size; |
| 281 | unsigned int alignedw, alignedh; |
Ramkumar Radhakrishnan | 30ea0f0 | 2018-10-11 19:25:10 -0700 | [diff] [blame] | 282 | int err = 0; |
Naseer Ahmed | 95dc288 | 2017-07-26 18:12:12 -0400 | [diff] [blame] | 283 | |
| 284 | int buffer_type = GetBufferType(format); |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 285 | BufferInfo info = GetBufferInfo(descriptor); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 286 | info.format = format; |
Saurabh Dubey | 7095738 | 2018-05-15 16:05:32 +0530 | [diff] [blame] | 287 | info.layer_count = layer_count; |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 288 | |
Saurabh Dubey | b28b82b | 2018-05-29 09:46:41 +0530 | [diff] [blame] | 289 | GraphicsMetadata graphics_metadata = {}; |
Ramkumar Radhakrishnan | 30ea0f0 | 2018-10-11 19:25:10 -0700 | [diff] [blame] | 290 | err = GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh, &graphics_metadata); |
| 291 | if (err < 0) { |
| 292 | return Error::BAD_DESCRIPTOR; |
| 293 | } |
Saurabh Dubey | b28b82b | 2018-05-29 09:46:41 +0530 | [diff] [blame] | 294 | |
Tharaga Balachandran | 887bed4 | 2019-09-26 19:17:58 -0400 | [diff] [blame] | 295 | if (testAlloc) { |
| 296 | return Error::NONE; |
| 297 | } |
| 298 | |
Saurabh Dubey | b28b82b | 2018-05-29 09:46:41 +0530 | [diff] [blame] | 299 | size = (bufferSize >= size) ? bufferSize : size; |
Saurabh Dubey | 48b757d | 2018-05-24 19:13:53 +0530 | [diff] [blame] | 300 | uint64_t flags = 0; |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 301 | auto page_size = UINT(getpagesize()); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 302 | AllocData data; |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 303 | data.align = GetDataAlignment(format, usage); |
Naseer Ahmed | f8e9c43 | 2017-06-13 17:45:12 -0400 | [diff] [blame] | 304 | data.size = size; |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 305 | data.handle = (uintptr_t)handle; |
Saurabh Dubey | 48b757d | 2018-05-24 19:13:53 +0530 | [diff] [blame] | 306 | data.uncached = UseUncached(format, usage); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 307 | |
| 308 | // Allocate buffer memory |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 309 | err = allocator_->AllocateMem(&data, usage, format); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 310 | if (err) { |
Ramkumar Radhakrishnan | 30ea0f0 | 2018-10-11 19:25:10 -0700 | [diff] [blame] | 311 | 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 313 | return Error::NO_RESOURCES; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 314 | } |
| 315 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 316 | // Allocate memory for MetaData |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 317 | AllocData e_data; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 318 | e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 319 | e_data.handle = data.handle; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 320 | e_data.align = page_size; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 321 | |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 322 | err = allocator_->AllocateMem(&e_data, 0, 0); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 323 | if (err) { |
| 324 | ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err)); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 325 | return Error::NO_RESOURCES; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 326 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 327 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 328 | flags = GetHandleFlags(format, usage); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 329 | flags |= data.alloc_type; |
| 330 | |
| 331 | // Create handle |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 332 | private_handle_t *hnd = new private_handle_t( |
Saurabh Dubey | 48b757d | 2018-05-24 19:13:53 +0530 | [diff] [blame] | 333 | data.fd, e_data.fd, INT(flags), INT(alignedw), INT(alignedh), descriptor.GetWidth(), |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 334 | descriptor.GetHeight(), format, buffer_type, data.size, usage); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 335 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 336 | hnd->id = ++next_id_; |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 337 | hnd->base = 0; |
| 338 | hnd->base_metadata = 0; |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 339 | hnd->layer_count = layer_count; |
Arun Kumar K.R | 0574622 | 2018-04-04 10:45:58 +0530 | [diff] [blame] | 340 | // 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 Chavan | 9a1704b | 2018-07-06 18:19:46 -0700 | [diff] [blame] | 342 | setMetaDataAndUnmap(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace)); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 343 | |
Saurabh Dubey | efd7f9e | 2018-06-15 18:00:02 +0530 | [diff] [blame] | 344 | bool use_adreno_for_size = CanUseAdrenoForSize(buffer_type, usage); |
Saurabh Dubey | b28b82b | 2018-05-29 09:46:41 +0530 | [diff] [blame] | 345 | if (use_adreno_for_size) { |
Praveen Chavan | 9a1704b | 2018-07-06 18:19:46 -0700 | [diff] [blame] | 346 | setMetaDataAndUnmap(hnd, SET_GRAPHICS_METADATA, reinterpret_cast<void *>(&graphics_metadata)); |
Saurabh Dubey | b28b82b | 2018-05-29 09:46:41 +0530 | [diff] [blame] | 347 | } |
| 348 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 349 | *handle = hnd; |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 350 | RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle); |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 351 | ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id); |
| 352 | if (DEBUG) { |
| 353 | private_handle_t::Dump(hnd); |
| 354 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 355 | return Error::NONE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 356 | } |
| 357 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 358 | Error BufferManager::Dump(std::ostringstream *os) { |
Uday Kiran Pichika | b58d884 | 2018-04-10 15:37:35 +0530 | [diff] [blame] | 359 | std::lock_guard<std::mutex> buffer_lock(buffer_lock_); |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 360 | 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 Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 364 | *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 Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 370 | *os << std::hex << std::setfill('0'); |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 371 | *os << " priv_flags: " |
| 372 | << "0x" << std::setw(8) << hnd->flags; |
| 373 | *os << " usage: " |
| 374 | << "0x" << std::setw(8) << hnd->usage; |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 375 | // TODO(user): get format string from qdutils |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 376 | *os << " format: " |
| 377 | << "0x" << std::setw(8) << hnd->format; |
| 378 | *os << std::dec << std::setfill(' ') << std::endl; |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 379 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 380 | return Error::NONE; |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 381 | } |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 382 | } // namespace gralloc |