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

fix small issues #6688

Merged
merged 14 commits into from May 21, 2020
2 changes: 1 addition & 1 deletion cocos2d/core/CCDirector.js
Expand Up @@ -580,7 +580,7 @@ cc.Director.prototype = {
return bundle.getSceneInfo(sceneName);
});
if (bundle) {
return bundle.preloadScene(sceneName, null, onProgress, onLoaded);
bundle.preloadScene(sceneName, null, onProgress, onLoaded);
}
else {
cc.errorID(1209, sceneName);
Expand Down
9 changes: 4 additions & 5 deletions cocos2d/core/asset-manager/CCAssetManager.js
Expand Up @@ -416,8 +416,8 @@ AssetManager.prototype = {
* Every custom parameter in `requests` will be tranfered to handler of `downloader` and `parser` as `options`.
* You can register you own handler downloader or parser to collect these custom parameters for some effect.
*
* Reserved Keyword: [`uuid`, `url`, `path`, `dir`, `scene`, `type`, `priority`, `preset`, `audioLoadMode`, `ext`, `bundle`, `onFileProgress`, `maxConcurrency`, `maxRequestsPerFrame`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

vs code 不识别中括号。。。

* `maxRetryCount`, `version`, `responseType`, `withCredentials`, `mimeType`, `timeout`, `header`, `reload`, `cacheAsset`, `cacheEnabled`],
* Reserved Keyword: `uuid`, `url`, `path`, `dir`, `scene`, `type`, `priority`, `preset`, `audioLoadMode`, `ext`, `bundle`, `onFileProgress`, `maxConcurrency`, `maxRequestsPerFrame`
* `maxRetryCount`, `version`, `responseType`, `withCredentials`, `mimeType`, `timeout`, `header`, `reload`, `cacheAsset`, `cacheEnabled`,
* Please DO NOT use these words as custom options!
*
* !#zh
Expand All @@ -426,8 +426,8 @@ AssetManager.prototype = {
* 依赖资源,则 `options` 中的参数会继续向依赖项中分发。request中的自定义参数都会以 `options` 形式传入加载流程中的 `downloader`, `parser` 的方法中, 你可以
* 扩展 `downloader`, `parser` 收集参数完成想实现的效果。
*
* 保留关键字: [`uuid`, `url`, `path`, `dir`, `scene`, `type`, `priority`, `preset`, `audioLoadMode`, `ext`, `bundle`, `onFileProgress`, `maxConcurrency`, `maxRequestsPerFrame`
* `maxRetryCount`, `version`, `responseType`, `withCredentials`, `mimeType`, `timeout`, `header`, `reload`, `cacheAsset`, `cacheEnabled`],
* 保留关键字: `uuid`, `url`, `path`, `dir`, `scene`, `type`, `priority`, `preset`, `audioLoadMode`, `ext`, `bundle`, `onFileProgress`, `maxConcurrency`, `maxRequestsPerFrame`
* `maxRetryCount`, `version`, `responseType`, `withCredentials`, `mimeType`, `timeout`, `header`, `reload`, `cacheAsset`, `cacheEnabled`,
* 请不要使用这些字段为自定义参数!
*
* @method loadAny
Expand Down Expand Up @@ -702,7 +702,6 @@ AssetManager.prototype = {
* 释放所有没有用到的资源。详细信息请参考 {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}}
*
* @method releaseUnusedAssets
* @private
*
* @typescript
* releaseUnusedAssets(): void
Expand Down
7 changes: 3 additions & 4 deletions cocos2d/core/asset-manager/bundle.js
Expand Up @@ -431,7 +431,7 @@ Bundle.prototype = {

options.preset = options.preset || 'scene';
options.bundle = this.name;
return cc.assetManager.loadAny({ 'scene': sceneName }, options, onProgress, function (err, sceneAsset) {
cc.assetManager.loadAny({ 'scene': sceneName }, options, onProgress, function (err, sceneAsset) {
if (err) {
cc.error(err.message, err.stack);
onComplete && onComplete(err);
Expand Down Expand Up @@ -548,7 +548,6 @@ Bundle.prototype = {
* 释放此包中的所有没有用到的资源。详细信息请参考 {{#crossLink "AssetManager/releaseAll:method"}}{{/crossLink}}
*
* @method releaseUnusedAssets
* @private
*
* @example
* // release all unused asset within bundle1
Expand All @@ -561,7 +560,7 @@ Bundle.prototype = {
var self = this;
assets.forEach(function (asset) {
let info = self.getAssetInfo(asset._uuid);
if (info && !info.redirect) {
if (info && info.path && !info.redirect) {
releaseManager.tryRelease(asset);
}
});
Expand All @@ -587,7 +586,7 @@ Bundle.prototype = {
var self = this;
assets.forEach(function (asset) {
let info = self.getAssetInfo(asset._uuid);
if (info && !info.redirect) {
if (info && info.path && !info.redirect) {
releaseManager.tryRelease(asset, true);
}
});
Expand Down
17 changes: 10 additions & 7 deletions cocos2d/core/asset-manager/depend-util.js
Expand Up @@ -151,19 +151,21 @@ var dependUtil = {
* parse(uuid: string, json: any): { deps?: string[], nativeDep?: any }
*/
parse (uuid, json) {
if (!CC_EDITOR && this._depends.has(uuid)) return this._depends.get(uuid);

var out = Object.create(null);
var type = json.__type__;

var out = null;
// scene or prefab
if (Array.isArray(json)) {

if (this._depends.has(uuid)) return this._depends.get(uuid)
out = {};
out.deps = cc.Asset._parseDepsFromJson(json);
out.asyncLoadAssets = json[0].asyncLoadAssets;
}
// get deps from json
else if (type) {
var ctor = js._getClassById(type);
else if (json.__type__) {

if (this._depends.has(uuid)) return this._depends.get(uuid);
out = {};
var ctor = js._getClassById(json.__type__);
out.preventPreloadNativeObject = ctor.preventPreloadNativeObject;
out.preventDeferredLoadDependents = ctor.preventDeferredLoadDependents;
out.deps = ctor._parseDepsFromJson(json);
Expand All @@ -173,6 +175,7 @@ var dependUtil = {
// get deps from an existing asset
else {
var asset = json;
out = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

out = { deps: [], preventPreloadNativeObject: xxx, preventDeferredLoadDependents: xxx };

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已改

out.deps = [];
out.preventPreloadNativeObject = asset.constructor.preventPreloadNativeObject;
out.preventDeferredLoadDependents = asset.constructor.preventDeferredLoadDependents;
Expand Down
12 changes: 5 additions & 7 deletions cocos2d/core/asset-manager/downloader.js
Expand Up @@ -102,28 +102,26 @@ var downloadBundle = function (url, options, onComplete) {
var version = options.version || downloader.bundleVers[bundleName];
var count = 0;
var config = version ? `${url}/config.${version}.json` : `${url}/config.json`;
let out = null;
let out = null, error = null;
downloadJson(config, options, function (err, response) {
if (err) {
onComplete(err);
return;
error = err;
}
out = response;
count++;
if (count === 2) {
onComplete(null, out);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

执行一次回调就行

onComplete(error, out);
}
});

var js = version ? `${url}/index.${version}.js` : `${url}/index.js`;
downloadScript(js, options, function (err) {
if (err) {
onComplete(err);
return;
error = err;
}
count++;
if (count === 2) {
onComplete(null, out);
onComplete(error, out);
}
});
};
Expand Down
1 change: 1 addition & 0 deletions cocos2d/core/asset-manager/load.js
Expand Up @@ -173,6 +173,7 @@ function loadDepends (task, asset, done, init) {
var { __asyncLoadAssets__, cacheAsset } = options;

var depends = [];
// add reference avoid being released during loading dependencies
asset.addRef && asset.addRef();
getDepends(uuid, asset, Object.create(null), depends, false, __asyncLoadAssets__, config);
task.dispatch('progress', ++progress.finish, progress.total += depends.length, item);
Expand Down
102 changes: 48 additions & 54 deletions cocos2d/core/asset-manager/pack-manager.js
Expand Up @@ -29,6 +29,11 @@ const { files } = require('./shared');

var _loading = new Cache();

function isLoading (val) {
return _loading.has(val.uuid);
}


/**
* @module cc.AssetManager
*/
Expand Down Expand Up @@ -196,64 +201,53 @@ var packManager = {
*/
load (item, options, onComplete) {
// if not in any package, download as uausl
if (item.isNative || !item.info || !item.info.packs) {
downloader.download(item.id, item.url, item.ext, item.options, onComplete);
}
else {
if (files.has(item.id)) {
onComplete(null, files.get(item.id));
if (item.isNative || !item.info || !item.info.packs) return downloader.download(item.id, item.url, item.ext, item.options, onComplete);

if (files.has(item.id)) return onComplete(null, files.get(item.id));

var packs = item.info.packs;

// find a loading package
var pack = packs.find(isLoading);

if (pack) return _loading.get(pack.uuid).push({ onComplete, id: item.id });

// download a new package
pack = packs[0];
_loading.add(pack.uuid, [{ onComplete, id: item.id }]);

let url = cc.assetManager._transform(pack.uuid, {ext: pack.ext, bundle: item.config.name});

downloader.download(pack.uuid, url, pack.ext, item.options, function (err, data) {
files.remove(pack.uuid);
if (err) {
cc.error(err.message, err.stack);
}
else {
var packs = item.info.packs;
// find a loading package
var pack = packs.find(function (val) {
return _loading.has(val.uuid);
});

if (pack) {
_loading.get(pack.uuid).push({ onComplete, id: item.id });
// unpack package
packManager.unpack(pack.packs, data, pack.ext, item.options, function (err, result) {
if (!err) {
for (var id in result) {
files.add(id, result[id]);
}
}
else {
// download a new package
pack = packs[0];
_loading.add(pack.uuid, [{ onComplete, id: item.id }]);

var url = cc.assetManager._transform(pack.uuid, {ext: pack.ext, bundle: item.config.name});
var callbacks = _loading.remove(pack.uuid);
for (var i = 0, l = callbacks.length; i < l; i++) {
var cb = callbacks[i];
if (err) {
cb.onComplete(err);
continue;
}

downloader.download(pack.uuid, url, pack.ext, item.options, function (err, data) {
files.remove(pack.uuid);
if (err) {
cc.error(err.message, err.stack);
}
// unpack package
packManager.unpack(pack.packs, data, pack.ext, item.options, function (err, result) {
if (!err) {
for (var id in result) {
files.add(id, result[id]);
}
}
var callbacks = _loading.remove(pack.uuid);
for (var i = 0, l = callbacks.length; i < l; i++) {
var cb = callbacks[i];
if (err) {
cb.onComplete(err);
}
else {
var data = result[cb.id];
if (!data) {
cb.onComplete(new Error('can not retrieve data from package'));
}
else {
cb.onComplete(null, data);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

提高下可读性,全是大括号太烦

}
}
});
});
var data = result[cb.id];
if (!data) {
cb.onComplete(new Error('can not retrieve data from package'));
}
else {
cb.onComplete(null, data);
}
}
}
}

});
});
}
};

Expand Down
20 changes: 2 additions & 18 deletions cocos2d/core/asset-manager/releaseManager.js
Expand Up @@ -198,25 +198,9 @@ var releaseManager = {
_free (asset, force) {
_toDelete.remove(asset._uuid);

if (!force) {

if (!CC_NATIVERENDERER) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个判断不对,_current.textureUnits 压根就不是本帧所有贴图,只是最后一个 drawcall 的贴图,没有地方能统计到本帧的,所以还是不判断了

var glTexture = null;
if (asset instanceof cc.Texture2D) {
glTexture = asset._texture;
}

if (glTexture && glTexture._glID != -1) {
var textureUnits = cc.renderer.device._current.textureUnits;
for (var i = 0; i < textureUnits.length; i++) {
if (glTexture === textureUnits[i]) {
console.error(`this texture ${asset._uuid} is being used`);
return;
}
}
}
}
if (!cc.isValid(asset, true)) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

如果资源已经释放过了,不应该再释放一次


if (!force) {
if (asset.refCount > 0) {
if (checkCircularReference(asset) > 0) return;
}
Expand Down
14 changes: 11 additions & 3 deletions cocos2d/core/platform/CCSys.js
Expand Up @@ -693,7 +693,7 @@ function initSys () {
height: 0
};
sys.capabilities = {
'imageBitmap': typeof createImageBitmap !== 'undefined' && typeof Blob !== 'undefined'
'imageBitmap': false
};
sys.__audioSupport = {};
}
Expand Down Expand Up @@ -766,7 +766,7 @@ function initSys () {
capabilities["touches"] = false;
}

capabilities['imageBitmap'] = typeof createImageBitmap !== 'undefined' && typeof Blob !== 'undefined';
capabilities['imageBitmap'] = false;

sys.__audioSupport = {
ONLY_ONE: false,
Expand Down Expand Up @@ -992,8 +992,16 @@ function initSys () {
"canvas": _supportCanvas,
"opengl": _supportWebGL,
"webp": _supportWebp,
'imageBitmap': typeof createImageBitmap !== 'undefined' && typeof Blob !== 'undefined',
'imageBitmap': false,
};

if (typeof createImageBitmap !== 'undefined' && typeof Blob !== 'undefined') {
_tmpCanvas1.width = _tmpCanvas1.height = 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

imageBitmap 在某些浏览器上,可能不支持 option 参数,所以得实际调用一次来判断是否真的支持

createImageBitmap(_tmpCanvas1, {}).then(imageBitmap => {
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么要使用 ImageBitmap ?
HTMLImageElement 不香么?

Copy link
Contributor Author

@holycanvas holycanvas Jul 3, 2020

Choose a reason for hiding this comment

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

HTMLImageElement 在上传GPU的时候,浏览器会执行一个 decode 的操作,这个操作需要时的时间跟图像的分辨率大小成正比,而且有个严重的问题是,这个操作是同步的,所以如果图片很多的时候,会非常卡顿,而且因为是同步的,所以图片只能一张接着一张的decode,所以会很慢。而 imageBitmap 在浏览器上是在多线程中解码的,所以可以并行,会更快,而且因为少了同步的操作,卡顿也会变少。不想使用的话,可以用 cc.macro.ALLOW_IMAGE_BITMAP 来关闭使用

capabilities.imageBitmap = true;
imageBitmap.close && imageBitmap.close();
}).catch(err => {});
}
if (docEle['ontouchstart'] !== undefined || doc['ontouchstart'] !== undefined || nav.msPointerEnabled)
capabilities["touches"] = true;
if (docEle['onmouseup'] !== undefined)
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/platform/CCView.js
Expand Up @@ -465,7 +465,7 @@ cc.js.mixin(View.prototype, {
if(cc.game.renderType === cc.game.RENDER_TYPE_WEBGL) {
var cache = cc.assetManager.assets;
cache.forEach(function (asset) {
if (asset instanceof cc.Texture2Dx) {
if (asset instanceof cc.Texture2D) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

typo

var Filter = cc.Texture2D.Filter;
if (enabled) {
asset.setFilters(Filter.LINEAR, Filter.LINEAR);
Expand Down