feat(perf): v3.26.123 缩略图改由原生生成(webview 不再解码 4K 原图)
This commit is contained in:
1 parent
602d6ce1da
commit
309963c7b3
9 files changed
+220
-14
No files matched your search
@@ -128,9 +128,23 @@
|
||||
img.src = v
|
||||
})
|
||||
|
||||
// v3.26.123:优先让**原生**生成缩略图(Rust image crate)。桌面端有这条路时,
|
||||
// webview 永远不解码原图 —— WebKit 忽略 createImageBitmap 的 resize 选项,
|
||||
// 一解码就是 21MB/张,100 张直接把进程顶到 GB 级(mac 实测 1.8GB)。
|
||||
const nativeThumb = (typeof o.nativeThumb === 'function') ? o.nativeThumb : null
|
||||
const runNative = async (key) => {
|
||||
if (!nativeThumb) return false
|
||||
let url = ''
|
||||
try { url = await nativeThumb(key, max) } catch (e) { return false }
|
||||
if (!url || typeof url !== 'string') return false
|
||||
remember(key, url)
|
||||
return true
|
||||
}
|
||||
|
||||
const run = async (key, v) => {
|
||||
let ok = false
|
||||
try { ok = await runBitmap(key, v) } catch (e) { ok = false }
|
||||
try { ok = await runNative(key) } catch (e) { ok = false }
|
||||
if (!ok) { try { ok = await runBitmap(key, v) } catch (e) { ok = false } }
|
||||
if (!ok) { try { ok = await runImg(key, v) } catch (e2) { ok = false } }
|
||||
if (ok) bump()
|
||||
return ok
|
||||
|
||||
+32
-7
@@ -1101,8 +1101,8 @@
|
||||
return ''
|
||||
}
|
||||
|
||||
async function thumbFileOf (hash) {
|
||||
const base = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768')
|
||||
async function thumbFileOf (hash, max) {
|
||||
const base = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_' + (Math.max(64, Number(max) || 768)))
|
||||
for (const ext of ['.webp', '.jpg']) {
|
||||
try { if (await fs().exists(base + ext)) return { path: base + ext, mime: ext === '.webp' ? 'image/webp' : 'image/jpeg' } } catch (e) { /* ignore */ }
|
||||
}
|
||||
@@ -1120,17 +1120,18 @@
|
||||
}
|
||||
// v3.25.73:缩略图以 Blob + objectURL 交给显示层(避免 dataURL 常驻 JS 堆)
|
||||
const thumbUrlCache = new Map() // hash → objectURL
|
||||
async function readDisplayThumbUrl(hash) {
|
||||
async function readDisplayThumbUrl(hash, max) {
|
||||
try {
|
||||
if (!enabled() || !dir || !hash) return ''
|
||||
const hit = thumbUrlCache.get(String(hash))
|
||||
const ck = String(hash) + '_' + (Math.max(64, Number(max) || 768))
|
||||
const hit = thumbUrlCache.get(ck)
|
||||
if (hit) return hit
|
||||
const f = await thumbFileOf(hash)
|
||||
const f = await thumbFileOf(hash, max)
|
||||
if (!f) return ''
|
||||
const u8 = await fs().readFile(f.path)
|
||||
if (!u8 || !u8.length) return ''
|
||||
const url = URL.createObjectURL(new Blob([u8], { type: sniffMime(u8) || f.mime }))
|
||||
thumbUrlCache.set(String(hash), url)
|
||||
thumbUrlCache.set(ck, url)
|
||||
// 不 revoke:这个 URL 可能正在被画布上的 <img> 使用,撤销会变成黑块/破图。
|
||||
if (thumbUrlCache.size > 600) { const k = thumbUrlCache.keys().next().value; try { thumbUrlCache.delete(k) } catch (e) { /* ignore */ } }
|
||||
return url
|
||||
@@ -1205,6 +1206,30 @@
|
||||
return { thumbs: a, legacy: b }
|
||||
}
|
||||
|
||||
// v3.26.123:原生缩略图。有就返回,没有就让 Rust 按目标尺寸解码/缩放/编码后写盘,再读成 objectURL。
|
||||
// 关键:整条链里 webview 只接触几十 KB 的小图,4K 原图永远不会被 WebKit 解码(mac 端 GB 级占用的根源)。
|
||||
async function makeDisplayThumb (hash, srcKey, max) {
|
||||
try {
|
||||
if (!enabled() || !dir || !hash || !srcKey) return null
|
||||
const m = Math.max(64, Number(max) || 768)
|
||||
const have = await thumbFileOf(hash, m)
|
||||
if (have) return { url: await readDisplayThumbUrl(hash, m), path: have.path, cached: true }
|
||||
const a = assets[String(srcKey)]
|
||||
if (!a) return null
|
||||
const src = await join(dir, a.f)
|
||||
const base = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_' + m)
|
||||
try { await fs().mkdir(await join(dir, IMG_DIR, 'thumbs'), { recursive: true }) } catch (e) { /* ignore */ }
|
||||
const T = (typeof window !== 'undefined') ? window.__TAURI__ : null
|
||||
const inv = T && T.core && T.core.invoke
|
||||
if (typeof inv !== 'function') return null
|
||||
const r = await inv('img_thumbs', { src, jobs: [{ base, max: m }] })
|
||||
if (!r || !r.ok || !Array.isArray(r.files) || !r.files.length) return null
|
||||
const url = await readDisplayThumbUrl(hash, m)
|
||||
if (!url) return null
|
||||
return { url, path: r.files[0].path, w: r.files[0].w, h: r.files[0].h, bytes: r.files[0].bytes, cached: false }
|
||||
} catch (e) { return null }
|
||||
}
|
||||
|
||||
async function writeDisplayThumbBlob(hash, blob) {
|
||||
try {
|
||||
if (!enabled() || !dir || !hash || !blob) return false
|
||||
@@ -1423,7 +1448,7 @@
|
||||
enabled, ready: init, flush,
|
||||
// 供其它模块把诊断写进 _boot.log(v3.26.109)
|
||||
logLine: (m) => blog(m),
|
||||
runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain,
|
||||
runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, makeDisplayThumb, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain,
|
||||
plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal, listDirs: pluginListDirs, readFile: pluginReadFile, writeFile: pluginWriteFile, writeBinary: pluginWriteBinary, mkdir: pluginMkdir, removeDir: pluginRemoveDir, renameDir: pluginRenameDir, existsDir: pluginExistsDir },
|
||||
pluginData: { read: pluginDataRead, write: pluginDataWrite, remove: pluginDataRemove, list: pluginDataList },
|
||||
pluginFiles: { put: pluginFilePut, get: pluginFileGet, list: pluginFileList, remove: pluginFileRemove, stat: pluginFileStat, dir: pluginFilesDir, readBytes: pluginFileReadBytes, listAll: pluginFilesListAll },
|
||||
|
||||
Reference in new issue
Block a user