Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [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 | |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 17 | #include "GrallocWrapper.h" |
| 18 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/graphics/allocator/IAllocator.h> |
| 20 | #include <aidlcommonsupport/NativeHandle.h> |
| 21 | #include <android/binder_manager.h> |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 22 | #include <android/hardware/graphics/allocator/2.0/IAllocator.h> |
| 23 | #include <android/hardware/graphics/allocator/3.0/IAllocator.h> |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 24 | #include <android/hardware/graphics/allocator/4.0/IAllocator.h> |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 25 | #include <android/hardware/graphics/mapper/2.0/IMapper.h> |
| 26 | #include <android/hardware/graphics/mapper/2.1/IMapper.h> |
| 27 | #include <android/hardware/graphics/mapper/3.0/IMapper.h> |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 28 | #include <android/hardware/graphics/mapper/4.0/IMapper.h> |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 29 | |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 32 | #include <cinttypes> |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 33 | #include <memory> |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 34 | #include <type_traits> |
| 35 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 36 | using IAllocatorAidl = ::aidl::android::hardware::graphics::allocator::IAllocator; |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 37 | using IAllocator2 = ::android::hardware::graphics::allocator::V2_0::IAllocator; |
| 38 | using IAllocator3 = ::android::hardware::graphics::allocator::V3_0::IAllocator; |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 39 | using IAllocator4 = ::android::hardware::graphics::allocator::V4_0::IAllocator; |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 40 | using IMapper2 = ::android::hardware::graphics::mapper::V2_0::IMapper; |
| 41 | using IMapper2_1 = ::android::hardware::graphics::mapper::V2_1::IMapper; |
| 42 | using IMapper3 = ::android::hardware::graphics::mapper::V3_0::IMapper; |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 43 | using IMapper4 = ::android::hardware::graphics::mapper::V4_0::IMapper; |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 44 | |
| 45 | using Error2 = ::android::hardware::graphics::mapper::V2_0::Error; |
| 46 | using Error3 = ::android::hardware::graphics::mapper::V3_0::Error; |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 47 | using Error4 = ::android::hardware::graphics::mapper::V4_0::Error; |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 48 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 49 | using ::aidl::android::hardware::common::NativeHandle; |
| 50 | using ::aidl::android::hardware::graphics::allocator::AllocationResult; |
| 51 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 52 | using ::android::hardware::graphics::common::V1_0::BufferUsage; |
| 53 | using ::android::hardware::graphics::common::V1_0::PixelFormat; |
| 54 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 55 | using ::android::hardware::hidl_handle; |
| 56 | using ::android::hardware::hidl_string; |
| 57 | using ::android::hardware::hidl_vec; |
| 58 | |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 59 | namespace android { |
| 60 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 61 | // Since we use the same APIs across allocator/mapper HALs but they have major |
| 62 | // version differences (meaning they are not related through inheritance), we |
| 63 | // create a common interface abstraction for the IAllocator + IMapper combination |
| 64 | // (major versions need to match in the current HALs, e.g. IAllocator 3.0 needs to |
| 65 | // be paired with IMapper 3.0, so these are tied together) |
| 66 | class IGrallocHalWrapper { |
| 67 | public: |
| 68 | virtual ~IGrallocHalWrapper() = default; |
| 69 | |
| 70 | // IAllocator |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 71 | virtual native_handle_t* allocate(uint32_t size) = 0; |
| 72 | virtual void freeBuffer(native_handle_t* bufferHandle) = 0; |
| 73 | |
| 74 | // IMapper |
| 75 | virtual void* lock(native_handle_t* bufferHandle) = 0; |
| 76 | virtual void unlock(native_handle_t* bufferHandle) = 0; |
| 77 | }; |
| 78 | |
| 79 | namespace { |
| 80 | |
| 81 | bool failed(Error2 error) { |
| 82 | return (error != Error2::NONE); |
| 83 | } |
| 84 | bool failed(Error3 error) { |
| 85 | return (error != Error3::NONE); |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 86 | } |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 87 | bool failed(Error4 error) { |
| 88 | return (error != Error4::NONE); |
| 89 | } |
| 90 | |
| 91 | template <typename> |
| 92 | struct FirstArg; |
| 93 | |
| 94 | // Template specialization for pointer to a non-static member function, which exposes |
| 95 | // the type of the first argument given to said function |
| 96 | template <typename ReturnType, typename ClassT, typename Arg1, typename... OtherArgs> |
| 97 | struct FirstArg<ReturnType (ClassT::*)(Arg1, OtherArgs...)> { |
| 98 | using type = Arg1; |
| 99 | }; |
| 100 | |
| 101 | // Alias to FirstArg which also removes any reference type and const associated |
| 102 | template <typename T> |
| 103 | using BaseTypeOfFirstArg = typename std::remove_const< |
| 104 | typename std::remove_reference<typename FirstArg<T>::type>::type>::type; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 105 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 106 | // Since all the type and function names are the same for the things we use across the major HAL |
| 107 | // versions, we use template magic to avoid repeating ourselves. |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 108 | template <typename AllocatorT, typename MapperT, |
| 109 | template <typename> typename AllocatorWrapperT = sp> |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 110 | class GrallocHalWrapper : public IGrallocHalWrapper { |
| 111 | public: |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 112 | GrallocHalWrapper(const AllocatorWrapperT<AllocatorT>& allocator, const sp<MapperT>& mapper) |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 113 | : mAllocator(allocator), mMapper(mapper) { |
| 114 | if (mapper->isRemote()) { |
| 115 | ALOGE("Mapper is in passthrough mode"); |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 118 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 119 | virtual native_handle_t* allocate(uint32_t size) override; |
| 120 | virtual void freeBuffer(native_handle_t* bufferHandle) override; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 121 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 122 | virtual void* lock(native_handle_t* bufferHandle) override; |
| 123 | virtual void unlock(native_handle_t* bufferHandle) override; |
| 124 | |
| 125 | private: |
| 126 | static constexpr uint64_t kBufferUsage = |
Arthur Ishiguro | 802c53d | 2023-08-09 18:17:23 +0000 | [diff] [blame] | 127 | static_cast<uint64_t>(BufferUsage::SENSOR_DIRECT_DATA | BufferUsage::CPU_READ_OFTEN | |
| 128 | BufferUsage::CPU_WRITE_RARELY); |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 129 | AllocatorWrapperT<AllocatorT> mAllocator; |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 130 | sp<MapperT> mMapper; |
| 131 | |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 132 | // v2.0 and v3.0 use vec<uint32_t> for BufferDescriptor, but v4.0 uses vec<uint8_t>, so use |
| 133 | // some template magic to deduce the right type based off of the first argument to allocate(), |
| 134 | // which is always the version-specific BufferDescriptor type |
| 135 | typedef BaseTypeOfFirstArg<decltype(&AllocatorT::allocate)> BufferDescriptorT; |
| 136 | |
| 137 | BufferDescriptorT getDescriptor(uint32_t size); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 138 | native_handle_t* importBuffer(const hidl_handle& rawHandle); |
| 139 | }; |
| 140 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 141 | template <> |
| 142 | native_handle_t* GrallocHalWrapper<IAllocatorAidl, IMapper4, std::shared_ptr>::allocate( |
| 143 | uint32_t size) { |
| 144 | constexpr uint32_t kBufferCount = 1; |
| 145 | BufferDescriptorT descriptor = getDescriptor(size); |
| 146 | native_handle_t* bufferHandle = nullptr; |
| 147 | |
| 148 | AllocationResult result; |
| 149 | auto status = mAllocator->allocate(descriptor, kBufferCount, &result); |
| 150 | if (!status.isOk()) { |
| 151 | status_t error = status.getExceptionCode(); |
| 152 | ALOGE("Failed to allocate buffer: %" PRId32, static_cast<int32_t>(error)); |
| 153 | } else if (result.buffers.size() != kBufferCount) { |
| 154 | ALOGE("Invalid buffer array size (got %zu, expected %" PRIu32 ")", result.buffers.size(), |
| 155 | kBufferCount); |
| 156 | } else { |
| 157 | // Convert from AIDL NativeHandle to native_handle_t to hidl_handle |
| 158 | hidl_handle hidlHandle; |
| 159 | hidlHandle.setTo(dupFromAidl(result.buffers[0]), /*shouldOwn*/ true); |
| 160 | bufferHandle = importBuffer(hidlHandle); |
| 161 | } |
| 162 | |
| 163 | return bufferHandle; |
| 164 | } |
| 165 | |
| 166 | template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT> |
| 167 | native_handle_t* GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::allocate( |
| 168 | uint32_t size) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 169 | constexpr uint32_t kBufferCount = 1; |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 170 | BufferDescriptorT descriptor = getDescriptor(size); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 171 | native_handle_t* bufferHandle = nullptr; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 172 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 173 | auto callback = [&](auto error, uint32_t /*stride*/, const hidl_vec<hidl_handle>& buffers) { |
| 174 | if (failed(error)) { |
| 175 | ALOGE("Failed to allocate buffer: %" PRId32, static_cast<int32_t>(error)); |
| 176 | } else if (buffers.size() != kBufferCount) { |
| 177 | ALOGE("Invalid buffer array size (got %zu, expected %" PRIu32 ")", buffers.size(), |
| 178 | kBufferCount); |
| 179 | } else { |
| 180 | bufferHandle = importBuffer(buffers[0]); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | mAllocator->allocate(descriptor, kBufferCount, callback); |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 185 | return bufferHandle; |
| 186 | } |
| 187 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 188 | template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT> |
| 189 | void GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::freeBuffer( |
| 190 | native_handle_t* bufferHandle) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 191 | auto error = mMapper->freeBuffer(bufferHandle); |
| 192 | if (!error.isOk() || failed(error)) { |
| 193 | ALOGE("Failed to free buffer %p", bufferHandle); |
| 194 | } |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 197 | template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT> |
| 198 | typename GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::BufferDescriptorT |
| 199 | GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::getDescriptor(uint32_t size) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 200 | typename MapperT::BufferDescriptorInfo descriptorInfo = { |
| 201 | .width = size, |
| 202 | .height = 1, |
| 203 | .layerCount = 1, |
Nick Desaulniers | 65d03e4 | 2019-10-28 13:44:49 -0700 | [diff] [blame] | 204 | .format = static_cast<decltype(descriptorInfo.format)>(PixelFormat::BLOB), |
Nick Desaulniers | 6f4a232 | 2019-11-12 13:48:58 -0800 | [diff] [blame] | 205 | .usage = kBufferUsage, |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 206 | }; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 207 | |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 208 | BufferDescriptorT descriptor; |
| 209 | auto callback = [&](auto error, const BufferDescriptorT& tmpDescriptor) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 210 | if (failed(error)) { |
| 211 | ALOGE("Failed to create descriptor: %" PRId32, static_cast<int32_t>(error)); |
| 212 | } else { |
| 213 | descriptor = tmpDescriptor; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 214 | } |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 215 | }; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 216 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 217 | mMapper->createDescriptor(descriptorInfo, callback); |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 218 | return descriptor; |
| 219 | } |
| 220 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 221 | template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT> |
| 222 | native_handle_t* GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::importBuffer( |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 223 | const hidl_handle& rawHandle) { |
| 224 | native_handle_t* bufferHandle = nullptr; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 225 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 226 | mMapper->importBuffer(rawHandle, [&](auto error, void* tmpBuffer) { |
| 227 | if (failed(error)) { |
| 228 | ALOGE("Failed to import buffer %p: %" PRId32, rawHandle.getNativeHandle(), |
| 229 | static_cast<int32_t>(error)); |
| 230 | } else { |
| 231 | bufferHandle = static_cast<native_handle_t*>(tmpBuffer); |
| 232 | } |
| 233 | }); |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 234 | |
| 235 | return bufferHandle; |
| 236 | } |
| 237 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 238 | template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT> |
| 239 | void* GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::lock( |
| 240 | native_handle_t* bufferHandle) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 241 | // Per the HAL, all-zeros Rect means the entire buffer |
| 242 | typename MapperT::Rect accessRegion = {}; |
| 243 | hidl_handle acquireFenceHandle; // No fence needed, already safe to lock |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 244 | |
| 245 | void* data = nullptr; |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 246 | mMapper->lock(bufferHandle, kBufferUsage, accessRegion, acquireFenceHandle, |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 247 | [&](auto error, void* tmpData, ...) { // V3/4 pass extra args we don't use |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 248 | if (failed(error)) { |
| 249 | ALOGE("Failed to lock buffer %p: %" PRId32, bufferHandle, |
| 250 | static_cast<int32_t>(error)); |
| 251 | } else { |
| 252 | data = tmpData; |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 253 | } |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 254 | }); |
| 255 | |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 256 | return data; |
| 257 | } |
| 258 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 259 | template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT> |
| 260 | void GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::unlock( |
| 261 | native_handle_t* bufferHandle) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 262 | mMapper->unlock(bufferHandle, [&](auto error, const hidl_handle& /*releaseFence*/) { |
| 263 | if (failed(error)) { |
| 264 | ALOGE("Failed to unlock buffer %p: %" PRId32, bufferHandle, |
| 265 | static_cast<int32_t>(error)); |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 266 | } |
| 267 | }); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 268 | } |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 269 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 270 | } // anonymous namespace |
| 271 | |
| 272 | GrallocWrapper::GrallocWrapper() { |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 273 | sp<IAllocator4> allocator4 = IAllocator4::getService(); |
| 274 | sp<IMapper4> mapper4 = IMapper4::getService(); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 275 | |
Brian Duddie | c325337 | 2022-06-09 12:37:53 -0700 | [diff] [blame] | 276 | const auto kAllocatorSvc = std::string(IAllocatorAidl::descriptor) + "/default"; |
| 277 | std::shared_ptr<IAllocatorAidl> allocatorAidl; |
| 278 | if (AServiceManager_isDeclared(kAllocatorSvc.c_str())) { |
| 279 | allocatorAidl = IAllocatorAidl::fromBinder( |
| 280 | ndk::SpAIBinder(AServiceManager_checkService(kAllocatorSvc.c_str()))); |
| 281 | } |
| 282 | |
| 283 | // As of T, AIDL Allocator is supported only with HIDL Mapper4 |
| 284 | // (ref: VtsHalGraphicsAllocatorAidl_TargetTest.cpp) |
| 285 | if (allocatorAidl != nullptr && mapper4 != nullptr) { |
| 286 | ALOGD("Using AIDL IAllocator + HIDL IMapper v4.0"); |
| 287 | mGrallocHal = std::unique_ptr<IGrallocHalWrapper>( |
| 288 | new GrallocHalWrapper<IAllocatorAidl, IMapper4, std::shared_ptr>(allocatorAidl, |
| 289 | mapper4)); |
| 290 | } else if (allocator4 != nullptr && mapper4 != nullptr) { |
| 291 | ALOGD("AIDL IAllocator not found, using HIDL IAllocator/IMapper v4.0"); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 292 | mGrallocHal = std::unique_ptr<IGrallocHalWrapper>( |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 293 | new GrallocHalWrapper<IAllocator4, IMapper4>(allocator4, mapper4)); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 294 | } else { |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 295 | ALOGD("Graphics HALs 4.0 not found (allocator %d mapper %d), falling back to 3.0", |
| 296 | (allocator4 != nullptr), (mapper4 != nullptr)); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 297 | |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 298 | sp<IAllocator3> allocator3 = IAllocator3::getService(); |
| 299 | sp<IMapper3> mapper3 = IMapper3::getService(); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 300 | |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 301 | if (allocator3 != nullptr && mapper3 != nullptr) { |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 302 | mGrallocHal = std::unique_ptr<IGrallocHalWrapper>( |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 303 | new GrallocHalWrapper<IAllocator3, IMapper3>(allocator3, mapper3)); |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 304 | } else { |
Brian Duddie | 97f1a3a | 2020-06-04 14:42:46 -0700 | [diff] [blame] | 305 | ALOGD("Graphics HALs 3.0 not found (allocator %d mapper %d), falling back to 2.x", |
| 306 | (allocator3 != nullptr), (mapper3 != nullptr)); |
| 307 | |
| 308 | sp<IAllocator2> allocator2 = IAllocator2::getService(); |
| 309 | sp<IMapper2> mapper2 = IMapper2_1::getService(); |
| 310 | if (mapper2 == nullptr) { |
| 311 | mapper2 = IMapper2::getService(); |
| 312 | } |
| 313 | |
| 314 | if (allocator2 != nullptr && mapper2 != nullptr) { |
| 315 | mGrallocHal = std::unique_ptr<IGrallocHalWrapper>( |
| 316 | new GrallocHalWrapper<IAllocator2, IMapper2>(allocator2, mapper2)); |
| 317 | } else { |
| 318 | ALOGE("Couldn't open graphics HALs (2.x allocator %d mapper %d)", |
| 319 | (allocator2 != nullptr), (mapper2 != nullptr)); |
| 320 | } |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | GrallocWrapper::~GrallocWrapper() { |
| 326 | for (auto bufferHandle : mAllocatedBuffers) { |
| 327 | mGrallocHal->unlock(bufferHandle); |
| 328 | mGrallocHal->freeBuffer(bufferHandle); |
| 329 | } |
| 330 | mAllocatedBuffers.clear(); |
| 331 | } |
| 332 | |
Brian Duddie | df6e2b7 | 2019-08-01 19:18:06 -0700 | [diff] [blame] | 333 | std::pair<native_handle_t*, void*> GrallocWrapper::allocate(uint32_t size) { |
| 334 | native_handle_t* bufferHandle = mGrallocHal->allocate(size); |
| 335 | void* buffer = nullptr; |
| 336 | if (bufferHandle) { |
| 337 | buffer = mGrallocHal->lock(bufferHandle); |
| 338 | if (buffer) { |
| 339 | mAllocatedBuffers.insert(bufferHandle); |
| 340 | } else { |
| 341 | mGrallocHal->freeBuffer(bufferHandle); |
| 342 | bufferHandle = nullptr; |
| 343 | } |
| 344 | } |
| 345 | return std::make_pair<>(bufferHandle, buffer); |
| 346 | } |
| 347 | |
| 348 | void GrallocWrapper::freeBuffer(native_handle_t* bufferHandle) { |
| 349 | if (mAllocatedBuffers.erase(bufferHandle)) { |
| 350 | mGrallocHal->unlock(bufferHandle); |
| 351 | mGrallocHal->freeBuffer(bufferHandle); |
| 352 | } |
Brian Stack | eb755f5 | 2018-09-26 14:53:25 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | } // namespace android |