1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <limits.h> 17 #include <errno.h> 18 #include <pthread.h> 19 #include <unistd.h> 20 #include <string.h> 21 22 #include <sys/mman.h> 23 #include <sys/stat.h> 24 #include <sys/types.h> 25 26 #include <cutils/hashmap.h> 27 #include <cutils/log.h> 28 #include <cutils/atomic.h> 29 30 #include <hardware/hardware.h> 31 #include <hardware/gralloc.h> 32 #include <system/graphics.h> 33 34 #include "gralloc_vsoc_priv.h" 35 #include "region_registry.h" 36 37 #define DEBUG_REFERENCES 1 38 #define DEBUG_MAX_LOCK_LEVEL 20 39 40 /*****************************************************************************/ 41 42 int gralloc_register_buffer(gralloc_module_t const* /*module*/, 43 buffer_handle_t handle) { 44 if (private_handle_t::validate(handle) < 0) { 45 return -EINVAL; 46 } 47 48 private_handle_t* hnd = (private_handle_t*)handle; 49 if (reference_region(__FUNCTION__, hnd)) { 50 return 0; 51 } else { 52 return -EIO; 53 } 54 } 55 56 int gralloc_unregister_buffer(gralloc_module_t const* /*module*/, 57 buffer_handle_t handle) { 58 if (private_handle_t::validate(handle) < 0) { 59 return -EINVAL; 60 } 61 private_handle_t* hnd = (private_handle_t*)handle; 62 return unreference_region("gralloc_unregister_buffer", hnd); 63 } 64 65 int gralloc_lock( 66 gralloc_module_t const* /*module*/, buffer_handle_t handle, int /*usage*/, 67 int /*l*/, int /*t*/, int /*w*/, int /*h*/, 68 void** vaddr) { 69 if (private_handle_t::validate(handle) < 0) { 70 return -EINVAL; 71 } 72 if (!vaddr) { 73 return -EINVAL; 74 } 75 private_handle_t* hnd = (private_handle_t*)handle; 76 #if DEBUG_REFERENCES 77 if (hnd->lock_level > DEBUG_MAX_LOCK_LEVEL) { 78 LOG_FATAL("%s: unbalanced lock detected. lock level = %d", 79 __FUNCTION__, hnd->lock_level); 80 } 81 ++hnd->lock_level; 82 #endif 83 void* base = reference_region("gralloc_lock", hnd); 84 *vaddr = reinterpret_cast<unsigned char*>(base) 85 + hnd->frame_offset; 86 return 0; 87 } 88 89 int gralloc_unlock( 90 gralloc_module_t const* /*module*/, buffer_handle_t handle) { 91 if (private_handle_t::validate(handle) < 0) { 92 return -EINVAL; 93 } 94 private_handle_t* hnd = (private_handle_t*) handle; 95 #if DEBUG_REFERENCES 96 if (hnd->lock_level <= 0) { 97 LOG_FATAL("%s unbalanced unlock detected. lock level = %d", 98 __FUNCTION__, hnd->lock_level); 99 } 100 --hnd->lock_level; 101 #endif 102 unreference_region("gralloc_unlock", hnd); 103 return 0; 104 } 105 106 int gralloc_lock_ycbcr( 107 gralloc_module_t const* /*module*/, buffer_handle_t handle, int /*usage*/, 108 int /*l*/, int /*t*/, int /*w*/, int /*h*/, 109 struct android_ycbcr* ycbcr) { 110 if (private_handle_t::validate(handle) < 0) { 111 return -EINVAL; 112 } 113 private_handle_t* hnd = (private_handle_t*)handle; 114 #if DEBUG_REFERENCES 115 if (hnd->lock_level > DEBUG_MAX_LOCK_LEVEL) { 116 LOG_FATAL("%s: unbalanced lock detected. lock level = %d", 117 __FUNCTION__, hnd->lock_level); 118 } 119 ++hnd->lock_level; 120 #endif 121 void* base = reference_region("gralloc_lock_ycbcr", hnd); 122 formatToYcbcr(hnd->format, hnd->x_res, hnd->y_res, base, ycbcr); 123 return 0; 124 } 125