Skip to content

Commit 9e322fa

Browse files
committed
增加GLTextureView(add GLTextureView)
1 parent 96c88f8 commit 9e322fa

File tree

9 files changed

+232
-3
lines changed

9 files changed

+232
-3
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<activity android:name=".SampleBindGLThreadToSurfaceView"/>
2525
<activity android:name=".SampleBindGLThreadToTextureView"/>
2626
<activity android:name=".SampleGLThreadShareContext"/>
27+
<activity android:name=".SampleGLTextureView"/>
2728
</application>
2829

2930
</manifest>

app/src/main/java/io/github/kenneycode/glkit/demo/MainActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.view.LayoutInflater
99
import android.view.View
1010
import android.view.ViewGroup
1111
import android.widget.Button
12+
import io.github.kenneycode.glkit.GLUtils
1213
import kotlinx.android.synthetic.main.activity_main.*
1314

1415
/**
@@ -24,7 +25,7 @@ class MainActivity : Activity() {
2425
override fun onCreate(savedInstanceState: Bundle?) {
2526
super.onCreate(savedInstanceState)
2627
setContentView(R.layout.activity_main)
27-
28+
val texture = GLUtils.createTexture()
2829
val sampleItems = arrayOf(
2930
SampleItem(getString(R.string.sample_egl_basic_usage), SampleEGLBasicUsage::class.java),
3031
SampleItem(getString(R.string.sample_bind_egl_to_surfaceview), SampleBindEGLToSurfaceView::class.java),
@@ -33,7 +34,8 @@ class MainActivity : Activity() {
3334
SampleItem(getString(R.string.sample_glthread_basic_usage), SampleGLThreadBasicUsage::class.java),
3435
SampleItem(getString(R.string.sample_bind_glthread_to_surfaceview), SampleBindEGLToSurfaceView::class.java),
3536
SampleItem(getString(R.string.sample_bind_glthread_to_textureview), SampleBindEGLToTextureView::class.java),
36-
SampleItem(getString(R.string.sample_glthread_share_context), SampleGLThreadShareContext::class.java)
37+
SampleItem(getString(R.string.sample_glthread_share_context), SampleGLThreadShareContext::class.java),
38+
SampleItem(getString(R.string.sample_gltextureview), SampleGLTextureView::class.java)
3739
)
3840

3941
val layoutManager = LinearLayoutManager(this)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.github.kenneycode.glkit.demo
2+
3+
import android.app.Activity
4+
import android.opengl.GLES30
5+
import android.os.Bundle
6+
import io.github.kenneycode.glkit.GLTextureView
7+
import io.github.kenneycode.glkit.GLUtils
8+
import kotlinx.android.synthetic.main.activity_sample_gltextureview.*
9+
10+
/**
11+
*
12+
* Coded by kenney
13+
*
14+
* http://www.github.com/kenneycode
15+
*
16+
**/
17+
18+
class SampleGLTextureView : Activity() {
19+
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
setContentView(R.layout.activity_sample_gltextureview)
23+
title = intent.getStringExtra(Constants.KEY_SAMPLE_NAME)
24+
25+
glTextureView.callback = object : GLTextureView.Callback {
26+
27+
var testTexture = 0
28+
29+
override fun onInit() {
30+
testTexture = GLUtils.createTexture()
31+
assert(GLES30.glIsTexture(testTexture))
32+
}
33+
34+
override fun onRender(width: Int, height: Int) {
35+
GLES30.glClearColor(0f, 0f, 1f, 1f)
36+
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT)
37+
}
38+
39+
override fun onRelease() {
40+
GLUtils.deleteTexture(testTexture)
41+
}
42+
43+
}
44+
glTextureView.requestRender()
45+
46+
}
47+
48+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent" >
6+
7+
<io.github.kenneycode.glkit.GLTextureView
8+
android:id="@+id/glTextureView"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"/>
11+
12+
</FrameLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
<string name="sample_bind_glthread_to_surfaceview">GLThread与SurfaceView绑定\n(bind GLThread to SurfaceView)</string>
99
<string name="sample_bind_glthread_to_textureview">GLThread与TextureView绑定\n(bind GLThread to TextureView)</string>
1010
<string name="sample_glthread_share_context">GLThread共享Context\n(GLThread share context)</string>
11+
<string name="sample_gltextureview">GLTextureView\n(GLTextureView)</string>
1112
</resources>

libglkit/src/main/java/io/github/kenneycode/glkit/EGL.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package io.github.kenneycode.glkit
33
import android.opengl.EGL14
44
import android.opengl.EGLContext
55
import android.opengl.EGLExt
6-
import android.util.Log
76
import android.view.Surface
87

98
/**
9+
*
10+
* EGL相关操作封装类
1011
*
1112
* Coded by kenney
1213
*
@@ -26,6 +27,7 @@ class EGL {
2627
private var previousContext = EGL14.EGL_NO_CONTEXT
2728

2829
/**
30+
*
2931
* 初始化EGL (init EGL)
3032
*
3133
* @param surface 要绑定的surface (the surface to bind)
@@ -60,6 +62,7 @@ class EGL {
6062
}
6163

6264
/**
65+
*
6366
* 绑定EGL到调用线程 (bind EGL to calling thread)
6467
*
6568
*/
@@ -72,6 +75,7 @@ class EGL {
7275
}
7376

