Skip to content

Commit

Permalink
add TextureRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneycode committed Apr 1, 2020
1 parent f36a10f commit b473da8
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 68 deletions.
Binary file added app/src/main/assets/test_image_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.github.kenneycode.fusion.demo.SimpleActivity.Companion.KEY_SAMPLE_INDEX
import io.github.kenneycode.fusion.util.BitmapUtil


/**
Expand All @@ -28,7 +29,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Util.context = applicationContext
BitmapUtil.context = applicationContext

val samplesList = findViewById<RecyclerView>(R.id.list)
val layoutManager = LinearLayoutManager(this)
Expand Down
38 changes: 0 additions & 38 deletions app/src/main/java/io/github/kenneycode/fusion/demo/Util.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.kenneycode.fusion.demo.R
import io.github.kenneycode.fusion.demo.Util
import io.github.kenneycode.fusion.util.BitmapUtil
import io.github.kenneycode.fusion.framebuffer.FrameBufferPool

import io.github.kenneycode.fusion.input.FusionImage
import io.github.kenneycode.fusion.process.RenderChain
import io.github.kenneycode.fusion.process.RenderPipeline
import io.github.kenneycode.fusion.program.GLProgramPool
import io.github.kenneycode.fusion.renderer.CropRenderer
import io.github.kenneycode.fusion.renderer.GaussianBlurRenderer
import io.github.kenneycode.fusion.renderer.LUTRenderer
import io.github.kenneycode.fusion.renderer.ScaleRenderer
import io.github.kenneycode.fusion.renderer.*
import io.github.kenneycode.fusion.texture.TexturePool
import kotlinx.android.synthetic.main.fragment_sample_fusion_gl_texture_view.*

Expand All @@ -42,14 +39,15 @@ class SampleBasicUsage : Fragment() {

// 创建RenderChain并添加一些renderer
val renderer = RenderChain()
.addRenderer(ScaleRenderer().apply { setFlip(false, true); setScale(0.8f) })
.addRenderer(ScaleRenderer().apply { setScale(0.8f) })
.addRenderer(CropRenderer().apply { setCropRect(0.1f, 0.9f, 0.8f, 0.2f) })
.addRenderer(LUTRenderer().apply { setLUTImage(Util.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(LUTRenderer().apply { setLUTImage(BitmapUtil.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(GaussianBlurRenderer().apply { setBlurRadius(10) })
.addRenderer(TextureRenderer().apply { setBitmap(BitmapUtil.decodeBitmapFromAssets("test_image_0.png")!!); setRenderRect(-1f, 1f, -0.5f, 0.8f) })

// 创建RenderPipeline,连接输入、渲染器与输出
renderPipeline = RenderPipeline
.input(FusionImage(Util.decodeBitmapFromAssets("test.png")!!))
.input(FusionImage(BitmapUtil.decodeBitmapFromAssets("test.png")!!))
.renderWith(renderer)
.useContext(fusionView)
.output(fusionView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.kenneycode.fusion.common.DataKeys
import io.github.kenneycode.fusion.demo.R
import io.github.kenneycode.fusion.demo.Util
import io.github.kenneycode.fusion.util.BitmapUtil
import io.github.kenneycode.fusion.framebuffer.FrameBufferPool
import io.github.kenneycode.fusion.process.RenderGraph
import io.github.kenneycode.fusion.program.GLProgramPool
Expand Down Expand Up @@ -84,7 +84,7 @@ class SampleGLSurfaceViewUsage0 : Fragment() {
}

// 创建图片输入源
val bitmap = Util.decodeBitmapFromAssets("test.png")!!
val bitmap = BitmapUtil.decodeBitmapFromAssets("test.png")!!
val buffer = ByteBuffer.allocate(bitmap.width * bitmap.height * 4)
bitmap.copyPixelsToBuffer(buffer)
buffer.position(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.kenneycode.fusion.demo.R
import io.github.kenneycode.fusion.demo.Util
import io.github.kenneycode.fusion.util.BitmapUtil
import io.github.kenneycode.fusion.framebuffer.FrameBufferPool
import io.github.kenneycode.fusion.input.FusionImage
import io.github.kenneycode.fusion.output.FusionBitmap
Expand Down Expand Up @@ -51,15 +51,15 @@ class SampleImageOffscreenRender : Fragment() {
val renderer = RenderChain()
.addRenderer(ScaleRenderer().apply { setFlip(false, true); setScale(0.8f) })
.addRenderer(CropRenderer().apply { setCropRect(0.1f, 0.9f, 0.8f, 0.2f) })
.addRenderer(LUTRenderer().apply { setLUTImage(Util.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(LUTRenderer().apply { setLUTImage(BitmapUtil.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(GaussianBlurRenderer().apply { setBlurRadius(10) })

// 输出bitmap
val output = FusionBitmap()

// 创建RenderPipeline,连接输入、渲染器与输出
val renderPipeline = RenderPipeline
.input(FusionImage(Util.decodeBitmapFromAssets(sourceImagePath)!!))
.input(FusionImage(BitmapUtil.decodeBitmapFromAssets(sourceImagePath)!!))
.renderWith(renderer)
.output(output)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.kenneycode.fusion.common.DataKeys
import io.github.kenneycode.fusion.demo.R
import io.github.kenneycode.fusion.demo.Util
import io.github.kenneycode.fusion.util.BitmapUtil
import io.github.kenneycode.fusion.framebuffer.FrameBufferPool

import io.github.kenneycode.fusion.process.RenderChain
Expand Down Expand Up @@ -88,7 +88,7 @@ class SampleMVPMatrix : Fragment() {
}

// 创建图片输入源
val bitmap = Util.decodeBitmapFromAssets("test.png")!!
val bitmap = BitmapUtil.decodeBitmapFromAssets("test.png")!!
val buffer = ByteBuffer.allocate(bitmap.width * bitmap.height * 4)
bitmap.copyPixelsToBuffer(buffer)
buffer.position(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.kenneycode.fusion.demo.R
import io.github.kenneycode.fusion.demo.Util
import io.github.kenneycode.fusion.util.BitmapUtil
import io.github.kenneycode.fusion.framebuffer.FrameBufferPool

import io.github.kenneycode.fusion.input.FusionImage
import io.github.kenneycode.fusion.input.FusionVideo
import io.github.kenneycode.fusion.process.RenderChain
import io.github.kenneycode.fusion.process.RenderPipeline
Expand Down Expand Up @@ -41,7 +40,7 @@ class SampleVideo : Fragment() {
// 创建RenderChain并添加一些renderer
val renderer = RenderChain()
.addRenderer(OES2RGBARenderer())
.addRenderer(LUTRenderer().apply { setLUTImage(Util.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(LUTRenderer().apply { setLUTImage(BitmapUtil.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(GaussianBlurRenderer().apply { setBlurRadius(10) })

// 创建RenderPipeline,连接输入、渲染器与输出
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.kenneycode.fusion.demo.R
import io.github.kenneycode.fusion.demo.Util
import io.github.kenneycode.fusion.util.BitmapUtil
import io.github.kenneycode.fusion.framebuffer.FrameBufferPool
import io.github.kenneycode.fusion.input.FusionVideoDecoder
import io.github.kenneycode.fusion.output.FusionVideoEncoder
Expand Down Expand Up @@ -48,7 +48,7 @@ class SampleVideoOffscreenRender : Fragment() {
// 创建RenderChain并添加一些renderer
val renderer = RenderChain()
.addRenderer(OES2RGBARenderer())
.addRenderer(LUTRenderer().apply { setLUTImage(Util.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(LUTRenderer().apply { setLUTImage(BitmapUtil.decodeBitmapFromAssets("test_lut.png")!!); setLUTStrength(0.8f) })
.addRenderer(GaussianBlurRenderer().apply { setBlurRadius(10) })

// 视频解码器
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.kenneycode.fusion.context.GLContext
import io.github.kenneycode.fusion.process.RenderPipeline
import io.github.kenneycode.fusion.texture.Texture
import io.github.kenneycode.fusion.texture.TexturePool
import io.github.kenneycode.fusion.util.BitmapUtil
import java.nio.ByteBuffer

/**
Expand Down Expand Up @@ -33,7 +34,7 @@ class FusionImage(private val image: Bitmap) : RenderPipeline.Input, InputReceiv

override fun onInit(glContext: GLContext, data: MutableMap<String, Any>) {
val buffer = ByteBuffer.allocate(image.width * image.height * 4)
image.copyPixelsToBuffer(buffer)
BitmapUtil.flipBitmap(image, false, true).copyPixelsToBuffer(buffer)
buffer.position(0)
imageTexture = TexturePool.obtainTexture(image.width, image.height).apply {
retain = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.kenneycode.fusion.parameter

import android.graphics.Bitmap
import android.opengl.GLES20.*
import android.util.Log
import io.github.kenneycode.fusion.util.GLUtil
import io.github.kenneycode.fusion.util.Util

/**
*
* Coded by kenney
*
* http://www.github.com/kenneycode/fusion
*
* Bitmap shader uniform sampler2D 参数
*
*/

