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

[Issue-8] Update bitmaps on fly #9

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)