Skip to content

Commit

Permalink
Add wechat code
Browse files Browse the repository at this point in the history
  • Loading branch information
dalyzhou committed Nov 30, 2023
1 parent 1da3ea6 commit c676cd8
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
41 changes: 41 additions & 0 deletions web/src/wechat/array-buffer-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2023 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 interface FrameData {
id: number;
data: ArrayBuffer;
width: number;
height: number;
}

export class ArrayBufferImage {
public buffer: ArrayBuffer;
public width: number;
public height: number;
public constructor(buffer: ArrayBuffer, width: number, height: number) {
this.buffer = buffer;
this.width = width;
this.height = height;
}

public setFrameData(frameData: FrameData) {
this.buffer = frameData.data;
this.width = frameData.width;
this.height = frameData.height;
}
}
33 changes: 33 additions & 0 deletions web/src/wechat/binding.ts
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) 2023 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.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

import { TGFX } from '../types';
import { setTGFXModule } from '../tgfx-module';
import * as tgfx from './tgfx';
import { Matrix } from '../core/matrix';
import { ScalerContext } from '../core/scaler-context';
import { WebMask } from '../core/web-mask';

export const TGFXBind = (module: TGFX) => {
setTGFXModule(module);
module.module = module;
module.ScalerContext = ScalerContext;
module.WebMask = WebMask;
module.Matrix = Matrix;
module.tgfx = { ...tgfx };
};
44 changes: 44 additions & 0 deletions web/src/wechat/canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2023 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.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

import { CANVAS_POOL_MAX_SIZE } from '../constant';

import type { wx } from './interfaces';

declare const wx: wx;

const canvasPool = new Array<OffscreenCanvas>();

export const getCanvas2D = (width: number, height: number) => {
let canvas = canvasPool.pop() || createCanvas2D();
if (canvas !== null) {
canvas.width = width;
canvas.height = height;
}
return canvas;
};

export const releaseCanvas2D = (canvas: OffscreenCanvas) => {
if (canvasPool.length < CANVAS_POOL_MAX_SIZE) {
canvasPool.push(canvas);
}
};

const createCanvas2D = () => {
return wx.createOffscreenCanvas({ type: '2d' });
};
72 changes: 72 additions & 0 deletions web/src/wechat/tgfx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2023 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.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

import { getSourceSize, isAndroidMiniprogram } from '../tgfx';
import { ArrayBufferImage } from './array-buffer-image';
import { getCanvas2D, releaseCanvas2D } from './canvas';

import type { EmscriptenGL } from '../types';

type WxOffscreenCanvas = OffscreenCanvas & { isOffscreenCanvas: boolean };

export const uploadToTexture = (
GL: EmscriptenGL,
source: TexImageSource | ArrayBufferImage | WxOffscreenCanvas,
textureID: number,
alphaOnly: boolean,
) => {
const gl = GL.currentContext?.GLctx as WebGLRenderingContext;
gl.bindTexture(gl.TEXTURE_2D, GL.textures[textureID]);
if (!alphaOnly && ('isOffscreenCanvas' in source) && source.isOffscreenCanvas) {
const ctx = source.getContext('2d') as OffscreenCanvasRenderingContext2D;
const imgData = ctx.getImageData(0, 0, source.width, source.height);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
imgData.width,
imgData.height,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
new Uint8Array(imgData.data),
);
return;
}

gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
if (source instanceof ArrayBufferImage) {
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
source.width,
source.height,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
new Uint8Array(source.buffer),
);
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source as TexImageSource);
}

gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
};

export { getSourceSize, isAndroidMiniprogram, getCanvas2D as createCanvas2D, releaseCanvas2D as releaseNativeImage };

0 comments on commit c676cd8

Please sign in to comment.