blob: a0bbe63cbd82be3a55cf3c2b74bd719ac3fb21c9 [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);
Craig Donner6ebc46a2016-10-21 15:23:44 -0700100 case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
101 return asFP<GRALLOC1_PFN_SET_LAYER_COUNT>(setLayerCountHook);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800102 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
103 return asFP<GRALLOC1_PFN_SET_PRODUCER_USAGE>(setProducerUsageHook);
104 case GRALLOC1_FUNCTION_GET_BACKING_STORE:
105 return asFP<GRALLOC1_PFN_GET_BACKING_STORE>(
106 bufferHook<decltype(&Buffer::getBackingStore),
107 &Buffer::getBackingStore, gralloc1_backing_store_t*>);
108 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
109 return asFP<GRALLOC1_PFN_GET_CONSUMER_USAGE>(getConsumerUsageHook);
110 case GRALLOC1_FUNCTION_GET_DIMENSIONS:
111 return asFP<GRALLOC1_PFN_GET_DIMENSIONS>(
112 bufferHook<decltype(&Buffer::getDimensions),
113 &Buffer::getDimensions, uint32_t*, uint32_t*>);
114 case GRALLOC1_FUNCTION_GET_FORMAT:
115 return asFP<GRALLOC1_PFN_GET_FORMAT>(
116 bufferHook<decltype(&Buffer::getFormat),
117 &Buffer::getFormat, int32_t*>);
Craig Donner6ebc46a2016-10-21 15:23:44 -0700118 case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
119 return asFP<GRALLOC1_PFN_GET_LAYER_COUNT>(
120 bufferHook<decltype(&Buffer::getLayerCount),
121 &Buffer::getLayerCount, uint32_t*>);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800122 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
123 return asFP<GRALLOC1_PFN_GET_PRODUCER_USAGE>(getProducerUsageHook);
124 case GRALLOC1_FUNCTION_GET_STRIDE:
125 return asFP<GRALLOC1_PFN_GET_STRIDE>(
126 bufferHook<decltype(&Buffer::getStride),
127 &Buffer::getStride, uint32_t*>);
128 case GRALLOC1_FUNCTION_ALLOCATE:
129 // Not provided, since we'll use ALLOCATE_WITH_ID
130 return nullptr;
131 case GRALLOC1_FUNCTION_ALLOCATE_WITH_ID:
132 if (mDevice != nullptr) {
133 return asFP<GRALLOC1_PFN_ALLOCATE_WITH_ID>(allocateWithIdHook);
134 } else {
135 return nullptr;
136 }
137 case GRALLOC1_FUNCTION_RETAIN:
138 return asFP<GRALLOC1_PFN_RETAIN>(
139 managementHook<&Gralloc1On0Adapter::retain>);
140 case GRALLOC1_FUNCTION_RELEASE:
141 return asFP<GRALLOC1_PFN_RELEASE>(
142 managementHook<&Gralloc1On0Adapter::release>);
143 case GRALLOC1_FUNCTION_RETAIN_GRAPHIC_BUFFER:
144 return asFP<GRALLOC1_PFN_RETAIN_GRAPHIC_BUFFER>(
145 retainGraphicBufferHook);
146 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
147 return asFP<GRALLOC1_PFN_GET_NUM_FLEX_PLANES>(
148 bufferHook<decltype(&Buffer::getNumFlexPlanes),
149 &Buffer::getNumFlexPlanes, uint32_t*>);
150 case GRALLOC1_FUNCTION_LOCK:
151 return asFP<GRALLOC1_PFN_LOCK>(
152 lockHook<void*, &Gralloc1On0Adapter::lock>);
153 case GRALLOC1_FUNCTION_LOCK_FLEX:
154 return asFP<GRALLOC1_PFN_LOCK_FLEX>(
155 lockHook<struct android_flex_layout,
156 &Gralloc1On0Adapter::lockFlex>);
157 case GRALLOC1_FUNCTION_LOCK_YCBCR:
158 return asFP<GRALLOC1_PFN_LOCK_YCBCR>(
159 lockHook<struct android_ycbcr,
160 &Gralloc1On0Adapter::lockYCbCr>);
161 case GRALLOC1_FUNCTION_UNLOCK:
162 return asFP<GRALLOC1_PFN_UNLOCK>(unlockHook);
163 case GRALLOC1_FUNCTION_INVALID:
164 ALOGE("Invalid function descriptor");
165 return nullptr;
166 }
167
168 ALOGE("Unknown function descriptor: %d", intDescriptor);
169 return nullptr;
170}
171
172void Gralloc1On0Adapter::dump(uint32_t* outSize, char* outBuffer)
173{
174 ALOGV("dump(%u (%p), %p", outSize ? *outSize : 0, outSize, outBuffer);
175
176 if (!mDevice->dump) {
177 // dump is optional on gralloc0 implementations
178 *outSize = 0;
179 return;
180 }
181
182 if (!outBuffer) {
183 constexpr int32_t BUFFER_LENGTH = 4096;
184 char buffer[BUFFER_LENGTH] = {};
185 mDevice->dump(mDevice, buffer, BUFFER_LENGTH);
186 buffer[BUFFER_LENGTH - 1] = 0; // Ensure the buffer is null-terminated
187 size_t actualLength = std::strlen(buffer);
188 mCachedDump.resize(actualLength);
189 std::copy_n(buffer, actualLength, mCachedDump.begin());
190 *outSize = static_cast<uint32_t>(actualLength);
191 } else {
192 *outSize = std::min(*outSize,
193 static_cast<uint32_t>(mCachedDump.size()));
194 outBuffer = std::copy_n(mCachedDump.cbegin(), *outSize, outBuffer);
195 }
196}
197
198gralloc1_error_t Gralloc1On0Adapter::createDescriptor(
199 gralloc1_buffer_descriptor_t* outDescriptor)
200{
201 auto descriptorId = sNextBufferDescriptorId++;
Dan Stoza923c0662016-06-21 16:22:06 -0700202 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800203 mDescriptors.emplace(descriptorId,
204 std::make_shared<Descriptor>(this, descriptorId));
205
206 ALOGV("Created descriptor %" PRIu64, descriptorId);
207
208 *outDescriptor = descriptorId;
209 return GRALLOC1_ERROR_NONE;
210}
211
212gralloc1_error_t Gralloc1On0Adapter::destroyDescriptor(
213 gralloc1_buffer_descriptor_t descriptor)
214{
215 ALOGV("Destroying descriptor %" PRIu64, descriptor);
216
Dan Stoza923c0662016-06-21 16:22:06 -0700217 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800218 if (mDescriptors.count(descriptor) == 0) {
219 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
220 }
221
222 mDescriptors.erase(descriptor);
223 return GRALLOC1_ERROR_NONE;
224}
225
226Gralloc1On0Adapter::Buffer::Buffer(buffer_handle_t handle,
227 gralloc1_backing_store_t store, const Descriptor& descriptor,
228 uint32_t stride, bool wasAllocated)
229 : mHandle(handle),
230 mReferenceCount(1),
231 mStore(store),
232 mDescriptor(descriptor),
233 mStride(stride),
234 mWasAllocated(wasAllocated) {}
235
236gralloc1_error_t Gralloc1On0Adapter::allocate(
237 const std::shared_ptr<Descriptor>& descriptor,
238 gralloc1_backing_store_t store,
239 buffer_handle_t* outBufferHandle)
240{
241 ALOGV("allocate(%" PRIu64 ", %#" PRIx64 ")", descriptor->id, store);
242
243 // If this function is being called, it's because we handed out its function
244 // pointer, which only occurs when mDevice has been loaded successfully and
245 // we are permitted to allocate
246
247 int usage = static_cast<int>(descriptor->producerUsage) |
248 static_cast<int>(descriptor->consumerUsage);
249 buffer_handle_t handle = nullptr;
250 int stride = 0;
251 ALOGV("Calling alloc(%p, %u, %u, %i, %u)", mDevice, descriptor->width,
252 descriptor->height, descriptor->format, usage);
253 auto error = mDevice->alloc(mDevice,
254 static_cast<int>(descriptor->width),
255 static_cast<int>(descriptor->height), descriptor->format,
256 usage, &handle, &stride);
257 if (error != 0) {
258 ALOGE("gralloc0 allocation failed: %d (%s)", error,
259 strerror(-error));
260 return GRALLOC1_ERROR_NO_RESOURCES;
261 }
262
263 *outBufferHandle = handle;
264 auto buffer = std::make_shared<Buffer>(handle, store, *descriptor, stride,
265 true);
Dan Stoza923c0662016-06-21 16:22:06 -0700266
267 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800268 mBuffers.emplace(handle, std::move(buffer));
269
270 return GRALLOC1_ERROR_NONE;
271}
272
273gralloc1_error_t Gralloc1On0Adapter::allocateWithIdHook(
274 gralloc1_device_t* device, gralloc1_buffer_descriptor_t descriptorId,
275 gralloc1_backing_store_t store, buffer_handle_t* outBuffer)
276{
277 auto adapter = getAdapter(device);
278
279 auto descriptor = adapter->getDescriptor(descriptorId);
280 if (!descriptor) {
281 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
282 }
283
284 buffer_handle_t bufferHandle = nullptr;
285 auto error = adapter->allocate(descriptor, store, &bufferHandle);
286 if (error != GRALLOC1_ERROR_NONE) {
287 return error;
288 }
289
290 *outBuffer = bufferHandle;
291 return error;
292}
293
294gralloc1_error_t Gralloc1On0Adapter::retain(
295 const std::shared_ptr<Buffer>& buffer)
296{
Ajit Kumar56ec3af2016-11-25 12:08:53 +0530297 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800298 buffer->retain();
299 return GRALLOC1_ERROR_NONE;
300}
301
302gralloc1_error_t Gralloc1On0Adapter::release(
303 const std::shared_ptr<Buffer>& buffer)
304{
Ajit Kumar56ec3af2016-11-25 12:08:53 +0530305 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800306 if (!buffer->release()) {
307 return GRALLOC1_ERROR_NONE;
308 }
309
310 buffer_handle_t handle = buffer->getHandle();
311 if (buffer->wasAllocated()) {
312 ALOGV("Calling free(%p)", handle);
313 int result = mDevice->free(mDevice, handle);
314 if (result != 0) {
315 ALOGE("gralloc0 free failed: %d", result);
316 }
317 } else {
318 ALOGV("Calling unregisterBuffer(%p)", handle);
319 int result = mModule->unregisterBuffer(mModule, handle);
320 if (result != 0) {
321 ALOGE("gralloc0 unregister failed: %d", result);
322 }
323 }
Dan Stoza923c0662016-06-21 16:22:06 -0700324
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800325 mBuffers.erase(handle);
326 return GRALLOC1_ERROR_NONE;
327}
328
329gralloc1_error_t Gralloc1On0Adapter::retain(
330 const android::GraphicBuffer* graphicBuffer)
331{
332 ALOGV("retainGraphicBuffer(%p, %#" PRIx64 ")",
333 graphicBuffer->getNativeBuffer()->handle, graphicBuffer->getId());
334
335 buffer_handle_t handle = graphicBuffer->getNativeBuffer()->handle;
Dan Stoza923c0662016-06-21 16:22:06 -0700336 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800337 if (mBuffers.count(handle) != 0) {
338 mBuffers[handle]->retain();
339 return GRALLOC1_ERROR_NONE;
340 }
341
342 ALOGV("Calling registerBuffer(%p)", handle);
343 int result = mModule->registerBuffer(mModule, handle);
344 if (result != 0) {
345 ALOGE("gralloc0 register failed: %d", result);
346 return GRALLOC1_ERROR_NO_RESOURCES;
347 }
348
349 Descriptor descriptor{this, sNextBufferDescriptorId++};
350 descriptor.setDimensions(graphicBuffer->getWidth(),
351 graphicBuffer->getHeight());
352 descriptor.setFormat(graphicBuffer->getPixelFormat());
353 descriptor.setProducerUsage(
354 static_cast<gralloc1_producer_usage_t>(graphicBuffer->getUsage()));
355 descriptor.setConsumerUsage(
356 static_cast<gralloc1_consumer_usage_t>(graphicBuffer->getUsage()));
357 auto buffer = std::make_shared<Buffer>(handle,
358 static_cast<gralloc1_backing_store_t>(graphicBuffer->getId()),
359 descriptor, graphicBuffer->getStride(), false);
360 mBuffers.emplace(handle, std::move(buffer));
361 return GRALLOC1_ERROR_NONE;
362}
363
364gralloc1_error_t Gralloc1On0Adapter::lock(
365 const std::shared_ptr<Buffer>& buffer,
366 gralloc1_producer_usage_t producerUsage,
367 gralloc1_consumer_usage_t consumerUsage,
368 const gralloc1_rect_t& accessRegion, void** outData,
369 const sp<Fence>& acquireFence)
370{
371 if (mMinorVersion >= 3) {
372 int result = mModule->lockAsync(mModule, buffer->getHandle(),
373 static_cast<int32_t>(producerUsage | consumerUsage),
374 accessRegion.left, accessRegion.top, accessRegion.width,
375 accessRegion.height, outData, acquireFence->dup());
376 if (result != 0) {
377 return GRALLOC1_ERROR_UNSUPPORTED;
378 }
379 } else {
380 acquireFence->waitForever("Gralloc1On0Adapter::lock");
381 int result = mModule->lock(mModule, buffer->getHandle(),
382 static_cast<int32_t>(producerUsage | consumerUsage),
383 accessRegion.left, accessRegion.top, accessRegion.width,
384 accessRegion.height, outData);
385 ALOGV("gralloc0 lock returned %d", result);
386 if (result != 0) {
387 return GRALLOC1_ERROR_UNSUPPORTED;
388 }
389 }
390 return GRALLOC1_ERROR_NONE;
391}
392
393gralloc1_error_t Gralloc1On0Adapter::lockFlex(
394 const std::shared_ptr<Buffer>& /*buffer*/,
395 gralloc1_producer_usage_t /*producerUsage*/,
396 gralloc1_consumer_usage_t /*consumerUsage*/,
397 const gralloc1_rect_t& /*accessRegion*/,
398 struct android_flex_layout* /*outData*/,
399 const sp<Fence>& /*acquireFence*/)
400{
401 // TODO
402 return GRALLOC1_ERROR_UNSUPPORTED;
403}
404
405gralloc1_error_t Gralloc1On0Adapter::lockYCbCr(
406 const std::shared_ptr<Buffer>& buffer,
407 gralloc1_producer_usage_t producerUsage,
408 gralloc1_consumer_usage_t consumerUsage,
409 const gralloc1_rect_t& accessRegion, struct android_ycbcr* outData,
410 const sp<Fence>& acquireFence)
411{
412 if (mMinorVersion >= 3 && mModule->lockAsync_ycbcr) {
413 int result = mModule->lockAsync_ycbcr(mModule, buffer->getHandle(),
414 static_cast<int>(producerUsage | consumerUsage),
415 accessRegion.left, accessRegion.top, accessRegion.width,
416 accessRegion.height, outData, acquireFence->dup());
417 if (result != 0) {
418 return GRALLOC1_ERROR_UNSUPPORTED;
419 }
420 } else if (mModule->lock_ycbcr) {
421 acquireFence->waitForever("Gralloc1On0Adapter::lockYCbCr");
422 int result = mModule->lock_ycbcr(mModule, buffer->getHandle(),
423 static_cast<int>(producerUsage | consumerUsage),
424 accessRegion.left, accessRegion.top, accessRegion.width,
425 accessRegion.height, outData);
426 ALOGV("gralloc0 lockYCbCr returned %d", result);
427 if (result != 0) {
428 return GRALLOC1_ERROR_UNSUPPORTED;
429 }
430 } else {
431 return GRALLOC1_ERROR_UNSUPPORTED;
432 }
433
434 return GRALLOC1_ERROR_NONE;
435}
436
437gralloc1_error_t Gralloc1On0Adapter::unlock(
438 const std::shared_ptr<Buffer>& buffer,
439 sp<Fence>* outReleaseFence)
440{
441 if (mMinorVersion >= 3) {
442 int fenceFd = -1;
443 int result = mModule->unlockAsync(mModule, buffer->getHandle(),
444 &fenceFd);
445 if (result != 0) {
446 close(fenceFd);
447 ALOGE("gralloc0 unlockAsync failed: %d", result);
448 } else {
449 *outReleaseFence = new Fence(fenceFd);
450 }
451 } else {
452 int result = mModule->unlock(mModule, buffer->getHandle());
453 if (result != 0) {
454 ALOGE("gralloc0 unlock failed: %d", result);
455 }
456 }
457 return GRALLOC1_ERROR_NONE;
458}
459
460std::shared_ptr<Gralloc1On0Adapter::Descriptor>
461Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId)
462{
Dan Stoza923c0662016-06-21 16:22:06 -0700463 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800464 if (mDescriptors.count(descriptorId) == 0) {
465 return nullptr;
466 }
467
468 return mDescriptors[descriptorId];
469}
470
471std::shared_ptr<Gralloc1On0Adapter::Buffer> Gralloc1On0Adapter::getBuffer(
472 buffer_handle_t bufferHandle)
473{
Dan Stoza923c0662016-06-21 16:22:06 -0700474 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800475 if (mBuffers.count(bufferHandle) == 0) {
476 return nullptr;
477 }
478
479 return mBuffers[bufferHandle];
480}
481
482std::atomic<gralloc1_buffer_descriptor_t>
483 Gralloc1On0Adapter::sNextBufferDescriptorId(1);
484
485} // namespace android