Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tgfx to the latest version. #2612

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ()
Expand Down Expand Up @@ -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 $<TARGET_FILE:tgfx>)
Expand Down Expand Up @@ -493,10 +500,10 @@ if (WEB)
-sMODULARIZE=1 -sENVIRONMENT='web,worker' -sEXPORT_ES6=1 -sUSE_ES6_IMPORT_META=0)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND PAG_COMPILE_OPTIONS -O0 -g3)
list(APPEND PAG_LINK_OPTIONS -O0 -g3 -sSAFE_HEAP=1 -Wno-limited-postlink-optimizations)
list(APPEND PAG_LINK_OPTIONS -O0 -g3 -sSAFE_HEAP=1 -Wno-limited-postlink-optimizations -s EXPORTED_FUNCTIONS=['_malloc','_free'])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要增加这个接口导出,后面的buffer.ts也不需要

else ()
list(APPEND PAG_COMPILE_OPTIONS -Oz)
list(APPEND PAG_LINK_OPTIONS -Oz)
list(APPEND PAG_LINK_OPTIONS -Oz -s EXPORTED_FUNCTIONS=['_malloc','_free'])
endif ()
elseif (PAG_BUILD_SHARED)
add_library(pag SHARED ${PAG_FILES})
Expand Down
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"url": "${PAG_GROUP}/tgfx.git",
"commit": "0ee494ab8246bb051e66b4681832ebcac18b8e39",
"commit": "bc72a3730fe0733391dbaf5ce8ec139a8073c1a6",
"dir": "third_party/tgfx"
},
{
Expand Down
2 changes: 1 addition & 1 deletion autotest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 47 additions & 11 deletions src/platform/web/PAGWasmBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
using namespace emscripten;

namespace pag {

std::unique_ptr<ByteData> CopyDataFromUint8Array(const val& emscriptenData) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从js拷贝内存到c++都统一用这个方法就行,不需要后面的buffer.ts

if (!emscriptenData.as<bool>()) {
return nullptr;
}
auto length = emscriptenData["length"].as<size_t>();
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<uintptr_t>(buffer->data()), length);
memoryView.call<void>("set", emscriptenData);
return buffer;
}

