blob: 021122a03b0f505ba5e88218d09d93b10ff29ed6 [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{
Chia-I Wub018bf02016-11-22 13:29:49 +080031 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 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 Wu9ba189d2016-09-22 17:13:08 +080058 BufferDescriptor& descriptor) const
59{
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
68 descriptor = tmpDescriptor;
69 });
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
79Error Allocator::allocate(BufferDescriptor descriptor, Buffer& buffer) const
80{
81 hardware::hidl_vec<BufferDescriptor> descriptors;
82 descriptors.setToExternal(&descriptor, 1);
83
84 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +080085 auto status = mClient->allocate(descriptors,
Chia-I Wu9ba189d2016-09-22 17:13:08 +080086 [&](const auto& tmpError, const auto& tmpBuffers) {
87 error = tmpError;
88 if (tmpError != Error::NONE) {
89 return;
90 }
91
92 buffer = tmpBuffers[0];
93 });
94
95 return error;
96}
97
98void Allocator::free(Buffer buffer) const
99{
Chia-I Wub018bf02016-11-22 13:29:49 +0800100 mClient->free(buffer);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800101}
102
103Error Allocator::exportHandle(BufferDescriptor descriptor, Buffer buffer,
104 native_handle_t*& bufferHandle) const
105{
106 Error error = kDefaultError;
Chia-I Wub018bf02016-11-22 13:29:49 +0800107 auto status = mClient->exportHandle(descriptor, buffer,
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800108 [&](const auto& tmpError, const auto& tmpBufferHandle) {
109 error = tmpError;
110 if (tmpError != Error::NONE) {
111 return;
112 }
113
114 bufferHandle = native_handle_clone(tmpBufferHandle);
115 if (!bufferHandle) {
116 error = Error::NO_RESOURCES;
117 }
118 });
119
120 return error;
121}
122
123} // namespace Gralloc2
124
125} // namespace android