@blotch/angular-lynx-native 22.3.1 → 22.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -15
- package/dist/android/project.d.ts +114 -1
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +440 -5
- package/dist/eject.d.ts +15 -1
- package/dist/eject.d.ts.map +1 -1
- package/dist/eject.js +14 -2
- package/dist/features.d.ts +70 -0
- package/dist/features.d.ts.map +1 -0
- package/dist/features.js +115 -0
- package/dist/files.d.ts +24 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +41 -0
- package/dist/generate.d.ts +34 -3
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +78 -19
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -2
- package/dist/ios/build.d.ts.map +1 -1
- package/dist/ios/build.js +8 -2
- package/dist/ios/frameworks.d.ts +6 -5
- package/dist/ios/frameworks.d.ts.map +1 -1
- package/dist/ios/frameworks.js +46 -15
- package/dist/ios/project.d.ts +192 -11
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +823 -33
- package/dist/options.d.ts +208 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +154 -0
- package/dist/permissions.d.ts +18 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +79 -4
- package/dist/plugins.d.ts +245 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +1244 -0
- package/package.json +1 -1
- package/templates/android/README.md +46 -5
- package/templates/android/angular-lynx.properties +2 -0
- package/templates/android/app/build.gradle.kts +78 -11
- package/templates/android/app/src/main/AndroidManifest.xml +19 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +523 -9
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +35 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +355 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +7 -8
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +58 -4
- package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
- package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
- package/templates/android/app/src/main/res/values/share_target.xml +11 -0
- package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
- package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
- package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
- package/templates/android/build.gradle.kts +1 -1
- package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
- package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
- package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
- package/templates/ios/App/AngularLynxPlugins.swift +22 -0
- package/templates/ios/App/AngularLynxVideo.h +37 -0
- package/templates/ios/App/AngularLynxVideo.m +435 -0
- package/templates/ios/App/AngularLynxWebGL.h +69 -7
- package/templates/ios/App/AngularLynxWebGL.m +538 -29
- package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
- package/templates/ios/App/AnimationFrameRate.swift +66 -0
- package/templates/ios/App/AppDelegate.swift +37 -3
- package/templates/ios/App/AppleSignInButton.swift +106 -0
- package/templates/ios/App/AppleSignInModule.swift +222 -0
- package/templates/ios/App/AudioRecorderModule.swift +433 -0
- package/templates/ios/App/BarcodeScannerModule.swift +284 -0
- package/templates/ios/App/BiometricsModule.swift +256 -0
- package/templates/ios/App/BluetoothModule.swift +1086 -0
- package/templates/ios/App/CalendarModule.swift +158 -0
- package/templates/ios/App/FilePickerModule.swift +240 -0
- package/templates/ios/App/GeolocationModule.swift +455 -0
- package/templates/ios/App/HapticsModule.swift +90 -0
- package/templates/ios/App/HostElementUI.swift +270 -0
- package/templates/ios/App/ImagePickerModule.swift +160 -1
- package/templates/ios/App/Info.plist +2 -0
- package/templates/ios/App/LynxViewController.swift +274 -9
- package/templates/ios/App/MotionModule.swift +430 -0
- package/templates/ios/App/NetworkStatus.swift +41 -0
- package/templates/ios/App/NfcModule.swift +346 -0
- package/templates/ios/App/PhotoLibraryModule.swift +169 -0
- package/templates/ios/App/ShareTargetModule.swift +150 -0
- package/templates/ios/App/ShellModule.swift +17 -0
- package/templates/ios/App/StoreReviewModule.swift +61 -0
- package/templates/ios/App/TrackingModule.swift +121 -0
- package/templates/ios/App/WifiModule.swift +237 -0
- package/templates/ios/App/XElementRegistry.swift +13 -3
- package/templates/ios/ShareExtension/Info.plist +42 -0
- package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.graphics.SurfaceTexture
|
|
4
|
+
import android.opengl.EGL14
|
|
5
|
+
import android.opengl.EGLContext
|
|
6
|
+
import android.opengl.GLES11Ext
|
|
7
|
+
import android.opengl.GLES20
|
|
8
|
+
import android.os.Handler
|
|
9
|
+
import android.os.HandlerThread
|
|
10
|
+
import android.view.Surface
|
|
11
|
+
import java.nio.ByteBuffer
|
|
12
|
+
import java.nio.ByteOrder
|
|
13
|
+
import java.nio.FloatBuffer
|
|
14
|
+
import java.util.concurrent.CountDownLatch
|
|
15
|
+
import java.util.concurrent.TimeUnit
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Somewhere for a `<video>`'s player to draw while its view has no surface,
|
|
19
|
+
* so a canvas can still read the video (issue #726).
|
|
20
|
+
*
|
|
21
|
+
* The player draws into its PlayerView's SurfaceView, which has a surface
|
|
22
|
+
* only while it's laid out with a size: with `display: none` it has none,
|
|
23
|
+
* and the player drops every frame rather than draw it, so there's nothing
|
|
24
|
+
* to copy. A browser decodes a hidden video all the same, and code that only
|
|
25
|
+
* reads one (three.js's VideoTexture, thumbnails, filters) often hides it.
|
|
26
|
+
* So while the view has no surface, VideoUI gives the player this target's
|
|
27
|
+
* surface instead: a SurfaceTexture the shell owns, which takes the
|
|
28
|
+
* decoder's frames whatever their format, as a TextureView's does, and keeps
|
|
29
|
+
* the latest one as a texture. A snapshot draws that texture into a
|
|
30
|
+
* framebuffer the frame's size and reads its pixels back.
|
|
31
|
+
*
|
|
32
|
+
* Every target lives on one thread of its own, with one EGL context that's
|
|
33
|
+
* always current there: SurfaceTexture must be latched on the thread whose
|
|
34
|
+
* context made its texture, and a context of the canvases' (WebGLContext) is
|
|
35
|
+
* made current on Lynx's engine thread, which this mustn't disturb. Nothing
|
|
36
|
+
* on the thread waits on another, so the engine thread, which waits on it
|
|
37
|
+
* for a snapshot, can't deadlock with it. The UI thread never waits on it: a
|
|
38
|
+
* target is made there and handed back.
|
|
39
|
+
*/
|
|
40
|
+
class VideoFrameTarget private constructor(
|
|
41
|
+
private val texture: Int,
|
|
42
|
+
private val surfaceTexture: SurfaceTexture,
|
|
43
|
+
) {
|
|
44
|
+
/** The surface the player draws into. */
|
|
45
|
+
val surface = Surface(surfaceTexture)
|
|
46
|
+
|
|
47
|
+
// Whether the latest frame is current: set on the target's thread, read
|
|
48
|
+
// on the engine thread (`videoWidth` asks), and cleared by a reset on
|
|
49
|
+
// the UI thread, under the lock so a latch can't set it back after.
|
|
50
|
+
private val readiness = Any()
|
|
51
|
+
@Volatile
|
|
52
|
+
private var hasImage = false
|
|
53
|
+
/**
|
|
54
|
+
* Resets that haven't reached the target's thread yet: a count, so the
|
|
55
|
+
* first of two can't clear the second's while a frame from between them
|
|
56
|
+
* is latched.
|
|
57
|
+
*/
|
|
58
|
+
private var resetsPending = 0
|
|
59
|
+
/** The timestamp of the frame the last reset forgot, which a latch with nothing new keeps. */
|
|
60
|
+
private var staleTimestamp = 0L
|
|
61
|
+
private var released = false
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Told on the target's thread each time a frame becomes current: the
|
|
65
|
+
* first one, or the first after a reset. The player saying it drew a
|
|
66
|
+
* frame doesn't mean one came (see VideoUI's frame check), so VideoUI
|
|
67
|
+
* waits for this before the app hears of it.
|
|
68
|
+
*/
|
|
69
|
+
@Volatile
|
|
70
|
+
var onFrame: (() -> Unit)? = null
|
|
71
|
+
|
|
72
|
+
private var framebuffer = 0
|
|
73
|
+
private var colorTexture = 0
|
|
74
|
+
private var framebufferWidth = 0
|
|
75
|
+
private var framebufferHeight = 0
|
|
76
|
+
|
|
77
|
+
init {
|
|
78
|
+
surfaceTexture.setOnFrameAvailableListener({ latch() }, handler)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Forgets the frame it has: the player is about to draw here after
|
|
83
|
+
* drawing somewhere else, or a new source starts, and until its next
|
|
84
|
+
* frame comes there's nothing current to copy.
|
|
85
|
+
*/
|
|
86
|
+
fun reset() {
|
|
87
|
+
// Not current from here on, though the thread may be busy: a latch
|
|
88
|
+
// it runs before the post below takes a frame from before the reset.
|
|
89
|
+
synchronized(readiness) {
|
|
90
|
+
resetsPending++
|
|
91
|
+
hasImage = false
|
|
92
|
+
}
|
|
93
|
+
handler.post {
|
|
94
|
+
synchronized(readiness) {
|
|
95
|
+
staleTimestamp = surfaceTexture.timestamp
|
|
96
|
+
resetsPending--
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Whether a frame has come since the last reset, as far as the target's
|
|
103
|
+
* thread has latched: for the UI thread, which mustn't wait for it.
|
|
104
|
+
*/
|
|
105
|
+
fun frameIsCurrent(): Boolean = hasImage
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether a frame has come since the last reset. A frame the player
|
|
109
|
+
* rendered may not have been latched yet (the listener's post can come
|
|
110
|
+
* after `bindfirstframe` reaches the app), so without one it latches
|
|
111
|
+
* whatever's queued, waiting a moment for the thread.
|
|
112
|
+
*/
|
|
113
|
+
fun hasFrame(): Boolean {
|
|
114
|
+
if (hasImage) return true
|
|
115
|
+
return onThread { latch(); hasImage } ?: false
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Reads the latest frame, drawn at `width` by `height` (the size the
|
|
120
|
+
* frame is shown at, stretched by its pixel aspect ratio), as RGBA rows
|
|
121
|
+
* top to bottom, and hands it to `done` on the target's thread, or null
|
|
122
|
+
* without one. It doesn't wait: the caller decides how long to, and a
|
|
123
|
+
* read that comes after the caller stopped waiting still hands its frame
|
|
124
|
+
* over, which VideoUI keeps for the next read that can't wait (issues
|
|
125
|
+
* #806 and #817).
|
|
126
|
+
*/
|
|
127
|
+
fun read(width: Int, height: Int, done: (DecodedImage?) -> Unit) {
|
|
128
|
+
handler.post {
|
|
129
|
+
done(
|
|
130
|
+
try {
|
|
131
|
+
readNow(width, height)
|
|
132
|
+
} catch (_: RuntimeException) {
|
|
133
|
+
null
|
|
134
|
+
},
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Lets go of the texture and the surface, once the player has let go of it. */
|
|
140
|
+
fun release() {
|
|
141
|
+
handler.post {
|
|
142
|
+
if (released) return@post
|
|
143
|
+
released = true
|
|
144
|
+
onFrame = null
|
|
145
|
+
surfaceTexture.setOnFrameAvailableListener(null)
|
|
146
|
+
surface.release()
|
|
147
|
+
surfaceTexture.release()
|
|
148
|
+
GLES20.glDeleteTextures(1, intArrayOf(texture), 0)
|
|
149
|
+
if (framebuffer != 0) {
|
|
150
|
+
GLES20.glDeleteFramebuffers(1, intArrayOf(framebuffer), 0)
|
|
151
|
+
GLES20.glDeleteTextures(1, intArrayOf(colorTexture), 0)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Takes the newest queued frame as the texture, on the target's thread. */
|
|
157
|
+
private fun latch() {
|
|
158
|
+
if (released) return
|
|
159
|
+
try {
|
|
160
|
+
surfaceTexture.updateTexImage()
|
|
161
|
+
} catch (_: RuntimeException) {
|
|
162
|
+
// The SurfaceTexture was abandoned (its producer went away badly):
|
|
163
|
+
// the last frame it latched, if any, is still the texture.
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
// A latch with nothing queued keeps the frame it had: it's current
|
|
167
|
+
// only if it came after the last reset. The player stamps each frame
|
|
168
|
+
// with the time it's released to be shown, so two never share one.
|
|
169
|
+
val timestamp = surfaceTexture.timestamp
|
|
170
|
+
val arrived = synchronized(readiness) {
|
|
171
|
+
val current = resetsPending == 0 && timestamp != 0L && timestamp != staleTimestamp
|
|
172
|
+
(current && !hasImage).also { if (current) hasImage = true }
|
|
173
|
+
}
|
|
174
|
+
if (arrived) onFrame?.invoke()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private fun readNow(width: Int, height: Int): DecodedImage? {
|
|
178
|
+
if (released || width <= 0 || height <= 0) return null
|
|
179
|
+
latch()
|
|
180
|
+
if (!hasImage) return null
|
|
181
|
+
// Errors an earlier call left would read as this one's.
|
|
182
|
+
for (i in 0 until 8) if (GLES20.glGetError() == GLES20.GL_NO_ERROR) break
|
|
183
|
+
if (!sizeFramebuffer(width, height)) return null
|
|
184
|
+
val matrix = FloatArray(16)
|
|
185
|
+
// The texture's transform: the decoder's crop, and the rotation it
|
|
186
|
+
// applies (a portrait recording), which a SurfaceView applies too.
|
|
187
|
+
surfaceTexture.getTransformMatrix(matrix)
|
|
188
|
+
val program = program()
|
|
189
|
+
if (program == 0) return null
|
|
190
|
+
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, framebuffer)
|
|
191
|
+
GLES20.glViewport(0, 0, width, height)
|
|
192
|
+
GLES20.glUseProgram(program)
|
|
193
|
+
GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
|
|
194
|
+
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture)
|
|
195
|
+
GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "frame"), 0)
|
|
196
|
+
GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(program, "transform"), 1, false, matrix, 0)
|
|
197
|
+
val position = GLES20.glGetAttribLocation(program, "position")
|
|
198
|
+
val coord = GLES20.glGetAttribLocation(program, "coord")
|
|
199
|
+
quad.position(0)
|
|
200
|
+
GLES20.glVertexAttribPointer(position, 2, GLES20.GL_FLOAT, false, 16, quad)
|
|
201
|
+
quad.position(2)
|
|
202
|
+
GLES20.glVertexAttribPointer(coord, 2, GLES20.GL_FLOAT, false, 16, quad)
|
|
203
|
+
GLES20.glEnableVertexAttribArray(position)
|
|
204
|
+
GLES20.glEnableVertexAttribArray(coord)
|
|
205
|
+
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
|
|
206
|
+
GLES20.glDisableVertexAttribArray(position)
|
|
207
|
+
GLES20.glDisableVertexAttribArray(coord)
|
|
208
|
+
val pixels = try {
|
|
209
|
+
ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.nativeOrder())
|
|
210
|
+
} catch (_: OutOfMemoryError) {
|
|
211
|
+
return null
|
|
212
|
+
}
|
|
213
|
+
// The quad puts the frame's top in the framebuffer's first row, which
|
|
214
|
+
// glReadPixels returns first, so the rows come top to bottom.
|
|
215
|
+
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixels)
|
|
216
|
+
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0)
|
|
217
|
+
if (GLES20.glGetError() != GLES20.GL_NO_ERROR) return null
|
|
218
|
+
pixels.rewind()
|
|
219
|
+
// A video frame is opaque, so premultiplied or not, its colors are the same.
|
|
220
|
+
return DecodedImage(pixels, width, height, true)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Makes the framebuffer the snapshot's size, keeping it while that holds. */
|
|
224
|
+
private fun sizeFramebuffer(width: Int, height: Int): Boolean {
|
|
225
|
+
if (framebuffer != 0 && width == framebufferWidth && height == framebufferHeight) return true
|
|
226
|
+
if (framebuffer == 0) {
|
|
227
|
+
val names = IntArray(1)
|
|
228
|
+
GLES20.glGenFramebuffers(1, names, 0)
|
|
229
|
+
framebuffer = names[0]
|
|
230
|
+
GLES20.glGenTextures(1, names, 0)
|
|
231
|
+
colorTexture = names[0]
|
|
232
|
+
}
|
|
233
|
+
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, colorTexture)
|
|
234
|
+
GLES20.glTexImage2D(
|
|
235
|
+
GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
|
|
236
|
+
GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null,
|
|
237
|
+
)
|
|
238
|
+
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR)
|
|
239
|
+
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, framebuffer)
|
|
240
|
+
GLES20.glFramebufferTexture2D(
|
|
241
|
+
GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, colorTexture, 0,
|
|
242
|
+
)
|
|
243
|
+
val complete = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER) == GLES20.GL_FRAMEBUFFER_COMPLETE
|
|
244
|
+
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0)
|
|
245
|
+
// A size the device can't make (past its largest texture) is tried
|
|
246
|
+
// again next time, rather than kept.
|
|
247
|
+
framebufferWidth = if (complete) width else 0
|
|
248
|
+
framebufferHeight = if (complete) height else 0
|
|
249
|
+
return complete
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
companion object {
|
|
253
|
+
/**
|
|
254
|
+
* How long the engine thread waits for the target's thread to say
|
|
255
|
+
* whether it has a frame (`videoWidth` asks). A latch takes well
|
|
256
|
+
* under a millisecond; one that doesn't come in time reads as no
|
|
257
|
+
* frame yet.
|
|
258
|
+
*/
|
|
259
|
+
private const val WAIT_MS = 250L
|
|
260
|
+
|
|
261
|
+
private val handler: Handler by lazy {
|
|
262
|
+
Handler(HandlerThread("AngularLynxVideoFrames").apply { start() }.looper)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The thread's EGL context, made on it the first time a target is and
|
|
267
|
+
* current there from then on, with the 1x1 pbuffer it needs.
|
|
268
|
+
*/
|
|
269
|
+
private var context: EGLContext = EGL14.EGL_NO_CONTEXT
|
|
270
|
+
private var programName = 0
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* A full-framebuffer strip: x, y, then the texture's u, v, with the
|
|
274
|
+
* frame's top (v = 1 in a SurfaceTexture's coordinates) at y = -1,
|
|
275
|
+
* the framebuffer's first row.
|
|
276
|
+
*/
|
|
277
|
+
private val quad: FloatBuffer = ByteBuffer.allocateDirect(16 * 4).order(ByteOrder.nativeOrder())
|
|
278
|
+
.asFloatBuffer().apply {
|
|
279
|
+
put(floatArrayOf(-1f, -1f, 0f, 1f, 1f, -1f, 1f, 1f, -1f, 1f, 0f, 0f, 1f, 1f, 1f, 0f))
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private const val VERTEX_SHADER = """
|
|
283
|
+
attribute vec2 position;
|
|
284
|
+
attribute vec2 coord;
|
|
285
|
+
uniform mat4 transform;
|
|
286
|
+
varying vec2 texCoord;
|
|
287
|
+
void main() {
|
|
288
|
+
gl_Position = vec4(position, 0.0, 1.0);
|
|
289
|
+
texCoord = (transform * vec4(coord, 0.0, 1.0)).xy;
|
|
290
|
+
}
|
|
291
|
+
"""
|
|
292
|
+
|
|
293
|
+
// High precision, so a wide frame's texture coordinates land on their
|
|
294
|
+
// own pixels: every GPU with OpenGL ES 3.0, which the shell's WebGL
|
|
295
|
+
// needs already, has it in fragment shaders.
|
|
296
|
+
private const val FRAGMENT_SHADER = """
|
|
297
|
+
#extension GL_OES_EGL_image_external : require
|
|
298
|
+
precision highp float;
|
|
299
|
+
uniform samplerExternalOES frame;
|
|
300
|
+
varying vec2 texCoord;
|
|
301
|
+
void main() {
|
|
302
|
+
gl_FragColor = texture2D(frame, texCoord);
|
|
303
|
+
}
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Makes a new target on its thread and hands it to `made` there, or
|
|
308
|
+
* null when the device has no OpenGL ES 2.0 to make one with (the
|
|
309
|
+
* video then draws nothing while it's hidden, as before). The first
|
|
310
|
+
* also starts EGL, which the UI thread shouldn't wait for.
|
|
311
|
+
*/
|
|
312
|
+
fun create(made: (VideoFrameTarget?) -> Unit) {
|
|
313
|
+
handler.post {
|
|
314
|
+
made(
|
|
315
|
+
try {
|
|
316
|
+
makeTarget()
|
|
317
|
+
} catch (_: RuntimeException) {
|
|
318
|
+
null
|
|
319
|
+
},
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private fun makeTarget(): VideoFrameTarget? {
|
|
325
|
+
if (!makeContext()) return null
|
|
326
|
+
// The program is compiled now, while nothing waits for the
|
|
327
|
+
// thread, rather than on the first read, which the engine thread
|
|
328
|
+
// waits for: on CI's software-drawn emulator, compiling it there
|
|
329
|
+
// was part of what made a first read miss its wait (issue #806).
|
|
330
|
+
program()
|
|
331
|
+
val names = IntArray(1)
|
|
332
|
+
GLES20.glGenTextures(1, names, 0)
|
|
333
|
+
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, names[0])
|
|
334
|
+
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR)
|
|
335
|
+
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR)
|
|
336
|
+
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
|
|
337
|
+
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
|
|
338
|
+
return VideoFrameTarget(names[0], SurfaceTexture(names[0]))
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Runs `block` on the targets' thread and returns what it does, or null if it took too long. */
|
|
342
|
+
private fun <T> onThread(block: () -> T?): T? {
|
|
343
|
+
if (handler.looper.isCurrentThread) return block()
|
|
344
|
+
val done = CountDownLatch(1)
|
|
345
|
+
var result: T? = null
|
|
346
|
+
handler.post {
|
|
347
|
+
result = try {
|
|
348
|
+
block()
|
|
349
|
+
} catch (_: RuntimeException) {
|
|
350
|
+
null
|
|
351
|
+
}
|
|
352
|
+
done.countDown()
|
|
353
|
+
}
|
|
354
|
+
return if (done.await(WAIT_MS, TimeUnit.MILLISECONDS)) result else null
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Makes the thread's context current, the first time; false if EGL can't. */
|
|
358
|
+
private fun makeContext(): Boolean {
|
|
359
|
+
if (context != EGL14.EGL_NO_CONTEXT) return true
|
|
360
|
+
val eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)
|
|
361
|
+
val version = IntArray(2)
|
|
362
|
+
if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) return false
|
|
363
|
+
val configs = arrayOfNulls<android.opengl.EGLConfig>(1)
|
|
364
|
+
val count = IntArray(1)
|
|
365
|
+
val attributes = intArrayOf(
|
|
366
|
+
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
|
|
367
|
+
EGL14.EGL_SURFACE_TYPE, EGL14.EGL_PBUFFER_BIT,
|
|
368
|
+
EGL14.EGL_RED_SIZE, 8,
|
|
369
|
+
EGL14.EGL_GREEN_SIZE, 8,
|
|
370
|
+
EGL14.EGL_BLUE_SIZE, 8,
|
|
371
|
+
EGL14.EGL_ALPHA_SIZE, 8,
|
|
372
|
+
EGL14.EGL_NONE,
|
|
373
|
+
)
|
|
374
|
+
if (!EGL14.eglChooseConfig(eglDisplay, attributes, 0, configs, 0, 1, count, 0) || count[0] == 0) return false
|
|
375
|
+
val config = configs[0] ?: return false
|
|
376
|
+
val eglContext = EGL14.eglCreateContext(
|
|
377
|
+
eglDisplay, config, EGL14.EGL_NO_CONTEXT,
|
|
378
|
+
intArrayOf(EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE), 0,
|
|
379
|
+
)
|
|
380
|
+
if (eglContext == EGL14.EGL_NO_CONTEXT) return false
|
|
381
|
+
// A context needs a surface to be current on; nothing draws to it.
|
|
382
|
+
val surface = EGL14.eglCreatePbufferSurface(
|
|
383
|
+
eglDisplay, config, intArrayOf(EGL14.EGL_WIDTH, 1, EGL14.EGL_HEIGHT, 1, EGL14.EGL_NONE), 0,
|
|
384
|
+
)
|
|
385
|
+
if (surface == EGL14.EGL_NO_SURFACE || !EGL14.eglMakeCurrent(eglDisplay, surface, surface, eglContext)) {
|
|
386
|
+
if (surface != EGL14.EGL_NO_SURFACE) EGL14.eglDestroySurface(eglDisplay, surface)
|
|
387
|
+
EGL14.eglDestroyContext(eglDisplay, eglContext)
|
|
388
|
+
return false
|
|
389
|
+
}
|
|
390
|
+
context = eglContext
|
|
391
|
+
return true
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** The program that draws a frame, compiled the first time; 0 if it can't be. */
|
|
395
|
+
private fun program(): Int {
|
|
396
|
+
if (programName != 0) return programName
|
|
397
|
+
val vertex = shader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER)
|
|
398
|
+
val fragment = shader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER)
|
|
399
|
+
if (vertex == 0 || fragment == 0) return 0
|
|
400
|
+
val program = GLES20.glCreateProgram()
|
|
401
|
+
GLES20.glAttachShader(program, vertex)
|
|
402
|
+
GLES20.glAttachShader(program, fragment)
|
|
403
|
+
GLES20.glLinkProgram(program)
|
|
404
|
+
GLES20.glDeleteShader(vertex)
|
|
405
|
+
GLES20.glDeleteShader(fragment)
|
|
406
|
+
val linked = IntArray(1)
|
|
407
|
+
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linked, 0)
|
|
408
|
+
if (linked[0] == 0) {
|
|
409
|
+
GLES20.glDeleteProgram(program)
|
|
410
|
+
return 0
|
|
411
|
+
}
|
|
412
|
+
programName = program
|
|
413
|
+
return program
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
private fun shader(type: Int, source: String): Int {
|
|
417
|
+
val shader = GLES20.glCreateShader(type)
|
|
418
|
+
GLES20.glShaderSource(shader, source.trimIndent())
|
|
419
|
+
GLES20.glCompileShader(shader)
|
|
420
|
+
val compiled = IntArray(1)
|
|
421
|
+
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0)
|
|
422
|
+
if (compiled[0] == 0) {
|
|
423
|
+
GLES20.glDeleteShader(shader)
|
|
424
|
+
return 0
|
|
425
|
+
}
|
|
426
|
+
return shader
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|