bool PAGBindInit() {
class_<PAGLayer>("_PAGLayer")
.smart_ptr<std::shared_ptr<PAGLayer>>("_PAGLayer")
Expand Down Expand Up @@ -215,8 +235,13 @@ bool PAGBindInit() {
class_<PAGFile, base<PAGComposition>>("_PAGFile")
.smart_ptr<std::shared_ptr<PAGFile>>("_PAGFile")
.class_function("_MaxSupportedTagLevel", PAGFile::MaxSupportedTagLevel)
.class_function("_Load", optional_override([](uintptr_t bytes, size_t length) {
return PAGFile::Load(reinterpret_cast<void*>(bytes), length);
.class_function("_Load",
optional_override([](const val& emscriptenData) -> std::shared_ptr<PAGFile> {
auto data = CopyDataFromUint8Array(emscriptenData);
if (data == nullptr) {
return nullptr;
}
return PAGFile::Load(data->data(), data->length());
}))
.function("_tagLevel", &PAGFile::tagLevel)
.function("_numTexts", &PAGFile::numTexts)
Expand Down Expand Up @@ -284,20 +309,31 @@ bool PAGBindInit() {

class_<PAGImage>("_PAGImage")
.smart_ptr<std::shared_ptr<PAGImage>>("_PAGImage")
.class_function("_FromBytes", optional_override([](uintptr_t bytes, size_t length) {
return PAGImage::FromBytes(reinterpret_cast<void*>(bytes), length);
.class_function("_FromBytes",
optional_override([](const val& emscriptenData) -> std::shared_ptr<PAGImage> {
auto data = CopyDataFromUint8Array(emscriptenData);
if (data == nullptr) {
return nullptr;
}
return PAGImage::FromBytes(reinterpret_cast<void*>(data->data()),
data->length());
}))
.class_function("_FromNativeImage", optional_override([](val nativeImage) {
auto image = tgfx::Image::MakeFrom(nativeImage);
return std::static_pointer_cast<PAGImage>(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<void*>(pixels), width, height,
rowBytes, static_cast<ColorType>(colorType),
static_cast<AlphaType>(alphaType));
}))
.class_function(
"_FromPixels",
optional_override([](const val& pixels, int width, int height, size_t rowBytes,
int colorType, int alphaType) -> std::shared_ptr<PAGImage> {
auto data = CopyDataFromUint8Array(pixels);
if (data == nullptr) {
return nullptr;
}
return PAGImage::FromPixels(reinterpret_cast<void*>(data->data()), width, height,
rowBytes, static_cast<ColorType>(colorType),
static_cast<AlphaType>(alphaType));
}))
.class_function("_FromTexture",
optional_override([](int textureID, int width, int height, bool flipY) {
GLTextureInfo glInfo = {};
Expand Down
10 changes: 5 additions & 5 deletions test/baseline/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -8451,7 +8451,7 @@
"NormalEmoji": "b434229f",
"PositionAnimator": "b434229f",
"RangeSelectorTriangleHighLow": "5c3b8bc5",
"SmallFontSizeScale": "b434229f",
"SmallFontSizeScale": "5ad579ad",
"SmallFontSizeScale_LowResolution": "b434229f",
"TextLayerScaleAnimationWithMipmap": "3bd38676",
"TextPathBox": "7f7435d6f",
Expand Down
4 changes: 2 additions & 2 deletions update_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions web/cypress/integration/pag-surface.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ describe('PAGSurface', () => {
pagPlayer.setProgress(0.5);
pagPlayer.setScaleMode(PAGTypes.PAGScaleMode.Stretch);
await pagPlayer.flush();
expect(pagSurface.readPixels(PAGTypes.ColorType.RGBA_8888, PAGTypes.AlphaType.Unpremultiplied).byteLength).to.be.eq(
360000,
);
console.log("readPixels");
const tmp = pagSurface.readPixels(PAGTypes.ColorType.RGBA_8888, PAGTypes.AlphaType.Unpremultiplied);
console.log(tmp);
expect(tmp.byteLength).to.be.eq(360000);
// expect(pagSurface.readPixels(PAGTypes.ColorType.RGBA_8888, PAGTypes.AlphaType.Unpremultiplied).byteLength).to.be.eq(
// 360000,
// );
});
});
6 changes: 2 additions & 4 deletions web/src/pag-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions web/src/pag-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions web/src/pag-surface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PAGModule } from './pag-module';
import { AlphaType, ColorType } from './types';
import { readBufferFromWasm } from '@tgfx/utils/buffer';
import { readBufferFromWasm } from './utils/buffer';
import { destroyVerify, wasmAwaitRewind } from './utils/decorators';

@destroyVerify
Expand Down Expand Up @@ -76,7 +76,7 @@ export class PAGSurface {
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 dataUint8Array = new Uint8Array(PAGModule.HEAPU8.buffer, length);
const { data, free } = readBufferFromWasm(PAGModule, dataUint8Array, (dataPtr) => {
return this.wasmIns._readPixels(colorType, alphaType, dataPtr, rowBytes) as boolean;
});
Expand Down
4 changes: 2 additions & 2 deletions web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions web/src/utils/buffer.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考一下tgfx的写法,已经不需要这个类了。这个类会多拷贝一次。

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

export const readBufferFromWasm = (
module: EmscriptenModule,
data: ArrayLike<number> | ArrayBufferLike,
handle: (byteOffset: number, length: number) => boolean,
) => {
const uint8Array = new Uint8Array(data);
const dataPtr = module._malloc(uint8Array.byteLength);
if (!handle(dataPtr, uint8Array.byteLength)) return {
data: null,
free: () => module._free(dataPtr)
};
const dataOnHeap = new Uint8Array(module.HEAPU8.buffer, dataPtr, uint8Array.byteLength);
uint8Array.set(dataOnHeap);
return {data: uint8Array, free: () => module._free(dataPtr)};
};
Loading