From a09147a27941af7dcb9c576c231eea380083ff3f Mon Sep 17 00:00:00 2001 From: Hparty <420024556@qq.com> Date: Sat, 7 Dec 2024 22:26:21 +0800 Subject: [PATCH] Update tgfx to the latest version. (#2612) Co-authored-by: domrjchen --- CMakeLists.txt | 7 +++ DEPS | 4 +- autotest.sh | 2 +- src/platform/web/PAGWasmBindings.cpp | 84 ++++++++++++++++++++++------ test/baseline/version.json | 10 ++-- update_baseline.sh | 4 +- web/src/pag-file.ts | 6 +- web/src/pag-image.ts | 5 +- web/src/pag-surface.ts | 11 +--- web/src/types.ts | 4 +- win/Win32Demo/WinPAGDemo.vcxproj | 4 ++ 11 files changed, 95 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e989e4546e..4728494603 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,10 @@ option(PAG_USE_RTTR "Enable RTTR support" OFF) option(PAG_USE_HARFBUZZ "Enable HarfBuzz support" OFF) option(PAG_USE_C "Enable c API" OFF) +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + option(PAG_ENABLE_PROFILING "Enable Profiling" ON) +endif () + if (NOT MACOS AND NOT IOS AND NOT WEB) option(PAG_USE_FREETYPE "Allow use of embedded freetype library" ON) endif () @@ -99,6 +103,7 @@ message("PAG_USE_C: ${PAG_USE_C}") message("PAG_BUILD_SHARED: ${PAG_BUILD_SHARED}") message("PAG_BUILD_FRAMEWORK: ${PAG_BUILD_FRAMEWORK}") message("PAG_BUILD_TESTS: ${PAG_BUILD_TESTS}") +message("PAG_ENABLE_PROFILING: ${PAG_ENABLE_PROFILING}") if (NOT CMAKE_OSX_DEPLOYMENT_TARGET) if (DEPLOYMENT_TARGET) @@ -433,6 +438,7 @@ if (NOT HAS_CUSTOM_TGFX_DIR AND EXISTS ${TGFX_CACHE_DIR}) list(APPEND TGFX_OPTIONS "-DTGFX_USE_JPEG_ENCODE=${PAG_USE_JPEG_ENCODE}") list(APPEND TGFX_OPTIONS "-DTGFX_USE_WEBP_DECODE=${PAG_USE_WEBP_DECODE}") list(APPEND TGFX_OPTIONS "-DTGFX_USE_WEBP_ENCODE=${PAG_USE_WEBP_ENCODE}") + list(APPEND TGFX_OPTIONS "-DTGFX_ENABLE_PROFILING=${PAG_ENABLE_PROFILING}") if (PAG_USE_QT) list(APPEND TGFX_OPTIONS "-DCMAKE_PREFIX_PATH=\"${CMAKE_PREFIX_PATH}\"") endif () @@ -463,6 +469,7 @@ else () set(TGFX_USE_JPEG_ENCODE ${PAG_USE_JPEG_ENCODE}) set(TGFX_USE_WEBP_DECODE ${PAG_USE_WEBP_DECODE}) set(TGFX_USE_WEBP_ENCODE ${PAG_USE_WEBP_ENCODE}) + set(TGFX_ENABLE_PROFILING ${PAG_ENABLE_PROFILING}) set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) add_subdirectory(${TGFX_DIR} tgfx EXCLUDE_FROM_ALL) list(APPEND PAG_STATIC_LIBS $) diff --git a/DEPS b/DEPS index 2c60c7d4e5..c133543409 100644 --- a/DEPS +++ b/DEPS @@ -7,12 +7,12 @@ "common": [ { "url": "${PAG_GROUP}/vendor_tools.git", - "commit": "effcc0d22be03e55fab5c42ae82815ee7d76f74f", + "commit": "4e331972e2e828655b1901dccd5081cc995b32fc", "dir": "third_party/vendor_tools" }, { "url": "${PAG_GROUP}/tgfx.git", - "commit": "0ee494ab8246bb051e66b4681832ebcac18b8e39", + "commit": "b062b9a779e481e99f0e1da1f0951f0f5a9f431f", "dir": "third_party/tgfx" }, { diff --git a/autotest.sh b/autotest.sh index d9f42c33c0..2a9899feb6 100755 --- a/autotest.sh +++ b/autotest.sh @@ -25,7 +25,7 @@ make_dir result make_dir build cd build -cmake -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DPAG_USE_SWIFTSHADER=ON -DPAG_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug ../ +cmake -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DPAG_USE_SWIFTSHADER=ON -DPAG_BUILD_TESTS=ON -DPAG_ENABLE_PROFILING=OFF -DCMAKE_BUILD_TYPE=Debug ../ if test $? -eq 0; then echo "~~~~~~~~~~~~~~~~~~~CMakeLists OK~~~~~~~~~~~~~~~~~~" else diff --git a/src/platform/web/PAGWasmBindings.cpp b/src/platform/web/PAGWasmBindings.cpp index 4fcb431eec..a24fc2a732 100644 --- a/src/platform/web/PAGWasmBindings.cpp +++ b/src/platform/web/PAGWasmBindings.cpp @@ -31,6 +31,26 @@ using namespace emscripten; namespace pag { + +std::unique_ptr CopyDataFromUint8Array(const val& emscriptenData) { + if (!emscriptenData.as()) { + return nullptr; + } + auto length = emscriptenData["length"].as(); + if (length == 0) { + return nullptr; + } + auto buffer = ByteData::Make(length); + if (!buffer) { + return nullptr; + } + auto memory = val::module_property("HEAPU8")["buffer"]; + auto memoryView = + val::global("Uint8Array").new_(memory, reinterpret_cast(buffer->data()), length); + memoryView.call("set", emscriptenData); + return buffer; +} + bool PAGBindInit() { class_("_PAGLayer") .smart_ptr>("_PAGLayer") @@ -215,8 +235,13 @@ bool PAGBindInit() { class_>("_PAGFile") .smart_ptr>("_PAGFile") .class_function("_MaxSupportedTagLevel", PAGFile::MaxSupportedTagLevel) - .class_function("_Load", optional_override([](uintptr_t bytes, size_t length) { - return PAGFile::Load(reinterpret_cast(bytes), length); + .class_function("_Load", + optional_override([](const val& emscriptenData) -> std::shared_ptr { + auto data = CopyDataFromUint8Array(emscriptenData); + if (data == nullptr) { + return nullptr; + } + return PAGFile::Load(data->data(), data->length()); })) .function("_tagLevel", &PAGFile::tagLevel) .function("_numTexts", &PAGFile::numTexts) @@ -274,30 +299,55 @@ bool PAGBindInit() { .function("_updateSize", &PAGSurface::updateSize) .function("_clearAll", &PAGSurface::clearAll) .function("_freeCache", &PAGSurface::freeCache) - .function("_readPixels", - optional_override([](PAGSurface& pagSurface, int colorType, int alphaType, - uintptr_t dstPixels, size_t dstRowBytes) { - return pagSurface.readPixels(static_cast(colorType), - static_cast(alphaType), - reinterpret_cast(dstPixels), dstRowBytes); + .function("_readPixels", optional_override([](PAGSurface& pagSurface, int colorType, + int alphaType, size_t dstRowBytes) -> val { + auto dataSize = dstRowBytes * pagSurface.height(); + if (dataSize == 0) { + return val::null(); + } + std::unique_ptr uint8Array(new (std::nothrow) uint8_t[dataSize]); + if (uint8Array && pagSurface.readPixels(static_cast(colorType), + static_cast(alphaType), + uint8Array.get(), dstRowBytes)) { + auto memory = val::module_property("HEAPU8")["buffer"]; + auto memoryView = + val::global("Uint8Array") + .new_(memory, reinterpret_cast(uint8Array.get()), dataSize); + auto newArrayBuffer = val::global("ArrayBuffer").new_(dataSize); + auto newUint8Array = val::global("Uint8Array").new_(newArrayBuffer); + newUint8Array.call("set", memoryView); + return newUint8Array; + } + return val::null(); })); class_("_PAGImage") .smart_ptr>("_PAGImage") - .class_function("_FromBytes", optional_override([](uintptr_t bytes, size_t length) { - return PAGImage::FromBytes(reinterpret_cast(bytes), length); + .class_function("_FromBytes", + optional_override([](const val& emscriptenData) -> std::shared_ptr { + auto data = CopyDataFromUint8Array(emscriptenData); + if (data == nullptr) { + return nullptr; + } + return PAGImage::FromBytes(reinterpret_cast(data->data()), + data->length()); })) .class_function("_FromNativeImage", optional_override([](val nativeImage) { auto image = tgfx::Image::MakeFrom(nativeImage); return std::static_pointer_cast(StillImage::MakeFrom(image)); })) - .class_function("_FromPixels", - optional_override([](uintptr_t pixels, int width, int height, size_t rowBytes, - int colorType, int alphaType) { - return PAGImage::FromPixels(reinterpret_cast(pixels), width, height, - rowBytes, static_cast(colorType), - static_cast(alphaType)); - })) + .class_function( + "_FromPixels", + optional_override([](const val& pixels, int width, int height, size_t rowBytes, + int colorType, int alphaType) -> std::shared_ptr { + auto data = CopyDataFromUint8Array(pixels); + if (data == nullptr) { + return nullptr; + } + return PAGImage::FromPixels(reinterpret_cast(data->data()), width, height, + rowBytes, static_cast(colorType), + static_cast(alphaType)); + })) .class_function("_FromTexture", optional_override([](int textureID, int width, int height, bool flipY) { GLTextureInfo glInfo = {}; diff --git a/test/baseline/version.json b/test/baseline/version.json index a8f46bbdf4..7329de4c65 100644 --- a/test/baseline/version.json +++ b/test/baseline/version.json @@ -4859,10 +4859,10 @@ "0010": "b434229f", "0011": "b434229f", "0012": "b434229f", - "0013": "b434229f", + "0013": "5ad579ad", "0014": "b434229f", "0015": "b434229f", - "0016": "b434229f", + "0016": "5ad579ad", "0017": "b434229f", "0018": "b434229f", "0019": "b434229f", @@ -4873,9 +4873,9 @@ "0024": "b434229f", "0025": "b434229f", "0026": "b434229f", - "0027": "b434229f", + "0027": "5ad579ad", "0028": "b434229f", - "0029": "b434229f", + "0029": "5ad579ad", "0030": "b434229f", "0031": "b434229f", "0032": "b434229f", @@ -8451,7 +8451,7 @@ "NormalEmoji": "b434229f", "PositionAnimator": "b434229f", "RangeSelectorTriangleHighLow": "5c3b8bc5", - "SmallFontSizeScale": "b434229f", + "SmallFontSizeScale": "5ad579ad", "SmallFontSizeScale_LowResolution": "b434229f", "TextLayerScaleAnimationWithMipmap": "3bd38676", "TextPathBox": "7f7435d6f", diff --git a/update_baseline.sh b/update_baseline.sh index 6c0565c375..5edb7bfc53 100755 --- a/update_baseline.sh +++ b/update_baseline.sh @@ -43,9 +43,9 @@ echo $CMAKE_COMMAND if [[ $1 == "1" ]]; then - $CMAKE_COMMAND -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DPAG_USE_SWIFTSHADER=ON -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DCMAKE_BUILD_TYPE=Debug ../ + $CMAKE_COMMAND -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DPAG_USE_SWIFTSHADER=ON -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DPAG_ENABLE_PROFILING=OFF -DCMAKE_BUILD_TYPE=Debug ../ else - $CMAKE_COMMAND -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DCMAKE_BUILD_TYPE=Debug ../ + $CMAKE_COMMAND -DPAG_BUILD_TESTS=ON -DPAG_SKIP_BASELINE_CHECK=ON -DPAG_ENABLE_PROFILING=OFF -DCMAKE_BUILD_TYPE=Debug ../ fi $CMAKE_COMMAND --build . --target UpdateBaseline -- -j 12 diff --git a/web/src/pag-file.ts b/web/src/pag-file.ts index 2b6c8d8b0a..5c26247cbe 100644 --- a/web/src/pag-file.ts +++ b/web/src/pag-file.ts @@ -6,7 +6,6 @@ import { getLayerTypeName, layer2typeLayer, proxyVector } from './utils/type-uti import type { PAGImage } from './pag-image'; import { LayerType, PAGTimeStretchMode, TextDocument } from './types'; -import { writeBufferToWasm } from '@tgfx/utils/buffer'; @destroyVerify @wasmAwaitRewind @@ -28,9 +27,8 @@ export class PAGFile extends PAGComposition { */ public static loadFromBuffer(buffer: ArrayBuffer) { if (!buffer || !(buffer.byteLength > 0)) throw new Error('Initialize PAGFile data not be empty!'); - const { byteOffset, length, free } = writeBufferToWasm(PAGModule, buffer); - const wasmIns = PAGModule._PAGFile._Load(byteOffset, length); - free(); + const uint8Buffer = new Uint8Array(buffer); + const wasmIns = PAGModule._PAGFile._Load(uint8Buffer); if (!wasmIns) throw new Error('Load PAGFile fail!'); const pagFile = new PAGFile(wasmIns); return pagFile; diff --git a/web/src/pag-image.ts b/web/src/pag-image.ts index cf44bb6193..45812f2b7f 100644 --- a/web/src/pag-image.ts +++ b/web/src/pag-image.ts @@ -2,7 +2,6 @@ import { AlphaType, ColorType, PAGScaleMode } from './types'; import { wasmAwaitRewind, wasmAsyncMethod, destroyVerify } from './utils/decorators'; import { PAGModule } from './pag-module'; import { Matrix } from './core/matrix'; -import { writeBufferToWasm } from '@tgfx/utils/buffer'; @destroyVerify @wasmAwaitRewind @@ -49,9 +48,7 @@ export class PAGImage { alphaType: AlphaType, ): PAGImage { const rowBytes = width * (colorType === ColorType.ALPHA_8 ? 1 : 4); - const { byteOffset, free } = writeBufferToWasm(PAGModule, pixels); - const wasmIns = PAGModule._PAGImage._FromPixels(byteOffset, width, height, rowBytes, colorType, alphaType); - free(); + const wasmIns = PAGModule._PAGImage._FromPixels(pixels, width, height, rowBytes, colorType, alphaType); if (!wasmIns) throw new Error('Make PAGImage from pixels fail!'); return new PAGImage(wasmIns); } diff --git a/web/src/pag-surface.ts b/web/src/pag-surface.ts index a66c9f8b64..e7a51d57df 100644 --- a/web/src/pag-surface.ts +++ b/web/src/pag-surface.ts @@ -1,6 +1,5 @@ import { PAGModule } from './pag-module'; import { AlphaType, ColorType } from './types'; -import { readBufferFromWasm } from '@tgfx/utils/buffer'; import { destroyVerify, wasmAwaitRewind } from './utils/decorators'; @destroyVerify @@ -74,14 +73,8 @@ export class PAGSurface { */ public readPixels(colorType: ColorType, alphaType: AlphaType): Uint8Array | null { if (colorType === ColorType.Unknown) return null; - const rowBytes = this.width() * (colorType === ColorType.ALPHA_8 ? 1 : 4); - const length = rowBytes * this.height(); - const dataUint8Array = new Uint8Array(length); - const { data, free } = readBufferFromWasm(PAGModule, dataUint8Array, (dataPtr) => { - return this.wasmIns._readPixels(colorType, alphaType, dataPtr, rowBytes) as boolean; - }); - free(); - return data; + const rowBytes = this.wasmIns._width() * (colorType === ColorType.ALPHA_8 ? 1 : 4); + return this.wasmIns._readPixels(colorType, alphaType, rowBytes); } public destroy(): void { diff --git a/web/src/types.ts b/web/src/types.ts index c6c79120b7..9a57110c1a 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -31,13 +31,13 @@ declare global { export interface PAG extends EmscriptenModule { _PAGFile: { - _Load: (bytes: number, length: number) => any; + _Load: (buffer: Uint8Array) => any; _MaxSupportedTagLevel: () => number; }; _PAGImage: { _FromNativeImage: (source: TexImageSource | ArrayBufferImage) => any; _FromPixels: ( - pixels: number, + pixels: Uint8Array, width: number, height: number, rowBytes: number, diff --git a/win/Win32Demo/WinPAGDemo.vcxproj b/win/Win32Demo/WinPAGDemo.vcxproj index 4a1c9bf4cc..de285179c5 100644 --- a/win/Win32Demo/WinPAGDemo.vcxproj +++ b/win/Win32Demo/WinPAGDemo.vcxproj @@ -108,6 +108,7 @@ cd "$(SolutionDir).." call npm install -g depsync --silent call depsync +set CMAKE_MSVC_PATH=$(VCInstallDir) node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF @@ -142,6 +143,7 @@ node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF cd "$(SolutionDir).." call npm install -g depsync --silent call depsync +set CMAKE_MSVC_PATH=$(VCInstallDir) node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF @@ -166,6 +168,7 @@ node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF cd "$(SolutionDir).." call npm install -g depsync --silent call depsync +set CMAKE_MSVC_PATH=$(VCInstallDir) node .\build_pag -a x64 -DPAG_BUILD_SHARED=OFF @@ -207,6 +210,7 @@ node .\build_pag -a x64 -DPAG_BUILD_SHARED=OFF cd "$(SolutionDir).." call npm install -g depsync --silent call depsync +set CMAKE_MSVC_PATH=$(VCInstallDir) node .\build_pag -a x64 -DPAG_BUILD_SHARED=OFF