dynamic textures: use weak references

This commit is contained in:
Bixilon 2022-05-02 22:36:50 +02:00
parent e7f3891cc4
commit 942a1a8787
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 14 additions and 7 deletions

View File

@ -97,4 +97,8 @@ open class DynamicImageElement(
override fun forceSilentApply() = Unit
override fun silentApply(): Boolean = false
protected fun finalize() {
texture?.usages?.decrementAndGet()
}
}

View File

@ -104,7 +104,7 @@ class TabListEntryElement(
nameElement.text = displayName
this.prefSize = Vec2i((PADDING * 6) + skinElement.prefSize.x + nameElement.prefSize.x + INNER_MARGIN + pingElement.prefSize.x, HEIGHT)
this.prefSize = Vec2i((PADDING * 6) + skinElement.size.x + nameElement.prefSize.x + INNER_MARGIN + pingElement.prefSize.x, HEIGHT)
background.size = size
cacheUpToDate = false
}

View File

@ -28,6 +28,7 @@ import org.lwjgl.opengl.GL12.glTexSubImage3D
import org.lwjgl.opengl.GL13.GL_TEXTURE0
import org.lwjgl.opengl.GL13.glActiveTexture
import org.lwjgl.opengl.GL30.GL_TEXTURE_2D_ARRAY
import java.lang.ref.WeakReference
import java.nio.ByteBuffer
import java.util.*
@ -37,7 +38,7 @@ class OpenGLDynamicTextureArray(
initialSize: Int = 32,
val resolution: Int,
) : DynamicTextureArray {
private val textures: Array<OpenGLDynamicTexture?> = arrayOfNulls(initialSize)
private val textures: Array<WeakReference<OpenGLDynamicTexture>?> = arrayOfNulls(initialSize)
private var textureId = -1
override val size: Int
@ -71,14 +72,15 @@ class OpenGLDynamicTextureArray(
override fun pushBuffer(identifier: UUID, data: () -> ByteBuffer): OpenGLDynamicTexture {
check(textureId >= 0) { "Dynamic texture array not yet initialized!" }
cleanup()
for (texture in textures) {
for (textureReference in textures) {
val texture = textureReference?.get()
if (texture?.uuid == identifier) {
return texture
}
}
val index = getNextIndex()
val texture = OpenGLDynamicTexture(identifier, createShaderIdentifier(index = index))
textures[index] = texture
textures[index] = WeakReference(texture)
texture.state = DynamicTextureState.LOADING
DefaultThreadPool += {
val bytes = data()
@ -138,11 +140,12 @@ class OpenGLDynamicTextureArray(
}
private fun cleanup() {
for ((index, texture) in textures.withIndex()) {
if (texture == null) {
for ((index, textureReference) in textures.withIndex()) {
if (textureReference == null) {
continue
}
if (texture.usages.get() > 0) {
val texture = textureReference.get()
if (texture != null && texture.usages.get() > 0) {
continue
}
textures[index] = null