makepad_platform/
texture.rs1use {
2 crate::{
3 makepad_live_compiler::{
4 LiveType,
5 LiveNode,
6 LiveId,
7 LiveModuleId,
8 LiveTypeInfo,
9 LiveNodeSliceApi
10 },
11 live_traits::{LiveNew, LiveApply, ApplyFrom},
12 makepad_live_tokenizer::{LiveErrorOrigin, live_error_origin},
13 id_pool::*,
14 makepad_error_log::*,
15 makepad_live_id::*,
16 cx::Cx,
17 os::CxOsTexture,
18 live_traits::*,
19 },
20 std::rc::Rc,
21};
22
23
24#[derive(Clone)]
25pub struct Texture(Rc<PoolId>);
26
27#[derive(Clone, Debug, PartialEq, Copy)]
28pub struct TextureId(pub (crate) usize, u64);
29
30impl Texture {
31 pub fn texture_id(&self) -> TextureId {TextureId(self.0.id, self.0.generation)}
32}
33
34#[derive(Default)]
35pub struct CxTexturePool(pub (crate) IdPool<CxTexture>);
36impl CxTexturePool {
37 pub fn alloc(&mut self) -> Texture {
38 Texture(Rc::new(self.0.alloc()))
39 }
40}
41
42impl std::ops::Index<TextureId> for CxTexturePool {
43 type Output = CxTexture;
44 fn index(&self, index: TextureId) -> &Self::Output {
45 let d = &self.0.pool[index.0];
46 if d.generation != index.1 {
47 error!("Texture id generation wrong {} {} {}", index.0, d.generation, index.1)
48 }
49 &d.item
50 }
51}
52
53impl std::ops::IndexMut<TextureId> for CxTexturePool {
54 fn index_mut(&mut self, index: TextureId) -> &mut Self::Output {
55 let d = &mut self.0.pool[index.0];
56 if d.generation != index.1 {
57 error!("Texture id generation wrong {} {} {}", index.0, d.generation, index.1)
58 }
59 &mut d.item
60 }
61}
62
63#[derive(Clone, Copy, Debug, PartialEq)]
64pub enum TextureFormat {
65 Default,
66 ImageBGRA,
67 Depth32Stencil8,
68 RenderBGRA,
69 RenderBGRAf16,
70 RenderBGRAf32,
71 SharedBGRA(crate::cx_stdin::PresentableImageId),
72 }
80impl TextureFormat{
81 pub fn is_shared(&self)->bool{
82 match self{
83 Self::SharedBGRA(_)=>true,
84 _=>false
85 }
86 }
87}
88#[derive(Clone, Copy, PartialEq)]
89pub struct TextureDesc {
90 pub format: TextureFormat,
91 pub width: Option<usize>,
92 pub height: Option<usize>,
93}
94
95impl Default for TextureDesc {
96 fn default() -> Self {
97 TextureDesc {
98 format: TextureFormat::Default,
99 width: None,
100 height: None,
101 }
102 }
103}
104
105impl LiveHook for Texture {}
106impl LiveNew for Texture {
107 fn live_design_with(_cx:&mut Cx){}
108 fn new(cx: &mut Cx) -> Self {
109 let texture = cx.textures.alloc();
110 texture
111 }
112
113 fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
114 LiveTypeInfo {
115 module_id: LiveModuleId::from_str(&module_path!()).unwrap(),
116 live_type: LiveType::of::<Self>(),
117 live_ignore: true,
118 fields: Vec::new(),
119 type_name: id_lut!(Texture)
120 }
121 }
122}
123
124impl LiveApply for Texture {
125 fn apply(&mut self, cx: &mut Cx, _apply_from: ApplyFrom, start_index: usize, nodes: &[LiveNode]) -> usize {
126
127 if !nodes[start_index].value.is_structy_type() {
128 cx.apply_error_wrong_type_for_struct(live_error_origin!(), start_index, nodes, live_id!(View));
129 return nodes.skip_node(start_index);
130 }
131
132 let mut index = start_index + 1;
133 loop {
134 if nodes[index].value.is_close() {
135 index += 1;
136 break;
137 }
138 match nodes[index].id {
139 _ => {
140 cx.apply_error_no_matching_field(live_error_origin!(), index, nodes);
141 index = nodes.skip_node(index);
142 }
143 }
144 }
145 return index;
146 }
147}
148
149
150impl Texture {
151 pub fn set_desc(&self, cx: &mut Cx, desc: TextureDesc) {
152 let cxtexture = &mut cx.textures[self.texture_id()];
153 cxtexture.desc = desc;
154 }
155
156 pub fn get_desc(&self, cx: &mut Cx) -> TextureDesc {
157 cx.textures[self.texture_id()].desc.clone()
158 }
159
160 pub fn swap_image_u32(&self, cx: &mut Cx, image_u32: &mut Vec<u32>) {
161 let cxtexture = &mut cx.textures[self.texture_id()];
162 std::mem::swap(&mut cxtexture.image_u32, image_u32);
163 cxtexture.update_image = true;
164 }
165}
166
167
168#[derive(Default)]
169pub struct CxTexture {
170 pub (crate) desc: TextureDesc,
171 pub (crate) image_u32: Vec<u32>,
172 pub (crate) update_image: bool,
174 pub os: CxOsTexture
175}