Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Saurabh Shah | c5b2b70 | 2016-10-24 17:16:01 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-2017 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> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 23 | #include <utility> |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 24 | #include <vector> |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 25 | #include <sstream> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 26 | |
| 27 | #include "qd_utils.h" |
| 28 | #include "gr_priv_handle.h" |
| 29 | #include "gr_buf_descriptor.h" |
| 30 | #include "gr_utils.h" |
| 31 | #include "gr_buf_mgr.h" |
| 32 | #include "qdMetaData.h" |
| 33 | |
| 34 | namespace gralloc1 { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 35 | std::atomic<gralloc1_buffer_descriptor_t> BufferDescriptor::next_id_(1); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 36 | |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 37 | static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) { |
| 38 | return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(), |
| 39 | descriptor.GetProducerUsage(), descriptor.GetConsumerUsage()); |
| 40 | } |
| 41 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 42 | BufferManager::BufferManager() : next_id_(0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 43 | char property[PROPERTY_VALUE_MAX]; |
| 44 | |
| 45 | // Map framebuffer memory |
| 46 | if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && |
| 47 | (!strncmp(property, "1", PROPERTY_VALUE_MAX) || |
| 48 | (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) { |
| 49 | map_fb_mem_ = true; |
| 50 | } |
| 51 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 52 | handles_map_.clear(); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 53 | allocator_ = new Allocator(); |
| 54 | allocator_->Init(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | gralloc1_error_t BufferManager::CreateBufferDescriptor( |
| 59 | gralloc1_buffer_descriptor_t *descriptor_id) { |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 60 | std::lock_guard<std::mutex> lock(descriptor_lock_); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 61 | auto descriptor = std::make_shared<BufferDescriptor>(); |
| 62 | descriptors_map_.emplace(descriptor->GetId(), descriptor); |
| 63 | *descriptor_id = descriptor->GetId(); |
| 64 | return GRALLOC1_ERROR_NONE; |
| 65 | } |
| 66 | |
| 67 | gralloc1_error_t BufferManager::DestroyBufferDescriptor( |
| 68 | gralloc1_buffer_descriptor_t descriptor_id) { |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 69 | std::lock_guard<std::mutex> lock(descriptor_lock_); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 70 | const auto descriptor = descriptors_map_.find(descriptor_id); |
| 71 | if (descriptor == descriptors_map_.end()) { |
| 72 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 73 | } |
| 74 | descriptors_map_.erase(descriptor); |
| 75 | return GRALLOC1_ERROR_NONE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | BufferManager::~BufferManager() { |
| 79 | if (allocator_) { |
| 80 | delete allocator_; |
| 81 | } |
| 82 | } |
| 83 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 84 | gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 85 | const gralloc1_buffer_descriptor_t *descriptor_ids, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 86 | buffer_handle_t *out_buffers) { |
| 87 | bool shared = true; |
| 88 | gralloc1_error_t status = GRALLOC1_ERROR_NONE; |
| 89 | |
| 90 | // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported |
| 91 | // client can ask to test the allocation by passing NULL out_buffers |
| 92 | bool test_allocate = !out_buffers; |
| 93 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 94 | // Validate descriptors |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 95 | std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 96 | std::vector<std::shared_ptr<BufferDescriptor>> descriptors; |
| 97 | for (uint32_t i = 0; i < num_descriptors; i++) { |
| 98 | const auto map_descriptor = descriptors_map_.find(descriptor_ids[i]); |
| 99 | if (map_descriptor == descriptors_map_.end()) { |
| 100 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 101 | } else { |
| 102 | descriptors.push_back(map_descriptor->second); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Resolve implementation defined formats |
| 107 | for (auto &descriptor : descriptors) { |
| 108 | descriptor->SetColorFormat(allocator_->GetImplDefinedFormat(descriptor->GetProducerUsage(), |
| 109 | descriptor->GetConsumerUsage(), |
| 110 | descriptor->GetFormat())); |
| 111 | } |
| 112 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 113 | // Check if input descriptors can be supported AND |
| 114 | // Find out if a single buffer can be shared for all the given input descriptors |
| 115 | uint32_t i = 0; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 116 | ssize_t max_buf_index = -1; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 117 | shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index); |
| 118 | |
| 119 | if (test_allocate) { |
| 120 | status = shared ? GRALLOC1_ERROR_NOT_SHARED : status; |
| 121 | return status; |
| 122 | } |
| 123 | |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 124 | std::lock_guard<std::mutex> buffer_lock(buffer_lock_); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 125 | if (shared && (max_buf_index >= 0)) { |
| 126 | // Allocate one and duplicate/copy the handles for each descriptor |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 127 | if (AllocateBuffer(*descriptors[UINT(max_buf_index)], &out_buffers[max_buf_index])) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 128 | return GRALLOC1_ERROR_NO_RESOURCES; |
| 129 | } |
| 130 | |
| 131 | for (i = 0; i < num_descriptors; i++) { |
| 132 | // Create new handle for a given descriptor. |
| 133 | // Current assumption is even MetaData memory would be same |
| 134 | // Need to revisit if there is a need for own metadata memory |
| 135 | if (i != UINT(max_buf_index)) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 136 | CreateSharedHandle(out_buffers[max_buf_index], *descriptors[i], &out_buffers[i]); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } else { |
| 140 | // Buffer sharing is not feasible. |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 141 | // Allocate separate buffer for each descriptor |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 142 | for (i = 0; i < num_descriptors; i++) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 143 | if (AllocateBuffer(*descriptors[i], &out_buffers[i])) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 144 | return GRALLOC1_ERROR_NO_RESOURCES; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Allocation is successful. If backstore is not shared inform the client. |
| 150 | if (!shared) { |
| 151 | return GRALLOC1_ERROR_NOT_SHARED; |
| 152 | } |
| 153 | |
| 154 | return status; |
| 155 | } |
| 156 | |
| 157 | void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor, |
| 158 | buffer_handle_t *outbuffer) { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 159 | // TODO(user): This path is not verified |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 160 | private_handle_t const *input = reinterpret_cast<private_handle_t const *>(inbuffer); |
| 161 | |
| 162 | // Get Buffer attributes or dimension |
| 163 | unsigned int alignedw = 0, alignedh = 0; |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 164 | BufferInfo info = GetBufferInfo(descriptor); |
| 165 | |
| 166 | GetAlignedWidthAndHeight(info, &alignedw, &alignedh); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 167 | |
| 168 | // create new handle from input reference handle and given descriptor |
| 169 | int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(), |
| 170 | descriptor.GetConsumerUsage()); |
| 171 | int buffer_type = GetBufferType(descriptor.GetFormat()); |
| 172 | |
| 173 | // Duplicate the fds |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 174 | // TODO(user): Not sure what to do for fb_id. Use duped fd and new dimensions? |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 175 | private_handle_t *out_hnd = new private_handle_t(dup(input->fd), |
| 176 | dup(input->fd_metadata), |
| 177 | flags, |
| 178 | INT(alignedw), |
| 179 | INT(alignedh), |
| 180 | descriptor.GetWidth(), |
| 181 | descriptor.GetHeight(), |
| 182 | descriptor.GetFormat(), |
| 183 | buffer_type, |
| 184 | input->size, |
| 185 | descriptor.GetProducerUsage(), |
| 186 | descriptor.GetConsumerUsage()); |
| 187 | out_hnd->id = ++next_id_; |
| 188 | // TODO(user): Base address of shared handle and ion handles |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 189 | RegisterHandleLocked(out_hnd, -1, -1); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 190 | *outbuffer = out_hnd; |
| 191 | } |
| 192 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 193 | gralloc1_error_t BufferManager::FreeBuffer(std::shared_ptr<Buffer> buf) { |
| 194 | auto hnd = buf->handle; |
Naseer Ahmed | a186b47 | 2017-07-07 18:50:49 -0400 | [diff] [blame] | 195 | ALOGD_IF(DEBUG, "FreeBuffer handle:%p", hnd); |
| 196 | |
| 197 | if (private_handle_t::validate(hnd) != 0) { |
| 198 | ALOGE("FreeBuffer: Invalid handle: %p", hnd); |
| 199 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 200 | } |
| 201 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 202 | if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 203 | hnd->fd, buf->ion_handle_main) != 0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 204 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 205 | } |
| 206 | |
| 207 | unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE); |
| 208 | if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base_metadata), meta_size, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 209 | hnd->offset_metadata, hnd->fd_metadata, buf->ion_handle_meta) != 0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 210 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 211 | } |
| 212 | |
Naseer Ahmed | 29a86dd | 2017-03-16 14:09:46 -0400 | [diff] [blame] | 213 | private_handle_t * handle = const_cast<private_handle_t *>(hnd); |
| 214 | handle->fd = -1; |
| 215 | handle->fd_metadata = -1; |
Naseer Ahmed | 2f8f8d4 | 2017-06-09 18:17:26 -0400 | [diff] [blame] | 216 | if (!(handle->flags & private_handle_t::PRIV_FLAGS_CLIENT_ALLOCATED)) { |
| 217 | delete handle; |
| 218 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 219 | return GRALLOC1_ERROR_NONE; |
| 220 | } |
| 221 | |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 222 | void BufferManager::RegisterHandleLocked(const private_handle_t *hnd, |
| 223 | int ion_handle, |
| 224 | int ion_handle_meta) { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 225 | auto buffer = std::make_shared<Buffer>(hnd, ion_handle, ion_handle_meta); |
| 226 | handles_map_.emplace(std::make_pair(hnd, buffer)); |
| 227 | } |
| 228 | |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 229 | gralloc1_error_t BufferManager::ImportHandleLocked(private_handle_t *hnd) { |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 230 | ALOGD_IF(DEBUG, "Importing handle:%p id: %" PRIu64, hnd, hnd->id); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 231 | int ion_handle = allocator_->ImportBuffer(hnd->fd); |
| 232 | if (ion_handle < 0) { |
| 233 | ALOGE("Failed to import ion buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, hnd->fd, hnd->id); |
| 234 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 235 | } |
| 236 | int ion_handle_meta = allocator_->ImportBuffer(hnd->fd_metadata); |
| 237 | if (ion_handle_meta < 0) { |
| 238 | ALOGE("Failed to import ion metadata buffer: hnd: %p, fd:%d, id:%" PRIu64, hnd, |
| 239 | hnd->fd, hnd->id); |
| 240 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 241 | } |
| 242 | // Set base pointers to NULL since the data here was received over binder |
| 243 | hnd->base = 0; |
| 244 | hnd->base_metadata = 0; |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 245 | RegisterHandleLocked(hnd, ion_handle, ion_handle_meta); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 246 | return GRALLOC1_ERROR_NONE; |
| 247 | } |
| 248 | |
| 249 | std::shared_ptr<BufferManager::Buffer> |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 250 | BufferManager::GetBufferFromHandleLocked(const private_handle_t *hnd) { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 251 | auto it = handles_map_.find(hnd); |
| 252 | if (it != handles_map_.end()) { |
| 253 | return it->second; |
| 254 | } else { |
| 255 | return nullptr; |
| 256 | } |
| 257 | } |
| 258 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 259 | gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) { |
| 260 | private_handle_t *hnd = const_cast<private_handle_t *>(handle); |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 261 | ALOGD_IF(DEBUG, "Map buffer handle:%p id: %" PRIu64, hnd, hnd->id); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 262 | |
| 263 | hnd->base = 0; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 264 | if (allocator_->MapBuffer(reinterpret_cast<void **>(&hnd->base), hnd->size, hnd->offset, |
| 265 | hnd->fd) != 0) { |
| 266 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 267 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 268 | return GRALLOC1_ERROR_NONE; |
| 269 | } |
| 270 | |
| 271 | gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) { |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 272 | ALOGD_IF(DEBUG, "Retain buffer handle:%p id: %" PRIu64, hnd, hnd->id); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 273 | gralloc1_error_t err = GRALLOC1_ERROR_NONE; |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 274 | std::lock_guard<std::mutex> lock(buffer_lock_); |
| 275 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 276 | if (buf != nullptr) { |
| 277 | buf->IncRef(); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 278 | } else { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 279 | private_handle_t *handle = const_cast<private_handle_t *>(hnd); |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 280 | err = ImportHandleLocked(handle); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 281 | } |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 282 | return err; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) { |
Naseer Ahmed | a186b47 | 2017-07-07 18:50:49 -0400 | [diff] [blame] | 286 | ALOGD_IF(DEBUG, "Release buffer handle:%p", hnd); |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 287 | std::lock_guard<std::mutex> lock(buffer_lock_); |
| 288 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 289 | if (buf == nullptr) { |
| 290 | ALOGE("Could not find handle: %p id: %" PRIu64, hnd, hnd->id); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 291 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 292 | } else { |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 293 | if (buf->DecRef()) { |
| 294 | handles_map_.erase(hnd); |
| 295 | // Unmap, close ion handle and close fd |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 296 | FreeBuffer(buf); |
| 297 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 298 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 299 | return GRALLOC1_ERROR_NONE; |
| 300 | } |
| 301 | |
| 302 | gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd, |
| 303 | gralloc1_producer_usage_t prod_usage, |
| 304 | gralloc1_consumer_usage_t cons_usage) { |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 305 | std::lock_guard<std::mutex> lock(buffer_lock_); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 306 | gralloc1_error_t err = GRALLOC1_ERROR_NONE; |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 307 | ALOGD_IF(DEBUG, "LockBuffer buffer handle:%p id: %" PRIu64, hnd, hnd->id); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 308 | |
| 309 | // If buffer is not meant for CPU return err |
| 310 | if (!CpuCanAccess(prod_usage, cons_usage)) { |
| 311 | return GRALLOC1_ERROR_BAD_VALUE; |
| 312 | } |
| 313 | |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 314 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 315 | if (buf == nullptr) { |
| 316 | return GRALLOC1_ERROR_BAD_HANDLE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 317 | } |
| 318 | |
Naseer Ahmed | 6733070 | 2017-05-02 15:00:26 -0400 | [diff] [blame] | 319 | if (hnd->base == 0) { |
| 320 | // we need to map for real |
| 321 | err = MapBuffer(hnd); |
| 322 | } |
| 323 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 324 | // Invalidate if CPU reads in software and there are non-CPU |
| 325 | // writers. No need to do this for the metadata buffer as it is |
| 326 | // only read/written in software. |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 327 | |
| 328 | // todo use handle here |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 329 | if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) && |
| 330 | (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) { |
| 331 | if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 332 | buf->ion_handle_main, CACHE_INVALIDATE)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 333 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // Mark the buffer to be flushed after CPU write. |
| 338 | if (!err && CpuCanWrite(prod_usage)) { |
| 339 | private_handle_t *handle = const_cast<private_handle_t *>(hnd); |
| 340 | handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 341 | } |
| 342 | |
| 343 | return err; |
| 344 | } |
| 345 | |
| 346 | gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) { |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 347 | std::lock_guard<std::mutex> lock(buffer_lock_); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 348 | gralloc1_error_t status = GRALLOC1_ERROR_NONE; |
| 349 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 350 | private_handle_t *hnd = const_cast<private_handle_t *>(handle); |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 351 | auto buf = GetBufferFromHandleLocked(hnd); |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 352 | if (buf == nullptr) { |
| 353 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 354 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 355 | |
| 356 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 357 | if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 358 | buf->ion_handle_main, CACHE_CLEAN) != 0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 359 | status = GRALLOC1_ERROR_BAD_HANDLE; |
| 360 | } |
| 361 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 362 | } |
| 363 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 364 | return status; |
| 365 | } |
| 366 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 367 | uint32_t BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 368 | gralloc1_consumer_usage_t cons_usage) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 369 | uint32_t align = UINT(getpagesize()); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 370 | if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) { |
| 371 | align = 8192; |
| 372 | } |
| 373 | |
| 374 | if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) { |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 375 | if ((prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) || |
| 376 | (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 377 | // The alignment here reflects qsee mmu V7L/V8L requirement |
| 378 | align = SZ_2M; |
| 379 | } else { |
| 380 | align = SECURE_ALIGN; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return align; |
| 385 | } |
| 386 | |
| 387 | int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage, |
| 388 | gralloc1_consumer_usage_t cons_usage) { |
| 389 | int flags = 0; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 390 | if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) { |
| 391 | flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; |
| 392 | } |
| 393 | |
| 394 | if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) { |
| 395 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; |
| 396 | } |
| 397 | |
| 398 | if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) { |
| 399 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; |
| 400 | } |
| 401 | |
| 402 | if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) { |
| 403 | flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; |
| 404 | } |
| 405 | |
| 406 | if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) { |
| 407 | flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE; |
| 408 | } |
| 409 | |
| 410 | if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) { |
| 411 | flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY; |
| 412 | } |
| 413 | |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 414 | if (IsUBwcEnabled(format, prod_usage, cons_usage)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 415 | flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; |
| 416 | } |
| 417 | |
| 418 | if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) { |
| 419 | flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED; |
| 420 | } |
| 421 | |
| 422 | // TODO(user): is this correct??? |
| 423 | if ((cons_usage & |
| 424 | (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) || |
| 425 | (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) { |
| 426 | flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER; |
| 427 | } |
| 428 | |
| 429 | if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) { |
| 430 | flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER; |
| 431 | } |
| 432 | |
Naseer Ahmed | fe6342e | 2017-04-20 16:52:40 -0400 | [diff] [blame] | 433 | if (!allocator_->UseUncached(prod_usage, cons_usage)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 434 | flags |= private_handle_t::PRIV_FLAGS_CACHED; |
| 435 | } |
| 436 | |
| 437 | return flags; |
| 438 | } |
| 439 | |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 440 | int BufferManager::GetBufferType(int inputFormat) { |
| 441 | int buffer_type = BUFFER_TYPE_VIDEO; |
| 442 | if (IsUncompressedRGBFormat(inputFormat)) { |
| 443 | // RGB formats |
| 444 | buffer_type = BUFFER_TYPE_UI; |
| 445 | } |
| 446 | |
| 447 | return buffer_type; |
| 448 | } |
| 449 | |
| 450 | int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle, |
| 451 | unsigned int bufferSize) { |
| 452 | if (!handle) |
| 453 | return -EINVAL; |
| 454 | |
| 455 | int format = descriptor.GetFormat(); |
| 456 | gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage(); |
| 457 | gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage(); |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 458 | uint32_t layer_count = descriptor.GetLayerCount(); |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 459 | |
| 460 | // Get implementation defined format |
| 461 | int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format); |
| 462 | |
| 463 | unsigned int size; |
| 464 | unsigned int alignedw, alignedh; |
| 465 | int buffer_type = GetBufferType(gralloc_format); |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 466 | BufferInfo info = GetBufferInfo(descriptor); |
| 467 | GetBufferSizeAndDimensions(info, &size, &alignedw, &alignedh); |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 468 | size = (bufferSize >= size) ? bufferSize : size; |
| 469 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 470 | int err = 0; |
| 471 | int flags = 0; |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 472 | auto page_size = UINT(getpagesize()); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 473 | AllocData data; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 474 | data.align = GetDataAlignment(format, prod_usage, cons_usage); |
Naseer Ahmed | f8e9c43 | 2017-06-13 17:45:12 -0400 | [diff] [blame] | 475 | size = ALIGN(size, data.align) * layer_count; |
| 476 | data.size = size; |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 477 | data.handle = (uintptr_t) handle; |
Naseer Ahmed | fe6342e | 2017-04-20 16:52:40 -0400 | [diff] [blame] | 478 | data.uncached = allocator_->UseUncached(prod_usage, cons_usage); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 479 | |
| 480 | // Allocate buffer memory |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 481 | err = allocator_->AllocateMem(&data, prod_usage, cons_usage); |
| 482 | if (err) { |
| 483 | ALOGE("gralloc failed to allocate err=%s", strerror(-err)); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 484 | return err; |
| 485 | } |
| 486 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 487 | // Allocate memory for MetaData |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 488 | AllocData e_data; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 489 | e_data.size = ALIGN(UINT(sizeof(MetaData_t)), page_size); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 490 | e_data.handle = data.handle; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 491 | e_data.align = page_size; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 492 | |
| 493 | err = |
| 494 | allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 495 | if (err) { |
| 496 | ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err)); |
| 497 | return err; |
| 498 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 499 | |
| 500 | flags = GetHandleFlags(format, prod_usage, cons_usage); |
| 501 | flags |= data.alloc_type; |
| 502 | |
| 503 | // Create handle |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 504 | private_handle_t *hnd = new private_handle_t(data.fd, |
| 505 | e_data.fd, |
| 506 | flags, |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 507 | INT(alignedw), |
| 508 | INT(alignedh), |
| 509 | descriptor.GetWidth(), |
| 510 | descriptor.GetHeight(), |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 511 | format, |
Naseer Ahmed | 4c0eec9 | 2017-03-27 16:51:48 -0400 | [diff] [blame] | 512 | buffer_type, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 513 | size, |
| 514 | prod_usage, |
| 515 | cons_usage); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 516 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 517 | hnd->id = ++next_id_; |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 518 | hnd->base = 0; |
| 519 | hnd->base_metadata = 0; |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 520 | hnd->layer_count = layer_count; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 521 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 522 | ColorSpace_t colorSpace = ITU_R_601; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 523 | setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast<void *>(&colorSpace)); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 524 | *handle = hnd; |
Naseer Ahmed | 378d858 | 2017-03-28 21:56:08 -0400 | [diff] [blame] | 525 | RegisterHandleLocked(hnd, data.ion_handle, e_data.ion_handle); |
Naseer Ahmed | 699b457 | 2017-03-09 12:28:45 -0500 | [diff] [blame] | 526 | ALOGD_IF(DEBUG, "Allocated buffer handle: %p id: %" PRIu64, hnd, hnd->id); |
| 527 | if (DEBUG) { |
| 528 | private_handle_t::Dump(hnd); |
| 529 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 530 | return err; |
| 531 | } |
| 532 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 533 | gralloc1_error_t BufferManager::Perform(int operation, va_list args) { |
| 534 | switch (operation) { |
| 535 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: { |
| 536 | int fd = va_arg(args, int); |
| 537 | unsigned int size = va_arg(args, unsigned int); |
| 538 | unsigned int offset = va_arg(args, unsigned int); |
| 539 | void *base = va_arg(args, void *); |
| 540 | int width = va_arg(args, int); |
| 541 | int height = va_arg(args, int); |
| 542 | int format = va_arg(args, int); |
| 543 | |
| 544 | native_handle_t **handle = va_arg(args, native_handle_t **); |
| 545 | private_handle_t *hnd = reinterpret_cast<private_handle_t *>( |
| 546 | native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts())); |
| 547 | if (hnd) { |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 548 | unsigned int alignedw = 0, alignedh = 0; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 549 | hnd->magic = private_handle_t::kMagic; |
| 550 | hnd->fd = fd; |
| 551 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
| 552 | hnd->size = size; |
| 553 | hnd->offset = offset; |
| 554 | hnd->base = uint64_t(base) + offset; |
| 555 | hnd->gpuaddr = 0; |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 556 | BufferInfo info(width, height, format); |
| 557 | GetAlignedWidthAndHeight(info, &alignedw, &alignedh); |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 558 | hnd->unaligned_width = width; |
| 559 | hnd->unaligned_height = height; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 560 | hnd->width = INT(alignedw); |
| 561 | hnd->height = INT(alignedh); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 562 | hnd->format = format; |
| 563 | *handle = reinterpret_cast<native_handle_t *>(hnd); |
| 564 | } |
| 565 | } break; |
| 566 | |
| 567 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: { |
| 568 | int width = va_arg(args, int); |
| 569 | int format = va_arg(args, int); |
| 570 | int *stride = va_arg(args, int *); |
| 571 | unsigned int alignedw = 0, alignedh = 0; |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 572 | BufferInfo info(width, width, format); |
| 573 | GetAlignedWidthAndHeight(info, &alignedw, &alignedh); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 574 | *stride = INT(alignedw); |
| 575 | } break; |
| 576 | |
| 577 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: { |
| 578 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 579 | int *stride = va_arg(args, int *); |
| 580 | if (private_handle_t::validate(hnd) != 0) { |
| 581 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 582 | } |
| 583 | |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 584 | BufferDim_t buffer_dim; |
| 585 | if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) { |
| 586 | *stride = buffer_dim.sliceWidth; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 587 | } else { |
| 588 | *stride = hnd->width; |
| 589 | } |
| 590 | } break; |
| 591 | |
| 592 | // TODO(user) : this alone should be sufficient, ask gfx to get rid of above |
| 593 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: { |
| 594 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 595 | int *stride = va_arg(args, int *); |
| 596 | int *height = va_arg(args, int *); |
| 597 | if (private_handle_t::validate(hnd) != 0) { |
| 598 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 599 | } |
| 600 | |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 601 | BufferDim_t buffer_dim; |
Rohit Kulkarni | 70537e0 | 2017-06-16 12:01:13 -0700 | [diff] [blame] | 602 | int interlaced = 0; |
Ramkumar Radhakrishnan | 0574753 | 2017-07-07 16:38:28 -0700 | [diff] [blame] | 603 | |
| 604 | *stride = hnd->width; |
| 605 | *height = hnd->height; |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 606 | if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) { |
| 607 | *stride = buffer_dim.sliceWidth; |
| 608 | *height = buffer_dim.sliceHeight; |
Rohit Kulkarni | 70537e0 | 2017-06-16 12:01:13 -0700 | [diff] [blame] | 609 | } else if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, &interlaced) == 0) { |
| 610 | if (interlaced && IsUBwcFormat(hnd->format)) { |
| 611 | unsigned int alignedw = 0, alignedh = 0; |
| 612 | // Get re-aligned height for single ubwc interlaced field and |
| 613 | // multiple by 2 to get frame height. |
| 614 | BufferInfo info(hnd->width, ((hnd->height+1)>>1), hnd->format); |
| 615 | GetAlignedWidthAndHeight(info, &alignedw, &alignedh); |
| 616 | *stride = static_cast<int>(alignedw); |
| 617 | *height = static_cast<int>(alignedh * 2); |
| 618 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 619 | } |
| 620 | } break; |
| 621 | |
| 622 | case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: { |
| 623 | // TODO(user): Usage is split now. take care of it from Gfx client. |
| 624 | // see if we can directly expect descriptor from gfx client. |
| 625 | int width = va_arg(args, int); |
| 626 | int height = va_arg(args, int); |
| 627 | int format = va_arg(args, int); |
| 628 | uint64_t producer_usage = va_arg(args, uint64_t); |
| 629 | uint64_t consumer_usage = va_arg(args, uint64_t); |
| 630 | gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t>(producer_usage); |
| 631 | gralloc1_consumer_usage_t cons_usage = static_cast<gralloc1_consumer_usage_t>(consumer_usage); |
| 632 | |
| 633 | int *aligned_width = va_arg(args, int *); |
| 634 | int *aligned_height = va_arg(args, int *); |
| 635 | int *tile_enabled = va_arg(args, int *); |
| 636 | unsigned int alignedw, alignedh; |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 637 | BufferInfo info(width, height, format, prod_usage, cons_usage); |
| 638 | *tile_enabled = IsUBwcEnabled(format, prod_usage, cons_usage); |
| 639 | GetAlignedWidthAndHeight(info, &alignedw, &alignedh); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 640 | *aligned_width = INT(alignedw); |
| 641 | *aligned_height = INT(alignedh); |
| 642 | } break; |
| 643 | |
| 644 | case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: { |
| 645 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 646 | int *color_space = va_arg(args, int *); |
| 647 | if (private_handle_t::validate(hnd) != 0) { |
| 648 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 649 | } |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 650 | *color_space = 0; |
Arun Kumar K.R | b2771bf | 2016-10-03 21:38:23 -0700 | [diff] [blame] | 651 | #ifdef USE_COLOR_METADATA |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 652 | ColorMetaData color_metadata; |
| 653 | if (getMetaData(hnd, GET_COLOR_METADATA, &color_metadata) == 0) { |
| 654 | switch (color_metadata.colorPrimaries) { |
| 655 | case ColorPrimaries_BT709_5: |
| 656 | *color_space = HAL_CSC_ITU_R_709; |
| 657 | break; |
| 658 | case ColorPrimaries_BT601_6_525: |
| 659 | *color_space = ((color_metadata.range) ? HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601); |
| 660 | break; |
| 661 | case ColorPrimaries_BT2020: |
| 662 | *color_space = (color_metadata.range) ? HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020; |
| 663 | break; |
| 664 | default: |
| 665 | ALOGE("Unknown Color Space = %d", color_metadata.colorPrimaries); |
| 666 | break; |
Arun Kumar K.R | b2771bf | 2016-10-03 21:38:23 -0700 | [diff] [blame] | 667 | } |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 668 | break; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 669 | } |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 670 | if (getMetaData(hnd, GET_COLOR_SPACE, &color_metadata) != 0) { |
| 671 | *color_space = 0; |
| 672 | } |
| 673 | #endif |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 674 | } break; |
| 675 | case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: { |
| 676 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 677 | android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *); |
| 678 | if (private_handle_t::validate(hnd) != 0) { |
| 679 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 680 | } |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 681 | if (GetYUVPlaneInfo(hnd, ycbcr)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 682 | return GRALLOC1_ERROR_UNDEFINED; |
| 683 | } |
| 684 | } break; |
| 685 | |
| 686 | case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: { |
| 687 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 688 | int *map_secure_buffer = va_arg(args, int *); |
| 689 | if (private_handle_t::validate(hnd) != 0) { |
| 690 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 691 | } |
Naseer Ahmed | 49f2e9c | 2017-03-23 22:13:17 -0400 | [diff] [blame] | 692 | |
| 693 | if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) == 0) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 694 | *map_secure_buffer = 0; |
| 695 | } |
| 696 | } break; |
| 697 | |
| 698 | case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: { |
| 699 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 700 | int *flag = va_arg(args, int *); |
| 701 | if (private_handle_t::validate(hnd) != 0) { |
| 702 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 703 | } |
| 704 | *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; |
Ramakant Singh | c7d0779 | 2017-07-26 15:36:33 +0530 | [diff] [blame] | 705 | int linear_format = 0; |
| 706 | if (getMetaData(hnd, GET_LINEAR_FORMAT, &linear_format) == 0) { |
| 707 | if (!linear_format) { |
| 708 | *flag = 0; |
| 709 | } |
| 710 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 711 | } break; |
| 712 | |
| 713 | case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: { |
| 714 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 715 | void **rgb_data = va_arg(args, void **); |
| 716 | if (private_handle_t::validate(hnd) != 0) { |
| 717 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 718 | } |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 719 | if (GetRgbDataAddress(hnd, rgb_data)) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 720 | return GRALLOC1_ERROR_UNDEFINED; |
| 721 | } |
| 722 | } break; |
| 723 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 724 | case GRALLOC1_MODULE_PERFORM_GET_BUFFER_SIZE_AND_DIMENSIONS: { |
| 725 | int width = va_arg(args, int); |
| 726 | int height = va_arg(args, int); |
| 727 | int format = va_arg(args, int); |
| 728 | uint64_t p_usage = va_arg(args, uint64_t); |
| 729 | uint64_t c_usage = va_arg(args, uint64_t); |
| 730 | gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage); |
| 731 | gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage); |
| 732 | uint32_t *aligned_width = va_arg(args, uint32_t *); |
| 733 | uint32_t *aligned_height = va_arg(args, uint32_t *); |
| 734 | uint32_t *size = va_arg(args, uint32_t *); |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 735 | auto info = BufferInfo(width, height, format, producer_usage, consumer_usage); |
| 736 | GetBufferSizeAndDimensions(info, size, aligned_width, aligned_height); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 737 | // Align size |
| 738 | auto align = GetDataAlignment(format, producer_usage, consumer_usage); |
| 739 | *size = ALIGN(*size, align); |
| 740 | } break; |
| 741 | |
| 742 | // TODO(user): Break out similar functionality, preferably moving to a common lib. |
| 743 | |
| 744 | case GRALLOC1_MODULE_PERFORM_ALLOCATE_BUFFER: { |
| 745 | int width = va_arg(args, int); |
| 746 | int height = va_arg(args, int); |
| 747 | int format = va_arg(args, int); |
| 748 | uint64_t p_usage = va_arg(args, uint64_t); |
| 749 | uint64_t c_usage = va_arg(args, uint64_t); |
| 750 | buffer_handle_t *hnd = va_arg(args, buffer_handle_t*); |
| 751 | gralloc1_producer_usage_t producer_usage = static_cast<gralloc1_producer_usage_t>(p_usage); |
| 752 | gralloc1_consumer_usage_t consumer_usage = static_cast<gralloc1_consumer_usage_t>(c_usage); |
| 753 | BufferDescriptor descriptor(width, height, format, producer_usage, consumer_usage); |
| 754 | unsigned int size; |
| 755 | unsigned int alignedw, alignedh; |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 756 | GetBufferSizeAndDimensions(GetBufferInfo(descriptor), &size, &alignedw, &alignedh); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 757 | AllocateBuffer(descriptor, hnd, size); |
| 758 | } break; |
| 759 | |
Rohit Kulkarni | a152c31 | 2017-06-02 14:22:35 -0700 | [diff] [blame] | 760 | case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: { |
| 761 | private_handle_t *hnd = va_arg(args, private_handle_t *); |
| 762 | int *flag = va_arg(args, int *); |
| 763 | if (private_handle_t::validate(hnd) != 0) { |
| 764 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 765 | } |
| 766 | if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) { |
| 767 | *flag = 0; |
| 768 | } |
| 769 | } break; |
| 770 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 771 | default: |
| 772 | break; |
| 773 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 774 | return GRALLOC1_ERROR_NONE; |
| 775 | } |
| 776 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 777 | static bool IsYuvFormat(const private_handle_t *hnd) { |
| 778 | switch (hnd->format) { |
| 779 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 780 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 781 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: |
| 782 | case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: // Same as YCbCr_420_SP_VENUS |
| 783 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: |
| 784 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 785 | case HAL_PIXEL_FORMAT_YCrCb_422_SP: |
| 786 | case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: |
| 787 | case HAL_PIXEL_FORMAT_NV21_ZSL: |
| 788 | case HAL_PIXEL_FORMAT_RAW16: |
Anjaneya Prasad Musunuri | b165414 | 2017-04-18 10:32:42 +0530 | [diff] [blame] | 789 | case HAL_PIXEL_FORMAT_Y16: |
Naseer Ahmed | 9299862 | 2017-03-20 12:39:17 -0400 | [diff] [blame] | 790 | case HAL_PIXEL_FORMAT_RAW12: |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 791 | case HAL_PIXEL_FORMAT_RAW10: |
| 792 | case HAL_PIXEL_FORMAT_YV12: |
Anjaneya Prasad Musunuri | b165414 | 2017-04-18 10:32:42 +0530 | [diff] [blame] | 793 | case HAL_PIXEL_FORMAT_Y8: |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 794 | return true; |
| 795 | default: |
| 796 | return false; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | gralloc1_error_t BufferManager::GetNumFlexPlanes(const private_handle_t *hnd, |
| 801 | uint32_t *out_num_planes) { |
| 802 | if (!IsYuvFormat(hnd)) { |
| 803 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 804 | } else { |
| 805 | *out_num_planes = 3; |
| 806 | } |
| 807 | return GRALLOC1_ERROR_NONE; |
| 808 | } |
| 809 | |
| 810 | gralloc1_error_t BufferManager::GetFlexLayout(const private_handle_t *hnd, |
| 811 | struct android_flex_layout *layout) { |
| 812 | if (!IsYuvFormat(hnd)) { |
| 813 | return GRALLOC1_ERROR_UNSUPPORTED; |
| 814 | } |
| 815 | |
| 816 | android_ycbcr ycbcr; |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 817 | int err = GetYUVPlaneInfo(hnd, &ycbcr); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 818 | |
| 819 | if (err != 0) { |
| 820 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 821 | } |
| 822 | |
| 823 | layout->format = FLEX_FORMAT_YCbCr; |
| 824 | layout->num_planes = 3; |
| 825 | |
| 826 | for (uint32_t i = 0; i < layout->num_planes; i++) { |
| 827 | layout->planes[i].bits_per_component = 8; |
| 828 | layout->planes[i].bits_used = 8; |
| 829 | layout->planes[i].h_increment = 1; |
| 830 | layout->planes[i].v_increment = 1; |
| 831 | layout->planes[i].h_subsampling = 2; |
| 832 | layout->planes[i].v_subsampling = 2; |
| 833 | } |
| 834 | |
| 835 | layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y); |
| 836 | layout->planes[0].component = FLEX_COMPONENT_Y; |
| 837 | layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride); |
| 838 | |
| 839 | layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb); |
| 840 | layout->planes[1].component = FLEX_COMPONENT_Cb; |
| 841 | layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step); |
| 842 | layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride); |
| 843 | |
| 844 | layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr); |
| 845 | layout->planes[2].component = FLEX_COMPONENT_Cr; |
| 846 | layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step); |
| 847 | layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride); |
| 848 | return GRALLOC1_ERROR_NONE; |
| 849 | } |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 850 | |
| 851 | gralloc1_error_t BufferManager::Dump(std::ostringstream *os) { |
| 852 | for (auto it : handles_map_) { |
| 853 | auto buf = it.second; |
| 854 | auto hnd = buf->handle; |
| 855 | *os << "handle id: " << std::setw(4) << hnd->id; |
| 856 | *os << " fd: " << std::setw(3) << hnd->fd; |
| 857 | *os << " fd_meta: " << std::setw(3) << hnd->fd_metadata; |
| 858 | *os << " wxh: " << std::setw(4) << hnd->width <<" x " << std::setw(4) << hnd->height; |
| 859 | *os << " uwxuh: " << std::setw(4) << hnd->unaligned_width << " x "; |
| 860 | *os << std::setw(4) << hnd->unaligned_height; |
| 861 | *os << " size: " << std::setw(9) << hnd->size; |
| 862 | *os << std::hex << std::setfill('0'); |
| 863 | *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags; |
| 864 | *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage; |
| 865 | *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage; |
| 866 | // TODO(user): get format string from qdutils |
| 867 | *os << " format: " << "0x" << std::setw(8) << hnd->format; |
| 868 | *os << std::dec << std::setfill(' ') << std::endl; |
| 869 | } |
| 870 | return GRALLOC1_ERROR_NONE; |
| 871 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 872 | } // namespace gralloc1 |