-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Refine: make multi skeleton component shared one instance of spine.Skeleton while spine working at sharedCache mode #16593
Changes from 2 commits
1fa3760
81413aa
2804b57
9de0f58
e048055
c75985f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import spine from './lib/spine-core.js'; | ||
import { SkeletonData } from './skeleton-data'; | ||
import { warn } from '../core/platform/debug'; | ||
import { Skeleton } from './skeleton'; | ||
|
||
const MaxCacheTime = 30; | ||
const FrameTime = 1 / 60; | ||
|
@@ -174,13 +175,13 @@ | |
const vPtr = model.vPtr; | ||
const vLength = vc * Float32Array.BYTES_PER_ELEMENT * floatStride; | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
vUint8Buf.set(HEAPU8.subarray(vPtr, vPtr + vLength)); | ||
|
||
const iPtr = model.iPtr; | ||
const iLength = Uint16Array.BYTES_PER_ELEMENT * ic; | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
const iUint8Buf = new Uint8Array(iUint16Buf.buffer); | ||
iUint8Buf.set(HEAPU8.subarray(iPtr, iPtr + iLength)); | ||
|
||
const modelData = new SpineModel(); | ||
modelData.vCount = vc; | ||
|
@@ -310,7 +311,10 @@ | |
|
||
protected _privateMode: boolean; | ||
protected _skeletonCache: { [key: string]: SkeletonCacheItemInfo }; | ||
|
||
//for shared mode only | ||
protected _animationPool: { [key: string]: AnimationCache }; | ||
private _sharedCacheMap: Map<string, Array<Skeleton>> = new Map<string, Array<Skeleton>>(); | ||
constructor () { | ||
this._privateMode = false; | ||
this._animationPool = {}; | ||
|
@@ -338,7 +342,28 @@ | |
} | ||
} | ||
|
||
public removeSkeleton (uuid: string): void { | ||
public removeSkeleton (uuid: string, skeletonComponent: Skeleton): void { | ||
const sharedInstances = this._sharedCacheMap.get(uuid); | ||
if (sharedInstances) { | ||
const index = sharedInstances.indexOf(skeletonComponent); | ||
if (index !== -1) { | ||
sharedInstances.splice(index, 1); | ||
} | ||
if (sharedInstances.length > 0) { | ||
return; | ||
} | ||
this._sharedCacheMap.delete(uuid); | ||
} | ||
|
||
const sharedOperate = (aniKey: string, animationCache: AnimationCache): void => { | ||
this._animationPool[`${uuid}#${aniKey}`] = animationCache; | ||
animationCache.clear(); | ||
}; | ||
const privateOperate = (aniKey: string, animationCache: AnimationCache): void => { | ||
animationCache.destroy(); | ||
}; | ||
const operate = sharedInstances ? sharedOperate : privateOperate; | ||
|
||
const skeletonInfo = this._skeletonCache[uuid]; | ||
if (!skeletonInfo) return; | ||
const animationsCache = skeletonInfo.animationsCache; | ||
|
@@ -347,17 +372,29 @@ | |
// No need to create TypedArray next time. | ||
const animationCache = animationsCache[aniKey]; | ||
if (!animationCache) continue; | ||
this._animationPool[`${uuid}#${aniKey}`] = animationCache; | ||
animationCache.clear(); | ||
operate(aniKey, animationCache); | ||
} | ||
|
||
if (skeletonInfo.skeleton) { | ||
spine.wasmUtil.destroySpineSkeleton(skeletonInfo.skeleton); | ||
} | ||
delete this._skeletonCache[uuid]; | ||
} | ||
|
||
public getSkeletonCache (uuid: string, skeletonData: spine.SkeletonData): SkeletonCacheItemInfo { | ||
public getSkeletonCache (uuid: string, skeletonData: spine.SkeletonData, skeletonComponent: Skeleton): SkeletonCacheItemInfo { | ||
if (SkeletonCache.sharedCache === this) { | ||
let sharedInstances = this._sharedCacheMap.get(uuid); | ||
if (!sharedInstances) { | ||
sharedInstances = new Array<Skeleton>(); | ||
this._sharedCacheMap.set(uuid, sharedInstances); | ||
} | ||
if (sharedInstances.indexOf(skeletonComponent) === -1) { | ||
sharedInstances.push(skeletonComponent); | ||
} | ||
} | ||
let skeletonInfo = this._skeletonCache[uuid]; | ||
if (!skeletonInfo) { | ||
const skeleton = null; | ||
const skeleton = new spine.Skeleton(skeletonData); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. skeleton is forever There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. skeleton will constructor by skeleton component's _instance in old code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we move the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, previous logic multi skeleton component can not share the same object of spine.skeleton |
||
const clipper = null; | ||
const state = null; | ||
const listener = new TrackEntryListeners(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it called
privateOperate
? What doesprivate
mean here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private corresponds to privateCache mode.