blob: 6e69df1621dfdec6e22270441034fb9229040f79 [file] [log] [blame]
Dan Stoza1e2a2a02016-01-11 15:21:07 -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#undef LOG_TAG
18#define LOG_TAG "Gralloc1On0Adapter"
19//#define LOG_NDEBUG 0
20
21#include <hardware/gralloc.h>
22
23#include <ui/Gralloc1On0Adapter.h>
24
25#include <utils/Log.h>
26
27#include <inttypes.h>
28
29template <typename PFN, typename T>
30static gralloc1_function_pointer_t asFP(T function)
31{
32 static_assert(std::is_same<PFN, T>::value, "Incompatible function pointer");
33 return reinterpret_cast<gralloc1_function_pointer_t>(function);
34}
35
36namespace android {
37
38Gralloc1On0Adapter::Gralloc1On0Adapter(const hw_module_t* module)
39 : mModule(reinterpret_cast<const gralloc_module_t*>(module)),
40 mMinorVersion(mModule->common.module_api_version & 0xFF),
41 mDevice(nullptr)
42{
43 ALOGV("Constructing");
44 getCapabilities = getCapabilitiesHook;
45 getFunction = getFunctionHook;
46 int error = ::gralloc_open(&(mModule->common), &mDevice);
47 if (error) {
48 ALOGE("Failed to open gralloc0 module: %d", error);
49 }
50 ALOGV("Opened gralloc0 device %p", mDevice);
51}
52
53Gralloc1On0Adapter::~Gralloc1On0Adapter()
54{
55 ALOGV("Destructing");
56 if (mDevice) {
57 ALOGV("Closing gralloc0 device %p", mDevice);
58 ::gralloc_close(mDevice);
59 }
60}
61
62void Gralloc1On0Adapter::doGetCapabilities(uint32_t* outCount,
63 int32_t* outCapabilities)
64{
65 if (outCapabilities == nullptr) {
66 *outCount = 1;
67 return;
68 }
69 if (*outCount >= 1) {
70 *outCapabilities = GRALLOC1_CAPABILITY_ON_ADAPTER;
71 *outCount = 1;
72 }
73}
74
75gralloc1_function_pointer_t Gralloc1On0Adapter::doGetFunction(
76 int32_t intDescriptor)
77{
78 constexpr auto lastDescriptor =
79 static_cast<int32_t>(GRALLOC1_LAST_ADAPTER_FUNCTION);
80 if (intDescriptor < 0 || intDescriptor > lastDescriptor) {
81 ALOGE("Invalid function descriptor");
82 return nullptr;
83 }
84
85 auto descriptor =
86 static_cast<gralloc1_function_descriptor_t>(intDescriptor);
87 switch (descriptor) {
88 case GRALLOC1_FUNCTION_DUMP:
89 return asFP<GRALLOC1_PFN_DUMP>(dumpHook);
90 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
91 return asFP<GRALLOC1_PFN_CREATE_DESCRIPTOR>(createDescriptorHook);
92 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
93 return asFP<GRALLOC1_PFN_DESTROY_DESCRIPTOR>(destroyDescriptorHook);
94 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
95 return asFP<GRALLOC1_PFN_SET_CONSUMER_USAGE>(setConsumerUsageHook);
96 case GRALLOC1_FUNCTION_SET_DIMENSIONS:
97 return asFP<GRALLOC1_PFN_SET_DIMENSIONS>(setDimensionsHook);
98 case GRALLOC1_FUNCTION_SET_FORMAT:
99 return asFP<GRALLOC1_PFN_SET_FORMAT>(setFormatHook);
100 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
101 return asFP<GRALLOC1_PFN_SET_PRODUCER_USAGE>(setProducerUsageHook);
102 case GRALLOC1_FUNCTION_GET_BACKING_STORE:
103 return asFP<GRALLOC1_PFN_GET_BACKING_STORE>(
104 bufferHook<decltype(&Buffer::getBackingStore),
105 &Buffer::getBackingStore, gralloc1_backing_store_t*>);
106 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
107 return asFP<GRALLOC1_PFN_GET_CONSUMER_USAGE>(getConsumerUsageHook);
108 case GRALLOC1_FUNCTION_GET_DIMENSIONS:
109 return asFP<GRALLOC1_PFN_GET_DIMENSIONS>(
110 bufferHook<decltype(&Buffer::getDimensions),
111 &Buffer::getDimensions, uint32_t*, uint32_t*>);
112 case GRALLOC1_FUNCTION_GET_FORMAT:
113 return asFP<GRALLOC1_PFN_GET_FORMAT>(
114 bufferHook<decltype(&Buffer::getFormat),
115 &Buffer::getFormat, int32_t*>);
116 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
117 return asFP<GRALLOC1_PFN_GET_PRODUCER_USAGE>(getProducerUsageHook);
118 case GRALLOC1_FUNCTION_GET_STRIDE:
119 return asFP<GRALLOC1_PFN_GET_STRIDE>(
120 bufferHook<decltype(&Buffer::getStride),
121 &Buffer::getStride, uint32_t*>);
122 case GRALLOC1_FUNCTION_ALLOCATE:
123 // Not provided, since we'll use ALLOCATE_WITH_ID
124 return nullptr;
125 case GRALLOC1_FUNCTION_ALLOCATE_WITH_ID:
126 if (mDevice != nullptr) {
127 return asFP<GRALLOC1_PFN_ALLOCATE_WITH_ID>(allocateWithIdHook);
128 } else {
129 return nullptr;
130 }
131 case GRALLOC1_FUNCTION_RETAIN:
132 return asFP<GRALLOC1_PFN_RETAIN>(
133 managementHook<&Gralloc1On0Adapter::retain>);
134 case GRALLOC1_FUNCTION_RELEASE:
135 return asFP<GRALLOC1_PFN_RELEASE>(
136 managementHook<&Gralloc1On0Adapter::release>);
137 case GRALLOC1_FUNCTION_RETAIN_GRAPHIC_BUFFER:
138 return asFP<GRALLOC1_PFN_RETAIN_GRAPHIC_BUFFER>(
139 retainGraphicBufferHook);
140 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
141 return asFP<GRALLOC1_PFN_GET_NUM_FLEX_PLANES>(
142 bufferHook<decltype(&Buffer::getNumFlexPlanes),
143 &Buffer::getNumFlexPlanes, uint32_t*>);
144 case GRALLOC1_FUNCTION_LOCK:
145 return asFP<GRALLOC1_PFN_LOCK>(
146 lockHook<void*, &Gralloc1On0Adapter::lock>);
147 case GRALLOC1_FUNCTION_LOCK_FLEX:
148 return asFP<GRALLOC1_PFN_LOCK_FLEX>(
149 lockHook<struct android_flex_layout,
150 &Gralloc1On0Adapter::lockFlex>);
151 case GRALLOC1_FUNCTION_LOCK_YCBCR:
152 return asFP<GRALLOC1_PFN_LOCK_YCBCR>(
153 lockHook<struct android_ycbcr,
154 &Gralloc1On0Adapter::lockYCbCr>);
155 case GRALLOC1_FUNCTION_UNLOCK:
156 return asFP<GRALLOC1_PFN_UNLOCK>(unlockHook);
157 case GRALLOC1_FUNCTION_INVALID:
158 ALOGE("Invalid function descriptor");
159 return nullptr;
160 }
161
162 ALOGE("Unknown function descriptor: %d", intDescriptor);
163 return nullptr;
164}
165
166void Gralloc1On0Adapter::dump(uint32_t* outSize, char* outBuffer)
167{
168 ALOGV("dump(%u (%p), %p", outSize ? *outSize : 0, outSize, outBuffer);
169
170 if (!mDevice->dump) {
171 // dump is optional on gralloc0 implementations
172 *outSize = 0;
173 return;
174 }
175
176 if (!outBuffer) {
177 constexpr int32_t BUFFER_LENGTH = 4096;
178 char buffer[BUFFER_LENGTH] = {};
179 mDevice->dump(mDevice, buffer, BUFFER_LENGTH);
180 buffer[BUFFER_LENGTH - 1] = 0; // Ensure the buffer is null-terminated
181 size_t actualLength = std::strlen(buffer);
182 mCachedDump.resize(actualLength);
183 std::copy_n(buffer, actualLength, mCachedDump.begin());
184 *outSize = static_cast<uint32_t>(actualLength);
185 } else {
186 *outSize = std::min(*outSize,
187 static_cast<uint32_t>(mCachedDump.size()));
188 outBuffer = std::copy_n(mCachedDump.cbegin(), *outSize, outBuffer);
189 }
190}
191
192gralloc1_error_t Gralloc1On0Adapter::createDescriptor(
193 gralloc1_buffer_descriptor_t* outDescriptor)
194{
195 auto descriptorId = sNextBufferDescriptorId++;
196 mDescriptors.emplace(descriptorId,
197 std::make_shared<Descriptor>(this, descriptorId));
198
199 ALOGV("Created descriptor %" PRIu64, descriptorId);
200
201 *outDescriptor = descriptorId;
202 return GRALLOC1_ERROR_NONE;
203}
204
205gralloc1_error_t Gralloc1On0Adapter::destroyDescriptor(
206 gralloc1_buffer_descriptor_t descriptor)
207{
208 ALOGV("Destroying descriptor %" PRIu64, descriptor);
209
210 if (mDescriptors.count(descriptor) == 0) {
211 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
212 }
213
214 mDescriptors.erase(descriptor);
215 return GRALLOC1_ERROR_NONE;
216}
217
218Gralloc1On0Adapter::Buffer::Buffer(buffer_handle_t handle,
219 gralloc1_backing_store_t store, const Descriptor& descriptor,
220 uint32_t stride, bool wasAllocated)
221 : mHandle(handle),
222 mReferenceCount(1),
223 mStore(store),
224 mDescriptor(descriptor),
225 mStride(stride),
226 mWasAllocated(wasAllocated) {}
227
228gralloc1_error_t Gralloc1On0Adapter::allocate(
229 const std::shared_ptr<Descriptor>& descriptor,
230 gralloc1_backing_store_t store,
231 buffer_handle_t* outBufferHandle)
232{
233 ALOGV("allocate(%" PRIu64 ", %#" PRIx64 ")", descriptor->id, store);
234
235 // If this function is being called, it's because we handed out its function
236 // pointer, which only occurs when mDevice has been loaded successfully and
237 // we are permitted to allocate
238
239 int usage = static_cast<int>(descriptor->producerUsage) |
240 static_cast<int>(descriptor->consumerUsage);
241 buffer_handle_t handle = nullptr;
242 int stride = 0;
243 ALOGV("Calling alloc(%p, %u, %u, %i, %u)", mDevice, descriptor->width,
244 descriptor->height, descriptor->format, usage);
245 auto error = mDevice->alloc(mDevice,
246 static_cast<int>(descriptor->width),
247 static_cast<int>(descriptor->height), descriptor->format,
248 usage, &handle, &stride);
249 if (error != 0) {
250 ALOGE("gralloc0 allocation failed: %d (%s)", error,
251 strerror(-error));
252 return GRALLOC1_ERROR_NO_RESOURCES;
253 }
254
255 *outBufferHandle = handle;
256 auto buffer = std::make_shared<Buffer>(handle, store, *descriptor, stride,
257 true);
258 mBuffers.emplace(handle, std::move(buffer));
259
260 return GRALLOC1_ERROR_NONE;
261}
262
263gralloc1_error_t Gralloc1On0Adapter::allocateWithIdHook(
264 gralloc1_device_t* device, gralloc1_buffer_descriptor_t descriptorId,
265 gralloc1_backing_store_t store, buffer_handle_t* outBuffer)
266{
267 auto adapter = getAdapter(device);
268
269 auto descriptor = adapter->getDescriptor(descriptorId);
270 if (!descriptor) {
271 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
272 }
273
274 buffer_handle_t bufferHandle = nullptr;
275 auto error = adapter->allocate(descriptor, store, &bufferHandle);
276 if (error != GRALLOC1_ERROR_NONE) {
277 return error;
278 }
279
280 *outBuffer = bufferHandle;
281 return error;
282}
283
284gralloc1_error_t Gralloc1On0Adapter::retain(
285 const std::shared_ptr<Buffer>& buffer)
286{
287 buffer->retain();
288 return GRALLOC1_ERROR_NONE;
289}
290
291gralloc1_error_t Gralloc1On0Adapter::release(
292 const std::shared_ptr<Buffer>& buffer)
293{
294 if (!buffer->release()) {
295 return GRALLOC1_ERROR_NONE;
296 }
297
298 buffer_handle_t handle = buffer->getHandle();
299 if (buffer->wasAllocated()) {
300 ALOGV("Calling free(%p)", handle);
301 int result = mDevice->free(mDevice, handle);
302 if (result != 0) {
303 ALOGE("gralloc0 free failed: %d", result);
304 }
305 } else {
306 ALOGV("Calling unregisterBuffer(%p)", handle);
307 int result = mModule->unregisterBuffer(mModule, handle);
308 if (result != 0) {
309 ALOGE("gralloc0 unregister failed: %d", result);
310 }
311 }
312 mBuffers.erase(handle);
313 return GRALLOC1_ERROR_NONE;
314}
315
316gralloc1_error_t Gralloc1On0Adapter::retain(
317 const android::GraphicBuffer* graphicBuffer)
318{
319 ALOGV("retainGraphicBuffer(%p, %#" PRIx64 ")",
320 graphicBuffer->getNativeBuffer()->handle, graphicBuffer->getId());
321
322 buffer_handle_t handle = graphicBuffer->getNativeBuffer()->handle;
323 if (mBuffers.count(handle) != 0) {
324 mBuffers[handle]->retain();
325 return GRALLOC1_ERROR_NONE;
326 }
327
328 ALOGV("Calling registerBuffer(%p)", handle);
329 int result = mModule->registerBuffer(mModule, handle);
330 if (result != 0) {
331 ALOGE("gralloc0 register failed: %d", result);
332 return GRALLOC1_ERROR_NO_RESOURCES;
333 }
334
335 Descriptor descriptor{this, sNextBufferDescriptorId++};
336 descriptor.setDimensions(graphicBuffer->getWidth(),
337 graphicBuffer->getHeight());
338 descriptor.setFormat(graphicBuffer->getPixelFormat());
339 descriptor.setProducerUsage(
340 static_cast<gralloc1_producer_usage_t>(graphicBuffer->getUsage()));
341 descriptor.setConsumerUsage(
342 static_cast<gralloc1_consumer_usage_t>(graphicBuffer->getUsage()));
343 auto buffer = std::make_shared<Buffer>(handle,
344 static_cast<gralloc1_backing_store_t>(graphicBuffer->getId()),
345 descriptor, graphicBuffer->getStride(), false);
346 mBuffers.emplace(handle, std::move(buffer));
347 return GRALLOC1_ERROR_NONE;
348}
349
350gralloc1_error_t Gralloc1On0Adapter::lock(
351 const std::shared_ptr<Buffer>& buffer,
352 gralloc1_producer_usage_t producerUsage,
353 gralloc1_consumer_usage_t consumerUsage,
354 const gralloc1_rect_t& accessRegion, void** outData,
355 const sp<Fence>& acquireFence)
356{
357 if (mMinorVersion >= 3) {
358 int result = mModule->lockAsync(mModule, buffer->getHandle(),
359 static_cast<int32_t>(producerUsage | consumerUsage),
360 accessRegion.left, accessRegion.top, accessRegion.width,
361 accessRegion.height, outData, acquireFence->dup());
362 if (result != 0) {
363 return GRALLOC1_ERROR_UNSUPPORTED;
364 }
365 } else {
366 acquireFence->waitForever("Gralloc1On0Adapter::lock");
367 int result = mModule->lock(mModule, buffer->getHandle(),
368 static_cast<int32_t>(producerUsage | consumerUsage),
369 accessRegion.left, accessRegion.top, accessRegion.width,
370 accessRegion.height, outData);
371 ALOGV("gralloc0 lock returned %d", result);
372 if (result != 0) {
373 return GRALLOC1_ERROR_UNSUPPORTED;
374 }
375 }
376 return GRALLOC1_ERROR_NONE;
377}
378
379gralloc1_error_t Gralloc1On0Adapter::lockFlex(
380 const std::shared_ptr<Buffer>& /*buffer*/,
381 gralloc1_producer_usage_t /*producerUsage*/,
382 gralloc1_consumer_usage_t /*consumerUsage*/,
383 const gralloc1_rect_t& /*accessRegion*/,
384 struct android_flex_layout* /*outData*/,
385 const sp<Fence>& /*acquireFence*/)
386{
387 // TODO
388 return GRALLOC1_ERROR_UNSUPPORTED;
389}
390
391gralloc1_error_t Gralloc1On0Adapter::lockYCbCr(
392 const std::shared_ptr<Buffer>& buffer,
393 gralloc1_producer_usage_t producerUsage,
394 gralloc1_consumer_usage_t consumerUsage,
395 const gralloc1_rect_t& accessRegion, struct android_ycbcr* outData,
396 const sp<Fence>& acquireFence)
397{
398 if (mMinorVersion >= 3 && mModule->lockAsync_ycbcr) {
399 int result = mModule->lockAsync_ycbcr(mModule, buffer->getHandle(),
400 static_cast<int>(producerUsage | consumerUsage),
401 accessRegion.left, accessRegion.top, accessRegion.width,
402 accessRegion.height, outData, acquireFence->dup());
403 if (result != 0) {
404 return GRALLOC1_ERROR_UNSUPPORTED;
405 }
406 } else if (mModule->lock_ycbcr) {
407 acquireFence->waitForever("Gralloc1On0Adapter::lockYCbCr");
408 int result = mModule->lock_ycbcr(mModule, buffer->getHandle(),
409 static_cast<int>(producerUsage | consumerUsage),
410 accessRegion.left, accessRegion.top, accessRegion.width,
411 accessRegion.height, outData);
412 ALOGV("gralloc0 lockYCbCr returned %d", result);
413 if (result != 0) {
414 return GRALLOC1_ERROR_UNSUPPORTED;
415 }
416 } else {
417 return GRALLOC1_ERROR_UNSUPPORTED;
418 }
419
420 return GRALLOC1_ERROR_NONE;
421}
422
423gralloc1_error_t Gralloc1On0Adapter::unlock(
424 const std::shared_ptr<Buffer>& buffer,
425 sp<Fence>* outReleaseFence)
426{
427 if (mMinorVersion >= 3) {
428 int fenceFd = -1;
429 int result = mModule->unlockAsync(mModule, buffer->getHandle(),
430 &fenceFd);
431 if (result != 0) {
432 close(fenceFd);
433 ALOGE("gralloc0 unlockAsync failed: %d", result);
434 } else {
435 *outReleaseFence = new Fence(fenceFd);
436 }
437 } else {
438 int result = mModule->unlock(mModule, buffer->getHandle());
439 if (result != 0) {
440 ALOGE("gralloc0 unlock failed: %d", result);
441 }
442 }
443 return GRALLOC1_ERROR_NONE;
444}
445
446std::shared_ptr<Gralloc1On0Adapter::Descriptor>
447Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId)
448{
449 if (mDescriptors.count(descriptorId) == 0) {
450 return nullptr;
451 }
452
453 return mDescriptors[descriptorId];
454}
455
456std::shared_ptr<Gralloc1On0Adapter::Buffer> Gralloc1On0Adapter::getBuffer(
457 buffer_handle_t bufferHandle)
458{
459 if (mBuffers.count(bufferHandle) == 0) {
460 return nullptr;
461 }
462
463 return mBuffers[bufferHandle];
464}
465
466std::atomic<gralloc1_buffer_descriptor_t>
467 Gralloc1On0Adapter::sNextBufferDescriptorId(1);
468
469} // namespace android