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 #include <cutils/log.h> 31 #include <sync/sync.h> 32 #include <algorithm> 33 #include <sstream> 34 #include <string> 35 36 #include "gr_device_impl.h" 37 #include "gr_buf_descriptor.h" 38 #include "gralloc_priv.h" 39 #include "qd_utils.h" 40 #include "qdMetaData.h" 41 #include "gr_utils.h" 42 43 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device); 44 45 int gralloc_device_close(struct hw_device_t *device); 46 47 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open}; 48 49 struct gralloc_module_t HAL_MODULE_INFO_SYM = { 50 .common = { 51 .tag = HARDWARE_MODULE_TAG, 52 .module_api_version = GRALLOC_MODULE_API_VERSION_1_0, 53 .hal_api_version = HARDWARE_HAL_API_VERSION, 54 .id = GRALLOC_HARDWARE_MODULE_ID, 55 .name = "Graphics Memory Module", 56 .author = "Code Aurora Forum", 57 .methods = &gralloc_module_methods, 58 .dso = 0, 59 .reserved = {0}, 60 }, 61 }; 62 63 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) { 64 int status = -EINVAL; 65 if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) { 66 gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module); 67 *device = reinterpret_cast<hw_device_t *>(dev); 68 if (dev) { 69 status = 0; 70 } else { 71 ALOGE("Fatal error opening gralloc1 device"); 72 } 73 } 74 return status; 75 } 76 77 namespace gralloc1 { 78 79 GrallocImpl::GrallocImpl(const hw_module_t *module) { 80 common.tag = HARDWARE_DEVICE_TAG; 81 common.version = GRALLOC_MODULE_API_VERSION_1_0; 82 common.module = const_cast<hw_module_t *>(module); 83 common.close = CloseDevice; 84 getFunction = GetFunction; 85 getCapabilities = GetCapabilities; 86 87 initalized_ = Init(); 88 } 89 90 bool GrallocImpl::Init() { 91 buf_mgr_ = BufferManager::GetInstance(); 92 return buf_mgr_ != nullptr; 93 } 94 95 GrallocImpl::~GrallocImpl() { 96 } 97 98 int GrallocImpl::CloseDevice(hw_device_t *device __unused) { 99 // No-op since the gralloc device is a singleton 100 return 0; 101 } 102 103 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count, 104 int32_t /*gralloc1_capability_t*/ *out_capabilities) { 105 if (device != nullptr) { 106 if (out_capabilities != nullptr && *out_count >= 3) { 107 out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE; 108 out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS; 109 out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE; 110 } 111 *out_count = 3; 112 } 113 return; 114 } 115 116 gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) { 117 if (!device) { 118 return NULL; 119 } 120 121 switch (function) { 122 case GRALLOC1_FUNCTION_DUMP: 123 return reinterpret_cast<gralloc1_function_pointer_t>(Dump); 124 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR: 125 return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor); 126 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR: 127 return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor); 128 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE: 129 return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage); 130 case GRALLOC1_FUNCTION_SET_DIMENSIONS: 131 return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions); 132 case GRALLOC1_FUNCTION_SET_FORMAT: 133 return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat); 134 case GRALLOC1_FUNCTION_SET_LAYER_COUNT: 135 return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount); 136 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE: 137 return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage); 138 case GRALLOC1_FUNCTION_GET_BACKING_STORE: 139 return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore); 140 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE: 141 return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage); 142 case GRALLOC1_FUNCTION_GET_DIMENSIONS: 143 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions); 144 case GRALLOC1_FUNCTION_GET_FORMAT: 145 return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat); 146 case GRALLOC1_FUNCTION_GET_LAYER_COUNT: 147 return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount); 148 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE: 149 return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage); 150 case GRALLOC1_FUNCTION_GET_STRIDE: 151 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride); 152 case GRALLOC1_FUNCTION_ALLOCATE: 153 return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers); 154 case GRALLOC1_FUNCTION_RETAIN: 155 return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer); 156 case GRALLOC1_FUNCTION_RELEASE: 157 return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer); 158 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES: 159 return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes); 160 case GRALLOC1_FUNCTION_LOCK: 161 return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer); 162 case GRALLOC1_FUNCTION_LOCK_FLEX: 163 return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex); 164 case GRALLOC1_FUNCTION_UNLOCK: 165 return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer); 166 case GRALLOC1_FUNCTION_PERFORM: 167 return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform); 168 default: 169 ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__); 170 return NULL; 171 } 172 173 return NULL; 174 } 175 176 gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size, 177 char *out_buffer __unused) { 178 if (!device) { 179 ALOGE("Gralloc Error : device=%p", (void *)device); 180 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 181 } 182 // nothing to dump 183 *out_size = 0; 184 185 return GRALLOC1_ERROR_NONE; 186 } 187 188 gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device, 189 buffer_handle_t buffer) { 190 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 191 if (!device || (private_handle_t::validate(hnd) != 0)) { 192 ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer); 193 return GRALLOC1_ERROR_BAD_HANDLE; 194 } 195 196 return GRALLOC1_ERROR_NONE; 197 } 198 199 gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device, 200 gralloc1_buffer_descriptor_t *out_descriptor) { 201 if (!device) { 202 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 203 } 204 GrallocImpl const *dev = GRALLOC_IMPL(device); 205 return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor); 206 } 207 208 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device, 209 gralloc1_buffer_descriptor_t descriptor) { 210 if (!device) { 211 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 212 } 213 GrallocImpl const *dev = GRALLOC_IMPL(device); 214 return dev->buf_mgr_->DestroyBufferDescriptor(descriptor); 215 } 216 217 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device, 218 gralloc1_buffer_descriptor_t descriptor, 219 gralloc1_consumer_usage_t usage) { 220 if (!device) { 221 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 222 } else { 223 GrallocImpl const *dev = GRALLOC_IMPL(device); 224 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 225 &BufferDescriptor::SetConsumerUsage, usage); 226 } 227 } 228 229 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device, 230 gralloc1_buffer_descriptor_t descriptor, 231 uint32_t width, uint32_t height) { 232 if (!device) { 233 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 234 } else { 235 GrallocImpl const *dev = GRALLOC_IMPL(device); 236 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 237 &BufferDescriptor::SetDimensions, 238 INT(width), INT(height)); 239 } 240 } 241 242 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device, 243 gralloc1_buffer_descriptor_t descriptor, 244 int32_t format) { 245 if (!device) { 246 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 247 } else { 248 GrallocImpl const *dev = GRALLOC_IMPL(device); 249 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 250 &BufferDescriptor::SetColorFormat, format); 251 } 252 } 253 254 gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device, 255 gralloc1_buffer_descriptor_t descriptor, 256 uint32_t layer_count) { 257 if (!device) { 258 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 259 } else { 260 GrallocImpl const *dev = GRALLOC_IMPL(device); 261 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 262 &BufferDescriptor::SetLayerCount, 263 layer_count); 264 } 265 } 266 267 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device, 268 gralloc1_buffer_descriptor_t descriptor, 269 gralloc1_producer_usage_t usage) { 270 if (!device) { 271 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 272 } else { 273 GrallocImpl const *dev = GRALLOC_IMPL(device); 274 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, 275 &BufferDescriptor::SetProducerUsage, usage); 276 } 277 } 278 279 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer, 280 gralloc1_backing_store_t *out_backstore) { 281 if (!device || !buffer) { 282 return GRALLOC1_ERROR_BAD_HANDLE; 283 } 284 285 *out_backstore = 286 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore()); 287 288 return GRALLOC1_ERROR_NONE; 289 } 290 291 gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer, 292 gralloc1_consumer_usage_t *outUsage) { 293 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 294 if (status == GRALLOC1_ERROR_NONE) { 295 *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage(); 296 } 297 298 return status; 299 } 300 301 gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer, 302 uint32_t *outWidth, uint32_t *outHeight) { 303 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 304 if (status == GRALLOC1_ERROR_NONE) { 305 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 306 *outWidth = UINT(hnd->GetUnalignedWidth()); 307 *outHeight = UINT(hnd->GetUnalignedHeight()); 308 } 309 310 return status; 311 } 312 313 gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer, 314 int32_t *outFormat) { 315 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 316 if (status == GRALLOC1_ERROR_NONE) { 317 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat(); 318 } 319 320 return status; 321 } 322 323 gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer, 324 uint32_t *outLayerCount) { 325 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 326 if (status == GRALLOC1_ERROR_NONE) { 327 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount(); 328 } 329 330 return status; 331 } 332 333 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer, 334 gralloc1_producer_usage_t *outUsage) { 335 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 336 if (status == GRALLOC1_ERROR_NONE) { 337 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 338 *outUsage = hnd->GetProducerUsage(); 339 } 340 341 return status; 342 } 343 344 gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer, 345 uint32_t *outStride) { 346 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 347 if (status == GRALLOC1_ERROR_NONE) { 348 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride()); 349 } 350 351 return status; 352 } 353 354 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors, 355 const gralloc1_buffer_descriptor_t *descriptors, 356 buffer_handle_t *out_buffers) { 357 if (!num_descriptors || !descriptors) { 358 return GRALLOC1_ERROR_BAD_DESCRIPTOR; 359 } 360 361 GrallocImpl const *dev = GRALLOC_IMPL(device); 362 gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors, 363 out_buffers); 364 365 return status; 366 } 367 368 gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { 369 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 370 if (status == GRALLOC1_ERROR_NONE) { 371 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 372 GrallocImpl const *dev = GRALLOC_IMPL(device); 373 status = dev->buf_mgr_->RetainBuffer(hnd); 374 } 375 376 return status; 377 } 378 379 gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { 380 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 381 if (status == GRALLOC1_ERROR_NONE) { 382 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 383 GrallocImpl const *dev = GRALLOC_IMPL(device); 384 status = dev->buf_mgr_->ReleaseBuffer(hnd); 385 } 386 387 return status; 388 } 389 390 gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer, 391 uint32_t *out_num_planes) { 392 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 393 if (status == GRALLOC1_ERROR_NONE) { 394 GrallocImpl const *dev = GRALLOC_IMPL(device); 395 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 396 status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes); 397 } 398 return status; 399 } 400 401 static inline void CloseFdIfValid(int fd) { 402 if (fd > 0) { 403 close(fd); 404 } 405 } 406 407 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, 408 gralloc1_producer_usage_t prod_usage, 409 gralloc1_consumer_usage_t cons_usage, 410 const gralloc1_rect_t *region, void **out_data, 411 int32_t acquire_fence) { 412 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 413 if (status != GRALLOC1_ERROR_NONE) { 414 CloseFdIfValid(acquire_fence); 415 return status; 416 } 417 418 if (acquire_fence > 0) { 419 int error = sync_wait(acquire_fence, 1000); 420 CloseFdIfValid(acquire_fence); 421 if (error < 0) { 422 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno)); 423 return GRALLOC1_ERROR_UNDEFINED; 424 } 425 } 426 427 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 428 GrallocImpl const *dev = GRALLOC_IMPL(device); 429 430 // Either producer usage or consumer usage must be *_USAGE_NONE 431 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) && 432 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) { 433 // Current gralloc1 clients do not satisfy this restriction. 434 // See b/33588773 for details 435 // return GRALLOC1_ERROR_BAD_VALUE; 436 } 437 438 // currently we ignore the region/rect client wants to lock 439 if (region == NULL) { 440 return GRALLOC1_ERROR_BAD_VALUE; 441 } 442 // TODO(user): Need to check if buffer was allocated with the same flags 443 status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage); 444 445 *out_data = reinterpret_cast<void *>(hnd->base); 446 447 return status; 448 } 449 450 gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer, 451 gralloc1_producer_usage_t prod_usage, 452 gralloc1_consumer_usage_t cons_usage, 453 const gralloc1_rect_t *region, 454 struct android_flex_layout *out_flex_layout, 455 int32_t acquire_fence) { 456 void *out_data; 457 gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region, 458 &out_data, acquire_fence); 459 if (status != GRALLOC1_ERROR_NONE) { 460 return status; 461 } 462 463 GrallocImpl const *dev = GRALLOC_IMPL(device); 464 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 465 dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout); 466 return status; 467 } 468 469 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, 470 int32_t *release_fence) { 471 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); 472 473 if (status != GRALLOC1_ERROR_NONE) { 474 return status; 475 } 476 477 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); 478 GrallocImpl const *dev = GRALLOC_IMPL(device); 479 480 *release_fence = -1; 481 482 return dev->buf_mgr_->UnlockBuffer(hnd); 483 } 484 485 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) { 486 va_list args; 487 va_start(args, operation); 488 GrallocImpl const *dev = GRALLOC_IMPL(device); 489 gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args); 490 va_end(args); 491 492 return err; 493 } 494 495 } // namespace gralloc1 496