Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "GrallocAndroid.hpp" |
| 16 | #include "Debug.hpp" |
| 17 | |
| 18 | #ifdef HAVE_GRALLOC1 |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 19 | # include <sync/sync.h> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 20 | #endif |
Jason Macnak | 1de497c | 2020-04-08 11:31:50 -0700 | [diff] [blame] | 21 | #ifdef HAVE_GRALLOC3 |
| 22 | using V3Error = android::hardware::graphics::mapper::V3_0::Error; |
| 23 | using V3Mapper = android::hardware::graphics::mapper::V3_0::IMapper; |
| 24 | using android::hardware::hidl_handle; |
| 25 | #endif |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 26 | |
| 27 | GrallocModule *GrallocModule::getInstance() |
| 28 | { |
| 29 | static GrallocModule instance; |
| 30 | return &instance; |
| 31 | } |
| 32 | |
| 33 | GrallocModule::GrallocModule() |
| 34 | { |
Jason Macnak | 1de497c | 2020-04-08 11:31:50 -0700 | [diff] [blame] | 35 | #ifdef HAVE_GRALLOC3 |
| 36 | m_gralloc3_mapper = V3Mapper::getService(); |
| 37 | if(m_gralloc3_mapper != nullptr) |
| 38 | { |
| 39 | return; |
| 40 | } |
| 41 | #endif |
| 42 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 43 | const hw_module_t *module = nullptr; |
| 44 | hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| 45 | |
| 46 | m_major_version = (module->module_api_version >> 8) & 0xff; |
| 47 | switch(m_major_version) |
| 48 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 49 | case 0: |
| 50 | m_module = reinterpret_cast<const gralloc_module_t *>(module); |
| 51 | break; |
| 52 | case 1: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 53 | #ifdef HAVE_GRALLOC1 |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 54 | gralloc1_open(module, &m_gralloc1_device); |
| 55 | m_gralloc1_lock = (GRALLOC1_PFN_LOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK); |
| 56 | m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK); |
| 57 | break; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 58 | #endif |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 59 | default: |
| 60 | TRACE("unknown gralloc major version (%d)", m_major_version); |
| 61 | break; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Jason Macnak | 1de497c | 2020-04-08 11:31:50 -0700 | [diff] [blame] | 65 | int GrallocModule::import(buffer_handle_t handle, buffer_handle_t *imported_handle) |
| 66 | { |
| 67 | #ifdef HAVE_GRALLOC3 |
| 68 | if(m_gralloc3_mapper != nullptr) |
| 69 | { |
| 70 | V3Error error; |
| 71 | auto ret = m_gralloc3_mapper->importBuffer(handle, |
| 72 | [&](const auto &tmp_err, const auto &tmp_buf) { |
| 73 | error = tmp_err; |
| 74 | if(error == V3Error::NONE) |
| 75 | { |
| 76 | *imported_handle = static_cast<buffer_handle_t>(tmp_buf); |
| 77 | } |
| 78 | }); |
| 79 | return ret.isOk() && error == V3Error::NONE ? 0 : -1; |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | *imported_handle = handle; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int GrallocModule::release(buffer_handle_t handle) |
| 88 | { |
| 89 | #ifdef HAVE_GRALLOC3 |
| 90 | if(m_gralloc3_mapper != nullptr) |
| 91 | { |
| 92 | native_handle_t *native_handle = const_cast<native_handle_t *>(handle); |
| 93 | return m_gralloc3_mapper->freeBuffer(native_handle).isOk() ? 0 : 1; |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 100 | int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr) |
| 101 | { |
Jason Macnak | 1de497c | 2020-04-08 11:31:50 -0700 | [diff] [blame] | 102 | #ifdef HAVE_GRALLOC3 |
| 103 | if(m_gralloc3_mapper != nullptr) |
| 104 | { |
| 105 | native_handle_t *native_handle = const_cast<native_handle_t *>(handle); |
| 106 | |
| 107 | V3Mapper::Rect rect; |
| 108 | rect.left = left; |
| 109 | rect.top = top; |
| 110 | rect.width = width; |
| 111 | rect.height = height; |
| 112 | |
| 113 | hidl_handle empty_fence_handle; |
| 114 | |
| 115 | V3Error error; |
| 116 | auto ret = m_gralloc3_mapper->lock(native_handle, usage, rect, empty_fence_handle, |
| 117 | [&](const auto &tmp_err, |
| 118 | const auto &tmp_vaddr, |
| 119 | const auto & /*bytes_per_pixel*/, |
| 120 | const auto & /*bytes_per_stride*/) { |
| 121 | error = tmp_err; |
| 122 | if(tmp_err == V3Error::NONE) |
| 123 | { |
| 124 | *vaddr = tmp_vaddr; |
| 125 | } |
| 126 | }); |
| 127 | return ret.isOk() && error == V3Error::NONE ? 0 : -1; |
| 128 | } |
| 129 | #endif |
| 130 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 131 | switch(m_major_version) |
| 132 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 133 | case 0: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 134 | { |
| 135 | return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr); |
| 136 | } |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 137 | case 1: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 138 | #ifdef HAVE_GRALLOC1 |
| 139 | { |
| 140 | gralloc1_rect_t outRect{}; |
| 141 | outRect.left = left; |
| 142 | outRect.top = top; |
| 143 | outRect.width = width; |
| 144 | outRect.height = height; |
| 145 | return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1); |
| 146 | } |
| 147 | #endif |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 148 | default: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 149 | { |
| 150 | TRACE("no gralloc module to lock"); |
| 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | int GrallocModule::unlock(buffer_handle_t handle) |
| 157 | { |
Jason Macnak | 1de497c | 2020-04-08 11:31:50 -0700 | [diff] [blame] | 158 | #ifdef HAVE_GRALLOC3 |
| 159 | if(m_gralloc3_mapper != nullptr) |
| 160 | { |
| 161 | native_handle_t *native_handle = const_cast<native_handle_t *>(handle); |
| 162 | |
| 163 | V3Error error; |
| 164 | auto ret = m_gralloc3_mapper->unlock(native_handle, |
| 165 | [&](const auto &tmp_err, const auto &) { |
| 166 | error = tmp_err; |
| 167 | }); |
| 168 | return ret.isOk() && error == V3Error::NONE ? 0 : -1; |
| 169 | } |
| 170 | #endif |
| 171 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 172 | switch(m_major_version) |
| 173 | { |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 174 | case 0: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 175 | { |
| 176 | return m_module->unlock(m_module, handle); |
| 177 | } |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 178 | case 1: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 179 | #ifdef HAVE_GRALLOC1 |
| 180 | { |
| 181 | int32_t fenceFd = -1; |
| 182 | int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 183 | if(!error) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 184 | { |
| 185 | sync_wait(fenceFd, -1); |
| 186 | close(fenceFd); |
| 187 | } |
| 188 | return error; |
| 189 | } |
| 190 | #endif |
Ben Clayton | 595d911 | 2019-12-17 20:37:57 +0000 | [diff] [blame] | 191 | default: |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 192 | { |
| 193 | TRACE("no gralloc module to unlock"); |
| 194 | return -1; |
| 195 | } |
| 196 | } |
| 197 | } |