-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
GlKernel.kt
259 lines (233 loc) · 8.51 KB
/
GlKernel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.haishinkit.graphics.gles
import android.opengl.GLES10
import android.opengl.GLES11Ext
import android.opengl.GLES20
import android.util.Size
import android.view.Surface
import com.haishinkit.graphics.ImageOrientation
import com.haishinkit.graphics.ResampleFilter
import com.haishinkit.graphics.VideoGravity
import com.haishinkit.lang.Utilize
import com.haishinkit.util.aspectRatio
import com.haishinkit.util.swap
import javax.microedition.khronos.opengles.GL10
internal class GlKernel(
override var utilizable: Boolean = false
) : Utilize {
var surface: Surface? = null
set(value) {
field = value
if (value == null) {
tearDown()
} else {
setUp()
}
}
var imageOrientation: ImageOrientation = ImageOrientation.UP
set(value) {
field = value
invalidateLayout = true
}
var videoGravity: VideoGravity = VideoGravity.RESIZE_ASPECT
set(value) {
field = value
invalidateLayout = true
}
var extent: Size = Size(0, 0)
set(value) {
field = value
invalidateLayout = true
}
var resampleFilter: ResampleFilter = ResampleFilter.NEAREST
private val inputSurfaceWindow: GlWindowSurface = GlWindowSurface()
private val vertexBuffer = GlUtil.createFloatBuffer(VERTECES)
private val texCoordBuffer = GlUtil.createFloatBuffer(TEX_COORDS_ROTATION_0)
private var program = INVALID_VALUE
private var positionHandle = INVALID_VALUE
private var texCoordHandle = INVALID_VALUE
private var textureHandle = INVALID_VALUE
private var invalidateLayout = true
override fun setUp() {
if (utilizable) return
inputSurfaceWindow.setUp(surface, null)
inputSurfaceWindow.makeCurrent()
GLES20.glTexParameteri(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MIN_FILTER,
resampleFilter.glValue
)
GLES20.glTexParameteri(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MAG_FILTER,
resampleFilter.glValue
)
GLES20.glTexParameteri(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE
)
GLES20.glTexParameteri(
GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_T,
GL10.GL_CLAMP_TO_EDGE
)
program = GlUtil.createProgram(GlShader.VERTEX, GlShader.FRAGMENT)
positionHandle = GLES20.glGetAttribLocation(program, "position")
GLES20.glEnableVertexAttribArray(positionHandle)
texCoordHandle = GLES20.glGetAttribLocation(program, "texcoord")
GLES20.glEnableVertexAttribArray(texCoordHandle)
textureHandle = GLES20.glGetAttribLocation(program, "texture")
utilizable = true
}
override fun tearDown() {
if (!utilizable) return
inputSurfaceWindow.tearDown()
program = INVALID_VALUE
positionHandle = INVALID_VALUE
texCoordHandle = INVALID_VALUE
texCoordHandle = INVALID_VALUE
utilizable = false
}
fun render(textureId: Int, textureSize: Size, matrix: FloatArray) {
if (invalidateLayout) {
layout(textureSize)
invalidateLayout = false
}
GLES20.glUseProgram(program)
GLES20.glVertexAttribPointer(texCoordHandle, 2, GLES20.GL_FLOAT, false, 0, texCoordBuffer)
GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer)
GlUtil.checkGlError("glVertexAttribPointer")
GLES20.glUniform1i(textureHandle, 0)
GlUtil.checkGlError("glUniform1i")
GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId)
GlUtil.checkGlError("glBindTexture")
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
GLES20.glUseProgram(0)
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0)
inputSurfaceWindow.swapBuffers()
}
private fun layout(newTextureSize: Size) {
var swapped = false
GLES10.glOrthof(
0.0f,
extent.width.toFloat(),
extent.height.toFloat(),
0.0f,
-1.0f,
1.0f
)
if (extent.width < extent.height) {
// portrait
when (imageOrientation) {
ImageOrientation.UP, ImageOrientation.UP_MIRRORED -> {
texCoordBuffer.put(TEX_COORDS_ROTATION_0)
}
ImageOrientation.LEFT, ImageOrientation.LEFT_MIRRORED -> {
swapped = true
texCoordBuffer.put(TEX_COORDS_ROTATION_270)
}
ImageOrientation.DOWN, ImageOrientation.DOWN_MIRRORED -> {
texCoordBuffer.put(TEX_COORDS_ROTATION_180)
}
ImageOrientation.RIGHT, ImageOrientation.RIGHT_MIRRORED -> {
swapped = true
texCoordBuffer.put(TEX_COORDS_ROTATION_90)
}
}
} else {
// landscape
when (imageOrientation) {
ImageOrientation.UP, ImageOrientation.UP_MIRRORED -> {
swapped = true
texCoordBuffer.put(TEX_COORDS_ROTATION_90)
}
ImageOrientation.LEFT, ImageOrientation.LEFT_MIRRORED -> {
texCoordBuffer.put(TEX_COORDS_ROTATION_0)
}
ImageOrientation.DOWN, ImageOrientation.DOWN_MIRRORED -> {
swapped = true
texCoordBuffer.put(TEX_COORDS_ROTATION_270)
}
ImageOrientation.RIGHT, ImageOrientation.RIGHT_MIRRORED -> {
texCoordBuffer.put(TEX_COORDS_ROTATION_180)
}
}
}
texCoordBuffer.position(0)
val textureSize = newTextureSize.swap(swapped)
when (videoGravity) {
VideoGravity.RESIZE_ASPECT -> {
val xRatio = extent.width.toFloat() / textureSize.width.toFloat()
val yRatio = extent.height.toFloat() / textureSize.height.toFloat()
if (yRatio < xRatio) {
GLES20.glViewport(
((extent.width - textureSize.width * yRatio) / 2).toInt(),
0,
(textureSize.width * yRatio).toInt(),
extent.height
)
} else {
GLES20.glViewport(
0,
((extent.height - textureSize.height * xRatio) / 2).toInt(),
extent.width,
(textureSize.height * xRatio).toInt()
)
}
}
VideoGravity.RESIZE_ASPECT_FILL -> {
val iRatio = extent.aspectRatio
val fRatio = textureSize.aspectRatio
if (iRatio < fRatio) {
GLES20.glViewport(
((extent.width - extent.height * fRatio) / 2).toInt(),
0,
(extent.height * fRatio).toInt(),
extent.height
)
} else {
GLES20.glViewport(
0,
((extent.height - extent.width / fRatio) / 2).toInt(),
extent.width,
(extent.width / fRatio).toInt()
)
}
}
}
}
companion object {
private const val INVALID_VALUE = 0
private val VERTECES = floatArrayOf(
-1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f
)
private val TEX_COORDS_ROTATION_0 = floatArrayOf(
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f
)
private val TEX_COORDS_ROTATION_90 = floatArrayOf(
1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
)
private val TEX_COORDS_ROTATION_180 = floatArrayOf(
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f
)
private val TEX_COORDS_ROTATION_270 = floatArrayOf(
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
)
}
}