makepad_platform/
cx.rs

1use {
2    makepad_futures::{executor, executor::{Executor, Spawner}},
3    std::{
4        collections::{
5            HashMap,
6            HashSet,
7        },
8        any::{Any, TypeId},
9        rc::Rc,
10        rc::Weak,
11        cell::RefCell,
12    },
13    crate::{
14        makepad_live_compiler::{
15            LiveRegistry,
16            LiveFileChange
17        },
18        makepad_shader_compiler::ShaderRegistry,
19        draw_shader::CxDrawShaders,
20        draw_matrix::CxDrawMatrixPool,
21        os::{CxOs},
22        debug::Debug,
23        event::{
24            DrawEvent,
25            CxFingers,
26            CxDragDrop,
27            Event,
28            Trigger,
29            CxKeyboard,
30            NextFrame,
31        },
32        cx_api::CxOsOp,
33        area::Area,
34        gpu_info::GpuInfo,
35        window::CxWindowPool,
36        draw_list::CxDrawListPool,
37        pass::CxPassPool,
38        texture::{CxTexturePool,TextureDesc,TextureFormat,Texture},
39        geometry::{
40            Geometry,
41            CxGeometryPool,
42            GeometryFingerprint
43        },
44    }
45};
46
47pub use makepad_shader_compiler::makepad_derive_live::*;
48pub use makepad_shader_compiler::makepad_math::*;
49
50pub struct Cx {
51    pub (crate) os_type: OsType,
52    pub (crate) in_makepad_studio: bool,
53   
54    pub (crate) gpu_info: GpuInfo,
55    pub (crate) xr_capabilities: XrCapabilities,
56    pub (crate) cpu_cores: usize,
57    pub null_texture: Texture,
58    pub windows: CxWindowPool,
59    pub passes: CxPassPool,
60    pub draw_lists: CxDrawListPool,
61    pub draw_matrices: CxDrawMatrixPool,
62    pub textures: CxTexturePool,
63    pub (crate) geometries: CxGeometryPool,
64    
65    pub (crate) geometries_refs: HashMap<GeometryFingerprint, Weak<Geometry >>,
66    
67    pub draw_shaders: CxDrawShaders,
68    
69    pub (crate) new_draw_event: DrawEvent,
70    
71    pub redraw_id: u64,
72    
73    pub (crate) repaint_id: u64,
74    pub (crate) event_id: u64,
75    pub (crate) timer_id: u64,
76    pub (crate) next_frame_id: u64,
77    
78    pub keyboard: CxKeyboard,
79    pub fingers: CxFingers,
80    pub (crate) ime_area: Area,
81    pub (crate) drag_drop: CxDragDrop,
82    
83    pub (crate) platform_ops: Vec<CxOsOp>,
84    
85    pub (crate) new_next_frames: HashSet<NextFrame>,
86    
87    pub (crate) dependencies: HashMap<String, CxDependency>,
88    
89    pub (crate) triggers: HashMap<Area, Vec<Trigger >>,
90    
91    pub live_registry: Rc<RefCell<LiveRegistry >>,
92
93    pub (crate) live_file_change_receiver: std::sync::mpsc::Receiver<Vec<LiveFileChange>>,
94    pub (crate) live_file_change_sender: std::sync::mpsc::Sender<Vec<LiveFileChange >>,
95
96    pub shader_registry: ShaderRegistry,
97    
98    pub os: CxOs,
99    // (cratethis cuts the compiletime of an end-user application in half
100    pub (crate) event_handler: Option<Box<dyn FnMut(&mut Cx, &Event) >>,
101    
102    pub (crate) globals: Vec<(TypeId, Box<dyn Any>)>,
103
104    pub (crate) self_ref: Option<Rc<RefCell<Cx>>>,
105    
106    pub debug: Debug,
107
108    #[allow(dead_code)]
109    pub(crate) executor: Option<Executor>,
110    pub(crate) spawner: Spawner,
111}
112
113#[derive(Clone)]
114pub struct CxRef(pub Rc<RefCell<Cx>>); //TODO: I probably shouldn't remove the (crate)
115
116pub struct CxDependency {
117    pub data: Option<Result<Rc<Vec<u8>>, String >>
118}
119#[derive(Clone, Debug)]
120pub struct AndroidParams {
121    pub cache_path: String,
122    pub density: f64
123}
124
125#[derive(Clone, Debug)]
126pub struct WebParams {
127    pub protocol: String,
128    pub host: String,
129    pub hostname: String,
130    pub pathname: String,
131    pub search: String,
132    pub hash: String
133}
134
135#[derive(Clone, Debug)]
136pub struct LinuxWindowParams {
137    pub custom_window_chrome: bool,
138}
139
140#[derive(Clone, Debug)]
141pub enum OsType {
142    Unknown,
143    Windows,
144    Macos,
145    Ios,
146    Android(AndroidParams),
147    LinuxWindow (LinuxWindowParams),
148    LinuxDirect,
149    Web(WebParams)
150}
151
152#[derive(Default)]
153pub struct XrCapabilities {
154    pub ar_supported: bool,
155    pub vr_supported: bool,
156}
157
158impl OsType {
159    pub fn is_single_window(&self)->bool{
160        match self{
161            OsType::Web(_) => true,
162            OsType::Ios=>true,
163            OsType::Android(_) => true,
164            OsType::LinuxDirect=> true,
165            _=> false
166        }
167    }
168    pub fn is_web(&self) -> bool {
169        match self {
170            OsType::Web(_) => true,
171            _ => false
172        }
173    }
174    
175    
176    pub fn get_cache_dir(&self)->Option<String>{
177        if let OsType::Android(params) = self {
178            Some(params.cache_path.clone())
179        }
180        else {
181            None
182        }
183    }
184}
185
186impl Cx {
187    pub fn new(event_handler: Box<dyn FnMut(&mut Cx, &Event)>) -> Self {
188        //#[cfg(any(target_arch = "wasm32", target_os = "android"))]
189        //crate::makepad_error_log::set_panic_hook();
190        // the null texture
191        let mut textures = CxTexturePool::default();
192        let null_texture = textures.alloc();
193        let texture = &mut textures[null_texture.texture_id()];
194        texture.desc = TextureDesc {
195            format: TextureFormat::ImageBGRA,
196            width: Some(4),
197            height: Some(4),
198        };
199        texture.image_u32 = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
200        texture.update_image =  true;
201        
202        let (executor, spawner) = executor::new_executor_and_spawner();
203        let (send, recv) = std::sync::mpsc::channel();
204        Self {
205            null_texture,
206            cpu_cores: 8,
207            in_makepad_studio: false,
208            os_type: OsType::Unknown,
209            gpu_info: Default::default(),
210            xr_capabilities: Default::default(),
211            
212            windows: Default::default(),
213            passes: Default::default(),
214            draw_lists: Default::default(),
215            draw_matrices: Default::default(),
216            geometries: Default::default(),
217            textures,
218            geometries_refs: Default::default(),
219            
220            draw_shaders: Default::default(),
221            
222            new_draw_event: Default::default(),
223            
224            redraw_id: 1,
225            event_id: 1,
226            repaint_id: 1,
227            timer_id: 1,
228            next_frame_id: 1,
229            
230            keyboard: Default::default(),
231            fingers: Default::default(),
232            drag_drop: Default::default(),
233            ime_area: Default::default(),
234            platform_ops: Default::default(),
235            
236            
237            new_next_frames: Default::default(),
238            
239            dependencies: Default::default(),
240            
241            triggers: Default::default(),
242            
243            live_registry: Rc::new(RefCell::new(LiveRegistry::default())),
244            
245            live_file_change_receiver: recv,
246            live_file_change_sender: send,
247            
248            shader_registry: ShaderRegistry::new(),
249            
250            os: CxOs::default(),
251            
252            event_handler: Some(event_handler),
253            
254            debug: Default::default(),
255            
256            globals: Default::default(),
257
258            executor: Some(executor),
259            spawner,
260
261            self_ref: None
262        }
263    }
264}