Chia-I Wu | 109571a | 2016-09-05 11:46:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | #define LOG_TAG "GrallocPassthrough" |
| 18 | |
| 19 | #include <type_traits> |
| 20 | #include <unordered_set> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <hardware/gralloc1.h> |
| 26 | #include <log/log.h> |
| 27 | |
| 28 | #include "Gralloc.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace graphics { |
| 33 | namespace allocator { |
| 34 | namespace V2_0 { |
| 35 | namespace implementation { |
| 36 | |
| 37 | class GrallocHal : public IAllocator { |
| 38 | public: |
| 39 | GrallocHal(const hw_module_t* module); |
| 40 | virtual ~GrallocHal(); |
| 41 | |
| 42 | // IAllocator interface |
| 43 | Return<void> getCapabilities(getCapabilities_cb hidl_cb) override; |
| 44 | Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override; |
| 45 | Return<void> createDescriptor(const BufferDescriptorInfo& descriptorInfo, |
| 46 | createDescriptor_cb hidl_cb) override; |
| 47 | Return<Error> destroyDescriptor(BufferDescriptor descriptor) override; |
| 48 | |
| 49 | Return<Error> testAllocate( |
| 50 | const hidl_vec<BufferDescriptor>& descriptors) override; |
| 51 | Return<void> allocate(const hidl_vec<BufferDescriptor>& descriptors, |
| 52 | allocate_cb hidl_cb) override; |
| 53 | Return<Error> free(Buffer buffer) override; |
| 54 | |
| 55 | Return<void> exportHandle(BufferDescriptor descriptor, |
| 56 | Buffer buffer, exportHandle_cb hidl_cb) override; |
| 57 | |
| 58 | private: |
| 59 | void initCapabilities(); |
| 60 | |
| 61 | template<typename T> |
| 62 | void initDispatch(T& func, gralloc1_function_descriptor_t desc); |
| 63 | void initDispatch(); |
| 64 | |
| 65 | bool hasCapability(Capability capability) const; |
| 66 | |
| 67 | gralloc1_device_t* mDevice; |
| 68 | |
| 69 | std::unordered_set<Capability> mCapabilities; |
| 70 | |
| 71 | struct { |
| 72 | GRALLOC1_PFN_DUMP dump; |
| 73 | GRALLOC1_PFN_CREATE_DESCRIPTOR createDescriptor; |
| 74 | GRALLOC1_PFN_DESTROY_DESCRIPTOR destroyDescriptor; |
| 75 | GRALLOC1_PFN_SET_DIMENSIONS setDimensions; |
| 76 | GRALLOC1_PFN_SET_FORMAT setFormat; |
Craig Donner | 0b00adf | 2016-10-20 17:12:58 -0700 | [diff] [blame] | 77 | GRALLOC1_PFN_SET_LAYER_COUNT setLayerCount; |
Chia-I Wu | 109571a | 2016-09-05 11:46:36 +0800 | [diff] [blame] | 78 | GRALLOC1_PFN_SET_CONSUMER_USAGE setConsumerUsage; |
| 79 | GRALLOC1_PFN_SET_PRODUCER_USAGE setProducerUsage; |
| 80 | GRALLOC1_PFN_ALLOCATE allocate; |
| 81 | GRALLOC1_PFN_RELEASE release; |
| 82 | GRALLOC1_PFN_GET_BACKING_STORE getBackingStore; |
| 83 | GRALLOC1_PFN_GET_STRIDE getStride; |
| 84 | GRALLOC1_PFN_GET_NUM_FLEX_PLANES getNumFlexPlanes; |
| 85 | } mDispatch; |
| 86 | }; |
| 87 | |
| 88 | GrallocHal::GrallocHal(const hw_module_t* module) |
| 89 | : mDevice(nullptr), mDispatch() |
| 90 | { |
| 91 | int status = gralloc1_open(module, &mDevice); |
| 92 | if (status) { |
| 93 | LOG_ALWAYS_FATAL("failed to open gralloc1 device: %s", |
| 94 | strerror(-status)); |
| 95 | } |
| 96 | |
| 97 | initCapabilities(); |
| 98 | initDispatch(); |
| 99 | } |
| 100 | |
| 101 | GrallocHal::~GrallocHal() |
| 102 | { |
| 103 | gralloc1_close(mDevice); |
| 104 | } |
| 105 | |
| 106 | void GrallocHal::initCapabilities() |
| 107 | { |
| 108 | uint32_t count; |
| 109 | mDevice->getCapabilities(mDevice, &count, nullptr); |
| 110 | |
| 111 | std::vector<Capability> caps(count); |
| 112 | mDevice->getCapabilities(mDevice, &count, reinterpret_cast< |
| 113 | std::underlying_type<Capability>::type*>(caps.data())); |
| 114 | caps.resize(count); |
| 115 | |
| 116 | mCapabilities.insert(caps.cbegin(), caps.cend()); |
| 117 | } |
| 118 | |
| 119 | template<typename T> |
| 120 | void GrallocHal::initDispatch(T& func, gralloc1_function_descriptor_t desc) |
| 121 | { |
| 122 | auto pfn = mDevice->getFunction(mDevice, desc); |
| 123 | if (!pfn) { |
| 124 | LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc); |
| 125 | } |
| 126 | |
| 127 | func = reinterpret_cast<T>(pfn); |
| 128 | } |
| 129 | |
| 130 | void GrallocHal::initDispatch() |
| 131 | { |
| 132 | initDispatch(mDispatch.dump, GRALLOC1_FUNCTION_DUMP); |
| 133 | initDispatch(mDispatch.createDescriptor, |
| 134 | GRALLOC1_FUNCTION_CREATE_DESCRIPTOR); |
| 135 | initDispatch(mDispatch.destroyDescriptor, |
| 136 | GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR); |
| 137 | initDispatch(mDispatch.setDimensions, GRALLOC1_FUNCTION_SET_DIMENSIONS); |
| 138 | initDispatch(mDispatch.setFormat, GRALLOC1_FUNCTION_SET_FORMAT); |
Craig Donner | 0b00adf | 2016-10-20 17:12:58 -0700 | [diff] [blame] | 139 | if (hasCapability(Capability::LAYERED_BUFFERS)) { |
| 140 | initDispatch( |
| 141 | mDispatch.setLayerCount, GRALLOC1_FUNCTION_SET_LAYER_COUNT); |
| 142 | } |
Chia-I Wu | 109571a | 2016-09-05 11:46:36 +0800 | [diff] [blame] | 143 | initDispatch(mDispatch.setConsumerUsage, |
| 144 | GRALLOC1_FUNCTION_SET_CONSUMER_USAGE); |
| 145 | initDispatch(mDispatch.setProducerUsage, |
| 146 | GRALLOC1_FUNCTION_SET_PRODUCER_USAGE); |
| 147 | initDispatch(mDispatch.allocate, GRALLOC1_FUNCTION_ALLOCATE); |
| 148 | initDispatch(mDispatch.release, GRALLOC1_FUNCTION_RELEASE); |
| 149 | } |
| 150 | |
| 151 | bool GrallocHal::hasCapability(Capability capability) const |
| 152 | { |
| 153 | return (mCapabilities.count(capability) > 0); |
| 154 | } |
| 155 | |
| 156 | Return<void> GrallocHal::getCapabilities(getCapabilities_cb hidl_cb) |
| 157 | { |
| 158 | std::vector<Capability> caps( |
| 159 | mCapabilities.cbegin(), mCapabilities.cend()); |
| 160 | |
| 161 | hidl_vec<Capability> reply; |
| 162 | reply.setToExternal(caps.data(), caps.size()); |
| 163 | hidl_cb(reply); |
| 164 | |
| 165 | return Void(); |
| 166 | } |
| 167 | |
| 168 | Return<void> GrallocHal::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) |
| 169 | { |
| 170 | uint32_t len = 0; |
| 171 | mDispatch.dump(mDevice, &len, nullptr); |
| 172 | |
| 173 | std::vector<char> buf(len + 1); |
| 174 | mDispatch.dump(mDevice, &len, buf.data()); |
| 175 | buf.resize(len + 1); |
| 176 | buf[len] = '\0'; |
| 177 | |
| 178 | hidl_string reply; |
| 179 | reply.setToExternal(buf.data(), len); |
| 180 | hidl_cb(reply); |
| 181 | |
| 182 | return Void(); |
| 183 | } |
| 184 | |
| 185 | Return<void> GrallocHal::createDescriptor( |
| 186 | const BufferDescriptorInfo& descriptorInfo, |
| 187 | createDescriptor_cb hidl_cb) |
| 188 | { |
| 189 | BufferDescriptor descriptor; |
| 190 | int32_t err = mDispatch.createDescriptor(mDevice, &descriptor); |
| 191 | if (err == GRALLOC1_ERROR_NONE) { |
| 192 | err = mDispatch.setDimensions(mDevice, descriptor, |
| 193 | descriptorInfo.width, descriptorInfo.height); |
| 194 | } |
| 195 | if (err == GRALLOC1_ERROR_NONE) { |
| 196 | err = mDispatch.setFormat(mDevice, descriptor, |
| 197 | static_cast<int32_t>(descriptorInfo.format)); |
| 198 | } |
Craig Donner | 0b00adf | 2016-10-20 17:12:58 -0700 | [diff] [blame] | 199 | if (err == GRALLOC1_ERROR_NONE && |
| 200 | hasCapability(Capability::LAYERED_BUFFERS)) { |
| 201 | err = mDispatch.setLayerCount(mDevice, descriptor, |
| 202 | descriptorInfo.layerCount); |
| 203 | } |
Chia-I Wu | 109571a | 2016-09-05 11:46:36 +0800 | [diff] [blame] | 204 | if (err == GRALLOC1_ERROR_NONE) { |
| 205 | uint64_t producerUsageMask = descriptorInfo.producerUsageMask; |
| 206 | if (producerUsageMask & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) { |
| 207 | producerUsageMask |= GRALLOC1_PRODUCER_USAGE_CPU_READ; |
| 208 | } |
| 209 | if (producerUsageMask & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) { |
| 210 | producerUsageMask |= GRALLOC1_PRODUCER_USAGE_CPU_WRITE; |
| 211 | } |
| 212 | err = mDispatch.setProducerUsage(mDevice, descriptor, |
| 213 | descriptorInfo.producerUsageMask); |
| 214 | } |
| 215 | if (err == GRALLOC1_ERROR_NONE) { |
| 216 | uint64_t consumerUsageMask = descriptorInfo.consumerUsageMask; |
| 217 | if (consumerUsageMask & GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) { |
| 218 | consumerUsageMask |= GRALLOC1_CONSUMER_USAGE_CPU_READ; |
| 219 | } |
| 220 | err = mDispatch.setConsumerUsage(mDevice, descriptor, |
| 221 | consumerUsageMask); |
| 222 | } |
| 223 | |
| 224 | hidl_cb(static_cast<Error>(err), descriptor); |
| 225 | |
| 226 | return Void(); |
| 227 | } |
| 228 | |
| 229 | Return<Error> GrallocHal::destroyDescriptor( |
| 230 | BufferDescriptor descriptor) |
| 231 | { |
| 232 | int32_t err = mDispatch.destroyDescriptor(mDevice, descriptor); |
| 233 | return static_cast<Error>(err); |
| 234 | } |
| 235 | |
| 236 | Return<Error> GrallocHal::testAllocate( |
| 237 | const hidl_vec<BufferDescriptor>& descriptors) |
| 238 | { |
| 239 | if (!hasCapability(Capability::TEST_ALLOCATE)) { |
| 240 | return Error::UNDEFINED; |
| 241 | } |
| 242 | |
| 243 | int32_t err = mDispatch.allocate(mDevice, descriptors.size(), |
| 244 | &descriptors[0], nullptr); |
| 245 | return static_cast<Error>(err); |
| 246 | } |
| 247 | |
| 248 | Return<void> GrallocHal::allocate( |
| 249 | const hidl_vec<BufferDescriptor>& descriptors, |
| 250 | allocate_cb hidl_cb) { |
| 251 | std::vector<buffer_handle_t> buffers(descriptors.size()); |
| 252 | int32_t err = mDispatch.allocate(mDevice, descriptors.size(), |
| 253 | &descriptors[0], buffers.data()); |
| 254 | if (err != GRALLOC1_ERROR_NONE && err != GRALLOC1_ERROR_NOT_SHARED) { |
| 255 | buffers.clear(); |
| 256 | } |
| 257 | |
| 258 | hidl_vec<Buffer> reply; |
| 259 | reply.setToExternal( |
| 260 | reinterpret_cast<Buffer*>(buffers.data()), |
| 261 | buffers.size()); |
| 262 | hidl_cb(static_cast<Error>(err), reply); |
| 263 | |
| 264 | return Void(); |
| 265 | } |
| 266 | |
| 267 | Return<Error> GrallocHal::free(Buffer buffer) |
| 268 | { |
| 269 | buffer_handle_t handle = reinterpret_cast<buffer_handle_t>(buffer); |
| 270 | int32_t err = mDispatch.release(mDevice, handle); |
| 271 | return static_cast<Error>(err); |
| 272 | } |
| 273 | |
| 274 | Return<void> GrallocHal::exportHandle(BufferDescriptor /*descriptor*/, |
| 275 | Buffer buffer, exportHandle_cb hidl_cb) |
| 276 | { |
| 277 | // do we want to validate? |
| 278 | buffer_handle_t handle = reinterpret_cast<buffer_handle_t>(buffer); |
| 279 | |
| 280 | hidl_cb(Error::NONE, handle); |
| 281 | |
| 282 | return Void(); |
| 283 | } |
| 284 | |
| 285 | IAllocator* HIDL_FETCH_IAllocator(const char* /* name */) { |
| 286 | const hw_module_t* module; |
| 287 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| 288 | if (err) { |
| 289 | ALOGE("failed to get gralloc module"); |
| 290 | return nullptr; |
| 291 | } |
| 292 | |
| 293 | uint8_t major = (module->module_api_version >> 8) & 0xff; |
| 294 | if (major != 1) { |
| 295 | ALOGE("unknown gralloc module major version %d", major); |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
| 299 | return new GrallocHal(module); |
| 300 | } |
| 301 | |
| 302 | } // namespace implementation |
| 303 | } // namespace V2_0 |
| 304 | } // namespace allocator |
| 305 | } // namespace graphics |
| 306 | } // namespace hardware |
| 307 | } // namespace android |