Skip to content

Commit 8553ccd

Browse files
committed
support more parameters, add OES convert renderer and fix bug
1 parent 2cb90ba commit 8553ccd

20 files changed

Lines changed: 256 additions & 52 deletions

File tree

app/src/main/java/io/github/kenneycode/fusion/demo/fragment/SampleGLSurfaceViewUsage0.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
88
import androidx.fragment.app.Fragment
9+
import io.github.kenneycode.fusion.common.DataKeys
910
import io.github.kenneycode.fusion.demo.R
1011
import io.github.kenneycode.fusion.demo.Util
1112
import io.github.kenneycode.fusion.framebuffer.FrameBufferCache
@@ -49,8 +50,8 @@ class SampleGLSurfaceViewUsage0 : Fragment() {
4950

5051
// 执行渲染
5152
renderGraph.update(mutableMapOf(
52-
DisplayRenderer.KEY_DISPLAY_WIDTH to surfaceWidth,
53-
DisplayRenderer.KEY_DISPLAY_HEIGHT to surfaceHeight
53+
DataKeys.KEY_DISPLAY_WIDTH to surfaceWidth,
54+
DataKeys.KEY_DISPLAY_HEIGHT to surfaceHeight
5455
))
5556
renderGraph.render()
5657

libfusion/src/main/java/io/github/kenneycode/fusion/common/Constants.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,43 @@ class Constants {
1414

1515
companion object {
1616

17-
val COMMON_VERTEX_SHADER = "precision mediump float;\n" +
17+
val COMMON_VERTEX_SHADER =
18+
"precision mediump float;\n" +
1819
"attribute vec4 a_position;\n" +
20+
"attribute vec2 a_textureCoordinate;\n" +
21+
"varying vec2 v_textureCoordinate;\n" +
1922
"void main() {\n" +
23+
" v_textureCoordinate = a_textureCoordinate;\n" +
2024
" gl_Position = a_position;\n" +
2125
"}"
2226

23-
val COMMON_FRAGMENT_SHADER = "precision mediump float;\n" +
27+
val COMMON_FRAGMENT_SHADER =
28+
"precision mediump float;\n" +
29+
"varying vec2 v_textureCoordinate;\n" +
30+
"uniform sampler2D u_texture;\n" +
2431
"void main() {\n" +
25-
" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" +
32+
" gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" +
2633
"}"
2734

28-
val COMMON_VERTEX_SHADER_2 = "precision mediump float;\n" +
35+
val OES_VERTEX_SHADER =
36+
"precision mediump float;\n" +
2937
"attribute vec4 a_position;\n" +
30-
"attribute vec2 a_textureCoordinate;\n" +
38+
"attribute vec4 a_textureCoordinate;\n" +
3139
"varying vec2 v_textureCoordinate;\n" +
40+
"uniform mat4 u_stMatrix;\n" +
3241
"void main() {\n" +
33-
" v_textureCoordinate = a_textureCoordinate;\n" +
42+
" v_textureCoordinate = (u_stMatrix * a_textureCoordinate).xy;\n" +
3443
" gl_Position = a_position;\n" +
3544
"}"
3645

37-
val COMMON_FRAGMENT_SHADER_2 = "precision mediump float;\n" +
46+
val OES_FRAGMENT_SHADER =
47+
"#extension GL_OES_EGL_image_external : require\n" +
3848
"varying vec2 v_textureCoordinate;\n" +
39-
"uniform sampler2D u_texture;\n" +
49+
"uniform samplerExternalOES u_texture;\n" +
4050
"void main() {\n" +
41-
" gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" +
51+
" gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" +
4252
"}"
4353

44-
4554
val COMMON_VERTEX = floatArrayOf(-1f, -1f, -1f, 1f, 1f, 1f, -1f, -1f, 1f, 1f, 1f, -1f)
4655
val COMMON_VERTEX_FLIP_X = floatArrayOf(1f, -1f, 1f, 1f, -1f, 1f, 1f, -1f, -1f, 1f, -1f, -1f)
4756
val COMMON_VERTEX_FLIP_Y = floatArrayOf(-1f, 1f, -1f, -1f, 1f, -1f, -1f, 1f, 1f, -1f, 1f, 1f)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.kenneycode.fusion.common
2+
3+
class DataKeys {
4+
5+
companion object {
6+
7+
const val ST_MATRIX = "ST_MATRIX"
8+
const val KEY_DISPLAY_WIDTH = "KEY_DISPLAY_WIDTH"
9+
const val KEY_DISPLAY_HEIGHT = "KEY_DISPLAY_HEIGHT"
10+
}
11+
12+
}

libfusion/src/main/java/io/github/kenneycode/fusion/common/Ref.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open class Ref {
2121
* @param count 要增加的引用计数
2222
*
2323
*/
24-
fun addRef(count: Int = 1) {
24+
fun increaseRef(count: Int = 1) {
2525
refCount += count
2626
}
2727

@@ -30,7 +30,7 @@ open class Ref {
3030
* 减少1个引用计数
3131
*
3232
*/
33-
open fun releaseRef() {
33+
open fun decreaseRef() {
3434
--refCount
3535
}
3636

libfusion/src/main/java/io/github/kenneycode/fusion/context/CommonGLContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package io.github.kenneycode.fusion.context
1212

1313
class SimpleGLContext : GLContext {
1414

15-
private val glThread = GLThread()
15+
private val glThread = FusionGLThread()
1616

1717
init {
1818
glThread.init()

libfusion/src/main/java/io/github/kenneycode/fusion/context/EGL.kt renamed to libfusion/src/main/java/io/github/kenneycode/fusion/context/FusionEGL.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import android.view.Surface
1616
*
1717
*/
1818

19-
class EGL {
19+
class FusionEGL {
2020

2121
var eglContext = EGL14.EGL_NO_CONTEXT
2222

libfusion/src/main/java/io/github/kenneycode/fusion/context/GLThread.kt renamed to libfusion/src/main/java/io/github/kenneycode/fusion/context/FusionGLThread.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import java.util.concurrent.Semaphore
1717
*
1818
*/
1919

20-
class GLThread {
20+
class FusionGLThread {
2121

22-
private val handlerThread : HandlerThread = HandlerThread("GLThread")
22+
private val handlerThread : HandlerThread = HandlerThread("FusionGLThread")
2323
private lateinit var handler : Handler
24-
lateinit var egl : EGL
24+
lateinit var egl : FusionEGL
2525

2626
/**
2727
*
@@ -34,7 +34,7 @@ class GLThread {
3434
fun init(surface: Surface? = null, shareContext: EGLContext = EGL14.EGL_NO_CONTEXT) {
3535
handlerThread.start()
3636
handler = Handler(handlerThread.looper)
37-
egl = EGL()
37+
egl = FusionEGL()
3838
egl.init(surface, shareContext)
3939
handler.post {
4040
egl.bind()

libfusion/src/main/java/io/github/kenneycode/fusion/framebuffer/FrameBuffer.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class FrameBuffer : Ref() {
3131
var texture = 0
3232
var frameBuffer = 0
3333
var retain = false
34+
var hasExternalTexture = false
3435

3536
/**
3637
*
@@ -86,15 +87,19 @@ class FrameBuffer : Ref() {
8687
* 减少引用计数,当引用计数为0时放回FrameBufferCache
8788
*
8889
*/
89-
override fun releaseRef() {
90-
super.releaseRef()
91-
if (refCount <= 0 && !retain) {
92-
FrameBufferCache.releaseFrameBuffer(this)
90+
override fun decreaseRef() {
91+
if (!retain) {
92+
super.decreaseRef()
93+
if (refCount <= 0) {
94+
FrameBufferCache.releaseFrameBuffer(this)
95+
}
9396
}
9497
}
9598

9699
fun release() {
97-
GLUtil.deleteTexture(texture)
100+
if (!hasExternalTexture) {
101+
GLUtil.deleteTexture(texture)
102+
}
98103
GLUtil.deleteFrameBuffer(frameBuffer)
99104
}
100105

libfusion/src/main/java/io/github/kenneycode/fusion/framebuffer/FrameBufferCache.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.kenneycode.fusion.framebuffer
22

33
import java.util.HashMap
4-
import java.util.LinkedList
54
import io.github.kenneycode.fusion.common.Size
65

76
/**
@@ -40,7 +39,7 @@ object FrameBufferCache {
4039
if (cache[tid]!![size]!!.isEmpty()) {
4140
cache[tid]!![size]!!.add(FrameBuffer())
4241
} else {
43-
cache[tid]!![size]!!.first().addRef()
42+
cache[tid]!![size]!!.first().increaseRef()
4443
}
4544
return cache[tid]!![size]!!.removeAt(0)
4645
}
@@ -64,7 +63,7 @@ object FrameBufferCache {
6463
cache.entries.forEach {
6564
it.value.entries.forEach { frameBufferMaps ->
6665
frameBufferMaps.value.forEach { frameBuffer ->
67-
frameBuffer.releaseRef()
66+
frameBuffer.decreaseRef()
6867
}
6968
}
7069
}

libfusion/src/main/java/io/github/kenneycode/fusion/outputtarget/FusionGLTextureView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import android.view.TextureView
99
import java.util.LinkedList
1010

1111
import io.github.kenneycode.fusion.common.FusionGLView
12-
import io.github.kenneycode.fusion.context.GLThread
12+
import io.github.kenneycode.fusion.context.FusionGLThread
1313
import io.github.kenneycode.fusion.framebuffer.FrameBuffer
1414
import io.github.kenneycode.fusion.renderer.DisplayRenderer
1515

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

2626
class FusionGLTextureView : TextureView, FusionGLView {
2727

28-
private var glThread: GLThread? = null
28+
private var glThread: FusionGLThread? = null
2929
private var displayRenderer = DisplayRenderer()
3030
private val pendingTasks = LinkedList<() -> Unit>()
3131
private var surfaceWidth = 0
@@ -54,7 +54,7 @@ class FusionGLTextureView : TextureView, FusionGLView {
5454
override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture?, width: Int, height: Int) {
5555
surfaceWidth = width
5656
surfaceHeight = height
57-
glThread = GLThread().apply {
57+
glThread = FusionGLThread().apply {
5858
surface = Surface(surfaceTexture)
5959
init(surface)
6060
pendingTasks.forEach { task ->

0 commit comments

Comments
 (0)