7477
/**
78+
*
7579
* 交换buffer,如果绑定了一个surface并希望渲染到这个surface上,
7680
* 则在渲染操作后需调用本方法
7781
*
@@ -85,6 +89,7 @@ class EGL {
8589
}
8690

8791
/**
92+
*
8893
* 将EGL与调用线程解绑并恢复之前的EGL
8994
* unbind EGL to the calling thread and restore the previous EGL
9095
*
@@ -94,6 +99,7 @@ class EGL {
9499
}
95100

96101
/**
102+
*
97103
* 释放资源
98104
* release resources
99105
*
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package io.github.kenneycode.glkit
2+
3+
import android.content.Context
4+
import android.graphics.SurfaceTexture
5+
import android.opengl.EGLContext
6+
import android.util.AttributeSet
7+
import android.view.Surface
8+
import android.view.TextureView
9+
10+
/**
11+
*
12+
* 自带GL环境的TextureView
13+
*
14+
* Coded by kenney
15+
*
16+
* http://www.github.com/kenneycode
17+
*
18+
**/
19+
20+
open class GLTextureView : TextureView {
21+
22+
protected var surfaceTextureWidth = 0
23+
protected var surfaceTextureHeight = 0
24+
protected var glThread: GLThread? = null
25+
26+
var callback: Callback? = null
27+
28+
constructor(context: Context?) : super(context)
29+
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
30+
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
31+
32+
init {
33+
surfaceTextureListener = object : SurfaceTextureListener {
34+
35+
private var surface: Surface? = null
36+
37+
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
38+
}
39+
40+
override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture?) {
41+
}
42+
43+
override fun onSurfaceTextureDestroyed(surfaceTexture: SurfaceTexture?): Boolean {
44+
return false
45+
}
46+
47+
override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int, height: Int) {
48+
surfaceTextureWidth = width
49+
surfaceTextureHeight = height
50+
surface = Surface(surfaceTexture)
51+
glThread = GLThread()
52+
glThread?.init(surface)
53+
glThread?.post(Runnable {
54+
callback?.onInit()
55+
requestRender()
56+
})
57+
}
58+
59+
}
60+
}
61+
62+
/**
63+
*
64+
* 请求执行一次渲染,调用后会回调Callback#onRender (Request a render operation, which will call Callback#onRender)
65+
*
66+
*/
67+
fun requestRender() {
68+
glThread?.post(Runnable {
69+
callback?.onRender(surfaceTextureWidth, surfaceTextureHeight)
70+
}, true)
71+
}
72+
73+
/**
74+
*
75+
* 在GL线程同步执行一个任务 (Execute a task synchronously on GL thread)
76+
*
77+
*/
78+
fun executeOnGLThread(r : Runnable) {
79+
glThread?.execute(r)
80+
}
81+
82+
/**
83+
*
84+
* 在GL线程异步执行一个任务 (Execute a task asynchronously on GL thread)
85+
*
86+
*/
87+
fun postToGLThread(r : Runnable) {
88+
glThread?.post(r)
89+
}
90+
91+
/**
92+
*
93+
* 获取EGLContext (obtain EGLContext)
94+
*
95+
*/
96+
fun getEGLContext(): EGLContext? {
97+
return glThread?.getEGLContext()
98+
}
99+
100+
override fun onDetachedFromWindow() {
101+
super.onDetachedFromWindow()
102+
glThread?.execute(Runnable {
103+
callback?.onRelease()
104+
})
105+
glThread?.release()
106+
}
107+
108+
/**
109+
*
110+
* 回调方法,均在GL线程回调 (The callbacks which are called on GL thread)
111+
*
112+
*/
113+
interface Callback {
114+
115+
/**
116+
*
117+
* 初化始回调 (Init callback)
118+
*
119+
*/
120+
fun onInit()
121+
122+
/**
123+
*
124+
* 渲染回调 (Render callback)
125+
*
126+
* @param width surface的宽度 (The width of surface)
127+
* @param height surface的高度 (The height of surface)
128+
*
129+
*/
130+
fun onRender(width: Int, height: Int)
131+
132+
/**
133+
*
134+
* 释放回调 (Release callback)
135+
*
136+
*/
137+
fun onRelease()
138+
}
139+
140+
}

libglkit/src/main/java/io/github/kenneycode/glkit/GLThread.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import android.view.Surface
88
import java.util.concurrent.Semaphore
99

1010
/**
11+
*
12+
* 带有EGL环境的线程封装
1113
*
1214
* Coded by kenney
1315
*
@@ -22,6 +24,7 @@ class GLThread {
2224
lateinit var egl : EGL
2325

2426
/**
27+
*
2528
* 初始化GLThread (init GLThread)
2629
*
2730
* @param surface 要绑定的surface (the surface to bind)
@@ -39,6 +42,7 @@ class GLThread {
3942
}
4043

4144
/**
45+
*
4246
* 在GLThread异步执行一个任务 (asynchronously run a task in GLThread)
4347
*
4448
* @param r 要执行的任务 (the task to run)
@@ -55,6 +59,7 @@ class GLThread {
5559
}
5660

5761
/**
62+
*
5863
* 在GLThread同步执行一个任务 (synchronously run a task in GLThread)
5964
*
6065
* @param r 要执行的任务 (the task to run)
@@ -74,6 +79,7 @@ class GLThread {
7479
}
7580

7681
/**
82+
*
7783
* 获取与GLThread绑定的EGL Context (obtain the EGL Context bound to the GLThread)
7884
*
7985
* @return 与GLThread绑定的EGL (the EGL Context bound to the GLThread)
@@ -85,6 +91,7 @@ class GLThread {
8591
}
8692

8793
/**
94+
*
8895
* 停止线程和释放资源
8996
* stop the thread and release resources
9097
*

0 commit comments

Comments
 (0)