Skip to content

Commit 84f3e63

Browse files
committed
feat: video support
1 parent d7c48de commit 84f3e63

File tree

1 file changed

+103
-3
lines changed
  • packages/canvas/src-native/canvas-android/canvas/src/main/java/com/github/triniwiz/canvas

1 file changed

+103
-3
lines changed

packages/canvas/src-native/canvas-android/canvas/src/main/java/com/github/triniwiz/canvas/Utils.kt

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.github.triniwiz.canvas
22

33
import android.graphics.Bitmap
4+
import android.graphics.SurfaceTexture
45
import android.opengl.GLES20
56
import android.os.Build
67
import android.os.Environment
78
import java.io.File
8-
import java.nio.ByteBuffer
9+
import java.util.concurrent.CountDownLatch
910

1011
object Utils {
1112

1213
@JvmStatic
13-
private external fun nativeGetBytesFromBitmap(bitmap: Bitmap?): ByteArray
14+
private external fun nativeGetByteBufferFromBitmap(bitmap: Bitmap?): ByteArray
1415
private var rating = -1
1516
val isEmulator: Boolean
1617
get() {
@@ -112,6 +113,105 @@ object Utils {
112113
}
113114

114115
fun getBytesFromBitmap(bitmap: Bitmap?): ByteArray {
115-
return nativeGetBytesFromBitmap(bitmap)
116+
return nativeGetByteBufferFromBitmap(bitmap)
117+
}
118+
119+
120+
@JvmStatic
121+
fun createSurfaceTexture(context: TNSWebGLRenderingContext): Array<Any> {
122+
val render = TextureRender()
123+
val lock = CountDownLatch(1)
124+
context.canvas.queueEvent {
125+
render.surfaceCreated()
126+
lock.countDown()
127+
}
128+
129+
try {
130+
lock.await()
131+
} catch (ignored: InterruptedException) {
132+
}
133+
134+
return arrayOf(SurfaceTexture(render.getTextureId()), render)
135+
136+
}
137+
138+
139+
@JvmStatic
140+
fun createRenderAndAttachToGLContext(
141+
context: TNSWebGLRenderingContext,
142+
texture: SurfaceTexture,
143+
): TextureRender {
144+
val render = TextureRender()
145+
val lock = CountDownLatch(1)
146+
context.canvas.queueEvent {
147+
render.surfaceCreated()
148+
texture.attachToGLContext(render.getTextureId())
149+
lock.countDown()
150+
}
151+
152+
try {
153+
lock.await()
154+
} catch (ignored: InterruptedException) {
155+
}
156+
return render
157+
}
158+
159+
160+
@JvmStatic
161+
fun attachToGLContext(
162+
context: TNSWebGLRenderingContext,
163+
texture: SurfaceTexture,
164+
render: TextureRender
165+
) {
166+
val lock = CountDownLatch(1)
167+
context.canvas.queueEvent {
168+
texture.attachToGLContext(render.getTextureId())
169+
lock.countDown()
170+
}
171+
172+
try {
173+
lock.await()
174+
} catch (ignored: InterruptedException) {
175+
}
176+
}
177+
178+
@JvmStatic
179+
fun detachFromGLContext(context: TNSWebGLRenderingContext, texture: SurfaceTexture) {
180+
val lock = CountDownLatch(1)
181+
context.canvas.queueEvent {
182+
texture.detachFromGLContext()
183+
lock.countDown()
184+
}
185+
186+
try {
187+
lock.await()
188+
} catch (ignored: InterruptedException) {
189+
}
190+
}
191+
192+
@JvmStatic
193+
fun updateTexImage(
194+
context: TNSWebGLRenderingContext,
195+
texture: SurfaceTexture,
196+
render: TextureRender
197+
) {
198+
val lock = CountDownLatch(1)
199+
context.runOnGLThread {
200+
val canvas = context.canvas
201+
texture.updateTexImage()
202+
canvas.mClearColor[0] = 0f
203+
canvas.mClearColor[1] = 0f
204+
canvas.mClearColor[2] = 0f
205+
canvas.mClearColor[3] = 1f
206+
render.drawFrame(texture)
207+
context.updateCanvas()
208+
lock.countDown()
209+
}
210+
211+
try {
212+
lock.await()
213+
} catch (ignored: InterruptedException) {
214+
}
215+
116216
}
117217
}

0 commit comments

Comments
 (0)