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
use {
    crate::{
        makepad_live_compiler::{
            LiveType,
            LiveNode,
            LiveId,
            LiveModuleId,
            LiveTypeInfo,
            LiveNodeSliceApi
        },
        live_traits::{LiveNew, LiveApply, ApplyFrom},
        makepad_live_tokenizer::{LiveErrorOrigin, live_error_origin},
        id_pool::*,
        makepad_error_log::*,
        makepad_live_id::*,
        cx::Cx,
        os::CxOsTexture,
        live_traits::*,
    },
    std::rc::Rc,
};


#[derive(Clone)]
pub struct Texture(Rc<PoolId>);

#[derive(Clone, Debug, PartialEq, Copy)]
pub struct TextureId(pub (crate) usize, u64);

impl Texture {
    pub fn texture_id(&self) -> TextureId {TextureId(self.0.id, self.0.generation)}
}

#[derive(Default)]
pub struct CxTexturePool(pub (crate) IdPool<CxTexture>);
impl CxTexturePool {
    pub fn alloc(&mut self) -> Texture {
        Texture(Rc::new(self.0.alloc()))
    }
}

impl std::ops::Index<TextureId> for CxTexturePool {
    type Output = CxTexture;
    fn index(&self, index: TextureId) -> &Self::Output {
        let d = &self.0.pool[index.0];
        if d.generation != index.1 {
            error!("Texture id generation wrong {} {} {}", index.0, d.generation, index.1)
        }
        &d.item
    }
}

impl std::ops::IndexMut<TextureId> for CxTexturePool {
    fn index_mut(&mut self, index: TextureId) -> &mut Self::Output {
        let d = &mut self.0.pool[index.0];
        if d.generation != index.1 {
            error!("Texture id generation wrong {} {} {}", index.0, d.generation, index.1)
        }
        &mut d.item
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TextureFormat {
    Default,
    ImageBGRA,
    Depth32Stencil8,
    RenderBGRA,
    RenderBGRAf16,
    RenderBGRAf32,
    SharedBGRA(crate::cx_stdin::PresentableImageId),
    //    ImageBGRAf32,
    //    ImageRf32,
    //    ImageRGf32,
    //    MappedBGRA,
    //    MappedBGRAf32,
    //    MappedRf32,
    //    MappedRGf32,
}
impl TextureFormat{
    pub fn is_shared(&self)->bool{
         match self{
             Self::SharedBGRA(_)=>true,
             _=>false
         }
    }
}
#[derive(Clone, Copy, PartialEq)]
pub struct TextureDesc {
    pub format: TextureFormat,
    pub width: Option<usize>,
    pub height: Option<usize>,
}

impl Default for TextureDesc {
    fn default() -> Self {
        TextureDesc {
            format: TextureFormat::Default,
            width: None,
            height: None,
        }
    }
}

impl LiveHook for Texture {}
impl LiveNew for Texture {
    fn live_design_with(_cx:&mut Cx){}
    fn new(cx: &mut Cx) -> Self {
        let texture = cx.textures.alloc();
        texture
    }
    
    fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
        LiveTypeInfo {
            module_id: LiveModuleId::from_str(&module_path!()).unwrap(),
            live_type: LiveType::of::<Self>(),
            live_ignore: true,
            fields: Vec::new(),
            type_name: id_lut!(Texture)
        }
    }
}

impl LiveApply for Texture {
    fn apply(&mut self, cx: &mut Cx, _apply_from: ApplyFrom, start_index: usize, nodes: &[LiveNode]) -> usize {
        
        if !nodes[start_index].value.is_structy_type() {
            cx.apply_error_wrong_type_for_struct(live_error_origin!(), start_index, nodes, live_id!(View));
            return nodes.skip_node(start_index);
        }
        
        let mut index = start_index + 1;
        loop {
            if nodes[index].value.is_close() {
                index += 1;
                break;
            }
            match nodes[index].id {
                _ => {
                    cx.apply_error_no_matching_field(live_error_origin!(), index, nodes);
                    index = nodes.skip_node(index);
                }
            }
        }
        return index;
    }
}


impl Texture {
    pub fn set_desc(&self, cx: &mut Cx, desc: TextureDesc) {
        let cxtexture = &mut cx.textures[self.texture_id()];
        cxtexture.desc = desc;
    }
    
    pub fn get_desc(&self, cx: &mut Cx) -> TextureDesc {
        cx.textures[self.texture_id()].desc.clone()
    }
    
    pub fn swap_image_u32(&self, cx: &mut Cx, image_u32: &mut Vec<u32>) {
        let cxtexture = &mut cx.textures[self.texture_id()];
        std::mem::swap(&mut cxtexture.image_u32, image_u32);
        cxtexture.update_image = true;
    }
}


#[derive(Default)]
pub struct CxTexture {
    pub (crate) desc: TextureDesc,
    pub (crate) image_u32: Vec<u32>,
    //pub(crate) _image_f32: Vec<f32>,
    pub (crate) update_image: bool,
    pub os: CxOsTexture
}