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 | { |
| 31 | mService = IAllocator::getService("gralloc"); |
| 32 | } |
| 33 | |
| 34 | std::string Allocator::dumpDebugInfo() const |
| 35 | { |
| 36 | std::string info; |
| 37 | |
| 38 | mService->dumpDebugInfo([&](const auto& tmpInfo) { |
| 39 | info = tmpInfo.c_str(); |
| 40 | }); |
| 41 | |
| 42 | return info; |
| 43 | } |
| 44 | |
| 45 | Error Allocator::createBufferDescriptor( |
| 46 | const IAllocator::BufferDescriptorInfo& descriptorInfo, |
| 47 | BufferDescriptor& descriptor) const |
| 48 | { |
| 49 | Error error = kDefaultError; |
| 50 | mService->createDescriptor(descriptorInfo, |
| 51 | [&](const auto& tmpError, const auto& tmpDescriptor) { |
| 52 | error = tmpError; |
| 53 | if (error != Error::NONE) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | descriptor = tmpDescriptor; |
| 58 | }); |
| 59 | |
| 60 | return error; |
| 61 | } |
| 62 | |
| 63 | void Allocator::destroyBufferDescriptor(BufferDescriptor descriptor) const |
| 64 | { |
| 65 | mService->destroyDescriptor(descriptor); |
| 66 | } |
| 67 | |
| 68 | Error Allocator::allocate(BufferDescriptor descriptor, Buffer& buffer) const |
| 69 | { |
| 70 | hardware::hidl_vec<BufferDescriptor> descriptors; |
| 71 | descriptors.setToExternal(&descriptor, 1); |
| 72 | |
| 73 | Error error = kDefaultError; |
| 74 | auto status = mService->allocate(descriptors, |
| 75 | [&](const auto& tmpError, const auto& tmpBuffers) { |
| 76 | error = tmpError; |
| 77 | if (tmpError != Error::NONE) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | buffer = tmpBuffers[0]; |
| 82 | }); |
| 83 | |
| 84 | return error; |
| 85 | } |
| 86 | |
| 87 | void Allocator::free(Buffer buffer) const |
| 88 | { |
| 89 | mService->free(buffer); |
| 90 | } |
| 91 | |
| 92 | Error Allocator::exportHandle(BufferDescriptor descriptor, Buffer buffer, |
| 93 | native_handle_t*& bufferHandle) const |
| 94 | { |
| 95 | Error error = kDefaultError; |
| 96 | auto status = mService->exportHandle(descriptor, buffer, |
| 97 | [&](const auto& tmpError, const auto& tmpBufferHandle) { |
| 98 | error = tmpError; |
| 99 | if (tmpError != Error::NONE) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | bufferHandle = native_handle_clone(tmpBufferHandle); |
| 104 | if (!bufferHandle) { |
| 105 | error = Error::NO_RESOURCES; |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | return error; |
| 110 | } |
| 111 | |
| 112 | } // namespace Gralloc2 |
| 113 | |
| 114 | } // namespace android |