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 "Gralloc2"
18
19 #include <ui/Gralloc2.h>
20
21 #include <cutils/native_handle.h>
22 #include <inttypes.h>
23 #include <log/log.h>
24 #include <system/graphics.h>
25 #pragma clang diagnostic push
26 #pragma clang diagnostic ignored "-Wzero-length-array"
27 #include <sync/sync.h>
28 #pragma clang diagnostic pop
29
30 #include <grallocusage/GrallocUsageConversion.h>
31 #include <cstring>
32 #include "GrallocDispatch.h"
33
34 namespace android {
35 namespace Gralloc2 {
36 namespace {
37 static constexpr Error kTransactionError = Error::NO_RESOURCES;
38
39 // uint64_t getValid10UsageBits() {
40 // static const uint64_t valid10UsageBits = []() -> uint64_t {
41 // using hardware::graphics::common::V1_0::BufferUsage;
42 // uint64_t bits = 0;
43 // for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) {
44 // bits = bits | bit;
45 // }
46 // // TODO(b/72323293, b/72703005): Remove these additional bits
47 // bits = bits | (1 << 10) | (1 << 13);
48 // return bits;
49 // }();
50 // return valid10UsageBits;
51 // }
52
53 // uint64_t getValid11UsageBits() {
54 // static const uint64_t valid11UsageBits = []() -> uint64_t {
55 // using hardware::graphics::common::V1_1::BufferUsage;
56 // uint64_t bits = 0;
57 // for (const auto bit : hardware::hidl_enum_range<BufferUsage>()) {
58 // bits = bits | bit;
59 // }
60 // return bits;
61 // }();
62 // return valid11UsageBits;
63 // }
64
65 } // anonymous namespace
66
preload()67 void Mapper::preload() {
68 }
69
Mapper()70 Mapper::Mapper()
71 {
72 // mMapper = hardware::graphics::mapper::V2_0::IMapper::getService();
73 // if (mMapper == nullptr) {
74 // LOG_ALWAYS_FATAL("gralloc-mapper is missing");
75 // }
76 // if (mMapper->isRemote()) {
77 // LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode");
78 // }
79 // // IMapper 2.1 is optional
80 // mMapperV2_1 = IMapper::castFrom(mMapper);
81 }
82
validateBufferDescriptorInfo(const IMapper::BufferDescriptorInfo & descriptorInfo) const83 Gralloc2::Error Mapper::validateBufferDescriptorInfo(
84 const IMapper::BufferDescriptorInfo& descriptorInfo) const {
85 return Error::NONE;
86
87 // uint64_t validUsageBits = getValid10UsageBits();
88 // if (mMapperV2_1 != nullptr) {
89 // validUsageBits = validUsageBits | getValid11UsageBits();
90 // }
91 // if (descriptorInfo.usage & ~validUsageBits) {
92 // ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64,
93 // descriptorInfo.usage & ~validUsageBits);
94 // return Error::BAD_VALUE;
95 // }
96 // return Error::NONE;
97 }
createDescriptor(const IMapper::BufferDescriptorInfo & descriptorInfo,BufferDescriptor * outDescriptor) const98 Error Mapper::createDescriptor(
99 const IMapper::BufferDescriptorInfo& descriptorInfo,
100 BufferDescriptor* outDescriptor) const
101 {
102 Error error = validateBufferDescriptorInfo(descriptorInfo);
103 if (error != Error::NONE) {
104 return error;
105 }
106 *outDescriptor = new IMapper::BufferDescriptorInfo;
107 memcpy(*outDescriptor, &descriptorInfo, sizeof(descriptorInfo));
108 return error;
109 }
110
importBuffer(const hardware::hidl_handle & rawHandle,buffer_handle_t * outBufferHandle) const111 Error Mapper::importBuffer(const hardware::hidl_handle& rawHandle,
112 buffer_handle_t* outBufferHandle) const
113 {
114 if (!rawHandle) return kTransactionError;
115 *outBufferHandle = rawHandle;
116 return Error::NONE;
117 }
118
freeBuffer(buffer_handle_t bufferHandle) const119 void Mapper::freeBuffer(buffer_handle_t bufferHandle) const
120 {
121 auto gralloc = get_global_gralloc_module();
122 if (!gralloc) {
123 fprintf(stderr, "Mapper::%s: no gralloc module loaded!\n", __func__);
124 return;
125 }
126
127 gralloc->free(bufferHandle);
128 }
129
validateBufferSize(buffer_handle_t bufferHandle,const IMapper::BufferDescriptorInfo & descriptorInfo,uint32_t stride) const130 Error Mapper::validateBufferSize(buffer_handle_t bufferHandle,
131 const IMapper::BufferDescriptorInfo& descriptorInfo,
132 uint32_t stride) const
133 {
134 return Error::NONE;
135 }
136
getTransportSize(buffer_handle_t bufferHandle,uint32_t * outNumFds,uint32_t * outNumInts) const137 void Mapper::getTransportSize(buffer_handle_t bufferHandle,
138 uint32_t* outNumFds, uint32_t* outNumInts) const
139 {
140 *outNumFds = 1;
141 *outNumInts = 1;
142 }
143
lock(buffer_handle_t bufferHandle,uint64_t usage,const IMapper::Rect & accessRegion,int acquireFence,void ** outData) const144 Error Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage,
145 const IMapper::Rect& accessRegion,
146 int acquireFence, void** outData) const
147 {
148 (void)acquireFence;
149
150 auto gralloc = get_global_gralloc_module();
151 if (!gralloc) {
152 fprintf(stderr, "Mapper::%s: no gralloc module loaded!\n", __func__);
153 return kTransactionError;
154 }
155
156 int32_t usage0 = android_convertGralloc1To0Usage(0, usage);
157
158 int res = gralloc->lock(
159 bufferHandle, usage0,
160 accessRegion.left, accessRegion.top,
161 accessRegion.width, accessRegion.height,
162 outData);
163
164 if (res) {
165 fprintf(stderr, "Mapper::%s: got error from gralloc lock: %d\n", __func__, res);
166 return Error::BAD_VALUE;
167 }
168
169 return Error::NONE;
170 }
171
lock(buffer_handle_t bufferHandle,uint64_t usage,const IMapper::Rect & accessRegion,int acquireFence,YCbCrLayout * outLayout) const172 Error Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage,
173 const IMapper::Rect& accessRegion,
174 int acquireFence, YCbCrLayout* outLayout) const {
175
176 (void)acquireFence;
177
178 auto gralloc = get_global_gralloc_module();
179 if (!gralloc) {
180 fprintf(stderr, "Mapper::%s: no gralloc module loaded!\n", __func__);
181 return kTransactionError;
182 }
183
184 int32_t usage0 = android_convertGralloc1To0Usage(0, usage);
185 android_ycbcr aycbcr;
186 int res = gralloc->lock_ycbcr(
187 bufferHandle, usage0,
188 accessRegion.left, accessRegion.top,
189 accessRegion.width, accessRegion.height,
190 &aycbcr);
191
192 if (res) {
193 fprintf(stderr, "Mapper::%s: got error from gralloc lock_ycbcr: %d\n", __func__, res);
194 return Error::BAD_VALUE;
195 }
196
197 outLayout->y = aycbcr.y;
198 outLayout->cb = aycbcr.cb;
199 outLayout->cr = aycbcr.cr;
200 outLayout->yStride = aycbcr.ystride;
201 outLayout->cStride = aycbcr.cstride;
202 outLayout->chromaStep = aycbcr.chroma_step;
203
204 return Error::NONE;
205 }
206
unlock(buffer_handle_t bufferHandle) const207 int Mapper::unlock(buffer_handle_t bufferHandle) const
208 {
209 auto gralloc = get_global_gralloc_module();
210
211 if (!gralloc) {
212 fprintf(stderr, "Mapper::%s: no gralloc module loaded!\n", __func__);
213 return kTransactionError;
214 }
215
216 int releaseFence = -1;
217
218 int res = gralloc->unlock(bufferHandle);
219
220 if (res) {
221 fprintf(stderr, "Mapper::%s: got error from gralloc unlock: %d\n", __func__, res);
222 return releaseFence;
223 }
224
225 return releaseFence;
226 }
227
Allocator(const Mapper & mapper)228 Allocator::Allocator(const Mapper& mapper)
229 : mMapper(mapper)
230 { }
231
dumpDebugInfo() const232 std::string Allocator::dumpDebugInfo() const
233 {
234 return "PlaceholderDebugInfo";
235 }
236
allocate(BufferDescriptor descriptor,uint32_t count,uint32_t * outStride,buffer_handle_t * outBufferHandles) const237 Error Allocator::allocate(BufferDescriptor descriptor, uint32_t count,
238 uint32_t* outStride, buffer_handle_t* outBufferHandles) const
239 {
240
241 auto gralloc = get_global_gralloc_module();
242
243 if (!gralloc) {
244 fprintf(stderr, "Mapper::%s: no gralloc module loaded!\n", __func__);
245 return kTransactionError;
246 }
247
248 const auto& info = *(IMapper::BufferDescriptorInfo*)(descriptor);
249
250 for (uint32_t i = 0; i < count; ++i) {
251 gralloc->alloc(
252 info.width, info.height,
253 info.format, info.usage,
254 outBufferHandles + i, (int32_t*)outStride + i);
255 }
256
257 return Error::NONE;
258 }
259
260 } // namespace Gralloc2
261 } // namespace android
262
263