From f8233998a2a8bae420ee44b470664a8f6ef95264 Mon Sep 17 00:00:00 2001 From: chaoxizhang Date: Wed, 11 Dec 2024 18:36:12 +0800 Subject: [PATCH] Fix external texture flush task after engine destroyed will be caused SIGSEGV crash. Signed-off-by: chaoxizhang Remove unused OhosImageFrameData struct Signed-off-by: chaoxizhang --- .../ohos/ohos_external_texture_gl.cpp | 67 +++++++------------ .../platform/ohos/ohos_external_texture_gl.h | 22 +----- 2 files changed, 25 insertions(+), 64 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index f160e09469..189fc2bb19 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -104,7 +104,6 @@ OHOSExternalTextureGL::OHOSExternalTextureGL( backGroundPixelMap_ = nullptr; lastImage_ = nullptr; isEmulator_ = OhosMain::IsEmulator(); - frameData_ = nullptr; } OHOSExternalTextureGL::~OHOSExternalTextureGL() @@ -125,24 +124,37 @@ OHOSExternalTextureGL::~OHOSExternalTextureGL() lastImage_ = nullptr; } -void OnNativeImageFrameAvailable(void *data) +void OnNativeImageFrameAvailable(void *context) { - auto frameData = reinterpret_cast(data); - if (frameData == nullptr) { - FML_LOG(ERROR) << "OnNativeImageFrameAvailable, frameData is null."; + auto ohosExternalTextureGL = reinterpret_cast(context); + if (ohosExternalTextureGL == nullptr) { + FML_LOG(ERROR) << "OnNativeImageFrameAvailable, ohosExternalTextureGL is null."; return; } - frameData->OnPlatformViewMarkTextureFrameAvailable(); + + if (ohosExternalTextureGL != nullptr) { + auto wp_ohosExternalTextureGL = std::weak_ptr( + ohosExternalTextureGL->shared_from_this()); + fml::TaskRunner::RunNowOrPostTask( + ohosExternalTextureGL->task_runners_.GetPlatformTaskRunner(), + [wp_ohosExternalTextureGL]() { + if (auto sp_ohosExternalTextureGL = wp_ohosExternalTextureGL.lock()) { + PlatformView::Delegate& delegate = + sp_ohosExternalTextureGL->delegate_; + delegate.OnPlatformViewMarkTextureFrameAvailable(sp_ohosExternalTextureGL->Id()); + } + }); + } } -bool RegisterFrameAvailableListener(OH_NativeImage *nativeImage, OhosImageFrameData *frameData) +bool RegisterFrameAvailableListener(OH_NativeImage *nativeImage, void *context) { - if (frameData == nullptr) { - FML_LOG(ERROR) << "Error with RegisterFrameAvailableListener, frameData is null"; + if (context == nullptr) { + FML_LOG(ERROR) << "Error with RegisterFrameAvailableListener, context is null"; return false; } OH_OnFrameAvailableListener listener; - listener.context = frameData; + listener.context = context; listener.onFrameAvailable = OnNativeImageFrameAvailable; int64_t ret = OH_NativeImage_SetOnFrameAvailableListener(nativeImage, listener); if (ret != 0) { @@ -191,12 +203,7 @@ void OHOSExternalTextureGL::Attach() FML_LOG(ERROR) << "OHOSExternalTextureGL OH_NativeImage_AttachContext err code:" << ret; } - if (frameData_ == nullptr) { - frameData_ = new OhosImageFrameData(this, Id()); - } - if (!RegisterFrameAvailableListener(nativeImage_, (OhosImageFrameData *)frameData_)) { - delete (OhosImageFrameData *)frameData_; - frameData_ = nullptr; + if (!RegisterFrameAvailableListener(nativeImage_, this)) { return; } state_ = AttachmentState::attached; @@ -375,10 +382,6 @@ void OHOSExternalTextureGL::Detach() OH_NativeImage_Destroy(&nativeImage_); nativeImage_ = nullptr; } - if (frameData_ != nullptr) { - delete (OhosImageFrameData *)frameData_; - frameData_ = nullptr; - } if (nativeWindow_ != nullptr) { OH_NativeWindow_DestroyNativeWindow(nativeWindow_); nativeWindow_ = nullptr; @@ -770,28 +773,4 @@ void OHOSExternalTextureGL::DispatchBackGroundPixelMap(NativePixelMap* pixelMap) } } -OhosImageFrameData::OhosImageFrameData( - OHOSExternalTextureGL *ohosExternalTextureGL, - int64_t textureId) - : ohosExternalTextureGL(ohosExternalTextureGL), - textureId_(textureId) -{} - -OhosImageFrameData::~OhosImageFrameData() -{ - ohosExternalTextureGL = nullptr; - textureId_ = 0; -} - -void OhosImageFrameData::OnPlatformViewMarkTextureFrameAvailable() -{ - if (ohosExternalTextureGL != nullptr) { - fml::TaskRunner::RunNowOrPostTask( - ohosExternalTextureGL->task_runners_.GetPlatformTaskRunner(), [textureId = textureId_, this]() { - PlatformView::Delegate& dalegate = this->ohosExternalTextureGL->delegate_; - dalegate.OnPlatformViewMarkTextureFrameAvailable(textureId); - }); - } -} - } // namespace flutter \ No newline at end of file diff --git a/shell/platform/ohos/ohos_external_texture_gl.h b/shell/platform/ohos/ohos_external_texture_gl.h index 24beb3279d..5ddd50b573 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.h +++ b/shell/platform/ohos/ohos_external_texture_gl.h @@ -36,7 +36,8 @@ // maybe now unused namespace flutter { -class OHOSExternalTextureGL : public flutter::Texture { +// Represents an external texture for OHOS platform, using std::weak_ptr to avoid circular reference. +class OHOSExternalTextureGL : public flutter::Texture, public std::enable_shared_from_this { public: explicit OHOSExternalTextureGL(int64_t id, const std::shared_ptr& ohos_surface); explicit OHOSExternalTextureGL(int64_t id, const std::shared_ptr& ohos_surface, @@ -52,8 +53,6 @@ class OHOSExternalTextureGL : public flutter::Texture { OH_NativeImage *backGroundNativeImage_; - void *frameData_; - bool first_update_ = false; void Paint(PaintContext& context, @@ -145,22 +144,5 @@ class OHOSExternalTextureGL : public flutter::Texture { bool IsContextCurrent(); }; -class OhosImageFrameData { - public: - OhosImageFrameData(OHOSExternalTextureGL *ohosExternalTextureGL, int64_t textureId); - - OhosImageFrameData() = delete; - - ~OhosImageFrameData(); - - void OnPlatformViewMarkTextureFrameAvailable(); - - private: - - OHOSExternalTextureGL *ohosExternalTextureGL; - - int64_t textureId_; -}; - } // namespace flutter #endif \ No newline at end of file -- Gitee