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