/* image-display-thumb.js —— 画布内大图的显示缩略图生成队列(v3.25.67 起,v3.25.73 重写) * * 职责:把一张原图(blob:/http/dataURL)变成 ≤768 的显示缩略图,并交给宿主缓存/落盘。 * * v3.25.73(参考 basketikun/infinite-canvas 的 image-thumbnail.ts): * ① 优先 createImageBitmap(blob) → canvas → toBlob('image/webp', 0.86) → **objectURL** * —— 解码在目标尺寸完成,产物是 Blob(objectURL)而不是 dataURL,不常驻 JS 堆; * ② 拿不到字节(跨域等)才回落到 + canvas + dataURL(老路径); * ③ 并发上限 2 + requestIdleCallback 空闲启动,避免打开画布时同时解码几十张; * ④ 宿主注入 save(key, blobOrDataUrl):负责落盘(文件存储)。 */ ;(function (global) { 'use strict' function create (opts) { const o = opts || {} // v3.26.109:显示缩略图 1024px / WebP 0.72。 // 640 时代的问题是不够用:画布 LOD 规则 need=渲染长边×缩放×dpr,dpr=2 时节点宽 400px 就需要 800 // ⇒ 直接改解码原图(一张 1.9MB 原图解出来 6~20MB,实测 WebContent footprint 冲到 1.2G)。 // 1024 让多数缩放档留在缩略图上;单张约 60~110KB,缓存 300 张约 30MB,远小于原图解码。 const max = Math.max(64, Number(o.max) || 640) const cacheMax = Math.max(16, Number(o.cacheMax) || 300) const concurrency = Math.max(1, Number(o.concurrency) || 2) const cache = o.cache const pending = o.pending if (!cache || !pending) throw new Error('cache/pending required') const queue = [] let running = 0 let scheduled = false const remember = (key, val) => { if (cache.size >= cacheMax) { const first = cache.keys().next().value if (first !== undefined) cache.delete(first) } cache.set(key, val) } const bump = () => { try { if (typeof o.bump === 'function') o.bump() } catch (e) { /* ignore */ } } // v3.26.112:大于 512KB 的 dataURL **绝不能**当缩略图缓存。 // 生成失败/降级路径原来会 remember(key, 原图 dataURL),画布随后把它当缩略图渲染, // 结果就是每个 都是 2MB 级 base64(Windows 实测 111 张 / 260MB)。 const tooBig = (x) => typeof x === 'string' && x.indexOf('data:') === 0 && x.length > 512 * 1024 // v3.26.112:缩略图生成失败的**原因**要留证据(每会话最多 20 条),否则只能看到"没有缩略图"。 let failLogs = 0 const noteFail = (why, key, v) => { try { if (failLogs >= 20) return failLogs++ const S = global.IWBStorageFile if (!S || typeof S.logLine !== 'function') return S.logLine('thumb-fail ' + why + ' key=' + String(key || '').slice(0, 40) + ' src=' + String(v || '').slice(0, 32) + ' len=' + (typeof v === 'string' ? v.length : -1)) } catch (e) { /* ignore */ } } const save = (key, payload) => { try { if (typeof o.save === 'function') o.save(key, payload) } catch (e) { /* ignore */ } } // ① 优先路径:字节 → bitmap → 缩放 → WebP Blob → objectURL const runBitmap = async (key, v) => { try { window.IWBPerf && window.IWBPerf.tag('thumb') } catch (e) { /* ignore */ } if (typeof fetch !== 'function' || typeof createImageBitmap !== 'function') return false const resp = await fetch(v) if (!resp || !resp.ok) return false const blob = await resp.blob() // v3.26.121(参考 Chromium 的 ImageDecodeCache 与 tldraw 的取图方式): // 先解码再缩放 = 整个原图(2016×2688 ≈ 21MB)先落地一次;改成在解码阶段就按目标宽度降采样, // 峰值内存与 CPU 都随 max 走,不随原图走。只给 resizeWidth(等比),纵向超出的再补一次 canvas 缩放。 let bitmap = null const decodeOpts = (typeof max === 'number' && max > 0) ? /** @type {ImageBitmapOptions} */ ({ resizeWidth: max, resizeQuality: 'medium' }) : undefined try { bitmap = decodeOpts ? await createImageBitmap(blob, decodeOpts) : await createImageBitmap(blob) } catch (e) { bitmap = await createImageBitmap(blob) } const iw = bitmap.width || 0 const ih = bitmap.height || 0 try { if (!iw || !ih) return false if (iw <= max && ih <= max) { if (!tooBig(v)) remember(key, v) return true } const scale = Math.min(max / Math.max(1, iw), max / Math.max(1, ih)) const cw = Math.max(1, Math.round(iw * scale)) const ch = Math.max(1, Math.round(ih * scale)) const cv = document.createElement('canvas') cv.width = cw cv.height = ch const ctx = cv.getContext('2d') if (!ctx) return false ctx.drawImage(bitmap, 0, 0, cw, ch) const small = await new Promise((res) => { try { cv.toBlob((b) => res(b || null), 'image/webp', 0.72) } catch (e) { res(null) } }) if (!small) return false const url = URL.createObjectURL(small) remember(key, url) save(key, small) return true } finally { try { bitmap.close() } catch (e) { /* ignore */ } } } // ② 回落路径: + canvas → dataURL(跨域时 fetch 会失败) const runImg = (key, v) => new Promise((resolve) => { if (typeof document === 'undefined' || typeof Image !== 'function') { resolve(false); return } const img = new Image() try { img.decoding = 'async' } catch (e) { /* ignore */ } img.onload = () => { try { const iw = img.naturalWidth || 0 const ih = img.naturalHeight || 0 if (iw <= max && ih <= max) { if (!tooBig(v)) remember(key, v); resolve(true); return } const scale = Math.min(max / Math.max(1, iw), max / Math.max(1, ih)) const cw = Math.max(1, Math.round(iw * scale)) const ch = Math.max(1, Math.round(ih * scale)) const cv = document.createElement('canvas') cv.width = cw cv.height = ch const ctx = cv.getContext('2d') if (!ctx) { resolve(false); return } ctx.drawImage(img, 0, 0, cw, ch) const small = cv.toDataURL('image/jpeg', 0.72) remember(key, small) save(key, small) resolve(true) } catch (e) { noteFail('canvas', key, v); if (!tooBig(v)) remember(key, v); resolve(false) } } img.onerror = () => { try { noteFail('img-error', key, v); if (!tooBig(v) && !(typeof o.isTransient === 'function' && o.isTransient(v))) remember(key, v) } catch (e) { /* ignore */ } resolve(false) } 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 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 } const pump = () => { while (running < concurrency && queue.length) { const job = queue.shift() running++ run(job.key, job.v).catch(() => { /* ignore */ }).then(() => { running-- try { pending.delete(job.key) } catch (e) { /* ignore */ } pump() }) } } const schedule = () => { if (scheduled) return scheduled = true const kick = () => { scheduled = false; pump() } try { if (typeof requestIdleCallback === 'function') requestIdleCallback(kick, { timeout: 1200 }) else setTimeout(kick, 60) } catch (e) { setTimeout(kick, 60) } } return { queue: function (key, v) { if (!key || cache.has(key) || pending.has(key)) return false pending.add(key) queue.push({ key: key, v: v }) schedule() return true }, stats: function () { return { queued: queue.length, running: running } } } } global.IWBImageDisplayThumb = { create: create } })(typeof window !== 'undefined' ? window : globalThis)