[adapters] 优化多线程特性文件结构,增加部分多线程音频系统代码,支持 Worker 子包特性(默认不开启),修复 Devtools 下强制不启用 Worker 问题

This commit is contained in:
SmallMain 2024-10-24 17:27:28 +08:00
parent 514e203483
commit fc9792c562
No known key found for this signature in database
15 changed files with 187 additions and 56 deletions

View File

@ -1,4 +1,4 @@
const cacheManager = CC_WORKER_ASSET_PIPELINE ? require('../cache-manager-proxy') : require('../cache-manager'); const cacheManager = CC_WORKER_ASSET_PIPELINE ? require('../worker/cache-manager-proxy') : require('../cache-manager');
const { fs, downloadFile, readText, readArrayBuffer, readJson, loadSubpackage, getUserDataPath, exists } = window.fsUtils; const { fs, downloadFile, readText, readArrayBuffer, readJson, loadSubpackage, getUserDataPath, exists } = window.fsUtils;
const REGEX = /^https?:\/\/.*/; const REGEX = /^https?:\/\/.*/;

View File

@ -8,7 +8,8 @@
"Audio": [ "Audio": [
"./common/engine/Audio.js", "./common/engine/Audio.js",
"./common/engine/AudioEngine.js", "./common/engine/AudioEngine.js",
"./platforms/alipay/wrapper/engine/AudioEngine.js" "./platforms/alipay/wrapper/engine/AudioEngine.js",
"./platforms/wechat/worker/audio.js"
], ],
"AudioSource": [], "AudioSource": [],
"Action": [], "Action": [],
@ -56,4 +57,4 @@
"WechatSubContext": [], "WechatSubContext": [],
"SwanSubContext": [], "SwanSubContext": [],
"TypeScript Polyfill": [] "TypeScript Polyfill": []
} }

View File

@ -1,8 +1,21 @@
const _global = GameGlobal; const _global = GameGlobal;
const adapter = _global.__globalAdapter = {}; const adapter = _global.__globalAdapter = {};
let inited = false;
let _callback = null;
let _wait_worker_t = 0;
Object.assign(adapter, { Object.assign(adapter, {
init () { init() {
const { init: initWorker } = require('./worker');
initWorker(() => {
inited = true;
if (CC_USE_WORKER && _callback) {
console.log("worker waiting time:", Date.now() - _wait_worker_t);
}
_callback && _callback();
_callback = null;
});
require('./wrapper/builtin'); require('./wrapper/builtin');
_global.DOMParser = require('../../common/xmldom/dom-parser').DOMParser; _global.DOMParser = require('../../common/xmldom/dom-parser').DOMParser;
require('./wrapper/unify'); require('./wrapper/unify');
@ -11,10 +24,22 @@ Object.assign(adapter, {
require('./wrapper/systemInfo'); require('./wrapper/systemInfo');
}, },
adaptEngine () { adaptEngine() {
require('./wrapper/error-reporter'); require('./wrapper/error-reporter');
require('../../common/engine'); require('../../common/engine');
require('./wrapper/engine'); require('./wrapper/engine');
require('./wrapper/sub-context-adapter'); require('./wrapper/sub-context-adapter');
}, },
onInited(callback) {
_wait_worker_t = Date.now();
if (inited) {
if (CC_USE_WORKER) {
console.log("worker waiting time:", Date.now() - _wait_worker_t);
}
callback();
} else {
_callback = callback;
}
},
}); });

View File

@ -1,6 +1,4 @@
const { init: initWorker, onInited: onWorkerInited } = require('./worker_adapter/index.js');
require('adapter-js-path'); require('adapter-js-path');
initWorker();
__globalAdapter.init(); __globalAdapter.init();
require('cocos2d-js-path'); require('cocos2d-js-path');
require('physics-js-path'); require('physics-js-path');
@ -19,8 +17,6 @@ if (cc.sys.platform !== cc.sys.WECHAT_GAME_SUB) {
cc.macro.CLEANUP_IMAGE_CACHE = true; cc.macro.CLEANUP_IMAGE_CACHE = true;
} }
const t = Date.now(); __globalAdapter.onInited(() => {
onWorkerInited(() => {
console.log("worker waiting time:", Date.now() - t);
window.boot(); window.boot();
}); });

View File

@ -0,0 +1,9 @@
var audio_worker = {
map: {},
create(callback, sn) {
this.map[sn] = worker.createInnerAudioContext();
},
};
module.exports = audio_worker;

View File

@ -4,3 +4,8 @@ if (globalThis.CC_WORKER_ASSET_PIPELINE) {
const cacheManager = require("./cache-manager-worker.js"); const cacheManager = require("./cache-manager-worker.js");
registerHandler("cacheManager", cacheManager); registerHandler("cacheManager", cacheManager);
} }
if (globalThis.CC_WORKER_AUDIO_SYSTEM) {
const audio = require("./audio-worker.js");
registerHandler("audio", audio);
}

View File

