blob: 111879aed1f56a3990118bfe76784990617993dd [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{
297 buffer->retain();
298 return GRALLOC1_ERROR_NONE;
299}
300
301gralloc1_error_t Gralloc1On0Adapter::release(
302 const std::shared_ptr<Buffer>& buffer)
303{
304 if (!buffer->release()) {
305 return GRALLOC1_ERROR_NONE;
306 }
307
308 buffer_handle_t handle = buffer->getHandle();
309 if (buffer->wasAllocated()) {
310 ALOGV("Calling free(%p)", handle);
311 int result = mDevice->free(mDevice, handle);
312 if (result != 0) {
313 ALOGE("gralloc0 free failed: %d", result);
314 }
315 } else {
316 ALOGV("Calling unregisterBuffer(%p)", handle);
317 int result = mModule->unregisterBuffer(mModule, handle);
318 if (result != 0) {
319 ALOGE("gralloc0 unregister failed: %d", result);
320 }
321 }
Dan Stoza923c0662016-06-21 16:22:06 -0700322
323 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800324 mBuffers.erase(handle);
325 return GRALLOC1_ERROR_NONE;
326}
327
328gralloc1_error_t Gralloc1On0Adapter::retain(
329 const android::GraphicBuffer* graphicBuffer)
330{
331 ALOGV("retainGraphicBuffer(%p, %#" PRIx64 ")",
332 graphicBuffer->getNativeBuffer()->handle, graphicBuffer->getId());
333
334 buffer_handle_t handle = graphicBuffer->getNativeBuffer()->handle;
Dan Stoza923c0662016-06-21 16:22:06 -0700335 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800336 if (mBuffers.count(handle) != 0) {
337 mBuffers[handle]->retain();
338 return GRALLOC1_ERROR_NONE;
339 }
340
341 ALOGV("Calling registerBuffer(%p)", handle);
342 int result = mModule->registerBuffer(mModule, handle);
343 if (result != 0) {
344 ALOGE("gralloc0 register failed: %d", result);
345 return GRALLOC1_ERROR_NO_RESOURCES;
346 }
347
348 Descriptor descriptor{this, sNextBufferDescriptorId++};
349 descriptor.setDimensions(graphicBuffer->getWidth(),
350 graphicBuffer->getHeight());
351 descriptor.setFormat(graphicBuffer->getPixelFormat());
352 descriptor.setProducerUsage(
353 static_cast<gralloc1_producer_usage_t>(graphicBuffer->getUsage()));
354 descriptor.setConsumerUsage(
355 static_cast<gralloc1_consumer_usage_t>(graphicBuffer->getUsage()));
356 auto buffer = std::make_shared<Buffer>(handle,
357 static_cast<gralloc1_backing_store_t>(graphicBuffer->getId()),
358 descriptor, graphicBuffer->getStride(), false);
359 mBuffers.emplace(handle, std::move(buffer));
360 return GRALLOC1_ERROR_NONE;
361}
362
363gralloc1_error_t Gralloc1On0Adapter::lock(
364 const std::shared_ptr<Buffer>& buffer,
365 gralloc1_producer_usage_t producerUsage,
366 gralloc1_consumer_usage_t consumerUsage,
367 const gralloc1_rect_t& accessRegion, void** outData,
368 const sp<Fence>& acquireFence)
369{
370 if (mMinorVersion >= 3) {
371 int result = mModule->lockAsync(mModule, buffer->getHandle(),
372 static_cast<int32_t>(producerUsage | consumerUsage),
373 accessRegion.left, accessRegion.top, accessRegion.width,
374 accessRegion.height, outData, acquireFence->dup());
375 if (result != 0) {
376 return GRALLOC1_ERROR_UNSUPPORTED;
377 }
378 } else {
379 acquireFence->waitForever("Gralloc1On0Adapter::lock");
380 int result = mModule->lock(mModule, buffer->getHandle(),
381 static_cast<int32_t>(producerUsage | consumerUsage),
382 accessRegion.left, accessRegion.top, accessRegion.width,
383 accessRegion.height, outData);
384 ALOGV("gralloc0 lock returned %d", result);
385 if (result != 0) {
386 return GRALLOC1_ERROR_UNSUPPORTED;
387 }
388 }
389 return GRALLOC1_ERROR_NONE;
390}
391
392gralloc1_error_t Gralloc1On0Adapter::lockFlex(
393 const std::shared_ptr<Buffer>& /*buffer*/,
394 gralloc1_producer_usage_t /*producerUsage*/,
395 gralloc1_consumer_usage_t /*consumerUsage*/,
396 const gralloc1_rect_t& /*accessRegion*/,
397 struct android_flex_layout* /*outData*/,
398 const sp<Fence>& /*acquireFence*/)
399{
400 // TODO
401 return GRALLOC1_ERROR_UNSUPPORTED;
402}
403
404gralloc1_error_t Gralloc1On0Adapter::lockYCbCr(
405 const std::shared_ptr<Buffer>& buffer,
406 gralloc1_producer_usage_t producerUsage,
407 gralloc1_consumer_usage_t consumerUsage,
408 const gralloc1_rect_t& accessRegion, struct android_ycbcr* outData,
409 const sp<Fence>& acquireFence)
410{
411 if (mMinorVersion >= 3 && mModule->lockAsync_ycbcr) {
412 int result = mModule->lockAsync_ycbcr(mModule, buffer->getHandle(),
413 static_cast<int>(producerUsage | consumerUsage),
414 accessRegion.left, accessRegion.top, accessRegion.width,
415 accessRegion.height, outData, acquireFence->dup());
416 if (result != 0) {
417 return GRALLOC1_ERROR_UNSUPPORTED;
418 }
419 } else if (mModule->lock_ycbcr) {
420 acquireFence->waitForever("Gralloc1On0Adapter::lockYCbCr");
421 int result = mModule->lock_ycbcr(mModule, buffer->getHandle(),
422 static_cast<int>(producerUsage | consumerUsage),
423 accessRegion.left, accessRegion.top, accessRegion.width,
424 accessRegion.height, outData);
425 ALOGV("gralloc0 lockYCbCr returned %d", result);
426 if (result != 0) {
427 return GRALLOC1_ERROR_UNSUPPORTED;
428 }
429 } else {
430 return GRALLOC1_ERROR_UNSUPPORTED;
431 }
432
433 return GRALLOC1_ERROR_NONE;
434}
435
436gralloc1_error_t Gralloc1On0Adapter::unlock(
437 const std::shared_ptr<Buffer>& buffer,
438 sp<Fence>* outReleaseFence)
439{
440 if (mMinorVersion >= 3) {
441 int fenceFd = -1;
442 int result = mModule->unlockAsync(mModule, buffer->getHandle(),
443 &fenceFd);
444 if (result != 0) {
445 close(fenceFd);
446 ALOGE("gralloc0 unlockAsync failed: %d", result);
447 } else {
448 *outReleaseFence = new Fence(fenceFd);
449 }
450 } else {
451 int result = mModule->unlock(mModule, buffer->getHandle());
452 if (result != 0) {
453 ALOGE("gralloc0 unlock failed: %d", result);
454 }
455 }
456 return GRALLOC1_ERROR_NONE;
457}
458
459std::shared_ptr<Gralloc1On0Adapter::Descriptor>
460Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId)
461{
Dan Stoza923c0662016-06-21 16:22:06 -0700462 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800463 if (mDescriptors.count(descriptorId) == 0) {
464 return nullptr;
465 }
466
467 return mDescriptors[descriptorId];
468}
469
470std::shared_ptr<Gralloc1On0Adapter::Buffer> Gralloc1On0Adapter::getBuffer(
471 buffer_handle_t bufferHandle)
472{
Dan Stoza923c0662016-06-21 16:22:06 -0700473 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800474 if (mBuffers.count(bufferHandle) == 0) {
475 return nullptr;
476 }
477
478 return mBuffers[bufferHandle];
479}
480
481std::atomic<gralloc1_buffer_descriptor_t>
482 Gralloc1On0Adapter::sNextBufferDescriptorId(1);
483
484} // namespace android