class BitmapTexture2DParameter(key: String, private var value: Bitmap, index: Int) : Texture2DParameter(key, 0, index) {

override fun onBind(location: Int) {
Util.assert(!value.isRecycled)
if (!glIsTexture(getValue() as Int)) {
super.update(GLUtil.bitmap2Texture(value, true))
}
super.onBind(location)
}

override fun update(value: Any) {
(value as? Bitmap)?.let {
if (this.value != it) {
GLUtil.deleteTexture(getValue() as Int)
}
this.value = it
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract class Parameter(val key : String) {
*/
protected abstract fun onBind(location: Int)

protected open fun getValue(): Any { return 0 }

/**
*
* 更新参数
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import io.github.kenneycode.fusion.util.Util
*
* http://www.github.com/kenneycode/fusion
*
* Shader uniform sampler2D数组参数
* Shader uniform sampler2D参数
*
*/

class Texture2DParameter(key: String, private var value: Int, private val index: Int) : UniformParameter(key) {
open class Texture2DParameter(key: String, private var value: Int, private val index: Int) : UniformParameter(key) {

override fun onBind(location: Int) {
Util.assert(glIsTexture(value))
Expand All @@ -24,6 +24,10 @@ class Texture2DParameter(key: String, private var value: Int, private val index:
glUniform1i(location, index)
}

override fun getValue(): Any {
return value
}

override fun update(value: Any) {
(value as? Int)?.let {
this.value = it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.kenneycode.fusion.renderer

import android.graphics.Bitmap

/**
*
* Coded by kenney
Expand Down Expand Up @@ -48,6 +50,16 @@ interface GLRenderer : Renderer {
*/
fun setTextureCoordinates(textureCoordinates: FloatArray)

/**
*
* 设置 bitmap 纹理参数
*
* @param key 纹理参数名
* @param value bitmap
*
*/
fun setUniformBitmapTexture2D(key: String, value: Bitmap, index: Int = 0)

/**
*
* 设置纹理参数
Expand Down
Loading

0 comments on commit b473da8

Please sign in to comment.