@ -144,7 +144,7 @@ function _initFromWorker(id, meta) {
wrappers, wrappers,
CC_WORKER_FS_SYNC, CC_WORKER_FS_SYNC,
CC_WORKER_ASSET_PIPELINE, CC_WORKER_ASSET_PIPELINE,
CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD, CC_WORKER_AUDIO_SYSTEM,
] = meta; ] = meta;
for (const wrapper of wrappers) { for (const wrapper of wrappers) {
@ -159,7 +159,7 @@ function _initFromWorker(id, meta) {
globalThis.CC_WORKER_FS_SYNC = CC_WORKER_FS_SYNC; globalThis.CC_WORKER_FS_SYNC = CC_WORKER_FS_SYNC;
globalThis.CC_WORKER_ASSET_PIPELINE = CC_WORKER_ASSET_PIPELINE; globalThis.CC_WORKER_ASSET_PIPELINE = CC_WORKER_ASSET_PIPELINE;
globalThis.CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD = CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD; globalThis.CC_WORKER_AUDIO_SYSTEM = CC_WORKER_AUDIO_SYSTEM;
_inited = true; _inited = true;
if (_initCallback) _initCallback(); if (_initCallback) _initCallback();

View File

@ -9,8 +9,8 @@ globalThis.CC_WORKER_DEBUG = false;
// 是否启用 Worker 使用同步版本的文件系统 API // 是否启用 Worker 使用同步版本的文件系统 API
globalThis.CC_WORKER_FS_SYNC = null; globalThis.CC_WORKER_FS_SYNC = null;
// 是否启用 Worker 驱动资源管线(下载、缓存) // 是否启用 Worker 驱动资源管线
globalThis.CC_WORKER_ASSET_PIPELINE = null; globalThis.CC_WORKER_ASSET_PIPELINE = null;
// 是否启用 Worker 驱动资源管线(加载) // 是否启用 Worker 驱动音频系统
globalThis.CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD = null; globalThis.CC_WORKER_AUDIO_SYSTEM = null;

View File

@ -0,0 +1,77 @@
let _id = 0;
class WorkerAudio {
id = ++_id;
get src() {
}
set src(str) {
}
get loop() {
}
set loop(v) {
}
_loop = false;
get volume() {
}
set volume(v) {
}
_volume = 1;
// 只读,从 Worker 单向同步值
duration = 0;
currentTime = 0;
paused = true;
constructor() {
}
get src() {
}
set src(clip) {
}
play() {
}
pause() {
}
seek() {
}
stop() {
}
destroy() {
}
}
var audioWorkerAdapter = {
on(id, callback) {
},
off(id, callback) {
},
};
globalThis.WorkerAudio = WorkerAudio;
module.exports = audioWorkerAdapter;

View File

@ -2,3 +2,8 @@ if (CC_WORKER_ASSET_PIPELINE) {
const assetManagerWorkerAdapter = require("./asset-manager.js"); const assetManagerWorkerAdapter = require("./asset-manager.js");
ipcMain.registerHandler("assetManager", assetManagerWorkerAdapter); ipcMain.registerHandler("assetManager", assetManagerWorkerAdapter);
} }
if (CC_WORKER_AUDIO_SYSTEM && cc._Audio) {
const audioWorkerAdapter = require("./audio.js");
ipcMain.registerHandler("audioAdapter", audioWorkerAdapter);
}

View File

@ -1,12 +1,9 @@
require("./macro.js"); require("./macro");
require("./ipc-main.js"); require("./ipc-main.js");
require("./handlers.js"); require("./handlers.js");
let inited = false;
let _callback = null;
module.exports = { module.exports = {
init() { init(callback) {
if (CC_USE_WORKER) { if (CC_USE_WORKER) {
var t = Date.now(); var t = Date.now();
ipcMain.init(() => { ipcMain.init(() => {
@ -15,25 +12,15 @@ module.exports = {
CC_USE_WORKER, CC_USE_WORKER,
CC_WORKER_DEBUG, CC_WORKER_DEBUG,
CC_WORKER_ASSET_PIPELINE, CC_WORKER_ASSET_PIPELINE,
CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD, CC_WORKER_AUDIO_SYSTEM,
CC_WORKER_SCHEDULER, CC_WORKER_SCHEDULER,
CC_WORKER_FS_SYNC, CC_WORKER_FS_SYNC,
CC_WORKER_SUB_PACKAGE,
}); });
inited = true; callback && callback();
_callback && _callback();
_callback = null;
}); });
} else { } else {
inited = true; callback && callback();
_callback && _callback();
_callback = null;
}
},
onInited(callback) {
if (inited) {
callback();
} else {
_callback = callback;
} }
}, },
}; };

View File

