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