@blotch/angular-lynx-native 22.2.20 → 22.2.22
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 +8 -4
- package/dist/android/build.d.ts +6 -1
- package/dist/android/build.d.ts.map +1 -1
- package/dist/android/build.js +14 -3
- package/dist/android/icon.d.ts +29 -0
- package/dist/android/icon.d.ts.map +1 -0
- package/dist/android/icon.js +142 -0
- package/dist/android/project.d.ts +2 -1
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +5 -2
- package/dist/android/run.d.ts +10 -1
- package/dist/android/run.d.ts.map +1 -1
- package/dist/android/run.js +46 -1
- package/dist/dev-build.d.ts +16 -0
- package/dist/dev-build.d.ts.map +1 -0
- package/dist/dev-build.js +44 -0
- package/dist/generate.d.ts +26 -7
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +25 -10
- package/dist/index.d.ts +13 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +77 -13
- package/dist/ios/build.d.ts +68 -12
- package/dist/ios/build.d.ts.map +1 -1
- package/dist/ios/build.js +210 -45
- package/dist/ios/iphone.d.ts +99 -0
- package/dist/ios/iphone.d.ts.map +1 -0
- package/dist/ios/iphone.js +207 -0
- package/dist/ios/project.d.ts +10 -1
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +27 -4
- package/dist/ios/run.d.ts +18 -3
- package/dist/ios/run.d.ts.map +1 -1
- package/dist/ios/run.js +34 -5
- package/dist/options.d.ts +49 -3
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +33 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +6 -0
- package/dist/png.d.ts +38 -0
- package/dist/png.d.ts.map +1 -1
- package/dist/png.js +167 -0
- package/dist/run.d.ts +54 -6
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +16 -1
- package/package.json +5 -1
- package/templates/android/README.md +5 -1
- package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +159 -0
- package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServerModules.kt +163 -0
- package/templates/android/app/src/debug/res/xml/network_security_config.xml +13 -0
- package/templates/android/app/src/main/AndroidManifest.xml +16 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +22 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +24 -1
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ResourceFetchers.kt +12 -6
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareModule.kt +274 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +57 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +95 -0
- package/templates/android/app/src/main/res/mipmap/ic_launcher_round.xml +6 -0
- package/templates/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml +6 -0
- package/templates/android/app/src/main/res/xml/share_paths.xml +8 -0
- package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +24 -0
- package/templates/ios/App/AngularLynxWebGL.h +7 -0
- package/templates/ios/App/AngularLynxWebGL.m +27 -0
- package/templates/ios/App/AngularLynxWebGLContext.m +139 -0
- package/templates/ios/App/AssetsModule.swift +21 -3
- package/templates/ios/App/BundleResourceFetcher.swift +46 -2
- package/templates/ios/App/DevServer.swift +190 -0
- package/templates/ios/App/DevServerModules.swift +223 -0
- package/templates/ios/App/LynxViewController.swift +11 -1
- package/templates/ios/App/ShareModule.swift +215 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.app.PendingIntent
|
|
4
|
+
import android.content.BroadcastReceiver
|
|
5
|
+
import android.content.ClipData
|
|
6
|
+
import android.content.Context
|
|
7
|
+
import android.content.Intent
|
|
8
|
+
import android.content.IntentFilter
|
|
9
|
+
import android.net.Uri
|
|
10
|
+
import android.os.Handler
|
|
11
|
+
import android.os.Looper
|
|
12
|
+
import android.util.Base64
|
|
13
|
+
import android.webkit.MimeTypeMap
|
|
14
|
+
import androidx.activity.ComponentActivity
|
|
15
|
+
import androidx.core.content.ContextCompat
|
|
16
|
+
import androidx.core.content.FileProvider
|
|
17
|
+
import androidx.lifecycle.DefaultLifecycleObserver
|
|
18
|
+
import androidx.lifecycle.LifecycleOwner
|
|
19
|
+
import com.lynx.jsbridge.LynxMethod
|
|
20
|
+
import com.lynx.jsbridge.LynxModule
|
|
21
|
+
import com.lynx.react.bridge.Callback
|
|
22
|
+
import com.lynx.react.bridge.JavaOnlyMap
|
|
23
|
+
import com.lynx.react.bridge.ReadableArray
|
|
24
|
+
import com.lynx.react.bridge.ReadableMap
|
|
25
|
+
import com.lynx.react.bridge.ReadableType
|
|
26
|
+
import java.io.File
|
|
27
|
+
import java.io.IOException
|
|
28
|
+
import java.util.UUID
|
|
29
|
+
import java.util.concurrent.Executors
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The native module behind the runtime's `LynxShare`, which calls it as
|
|
33
|
+
* `NativeModules.AngularLynxShare` (from the background thread, where
|
|
34
|
+
* NativeModules lives). MainActivity registers it with itself as the param:
|
|
35
|
+
* the module starts the share sheet from it and watches its lifecycle.
|
|
36
|
+
*
|
|
37
|
+
* Lynx has no `navigator.share()`. Android's share sheet stands in for it:
|
|
38
|
+
* an ACTION_SEND (or ACTION_SEND_MULTIPLE) intent in a chooser. Each file is
|
|
39
|
+
* written to the app's cache first, under the name the app gave it, and
|
|
40
|
+
* handed over as a content:// URI of the app's FileProvider (the manifest's
|
|
41
|
+
* `<provider>`), which the receiving app may read once.
|
|
42
|
+
*
|
|
43
|
+
* Android doesn't say whether the person finished sharing in the app they
|
|
44
|
+
* picked. The chooser does say which app they picked, through an
|
|
45
|
+
* IntentSender, so the module calls back `{ shared: true }` then, and
|
|
46
|
+
* `{ shared: false }` when the activity comes back to the front without a
|
|
47
|
+
* pick, as when they close the sheet. Or `{ error: "failed", message }`.
|
|
48
|
+
*/
|
|
49
|
+
class ShareModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
50
|
+
private val activity = param as ComponentActivity
|
|
51
|
+
private val main = Handler(Looper.getMainLooper())
|
|
52
|
+
|
|
53
|
+
/** The open share sheet. Only touched on the main thread. */
|
|
54
|
+
private var open: Share? = null
|
|
55
|
+
|
|
56
|
+
private inner class Share(val callback: Callback) : DefaultLifecycleObserver {
|
|
57
|
+
private var answered = false
|
|
58
|
+
private var paused = false
|
|
59
|
+
|
|
60
|
+
val receiver = object : BroadcastReceiver() {
|
|
61
|
+
override fun onReceive(context: Context, intent: Intent) = answer(true)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
override fun onPause(owner: LifecycleOwner) {
|
|
65
|
+
paused = true
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
override fun onResume(owner: LifecycleOwner) {
|
|
69
|
+
if (!paused) return
|
|
70
|
+
// The pick's broadcast comes before the app is back in front;
|
|
71
|
+
// the wait lets a late one in before this calls it a close.
|
|
72
|
+
main.postDelayed({ answer(false) }, PICK_GRACE_MS)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fun answer(shared: Boolean) {
|
|
76
|
+
if (answered) return
|
|
77
|
+
answered = true
|
|
78
|
+
callback.invoke(JavaOnlyMap.from(mapOf("shared" to shared)))
|
|
79
|
+
// Once the pick is in, the module is done with the sheet: a
|
|
80
|
+
// second share can open when the person comes back.
|
|
81
|
+
close()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fun close() {
|
|
85
|
+
if (open === this) open = null
|
|
86
|
+
activity.lifecycle.removeObserver(this)
|
|
87
|
+
try {
|
|
88
|
+
activity.unregisterReceiver(receiver)
|
|
89
|
+
} catch (_: IllegalArgumentException) {
|
|
90
|
+
// Already unregistered.
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Opens the share sheet with [request]'s `files` (`{ base64, name, type }`),
|
|
97
|
+
* `title`, `text` and `url`, and calls back once with `{ shared }`, or
|
|
98
|
+
* with `{ error: "failed", message }`.
|
|
99
|
+
*/
|
|
100
|
+
@LynxMethod
|
|
101
|
+
fun share(request: ReadableMap, callback: Callback) {
|
|
102
|
+
// Decoding and writing a photo takes a moment: off the main thread.
|
|
103
|
+
worker.execute {
|
|
104
|
+
val uris = try {
|
|
105
|
+
writeFiles(request.optArray("files"))
|
|
106
|
+
} catch (error: IOException) {
|
|
107
|
+
callback.invoke(failed("The files couldn't be written to share: ${error.message}"))
|
|
108
|
+
return@execute
|
|
109
|
+
} catch (error: IllegalArgumentException) {
|
|
110
|
+
callback.invoke(failed("A file's bytes couldn't be read."))
|
|
111
|
+
return@execute
|
|
112
|
+
}
|
|
113
|
+
val intent = sendIntent(
|
|
114
|
+
uris,
|
|
115
|
+
request.optString("title"),
|
|
116
|
+
request.optString("text"),
|
|
117
|
+
request.optString("url"),
|
|
118
|
+
)
|
|
119
|
+
// The chooser is UI: the main thread starts it.
|
|
120
|
+
main.post { present(intent, callback) }
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private fun present(intent: Intent, callback: Callback) {
|
|
125
|
+
if (open != null) {
|
|
126
|
+
callback.invoke(failed("A share sheet is already open."))
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
val share = Share(callback)
|
|
130
|
+
// The chooser sends this when the person picks an app. It's the
|
|
131
|
+
// app's own, so the receiver needn't be exported, and immutable,
|
|
132
|
+
// since nothing it would fill in is read.
|
|
133
|
+
val action = "${activity.packageName}.ANGULAR_LYNX_SHARE_PICKED.${UUID.randomUUID()}"
|
|
134
|
+
ContextCompat.registerReceiver(
|
|
135
|
+
activity,
|
|
136
|
+
share.receiver,
|
|
137
|
+
IntentFilter(action),
|
|
138
|
+
ContextCompat.RECEIVER_NOT_EXPORTED,
|
|
139
|
+
)
|
|
140
|
+
val picked = PendingIntent.getBroadcast(
|
|
141
|
+
activity,
|
|
142
|
+
0,
|
|
143
|
+
Intent(action).setPackage(activity.packageName),
|
|
144
|
+
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE,
|
|
145
|
+
)
|
|
146
|
+
open = share
|
|
147
|
+
activity.lifecycle.addObserver(share)
|
|
148
|
+
try {
|
|
149
|
+
activity.startActivity(Intent.createChooser(intent, null, picked.intentSender))
|
|
150
|
+
} catch (error: RuntimeException) {
|
|
151
|
+
share.close()
|
|
152
|
+
callback.invoke(failed("The share sheet couldn't open: ${error.message}"))
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Writes the files under the cache's share folder, the one the
|
|
158
|
+
* FileProvider's paths (res/xml/share_paths.xml) name, and returns their
|
|
159
|
+
* content:// URIs. The last share's files go first: the app that got them
|
|
160
|
+
* may read them after the share sheet closes, so they're kept until the
|
|
161
|
+
* next share rather than deleted when it closes.
|
|
162
|
+
*/
|
|
163
|
+
private fun writeFiles(files: ReadableArray?): List<Pair<Uri, String>> {
|
|
164
|
+
val root = File(mContext.cacheDir, SHARE_FOLDER)
|
|
165
|
+
root.deleteRecursively()
|
|
166
|
+
if (files == null || files.size() == 0) return emptyList()
|
|
167
|
+
val folder = File(root, UUID.randomUUID().toString())
|
|
168
|
+
if (!folder.mkdirs()) throw IOException("No folder for the files")
|
|
169
|
+
val used = HashSet<String>()
|
|
170
|
+
return (0 until files.size()).map { index ->
|
|
171
|
+
val file = files.getMap(index)
|
|
172
|
+
val bytes = Base64.decode(file.optString("base64") ?: "", Base64.DEFAULT)
|
|
173
|
+
val name = uniqueName(safeName(file.optString("name")), used)
|
|
174
|
+
val target = File(folder, name)
|
|
175
|
+
target.writeBytes(bytes)
|
|
176
|
+
val uri = FileProvider.getUriForFile(mContext, "${mContext.packageName}$AUTHORITY_SUFFIX", target)
|
|
177
|
+
uri to (file.optString("type")?.takeIf { it.isNotEmpty() } ?: typeOf(name))
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private fun sendIntent(files: List<Pair<Uri, String>>, title: String?, text: String?, url: String?): Intent {
|
|
182
|
+
val intent = if (files.size > 1) {
|
|
183
|
+
Intent(Intent.ACTION_SEND_MULTIPLE).putParcelableArrayListExtra(
|
|
184
|
+
Intent.EXTRA_STREAM,
|
|
185
|
+
ArrayList(files.map { it.first }),
|
|
186
|
+
)
|
|
187
|
+
} else {
|
|
188
|
+
Intent(Intent.ACTION_SEND).apply {
|
|
189
|
+
files.firstOrNull()?.let { putExtra(Intent.EXTRA_STREAM, it.first) }
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
intent.type = when {
|
|
193
|
+
files.isEmpty() -> "text/plain"
|
|
194
|
+
else -> commonType(files.map { it.second })
|
|
195
|
+
}
|
|
196
|
+
// Text and a link go together, as Chrome shares them on Android. A
|
|
197
|
+
// title on its own, as the Web Share API allows, is the text.
|
|
198
|
+
val body = listOfNotNull(text, url).joinToString(" ").ifEmpty { if (files.isEmpty()) title else null }
|
|
199
|
+
if (body != null) intent.putExtra(Intent.EXTRA_TEXT, body)
|
|
200
|
+
if (title != null) {
|
|
201
|
+
// The subject of an email, and the heading of the sheet's preview.
|
|
202
|
+
intent.putExtra(Intent.EXTRA_SUBJECT, title)
|
|
203
|
+
intent.putExtra(Intent.EXTRA_TITLE, title)
|
|
204
|
+
}
|
|
205
|
+
if (files.isNotEmpty()) {
|
|
206
|
+
// The ClipData carries the read grant to the app picked, and lets
|
|
207
|
+
// the sheet show a preview of the files.
|
|
208
|
+
val clip = ClipData.newUri(mContext.contentResolver, title ?: "", files[0].first)
|
|
209
|
+
for (file in files.drop(1)) clip.addItem(ClipData.Item(file.first))
|
|
210
|
+
intent.clipData = clip
|
|
211
|
+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
212
|
+
}
|
|
213
|
+
return intent
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
private fun failed(message: String) =
|
|
217
|
+
JavaOnlyMap.from(mapOf("error" to "failed", "message" to message))
|
|
218
|
+
|
|
219
|
+
private fun ReadableMap.optString(key: String): String? =
|
|
220
|
+
if (hasKey(key) && getType(key) == ReadableType.String) getString(key) else null
|
|
221
|
+
|
|
222
|
+
private fun ReadableMap.optArray(key: String): ReadableArray? =
|
|
223
|
+
if (hasKey(key) && getType(key) == ReadableType.Array) getArray(key) else null
|
|
224
|
+
|
|
225
|
+
companion object {
|
|
226
|
+
const val NAME = "AngularLynxShare"
|
|
227
|
+
|
|
228
|
+
/** The FileProvider's authority after the app's id (AndroidManifest.xml). */
|
|
229
|
+
private const val AUTHORITY_SUFFIX = ".angularlynx.share"
|
|
230
|
+
|
|
231
|
+
/** The cache folder the files are written to (res/xml/share_paths.xml). */
|
|
232
|
+
private const val SHARE_FOLDER = "angular-lynx-share"
|
|
233
|
+
|
|
234
|
+
/** How long a pick's broadcast may come after the app is back in front. */
|
|
235
|
+
private const val PICK_GRACE_MS = 500L
|
|
236
|
+
|
|
237
|
+
/** One thread for writing files, so two shares can't hold two photos at once. */
|
|
238
|
+
private val worker = Executors.newSingleThreadExecutor()
|
|
239
|
+
|
|
240
|
+
/** A file name the file system takes: no folders, and something there. */
|
|
241
|
+
private fun safeName(name: String?): String {
|
|
242
|
+
val base = (name ?: "").replace('/', '_').trim()
|
|
243
|
+
return if (base.isEmpty() || base == "." || base == "..") "file" else base
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** `photo.jpg`, then `photo 2.jpg`: two files of one name would overwrite each other. */
|
|
247
|
+
private fun uniqueName(name: String, used: MutableSet<String>): String {
|
|
248
|
+
val dot = name.lastIndexOf('.')
|
|
249
|
+
val stem = if (dot > 0) name.substring(0, dot) else name
|
|
250
|
+
val extension = if (dot > 0) name.substring(dot) else ""
|
|
251
|
+
var candidate = name
|
|
252
|
+
var number = 2
|
|
253
|
+
while (!used.add(candidate.lowercase())) candidate = "$stem ${number++}$extension"
|
|
254
|
+
return candidate
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** The type of a file the app gave none, from its extension. */
|
|
258
|
+
private fun typeOf(name: String): String =
|
|
259
|
+
MimeTypeMap.getSingleton()
|
|
260
|
+
.getMimeTypeFromExtension(name.substringAfterLast('.', "").lowercase())
|
|
261
|
+
?: "application/octet-stream"
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The intent's type for its files: theirs when they share one,
|
|
265
|
+
* `image/` with `*` when they're all images, and any type otherwise,
|
|
266
|
+
* so the sheet lists the apps that take them all.
|
|
267
|
+
*/
|
|
268
|
+
private fun commonType(types: List<String>): String {
|
|
269
|
+
if (types.distinct().size == 1) return types[0]
|
|
270
|
+
val kinds = types.map { it.substringBefore('/') }.distinct()
|
|
271
|
+
return if (kinds.size == 1) "${kinds[0]}/*" else "*/*"
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
@@ -325,6 +325,63 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
|
|
|
325
325
|
}
|
|
326
326
|
}
|
|
327
327
|
|
|
328
|
+
/**
|
|
329
|
+
* The drawing buffer's pixels, for canvas.toBlob() and toDataURL(): RGBA,
|
|
330
|
+
* rows bottom to top as GL reads them, or null once the context is gone.
|
|
331
|
+
* It's what the drawing buffer holds now, as a browser reads it: what the
|
|
332
|
+
* app has drawn since the last frame was shown, or, once a frame has been
|
|
333
|
+
* shown without preserveDrawingBuffer, the cleared buffer.
|
|
334
|
+
*/
|
|
335
|
+
fun readDrawingBuffer(): DrawingBufferPixels? {
|
|
336
|
+
if (destroyed) return null
|
|
337
|
+
val pixels = ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.nativeOrder())
|
|
338
|
+
withCurrent {
|
|
339
|
+
// A plain read of the whole default framebuffer, whatever the app
|
|
340
|
+
// has bound and set: its pixel pack buffer and pack parameters
|
|
341
|
+
// would move or redirect the read. The app's state is put back
|
|
342
|
+
// after.
|
|
343
|
+
val pack = IntArray(5)
|
|
344
|
+
GLES30.glGetIntegerv(GLES30.GL_PIXEL_PACK_BUFFER_BINDING, pack, 0)
|
|
345
|
+
GLES30.glGetIntegerv(GLES30.GL_PACK_ALIGNMENT, pack, 1)
|
|
346
|
+
GLES30.glGetIntegerv(GLES30.GL_PACK_ROW_LENGTH, pack, 2)
|
|
347
|
+
GLES30.glGetIntegerv(GLES30.GL_PACK_SKIP_PIXELS, pack, 3)
|
|
348
|
+
GLES30.glGetIntegerv(GLES30.GL_PACK_SKIP_ROWS, pack, 4)
|
|
349
|
+
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
|
350
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT, 4)
|
|
351
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_ROW_LENGTH, 0)
|
|
352
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_SKIP_PIXELS, 0)
|
|
353
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_SKIP_ROWS, 0)
|
|
354
|
+
var source = framebuffer
|
|
355
|
+
if (samples > 0) {
|
|
356
|
+
// GLES can't read a multisampled framebuffer: its samples are
|
|
357
|
+
// resolved first, as present and reading do. A blit is clipped
|
|
358
|
+
// by the scissor test, and skipped with rasterizer discard on,
|
|
359
|
+
// so both are off for it.
|
|
360
|
+
val scissor = GLES30.glIsEnabled(GLES30.GL_SCISSOR_TEST)
|
|
361
|
+
val discard = GLES30.glIsEnabled(GLES30.GL_RASTERIZER_DISCARD)
|
|
362
|
+
GLES30.glDisable(GLES30.GL_SCISSOR_TEST)
|
|
363
|
+
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD)
|
|
364
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, framebuffer)
|
|
365
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, resolveFramebuffer)
|
|
366
|
+
GLES30.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GLES30.GL_COLOR_BUFFER_BIT, GLES30.GL_NEAREST)
|
|
367
|
+
if (scissor) GLES30.glEnable(GLES30.GL_SCISSOR_TEST)
|
|
368
|
+
if (discard) GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD)
|
|
369
|
+
source = resolveFramebuffer
|
|
370
|
+
}
|
|
371
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, source)
|
|
372
|
+
GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0)
|
|
373
|
+
GLES30.glReadPixels(0, 0, width, height, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, pixels)
|
|
374
|
+
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, pack[0])
|
|
375
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT, pack[1])
|
|
376
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_ROW_LENGTH, pack[2])
|
|
377
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_SKIP_PIXELS, pack[3])
|
|
378
|
+
GLES30.glPixelStorei(GLES30.GL_PACK_SKIP_ROWS, pack[4])
|
|
379
|
+
restoreFramebuffers()
|
|
380
|
+
}
|
|
381
|
+
pixels.rewind()
|
|
382
|
+
return DrawingBufferPixels(pixels, width, height, alpha)
|
|
383
|
+
}
|
|
384
|
+
|
|
328
385
|
/**
|
|
329
386
|
* Shows the finished frame: copies the default framebuffer (resolving it
|
|
330
387
|
* when it's multisampled) into the TextureView's surface, scaled to its
|
|
@@ -3,6 +3,7 @@ package dev.angularlynx.shell
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import android.graphics.Bitmap
|
|
5
5
|
import android.graphics.BitmapFactory
|
|
6
|
+
import android.util.Base64
|
|
6
7
|
import com.lynx.jsbridge.LynxMethod
|
|
7
8
|
import com.lynx.jsbridge.LynxModule
|
|
8
9
|
import com.lynx.react.bridge.JavaOnlyArray
|
|
@@ -10,9 +11,11 @@ import com.lynx.react.bridge.JavaOnlyMap
|
|
|
10
11
|
import com.lynx.react.bridge.ReadableArray
|
|
11
12
|
import com.lynx.react.bridge.ReadableMap
|
|
12
13
|
import com.lynx.react.bridge.ReadableType
|
|
14
|
+
import java.io.ByteArrayOutputStream
|
|
13
15
|
import java.lang.ref.WeakReference
|
|
14
16
|
import java.nio.ByteBuffer
|
|
15
17
|
import java.nio.ByteOrder
|
|
18
|
+
import kotlin.math.roundToInt
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* `<canvas>` with WebGL2 for the AngularLynx runtime: the `AngularLynxWebGL`
|
|
@@ -71,6 +74,95 @@ class WebGLModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
|
71
74
|
return true
|
|
72
75
|
}
|
|
73
76
|
|
|
77
|
+
/**
|
|
78
|
+
* The drawing buffer as an image file, for canvas.toBlob() and
|
|
79
|
+
* toDataURL(): `{ base64, type }`, or `{ error }`. [options] holds the
|
|
80
|
+
* MIME type (image/jpeg, or image/png for anything else, as browsers fall
|
|
81
|
+
* back), the JPEG quality and premultipliedAlpha.
|
|
82
|
+
*
|
|
83
|
+
* The pixels are encoded here rather than sent to the runtime, which
|
|
84
|
+
* would have to carry 11 MB of RGBA for a 2048 by 1365 photo across the
|
|
85
|
+
* bridge as numbers, and encode it in JavaScript on PrimJS, which has no
|
|
86
|
+
* JIT. The file crosses as base64: the bridge can't return bytes (see
|
|
87
|
+
* webgl-bridge.ts).
|
|
88
|
+
*/
|
|
89
|
+
@LynxMethod
|
|
90
|
+
fun encodeImage(contextId: Int, options: ReadableMap?): JavaOnlyMap {
|
|
91
|
+
val context = contexts[contextId] ?: return encodeFailed("The canvas has no WebGL context")
|
|
92
|
+
val jpeg = options?.optString("type") == "image/jpeg"
|
|
93
|
+
val quality = if (options != null && options.hasKey("quality") && options.getType("quality") in NUMBER_TYPES) {
|
|
94
|
+
options.getDouble("quality")
|
|
95
|
+
} else {
|
|
96
|
+
0.92
|
|
97
|
+
}
|
|
98
|
+
val premultiplied = !(
|
|
99
|
+
options != null && options.hasKey("premultipliedAlpha") &&
|
|
100
|
+
options.getType("premultipliedAlpha") == ReadableType.Boolean &&
|
|
101
|
+
!options.getBoolean("premultipliedAlpha")
|
|
102
|
+
)
|
|
103
|
+
return try {
|
|
104
|
+
val pixels = context.readDrawingBuffer() ?: return encodeFailed("The WebGL context is gone")
|
|
105
|
+
val file = encode(pixels, jpeg, quality, premultiplied)
|
|
106
|
+
JavaOnlyMap.from(
|
|
107
|
+
mapOf(
|
|
108
|
+
"base64" to Base64.encodeToString(file, Base64.NO_WRAP),
|
|
109
|
+
"type" to if (jpeg) "image/jpeg" else "image/png",
|
|
110
|
+
),
|
|
111
|
+
)
|
|
112
|
+
} catch (_: OutOfMemoryError) {
|
|
113
|
+
encodeFailed("The canvas is too large to encode")
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private fun encodeFailed(message: String) = JavaOnlyMap.from(mapOf("error" to message))
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Encodes RGBA pixels, rows bottom to top, as a PNG or JPEG file.
|
|
121
|
+
*/
|
|
122
|
+
private fun encode(pixels: DrawingBufferPixels, jpeg: Boolean, quality: Double, premultiplied: Boolean): ByteArray {
|
|
123
|
+
val buffer = pixels.rgba
|
|
124
|
+
val stride = pixels.width * 4
|
|
125
|
+
// GL's first row is the bottom of the image; a bitmap's is the top.
|
|
126
|
+
flipRows(buffer, stride, pixels.height)
|
|
127
|
+
if (jpeg && pixels.alpha) {
|
|
128
|
+
// A JPEG has no transparency. Browsers draw a canvas's
|
|
129
|
+
// transparent pixels over black for one, which premultiplied
|
|
130
|
+
// colors already are: straight ones are multiplied first. Then
|
|
131
|
+
// every pixel is opaque, so the encoder has no alpha to treat.
|
|
132
|
+
for (i in 0 until buffer.limit() step 4) {
|
|
133
|
+
val alpha = buffer.get(i + 3).toInt() and 0xff
|
|
134
|
+
if (alpha == 255) continue
|
|
135
|
+
if (!premultiplied) {
|
|
136
|
+
for (c in 0 until 3) {
|
|
137
|
+
val value = buffer.get(i + c).toInt() and 0xff
|
|
138
|
+
buffer.put(i + c, ((value * alpha + 127) / 255).toByte())
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
buffer.put(i + 3, 0xff.toByte())
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
val bitmap = Bitmap.createBitmap(pixels.width, pixels.height, Bitmap.Config.ARGB_8888)
|
|
145
|
+
// The drawing buffer's colors are premultiplied by alpha unless the
|
|
146
|
+
// app asked for premultipliedAlpha: false. A bitmap takes either, and
|
|
147
|
+
// compress() writes a PNG's straight colors from it.
|
|
148
|
+
if (!premultiplied && pixels.alpha && !jpeg) bitmap.isPremultiplied = false
|
|
149
|
+
// ARGB_8888 is R, G, B, A in memory: what GL_RGBA reads.
|
|
150
|
+
bitmap.copyPixelsFromBuffer(buffer)
|
|
151
|
+
if (jpeg || !pixels.alpha) bitmap.setHasAlpha(false)
|
|
152
|
+
return try {
|
|
153
|
+
ByteArrayOutputStream().use { output ->
|
|
154
|
+
bitmap.compress(
|
|
155
|
+
if (jpeg) Bitmap.CompressFormat.JPEG else Bitmap.CompressFormat.PNG,
|
|
156
|
+
(quality.coerceIn(0.0, 1.0) * 100).roundToInt(),
|
|
157
|
+
output,
|
|
158
|
+
)
|
|
159
|
+
output.toByteArray()
|
|
160
|
+
}
|
|
161
|
+
} finally {
|
|
162
|
+
bitmap.recycle()
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
74
166
|
override fun destroy() {
|
|
75
167
|
for (context in contexts.values) context.destroy()
|
|
76
168
|
contexts.clear()
|
|
@@ -124,6 +216,9 @@ class WebGLModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
|
124
216
|
|
|
125
217
|
internal val NUMBER_TYPES = setOf(ReadableType.Int, ReadableType.Long, ReadableType.Number)
|
|
126
218
|
|
|
219
|
+
/** The drawing buffer's pixels: RGBA, rows bottom to top, and whether the canvas has alpha. */
|
|
220
|
+
class DrawingBufferPixels(val rgba: ByteBuffer, val width: Int, val height: Int, val alpha: Boolean)
|
|
221
|
+
|
|
127
222
|
/** Pixels the module decoded, which texture uploads read by id: RGBA, rows top to bottom. */
|
|
128
223
|
internal class DecodedImage(val pixels: ByteBuffer, val width: Int, val height: Int, val premultiplied: Boolean)
|
|
129
224
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!-- Android 7 (API 24-25) has no adaptive icons, so it gets the two layers flattened. -->
|
|
3
|
+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
|
4
|
+
<item android:drawable="@drawable/ic_launcher_background" />
|
|
5
|
+
<item android:drawable="@drawable/ic_launcher_foreground" />
|
|
6
|
+
</layer-list>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<!-- Placeholder launcher icon; the generator replaces it with the app's icon. -->
|
|
3
|
+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
|
4
|
+
<background android:drawable="@drawable/ic_launcher_background" />
|
|
5
|
+
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
|
6
|
+
</adaptive-icon>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.widget.FrameLayout
|
|
5
|
+
import com.lynx.tasm.LynxViewBuilder
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The release build's DevServer: nothing. Only the debug build type, which
|
|
9
|
+
* `ng serve --platform android` builds, has the dev server support
|
|
10
|
+
* (src/debug/.../DevServer.kt), so nothing can point a release build at
|
|
11
|
+
* another server: its bundle is always the one in its assets. The two files
|
|
12
|
+
* keep the same functions, which MainActivity and the loaders call.
|
|
13
|
+
*/
|
|
14
|
+
object DevServer {
|
|
15
|
+
fun bundleUrl(activity: Activity): String? = null
|
|
16
|
+
|
|
17
|
+
fun register(builder: LynxViewBuilder, reload: () -> Unit) = Unit
|
|
18
|
+
|
|
19
|
+
fun attach(container: FrameLayout) = Unit
|
|
20
|
+
|
|
21
|
+
fun remoteUrl(url: String): String? = null
|
|
22
|
+
|
|
23
|
+
fun loaded(url: String, error: Throwable?) = Unit
|
|
24
|
+
}
|
|
@@ -58,8 +58,15 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
58
58
|
error:(NSString *_Nullable *_Nonnull)error;
|
|
59
59
|
/// Runs the commands in order; returns one result per query.
|
|
60
60
|
- (NSArray *)run:(NSArray *)commands;
|
|
61
|
+
/// The drawing buffer as a PNG or JPEG file, in base64: `{ base64, type }`,
|
|
62
|
+
/// or `{ error }`. For the runtime's canvas.toBlob() and toDataURL().
|
|
63
|
+
- (NSDictionary *)encodeImageOfType:(NSString *)type
|
|
64
|
+
quality:(double)quality
|
|
65
|
+
premultiplied:(BOOL)premultiplied;
|
|
61
66
|
/// Shows frames in this view, or nowhere while it's nil.
|
|
62
67
|
- (void)attachView:(nullable AngularLynxCanvasView *)view;
|
|
68
|
+
/// The view was laid out: a frame that waited for it to have a size is shown.
|
|
69
|
+
- (void)viewDidLayOut:(AngularLynxCanvasView *)view;
|
|
63
70
|
- (void)destroy;
|
|
64
71
|
@end
|
|
65
72
|
|
|
@@ -195,6 +195,7 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
|
|
|
195
195
|
@"destroy" : NSStringFromSelector(@selector(destroy:)),
|
|
196
196
|
@"decodeImage" : NSStringFromSelector(@selector(decodeImage:bytes:options:)),
|
|
197
197
|
@"releaseImage" : NSStringFromSelector(@selector(releaseImage:)),
|
|
198
|
+
@"encodeImage" : NSStringFromSelector(@selector(encodeImage:options:)),
|
|
198
199
|
};
|
|
199
200
|
}
|
|
200
201
|
|
|
@@ -269,6 +270,21 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
|
|
|
269
270
|
return @YES;
|
|
270
271
|
}
|
|
271
272
|
|
|
273
|
+
/// The drawing buffer as an image file, for canvas.toBlob() and toDataURL():
|
|
274
|
+
/// `options` holds the MIME type (image/jpeg, or image/png for anything
|
|
275
|
+
/// else, as browsers fall back), the JPEG quality and premultipliedAlpha.
|
|
276
|
+
- (NSDictionary *)encodeImage:(NSNumber *)contextId options:(NSDictionary *)options {
|
|
277
|
+
AngularLynxWebGLContext *context = _contexts[contextId];
|
|
278
|
+
if (!context) return @{@"error" : @"The canvas has no WebGL context"};
|
|
279
|
+
if (![options isKindOfClass:NSDictionary.class]) options = @{};
|
|
280
|
+
NSString *type = [options[@"type"] isEqual:@"image/jpeg"] ? @"image/jpeg" : @"image/png";
|
|
281
|
+
NSNumber *quality = options[@"quality"];
|
|
282
|
+
return [context
|
|
283
|
+
encodeImageOfType:type
|
|
284
|
+
quality:[quality isKindOfClass:NSNumber.class] ? quality.doubleValue : 0.92
|
|
285
|
+
premultiplied:![options[@"premultipliedAlpha"] isEqual:@NO]];
|
|
286
|
+
}
|
|
287
|
+
|
|
272
288
|
@end
|
|
273
289
|
|
|
274
290
|
#pragma mark - Canvas
|
|
@@ -293,6 +309,17 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
|
|
|
293
309
|
return self;
|
|
294
310
|
}
|
|
295
311
|
|
|
312
|
+
// A frame drawn before Lynx gave the view a size waits for one (see
|
|
313
|
+
// present). An app that draws only when something changes, such as a photo
|
|
314
|
+
// editor drawing its photo once, would otherwise show nothing until it drew
|
|
315
|
+
// again; one drawing every frame never noticed.
|
|
316
|
+
- (void)layoutSubviews {
|
|
317
|
+
[super layoutSubviews];
|
|
318
|
+
if (_canvasId) {
|
|
319
|
+
[[AngularLynxCanvasRegistry contextForCanvasId:_canvasId] viewDidLayOut:self];
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
296
323
|
- (void)setCanvasId:(NSString *)canvasId {
|
|
297
324
|
if ([_canvasId isEqualToString:canvasId]) return;
|
|
298
325
|
if (_canvasId && [AngularLynxCanvasRegistry viewForCanvasId:_canvasId] == self) {
|