Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #define LOG_TAG "HandleImporter" |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 18 | #include "HandleImporter.h" |
Steven Moreland | 4e7a307 | 2017-04-06 12:15:23 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace camera { |
| 24 | namespace common { |
| 25 | namespace V1_0 { |
| 26 | namespace helper { |
| 27 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 28 | using MapperError = android::hardware::graphics::mapper::V2_0::Error; |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 29 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 30 | HandleImporter::HandleImporter() : mInitialized(false) {} |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 31 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 32 | void HandleImporter::initializeLocked() { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 33 | if (mInitialized) { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 37 | mMapper = IMapper::getService(); |
| 38 | if (mMapper == nullptr) { |
| 39 | ALOGE("%s: cannnot acccess graphics mapper HAL!", __FUNCTION__); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | mInitialized = true; |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | void HandleImporter::cleanup() { |
| 48 | mMapper.clear(); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 49 | mInitialized = false; |
| 50 | } |
| 51 | |
| 52 | // In IComposer, any buffer_handle_t is owned by the caller and we need to |
| 53 | // make a clone for hwcomposer2. We also need to translate empty handle |
| 54 | // to nullptr. This function does that, in-place. |
| 55 | bool HandleImporter::importBuffer(buffer_handle_t& handle) { |
| 56 | if (!handle->numFds && !handle->numInts) { |
| 57 | handle = nullptr; |
| 58 | return true; |
| 59 | } |
| 60 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 61 | Mutex::Autolock lock(mLock); |
| 62 | if (!mInitialized) { |
| 63 | initializeLocked(); |
| 64 | } |
| 65 | |
| 66 | if (mMapper == nullptr) { |
| 67 | ALOGE("%s: mMapper is null!", __FUNCTION__); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 71 | MapperError error; |
| 72 | buffer_handle_t importedHandle; |
| 73 | auto ret = mMapper->importBuffer( |
| 74 | hidl_handle(handle), |
| 75 | [&](const auto& tmpError, const auto& tmpBufferHandle) { |
| 76 | error = tmpError; |
| 77 | importedHandle = static_cast<buffer_handle_t>(tmpBufferHandle); |
| 78 | }); |
| 79 | |
| 80 | if (!ret.isOk()) { |
| 81 | ALOGE("%s: mapper importBuffer failed: %s", |
| 82 | __FUNCTION__, ret.description().c_str()); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (error != MapperError::NONE) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | handle = importedHandle; |
| 91 | |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | void HandleImporter::freeBuffer(buffer_handle_t handle) { |
| 96 | if (!handle) { |
| 97 | return; |
| 98 | } |
| 99 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 100 | Mutex::Autolock lock(mLock); |
| 101 | if (mMapper == nullptr) { |
| 102 | ALOGE("%s: mMapper is null!", __FUNCTION__); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | auto ret = mMapper->freeBuffer(const_cast<native_handle_t*>(handle)); |
| 107 | if (!ret.isOk()) { |
| 108 | ALOGE("%s: mapper freeBuffer failed: %s", |
| 109 | __FUNCTION__, ret.description().c_str()); |
| 110 | } |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 113 | bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 114 | if (handle == nullptr || handle->numFds == 0) { |
| 115 | fd = -1; |
| 116 | } else if (handle->numFds == 1) { |
| 117 | fd = dup(handle->data[0]); |
| 118 | if (fd < 0) { |
| 119 | ALOGE("failed to dup fence fd %d", handle->data[0]); |
| 120 | return false; |
| 121 | } |
| 122 | } else { |
| 123 | ALOGE("invalid fence handle with %d file descriptors", |
| 124 | handle->numFds); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 131 | void HandleImporter::closeFence(int fd) const { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 132 | if (fd >= 0) { |
| 133 | close(fd); |
| 134 | } |
| 135 | } |
| 136 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame^] | 137 | YCbCrLayout HandleImporter::lockYCbCr( |
| 138 | buffer_handle_t& buf, uint64_t cpuUsage, |
| 139 | const IMapper::Rect& accessRegion) { |
| 140 | Mutex::Autolock lock(mLock); |
| 141 | YCbCrLayout layout = {}; |
| 142 | |
| 143 | if (!mInitialized) { |
| 144 | initializeLocked(); |
| 145 | } |
| 146 | |
| 147 | if (mMapper == nullptr) { |
| 148 | ALOGE("%s: mMapper is null!", __FUNCTION__); |
| 149 | return layout; |
| 150 | } |
| 151 | |
| 152 | hidl_handle acquireFenceHandle; |
| 153 | auto buffer = const_cast<native_handle_t*>(buf); |
| 154 | mMapper->lockYCbCr(buffer, cpuUsage, accessRegion, acquireFenceHandle, |
| 155 | [&](const auto& tmpError, const auto& tmpLayout) { |
| 156 | if (tmpError == MapperError::NONE) { |
| 157 | layout = tmpLayout; |
| 158 | } else { |
| 159 | ALOGE("%s: failed to lockYCbCr error %d!", __FUNCTION__, tmpError); |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | ALOGV("%s: layout y %p cb %p cr %p y_str %d c_str %d c_step %d", |
| 164 | __FUNCTION__, layout.y, layout.cb, layout.cr, |
| 165 | layout.yStride, layout.cStride, layout.chromaStep); |
| 166 | return layout; |
| 167 | } |
| 168 | |
| 169 | int HandleImporter::unlock(buffer_handle_t& buf) { |
| 170 | int releaseFence = -1; |
| 171 | auto buffer = const_cast<native_handle_t*>(buf); |
| 172 | mMapper->unlock( |
| 173 | buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) { |
| 174 | if (tmpError == MapperError::NONE) { |
| 175 | auto fenceHandle = tmpReleaseFence.getNativeHandle(); |
| 176 | if (fenceHandle) { |
| 177 | if (fenceHandle->numInts != 0 || fenceHandle->numFds != 1) { |
| 178 | ALOGE("%s: bad release fence numInts %d numFds %d", |
| 179 | __FUNCTION__, fenceHandle->numInts, fenceHandle->numFds); |
| 180 | return; |
| 181 | } |
| 182 | releaseFence = dup(fenceHandle->data[0]); |
| 183 | if (releaseFence <= 0) { |
| 184 | ALOGE("%s: bad release fence FD %d", |
| 185 | __FUNCTION__, releaseFence); |
| 186 | } |
| 187 | } |
| 188 | } else { |
| 189 | ALOGE("%s: failed to unlock error %d!", __FUNCTION__, tmpError); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | return releaseFence; |
| 194 | } |
| 195 | |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 196 | } // namespace helper |
| 197 | } // namespace V1_0 |
| 198 | } // namespace common |
| 199 | } // namespace camera |
| 200 | } // namespace hardware |
| 201 | } // namespace android |