|
1 | 1 | package com.github.triniwiz.canvas
|
2 | 2 |
|
3 | 3 | import android.graphics.Bitmap
|
| 4 | +import android.graphics.SurfaceTexture |
4 | 5 | import android.opengl.GLES20
|
5 | 6 | import android.os.Build
|
6 | 7 | import android.os.Environment
|
7 | 8 | import java.io.File
|
8 |
| -import java.nio.ByteBuffer |
| 9 | +import java.util.concurrent.CountDownLatch |
9 | 10 |
|
10 | 11 | object Utils {
|
11 | 12 |
|
12 | 13 | @JvmStatic
|
13 |
| - private external fun nativeGetBytesFromBitmap(bitmap: Bitmap?): ByteArray |
| 14 | + private external fun nativeGetByteBufferFromBitmap(bitmap: Bitmap?): ByteArray |
14 | 15 | private var rating = -1
|
15 | 16 | val isEmulator: Boolean
|
16 | 17 | get() {
|
@@ -112,6 +113,105 @@ object Utils {
|
112 | 113 | }
|
113 | 114 |
|
114 | 115 | 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 | + |
116 | 216 | }
|
117 | 217 | }
|
0 commit comments