From c24c53f94cf2bfd8021c7d9628a81817e8a3ff2d Mon Sep 17 00:00:00 2001 From: Nana Zhang Date: Tue, 3 Sep 2024 01:12:22 +0000 Subject: [PATCH] Clean code for MFX_BUFFER_QUEUE Signed-off-by: Nana Zhang Tracked-On: OAM-124733 --- c2_components/Android.bp | 1 - .../include/mfx_c2_decoder_component.h | 4 +- .../src/mfx_c2_decoder_component.cpp | 11 +- c2_utils/Android.bp | 5 - c2_utils/src/mfx_va_frame_pool_allocator.cpp | 9 - plugin_store/Android.bp | 49 -- plugin_store/include/mfx_c2_allocator_id.h | 27 - plugin_store/include/mfx_c2_buffer_queue.h | 86 -- plugin_store/src/mfx_c2_buffer_queue.cpp | 780 ------------------ plugin_store/src/mfx_c2_plugin_store.cpp | 99 --- 10 files changed, 2 insertions(+), 1069 deletions(-) delete mode 100644 plugin_store/Android.bp delete mode 100755 plugin_store/include/mfx_c2_allocator_id.h delete mode 100755 plugin_store/include/mfx_c2_buffer_queue.h delete mode 100755 plugin_store/src/mfx_c2_buffer_queue.cpp delete mode 100755 plugin_store/src/mfx_c2_plugin_store.cpp diff --git a/c2_components/Android.bp b/c2_components/Android.bp index 1e560bd4..bb063b84 100644 --- a/c2_components/Android.bp +++ b/c2_components/Android.bp @@ -66,7 +66,6 @@ cc_library_shared { include_dirs: [ "vendor/intel/mediasdk_c2/c2_utils/include", "vendor/intel/mediasdk_c2/c2_buffers/include", - "vendor/intel/mediasdk_c2/plugin_store/include", "frameworks/native/libs/ui/include", "frameworks/av/media/codec2/sfplugin/utils", ], diff --git a/c2_components/include/mfx_c2_decoder_component.h b/c2_components/include/mfx_c2_decoder_component.h index bc895460..98f6f493 100755 --- a/c2_components/include/mfx_c2_decoder_component.h +++ b/c2_components/include/mfx_c2_decoder_component.h @@ -258,7 +258,6 @@ class MfxC2DecoderComponent : public MfxC2Component std::shared_ptr m_domain; std::shared_ptr m_size; std::shared_ptr m_surfaceAllocator; - std::shared_ptr m_inputAllocators; std::shared_ptr m_outputAllocators; std::shared_ptr m_maxSize; std::shared_ptr m_maxInputSize; @@ -271,13 +270,12 @@ class MfxC2DecoderComponent : public MfxC2Component std::shared_ptr m_outputUsage; std::shared_ptr m_profileLevel; std::shared_ptr m_actualOutputDelay; - std::shared_ptr m_requestedInputDelay; - std::shared_ptr m_actualInputDelay; std::shared_ptr m_inputDelay; std::shared_ptr m_defaultColorAspects; std::shared_ptr m_inColorAspects; std::shared_ptr m_outColorAspects; std::shared_ptr m_pixelFormat; + /* ----------------------------------------Setters------------------------------------------- */ static C2R OutputSurfaceAllocatorSetter(bool mayBlock, C2P &me); static C2R SizeSetter(bool mayBlock, const C2P &oldMe, diff --git a/c2_components/src/mfx_c2_decoder_component.cpp b/c2_components/src/mfx_c2_decoder_component.cpp index c19dcefa..f1ed1a00 100755 --- a/c2_components/src/mfx_c2_decoder_component.cpp +++ b/c2_components/src/mfx_c2_decoder_component.cpp @@ -26,8 +26,6 @@ #include "mfx_c2_components_registry.h" #include "mfx_c2_utils.h" #include "mfx_defaults.h" -#include "mfx_c2_allocator_id.h" -#include "mfx_c2_buffer_queue.h" #include "C2PlatformSupport.h" #include "mfx_c2_color_aspects_utils.h" @@ -1365,14 +1363,7 @@ c2_status_t MfxC2DecoderComponent::UpdateC2Param(const mfxVideoParam* src, C2Par } case kParamIndexAllocators: { if (C2PortAllocatorsTuning::output::PARAM_TYPE == index) { - if (m_mfxVideoParams.IOPattern == MFX_IOPATTERN_OUT_VIDEO_MEMORY) -#ifdef MFX_BUFFER_QUEUE - m_outputAllocators->m.values[0] = MFX_BUFFERQUEUE; -#else - m_outputAllocators->m.values[0] = C2PlatformAllocatorStore::GRALLOC; -#endif - else - m_outputAllocators->m.values[0] = C2PlatformAllocatorStore::GRALLOC; + m_outputAllocators->m.values[0] = C2PlatformAllocatorStore::GRALLOC; MFX_DEBUG_TRACE_PRINTF("Set output port alloctor to: %d", m_outputAllocators->m.values[0]); } break; diff --git a/c2_utils/Android.bp b/c2_utils/Android.bp index 2082e9ac..c1c42907 100644 --- a/c2_utils/Android.bp +++ b/c2_utils/Android.bp @@ -42,16 +42,11 @@ cc_library_static { "src/*.cpp", ], - cflags: [ - //"-DMFX_BUFFER_QUEUE" - ], - local_include_dirs: [ "include", ], include_dirs: [ - "vendor/intel/mediasdk_c2/plugin_store/include", "hardware/intel/external/minigbm-intel", "frameworks/native/include", "frameworks/native/libs/ui/include", diff --git a/c2_utils/src/mfx_va_frame_pool_allocator.cpp b/c2_utils/src/mfx_va_frame_pool_allocator.cpp index a2e0a124..d8aae064 100755 --- a/c2_utils/src/mfx_va_frame_pool_allocator.cpp +++ b/c2_utils/src/mfx_va_frame_pool_allocator.cpp @@ -21,8 +21,6 @@ #if defined(LIBVA_SUPPORT) #include "mfx_va_frame_pool_allocator.h" -#include "mfx_c2_buffer_queue.h" - #include "mfx_c2_utils.h" #include "mfx_debug.h" #include "mfx_msdk_debug.h" @@ -89,13 +87,6 @@ mfxStatus MfxVaFramePoolAllocator::AllocFrames(mfxFrameAllocRequest *request, break; } -#ifdef MFX_BUFFER_QUEUE - res = std::static_pointer_cast(m_c2Allocator)->requestNewBufferSet(max_buffers); - if(res != C2_OK) { - mfx_res = MFX_ERR_MEMORY_ALLOC; - break; - } -#endif response->NumFrameActual = 0; #define RETRY_TIMES 5 for (int i = 0; i < opt_buffers; ++i) { diff --git a/plugin_store/Android.bp b/plugin_store/Android.bp deleted file mode 100644 index 55da2fcb..00000000 --- a/plugin_store/Android.bp +++ /dev/null @@ -1,49 +0,0 @@ -cc_library_shared { - name: "libc2plugin_store_celadon", - vendor: true, - - defaults: [ - "mfx_c2_defaults", - ], - - srcs: ["src/*.cpp"], - - export_include_dirs: [ - "include", - ], - - include_dirs: [ - "frameworks/av/media/codec2/vndk/include", - "frameworks/av/media/codec2/components/base/include", - "frameworks/av/media/codec2/core/include", - "frameworks/av/media/codec2/vndk/include", - "frameworks/av/media/libstagefright/include", - "frameworks/av/media/libstagefright/include", - "vendor/intel/mediasdk_c2/c2_utils/include", - ], - - header_libs: [ - "libcodec2_internal", - ], - - static_libs: [ - "libmfx_c2_utils_va" - ], - - shared_libs: [ - "android.hardware.graphics.bufferqueue@2.0", - "libcutils", - "libhardware", - "libhidlbase", - "libnativewindow", - "libstagefright_bufferqueue_helper", - "libstagefright_foundation", - "libui", - "libutilscallstack", - ], - - cflags: [ - "-Werror", - "-Wall", - ], -} diff --git a/plugin_store/include/mfx_c2_allocator_id.h b/plugin_store/include/mfx_c2_allocator_id.h deleted file mode 100755 index ff2a755e..00000000 --- a/plugin_store/include/mfx_c2_allocator_id.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2021 Intel Corporation -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#pragma once - -#include - -enum : C2AllocatorStore::id_t { - MFX_BUFFERQUEUE = android::C2PlatformAllocatorStore::PLATFORM_END, //0x14 -}; diff --git a/plugin_store/include/mfx_c2_buffer_queue.h b/plugin_store/include/mfx_c2_buffer_queue.h deleted file mode 100755 index 03cdc872..00000000 --- a/plugin_store/include/mfx_c2_buffer_queue.h +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) 2021 Intel Corporation -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#pragma once - -#include - -#include -#include - -#include - -class MfxC2BufferQueueBlockPool : public C2BlockPool { -public: - MfxC2BufferQueueBlockPool(const std::shared_ptr &allocator, const local_id_t localId); - - virtual C2Allocator::id_t getAllocatorId() const override { - return android::C2PlatformAllocatorStore::BUFFERQUEUE; - }; - - virtual local_id_t getLocalId() const override { - return local_id_; - }; - - virtual c2_status_t fetchGraphicBlock( - uint32_t width, - uint32_t height, - uint32_t format, - C2MemoryUsage usage, - std::shared_ptr *block /* nonnull */) override; - - typedef std::function OnRenderCallback; - - /** - * Sets render callback. - * - * \param renderCallbak callback to call for all dequeue buffer. - */ - virtual void setRenderCallback(const OnRenderCallback &renderCallback = OnRenderCallback()); - - typedef ::android::hardware::graphics::bufferqueue::V2_0:: - IGraphicBufferProducer HGraphicBufferProducer; - /** - * Configures an IGBP in order to create blocks. A newly created block is - * dequeued from the configured IGBP. Unique Id of IGBP and the slot number of - * blocks are passed via native_handle. Managing IGBP is responsibility of caller. - * When IGBP is not configured, block will be created via allocator. - * Since zero is not used for Unique Id of IGBP, if IGBP is not configured or producer - * is configured as nullptr, unique id which is bundled in native_handle is zero. - * - * \param producer the IGBP, which will be used to fetch blocks - */ - virtual void configureProducer(const android::sp &producer); - - virtual c2_status_t ImportHandle( - const std::shared_ptr block, buffer_handle_t *hndl); - virtual c2_status_t requestNewBufferSet(int32_t bufferCount); - - virtual bool outputSurfaceSet(void); - -private: - const std::shared_ptr mAllocator; - const local_id_t local_id_; - - class Impl; - std::shared_ptr impl_; - - friend struct MfxC2BufferQueueBlockPoolData; -}; diff --git a/plugin_store/src/mfx_c2_buffer_queue.cpp b/plugin_store/src/mfx_c2_buffer_queue.cpp deleted file mode 100755 index 9f029bfd..00000000 --- a/plugin_store/src/mfx_c2_buffer_queue.cpp +++ /dev/null @@ -1,780 +0,0 @@ -// Copyright (c) 2021 Intel Corporation -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - - -#include - -#include -#include -#include - -#include -#include - -#include -#include - -#include -#include -#include - -#include "mfx_debug.h" -#include "mfx_c2_debug.h" -#include "mfx_c2_buffer_queue.h" - -using ::android::BufferQueueDefs::NUM_BUFFER_SLOTS; -using ::android::C2AndroidMemoryUsage; -using ::android::Fence; -using ::android::GraphicBuffer; -using ::android::sp; -using ::android::status_t; -using ::android::wp; -using ::android::hardware::hidl_handle; -using ::android::hardware::Return; - -using HBuffer = ::android::hardware::graphics::common::V1_2::HardwareBuffer; -using HStatus = ::android::hardware::graphics::bufferqueue::V2_0::Status; -using ::android::hardware::graphics::bufferqueue::V2_0::utils::b2h; -using ::android::hardware::graphics::bufferqueue::V2_0::utils::h2b; -using ::android::hardware::graphics::bufferqueue::V2_0::utils::HFenceWrapper; - -using HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::V2_0 - ::IGraphicBufferProducer; -using namespace android; - -struct MfxC2BufferQueueBlockPoolData : public _C2BlockPoolData { - - bool held; - bool local; - uint32_t generation; - uint64_t bqId; - int32_t bqSlot; - bool transfer; // local transfer to remote - bool attach; // attach on remote - bool display; // display on remote; - std::weak_ptr owner; - sp igbp; - std::shared_ptr localPool; - mutable std::mutex lock; - - virtual type_t getType() const override { - return TYPE_BUFFERQUEUE; - } - - // Create a remote BlockPoolData. - MfxC2BufferQueueBlockPoolData( - uint32_t generation, uint64_t bqId, int32_t bqSlot, - const std::shared_ptr &owner, - const sp& producer); - - // Create a local BlockPoolData. - MfxC2BufferQueueBlockPoolData( - uint32_t generation, uint64_t bqId, int32_t bqSlot, - const std::shared_ptr& pool); - - virtual ~MfxC2BufferQueueBlockPoolData() override; - - int migrate(const sp& producer, - uint32_t toGeneration, uint64_t toBqId, - sp *buffers, uint32_t oldGeneration); -}; - -namespace { - -int64_t getTimestampNow() { - int64_t stamp; - struct timespec ts; - // TODO: CLOCK_MONOTONIC_COARSE? - clock_gettime(CLOCK_MONOTONIC, &ts); - stamp = ts.tv_nsec / 1000; - stamp += (ts.tv_sec * 1000000LL); - return stamp; -} - -bool getGenerationNumber(const sp &producer, - uint32_t *generation) { - MFX_DEBUG_TRACE_FUNC; - status_t status{}; - int slot{}; - bool bufferNeedsReallocation{}; - sp fence = new Fence(); - - using Input = HGraphicBufferProducer::DequeueBufferInput; - using Output = HGraphicBufferProducer::DequeueBufferOutput; - Return transResult = producer->dequeueBuffer( - Input{640, 480, HAL_PIXEL_FORMAT_YCBCR_420_888, 0}, - [&status, &slot, &bufferNeedsReallocation, &fence] - (HStatus hStatus, int32_t hSlot, Output const& hOutput) { - slot = static_cast(hSlot); - if (!h2b(hStatus, &status) || !h2b(hOutput.fence, &fence)) { - status = ::android::BAD_VALUE; - } else { - bufferNeedsReallocation = - hOutput.bufferNeedsReallocation; - } - }); - if (!transResult.isOk() || status != android::OK) { - return false; - } - HFenceWrapper hFenceWrapper{}; - if (!b2h(fence, &hFenceWrapper)) { - (void)producer->detachBuffer(static_cast(slot)).isOk(); - MFX_DEBUG_TRACE_PRINTF("Invalid fence received from dequeueBuffer."); - return false; - } - sp slotBuffer = new GraphicBuffer(); - // This assumes requestBuffer# returns an existing allocation - // instead of a new allocation. - transResult = producer->requestBuffer( - slot, - [&status, &slotBuffer, &generation]( - HStatus hStatus, - HBuffer const& hBuffer, - uint32_t generationNumber){ - if (h2b(hStatus, &status) && - h2b(hBuffer, &slotBuffer) && - slotBuffer) { - *generation = generationNumber; - slotBuffer->setGenerationNumber(generationNumber); - } else { - status = android::BAD_VALUE; - } - }); - if (!transResult.isOk()) { - return false; - } else if (status != android::NO_ERROR) { - (void)producer->detachBuffer(static_cast(slot)).isOk(); - return false; - } - (void)producer->detachBuffer(static_cast(slot)).isOk(); - return true; -} - -}; - -class MfxC2BufferQueueBlockPool::Impl - : public std::enable_shared_from_this { -private: - c2_status_t fetchFromIgbp_l( - uint32_t width, - uint32_t height, - uint32_t format, - C2MemoryUsage usage, - std::shared_ptr *block /* nonnull */) { - MFX_DEBUG_TRACE_FUNC; - // We have an IGBP now. - C2AndroidMemoryUsage androidUsage = usage; - status_t status{}; - int slot{}; - bool bufferNeedsReallocation{}; - sp fence = new Fence(); - - { // Call dequeueBuffer(). - using Input = HGraphicBufferProducer::DequeueBufferInput; - using Output = HGraphicBufferProducer::DequeueBufferOutput; - Return transResult = mProducer->dequeueBuffer( - Input{ - width, - height, - format, - androidUsage.asGrallocUsage()}, - [&status, &slot, &bufferNeedsReallocation, - &fence](HStatus hStatus, - int32_t hSlot, - Output const& hOutput) { - slot = static_cast(hSlot); - if (!h2b(hStatus, &status) || - !h2b(hOutput.fence, &fence)) { - status = ::android::BAD_VALUE; - } else { - bufferNeedsReallocation = - hOutput.bufferNeedsReallocation; - } - }); - if (!transResult.isOk() || status != android::OK) { - if (transResult.isOk()) { - ++mDqFailure; - if (status == android::INVALID_OPERATION || - status == android::TIMED_OUT || - status == android::WOULD_BLOCK) { - // Dequeue buffer is blocked temporarily. Retrying is - // required. - return C2_BLOCKING; - } - } - MFX_DEBUG_TRACE_PRINTF("cannot dequeue buffer %d", status); - return C2_BAD_VALUE; - } - mDqFailure = 0; - mLastDqTs = getTimestampNow(); - } - HFenceWrapper hFenceWrapper{}; - if (!b2h(fence, &hFenceWrapper)) { - MFX_DEBUG_TRACE_PRINTF("Invalid fence received from dequeueBuffer."); - return C2_BAD_VALUE; - } - MFX_DEBUG_TRACE_PRINTF("dequeued a buffer successfully"); - if (fence) { - static constexpr int kFenceWaitTimeMs = 10; - - status_t status = fence->wait(kFenceWaitTimeMs); - if (status == -ETIME) { - // fence is not signalled yet. - (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk(); - return C2_BLOCKING; - } - if (status != android::NO_ERROR) { - MFX_DEBUG_TRACE_PRINTF("buffer fence wait error %d", status); - (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk(); - return C2_BAD_VALUE; - } else if (mRenderCallback) { - nsecs_t signalTime = fence->getSignalTime(); - if (signalTime >= 0 && signalTime < INT64_MAX) { - mRenderCallback(mProducerId, slot, signalTime); - } else { - MFX_DEBUG_TRACE_PRINTF("got fence signal time of %lld", (long long)signalTime); - } - } - } - - sp &slotBuffer = mBuffers[slot]; - uint32_t outGeneration; - if (bufferNeedsReallocation || !slotBuffer) { - if (!slotBuffer) { - slotBuffer = new GraphicBuffer(); - } - // N.B. This assumes requestBuffer# returns an existing allocation - // instead of a new allocation. - Return transResult = mProducer->requestBuffer( - slot, - [&status, &slotBuffer, &outGeneration]( - HStatus hStatus, - HBuffer const& hBuffer, - uint32_t generationNumber){ - if (h2b(hStatus, &status) && - h2b(hBuffer, &slotBuffer) && - slotBuffer) { - slotBuffer->setGenerationNumber(generationNumber); - outGeneration = generationNumber; - } else { - status = android::BAD_VALUE; - } - }); - if (!transResult.isOk()) { - slotBuffer.clear(); - return C2_BAD_VALUE; - } else if (status != android::NO_ERROR) { - slotBuffer.clear(); - (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk(); - return C2_BAD_VALUE; - } - if (mGeneration == 0) { - // getting generation # lazily due to dequeue failure. - mGeneration = outGeneration; - } - } - if (slotBuffer) { - MFX_DEBUG_TRACE_PRINTF("buffer wraps %llu %d, handle:%p", (unsigned long long)mProducerId, slot,slotBuffer->handle); - C2Handle *c2Handle = WrapNativeCodec2GrallocHandle( - slotBuffer->handle, - slotBuffer->width, - slotBuffer->height, - slotBuffer->format, - slotBuffer->usage, - slotBuffer->stride, - slotBuffer->getGenerationNumber(), - mProducerId, slot); - if (c2Handle) { - std::shared_ptr alloc; - c2_status_t err = mAllocator->priorGraphicAllocation(c2Handle, &alloc); - if (err != C2_OK) { - native_handle_close(c2Handle); - native_handle_delete(c2Handle); - return err; - } - std::shared_ptr poolData = - std::make_shared( - slotBuffer->getGenerationNumber(), - mProducerId, slot, - shared_from_this()); - mPoolDatas[slot] = poolData; - *block = _C2BlockFactory::CreateGraphicBlock(alloc, poolData); - auto iter = gbuffer_.find(c2Handle); - if (iter == gbuffer_.end()) { - gbuffer_.emplace(c2Handle, slotBuffer->handle); - } else { - iter->second = slotBuffer->handle; - } - return C2_OK; - } - // Block was not created. call requestBuffer# again next time. - slotBuffer.clear(); - (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk(); - } - return C2_BAD_VALUE; - } - -public: - Impl(const std::shared_ptr &allocator) - : mInit(C2_OK), mProducerId(0), mGeneration(0), - mDqFailure(0), mLastDqTs(0), mLastDqLogTs(0), - mAllocator(allocator) { - } - - ~Impl() { - MFX_DEBUG_TRACE_FUNC; - bool noInit = false; - try { - for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) { - if (!noInit && mProducer) { - Return transResult = - mProducer->detachBuffer(static_cast(i)); - noInit = !transResult.isOk() || - static_cast(transResult) == HStatus::NO_INIT; - } - mBuffers[i].clear(); - } - gbuffer_.clear(); - } catch(const std::exception& e) { - MFX_DEBUG_TRACE_STREAM("Got an exception: " << e.what()); - } - } - - c2_status_t handle(const C2Handle * c2_hdl, buffer_handle_t *hndl){ - MFX_DEBUG_TRACE_FUNC; - auto iter = gbuffer_.find(c2_hdl); - if (iter != gbuffer_.end()) { - buffer_handle_t handle = iter->second; - if(handle != NULL) { - *hndl = handle; - return C2_OK; - } - } - MFX_DEBUG_TRACE_PRINTF("not BQ buffer" ); - return C2_CORRUPTED; - } - - c2_status_t fetchGraphicBlock( - uint32_t width, - uint32_t height, - uint32_t format, - C2MemoryUsage usage, - std::shared_ptr *block /* nonnull */) { - MFX_DEBUG_TRACE_FUNC; - block->reset(); - if (mInit != C2_OK) { - return mInit; - } - - static int kMaxIgbpRetryDelayUs = 10000; - - std::unique_lock lock(mMutex); - if (mLastDqLogTs == 0) { - mLastDqLogTs = getTimestampNow(); - } else { - int64_t now = getTimestampNow(); - if (now >= mLastDqLogTs + 5000000) { - if (now >= mLastDqTs + 1000000 || mDqFailure > 5) { - MFX_DEBUG_TRACE_PRINTF("last successful dequeue was %lld us ago, " - "%zu consecutive failures", - (long long)(now - mLastDqTs), mDqFailure); - } - mLastDqLogTs = now; - } - } - if (mProducerId == 0) { - std::shared_ptr alloc; - c2_status_t err = mAllocator->newGraphicAllocation( - width, height, format, usage, &alloc); - if (err != C2_OK) { - return err; - } - std::shared_ptr poolData = - std::make_shared( - 0, (uint64_t)0, ~0, shared_from_this()); - *block = _C2BlockFactory::CreateGraphicBlock(alloc, poolData); - MFX_DEBUG_TRACE_PRINTF("no producer allocated a buffer successfully"); - - return C2_OK; - } - c2_status_t status = fetchFromIgbp_l(width, height, format, usage, block); - if (status == C2_BLOCKING) { - lock.unlock(); - // in order not to drain cpu from component's spinning - ::usleep(kMaxIgbpRetryDelayUs); - } - - MFX_DEBUG_TRACE_I32(status); - return status; - } - - void setRenderCallback(const OnRenderCallback &renderCallback) { - MFX_DEBUG_TRACE_FUNC; - std::scoped_lock lock(mMutex); - mRenderCallback = renderCallback; - } - - void configureProducer(const sp &producer) { - MFX_DEBUG_TRACE_FUNC; - uint64_t producerId = 0; - uint32_t generation = 0; - bool haveGeneration = false; - if (producer) { - Return transResult = producer->getUniqueId(); - if (!transResult.isOk()) { - MFX_DEBUG_TRACE_PRINTF("configureProducer -- failed to connect to the producer"); - return; - } - producerId = static_cast(transResult); - // TODO: provide gneration number from parameter. - haveGeneration = getGenerationNumber(producer, &generation); - if (!haveGeneration) { - MFX_DEBUG_TRACE_PRINTF("get generationNumber failed %llu", - (unsigned long long)producerId); - } - } - int migrated = 0; - // poolDatas dtor should not be called during lock is held. - std::shared_ptr - poolDatas[NUM_BUFFER_SLOTS]; - { - sp buffers[NUM_BUFFER_SLOTS]; - std::scoped_lock lock(mMutex); - bool noInit = false; - for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) { - if (!noInit && mProducer) { - Return transResult = - mProducer->detachBuffer(static_cast(i)); - noInit = !transResult.isOk() || - static_cast(transResult) == HStatus::NO_INIT; - } - } - int32_t oldGeneration = mGeneration; - if (producer) { - mProducer = producer; - mProducerId = producerId; - mGeneration = haveGeneration ? generation : 0; - } else { - mProducer = nullptr; - mProducerId = 0; - mGeneration = 0; - MFX_DEBUG_TRACE_PRINTF("invalid producer producer(%d), generation(%d)", - (bool)producer, haveGeneration); - } - if (mProducer && haveGeneration) { // migrate buffers - for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) { - std::shared_ptr data = - mPoolDatas[i].lock(); - if (data) { - int slot = data->migrate( - mProducer, generation, - producerId, mBuffers, oldGeneration); - if (slot >= 0) { - buffers[slot] = mBuffers[i]; - mBuffers[i] = NULL; - poolDatas[slot] = data; - ++migrated; - } - } else { - // free buffer migrate - if (mBuffers[i]) { - sp const& graphicBuffer = mBuffers[i]; - graphicBuffer->setGenerationNumber(generation); - - HBuffer hBuffer{}; - uint32_t hGenerationNumber{}; - if (!b2h(graphicBuffer, &hBuffer, &hGenerationNumber)) - { - MFX_DEBUG_TRACE_PRINTF("I to O conversion failed"); - continue; - } - - bool converted{}; - status_t bStatus{}; - int slot_new; - int *outSlot = &slot_new; - Return transResult = - producer->attachBuffer(hBuffer, hGenerationNumber, - [&converted, &bStatus, outSlot]( - HStatus hStatus, int32_t hSlot, bool releaseAll) { - converted = h2b(hStatus, &bStatus); - *outSlot = static_cast(hSlot); - if (converted && releaseAll && bStatus == android::OK) - { - bStatus = android::INVALID_OPERATION; - } - }); - if (!transResult.isOk() || !converted || bStatus != android::OK) - { - MFX_DEBUG_TRACE_PRINTF("attach failed %d", static_cast(bStatus)); - continue; - } - MFX_DEBUG_TRACE_PRINTF("local migration from gen %u : %u slot %d : %d", - oldGeneration, generation, i, slot_new); - - buffers[slot_new] = mBuffers[i]; - producer->cancelBuffer(slot_new, hidl_handle{}).isOk(); - ++migrated; - } - } - } - } - for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) { - mBuffers[i] = buffers[i]; - mPoolDatas[i] = poolDatas[i]; - } - } - if (producer && haveGeneration) { - MFX_DEBUG_TRACE_PRINTF("local generation change %u , " - "bqId: %llu migrated buffers # %d", - generation, (unsigned long long)producerId, migrated); - } - } - - c2_status_t requestNewBufferSet(int32_t bufferCount) { - MFX_DEBUG_TRACE_FUNC; - - if (bufferCount <= 0) { - MFX_DEBUG_TRACE_PRINTF("Invalid requested buffer count = %d", bufferCount); - return C2_BAD_VALUE; - } - - std::lock_guard lock(mMutex); - if (!mProducer) { - MFX_DEBUG_TRACE_PRINTF("No HGraphicBufferProducer is configured..."); - return C2_NO_INIT; - } - - mProducer->setMaxDequeuedBufferCount(bufferCount); - return C2_OK; - } - - void getBufferQueueProducer(sp &producer) { - producer = mProducer; - } - -private: - friend struct MfxC2BufferQueueBlockPoolData; - - void cancel(uint32_t generation, uint64_t igbp_id, int32_t igbp_slot) { - bool cancelled = false; - { - MFX_DEBUG_TRACE_FUNC; - try { - std::scoped_lock lock(mMutex); - if (generation == mGeneration && igbp_id == mProducerId && mProducer) { - (void)mProducer->cancelBuffer(igbp_slot, hidl_handle{}).isOk(); - cancelled = true; - } - } catch(const std::exception& e) { - MFX_DEBUG_TRACE_STREAM("Got an exception: " << e.what()); - } - } - } - - c2_status_t mInit; - uint64_t mProducerId; - uint32_t mGeneration; - OnRenderCallback mRenderCallback; - - size_t mDqFailure; - int64_t mLastDqTs; - int64_t mLastDqLogTs; - - const std::shared_ptr mAllocator; - - std::mutex mMutex; - sp mProducer; - sp mSavedProducer; - - sp mBuffers[NUM_BUFFER_SLOTS]; - std::weak_ptr mPoolDatas[NUM_BUFFER_SLOTS]; - std::map gbuffer_; -}; - -MfxC2BufferQueueBlockPoolData::MfxC2BufferQueueBlockPoolData( - uint32_t generation, uint64_t bqId, int32_t bqSlot, - const std::shared_ptr& owner, - const sp& producer) : - held(producer && bqId != 0), local(false), - generation(generation), bqId(bqId), bqSlot(bqSlot), - transfer(false), attach(false), display(false), - owner(owner), igbp(producer), - localPool() { - MFX_DEBUG_TRACE_FUNC; -} - -MfxC2BufferQueueBlockPoolData::MfxC2BufferQueueBlockPoolData( - uint32_t generation, uint64_t bqId, int32_t bqSlot, - const std::shared_ptr& pool) : - held(true), local(true), - generation(generation), bqId(bqId), bqSlot(bqSlot), - transfer(false), attach(false), display(false), - igbp(pool ? pool->mProducer : nullptr), - localPool(pool) { - MFX_DEBUG_TRACE_FUNC; -} - -MfxC2BufferQueueBlockPoolData::~MfxC2BufferQueueBlockPoolData() { - MFX_DEBUG_TRACE_FUNC; - if (!held || bqId == 0) { - return; - } - if (local) { - if (localPool) { - localPool->cancel(generation, bqId, bqSlot); - } - } else if (igbp && !owner.expired()) { - (void)igbp->cancelBuffer(bqSlot, hidl_handle{}).isOk(); - } -} - -int MfxC2BufferQueueBlockPoolData::migrate( - const sp& producer, - uint32_t toGeneration, uint64_t toBqId, - sp *buffers, uint32_t oldGeneration) { - MFX_DEBUG_TRACE_FUNC; - std::scoped_lock l(lock); - if (!held || bqId == 0) { - MFX_DEBUG_TRACE_PRINTF("buffer is not owned"); - return -1; - } - if (!local || !localPool) { - MFX_DEBUG_TRACE_PRINTF("pool is not local"); - return -1; - } - if (bqSlot < 0 || bqSlot >= NUM_BUFFER_SLOTS || !buffers[bqSlot]) { - MFX_DEBUG_TRACE_PRINTF("slot is not in effect"); - return -1; - } - if (toGeneration == generation && bqId == toBqId) { - MFX_DEBUG_TRACE_PRINTF("cannot migrate to same bufferqueue"); - return -1; - } - if (oldGeneration != generation) { - MFX_DEBUG_TRACE_PRINTF("cannot migrate stale buffer"); - } - if (transfer) { - // either transferred or detached. - MFX_DEBUG_TRACE_PRINTF("buffer is in transfer"); - return -1; - } - sp const& graphicBuffer = buffers[bqSlot]; - graphicBuffer->setGenerationNumber(toGeneration); - - HBuffer hBuffer{}; - uint32_t hGenerationNumber{}; - if (!b2h(graphicBuffer, &hBuffer, &hGenerationNumber)) { - MFX_DEBUG_TRACE_PRINTF("I to O conversion failed"); - return -1; - } - - bool converted{}; - status_t bStatus{}; - int slot; - int *outSlot = &slot; - Return transResult = - producer->attachBuffer(hBuffer, hGenerationNumber, - [&converted, &bStatus, outSlot]( - HStatus hStatus, int32_t hSlot, bool releaseAll) { - converted = h2b(hStatus, &bStatus); - *outSlot = static_cast(hSlot); - if (converted && releaseAll && bStatus == android::OK) { - bStatus = android::INVALID_OPERATION; - } - }); - if (!transResult.isOk() || !converted || bStatus != android::OK) { - MFX_DEBUG_TRACE_PRINTF("attach failed %d", static_cast(bStatus)); - return -1; - } - MFX_DEBUG_TRACE_PRINTF("local migration from gen %u : %u slot %d : %d", - generation, toGeneration, bqSlot, slot); - generation = toGeneration; - bqId = toBqId; - bqSlot = slot; - return slot; -} -MfxC2BufferQueueBlockPool::MfxC2BufferQueueBlockPool( - const std::shared_ptr &allocator, const local_id_t localId) - : mAllocator(allocator), local_id_(localId), impl_(new Impl(allocator)) {} - -c2_status_t MfxC2BufferQueueBlockPool::fetchGraphicBlock( - uint32_t width, - uint32_t height, - uint32_t format, - C2MemoryUsage usage, - std::shared_ptr *block /* nonnull */) { - MFX_DEBUG_TRACE_FUNC; - if (impl_) { - return impl_->fetchGraphicBlock(width, height, format, usage, block); - } - return C2_CORRUPTED; -} - -void MfxC2BufferQueueBlockPool::configureProducer(const sp &producer) { - MFX_DEBUG_TRACE_FUNC; - ALOGE("MfxC2BufferQueueBlockPool::configureProducer, line:%d", __LINE__); - if (impl_) { - impl_->configureProducer(producer); - } -} - -void MfxC2BufferQueueBlockPool::setRenderCallback(const OnRenderCallback &renderCallback) { - MFX_DEBUG_TRACE_FUNC; - ALOGE("MfxC2BufferQueueBlockPool::setRenderCallback, line:%d", __LINE__); - if (impl_) { - impl_->setRenderCallback(renderCallback); - } -} - -c2_status_t MfxC2BufferQueueBlockPool::ImportHandle(const std::shared_ptr block, buffer_handle_t *hndl){ - MFX_DEBUG_TRACE_FUNC; - uint32_t width, height, format, stride, igbp_slot, generation; - uint64_t usage, igbp_id; - _UnwrapNativeCodec2GrallocMetadata(block->handle(), &width, &height, - &format, &usage, &stride, &generation, &igbp_id, - &igbp_slot); - - if (impl_ && igbp_slot < NUM_BUFFER_SLOTS) { - return impl_->handle(block->handle(), hndl); - } - - MFX_DEBUG_TRACE_PRINTF("invalid C2GraphicBlock, igbp_slot = %d", igbp_slot); - return C2_CORRUPTED; -} -c2_status_t MfxC2BufferQueueBlockPool::requestNewBufferSet(int32_t bufferCount) { - if (impl_) { - return impl_->requestNewBufferSet(bufferCount); - } - return C2_NO_INIT; -} - -bool MfxC2BufferQueueBlockPool::outputSurfaceSet(void) { - if (impl_) { - sp producer; - impl_->getBufferQueueProducer(producer); - - if (!producer) { - ALOGI("No HGraphicBufferProducer is configured..."); - return false; - } - } - return true; -} diff --git a/plugin_store/src/mfx_c2_plugin_store.cpp b/plugin_store/src/mfx_c2_plugin_store.cpp deleted file mode 100755 index 27c69671..00000000 --- a/plugin_store/src/mfx_c2_plugin_store.cpp +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2021 Intel Corporation -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -#include -#include -#include -#include - -#include - -#include "mfx_c2_buffer_queue.h" -#include "mfx_c2_allocator_id.h" -#include "mfx_debug.h" -#include "mfx_c2_debug.h" - -namespace android { - -C2Allocator* createAllocator(C2Allocator::id_t allocatorId) { - MFX_DEBUG_TRACE_FUNC; - MFX_DEBUG_TRACE_PRINTF("%s(): Fallback to create C2AllocatorGralloc (id=%u)", __func__, allocatorId); - return new C2AllocatorGralloc(allocatorId, true); -} - -std::shared_ptr fetchAllocator(C2Allocator::id_t allocatorId) { - MFX_DEBUG_TRACE_FUNC; - MFX_DEBUG_TRACE_PRINTF("%s(allocatorId=%d)", __func__, allocatorId); - static std::mutex sMutex; - static std::map> sCacheAllocators; - - std::lock_guard lock(sMutex); - - std::shared_ptr allocator; - auto iter = sCacheAllocators.find(allocatorId); - if (iter != sCacheAllocators.end()) { - allocator = iter->second.lock(); - if (allocator != nullptr) { - return allocator; - } - } - - allocator.reset(createAllocator(allocatorId)); - sCacheAllocators[allocatorId] = allocator; - return allocator; -} - -C2BlockPool* createBlockPool(C2Allocator::id_t allocatorId, C2BlockPool::local_id_t poolId) { - MFX_DEBUG_TRACE_FUNC; - MFX_DEBUG_TRACE_PRINTF("%s(allocatorId=%d, poolId=%" PRIu64 ")", __func__, allocatorId, poolId); - - std::shared_ptr allocator = fetchAllocator(allocatorId); - if (allocator == nullptr) { - MFX_DEBUG_TRACE_PRINTF("%s(): Failed to create allocator id=%u", __func__, allocatorId); - return nullptr; - } - - switch (allocatorId) { - case MFX_BUFFERQUEUE: - MFX_DEBUG_TRACE_PRINTF("%s(new MfxC2BufferQueueBlockPool allocatorId=%d, poolId=%" PRIu64 ")", __func__, allocatorId, poolId); - return new MfxC2BufferQueueBlockPool(allocator, poolId); - default: - MFX_DEBUG_TRACE_PRINTF("%s(): Unknown allocator id=%u", __func__, allocatorId); - return nullptr; - } -} - -} // namespace android - -extern "C" ::C2BlockPool* CreateBlockPool(::C2Allocator::id_t allocatorId, - ::C2BlockPool::local_id_t poolId) { - MFX_DEBUG_TRACE_FUNC; - MFX_DEBUG_TRACE_PRINTF("%s(allocatorId=%d, poolId=%" PRIu64 ")", __func__, allocatorId, poolId); - return ::android::createBlockPool(allocatorId, poolId); -} - -extern "C" ::C2Allocator* CreateAllocator(::C2Allocator::id_t allocatorId, ::c2_status_t* status) { - MFX_DEBUG_TRACE_FUNC; - MFX_DEBUG_TRACE_PRINTF("%s(allocatorId=%d)", __func__, allocatorId); - - ::C2Allocator* res = ::android::createAllocator(allocatorId); - *status = (res != nullptr) ? C2_OK : C2_BAD_INDEX; - return res; -}