Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-photo

Source of release 0.1.1

0.1.1 / src / rerender.lucb

## Re-rendering photos: after a photo's develop changes, or when one is shown
## without a developed picture of its settings, its developed picture (3072 px,
## stamped with the settings it was made with) and grid thumbnail are developed
## again on the CPU (luce-develop's process) on a thread of their own, so the
## grid shows edits and the Library mode shows the photo without developing it. A newer request
## for the same photo replaces one still waiting; the window polls which photos
## are done and reloads their cells. Each request names its library, so one
## asked for before the window opened another library still lands in its own.
import c
import interop
import memory
import strings
import sync
import thread
from luce_std.collections import Buffer, List
from luce_develop import process
from luce_develop.settings import parse
from luce_std import files
import library
from luce_ui import ui

struct Job:
    var id: i64
    ## NUL-terminated heap copies (strings.copy).
    var root: str
    var original: str
    var settings: str

    func release():
        strings.release(self.root)
        strings.release(self.original)
        strings.release(self.settings)

struct Shared:
    var lock: sync.Mutex
    var wake: sync.Condition
    var jobs: List[Job]
    var done: List[i64]
    ## The photo being developed now (-1: none).
    var active: i64
    var stopping: bool
    ## Who to tell when there is something to take: the window's notifier,
    ## held for this thread (luce-ui's notify_handle), none before `wake_with`.
    var waker: ui.NotifyHandle?

func work(context: void*?):
    let shared = (Shared*)(context else return)
    var path: Buffer
    defer path.destroy()
    while true:
        shared.lock.lock()
        while shared.jobs.length() == 0 and not shared.stopping:
            shared.wake.wait(&shared.lock)
        if shared.stopping:
            shared.lock.unlock()
            return
        let job = shared.jobs.at(0)
        shared.jobs.remove(0)
        shared.active = job.id
        shared.lock.unlock()
        render(&job, &path) catch failure:
            _ = failure
        job.release()
        shared.lock.lock()
        shared.active = -1
        shared.done.append(job.id) catch failure:
            _ = failure
        if let waker = shared.waker:
            waker.notify()
        shared.lock.unlock()

## Develop `job`'s photo into its developed picture, its stamp, and its
## thumbnail. The stamp is written last, so a picture half-written is never
## taken for one of these settings.
func render(job: const Job*, path: Buffer*) -> !:
    let root = job.root
    let settings = parse(job.settings)
    var opened = try process.opened(job.original, &settings, (usize)library.developed_edge)
    defer opened.close()
    var developed = try process.render(&opened, settings)
    defer developed.close()
    try library.cache_path(root, "developed", job.id, path)
    try developed.save_jpeg(path.text(), 92)
    try library.stamp_path(root, job.id, path)
    try files.write_text(path.text(), job.settings)
    var thumb = try developed.shrunk((usize)library.thumb_edge)
    defer thumb.close()
    try library.cache_path(root, "thumbs", job.id, path)
    try thumb.save_jpeg(path.text(), 82)

pub struct Rerenderer:
    var shared: Shared*?
    var worker: thread.Handle

    ## Develop photo `id` of the library at `root` from `original` with
    ## `settings` (text form) into its cached preview and thumbnail.
    pub func request(id: i64, root: str, original: str, settings: str) -> !:
        let shared = self.shared else return
        let job = Job(id = id, root = try strings.copy(root), original = try strings.copy(original), settings = try strings.copy(settings))
        shared.lock.lock()
        var at: usize = 0
        while at < shared.jobs.length():
            if shared.jobs.at(at).id == id:
                shared.jobs.at(at).release()
                shared.jobs.remove(at)
            else:
                at += 1
        shared.jobs.append(job) catch failure:
            shared.lock.unlock()
            job.release()
            error(failure.code, failure.message)
        shared.lock.unlock()
        shared.wake.signal()

    ## Notify `notifier` whenever something finishes, so the window takes it
    ## on its loop's next turn instead of polling.
    pub func wake_with(notifier: interop.Reference[ui.Notifier]) -> !:
        let shared = self.shared else return
        let handle = try ui.notify_handle(notifier)
        shared.lock.lock()
        if let old = shared.waker:
            old.release()
        shared.waker = handle
        shared.lock.unlock()

    ## How many photos are waiting to be developed again, the one under way included.
    pub func waiting() -> i64:
        let shared = self.shared else return 0
        shared.lock.lock()
        defer shared.lock.unlock()
        return (i64)shared.jobs.length() + (1 if shared.active >= 0 else 0)

    ## Whether photo `id` is waiting to be developed again (or being developed).
    pub func is_waiting(id: i64) -> bool:
        let shared = self.shared else return false
        shared.lock.lock()
        defer shared.lock.unlock()
        if shared.active == id:
            return true
        for job in shared.jobs.view():
            if job.id == id:
                return true
        return false

    ## The next photo whose files were made again, or -1.
    pub func take() -> i64:
        let shared = self.shared else return -1
        shared.lock.lock()
        defer shared.lock.unlock()
        if shared.done.length() == 0:
            return -1
        let id = shared.done.at(0)
        shared.done.remove(0)
        return id

    pub func close():
        let shared = self.shared else return
        shared.lock.lock()
        shared.stopping = true
        shared.lock.unlock()
        shared.wake.broadcast()
        self.worker.join() catch failure:
            trap("the re-rendering thread could not be joined")
        for job in shared.jobs.view():
            job.release()
        shared.jobs.destroy()
        shared.done.destroy()
        if let waker = shared.waker:
            waker.release()
        free(shared) in memory.heap
        self.shared = none

pub let rerenderer_type: interop.Type[Rerenderer] = interop.Type[Rerenderer]("Rerenderer", Rerenderer.close, closeable = true, main_thread = true)

## A re-renderer, its thread started.
pub func start() -> interop.Reference[Rerenderer]!:
    let shared = try new Shared(active = -1) in memory.heap
    errdefer free(shared) in memory.heap
    let worker = try thread.spawn(work, (void*)shared, name = "luced-photo rerender")
    let made = try new Rerenderer(shared = shared, worker = worker) in memory.heap
    return try interop.Reference[Rerenderer].adopt(made, rerenderer_type, memory.heap)