From 925bf7fe6681fe2981522cd9de54ceb2565bc7f4 Mon Sep 17 00:00:00 2001 From: Marissa Wall Date: Sat, 29 Dec 2018 14:27:11 -0800 Subject: gralloc: add allocator and mapper 3.0 support to SF Add support for IAllocator and IMapper 3.0 support to SurfaceFlinger. Bug: 120493579 Test: manual Change-Id: I8b7311cfa6b58502b334460d65eb66715ef429e8 --- libs/ui/Gralloc3.cpp | 371 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 371 insertions(+) create mode 100644 libs/ui/Gralloc3.cpp (limited to 'libs/ui/Gralloc3.cpp') diff --git a/libs/ui/Gralloc3.cpp b/libs/ui/Gralloc3.cpp new file mode 100644 index 0000000000..128200e33e --- /dev/null +++ b/libs/ui/Gralloc3.cpp @@ -0,0 +1,371 @@ +/* + * 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 "Gralloc3" + +#include +#include +#include + +#include +#include +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wzero-length-array" +#include +#pragma clang diagnostic pop + +using android::hardware::graphics::allocator::V3_0::IAllocator; +using android::hardware::graphics::common::V1_1::BufferUsage; +using android::hardware::graphics::mapper::V3_0::BufferDescriptor; +using android::hardware::graphics::mapper::V3_0::Error; +using android::hardware::graphics::mapper::V3_0::IMapper; +using android::hardware::graphics::mapper::V3_0::YCbCrLayout; + +namespace android { + +namespace { + +static constexpr Error kTransactionError = Error::NO_RESOURCES; + +uint64_t getValidUsageBits() { + static const uint64_t validUsageBits = []() -> uint64_t { + uint64_t bits = 0; + for (const auto bit : + hardware::hidl_enum_range()) { + bits = bits | bit; + } + for (const auto bit : + hardware::hidl_enum_range()) { + bits = bits | bit; + } + return bits; + }(); + return validUsageBits; +} + +static inline IMapper::Rect sGralloc3Rect(const Rect& rect) { + IMapper::Rect outRect{}; + outRect.left = rect.left; + outRect.top = rect.top; + outRect.width = rect.width(); + outRect.height = rect.height(); + return outRect; +} +static inline void sBufferDescriptorInfo(uint32_t width, uint32_t height, + android::PixelFormat format, uint32_t layerCount, + uint64_t usage, + IMapper::BufferDescriptorInfo* outDescriptorInfo) { + outDescriptorInfo->width = width; + outDescriptorInfo->height = height; + outDescriptorInfo->layerCount = layerCount; + outDescriptorInfo->format = static_cast(format); + outDescriptorInfo->usage = usage; +} + +} // anonymous namespace + +void Gralloc3Mapper::preload() { + android::hardware::preloadPassthroughService(); +} + +Gralloc3Mapper::Gralloc3Mapper() { + mMapper = IMapper::getService(); + if (mMapper == nullptr) { + ALOGW("mapper 3.x is not supported"); + return; + } + if (mMapper->isRemote()) { + LOG_ALWAYS_FATAL("gralloc-mapper must be in passthrough mode"); + } +} + +bool Gralloc3Mapper::isSupported() const { + return mMapper != nullptr; +} + +status_t Gralloc3Mapper::validateBufferDescriptorInfo( + IMapper::BufferDescriptorInfo* descriptorInfo) const { + uint64_t validUsageBits = getValidUsageBits(); + + if (descriptorInfo->usage & ~validUsageBits) { + ALOGE("buffer descriptor contains invalid usage bits 0x%" PRIx64, + descriptorInfo->usage & ~validUsageBits); + return BAD_VALUE; + } + return NO_ERROR; +} + +status_t Gralloc3Mapper::createDescriptor(void* bufferDescriptorInfo, + void* outBufferDescriptor) const { + IMapper::BufferDescriptorInfo* descriptorInfo = + static_cast(bufferDescriptorInfo); + BufferDescriptor* outDescriptor = static_cast(outBufferDescriptor); + + status_t status = validateBufferDescriptorInfo(descriptorInfo); + if (status != NO_ERROR) { + return status; + } + + Error error; + auto hidl_cb = [&](const auto& tmpError, const auto& tmpDescriptor) { + error = tmpError; + if (error != Error::NONE) { + return; + } + *outDescriptor = tmpDescriptor; + }; + + hardware::Return ret = mMapper->createDescriptor(*descriptorInfo, hidl_cb); + + return static_cast((ret.isOk()) ? error : kTransactionError); +} + +status_t Gralloc3Mapper::importBuffer(const hardware::hidl_handle& rawHandle, + buffer_handle_t* outBufferHandle) const { + Error error; + auto ret = mMapper->importBuffer(rawHandle, [&](const auto& tmpError, const auto& tmpBuffer) { + error = tmpError; + if (error != Error::NONE) { + return; + } + *outBufferHandle = static_cast(tmpBuffer); + }); + + return static_cast((ret.isOk()) ? error : kTransactionError); +} + +void Gralloc3Mapper::freeBuffer(buffer_handle_t bufferHandle) const { + auto buffer = const_cast(bufferHandle); + auto ret = mMapper->freeBuffer(buffer); + + auto error = (ret.isOk()) ? static_cast(ret) : kTransactionError; + ALOGE_IF(error != Error::NONE, "freeBuffer(%p) failed with %d", buffer, error); +} + +status_t Gralloc3Mapper::validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, + uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, + uint32_t stride) const { + IMapper::BufferDescriptorInfo descriptorInfo; + sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo); + + auto buffer = const_cast(bufferHandle); + auto ret = mMapper->validateBufferSize(buffer, descriptorInfo, stride); + + return static_cast((ret.isOk()) ? static_cast(ret) : kTransactionError); +} + +void Gralloc3Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds, + uint32_t* outNumInts) const { + *outNumFds = uint32_t(bufferHandle->numFds); + *outNumInts = uint32_t(bufferHandle->numInts); + + Error error; + auto buffer = const_cast(bufferHandle); + auto ret = mMapper->getTransportSize(buffer, + [&](const auto& tmpError, const auto& tmpNumFds, + const auto& tmpNumInts) { + error = tmpError; + if (error != Error::NONE) { + return; + } + *outNumFds = tmpNumFds; + *outNumInts = tmpNumInts; + }); + + error = (ret.isOk()) ? error : kTransactionError; + + ALOGE_IF(error != Error::NONE, "getTransportSize(%p) failed with %d", buffer, error); +} + +status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, + int acquireFence, void** outData) const { + auto buffer = const_cast(bufferHandle); + + IMapper::Rect accessRegion = sGralloc3Rect(bounds); + + // put acquireFence in a hidl_handle + 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; + } + + Error error; + auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, + [&](const auto& tmpError, const auto& tmpData) { + error = tmpError; + if (error != Error::NONE) { + return; + } + *outData = tmpData; + }); + + // we own acquireFence even on errors + if (acquireFence >= 0) { + close(acquireFence); + } + + error = (ret.isOk()) ? error : kTransactionError; + + ALOGW_IF(error != Error::NONE, "lock(%p, ...) failed: %d", bufferHandle, error); + + return static_cast(error); +} + +status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, + int acquireFence, android_ycbcr* ycbcr) const { + auto buffer = const_cast(bufferHandle); + + IMapper::Rect accessRegion = sGralloc3Rect(bounds); + + // put acquireFence in a hidl_handle + 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; + } + + YCbCrLayout layout; + Error error; + auto ret = mMapper->lockYCbCr(buffer, usage, accessRegion, acquireFenceHandle, + [&](const auto& tmpError, const auto& tmpLayout) { + error = tmpError; + if (error != Error::NONE) { + return; + } + + layout = tmpLayout; + }); + + if (error == Error::NONE) { + ycbcr->y = layout.y; + ycbcr->cb = layout.cb; + ycbcr->cr = layout.cr; + ycbcr->ystride = static_cast(layout.yStride); + ycbcr->cstride = static_cast(layout.cStride); + ycbcr->chroma_step = static_cast(layout.chromaStep); + } + + // we own acquireFence even on errors + if (acquireFence >= 0) { + close(acquireFence); + } + + return static_cast((ret.isOk()) ? error : kTransactionError); +} + +int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const { + auto buffer = const_cast(bufferHandle); + + int releaseFence = -1; + Error error; + auto ret = mMapper->unlock(buffer, [&](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 { + ALOGD("failed to dup unlock release fence"); + sync_wait(fenceHandle->data[0], -1); + } + } + }); + + if (!ret.isOk()) { + error = kTransactionError; + } + + if (error != Error::NONE) { + ALOGE("unlock(%p) failed with %d", buffer, error); + } + + return releaseFence; +} + +Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) { + mAllocator = IAllocator::getService(); + if (mAllocator == nullptr) { + ALOGW("allocator 3.x is not supported"); + return; + } +} + +bool Gralloc3Allocator::isSupported() const { + return mAllocator != nullptr; +} + +std::string Gralloc3Allocator::dumpDebugInfo() const { + std::string debugInfo; + + mAllocator->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); }); + + return debugInfo; +} + +status_t Gralloc3Allocator::allocate(uint32_t width, uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, uint32_t bufferCount, + uint32_t* outStride, buffer_handle_t* outBufferHandles) const { + IMapper::BufferDescriptorInfo descriptorInfo; + sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo); + + BufferDescriptor descriptor; + status_t error = mMapper.createDescriptor(static_cast(&descriptorInfo), + static_cast(&descriptor)); + if (error != NO_ERROR) { + return error; + } + + auto ret = mAllocator->allocate(descriptor, bufferCount, + [&](const auto& tmpError, const auto& tmpStride, + const auto& tmpBuffers) { + error = static_cast(tmpError); + if (tmpError != Error::NONE) { + return; + } + + // import buffers + for (uint32_t i = 0; i < bufferCount; i++) { + error = mMapper.importBuffer(tmpBuffers[i], + &outBufferHandles[i]); + if (error != NO_ERROR) { + for (uint32_t j = 0; j < i; j++) { + mMapper.freeBuffer(outBufferHandles[j]); + outBufferHandles[j] = nullptr; + } + return; + } + } + *outStride = tmpStride; + }); + + // make sure the kernel driver sees BC_FREE_BUFFER and closes the fds now + hardware::IPCThreadState::self()->flushCommands(); + + return (ret.isOk()) ? error : static_cast(kTransactionError); +} + +} // namespace android -- cgit v1.2.3-59-g8ed1b From 0c9fc361b466e3ac66d362b23d71ab7ba9b9ca96 Mon Sep 17 00:00:00 2001 From: Valerie Hau Date: Tue, 22 Jan 2019 09:17:19 -0800 Subject: Modifying Gralloc to handle bytesPerPixel and bytesPerStride Bug: 120493579 Test: build, boot Change-Id: I1fe5acc17389db6d57529d2e120ab6e1f3715705 --- libs/ui/Gralloc2.cpp | 9 ++++++++- libs/ui/Gralloc3.cpp | 12 ++++++++++-- libs/ui/GraphicBuffer.cpp | 11 ++++++++--- libs/ui/GraphicBufferMapper.cpp | 28 +++++++++++++++------------- libs/ui/include/ui/Gralloc.h | 3 ++- libs/ui/include/ui/Gralloc2.h | 3 ++- libs/ui/include/ui/Gralloc3.h | 3 ++- libs/ui/include/ui/GraphicBufferMapper.h | 15 ++++++++------- 8 files changed, 55 insertions(+), 29 deletions(-) (limited to 'libs/ui/Gralloc3.cpp') diff --git a/libs/ui/Gralloc2.cpp b/libs/ui/Gralloc2.cpp index ea7321e24c..2c4b5f32b1 100644 --- a/libs/ui/Gralloc2.cpp +++ b/libs/ui/Gralloc2.cpp @@ -227,7 +227,14 @@ void Gralloc2Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* ou } status_t Gralloc2Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, - int acquireFence, void** outData) const { + int acquireFence, void** outData, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) const { + if (outBytesPerPixel) { + *outBytesPerPixel = -1; + } + if (outBytesPerStride) { + *outBytesPerStride = -1; + } auto buffer = const_cast(bufferHandle); IMapper::Rect accessRegion = sGralloc2Rect(bounds); diff --git a/libs/ui/Gralloc3.cpp b/libs/ui/Gralloc3.cpp index 128200e33e..acb6b01188 100644 --- a/libs/ui/Gralloc3.cpp +++ b/libs/ui/Gralloc3.cpp @@ -192,7 +192,8 @@ void Gralloc3Mapper::getTransportSize(buffer_handle_t bufferHandle, uint32_t* ou } status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, - int acquireFence, void** outData) const { + int acquireFence, void** outData, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) const { auto buffer = const_cast(bufferHandle); IMapper::Rect accessRegion = sGralloc3Rect(bounds); @@ -208,12 +209,19 @@ status_t Gralloc3Mapper::lock(buffer_handle_t bufferHandle, uint64_t usage, cons Error error; auto ret = mMapper->lock(buffer, usage, accessRegion, acquireFenceHandle, - [&](const auto& tmpError, const auto& tmpData) { + [&](const auto& tmpError, const auto& tmpData, + const auto& tmpBytesPerPixel, const auto& tmpBytesPerStride) { error = tmpError; if (error != Error::NONE) { return; } *outData = tmpData; + if (outBytesPerPixel) { + *outBytesPerPixel = tmpBytesPerPixel; + } + if (outBytesPerStride) { + *outBytesPerStride = tmpBytesPerStride; + } }); // we own acquireFence even on errors diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp index f408fcbe84..da24cf1c06 100644 --- a/libs/ui/GraphicBuffer.cpp +++ b/libs/ui/GraphicBuffer.cpp @@ -252,7 +252,10 @@ status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr) width, height); return BAD_VALUE; } - status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr); + int32_t bytesPerPixel, bytesPerStride; + + status_t res = + getBufferMapper().lock(handle, inUsage, rect, vaddr, &bytesPerPixel, &bytesPerStride); return res; } @@ -306,8 +309,10 @@ status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, width, height); return BAD_VALUE; } - status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, - inConsumerUsage, rect, vaddr, fenceFd); + + int32_t bytesPerPixel, bytesPerStride; + status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect, + vaddr, fenceFd, &bytesPerPixel, &bytesPerStride); return res; } diff --git a/libs/ui/GraphicBufferMapper.cpp b/libs/ui/GraphicBufferMapper.cpp index b04932938c..9e36377c3d 100644 --- a/libs/ui/GraphicBufferMapper.cpp +++ b/libs/ui/GraphicBufferMapper.cpp @@ -102,10 +102,10 @@ status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle) return NO_ERROR; } -status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, - const Rect& bounds, void** vaddr) -{ - return lockAsync(handle, usage, bounds, vaddr, -1); +status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds, + void** vaddr, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) { + return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride); } status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, @@ -125,21 +125,23 @@ status_t GraphicBufferMapper::unlock(buffer_handle_t handle) return error; } -status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, - uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd) -{ - return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd); +status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds, + void** vaddr, int fenceFd, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) { + return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd, outBytesPerPixel, + outBytesPerStride); } -status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, - uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds, - void** vaddr, int fenceFd) -{ +status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage, + uint64_t consumerUsage, const Rect& bounds, void** vaddr, + int fenceFd, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) { ATRACE_CALL(); const uint64_t usage = static_cast( android_convertGralloc1To0Usage(producerUsage, consumerUsage)); - return mMapper->lock(handle, usage, bounds, fenceFd, vaddr); + return mMapper->lock(handle, usage, bounds, fenceFd, vaddr, outBytesPerPixel, + outBytesPerStride); } status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, diff --git a/libs/ui/include/ui/Gralloc.h b/libs/ui/include/ui/Gralloc.h index 92bf043064..a484bce4df 100644 --- a/libs/ui/include/ui/Gralloc.h +++ b/libs/ui/include/ui/Gralloc.h @@ -56,7 +56,8 @@ public: // The ownership of acquireFence is always transferred to the callee, even // on errors. virtual status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, - int acquireFence, void** outData) const = 0; + int acquireFence, void** outData, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) const = 0; // The ownership of acquireFence is always transferred to the callee, even // on errors. diff --git a/libs/ui/include/ui/Gralloc2.h b/libs/ui/include/ui/Gralloc2.h index e03cb4357c..b23d8f77e4 100644 --- a/libs/ui/include/ui/Gralloc2.h +++ b/libs/ui/include/ui/Gralloc2.h @@ -53,7 +53,8 @@ public: uint32_t* outNumInts) const override; status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, - int acquireFence, void** outData) const override; + int acquireFence, void** outData, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) const override; status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, int acquireFence, android_ycbcr* ycbcr) const override; diff --git a/libs/ui/include/ui/Gralloc3.h b/libs/ui/include/ui/Gralloc3.h index 510ce4ac23..b0cbcc1300 100644 --- a/libs/ui/include/ui/Gralloc3.h +++ b/libs/ui/include/ui/Gralloc3.h @@ -52,7 +52,8 @@ public: uint32_t* outNumInts) const override; status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, - int acquireFence, void** outData) const override; + int acquireFence, void** outData, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) const override; status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds, int acquireFence, android_ycbcr* ycbcr) const override; diff --git a/libs/ui/include/ui/GraphicBufferMapper.h b/libs/ui/include/ui/GraphicBufferMapper.h index 156bd7abcd..072926ff44 100644 --- a/libs/ui/include/ui/GraphicBufferMapper.h +++ b/libs/ui/include/ui/GraphicBufferMapper.h @@ -56,20 +56,21 @@ public: void getTransportSize(buffer_handle_t handle, uint32_t* outTransportNumFds, uint32_t* outTransportNumInts); - status_t lock(buffer_handle_t handle, - uint32_t usage, const Rect& bounds, void** vaddr); + status_t lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds, void** vaddr, + int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); status_t lockYCbCr(buffer_handle_t handle, uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr); status_t unlock(buffer_handle_t handle); - status_t lockAsync(buffer_handle_t handle, - uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd); + status_t lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds, void** vaddr, + int fenceFd, int32_t* outBytesPerPixel = nullptr, + int32_t* outBytesPerStride = nullptr); - status_t lockAsync(buffer_handle_t handle, - uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds, - void** vaddr, int fenceFd); + status_t lockAsync(buffer_handle_t handle, uint64_t producerUsage, uint64_t consumerUsage, + const Rect& bounds, void** vaddr, int fenceFd, + int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); status_t lockAsyncYCbCr(buffer_handle_t handle, uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, -- cgit v1.2.3-59-g8ed1b From 250c654efecb084f5da8157eae2e210a4eddf19f Mon Sep 17 00:00:00 2001 From: Valerie Hau Date: Thu, 31 Jan 2019 14:23:43 -0800 Subject: Adding optional 3.0 lock support to GraphicBuffer Adding optional 3.0 lock parameters to GraphicBuffer Modifying isSupported to isLoaded Bug: 123423521 Test: build, boot Change-Id: If4a92bd7916072c2cd7005ca6f332f4a90098d27 --- libs/ui/Gralloc2.cpp | 4 ++-- libs/ui/Gralloc3.cpp | 4 ++-- libs/ui/GraphicBuffer.cpp | 16 ++++++++-------- libs/ui/GraphicBufferAllocator.cpp | 4 ++-- libs/ui/GraphicBufferMapper.cpp | 4 ++-- libs/ui/include/ui/Gralloc.h | 4 ++-- libs/ui/include/ui/Gralloc2.h | 4 ++-- libs/ui/include/ui/Gralloc3.h | 4 ++-- libs/ui/include/ui/GraphicBuffer.h | 8 ++++++-- 9 files changed, 28 insertions(+), 24 deletions(-) (limited to 'libs/ui/Gralloc3.cpp') diff --git a/libs/ui/Gralloc2.cpp b/libs/ui/Gralloc2.cpp index 2c4b5f32b1..92ea07cbc4 100644 --- a/libs/ui/Gralloc2.cpp +++ b/libs/ui/Gralloc2.cpp @@ -94,7 +94,7 @@ Gralloc2Mapper::Gralloc2Mapper() { mMapperV2_1 = IMapper::castFrom(mMapper); } -bool Gralloc2Mapper::isSupported() const { +bool Gralloc2Mapper::isLoaded() const { return mMapper != nullptr; } @@ -359,7 +359,7 @@ Gralloc2Allocator::Gralloc2Allocator(const Gralloc2Mapper& mapper) : mMapper(map } } -bool Gralloc2Allocator::isSupported() const { +bool Gralloc2Allocator::isLoaded() const { return mAllocator != nullptr; } diff --git a/libs/ui/Gralloc3.cpp b/libs/ui/Gralloc3.cpp index acb6b01188..306a74b80c 100644 --- a/libs/ui/Gralloc3.cpp +++ b/libs/ui/Gralloc3.cpp @@ -92,7 +92,7 @@ Gralloc3Mapper::Gralloc3Mapper() { } } -bool Gralloc3Mapper::isSupported() const { +bool Gralloc3Mapper::isLoaded() const { return mMapper != nullptr; } @@ -322,7 +322,7 @@ Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(map } } -bool Gralloc3Allocator::isSupported() const { +bool Gralloc3Allocator::isLoaded() const { return mAllocator != nullptr; } diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp index da24cf1c06..15597eb666 100644 --- a/libs/ui/GraphicBuffer.cpp +++ b/libs/ui/GraphicBuffer.cpp @@ -236,15 +236,15 @@ status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWr return NO_ERROR; } -status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr) -{ +status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel, + int32_t* outBytesPerStride) { const Rect lockBounds(width, height); - status_t res = lock(inUsage, lockBounds, vaddr); + status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride); return res; } -status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr) -{ +status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr, + int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { if (rect.left < 0 || rect.right > width || rect.top < 0 || rect.bottom > height) { ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", @@ -252,10 +252,10 @@ status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr) width, height); return BAD_VALUE; } - int32_t bytesPerPixel, bytesPerStride; - status_t res = - getBufferMapper().lock(handle, inUsage, rect, vaddr, &bytesPerPixel, &bytesPerStride); + status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel, + outBytesPerStride); + return res; } diff --git a/libs/ui/GraphicBufferAllocator.cpp b/libs/ui/GraphicBufferAllocator.cpp index efb5798167..5a67dc4a4d 100644 --- a/libs/ui/GraphicBufferAllocator.cpp +++ b/libs/ui/GraphicBufferAllocator.cpp @@ -48,12 +48,12 @@ KeyedVector( reinterpret_cast(mMapper.getGrallocMapper())); - if (!mAllocator->isSupported()) { + if (!mAllocator->isLoaded()) { mAllocator = std::make_unique( reinterpret_cast(mMapper.getGrallocMapper())); } - if (!mAllocator->isSupported()) { + if (!mAllocator->isLoaded()) { LOG_ALWAYS_FATAL("gralloc-allocator is missing"); } } diff --git a/libs/ui/GraphicBufferMapper.cpp b/libs/ui/GraphicBufferMapper.cpp index 9e36377c3d..79c333fce4 100644 --- a/libs/ui/GraphicBufferMapper.cpp +++ b/libs/ui/GraphicBufferMapper.cpp @@ -51,11 +51,11 @@ void GraphicBufferMapper::preloadHal() { GraphicBufferMapper::GraphicBufferMapper() { mMapper = std::make_unique(); - if (!mMapper->isSupported()) { + if (!mMapper->isLoaded()) { mMapper = std::make_unique(); } - if (!mMapper->isSupported()) { + if (!mMapper->isLoaded()) { LOG_ALWAYS_FATAL("gralloc-mapper is missing"); } } diff --git a/libs/ui/include/ui/Gralloc.h b/libs/ui/include/ui/Gralloc.h index a484bce4df..1b8a930c4c 100644 --- a/libs/ui/include/ui/Gralloc.h +++ b/libs/ui/include/ui/Gralloc.h @@ -31,7 +31,7 @@ class GrallocMapper { public: virtual ~GrallocMapper(); - virtual bool isSupported() const = 0; + virtual bool isLoaded() const = 0; virtual status_t createDescriptor(void* bufferDescriptorInfo, void* outBufferDescriptor) const = 0; @@ -74,7 +74,7 @@ class GrallocAllocator { public: virtual ~GrallocAllocator(); - virtual bool isSupported() const = 0; + virtual bool isLoaded() const = 0; virtual std::string dumpDebugInfo() const = 0; diff --git a/libs/ui/include/ui/Gralloc2.h b/libs/ui/include/ui/Gralloc2.h index b23d8f77e4..4ef9b51e4f 100644 --- a/libs/ui/include/ui/Gralloc2.h +++ b/libs/ui/include/ui/Gralloc2.h @@ -36,7 +36,7 @@ public: Gralloc2Mapper(); - bool isSupported() const override; + bool isLoaded() const override; status_t createDescriptor(void* bufferDescriptorInfo, void* outBufferDescriptor) const override; @@ -76,7 +76,7 @@ public: // time. Gralloc2Allocator(const Gralloc2Mapper& mapper); - bool isSupported() const override; + bool isLoaded() const override; std::string dumpDebugInfo() const override; diff --git a/libs/ui/include/ui/Gralloc3.h b/libs/ui/include/ui/Gralloc3.h index b0cbcc1300..879e96ed40 100644 --- a/libs/ui/include/ui/Gralloc3.h +++ b/libs/ui/include/ui/Gralloc3.h @@ -35,7 +35,7 @@ public: Gralloc3Mapper(); - bool isSupported() const override; + bool isLoaded() const override; status_t createDescriptor(void* bufferDescriptorInfo, void* outBufferDescriptor) const override; @@ -74,7 +74,7 @@ public: // time. Gralloc3Allocator(const Gralloc3Mapper& mapper); - bool isSupported() const override; + bool isLoaded() const override; std::string dumpDebugInfo() const override; diff --git a/libs/ui/include/ui/GraphicBuffer.h b/libs/ui/include/ui/GraphicBuffer.h index b73ca2b793..18a27a01dd 100644 --- a/libs/ui/include/ui/GraphicBuffer.h +++ b/libs/ui/include/ui/GraphicBuffer.h @@ -169,8 +169,12 @@ public: bool needsReallocation(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); - status_t lock(uint32_t inUsage, void** vaddr); - status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr); + // For the following two lock functions, if bytesPerStride or bytesPerPixel + // are unknown or variable, -1 will be returned + status_t lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel = nullptr, + int32_t* outBytesPerStride = nullptr); + status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr, + int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); // For HAL_PIXEL_FORMAT_YCbCr_420_888 status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr); status_t lockYCbCr(uint32_t inUsage, const Rect& rect, -- cgit v1.2.3-59-g8ed1b From ddbfaeb7a6b67e93f7a2519c7725561de644d671 Mon Sep 17 00:00:00 2001 From: Valerie Hau Date: Fri, 1 Feb 2019 09:54:20 -0800 Subject: Adding isSupported HIDL Mapper function support to framework Bug: 123423521 Test: build, boot, manual testing Change-Id: Iee52de6f5610a8465b1f4435d02dc61a8064064a --- libs/ui/Gralloc2.cpp | 6 ++++++ libs/ui/Gralloc3.cpp | 30 ++++++++++++++++++++++++++++++ libs/ui/GraphicBuffer.cpp | 7 +++++++ libs/ui/GraphicBufferMapper.cpp | 5 +++++ libs/ui/include/ui/Gralloc.h | 9 +++++++++ libs/ui/include/ui/Gralloc2.h | 3 +++ libs/ui/include/ui/Gralloc3.h | 3 +++ libs/ui/include/ui/GraphicBuffer.h | 3 +++ libs/ui/include/ui/GraphicBufferMapper.h | 3 +++ 9 files changed, 69 insertions(+) (limited to 'libs/ui/Gralloc3.cpp') diff --git a/libs/ui/Gralloc2.cpp b/libs/ui/Gralloc2.cpp index 92ea07cbc4..5dc453005d 100644 --- a/libs/ui/Gralloc2.cpp +++ b/libs/ui/Gralloc2.cpp @@ -351,6 +351,12 @@ int Gralloc2Mapper::unlock(buffer_handle_t bufferHandle) const { return releaseFence; } +status_t Gralloc2Mapper::isSupported(uint32_t /*width*/, uint32_t /*height*/, + android::PixelFormat /*format*/, uint32_t /*layerCount*/, + uint64_t /*usage*/, bool* /*outSupported*/) const { + return INVALID_OPERATION; +} + Gralloc2Allocator::Gralloc2Allocator(const Gralloc2Mapper& mapper) : mMapper(mapper) { mAllocator = IAllocator::getService(); if (mAllocator == nullptr) { diff --git a/libs/ui/Gralloc3.cpp b/libs/ui/Gralloc3.cpp index 306a74b80c..7f8e57c9cb 100644 --- a/libs/ui/Gralloc3.cpp +++ b/libs/ui/Gralloc3.cpp @@ -314,6 +314,36 @@ int Gralloc3Mapper::unlock(buffer_handle_t bufferHandle) const { return releaseFence; } +status_t Gralloc3Mapper::isSupported(uint32_t width, uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, + bool* outSupported) const { + IMapper::BufferDescriptorInfo descriptorInfo; + sBufferDescriptorInfo(width, height, format, layerCount, usage, &descriptorInfo); + + Error error; + auto ret = mMapper->isSupported(descriptorInfo, + [&](const auto& tmpError, const auto& tmpSupported) { + error = tmpError; + if (error != Error::NONE) { + return; + } + if (outSupported) { + *outSupported = tmpSupported; + } + }); + + if (!ret.isOk()) { + error = kTransactionError; + } + + if (error != Error::NONE) { + ALOGE("isSupported(%u, %u, %d, %u, ...) failed with %d", width, height, format, layerCount, + error); + } + + return static_cast(error); +} + Gralloc3Allocator::Gralloc3Allocator(const Gralloc3Mapper& mapper) : mMapper(mapper) { mAllocator = IAllocator::getService(); if (mAllocator == nullptr) { diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp index 15597eb666..e678c58cfa 100644 --- a/libs/ui/GraphicBuffer.cpp +++ b/libs/ui/GraphicBuffer.cpp @@ -344,6 +344,13 @@ status_t GraphicBuffer::unlockAsync(int *fenceFd) return res; } +status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, + uint32_t inLayerCount, uint64_t inUsage, + bool* outSupported) const { + return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage, + outSupported); +} + size_t GraphicBuffer::getFlattenedSize() const { return static_cast(13 + (handle ? mTransportNumInts : 0)) * sizeof(int); } diff --git a/libs/ui/GraphicBufferMapper.cpp b/libs/ui/GraphicBufferMapper.cpp index 79c333fce4..06981c39a5 100644 --- a/libs/ui/GraphicBufferMapper.cpp +++ b/libs/ui/GraphicBufferMapper.cpp @@ -161,5 +161,10 @@ status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) return NO_ERROR; } +status_t GraphicBufferMapper::isSupported(uint32_t width, uint32_t height, + android::PixelFormat format, uint32_t layerCount, + uint64_t usage, bool* outSupported) { + return mMapper->isSupported(width, height, format, layerCount, usage, outSupported); +} // --------------------------------------------------------------------------- }; // namespace android diff --git a/libs/ui/include/ui/Gralloc.h b/libs/ui/include/ui/Gralloc.h index 1b8a930c4c..6cc23f0ef8 100644 --- a/libs/ui/include/ui/Gralloc.h +++ b/libs/ui/include/ui/Gralloc.h @@ -67,6 +67,15 @@ public: // unlock returns a fence sync object (or -1) and the fence sync object is // owned by the caller virtual int unlock(buffer_handle_t bufferHandle) const = 0; + + // isSupported queries whether or not a buffer with the given width, height, + // format, layer count, and usage can be allocated on the device. If + // *outSupported is set to true, a buffer with the given specifications may be successfully + // allocated if resources are available. If false, a buffer with the given specifications will + // never successfully allocate on this device. Note that this function is not guaranteed to be + // supported on all devices, in which case a status_t of INVALID_OPERATION will be returned. + virtual status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, bool* outSupported) const = 0; }; // A wrapper to IAllocator diff --git a/libs/ui/include/ui/Gralloc2.h b/libs/ui/include/ui/Gralloc2.h index 4ef9b51e4f..948f5976eb 100644 --- a/libs/ui/include/ui/Gralloc2.h +++ b/libs/ui/include/ui/Gralloc2.h @@ -61,6 +61,9 @@ public: int unlock(buffer_handle_t bufferHandle) const override; + status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, bool* outSupported) const override; + private: // Determines whether the passed info is compatible with the mapper. status_t validateBufferDescriptorInfo( diff --git a/libs/ui/include/ui/Gralloc3.h b/libs/ui/include/ui/Gralloc3.h index 879e96ed40..0965f52772 100644 --- a/libs/ui/include/ui/Gralloc3.h +++ b/libs/ui/include/ui/Gralloc3.h @@ -60,6 +60,9 @@ public: int unlock(buffer_handle_t bufferHandle) const override; + status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, bool* outSupported) const override; + private: // Determines whether the passed info is compatible with the mapper. status_t validateBufferDescriptorInfo( diff --git a/libs/ui/include/ui/GraphicBuffer.h b/libs/ui/include/ui/GraphicBuffer.h index 18a27a01dd..7d88c58937 100644 --- a/libs/ui/include/ui/GraphicBuffer.h +++ b/libs/ui/include/ui/GraphicBuffer.h @@ -191,6 +191,9 @@ public: android_ycbcr *ycbcr, int fenceFd); status_t unlockAsync(int *fenceFd); + status_t isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, + uint32_t inLayerCount, uint64_t inUsage, bool* outSupported) const; + ANativeWindowBuffer* getNativeBuffer() const; // for debugging diff --git a/libs/ui/include/ui/GraphicBufferMapper.h b/libs/ui/include/ui/GraphicBufferMapper.h index 072926ff44..77c99ccd07 100644 --- a/libs/ui/include/ui/GraphicBufferMapper.h +++ b/libs/ui/include/ui/GraphicBufferMapper.h @@ -78,6 +78,9 @@ public: status_t unlockAsync(buffer_handle_t handle, int *fenceFd); + status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format, + uint32_t layerCount, uint64_t usage, bool* outSupported); + const GrallocMapper& getGrallocMapper() const { return reinterpret_cast(*mMapper); } -- cgit v1.2.3-59-g8ed1b From 634299194c6c0c2a7fe05297795392366866d82b Mon Sep 17 00:00:00 2001 From: Marissa Wall Date: Tue, 9 Apr 2019 14:05:06 -0700 Subject: mapper: support new usage and format Update Gralloc3 to use usage and format from android.hardware.graphics.common@1.2. This enables support for the usage HW_IMAGE_ENCODER and the format HSV_888. Test: IMapper and AHardwareBuffer tests Bug: 79465976 Change-Id: I91562c609299829d9c33980a59cdf5adfc6596a9 --- libs/ui/Gralloc3.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'libs/ui/Gralloc3.cpp') diff --git a/libs/ui/Gralloc3.cpp b/libs/ui/Gralloc3.cpp index 7f8e57c9cb..eb43765733 100644 --- a/libs/ui/Gralloc3.cpp +++ b/libs/ui/Gralloc3.cpp @@ -28,7 +28,7 @@ #pragma clang diagnostic pop using android::hardware::graphics::allocator::V3_0::IAllocator; -using android::hardware::graphics::common::V1_1::BufferUsage; +using android::hardware::graphics::common::V1_2::BufferUsage; using android::hardware::graphics::mapper::V3_0::BufferDescriptor; using android::hardware::graphics::mapper::V3_0::Error; using android::hardware::graphics::mapper::V3_0::IMapper; @@ -44,11 +44,7 @@ uint64_t getValidUsageBits() { static const uint64_t validUsageBits = []() -> uint64_t { uint64_t bits = 0; for (const auto bit : - hardware::hidl_enum_range()) { - bits = bits | bit; - } - for (const auto bit : - hardware::hidl_enum_range()) { + hardware::hidl_enum_range()) { bits = bits | bit; } return bits; @@ -71,7 +67,7 @@ static inline void sBufferDescriptorInfo(uint32_t width, uint32_t height, outDescriptorInfo->width = width; outDescriptorInfo->height = height; outDescriptorInfo->layerCount = layerCount; - outDescriptorInfo->format = static_cast(format); + outDescriptorInfo->format = static_cast(format); outDescriptorInfo->usage = usage; } -- cgit v1.2.3-59-g8ed1b