nandi/jolt-nativepublic Fork 0
0b92f67f944e81120d053ace058b0ffde413140e
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

video_check.clj · 77 lines · 3.7 KBClojure Blame HistoryRaw
Give jvui a picture from somewhere else, and a clock 32fae8d nandi 9d ago1(ns glimmer-jvui.video-check
2 "A :video node, painted, and the pixels checked.
3
4 The layout tests run headless and cannot see this: with no renderer every
5 frame is dropped and a video tile is an empty rectangle that lays out
6 correctly. What is actually at stake is the CHANNEL ORDER — a buffer whose
7 bytes run R,G,B,A is the 32-bit word 0xAABBGGRR, so SDL wants ABGR8888 and
8 the obvious-looking ARGB8888 rotates every channel and turns skin blue. A
9 test that only asked whether a texture existed would pass either way.
10
11 So: a window under SDL's dummy driver, a frame with a RED left half and a
12 BLUE right half, a screenshot, and a look at what came out."
13 (:require [glimmer.core :as ui]
14 [glimmer.ratom :as ra]
15 [glimmer-jvui.core :as backend]
16 [jvui.sdl :as sdl]
17 [jolt.ffi :as ffi]))
18
19(defn- red-blue
20 "w by h RGBA: red left, blue right, opaque."
21 [w h]
22 (let [p (ffi/alloc (* 4 w h))]
23 (dotimes [y h]
24 (dotimes [x w]
25 (let [o (* 4 (+ (* y w) x))
26 left? (< x (quot w 2))]
27 (ffi/write (+ p o 0) :uint8 (if left? 230 10)) ; R
28 (ffi/write (+ p o 1) :uint8 10) ; G
29 (ffi/write (+ p o 2) :uint8 (if left? 10 230)) ; B
30 (ffi/write (+ p o 3) :uint8 255)))) ; A
31 p))
32
33;; A BMP from SDL is bottom-up and 24bpp, with rows padded to a four-byte
34;; boundary and the pixel array offset in the header at byte 10. Every one of
35;; those was got wrong first time round — 32bpp and no padding — and the
36;; result was not an error but plausible-looking numbers from the wrong
37;; addresses, which is exactly how a pixel test lies to you.
38(defn- bmp-pixel [path x y]
39 (let [b (java.nio.file.Files/readAllBytes (java.nio.file.Path/of path (into-array String [])))
40 u (fn [i] (bit-and (int (aget b i)) 255))
41 le (fn [i] (+ (u i) (bit-shift-left (u (+ i 1)) 8)
42 (bit-shift-left (u (+ i 2)) 16) (bit-shift-left (u (+ i 3)) 24)))
43 off (le 10)
44 w (le 18)
45 h (le 22)
46 stride (* 4 (quot (+ (* w 3) 3) 4))
47 i (+ off (* (- h 1 y) stride) (* 3 x))]
48 {:b (u i) :g (u (+ i 1)) :r (u (+ i 2))}))
49
50(defn -main [& _]
51 (let [px (red-blue 64 64)
52 shot (str (System/getProperty "java.io.tmpdir") "/jvui-video-check.bmp")
53 n (ra/atom 0)]
54 (try
55 ;; Pushed from a TIMER, which is how frq does it — av.clj drives its
56 ;; whole media plane from (every! 16 pump!). Pushing from the
57 ;; component body instead is what the first version of this test did,
58 ;; and it silently proved nothing: a component with no state renders
59 ;; once, at mount, before the window exists — so the frame was
60 ;; dropped for want of a renderer and the tile was empty for a
61 ;; reason that had nothing to do with the picture.
62 (backend/every! 16 (fn [] (backend/frame-rgba! "peer" 64 64 px)))
63 (ui/run (fn [] [:video {:feed "peer" :size [120.0 120.0]}])
64 {:title "video" :width 160 :height 160 :frames 6 :shot shot})
65 (finally (ffi/free px)))
66 (let [left (bmp-pixel shot 40 80)
67 right (bmp-pixel shot 120 80)
68 ok-l (and (> (:r left) 150) (< (:b left) 100))
69 ok-r (and (> (:b right) 150) (< (:r right) 100))]
70 (println " left pixel " (pr-str left))
71 (println " right pixel" (pr-str right))
72 (println (if ok-l "- the red half is red" "FAIL the red half is not red"))
73 (println (if ok-r "- the blue half is blue" "FAIL the blue half is not blue"))
74 (if (and ok-l ok-r)
75 (println "all 2 checks passed")
76 (do (println "channel order is wrong — see the ABGR note in jvui.sdl")
77 (System/exit 1))))))