1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#![cfg(feature = "gui")]
use anyhow::{anyhow, Result};
use gtk4::{self as gtk, glib, cairo, gio};
use gtk4::prelude::*;
use glib::{clone, spawn_future_local};
use std::thread;
use std::sync::{Arc, Mutex};
use std::rc::Rc;
use std::cell::Cell;
use crate::sync::Signal;
use std::future::poll_fn;
use std::task::Poll;
pub struct FbPool{
size: usize,
pool: Vec<cairo::ImageSurfaceDataOwned>,
}
impl FbPool{
pub fn new(size: usize) -> Self{
FbPool{
size,
pool: Vec::with_capacity(size),
}
}
pub fn put(&mut self, buf: cairo::ImageSurfaceDataOwned){
if self.pool.len() < self.size{
self.pool.push(buf);
}
}
pub fn get(&mut self, w: usize, h: usize) -> Result<cairo::ImageSurface>{
while let Some(i) = self.pool.pop(){
let i = i.into_inner();
if i.width() as usize == w && i.height() as usize == h{
return Ok(i);
}
}
Ok(cairo::ImageSurface::create(
cairo::Format::Rgb24,
w.try_into()?, h.try_into()?)?)
}
}
pub trait FbSourceOnce{
fn get(self, w: usize, h: usize) -> Result<cairo::ImageSurface>;
}
impl FbSourceOnce for &Mutex<FbPool>{
fn get(self, w: usize, h: usize) -> Result<cairo::ImageSurface>{
self.lock().map_err(|e| anyhow!("{}", e))?.get(w, h)
}
}
pub struct Packet<T>{
pub image: cairo::ImageSurfaceDataOwned,
pub attr: T,
}
pub trait Overlay: Send + 'static{
type Widget: glib::object::IsA<gtk::Widget>;
fn empty() -> Result<Self::Widget>;
fn activate(_widget: &Self::Widget) -> Result<()>{
Ok(())
}
fn update(&self, _widget: &Self::Widget) -> Result<()>{
Ok(())
}
}
pub trait Source: Send + 'static{
type Attr: Overlay;
fn next(&mut self, fbpool: impl FbSourceOnce) -> Result<Packet<Self::Attr>>;
}
impl Overlay for (){
type Widget = gtk::Box;
fn empty() -> Result<gtk::Box>{
Ok(gtk::Box::builder()
.halign(gtk::Align::Start)
.valign(gtk::Align::Start)
.visible(false)
.build())
}
}
struct AppState<T>{
next: Mutex<Option<Packet<T>>>,
update: Mutex<Signal>,
abort: Mutex<Signal>,
fbpool: Mutex<FbPool>,
}
fn sourcing_loop<Attr: Overlay>(
apps: &AppState<Attr>,
src: &mut impl Source<Attr=Attr>
) -> Result<()>{
loop{
let p = src.next(&apps.fbpool)?;
let old = apps.next.lock()
.map_err(|e| anyhow!("{}", e))?
.replace(p);
apps.update.lock().map_err(|e| anyhow!("{}", e))?.wake();
if let Some(old) = old{
apps.fbpool.lock()
.map_err(|e| anyhow!("{}", e))?
.put(old.image);
}
}
}
fn activate<Attr: Overlay>(app: >k::Application, apps: Arc<AppState<Attr>>){
let draw = gtk::DrawingArea::new();
let overlay = Attr::empty().unwrap();
let frame_cache: Rc<Cell<Option<cairo::ImageSurface>>>
= Rc::new(Cell::new(None));
let attr_cache: Rc<Cell<Option<Attr>>> = Rc::new(Cell::new(None));
draw.set_draw_func(clone!{
#[strong] frame_cache,
move |_draw, ctx, canvas_w, canvas_h|{
ctx.set_source_rgb(0., 0., 0.);
ctx.paint().unwrap();
if let Some(image) = frame_cache.take(){
let ipat = cairo::SurfacePattern::create(&image);
let scale = ((canvas_w as f64) / (image.width() as f64)).min(
(canvas_h as f64) / (image.height() as f64));
ctx.scale(scale, scale);
ctx.set_source(&ipat).unwrap();
ctx.paint().unwrap();
frame_cache.replace(Some(image));
}
}
});
overlay.add_tick_callback(clone!{
#[strong] attr_cache,
move |overlay, _clock|{
if let Some(attr) = attr_cache.take(){
attr.update(overlay).unwrap();
attr_cache.replace(Some(attr));
}
glib::ControlFlow::Continue
}
});
spawn_future_local(poll_fn(clone!{
#[strong] apps,
#[strong] draw,
#[strong] frame_cache,
#[strong] attr_cache,
#[strong] overlay,
move |ctx|{
loop{
match apps.update.lock().unwrap().poll(ctx){
Poll::Ready(_) => {
if let Some(newfb) = apps.next.lock().unwrap().take(){
if let Some(lastframe) = frame_cache.take(){
apps.fbpool.lock().unwrap()
.put(lastframe.take_data().unwrap());
}
frame_cache.replace(Some(newfb.image.into_inner()));
newfb.attr.update(&overlay).unwrap();
attr_cache.replace(Some(newfb.attr));
}
draw.queue_draw();
},
pending => return pending,
}
}
}
}));
spawn_future_local(poll_fn(clone!{
#[strong] apps,
#[strong] app,
move |ctx|{
loop{
match apps.abort.lock().unwrap().poll(ctx){
Poll::Ready(_) => {
app.quit();
},
pending => return pending,
}
}
}
}));
let olcontainer = gtk::Overlay::builder()
.child(&draw)
.build();
olcontainer.add_overlay(&overlay);
let win = gtk::ApplicationWindow::builder()
.application(app)
.child(&olcontainer)
.build();
win.present();
Attr::activate(&overlay).unwrap();
}
pub fn main(src: impl Source + 'static) -> Result<glib::ExitCode>{
let apps = Arc::new(AppState{
next: Mutex::new(None),
update: Mutex::new(Signal::new()),
abort: Mutex::new(Signal::new()),
fbpool: Mutex::new(FbPool::new(4)),
});
thread::spawn(clone!{
#[strong] apps,
move ||{
let mut src = src;
let res = sourcing_loop(&apps, &mut src);
apps.abort.lock().unwrap().wake();
res.unwrap();
}
});
let app = gtk::Application::builder()
.flags(gio::ApplicationFlags::HANDLES_COMMAND_LINE)
.build();
app.connect_command_line(clone!{
#[strong] apps,
move |app, _| {
activate(app, apps.clone());
0
}
});
Ok(app.run())
}
|