@ -16,25 +16,44 @@ const ipcMain = {
init(callback) { init(callback) {
this._initCallback = callback; this._initCallback = callback;
// NOTE { useExperimentalWorker: true } 会让有状态的 Worker 处理很复杂,暂时不使用 const loadSrc = (cb) => {
this.worker = wx.createWorker("workers/index.js"); if (CC_WORKER_SUB_PACKAGE) {
wx.preDownloadSubpackage({
this.worker.onMessage( packageType: "workers",
CC_WORKER_SCHEDULER success() {
? msgs => { cb();
for (let index = 0; index < msgs.length; index++) { },
const msg = msgs[index]; fail(res) {
this._handleWorkerMessage(msg); console.error("load worker fail:", res);
loadSrc(cb);
} }
} });
: this._handleWorkerMessage.bind(this) } else {
); cb();
}
};
if (CC_WORKER_SCHEDULER) { loadSrc(() => {
sendScheduler.init(this); // NOTE { useExperimentalWorker: true } 会让有状态的 Worker 处理很复杂,暂时不使用
} this.worker = wx.createWorker("workers/index.js");
this._init(); this.worker.onMessage(
CC_WORKER_SCHEDULER
? msgs => {
for (let index = 0; index < msgs.length; index++) {
const msg = msgs[index];
this._handleWorkerMessage(msg);
}
}
: this._handleWorkerMessage.bind(this)
);
if (CC_WORKER_SCHEDULER) {
sendScheduler.init(this);
}
this._init();
});
}, },
_handleWorkerMessage(msg) { _handleWorkerMessage(msg) {
@ -90,7 +109,7 @@ const ipcMain = {
_handlers, _handlers,
CC_WORKER_FS_SYNC, CC_WORKER_FS_SYNC,
CC_WORKER_ASSET_PIPELINE, CC_WORKER_ASSET_PIPELINE,
CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD, CC_WORKER_AUDIO_SYSTEM,
]); ]);
}, },

View File

@ -2,18 +2,21 @@ const isSubContext = wx.getOpenDataContext === undefined;
const sysinfo = wx.getSystemInfoSync(); const sysinfo = wx.getSystemInfoSync();
const platform = sysinfo.platform.toLowerCase(); const platform = sysinfo.platform.toLowerCase();
const isAndroid = platform === "android"; const isAndroid = platform === "android";
const isDevtools = platform === "devtools";
const sdkVersion = sysinfo.SDKVersion.split('.').map(Number); const sdkVersion = sysinfo.SDKVersion.split('.').map(Number);
// >= 2.20.2 // >= 2.20.2
const hasWorker = sdkVersion[0] > 2 || (sdkVersion[0] === 2 && (sdkVersion[1] > 20 || (sdkVersion[1] === 20 && sdkVersion[2] >= 2))); const hasWorker = sdkVersion[0] > 2 || (sdkVersion[0] === 2 && (sdkVersion[1] > 20 || (sdkVersion[1] === 20 && sdkVersion[2] >= 2)));
// >= 2.27.3
const useSubpackage = sdkVersion[0] > 2 || (sdkVersion[0] === 2 && (sdkVersion[1] > 27 || (sdkVersion[1] === 27 && sdkVersion[2] >= 3)));
// 是否启用 Worker 驱动资源管线(下载、缓存) // 是否启用 Worker 驱动资源管线
globalThis.CC_WORKER_ASSET_PIPELINE = false; globalThis.CC_WORKER_ASSET_PIPELINE = false;
// 是否启用 Worker 驱动资源管线(加载) // 是否启用 Worker 驱动音频系统
globalThis.CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD = false; globalThis.CC_WORKER_AUDIO_SYSTEM = false;
// NOTE 截止 2024.10.22,微信未修复 iOS、Windows、Mac 上仅文件系统 API 可以正常使用的问题 // NOTE 截止 2024.10.22,微信未修复 iOS、Windows、Mac 上仅文件系统 API 可以正常使用的问题
globalThis.CC_WORKER_ASSET_PIPELINE = isAndroid && globalThis.CC_WORKER_ASSET_PIPELINE; globalThis.CC_WORKER_ASSET_PIPELINE = (isAndroid || isDevtools) && globalThis.CC_WORKER_ASSET_PIPELINE;
// 是否启用 Worker // 是否启用 Worker
globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE) && hasWorker && !isSubContext; globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE) && hasWorker && !isSubContext;
@ -26,4 +29,8 @@ globalThis.CC_WORKER_SCHEDULER = true;
// 是否启用 Worker 使用同步版本的文件系统 API // 是否启用 Worker 使用同步版本的文件系统 API
// NOTE: IOS 不支持 async 文件系统 APIAndroid 不支持部分 sync 文件系统 API其余系统暂不确定 // NOTE: IOS 不支持 async 文件系统 APIAndroid 不支持部分 sync 文件系统 API其余系统暂不确定
globalThis.CC_WORKER_FS_SYNC = !isAndroid; globalThis.CC_WORKER_FS_SYNC = !isAndroid && !isDevtools;
// 是否启用 Worker 子包
// NOTE 截止 2024.10.22,部分安卓机型声明使用子包 Worker 会报 java.string 错误
globalThis.CC_WORKER_SUB_PACKAGE = false;