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

Support device lost and automatic restore #1374

Merged
merged 134 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 132 commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
c265ca0
feat: add device lost relate API
GuoLei1990 Feb 12, 2023
1a2281a
refactorZ: opt code
GuoLei1990 Feb 12, 2023
447cd9a
refactor: clear deprecate code
GuoLei1990 Feb 12, 2023
f972f13
refactor: clear code
GuoLei1990 Feb 12, 2023
e10c914
refactor: opt code
GuoLei1990 Feb 12, 2023
9bf23ca
refactor: opt code
GuoLei1990 Feb 12, 2023
901cfa2
refactor: opt code
GuoLei1990 Feb 12, 2023
ce5c2b5
refactor:opt code
GuoLei1990 Feb 12, 2023
7b9f919
refactor: delete deprecate code
GuoLei1990 Feb 13, 2023
020ae9b
style: fix spell
GuoLei1990 Feb 13, 2023
0ed776f
style: fix spell bug
GuoLei1990 Feb 13, 2023
90ea2cb
refactor: split buffer
GuoLei1990 Feb 13, 2023
f3ead8f
refactor: add GraphicResource
GuoLei1990 Feb 14, 2023
09b9100
refactor: opt code
GuoLei1990 Feb 14, 2023
49482b8
refactor: opt code
GuoLei1990 Feb 14, 2023
b9fb4ad
refactor: opt code
GuoLei1990 Feb 14, 2023
0f68572
refactor: opt code
GuoLei1990 Feb 14, 2023
25a0bd3
refactor: opt code
GuoLei1990 Feb 14, 2023
7d73763
refactor: opt code
GuoLei1990 Feb 14, 2023
3939ba9
refactor: opt code
GuoLei1990 Feb 14, 2023
acf8043
refactor: add rebuild func
GuoLei1990 Feb 15, 2023
8f89953
refactor: opt code
GuoLei1990 Feb 15, 2023
3fd0408
refactor: opt code
GuoLei1990 Feb 15, 2023
25b1a75
refactor: opt code
GuoLei1990 Feb 15, 2023
fbdc746
refactor: opt code
GuoLei1990 Feb 15, 2023
59c625f
refactor: opt code
GuoLei1990 Feb 15, 2023
2047409
refactor: opt code
GuoLei1990 Feb 15, 2023
0f93dd5
refactor: opt code
GuoLei1990 Feb 15, 2023
0182ee6
refactor: opt code
GuoLei1990 Feb 15, 2023
3e9e039
refactor: opt code
GuoLei1990 Feb 16, 2023
b8884e8
refactor: opt code
GuoLei1990 Feb 16, 2023
4137eab
refactor: opt code
GuoLei1990 Feb 16, 2023
4b40ae9
refactor: opt code
GuoLei1990 Feb 16, 2023
5e7647c
refactor: opt code
GuoLei1990 Feb 16, 2023
058901b
refactor: opt code
GuoLei1990 Feb 16, 2023
90ddc48
refactor: opt code
GuoLei1990 Feb 16, 2023
bf8b725
refactor: opt code
GuoLei1990 Feb 16, 2023
2093269
refactor: opt code
GuoLei1990 Feb 17, 2023
a4a6d20
refactor: opt code
GuoLei1990 Feb 17, 2023
29ef428
refactor: opt code
GuoLei1990 Feb 17, 2023
fc5082f
refactor: opt code
GuoLei1990 Feb 17, 2023
15f20fe
refactor: opt code
GuoLei1990 Feb 17, 2023
9a6ee18
docs: add comments for loader
GuoLei1990 Feb 17, 2023
0b135b9
refactor: opt code
GuoLei1990 Feb 17, 2023
e1ff98c
refactor: opt code
GuoLei1990 Feb 17, 2023
325c5d2
refactor: opt code
GuoLei1990 Feb 17, 2023
5de0e8b
refactor: opt code
GuoLei1990 Feb 17, 2023
c386a14
refactor: opt code
GuoLei1990 Feb 17, 2023
b680080
refactor: opt code
GuoLei1990 Feb 17, 2023
e7fe9b4
refactor: opt code
GuoLei1990 Feb 17, 2023
11fcb43
refactor: opt code
GuoLei1990 Feb 17, 2023
9bd7c28
refactor: opt code
GuoLei1990 Feb 17, 2023
57731db
refactor: opt code
GuoLei1990 Feb 17, 2023
4576d03
refactor: opt code
GuoLei1990 Feb 18, 2023
7929065
refactor: opt code
GuoLei1990 Feb 18, 2023
2301a37
refactor: opt code
GuoLei1990 Feb 18, 2023
268ea43
refactor: opt code
GuoLei1990 Feb 18, 2023
b54b19e
refactor: opt code
GuoLei1990 Feb 18, 2023
097fa7e
refactor: opt code
GuoLei1990 Feb 18, 2023
87b4f18
refactor: opt code
GuoLei1990 Feb 18, 2023
90daf0c
refactor: opt code
GuoLei1990 Feb 18, 2023
ec39e5b
refactor: opt code
GuoLei1990 Feb 18, 2023
1c4823f
refactor: opt code
GuoLei1990 Feb 18, 2023
3503762
refactor: opt code
GuoLei1990 Feb 18, 2023
c210687
refactor: opt code
GuoLei1990 Feb 19, 2023
b085d38
refactor: opt code
GuoLei1990 Feb 19, 2023
deb468f
refactor: opt code
GuoLei1990 Feb 20, 2023
014a363
refactor: fix unit test
GuoLei1990 Feb 20, 2023
1dbe2e3
refactor: opt code
GuoLei1990 Feb 20, 2023
8f17abf
refactor: fix spell error
GuoLei1990 Feb 20, 2023
e64243e
refactor: opt code
GuoLei1990 Feb 21, 2023
9574d04
refactor: opt code
GuoLei1990 Feb 21, 2023
e9b4392
refactor: opt code
GuoLei1990 Feb 21, 2023
3201525
refactor: opt code
GuoLei1990 Feb 21, 2023
df037b2
refactor: opt code
GuoLei1990 Feb 21, 2023
4451cf3
test: opt code
GuoLei1990 Feb 21, 2023
e4cbd2b
refactor: opt code
GuoLei1990 Feb 22, 2023
bf53a69
refactor: opt code
GuoLei1990 Feb 22, 2023
6b7bd55
refactor: delete unused file
GuoLei1990 Feb 22, 2023
e738125
refactor: opt code
GuoLei1990 Feb 22, 2023
1df6018
refactor: opt code
GuoLei1990 Feb 22, 2023
9b12f65
refactor: opt code
GuoLei1990 Feb 22, 2023
bbb3b7a
refactor: opt code
GuoLei1990 Feb 22, 2023
c8eef14
refactor: opt code
GuoLei1990 Feb 22, 2023
db1e17c
refactor: opt code
GuoLei1990 Feb 22, 2023
a5df4e7
refactor: opt code
GuoLei1990 Feb 22, 2023
525c0fa
refactor: opt code
GuoLei1990 Feb 22, 2023
ff426b1
refactor: opt code
GuoLei1990 Feb 22, 2023
520c66f
refactor: opt code
GuoLei1990 Feb 22, 2023
961f6f2
refactor: opt code
GuoLei1990 Feb 22, 2023
b181b62
refactor: opt code
GuoLei1990 Feb 22, 2023
b093c09
refactor: opt code
GuoLei1990 Feb 23, 2023
71e82ab
refactor: opt code
GuoLei1990 Feb 23, 2023
916599a
refactor: opt code
GuoLei1990 Feb 23, 2023
cfb9632
refactor: opt code
GuoLei1990 Feb 23, 2023
d22cbbe
refactor: opt code
GuoLei1990 Feb 23, 2023
f6e8350
热factor: opt code
GuoLei1990 Feb 23, 2023
ca6ee8d
refactor: opt code
GuoLei1990 Feb 23, 2023
a5acda9
refactor: opt code
GuoLei1990 Feb 23, 2023
b0aa8d2
refactor: opt code
GuoLei1990 Feb 23, 2023
58ce581
refactor: opt code
GuoLei1990 Feb 23, 2023
af0fa88
refactor: opt code
GuoLei1990 Feb 23, 2023
f398db1
refactor: opt code
GuoLei1990 Feb 23, 2023
c8b5ff9
refactor: opt code
GuoLei1990 Feb 23, 2023
223ef9e
refactor: opt code
GuoLei1990 Feb 23, 2023
418c127
fix: spell error
GuoLei1990 Feb 24, 2023
c669b77
refactor: opt code
GuoLei1990 Feb 24, 2023
bbaa392
refactor: opt code
GuoLei1990 Feb 24, 2023
d27a021
refactor: opt code
GuoLei1990 Feb 24, 2023
f5ec167
feat: opt code
GuoLei1990 Feb 24, 2023
2af4c13
refactor: opt code
GuoLei1990 Feb 24, 2023
19f8150
refactor: opt code
GuoLei1990 Feb 24, 2023
dfb098d
refactor: opt code
GuoLei1990 Feb 24, 2023
63b39d9
refactor: opt code
GuoLei1990 Feb 24, 2023
8a38516
refactor: opt code
GuoLei1990 Feb 24, 2023
a703a38
refactor: opt code
GuoLei1990 Feb 24, 2023
faf448d
refactor: opt code
GuoLei1990 Feb 24, 2023
c4ee3e7
refactor: opt code
GuoLei1990 Feb 25, 2023
4983309
refactor: opt code
GuoLei1990 Feb 25, 2023
797f1a4
refactor: opt code
GuoLei1990 Feb 25, 2023
f44fc56
refactor: opt code
GuoLei1990 Feb 25, 2023
e9bd9de
refactor: opt code
GuoLei1990 Feb 25, 2023
64c6499
refactor: opt code
GuoLei1990 Feb 25, 2023
4dbb60e
Merge commit 'add6e916a8327e492f8fec16bc0e7cd543a59566' into pr/conte…
GuoLei1990 Mar 2, 2023
b05d137
refactor: merge dev/1.0
GuoLei1990 Mar 2, 2023
d657f37
refactor: opt code
GuoLei1990 Mar 2, 2023
13b9619
refactor: opt code
GuoLei1990 Mar 2, 2023
4932d07
feat: support restore text
GuoLei1990 Mar 2, 2023
4195c2e
refactor: opt code
GuoLei1990 Mar 2, 2023
4b78f9c
refactor: opt code
GuoLei1990 Mar 2, 2023
315794f
Merge branch 'dev/1.0' into pr/context-restore
GuoLei1990 Mar 13, 2023
962c948
refactor: opt code
GuoLei1990 Mar 13, 2023
7028a0a
test(DeviceLost): add device lost and restore unit test
GuoLei1990 Mar 15, 2023
bec11d4
refactor: opt code
GuoLei1990 Mar 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions packages/core/src/2d/atlas/FontAtlas.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
import { RefObject } from "../../asset/RefObject";
import { Vector2 } from "@oasis-engine/math";
import { ReferResource } from "../../asset/ReferResource";
import { Engine } from "../../Engine";
import { Texture2D } from "../../texture/Texture2D";
import { CharInfo } from "../text/CharInfo";

