diff options
-rw-r--r-- | include/ui/GrallocMapper.h | 77 | ||||
-rw-r--r-- | libs/ui/Android.bp | 5 | ||||
-rw-r--r-- | libs/ui/GrallocMapper.cpp | 171 | ||||
-rw-r--r-- | libs/ui/GraphicBufferMapper.cpp | 73 |
4 files changed, 162 insertions, 164 deletions
diff --git a/include/ui/GrallocMapper.h b/include/ui/GrallocMapper.h index 509cb6e054..f533dfb71b 100644 --- a/include/ui/GrallocMapper.h +++ b/include/ui/GrallocMapper.h @@ -32,98 +32,35 @@ using hardware::graphics::allocator::V2_0::ConsumerUsage; using hardware::graphics::common::V1_0::PixelFormat; using hardware::graphics::mapper::V2_0::FlexLayout; using hardware::graphics::mapper::V2_0::BackingStore; -using hardware::graphics::mapper::V2_0::Device; using hardware::graphics::mapper::V2_0::IMapper; // Mapper is a wrapper to IMapper, a client-side graphics buffer mapper. class Mapper { public: Mapper(); - ~Mapper(); // this will be removed and Mapper will be always valid bool valid() const { return (mMapper != nullptr); } - Error retain(buffer_handle_t handle) const - { - return mMapper->retain(mDevice, handle); - } - + Error retain(buffer_handle_t handle) const; void release(buffer_handle_t handle) const; - Error getDimensions(buffer_handle_t handle, - uint32_t* outWidth, uint32_t* outHeight) const - { - return mMapper->getDimensions(mDevice, handle, outWidth, outHeight); - } - - Error getFormat(buffer_handle_t handle, - PixelFormat* outFormat) const - { - return mMapper->getFormat(mDevice, handle, outFormat); - } - - Error getLayerCount(buffer_handle_t handle, uint32_t* outLayerCount) const - { - return mMapper->getLayerCount(mDevice, handle, outLayerCount); - } - - Error getProducerUsageMask(buffer_handle_t handle, - uint64_t* outUsageMask) const - { - return mMapper->getProducerUsageMask(mDevice, handle, outUsageMask); - } - - Error getConsumerUsageMask(buffer_handle_t handle, - uint64_t* outUsageMask) const - { - return mMapper->getConsumerUsageMask(mDevice, handle, outUsageMask); - } - - Error getBackingStore(buffer_handle_t handle, - BackingStore* outStore) const - { - return mMapper->getBackingStore(mDevice, handle, outStore); - } - - Error getStride(buffer_handle_t handle, uint32_t* outStride) const - { - return mMapper->getStride(mDevice, handle, outStride); - } - - Error getNumFlexPlanes(buffer_handle_t handle, - uint32_t* outNumPlanes) const - { - return mMapper->getNumFlexPlanes(mDevice, handle, outNumPlanes); - } + Error getStride(buffer_handle_t handle, uint32_t* outStride) const; Error lock(buffer_handle_t handle, uint64_t producerUsageMask, uint64_t consumerUsageMask, - const Device::Rect& accessRegion, - int acquireFence, void** outData) const - { - return mMapper->lock(mDevice, handle, - producerUsageMask, consumerUsageMask, - &accessRegion, acquireFence, outData); - } - + const IMapper::Rect& accessRegion, + int acquireFence, void** outData) const; Error lock(buffer_handle_t handle, uint64_t producerUsageMask, uint64_t consumerUsageMask, - const Device::Rect& accessRegion, - int acquireFence, FlexLayout* outFlexLayout) const - { - return mMapper->lockFlex(mDevice, handle, - producerUsageMask, consumerUsageMask, - &accessRegion, acquireFence, outFlexLayout); - } - + const IMapper::Rect& accessRegion, + int acquireFence, FlexLayout* outLayout) const; int unlock(buffer_handle_t handle) const; private: - const IMapper* mMapper; - Device* mDevice; + sp<IMapper> mMapper; }; } // namespace Gralloc2 diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp index 3328a9259c..c207a7996b 100644 --- a/libs/ui/Android.bp +++ b/libs/ui/Android.bp @@ -59,12 +59,9 @@ cc_library_shared { "UiConfig.cpp", ], - static_libs: [ - "android.hardware.graphics.mapper@2.0", - ], - shared_libs: [ "android.hardware.graphics.allocator@2.0", + "android.hardware.graphics.mapper@2.0", "libbinder", "libcutils", "libhardware", diff --git a/libs/ui/GrallocMapper.cpp b/libs/ui/GrallocMapper.cpp index d568b68d7b..b444871e3b 100644 --- a/libs/ui/GrallocMapper.cpp +++ b/libs/ui/GrallocMapper.cpp @@ -26,85 +26,144 @@ namespace android { namespace Gralloc2 { -typedef const void*(*FetchInterface)(const char* name); +static constexpr Error kDefaultError = Error::NO_RESOURCES; -static FetchInterface loadHalLib(const char* pkg_name) +Mapper::Mapper() { - static const std::array<const char*, 3> sSearchDirs = {{ - HAL_LIBRARY_PATH_ODM, - HAL_LIBRARY_PATH_VENDOR, - HAL_LIBRARY_PATH_SYSTEM, - }}; - static const char sSymbolName[] = "HALLIB_FETCH_Interface"; - - void* handle = nullptr; - std::string path; - for (auto dir : sSearchDirs) { - path = dir; - path += pkg_name; - path += ".hallib.so"; - handle = dlopen(path.c_str(), RTLD_LOCAL | RTLD_NOW); - if (handle) { - break; - } - } - if (!handle) { - return nullptr; + mMapper = IMapper::getService("gralloc-mapper"); + if (mMapper != nullptr && mMapper->isRemote()) { + LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); } +} - void* symbol = dlsym(handle, sSymbolName); - if (!symbol) { - ALOGE("%s is missing from %s", sSymbolName, path.c_str()); - dlclose(handle); - return nullptr; - } +Error Mapper::retain(buffer_handle_t handle) const +{ + auto ret = mMapper->retain(handle); + return (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError; +} - return reinterpret_cast<FetchInterface>(symbol); +void Mapper::release(buffer_handle_t handle) const +{ + auto ret = mMapper->release(handle); + + auto error = (ret.isOk()) ? static_cast<Error>(ret) : kDefaultError; + ALOGE_IF(error != Error::NONE, + "release(%p) failed with %d", handle, error); } -Mapper::Mapper() - : mMapper(nullptr), mDevice(nullptr) +Error Mapper::getStride(buffer_handle_t handle, uint32_t* outStride) const +{ + Error error = kDefaultError; + mMapper->getStride(handle, + [&](const auto& tmpError, const auto& tmpStride) + { + error = tmpError; + if (error != Error::NONE) { + return; + } + + *outStride = tmpStride; + }); + + return error; +} + +Error Mapper::lock(buffer_handle_t handle, + uint64_t producerUsageMask, + uint64_t consumerUsageMask, + const IMapper::Rect& accessRegion, + int acquireFence, void** outData) const { - static const char sHalLibName[] = "android.hardware.graphics.mapper"; - static const char sSupportedInterface[] = - "android.hardware.graphics.mapper@2.0::IMapper"; + hardware::hidl_handle acquireFenceHandle; - FetchInterface fetchInterface = loadHalLib(sHalLibName); - if (!fetchInterface) { - return; + NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); + if (acquireFence >= 0) { + auto h = native_handle_init(acquireFenceStorage, 1, 0); + h->data[0] = acquireFence; + acquireFenceHandle = h; } - mMapper = static_cast<const IMapper*>( - fetchInterface(sSupportedInterface)); - if (!mMapper) { - ALOGE("%s is not supported", sSupportedInterface); - return; + Error error = kDefaultError; + mMapper->lock(handle, producerUsageMask, consumerUsageMask, + accessRegion, acquireFenceHandle, + [&](const auto& tmpError, const auto& tmpData) + { + error = tmpError; + if (error != Error::NONE) { + return; + } + + *outData = tmpData; + }); + + if (error == Error::NONE && acquireFence >= 0) { + close(acquireFence); } - if (mMapper->createDevice(&mDevice) != Error::NONE) { - ALOGE("failed to create mapper device"); - mMapper = nullptr; - } + return error; } -Mapper::~Mapper() +Error Mapper::lock(buffer_handle_t handle, + uint64_t producerUsageMask, + uint64_t consumerUsageMask, + const IMapper::Rect& accessRegion, + int acquireFence, FlexLayout* outLayout) const { - if (mMapper) { - mMapper->destroyDevice(mDevice); + hardware::hidl_handle acquireFenceHandle; + + NATIVE_HANDLE_DECLARE_STORAGE(acquireFenceStorage, 1, 0); + if (acquireFence >= 0) { + auto h = native_handle_init(acquireFenceStorage, 1, 0); + h->data[0] = acquireFence; + acquireFenceHandle = h; } -} -void Mapper::release(buffer_handle_t handle) const -{ - auto error = mMapper->release(mDevice, handle); - ALOGE_IF(error != Error::NONE, - "release(%p) failed with %d", handle, error); + Error error = kDefaultError; + mMapper->lockFlex(handle, producerUsageMask, consumerUsageMask, + accessRegion, acquireFenceHandle, + [&](const auto& tmpError, const auto& tmpLayout) + { + error = tmpError; + if (error != Error::NONE) { + return; + } + + *outLayout = tmpLayout; + }); + + if (error == Error::NONE && acquireFence >= 0) { + close(acquireFence); + } + + return error; } int Mapper::unlock(buffer_handle_t handle) const { int releaseFence; - auto error = mMapper->unlock(mDevice, handle, &releaseFence); + + Error error = kDefaultError; + mMapper->unlock(handle, + [&](const auto& tmpError, const auto& tmpReleaseFence) + { + error = tmpError; + if (error != Error::NONE) { + return; + } + + auto fenceHandle = tmpReleaseFence.getNativeHandle(); + if (fenceHandle && fenceHandle->numFds == 1) { + int fd = dup(fenceHandle->data[0]); + if (fd >= 0) { + releaseFence = fd; + } else { + error = Error::NO_RESOURCES; + } + } else { + releaseFence = -1; + } + }); + if (error != Error::NONE) { ALOGE("unlock(%p) failed with %d", handle, error); releaseFence = -1; diff --git a/libs/ui/GraphicBufferMapper.cpp b/libs/ui/GraphicBufferMapper.cpp index 13ab38c957..1ff934bfe9 100644 --- a/libs/ui/GraphicBufferMapper.cpp +++ b/libs/ui/GraphicBufferMapper.cpp @@ -148,8 +148,8 @@ status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); gralloc1_error_t error; if (mMapper->valid()) { - const Gralloc2::Device::Rect& accessRect = - *reinterpret_cast<Gralloc2::Device::Rect*>(&accessRegion); + const Gralloc2::IMapper::Rect& accessRect = + *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion); error = static_cast<gralloc1_error_t>(mMapper->lock( handle, usage, usage, accessRect, fenceFd, vaddr)); } else { @@ -196,10 +196,32 @@ status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, gralloc1_rect_t accessRegion = asGralloc1Rect(bounds); - if (!mMapper->valid()) { + std::vector<android_flex_plane_t> planes; + android_flex_layout_t flexLayout{}; + gralloc1_error_t error; + + if (mMapper->valid()) { + const Gralloc2::IMapper::Rect& accessRect = + *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion); + Gralloc2::FlexLayout layout{}; + error = static_cast<gralloc1_error_t>(mMapper->lock( + handle, usage, usage, accessRect, fenceFd, &layout)); + + if (error == GRALLOC1_ERROR_NONE) { + planes.resize(layout.planes.size()); + memcpy(planes.data(), layout.planes.data(), + sizeof(planes[0]) * planes.size()); + + flexLayout.format = static_cast<android_flex_format_t>( + layout.format); + flexLayout.num_planes = static_cast<uint32_t>(planes.size()); + flexLayout.planes = planes.data(); + } + } else { + sp<Fence> fence = new Fence(fenceFd); + if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) { - sp<Fence> fence = new Fence(fenceFd); - gralloc1_error_t error = mDevice->lockYCbCr(handle, + error = mDevice->lockYCbCr(handle, static_cast<gralloc1_producer_usage_t>(usage), static_cast<gralloc1_consumer_usage_t>(usage), &accessRegion, ycbcr, fence); @@ -207,40 +229,23 @@ status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, "lockYCbCr(%p, ...) failed: %d", handle, error); return error; } - } - uint32_t numPlanes = 0; - gralloc1_error_t error; - if (mMapper->valid()) { - error = static_cast<gralloc1_error_t>( - mMapper->getNumFlexPlanes(handle, &numPlanes)); - } else { + uint32_t numPlanes = 0; error = mDevice->getNumFlexPlanes(handle, &numPlanes); - } - if (error != GRALLOC1_ERROR_NONE) { - ALOGV("Failed to retrieve number of flex planes: %d", error); - return error; - } - if (numPlanes < 3) { - ALOGV("Not enough planes for YCbCr (%u found)", numPlanes); - return GRALLOC1_ERROR_UNSUPPORTED; - } + if (error != GRALLOC1_ERROR_NONE) { + ALOGV("Failed to retrieve number of flex planes: %d", error); + return error; + } + if (numPlanes < 3) { + ALOGV("Not enough planes for YCbCr (%u found)", numPlanes); + return GRALLOC1_ERROR_UNSUPPORTED; + } - std::vector<android_flex_plane_t> planes(numPlanes); - android_flex_layout_t flexLayout{}; - flexLayout.num_planes = numPlanes; - flexLayout.planes = planes.data(); + planes.resize(numPlanes); + flexLayout.num_planes = numPlanes; + flexLayout.planes = planes.data(); - if (mMapper->valid()) { - const Gralloc2::Device::Rect& accessRect = - *reinterpret_cast<Gralloc2::Device::Rect*>(&accessRegion); - Gralloc2::FlexLayout& layout = - *reinterpret_cast<Gralloc2::FlexLayout*>(&flexLayout); - error = static_cast<gralloc1_error_t>(mMapper->lock( - handle, usage, usage, accessRect, fenceFd, &layout)); - } else { - sp<Fence> fence = new Fence(fenceFd); error = mDevice->lockFlex(handle, static_cast<gralloc1_producer_usage_t>(usage), static_cast<gralloc1_consumer_usage_t>(usage), |