blob: 7ee01ad92148a1120190b9fd840fc09dd52a1fbd [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 "GrallocMapper"
18
19#include <array>
20#include <string>
21
22#include <log/log.h>
23#include <ui/GrallocMapper.h>
24
25namespace android {
26
27namespace Gralloc2 {
28
Chia-I Wu31669472016-12-07 14:55:24 +080029static constexpr Error kDefaultError = Error::NO_RESOURCES;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080030
31Mapper::Mapper()
Chia-I Wu9ba189d2016-09-22 17:13:08 +080032{
Chia-I Wu31669472016-12-07 14:55:24 +080033 mMapper = IMapper::getService("gralloc-mapper");
34 if (mMapper != nullptr && mMapper->isRemote()) {
35 LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
Chia-I Wu9ba189d2016-09-22 17:13:08 +080036 }
37}
38
Chia-I Wu31669472016-12-07 14:55:24 +080039Error Mapper::retain(buffer_handle_t handle) const
Chia-I Wu9ba189d2016-09-22 17:13:08 +080040{
Chia-I Wu31669472016-12-07 14:55:24 +080041 auto ret = mMapper->retain(handle);
42 return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080043}
44
45void Mapper::release(buffer_handle_t handle) const
46{
Chia-I Wu31669472016-12-07 14:55:24 +080047 auto ret = mMapper->release(handle);
48
49 auto error = (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080050 ALOGE_IF(error != Error::NONE,
51 "release(%p) failed with %d", handle, error);
52}
53
Chia-I Wu31669472016-12-07 14:55:24 +080054Error Mapper::getStride(buffer_handle_t handle, uint32_t* outStride) const
55{
56 Error error = kDefaultError;
57 mMapper->getStride(handle,
58 [&](const auto& tmpError, const auto& tmpStride)
59 {
60 error = tmpError;
61 if (error != Error::NONE) {
62 return;
63 }
64
65 *outStride = tmpStride;
66 });
67
68 return error;
69}
70
71Error Mapper::lock(buffer_handle_t handle,
72 uint64_t producerUsageMask,
73 uint64_t consumerUsageMask,
74 const IMapper::Rect& accessRegion,
75 int acquireFence, void** outData) const
76{
77 hardware::hidl_handle acquireFenceHandle;
78
79 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
80 if (acquireFence >= 0) {
81 auto h = native_handle_init(acquireFenceStorage, 1, 0);
82 h->data[0] = acquireFence;
83 acquireFenceHandle = h;
84 }
85
86 Error error = kDefaultError;
87 mMapper->lock(handle, producerUsageMask, consumerUsageMask,
88 accessRegion, acquireFenceHandle,
89 [&](const auto& tmpError, const auto& tmpData)
90 {
91 error = tmpError;
92 if (error != Error::NONE) {
93 return;
94 }
95
96 *outData = tmpData;
97 });
98
99 if (error == Error::NONE && acquireFence >= 0) {
100 close(acquireFence);
101 }
102
103 return error;
104}
105
106Error Mapper::lock(buffer_handle_t handle,
107 uint64_t producerUsageMask,
108 uint64_t consumerUsageMask,
109 const IMapper::Rect& accessRegion,
110 int acquireFence, FlexLayout* outLayout) const
111{
112 hardware::hidl_handle acquireFenceHandle;
113
114 NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0);
115 if (acquireFence >= 0) {
116 auto h = native_handle_init(acquireFenceStorage, 1, 0);
117 h->data[0] = acquireFence;
118 acquireFenceHandle = h;
119 }
120
121 Error error = kDefaultError;
122 mMapper->lockFlex(handle, producerUsageMask, consumerUsageMask,
123 accessRegion, acquireFenceHandle,
124 [&](const auto& tmpError, const auto& tmpLayout)
125 {
126 error = tmpError;
127 if (error != Error::NONE) {
128 return;
129 }
130
131 *outLayout = tmpLayout;
132 });
133
134 if (error == Error::NONE && acquireFence >= 0) {
135 close(acquireFence);
136 }
137
138 return error;
139}
140
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800141int Mapper::unlock(buffer_handle_t handle) const
142{
Fabien Sanglard521698f2017-01-10 13:45:45 -0800143 int releaseFence = -1;
Chia-I Wu31669472016-12-07 14:55:24 +0800144
145 Error error = kDefaultError;
146 mMapper->unlock(handle,
147 [&](const auto& tmpError, const auto& tmpReleaseFence)
148 {
149 error = tmpError;
150 if (error != Error::NONE) {
151 return;
152 }
153
154 auto fenceHandle = tmpReleaseFence.getNativeHandle();
155 if (fenceHandle && fenceHandle->numFds == 1) {
156 int fd = dup(fenceHandle->data[0]);
157 if (fd >= 0) {
158 releaseFence = fd;
159 } else {
160 error = Error::NO_RESOURCES;
161 }
162 } else {
163 releaseFence = -1;
164 }
165 });
166
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800167 if (error != Error::NONE) {
168 ALOGE("unlock(%p) failed with %d", handle, error);
169 releaseFence = -1;
170 }
171
172 return releaseFence;
173}
174
175} // namespace Gralloc2
176
177} // namespace android