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