From 4d971e0088df5c61355ca49ca91be2343292fe68 Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Sun, 18 Aug 2024 16:09:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9ets=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E7=A8=B3=E5=AE=9A=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../ets/embedding/ohos/FlutterManager.ets | 3 +- .../flutter/src/main/ets/view/FlutterView.ets | 76 +++++++++---------- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterManager.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterManager.ets index 856aa4f4ec..8cac73b0dc 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterManager.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterManager.ets @@ -144,12 +144,11 @@ export class DefaultFullScreenListener implements FullScreenListener { onScreenStateChanged(data: window.WindowStatusType): void { if (this.skipCheck) { - Log.i(TAG, "onScreenStateChanged: skipCheck is on, WindowStatusType = " + JSON.stringify(data)) + Log.i(TAG, "onScreenStateChanged: skipCheck is on, WindowStatusType = " + data) return; } switch (data) { case window.WindowStatusType.FULL_SCREEN: - case window.WindowStatusType.SPLIT_SCREEN: case window.WindowStatusType.FLOATING: case window.WindowStatusType.MAXIMIZE: diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index a2022f2d28..03bdbc5b08 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -27,7 +27,7 @@ import { EmbeddingNodeController } from '../embedding/ohos/EmbeddingNodeControll import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; -const TAG = "FlutterView"; +const TAG = "FlutterViewTag"; export class ViewportMetrics { devicePixelRatio: number = 1.0; @@ -79,7 +79,7 @@ export class FlutterView { private viewportMetrics = new ViewportMetrics(); private displayInfo?: display.Display; private keyboardManager: KeyboardManager | null = null; - private mainWindow: window.Window + private mainWindow: window.Window | null = null; private mouseCursorPlugin?: MouseCursorPlugin; private uiContext?: UIContext | undefined; private settings?: Settings; @@ -105,29 +105,28 @@ export class FlutterView { this.mainWindow?.on('windowSizeChange', this.windowSizeChangeCallback); this.mainWindow?.on('avoidAreaChange', this.avoidAreaChangeCallback); - this.mainWindow?.on('keyboardHeightChange', this.keyboardHeightChangeCallback); this.mainWindow?.on('windowStatusChange', this.windowStatusChangeCallback); } - private windowSizeChangeCallback= (data: window.Size) => { - Log.i(TAG, "windowSizeChangeCallback"); - this.onAreaChange(null); - } - - private avoidAreaChangeCallback = (data: Any) => { - Log.i(TAG, "avoidAreaChangeCallback"); - this.onAreaChange(null); + private windowSizeChangeCallback = (data: window.Size) => { + Log.i(TAG, "windowSizeChangeCallback w:" + data.width + ", h:" + data.height); + if (this.isAttachedToFlutterEngine()) { + this.onAreaChange(null); + } } - private keyboardHeightChangeCallback = (data: number) => { - Log.i(TAG, "keyboardHeightChangeCallback"); - this.onAreaChange(null); + private avoidAreaChangeCallback = (data: window.AvoidAreaOptions) => { + Log.i(TAG, "avoidAreaChangeCallback, type=" + data.type); + if (this.isAttachedToFlutterEngine()) { + this.onAreaChange(null); + } } private windowStatusChangeCallback = (data: window.WindowStatusType) => { Log.i(TAG, "windowStatusChangeCallback " + data); - FlutterManager.getInstance().getFullScreenListener().onScreenStateChanged(data); - this.onAreaChange(null); + if (this.isAttachedToFlutterEngine()) { + FlutterManager.getInstance().getFullScreenListener().onScreenStateChanged(data); + } }; getId(): string { @@ -171,6 +170,14 @@ export class FlutterView { } onDestroy() { + try { + this.mainWindow?.off('windowSizeChange', this.windowSizeChangeCallback); + this.mainWindow?.off('avoidAreaChange', this.avoidAreaChangeCallback); + this.mainWindow?.off('windowStatusChange', this.windowStatusChangeCallback); + } catch (e) { + Log.e(TAG, "mainWindow off error: " + JSON.stringify(e)); + } + this.mainWindow = null; } attachToFlutterEngine(flutterEngine: FlutterEngine): void { @@ -195,7 +202,7 @@ export class FlutterView { this.flutterEngine?.getFlutterNapi()?.updateRefreshRate(this.displayInfo!.refreshRate) flutterEngine.getPlatformViewsController()?.attachToView(this); this.updateViewportMetrics() - let windowId = this.mainWindow?.getWindowProperties()?.id + let windowId = this.mainWindow?.getWindowProperties()?.id ?? 0 this.mouseCursorPlugin = new MouseCursorPlugin(windowId, this.flutterEngine?.getMouseCursorChannel()!); this.settings = new Settings(this.flutterEngine.getSettingsChannel()!); this.sendSettings(); @@ -208,15 +215,6 @@ export class FlutterView { detachFromFlutterEngine(): void { Log.i(TAG, "detachFromFlutterEngine"); - try { - this.mainWindow?.off('windowSizeChange', this.windowSizeChangeCallback); - this.mainWindow?.off('avoidAreaChange', this.avoidAreaChangeCallback); - this.mainWindow?.off('keyboardHeightChange', this.keyboardHeightChangeCallback); - this.mainWindow?.off('windowStatusChange', this.windowStatusChangeCallback); - } catch (e) { - Log.e(TAG, "mainWindow off error: " + JSON.stringify(e)); - } - if (!this.isAttachedToFlutterEngine()) { Log.d(TAG, "FlutterView not attached to an engine. Not detaching."); return; @@ -270,18 +268,16 @@ export class FlutterView { // 根据是否全屏显示,设置标题栏高度(若全屏,则及时规避) if (this.checkFullScreen && FlutterManager.getInstance().getFullScreenListener().useFullScreen()) { // 全屏显示 - this.viewportMetrics.physicalViewPaddingTop = systemAvoidArea!.topRect.height; - this.viewportMetrics.physicalViewPaddingBottom = navigationAvoidArea!.bottomRect.height; - Log.i(TAG, "--- fullscreen mode is on ---"); + this.viewportMetrics.physicalViewPaddingTop = systemAvoidArea?.topRect.height ?? 0; + this.viewportMetrics.physicalViewPaddingBottom = navigationAvoidArea?.bottomRect.height ?? 0; } else { // 非全屏显示(保持规避效果) // 顶部状态栏和底部导航栏规避为0,无平滑过渡效果 this.viewportMetrics.physicalViewPaddingTop = 0; this.viewportMetrics.physicalViewPaddingBottom = 0; - Log.i(TAG, "--- minimize mode is on ---"); } - this.viewportMetrics.physicalViewPaddingLeft = systemAvoidArea!.leftRect.width; - this.viewportMetrics.physicalViewPaddingRight = systemAvoidArea!.rightRect.width; + this.viewportMetrics.physicalViewPaddingLeft = systemAvoidArea?.leftRect.width ?? 0; + this.viewportMetrics.physicalViewPaddingRight = systemAvoidArea?.rightRect.width ?? 0; this.onKeyboardAreaChange() this.onGestureAreaChange() @@ -291,10 +287,10 @@ export class FlutterView { private onKeyboardAreaChange() { if (this.checkKeyboard) { let keyboardAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD); - this.viewportMetrics.physicalViewInsetTop = keyboardAvoidArea!.topRect.height - this.viewportMetrics.physicalViewInsetLeft = keyboardAvoidArea!.leftRect.width - this.viewportMetrics.physicalViewInsetBottom = keyboardAvoidArea!.bottomRect.height - this.viewportMetrics.physicalViewInsetRight = keyboardAvoidArea!.rightRect.width + this.viewportMetrics.physicalViewInsetTop = keyboardAvoidArea?.topRect.height ?? 0 + this.viewportMetrics.physicalViewInsetLeft = keyboardAvoidArea?.leftRect.width ?? 0 + this.viewportMetrics.physicalViewInsetBottom = keyboardAvoidArea?.bottomRect.height ?? 0 + this.viewportMetrics.physicalViewInsetRight = keyboardAvoidArea?.rightRect.width ?? 0 } else { this.viewportMetrics.physicalViewInsetTop = 0 this.viewportMetrics.physicalViewInsetLeft = 0 @@ -306,10 +302,10 @@ export class FlutterView { private onGestureAreaChange() { if (this.checkGesture) { let gestureAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM_GESTURE); - this.viewportMetrics.systemGestureInsetTop = gestureAvoidArea!.topRect.height - this.viewportMetrics.systemGestureInsetLeft = gestureAvoidArea!.leftRect.width - this.viewportMetrics.systemGestureInsetBottom = gestureAvoidArea!.bottomRect.height - this.viewportMetrics.systemGestureInsetRight = gestureAvoidArea!.rightRect.width + this.viewportMetrics.systemGestureInsetTop = gestureAvoidArea?.topRect.height ?? 0 + this.viewportMetrics.systemGestureInsetLeft = gestureAvoidArea?.leftRect.width ?? 0 + this.viewportMetrics.systemGestureInsetBottom = gestureAvoidArea?.bottomRect.height ?? 0 + this.viewportMetrics.systemGestureInsetRight = gestureAvoidArea?.rightRect.width ?? 0 } else { this.viewportMetrics.systemGestureInsetTop = 0 this.viewportMetrics.systemGestureInsetLeft = 0 -- Gitee From 11f3ab584f9028da4754e7bea4a3691ccee434b1 Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Sun, 18 Aug 2024 16:09:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=96=E6=8E=A5?= =?UTF-8?q?=E7=BA=B9=E7=90=86=E5=9B=BE=E7=89=87=E5=9C=BA=E6=99=AF=E7=A8=B3?= =?UTF-8?q?=E5=AE=9A=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../ohos/ohos_external_texture_gl.cpp | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index 35f013f7ac..f83cb5efc5 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -209,15 +209,23 @@ void OHOSExternalTextureGL::OnTextureUnregistered() { FML_DLOG(INFO)<<" OHOSExternalTextureGL::OnTextureUnregistered"; first_update_ = false; - OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); - OH_NativeImage_Destroy(&nativeImage_); - OH_NativeImage_Destroy(&backGroundNativeImage_); - nativeImage_ = nullptr; - backGroundNativeImage_ = nullptr; + if (nativeImage_ != nullptr) { + OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); + OH_NativeImage_Destroy(&nativeImage_); + nativeImage_ = nullptr; + } + if (backGroundNativeImage_ != nullptr) { + OH_NativeImage_Destroy(&backGroundNativeImage_); + backGroundNativeImage_ = nullptr; + } } void OHOSExternalTextureGL::Update() { + if (nativeImage_ == nullptr) { + FML_LOG(ERROR) << "Update, nativeImage_ is nullptr"; + return; + } int32_t ret = OH_NativeImage_UpdateSurfaceImage(nativeImage_); if (ret != 0) { FML_LOG(ERROR) << "OHOSExternalTextureGL OH_NativeImage_UpdateSurfaceImage err code:" << ret; @@ -424,6 +432,10 @@ void OHOSExternalTextureGL::ProducePixelMapToBackGroundImage() void OHOSExternalTextureGL::HandlePixelMapBuffer(NativePixelMap* pixelMap, OHNativeWindowBuffer* buffer) { BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer); + if (handle == nullptr) { + FML_LOG(ERROR) << "OHOSExternalTextureGL::HandlePixelMapBuffer, handle is nullptr."; + return; + } // get virAddr of bufferHandl by mmap sys interface uint32_t stride = handle->stride; FML_DLOG(INFO) << "OHOSExternalTextureGL stride:" << stride; -- Gitee From d093d2795afaf44d6c25650bbfd5794a4c5ad91f Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Sun, 18 Aug 2024 16:10:49 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A4=96=E7=95=8C=E7=BA=B9=E7=90=86?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=9C=BA=E6=99=AF=E7=9A=84=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../ohos/ohos_external_texture_gl.cpp | 37 ++++++++++++++----- shell/platform/ohos/platform_view_ohos.cpp | 1 + 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index f83cb5efc5..59d505b4a6 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -59,13 +59,16 @@ OHOSExternalTextureGL::OHOSExternalTextureGL(int64_t id, const std::shared_ptrGetResult()) { FML_DLOG(INFO)<<"ResourceContextMakeCurrent successed"; glGenTextures(1, &texture_name_); - FML_DLOG(INFO) << "OHOSExternalTextureGL::Paint, glGenTextures texture_name_=" << texture_name_; + FML_DLOG(INFO) << "OHOSExternalTextureGL::Paint, glGenTextures texture_name_=" << texture_name_ << ", Id()=" << Id(); if (nativeImage_ == nullptr) { nativeImage_ = OH_NativeImage_Create(texture_name_, GL_TEXTURE_EXTERNAL_OES); if (nativeImage_ == nullptr) { @@ -202,12 +206,19 @@ void OHOSExternalTextureGL::MarkNewFrameAvailable() { FML_DLOG(INFO)<<" OHOSExternalTextureGL::MarkNewFrameAvailable"; new_frame_ready_ = true; - Update(); + if (pixelMap_ == nullptr) { + Update(); + } else { + FML_DLOG(INFO) << "pixelMap_ is nullptr, texture_name_=" << texture_name_; + } } void OHOSExternalTextureGL::OnTextureUnregistered() { - FML_DLOG(INFO)<<" OHOSExternalTextureGL::OnTextureUnregistered"; + FML_DLOG(INFO) << " OHOSExternalTextureGL::OnTextureUnregistered, texture_name_=" << texture_name_ + << ", Id()=" << Id() + << ", nativeImage_=" << nativeImage_ + << ", backGroundNativeImage_=" << backGroundNativeImage_; first_update_ = false; if (nativeImage_ != nullptr) { OH_NativeImage_UnsetOnFrameAvailableListener(nativeImage_); @@ -222,8 +233,9 @@ void OHOSExternalTextureGL::OnTextureUnregistered() void OHOSExternalTextureGL::Update() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::Update, texture_name_=" << texture_name_; if (nativeImage_ == nullptr) { - FML_LOG(ERROR) << "Update, nativeImage_ is nullptr"; + FML_LOG(ERROR) << "Update, nativeImage_ is nullptr, texture_name_=" << texture_name_; return; } int32_t ret = OH_NativeImage_UpdateSurfaceImage(nativeImage_); @@ -237,6 +249,7 @@ 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"; return; @@ -483,6 +496,7 @@ void OHOSExternalTextureGL::HandlePixelMapBuffer(NativePixelMap* pixelMap, OHNat void OHOSExternalTextureGL::ProducePixelMapToNativeImage() { + FML_DLOG(INFO) << "OHOSExternalTextureGL::ProducePixelMapToNativeImage, pixelMap_=" << pixelMap_; if (state_ == AttachmentState::detached) { FML_LOG(ERROR) << "OHOSExternalTextureGL AttachmentState err"; return; @@ -495,12 +509,14 @@ void OHOSExternalTextureGL::ProducePixelMapToNativeImage() ret = OH_PixelMap_GetImageInfo(pixelMap_, &pixelMapInfo); if (ret != 0) { FML_LOG(ERROR) << "OHOSExternalTextureGL OH_PixelMap_GetImageInfo err:" << ret; + return; } int code = SET_BUFFER_GEOMETRY; ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow_, code, pixelMapInfo.width, pixelMapInfo.height); if (ret != 0) { FML_LOG(ERROR) << "OHOSExternalTextureGL OH_NativeWindow_NativeWindowHandleOpt err:" << ret; + return; } int32_t usage = 0; @@ -515,6 +531,7 @@ void OHOSExternalTextureGL::ProducePixelMapToNativeImage() ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer_, &fenceFd); if (ret != 0) { FML_LOG(ERROR) << "OHOSExternalTextureGL OH_NativeWindow_NativeWindowRequestBuffer err:" << ret; + return; } HandlePixelMapBuffer(pixelMap_, buffer_); Region region{nullptr, 0}; diff --git a/shell/platform/ohos/platform_view_ohos.cpp b/shell/platform/ohos/platform_view_ohos.cpp index fe4c393b2c..34c6193685 100644 --- a/shell/platform/ohos/platform_view_ohos.cpp +++ b/shell/platform/ohos/platform_view_ohos.cpp @@ -509,6 +509,7 @@ void PlatformViewOHOS::OnNativeImageFrameAvailable(void *data) 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); -- Gitee