Skip to content

Commit

Permalink
Delete export functions and create result in c++
Browse files Browse the repository at this point in the history
  • Loading branch information
Hparty committed Dec 7, 2024
1 parent d9d8a78 commit afacadf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 57 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,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 -s EXPORTED_FUNCTIONS=['_malloc','_free'])
list(APPEND PAG_LINK_OPTIONS -O0 -g3 -sSAFE_HEAP=1 -Wno-limited-postlink-optimizations)
else ()
list(APPEND PAG_COMPILE_OPTIONS -Oz)
list(APPEND PAG_LINK_OPTIONS -Oz -s EXPORTED_FUNCTIONS=['_malloc','_free'])
list(APPEND PAG_LINK_OPTIONS -Oz)
endif ()
elseif (PAG_BUILD_SHARED)
add_library(pag SHARED ${PAG_FILES})
Expand Down
26 changes: 20 additions & 6 deletions src/platform/web/PAGWasmBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,26 @@ 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")
Expand Down
10 changes: 3 additions & 7 deletions web/cypress/integration/pag-surface.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,8 @@ describe('PAGSurface', () => {
pagPlayer.setProgress(0.5);
pagPlayer.setScaleMode(PAGTypes.PAGScaleMode.Stretch);
await pagPlayer.flush();
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,
// );
expect(pagSurface.readPixels(PAGTypes.ColorType.RGBA_8888, PAGTypes.AlphaType.Unpremultiplied).byteLength).to.be.eq(
360000,
);
});
});
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 './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(PAGModule.HEAPU8.buffer, 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
33 changes: 0 additions & 33 deletions web/src/utils/buffer.ts

This file was deleted.

0 comments on commit afacadf

Please sign in to comment.