From 699df2167a9749ae3f091b7f5ed2e343251afbcc Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Fri, 15 Dec 2017 14:58:58 -0800 Subject: [PATCH] graphics: use allocator HAL support library in default impl Rename Gralloc0Allocator to Gralloc0Hal and make it inherit from AllocatorHal. Do the same to Gralloc1Allocator. Add GrallocLoader to load either of Gralloc[01]Hal and create a IAllocator instance. Test: boots and VTS Change-Id: I09ae680c0086ca9e73e412a34d7cd2f3665d3bc2 --- graphics/allocator/2.0/default/Android.bp | 5 +- graphics/allocator/2.0/default/Gralloc.cpp | 57 ------- .../default/{Gralloc.h => passthrough.cpp} | 25 +-- .../2.0/utils/passthrough/Android.bp | 12 +- .../2.0/utils/passthrough/Gralloc0Allocator.h | 59 ------- ...{Gralloc0Allocator.cpp => Gralloc0Hal.cpp} | 129 +++++++------- ...{Gralloc1Allocator.cpp => Gralloc1Hal.cpp} | 158 ++++++++++-------- .../2.0/utils/passthrough/GrallocLoader.cpp | 76 +++++++++ .../allocator-passthrough/2.0/Gralloc0Hal.h | 58 +++++++ .../allocator-passthrough/2.0/Gralloc1Hal.h} | 63 +++---- .../allocator-passthrough/2.0/GrallocLoader.h | 64 +++++++ 11 files changed, 401 insertions(+), 305 deletions(-) delete mode 100644 graphics/allocator/2.0/default/Gralloc.cpp rename graphics/allocator/2.0/default/{Gralloc.h => passthrough.cpp} (56%) delete mode 100644 graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.h rename graphics/allocator/2.0/utils/passthrough/{Gralloc0Allocator.cpp => Gralloc0Hal.cpp} (50%) rename graphics/allocator/2.0/utils/passthrough/{Gralloc1Allocator.cpp => Gralloc1Hal.cpp} (65%) create mode 100644 graphics/allocator/2.0/utils/passthrough/GrallocLoader.cpp create mode 100644 graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc0Hal.h rename graphics/allocator/2.0/utils/passthrough/{Gralloc1Allocator.h => include/allocator-passthrough/2.0/Gralloc1Hal.h} (55%) create mode 100644 graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/GrallocLoader.h diff --git a/graphics/allocator/2.0/default/Android.bp b/graphics/allocator/2.0/default/Android.bp index f100d96030..ed2ecbb508 100644 --- a/graphics/allocator/2.0/default/Android.bp +++ b/graphics/allocator/2.0/default/Android.bp @@ -3,10 +3,13 @@ cc_library_shared { defaults: ["hidl_defaults"], vendor: true, relative_install_path: "hw", - srcs: ["Gralloc.cpp"], + srcs: ["passthrough.cpp"], static_libs: [ "android.hardware.graphics.allocator@2.0-passthrough", ], + header_libs: [ + "android.hardware.graphics.allocator@2.0-hal", + ], shared_libs: [ "android.hardware.graphics.allocator@2.0", "libbase", diff --git a/graphics/allocator/2.0/default/Gralloc.cpp b/graphics/allocator/2.0/default/Gralloc.cpp deleted file mode 100644 index af578e8e19..0000000000 --- a/graphics/allocator/2.0/default/Gralloc.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "GrallocPassthrough" - -#include "Gralloc.h" -#include "Gralloc0Allocator.h" -#include "Gralloc1Allocator.h" - -#include - -namespace android { -namespace hardware { -namespace graphics { -namespace allocator { -namespace V2_0 { -namespace implementation { - -IAllocator* HIDL_FETCH_IAllocator(const char* /* name */) { - const hw_module_t* module = nullptr; - int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); - if (err) { - ALOGE("failed to get gralloc module"); - return nullptr; - } - - uint8_t major = (module->module_api_version >> 8) & 0xff; - switch (major) { - case 1: - return new Gralloc1Allocator(module); - case 0: - return new Gralloc0Allocator(module); - default: - ALOGE("unknown gralloc module major version %d", major); - return nullptr; - } -} - -} // namespace implementation -} // namespace V2_0 -} // namespace allocator -} // namespace graphics -} // namespace hardware -} // namespace android diff --git a/graphics/allocator/2.0/default/Gralloc.h b/graphics/allocator/2.0/default/passthrough.cpp similarity index 56% rename from graphics/allocator/2.0/default/Gralloc.h rename to graphics/allocator/2.0/default/passthrough.cpp index 18c52023e0..132cab3c60 100644 --- a/graphics/allocator/2.0/default/Gralloc.h +++ b/graphics/allocator/2.0/default/passthrough.cpp @@ -14,25 +14,12 @@ * limitations under the License. */ -#ifndef ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC_H -#define ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC_H - +#include #include -namespace android { -namespace hardware { -namespace graphics { -namespace allocator { -namespace V2_0 { -namespace implementation { +using android::hardware::graphics::allocator::V2_0::IAllocator; +using android::hardware::graphics::allocator::V2_0::passthrough::GrallocLoader; -extern "C" IAllocator* HIDL_FETCH_IAllocator(const char* name); - -} // namespace implementation -} // namespace V2_0 -} // namespace allocator -} // namespace graphics -} // namespace hardware -} // namespace android - -#endif // ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC_H +extern "C" IAllocator* HIDL_FETCH_IAllocator(const char* /* name */) { + return GrallocLoader::load(); +} diff --git a/graphics/allocator/2.0/utils/passthrough/Android.bp b/graphics/allocator/2.0/utils/passthrough/Android.bp index fe7096188b..b95656572d 100644 --- a/graphics/allocator/2.0/utils/passthrough/Android.bp +++ b/graphics/allocator/2.0/utils/passthrough/Android.bp @@ -3,8 +3,9 @@ cc_library_static { defaults: ["hidl_defaults"], vendor: true, srcs: [ - "Gralloc0Allocator.cpp", - "Gralloc1Allocator.cpp", + "Gralloc0Hal.cpp", + "Gralloc1Hal.cpp", + "GrallocLoader.cpp", ], shared_libs: [ "android.hardware.graphics.allocator@2.0", @@ -16,8 +17,13 @@ cc_library_static { "android.hardware.graphics.mapper@2.0", "libhardware", ], - export_include_dirs: ["."], header_libs: [ + "android.hardware.graphics.allocator@2.0-hal", "libgrallocmapperincludes", ], + export_header_lib_headers: [ + "android.hardware.graphics.allocator@2.0-hal", + ], + export_include_dirs: ["include"], + cflags: ["-DLOG_TAG=\"AllocatorHal\""], } diff --git a/graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.h b/graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.h deleted file mode 100644 index 6052282048..0000000000 --- a/graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC0ALLOCATOR_H -#define ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC0ALLOCATOR_H - -#include -#include -#include - -namespace android { -namespace hardware { -namespace graphics { -namespace allocator { -namespace V2_0 { -namespace implementation { - -using android::hardware::graphics::mapper::V2_0::BufferDescriptor; -using android::hardware::graphics::mapper::V2_0::Error; -using android::hardware::graphics::mapper::V2_0::IMapper; - -class Gralloc0Allocator : public IAllocator { - public: - Gralloc0Allocator(const hw_module_t* module); - virtual ~Gralloc0Allocator(); - - // IAllocator interface - Return dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override; - Return allocate(const BufferDescriptor& descriptor, uint32_t count, - allocate_cb hidl_cb) override; - - private: - Error allocateOne(const IMapper::BufferDescriptorInfo& info, buffer_handle_t* outBuffer, - uint32_t* outStride); - - alloc_device_t* mDevice; -}; - -} // namespace implementation -} // namespace V2_0 -} // namespace allocator -} // namespace graphics -} // namespace hardware -} // namespace android - -#endif // ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC0ALLOCATOR_H diff --git a/graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.cpp b/graphics/allocator/2.0/utils/passthrough/Gralloc0Hal.cpp similarity index 50% rename from graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.cpp rename to graphics/allocator/2.0/utils/passthrough/Gralloc0Hal.cpp index b49370a393..8edb7dc500 100644 --- a/graphics/allocator/2.0/utils/passthrough/Gralloc0Allocator.cpp +++ b/graphics/allocator/2.0/utils/passthrough/Gralloc0Hal.cpp @@ -14,15 +14,12 @@ * limitations under the License. */ -#define LOG_TAG "Gralloc0Allocator" - -#include "Gralloc0Allocator.h" -#include "GrallocBufferDescriptor.h" - -#include +#include #include +#include +#include #include namespace android { @@ -30,109 +27,113 @@ namespace hardware { namespace graphics { namespace allocator { namespace V2_0 { -namespace implementation { +namespace passthrough { -using android::hardware::graphics::mapper::V2_0::implementation::grallocDecodeBufferDescriptor; +using mapper::V2_0::implementation::grallocDecodeBufferDescriptor; -Gralloc0Allocator::Gralloc0Allocator(const hw_module_t* module) { - int result = gralloc_open(module, &mDevice); - if (result) { - LOG_ALWAYS_FATAL("failed to open gralloc0 device: %s", strerror(-result)); +Gralloc0Hal::~Gralloc0Hal() { + if (mDevice) { + gralloc_close(mDevice); } } -Gralloc0Allocator::~Gralloc0Allocator() { - gralloc_close(mDevice); +bool Gralloc0Hal::initWithModule(const hw_module_t* module) { + int result = gralloc_open(module, &mDevice); + if (result) { + ALOGE("failed to open gralloc0 device: %s", strerror(-result)); + mDevice = nullptr; + return false; + } + + return true; } -Return Gralloc0Allocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) { +std::string Gralloc0Hal::dumpDebugInfo() { char buf[4096] = {}; if (mDevice->dump) { mDevice->dump(mDevice, buf, sizeof(buf)); buf[sizeof(buf) - 1] = '\0'; } - hidl_cb(hidl_string(buf)); - - return Void(); + return buf; } -Return Gralloc0Allocator::allocate(const BufferDescriptor& descriptor, uint32_t count, - allocate_cb hidl_cb) { - IMapper::BufferDescriptorInfo descriptorInfo; +Error Gralloc0Hal::allocateBuffers(const BufferDescriptor& descriptor, uint32_t count, + uint32_t* outStride, + std::vector* outBuffers) { + mapper::V2_0::IMapper::BufferDescriptorInfo descriptorInfo; if (!grallocDecodeBufferDescriptor(descriptor, &descriptorInfo)) { - hidl_cb(Error::BAD_DESCRIPTOR, 0, hidl_vec()); - return Void(); + return Error::BAD_DESCRIPTOR; } Error error = Error::NONE; uint32_t stride = 0; - std::vector buffers; + std::vector buffers; buffers.reserve(count); // allocate the buffers for (uint32_t i = 0; i < count; i++) { - buffer_handle_t tmpBuffer; + const native_handle_t* tmpBuffer; uint32_t tmpStride; - error = allocateOne(descriptorInfo, &tmpBuffer, &tmpStride); + error = allocateOneBuffer(descriptorInfo, &tmpBuffer, &tmpStride); if (error != Error::NONE) { break; } + buffers.push_back(tmpBuffer); + if (stride == 0) { stride = tmpStride; } else if (stride != tmpStride) { // non-uniform strides - mDevice->free(mDevice, tmpBuffer); - stride = 0; error = Error::UNSUPPORTED; break; } - - buffers.emplace_back(hidl_handle(tmpBuffer)); } - // return the buffers - hidl_vec hidl_buffers; - if (error == Error::NONE) { - hidl_buffers.setToExternal(buffers.data(), buffers.size()); - } - hidl_cb(error, stride, hidl_buffers); - - // free the buffers - for (const auto& buffer : buffers) { - mDevice->free(mDevice, buffer.getNativeHandle()); + if (error != Error::NONE) { + freeBuffers(buffers); + return error; } - return Void(); -} - -Error Gralloc0Allocator::allocateOne(const IMapper::BufferDescriptorInfo& info, - buffer_handle_t* outBuffer, uint32_t* outStride) { - if (info.layerCount > 1 || (info.usage >> 32) != 0) { - return Error::BAD_VALUE; - } - - buffer_handle_t buffer = nullptr; - int stride = 0; - int result = mDevice->alloc(mDevice, info.width, info.height, static_cast(info.format), - info.usage, &buffer, &stride); - if (result) { - switch (result) { - case -EINVAL: - return Error::BAD_VALUE; - default: - return Error::NO_RESOURCES; - } - } - - *outBuffer = buffer; *outStride = stride; + *outBuffers = std::move(buffers); return Error::NONE; } -} // namespace implementation +void Gralloc0Hal::freeBuffers(const std::vector& buffers) { + for (auto buffer : buffers) { + int result = mDevice->free(mDevice, buffer); + if (result != 0) { + ALOGE("failed to free buffer %p: %d", buffer, result); + } + } +} + +Error Gralloc0Hal::allocateOneBuffer(const mapper::V2_0::IMapper::BufferDescriptorInfo& info, + const native_handle_t** outBuffer, uint32_t* outStride) { + if (info.layerCount > 1 || (info.usage >> 32) != 0) { + return Error::BAD_VALUE; + } + + const native_handle_t* buffer = nullptr; + int stride = 0; + int result = mDevice->alloc(mDevice, info.width, info.height, static_cast(info.format), + info.usage, &buffer, &stride); + switch (result) { + case 0: + *outBuffer = buffer; + *outStride = stride; + return Error::NONE; + case -EINVAL: + return Error::BAD_VALUE; + default: + return Error::NO_RESOURCES; + } +} + +} // namespace passthrough } // namespace V2_0 } // namespace allocator } // namespace graphics diff --git a/graphics/allocator/2.0/utils/passthrough/Gralloc1Allocator.cpp b/graphics/allocator/2.0/utils/passthrough/Gralloc1Hal.cpp similarity index 65% rename from graphics/allocator/2.0/utils/passthrough/Gralloc1Allocator.cpp rename to graphics/allocator/2.0/utils/passthrough/Gralloc1Hal.cpp index b7e27bb015..e343ecd294 100644 --- a/graphics/allocator/2.0/utils/passthrough/Gralloc1Allocator.cpp +++ b/graphics/allocator/2.0/utils/passthrough/Gralloc1Hal.cpp @@ -14,15 +14,11 @@ * limitations under the License. */ -#define LOG_TAG "Gralloc1Allocator" - -#include "Gralloc1Allocator.h" -#include "GrallocBufferDescriptor.h" - -#include +#include #include +#include #include namespace android { @@ -30,27 +26,36 @@ namespace hardware { namespace graphics { namespace allocator { namespace V2_0 { -namespace implementation { +namespace passthrough { using android::hardware::graphics::common::V1_0::BufferUsage; -using android::hardware::graphics::mapper::V2_0::implementation::grallocDecodeBufferDescriptor; +using mapper::V2_0::implementation::grallocDecodeBufferDescriptor; -Gralloc1Allocator::Gralloc1Allocator(const hw_module_t* module) - : mDevice(nullptr), mCapabilities(), mDispatch() { +Gralloc1Hal::~Gralloc1Hal() { + if (mDevice) { + gralloc1_close(mDevice); + } +} + +bool Gralloc1Hal::initWithModule(const hw_module_t* module) { int result = gralloc1_open(module, &mDevice); if (result) { - LOG_ALWAYS_FATAL("failed to open gralloc1 device: %s", strerror(-result)); + ALOGE("failed to open gralloc1 device: %s", strerror(-result)); + mDevice = nullptr; + return false; } initCapabilities(); - initDispatch(); + if (!initDispatch()) { + gralloc1_close(mDevice); + mDevice = nullptr; + return false; + } + + return true; } -Gralloc1Allocator::~Gralloc1Allocator() { - gralloc1_close(mDevice); -} - -void Gralloc1Allocator::initCapabilities() { +void Gralloc1Hal::initCapabilities() { uint32_t count = 0; mDevice->getCapabilities(mDevice, &count, nullptr); @@ -66,33 +71,40 @@ void Gralloc1Allocator::initCapabilities() { } } -template -void Gralloc1Allocator::initDispatch(gralloc1_function_descriptor_t desc, T* outPfn) { +gralloc1_function_pointer_t Gralloc1Hal::getDispatchFunction( + gralloc1_function_descriptor_t desc) const { auto pfn = mDevice->getFunction(mDevice, desc); if (!pfn) { - LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc); + ALOGE("failed to get gralloc1 function %d", desc); + return nullptr; } - - *outPfn = reinterpret_cast(pfn); + return pfn; } -void Gralloc1Allocator::initDispatch() { - initDispatch(GRALLOC1_FUNCTION_DUMP, &mDispatch.dump); - initDispatch(GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, &mDispatch.createDescriptor); - initDispatch(GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, &mDispatch.destroyDescriptor); - initDispatch(GRALLOC1_FUNCTION_SET_DIMENSIONS, &mDispatch.setDimensions); - initDispatch(GRALLOC1_FUNCTION_SET_FORMAT, &mDispatch.setFormat); +bool Gralloc1Hal::initDispatch() { + if (!initDispatchFunction(GRALLOC1_FUNCTION_DUMP, &mDispatch.dump) || + !initDispatchFunction(GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, &mDispatch.createDescriptor) || + !initDispatchFunction(GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, &mDispatch.destroyDescriptor) || + !initDispatchFunction(GRALLOC1_FUNCTION_SET_DIMENSIONS, &mDispatch.setDimensions) || + !initDispatchFunction(GRALLOC1_FUNCTION_SET_FORMAT, &mDispatch.setFormat) || + !initDispatchFunction(GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, &mDispatch.setConsumerUsage) || + !initDispatchFunction(GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, &mDispatch.setProducerUsage) || + !initDispatchFunction(GRALLOC1_FUNCTION_GET_STRIDE, &mDispatch.getStride) || + !initDispatchFunction(GRALLOC1_FUNCTION_ALLOCATE, &mDispatch.allocate) || + !initDispatchFunction(GRALLOC1_FUNCTION_RELEASE, &mDispatch.release)) { + return false; + } + if (mCapabilities.layeredBuffers) { - initDispatch(GRALLOC1_FUNCTION_SET_LAYER_COUNT, &mDispatch.setLayerCount); + if (!initDispatchFunction(GRALLOC1_FUNCTION_SET_LAYER_COUNT, &mDispatch.setLayerCount)) { + return false; + } } - initDispatch(GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, &mDispatch.setConsumerUsage); - initDispatch(GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, &mDispatch.setProducerUsage); - initDispatch(GRALLOC1_FUNCTION_GET_STRIDE, &mDispatch.getStride); - initDispatch(GRALLOC1_FUNCTION_ALLOCATE, &mDispatch.allocate); - initDispatch(GRALLOC1_FUNCTION_RELEASE, &mDispatch.release); + + return true; } -Return Gralloc1Allocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) { +std::string Gralloc1Hal::dumpDebugInfo() { uint32_t len = 0; mDispatch.dump(mDevice, &len, nullptr); @@ -101,72 +113,70 @@ Return Gralloc1Allocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) { buf.resize(len + 1); buf[len] = '\0'; - hidl_string reply; - reply.setToExternal(buf.data(), len); - hidl_cb(reply); - - return Void(); + return buf.data(); } -Return Gralloc1Allocator::allocate(const BufferDescriptor& descriptor, uint32_t count, - allocate_cb hidl_cb) { - IMapper::BufferDescriptorInfo descriptorInfo; +Error Gralloc1Hal::allocateBuffers(const BufferDescriptor& descriptor, uint32_t count, + uint32_t* outStride, + std::vector* outBuffers) { + mapper::V2_0::IMapper::BufferDescriptorInfo descriptorInfo; if (!grallocDecodeBufferDescriptor(descriptor, &descriptorInfo)) { - hidl_cb(Error::BAD_DESCRIPTOR, 0, hidl_vec()); - return Void(); + return Error::BAD_DESCRIPTOR; } gralloc1_buffer_descriptor_t desc; Error error = createDescriptor(descriptorInfo, &desc); if (error != Error::NONE) { - hidl_cb(error, 0, hidl_vec()); - return Void(); + return error; } uint32_t stride = 0; - std::vector buffers; + std::vector buffers; buffers.reserve(count); // allocate the buffers for (uint32_t i = 0; i < count; i++) { - buffer_handle_t tmpBuffer; + const native_handle_t* tmpBuffer; uint32_t tmpStride; - error = allocateOne(desc, &tmpBuffer, &tmpStride); + error = allocateOneBuffer(desc, &tmpBuffer, &tmpStride); if (error != Error::NONE) { break; } + buffers.push_back(tmpBuffer); + if (stride == 0) { stride = tmpStride; } else if (stride != tmpStride) { // non-uniform strides - mDispatch.release(mDevice, tmpBuffer); - stride = 0; error = Error::UNSUPPORTED; break; } - - buffers.emplace_back(hidl_handle(tmpBuffer)); } mDispatch.destroyDescriptor(mDevice, desc); - // return the buffers - hidl_vec hidl_buffers; - if (error == Error::NONE) { - hidl_buffers.setToExternal(buffers.data(), buffers.size()); - } - hidl_cb(error, stride, hidl_buffers); - - // free the buffers - for (const auto& buffer : buffers) { - mDispatch.release(mDevice, buffer.getNativeHandle()); + if (error != Error::NONE) { + freeBuffers(buffers); + return error; } - return Void(); + *outStride = stride; + *outBuffers = std::move(buffers); + + return Error::NONE; } -Error Gralloc1Allocator::toError(int32_t error) { +void Gralloc1Hal::freeBuffers(const std::vector& buffers) { + for (auto buffer : buffers) { + int32_t error = mDispatch.release(mDevice, buffer); + if (error != GRALLOC1_ERROR_NONE) { + ALOGE("failed to free buffer %p: %d", buffer, error); + } + } +} + +Error Gralloc1Hal::toError(int32_t error) { switch (error) { case GRALLOC1_ERROR_NONE: return Error::NONE; @@ -187,7 +197,7 @@ Error Gralloc1Allocator::toError(int32_t error) { } } -uint64_t Gralloc1Allocator::toProducerUsage(uint64_t usage) { +uint64_t Gralloc1Hal::toProducerUsage(uint64_t usage) { // this is potentially broken as we have no idea which private flags // should be filtered out uint64_t producerUsage = @@ -221,7 +231,7 @@ uint64_t Gralloc1Allocator::toProducerUsage(uint64_t usage) { return producerUsage; } -uint64_t Gralloc1Allocator::toConsumerUsage(uint64_t usage) { +uint64_t Gralloc1Hal::toConsumerUsage(uint64_t usage) { // this is potentially broken as we have no idea which private flags // should be filtered out uint64_t consumerUsage = @@ -249,8 +259,8 @@ uint64_t Gralloc1Allocator::toConsumerUsage(uint64_t usage) { return consumerUsage; } -Error Gralloc1Allocator::createDescriptor(const IMapper::BufferDescriptorInfo& info, - gralloc1_buffer_descriptor_t* outDescriptor) { +Error Gralloc1Hal::createDescriptor(const mapper::V2_0::IMapper::BufferDescriptorInfo& info, + gralloc1_buffer_descriptor_t* outDescriptor) { gralloc1_buffer_descriptor_t descriptor; int32_t error = mDispatch.createDescriptor(mDevice, &descriptor); @@ -284,9 +294,9 @@ Error Gralloc1Allocator::createDescriptor(const IMapper::BufferDescriptorInfo& i return toError(error); } -Error Gralloc1Allocator::allocateOne(gralloc1_buffer_descriptor_t descriptor, - buffer_handle_t* outBuffer, uint32_t* outStride) { - buffer_handle_t buffer = nullptr; +Error Gralloc1Hal::allocateOneBuffer(gralloc1_buffer_descriptor_t descriptor, + const native_handle_t** outBuffer, uint32_t* outStride) { + const native_handle_t* buffer = nullptr; int32_t error = mDispatch.allocate(mDevice, 1, &descriptor, &buffer); if (error != GRALLOC1_ERROR_NONE && error != GRALLOC1_ERROR_NOT_SHARED) { return toError(error); @@ -305,7 +315,7 @@ Error Gralloc1Allocator::allocateOne(gralloc1_buffer_descriptor_t descriptor, return Error::NONE; } -} // namespace implementation +} // namespace passthrough } // namespace V2_0 } // namespace allocator } // namespace graphics diff --git a/graphics/allocator/2.0/utils/passthrough/GrallocLoader.cpp b/graphics/allocator/2.0/utils/passthrough/GrallocLoader.cpp new file mode 100644 index 0000000000..dae7a78ca5 --- /dev/null +++ b/graphics/allocator/2.0/utils/passthrough/GrallocLoader.cpp @@ -0,0 +1,76 @@ +/* + * Copyright 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace android { +namespace hardware { +namespace graphics { +namespace allocator { +namespace V2_0 { +namespace passthrough { + +const hw_module_t* GrallocLoader::loadModule() { + const hw_module_t* module; + int error = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); + if (error) { + ALOGE("failed to get gralloc module"); + return nullptr; + } + + return module; +} + +int GrallocLoader::getModuleMajorApiVersion(const hw_module_t* module) { + return (module->module_api_version >> 8) & 0xff; +} + +std::unique_ptr GrallocLoader::createHal(const hw_module_t* module) { + int major = getModuleMajorApiVersion(module); + switch (major) { + case 1: { + auto hal = std::make_unique(); + return hal->initWithModule(module) ? std::move(hal) : nullptr; + } + case 0: { + auto hal = std::make_unique(); + return hal->initWithModule(module) ? std::move(hal) : nullptr; + } + default: + ALOGE("unknown gralloc module major version %d", major); + return nullptr; + } +} + +IAllocator* GrallocLoader::createAllocator(std::unique_ptr hal) { + auto allocator = std::make_unique(); + return allocator->init(std::move(hal)) ? allocator.release() : nullptr; +} + +} // namespace passthrough +} // namespace V2_0 +} // namespace allocator +} // namespace graphics +} // namespace hardware +} // namespace android diff --git a/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc0Hal.h b/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc0Hal.h new file mode 100644 index 0000000000..8f433b0f56 --- /dev/null +++ b/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc0Hal.h @@ -0,0 +1,58 @@ +/* + * Copyright 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +struct alloc_device_t; +struct hw_module_t; + +namespace android { +namespace hardware { +namespace graphics { +namespace allocator { +namespace V2_0 { +namespace passthrough { + +using mapper::V2_0::BufferDescriptor; +using mapper::V2_0::Error; + +class Gralloc0Hal : public virtual hal::AllocatorHal { + public: + ~Gralloc0Hal(); + bool initWithModule(const hw_module_t* module); + + std::string dumpDebugInfo() override; + + Error allocateBuffers(const BufferDescriptor& descriptor, uint32_t count, uint32_t* outStride, + std::vector* outBuffers) override; + + void freeBuffers(const std::vector& buffers) override; + + protected: + Error allocateOneBuffer(const mapper::V2_0::IMapper::BufferDescriptorInfo& info, + const native_handle_t** outBuffer, uint32_t* outStride); + + alloc_device_t* mDevice = nullptr; +}; + +} // namespace passthrough +} // namespace V2_0 +} // namespace allocator +} // namespace graphics +} // namespace hardware +} // namespace android diff --git a/graphics/allocator/2.0/utils/passthrough/Gralloc1Allocator.h b/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc1Hal.h similarity index 55% rename from graphics/allocator/2.0/utils/passthrough/Gralloc1Allocator.h rename to graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc1Hal.h index 686e8b35dd..3126e91506 100644 --- a/graphics/allocator/2.0/utils/passthrough/Gralloc1Allocator.h +++ b/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/Gralloc1Hal.h @@ -14,11 +14,9 @@ * limitations under the License. */ -#ifndef ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC1ALLOCATOR_H -#define ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC1ALLOCATOR_H +#pragma once -#include -#include +#include #include namespace android { @@ -26,43 +24,54 @@ namespace hardware { namespace graphics { namespace allocator { namespace V2_0 { -namespace implementation { +namespace passthrough { -using android::hardware::graphics::mapper::V2_0::BufferDescriptor; -using android::hardware::graphics::mapper::V2_0::Error; -using android::hardware::graphics::mapper::V2_0::IMapper; +using mapper::V2_0::BufferDescriptor; +using mapper::V2_0::Error; -class Gralloc1Allocator : public IAllocator { +class Gralloc1Hal : public virtual hal::AllocatorHal { public: - Gralloc1Allocator(const hw_module_t* module); - virtual ~Gralloc1Allocator(); + ~Gralloc1Hal(); + bool initWithModule(const hw_module_t* module); - // IAllocator interface - Return dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override; - Return allocate(const BufferDescriptor& descriptor, uint32_t count, - allocate_cb hidl_cb) override; + std::string dumpDebugInfo() override; - private: - void initCapabilities(); + Error allocateBuffers(const BufferDescriptor& descriptor, uint32_t count, uint32_t* outStride, + std::vector* outBuffers) override; + void freeBuffers(const std::vector& buffers) override; + + protected: template - void initDispatch(gralloc1_function_descriptor_t desc, T* outPfn); - void initDispatch(); + bool initDispatchFunction(gralloc1_function_descriptor_t desc, T* outPfn) { + auto pfn = getDispatchFunction(desc); + if (pfn) { + *outPfn = reinterpret_cast(pfn); + return true; + } else { + return false; + } + } + gralloc1_function_pointer_t getDispatchFunction(gralloc1_function_descriptor_t desc) const; + + virtual void initCapabilities(); + virtual bool initDispatch(); static Error toError(int32_t error); static uint64_t toProducerUsage(uint64_t usage); static uint64_t toConsumerUsage(uint64_t usage); - Error createDescriptor(const IMapper::BufferDescriptorInfo& info, + Error createDescriptor(const mapper::V2_0::IMapper::BufferDescriptorInfo& info, gralloc1_buffer_descriptor_t* outDescriptor); - Error allocateOne(gralloc1_buffer_descriptor_t descriptor, buffer_handle_t* outBuffer, - uint32_t* outStride); - gralloc1_device_t* mDevice; + Error allocateOneBuffer(gralloc1_buffer_descriptor_t descriptor, + const native_handle_t** outBuffer, uint32_t* outStride); + + gralloc1_device_t* mDevice = nullptr; struct { bool layeredBuffers; - } mCapabilities; + } mCapabilities = {}; struct { GRALLOC1_PFN_DUMP dump; @@ -76,14 +85,12 @@ class Gralloc1Allocator : public IAllocator { GRALLOC1_PFN_GET_STRIDE getStride; GRALLOC1_PFN_ALLOCATE allocate; GRALLOC1_PFN_RELEASE release; - } mDispatch; + } mDispatch = {}; }; -} // namespace implementation +} // namespace passthrough } // namespace V2_0 } // namespace allocator } // namespace graphics } // namespace hardware } // namespace android - -#endif // ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC1ALLOCATOR_H diff --git a/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/GrallocLoader.h b/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/GrallocLoader.h new file mode 100644 index 0000000000..a0b9503c30 --- /dev/null +++ b/graphics/allocator/2.0/utils/passthrough/include/allocator-passthrough/2.0/GrallocLoader.h @@ -0,0 +1,64 @@ +/* + * Copyright 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include + +struct hw_module_t; + +namespace android { +namespace hardware { +namespace graphics { +namespace allocator { +namespace V2_0 { +namespace passthrough { + +class GrallocLoader { + public: + static IAllocator* load() { + const hw_module_t* module = loadModule(); + if (!module) { + return nullptr; + } + auto hal = createHal(module); + if (!hal) { + return nullptr; + } + return createAllocator(std::move(hal)); + } + + // load the gralloc module + static const hw_module_t* loadModule(); + + // return the major api version of the module + static int getModuleMajorApiVersion(const hw_module_t* module); + + // create an AllocatorHal instance + static std::unique_ptr createHal(const hw_module_t* module); + + // create an IAllocator instance + static IAllocator* createAllocator(std::unique_ptr hal); +}; + +} // namespace passthrough +} // namespace V2_0 +} // namespace allocator +} // namespace graphics +} // namespace hardware +} // namespace android