Skip to content

Commit

Permalink
support more parameters, add OES convert renderer and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneycode committed Dec 4, 2019
1 parent 2cb90ba commit 8553ccd
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
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.framebuffer.FrameBufferCache
Expand Down Expand Up @@ -49,8 +50,8 @@ class SampleGLSurfaceViewUsage0 : Fragment() {

// 执行渲染
renderGraph.update(mutableMapOf(
DisplayRenderer.KEY_DISPLAY_WIDTH to surfaceWidth,
DisplayRenderer.KEY_DISPLAY_HEIGHT to surfaceHeight
DataKeys.KEY_DISPLAY_WIDTH to surfaceWidth,
DataKeys.KEY_DISPLAY_HEIGHT to surfaceHeight
))
renderGraph.render()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,43 @@ class Constants {

companion object {

val COMMON_VERTEX_SHADER = "precision mediump float;\n" +
val COMMON_VERTEX_SHADER =
"precision mediump float;\n" +
"attribute vec4 a_position;\n" +
"attribute vec2 a_textureCoordinate;\n" +
"varying vec2 v_textureCoordinate;\n" +
"void main() {\n" +
" v_textureCoordinate = a_textureCoordinate;\n" +
" gl_Position = a_position;\n" +
"}"

val COMMON_FRAGMENT_SHADER = "precision mediump float;\n" +
val COMMON_FRAGMENT_SHADER =
"precision mediump float;\n" +
"varying vec2 v_textureCoordinate;\n" +
"uniform sampler2D u_texture;\n" +
"void main() {\n" +
" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" +
" gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" +
"}"

val COMMON_VERTEX_SHADER_2 = "precision mediump float;\n" +
val OES_VERTEX_SHADER =
"precision mediump float;\n" +
"attribute vec4 a_position;\n" +
"attribute vec2 a_textureCoordinate;\n" +
"attribute vec4 a_textureCoordinate;\n" +
"varying vec2 v_textureCoordinate;\n" +
"uniform mat4 u_stMatrix;\n" +
"void main() {\n" +
" v_textureCoordinate = a_textureCoordinate;\n" +
" v_textureCoordinate = (u_stMatrix * a_textureCoordinate).xy;\n" +
" gl_Position = a_position;\n" +
"}"

val COMMON_FRAGMENT_SHADER_2 = "precision mediump float;\n" +
val OES_FRAGMENT_SHADER =
"#extension GL_OES_EGL_image_external : require\n" +
"varying vec2 v_textureCoordinate;\n" +
"uniform sampler2D u_texture;\n" +
"uniform samplerExternalOES u_texture;\n" +
"void main() {\n" +
" gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" +
" gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" +
"}"


val COMMON_VERTEX = floatArrayOf(-1f, -1f, -1f, 1f, 1f, 1f, -1f, -1f, 1f, 1f, 1f, -1f)
val COMMON_VERTEX_FLIP_X = floatArrayOf(1f, -1f, 1f, 1f, -1f, 1f, 1f, -1f, -1f, 1f, -1f, -1f)
val COMMON_VERTEX_FLIP_Y = floatArrayOf(-1f, 1f, -1f, -1f, 1f, -1f, -1f, 1f, 1f, -1f, 1f, 1f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.kenneycode.fusion.common

class DataKeys {

companion object {

const val ST_MATRIX = "ST_MATRIX"
const val KEY_DISPLAY_WIDTH = "KEY_DISPLAY_WIDTH"
const val KEY_DISPLAY_HEIGHT = "KEY_DISPLAY_HEIGHT"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class Ref {
* @param count 要增加的引用计数
*
*/
fun addRef(count: Int = 1) {
fun increaseRef(count: Int = 1) {
refCount += count
}

Expand All @@ -30,7 +30,7 @@ open class Ref {
* 减少1个引用计数
*
*/
open fun releaseRef() {
open fun decreaseRef() {
--refCount
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.github.kenneycode.fusion.context

class SimpleGLContext : GLContext {

private val glThread = GLThread()
private val glThread = FusionGLThread()

init {
glThread.init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import android.view.Surface
*
*/

class EGL {
class FusionEGL {

var eglContext = EGL14.EGL_NO_CONTEXT

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import java.util.concurrent.Semaphore
*
*/

class GLThread {
class FusionGLThread {

private val handlerThread : HandlerThread = HandlerThread("GLThread")
private val handlerThread : HandlerThread = HandlerThread("FusionGLThread")
private lateinit var handler : Handler
lateinit var egl : EGL
lateinit var egl : FusionEGL

/**
*
Expand All @@ -34,7 +34,7 @@ class GLThread {
fun init(surface: Surface? = null, shareContext: EGLContext = EGL14.EGL_NO_CONTEXT) {
handlerThread.start()
handler = Handler(handlerThread.looper)
egl = EGL()
egl = FusionEGL()
egl.init(surface, shareContext)
handler.post {
egl.bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FrameBuffer : Ref() {
var texture = 0
var frameBuffer = 0
var retain = false
var hasExternalTexture = false

/**
*
Expand Down Expand Up @@ -86,15 +87,19 @@ class FrameBuffer : Ref() {
* 减少引用计数,当引用计数为0时放回FrameBufferCache
*
*/
override fun releaseRef() {
super.releaseRef()
if (refCount <= 0 && !retain) {
FrameBufferCache.releaseFrameBuffer(this)
override fun decreaseRef() {
if (!retain) {
super.decreaseRef()
if (refCount <= 0) {
FrameBufferCache.releaseFrameBuffer(this)
}
}
}

fun release() {
GLUtil.deleteTexture(texture)
if (!hasExternalTexture) {
GLUtil.deleteTexture(texture)
}
GLUtil.deleteFrameBuffer(frameBuffer)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.kenneycode.fusion.framebuffer

import java.util.HashMap
import java.util.LinkedList
import io.github.kenneycode.fusion.common.Size

/**
Expand Down Expand Up @@ -40,7 +39,7 @@ object FrameBufferCache {
if (cache[tid]!![size]!!.isEmpty()) {
cache[tid]!![size]!!.add(FrameBuffer())
} else {
cache[tid]!![size]!!.first().addRef()
cache[tid]!![size]!!.first().increaseRef()
}
return cache[tid]!![size]!!.removeAt(0)
}
Expand All @@ -64,7 +63,7 @@ object FrameBufferCache {
cache.entries.forEach {
it.value.entries.forEach { frameBufferMaps ->
frameBufferMaps.value.forEach { frameBuffer ->
frameBuffer.releaseRef()
frameBuffer.decreaseRef()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.view.TextureView
import java.util.LinkedList

import io.github.kenneycode.fusion.common.FusionGLView
import io.github.kenneycode.fusion.context.GLThread
import io.github.kenneycode.fusion.context.FusionGLThread
import io.github.kenneycode.fusion.framebuffer.FrameBuffer
import io.github.kenneycode.fusion.renderer.DisplayRenderer

Expand All @@ -25,7 +25,7 @@ import io.github.kenneycode.fusion.renderer.DisplayRenderer

class FusionGLTextureView : TextureView, FusionGLView {

private var glThread: GLThread? = null
private var glThread: FusionGLThread? = null
private var displayRenderer = DisplayRenderer()
private val pendingTasks = LinkedList<() -> Unit>()
private var surfaceWidth = 0
Expand Down Expand Up @@ -54,7 +54,7 @@ class FusionGLTextureView : TextureView, FusionGLView {
override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture?, width: Int, height: Int) {
surfaceWidth = width
surfaceHeight = height
glThread = GLThread().apply {
glThread = FusionGLThread().apply {
surface = Surface(surfaceTexture)
init(surface)
pendingTasks.forEach { task ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.kenneycode.fusion.parameter

import android.opengl.GLES30

/**
*
* Coded by kenney
*
* http://www.github.com/kenneycode/fusion
*
* 4*4 Matrix 参数
*
*/

class Mat4Parameter(key : String, private var value : FloatArray) : Parameter(key) {

override fun bindUniform(program: Int) {
if (location < 0) {
location = GLES30.glGetUniformLocation(program, key)
}
GLES30.glUniformMatrix4fv(location, 1, false, value, 0)
}

override fun updateValue(value: Any) {
this.value = value as FloatArray
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.kenneycode.fusion.parameter

import android.opengl.GLES11Ext
import android.opengl.GLES30

/**
*
* Coded by kenney
*
* http://www.github.com/kenneycode/fusion
*
* OES纹理参数
*
*/

class OESTextureParameter(key : String, private var value : Int) : Parameter(key) {

override fun bindUniform(program: Int) {
if (location < 0) {
location = GLES30.glGetUniformLocation(program, key)
}
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, value)
GLES30.glUniform1i(location, 0)
}

override fun updateValue(value: Any) {
this.value = value as Int
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ class RenderGraph(private val rootRenderer: Renderer) : Renderer {
node.outputTarget.onInputReady(node.input)
}
}
node.input.clear()
if (node.nextNodes.isNotEmpty()) {
output.addRef(node.nextNodes.size - 1)
output.increaseRef(node.nextNodes.size - 1)
}
node.nextNodes.forEach { nextNode ->
nextNode.input.add(output)
Expand Down Expand Up @@ -261,8 +262,8 @@ class RenderGraph(private val rootRenderer: Renderer) : Renderer {
private open inner class Node(var layer: Int = 0) {

var needRender = true
var input: MutableList<FrameBuffer> = ArrayList()
var nextNodes: MutableList<Node> = ArrayList()
var input = mutableListOf<FrameBuffer>()
var nextNodes= mutableListOf<Node>()

fun addNext(nextNode: Node) {
nextNodes.add(nextNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class GLProgram(val shader: Shader) : Ref() {
* 减少引用计数,当引用计数为0时放回GLProgramCache
*
*/
override fun releaseRef() {
super.releaseRef()
override fun decreaseRef() {
super.decreaseRef()
if (refCount == 0) {
GLProgramCache.releaseGLProgram(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object GLProgramCache {
if (!cache.containsKey(shader)) {
cache[shader] = GLProgram(shader)
} else {
cache[shader]!!.addRef()
cache[shader]!!.increaseRef()
}
return cache[shader]!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.opengl.GLES20
import io.github.kenneycode.fusion.framebuffer.FrameBufferCache

import android.opengl.GLES20.glViewport
import io.github.kenneycode.fusion.common.DataKeys

/**
*
Expand All @@ -17,13 +18,6 @@ import android.opengl.GLES20.glViewport

class DisplayRenderer : SimpleRenderer() {

companion object {

const val KEY_DISPLAY_WIDTH = "KEY_DISPLAY_WIDTH"
const val KEY_DISPLAY_HEIGHT = "KEY_DISPLAY_HEIGHT"

}

private var displayWidth: Int = 0
private var displayHeight: Int = 0

Expand All @@ -33,7 +27,7 @@ class DisplayRenderer : SimpleRenderer() {
}

override fun update(data: MutableMap<String, Any>): Boolean {
setDisplaySize(data[KEY_DISPLAY_WIDTH] as Int, data[KEY_DISPLAY_HEIGHT] as Int)
setDisplaySize(data[DataKeys.KEY_DISPLAY_WIDTH] as Int, data[DataKeys.KEY_DISPLAY_HEIGHT] as Int)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ interface GLRenderer : Renderer {
*/
fun setUniformTexture2D(key: String, value: Int)

/**
*
* 设置OES纹理参数
*
* @param key 纹理参数名
* @param value 纹理id
*
*/
fun setUniformOESTexture(key: String, value: Int)

/**
*
* 设置4*4 Matrix参数
*
* @param key 纹理参数名
* @param value 4*4 Matrix
*
*/
fun setUniformMat4(key: String, value: FloatArray)

/**
*
* 绑定输入
Expand Down
Loading

0 comments on commit 8553ccd

Please sign in to comment.