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

Optimize BaseGraphics when removed from stage. And adds View.onAncestorChanged #1731

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ package korlibs.korge.internal
/** A mechanism to annotate Korge internal properties and methods that were left open in KorGE 1.0 so they can be marked as internal in KorGE 2.0 */
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
annotation class KorgeInternal

//object KorgeInternalApi

3 changes: 2 additions & 1 deletion korge/src/commonMain/kotlin/korlibs/korge/ui/UIView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ open class UIView(
parent?.onChildChangedSize(this)
}

override fun onParentChanged() {
override fun onAncestorChanged() {
super.onAncestorChanged()
updateState()
}

Expand Down
30 changes: 27 additions & 3 deletions korge/src/commonMain/kotlin/korlibs/korge/view/BaseGraphics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,38 @@ abstract class BaseGraphics(
invalidateLocalBounds()
}

@OptIn(KorgeExperimental::class)
override fun renderInternal(ctx: RenderContext) {
override fun onAncestorChanged() {
super.onAncestorChanged()
if (parent == null || stage == null) {
if (bitmap.base != Bitmaps.transparent.bmp) {
//println("REMOVED FROM STAGE! $bitmap")
bitmapsToRemove.add(bitmap.base)
}
clearBitmaps(onParentChanged = true)
}
}

private fun clearBitmaps(onParentChanged: Boolean = false) {
if (bitmapsToRemove.isEmpty()) return

if (onParentChanged) {
//println("clearBitmaps! onParentChanged=$onParentChanged : $bitmapsToRemove")
}

bitmapsToRemove.fastForEach {
if (it != Bitmaps.transparent.bmp) {
ctx.agBitmapTextureManager.removeBitmap(it, "BaseGraphics")
_ctx?.agBitmapTextureManager?.removeBitmap(it, "BaseGraphics")
}
}
bitmapsToRemove.clear()
}

private var _ctx: RenderContext? = null

@OptIn(KorgeExperimental::class)
override fun renderInternal(ctx: RenderContext) {
_ctx = ctx
clearBitmaps(onParentChanged = false)

if (redrawIfRequired()) {
//ctx.coroutineContext.launchUnscoped { this.bitmap.bmpBase.writeTo(localVfs("/tmp/image.png"), PNG) }
Expand Down
7 changes: 7 additions & 0 deletions korge/src/commonMain/kotlin/korlibs/korge/view/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ open class Container(
open fun onChildChangedSize(view: View) {
}

override fun onAncestorChanged() {
super.onAncestorChanged()
forEachChild {
//it.apply { KorgeInternalApi._onAncestorChanged() }
it._onAncestorChanged()
}
}
/**
* Retrieves the index of a given child [View].
*/
Expand Down
9 changes: 9 additions & 0 deletions korge/src/commonMain/kotlin/korlibs/korge/view/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,15 @@ abstract class View internal constructor(
}

protected open fun onParentChanged() {
onAncestorChanged()
}

//fun KorgeInternalApi._onAncestorChanged() {
internal fun _onAncestorChanged() {
onAncestorChanged()
}

protected open fun onAncestorChanged() {
}

override fun invalidateRender() {
Expand Down