blob: 8799fab59a2db663e46bf8cedd76e1874ee4de5e [file] [log] [blame]
Chia-I Wu9ba189d2016-09-22 17:13:08 +08001/*
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
22namespace android {
23
24namespace Gralloc2 {
25
26// assume NO_RESOURCES when Status::isOk returns false
27constexpr Error kDefaultError = Error::NO_RESOURCES;
28
29Allocator::Allocator()
30{
Chris Phoenix790c3502017-01-25 13:17:26 -080031 mAllocator = IAllocator::getService();
Chia-I Wub018bf02016-11-22 13:29:49 +080032 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 Wu9ba189d2016-09-22 17:13:08 +080043}
44
45std::string Allocator::dumpDebugInfo() const
46{
47 std::string info;
48
Chia-I Wub018bf02016-11-22 13:29:49 +080049 mAllocator->dumpDebugInfo([&](const auto& tmpInfo) {
Chia-I Wu9ba189d2016-09-22 17:13:08 +080050 info = tmpInfo.c_str();
51 });
52
53 return info;
54}
55
56Error Allocator::createBufferDescriptor(
Chia-I Wub018bf02016-11-22 13:29:49 +080057 const IAllocatorClient::BufferDescriptorInfo& descriptorInfo,
Chia-I Wu67e376d2016-12-19 11:36:22 +080058 BufferDescriptor* outDescriptor) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +080059{
60 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +080061 mClient->createDescriptor(descriptorInfo,
Chia-I Wu9ba189d2016-09-22 17:13:08 +080062 [&](const auto& tmpError, const auto& tmpDescriptor) {
63 error = tmpError;
64 if (error != Error::NONE) {
65 return;
66 }
67
Chia-I Wu67e376d2016-12-19 11:36:22 +080068 *outDescriptor = tmpDescriptor;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080069 });
70
71 return error;
72}
73
74void Allocator::destroyBufferDescriptor(BufferDescriptor descriptor) const
75{
Chia-I Wub018bf02016-11-22 13:29:49 +080076 mClient->destroyDescriptor(descriptor);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080077}
78
Chia-I Wu67e376d2016-12-19 11:36:22 +080079Error Allocator::allocate(BufferDescriptor descriptor,
80 Buffer* outBuffer) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +080081{
82 hardware::hidl_vec<BufferDescriptor> descriptors;
83 descriptors.setToExternal(&descriptor, 1);
84
85 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +080086 auto status = mClient->allocate(descriptors,
Chia-I Wu9ba189d2016-09-22 17:13:08 +080087 [&](const auto& tmpError, const auto& tmpBuffers) {
88 error = tmpError;
89 if (tmpError != Error::NONE) {
90 return;
91 }
92
Chia-I Wu67e376d2016-12-19 11:36:22 +080093 *outBuffer = tmpBuffers[0];
Chia-I Wu9ba189d2016-09-22 17:13:08 +080094 });
95
96 return error;
97}
98
99void Allocator::free(Buffer buffer) const
100{
Chia-I Wub018bf02016-11-22 13:29:49 +0800101 mClient->free(buffer);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800102}
103
104Error Allocator::exportHandle(BufferDescriptor descriptor, Buffer buffer,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800105 native_handle_t** outBufferHandle) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800106{
107 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +0800108 auto status = mClient->exportHandle(descriptor, buffer,
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800109 [&](const auto& tmpError, const auto& tmpBufferHandle) {
110 error = tmpError;
111 if (tmpError != Error::NONE) {
112 return;
113 }
114
Chia-I Wu67e376d2016-12-19 11:36:22 +0800115 *outBufferHandle = native_handle_clone(tmpBufferHandle);
116 if (!*outBufferHandle) {
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800117 error = Error::NO_RESOURCES;
118 }
119 });
120
121 return error;
122}
123
124} // namespace Gralloc2
125
126} // namespace android