diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index 8c8d58f72aef821c8fb82f54d8dbb4dfe7eaa046..f52ffd16706f4c899b9599775dcb67d59bfbc5dd 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -42,7 +42,6 @@ constexpr const char *EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT"; constexpr uint32_t WHITE_COLOR = 0xFFFFFFFF; const SkScalar DEFAULT_MATRIX[] = {1, 0, 0, 0, -1, 1, 0, 0, 1}; -const int UPDATE_FRAME_COUNT = 2; static int PixelMapToWindowFormat(PIXEL_FORMAT pixel_format) { @@ -83,8 +82,10 @@ static bool IsPixelMapYUVFormat(PIXEL_FORMAT format) OHOSExternalTextureGL::OHOSExternalTextureGL( int64_t id, - const std::shared_ptr& ohos_surface) + const std::shared_ptr& ohos_surface, + PlatformView::Delegate& delegate) : Texture(id), + delegate_(delegate), ohos_surface_(std::move(ohos_surface)), transform(SkMatrix::I()) { @@ -99,6 +100,7 @@ OHOSExternalTextureGL::OHOSExternalTextureGL( backGroundPixelMap_ = nullptr; lastImage_ = nullptr; isEmulator_ = OhosMain::IsEmulator(); + frameData_ = nullptr; } OHOSExternalTextureGL::~OHOSExternalTextureGL() @@ -119,6 +121,33 @@ OHOSExternalTextureGL::~OHOSExternalTextureGL() lastImage_ = nullptr; } +void OnNativeImageFrameAvailable(void *data) +{ + auto frameData = reinterpret_cast(data); + if (frameData == nullptr) { + FML_LOG(ERROR) << "OnNativeImageFrameAvailable, frameData is null."; + return; + } + frameData->OnPlatformViewMarkTextureFrameAvailable(); +} + +bool RegisterFrameAvailableListener(OH_NativeImage *nativeImage, OhosImageFrameData *frameData) +{ + if (frameData == nullptr) { + FML_LOG(ERROR) << "Error with RegisterFrameAvailableListener, frameData is null"; + return false; + } + OH_OnFrameAvailableListener listener; + listener.context = frameData; + listener.onFrameAvailable = OnNativeImageFrameAvailable; + int64_t ret = OH_NativeImage_SetOnFrameAvailableListener(nativeImage, listener); + if (ret != 0) { + FML_LOG(ERROR) << "Error with OH_NativeImage_SetOnFrameAvailableListener"; + return false; + } + return true; +} + void OHOSExternalTextureGL::Attach() { FML_DLOG(INFO) << "Attach, texture_name_=" << texture_name_ @@ -153,6 +182,15 @@ void OHOSExternalTextureGL::Attach() if (ret != 0) { 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; + return; + } state_ = AttachmentState::attached; } else { FML_LOG(ERROR) << "ResourceContextMakeCurrent failed"; @@ -173,17 +211,12 @@ void OHOSExternalTextureGL::Paint(PaintContext& context, if (pixelMap_ != nullptr) { // 外接纹理图片场景 ProducePixelMapToNativeImage(); - newFrameCount++; + Update(); } } - if (!freeze && newFrameCount > 0) { - Update(); - new_frame_ready_ = false; - newFrameCount--; - } GrGLTextureInfo textureInfo; - if (!freeze && !first_update_ && !isEmulator_ && !new_frame_ready_ && pixelMap_ == nullptr) { + if (!freeze && !first_update_ && !isEmulator_ && pixelMap_ == nullptr) { setBackground(bounds.width(), bounds.height()); textureInfo = {GL_TEXTURE_EXTERNAL_OES, backGroundTextureName_, GL_RGBA8_OES}; } else { @@ -226,7 +259,9 @@ void OHOSExternalTextureGL::OnGrContextCreated() FML_DLOG(INFO) << " OHOSExternalTextureGL::OnGrContextCreated" << ", texture_name_=" << texture_name_ << ", Id()=" << Id(); - state_ = AttachmentState::uninitialized; + if (state_ == AttachmentState::attached) { + delegate_.OnPlatformViewMarkTextureFrameAvailable(Id()); + } } void OHOSExternalTextureGL::OnGrContextDestroyed() @@ -234,17 +269,14 @@ void OHOSExternalTextureGL::OnGrContextDestroyed() FML_DLOG(INFO) << " OHOSExternalTextureGL::OnGrContextDestroyed" << ", texture_name_=" << texture_name_ << ", Id()=" << Id(); - if (state_ == AttachmentState::attached) { - Detach(); - } - state_ = AttachmentState::detached; } void OHOSExternalTextureGL::MarkNewFrameAvailable() { FML_DLOG(INFO) << " OHOSExternalTextureGL::MarkNewFrameAvailable"; - new_frame_ready_ = true; - newFrameCount = UPDATE_FRAME_COUNT; + if (state_ == AttachmentState::attached) { + Update(); + } } void OHOSExternalTextureGL::OnTextureUnregistered() @@ -289,6 +321,10 @@ 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; @@ -677,4 +713,25 @@ 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) { + PlatformView::Delegate& dalegate = 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 a8b5de66296fbfd2ed4523263c33073b41b64e6f..6f8379cc0ca4047514e7064be572b2a39c513050 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.h +++ b/shell/platform/ohos/ohos_external_texture_gl.h @@ -31,6 +31,7 @@ #include "flutter/shell/platform/ohos/napi/platform_view_ohos_napi.h" #include "flutter/shell/platform/ohos/ohos_surface_gl_skia.h" #include "flutter/shell/platform/ohos/surface/ohos_surface.h" +#include "flutter/shell/common/platform_view.h" // maybe now unused namespace flutter { @@ -38,13 +39,19 @@ namespace flutter { class OHOSExternalTextureGL : public flutter::Texture { public: explicit OHOSExternalTextureGL(int64_t id, const std::shared_ptr& ohos_surface); + explicit OHOSExternalTextureGL(int64_t id, const std::shared_ptr& ohos_surface, + PlatformView::Delegate& delegate); ~OHOSExternalTextureGL() override; + PlatformView::Delegate& delegate_; + OH_NativeImage *nativeImage_; OH_NativeImage *backGroundNativeImage_; + void *frameData_; + bool first_update_ = false; void Paint(PaintContext& context, @@ -95,9 +102,6 @@ class OHOSExternalTextureGL : public flutter::Texture { AttachmentState state_; - bool new_frame_ready_ = false; - int newFrameCount = 0; - GLuint texture_name_ = 0; GLuint backGroundTextureName_ = 0; @@ -129,5 +133,23 @@ class OHOSExternalTextureGL : public flutter::Texture { FML_DISALLOW_COPY_AND_ASSIGN(OHOSExternalTextureGL); }; + +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 diff --git a/shell/platform/ohos/platform_view_ohos.cpp b/shell/platform/ohos/platform_view_ohos.cpp index 4285b3d8fd7824ebad4dcdf8d8962f565bdc4490..1f5fd79368785ff899e7da844276710ae2b4e974 100644 --- a/shell/platform/ohos/platform_view_ohos.cpp +++ b/shell/platform/ohos/platform_view_ohos.cpp @@ -117,17 +117,6 @@ PlatformViewOHOS::PlatformViewOHOS( PlatformViewOHOS::~PlatformViewOHOS() { FML_LOG(INFO) << "PlatformViewOHOS::~PlatformViewOHOS"; - for (std::map::iterator it = contextDatas_.begin(); - it != contextDatas_.end(); ++it) { - if (it->second != nullptr) { - OhosImageFrameData* data = - reinterpret_cast(it->second); - delete data; - data = nullptr; - it->second = nullptr; - } - } - contextDatas_.clear(); } void PlatformViewOHOS::NotifyCreate( @@ -435,7 +424,7 @@ void PlatformViewOHOS::RegisterExternalTextureByImage(int64_t texture_id, iter->second->DispatchImage(image); } else { std::shared_ptr ohos_external_gl = - std::make_shared(texture_id, ohos_surface_); + std::make_shared(texture_id, ohos_surface_, delegate_); external_texture_gl_[texture_id] = ohos_external_gl; RegisterTexture(ohos_external_gl); ohos_external_gl->DispatchImage(image); @@ -456,25 +445,13 @@ uint64_t PlatformViewOHOS::RegisterExternalTexture(int64_t texture_id) int ret = -1; if (ohos_context_->RenderingApi() == OHOSRenderingAPI::kOpenGLES) { std::shared_ptr ohos_external_gl = - std::make_shared(texture_id, ohos_surface_); + std::make_shared(texture_id, ohos_surface_, delegate_); ohos_external_gl->nativeImage_ = OH_NativeImage_Create(texture_id, GL_TEXTURE_EXTERNAL_OES); if (ohos_external_gl->nativeImage_ == nullptr) { FML_DLOG(ERROR) << "Error with OH_NativeImage_Create"; return surface_id; } - void* contextData = new OhosImageFrameData(this, texture_id); - contextDatas_.insert(std::pair(texture_id, contextData)); - OH_OnFrameAvailableListener listener; - listener.context = contextData; - listener.onFrameAvailable = &PlatformViewOHOS::OnNativeImageFrameAvailable; - ret = OH_NativeImage_SetOnFrameAvailableListener( - ohos_external_gl->nativeImage_, listener); - if (ret != 0) { - FML_DLOG(ERROR) - << "Error with OH_NativeImage_SetOnFrameAvailableListener"; - return surface_id; - } ret = OH_NativeImage_GetSurfaceId(ohos_external_gl->nativeImage_, &surface_id); ohos_external_gl->first_update_ = false; @@ -501,54 +478,11 @@ void PlatformViewOHOS::SetTextureBufferSize( } } -void PlatformViewOHOS::OnNativeImageFrameAvailable(void* data) { - auto frameData = reinterpret_cast(data); - if (frameData == nullptr || frameData->context_ == nullptr) { - FML_DLOG(ERROR) - << "OnNativeImageFrameAvailable, frameData or context_ is null."; - return; - } - - if (frameData->context_->GetDestroyed()) { - FML_LOG(ERROR) << "OnNativeImageFrameAvailable NotifyDstroyed, will not " - "MarkTextureFrameAvailable"; - return; - } - - std::shared_ptr ohos_surface = - frameData->context_->ohos_surface_; - const TaskRunners task_runners = frameData->context_->task_runners_; - if (ohos_surface) { - fml::TaskRunner::RunNowOrPostTask( - task_runners.GetPlatformTaskRunner(), [frameData]() { - if (frameData->context_->GetDestroyed()) { - FML_LOG(ERROR) << "OnNativeImageFrameAvailable NotifyDstroyed, " - "will not MarkTextureFrameAvailable"; - return; - } - frameData->context_->MarkTextureFrameAvailable( - frameData->texture_id_); - }); - } -} - void PlatformViewOHOS::UnRegisterExternalTexture(int64_t texture_id) { FML_DLOG(INFO) << "PlatformViewOHOS::UnRegisterExternalTexture, texture_id=" << texture_id; external_texture_gl_.erase(texture_id); UnregisterTexture(texture_id); - std::map::iterator it = contextDatas_.find(texture_id); - if (it != contextDatas_.end()) { - if (it->second != nullptr) { - OhosImageFrameData* data = - reinterpret_cast(it->second); - task_runners_.GetPlatformTaskRunner()->PostDelayedTask( - [data_ = data]() { delete data_; }, fml::TimeDelta::FromSeconds(2)); - data = nullptr; - it->second = nullptr; - } - contextDatas_.erase(texture_id); - } } void PlatformViewOHOS::RegisterExternalTextureByPixelMap( @@ -560,7 +494,7 @@ void PlatformViewOHOS::RegisterExternalTextureByPixelMap( iter->second->DispatchPixelMap(pixelMap); } else { std::shared_ptr ohos_external_gl = - std::make_shared(texture_id, ohos_surface_); + std::make_shared(texture_id, ohos_surface_, delegate_); external_texture_gl_[texture_id] = ohos_external_gl; RegisterTexture(ohos_external_gl); ohos_external_gl->DispatchPixelMap(pixelMap); @@ -613,10 +547,4 @@ void PlatformViewOHOS::RunTask(OHOS_THREAD_TYPE type, const fml::closure& task) fml::TaskRunner::RunNowOrPostTask(TaskRunnerPtr, task); } -OhosImageFrameData::OhosImageFrameData(PlatformViewOHOS* context, - int64_t texture_id) - : context_(context), texture_id_(texture_id) {} - -OhosImageFrameData::~OhosImageFrameData() = default; - } // namespace flutter diff --git a/shell/platform/ohos/platform_view_ohos.h b/shell/platform/ohos/platform_view_ohos.h index 8e1b233b3b069218dfa94f6fb2f6872968a03def..c6dd57ccfb4241e6d16a568f0f2c08d3c2e1a81a 100644 --- a/shell/platform/ohos/platform_view_ohos.h +++ b/shell/platform/ohos/platform_view_ohos.h @@ -154,7 +154,6 @@ class PlatformViewOHOS final : public PlatformView { std::shared_ptr surface_factory_; std::map> external_texture_gl_; - std::map contextDatas_; std::atomic isDestroyed_; @@ -208,20 +207,6 @@ class PlatformViewOHOS final : public PlatformView { void FireFirstFrameCallback(); FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewOHOS); - - static void OnNativeImageFrameAvailable(void *data); -}; - -class OhosImageFrameData { - public: - OhosImageFrameData(PlatformViewOHOS* context, - int64_t texture_id); - - ~OhosImageFrameData(); - - PlatformViewOHOS* context_; - - int64_t texture_id_; }; } // namespace flutter