From ffe2fca1d92b64f1f6ce19f3911b31df348bc34f Mon Sep 17 00:00:00 2001 From: Alexey Glukharev Date: Sun, 18 Jul 2021 12:19:18 -0700 Subject: [PATCH] [Issue-8] Update bitmaps on fly --- .../annotations/ShaderExperimentalApi.kt | 6 +++++ .../shaderview/gl/params/ShaderParams.kt | 13 +++++++++++ .../gl/params/ShaderParamsBuilder.kt | 6 +++-- .../shaderview/gl/params/ShaderParamsImpl.kt | 23 ++++++++++++++++++- .../shaderview/gl/params/SpecialParam.kt | 1 + 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 ShaderView/src/main/java/com/appspell/shaderview/annotations/ShaderExperimentalApi.kt diff --git a/ShaderView/src/main/java/com/appspell/shaderview/annotations/ShaderExperimentalApi.kt b/ShaderView/src/main/java/com/appspell/shaderview/annotations/ShaderExperimentalApi.kt new file mode 100644 index 0000000..0cb646d --- /dev/null +++ b/ShaderView/src/main/java/com/appspell/shaderview/annotations/ShaderExperimentalApi.kt @@ -0,0 +1,6 @@ +package com.appspell.shaderview.annotations + +@Suppress("DEPRECATION") +@Experimental(level = Experimental.Level.WARNING) +@RequiresOptIn(level = RequiresOptIn.Level.WARNING) +public annotation class ShaderExperimentalApi \ No newline at end of file diff --git a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParams.kt b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParams.kt index 8ca6a09..b7dd2a4 100644 --- a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParams.kt +++ b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParams.kt @@ -1,6 +1,9 @@ package com.appspell.shaderview.gl.params import android.content.res.Resources +import android.graphics.Bitmap +import androidx.annotation.DrawableRes +import com.appspell.shaderview.annotations.ShaderExperimentalApi const val UNKNOWN_LOCATION = -1 @@ -13,6 +16,16 @@ interface ShaderParams { fun updateValue(paramName: String, value: FloatArray) fun updateValue(paramName: String, value: IntArray) + /** + * Update Sample2D with particular Bitmap + * Note: don't forget to recycle Bitmap manually + */ + @ShaderExperimentalApi + fun updateValue2D(paramName: String, value: Bitmap?, needToRecycleWhenUploaded: Boolean = false) + + @ShaderExperimentalApi + fun updateValue2D(paramName: String, @DrawableRes res: Int) + fun getParamShaderLocation(paramName: String): Int? fun getParamValue(paramName: String): Any? diff --git a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsBuilder.kt b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsBuilder.kt index efab9bc..9a08012 100644 --- a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsBuilder.kt +++ b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsBuilder.kt @@ -131,7 +131,8 @@ class ShaderParamsBuilder { valeType = Param.ValueType.SAMPLER_2D, value = TextureParam( bitmap = bitmap, - textureSlot = textureSlot + textureSlot = textureSlot, + needToRecycleWhenUploaded = false ) ) result.updateParam(paramName = paramName, param = param) @@ -151,7 +152,8 @@ class ShaderParamsBuilder { valeType = Param.ValueType.SAMPLER_2D, value = TextureParam( textureResourceId = textureResourceId, - textureSlot = textureSlot + textureSlot = textureSlot, + needToRecycleWhenUploaded = true ) ) result.updateParam(paramName = paramName, param = param) diff --git a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsImpl.kt b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsImpl.kt index e3d677c..b896538 100644 --- a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsImpl.kt +++ b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/ShaderParamsImpl.kt @@ -1,9 +1,11 @@ package com.appspell.shaderview.gl.params import android.content.res.Resources +import android.graphics.Bitmap import android.graphics.SurfaceTexture import android.opengl.GLES30 import android.view.Surface +import com.appspell.shaderview.annotations.ShaderExperimentalApi import com.appspell.shaderview.ext.createExternalTexture import com.appspell.shaderview.ext.loadBitmapForTexture import com.appspell.shaderview.ext.toGlTexture @@ -37,6 +39,22 @@ class ShaderParamsImpl : ShaderParams { map[paramName]?.value = value } + @ShaderExperimentalApi + override fun updateValue2D(paramName: String, value: Bitmap?, needToRecycleWhenUploaded: Boolean) { + map[paramName]?.value = (map[paramName]?.value as? TextureParam)?.copy( + bitmap = value, + needToRecycleWhenUploaded = needToRecycleWhenUploaded + ) + } + + @ShaderExperimentalApi + override fun updateValue2D(paramName: String, res: Int) { + map[paramName]?.value = (map[paramName]?.value as? TextureParam)?.copy( + textureResourceId = res, + needToRecycleWhenUploaded = true + ) + } + /** * Usually it returns uniform shader ID of particaluar parameter (if initialized) */ @@ -150,7 +168,10 @@ class ShaderParamsImpl : ShaderParams { } // upload bitmap to GPU - bitmap?.toGlTexture(needToRecycle = true, textureParam.textureSlot) + bitmap?.toGlTexture( + needToRecycle = textureParam.needToRecycleWhenUploaded, + textureSlot = textureParam.textureSlot + ) }.also { textureId -> value = (value as? TextureParam)?.copy( textureId = textureId diff --git a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/SpecialParam.kt b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/SpecialParam.kt index 108758d..a72f749 100644 --- a/ShaderView/src/main/java/com/appspell/shaderview/gl/params/SpecialParam.kt +++ b/ShaderView/src/main/java/com/appspell/shaderview/gl/params/SpecialParam.kt @@ -19,5 +19,6 @@ data class TextureParam( @DrawableRes val textureResourceId: Int? = null, val bitmap: Bitmap? = null, var textureId: Int? = null, + val needToRecycleWhenUploaded: Boolean = true, val textureSlot: Int = GLES30.GL_TEXTURE0 ) \ No newline at end of file