Skip to content

Commit

Permalink
fixed #18139: Bug: [Scene] frame should not be invalid, it may have b…
Browse files Browse the repository at this point in the history
…een released. (#18144)
  • Loading branch information
dumganhar authored Jan 10, 2025
1 parent e400883 commit 13888fc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cocos/2d/assembler/label/font-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { BufferTextureCopy } from '../../../gfx';
import { safeMeasureText, BASELINE_RATIO, MIDDLE_RATIO, getBaselineOffset, getSymbolCodeAt } from '../../utils/text-utils';
import { director, DirectorEvent } from '../../../game/director';
import { ccwindow } from '../../../core/global-exports';
import { TextureBase } from '../../../asset/assets/texture-base';

export interface ISharedLabelData {
canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -366,12 +367,11 @@ export class LetterAtlas {
this.reset();
const dict = this.fontDefDictionary;
if (dict && dict.texture) {
dict.texture.destroy();
dict.texture = null;
}
}

getTexture (): any {
getTexture (): TextureBase | null {
return this.fontDefDictionary.getTexture();
}

Expand Down
23 changes: 21 additions & 2 deletions cocos/2d/assets/bitmap-font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,31 @@ export interface ILetterDefinition {

export class FontAtlas {
public letterDefinitions: ILetterDefinition = {};
public declare texture: TextureBase | null;
public _texture: TextureBase | null = null;

constructor (texture: TextureBase | null) {
this.texture = texture;
}

public set texture (texture: TextureBase | null) {
const oldTexture = this._texture;
if (oldTexture === texture) return;
if (oldTexture) {
oldTexture.decRef(false);
if (oldTexture.refCount <= 0) {
oldTexture.destroy();
}
}
if (texture) {
texture.addRef();
}
this._texture = texture;
}

public get texture (): TextureBase | null {
return this._texture;
}

public addLetterDefinitions (letter: string, letterDefinition: FontLetterDefinition): void {
this.letterDefinitions[letter] = letterDefinition;
}
Expand All @@ -73,7 +92,7 @@ export class FontAtlas {
}

public getTexture (): TextureBase | null {
return this.texture;
return this._texture;
}

public getLetter (key: string): FontLetterDefinition {
Expand Down
24 changes: 20 additions & 4 deletions cocos/2d/components/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ export class Label extends UIRenderer {
}
if (this._cacheMode === CacheMode.CHAR) {
this._ttfSpriteFrame = null;
this.destroyLetterTexture();
}

this._cacheMode = value;
Expand Down Expand Up @@ -932,12 +933,22 @@ export class Label extends UIRenderer {
// this._textRenderData = null;
// this._textLayoutData = null;

// texture cannot be destroyed in here, lettertexture image source is public.
this._letterTexture = null;
this.destroyLetterTexture();

super.onDestroy();
}

private destroyLetterTexture (): void {
const letterTexture = this._letterTexture;
if (letterTexture) {
letterTexture.decRef(false);
if (letterTexture.refCount <= 0) {
letterTexture.destroy();
}
}
this._letterTexture = null;
}

/**
* @en update render data.
* @zh 更新渲染相关数据。
Expand Down Expand Up @@ -1035,8 +1046,13 @@ export class Label extends UIRenderer {
}
} else {
if (this.cacheMode === CacheMode.CHAR) {
this._letterTexture = this._assembler!.getAssemblerData();
this._texture = this._letterTexture;
const oldLetterTexture = this._letterTexture;
const letterTexture = this._assembler!.getAssemblerData();
if (letterTexture !== oldLetterTexture) {
this.destroyLetterTexture();
letterTexture.addRef();
}
this._texture = this._letterTexture = letterTexture;
} else if (!this._ttfSpriteFrame) {
this._ttfSpriteFrame = new SpriteFrame();
this._assemblerData = this._assembler!.getAssemblerData();
Expand Down

0 comments on commit 13888fc

Please sign in to comment.