///| Images the compositor loaded (app icons) and the `Image` draw op that shows them. ///| Loaded images; handle `i + 1` is `images[i]` (0 means "no image"). let images : Array[Pixmap] = [] ///| Each image scaled to the sizes it has been drawn at, by (handle, w, h). let scaled : Map[(UInt, Int, Int), Pixmap] = {} ///| Keep `p` for `Image` ops and return its handle (never 0). pub fn add_image(p : Pixmap) -> UInt { images.push(p) images.length().reinterpret_as_uint() } ///| Put `p` in place of image `handle` (from `add_image`), dropping its cached sizes; a handle that /// isn't one gets `p` added instead. Returns the handle `p` is under. pub fn set_image(handle : UInt, p : Pixmap) -> UInt { let i = handle.reinterpret_as_int() - 1 if i < 0 || i >= images.length() { return add_image(p) } images[i] = p let stale = scaled.keys().filter(k => k.0 == handle).collect() for k in stale { scaled.remove(k) } handle } ///| A `w`x`h` preview of a client buffer (wl_shm ARGB8888/XRGB8888, little-endian B, G, R, A). /// Each target pixel averages a few points spread over the source pixels it covers (at most 3x3), /// so a big window costs about the same as a small one: this runs on commits while the Alt+Tab /// switcher is up, not once per source pixel. pub fn shm_thumbnail(buf : @scene.ShmBufferSnapshot, w : Int, h : Int) -> Pixmap? { if w <= 0 || h <= 0 || buf.width <= 0 || buf.height <= 0 || buf.stride < buf.width * 4 || buf.bytes.length() < buf.stride * (buf.height - 1) + buf.width * 4 { return None } let out = Pixmap::new(w, h) let xrgb = buf.format == 1 let src = buf.bytes for y = 0; y < h; y = y + 1 { let y0 = y * buf.height / h let y1 = { let e = (y + 1) * buf.height / h; if e > y0 { e } else { y0 + 1 } } let ny = if y1 - y0 < 3 { y1 - y0 } else { 3 } for x = 0; x < w; x = x + 1 { let x0 = x * buf.width / w let x1 = { let e = (x + 1) * buf.width / w; if e > x0 { e } else { x0 + 1 } } let nx = if x1 - x0 < 3 { x1 - x0 } else { 3 } let mut r = 0 let mut g = 0 let mut b = 0 let mut a = 0 for j = 0; j < ny; j = j + 1 { let sy = y0 + (y1 - y0) * (2 * j + 1) / (2 * ny) for i = 0; i < nx; i = i + 1 { let sx = x0 + (x1 - x0) * (2 * i + 1) / (2 * nx) let o = sy * buf.stride + sx * 4 let pa = if xrgb { 255 } else { src[o + 3].to_int() } r = r + src[o + 2].to_int() * pa g = g + src[o + 1].to_int() * pa b = b + src[o].to_int() * pa a = a + pa } } let o = (y * w + x) * 4 if a > 0 { out.data[o] = (r / a).to_byte() out.data[o + 1] = (g / a).to_byte() out.data[o + 2] = (b / a).to_byte() out.data[o + 3] = (a / (nx * ny)).to_byte() } } } Some(out) } ///| Scale `src` to `w`x`h`: each target pixel averages the source pixels it covers (alpha /// weighted, so transparent edges don't darken), or takes the nearest one when enlarging. pub fn scale_pixmap(src : Pixmap, w : Int, h : Int) -> Pixmap { let out = Pixmap::new(w, h) for y = 0; y < h; y = y + 1 { let y0 = y * src.height / h let y1 = { let e = (y + 1) * src.height / h; if e > y0 { e } else { y0 + 1 } } for x = 0; x < w; x = x + 1 { let x0 = x * src.width / w let x1 = { let e = (x + 1) * src.width / w; if e > x0 { e } else { x0 + 1 } } let mut r = 0 let mut g = 0 let mut b = 0 let mut a = 0 let mut n = 0 for sy = y0; sy < y1; sy = sy + 1 { for sx = x0; sx < x1; sx = sx + 1 { let o = (sy * src.width + sx) * 4 let pa = src.data[o + 3].to_int() r = r + src.data[o].to_int() * pa g = g + src.data[o + 1].to_int() * pa b = b + src.data[o + 2].to_int() * pa a = a + pa n = n + 1 } } let o = (y * w + x) * 4 if a > 0 { out.data[o] = (r / a).to_byte() out.data[o + 1] = (g / a).to_byte() out.data[o + 2] = (b / a).to_byte() out.data[o + 3] = (a / n).to_byte() } } } out } ///| Sizes past which only one scaled copy of an image is cached (icons are drawn at a few small /// sizes; previews at one large one, or a new one each frame while they move). fn big_size(w : Int, h : Int) -> Bool { w * h > 128 * 128 } ///| Draw image `handle` scaled into `r` (unknown handles draw nothing). fn draw_image(dst : Pixmap, handle : UInt, r : @scene.Rect, clip : @scene.Rect?) -> Unit { let i = handle.reinterpret_as_int() - 1 if i < 0 || i >= images.length() || r.w <= 0 || r.h <= 0 || r.w > 4096 || r.h > 4096 { return } let key = (handle, r.w, r.h) let img = match scaled.get(key) { Some(p) => p None => { let src = images[i] let p = if src.width == r.w && src.height == r.h { src } else { scale_pixmap(src, r.w, r.h) } // A large image is kept at one large size only: a window preview that glides (the // overview) is drawn at a new size every frame. if big_size(r.w, r.h) { let stale = scaled.keys().filter(k => k.0 == handle && big_size(k.1, k.2)).collect() for k in stale { scaled.remove(k) } } scaled.set(key, p) p } } for y = 0; y < r.h; y = y + 1 { for x = 0; x < r.w; x = x + 1 { let o = (y * r.w + x) * 4 blend_pixel( dst, r.x + x, r.y + y, img.data[o].to_int(), img.data[o + 1].to_int(), img.data[o + 2].to_int(), img.data[o + 3].to_int(), clip, ) } } }