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; |
| 77 | GRALLOC1_PFN_SET_CONSUMER_USAGE setConsumerUsage; |
| 78 | GRALLOC1_PFN_SET_PRODUCER_USAGE setProducerUsage; |
| 79 | GRALLOC1_PFN_ALLOCATE allocate; |
| 80 | GRALLOC1_PFN_RELEASE release; |
| 81 | GRALLOC1_PFN_GET_BACKING_STORE getBackingStore; |
| 82 | GRALLOC1_PFN_GET_STRIDE getStride; |
| 83 | GRALLOC1_PFN_GET_NUM_FLEX_PLANES getNumFlexPlanes; |
| 84 | } mDispatch; |
| 85 | }; |
| 86 | |
| 87 | GrallocHal::GrallocHal(const hw_module_t* module) |
| 88 | : mDevice(nullptr), mDispatch() |
| 89 | { |
| 90 | int status = gralloc1_open(module, &mDevice); |
| 91 | if (status) { |
| 92 | LOG_ALWAYS_FATAL("failed to open gralloc1 device: %s", |
| 93 | strerror(-status)); |
| 94 | } |
| 95 | |
| 96 | initCapabilities(); |
| 97 | initDispatch(); |
| 98 | } |
| 99 | |
| 100 | GrallocHal::~GrallocHal() |
| 101 | { |
| 102 | gralloc1_close(mDevice); |
| 103 | } |
| 104 | |
| 105 | void GrallocHal::initCapabilities() |
| 106 | { |
| 107 | uint32_t count; |
| 108 | mDevice->getCapabilities(mDevice, &count, nullptr); |
| 109 | |
| 110 | std::vector<Capability> caps(count); |
| 111 | mDevice->getCapabilities(mDevice, &count, reinterpret_cast< |
| 112 | std::underlying_type<Capability>::type*>(caps.data())); |
| 113 | caps.resize(count); |
| 114 | |
| 115 | mCapabilities.insert(caps.cbegin(), caps.cend()); |
| 116 | } |
| 117 | |
| 118 | template<typename T> |
| 119 | void GrallocHal::initDispatch(T& func, gralloc1_function_descriptor_t desc) |
| 120 | { |
| 121 | auto pfn = mDevice->getFunction(mDevice, desc); |
| 122 | if (!pfn) { |
| 123 | LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc); |
| 124 | } |
| 125 | |
| 126 | func = reinterpret_cast<T>(pfn); |
| 127 | } |
| 128 | |
| 129 | void GrallocHal::initDispatch() |
| 130 | { |
| 131 | initDispatch(mDispatch.dump, GRALLOC1_FUNCTION_DUMP); |
| 132 | initDispatch(mDispatch.createDescriptor, |
| 133 | GRALLOC1_FUNCTION_CREATE_DESCRIPTOR); |
| 134 | initDispatch(mDispatch.destroyDescriptor, |
| 135 | GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR); |
| 136 | initDispatch(mDispatch.setDimensions, GRALLOC1_FUNCTION_SET_DIMENSIONS); |
| 137 | initDispatch(mDispatch.setFormat, GRALLOC1_FUNCTION_SET_FORMAT); |
| 138 | initDispatch(mDispatch.setConsumerUsage, |
| 139 | GRALLOC1_FUNCTION_SET_CONSUMER_USAGE); |
| 140 | initDispatch(mDispatch.setProducerUsage, |
| 141 | GRALLOC1_FUNCTION_SET_PRODUCER_USAGE); |
| 142 | initDispatch(mDispatch.allocate, GRALLOC1_FUNCTION_ALLOCATE); |
| 143 | initDispatch(mDispatch.release, GRALLOC1_FUNCTION_RELEASE); |
| 144 | } |
| 145 | |
| 146 | bool GrallocHal::hasCapability(Capability capability) const |
| 147 | { |
| 148 | return (mCapabilities.count(capability) > 0); |
| 149 | } |
| 150 | |
| 151 | Return<void> GrallocHal::getCapabilities(getCapabilities_cb hidl_cb) |
| 152 | { |
| 153 | std::vector<Capability> caps( |
| 154 | mCapabilities.cbegin(), mCapabilities.cend()); |
| 155 | |
| 156 | hidl_vec<Capability> reply; |
| 157 | reply.setToExternal(caps.data(), caps.size()); |
| 158 | hidl_cb(reply); |
| 159 | |
| 160 | return Void(); |
| 161 | } |
| 162 | |
| 163 | Return<void> GrallocHal::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) |
| 164 | { |
| 165 | uint32_t len = 0; |
| 166 | mDispatch.dump(mDevice, &len, nullptr); |
| 167 | |
| 168 | std::vector<char> buf(len + 1); |
| 169 | mDispatch.dump(mDevice, &len, buf.data()); |
| 170 | buf.resize(len + 1); |
| 171 | buf[len] = '\0'; |
| 172 | |
| 173 | hidl_string reply; |
| 174 | reply.setToExternal(buf.data(), len); |
| 175 | hidl_cb(reply); |
| 176 | |
| 177 | return Void(); |
| 178 | } |
| 179 | |
| 180 | Return<void> GrallocHal::createDescriptor( |
| 181 | const BufferDescriptorInfo& descriptorInfo, |
| 182 | createDescriptor_cb hidl_cb) |
| 183 | { |
| 184 | BufferDescriptor descriptor; |
| 185 | int32_t err = mDispatch.createDescriptor(mDevice, &descriptor); |
| 186 | if (err == GRALLOC1_ERROR_NONE) { |
| 187 | err = mDispatch.setDimensions(mDevice, descriptor, |
| 188 | descriptorInfo.width, descriptorInfo.height); |
| 189 | } |
| 190 | if (err == GRALLOC1_ERROR_NONE) { |
| 191 | err = mDispatch.setFormat(mDevice, descriptor, |
| 192 | static_cast<int32_t>(descriptorInfo.format)); |
| 193 | } |
| 194 | if (err == GRALLOC1_ERROR_NONE) { |
| 195 | uint64_t producerUsageMask = descriptorInfo.producerUsageMask; |
| 196 | if (producerUsageMask & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) { |
| 197 | producerUsageMask |= GRALLOC1_PRODUCER_USAGE_CPU_READ; |
| 198 | } |
| 199 | if (producerUsageMask & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) { |
| 200 | producerUsageMask |= GRALLOC1_PRODUCER_USAGE_CPU_WRITE; |
| 201 | } |
| 202 | err = mDispatch.setProducerUsage(mDevice, descriptor, |
| 203 | descriptorInfo.producerUsageMask); |
| 204 | } |
| 205 | if (err == GRALLOC1_ERROR_NONE) { |
| 206 | uint64_t consumerUsageMask = descriptorInfo.consumerUsageMask; |
| 207 | if (consumerUsageMask & GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) { |
| 208 | consumerUsageMask |= GRALLOC1_CONSUMER_USAGE_CPU_READ; |
| 209 | } |
| 210 | err = mDispatch.setConsumerUsage(mDevice, descriptor, |
| 211 | consumerUsageMask); |
| 212 | } |
| 213 | |
| 214 | hidl_cb(static_cast<Error>(err), descriptor); |
| 215 | |
| 216 | return Void(); |
| 217 | } |
| 218 | |
| 219 | Return<Error> GrallocHal::destroyDescriptor( |
| 220 | BufferDescriptor descriptor) |
| 221 | { |
| 222 | int32_t err = mDispatch.destroyDescriptor(mDevice, descriptor); |
| 223 | return static_cast<Error>(err); |
| 224 | } |
| 225 | |
| 226 | Return<Error> GrallocHal::testAllocate( |
| 227 | const hidl_vec<BufferDescriptor>& descriptors) |
| 228 | { |
| 229 | if (!hasCapability(Capability::TEST_ALLOCATE)) { |
| 230 | return Error::UNDEFINED; |
| 231 | } |
| 232 | |
| 233 | int32_t err = mDispatch.allocate(mDevice, descriptors.size(), |
| 234 | &descriptors[0], nullptr); |
| 235 | return static_cast<Error>(err); |
| 236 | } |
| 237 | |
| 238 | Return<void> GrallocHal::allocate( |
| 239 | const hidl_vec<BufferDescriptor>& descriptors, |
| 240 | allocate_cb hidl_cb) { |
| 241 | std::vector<buffer_handle_t> buffers(descriptors.size()); |
| 242 | int32_t err = mDispatch.allocate(mDevice, descriptors.size(), |
| 243 | &descriptors[0], buffers.data()); |
| 244 | if (err != GRALLOC1_ERROR_NONE && err != GRALLOC1_ERROR_NOT_SHARED) { |
| 245 | buffers.clear(); |
| 246 | } |
| 247 | |
| 248 | hidl_vec<Buffer> reply; |
| 249 | reply.setToExternal( |
| 250 | reinterpret_cast<Buffer*>(buffers.data()), |
| 251 | buffers.size()); |
| 252 | hidl_cb(static_cast<Error>(err), reply); |
| 253 | |
| 254 | return Void(); |
| 255 | } |
| 256 | |
| 257 | Return<Error> GrallocHal::free(Buffer buffer) |
| 258 | { |
| 259 | buffer_handle_t handle = reinterpret_cast<buffer_handle_t>(buffer); |
| 260 | int32_t err = mDispatch.release(mDevice, handle); |
| 261 | return static_cast<Error>(err); |
| 262 | } |
| 263 | |
| 264 | Return<void> GrallocHal::exportHandle(BufferDescriptor /*descriptor*/, |
| 265 | Buffer buffer, exportHandle_cb hidl_cb) |
| 266 | { |
| 267 | // do we want to validate? |
| 268 | buffer_handle_t handle = reinterpret_cast<buffer_handle_t>(buffer); |
| 269 | |
| 270 | hidl_cb(Error::NONE, handle); |
| 271 | |
| 272 | return Void(); |
| 273 | } |
| 274 | |
| 275 | IAllocator* HIDL_FETCH_IAllocator(const char* /* name */) { |
| 276 | const hw_module_t* module; |
| 277 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| 278 | if (err) { |
| 279 | ALOGE("failed to get gralloc module"); |
| 280 | return nullptr; |
| 281 | } |
| 282 | |
| 283 | uint8_t major = (module->module_api_version >> 8) & 0xff; |
| 284 | if (major != 1) { |
| 285 | ALOGE("unknown gralloc module major version %d", major); |
| 286 | return nullptr; |
| 287 | } |
| 288 | |
| 289 | return new GrallocHal(module); |
| 290 | } |
| 291 | |
| 292 | } // namespace implementation |
| 293 | } // namespace V2_0 |
| 294 | } // namespace allocator |
| 295 | } // namespace graphics |
| 296 | } // namespace hardware |
| 297 | } // namespace android |