/**
* @internal
* Font Atlas.
*/
export class FontAtlas extends RefObject {
private _charInfoMap: Record<number, CharInfo> = {};
private _texture: Texture2D;
export class FontAtlas extends ReferResource {
texture: Texture2D;

_charInfoMap: Record<number, CharInfo> = {};
private _space: number = 1;
private _curX: number = 1;
private _curY: number = 1;
private _nextY: number = 1;

get texture(): Texture2D {
return this._texture;
}

set texture(value: Texture2D) {
this._texture = value;
}

/**
* Constructor a FontAtlas.
* @param engine - Engine to which the FontAtlas belongs
*/
constructor(engine: Engine) {
super(engine);
}

/**
* @override
*/
_onDestroy(): void {
this._texture.destroy();
this._texture = null;
this._charInfoMap = {};
}

uploadCharTexture(charInfo: CharInfo): boolean {
const { w: width, h: height, data } = charInfo;
const { _space: space, texture } = this;
Expand All @@ -64,6 +44,7 @@ export class FontAtlas extends RefObject {
}

if (width > 0 && height > 0 && data) {
charInfo.bufferOffset = new Vector2(this._curX, this._curY);
texture.setPixelBuffer(data, 0, this._curX, this._curY, width, height);
texture.generateMipmaps();
}
Expand Down Expand Up @@ -97,4 +78,15 @@ export class FontAtlas extends RefObject {
getCharInfo(char: string): CharInfo {
return this._charInfoMap[char.charCodeAt(0)];
}

/**
* @override
* @internal
*/
protected _onDestroy(): void {
super._onDestroy();
this.texture.destroy();
this.texture = null;
this._charInfoMap = {};
}
}
8 changes: 5 additions & 3 deletions packages/core/src/2d/atlas/SpriteAtlas.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RefObject } from "../../asset/RefObject";
import { ReferResource } from "../../asset/ReferResource";
import { Engine } from "../../Engine";
import { Sprite } from "../sprite/Sprite";

/**
* Sprite Atlas.
*/
export class SpriteAtlas extends RefObject {
export class SpriteAtlas extends ReferResource {
private _sprites: Sprite[] = new Array<Sprite>();
private _spriteNamesToIndex: Record<string, number> = {};

Expand Down Expand Up @@ -67,8 +67,10 @@ export class SpriteAtlas extends RefObject {

/**
* @override
* @internal
*/
_onDestroy(): void {
protected _onDestroy(): void {
super._onDestroy();
this._sprites = null;
this._spriteNamesToIndex = null;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/2d/sprite/Sprite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BoundingBox, MathUtil, Rect, Vector2, Vector4 } from "@oasis-engine/math";
import { RefObject } from "../../asset/RefObject";
import { ReferResource } from "../../asset/ReferResource";
import { Engine } from "../../Engine";
import { Texture2D } from "../../texture/Texture2D";
import { UpdateFlagManager } from "../../UpdateFlagManager";
Expand All @@ -8,7 +8,7 @@ import { SpriteModifyFlags } from "../enums/SpriteModifyFlags";
/**
* 2D sprite.
*/
export class Sprite extends RefObject {
export class Sprite extends ReferResource {
/** The name of sprite. */
name: string;

Expand Down Expand Up @@ -240,11 +240,11 @@ export class Sprite extends RefObject {

/**
* @override
* @internal
*/
_onDestroy(): void {
if (this._texture) {
this._texture = null;
}
protected _onDestroy(): void {
super._onDestroy();
this._texture = null;
}

private _calDefaultSize(): void {
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,6 @@ export class SpriteMask extends Renderer implements ICustomClone {
this._onSpriteChange = this._onSpriteChange.bind(this);
}

/**
* @override
* @inheritdoc
*/
_onDestroy(): void {
this._sprite?._updateFlagManager.removeListener(this._onSpriteChange);
this._sprite = null;
this._verticesData = null;
super._onDestroy();
}

/**
* @internal
*/
Expand Down Expand Up @@ -218,6 +207,17 @@ export class SpriteMask extends Renderer implements ICustomClone {
this._maskElement = renderElement;
}

/**
* @override
* @inheritdoc
*/
protected _onDestroy(): void {
super._onDestroy();
this._sprite?._updateFlagManager.removeListener(this._onSpriteChange);
this._sprite = null;
this._verticesData = null;
}

@ignoreClone
private _onSpriteChange(type: SpriteModifyFlags): void {
switch (type) {
Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,6 @@ export class SpriteRenderer extends Renderer implements ICustomClone {
target.sprite = this._sprite;
}

/**
* @internal
*/
_onDestroy(): void {
this._sprite?._updateFlagManager.removeListener(this._onSpriteChange);
this._color = null;
this._sprite = null;
this._assembler = null;
this._verticesData = null;
super._onDestroy();
}

/**
* @override
*/
Expand Down Expand Up @@ -278,6 +266,19 @@ export class SpriteRenderer extends Renderer implements ICustomClone {
context.camera._renderPipeline.pushRenderData(context, renderData);
}

/**
* @override
* @internal
*/
protected _onDestroy(): void {
super._onDestroy();
this._sprite?._updateFlagManager.removeListener(this._onSpriteChange);
this._color = null;
this._sprite = null;
this._assembler = null;
this._verticesData = null;
}

private _updateStencilState(): void {
// Update stencil.
const material = this.getInstanceMaterial();
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/2d/text/CharInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Vector2 } from "@oasis-engine/math";
* @internal
*/
export interface CharInfo {
char: string;
x: number;
y: number;
w: number;
Expand All @@ -16,4 +17,5 @@ export interface CharInfo {
index: number;
data?: Uint8Array;
uvs: Vector2[];
bufferOffset?: Vector2;
}
8 changes: 5 additions & 3 deletions packages/core/src/2d/text/Font.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { RefObject } from "../../asset/RefObject";
import { ReferResource } from "../../asset/ReferResource";
import { Engine } from "../../Engine";
import { FontStyle } from "../enums/FontStyle";
import { SubFont } from "./SubFont";

/**
* Font.
*/
export class Font extends RefObject {
export class Font extends ReferResource {
/**
* Create a system font.
* @param engine - Engine to which the font belongs
Expand Down Expand Up @@ -59,8 +59,10 @@ export class Font extends RefObject {

/**
* @override
* @internal
*/
_onDestroy(): void {
protected _onDestroy(): void {
super._onDestroy();
const subFontMap = this._subFontMap;
for (let k in subFontMap) {
subFontMap[k].destroy();
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/2d/text/SubFont.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { AssetPromise } from "../../asset/AssetPromise";
import { ContentRestorer } from "../../asset/ContentRestorer";
import { Engine } from "../../Engine";
import { Texture2D } from "../../texture";
import { FontAtlas } from "../atlas/FontAtlas";
import { CharInfo } from "./CharInfo";
import { TextUtils } from "./TextUtils";

/**
* @internal
*/
export class SubFont {
nativeFontString: string;

private _engine: Engine;
private _fontAtlases: FontAtlas[] = [];
private _lastIndex: number = -1;
Expand Down Expand Up @@ -91,6 +96,32 @@ export class SubFont {
const texture = new Texture2D(engine, 256, 256);
fontAtlas.texture = texture;
this._fontAtlases.push(fontAtlas);

const nativeFontString = this.nativeFontString;

engine.resourceManager.addContentRestorer(
new (class extends ContentRestorer<FontAtlas> {
constructor() {
super(fontAtlas);
}
restoreContent(): void | AssetPromise<FontAtlas> {
const { resource } = this;
const charInfoMap = resource._charInfoMap;
const texture = resource.texture;
for (let k in charInfoMap) {
const charInfo = charInfoMap[k];
const data = TextUtils.measureChar(charInfo.char, nativeFontString).data;

if (charInfo.w > 0 && charInfo.h > 0 && data) {
const { bufferOffset } = charInfo;
texture.setPixelBuffer(data, 0, bufferOffset.x, bufferOffset.y, charInfo.w, charInfo.h);
}
texture.generateMipmaps();
}
}
})()
);

return fontAtlas;
}
}
17 changes: 9 additions & 8 deletions packages/core/src/2d/text/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export class TextRenderer extends Renderer implements ICustomClone {
set font(value: Font) {
const lastFont = this._font;
if (lastFont !== value) {
lastFont && lastFont._addRefCount(-1);
value && value._addRefCount(1);
lastFont && lastFont._addReferCount(-1);
value && value._addReferCount(1);
this._font = value;
this._setDirtyFlagTrue(DirtyFlag.Font);
}
Expand Down Expand Up @@ -287,14 +287,15 @@ export class TextRenderer extends Renderer implements ICustomClone {
_init(): void {
const { engine } = this;
this._font = engine._textDefaultFont;
this._font._addRefCount(1);
this._font._addReferCount(1);
this.setMaterial(engine._spriteDefaultMaterial);
}

/**
* @internal
*/
_onDestroy(): void {
protected _onDestroy(): void {
super._onDestroy();
// Clear render data.
const charRenderDatas = this._charRenderDatas;
for (let i = 0, n = charRenderDatas.length; i < n; ++i) {
Expand All @@ -303,12 +304,10 @@ export class TextRenderer extends Renderer implements ICustomClone {
charRenderDatas.length = 0;

if (this._font) {
this._font._addRefCount(-1);
this._font._addReferCount(-1);
this._font = null;
}
this._subFont && (this._subFont = null);

super._onDestroy();
}

/**
Expand Down Expand Up @@ -431,7 +430,9 @@ export class TextRenderer extends Renderer implements ICustomClone {
}

private _resetSubFont(): void {
this._subFont = this._font._getSubFont(this.fontSize, this.fontStyle);
const font = this._font;
this._subFont = font._getSubFont(this.fontSize, this.fontStyle);
this._subFont.nativeFontString = TextUtils.getNativeFontString(font.name, this.fontSize, this.fontStyle);
}

private _updatePosition(): void {
Expand Down
Loading