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" |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 19 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 20 | #include <aidl/android/hardware/graphics/common/Smpte2086.h> |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 21 | #include <gralloctypes/Gralloc4.h> |
Steven Moreland | 4e7a307 | 2017-04-06 12:15:23 -0700 | [diff] [blame] | 22 | #include <log/log.h> |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 23 | #include <ui/GraphicBufferMapper.h> |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace camera { |
| 28 | namespace common { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 29 | namespace helper { |
| 30 | |
Jason Macnak | eda6dca | 2020-04-15 15:20:59 -0700 | [diff] [blame] | 31 | using aidl::android::hardware::graphics::common::PlaneLayout; |
| 32 | using aidl::android::hardware::graphics::common::PlaneLayoutComponent; |
| 33 | using aidl::android::hardware::graphics::common::PlaneLayoutComponentType; |
Emilian Peev | dda1eb7 | 2022-07-28 16:37:40 -0700 | [diff] [blame] | 34 | using aidl::android::hardware::graphics::common::Smpte2086; |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 35 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 36 | HandleImporter::HandleImporter() : mInitialized(false) {} |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 37 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 38 | void HandleImporter::initializeLocked() { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 39 | if (mInitialized) { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 43 | GraphicBufferMapper::preloadHal(); |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 44 | mInitialized = true; |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | void HandleImporter::cleanup() { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 49 | mInitialized = false; |
| 50 | } |
| 51 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 52 | bool HandleImporter::importBufferInternal(buffer_handle_t& handle) { |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 53 | buffer_handle_t importedHandle; |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 54 | auto status = GraphicBufferMapper::get().importBufferNoValidate(handle, &importedHandle); |
| 55 | if (status != OK) { |
| 56 | ALOGE("%s: mapper importBuffer failed: %d", __FUNCTION__, status); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | handle = importedHandle; |
| 61 | return true; |
| 62 | } |
| 63 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 64 | android_ycbcr HandleImporter::lockYCbCr(buffer_handle_t& buf, uint64_t cpuUsage, |
| 65 | const android::Rect& accessRegion) { |
| 66 | Mutex::Autolock lock(mLock); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 67 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 68 | if (!mInitialized) { |
| 69 | initializeLocked(); |
| 70 | } |
| 71 | android_ycbcr layout; |
| 72 | |
| 73 | status_t status = GraphicBufferMapper::get().lockYCbCr(buf, cpuUsage, accessRegion, &layout); |
| 74 | |
| 75 | if (status != OK) { |
| 76 | ALOGE("%s: failed to lockYCbCr error %d!", __FUNCTION__, status); |
| 77 | } |
| 78 | |
Michael Stokes | 49ba82c | 2023-10-02 09:56:59 +0000 | [diff] [blame] | 79 | return layout; |
| 80 | } |
| 81 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 82 | std::vector<PlaneLayout> getPlaneLayouts(buffer_handle_t& buf) { |
Michael Stokes | 49ba82c | 2023-10-02 09:56:59 +0000 | [diff] [blame] | 83 | std::vector<PlaneLayout> planeLayouts; |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 84 | status_t status = GraphicBufferMapper::get().getPlaneLayouts(buf, &planeLayouts); |
| 85 | if (status != OK) { |
| 86 | ALOGE("%s: failed to get PlaneLayouts! Status %d", __FUNCTION__, status); |
| 87 | } |
Michael Stokes | 49ba82c | 2023-10-02 09:56:59 +0000 | [diff] [blame] | 88 | |
| 89 | return planeLayouts; |
| 90 | } |
| 91 | |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 92 | // In IComposer, any buffer_handle_t is owned by the caller and we need to |
| 93 | // make a clone for hwcomposer2. We also need to translate empty handle |
| 94 | // to nullptr. This function does that, in-place. |
| 95 | bool HandleImporter::importBuffer(buffer_handle_t& handle) { |
| 96 | if (!handle->numFds && !handle->numInts) { |
| 97 | handle = nullptr; |
| 98 | return true; |
| 99 | } |
| 100 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 101 | Mutex::Autolock lock(mLock); |
| 102 | if (!mInitialized) { |
| 103 | initializeLocked(); |
| 104 | } |
| 105 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 106 | return importBufferInternal(handle); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void HandleImporter::freeBuffer(buffer_handle_t handle) { |
| 110 | if (!handle) { |
| 111 | return; |
| 112 | } |
| 113 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 114 | Mutex::Autolock lock(mLock); |
Yin-Chia Yeh | 97978fb | 2020-01-25 18:15:00 -0800 | [diff] [blame] | 115 | if (!mInitialized) { |
| 116 | initializeLocked(); |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 119 | status_t status = GraphicBufferMapper::get().freeBuffer(handle); |
| 120 | if (status != OK) { |
| 121 | ALOGE("%s: mapper freeBuffer failed. Status %d", __FUNCTION__, status); |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 122 | } |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 125 | bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 126 | if (handle == nullptr || handle->numFds == 0) { |
| 127 | fd = -1; |
| 128 | } else if (handle->numFds == 1) { |
| 129 | fd = dup(handle->data[0]); |
| 130 | if (fd < 0) { |
| 131 | ALOGE("failed to dup fence fd %d", handle->data[0]); |
| 132 | return false; |
| 133 | } |
| 134 | } else { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 135 | ALOGE("invalid fence handle with %d file descriptors", handle->numFds); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
Yin-Chia Yeh | 519c167 | 2017-04-21 14:59:31 -0700 | [diff] [blame] | 142 | void HandleImporter::closeFence(int fd) const { |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 143 | if (fd >= 0) { |
| 144 | close(fd); |
| 145 | } |
| 146 | } |
| 147 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 148 | void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage, size_t size) { |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 149 | android::Rect accessRegion{0, 0, static_cast<int>(size), 1}; |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 150 | return lock(buf, cpuUsage, accessRegion); |
| 151 | } |
| 152 | |
| 153 | void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage, |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 154 | const android::Rect& accessRegion) { |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 155 | Mutex::Autolock lock(mLock); |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 156 | |
| 157 | if (!mInitialized) { |
| 158 | initializeLocked(); |
| 159 | } |
| 160 | |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 161 | void* ret = nullptr; |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 162 | status_t status = GraphicBufferMapper::get().lock(buf, cpuUsage, accessRegion, &ret); |
| 163 | if (status != OK) { |
| 164 | ALOGE("%s: failed to lock error %d!", __FUNCTION__, status); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 165 | } |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 166 | |
Jason Macnak | f2c9ed1 | 2020-04-20 14:43:58 -0700 | [diff] [blame] | 167 | ALOGV("%s: ptr %p accessRegion.top: %d accessRegion.left: %d accessRegion.width: %d " |
| 168 | "accessRegion.height: %d", |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 169 | __FUNCTION__, ret, accessRegion.top, accessRegion.left, accessRegion.width(), |
| 170 | accessRegion.height()); |
Yuriy Romanenko | d0bd4f1 | 2018-01-19 16:00:00 -0800 | [diff] [blame] | 171 | return ret; |
| 172 | } |
| 173 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 174 | status_t HandleImporter::getMonoPlanarStrideBytes(buffer_handle_t& buf, uint32_t* stride /*out*/) { |
Emilian Peev | 6a2572b | 2021-05-24 16:51:17 -0700 | [diff] [blame] | 175 | if (stride == nullptr) { |
| 176 | return BAD_VALUE; |
| 177 | } |
| 178 | |
| 179 | Mutex::Autolock lock(mLock); |
| 180 | |
| 181 | if (!mInitialized) { |
| 182 | initializeLocked(); |
| 183 | } |
| 184 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 185 | std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(buf); |
| 186 | if (planeLayouts.size() != 1) { |
| 187 | ALOGE("%s: Unexpected number of planes %zu!", __FUNCTION__, planeLayouts.size()); |
| 188 | return BAD_VALUE; |
Michael Stokes | 49ba82c | 2023-10-02 09:56:59 +0000 | [diff] [blame] | 189 | } |
Devin Moore | 5e15409 | 2023-09-13 16:18:30 +0000 | [diff] [blame] | 190 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 191 | *stride = planeLayouts[0].strideInBytes; |
| 192 | |
Emilian Peev | 6a2572b | 2021-05-24 16:51:17 -0700 | [diff] [blame] | 193 | return OK; |
| 194 | } |
| 195 | |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 196 | int HandleImporter::unlock(buffer_handle_t& buf) { |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 197 | int releaseFence = -1; |
| 198 | |
| 199 | status_t status = GraphicBufferMapper::get().unlockAsync(buf, &releaseFence); |
| 200 | if (status != OK) { |
| 201 | ALOGE("%s: failed to unlock error %d!", __FUNCTION__, status); |
Shuzhen Wang | 915115e | 2019-05-10 12:07:14 -0700 | [diff] [blame] | 202 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 203 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 204 | return releaseFence; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 207 | bool HandleImporter::isSmpte2086Present(const buffer_handle_t& buf) { |
| 208 | Mutex::Autolock lock(mLock); |
| 209 | |
| 210 | if (!mInitialized) { |
| 211 | initializeLocked(); |
| 212 | } |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 213 | std::optional<ui::Smpte2086> metadata; |
| 214 | status_t status = GraphicBufferMapper::get().getSmpte2086(buf, &metadata); |
| 215 | if (status != OK) { |
| 216 | ALOGE("%s: Mapper failed to get Smpte2094_40 metadata! Status: %d", __FUNCTION__, status); |
| 217 | return false; |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 220 | return metadata.has_value(); |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | bool HandleImporter::isSmpte2094_10Present(const buffer_handle_t& buf) { |
| 224 | Mutex::Autolock lock(mLock); |
| 225 | |
| 226 | if (!mInitialized) { |
| 227 | initializeLocked(); |
| 228 | } |
| 229 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 230 | std::optional<std::vector<uint8_t>> metadata; |
| 231 | status_t status = GraphicBufferMapper::get().getSmpte2094_10(buf, &metadata); |
| 232 | if (status != OK) { |
| 233 | ALOGE("%s: Mapper failed to get Smpte2094_40 metadata! Status: %d", __FUNCTION__, status); |
| 234 | return false; |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 237 | return metadata.has_value(); |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool HandleImporter::isSmpte2094_40Present(const buffer_handle_t& buf) { |
| 241 | Mutex::Autolock lock(mLock); |
| 242 | |
| 243 | if (!mInitialized) { |
| 244 | initializeLocked(); |
| 245 | } |
| 246 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 247 | std::optional<std::vector<uint8_t>> metadata; |
| 248 | status_t status = GraphicBufferMapper::get().getSmpte2094_40(buf, &metadata); |
| 249 | if (status != OK) { |
| 250 | ALOGE("%s: Mapper failed to get Smpte2094_40 metadata! Status: %d", __FUNCTION__, status); |
| 251 | return false; |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Devin Moore | 523660c | 2023-10-02 15:55:11 +0000 | [diff] [blame] | 254 | return metadata.has_value(); |
Emilian Peev | b5f634f | 2021-12-13 15:13:46 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 257 | } // namespace helper |
| 258 | } // namespace common |
| 259 | } // namespace camera |
| 260 | } // namespace hardware |
| 261 | } // namespace android |