From a0bfde5c236d9886db9cdaa2c19641f4bab866d1 Mon Sep 17 00:00:00 2001 From: liujiake Date: Wed, 25 Dec 2024 20:27:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=96=E6=8E=A5?= =?UTF-8?q?=E7=BA=B9=E7=90=86=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E5=87=BD?= =?UTF-8?q?=E6=95=B0OnGrContextDestroyed=E7=9A=84=E5=86=85=E5=AE=B9?= =?UTF-8?q?=EF=BC=8C=E5=8F=8A=E6=97=B6=E9=87=8A=E6=94=BE=E7=BA=B9=E7=90=86?= =?UTF-8?q?=E8=B5=84=E6=BA=90=EF=BC=8C=E9=81=BF=E5=85=8D=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liujiake --- .../ohos/ohos_external_texture_gl.cpp | 103 +++++++++++++++--- .../platform/ohos/ohos_external_texture_gl.h | 6 +- shell/platform/ohos/platform_view_ohos.cpp | 6 +- 3 files changed, 98 insertions(+), 17 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index 8078821bb1..23ee4e2c6a 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -280,14 +280,74 @@ void OHOSExternalTextureGL::Paint(PaintContext& context, } } +void OHOSExternalTextureGL::Show() +{ + if (state_ != AttachmentState::hide) { + FML_LOG(WARNING) << "OHOSExternalTextureGL show, state is not hide, ignore this call"; + return; + } + OHOSSurface* ohos_surface_ptr = ohos_surface_.get(); + OhosSurfaceGLSkia* ohosSurfaceGLSkia_ = (OhosSurfaceGLSkia*)ohos_surface_ptr; + if (ohosSurfaceGLSkia_->GetOnscreenSurface() == nullptr) { + FML_LOG(ERROR) << "OHOSExternalTextureGL show, GetSurface failed"; + return; + } + auto result = ohosSurfaceGLSkia_->GLContextMakeCurrent(); + if (result->GetResult()) { + FML_DLOG(INFO) << "OHOSExternalTextureGL show, MakeCurrent successed"; + glGenTextures(1, &texture_name_); + FML_DLOG(INFO) << "OHOSExternalTextureGL show, glGenTextures texture_name_=" + << texture_name_ << ", Id()=" << Id(); + int32_t ret = OH_NativeImage_AttachContext(nativeImage_, texture_name_); + if (ret != 0) { + FML_LOG(ERROR) << "OHOSExternalTextureGL show, AttachContext failed, err code:" << ret; + } + state_ = AttachmentState::attached; + } else { + FML_LOG(ERROR) << "OHOSExternalTextureGL show, MakeCurrent failed"; + return; + } +} + void OHOSExternalTextureGL::OnGrContextCreated() { FML_DLOG(INFO) << " OHOSExternalTextureGL::OnGrContextCreated" << ", texture_name_=" << texture_name_ << ", Id()=" << Id(); - if (state_ == AttachmentState::attached) { - delegate_.OnPlatformViewMarkTextureFrameAvailable(Id()); + fml::TaskRunner::RunNowOrPostTask( + task_runners_.GetRasterTaskRunner(), + [this]() { + Show(); + }); + fml::TaskRunner::RunNowOrPostTask( + task_runners_.GetPlatformTaskRunner(), + [this]() { + delegate_.OnPlatformViewMarkTextureFrameAvailable(Id()); + }); +} + +void OHOSExternalTextureGL::Hide() +{ + if (state_ != AttachmentState::attached) { + FML_LOG(WARNING) << "OHOSExternalTextureGL hide, state is not attached, ignore this call"; + return; + } + if (nativeImage_ != nullptr) { + OH_NativeImage_DetachContext(nativeImage_); + if (backGroundTextureName_ != 0) { + glDeleteTextures(1, &texture_name_); + texture_name_ = 0; + } + } + if (backGroundNativeWindow_ != nullptr) { + OH_NativeImage_DetachContext(backGroundNativeImage_); + if (backGroundTextureName_ != 0) { + infoMap.erase(backGroundTextureName_); + glDeleteTextures(1, &backGroundTextureName_); + backGroundTextureName_ = 0; + } } + state_ = AttachmentState::hide; } void OHOSExternalTextureGL::OnGrContextDestroyed() @@ -295,6 +355,11 @@ void OHOSExternalTextureGL::OnGrContextDestroyed() FML_DLOG(INFO) << " OHOSExternalTextureGL::OnGrContextDestroyed" << ", texture_name_=" << texture_name_ << ", Id()=" << Id(); + fml::TaskRunner::RunNowOrPostTask( + task_runners_.GetRasterTaskRunner(), + [this]() { + Hide(); + }); } void OHOSExternalTextureGL::MarkNewFrameAvailable() @@ -320,10 +385,14 @@ void OHOSExternalTextureGL::OnTextureUnregistered() << ", nativeImage_=" << nativeImage_ << ", backGroundNativeImage_=" << backGroundNativeImage_; first_update_ = false; - if (state_ == AttachmentState::attached) { - Detach(); - state_ = AttachmentState::detached; - } + fml::TaskRunner::RunNowOrPostTask( + task_runners_.GetRasterTaskRunner(), + [this]() { + if (state_ == AttachmentState::attached || state_ == AttachmentState::hide) { + Detach(); + state_ = AttachmentState::detached; + } + }); } bool OHOSExternalTextureGL::IsContextCurrent() @@ -372,12 +441,14 @@ void OHOSExternalTextureGL::Update() void OHOSExternalTextureGL::Detach() { FML_LOG(INFO) << "OHOSExternalTextureGL::Detach, texture_name_=" << texture_name_; - if (state_ != AttachmentState::attached) { - FML_LOG(ERROR) << "OHOSExternalTextureGL::Detach, the current status is not attached"; + if (state_ != AttachmentState::attached && state_ != AttachmentState::hide) { + FML_LOG(ERROR) << "OHOSExternalTextureGL::Detach, the current status is not attached or hide"; return; } if (nativeImage_ != nullptr) { - OH_NativeImage_DetachContext(nativeImage_); + if (state_ == AttachmentState::attached) { + OH_NativeImage_DetachContext(nativeImage_); + } OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); OH_NativeImage_Destroy(&nativeImage_); nativeImage_ = nullptr; @@ -388,7 +459,9 @@ void OHOSExternalTextureGL::Detach() } if (backGroundNativeImage_ != nullptr) { - OH_NativeImage_DetachContext(backGroundNativeImage_); + if (state_ == AttachmentState::attached) { + OH_NativeImage_DetachContext(backGroundNativeImage_); + } OH_NativeImage_Destroy(&backGroundNativeImage_); backGroundNativeImage_ = nullptr; } @@ -396,10 +469,12 @@ void OHOSExternalTextureGL::Detach() OH_NativeWindow_DestroyNativeWindow(backGroundNativeWindow_); backGroundNativeWindow_ = nullptr; } - glDeleteTextures(1, &texture_name_); - glDeleteTextures(1, &backGroundTextureName_); - if (backGroundTextureName_ != 0) { - infoMap.erase(backGroundTextureName_); + if (state_ == AttachmentState::attached) { + glDeleteTextures(1, &texture_name_); + glDeleteTextures(1, &backGroundTextureName_); + if (backGroundTextureName_ != 0) { + infoMap.erase(backGroundTextureName_); + } } } diff --git a/shell/platform/ohos/ohos_external_texture_gl.h b/shell/platform/ohos/ohos_external_texture_gl.h index 5ddd50b573..113aeb5296 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.h +++ b/shell/platform/ohos/ohos_external_texture_gl.h @@ -87,6 +87,10 @@ class OHOSExternalTextureGL : public flutter::Texture, public std::enable_shared void Detach(); + void Hide(); + + void Show(); + void UpdateTransform(OH_NativeImage *image); EGLDisplay GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list); @@ -101,7 +105,7 @@ class OHOSExternalTextureGL : public flutter::Texture, public std::enable_shared void ProducePixelMapToBackGroundImage(); - enum class AttachmentState { uninitialized, attached, detached }; + enum class AttachmentState { uninitialized, attached, detached, hide }; AttachmentState state_; diff --git a/shell/platform/ohos/platform_view_ohos.cpp b/shell/platform/ohos/platform_view_ohos.cpp index 58b1056978..2ba37590a3 100644 --- a/shell/platform/ohos/platform_view_ohos.cpp +++ b/shell/platform/ohos/platform_view_ohos.cpp @@ -120,8 +120,10 @@ PlatformViewOHOS::~PlatformViewOHOS() { for (auto const &it : external_texture_gl_) { if (it.second != nullptr) { FML_LOG(INFO) << " nativeImage of textureId " << it.first << " will destroy"; - OH_NativeImage_Destroy(&(it.second->nativeImage_)); - it.second->nativeImage_ = nullptr; + if (it.second->nativeImage_ != nullptr) { + OH_NativeImage_Destroy(&(it.second->nativeImage_)); + it.second->nativeImage_ = nullptr; + } } } external_texture_gl_.clear(); -- Gitee From dd1ba04cf855b6579db58a81173c089d9343e3a2 Mon Sep 17 00:00:00 2001 From: liujiake Date: Wed, 25 Dec 2024 21:35:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=96=E6=8E=A5?= =?UTF-8?q?=E7=BA=B9=E7=90=86OnGrContextCreated=E4=B8=AD=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=BA=B9=E7=90=86=E5=92=8C=E8=A7=A6=E5=8F=91paint=E7=9A=84?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=EF=BC=8C=E7=A1=AE=E4=BF=9D=E7=BA=BF=E6=80=A7?= =?UTF-8?q?=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liujiake --- .../ohos/ohos_external_texture_gl.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index 23ee4e2c6a..69b1a5646b 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -303,6 +303,11 @@ void OHOSExternalTextureGL::Show() FML_LOG(ERROR) << "OHOSExternalTextureGL show, AttachContext failed, err code:" << ret; } state_ = AttachmentState::attached; + fml::TaskRunner::RunNowOrPostTask( + task_runners_.GetPlatformTaskRunner(), + [this]() { + delegate_.OnPlatformViewMarkTextureFrameAvailable(Id()); + }); } else { FML_LOG(ERROR) << "OHOSExternalTextureGL show, MakeCurrent failed"; return; @@ -319,11 +324,6 @@ void OHOSExternalTextureGL::OnGrContextCreated() [this]() { Show(); }); - fml::TaskRunner::RunNowOrPostTask( - task_runners_.GetPlatformTaskRunner(), - [this]() { - delegate_.OnPlatformViewMarkTextureFrameAvailable(Id()); - }); } void OHOSExternalTextureGL::Hide() @@ -386,13 +386,13 @@ void OHOSExternalTextureGL::OnTextureUnregistered() << ", backGroundNativeImage_=" << backGroundNativeImage_; first_update_ = false; fml::TaskRunner::RunNowOrPostTask( - task_runners_.GetRasterTaskRunner(), - [this]() { - if (state_ == AttachmentState::attached || state_ == AttachmentState::hide) { - Detach(); - state_ = AttachmentState::detached; - } - }); + task_runners_.GetRasterTaskRunner(), + [this]() { + if (state_ == AttachmentState::attached || state_ == AttachmentState::hide) { + Detach(); + state_ = AttachmentState::detached; + } + }); } bool OHOSExternalTextureGL::IsContextCurrent() -- Gitee