aboutsummaryrefslogtreecommitdiffstats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-09-19 17:46:27 +0200
committerbptato <nincsnevem662@gmail.com>2024-09-22 22:44:53 +0200
commit080493c058f52a5c20638f1b975d032af45f4d3f (patch)
tree60e6ba6b3cb967d29d349018b3f315e7637b4b9e /src/local
parente23fa780cf2fff7146efcd64b2806ce428858b80 (diff)
downloadchawan-080493c058f52a5c20638f1b975d032af45f4d3f.tar.gz
loader: mmap intermediate image files, misc refactoring
* refactor parseHeader * optimize response blob() * add direct "to cache" mode for loader requests which sets stdout to a file, and use it for image processing * move image resizing into a separate process * mmap cache files in between processing steps when possible At last, resize is no longer a part of image decoding. Also, it feels much nicer to keep encoded image data in the same cache as everything else. The mmap operations *should* be more efficient than copying the whole RGBA data through a pipe. In practice, it only makes a difference for loading (well, now just mmapping) the encoded image into the pager, where it singlehandedly speeds up image display by 10x on my test image. For the other steps, the unfortunate fact that "tocache" must delay the next fork/exec in the pipeline until the entire image is processed seems to equal out any wins we might have gotten from skipping a single raw RGBA copy. I have tried moving the delay before the exec (it's possible with yet another pipe), but it didn't help much and made the code much uglier. (Not that tocache didn't, but I can live with this...)
Diffstat (limited to 'src/local')
-rw-r--r--src/local/client.nim6
-rw-r--r--src/local/container.nim8
-rw-r--r--src/local/pager.nim101
3 files changed, 88 insertions, 27 deletions
diff --git a/src/local/client.nim b/src/local/client.nim
index c8bf9b24..c20a460b 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -356,7 +356,7 @@ proc input(client: Client): EmptyPromise =
p.resolve()
return p
-when not defined(android):
+when ioselSupportedPlatform:
let SIGWINCH {.importc, header: "<signal.h>", nodecl.}: cint
proc showConsole(client: Client) {.jsfunc.} =
@@ -542,7 +542,7 @@ proc handleError(client: Client; fd: int) =
proc inputLoop(client: Client) =
let selector = client.selector
selector.registerHandle(int(client.pager.term.istream.fd), {Read}, 0)
- when not defined(android):
+ when ioselSupportedPlatform:
let sigwinch = selector.registerSignal(int(SIGWINCH), 0)
var keys: array[64, ReadyKey]
while true:
@@ -554,7 +554,7 @@ proc inputLoop(client: Client) =
client.handleWrite(event.fd)
if Error in event.events:
client.handleError(event.fd)
- when not defined(android):
+ when ioselSupportedPlatform:
if Signal in event.events:
assert event.fd == sigwinch
client.pager.windowChange()
diff --git a/src/local/container.nim b/src/local/container.nim
index 8f295ad9..e22f0f4e 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -101,11 +101,15 @@ type
ContainerFlag* = enum
cfCloned, cfUserRequested, cfHasStart, cfCanReinterpret, cfSave, cfIsHTML
+ CachedImageState* = enum
+ cisLoading, cisCanceled, cisLoaded
+
CachedImage* = ref object
- loaded*: bool
+ state*: CachedImageState
width*: int
height*: int
- data*: Blob
+ data*: Blob # mmapped blob of image data
+ cacheId*: int # cache id of the file backing "data"
bmp*: NetworkBitmap
# Following variables are always 0 in kitty mode; they exist to support
# sixel cropping.
diff --git a/src/local/pager.nim b/src/local/pager.nim
index fa205596..d6e31e2a 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -214,6 +214,10 @@ proc clearStatus(pager: Pager) =
proc setContainer*(pager: Pager; c: Container) {.jsfunc.} =
if pager.term.imageMode != imNone and pager.container != nil:
+ for cachedImage in pager.container.cachedImages:
+ if cachedImage.state == cisLoaded:
+ pager.loader.removeCachedItem(cachedImage.cacheId)
+ cachedImage.state = cisCanceled
pager.container.cachedImages.setLen(0)
pager.container = c
if c != nil:
@@ -503,6 +507,9 @@ proc redraw(pager: Pager) {.jsfunc.} =
if pager.container.select != nil:
pager.container.select.redraw = true
+proc getTempFile(pager: Pager; ext = ""): string =
+ return getTempFile(pager.config.external.tmpdir, ext)
+
proc loadCachedImage(pager: Pager; container: Container; image: PosBitmap;
offx, erry, dispw: int) =
let bmp = image.bmp
@@ -517,20 +524,53 @@ proc loadCachedImage(pager: Pager; container: Container; image: PosBitmap;
pager.loader.shareCachedItem(bmp.cacheId, pager.loader.clientPid,
container.process)
let imageMode = pager.term.imageMode
- let headers = newHeaders()
- if image.width != bmp.width or image.height != bmp.height:
- headers.add("Cha-Image-Target-Dimensions", $image.width & 'x' &
- $image.height)
pager.loader.fetch(newRequest(
newURL("img-codec+" & bmp.contentType.after('/') & ":decode").get,
httpMethod = hmPost,
- headers = headers,
body = RequestBody(t: rbtCache, cacheId: bmp.cacheId),
- )).then(proc(res: JSResult[Response]) =
+ tocache = true
+ )).then(proc(res: JSResult[Response]): FetchPromise =
+ # remove previous step
+ pager.loader.removeCachedItem(bmp.cacheId)
+ if res.isNone:
+ return nil
+ let response = res.get
+ let cacheId = response.outputId # set by loader in tocache
+ if cachedImage.state == cisCanceled: # container is no longer visible
+ pager.loader.removeCachedItem(cacheId)
+ return nil
+ if image.width == bmp.width and image.height == bmp.height:
+ # skip resize
+ return newResolvedPromise(res)
+ # resize
+ # use a temp file, so that img-resize can mmap its output
+ let headers = newHeaders({
+ "Cha-Image-Dimensions": $bmp.width & 'x' & $bmp.height,
+ "Cha-Image-Target-Dimensions": $image.width & 'x' & $image.height
+ })
+ let p = pager.loader.fetch(newRequest(
+ newURL("cgi-bin:resize").get,
+ httpMethod = hmPost,
+ headers = headers,
+ body = RequestBody(t: rbtCache, cacheId: cacheId),
+ tocache = true
+ )).then(proc(res: JSResult[Response]): FetchPromise =
+ # ugh. I must remove the previous cached item, but only after
+ # resize is done...
+ pager.loader.removeCachedItem(cacheId)
+ return newResolvedPromise(res)
+ )
+ response.resume()
+ response.close()
+ return p
+ ).then(proc(res: JSResult[Response]) =
if res.isNone:
- pager.loader.removeCachedItem(bmp.cacheId)
return
let response = res.get
+ let cacheId = response.outputId
+ if cachedImage.state == cisCanceled:
+ pager.loader.removeCachedItem(cacheId)
+ return
let headers = newHeaders({
"Cha-Image-Dimensions": $image.width & 'x' & $image.height
})
@@ -550,23 +590,40 @@ proc loadCachedImage(pager: Pager; container: Container; image: PosBitmap;
url,
httpMethod = hmPost,
headers = headers,
- body = RequestBody(t: rbtOutput, outputId: response.outputId),
+ body = RequestBody(t: rbtCache, cacheId: cacheId),
+ tocache = true
)
let r = pager.loader.fetch(request)
response.resume()
response.close()
- r.then(proc(res: JSResult[Response]): Promise[JSResult[Blob]] =
+ r.then(proc(res: JSResult[Response]) =
+ # remove previous step
+ pager.loader.removeCachedItem(cacheId)
if res.isNone:
- let p = newPromise[JSResult[Blob]]()
- p.resolve(JSResult[Blob].err(res.error))
- return p
- return res.get.blob()
- ).then(proc(res: JSResult[Blob]) =
- if res.isSome:
- container.redraw = true
- cachedImage.data = res.get
- cachedImage.loaded = true
- pager.loader.removeCachedItem(bmp.cacheId)
+ return
+ let cacheId = res.get.outputId
+ if cachedImage.state == cisCanceled:
+ pager.loader.removeCachedItem(cacheId)
+ return
+ let ps = pager.loader.openCachedItem(cacheId)
+ if ps == nil:
+ pager.loader.removeCachedItem(cacheId)
+ return
+ let mem = ps.recvDataLoopOrMmap()
+ ps.sclose()
+ if mem == nil:
+ pager.loader.removeCachedItem(cacheId)
+ return
+ let blob = newBlob(mem.p, mem.len, "image/x-sixel",
+ (proc(opaque, p: pointer) =
+ let mem = cast[MaybeMappedMemory](opaque)
+ dealloc(mem)
+ ), mem
+ )
+ container.redraw = true
+ cachedImage.data = blob
+ cachedImage.state = cisLoaded
+ cachedImage.cacheId = cacheId
)
)
container.cachedImages.add(cachedImage)
@@ -592,7 +649,7 @@ proc initImages(pager: Pager; container: Container) =
if cached == nil:
pager.loadCachedImage(container, image, offx, erry, dispw)
continue
- if not cached.loaded:
+ if cached.state != cisLoaded:
continue # loading
let canvasImage = pager.term.loadImage(cached.data, container.process,
imageId, image.x - container.fromx, image.y - container.fromy,
@@ -1110,7 +1167,7 @@ proc getEditorCommand(pager: Pager; file: string; line = 1): string {.jsfunc.} =
proc openInEditor(pager: Pager; input: var string): bool =
try:
- let tmpf = getTempFile(pager.config.external.tmpdir)
+ let tmpf = pager.getTempFile()
if input != "":
writeFile(tmpf, input)
let cmd = pager.getEditorCommand(tmpf)
@@ -1815,7 +1872,7 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
if entry == nil:
return CheckMailcapResult(connect: true, fdout: stream.fd, found: false)
let ext = url.pathname.afterLast('.')
- let tempfile = getTempFile(pager.config.external.tmpdir, ext)
+ let tempfile = pager.getTempFile(ext)
let outpath = if entry.nametemplate != "":
unquoteCommand(entry.nametemplate, contentType, tempfile, url)
else: