1 /* 2 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. 3 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 13 * * Neither the name of The Linux Foundation nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) 31 #include <cutils/log.h> 32 #include <utils/Trace.h> 33 #include <cutils/trace.h> 34 #include <sync/sync.h> 35 #include <algorithm> 36 #include <sstream> 37 #include <string> 38 39 #include "gr_device_impl.h" 40 #include "gr_buf_descriptor.h" 41 #include "gralloc_priv.h" 42 #include "qd_utils.h" 43 #include "qdMetaData.h" 44 #include "gr_utils.h" 45 46 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device); 47 48 int gralloc_device_close(struct hw_device_t *device); 49 50 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open}; 51 52 struct gralloc_module_t HAL_MODULE_INFO_SYM = { 53 .common = { 54 .tag = HARDWARE_MODULE_TAG, 55 .module_api_version = GRALLOC_MODULE_API_VERSION_1_0, 56 .hal_api_version = HARDWARE_HAL_API_VERSION, 57 .id = GRALLOC_HARDWARE_MODULE_ID, 58 .name = "Graphics Memory Module", 59 .author = "Code Aurora Forum", 60 .methods = &gralloc_module_methods, 61 .dso = 0, 62 .reserved = {0}, 63 }, 64 }; 65 66 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) { 67 int status = -EINVAL; 68 if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) { 69 gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module); 70 *device = reinterpret_cast<hw_device_t *>(dev); 71 if (dev) { 72 status = 0; 73 } else { 74 ALOGE("Fatal error opening gralloc1 device"); 75 } 76 } 77 return status; 78 } 79 80 namespace gralloc1 { 81 82 GrallocImpl::GrallocImpl(const hw_module_t *module) { 83 common.tag = HARDWARE_DEVICE_TAG; 84 common.version = GRALLOC_MODULE_API_VERSION_1_0; 85 common.module = const_cast<hw_module_t *>(module); 86 common.close = CloseDevice; 87 getFunction = GetFunction; 88 getCapabilities = GetCapabilities; 89 90 initalized_ = Init(); 91 } 92 93 bool GrallocImpl::Init() { 94 buf_mgr_ = BufferManager::GetInstance(); 95 return buf_mgr_ != nullptr; 96 } 97 98 GrallocImpl::~GrallocImpl() { 99 } 100 101 int GrallocImpl::CloseDevice(hw_device_t *device __unused) { 102 // No-op since the gralloc device is a singleton 103 return 0; 104 } 105 106 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count, 107 int32_t /*gralloc1_capability_t*/ *out_capabilities) { 108 if (device != nullptr) { 109 if (out_capabilities != nullptr && *out_count >= 3) { 110 out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE; 111 out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS; 112 out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE; 113 } 114 *out_count = 3; 115 } 116 return; 117 } 118 119 gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) { 120 if (!device) { 121 return NULL; 122 } 123 124 switch (function) { 125 case GRALLOC1_FUNCTION_DUMP: 126 return reinterpret_cast<gralloc1_function_pointer_t>(Dump); 127 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR: 128 return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor); 129 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR: 130 return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor); 131 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE: 132 return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage); 133 case GRALLOC1_FUNCTION_SET_DIMENSIONS: 134 return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions); 135 case GRALLOC1_FUNCTION_SET_FORMAT: 136 return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat); 137 case GRALLOC1_FUNCTION_SET_LAYER_COUNT: 138 return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount); 139 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE: 140 return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage); 141 case GRALLOC1_FUNCTION_GET_BACKING_STORE: 142 return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore); 143 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE: 144 return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage); 145 case GRALLOC1_FUNCTION_GET_DIMENSIONS: 146 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions); 147 case GRALLOC1_FUNCTION_GET_FORMAT: 148 return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat); 149 case GRALLOC1_FUNCTION_GET_LAYER_COUNT: 150 return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount); 151 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE: 152 return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage); 153 case GRALLOC1_FUNCTION_GET_STRIDE: 154 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride); 155 case GRALLOC1_FUNCTION_ALLOCATE: 156 return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers); 157 case GRALLOC1_FUNCTION_RETAIN: 158 return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer); 159 case GRALLOC1_FUNCTION_RELEASE: 160 return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer); 161 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES: 162 return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes); 163 case GRALLOC1_FUNCTION_LOCK: 164 return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer); 165 case GRALLOC1_FUNCTION_LOCK_FLEX: 166 return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex); 167 case GRALLOC1_FUNCTION_UNLOCK: 168 return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer); 169 case GRALLOC1_FUNCTION_PERFORM: 170 return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform); 171 default: 172 ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__); 173 return NULL; 174 } 175 176 return NULL; 177 } 178 179 gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size, 180 char *out_buffer) { 181 if (!device) { 182 ALOGE("Gralloc Error : device=%p", (void *)device); 183 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 184 } 185 const size_t max_dump_size = 8192; 186 if (out_buffer == nullptr) { 187 *out_size = max_dump_size; 188 } else { 189 std::ostringstream os; 190 os << "-------------------------------" << std::endl; 191 os << "QTI gralloc dump:" << std::endl; 192 os << "-------------------------------" << std::endl; 193 GrallocImpl const *dev = GRALLOC_IMPL(device); 194 dev->buf_mgr_->Dump(&os); 195 os << "-------------------------------" << std::endl; 196 auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0); 197 *out_size = UINT(copied); 198 } 199 200 return GRALLOC1_ERROR_NONE; 201 } 202 203 gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device, 204 buffer_handle_t buffer) { 205 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 206 if (!device || (private_handle_t::validate(hnd) != 0)) { 207 ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer); 208 return GRALLOC1_ERROR_BAD_HANDLE; 209 } 210 211 return GRALLOC1_ERROR_NONE; 212 } 213 214 gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device, 215 gralloc1_buffer_descriptor_t *out_descriptor) { 216 if (!device) { 217 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 218 } 219 GrallocImpl const *dev = GRALLOC_IMPL(device); 220 return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor); 221 } 222 223 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device, 224 gralloc1_buffer_descriptor_t descriptor) { 225 if (!device) { 226 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 227 } 228 GrallocImpl const *dev = GRALLOC_IMPL(device); 229 return dev->buf_mgr_->DestroyBufferDescriptor(descriptor); 230 } 231 232 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device, 233 gralloc1_buffer_descriptor_t descriptor, 234 gralloc1_consumer_usage_t usage) { 235 if (!device) { 236 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 237 } else { 238 GrallocImpl const *dev = GRALLOC_IMPL(device); 239 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 240 &BufferDescriptor::SetConsumerUsage, usage); 241 } 242 } 243 244 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device, 245 gralloc1_buffer_descriptor_t descriptor, 246 uint32_t width, uint32_t height) { 247 if (!device) { 248 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 249 } else { 250 GrallocImpl const *dev = GRALLOC_IMPL(device); 251 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 252 &BufferDescriptor::SetDimensions, 253 INT(width), INT(height)); 254 } 255 } 256 257 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device, 258 gralloc1_buffer_descriptor_t descriptor, 259 int32_t format) { 260 if (!device) { 261 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 262 } else { 263 GrallocImpl const *dev = GRALLOC_IMPL(device); 264 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 265 &BufferDescriptor::SetColorFormat, format); 266 } 267 } 268 269 gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device, 270 gralloc1_buffer_descriptor_t descriptor, 271 uint32_t layer_count) { 272 if (!device) { 273 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 274 } else { 275 GrallocImpl const *dev = GRALLOC_IMPL(device); 276 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 277 &BufferDescriptor::SetLayerCount, 278 layer_count); 279 } 280 } 281 282 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device, 283 gralloc1_buffer_descriptor_t descriptor, 284 gralloc1_producer_usage_t usage) { 285 if (!device) { 286 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 287 } else { 288 GrallocImpl const *dev = GRALLOC_IMPL(device); 289 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 290 &BufferDescriptor::SetProducerUsage, usage); 291 } 292 } 293 294 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer, 295 gralloc1_backing_store_t *out_backstore) { 296 if (!device || !buffer) { 297 return GRALLOC1_ERROR_BAD_HANDLE; 298 } 299 300 *out_backstore = 301 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore()); 302 303 return GRALLOC1_ERROR_NONE; 304 } 305 306 gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer, 307 gralloc1_consumer_usage_t *outUsage) { 308 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 309 if (status == GRALLOC1_ERROR_NONE) { 310 *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage(); 311 } 312 313 return status; 314 } 315 316 gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer, 317 uint32_t *outWidth, uint32_t *outHeight) { 318 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 319 if (status == GRALLOC1_ERROR_NONE) { 320 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 321 *outWidth = UINT(hnd->GetUnalignedWidth()); 322 *outHeight = UINT(hnd->GetUnalignedHeight()); 323 } 324 325 return status; 326 } 327 328 gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer, 329 int32_t *outFormat) { 330 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 331 if (status == GRALLOC1_ERROR_NONE) { 332 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat(); 333 } 334 335 return status; 336 } 337 338 gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer, 339 uint32_t *outLayerCount) { 340 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 341 if (status == GRALLOC1_ERROR_NONE) { 342 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount(); 343 } 344 345 return status; 346 } 347 348 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer, 349 gralloc1_producer_usage_t *outUsage) { 350 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 351 if (status == GRALLOC1_ERROR_NONE) { 352 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 353 *outUsage = hnd->GetProducerUsage(); 354 } 355 356 return status; 357 } 358 359 gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer, 360 uint32_t *outStride) { 361 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 362 if (status == GRALLOC1_ERROR_NONE) { 363 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride()); 364 } 365 366 return status; 367 } 368 369 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors, 370 const gralloc1_buffer_descriptor_t *descriptors, 371 buffer_handle_t *out_buffers) { 372 if (!num_descriptors || !descriptors) { 373 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 374 } 375 376 GrallocImpl const *dev = GRALLOC_IMPL(device); 377 gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors, 378 out_buffers); 379 380 return status; 381 } 382 383 gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { 384 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 385 if (status == GRALLOC1_ERROR_NONE) { 386 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 387 GrallocImpl const *dev = GRALLOC_IMPL(device); 388 status = dev->buf_mgr_->RetainBuffer(hnd); 389 } 390 391 return status; 392 } 393 394 gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { 395 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 396 if (status == GRALLOC1_ERROR_NONE) { 397 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 398 GrallocImpl const *dev = GRALLOC_IMPL(device); 399 status = dev->buf_mgr_->ReleaseBuffer(hnd); 400 } 401 402 return status; 403 } 404 405 gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer, 406 uint32_t *out_num_planes) { 407 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 408 if (status == GRALLOC1_ERROR_NONE) { 409 GrallocImpl const *dev = GRALLOC_IMPL(device); 410 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 411 status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes); 412 } 413 return status; 414 } 415 416 static inline void CloseFdIfValid(int fd) { 417 if (fd > 0) { 418 close(fd); 419 } 420 } 421 422 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, 423 gralloc1_producer_usage_t prod_usage, 424 gralloc1_consumer_usage_t cons_usage, 425 const gralloc1_rect_t *region, void **out_data, 426 int32_t acquire_fence) { 427 ATRACE_CALL(); 428 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 429 if (status != GRALLOC1_ERROR_NONE) { 430 CloseFdIfValid(acquire_fence); 431 return status; 432 } 433 434 if (acquire_fence > 0) { 435 ATRACE_BEGIN("fence wait"); 436 int error = sync_wait(acquire_fence, 1000); 437 ATRACE_END(); 438 CloseFdIfValid(acquire_fence); 439 if (error < 0) { 440 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno)); 441 return GRALLOC1_ERROR_UNDEFINED; 442 } 443 } 444 445 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 446 GrallocImpl const *dev = GRALLOC_IMPL(device); 447 448 // Either producer usage or consumer usage must be *_USAGE_NONE 449 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) && 450 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) { 451 // Current gralloc1 clients do not satisfy this restriction. 452 // See b/33588773 for details 453 // return GRALLOC1_ERROR_BAD_VALUE; 454 } 455 456 // currently we ignore the region/rect client wants to lock 457 if (region == NULL) { 458 return GRALLOC1_ERROR_BAD_VALUE; 459 } 460 // TODO(user): Need to check if buffer was allocated with the same flags 461 status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage); 462 463 *out_data = reinterpret_cast<void *>(hnd->base); 464 465 return status; 466 } 467 468 gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer, 469 gralloc1_producer_usage_t prod_usage, 470 gralloc1_consumer_usage_t cons_usage, 471 const gralloc1_rect_t *region, 472 struct android_flex_layout *out_flex_layout, 473 int32_t acquire_fence) { 474 void *out_data; 475 gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region, 476 &out_data, acquire_fence); 477 if (status != GRALLOC1_ERROR_NONE) { 478 return status; 479 } 480 481 GrallocImpl const *dev = GRALLOC_IMPL(device); 482 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 483 dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout); 484 return status; 485 } 486 487 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, 488 int32_t *release_fence) { 489 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 490 491 if (status != GRALLOC1_ERROR_NONE) { 492 return status; 493 } 494 495 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 496 GrallocImpl const *dev = GRALLOC_IMPL(device); 497 498 *release_fence = -1; 499 500 return dev->buf_mgr_->UnlockBuffer(hnd); 501 } 502 503 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) { 504 va_list args; 505 va_start(args, operation); 506 GrallocImpl const *dev = GRALLOC_IMPL(device); 507 gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args); 508 va_end(args); 509 510 return err; 511 } 512 513 } // namespace gralloc1 514