diff --git a/README.en.md b/README.en.md index 1b57e89b92d73492261975d2ceeef0cfe3262632..3fdf8e6498e23241deefdf81bfa409ec12b5a564 100644 --- a/README.en.md +++ b/README.en.md @@ -92,5 +92,3 @@ export OHOS_SDK_HOME= 3. The har file output path is: shell/platform/ohos/flutter_embedding/flutter/build ps: If you are using the Beta version of DevEco Studio and encounter the error "must have required property 'compatibleSdkVersion', location: build-profile.json5:17:11" when compiling the project, please refer to the "DevEco Studio Environment Configuration Guide." docx》Chapter '6 Creating Projects and Running Hello World' [Configuration Plugin] Modify the shell/platform/ohos/flutter_embedding/hvigor/hvigor-config.json5 file. - -[hvigor-dependencies-config](shell/platform/ohos/flutter_embedding/dependencies/hvigor-dependencies-config.md) diff --git a/README.md b/README.md index 5cb27d599c6e68ef28d28da501eef57e066c26d8..e19e793031e61cc34fabd0d8f19e20f7b4315c8f 100644 --- a/README.md +++ b/README.md @@ -94,5 +94,3 @@ export OHOS_SDK_HOME= 3. har文件输出路径为:shell/platform/ohos/flutter_embedding/flutter/build ps:如果你使用的是DevEco Studio的Beta版本,编译工程时遇到“must have required property 'compatibleSdkVersion', location: build-profile.json5:17:11"错误,请参考《DevEco Studio环境配置指导.docx》中的‘6 创建工程和运行Hello World’【配置插件】章节修改 shell/platform/ohos/flutter_embedding/hvigor/hvigor-config.json5文件。 - -[hvigor本地依赖配置](shell/platform/ohos/flutter_embedding/dependencies/hvigor-dependencies-config.md) diff --git a/attachment/repos/bootstrap/ohos.py b/attachment/repos/bootstrap/ohos.py index 21022e045442452b15fc086c37bfb8571471c03e..f59d783c8f207ffc1e786e32be9df2112d955091 100644 --- a/attachment/repos/bootstrap/ohos.py +++ b/attachment/repos/bootstrap/ohos.py @@ -315,17 +315,9 @@ def zipFiles(buildInfo, useZip2=False): TIME_STR, ) prefixInZip = os.path.join("src", "out", outputName) + excludes = ["obj", "exe.unstripped", "so.unstripped"] if IS_WINDOWS: - excludes = [ - ".*\.ilk", - ".*\.pdb", - ] - else: - excludes = [ - "obj", - "exe.unstripped", - "so.unstripped", - ] + excludes.extend([".*\.ilk", ".*\.pdb"]) zipFileDir(fileIn, fileName, prefixInZip, excludes=excludes, useZip2=useZip2) diff --git a/attachment/repos/build.patch b/attachment/repos/build.patch index c3df91e35fb66e5af9b41004b66ad0e6af1f644f..7dd52e5b16332809ddd85dcf3a179af9737c7778 100644 --- a/attachment/repos/build.patch +++ b/attachment/repos/build.patch @@ -12,18 +12,10 @@ index 4e62965..ea49a53 100644 libs = [ "dl" ] } diff --git a/build/config/BUILDCONFIG.gn b/build/config/BUILDCONFIG.gn -index b8d0f47..d789796 100644 +index b8d0f47..a3d1ed9 100644 --- a/build/config/BUILDCONFIG.gn +++ b/build/config/BUILDCONFIG.gn -@@ -34,6 +34,7 @@ - # When writing build files, to do something only for the host: - # if (current_toolchain == host_toolchain) { ... - -+ - if (target_os == "") { - target_os = host_os - } -@@ -190,6 +191,7 @@ if (current_os == "win") { +@@ -190,6 +190,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = false @@ -31,7 +23,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = false is_win = true -@@ -198,6 +200,7 @@ if (current_os == "win") { +@@ -198,6 +199,7 @@ if (current_os == "win") { is_android = false is_chromeos = false is_fuchsia = false @@ -39,7 +31,7 @@ index b8d0f47..d789796 100644 is_fuchsia_host = false is_ios = false is_linux = false -@@ -212,6 +215,7 @@ if (current_os == "win") { +@@ -212,6 +214,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = false @@ -47,7 +39,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = true is_win = false -@@ -223,6 +227,7 @@ if (current_os == "win") { +@@ -223,6 +226,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = true @@ -55,7 +47,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = true is_win = false -@@ -237,6 +242,7 @@ if (current_os == "win") { +@@ -237,6 +241,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = false @@ -63,7 +55,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = true is_win = false -@@ -248,6 +254,19 @@ if (current_os == "win") { +@@ -248,6 +253,19 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = true is_linux = false @@ -83,7 +75,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = true is_win = false -@@ -259,6 +278,7 @@ if (current_os == "win") { +@@ -259,6 +277,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = true @@ -91,7 +83,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = true is_win = false -@@ -270,6 +290,7 @@ if (current_os == "win") { +@@ -270,6 +289,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = false @@ -99,7 +91,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = true is_win = false -@@ -281,6 +302,7 @@ if (current_os == "win") { +@@ -281,6 +301,7 @@ if (current_os == "win") { is_fuchsia_host = false is_ios = false is_linux = false @@ -107,7 +99,7 @@ index b8d0f47..d789796 100644 is_mac = false is_posix = false is_win = false -@@ -313,7 +335,7 @@ if (!is_clang && using_sanitizer) { +@@ -313,7 +334,7 @@ if (!is_clang && using_sanitizer) { is_clang = true } @@ -116,18 +108,18 @@ index b8d0f47..d789796 100644 if (is_msan && !is_linux) { assert(false, "Memory sanitizer is only available on Linux.") -@@ -365,7 +387,9 @@ if (is_posix) { +@@ -365,7 +386,9 @@ if (is_posix) { ] } -if (is_linux) { -+if(is_ohos || (is_linux && host_os == "mac")){ ++if (is_ohos || (is_linux && host_os == "mac")) { + _native_compiler_configs += [ "//build/config/ohos:sdk" ] -+ }else if (is_linux) { ++} else if (is_linux) { _native_compiler_configs += [ "//build/config/linux:sdk" ] } else if (is_mac) { _native_compiler_configs += [ "//build/config/mac:sdk" ] -@@ -510,8 +534,18 @@ shlib_toolchain = false +@@ -510,8 +533,18 @@ shlib_toolchain = false if (custom_toolchain != "") { assert(custom_sysroot != "") assert(custom_target_triple != "") @@ -147,22 +139,20 @@ index b8d0f47..d789796 100644 } else if (is_win) { if (is_clang) { host_toolchain = "//build/toolchain/win:clang_$host_cpu" -@@ -544,6 +578,21 @@ if (custom_toolchain != "") { +@@ -544,6 +577,19 @@ if (custom_toolchain != "") { } else { set_default_toolchain("//build/toolchain/android:$current_cpu") } +} else if (is_ohos) { ++ set_default_toolchain("//build/toolchain/custom") + if (host_os == "linux") { + if (is_clang) { + host_toolchain = "//build/toolchain/linux:clang_$host_cpu" -+ set_default_toolchain("//build/toolchain/linux:clang_$current_cpu") + } else { + host_toolchain = "//build/toolchain/linux:$host_cpu" -+ set_default_toolchain("//build/toolchain/linux:$current_cpu") + } + } else if (host_os == "mac") { + host_toolchain = "//build/toolchain/mac:clang_$host_cpu" -+ set_default_toolchain("//build/toolchain/ohos:clang_$current_cpu") + } else { + assert(false, "Unknown host for ohos cross compile") + } @@ -170,10 +160,10 @@ index b8d0f47..d789796 100644 if (is_clang) { host_toolchain = "//build/toolchain/linux:clang_$host_cpu" diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn -index ba20010..24a7d4e 100644 +index ba20010..67371f5 100644 --- a/build/config/compiler/BUILD.gn +++ b/build/config/compiler/BUILD.gn -@@ -191,11 +191,18 @@ config("compiler") { +@@ -191,10 +191,16 @@ config("compiler") { # CPU architecture. We may or may not be doing a cross compile now, so for # simplicity we always explicitly set the architecture. if (current_cpu == "x64") { @@ -181,61 +171,64 @@ index ba20010..24a7d4e 100644 - "-m64", - "-march=x86-64", - ] -+ if ( is_ohos ){ ++ if (is_ohos) { + cflags += [ + "-m64", + ] -+ }else { ++ } else { + cflags += [ + "-m64", + "-march=x86-64", + ] + } ldflags += [ "-m64" ] -+ } else if (current_cpu == "x86") { cflags += [ "-m32" ] - ldflags += [ "-m32" ] -@@ -315,7 +322,7 @@ config("compiler") { +@@ -315,7 +321,7 @@ config("compiler") { # Linux/Android common flags setup. # --------------------------------- - if (is_linux || is_android) { -+ if (is_linux || is_android || is_ohos ) { ++ if (is_linux || is_android || is_ohos) { cflags += [ "-fPIC", "-pipe", # Use pipes for communicating between sub-processes. Faster. -@@ -334,7 +341,16 @@ config("compiler") { +@@ -334,7 +340,16 @@ config("compiler") { # Linux-specific compiler flags setup. # ------------------------------------ - if (is_linux) { -+ if (is_ohos ) { ++ if (is_ohos) { + cflags += [ "-pthread" ] + ldflags += [ "-pthread" ] + + if (current_cpu == "arm64") { -+ cflags += [ "--target=aarch64-linux-ohos" ] -+ ldflags += [ "--target=aarch64-linux-ohos" ] ++ cflags += [ "--target=aarch64-linux-ohos" ] ++ ldflags += [ "--target=aarch64-linux-ohos" ] + cflags += [ "-DBORINGSSL_CLANG_SUPPORTS_DOT_ARCH" ] + } -+ }else if (is_linux ) { ++ } else if (is_linux) { cflags += [ "-pthread" ] ldflags += [ "-pthread" ] -@@ -521,8 +537,8 @@ config("runtime_library") { +@@ -521,9 +536,13 @@ config("runtime_library") { ldflags += [ "-nostdlib++" ] } include_dirs = [ - "//third_party/libcxx/include", "//third_party/libcxxabi/include", -+ "$custom_toolchain/include/c++/v1", ] ++ if (custom_toolchain != "") { ++ include_dirs += [ "$custom_toolchain/include/c++/v1" ] ++ } else { ++ include_dirs += [ "//third_party/libcxx/include" ] ++ } } + # Android standard library setup. diff --git a/build/config/ohos/BUILD.gn b/build/config/ohos/BUILD.gn new file mode 100644 -index 0000000..1d29b40 +index 0000000..b2675d2 --- /dev/null +++ b/build/config/ohos/BUILD.gn @@ -0,0 +1,29 @@ @@ -243,6 +236,7 @@ index 0000000..1d29b40 +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + ++import("//build/config/ohos/config.gni") +import("//build/config/ohos/pkg_config.gni") +import("//build/config/features.gni") +import("//build/config/sysroot.gni") @@ -267,7 +261,25 @@ index 0000000..1d29b40 +config("fontconfig") { + libs = [ ] +} +diff --git a/build/config/ohos/config.gni b/build/config/ohos/config.gni +new file mode 100644 +index 0000000..0022d70 +--- /dev/null ++++ b/build/config/ohos/config.gni +@@ -0,0 +1,13 @@ ++# Copyright 2014 The Chromium Authors. All rights reserved. ++# Use of this source code is governed by a BSD-style license that can be ++# found in the LICENSE file. + ++# This file contains common system config stuff for the Ohos build. ++ ++if (is_ohos) { ++ if (current_cpu == "arm64") { ++ ohos_app_abi = "arm64-v8a" ++ } else { ++ assert(false, "Unknown Ohos ABI: " + current_cpu) ++ } ++} diff --git a/build/config/ohos/pkg-config.py b/build/config/ohos/pkg-config.py new file mode 100644 index 0000000..b4a6aff @@ -703,6 +715,145 @@ index 7987e51..cd7b060 100644 if (current_cpu == "x64") { sysroot = rebase_path("//build/linux/debian_sid_amd64-sysroot") } else { +diff --git a/build/ohos/ohos_create_flutter_har.py b/build/ohos/ohos_create_flutter_har.py +new file mode 100644 +index 0000000..fc46359 +--- /dev/null ++++ b/build/ohos/ohos_create_flutter_har.py +@@ -0,0 +1,133 @@ ++#!/usr/bin/env python3 ++# ++# Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. ++# Licensed under the Apache License, Version 2.0 (the "License"); ++# you may not use this file except in compliance with the License. ++# You may obtain a copy of the License at ++# ++# http://www.apache.org/licenses/LICENSE-2.0 ++# ++# Unless required by applicable law or agreed to in writing, software ++# distributed under the License is distributed on an "AS IS" BASIS, ++# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ++# See the License for the specific language governing permissions and ++# limitations under the License. ++ ++"""Create a HAR incorporating all the components required to build a Flutter application""" ++ ++import argparse ++import logging ++import os ++import re ++import shutil ++import subprocess ++import sys ++ ++ ++HAR_CONFIG_TEMPLATE = """ ++{ ++ "app": { ++ "signingConfigs": [], ++ "products": [ ++ { ++ "name": "default", ++ "signingConfig": "default", ++ "compileSdkVersion": "%s", ++ "compatibleSdkVersion": "%s", ++ "runtimeOS": "HarmonyOS", ++ } ++ ], ++ "buildModeSet": [ ++ { ++ "name": "debug", ++ }, ++ { ++ "name": "release" ++ }, ++ { ++ "name": "profile" ++ }, ++ ] ++ }, ++ "modules": [ ++ { ++ "name": "flutter", ++ "srcPath": "./flutter" ++ } ++ ] ++} ++""" ++ ++ ++# 更新har的配置文件,指定编译使用的api版本 ++def updateConfig(buildDir, apiInt): ++ apiStr = "4.1.0(11)" if apiInt == 11 else "4.0.0(10)" ++ jsonFile = os.path.join(buildDir, "build-profile.json5") ++ with open(jsonFile, "w", encoding="utf-8") as file: ++ file.write(HAR_CONFIG_TEMPLATE % (apiStr, apiStr)) ++ ++ ++# 执行命令 ++def runCommand(command, checkCode=True, timeout=None): ++ logging.info("runCommand start, command = %s" % (command)) ++ code = subprocess.Popen(command, shell=True).wait(timeout) ++ if code != 0: ++ logging.error("runCommand error, code = %s, command = %s" % (code, command)) ++ if checkCode: ++ exit(code) ++ else: ++ logging.info("runCommand finish, code = %s, command = %s" % (code, command)) ++ ++ ++# 编译har文件,通过hvigorw的命令行参数指定编译类型(debug/release/profile) ++def buildHar(buildDir, apiInt, buildType): ++ updateConfig(buildDir, apiInt) ++ runCommand( ++ "cd %s && .%shvigorw clean --mode module " % (buildDir, os.sep) ++ + "-p module=flutter@default -p product=default -p buildMode=%s " % buildType ++ + "assembleHar --no-daemon" ++ ) ++ ++ ++def main(): ++ parser = argparse.ArgumentParser() ++ parser.add_argument("--embedding_src", help="Path of embedding source code.") ++ parser.add_argument("--build_dir", help="Path to build.") ++ parser.add_argument( ++ "--build_type", ++ choices=["debug", "release", "profile"], ++ help="Type to build har.", ++ ) ++ parser.add_argument("--output", help="Path to output har.") ++ parser.add_argument("--native_lib", action="append", help="Native code library.") ++ parser.add_argument("--ohos_abi", help="Native code ABI.") ++ parser.add_argument( ++ "--ohos_api_int", type=int, choices=[10, 11], help="Ohos api int." ++ ) ++ options = parser.parse_args() ++ # copy source code ++ if os.path.exists(options.build_dir): ++ shutil.rmtree(options.build_dir) ++ shutil.copytree(options.embedding_src, options.build_dir) ++ ++ # copy so files ++ for file in options.native_lib: ++ dir_name, full_file_name = os.path.split(file) ++ targetDir = os.path.join(options.build_dir, "flutter/libs", options.ohos_abi) ++ if not os.path.exists(targetDir): ++ os.makedirs(targetDir) ++ shutil.copyfile( ++ file, ++ os.path.join(targetDir, full_file_name), ++ ) ++ buildHar(options.build_dir, options.ohos_api_int, options.build_type) ++ shutil.copyfile( ++ os.path.join( ++ options.build_dir, "flutter/build/default/outputs/default/flutter.har" ++ ), ++ options.output, ++ ) ++ ++ ++if __name__ == "__main__": ++ sys.exit(main()) diff --git a/build/ohos/sysroot_ld_path.sh b/build/ohos/sysroot_ld_path.sh new file mode 100755 index 0000000..4b8bf73 diff --git a/attachment/repos/skia.patch b/attachment/repos/skia.patch index 455594c1ba654eb1d776fe23ed272fe9cdcd9a1d..d993bdffa7bae8ef54f07c45fbb2824284ef191c 100644 --- a/attachment/repos/skia.patch +++ b/attachment/repos/skia.patch @@ -327,59 +327,6 @@ index b43526fe1f..fb34289da7 100644 ldflags = [ "-rdynamic", "-Wl,-rpath,\$ORIGIN", -diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp -index ab23b24edd..c5f714712b 100644 ---- a/src/core/SkCanvas.cpp -+++ b/src/core/SkCanvas.cpp -@@ -82,6 +82,16 @@ static_assert(std::max(3,4) == 4); - - using Slug = sktext::gpu::Slug; - -+#ifdef TARGET_OS_OHOS -+#include -+#define SKIA_LOG_INFO LOG_INFO -+#define SKIA_TAG "XComSkia" -+//int OH_LOG_Print(int type, HiLog_LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) ; -+#define OHOS_LOG_DEBUG(...) (void) OH_LOG_Print(LOG_APP,SKIA_LOG_INFO,LOG_DOMAIN,SKIA_TAG,__VA_ARGS__) -+#else -+#define OHOS_LOG_DEBUG(...) -+#endif -+ - /////////////////////////////////////////////////////////////////////////////////////////////////// - - /* -@@ -2044,7 +2054,7 @@ void SkCanvas::onDrawPaint(const SkPaint& paint) { - } - - void SkCanvas::internalDrawPaint(const SkPaint& paint) { -- // drawPaint does not call internalQuickReject() because computing its geometry is not free -+ // drawPaint does not call exitinternalQuickReject() because computing its geometry is not free - // (see getLocalClipBounds(), and the two conditions below are sufficient. - if (paint.nothingToDraw() || this->isClipEmpty()) { - return; -@@ -2358,16 +2368,22 @@ void SkCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src, const S - SkPaint realPaint = clean_paint_for_drawImage(paint); - SkSamplingOptions realSampling = clean_sampling_for_constraint(sampling, constraint); - -+ OHOS_LOG_DEBUG("SkCanvas::onDrawImageRect2 ...enter "); - if (this->internalQuickReject(dst, realPaint)) { -+ OHOS_LOG_DEBUG("SkCanvas::onDrawImageRect2 ...internalQuickReject rejected"); - return; - } - -+ OHOS_LOG_DEBUG("SkCanvas::onDrawImageRect2 ...aboutToDraw"); - auto layer = this->aboutToDraw(this, realPaint, &dst, CheckForOverwrite::kYes, - image->isOpaque() ? kOpaque_ShaderOverrideOpacity - : kNotOpaque_ShaderOverrideOpacity); -+ - if (layer) { -+ OHOS_LOG_DEBUG("SkCanvas::onDrawImageRect2 ...topDevice->drawImageRect"); - this->topDevice()->drawImageRect(image, &src, dst, realSampling, layer->paint(), constraint); - } -+ OHOS_LOG_DEBUG("SkCanvas::onDrawImageRect2 ...exit"); - } - - void SkCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice, const SkRect& dst, diff --git a/src/gpu/ganesh/gl/egl/GrGLMakeEGLInterface.cpp b/src/gpu/ganesh/gl/egl/GrGLMakeEGLInterface.cpp index 78225b4610..c2105852b1 100644 --- a/src/gpu/ganesh/gl/egl/GrGLMakeEGLInterface.cpp diff --git a/build/archives/BUILD.gn b/build/archives/BUILD.gn index 90993ddb427930aab584a484190c10e702028f19..dd4fbdc66517078e684b9062cc3b0a526219a797 100644 --- a/build/archives/BUILD.gn +++ b/build/archives/BUILD.gn @@ -20,7 +20,7 @@ generated_file("artifacts_entitlement_config") { } } -if (build_engine_artifacts && !is_android) { +if (build_engine_artifacts && !is_android && !is_ohos) { zip_bundle("artifacts") { deps = [ "//flutter/flutter_frontend_server:frontend_server", @@ -129,6 +129,12 @@ if (build_engine_artifacts && !is_android) { } } +if (is_ohos) { + group("artifacts") { + deps = [ "//flutter/shell/platform/ohos:flutter_har_zip" ] + } +} + if (host_os == "linux" || host_os == "win") { group("embedder") { deps = [ "//flutter/shell/platform/embedder:embedder-archive" ] diff --git a/build/dart/rules.gni b/build/dart/rules.gni index c239d536c13fbaf70b242396b5543ae0c2ec3886..d92acff60e6fc61214addbdd9089046f7dd3c281 100644 --- a/build/dart/rules.gni +++ b/build/dart/rules.gni @@ -155,7 +155,7 @@ template("flutter_snapshot") { } else { output_aot_lib = "libapp.so" } - libapp = "$target_gen_dir/ohos/libs/$output_aot_lib" + libapp = "$target_gen_dir/ohos/libs/$ohos_app_abi/$output_aot_lib" outputs += [ libapp ] args += [ "--snapshot_kind=app-aot-elf", diff --git a/common/config.gni b/common/config.gni index 21a43ba4f89a36eeb0ed60772bc9d3b05fb2ca62..d74f2c9bc20c612c9b8e7fb48876a73d3c971e6f 100644 --- a/common/config.gni +++ b/common/config.gni @@ -6,6 +6,10 @@ if (is_android) { import("//build/config/android/config.gni") } +if (is_ohos) { + import("//build/config/ohos/config.gni") +} + if (target_cpu == "arm" || target_cpu == "arm64") { import("//build/config/arm.gni") } diff --git a/impeller/renderer/backend/vulkan/context_vk.cc b/impeller/renderer/backend/vulkan/context_vk.cc index db5574ce4ff941691548b3d3178294ce24f7b2fb..0427f9fdf0d928231b574473adf2a442d27ba442 100644 --- a/impeller/renderer/backend/vulkan/context_vk.cc +++ b/impeller/renderer/backend/vulkan/context_vk.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. // FLUTTER_NOLINT: https://github.com/flutter/flutter/issues/68331 +#pragma clang diagnostic ignored "-Wc++11-narrowing" #include "impeller/renderer/backend/vulkan/context_vk.h" diff --git a/shell/platform/ohos/BUILD.gn b/shell/platform/ohos/BUILD.gn index 8d18085468e772ad3fbb9ab6c40482c61e7fb387..7b888c0d6e3e9ab29fbd6c831731c0a7e0bef259 100644 --- a/shell/platform/ohos/BUILD.gn +++ b/shell/platform/ohos/BUILD.gn @@ -13,17 +13,25 @@ assert(is_ohos) +import("//build/config/ohos/config.gni") +import("//build/toolchain/clang.gni") import("//flutter/build/zip_bundle.gni") -import("//flutter/shell/platform/glfw/config.gni") -import("//flutter/testing/testing.gni") -import("//build/config/ohos/pkg_config.gni") +import("//flutter/common/config.gni") import("//flutter/shell/gpu/gpu.gni") +import("//flutter/shell/version/version.gni") +import("//flutter/testing/testing.gni") group("ohos") { deps = [ - ":flutter", + ":flutter_shell_native", #":publish_headers_ohos", + #":ohos_har", + ":flutter_har_zip", + ":ohos_symbols", ] + if (target_cpu != "x86") { + deps += [ ":gen_snapshot" ] + } } shell_gpu_configuration("ohos_gpu_configuration") { enable_software = true @@ -192,13 +200,10 @@ executable("flutter_ohos_unittests") { ] } -shared_library("flutter") { +shared_library("flutter_shell_native") { + output_name = "flutter" deps = [ ":flutter_ohos_src" ] - if (flutter_runtime_mode == "profile") { - deps += [ "//flutter/shell/vmservice:vmservice_snapshot" ] - } - ldflags = ["--rtlib=compiler-rt", "-fuse-ld=lld", "-static-libstdc++", @@ -220,6 +225,9 @@ shared_library("flutter") { ldflags += ["-lrawfile.z"] ldflags += ["-lEGL"] ldflags += ["-lGLESv3"] + # ldflags += ["-lGLESv2"] + ldflags += ["-limage_ndk.z"] + ldflags += ["-lnative_image"] ldflags += ["-lc++_shared"] ldflags += ["-lm"] @@ -268,3 +276,232 @@ shared_library("flutter") { # ] # files = tmp_files #} + +declare_args() { + embedding_artifact_id = "flutter_embedding_$flutter_runtime_mode" + embedding_har_filename = "$embedding_artifact_id.har" + embedding_har_path = "$root_out_dir/$embedding_har_filename" + engine_artifact_id = + string_replace(ohos_app_abi, "-", "_") + "_" + flutter_runtime_mode + engine_har_filename = "$engine_artifact_id.har" + + ohos_zip_archive_dir = "ohos-$target_cpu" + if (flutter_runtime_mode != "debug") { + ohos_zip_archive_dir += "-$flutter_runtime_mode" + } + ohos_api_int = 11 +} + +action("ohos_har") { + script = "//build/ohos/ohos_create_flutter_har.py" + + if (stripped_symbols) { + engine_library = "libflutter.so" + } else { + engine_library = "so.unstripped/libflutter.so" + } + + inputs = [ + # "$root_build_dir/$embedding_har_filename", + "$root_build_dir/$engine_library", + ] + + outputs = [ + "$root_build_dir/flutter.har", + # "$root_build_dir/$engine_har_filename", + ] + + args = [ + "--embedding_src", + "../../flutter/shell/platform/ohos/flutter_embedding", + "--build_dir", + "./obj/ohos/flutter_embedding", + "--build_type", + flutter_runtime_mode, + "--output", + rebase_path("flutter.har", root_build_dir, root_build_dir), + "--native_lib", + rebase_path("$engine_library", root_build_dir, root_build_dir), + "--ohos_abi", + ohos_app_abi, + "--ohos_api_int", + ohos_api_int + ] + + deps = [ + ":flutter_shell_native" + ] + + if (flutter_runtime_mode == "profile") { + deps += [ "//flutter/shell/vmservice:vmservice_snapshot" ] + args += [ + "--native_lib", + rebase_path( + "$root_gen_dir/flutter/shell/vmservice/ohos/libs/$ohos_app_abi/libvmservice_snapshot.so", + root_build_dir, + root_build_dir), + ] + } +} + +zip_bundle("flutter_har_zip") { + output = "$ohos_zip_archive_dir/artifacts.zip" + files = [ + { + source = "$root_build_dir/flutter.har" + destination = "flutter.har" + } + ] + + deps = [ ":ohos_har" ] +} + +zip_bundle("ohos_symbols") { + output = "$ohos_zip_archive_dir/symbols.zip" + files = [ + { + source = "$root_build_dir/so.unstripped/libflutter.so" + destination = "libflutter.so" + }, + ] + + deps = [ ":flutter_shell_native" ] +} + +generated_file("ohos_entitlement_config") { + outputs = [ "$target_gen_dir/ohos_entitlements.txt" ] + contents = [ "gen_snapshot" ] + deps = [] +} + +if (target_cpu != "x86") { + zip_bundle("gen_snapshot") { + gen_snapshot_bin = "gen_snapshot" + gen_snapshot_out_dir = get_label_info( + "//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)", + "root_out_dir") + gen_snapshot_path = rebase_path("$gen_snapshot_out_dir/$gen_snapshot_bin") + + if (host_os == "linux") { + output = "$ohos_zip_archive_dir/linux-x64.zip" + } else if (host_os == "mac") { + output = "$ohos_zip_archive_dir/darwin-x64.zip" + } else if (host_os == "win") { + output = "$ohos_zip_archive_dir/windows-x64.zip" + gen_snapshot_bin = "gen_snapshot.exe" + gen_snapshot_path = rebase_path("$root_out_dir/$gen_snapshot_bin") + } + + files = [ + { + source = gen_snapshot_path + destination = gen_snapshot_bin + }, + ] + + deps = [ "//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)" ] + } + + # TODO(godofredoc): Remove gen_snapshot and rename new_gen_snapshot when v2 migration is complete. + # BUG: https://github.com/flutter/flutter/issues/105351 + zip_bundle("new_gen_snapshot") { + gen_snapshot_bin = "gen_snapshot" + gen_snapshot_out_dir = get_label_info( + "//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)", + "root_out_dir") + gen_snapshot_path = rebase_path("$gen_snapshot_out_dir/$gen_snapshot_bin") + + if (host_os == "linux") { + output = "$ohos_zip_archive_dir/$full_platform_name-$flutter_runtime_mode/linux-x64.zip" + } else if (host_os == "mac") { + output = "$ohos_zip_archive_dir/$full_platform_name-$flutter_runtime_mode/darwin-x64.zip" + } else if (host_os == "win") { + output = "$ohos_zip_archive_dir/$full_platform_name-$flutter_runtime_mode/windows-x64.zip" + gen_snapshot_bin = "gen_snapshot.exe" + gen_snapshot_path = rebase_path("$root_out_dir/$gen_snapshot_bin") + } + + files = [ + { + source = gen_snapshot_path + destination = gen_snapshot_bin + }, + ] + + deps = [ "//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)" ] + + if (host_os == "mac") { + deps += [ ":ohos_entitlement_config" ] + files += [ + { + source = "$target_gen_dir/ohos_entitlements.txt" + destination = "entitlements.txt" + }, + ] + } + } +} + +if (target_cpu == "x64" || target_cpu == "arm64") { + zip_bundle("analyze_snapshot") { + deps = + [ "//third_party/dart/runtime/bin:analyze_snapshot($host_toolchain)" ] + + analyze_snapshot_bin = "analyze_snapshot" + analyze_snapshot_out_dir = + get_label_info( + "//third_party/dart/runtime/bin:analyze_snapshot($host_toolchain)", + "root_out_dir") + analyze_snapshot_path = + rebase_path("$analyze_snapshot_out_dir/$analyze_snapshot_bin") + + if (host_os == "linux") { + output = "$ohos_zip_archive_dir/analyze-snapshot-linux-x64.zip" + } else if (host_os == "mac") { + output = "$ohos_zip_archive_dir/analyze-snapshot-darwin-x64.zip" + } else if (host_os == "win") { + output = "$ohos_zip_archive_dir/analyze-snapshot-windows-x64.zip" + analyze_snapshot_bin = "analyze-snapshot.exe" + analyze_snapshot_path = rebase_path("$root_out_dir/$analyze_snapshot_bin") + } + + files = [ + { + source = analyze_snapshot_path + destination = analyze_snapshot_bin + }, + ] + } + + # TODO(godofredoc): Remove analyze_snapshot and rename new_analyze_snapshot when v2 migration is complete. + # BUG: https://github.com/flutter/flutter/issues/105351 + zip_bundle("new_analyze_snapshot") { + deps = + [ "//third_party/dart/runtime/bin:analyze_snapshot($host_toolchain)" ] + + analyze_snapshot_bin = "analyze_snapshot" + analyze_snapshot_out_dir = + get_label_info( + "//third_party/dart/runtime/bin:analyze_snapshot($host_toolchain)", + "root_out_dir") + analyze_snapshot_path = + rebase_path("$analyze_snapshot_out_dir/$analyze_snapshot_bin") + + if (host_os == "linux") { + output = "$ohos_zip_archive_dir/$full_platform_name-$flutter_runtime_mode/analyze-snapshot-linux-x64.zip" + } else if (host_os == "mac") { + output = "$ohos_zip_archive_dir/$full_platform_name-$flutter_runtime_mode/analyze-snapshot-darwin-x64.zip" + } else if (host_os == "win") { + output = "$ohos_zip_archive_dir/$full_platform_name-$flutter_runtime_mode/analyze-snapshot-windows-x64.zip" + analyze_snapshot_bin = "analyze-snapshot.exe" + analyze_snapshot_path = rebase_path("$root_out_dir/$analyze_snapshot_bin") + } + + files = [ + { + source = analyze_snapshot_path + destination = analyze_snapshot_bin + }, + ] + } +} diff --git a/shell/platform/ohos/flutter_embedding/.gitignore b/shell/platform/ohos/flutter_embedding/.gitignore index 4510e76666c8541e4a1195a0ed2850754c9fc243..969cec852a5aba0b1c35703eb108a5e6097bd818 100644 --- a/shell/platform/ohos/flutter_embedding/.gitignore +++ b/shell/platform/ohos/flutter_embedding/.gitignore @@ -8,3 +8,4 @@ /.clang-format /.clang-tidy **/.test +flutter/BuildProfile.ets diff --git a/shell/platform/ohos/flutter_embedding/build-profile.json5 b/shell/platform/ohos/flutter_embedding/build-profile.json5 index ae4533ee39e5c2954b75b7b19fe2ea1ef2b4ea82..2d2ee025698136d74f0a9ec4e9472bb263df8834 100755 --- a/shell/platform/ohos/flutter_embedding/build-profile.json5 +++ b/shell/platform/ohos/flutter_embedding/build-profile.json5 @@ -24,6 +24,17 @@ "compatibleSdkVersion": "4.1.0(11)", "runtimeOS": "HarmonyOS", } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + }, + { + "name": "profile" + }, ] }, "modules": [ diff --git a/shell/platform/ohos/flutter_embedding/flutter/index.ets b/shell/platform/ohos/flutter_embedding/flutter/index.ets index 3c96b70703b57d3e53a171aabf07727db248e94b..a9e43723cbf3971e9eb7e4a29aad5145731dff10 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/index.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/index.ets @@ -13,6 +13,193 @@ * limitations under the License. */ -export { FlutterAbility } from './src/main/ets/embedding/ohos/FlutterAbility' +import FlutterInjector from './src/main/ets/FlutterInjector'; +import FlutterPluginRegistry from './src/main/ets/app/FlutterPluginRegistry'; +import FlutterComponent from './src/main/ets/component/FlutterComponent'; +import FlutterEngine from './src/main/ets/embedding/engine/FlutterEngine'; +import FlutterEngineCache from './src/main/ets/embedding/engine/FlutterEngineCache'; +import FlutterEngineConnectionRegistry from './src/main/ets/embedding/engine/FlutterEngineConnectionRegistry'; +import FlutterEngineGroup from './src/main/ets/embedding/engine/FlutterEngineGroup'; +import FlutterEngineGroupCache from './src/main/ets/embedding/engine/FlutterEngineGroupCache'; +import FlutterNapi from './src/main/ets/embedding/engine/FlutterNapi'; +import { FlutterOverlaySurface } from './src/main/ets/embedding/engine/FlutterOverlaySurface'; +import FlutterShellArgs from './src/main/ets/embedding/engine/FlutterShellArgs'; +import DartExecutor from './src/main/ets/embedding/engine/dart/DartExecutor'; +import { DartMessenger } from './src/main/ets/embedding/engine/dart/DartMessenger'; +import { PlatformMessageHandler } from './src/main/ets/embedding/engine/dart/PlatformMessageHandler'; +import ApplicationInfoLoader from './src/main/ets/embedding/engine/loader/ApplicationInfoLoader'; +import FlutterApplicationInfo from './src/main/ets/embedding/engine/loader/FlutterApplicationInfo'; +import FlutterLoader from './src/main/ets/embedding/engine/loader/FlutterLoader'; +import { FlutterMutatorView } from './src/main/ets/embedding/engine/mutatorsstack/FlutterMutatorView'; +import { FlutterMutatorsStack } from './src/main/ets/embedding/engine/mutatorsstack/FlutterMutatorsStack'; +import { FlutterPlugin } from './src/main/ets/embedding/engine/plugins/FlutterPlugin'; +import AbilityAware from './src/main/ets/embedding/engine/plugins/ability/AbilityAware'; +import AbilityControlSurface from './src/main/ets/embedding/engine/plugins/ability/AbilityControlSurface'; +import { AbilityPluginBinding } from './src/main/ets/embedding/engine/plugins/ability/AbilityPluginBinding'; +import GeneratedPluginRegister from './src/main/ets/embedding/engine/plugins/util/GeneratedPluginRegister'; +import { FlutterUiDisplayListener } from './src/main/ets/embedding/engine/renderer/FlutterUiDisplayListener'; +import AccessibilityChannel from './src/main/ets/embedding/engine/systemchannels/AccessibilityChannel'; +import KeyEventChannel from './src/main/ets/embedding/engine/systemchannels/KeyEventChannel'; +import LifecycleChannel from './src/main/ets/embedding/engine/systemchannels/LifecycleChannel'; +import LocalizationChannel from './src/main/ets/embedding/engine/systemchannels/LocalizationChannel'; +import MouseCursorChannel from './src/main/ets/embedding/engine/systemchannels/MouseCursorChannel'; +import NavigationChannel from './src/main/ets/embedding/engine/systemchannels/NavigationChannel'; +import PlatformChannel from './src/main/ets/embedding/engine/systemchannels/PlatformChannel'; +import PlatformViewsChannel from './src/main/ets/embedding/engine/systemchannels/PlatformViewsChannel'; +import RestorationChannel from './src/main/ets/embedding/engine/systemchannels/RestorationChannel'; +import SettingsChannel from './src/main/ets/embedding/engine/systemchannels/SettingsChannel'; +import SystemChannel from './src/main/ets/embedding/engine/systemchannels/SystemChannel'; +import TestChannel from './src/main/ets/embedding/engine/systemchannels/TestChannel'; +import TextInputChannel from './src/main/ets/embedding/engine/systemchannels/TextInputChannel'; +import ExclusiveAppComponent from './src/main/ets/embedding/ohos/ExclusiveAppComponent'; +import { FlutterAbility } from './src/main/ets/embedding/ohos/FlutterAbility'; +import { FlutterAbilityAndEntryDelegate } from './src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate'; +import FlutterAbilityLaunchConfigs from './src/main/ets/embedding/ohos/FlutterAbilityLaunchConfigs'; +import FlutterEngineConfigurator from './src/main/ets/embedding/ohos/FlutterEngineConfigurator'; +import FlutterEngineProvider from './src/main/ets/embedding/ohos/FlutterEngineProvider'; +import FlutterEntry from './src/main/ets/embedding/ohos/FlutterEntry'; +import FlutterManager from './src/main/ets/embedding/ohos/FlutterManager'; +import { FlutterPage } from './src/main/ets/embedding/ohos/FlutterPage'; +import KeyboardManager from './src/main/ets/embedding/ohos/KeyboardManager'; +import OhosTouchProcessor from './src/main/ets/embedding/ohos/OhosTouchProcessor'; +import Settings from './src/main/ets/embedding/ohos/Settings'; +import { TouchEventTracker } from './src/main/ets/embedding/ohos/TouchEventTracker'; +import WindowInfoRepositoryCallbackAdapterWrapper + from './src/main/ets/embedding/ohos/WindowInfoRepositoryCallbackAdapterWrapper'; +import PlatformPlugin from './src/main/ets/plugin/PlatformPlugin'; +import BasicMessageChannel from './src/main/ets/plugin/common/BasicMessageChannel'; +import BinaryCodec from './src/main/ets/plugin/common/BinaryCodec'; +import { BinaryMessenger } from './src/main/ets/plugin/common/BinaryMessenger'; +import EventChannel from './src/main/ets/plugin/common/EventChannel'; +import FlutterException from './src/main/ets/plugin/common/FlutterException'; +import JSONMessageCodec from './src/main/ets/plugin/common/JSONMessageCodec'; +import JSONMethodCodec from './src/main/ets/plugin/common/JSONMethodCodec'; +import MessageCodec from './src/main/ets/plugin/common/MessageCodec'; +import MethodCall from './src/main/ets/plugin/common/MethodCall'; +import MethodChannel from './src/main/ets/plugin/common/MethodChannel'; +import MethodCodec from './src/main/ets/plugin/common/MethodCodec'; +import StandardMessageCodec from './src/main/ets/plugin/common/StandardMessageCodec'; +import StandardMethodCodec from './src/main/ets/plugin/common/StandardMethodCodec'; +import StringCodec from './src/main/ets/plugin/common/StringCodec'; +import { ListenableEditingState } from './src/main/ets/plugin/editing/ListenableEditingState'; +import { TextEditingDelta } from './src/main/ets/plugin/editing/TextEditingDelta'; +import TextInputPlugin from './src/main/ets/plugin/editing/TextInputPlugin'; +import LocalizationPlugin from './src/main/ets/plugin/localization/LocalizationPlugin'; +import MouseCursorPlugin from './src/main/ets/plugin/mouse/MouseCursorPlugin'; +import { AccessibilityEventsDelegate } from './src/main/ets/plugin/platform/AccessibilityEventsDelegate'; +import { PlatformOverlayView } from './src/main/ets/plugin/platform/PlatformOverlayView'; +import PlatformView from './src/main/ets/plugin/platform/PlatformView'; +import PlatformViewFactory from './src/main/ets/plugin/platform/PlatformViewFactory'; +import PlatformViewRegistry from './src/main/ets/plugin/platform/PlatformViewRegistry'; +import PlatformViewRegistryImpl from './src/main/ets/plugin/platform/PlatformViewRegistryImpl'; +import { PlatformViewWrapper } from './src/main/ets/plugin/platform/PlatformViewWrapper'; +import { PlatformViewsAccessibilityDelegate } from './src/main/ets/plugin/platform/PlatformViewsAccessibilityDelegate'; +import PlatformViewsController from './src/main/ets/plugin/platform/PlatformViewsController'; +import AccessibilityBridge from './src/main/ets/view/AccessibilityBridge'; +import { FlutterCallbackInformation } from './src/main/ets/view/FlutterCallbackInformation'; +import FlutterRunArguments from './src/main/ets/view/FlutterRunArguments'; +import { FlutterView } from './src/main/ets/view/FlutterView'; +import { TextureRegistry } from './src/main/ets/view/TextureRegistry'; +import { ByteBuffer } from './src/main/ets/util/ByteBuffer'; +import Log from './src/main/ets/util/Log'; +import MessageChannelUtils from './src/main/ets/util/MessageChannelUtils'; +import PathUtils from './src/main/ets/util/PathUtils'; +import StringUtils from './src/main/ets/util/StringUtils'; +import ToolUtils from './src/main/ets/util/ToolUtils'; +import { TraceSection } from './src/main/ets/util/TraceSection'; -export { FlutterPage } from './src/main/ets/embedding/ohos/FlutterPage' +export { + FlutterInjector, + FlutterPluginRegistry, + FlutterComponent, + FlutterEngine, + FlutterEngineCache, + FlutterEngineConnectionRegistry, + FlutterEngineGroup, + FlutterEngineGroupCache, + FlutterNapi, + FlutterOverlaySurface, + FlutterShellArgs, + DartExecutor, + DartMessenger, + PlatformMessageHandler, + ApplicationInfoLoader, + FlutterApplicationInfo, + FlutterLoader, + FlutterMutatorView, + FlutterMutatorsStack, + FlutterPlugin, + AbilityAware, + AbilityControlSurface, + AbilityPluginBinding, + GeneratedPluginRegister, + FlutterUiDisplayListener, + AccessibilityChannel, + KeyEventChannel, + LifecycleChannel, + LocalizationChannel, + MouseCursorChannel, + NavigationChannel, + PlatformChannel, + PlatformViewsChannel, + RestorationChannel, + SettingsChannel, + SystemChannel, + TestChannel, + TextInputChannel, + ExclusiveAppComponent, + FlutterAbility, + FlutterAbilityAndEntryDelegate, + FlutterAbilityLaunchConfigs, + FlutterEngineConfigurator, + FlutterEngineProvider, + FlutterEntry, + FlutterManager, + FlutterPage, + KeyboardManager, + OhosTouchProcessor, + Settings, + TouchEventTracker, + WindowInfoRepositoryCallbackAdapterWrapper, + PlatformPlugin, + BasicMessageChannel, + BinaryCodec, + BinaryMessenger, + EventChannel, + FlutterException, + JSONMessageCodec, + JSONMethodCodec, + MessageCodec, + MethodCall, + MethodChannel, + MethodCodec, + StandardMessageCodec, + StandardMethodCodec, + StringCodec, + ListenableEditingState, + TextEditingDelta, + TextInputPlugin, + LocalizationPlugin, + MouseCursorPlugin, + AccessibilityEventsDelegate, + PlatformOverlayView, + PlatformView, + PlatformViewFactory, + PlatformViewRegistry, + PlatformViewRegistryImpl, + PlatformViewWrapper, + PlatformViewsAccessibilityDelegate, + PlatformViewsController, + AccessibilityBridge, + FlutterCallbackInformation, + FlutterRunArguments, + FlutterView, + TextureRegistry, + ByteBuffer, + Log, + MessageChannelUtils, + PathUtils, + StringUtils, + ToolUtils, + TraceSection, +} diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/cpp/types/libflutter/index.d.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/cpp/types/libflutter/index.d.ets index c53957c7aaee4402afa25585c981d83830351ce5..6cb09bd9f02b4c6798e4778451f57c48ec925b0d 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/cpp/types/libflutter/index.d.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/cpp/types/libflutter/index.d.ets @@ -99,4 +99,12 @@ export const nativeXComponentDetachFlutterEngine: (xcomponentId: number, nativeS */ export const nativeDestroy: ( nativeShellHolderId: number -) => void; \ No newline at end of file +) => void; + +export const nativeInitNativeImage: (nativeShellHolderId: number, textureId: number, aImage: image.Image) => void; + +export const nativeUnregisterTexture: (nativeShellHolderId: number, textureId: number) => void; + +export const nativeRegisterPixelMap: (nativeShellHolderId: number, textureId: number, pixelMap: PixelMap) => void; + +export const nativeRegisterTexture: (nativeShellHolderId: number, textureId: number) => number; \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/app/FlutterPluginRegistry.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/app/FlutterPluginRegistry.ets index 2a2a44964d3903c884f533e2bed115a4b624b745..758b5f6e1ce5fd7c97312ab4cc0cfe76954fc422 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/app/FlutterPluginRegistry.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/app/FlutterPluginRegistry.ets @@ -18,11 +18,13 @@ import PlatformViewController from '../plugin/platform/PlatformViewsController' export default class FlutterPluginRegistry { private mPlatformViewsController: PlatformViewController; - private mFlutterView: FlutterView; - private mContext: common.Context; + private mFlutterView: FlutterView | null = null; + private mContext: common.Context | null = null; constructor() { this.mPlatformViewsController = new PlatformViewController(); + this.mFlutterView = null; + this.mContext = null; } attach(flutterView: FlutterView, context: common.Context): void { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/component/XComponentStruct.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/component/XComponentStruct.ets index e9d66ef987b8c8f070bb6279a2035fc06fc8c054..d8198a8b32cddbc18ae63f0dbcf2cefbf6935939 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/component/XComponentStruct.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/component/XComponentStruct.ets @@ -21,7 +21,7 @@ struct XComponentStruct { dvModelParams: DVModelParameters = new DVModelParameters(); build() { - XComponent({ id: (this.dvModelParams as Record)["xComponentId"], type: 'texture', libraryname: 'flutter'}) + XComponent({ id: (this.dvModelParams as Record)["xComponentId"], type: XComponentType.TEXTURE, libraryname: 'flutter'}) .onLoad((context) => { this.context = context; }) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngine.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngine.ets index 880e9bc78517cedba409c6e9939680f4fe503d71..b99be906f1f4038c01ac535ad6ee5986540cc1dc 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngine.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngine.ets @@ -42,7 +42,7 @@ import PlatformViewsController from '../../plugin/platform/PlatformViewsControll import { FlutterPlugin } from './plugins/FlutterPlugin'; import List from '@ohos.util.List'; import GeneratedPluginRegister from './plugins/util/GeneratedPluginRegister'; - +import { FlutterRenderer } from './renderer/FlutterRenderer'; const TAG = "FlutterEngine"; @@ -68,6 +68,7 @@ export default class FlutterEngine implements EngineLifecycleListener{ private accessibilityChannel: AccessibilityChannel | null = null; private localeChannel: LocalizationChannel | null = null; private flutterNapi: FlutterNapi; + private renderer: FlutterRenderer; private pluginRegistry: FlutterEngineConnectionRegistry | null = null; private textInputPlugin: TextInputPlugin | null = null; private localizationPlugin: LocalizationPlugin | null = null; @@ -99,6 +100,8 @@ export default class FlutterEngine implements EngineLifecycleListener{ } this.flutterLoader = flutterLoader; + this.renderer = new FlutterRenderer(this.flutterNapi); + if(platformViewsController == null) { platformViewsController = new PlatformViewsController(); } @@ -141,6 +144,8 @@ export default class FlutterEngine implements EngineLifecycleListener{ if (automaticallyRegisterPlugins && plugins) { GeneratedPluginRegister.registerGeneratedPlugins(this, plugins); } + + Log.d(TAG, "Call init finished.") } private attachToNapi(): void { @@ -218,6 +223,10 @@ export default class FlutterEngine implements EngineLifecycleListener{ return this.flutterNapi; } + getFlutterRenderer(): FlutterRenderer { + return this.renderer; + } + getDartExecutor(): DartExecutor { return this.dartExecutor } @@ -234,6 +243,10 @@ export default class FlutterEngine implements EngineLifecycleListener{ return this.settingsChannel; } + getFlutterLoader() { + return this.flutterLoader; + } + onPreEngineRestart(): void { } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngineConnectionRegistry.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngineConnectionRegistry.ets index be25395a31aa20a1b9cfc4d470964d1bf387d724..90044b0dfc32e96a6509c6556a624622d73e2b6d 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngineConnectionRegistry.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterEngineConnectionRegistry.ets @@ -55,7 +55,7 @@ export default class FlutterEngineConnectionRegistry implements PluginRegistry, constructor(appContext: common.Context, flutterEngine: FlutterEngine, flutterLoader: FlutterLoader) { this.flutterEngine = flutterEngine; - this.pluginBinding = new FlutterPluginBinding(appContext, flutterEngine, flutterEngine.getDartExecutor(), new DefaultFlutterAssets(flutterLoader), flutterEngine.getPlatformViewsController()?.getRegistry()); + this.pluginBinding = new FlutterPluginBinding(appContext, flutterEngine, flutterEngine.getDartExecutor(), new DefaultFlutterAssets(flutterLoader), flutterEngine.getFlutterRenderer(), flutterEngine.getPlatformViewsController()?.getRegistry()); } add(plugin: FlutterPlugin): void { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets index 4b8d9a3a54eb434722429bae904d14da1226b2d9..271ec23e6e79285837963b8abb6250f955daa16e 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets @@ -373,6 +373,26 @@ export default class FlutterNapi { detachFromNativeAndReleaseResources() { flutter.nativeDestroy(this.nativeShellHolderId!!); } + + initNativeImage(textureId: number, aImage: image.Image) { + Log.d(TAG, "called initNativeImage "); + flutter.nativeInitNativeImage(this.nativeShellHolderId!, textureId, aImage); + } + + unregisterTexture(textureId: number): void { + Log.d(TAG, "called unregisterTexture "); + flutter.nativeUnregisterTexture(this.nativeShellHolderId!, textureId); + } + + registerPixelMap(textureId: number, pixelMap: PixelMap): void { + Log.d(TAG, "called registerPixelMap "); + flutter.nativeRegisterPixelMap(this.nativeShellHolderId!, textureId, pixelMap); + } + + registerTexture(textureId: number): number { + Log.d(TAG, "called unregisterTexture "); + return flutter.nativeRegisterTexture(this.nativeShellHolderId!, textureId); + } } export interface AccessibilityDelegate { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartExecutor.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartExecutor.ets index 46d08cd458b5795d1daaa1f4379a088b129d5dc7..383927204c7bc83df203d2e779430c3969762a20 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartExecutor.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartExecutor.ets @@ -172,7 +172,7 @@ export default class DartExecutor implements BinaryMessenger { this.getBinaryMessenger().send(channel, message, callback); } - setMessageHandler(channel: String, handler: BinaryMessageHandler, taskQueue?: TaskQueue): void { + setMessageHandler(channel: String, handler: BinaryMessageHandler | null, taskQueue?: TaskQueue): void { this.getBinaryMessenger().setMessageHandler(channel, handler, taskQueue); } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartMessenger.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartMessenger.ets index 6e9bfab834fbdc2b0929e774ecd94c78d4683e7a..3567d610f4ddafb1e845baf55a0d7d8028e878a5 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartMessenger.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/dart/DartMessenger.ets @@ -68,7 +68,7 @@ export class DartMessenger implements BinaryMessenger, PlatformMessageHandler { } - setMessageHandler(channel: String, handler: BinaryMessageHandler, taskQueue?: TaskQueue): void { + setMessageHandler(channel: String, handler: BinaryMessageHandler | null, taskQueue?: TaskQueue): void { if (handler == null) { Log.d(TAG, "Removing handler for channel '" + channel + "'"); this.messageHandlers.delete(channel); @@ -255,7 +255,7 @@ class Reply implements BinaryReply { this.replyId = replyId; } - reply(reply: ArrayBuffer) { + reply(reply: ArrayBuffer | null) { if (this.done) { throw new Error("Reply already submitted"); } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterApplicationInfo.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterApplicationInfo.ets index 272615d612c8d056fb3a58c280f9f2e69b02d502..8ff3aec5b8e0a72c4427b0cfab711d62e266a9b3 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterApplicationInfo.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterApplicationInfo.ets @@ -13,6 +13,8 @@ * limitations under the License. */ +import BuildProfile from "../../../../../../BuildProfile.ets"; + const DEFAULT_AOT_SHARED_LIBRARY_NAME = "libapp.so"; const DEFAULT_VM_SNAPSHOT_DATA = "vm_snapshot_data"; const DEFAULT_ISOLATE_SNAPSHOT_DATA = "isolate_snapshot_data"; @@ -49,7 +51,7 @@ export default class FlutterApplicationInfo { this.domainNetworkPolicy = domainNetworkPolicy == null ? "" : domainNetworkPolicy; this.nativeLibraryDir = nativeLibraryDir; this.automaticallyRegisterPlugins = automaticallyRegisterPlugins; - this.isDebugMode = false; - this.isProfile = false; + this.isDebugMode = "debug" == String(BuildProfile.BUILD_MODE_NAME); + this.isProfile = "profile" == String(BuildProfile.BUILD_MODE_NAME); } } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterLoader.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterLoader.ets index 89e7cf3264c881a0c61e4503f25b645e979b4ed7..2317af9f61ca4d70aecbc77d162305ed9c45f615 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterLoader.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/loader/FlutterLoader.ets @@ -170,7 +170,6 @@ export default class FlutterLoader { if (this.initialized) { return; } - Log.d(TAG, "ensureInitializationComplete") if (shellArgs == null) { shellArgs = new Array(); } @@ -231,6 +230,7 @@ export default class FlutterLoader { costTime ); this.initialized = true; + Log.d(TAG, "ensureInitializationComplete") } findAppBundlePath(): string { @@ -274,6 +274,10 @@ export default class FlutterLoader { }) : new Array(); } + isInitialized(): boolean { + return this.initialized; + } + } class InitResult { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/plugins/FlutterPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/plugins/FlutterPlugin.ets index 1b8ea40a0513043a824ea0fd997f70cf0ab27e8d..92e08f8dd9c22e441ecfe852895522a7417f859b 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/plugins/FlutterPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/plugins/FlutterPlugin.ets @@ -17,6 +17,7 @@ import common from '@ohos.app.ability.common'; import { BinaryMessenger } from '../../../plugin/common/BinaryMessenger'; import PlatformViewFactory from '../../../plugin/platform/PlatformViewFactory'; import PlatformViewRegistry from '../../../plugin/platform/PlatformViewRegistry'; +import { TextureRegistry } from '../../../view/TextureRegistry'; import FlutterEngine from '../FlutterEngine'; export interface FlutterPlugin { @@ -52,13 +53,15 @@ export class FlutterPluginBinding { private flutterEngine: FlutterEngine; private binaryMessenger: BinaryMessenger; private flutterAssets: FlutterAssets; + private textureRegistry: TextureRegistry; private platformViewRegistry: PlatformViewRegistry; - constructor(applicationContext: common.Context, flutterEngine: FlutterEngine, binaryMessenger: BinaryMessenger, flutterAssets: FlutterAssets, platformViewRegistry?: PlatformViewRegistry) { + constructor(applicationContext: common.Context, flutterEngine: FlutterEngine, binaryMessenger: BinaryMessenger, flutterAssets: FlutterAssets, textureRegistry: TextureRegistry, platformViewRegistry?: PlatformViewRegistry) { this.applicationContext = applicationContext; this.flutterEngine = flutterEngine; this.binaryMessenger = binaryMessenger; this.flutterAssets = flutterAssets; + this.textureRegistry = textureRegistry; this.platformViewRegistry = platformViewRegistry ?? new EmptyPlatformViewRegistry(); } @@ -78,6 +81,10 @@ export class FlutterPluginBinding { return this.flutterAssets; } + getTextureRegistry(): TextureRegistry { + return this.textureRegistry; + } + public getPlatformViewRegistry(): PlatformViewRegistry { return this.platformViewRegistry; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/renderer/FlutterRenderer.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/renderer/FlutterRenderer.ets new file mode 100644 index 0000000000000000000000000000000000000000..f2f1f75c948a087081de0de83ae419ea787ea0d1 --- /dev/null +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/renderer/FlutterRenderer.ets @@ -0,0 +1,123 @@ +import image from '@ohos.multimedia.image'; +import { BusinessError } from '@ohos.base'; +import { SurfaceTextureEntry, TextureRegistry } from '../../../view/TextureRegistry'; +import { FlutterAbility } from '../../ohos/FlutterAbility'; +import FlutterNapi from '../FlutterNapi'; +import { SurfaceTextureWrapper } from './SurfaceTextureWrapper'; +import Log from '../../../util/Log'; + +const TAG = "FlutterRenderer" +export class FlutterRenderer implements TextureRegistry { + private nextTextureId: number = 0; + private flutterNapi: FlutterNapi; + + constructor(flutterNapi: FlutterNapi) { + this.flutterNapi = flutterNapi; + } + createSurfaceTexture(): SurfaceTextureEntry { + let receiver: image.ImageReceiver = this.getImageReceiver(); + return this.registerSurfaceTexture(receiver); + } + + getTextureId(): number { + this.nextTextureId = this.nextTextureId + 1; + Log.i(TAG, "getTextureId: ", this.nextTextureId ) + return this.nextTextureId; + } + + registerTexture(textureId: number): SurfaceTextureEntry { + let surfaceTextureRegistryEntry = new SurfaceTextureRegistryEntry(this.nextTextureId); + let surfaceId = this.flutterNapi.registerTexture(textureId); + Log.i(TAG, "registerTexture, surfaceId=" + surfaceId); + surfaceTextureRegistryEntry.setSurfaceId(surfaceId); + return surfaceTextureRegistryEntry; + } + + registerSurfaceTexture(receiver: image.ImageReceiver): SurfaceTextureEntry { + this.nextTextureId = this.nextTextureId + 1; + let surfaceTextureRegistryEntry = new SurfaceTextureRegistryEntry(this.nextTextureId); + surfaceTextureRegistryEntry.setImageReceiver(receiver); + this.registerImage(surfaceTextureRegistryEntry.getTextureId(), surfaceTextureRegistryEntry.textureWrapper()); + return surfaceTextureRegistryEntry; + } + + registerPixelMap(pixelMap: PixelMap): number { + let textureId = this.nextTextureId + 1; + this.flutterNapi.registerPixelMap(textureId, pixelMap); + return textureId; + } + + unregisterTexture(textureId: number): void { + this.flutterNapi.unregisterTexture(textureId); + } + onTrimMemory(level: number) { + throw new Error('Method not implemented.'); + } + + private getImageReceiver(): image.ImageReceiver { + let receiver: image.ImageReceiver = image.createImageReceiver(640, 480, 4, 8); + if (receiver !== undefined) { + console.info('[camera test] ImageReceiver is ok'); + } else { + console.info('[camera test] ImageReceiver is not ok'); + } + return receiver; + } + private registerImage(textureId: number, surfaceTextureWrapper: SurfaceTextureWrapper): void { + let receiver = surfaceTextureWrapper.getImageReceiver(); + receiver.on('imageArrival', () => { + receiver.readNextImage((err: BusinessError, nextImage: image.Image) => { + if (err || nextImage === undefined) { + return; + } + this.flutterNapi.initNativeImage(textureId, nextImage); + console.log("[camera test] format: " + nextImage.format); + nextImage.release((err : BusinessError) =>{ + if (err != undefined) { + console.log('Failed to release the image source instance.'); + } else { + console.log('Succeeded in releasing the image source instance.'); + } + }); + }) + }) + } +} + +export class SurfaceTextureRegistryEntry implements SurfaceTextureEntry { + private textureId: number = 0; + private surfaceId: number = 0; + private surfaceTextureWrapper: SurfaceTextureWrapper | null = null; + private released: boolean = false; + + constructor(id: number) { + this.textureId = id; + } + + getImageReceiver(): image.ImageReceiver { + return this.surfaceTextureWrapper!.getImageReceiver(); + } + + setImageReceiver(receiver: image.ImageReceiver): void { + this.surfaceTextureWrapper = new SurfaceTextureWrapper(receiver); + } + + getTextureId(): number { + return this.textureId; + } + + getSurfaceId(): number { + return this.surfaceId; + } + + setSurfaceId(surfaceId: number): void { + this.surfaceId = surfaceId; + } + + textureWrapper(): SurfaceTextureWrapper { + return this.surfaceTextureWrapper!; + } + release() { + throw new Error('Method not implemented.'); + } +} \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/renderer/SurfaceTextureWrapper.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/renderer/SurfaceTextureWrapper.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1da18061bc2398f9ce0acd5f9f2c42e248b9204 --- /dev/null +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/renderer/SurfaceTextureWrapper.ets @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import image from '@ohos.multimedia.image'; + +export class SurfaceTextureWrapper { + private receiver: image.ImageReceiver; + private released: boolean = false; + private attached: boolean = false; + + constructor(receiver: image.ImageReceiver) { + this.receiver = receiver; + } + + getImageReceiver(): image.ImageReceiver { + return this.receiver; + } +} \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets index c02c3ad005371a98e50fed4d23ca2b3852c89cd6..315a433f8491dd40e4f533287dfa5de49a49302f 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets @@ -20,6 +20,8 @@ import TextInputPlugin from '../../../plugin/editing/TextInputPlugin'; import Log from '../../../util/Log'; import DartExecutor from '../dart/DartExecutor'; import inputMethod from '@ohos.inputMethod'; +import ArrayList from '@ohos.util.ArrayList'; +import { TextEditingDelta, TextEditingDeltaJson } from '../../../plugin/editing/TextEditingDelta'; const TAG = "TextInputChannel"; @@ -56,6 +58,18 @@ export default class TextInputChannel { return state; } + createEditingDeltaJSON(batchDeltas: ArrayList): EditingDelta { + let deltas: Array = []; + batchDeltas.forEach((val, idx, array) => { + deltas.push(val.toJSON()); + }) + + let state: EditingDelta = { + deltas: deltas, + }; + return state; + } + /** * Instructs Flutter to update its text input editing state to reflect the given configuration. */ @@ -72,6 +86,12 @@ export default class TextInputChannel { this.channel.invokeMethod('TextInputClient.updateEditingState', [inputClientId, state]); } + updateEditingStateWithDeltas(inputClientId: number, batchDeltas: ArrayList): void { + Log.d(TAG, "updateEditingStateWithDeltas:" + "batchDeltas length: " + batchDeltas.length); + const state: ESObject = this.createEditingDeltaJSON(batchDeltas); + this.channel.invokeMethod('TextInputClient.updateEditingStateWithDeltas', [inputClientId, state]); + } + newline(inputClientId: number): void { Log.d(TAG, "Sending 'newline' message."); this.channel.invokeMethod("TextInputClient.performAction", [inputClientId, "TextInputAction.newline"]); @@ -124,7 +144,6 @@ export default class TextInputChannel { performPrivateCommand(inputClientId: number, action: string, data: ESObject) { } - } interface EditingState { @@ -135,6 +154,12 @@ interface EditingState { composingExtent: number; } + +interface EditingDelta { + deltas: Array; +} + + export interface TextInputMethodHandler { show(): void; @@ -175,12 +200,12 @@ export class Configuration { enableSuggestions: boolean, enableIMEPersonalizedLearning: boolean, enableDeltaModel: boolean, - textCapitalization:TextCapitalization, + textCapitalization: TextCapitalization, inputType: InputType, inputAction: Number, actionLabel: String, - autofill :boolean, - contentListString:[], + autofill: boolean, + contentListString: [], fields: Configuration[] ) { this.obscureText = obscureText; @@ -197,8 +222,9 @@ export class Configuration { this.fields = fields } + static getTextCapitalizationFromValue(encodedName: string): TextCapitalization { - let textKeys= ["CHARACTERS", "WORDS","SENTENCES", "NONE"]; + let textKeys = ["CHARACTERS", "WORDS", "SENTENCES", "NONE"]; for (let textKey of textKeys) { if (TextCapitalization[textKey] === encodedName) { return textKey as TextCapitalization; @@ -206,8 +232,9 @@ export class Configuration { } throw new Error("No such TextCapitalization: " + encodedName); } - private static inputActionFromTextInputAction(inputActionName:string):number{ - switch(inputActionName){ + + private static inputActionFromTextInputAction(inputActionName: string): number { + switch (inputActionName) { case "TextInputAction.previous": return inputMethod.EnterKeyType.PREVIOUS case "TextInputAction.unspecified": @@ -228,23 +255,24 @@ export class Configuration { } } - static fromJson(json:ESObject) { - const inputActionName:string = json.inputAction; + + static fromJson(json: ESObject) { + const inputActionName: string = json.inputAction; if (!inputActionName) { throw new Error("Configuration JSON missing 'inputAction' property."); } - let fields:Array = new Array(); + let fields: Array = new Array(); if (json.fields !== null && json.fields !== undefined) { - fields = json.fields.map((field:ESObject):ESObject => Configuration.fromJson(field)); + fields = json.fields.map((field: ESObject): ESObject => Configuration.fromJson(field)); } - const inputAction:number = Configuration.inputActionFromTextInputAction(inputActionName); + const inputAction: number = Configuration.inputActionFromTextInputAction(inputActionName); // Build list of content commit mime types from the data in the JSON list. - const contentList:Array = []; + const contentList: Array = []; if (json.contentCommitMimeTypes !== null && json.contentCommitMimeTypes !== undefined) { - json.contentCommitMimeTypes.forEach((type:ESObject) => { + json.contentCommitMimeTypes.forEach((type: ESObject) => { contentList.push(type); }); } @@ -263,7 +291,6 @@ export class Configuration { fields ); } - } enum TextCapitalization { @@ -304,7 +331,7 @@ export class InputType { } static getTextInputTypeFromValue(encodedName: string): TextInputType { - let textKeys = [ "TEXT", "DATETIME","NAME" , "POSTAL_ADDRESS", "NUMBER", "PHONE", "MULTILINE", "EMAIL_ADDRESS", "URL", "VISIBLE_PASSWORD", "NONE" ,]; + let textKeys = ["TEXT", "DATETIME", "NAME", "POSTAL_ADDRESS", "NUMBER", "PHONE", "MULTILINE", "EMAIL_ADDRESS", "URL", "VISIBLE_PASSWORD", "NONE",]; for (let textKey of textKeys) { if (TextInputType[textKey] === encodedName) { return textKey as TextInputType; @@ -403,7 +430,7 @@ class TextInputCallback implements MethodCallHandler { break; case "TextInput.setClient": const textInputClientId: number = args[0] as number; - const jsonConfiguration :string = args[1] ; + const jsonConfiguration: string = args[1]; const config: Configuration | null = Configuration.fromJson(jsonConfiguration); this.textInputMethodHandler.setClient(textInputClientId, config); @@ -442,5 +469,4 @@ class TextInputCallback implements MethodCallHandler { break; } } - } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index a5869a3cbf58fcf336c338cfc1d3619538ed7cef..7c63ef987363180db37d57517e0f2107c3a8b950 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -208,6 +208,10 @@ export class FlutterAbility extends UIAbility implements Host { return this; } + getFlutterAbilityAndEntryDelegate(): FlutterAbilityAndEntryDelegate | null{ + return this.delegate ?? null; + } + shouldDispatchAppLifecycleState(): boolean { return true; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets index 056d8c0b5cad796975b3121ef57848fa606cd23b..baf4d2e3dafc444950711f8c7a77873372d19516 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets @@ -39,6 +39,7 @@ import GeneratedPluginRegister from '../engine/plugins/util/GeneratedPluginRegis import { UIContext } from '@ohos.arkui.UIContext'; import { FlutterView } from '../../view/FlutterView'; import FlutterManager from './FlutterManager'; +import router from '@ohos.router'; const TAG = "FlutterAbilityDelegate"; const PLUGINS_RESTORATION_BUNDLE_KEY = "plugins"; @@ -97,7 +98,7 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent } if (this.flutterEngine) { this.textInputPlugin = new TextInputPlugin(this.flutterEngine.getTextInputChannel()!); - this.platformPlugin = new PlatformPlugin(this.flutterEngine.getPlatformChannel()!, this.context); + this.platformPlugin = new PlatformPlugin(this.flutterEngine.getPlatformChannel()!, this.context, this); this.settings = new Settings(this.flutterEngine.getSettingsChannel()!); this.flutterEngine.getSystemLanguages(); } @@ -107,6 +108,11 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent this.host?.configureFlutterEngine(this.flutterEngine!!); } + popSystemNavigator(): boolean { + router.back(); + return true; + } + /** * 加载app.so资源或者snapshot */ @@ -397,6 +403,10 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent return this.flutterEngine?.getFlutterNapi() ?? null } + getFlutterEngine(): FlutterEngine | null { + return this.flutterEngine ?? null; + } + detachFromFlutterEngine() { if (this.host?.shouldDestroyEngineWithHost()) { // The host owns the engine and should never have its engine taken by another exclusive diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets index e995319b549201eb1dfa72ce972df8d64d53138f..2d5a4a0ef800ce96b6f33f2507762f97c592bfef 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets @@ -38,26 +38,31 @@ export default class FlutterEntry implements Host { private flutterView: FlutterView | null = null private context: Context; private windowStage: window.WindowStage | null = null - private parameters: ESObject = {}; + private parameters: Record = {}; private engineConfigurator: FlutterEngineConfigurator | null = null + private hasInit: boolean = false; - constructor(context: Context, params: ESObject = {}) { + constructor(context: Context, params: Record = {}) { this.context = context; this.uiAbility = FlutterManager.getInstance().getUIAbility(context); this.parameters = params; this.windowStage = FlutterManager.getInstance().getWindowStage(this.uiAbility); + this.hasInit = false; } async aboutToAppear() { Log.i(TAG, 'aboutToAppear'); - this.delegate = new FlutterAbilityAndEntryDelegate(this); - this.flutterView = this.delegate?.createView(this.context); - await this?.delegate?.onAttach(this.context); - Log.i(TAG, 'onAttach end'); - this?.delegate?.platformPlugin?.setUIAbilityContext(this.uiAbility!!.context); - this.delegate?.onCreate(); - this.delegate?.onWindowStageCreate() - this.windowStage?.on('windowStageEvent', this.windowStageEventCallback); + if (this.hasInit == false) { + this.delegate = new FlutterAbilityAndEntryDelegate(this); + this.flutterView = this.delegate?.createView(this.context); + await this?.delegate?.onAttach(this.context); + Log.i(TAG, 'onAttach end'); + this?.delegate?.platformPlugin?.setUIAbilityContext(this.uiAbility!!.context); + this.delegate?.onCreate(); + this.delegate?.onWindowStageCreate() + this.windowStage?.on('windowStageEvent', this.windowStageEventCallback); + this.hasInit = true; + } } private windowStageEventCallback = (data: window.WindowStageEventType) => { @@ -119,6 +124,11 @@ export default class FlutterEntry implements Host { this?.delegate?.onBackground(); } + onBackPress() { + Log.d(TAG, "FlutterEntry onBackPress==="); + this?.delegate?.flutterEngine?.getNavigationChannel()?.popRoute(); + } + shouldDispatchAppLifecycleState(): boolean { return true; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets index 483813fa6ae5e97b4b79abf9c8748ecdac90eebb..53e7ef62d1d0724238bbdf4e2cfe58bf1de4eb64 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets @@ -46,7 +46,7 @@ export struct FlutterPage { build() { Stack() { - XComponent({ id: this.viewId, type: 'texture', libraryname: 'flutter' }) + XComponent({ id: this.viewId, type: XComponentType.TEXTURE, libraryname: 'flutter' }) .focusable(true) .onLoad((context) => { this.flutterView?.onSurfaceCreated() diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BasicMessageChannel.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BasicMessageChannel.ets index 796f8edb8936927b11819282e8721b41da55a103..4b627aea9e4048ed696dedb912991fd13c70a785 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BasicMessageChannel.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BasicMessageChannel.ets @@ -136,7 +136,7 @@ class IncomingReplyHandler implements BinaryReply { this.codec = codec } - reply(reply: ArrayBuffer) { + reply(reply: ArrayBuffer | null) { try { this.callback(this.codec.decodeMessage(reply)); } catch (e) { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryCodec.ets index 578436417dc4a23cfaf2c5adbb9c8340efa5795d..1c15c762108efc389aef0b9d2bf3ff8b07b01991 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryCodec.ets @@ -37,9 +37,9 @@ export default class BinaryCodec implements MessageCodec { return message } - decodeMessage(message: ArrayBuffer): ArrayBuffer { + decodeMessage(message: ArrayBuffer | null): ArrayBuffer { if (message == null) { - return message; + return new ArrayBuffer(0); } else if (this.returnsDirectByteBufferFromDecoding) { return message; } else { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryMessenger.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryMessenger.ets index e0b55276beb3893f0347d9893ecaab9d2754174c..68925a8b1abcaf1f99964f67e52da6e99cc8ba8c 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryMessenger.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/BinaryMessenger.ets @@ -49,7 +49,7 @@ export interface BinaryReply { * outgoing replies must place the reply bytes between position zero and current position. * Reply receivers can read from the buffer directly. */ - reply(reply: ArrayBuffer): void; + reply(reply: ArrayBuffer | null): void; } /** Handler for incoming binary messages from Flutter. */ @@ -96,7 +96,7 @@ export interface BinaryMessenger { * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message * bytes between position zero and current position, or null. */ - send(channel: String, message: ArrayBuffer): void; + send(channel: String, message: ArrayBuffer | null): void; /** * Sends a binary message to the Flutter application, optionally expecting a reply. @@ -127,7 +127,7 @@ export interface BinaryMessenger { * the handler. Specifying null means execute on the platform thread. */ //setMessageHandler(channel: String, handler: BinaryMessageHandler) - setMessageHandler(channel: String, handler: BinaryMessageHandler, taskQueue?: TaskQueue): void; + setMessageHandler(channel: String, handler: BinaryMessageHandler | null, taskQueue?: TaskQueue): void; // { // if (taskQueue != null) { // throw new Error("setMessageHandler called with nonnull taskQueue is not supported.") diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/EventChannel.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/EventChannel.ets index 166659a32e819580f2d433886f763dc453d8a597..0c2ee0884cd3ee114ef244fda91631d4bbc71407 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/EventChannel.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/EventChannel.ets @@ -40,13 +40,13 @@ export default class EventChannel { private messenger: BinaryMessenger; private name: string; private codec: MethodCodec; - private taskQueue: TaskQueue; + private taskQueue: TaskQueue | null; constructor(messenger: BinaryMessenger, name: string, codec?: MethodCodec, taskQueue?: TaskQueue) { this.messenger = messenger this.name = name this.codec = codec ? codec : StandardMethodCodec.INSTANCE - this.taskQueue = taskQueue + this.taskQueue = taskQueue ?? null } @@ -236,26 +236,26 @@ class EventSinkImplementation implements EventSink { return; } this.hasEnded = true; - this.messenger.send(this.name, null); + this.messenger.send(this.name, new ArrayBuffer(0)); } } class AtomicReference { - private value: T; + private value: T | null; - constructor(value: T) { + constructor(value: T | null) { this.value = value } - get(): T { + get(): T | null { return this.value; } - set(newValue: T): void { + set(newValue: T | null): void { this.value = newValue; } - getAndSet(newValue: T) { + getAndSet(newValue: T | null) { const oldValue = this.value; this.value = newValue; return oldValue; diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets index e766db57fc08160e21e5061e525a2c03e986621f..e37a3d27e51b0ad6e3b50c4a80d5760ffac28901 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/JSONMessageCodec.ets @@ -44,9 +44,9 @@ export default class JSONMessageCodec implements MessageCodec { return StringCodec.INSTANCE.encodeMessage(JSON.stringify(this.toBaseData(message))); } - decodeMessage(message: ArrayBuffer): ESObject { + decodeMessage(message: ArrayBuffer | null): ESObject { if (message == null) { - return null; + return StringUtils.stringToArrayBuffer(""); } try { const jsonStr = StringCodec.INSTANCE.decodeMessage(message); diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MessageCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MessageCodec.ets index 663d57af19c70523e44a5f48cc61e22ab0dc5b33..6cb2c033b63e20323e89aaecec9893bdfbadb92e 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MessageCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MessageCodec.ets @@ -26,5 +26,5 @@ export default interface MessageCodec { * Decodes the specified message from binary. * */ - decodeMessage(message: ArrayBuffer): T; + decodeMessage(message: ArrayBuffer | null): T; } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MethodChannel.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MethodChannel.ets index f42df11830d1a0eab1adc73acc30bde2d09406e1..e9c3a7cc150e82b69b9b0b488a4920924a6ea98f 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MethodChannel.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/MethodChannel.ets @@ -165,7 +165,7 @@ export class IncomingResultHandler implements BinaryReply { this.codec = codec } - reply(reply: ArrayBuffer): void { + reply(reply: ArrayBuffer | null): void { try { if (reply == null) { this.callback.notImplemented(); diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets index 2ee2ef39b25336d660cf74425d99f5477f11032e..8e740f95fda488bacf9c52263ea43b69e9d9e64d 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StandardMessageCodec.ets @@ -76,7 +76,7 @@ export default class StandardMessageCodec implements MessageCodec { return stream.buffer } - decodeMessage(message: ArrayBuffer): ESObject { + decodeMessage(message: ArrayBuffer | null): ESObject { if (message == null) { return null } @@ -233,7 +233,7 @@ export default class StandardMessageCodec implements MessageCodec { } readValue(buffer: ByteBuffer): ESObject { - let type = buffer.readInt8() + let type = buffer.readUint8() return this.readValueOfType(type, buffer); } @@ -339,7 +339,7 @@ export default class StandardMessageCodec implements MessageCodec { break; } default: - throw new Error("Message corrupted"); + throw new Error("Message corrupted, type=" + type); } return result; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StringCodec.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StringCodec.ets index b824ac1f901b1dc9d1ae36f49e7fc74954595f4f..3ce96e365ce74d7e82bbf2f742c1bdb3800afb27 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StringCodec.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/common/StringCodec.ets @@ -33,7 +33,7 @@ export default class StringCodec implements MessageCodec { return StringUtils.stringToArrayBuffer(message); } - decodeMessage(message: ArrayBuffer): string { + decodeMessage(message: ArrayBuffer | null): string { if (message == null) { return ""; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets index 953f7cad09b96e97f5bbc45ee519cf70358d2eed..0726ab0fb43398a1d60062f472484c3f6b3601d5 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets @@ -19,10 +19,11 @@ import inputMethod from '@ohos.inputMethod'; import ArrayList from '@ohos.util.ArrayList'; import { TextEditingDelta } from './TextEditingDelta'; import TextInputChannel from '../../embedding/engine/systemchannels/TextInputChannel'; -const TAG = "ListenableEditingState"; -export class ListenableEditingState { - private TextInputChannel: TextInputChannel | null = null ; +const TAG = "ListenableEditingState"; + +export class ListenableEditingState { + private TextInputChannel: TextInputChannel | null = null; private client: number = 0 //Cache used to storage software keyboard input action private mStringCache: string; @@ -56,6 +57,34 @@ export class ListenableEditingState { this.mComposingEndCache = -1; } + extractBatchTextEditingDeltas(): ArrayList { + let currentBatchDeltas = new ArrayList(); + this.mBatchTextEditingDeltas.forEach((data) => { + currentBatchDeltas.add(data); + }) + this.mBatchTextEditingDeltas.clear(); + return currentBatchDeltas; + } + + clearBatchDeltas(): void { + this.mBatchTextEditingDeltas.clear(); + } + + replace(start: number, end: number, tb: String, tbStart: number, tbEnd: number): void { + const placeIndex = this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + + this.mBatchTextEditingDeltas.add( + new TextEditingDelta( + this.mStringCache.toString(), + placeIndex + tbEnd, + placeIndex + tbEnd, + this.getComposingStart(), + this.getComposingEnd(), + start, + end + tbStart, + tb.toString() + )); + } getSelectionStart(): number { return this.mSelectionStartCache; @@ -116,20 +145,26 @@ export class ListenableEditingState { } handleInsertTextEvent(text: string): void { - if(this.mStringCache.length == this.mSelectionStartCache) { + let start = this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + let end = this.mSelectionStartCache > this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + const length = text.length; + this.replace(start, end, text, 0, length); + + if (this.mStringCache.length == this.mSelectionStartCache) { //Insert text one by one - this.mStringCache += text; + let tempStr: string = this.mStringCache.substring(0, start) + text + this.mStringCache.substring(end); + this.mStringCache = tempStr; this.setSelectionStart(this.mStringCache.length); this.setSelectionEnd(this.mStringCache.length); - } else if(this.mStringCache.length > this.mSelectionStartCache) { + } else if (this.mStringCache.length > this.mSelectionStartCache) { //Insert text in the middle of string - let tempStr: string = this.mStringCache.substring(0, this.mSelectionStartCache) + text + this.mStringCache.substring(this.mSelectionStartCache); + let tempStr: string = this.mStringCache.substring(0, start) + text + this.mStringCache.substring(end); this.mStringCache = tempStr; - this.mSelectionStartCache += text.length; + this.mSelectionStartCache = start + text.length; this.mSelectionEndCache = this.mSelectionStartCache; } - if(this.mListeners == null) { + if (this.mListeners == null) { Log.e(TAG, "mListeners is null"); return; } @@ -139,7 +174,7 @@ export class ListenableEditingState { updateTextInputState(state: TextEditState): void { this.beginBatchEdit(); this.setStringCache(state.text); - if(state.hasSelection()) { + if (state.hasSelection()) { this.setSelectionStart(state.selectionStart); this.setSelectionEnd(state.selectionEnd); } else { @@ -217,22 +252,30 @@ export class ListenableEditingState { } handleDeleteEvent(leftOrRight: boolean, length: number): void { - if(leftOrRight == false) { + let start = this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + let end = this.mSelectionStartCache > this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + + if (leftOrRight == false) { //delete left - if(this.mSelectionStartCache == 0) { + if (start == 0 && end == 0) { return; } - this.mSelectionStartCache -= length; - let tempStr: string = this.mStringCache.slice(0, this.mSelectionStartCache) + this.mStringCache.slice(this.mSelectionStartCache + length); + const startData = start == end ? start - 1 : start; + const tbEnd = start == end ? -1 : 0; + this.replace(startData, end, "", 0, tbEnd); + this.mSelectionStartCache = startData; + let tempStr: string = this.mStringCache.slice(0, startData) + this.mStringCache.slice(end); this.mStringCache = tempStr; this.mSelectionEndCache = this.mSelectionStartCache; - } else if(leftOrRight == true) { + } else if (leftOrRight == true) { //delete right - if(this.mSelectionStartCache == this.mStringCache.length) { + if (start == this.mStringCache.length) { return; } - this.mSelectionEndCache += length; - let tempStr: string = this.mStringCache.slice(0,this.mSelectionStartCache) + this.mStringCache.slice(this.mSelectionEndCache); + const tbStart = start == end ? 1 : 0; + this.replace(start, end, "", tbStart, 0); + this.mSelectionEndCache = start; + let tempStr: string = this.mStringCache.slice(0, start) + (end + 1 >= this.mStringCache.length ? "" : this.mStringCache.slice(end + 1)); this.mStringCache = tempStr; this.mSelectionStartCache = this.mSelectionEndCache; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextEditingDelta.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextEditingDelta.ets index 1aa1b2d87245f029c1b8a630fecf02bceb41ea8d..495c4a51821d5b9279c56dc134138e5ccb17b41f 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextEditingDelta.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextEditingDelta.ets @@ -38,11 +38,11 @@ export class TextEditingDelta { this.newSelectionEnd = selectionEnd; this.newComposingStart = composingStart; this.newComposingEnd = composingEnd; - if(replacementDestinationStart === undefined || + if (replacementDestinationStart === undefined || replacementDestinationEnd === undefined || replacementSource === undefined) { this.setDeltas(oldEditable, "", -1, -1); - } else { + } else { this.setDeltas( oldEditable, replacementSource, @@ -58,4 +58,30 @@ export class TextEditingDelta { this.deltaStart = newStart; this.deltaEnd = newExtent; } + + toJSON(): TextEditingDeltaJson { + let state: TextEditingDeltaJson = { + oldText: this.oldText.toString(), + deltaText: this.deltaText.toString(), + deltaStart: this.deltaStart, + deltaEnd: this.deltaEnd, + selectionBase: this.newSelectionStart, + selectionExtent: this.newSelectionEnd, + composingBase: this.newComposingStart, + composingExtent: this.newComposingEnd, + }; + return state; + } +} + + +export interface TextEditingDeltaJson { + oldText: string; + deltaText: string; + deltaStart: number; + deltaEnd: number; + selectionBase: number; + selectionExtent: number; + composingBase: number; + composingExtent: number; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets index ca7f22a339dd13c6b6fcf4c06a1a1b4f40604e16..9252bf0b0242a9e09e579d883b4546f25e36bf6d 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets @@ -13,14 +13,17 @@ * limitations under the License. */ -import TextInputChannel, { Configuration, TextEditState, +import TextInputChannel, { + Configuration, + TextEditState, TextInputMethodHandler, - TextInputType } from '../../embedding/engine/systemchannels/TextInputChannel'; + TextInputType +} from '../../embedding/engine/systemchannels/TextInputChannel'; import inputMethod from '@ohos.inputMethod'; import Log from '../../util/Log'; import { EditingStateWatcher, ListenableEditingState } from './ListenableEditingState'; -export default class TextInputPlugin implements EditingStateWatcher{ +export default class TextInputPlugin implements EditingStateWatcher { private static TAG = "TextInputPlugin"; private textInputChannel: TextInputChannel; private mTextInputHandler: TextInputMethodHandlerImpl; @@ -42,19 +45,25 @@ export default class TextInputPlugin implements EditingStateWatcher{ didChangeEditingState(textChanged: boolean, selectionChanged: boolean, composingRegionChanged: boolean): void { let editable = this.mTextInputHandler.mEditable; let inputTarget = this.mTextInputHandler.inputTarget; - this.textInputChannel.updateEditingState(inputTarget.id, editable.getStringCache(), - editable.getSelectionStart(), editable.getSelectionEnd(), - editable.getComposingStart(), editable.getComposingEnd()) + let configuration = this.mTextInputHandler.configuration; + if (configuration!= null && configuration.enableDeltaModel) { + this.textInputChannel.updateEditingStateWithDeltas(inputTarget.id,editable.extractBatchTextEditingDeltas()); + editable.clearBatchDeltas(); + } else { + this.textInputChannel.updateEditingState(inputTarget.id, editable.getStringCache(), + editable.getSelectionStart(), editable.getSelectionEnd(), + editable.getComposingStart(), editable.getComposingEnd()) + } + } detach(): void { this.mTextInputHandler.inputMethodController.detach((err) => { - if(err) { + if (err) { Log.e(TextInputPlugin.TAG, "Failed to detach: " + JSON.stringify(err)); } }) } - } class TextInputMethodHandlerImpl implements TextInputMethodHandler { @@ -62,7 +71,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { private textConfig: inputMethod.TextConfig; inputMethodController: inputMethod.InputMethodController; inputTarget: InputTarget; - private configuration: Configuration | null = null; + public configuration: Configuration | null = null; mEditable: ListenableEditingState; private mRestartInputPending: boolean = false; private plugin: EditingStateWatcher | ESObject; @@ -74,7 +83,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { inputAttribute: { textInputType: 0, enterKeyType: 1 - }}; + } }; this.plugin = plugin; this.mEditable = new ListenableEditingState(null, 0); this.inputMethodController = inputMethod.getController(); @@ -82,7 +91,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } show(): void { - if(this.keyboardStatus == KeyboardStatus.SHOW){ + if (this.keyboardStatus == KeyboardStatus.SHOW) { return; } this.showTextInput(); @@ -103,7 +112,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } setClient(textInputClientId: number, configuration: Configuration | null): void { - Log.d(TextInputMethodHandlerImpl.TAG,"textInputClientId: " + textInputClientId); + Log.d(TextInputMethodHandlerImpl.TAG, "textInputClientId: " + textInputClientId); this.setTextInputClient(textInputClientId, configuration); } @@ -116,7 +125,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } setEditingState(editingState: TextEditState): void { - Log.d(TextInputMethodHandlerImpl.TAG, "text:" + editingState.text +" selectionStart:" + editingState.selectionStart + " selectionEnd:" + Log.d(TextInputMethodHandlerImpl.TAG, "text:" + editingState.text + " selectionStart:" + editingState.selectionStart + " selectionEnd:" + editingState.selectionEnd + " composingStart:" + editingState.composingStart + " composingEnd" + editingState.composingEnd); this.mEditable.updateTextInputState(editingState); } @@ -127,10 +136,10 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { private async showTextInput(): Promise { await this.attach(true); - if(this.imcFlag != true) { + if (this.imcFlag != true) { this.listenKeyBoardEvent(); } - this.inputMethodController.showTextInput().then(()=> { + this.inputMethodController.showTextInput().then(() => { Log.d(TextInputMethodHandlerImpl.TAG, "Succeeded in showing softKeyboard"); }).catch((err: ESObject) => { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to show softKeyboard:" + JSON.stringify(err)); @@ -145,7 +154,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { }) } - async attach(showKeyboard: boolean): Promise { + async attach(showKeyboard: boolean): Promise { try { await this.inputMethodController.attach(showKeyboard, this.textConfig); } catch (err) { @@ -154,17 +163,15 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } setTextInputClient(client: number, configuration: Configuration | null): void { - const INPUT_TYPE_NAME = ['NONE', 'TEXT', 'MULTILINE', 'NUMBER', 'PHONE','DATETIME','EMAIL_ADDRESS','URL','VISIBLE_PASSWORD'] - if(configuration) - { + const INPUT_TYPE_NAME = ['NONE', 'TEXT', 'MULTILINE', 'NUMBER', 'PHONE', 'DATETIME', 'EMAIL_ADDRESS', 'URL', 'VISIBLE_PASSWORD'] + if (configuration) { this.configuration = configuration; - if(configuration.inputType) - { - this.textConfig.inputAttribute.textInputType =INPUT_TYPE_NAME.indexOf(configuration.inputType.type) + if (configuration.inputType) { + this.textConfig.inputAttribute.textInputType = INPUT_TYPE_NAME.indexOf(configuration.inputType.type) this.textConfig.inputAttribute.enterKeyType = configuration.inputAction as ESObject; } } - if(this.canShowTextInput()) { + if (this.canShowTextInput()) { this.inputTarget = new InputTarget(Type.FRAMEWORK_CLIENT, client); } else { this.inputTarget = new InputTarget(Type.NO_TARGET, client); @@ -178,7 +185,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } canShowTextInput(): boolean { - if(this.configuration == null || this.configuration.inputType == null) { + if (this.configuration == null || this.configuration.inputType == null) { return true; } return this.configuration.inputType.type != TextInputType.NONE; @@ -228,7 +235,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { try { this.inputMethodController.on('sendKeyboardStatus', (state) => { - if(state == KeyboardStatus.HIDE) { + if (state == KeyboardStatus.HIDE) { this.keyboardStatus = KeyboardStatus.HIDE; this.plugin.textInputChannel.onConnectionClosed(this.inputTarget.id); } @@ -261,7 +268,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } public clearTextInputClient(): void { - if(this.inputTarget.type == Type.VIRTUAL_DISPLAY_PLATFORM_VIEW) { + if (this.inputTarget.type == Type.VIRTUAL_DISPLAY_PLATFORM_VIEW) { return; } this.mEditable.removeEditingStateListener(this.plugin); @@ -295,5 +302,4 @@ export class InputTarget { this.type = type; this.id = id; } - } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/Log.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/Log.ets index 437b52f18049b9ba77e47350033482a06368c5c4..33796b25fe8d344ceb1f83eeefc36b210f5f5711 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/Log.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/Log.ets @@ -91,6 +91,12 @@ export default class Log { */ static e(tag: string, format: string, ...args: ESObject[]) { if (Log.isLoggable(HiLog.LogLevel.ERROR)) { + args.forEach((item: ESObject, index: number) => { + if (item instanceof Error) { + args[index] = item.message + item.stack; + } + format+="%{public}s"; + }) HiLog.error(DOMAIN, TAG, tag + SYMBOL + format, args); } } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/PathUtils.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/PathUtils.ets index 691ea0f360b40adb266bde566c2f66292a2aa0ff..f47bcb68b0755dba14200d8a44db899528119191 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/PathUtils.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/PathUtils.ets @@ -30,7 +30,7 @@ export default class PathUtils { return context.cacheDir; } - static getDataDirectory(context: common.Context): string { + static getDataDirectory(context: common.Context): string | null { const name = "flutter"; const flutterDir = context.filesDir + "/" + name; if (!fs.accessSync(flutterDir)) { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterNativeView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterNativeView.ets deleted file mode 100644 index 9138ad728502e49e675e69e1fd533d80ad7016bc..0000000000000000000000000000000000000000 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterNativeView.ets +++ /dev/null @@ -1,137 +0,0 @@ -/* -* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import common from '@ohos.app.ability.common'; -import DartExecutor from '../embedding/engine/dart/DartExecutor'; -import { EngineLifecycleListener } from '../embedding/engine/FlutterEngine'; -import FlutterNapi from '../embedding/engine/FlutterNapi'; -import Log from '../util/Log'; -import FlutterPluginRegistry from '../app/FlutterPluginRegistry'; -import FlutterRunArguments from './FlutterRunArguments'; -import { FlutterView } from './FlutterView'; - -const TAG: string = "FlutterNativeView"; - -export default class FlutterNativeView { - private mContext: common.Context; - private mPluginRegistry: FlutterPluginRegistry; - private mFlutterNapi: FlutterNapi; - private dartExecutor: DartExecutor; - private mFlutterView: FlutterView; - private applicationIsRunning: boolean; - - constructor(context: common.Context, isBackgroundView?: boolean) { - if (isBackgroundView) { - Log.i(TAG, "isBackgroundView is no longer supported and will be ignored"); - } - this.mContext = context; - this.mPluginRegistry = new FlutterPluginRegistry(); - this.mFlutterNapi = new FlutterNapi(); - //this.mFlutterNapi.addIsDisplayingFlutterUiListener(this.flutterUiDisplayListener); - this.dartExecutor = new DartExecutor(this.mFlutterNapi, this.mContext.resourceManager); - this.mFlutterNapi.addEngineLifecycleListener(new EngineLifecycleListenerImpl(this.mFlutterView, this.mPluginRegistry)); - this.attach(this.mFlutterNapi, this.dartExecutor); - this.assertAttached(this.mFlutterNapi); - } - - attach(flutterNapi: FlutterNapi, dartExecutor: DartExecutor): void { - flutterNapi.attachToNative(); - dartExecutor.onAttachedToNAPI(); - } - - assertAttached(flutterNapi: FlutterNapi): void { - if (!this.isAttached(flutterNapi)) { - throw new Error('Platform View is not attached'); - } - } - - isAttached(flutterNapi: FlutterNapi): boolean { - return flutterNapi.isAttached(); - } - - detachFromFlutterView(): void { - this.mPluginRegistry.detach(); - this.mFlutterView = null; - } - - destroy(): void { - this.mPluginRegistry.destroy(); - this.dartExecutor.onDetachedFromNAPI(); - this.mFlutterView = null; - //this.mFlutterNapi.removeIsDisplayingFlutterUiListener(this.flutterUiDisplayListener); - this.applicationIsRunning = false; - } - - getDartExecutor(): DartExecutor { - return this.dartExecutor; - } - - getPluginRegistry(): FlutterPluginRegistry { - return this.mPluginRegistry; - } - - attachViewAndAbility(flutterView: FlutterView, context: common.Context): void { - this.mFlutterView = flutterView; - this.mPluginRegistry.attach(flutterView, context); - } - - runFromBundle(args: FlutterRunArguments): void { - if (args.entrypoint == null) { - throw new Error("an entrypoint must be specific"); - } - this.assertAttached(this.mFlutterNapi); - if (this.applicationIsRunning) { - throw new Error("this flutter engine instance is already running an application"); - } - this.mFlutterNapi.runBundleAndSnapshotFromLibrary(args.bundlePath, args.entrypoint, args.libraryPath, this.mContext.resourceManager, null); - this.applicationIsRunning = true; - } - - isApplicationRunning(): boolean { - return this.applicationIsRunning; - } - - // getObservatoryUri(): string { - // return this.mFlutterNapi.getObservatoryUri(); - // } -} - -class EngineLifecycleListenerImpl implements EngineLifecycleListener { - private flutterView: FlutterView; - private pluginRegistry: FlutterPluginRegistry; - - onPreEngineRestart(): void { - if (this.flutterView != null) { - //this.flutterView.resetAccessibilityTree(); - } - - if (this.pluginRegistry == null) { - return; - } - - this.pluginRegistry.onPreEngineRestart(); - } - - onEngineWillDestroy(): void { - - } - - constructor(flutterView: FlutterView, pluginRegistry: FlutterPluginRegistry) { - this.flutterView = flutterView; - this.pluginRegistry = pluginRegistry; - } -} - - diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterRunArguments.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterRunArguments.ets index d17ec28d1fdc6237ee486362751ee25e58590887..c81e33f381f06662a8b881df3452d76038837361 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterRunArguments.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterRunArguments.ets @@ -17,4 +17,10 @@ export default class FlutterRunArguments { public bundlePath: string; public entrypoint: string; public libraryPath: string; + + constructor(bundlePath: string, entrypoint: string, libraryPath: string) { + this.bundlePath = bundlePath; + this.entrypoint = entrypoint; + this.libraryPath = libraryPath; + } } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/TextureRegistry.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/TextureRegistry.ets index a37ab971b769320021695970ff20e2cac62d880b..fa79eb238c0781f3f23b690e00d08101a0969830 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/TextureRegistry.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/TextureRegistry.ets @@ -12,25 +12,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import image from '@ohos.multimedia.image'; export interface TextureRegistry { createSurfaceTexture(): SurfaceTextureEntry; - registerSurfaceTexture(): SurfaceTextureEntry; - + getTextureId(): number; + registerTexture(textureId: number): SurfaceTextureEntry; + registerSurfaceTexture(receiver: image.ImageReceiver): SurfaceTextureEntry; + registerPixelMap(pixelMap: PixelMap): number; + unregisterTexture(textureId: number): void; onTrimMemory(level: number) : void; } -interface SurfaceTextureEntry { - id(): number; +export interface SurfaceTextureEntry { + getTextureId(): number; + + getSurfaceId(): number; + + getImageReceiver(): image.ImageReceiver; release(): void; } -interface OnFrameConsumedListener { +export interface OnFrameConsumedListener { onFrameConsumed(): void; } -interface OnTrimMemoryListener { +export interface OnTrimMemoryListener { onTrimMemory(level: number) : void; } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/hvigor/hvigor-config.json5 b/shell/platform/ohos/flutter_embedding/hvigor/hvigor-config.json5 index 3496547c0bf7facd5ffce8a2ee7ff8ea64fd5ef8..286777e6ccb87c14ece3c00f77729bae7baf72c9 100755 --- a/shell/platform/ohos/flutter_embedding/hvigor/hvigor-config.json5 +++ b/shell/platform/ohos/flutter_embedding/hvigor/hvigor-config.json5 @@ -12,9 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + { "hvigorVersion": "4.0.2", "dependencies": { "@ohos/hvigor-ohos-plugin": "4.0.2", } -} + } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/hvigorfile.ts b/shell/platform/ohos/flutter_embedding/hvigorfile.ts index 5a172b770e3b15f67c12152d00f38f2084d3915b..8f2d2aafe6d6a3a71a9944ebd0c91fbc308ac9d1 100755 --- a/shell/platform/ohos/flutter_embedding/hvigorfile.ts +++ b/shell/platform/ohos/flutter_embedding/hvigorfile.ts @@ -13,5 +13,9 @@ * limitations under the License. */ -// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. -export { appTasks } from '@ohos/hvigor-ohos-plugin'; \ No newline at end of file +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/shell/platform/ohos/library_loader.cpp b/shell/platform/ohos/library_loader.cpp index 6e38f769bc54fc7115650c6372072d1fe36f67da..f6c52a6aff2433326b3cb4d5a1c250ca5054b355 100644 --- a/shell/platform/ohos/library_loader.cpp +++ b/shell/platform/ohos/library_loader.cpp @@ -107,8 +107,25 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION( "nativeXComponentDetachFlutterEngine", flutter::PlatformViewOHOSNapi::nativeXComponentDetachFlutterEngine), + DECLARE_NAPI_FUNCTION( + "nativeInitNativeImage", + flutter::PlatformViewOHOSNapi::nativeInitNativeImage), + DECLARE_NAPI_FUNCTION( + "nativeRegisterTexture", + flutter::PlatformViewOHOSNapi::nativeRegisterTexture), + DECLARE_NAPI_FUNCTION( + "nativeUnregisterTexture", + flutter::PlatformViewOHOSNapi::nativeUnregisterTexture), + DECLARE_NAPI_FUNCTION( + "nativeMarkTextureFrameAvailable", + flutter::PlatformViewOHOSNapi::nativeMarkTextureFrameAvailable), + DECLARE_NAPI_FUNCTION( + "nativeRegisterPixelMap", + flutter::PlatformViewOHOSNapi::nativeRegisterPixelMap), }; + + FML_DLOG(INFO) << "Init NAPI size=" << sizeof(desc) / sizeof(desc[0]); napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); bool ret = flutter::XComponentAdapter::GetInstance()->Export(env, exports); if (!ret) { diff --git a/shell/platform/ohos/napi/platform_view_ohos_napi.cpp b/shell/platform/ohos/napi/platform_view_ohos_napi.cpp index 3572bee972dbbea2ffeb24e13ac504add33ac409..848ac957d7bec3074ce03a128293c2c67a98c739 100644 --- a/shell/platform/ohos/napi/platform_view_ohos_napi.cpp +++ b/shell/platform/ohos/napi/platform_view_ohos_napi.cpp @@ -27,6 +27,9 @@ #include "flutter/shell/platform/ohos/surface/ohos_native_window.h" #include "unicode/uchar.h" #include "flutter/shell/platform/ohos/ohos_xcomponent_adapter.h" +#include +#include +#include #define OHOS_SHELL_HOLDER (reinterpret_cast(shell_holder)) namespace flutter { @@ -303,7 +306,7 @@ void PlatformViewOHOSNapi::FlutterViewHandlePlatformMessage( FML_DLOG(ERROR) << "napi_create_string_utf8 err " << status; return; } - + FML_DLOG(INFO) << "FlutterViewHandlePlatformMessage mapData= " << mapData; if (mapData) { delete mapData; } @@ -1419,6 +1422,80 @@ napi_value PlatformViewOHOSNapi::nativeGetSystemLanguages( return nullptr; } +napi_value PlatformViewOHOSNapi::nativeInitNativeImage( + napi_env env, + napi_callback_info info) { + FML_DLOG(INFO)<<"PlatformViewOHOSNapi::nativeInitNativeImage"; + size_t argc = 3; + napi_value args[3] = {nullptr}; + int64_t shell_holder, textureId; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + NAPI_CALL(env, napi_get_value_int64(env, args[0], &shell_holder)); + NAPI_CALL(env, napi_get_value_int64(env, args[1], &textureId)); + ImageNative *imageNative = OH_Image_InitImageNative(env, args[2]); + // std::unique_ptr uImage; + OHOS_SHELL_HOLDER->GetPlatformView()->RegisterExternalTextureByImage(textureId, imageNative); + return nullptr; +} + +napi_value PlatformViewOHOSNapi::nativeRegisterTexture(napi_env env, + napi_callback_info info) +{ + FML_DLOG(INFO)<<"PlatformViewOHOSNapi::nativeRegisterTexture"; + size_t argc = 2; + napi_value args[2] = {nullptr}; + int64_t shell_holder, textureId; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + NAPI_CALL(env, napi_get_value_int64(env, args[0], &shell_holder)); + NAPI_CALL(env, napi_get_value_int64(env, args[1], &textureId)); + int64_t surfaceId = OHOS_SHELL_HOLDER->GetPlatformView()->RegisterExternalTexture(textureId); + napi_value res; + napi_create_int64(env, surfaceId, &res); + return res; +} + +napi_value PlatformViewOHOSNapi::nativeUnregisterTexture( + napi_env env, napi_callback_info info) +{ + FML_DLOG(INFO)<<"PlatformViewOHOSNapi::nativeUnregisterTexture"; + size_t argc = 2; + napi_value args[2] = {nullptr}; + int64_t shell_holder, textureId; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + NAPI_CALL(env, napi_get_value_int64(env, args[0], &shell_holder)); + NAPI_CALL(env, napi_get_value_int64(env, args[1], &textureId)); + OHOS_SHELL_HOLDER->GetPlatformView()->UnRegisterExternalTexture(textureId); + return nullptr; +} + +napi_value PlatformViewOHOSNapi::nativeMarkTextureFrameAvailable( + napi_env env, napi_callback_info info) +{ + size_t argc = 2; + napi_value args[2] = {nullptr}; + int64_t shell_holder, textureId; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + NAPI_CALL(env, napi_get_value_int64(env, args[0], &shell_holder)); + NAPI_CALL(env, napi_get_value_int64(env, args[1], &textureId)); + OHOS_SHELL_HOLDER->GetPlatformView()->MarkTextureFrameAvailable(textureId); + return nullptr; +} + +napi_value PlatformViewOHOSNapi::nativeRegisterPixelMap(napi_env env, + napi_callback_info info) +{ + FML_DLOG(INFO)<<"PlatformViewOHOSNapi::nativeRegisterPixelMap"; + size_t argc = 3; + napi_value args[3] = {nullptr}; + int64_t shell_holder, textureId; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + NAPI_CALL(env, napi_get_value_int64(env, args[0], &shell_holder)); + NAPI_CALL(env, napi_get_value_int64(env, args[1], &textureId)); + NativePixelMap *nativePixelMap = OH_PixelMap_InitNativePixelMap(env, args[2]); + OHOS_SHELL_HOLDER->GetPlatformView()->RegisterExternalTextureByPixelMap(textureId, nativePixelMap); + return nullptr; +} + void PlatformViewOHOSNapi::SurfaceCreated(int64_t shell_holder, void* window) { auto native_window = fml::MakeRefCounted( static_cast(window)); diff --git a/shell/platform/ohos/napi/platform_view_ohos_napi.h b/shell/platform/ohos/napi/platform_view_ohos_napi.h index 1e83a43017249553695f505e1b37df64bacabc9a..8339308b005f9f2ca0388d78accd55672cfc1cc8 100644 --- a/shell/platform/ohos/napi/platform_view_ohos_napi.h +++ b/shell/platform/ohos/napi/platform_view_ohos_napi.h @@ -143,6 +143,21 @@ class PlatformViewOHOSNapi { napi_env env, napi_callback_info info); // 应用下发系统语言设置 + static napi_value nativeInitNativeImage(napi_env env, + napi_callback_info info); + + static napi_value nativeUnregisterTexture(napi_env env, + napi_callback_info info); + + static napi_value nativeMarkTextureFrameAvailable(napi_env env, + napi_callback_info info); + + static napi_value nativeRegisterPixelMap(napi_env env, + napi_callback_info info); + + static napi_value nativeRegisterTexture(napi_env env, + napi_callback_info info); + // Surface相关,XComponent调用 static void SurfaceCreated(int64_t shell_holder, void* window); static void SurfaceChanged(int64_t shell_holder, diff --git a/shell/platform/ohos/ohos_external_texture_gl.cpp b/shell/platform/ohos/ohos_external_texture_gl.cpp index f2ffccbaa8168fda70c7f6cd5afdfa12b56767cc..b24c181e383f6c2fb484a5855cb3e6d3abdf6f0e 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.cpp +++ b/shell/platform/ohos/ohos_external_texture_gl.cpp @@ -14,7 +14,6 @@ */ #include "ohos_external_texture_gl.h" -#include #include @@ -25,32 +24,82 @@ #include "third_party/skia/include/gpu/GrBackendSurface.h" #include "third_party/skia/include/gpu/GrDirectContext.h" +#include +#include + +#define EGL_PLATFORM_OHOS_KHR 0x34E0 + namespace flutter { +using GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC; +constexpr char CHARACTER_WHITESPACE = ' '; +constexpr const char *CHARACTER_STRING_WHITESPACE = " "; +constexpr const char *EGL_EXT_PLATFORM_WAYLAND = "EGL_EXT_platform_wayland"; +constexpr const char *EGL_KHR_PLATFORM_WAYLAND = "EGL_KHR_platform_wayland"; +constexpr const char *EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT"; -OHOSExternalTextureGL::OHOSExternalTextureGL(int id) - : Texture(id), transform(SkMatrix::I()) {} +OHOSExternalTextureGL::OHOSExternalTextureGL(int64_t id, const std::shared_ptr& ohos_surface) + : Texture(id), ohos_surface_(std::move(ohos_surface)), transform(SkMatrix::I()) { + nativeImage_ = nullptr; + nativeWindow_ = nullptr; + eglContext_ = EGL_NO_CONTEXT; + eglDisplay_ = EGL_NO_DISPLAY; + buffer_ = nullptr; + pixelMap_ = nullptr; + lastImage_ = nullptr; +} OHOSExternalTextureGL::~OHOSExternalTextureGL() { if (state_ == AttachmentState::attached) { + glDeleteTextures(1, &texture_name_); } } + void OHOSExternalTextureGL::Paint(PaintContext& context, const SkRect& bounds, bool freeze, const SkSamplingOptions& sampling) { if (state_ == AttachmentState::detached) { + FML_DLOG(ERROR) << "OHOSExternalTextureGL::Paint"; return; } if (state_ == AttachmentState::uninitialized) { - Attach(static_cast(texture_name_)); - state_ = AttachmentState::attached; + OHOSSurface* ohos_surface_ptr = ohos_surface_.get(); + OhosSurfaceGLSkia* ohosSurfaceGLSkia_ = (OhosSurfaceGLSkia*)ohos_surface_ptr; + auto result = ohosSurfaceGLSkia_->GLContextMakeCurrent(); + if (result->GetResult()) { + FML_DLOG(INFO)<<"ResourceContextMakeCurrent successed"; + glGenTextures(1, &texture_name_); + FML_DLOG(INFO) << "OHOSExternalTextureGL::Paint, glGenTextures texture_name_=" << texture_name_; + if (nativeImage_ == nullptr) { + nativeImage_ = OH_NativeImage_Create(texture_name_, GL_TEXTURE_EXTERNAL_OES); + if (nativeImage_ == nullptr) { + FML_DLOG(ERROR) << "Error with OH_NativeImage_Create"; + return; + } + nativeWindow_ = OH_NativeImage_AcquireNativeWindow(nativeImage_); + if (nativeWindow_ == nullptr) { + FML_DLOG(ERROR) << "Error with OH_NativeImage_AcquireNativeWindow"; + return; + } + } + + int32_t ret = OH_NativeImage_AttachContext(nativeImage_, texture_name_); + if(ret != 0) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeImage_AttachContext err code:"<< ret; + } + state_ = AttachmentState::attached; + } else { + FML_DLOG(FATAL)<<"ResourceContextMakeCurrent failed"; + } } if (!freeze && new_frame_ready_) { Update(); new_frame_ready_ = false; } - GrGLTextureInfo textureInfo = {0, texture_name_, GL_RGBA8_OES}; + + GrGLTextureInfo textureInfo = {GL_TEXTURE_EXTERNAL_OES, texture_name_, + GL_RGBA8_OES}; GrBackendTexture backendTexture(1, 1, GrMipMapped::kNo, textureInfo); sk_sp image = SkImage::MakeFromTexture( context.gr_context, backendTexture, kTopLeft_GrSurfaceOrigin, @@ -61,8 +110,8 @@ void OHOSExternalTextureGL::Paint(PaintContext& context, // The incoming texture is vertically flipped, so we flip it // back. OpenGL's coordinate system has Positive Y equivalent to up, while // Skia's coordinate system has Negative Y equvalent to up. - context.canvas->translate(bounds.x(), bounds.y() + bounds.height()); - context.canvas->scale(bounds.width(), -bounds.height()); + context.canvas->translate(bounds.x(), bounds.y()); + context.canvas->scale(bounds.width(), bounds.height()); if (!transform.isIdentity()) { sk_sp shader = image->makeShader( @@ -81,41 +130,193 @@ void OHOSExternalTextureGL::Paint(PaintContext& context, } void OHOSExternalTextureGL::OnGrContextCreated() { + FML_DLOG(INFO)<<" OHOSExternalTextureGL::OnGrContextCreated"; state_ = AttachmentState::uninitialized; } void OHOSExternalTextureGL::OnGrContextDestroyed() { + FML_DLOG(INFO)<<" OHOSExternalTextureGL::OnGrContextDestroyed"; if (state_ == AttachmentState::attached) { Detach(); + glDeleteTextures(1, &texture_name_); + OH_NativeImage_Destroy(&nativeImage_); } state_ = AttachmentState::detached; } void OHOSExternalTextureGL::MarkNewFrameAvailable() { + FML_DLOG(INFO)<<" OHOSExternalTextureGL::MarkNewFrameAvailable"; new_frame_ready_ = true; } void OHOSExternalTextureGL::OnTextureUnregistered() { - // do nothing -} - -void OHOSExternalTextureGL::Attach(int textureName) { + FML_DLOG(INFO)<<" OHOSExternalTextureGL::OnTextureUnregistered"; // do nothing } void OHOSExternalTextureGL::Update() { + ProducePixelMapToNativeImage(); + int32_t ret = OH_NativeImage_UpdateSurfaceImage(nativeImage_); + if (ret != 0) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeImage_UpdateSurfaceImage err code:"<< ret; + } UpdateTransform(); } void OHOSExternalTextureGL::Detach() { - // do nothing + OH_NativeImage_DetachContext(nativeImage_); + OH_NativeWindow_DestroyNativeWindow(nativeWindow_); } void OHOSExternalTextureGL::UpdateTransform() { + float m[16] = { 0.0f }; + int32_t ret = OH_NativeImage_GetTransformMatrix(nativeImage_, m); + if(ret != 0) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeImage_GetTransformMatrix err code:"<< ret; + } + // transform ohos 4x4 matrix to skia 3x3 matrix + FML_DLOG(INFO)<<"OHOSExternalTextureGL::UpdateTransform "<virAddr, handle->size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd, 0); + if (mappedAddr == MAP_FAILED) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL mmap failed"; + return; + } + + void *pixelAddr = nullptr; + ret = OH_PixelMap_AccessPixels(pixelMap_, &pixelAddr); + if (ret != IMAGE_RESULT_SUCCESS) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_PixelMap_AccessPixels err:"<< ret; + return; + } + + uint32_t *value = static_cast(pixelAddr); + uint32_t *pixel = static_cast(mappedAddr); + uint32_t rowDataSize = 256; // DMA内存会自动补齐,分配内存时是 256 的整数倍 + while (rowDataSize < pixelMapInfo.rowSize) { + rowDataSize += 256; + } + + FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo w:" << pixelMapInfo.width << " h:" << pixelMapInfo.height; + FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo rowSize:" << pixelMapInfo.rowSize << " format:" << pixelMapInfo.pixelFormat; + FML_DLOG(INFO) << "OHOSExternalTextureGL pixelMapInfo rowDataSize:" << rowDataSize; + + // 复制图片纹理数据到内存中,需要处理DMA内存补齐相关的逻辑 + for (uint32_t i = 0; i < pixelMapInfo.height; i++) { + memcpy(pixel, value, rowDataSize); + pixel += rowDataSize / 4; + value += pixelMapInfo.width; + } + + OH_PixelMap_UnAccessPixels(pixelMap_); + //munmap after use + ret = munmap(mappedAddr, handle->size); + if (ret == -1) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL munmap failed"; + return; + } + Region region{nullptr, 0}; + ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer_, fenceFd, region); + if(ret != 0) { + FML_DLOG(FATAL)<<"OHOSExternalTextureGL OH_NativeWindow_NativeWindowFlushBuffer err:"<< ret; + } +} + +EGLDisplay OHOSExternalTextureGL::GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list) +{ + GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL; + + if (!eglGetPlatformDisplayExt) { + const char* extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); + if (extensions && + (CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) || + CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) { + eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT); + } + } + + if (eglGetPlatformDisplayExt) { + return eglGetPlatformDisplayExt(platform, native_display, attrib_list); + } + + return eglGetDisplay((EGLNativeDisplayType)native_display); +} + +bool OHOSExternalTextureGL::CheckEglExtension(const char *extensions, const char *extension) +{ + size_t extlen = strlen(extension); + const char *end = extensions + strlen(extensions); + while (extensions < end) { + size_t n = 0; + if (*extensions == CHARACTER_WHITESPACE) { + extensions++; + continue; + } + n = strcspn(extensions, CHARACTER_STRING_WHITESPACE); + if (n == extlen && strncmp(extension, extensions, n) == 0) { + return true; + } + extensions += n; + } + return false; +} + +void OHOSExternalTextureGL::DispatchPixelMap(NativePixelMap* pixelMap) +{ + if(pixelMap != nullptr) { + pixelMap_ = pixelMap; + } +} + } // 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 aa08880048c15b286713289593a7fb8073d7cb29..0e900e60a4b039166342332519f088cf5acfc3df 100755 --- a/shell/platform/ohos/ohos_external_texture_gl.h +++ b/shell/platform/ohos/ohos_external_texture_gl.h @@ -15,20 +15,32 @@ #ifndef OHOS_EXTERNAL_TEXTURE_GL_H #define OHOS_EXTERNAL_TEXTURE_GL_H -#include + +#include +#include +#include #include "flutter/common/graphics/texture.h" +#include "flutter/shell/platform/ohos/surface/ohos_surface.h" +#include "flutter/shell/platform/ohos/ohos_surface_gl_skia.h" #include "napi/platform_view_ohos_napi.h" +#include +#include +#include +#include +#include // maybe now unused namespace flutter { class OHOSExternalTextureGL : public flutter::Texture { public: - explicit OHOSExternalTextureGL(int id); + explicit OHOSExternalTextureGL(int64_t id, const std::shared_ptr& ohos_surface); ~OHOSExternalTextureGL() override; + OH_NativeImage *nativeImage_; + void Paint(PaintContext& context, const SkRect& bounds, bool freeze, @@ -42,15 +54,23 @@ class OHOSExternalTextureGL : public flutter::Texture { void OnTextureUnregistered() override; - private: - void Attach(int textureName); + void DispatchImage(ImageNative* image); + + void DispatchPixelMap(NativePixelMap* pixelMap); + private: void Update(); void Detach(); void UpdateTransform(); + EGLDisplay GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list); + + bool CheckEglExtension(const char *extensions, const char *extension); + + void ProducePixelMapToNativeImage(); + enum class AttachmentState { uninitialized, attached, detached }; AttachmentState state_ = AttachmentState::uninitialized; @@ -59,8 +79,25 @@ class OHOSExternalTextureGL : public flutter::Texture { GLuint texture_name_ = 0; + std::shared_ptr ohos_surface_; + SkMatrix transform; + OHNativeWindow *nativeWindow_; + + OHNativeWindowBuffer *buffer_; + + NativePixelMap* pixelMap_; + + ImageNative* lastImage_; + + OhosPixelMapInfos pixelMapInfo; + + int fenceFd = -1; + + EGLContext eglContext_; + EGLDisplay eglDisplay_; + FML_DISALLOW_COPY_AND_ASSIGN(OHOSExternalTextureGL); }; } // namespace flutter diff --git a/shell/platform/ohos/ohos_unified_surface.cpp b/shell/platform/ohos/ohos_unified_surface.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73c3a0c954d6b7f9ee95af2336d8e7b4f37284bb --- /dev/null +++ b/shell/platform/ohos/ohos_unified_surface.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flutter/shell/platform/ohos/ohos_unified_surface.h" +namespace flutter { + +OHOSUnifiedSurface::OHOSUnifiedSurface() {} + + +} // namespace flutter \ No newline at end of file diff --git a/shell/platform/ohos/ohos_unified_surface.h b/shell/platform/ohos/ohos_unified_surface.h new file mode 100644 index 0000000000000000000000000000000000000000..49aa35a3d6358c46efd96a4ce43f228ed02bcbe5 --- /dev/null +++ b/shell/platform/ohos/ohos_unified_surface.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_UNIFIED_SURFACE_H +#define OHOS_UNIFIED_SURFACE_H + +#include +#include "flutter/flow/surface.h" +#include "flutter/shell/platform/ohos/context/ohos_context.h" +#include "flutter/shell/platform/ohos/surface/ohos_native_window.h" +#include "flutter/shell/platform/ohos/surface/ohos_surface.h" +#include "flutter/shell/platform/ohos/ohos_surface_gl_skia.h" +#include "third_party/skia/include/core/SkSize.h" + +namespace flutter { + +class OHOSUnifiedSurface : public GPUSurfaceGLDelegate, + public OHOSSurface { + public: + ~OHOSUnifiedSurface() {} + bool IsValid() {} + void TeardownOnScreenContext() {} + + bool OnScreenSurfaceResize(const SkISize& size) {} + + bool ResourceContextMakeCurrent() {} + + bool ResourceContextClearCurrent() {} + + bool SetNativeWindow(fml::RefPtr window) {} + + std::unique_ptr CreateSnapshotSurface() + + std::unique_ptr CreateGPUSurface( + GrDirectContext* gr_context = nullptr) {} + + std::shared_ptr GetImpellerContext() {} + + std::unique_ptr GLContextMakeCurrent() {} + + bool GLContextClearCurrent() {} + + void GLContextSetDamageRegion(const std::optional& region) {} + + bool GLContextPresent(const GLPresentInfo& present_info) {} + + GLFBOInfo GLContextFBO(GLFrameInfo frame_info) {} + + bool GLContextFBOResetAfterPresent() {} + + SurfaceFrame::FramebufferInfo GLContextFramebufferInfo() {} + + SkMatrix GLContextSurfaceTransformation() {} + + sk_sp GetGLInterface() {} + + static sk_sp GetDefaultPlatformGLInterface() {} + + using GLProcResolver = + std::function; + + GLProcResolver GetGLProcResolver() {} + + bool AllowsDrawingWhenGpuDisabled() {} +}; +} // namespace flutter + +#endif \ No newline at end of file diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index f592b7e0dfcaf661d8c3e14d4100f2191a1e3dff..a657f6b949fecfd0af46467abe5b3852a0fe8f0f 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -268,10 +268,9 @@ void XComponentBase::DetachFlutterEngine() { if (window_ != nullptr) { PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(shellholderId_)); } else { - LOGE("OnSurfaceCreated XComponentBase is not attached"); + LOGE("DetachFlutterEngine XComponentBase is not attached"); } shellholderId_ = ""; - window_ = nullptr; isEngineAttached_ = false; } diff --git a/shell/platform/ohos/platform_view_ohos.cpp b/shell/platform/ohos/platform_view_ohos.cpp index b0a382254af8e4c02dc3e237918bbda4db549f1e..19f45611cf6ca78d6b12f8b0f7cd2eee86a2863c 100644 --- a/shell/platform/ohos/platform_view_ohos.cpp +++ b/shell/platform/ohos/platform_view_ohos.cpp @@ -25,6 +25,9 @@ #include "napi_common.h" #include "ohos_context_gl_impeller.h" #include "ohos_surface_gl_impeller.h" +#include "ohos_external_texture_gl.h" + +#include namespace flutter { @@ -387,4 +390,97 @@ void PlatformViewOHOS::FireFirstFrameCallback() { napi_facade_->FlutterViewOnFirstFrame(); } +void PlatformViewOHOS::RegisterExternalTextureByImage( + int64_t texture_id, + ImageNative* image) { + if(ohos_context_->RenderingApi() == OHOSRenderingAPI::kOpenGLES) { + auto iter = external_texture_gl_.find(texture_id); + if(iter != external_texture_gl_.end()) { + iter->second->DispatchImage(image); + } else { + std::shared_ptr ohos_external_gl = std::make_shared(texture_id, ohos_surface_); + external_texture_gl_[texture_id] = ohos_external_gl; + RegisterTexture(ohos_external_gl); + ohos_external_gl->DispatchImage(image); + } + } +} + +uint64_t PlatformViewOHOS::RegisterExternalTexture(int64_t texture_id) { + uint64_t surface_id = 0; + int ret = -1; + if (ohos_context_->RenderingApi() == OHOSRenderingAPI::kOpenGLES) { + std::shared_ptr ohos_external_gl = std::make_shared(texture_id, ohos_surface_); + 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); + nativeImageFrameAvailableListener_.context = contextData; + nativeImageFrameAvailableListener_.onFrameAvailable = &PlatformViewOHOS::OnNativeImageFrameAvailable; + ret = OH_NativeImage_SetOnFrameAvailableListener(ohos_external_gl->nativeImage_, nativeImageFrameAvailableListener_); + if (ret != 0) { + FML_DLOG(ERROR) << "Error with OH_NativeImage_SetOnFrameAvailableListener"; + return surface_id; + } + ret = OH_NativeImage_GetSurfaceId(ohos_external_gl->nativeImage_, &surface_id); + if (ret != 0) { + FML_DLOG(ERROR) << "Error with OH_NativeImage_GetSurfaceId"; + return surface_id; + } + RegisterTexture(ohos_external_gl); + } + return surface_id; +} + +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; + } + std::shared_ptr ohos_surface = frameData->context_->ohos_surface_; + const TaskRunners task_runners = frameData->context_->task_runners_;; + if (ohos_surface) { + fml::AutoResetWaitableEvent latch; + fml::TaskRunner::RunNowOrPostTask( + task_runners.GetPlatformTaskRunner(), + [&latch, surface = ohos_surface.get(), frameData]() { + frameData->context_->MarkTextureFrameAvailable(frameData->texture_id_); + latch.Signal(); + }); + latch.Wait(); + } +} + +void PlatformViewOHOS::UnRegisterExternalTexture(int64_t texture_id) +{ + external_texture_gl_.erase(texture_id); + UnregisterTexture(texture_id); +} + +void PlatformViewOHOS::RegisterExternalTextureByPixelMap(int64_t texture_id, NativePixelMap* pixelMap) +{ + if (ohos_context_->RenderingApi() == OHOSRenderingAPI::kOpenGLES) { + auto iter = external_texture_gl_.find(texture_id); + if (iter != external_texture_gl_.end()) { + iter->second->DispatchPixelMap(pixelMap); + } else { + std::shared_ptr ohos_external_gl = std::make_shared(texture_id, ohos_surface_); + external_texture_gl_[texture_id] = ohos_external_gl; + RegisterTexture(ohos_external_gl); + ohos_external_gl->DispatchPixelMap(pixelMap); + } + MarkTextureFrameAvailable(texture_id); + } +} + +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 a1b863ed345f0ee206825a79323a4c2fa67cfa61..387022cc08d65da5166961d155011b5d06a62432 100644 --- a/shell/platform/ohos/platform_view_ohos.h +++ b/shell/platform/ohos/platform_view_ohos.h @@ -31,6 +31,9 @@ #include "flutter/shell/platform/ohos/surface/ohos_snapshot_surface_producer.h" #include "flutter/shell/platform/ohos/surface/ohos_surface.h" #include "flutter/shell/platform/ohos/vsync_waiter_ohos.h" +#include +#include "ohos_external_texture_gl.h" +#include namespace flutter { @@ -84,6 +87,15 @@ class PlatformViewOHOS final : public PlatformView { int action, void* actionData, int actionDataLenth); + void RegisterExternalTextureByImage( + int64_t texture_id, + ImageNative* image); + + uint64_t RegisterExternalTexture(int64_t texture_id); + + void RegisterExternalTextureByPixelMap(int64_t texture_id, NativePixelMap* pixelMap); + + void UnRegisterExternalTexture(int64_t texture_id); // |PlatformView| void LoadDartDeferredLibrary( @@ -117,6 +129,7 @@ class PlatformViewOHOS final : public PlatformView { std::shared_ptr platform_message_handler_; std::shared_ptr surface_factory_; + std::map> external_texture_gl_; // |PlatformView| void UpdateSemantics( @@ -164,6 +177,23 @@ class PlatformViewOHOS final : public PlatformView { void FireFirstFrameCallback(); FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewOHOS); + + OH_OnFrameAvailableListener nativeImageFrameAvailableListener_{}; + + static void OnNativeImageFrameAvailable(void *data); +}; + +class OhosImageFrameData { + public: + OhosImageFrameData(PlatformViewOHOS* context, + int64_t texture_id); + + ~OhosImageFrameData(); + + PlatformViewOHOS* context_; + + int64_t texture_id_; }; + } // namespace flutter #endif diff --git a/tools/gn b/tools/gn index a5efe0f75d58748cd6047a78e22dc9735d7e7eb5..98a207a1bef7ad0368977ff735220ac56503f524 100755 --- a/tools/gn +++ b/tools/gn @@ -303,6 +303,9 @@ def to_gn_args(args): gn_args['is_official_build'] = True # Disable Skia test utilities. gn_args['android_full_debug' ] = args.target_os == 'android' and args.unoptimized + + gn_args['ohos_api_int'] = args.ohos_api_int + if args.clang is None: gn_args['is_clang'] = True else: @@ -761,6 +764,9 @@ def parse_args(args): parser.add_argument( '--ohos-cpu', type=str, choices=['x64', 'x86', 'arm64', 'arm'] ) + parser.add_argument( + '--ohos-api-int', type=int, choices=[10, 11], default=10 + ) parser.add_argument( '--fuchsia-cpu', type=str, choices=['x64', 'arm64'], default='x64' ) @@ -1047,10 +1053,10 @@ def main(argv): print('XX target_os:%s' % args.target_os) if args.target_os == 'ohos': - if args.ohos_cpu is not None: - args.linux_cpu = args.ohos_cpu - elif args.linux_cpu is not None : - args.ohos_cpu = args.linux_cpu + if args.ohos_cpu is not None: + args.linux_cpu = args.ohos_cpu + elif args.linux_cpu is not None : + args.ohos_cpu = args.linux_cpu if not args.web: if args.ide != '':