TianZerL / Anime4KCPP

A high performance anime upscaler
GNU General Public License v3.0
1.82k stars 139 forks source link

Android 端无法启用 GPU 模式 #112

Closed liguobing closed 1 year ago

liguobing commented 1 year ago

您好,我 使用 https://github.com/TianZerL/Anime4KCPP/issues/108#issuecomment-1801871654 中您提供的 opencl SDK,编译成功,但提示不支持 GPU 模式,我定位到代码是: ACOpenCL.cpp 中的 cl::Platform::get(&platforms); 这句会抛出异常。

我又尝试了将手机中的 libOpenCL.so 文件导出替换,开启 GPU 模式时会提示如下错误:

java.lang.UnsatisfiedLinkError: dlopen failed: library "xxx.so" not found: needed by /data/app/~~ATBdI8nnpXGX2afxomHLlg==/github.tianzerl.anime4kcpp-VUgdHRxAeIR4125jBBF98Q==/base.apk!/lib/arm64-v8a/libvndksupport.so in namespace classloader-namespace

通过搜索,应该是调用了系统私有 NDK 所致,于是将手机中的 so 文件导出,在 CMkeLists 中加入

add_library(ld-android SHARED IMPORTED )
set_target_properties(ld-android PROPERTIES IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/src/main/jniLibs/arm64-v8a/ld-android.so")

以此方式加入了 libcutils.so libvndksupport.so libc++.so libbase.so libdl_android.so ld-android.so

但这个时候再开启 GPU,提示如下错误:

[VRI[MainActivity]#0](f:0,a:2) Faking releaseBufferCallback from transactionCompleteCallback
Cmdline: github.tianzerl.anime4kcpp
pid: 20282, tid: 20282, name: zerl.anime4kcpp  >>> github.tianzerl.anime4kcpp <<<
      #00 pc 0000000000001004  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!ld-android.so (__loader_dlopen+4) (BuildId: 299bce43c3a1f1a20cc755f9ae32a56c)
      #01 pc 00000000000010e0  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!libdl_android.so (android_get_exported_namespace+12) (BuildId: a506d7cbefe663d77d99d39034bddf74)
      #02 pc 0000000000001194  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!libvndksupport.so (android_load_sphal_library+308) (BuildId: fa178c0474c2bbfb9380bc25fc0547ec)
      #03 pc 00000000000221c0  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!libOpenCL.so (clGetPlatformIDs+48) (BuildId: 9813a971cf4f8d54e568f1b386b2e1c2)
      #04 pc 00000000000ca1b0  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!libAnime4KCPPCore.so (cl::Platform::get(std::__ndk1::vector<cl::Platform, std::__ndk1::allocator<cl::Platform> >*)+84) (BuildId: 780e18a7034bac956ac8d2cecc521fb0bbef05b1)
      #05 pc 00000000000cad78  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!libAnime4KCPPCore.so (Anime4KCPP::OpenCL::checkGPUSupport(int, int)+168) (BuildId: 780e18a7034bac956ac8d2cecc521fb0bbef05b1)
      #06 pc 00000000000b3788  /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!libAnime4KCPPCore.so (Java_github_tianzerl_anime4kcpp_wrapper_Anime4KGPU_checkGPUSupportAnime4KGPU+60) (BuildId: 780e18a7034bac956ac8d2cecc521fb0bbef05b1)
      #14 pc 0000000000000c10  [anon:dalvik-classes5.dex extracted in memory from /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!classes5.dex] (github.tianzerl.anime4kcpp.wrapper.Anime4KGPU.checkGPUSupport+0)
      #19 pc 0000000000003d22  [anon:dalvik-classes4.dex extracted in memory from /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!classes4.dex] (github.tianzerl.anime4kcpp.MainActivity.lambda$setSwitches$5$github-tianzerl-anime4kcpp-MainActivity+34)
      #21 pc 0000000000001d08  [anon:dalvik-classes4.dex extracted in memory from /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk!classes4.dex] (github.tianzerl.anime4kcpp.MainActivity$$ExternalSyntheticLambda1.onCheckedChanged+4)
      #25 pc 00000000000fe344  [anon:dalvik-classes.dex extracted in memory from /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk] (androidx.appcompat.widget.SwitchCompat.setChecked+0)
      #27 pc 00000000000fe8cc  [anon:dalvik-classes.dex extracted in memory from /data/app/~~RweT7JyRJm8P-56cimkbjw==/github.tianzerl.anime4kcpp-cQON6PPyIEtsyVC54N-E5g==/base.apk] (androidx.appcompat.widget.SwitchCompat.toggle+12)
[VRI[MainActivity]#0](f:0,a:2) Faking releaseBufferCallback from transactionCompleteCallback

但是使用您再 release 中提供的 apk,是可以开启 GPU 模式的,所以您提供的 OpenCL SDK,对应的是最新的 Anime4KCPP_Android 吗?

TianZerL commented 1 year ago

release编译使用的so库就是我发的,但是头文件应该是不一样的,release的opencl部分代码是纯C的,而Anime4KCPP_Android则用了OpenCL官方的cpp封装库。

还是so库的兼容性问题,sdk的so库我换过,然后忘记了😓。 release的so库应该是zuk z2里扒下来的,我发的sdk的so库是从一款联想的855手机里扒下来的。我用release的代码链接现在的so库一样一堆问题。

但是不知道是不是因为z2的so库版本太老(骁龙820),在855上测试GPU跑OpenCL性能比使用Eigen3的CPU慢得多(也可能是手机的GPU带宽瓶颈)。

zukz2libOpenCL_arm64-v8a.zip(不是很确定这个so库到底是不是arm64的,AS会提示不兼容但是又可以链接上并正常工作) EDIT:非常离谱且无语,我发现现在用AS的bug在编译链接上armv7的OpenCL库就可以工作,具体就是先用arm64的库编译一遍然后然后替换so库再点编译就能链接上armv7的库,而且一切正常???估计那个z2的so库也是armv7的,怀疑是安卓版本或者编译目标SDK版本的问题。

liguobing commented 1 year ago

感谢您的回复,这几天我试了各种操作,都不行,各种出错,头发都快薅秃了。

我按照您的操作试了一下,好像还是不行,您看下是不是我的理解有问题

我的操作步骤如下:

还是使用 opencl.zip 中的 so 库和头文件

1.修改 build.gradle,将 include 'arm64-v8a','armeabi-v7a' 中的 'armeabi-v7a' 删除,这样只保留了编译 arm64 2.在 AndroidStudio 中 rebuild 这里 rebuild 成功 3.替换 lib 中 'arm64-v8a' 目录下的 libOpenCL.so 为 'armeabi-v7a' 下的 so 文件 4.rebuild 这里 rebuild 不成功,报错如下

clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[2/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACCuda.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[3/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUAnime4K09.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[4/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACNCNN.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[5/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUCNNProcessor.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[6/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACCreator.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[7/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCPPWrapper.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[8/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUACNet.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[9/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/AC.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[10/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUACNetProcessor.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[11/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CudaAnime4K09.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[12/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/NCNNACNet.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[13/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/OpenCLACNet.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[14/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CudaACNet.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[15/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/FilterProcessor.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[16/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/VideoProcessor.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[17/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/OpenCLAnime4K09.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[18/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/VideoIO.cpp.o
clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
[19/19] Linking CXX shared library E:\Work_Space\Anime4KCPP_Android\app\build\intermediates\cxx\Debug\5av2r2w1\obj\arm64-v8a\libAnime4KCPPCore.so
FAILED: E:/Work_Space/Anime4KCPP_Android/app/build/intermediates/cxx/Debug/5av2r2w1/obj/arm64-v8a/libAnime4KCPPCore.so 

cmd.exe /C "cd . && D:\AndroidSDK\ndk\21.3.6528147\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android24 --gcc-toolchain=D:/AndroidSDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/AndroidSDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -std=c++17 -frtti -fexceptions -O3 -static-openmp -fopenmp -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libAnime4KCPPCore.so -o E:\Work_Space\Anime4KCPP_Android\app\build\intermediates\cxx\Debug\5av2r2w1\obj\arm64-v8a\libAnime4KCPPCore.so @CMakeFiles\Anime4KCPPCore.rsp  && cd ."

clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Execution failed for task ':app:buildCMakeDebug[arm64-v8a]'.
> com.android.ide.common.process.ProcessException: ninja: Entering directory `E:\Work_Space\Anime4KCPP_Android\app\.cxx\Debug\5av2r2w1\arm64-v8a'
  [1/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACOpenCL.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [2/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACCuda.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [3/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUAnime4K09.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [4/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACNCNN.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [5/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUCNNProcessor.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [6/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/ACCreator.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [7/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCPPWrapper.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [8/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUACNet.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [9/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/AC.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [10/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CPUACNetProcessor.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [11/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CudaAnime4K09.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [12/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/NCNNACNet.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [13/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/OpenCLACNet.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [14/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/CudaACNet.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [15/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/FilterProcessor.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [16/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/VideoProcessor.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [17/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/OpenCLAnime4K09.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [18/19] Building CXX object CMakeFiles/Anime4KCPPCore.dir/Anime4KCore/src/VideoIO.cpp.o
  clang++: warning: argument unused during compilation: '-static-openmp' [-Wunused-command-line-argument]
  [19/19] Linking CXX shared library E:\Work_Space\Anime4KCPP_Android\app\build\intermediates\cxx\Debug\5av2r2w1\obj\arm64-v8a\libAnime4KCPPCore.so
  FAILED: E:/Work_Space/Anime4KCPP_Android/app/build/intermediates/cxx/Debug/5av2r2w1/obj/arm64-v8a/libAnime4KCPPCore.so 
  cmd.exe /C "cd . && D:\AndroidSDK\ndk\21.3.6528147\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android24 --gcc-toolchain=D:/AndroidSDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/AndroidSDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -std=c++17 -frtti -fexceptions -O3 -static-openmp -fopenmp -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libAnime4KCPPCore.so -o E:\Work_Space\Anime4KCPP_Android\app\build\intermediates\cxx\Debug\5av2r2w1\obj\arm64-v8a\libAnime4KCPPCore.so @CMakeFiles\Anime4KCPPCore.rsp  && cd ."
  E:/Libs/Android/OpenCL/lib/arm64-v8a/libOpenCL.so: error adding symbols: File in wrong format
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.
TianZerL commented 1 year ago

我的操作是这样的,没有修改配置文件,而是插上手机开启usb调试,然后直接点run,此时应该会编译并在手机上运行,这个时候点all stop,然后去换掉so库,再点run,会发现此时依然可以run成功。但是过一段时间再run又会提示so库不兼容。就很离谱。

如果还不成功的话,不妨直接替换编译出的apk的lib文件夹里打包的so库试一下。

然后可以尝试降低sdk版本到30,以前是没有这个奇怪的问题的,opencl相关代码也没有什么修改。我看谷歌在sdk31加入了对load native库的限制。

我按你的方法试了一下,也可以编译成功。先删除armv7然后点make,此时成功,然后替换so库再make依然成功。

liguobing commented 1 year ago

我不知道该怎么形容了,替换之后成功开启了 GPU 啥也不说了,我给你磕一个吧