@onekeyfe/react-native-image 6.7.0-54 → 6.7.0-55
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/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +157 -41
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +56 -1
- package/ios/OneKeyImageStartupPrewarm.swift +29 -3
- package/ios/OneKeyNamedSVG.swift +25 -2
- package/ios/tests/OneKeyIconTests.swift +60 -0
- package/ios/tests/OneKeyImageInfrastructureTests.swift +156 -0
- package/package.json +1 -1
|
@@ -61,6 +61,9 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
61
61
|
private const val MAX_LAUNCH_AGE = 3
|
|
62
62
|
private const val MAX_MEMORY_ENTRIES = 64
|
|
63
63
|
private const val MAX_MEMORY_BYTES = 16L * 1024L * 1024L
|
|
64
|
+
internal const val MAX_FALLBACK_DISK_FILES = MAX_ENTRIES
|
|
65
|
+
internal const val MAX_FALLBACK_DISK_BYTES =
|
|
66
|
+
MAX_ENTRIES.toLong() * OneKeyMainThreadDecode.MAX_ENCODED_BYTES.toLong()
|
|
64
67
|
private const val FILE_NAME = "onekey-image-startup-v1.json"
|
|
65
68
|
internal const val GLIDE_DISK_CACHE_DIR = "image_manager_disk_cache"
|
|
66
69
|
internal const val FALLBACK_DISK_CACHE_DIR = "onekey-image-startup-disk-v1"
|
|
@@ -70,6 +73,7 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
70
73
|
private val diskIoLock = Any()
|
|
71
74
|
private val recorded = ArrayList<StartupEntry>()
|
|
72
75
|
private val recordedKeys = HashSet<String>()
|
|
76
|
+
private val activeStartupDiskFiles = HashSet<String>()
|
|
73
77
|
private val pendingFallbackSources = HashMap<String, String>()
|
|
74
78
|
private var recordStartTimeMs: Long? = null
|
|
75
79
|
private var recordingClosed = false
|
|
@@ -143,8 +147,16 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
143
147
|
|
|
144
148
|
internal fun ensureFallbackDiskBytesAsync(cacheDir: File, uri: String, bytes: ByteArray) {
|
|
145
149
|
val safeKey = glideDataSafeKey(uri) ?: return
|
|
150
|
+
val expectedGeneration = synchronized(stateLock) {
|
|
151
|
+
if (recordingClosed && !activeStartupDiskFiles.contains(safeKey)) {
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
if (!recordingClosed && recorded.size >= MAX_ENTRIES && !activeStartupDiskFiles.contains(safeKey)) {
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
diskClearGeneration
|
|
158
|
+
}
|
|
146
159
|
if (findDiskCacheFile(cacheDir, safeKey) != null) return
|
|
147
|
-
val expectedGeneration = synchronized(stateLock) { diskClearGeneration }
|
|
148
160
|
scheduler.execute {
|
|
149
161
|
writeFallbackDiskBytes(cacheDir, safeKey, bytes, expectedGeneration)
|
|
150
162
|
}
|
|
@@ -156,25 +168,80 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
156
168
|
bytes: ByteArray,
|
|
157
169
|
expectedGeneration: Long = synchronized(stateLock) { diskClearGeneration },
|
|
158
170
|
) {
|
|
159
|
-
if (
|
|
171
|
+
if (
|
|
172
|
+
!isSafeToken(safeKey) ||
|
|
173
|
+
bytes.isEmpty() ||
|
|
174
|
+
bytes.size.toLong() > MAX_FALLBACK_DISK_BYTES
|
|
175
|
+
) {
|
|
176
|
+
return
|
|
177
|
+
}
|
|
160
178
|
synchronized(diskIoLock) {
|
|
161
|
-
|
|
179
|
+
val allowed = synchronized(stateLock) {
|
|
180
|
+
if (diskClearGeneration != expectedGeneration) {
|
|
181
|
+
false
|
|
182
|
+
} else if (recordingClosed) {
|
|
183
|
+
activeStartupDiskFiles.contains(safeKey)
|
|
184
|
+
} else {
|
|
185
|
+
recorded.size < MAX_ENTRIES || activeStartupDiskFiles.contains(safeKey)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!allowed) return
|
|
162
189
|
try {
|
|
163
190
|
val dir = File(cacheDir, FALLBACK_DISK_CACHE_DIR)
|
|
164
191
|
if (!dir.exists()) dir.mkdirs()
|
|
165
192
|
val target = File(dir, safeKey)
|
|
166
|
-
if (target.isFile
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
193
|
+
if (!target.isFile || target.length() != bytes.size.toLong()) {
|
|
194
|
+
val tmp = File(dir, "$safeKey.tmp")
|
|
195
|
+
tmp.writeBytes(bytes)
|
|
196
|
+
if (!tmp.renameTo(target)) {
|
|
197
|
+
tmp.copyTo(target, overwrite = true)
|
|
198
|
+
tmp.delete()
|
|
199
|
+
}
|
|
172
200
|
}
|
|
201
|
+
trimFallbackDiskCacheLocked(dir, protectedKey = safeKey)
|
|
173
202
|
} catch (_: Exception) {
|
|
174
203
|
}
|
|
175
204
|
}
|
|
176
205
|
}
|
|
177
206
|
|
|
207
|
+
private fun trimFallbackDiskCacheLocked(dir: File, protectedKey: String? = null) {
|
|
208
|
+
val files = dir.listFiles() ?: return
|
|
209
|
+
val regularFiles = ArrayList<File>(files.size)
|
|
210
|
+
for (candidate in files) {
|
|
211
|
+
if (!candidate.isFile) continue
|
|
212
|
+
if (candidate.name.endsWith(".tmp")) {
|
|
213
|
+
candidate.delete()
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
regularFiles.add(candidate)
|
|
217
|
+
}
|
|
218
|
+
var totalBytes = regularFiles.sumOf { it.length().coerceAtLeast(0L) }
|
|
219
|
+
if (regularFiles.size <= MAX_FALLBACK_DISK_FILES && totalBytes <= MAX_FALLBACK_DISK_BYTES) {
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
val activeSnapshot = synchronized(stateLock) { HashSet(activeStartupDiskFiles) }
|
|
223
|
+
val evictionOrder = regularFiles
|
|
224
|
+
.filter { it.name != protectedKey }
|
|
225
|
+
.sortedWith(
|
|
226
|
+
compareBy<File>(
|
|
227
|
+
{ activeSnapshot.contains(it.name) },
|
|
228
|
+
{ it.lastModified() },
|
|
229
|
+
{ it.name },
|
|
230
|
+
),
|
|
231
|
+
)
|
|
232
|
+
var remainingCount = regularFiles.size
|
|
233
|
+
for (victim in evictionOrder) {
|
|
234
|
+
if (remainingCount <= MAX_FALLBACK_DISK_FILES && totalBytes <= MAX_FALLBACK_DISK_BYTES) {
|
|
235
|
+
break
|
|
236
|
+
}
|
|
237
|
+
val len = victim.length().coerceAtLeast(0L)
|
|
238
|
+
if (victim.delete()) {
|
|
239
|
+
remainingCount -= 1
|
|
240
|
+
totalBytes = (totalBytes - len).coerceAtLeast(0L)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
178
245
|
fun cachedBitmap(keyHash: String): Bitmap? = synchronized(memoryLock) {
|
|
179
246
|
memoryCache[keyHash]
|
|
180
247
|
}
|
|
@@ -223,6 +290,7 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
223
290
|
flushScheduled = false
|
|
224
291
|
recorded.clear()
|
|
225
292
|
recordedKeys.clear()
|
|
293
|
+
activeStartupDiskFiles.clear()
|
|
226
294
|
pendingFallbackSources.clear()
|
|
227
295
|
file = startupFile()
|
|
228
296
|
resolvedDir = cacheDir ?: baseCacheDir ?: file?.parentFile
|
|
@@ -293,10 +361,19 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
293
361
|
schedulePersistLocked(delayMs = 0L)
|
|
294
362
|
return
|
|
295
363
|
}
|
|
364
|
+
var accepted = false
|
|
296
365
|
if (recorded.size < MAX_ENTRIES && recordedKeys.add(entry.identity)) {
|
|
297
366
|
recorded.add(entry)
|
|
367
|
+
activeStartupDiskFiles.add(diskFile)
|
|
368
|
+
rawDiskFile?.let { activeStartupDiskFiles.add(it) }
|
|
369
|
+
accepted = true
|
|
370
|
+
} else if (activeStartupDiskFiles.contains(diskFile)) {
|
|
371
|
+
accepted = true
|
|
298
372
|
}
|
|
299
|
-
if (
|
|
373
|
+
if (
|
|
374
|
+
accepted &&
|
|
375
|
+
(requestUrl.startsWith("file://") || requestUrl.startsWith("data:") || OneKeyBlockieAvatar.isAvatarUri(requestUrl))
|
|
376
|
+
) {
|
|
300
377
|
pendingFallbackSources.putIfAbsent(diskFile, requestUrl)
|
|
301
378
|
}
|
|
302
379
|
schedulePersistLocked(delayMs = RECORD_DURATION_MS)
|
|
@@ -367,14 +444,14 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
367
444
|
uri.startsWith("file://") -> {
|
|
368
445
|
val rawPath = uri.removePrefix("file://").substringBefore('?').substringBefore('#')
|
|
369
446
|
val f = File(rawPath)
|
|
370
|
-
if (f.isFile && f.length() in 1L..
|
|
447
|
+
if (f.isFile && f.length() in 1L..MAX_FALLBACK_DISK_BYTES) f.readBytes() else null
|
|
371
448
|
}
|
|
372
449
|
uri.startsWith("data:") -> {
|
|
373
450
|
val comma = uri.indexOf(',')
|
|
374
451
|
if (comma > 0 && uri.substring(0, comma).lowercase(Locale.ROOT).contains(";base64")) {
|
|
375
452
|
val payloadStart = comma + 1
|
|
376
453
|
val count = OneKeyImageSafety.dataUriDecodedByteCount(uri, payloadStart)
|
|
377
|
-
if (count in 1L..OneKeyImageSafety.MAX_DATA_URI_DECODED_BYTES) {
|
|
454
|
+
if (count in 1L..minOf(MAX_FALLBACK_DISK_BYTES, OneKeyImageSafety.MAX_DATA_URI_DECODED_BYTES)) {
|
|
378
455
|
OneKeyImageSafety.decodeDataUriBase64(uri.substring(payloadStart))
|
|
379
456
|
} else null
|
|
380
457
|
} else null
|
|
@@ -402,7 +479,27 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
402
479
|
file: File,
|
|
403
480
|
expectedGeneration: Long = synchronized(stateLock) { diskClearGeneration },
|
|
404
481
|
) {
|
|
405
|
-
if (entries.isEmpty())
|
|
482
|
+
if (entries.isEmpty()) {
|
|
483
|
+
synchronized(diskIoLock) {
|
|
484
|
+
if (synchronized(stateLock) { diskClearGeneration != expectedGeneration }) return
|
|
485
|
+
val existing = readEntries(file)
|
|
486
|
+
val activeFiles = HashSet<String>(existing.size * 2)
|
|
487
|
+
for (entry in existing) {
|
|
488
|
+
activeFiles.add(entry.diskFile)
|
|
489
|
+
entry.rawDiskFile?.let { activeFiles.add(it) }
|
|
490
|
+
}
|
|
491
|
+
synchronized(stateLock) {
|
|
492
|
+
if (diskClearGeneration == expectedGeneration) {
|
|
493
|
+
activeStartupDiskFiles.clear()
|
|
494
|
+
activeStartupDiskFiles.addAll(activeFiles)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
file.parentFile?.let { parent ->
|
|
498
|
+
pruneFallbackDiskCacheLocked(File(parent, FALLBACK_DISK_CACHE_DIR), activeFiles)
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return
|
|
502
|
+
}
|
|
406
503
|
synchronized(diskIoLock) {
|
|
407
504
|
if (synchronized(stateLock) { diskClearGeneration != expectedGeneration }) return
|
|
408
505
|
val existing = readEntries(file)
|
|
@@ -434,45 +531,56 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
434
531
|
}
|
|
435
532
|
}
|
|
436
533
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
534
|
+
val activeFiles = HashSet<String>(merged.size * 2)
|
|
535
|
+
for (entry in merged) {
|
|
536
|
+
activeFiles.add(entry.diskFile)
|
|
537
|
+
entry.rawDiskFile?.let { activeFiles.add(it) }
|
|
538
|
+
}
|
|
539
|
+
synchronized(stateLock) {
|
|
540
|
+
if (diskClearGeneration == expectedGeneration) {
|
|
541
|
+
activeStartupDiskFiles.clear()
|
|
542
|
+
activeStartupDiskFiles.addAll(activeFiles)
|
|
444
543
|
}
|
|
445
|
-
) {
|
|
446
|
-
return
|
|
447
544
|
}
|
|
448
545
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
tmp.delete()
|
|
457
|
-
}
|
|
458
|
-
val fallbackDir = File(file.parentFile, FALLBACK_DISK_CACHE_DIR)
|
|
459
|
-
if (fallbackDir.isDirectory) {
|
|
460
|
-
val activeFiles = HashSet<String>(merged.size * 2)
|
|
461
|
-
for (entry in merged) {
|
|
462
|
-
activeFiles.add(entry.diskFile)
|
|
463
|
-
entry.rawDiskFile?.let { activeFiles.add(it) }
|
|
546
|
+
val unchanged =
|
|
547
|
+
existing.size == merged.size &&
|
|
548
|
+
existing.indices.all { i ->
|
|
549
|
+
existing[i].identity == merged[i].identity &&
|
|
550
|
+
existing[i].diskFile == merged[i].diskFile &&
|
|
551
|
+
existing[i].rawDiskFile == merged[i].rawDiskFile &&
|
|
552
|
+
(currentLaunch - existing[i].launch) < 1
|
|
464
553
|
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
554
|
+
|
|
555
|
+
try {
|
|
556
|
+
if (!unchanged) {
|
|
557
|
+
file.parentFile?.mkdirs()
|
|
558
|
+
val json = encodeEntriesJson(merged)
|
|
559
|
+
val tmp = File(file.parentFile, "${file.name}.tmp")
|
|
560
|
+
tmp.writeText(json, Charsets.UTF_8)
|
|
561
|
+
if (!tmp.renameTo(file)) {
|
|
562
|
+
tmp.copyTo(file, overwrite = true)
|
|
563
|
+
tmp.delete()
|
|
469
564
|
}
|
|
470
565
|
}
|
|
566
|
+
file.parentFile?.let { parent ->
|
|
567
|
+
pruneFallbackDiskCacheLocked(File(parent, FALLBACK_DISK_CACHE_DIR), activeFiles)
|
|
568
|
+
}
|
|
471
569
|
} catch (_: Exception) {
|
|
472
570
|
}
|
|
473
571
|
}
|
|
474
572
|
}
|
|
475
573
|
|
|
574
|
+
private fun pruneFallbackDiskCacheLocked(fallbackDir: File, activeFiles: Set<String>) {
|
|
575
|
+
if (!fallbackDir.isDirectory) return
|
|
576
|
+
fallbackDir.listFiles()?.forEach { candidate ->
|
|
577
|
+
if (candidate.isFile && !activeFiles.contains(candidate.name)) {
|
|
578
|
+
candidate.delete()
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
trimFallbackDiskCacheLocked(fallbackDir, protectedKey = null)
|
|
582
|
+
}
|
|
583
|
+
|
|
476
584
|
fun startPrewarmAsync(context: Context) {
|
|
477
585
|
val appContext = context.applicationContext ?: context
|
|
478
586
|
setCacheDirIfNeeded(appContext.cacheDir)
|
|
@@ -944,12 +1052,20 @@ internal object OneKeyImageStartupPrewarm {
|
|
|
944
1052
|
}
|
|
945
1053
|
}
|
|
946
1054
|
|
|
1055
|
+
internal fun drainSchedulerForTesting() {
|
|
1056
|
+
try {
|
|
1057
|
+
scheduler.submit {}.get(5, TimeUnit.SECONDS)
|
|
1058
|
+
} catch (_: Exception) {
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
947
1062
|
internal fun resetForTesting(file: File? = null, cacheDir: File? = null) {
|
|
948
1063
|
synchronized(stateLock) {
|
|
949
1064
|
debounceFuture?.cancel(false)
|
|
950
1065
|
debounceFuture = null
|
|
951
1066
|
recorded.clear()
|
|
952
1067
|
recordedKeys.clear()
|
|
1068
|
+
activeStartupDiskFiles.clear()
|
|
953
1069
|
pendingFallbackSources.clear()
|
|
954
1070
|
recordStartTimeMs = null
|
|
955
1071
|
recordingClosed = false
|
|
@@ -306,7 +306,62 @@ class OneKeyStartupPrewarmTest {
|
|
|
306
306
|
}
|
|
307
307
|
}
|
|
308
308
|
|
|
309
|
-
// 6.
|
|
309
|
+
// 6. After the startup recorder closes, continuous promoteToMemory / writeFallbackDiskBytes
|
|
310
|
+
// for new local images must not add non-startup files, and pre-close writes stay bounded.
|
|
311
|
+
val fallbackDir = File(tempDir, OneKeyImageStartupPrewarm.FALLBACK_DISK_CACHE_DIR)
|
|
312
|
+
val baseSmallPng = createValidPng(16, 16)
|
|
313
|
+
val padded32KiB = baseSmallPng + ByteArray(OneKeyMainThreadDecode.MAX_ENCODED_BYTES - baseSmallPng.size)
|
|
314
|
+
for (i in 0 until 100) {
|
|
315
|
+
val localFile = File(tempDir, "post-close-$i.png").apply { writeBytes(baseSmallPng) }
|
|
316
|
+
val localUrl = "file://${localFile.absolutePath}"
|
|
317
|
+
assertTrue(OneKeyMainThreadDecode.promoteToMemory(tempDir, localUrl, localUrl, 32, 32, false))
|
|
318
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
319
|
+
cacheDir = tempDir,
|
|
320
|
+
safeKey = "post_close_key_$i",
|
|
321
|
+
bytes = padded32KiB,
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
OneKeyImageStartupPrewarm.drainSchedulerForTesting()
|
|
325
|
+
val postCloseFiles = fallbackDir.listFiles()?.filter { it.isFile } ?: emptyList()
|
|
326
|
+
assertEquals(
|
|
327
|
+
"No new fallback files may be written after startup recorder closes",
|
|
328
|
+
0,
|
|
329
|
+
postCloseFiles.size,
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
// 6b. Even before the startup recorder closes, fallback directory enforces file count & byte budget,
|
|
333
|
+
// including rejecting a single protected source that individually exceeds MAX_FALLBACK_DISK_BYTES.
|
|
334
|
+
OneKeyImageStartupPrewarm.resetForTesting(file = startupJsonFile, cacheDir = tempDir)
|
|
335
|
+
val oversizedSingleKey = "oversized_single_key"
|
|
336
|
+
val oversizedBytes = ByteArray(OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_BYTES.toInt() + 1)
|
|
337
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
338
|
+
cacheDir = tempDir,
|
|
339
|
+
safeKey = oversizedSingleKey,
|
|
340
|
+
bytes = oversizedBytes,
|
|
341
|
+
)
|
|
342
|
+
assertFalse(
|
|
343
|
+
"Single write exceeding MAX_FALLBACK_DISK_BYTES must be rejected before creating a file",
|
|
344
|
+
File(fallbackDir, oversizedSingleKey).exists(),
|
|
345
|
+
)
|
|
346
|
+
for (i in 0 until 100) {
|
|
347
|
+
OneKeyImageStartupPrewarm.writeFallbackDiskBytes(
|
|
348
|
+
cacheDir = tempDir,
|
|
349
|
+
safeKey = "open_window_key_$i",
|
|
350
|
+
bytes = padded32KiB,
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
val openWindowFiles = fallbackDir.listFiles()?.filter { it.isFile } ?: emptyList()
|
|
354
|
+
val openWindowBytes = openWindowFiles.sumOf { it.length() }
|
|
355
|
+
assertTrue(
|
|
356
|
+
"Fallback file count (${openWindowFiles.size}) must not exceed ${OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_FILES}",
|
|
357
|
+
openWindowFiles.size <= OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_FILES,
|
|
358
|
+
)
|
|
359
|
+
assertTrue(
|
|
360
|
+
"Fallback total bytes ($openWindowBytes) must not exceed ${OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_BYTES}",
|
|
361
|
+
openWindowBytes <= OneKeyImageStartupPrewarm.MAX_FALLBACK_DISK_BYTES,
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
// 7. clearDiskCache removes startup JSON and fallback disk cache directory,
|
|
310
365
|
// and invalidates any stale pre-clear fallback write generation.
|
|
311
366
|
val preClearGeneration = OneKeyImageStartupPrewarm.currentDiskClearGenerationForTesting()
|
|
312
367
|
OneKeyImageStartupPrewarm.clearDiskCache(tempDir)
|
|
@@ -23,6 +23,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
23
23
|
let namespace: String?
|
|
24
24
|
let width: Double
|
|
25
25
|
let height: Double
|
|
26
|
+
let scale: Double
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
struct Entry: Codable, Equatable {
|
|
@@ -32,6 +33,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
32
33
|
let namespace: String?
|
|
33
34
|
let width: Double
|
|
34
35
|
let height: Double
|
|
36
|
+
let scale: Double
|
|
35
37
|
var launch: Int
|
|
36
38
|
|
|
37
39
|
var identity: Identity {
|
|
@@ -41,7 +43,8 @@ final class OneKeyImageStartupPrewarm {
|
|
|
41
43
|
thumbDiskFile: thumbDiskFile,
|
|
42
44
|
namespace: namespace,
|
|
43
45
|
width: width,
|
|
44
|
-
height: height
|
|
46
|
+
height: height,
|
|
47
|
+
scale: scale
|
|
45
48
|
)
|
|
46
49
|
}
|
|
47
50
|
|
|
@@ -52,6 +55,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
52
55
|
case namespace
|
|
53
56
|
case width
|
|
54
57
|
case height
|
|
58
|
+
case scale
|
|
55
59
|
case launch
|
|
56
60
|
// Legacy v1 keys accepted on read only for seamless migration.
|
|
57
61
|
case url
|
|
@@ -64,6 +68,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
64
68
|
namespace: String?,
|
|
65
69
|
width: Double,
|
|
66
70
|
height: Double,
|
|
71
|
+
scale: Double = 1,
|
|
67
72
|
launch: Int = 0
|
|
68
73
|
) {
|
|
69
74
|
self.keyHash = keyHash
|
|
@@ -72,6 +77,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
72
77
|
self.namespace = namespace
|
|
73
78
|
self.width = width
|
|
74
79
|
self.height = height
|
|
80
|
+
self.scale = (scale.isFinite && scale >= 1) ? scale : 1
|
|
75
81
|
self.launch = launch
|
|
76
82
|
}
|
|
77
83
|
|
|
@@ -79,6 +85,8 @@ final class OneKeyImageStartupPrewarm {
|
|
|
79
85
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
80
86
|
let width = try container.decode(Double.self, forKey: .width)
|
|
81
87
|
let height = try container.decode(Double.self, forKey: .height)
|
|
88
|
+
let rawScale = try container.decodeIfPresent(Double.self, forKey: .scale) ?? 1
|
|
89
|
+
let scale = (rawScale.isFinite && rawScale >= 1) ? rawScale : 1
|
|
82
90
|
let launch = try container.decodeIfPresent(Int.self, forKey: .launch) ?? 0
|
|
83
91
|
if let keyHash = try container.decodeIfPresent(String.self, forKey: .keyHash),
|
|
84
92
|
let diskFile = try container.decodeIfPresent(String.self, forKey: .diskFile) {
|
|
@@ -91,6 +99,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
91
99
|
namespace: namespace,
|
|
92
100
|
width: width,
|
|
93
101
|
height: height,
|
|
102
|
+
scale: scale,
|
|
94
103
|
launch: launch
|
|
95
104
|
)
|
|
96
105
|
return
|
|
@@ -109,6 +118,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
109
118
|
namespace: resolved.namespace,
|
|
110
119
|
width: width,
|
|
111
120
|
height: height,
|
|
121
|
+
scale: resolved.scale,
|
|
112
122
|
launch: launch
|
|
113
123
|
)
|
|
114
124
|
return
|
|
@@ -130,6 +140,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
130
140
|
}
|
|
131
141
|
try container.encode(width, forKey: .width)
|
|
132
142
|
try container.encode(height, forKey: .height)
|
|
143
|
+
try container.encode(scale, forKey: .scale)
|
|
133
144
|
if launch > 0 {
|
|
134
145
|
try container.encode(launch, forKey: .launch)
|
|
135
146
|
}
|
|
@@ -223,13 +234,17 @@ final class OneKeyImageStartupPrewarm {
|
|
|
223
234
|
return name
|
|
224
235
|
}()
|
|
225
236
|
let isAvatar = requestURL.scheme?.lowercased() == "onekey-avatar"
|
|
237
|
+
let resolvedScale = (context[.imageScaleFactor] as? NSNumber)?.doubleValue
|
|
238
|
+
?? Double(SDImageScaleFactorForKey(key))
|
|
239
|
+
let scale = (resolvedScale.isFinite && resolvedScale >= 1) ? resolvedScale : 1
|
|
226
240
|
return Identity(
|
|
227
241
|
keyHash: sha256Hex(key),
|
|
228
242
|
diskFile: diskFile,
|
|
229
243
|
thumbDiskFile: thumbDiskFile,
|
|
230
244
|
namespace: isAvatar ? avatarNamespace : nil,
|
|
231
245
|
width: Double(thumbnailPixelSize.width),
|
|
232
|
-
height: Double(thumbnailPixelSize.height)
|
|
246
|
+
height: Double(thumbnailPixelSize.height),
|
|
247
|
+
scale: scale
|
|
233
248
|
)
|
|
234
249
|
}
|
|
235
250
|
|
|
@@ -285,6 +300,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
285
300
|
namespace: identity.namespace,
|
|
286
301
|
width: identity.width,
|
|
287
302
|
height: identity.height,
|
|
303
|
+
scale: identity.scale,
|
|
288
304
|
launch: 0
|
|
289
305
|
)
|
|
290
306
|
)
|
|
@@ -484,7 +500,8 @@ final class OneKeyImageStartupPrewarm {
|
|
|
484
500
|
OneKeyImageSafetyPolicy.violation(encodedByteCount: encoded.count, metadata: metadata) == nil
|
|
485
501
|
else { return }
|
|
486
502
|
let size = CGSize(width: entry.width, height: entry.height)
|
|
487
|
-
let
|
|
503
|
+
let scale = (entry.scale.isFinite && entry.scale >= 1) ? entry.scale : 1
|
|
504
|
+
var context = OneKeyImageRequestContext.make(
|
|
488
505
|
headersJson: nil,
|
|
489
506
|
cachePolicy: .memoryDisk,
|
|
490
507
|
thumbnailPixelSize: size,
|
|
@@ -492,6 +509,7 @@ final class OneKeyImageStartupPrewarm {
|
|
|
492
509
|
manager: OneKeyImagePipeline.manager,
|
|
493
510
|
url: nil
|
|
494
511
|
)
|
|
512
|
+
context[.imageScaleFactor] = NSNumber(value: scale)
|
|
495
513
|
guard let image = SDImageCacheDecodeImageData(encoded, entry.diskFile, [], context),
|
|
496
514
|
OneKeyImageSafetyPolicy.violation(for: image) == nil
|
|
497
515
|
else { return }
|
|
@@ -524,6 +542,14 @@ final class OneKeyImageStartupPrewarm {
|
|
|
524
542
|
return metrics
|
|
525
543
|
}
|
|
526
544
|
|
|
545
|
+
static func decodeForTesting(
|
|
546
|
+
data: Data,
|
|
547
|
+
cacheKey: String,
|
|
548
|
+
context: [SDWebImageContextOption: Any]
|
|
549
|
+
) -> UIImage? {
|
|
550
|
+
SDImageCacheDecodeImageData(data, cacheKey, [], context)
|
|
551
|
+
}
|
|
552
|
+
|
|
527
553
|
func flushForTesting(forceClose: Bool = true) {
|
|
528
554
|
Self.writer.sync {
|
|
529
555
|
self.lock.lock()
|
package/ios/OneKeyNamedSVG.swift
CHANGED
|
@@ -106,10 +106,15 @@ public final class OneKeyNamedSVG {
|
|
|
106
106
|
// Source digests invalidate changed assets; retain only size-based eviction.
|
|
107
107
|
config.maxDiskAge = -1
|
|
108
108
|
config.maxDiskSize = 128 * 1024 * 1024
|
|
109
|
+
config.diskCacheWritingOptions = .atomic
|
|
109
110
|
return SDImageCache(namespace: "onekey-named-svg-raster-v1", diskCacheDirectory: nil, config: config)
|
|
110
111
|
}()
|
|
111
112
|
private static let diskWriter = DispatchQueue(label: "onekey.svg.raster-disk", qos: .utility)
|
|
112
113
|
|
|
114
|
+
var rasterDiskCacheForTesting: SDImageCache {
|
|
115
|
+
Self.rasterDiskCache
|
|
116
|
+
}
|
|
117
|
+
|
|
113
118
|
private let lock = NSLock()
|
|
114
119
|
private let metadataLock = NSLock()
|
|
115
120
|
private var pending: [ObjectIdentifier: Request] = [:]
|
|
@@ -612,8 +617,10 @@ public final class OneKeyNamedSVG {
|
|
|
612
617
|
lock.unlock(); return image
|
|
613
618
|
}
|
|
614
619
|
lock.unlock()
|
|
615
|
-
// The source digest and all raster inputs are in the key; diskOnly bounds encoded byte size
|
|
620
|
+
// The source digest and all raster inputs are in the key; diskOnly bounds encoded byte size
|
|
621
|
+
// and reads directly from disk to avoid blocking on SDImageCache's shared ioQueue.
|
|
616
622
|
let diskCache = Self.rasterDiskCache
|
|
623
|
+
let diskImage: UIImage?
|
|
617
624
|
if diskOnly {
|
|
618
625
|
guard let path = diskCache.cachePath(forKey: key),
|
|
619
626
|
let attributes = try? FileManager.default.attributesOfItem(atPath: path),
|
|
@@ -626,8 +633,24 @@ public final class OneKeyNamedSVG {
|
|
|
626
633
|
OneKeyMainThreadDecode.noteLimitRejection()
|
|
627
634
|
return nil
|
|
628
635
|
}
|
|
636
|
+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: path)),
|
|
637
|
+
!data.isEmpty else {
|
|
638
|
+
OneKeyMainThreadDecode.noteDiskMiss()
|
|
639
|
+
return nil
|
|
640
|
+
}
|
|
641
|
+
guard data.count <= OneKeyMainThreadDecode.maximumEncodedBytes else {
|
|
642
|
+
OneKeyMainThreadDecode.noteLimitRejection()
|
|
643
|
+
return nil
|
|
644
|
+
}
|
|
645
|
+
diskImage = SDImageCacheDecodeImageData(
|
|
646
|
+
data,
|
|
647
|
+
key,
|
|
648
|
+
[],
|
|
649
|
+
[.imageScaleFactor: NSNumber(value: 1.0)]
|
|
650
|
+
)
|
|
651
|
+
} else {
|
|
652
|
+
diskImage = diskCache.imageFromDiskCache(forKey: key)
|
|
629
653
|
}
|
|
630
|
-
let diskImage = diskCache.imageFromDiskCache(forKey: key)
|
|
631
654
|
if let image = diskImage,
|
|
632
655
|
image.cgImage?.width == request.width, image.cgImage?.height == request.height {
|
|
633
656
|
let result = RenderResult(image: image, mode: mode, sourceIdentity: sourceIdentity, maskAlpha: maskAlpha)
|
|
@@ -608,6 +608,66 @@ final class OneKeyIconTests: XCTestCase {
|
|
|
608
608
|
XCTAssertEqual(decodeMetrics.hits, 1)
|
|
609
609
|
XCTAssertEqual(decodeMetrics.limitRejections, 1)
|
|
610
610
|
|
|
611
|
+
// 2b. Synchronous diskRender must not wait on SDImageCache.ioQueue when a concurrent
|
|
612
|
+
// write or cache cleanup is occupying ioQueue, and must handle removed files cleanly.
|
|
613
|
+
if let ioQueue = OneKeyNamedSVG.shared.rasterDiskCacheForTesting.value(forKey: "ioQueue")
|
|
614
|
+
as? DispatchQueue
|
|
615
|
+
{
|
|
616
|
+
OneKeyNamedSVG.shared.clearMemoryCacheForTesting()
|
|
617
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
618
|
+
let ioEntered = DispatchSemaphore(value: 0)
|
|
619
|
+
let ioRelease = DispatchSemaphore(value: 0)
|
|
620
|
+
let ioFinished = DispatchSemaphore(value: 0)
|
|
621
|
+
ioQueue.async {
|
|
622
|
+
ioEntered.signal()
|
|
623
|
+
_ = ioRelease.wait(timeout: .now() + 2)
|
|
624
|
+
ioFinished.signal()
|
|
625
|
+
}
|
|
626
|
+
XCTAssertEqual(ioEntered.wait(timeout: .now() + 1), .success)
|
|
627
|
+
let writerDone = DispatchSemaphore(value: 0)
|
|
628
|
+
DispatchQueue.global(qos: .utility).async {
|
|
629
|
+
OneKeyNamedSVG.shared.rasterDiskCacheForTesting.storeImageData(
|
|
630
|
+
toDisk: originalRasterData,
|
|
631
|
+
forKey: "concurrent-writer-probe"
|
|
632
|
+
)
|
|
633
|
+
writerDone.signal()
|
|
634
|
+
}
|
|
635
|
+
var finishedWhileIoBlocked = false
|
|
636
|
+
let hitWhileBusy = OneKeyNamedSVG.shared.diskRender(
|
|
637
|
+
name: "DotVerSolid",
|
|
638
|
+
color: "rgba(0,0,0,1)",
|
|
639
|
+
width: 48,
|
|
640
|
+
height: 48,
|
|
641
|
+
scale: 2
|
|
642
|
+
)
|
|
643
|
+
finishedWhileIoBlocked = (ioFinished.wait(timeout: .now()) == .timedOut)
|
|
644
|
+
ioRelease.signal()
|
|
645
|
+
_ = ioFinished.wait(timeout: .now() + 2)
|
|
646
|
+
_ = writerDone.wait(timeout: .now() + 2)
|
|
647
|
+
OneKeyNamedSVG.shared.rasterDiskCacheForTesting.removeImageFromDisk(
|
|
648
|
+
forKey: "concurrent-writer-probe"
|
|
649
|
+
)
|
|
650
|
+
XCTAssertNotNil(hitWhileBusy)
|
|
651
|
+
XCTAssertTrue(
|
|
652
|
+
finishedWhileIoBlocked,
|
|
653
|
+
"diskRender must read directly without waiting for SDImageCache.ioQueue"
|
|
654
|
+
)
|
|
655
|
+
}
|
|
656
|
+
try? FileManager.default.removeItem(at: rasterURL)
|
|
657
|
+
OneKeyNamedSVG.shared.clearMemoryCacheForTesting()
|
|
658
|
+
OneKeyMainThreadDecode.resetForTesting()
|
|
659
|
+
XCTAssertNil(
|
|
660
|
+
OneKeyNamedSVG.shared.diskRender(
|
|
661
|
+
name: "DotVerSolid",
|
|
662
|
+
color: "rgba(0,0,0,1)",
|
|
663
|
+
width: 48,
|
|
664
|
+
height: 48,
|
|
665
|
+
scale: 2
|
|
666
|
+
)
|
|
667
|
+
)
|
|
668
|
+
XCTAssertEqual(OneKeyMainThreadDecode.metricsSnapshot().diskMisses, 1)
|
|
669
|
+
try originalRasterData.write(to: rasterURL, options: .atomic)
|
|
670
|
+
|
|
611
671
|
// 3. App Lock multi-burst window + launch-generation eviction.
|
|
612
672
|
OneKeyNamedSVG.shared.resetStartupForTesting(fileURL: tempFile)
|
|
613
673
|
_ = OneKeyNamedSVG.shared.cachedRender(
|
|
@@ -1114,4 +1114,160 @@ final class OneKeyImageInfrastructureTests: XCTestCase {
|
|
|
1114
1114
|
XCTAssertNil(rejectedLarge)
|
|
1115
1115
|
XCTAssertGreaterThanOrEqual(OneKeyMainThreadDecode.metricsSnapshot().limitRejections, 1)
|
|
1116
1116
|
}
|
|
1117
|
+
|
|
1118
|
+
func testStartupPrewarmPreservesRetinaScaleLogicalSizeAndCenterDisplay() throws {
|
|
1119
|
+
let tempFile = FileManager.default.temporaryDirectory
|
|
1120
|
+
.appendingPathComponent("onekey-image-startup-scale-test-\(UUID().uuidString).json")
|
|
1121
|
+
defer {
|
|
1122
|
+
OneKeyImageStartupPrewarm.shared.resetForTesting(fileURL: nil)
|
|
1123
|
+
try? FileManager.default.removeItem(at: tempFile)
|
|
1124
|
+
}
|
|
1125
|
+
OneKeyImageStartupPrewarm.shared.resetForTesting(fileURL: tempFile)
|
|
1126
|
+
|
|
1127
|
+
let format = UIGraphicsImageRendererFormat()
|
|
1128
|
+
format.scale = 1
|
|
1129
|
+
let png40 = try XCTUnwrap(
|
|
1130
|
+
UIGraphicsImageRenderer(size: CGSize(width: 40, height: 40), format: format).image { ctx in
|
|
1131
|
+
UIColor(red: 1, green: 0, blue: 0, alpha: 1).setFill()
|
|
1132
|
+
ctx.fill(CGRect(x: 0, y: 0, width: 40, height: 40))
|
|
1133
|
+
}.pngData()
|
|
1134
|
+
)
|
|
1135
|
+
let png60 = try XCTUnwrap(
|
|
1136
|
+
UIGraphicsImageRenderer(size: CGSize(width: 60, height: 60), format: format).image { ctx in
|
|
1137
|
+
UIColor(red: 0, green: 0, blue: 1, alpha: 1).setFill()
|
|
1138
|
+
ctx.fill(CGRect(x: 0, y: 0, width: 60, height: 60))
|
|
1139
|
+
}.pngData()
|
|
1140
|
+
)
|
|
1141
|
+
|
|
1142
|
+
let url2x = try XCTUnwrap(URL(string: "https://example.com/badge@2x.png"))
|
|
1143
|
+
let url3x = try XCTUnwrap(URL(string: "https://example.com/badge@3x.png"))
|
|
1144
|
+
let viewBounds = CGSize(width: 80, height: 80)
|
|
1145
|
+
let screenScale = min(max(UIScreen.main.scale, 1), 3)
|
|
1146
|
+
let thumbSize = try XCTUnwrap(
|
|
1147
|
+
OneKeyImageDecodeSizing.thumbnailPixelSize(
|
|
1148
|
+
viewSize: viewBounds,
|
|
1149
|
+
scale: screenScale,
|
|
1150
|
+
contentFit: .center
|
|
1151
|
+
)
|
|
1152
|
+
)
|
|
1153
|
+
|
|
1154
|
+
for (url, data, expectedScale) in [(url2x, png40, CGFloat(2)), (url3x, png60, CGFloat(3))] {
|
|
1155
|
+
let originalContext = OneKeyImageRequestContext.make(
|
|
1156
|
+
headersJson: nil,
|
|
1157
|
+
cachePolicy: .memoryDisk,
|
|
1158
|
+
thumbnailPixelSize: nil,
|
|
1159
|
+
safetyTracker: nil,
|
|
1160
|
+
manager: OneKeyImagePipeline.manager,
|
|
1161
|
+
url: url
|
|
1162
|
+
)
|
|
1163
|
+
let originalKey = try XCTUnwrap(
|
|
1164
|
+
OneKeyImagePipeline.manager.cacheKey(for: url, context: originalContext)
|
|
1165
|
+
)
|
|
1166
|
+
SDImageCache.shared.storeImageData(toDisk: data, forKey: originalKey)
|
|
1167
|
+
|
|
1168
|
+
let thumbContext = OneKeyImageRequestContext.make(
|
|
1169
|
+
headersJson: nil,
|
|
1170
|
+
cachePolicy: .memoryDisk,
|
|
1171
|
+
thumbnailPixelSize: thumbSize,
|
|
1172
|
+
safetyTracker: nil,
|
|
1173
|
+
manager: OneKeyImagePipeline.manager,
|
|
1174
|
+
url: url
|
|
1175
|
+
)
|
|
1176
|
+
let thumbKey = try XCTUnwrap(
|
|
1177
|
+
OneKeyImagePipeline.manager.cacheKey(for: url, context: thumbContext)
|
|
1178
|
+
)
|
|
1179
|
+
let normalDecoded = try XCTUnwrap(
|
|
1180
|
+
OneKeyImageStartupPrewarm.decodeForTesting(
|
|
1181
|
+
data: data,
|
|
1182
|
+
cacheKey: thumbKey,
|
|
1183
|
+
context: thumbContext
|
|
1184
|
+
)
|
|
1185
|
+
)
|
|
1186
|
+
XCTAssertEqual(normalDecoded.scale, expectedScale)
|
|
1187
|
+
XCTAssertEqual(normalDecoded.size, CGSize(width: 20, height: 20))
|
|
1188
|
+
|
|
1189
|
+
OneKeyImageStartupPrewarm.shared.note(
|
|
1190
|
+
requestURL: url,
|
|
1191
|
+
rawURL: url,
|
|
1192
|
+
headersJson: nil,
|
|
1193
|
+
cachePolicy: .memoryDisk,
|
|
1194
|
+
thumbnailPixelSize: thumbSize
|
|
1195
|
+
)
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
OneKeyImageStartupPrewarm.shared.flushForTesting(forceClose: true)
|
|
1199
|
+
let jsonText = try String(contentsOf: tempFile, encoding: .utf8)
|
|
1200
|
+
XCTAssertFalse(jsonText.contains("badge@2x"))
|
|
1201
|
+
XCTAssertFalse(jsonText.contains("badge@3x"))
|
|
1202
|
+
|
|
1203
|
+
let persisted = try JSONDecoder().decode(
|
|
1204
|
+
[OneKeyImageStartupPrewarm.Entry].self,
|
|
1205
|
+
from: Data(jsonText.utf8)
|
|
1206
|
+
)
|
|
1207
|
+
XCTAssertEqual(persisted.map(\.scale), [2, 3])
|
|
1208
|
+
|
|
1209
|
+
SDImageCache.shared.clearMemory()
|
|
1210
|
+
OneKeyImageStartupPrewarm.shared.resetForTesting(fileURL: tempFile)
|
|
1211
|
+
OneKeyImageStartupPrewarm.shared.prewarm()
|
|
1212
|
+
XCTAssertEqual(OneKeyImageStartupPrewarm.shared.metricsSnapshot().decodedCount, 2)
|
|
1213
|
+
|
|
1214
|
+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
|
|
1215
|
+
for (url, expectedScale) in [(url2x, CGFloat(2)), (url3x, CGFloat(3))] {
|
|
1216
|
+
let image = HybridOneKeyImage()
|
|
1217
|
+
let hostView = try XCTUnwrap(image.view as? SDAnimatedImageView)
|
|
1218
|
+
hostView.frame = CGRect(origin: .zero, size: viewBounds)
|
|
1219
|
+
window.addSubview(hostView)
|
|
1220
|
+
defer { hostView.removeFromSuperview() }
|
|
1221
|
+
|
|
1222
|
+
var loadedWidth: Double?
|
|
1223
|
+
var loadedHeight: Double?
|
|
1224
|
+
var loadedCacheType: OneKeyImageCacheType?
|
|
1225
|
+
image.onLoad = { width, height, cacheType in
|
|
1226
|
+
loadedWidth = width
|
|
1227
|
+
loadedHeight = height
|
|
1228
|
+
loadedCacheType = cacheType
|
|
1229
|
+
}
|
|
1230
|
+
image.optimizeTos = false
|
|
1231
|
+
image.contentFit = .center
|
|
1232
|
+
image.sourceUri = url.absoluteString
|
|
1233
|
+
image.afterUpdate()
|
|
1234
|
+
hostView.layoutIfNeeded()
|
|
1235
|
+
|
|
1236
|
+
let displayed = try XCTUnwrap(hostView.image)
|
|
1237
|
+
XCTAssertEqual(displayed.scale, expectedScale)
|
|
1238
|
+
XCTAssertEqual(displayed.size, CGSize(width: 20, height: 20))
|
|
1239
|
+
XCTAssertEqual(hostView.contentMode, .center)
|
|
1240
|
+
XCTAssertEqual(loadedWidth, 20)
|
|
1241
|
+
XCTAssertEqual(loadedHeight, 20)
|
|
1242
|
+
XCTAssertEqual(loadedCacheType, .memory)
|
|
1243
|
+
|
|
1244
|
+
let renderFormat = UIGraphicsImageRendererFormat()
|
|
1245
|
+
renderFormat.scale = 1
|
|
1246
|
+
let snapshot = UIGraphicsImageRenderer(size: viewBounds, format: renderFormat).image { ctx in
|
|
1247
|
+
hostView.layer.render(in: ctx.cgContext)
|
|
1248
|
+
}
|
|
1249
|
+
let cgSnapshot = try XCTUnwrap(snapshot.cgImage)
|
|
1250
|
+
var pixels = [UInt8](repeating: 0, count: 80 * 80 * 4)
|
|
1251
|
+
let bitmapCtx = try XCTUnwrap(
|
|
1252
|
+
CGContext(
|
|
1253
|
+
data: &pixels,
|
|
1254
|
+
width: 80,
|
|
1255
|
+
height: 80,
|
|
1256
|
+
bitsPerComponent: 8,
|
|
1257
|
+
bytesPerRow: 80 * 4,
|
|
1258
|
+
space: CGColorSpaceCreateDeviceRGB(),
|
|
1259
|
+
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
|
|
1260
|
+
)
|
|
1261
|
+
)
|
|
1262
|
+
bitmapCtx.draw(cgSnapshot, in: CGRect(origin: .zero, size: viewBounds))
|
|
1263
|
+
let centerAlpha = pixels[(40 * 80 + 40) * 4 + 3]
|
|
1264
|
+
let outerRingAlpha = pixels[(15 * 80 + 15) * 4 + 3]
|
|
1265
|
+
XCTAssertEqual(centerAlpha, 255, "Centered 20x20pt image must cover (40,40)")
|
|
1266
|
+
XCTAssertEqual(
|
|
1267
|
+
outerRingAlpha,
|
|
1268
|
+
0,
|
|
1269
|
+
"Outer ring (15,15) must stay transparent (would be covered if scale=1 60x60pt)"
|
|
1270
|
+
)
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1117
1273
|
}
|
package/package.json
CHANGED