blob: ec7df31f9c2e4f2b37edc744534f0c6aa4cf6665 [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++;
Dan Stoza923c0662016-06-21 16:22:06 -0700196 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800197 mDescriptors.emplace(descriptorId,
198 std::make_shared<Descriptor>(this, descriptorId));
199
200 ALOGV("Created descriptor %" PRIu64, descriptorId);
201
202 *outDescriptor = descriptorId;
203 return GRALLOC1_ERROR_NONE;
204}
205
206gralloc1_error_t Gralloc1On0Adapter::destroyDescriptor(
207 gralloc1_buffer_descriptor_t descriptor)
208{
209 ALOGV("Destroying descriptor %" PRIu64, descriptor);
210
Dan Stoza923c0662016-06-21 16:22:06 -0700211 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800212 if (mDescriptors.count(descriptor) == 0) {
213 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
214 }
215
216 mDescriptors.erase(descriptor);
217 return GRALLOC1_ERROR_NONE;
218}
219
220Gralloc1On0Adapter::Buffer::Buffer(buffer_handle_t handle,
221 gralloc1_backing_store_t store, const Descriptor& descriptor,
222 uint32_t stride, bool wasAllocated)
223 : mHandle(handle),
224 mReferenceCount(1),
225 mStore(store),
226 mDescriptor(descriptor),
227 mStride(stride),
228 mWasAllocated(wasAllocated) {}
229
230gralloc1_error_t Gralloc1On0Adapter::allocate(
231 const std::shared_ptr<Descriptor>& descriptor,
232 gralloc1_backing_store_t store,
233 buffer_handle_t* outBufferHandle)
234{
235 ALOGV("allocate(%" PRIu64 ", %#" PRIx64 ")", descriptor->id, store);
236
237 // If this function is being called, it's because we handed out its function
238 // pointer, which only occurs when mDevice has been loaded successfully and
239 // we are permitted to allocate
240
241 int usage = static_cast<int>(descriptor->producerUsage) |
242 static_cast<int>(descriptor->consumerUsage);
243 buffer_handle_t handle = nullptr;
244 int stride = 0;
245 ALOGV("Calling alloc(%p, %u, %u, %i, %u)", mDevice, descriptor->width,
246 descriptor->height, descriptor->format, usage);
247 auto error = mDevice->alloc(mDevice,
248 static_cast<int>(descriptor->width),
249 static_cast<int>(descriptor->height), descriptor->format,
250 usage, &handle, &stride);
251 if (error != 0) {
252 ALOGE("gralloc0 allocation failed: %d (%s)", error,
253 strerror(-error));
254 return GRALLOC1_ERROR_NO_RESOURCES;
255 }
256
257 *outBufferHandle = handle;
258 auto buffer = std::make_shared<Buffer>(handle, store, *descriptor, stride,
259 true);
Dan Stoza923c0662016-06-21 16:22:06 -0700260
261 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800262 mBuffers.emplace(handle, std::move(buffer));
263
264 return GRALLOC1_ERROR_NONE;
265}
266
267gralloc1_error_t Gralloc1On0Adapter::allocateWithIdHook(
268 gralloc1_device_t* device, gralloc1_buffer_descriptor_t descriptorId,
269 gralloc1_backing_store_t store, buffer_handle_t* outBuffer)
270{
271 auto adapter = getAdapter(device);
272
273 auto descriptor = adapter->getDescriptor(descriptorId);
274 if (!descriptor) {
275 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
276 }
277
278 buffer_handle_t bufferHandle = nullptr;
279 auto error = adapter->allocate(descriptor, store, &bufferHandle);
280 if (error != GRALLOC1_ERROR_NONE) {
281 return error;
282 }
283
284 *outBuffer = bufferHandle;
285 return error;
286}
287
288gralloc1_error_t Gralloc1On0Adapter::retain(
289 const std::shared_ptr<Buffer>& buffer)
290{
Ajit Kumar56ec3af2016-11-25 12:08:53 +0530291 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800292 buffer->retain();
293 return GRALLOC1_ERROR_NONE;
294}
295
296gralloc1_error_t Gralloc1On0Adapter::release(
297 const std::shared_ptr<Buffer>& buffer)
298{
Ajit Kumar56ec3af2016-11-25 12:08:53 +0530299 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800300 if (!buffer->release()) {
301 return GRALLOC1_ERROR_NONE;
302 }
303
304 buffer_handle_t handle = buffer->getHandle();
305 if (buffer->wasAllocated()) {
306 ALOGV("Calling free(%p)", handle);
307 int result = mDevice->free(mDevice, handle);
308 if (result != 0) {
309 ALOGE("gralloc0 free failed: %d", result);
310 }
311 } else {
312 ALOGV("Calling unregisterBuffer(%p)", handle);
313 int result = mModule->unregisterBuffer(mModule, handle);
314 if (result != 0) {
315 ALOGE("gralloc0 unregister failed: %d", result);
316 }
317 }
Dan Stoza923c0662016-06-21 16:22:06 -0700318
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800319 mBuffers.erase(handle);
320 return GRALLOC1_ERROR_NONE;
321}
322
323gralloc1_error_t Gralloc1On0Adapter::retain(
324 const android::GraphicBuffer* graphicBuffer)
325{
326 ALOGV("retainGraphicBuffer(%p, %#" PRIx64 ")",
327 graphicBuffer->getNativeBuffer()->handle, graphicBuffer->getId());
328
329 buffer_handle_t handle = graphicBuffer->getNativeBuffer()->handle;
Dan Stoza923c0662016-06-21 16:22:06 -0700330 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800331 if (mBuffers.count(handle) != 0) {
332 mBuffers[handle]->retain();
333 return GRALLOC1_ERROR_NONE;
334 }
335
336 ALOGV("Calling registerBuffer(%p)", handle);
337 int result = mModule->registerBuffer(mModule, handle);
338 if (result != 0) {
339 ALOGE("gralloc0 register failed: %d", result);
340 return GRALLOC1_ERROR_NO_RESOURCES;
341 }
342
343 Descriptor descriptor{this, sNextBufferDescriptorId++};
344 descriptor.setDimensions(graphicBuffer->getWidth(),
345 graphicBuffer->getHeight());
346 descriptor.setFormat(graphicBuffer->getPixelFormat());
347 descriptor.setProducerUsage(
348 static_cast<gralloc1_producer_usage_t>(graphicBuffer->getUsage()));
349 descriptor.setConsumerUsage(
350 static_cast<gralloc1_consumer_usage_t>(graphicBuffer->getUsage()));
351 auto buffer = std::make_shared<Buffer>(handle,
352 static_cast<gralloc1_backing_store_t>(graphicBuffer->getId()),
353 descriptor, graphicBuffer->getStride(), false);
354 mBuffers.emplace(handle, std::move(buffer));
355 return GRALLOC1_ERROR_NONE;
356}
357
358gralloc1_error_t Gralloc1On0Adapter::lock(
359 const std::shared_ptr<Buffer>& buffer,
360 gralloc1_producer_usage_t producerUsage,
361 gralloc1_consumer_usage_t consumerUsage,
362 const gralloc1_rect_t& accessRegion, void** outData,
363 const sp<Fence>& acquireFence)
364{
365 if (mMinorVersion >= 3) {
366 int result = mModule->lockAsync(mModule, buffer->getHandle(),
367 static_cast<int32_t>(producerUsage | consumerUsage),
368 accessRegion.left, accessRegion.top, accessRegion.width,
369 accessRegion.height, outData, acquireFence->dup());
370 if (result != 0) {
371 return GRALLOC1_ERROR_UNSUPPORTED;
372 }
373 } else {
374 acquireFence->waitForever("Gralloc1On0Adapter::lock");
375 int result = mModule->lock(mModule, buffer->getHandle(),
376 static_cast<int32_t>(producerUsage | consumerUsage),
377 accessRegion.left, accessRegion.top, accessRegion.width,
378 accessRegion.height, outData);
379 ALOGV("gralloc0 lock returned %d", result);
380 if (result != 0) {
381 return GRALLOC1_ERROR_UNSUPPORTED;
382 }
383 }
384 return GRALLOC1_ERROR_NONE;
385}
386
387gralloc1_error_t Gralloc1On0Adapter::lockFlex(
388 const std::shared_ptr<Buffer>& /*buffer*/,
389 gralloc1_producer_usage_t /*producerUsage*/,
390 gralloc1_consumer_usage_t /*consumerUsage*/,
391 const gralloc1_rect_t& /*accessRegion*/,
392 struct android_flex_layout* /*outData*/,
393 const sp<Fence>& /*acquireFence*/)
394{
395 // TODO
396 return GRALLOC1_ERROR_UNSUPPORTED;
397}
398
399gralloc1_error_t Gralloc1On0Adapter::lockYCbCr(
400 const std::shared_ptr<Buffer>& buffer,
401 gralloc1_producer_usage_t producerUsage,
402 gralloc1_consumer_usage_t consumerUsage,
403 const gralloc1_rect_t& accessRegion, struct android_ycbcr* outData,
404 const sp<Fence>& acquireFence)
405{
406 if (mMinorVersion >= 3 && mModule->lockAsync_ycbcr) {
407 int result = mModule->lockAsync_ycbcr(mModule, buffer->getHandle(),
408 static_cast<int>(producerUsage | consumerUsage),
409 accessRegion.left, accessRegion.top, accessRegion.width,
410 accessRegion.height, outData, acquireFence->dup());
411 if (result != 0) {
412 return GRALLOC1_ERROR_UNSUPPORTED;
413 }
414 } else if (mModule->lock_ycbcr) {
415 acquireFence->waitForever("Gralloc1On0Adapter::lockYCbCr");
416 int result = mModule->lock_ycbcr(mModule, buffer->getHandle(),
417 static_cast<int>(producerUsage | consumerUsage),
418 accessRegion.left, accessRegion.top, accessRegion.width,
419 accessRegion.height, outData);
420 ALOGV("gralloc0 lockYCbCr returned %d", result);
421 if (result != 0) {
422 return GRALLOC1_ERROR_UNSUPPORTED;
423 }
424 } else {
425 return GRALLOC1_ERROR_UNSUPPORTED;
426 }
427
428 return GRALLOC1_ERROR_NONE;
429}
430
431gralloc1_error_t Gralloc1On0Adapter::unlock(
432 const std::shared_ptr<Buffer>& buffer,
433 sp<Fence>* outReleaseFence)
434{
435 if (mMinorVersion >= 3) {
436 int fenceFd = -1;
437 int result = mModule->unlockAsync(mModule, buffer->getHandle(),
438 &fenceFd);
439 if (result != 0) {
440 close(fenceFd);
441 ALOGE("gralloc0 unlockAsync failed: %d", result);
442 } else {
443 *outReleaseFence = new Fence(fenceFd);
444 }
445 } else {
446 int result = mModule->unlock(mModule, buffer->getHandle());
447 if (result != 0) {
448 ALOGE("gralloc0 unlock failed: %d", result);
449 }
450 }
451 return GRALLOC1_ERROR_NONE;
452}
453
454std::shared_ptr<Gralloc1On0Adapter::Descriptor>
455Gralloc1On0Adapter::getDescriptor(gralloc1_buffer_descriptor_t descriptorId)
456{
Dan Stoza923c0662016-06-21 16:22:06 -0700457 std::lock_guard<std::mutex> lock(mDescriptorMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800458 if (mDescriptors.count(descriptorId) == 0) {
459 return nullptr;
460 }
461
462 return mDescriptors[descriptorId];
463}
464
465std::shared_ptr<Gralloc1On0Adapter::Buffer> Gralloc1On0Adapter::getBuffer(
466 buffer_handle_t bufferHandle)
467{
Dan Stoza923c0662016-06-21 16:22:06 -0700468 std::lock_guard<std::mutex> lock(mBufferMutex);
Dan Stoza1e2a2a02016-01-11 15:21:07 -0800469 if (mBuffers.count(bufferHandle) == 0) {
470 return nullptr;
471 }
472
473 return mBuffers[bufferHandle];
474}
475
476std::atomic<gralloc1_buffer_descriptor_t>
477 Gralloc1On0Adapter::sNextBufferDescriptorId(1);
478
479} // namespace android