| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +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 "GrallocAllocator" | 
|  | 18 |  | 
|  | 19 | #include <log/log.h> | 
|  | 20 | #include <ui/GrallocAllocator.h> | 
|  | 21 |  | 
|  | 22 | namespace android { | 
|  | 23 |  | 
|  | 24 | namespace Gralloc2 { | 
|  | 25 |  | 
|  | 26 | // assume NO_RESOURCES when Status::isOk returns false | 
|  | 27 | constexpr Error kDefaultError = Error::NO_RESOURCES; | 
|  | 28 |  | 
|  | 29 | Allocator::Allocator() | 
|  | 30 | { | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 31 | mAllocator = IAllocator::getService("gralloc"); | 
|  | 32 | if (mAllocator != nullptr) { | 
|  | 33 | mAllocator->createClient( | 
|  | 34 | [&](const auto& tmpError, const auto& tmpClient) { | 
|  | 35 | if (tmpError == Error::NONE) { | 
|  | 36 | mClient = tmpClient; | 
|  | 37 | } | 
|  | 38 | }); | 
|  | 39 | if (mClient == nullptr) { | 
|  | 40 | mAllocator.clear(); | 
|  | 41 | } | 
|  | 42 | } | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | std::string Allocator::dumpDebugInfo() const | 
|  | 46 | { | 
|  | 47 | std::string info; | 
|  | 48 |  | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 49 | mAllocator->dumpDebugInfo([&](const auto& tmpInfo) { | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 50 | info = tmpInfo.c_str(); | 
|  | 51 | }); | 
|  | 52 |  | 
|  | 53 | return info; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | Error Allocator::createBufferDescriptor( | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 57 | const IAllocatorClient::BufferDescriptorInfo& descriptorInfo, | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 58 | BufferDescriptor* outDescriptor) const | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 59 | { | 
|  | 60 | Error error = kDefaultError; | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 61 | mClient->createDescriptor(descriptorInfo, | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 62 | [&](const auto& tmpError, const auto& tmpDescriptor) { | 
|  | 63 | error = tmpError; | 
|  | 64 | if (error != Error::NONE) { | 
|  | 65 | return; | 
|  | 66 | } | 
|  | 67 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 68 | *outDescriptor = tmpDescriptor; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 69 | }); | 
|  | 70 |  | 
|  | 71 | return error; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | void Allocator::destroyBufferDescriptor(BufferDescriptor descriptor) const | 
|  | 75 | { | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 76 | mClient->destroyDescriptor(descriptor); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 79 | Error Allocator::allocate(BufferDescriptor descriptor, | 
|  | 80 | Buffer* outBuffer) const | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 81 | { | 
|  | 82 | hardware::hidl_vec<BufferDescriptor> descriptors; | 
|  | 83 | descriptors.setToExternal(&descriptor, 1); | 
|  | 84 |  | 
|  | 85 | Error error = kDefaultError; | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 86 | auto status = mClient->allocate(descriptors, | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 87 | [&](const auto& tmpError, const auto& tmpBuffers) { | 
|  | 88 | error = tmpError; | 
|  | 89 | if (tmpError != Error::NONE) { | 
|  | 90 | return; | 
|  | 91 | } | 
|  | 92 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 93 | *outBuffer = tmpBuffers[0]; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 94 | }); | 
|  | 95 |  | 
|  | 96 | return error; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | void Allocator::free(Buffer buffer) const | 
|  | 100 | { | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 101 | mClient->free(buffer); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | Error Allocator::exportHandle(BufferDescriptor descriptor, Buffer buffer, | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 105 | native_handle_t** outBufferHandle) const | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 106 | { | 
|  | 107 | Error error = kDefaultError; | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 108 | auto status = mClient->exportHandle(descriptor, buffer, | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 109 | [&](const auto& tmpError, const auto& tmpBufferHandle) { | 
|  | 110 | error = tmpError; | 
|  | 111 | if (tmpError != Error::NONE) { | 
|  | 112 | return; | 
|  | 113 | } | 
|  | 114 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 115 | *outBufferHandle = native_handle_clone(tmpBufferHandle); | 
|  | 116 | if (!*outBufferHandle) { | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 117 | error = Error::NO_RESOURCES; | 
|  | 118 | } | 
|  | 119 | }); | 
|  | 120 |  | 
|  | 121 | return error; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | } // namespace Gralloc2 | 
|  | 125 |  | 
|  | 126 | } // namespace android |