blob: 5c5d5b382fbcd257bc9918616878d1a749824c62 [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
Chia-I Wu9ba189d2016-09-22 17:13:08 +080019#include <ui/GrallocAllocator.h>
20
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080021#include <log/log.h>
22
Chia-I Wu9ba189d2016-09-22 17:13:08 +080023namespace android {
24
25namespace Gralloc2 {
26
27// assume NO_RESOURCES when Status::isOk returns false
28constexpr Error kDefaultError = Error::NO_RESOURCES;
29
30Allocator::Allocator()
31{
Chia-I Wub018bf02016-11-22 13:29:49 +080032 mAllocator = IAllocator::getService("gralloc");
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 Wu9ba189d2016-09-22 17:13:08 +080044}
45
46std::string Allocator::dumpDebugInfo() const
47{
48 std::string info;
49
Chia-I Wub018bf02016-11-22 13:29:49 +080050 mAllocator->dumpDebugInfo([&](const auto& tmpInfo) {
Chia-I Wu9ba189d2016-09-22 17:13:08 +080051 info = tmpInfo.c_str();
52 });
53
54 return info;
55}
56
57Error Allocator::createBufferDescriptor(
Chia-I Wub018bf02016-11-22 13:29:49 +080058 const IAllocatorClient::BufferDescriptorInfo& descriptorInfo,
Chia-I Wu67e376d2016-12-19 11:36:22 +080059 BufferDescriptor* outDescriptor) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +080060{
61 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +080062 mClient->createDescriptor(descriptorInfo,
Chia-I Wu9ba189d2016-09-22 17:13:08 +080063 [&](const auto& tmpError, const auto& tmpDescriptor) {
64 error = tmpError;
65 if (error != Error::NONE) {
66 return;
67 }
68
Chia-I Wu67e376d2016-12-19 11:36:22 +080069 *outDescriptor = tmpDescriptor;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080070 });
71
72 return error;
73}
74
75void Allocator::destroyBufferDescriptor(BufferDescriptor descriptor) const
76{
Chia-I Wub018bf02016-11-22 13:29:49 +080077 mClient->destroyDescriptor(descriptor);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080078}
79
Chia-I Wu67e376d2016-12-19 11:36:22 +080080Error Allocator::allocate(BufferDescriptor descriptor,
81 Buffer* outBuffer) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +080082{
83 hardware::hidl_vec<BufferDescriptor> descriptors;
84 descriptors.setToExternal(&descriptor, 1);
85
86 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +080087 auto status = mClient->allocate(descriptors,
Chia-I Wu9ba189d2016-09-22 17:13:08 +080088 [&](const auto& tmpError, const auto& tmpBuffers) {
89 error = tmpError;
90 if (tmpError != Error::NONE) {
91 return;
92 }
93
Chia-I Wu67e376d2016-12-19 11:36:22 +080094 *outBuffer = tmpBuffers[0];
Chia-I Wu9ba189d2016-09-22 17:13:08 +080095 });
96
97 return error;
98}
99
100void Allocator::free(Buffer buffer) const
101{
Chia-I Wub018bf02016-11-22 13:29:49 +0800102 mClient->free(buffer);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800103}
104
105Error Allocator::exportHandle(BufferDescriptor descriptor, Buffer buffer,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800106 native_handle_t** outBufferHandle) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800107{
108 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +0800109 auto status = mClient->exportHandle(descriptor, buffer,
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800110 [&](const auto& tmpError, const auto& tmpBufferHandle) {
111 error = tmpError;
112 if (tmpError != Error::NONE) {
113 return;
114 }
115
Chia-I Wu67e376d2016-12-19 11:36:22 +0800116 *outBufferHandle = native_handle_clone(tmpBufferHandle);
117 if (!*outBufferHandle) {
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800118 error = Error::NO_RESOURCES;
119 }
120 });
121
122 return error;
123}
124
125} // namespace Gralloc2
126
127} // namespace android