Skip to content

Commit

Permalink
Update tgfx to the latest version. (#2612)
Browse files Browse the repository at this point in the history
Co-authored-by: domrjchen <[email protected]>
  • Loading branch information
Hparty and domchen authored Dec 7, 2024
1 parent 6af0aea commit a09147a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 46 deletions.
7 changes: 7 additions & 0 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
4 changes: 2 additions & 2 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
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
84 changes: 67 additions & 17 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) {
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 @@ -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>(colorType),
static_cast<AlphaType>(alphaType),
reinterpret_cast<void*>(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<uint8_t[]> uint8Array(new (std::nothrow) uint8_t[dataSize]);
if (uint8Array && pagSurface.readPixels(static_cast<ColorType>(colorType),
static_cast<AlphaType>(alphaType),
uint8Array.get(), dstRowBytes)) {
auto memory = val::module_property("HEAPU8")["buffer"];
auto memoryView =
val::global("Uint8Array")
.new_(memory, reinterpret_cast<uintptr_t>(uint8Array.get()), dataSize);
auto newArrayBuffer = val::global("ArrayBuffer").new_(dataSize);
auto newUint8Array = val::global("Uint8Array").new_(newArrayBuffer);
newUint8Array.call<void>("set", memoryView);
return newUint8Array;
}
return val::null();
}));

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
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
11 changes: 2 additions & 9 deletions web/src/pag-surface.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
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
4 changes: 4 additions & 0 deletions win/Win32Demo/WinPAGDemo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<Command>cd "$(SolutionDir).."
call npm install -g depsync --silent
call depsync
set CMAKE_MSVC_PATH=$(VCInstallDir)
node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -142,6 +143,7 @@ node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF</Command>
<Command>cd "$(SolutionDir).."
call npm install -g depsync --silent
call depsync
set CMAKE_MSVC_PATH=$(VCInstallDir)
node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand All @@ -166,6 +168,7 @@ node .\build_pag -a x86 -DPAG_BUILD_SHARED=OFF</Command>
<Command>cd "$(SolutionDir).."
call npm install -g depsync --silent
call depsync
set CMAKE_MSVC_PATH=$(VCInstallDir)
node .\build_pag -a x64 -DPAG_BUILD_SHARED=OFF</Command>
</PreBuildEvent>
<ProjectReference>
Expand Down Expand Up @@ -207,6 +210,7 @@ node .\build_pag -a x64 -DPAG_BUILD_SHARED=OFF</Command>
<Command>cd "$(SolutionDir).."
call npm install -g depsync --silent
call depsync
set CMAKE_MSVC_PATH=$(VCInstallDir)
node .\build_pag -a x64 -DPAG_BUILD_SHARED=OFF</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand Down

0 comments on commit a09147a

Please sign in to comment.