makepad-platform 1.0.0

Makepad platform layer
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use {
    crate::{
        id_pool::*,
        cx::Cx,
        makepad_math::*,
        os::CxOsTexture,
    },
    std::rc::Rc,
};


#[derive(Debug, Clone, PartialEq)]
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 {
    // Allocates a new texture in the pool, potentially reusing an existing texture slot.
    ///
    /// This method attempts to find a compatible texture slot for reuse. If found, it preserves
    /// the old os-specific resources for proper cleanup. If not, it allocates a new slot.
    ///
    /// # Arguments
    /// * `requested_format` - The format of the texture to be allocated.
    ///
    /// # Returns
    /// A `Texture` instance representing the allocated or reused texture.
    ///
    /// # Note
    /// When a texture slot is reused, the old platofrm-specific resources are stored in the `previous_platform_resource` field
    /// of the new `CxTexture`. This allows for proper resource management and cleanup in the corresponding platform.
    pub fn alloc(&mut self, requested_format: TextureFormat) -> Texture {
        let is_video = requested_format.is_video();
        let cx_texture = CxTexture {
            format: requested_format,
            alloc: None,
            ..Default::default()
        };

        let (new_id, previous_item) = self.0.alloc_with_reuse_filter(|item| {
            // Check for compatibility, intentionally not using `is_compatible_with` to avoid passing the whole format and cloning vec contents    
            is_video == item.item.format.is_video()
        }, cx_texture);

        if let Some(previous_item) = previous_item {
            // We know this index is valid because it was just reused
            self.0.pool[new_id.id].item.previous_platform_resource = Some(previous_item.os);
        }

        Texture(Rc::new(new_id))
    }
}

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, Debug)]
pub enum TextureSize {
    Auto,
    Fixed{width: usize, height: usize}
}

impl TextureSize{
    fn width_height(&self, w:usize, h:usize)->(usize,usize){
        match self{
            TextureSize::Auto=>(w,h),
            TextureSize::Fixed{width, height}=>(*width,*height)
        }
    }
}


#[derive(Clone)]
pub enum TextureFormat {
    Unknown,
    VecBGRAu8_32{width:usize, height:usize, data:Option<Vec<u32>>, updated: TextureUpdated},
    VecMipBGRAu8_32{width:usize, height:usize, data:Option<Vec<u32>>, max_level:Option<usize>, updated: TextureUpdated},
    VecRGBAf32{width:usize, height:usize, data:Option<Vec<f32>>, updated: TextureUpdated},
    VecRu8{width:usize, height:usize, data:Option<Vec<u8>>, unpack_row_length:Option<usize>, updated: TextureUpdated},
    VecRGu8{width:usize, height:usize, data:Option<Vec<u8>>, unpack_row_length:Option<usize>, updated: TextureUpdated},
    VecRf32{width:usize, height:usize, data:Option<Vec<f32>>, updated: TextureUpdated},
    DepthD32{size:TextureSize, initial: bool},
    RenderBGRAu8{size:TextureSize, initial: bool},
    RenderRGBAf16{size:TextureSize, initial: bool},
    RenderRGBAf32{size:TextureSize, initial: bool},
    
    SharedBGRAu8{width:usize, height:usize, id:crate::cx_stdin::PresentableImageId, initial: bool},
    #[cfg(any(target_os = "android", target_os = "linux"))]
    VideoRGB,
}

impl std::fmt::Debug for TextureFormat{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error>{
        match self{
            TextureFormat::Unknown=>write!(f, "TextureFormat::Unknown"),
            TextureFormat::VecBGRAu8_32{width, height,..}=>write!(f, "TextureFormat::VecBGRAu8_32(width:{width},height:{height})"),
            TextureFormat::VecMipBGRAu8_32{width, height,..}=>write!(f, "TextureFormat::VecMipBGRAu8_32(width:{width},height:{height})"),
            TextureFormat::VecRGBAf32{width, height,..}=>write!(f, "TextureFormat::VecRGBAf32(width:{width},height:{height})"),
            TextureFormat::VecRu8{width, height,..}=>write!(f, "TextureFormat::VecRu8(width:{width},height:{height})"),
            TextureFormat::VecRGu8{width, height,..}=>write!(f, "TextureFormat::VecRGu8(width:{width},height:{height})"),
            TextureFormat::VecRf32{width, height,..}=>write!(f, "TextureFormat::VecRf32(width:{width},height:{height})"),
            TextureFormat::DepthD32{size,..}=>write!(f, "TextureFormat::DepthD32(size:{:?})", size),
            TextureFormat::RenderBGRAu8{size,..}=>write!(f, "TextureFormat::RenderBGRAu8(size:{:?})", size),
            TextureFormat::RenderRGBAf16{size,..}=>write!(f, "TextureFormat::RenderRGBAf16(size:{:?})", size),
            TextureFormat::RenderRGBAf32{size,..}=>write!(f, "TextureFormat::RenderRGBAf32(size:{:?})", size),
            TextureFormat::SharedBGRAu8{width,height,..}=>write!(f, "TextureFormat::SharedBGRAu8(width:{width},height:{height})"),
            #[cfg(any(target_os = "android", target_os = "linux"))]
            TextureFormat::VideoRGB=>write!(f, "TextureFormat::VideoRGB"),
        }
    }
}

#[derive(Debug, Default, Clone)] 
pub struct TextureAnimation {
    pub width: usize,
    pub height: usize,
    pub num_frames: usize
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct TextureAlloc{
    pub category: TextureCategory,
    pub pixel: TexturePixel,
    pub width: usize,
    pub height: usize,
}

#[allow(unused)]    
#[derive(Clone, Debug)]
pub enum TextureCategory{
    Vec,
    Render,
    DepthBuffer,
    Shared,
    Video,
}

impl PartialEq for TextureCategory{
    fn eq(&self, other: &TextureCategory) -> bool{
        match self{
            Self::Vec{..} => if let Self::Vec{..} = other{true} else {false},
            Self::Render{..} => if let Self::Render{..} = other{true} else {false},
            Self::Shared{..} => if let Self::Shared{..} = other{true} else {false},
            Self::DepthBuffer{..} => if let Self::DepthBuffer{..} = other{true} else {false},           
            Self::Video{..} => if let Self::Video{..} = other{true} else {false},           
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum TextureUpdated {
    Empty,
    Partial(RectUsize),
    Full,
}

impl TextureUpdated {
    pub fn is_empty(&self) -> bool {
        match self {
            TextureUpdated::Empty => true,
            _ => false,
        }
    }

    pub fn update(self, dirty_rect: Option<RectUsize>) -> Self {
        let new = match dirty_rect {
            Some(dirty_rect) => match self {
                TextureUpdated::Empty => TextureUpdated::Partial(dirty_rect),
                TextureUpdated::Partial(rect) => 
                TextureUpdated::Partial(rect.union(dirty_rect)),
                TextureUpdated::Full => TextureUpdated::Full,
            },
            None => TextureUpdated::Full,
        };
        if let TextureUpdated::Partial(p) = new{
            if p.size == SizeUsize::new(0,0){
                return TextureUpdated::Empty
            }
        }
        new
    }
}

#[allow(unused)]    
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum TexturePixel{
    BGRAu8,
    RGBAf16,
    RGBAf32,
    Ru8,
    RGu8,
    Rf32,
    D32,
    #[cfg(any(target_os = "android", target_os = "linux"))]
    VideoRGB
}

impl CxTexture{
    pub(crate) fn updated(&self) -> TextureUpdated {
        match self.format {
            TextureFormat::VecBGRAu8_32 { updated, .. } => updated,
            TextureFormat::VecMipBGRAu8_32{ updated, .. } => updated,
            TextureFormat::VecRGBAf32 { updated, .. } => updated,
            TextureFormat::VecRu8 { updated, .. } => updated,
            TextureFormat::VecRGu8 { updated, .. } => updated,
            TextureFormat::VecRf32 { updated, .. } => updated,
            _ => panic!(),
        }
    }
    
    #[allow(unused)]
    pub(crate) fn initial(&mut self) -> bool {
        match self.format {
            TextureFormat::DepthD32{ initial, .. } => initial,
            TextureFormat::RenderBGRAu8{ initial, .. } => initial,
            TextureFormat::RenderRGBAf16{ initial, .. } => initial,
            TextureFormat::RenderRGBAf32{ initial, .. } => initial,
            TextureFormat::SharedBGRAu8{ initial, .. } => initial,
            _ => panic!()
        }
    }

    pub(crate) fn set_updated(&mut self, updated: TextureUpdated) {
        *match &mut self.format {
            TextureFormat::VecBGRAu8_32 { updated, .. } => updated,
            TextureFormat::VecMipBGRAu8_32{ updated, .. } => updated,
            TextureFormat::VecRGBAf32 { updated, .. } => updated,
            TextureFormat::VecRu8 { updated, .. } => updated,
            TextureFormat::VecRGu8 { updated, .. } => updated,
            TextureFormat::VecRf32 { updated, .. } => updated,
            _ => panic!(),
        } = updated;
    }

    pub fn set_initial(&mut self, initial: bool) {
        *match &mut self.format {
            TextureFormat::DepthD32{ initial, .. } => initial,
            TextureFormat::RenderBGRAu8{ initial, .. } => initial,
            TextureFormat::RenderRGBAf16{ initial, .. } => initial,
            TextureFormat::RenderRGBAf32{ initial, .. } => initial,
            TextureFormat::SharedBGRAu8{ initial, .. } => initial,
            _ => panic!()
        } = initial;
    }

    pub(crate) fn take_updated(&mut self) -> TextureUpdated {
        let updated = self.updated();
        self.set_updated(TextureUpdated::Empty);
        updated
    }
    #[allow(unused)]
    pub(crate) fn take_initial(&mut self) -> bool {
        let initial = self.initial();
        self.set_initial(false);
        initial
    }
        
    pub(crate) fn alloc_vec(&mut self)->bool{
        if let Some(alloc) = self.format.as_vec_alloc(){
            if self.alloc.is_none() || self.alloc.as_ref().unwrap() != &alloc{
                self.alloc = Some(alloc);
                return true;
            }
        }
        false
    }
    
    #[allow(unused)]
    pub(crate) fn alloc_shared(&mut self)->bool{
        if let Some(alloc) = self.format.as_shared_alloc(){
            if self.alloc.is_none() || self.alloc.as_ref().unwrap() != &alloc{
                self.alloc = Some(alloc);
                return true;
            }
        }
        false
    }
    
    pub(crate) fn alloc_render(&mut self, width:usize, height: usize)->bool{
        if let Some(alloc) = self.format.as_render_alloc(width, height){
            if self.alloc.is_none() || self.alloc.as_ref().unwrap() != &alloc{
                self.alloc = Some(alloc);
                return true;
            }
        }
        false
    }
    
    pub(crate) fn alloc_depth(&mut self, width:usize, height: usize)->bool{
        if let Some(alloc) = self.format.as_depth_alloc(width, height){
            if self.alloc.is_none() || self.alloc.as_ref().unwrap() != &alloc{
                self.alloc = Some(alloc);
                return true;
            }
        }
        false
    }

    #[cfg(any(target_os = "android", target_os = "linux"))]
    #[allow(unused)]
    pub(crate) fn alloc_video(&mut self)->bool{
        if let Some(alloc) = self.format.as_video_alloc(){
            if self.alloc.is_none() || self.alloc.as_ref().unwrap() != &alloc{
                self.alloc = Some(alloc);
                return true;
            }
        }
        false
    }
}

impl TextureFormat{
    pub fn is_shared(&self)->bool{
         match self{
             Self::SharedBGRAu8{..}=>true,
             _=>false
         }
    }
    pub fn is_vec(&self)->bool{
        match self{
            Self::VecBGRAu8_32{..}=>true,
            Self::VecMipBGRAu8_32{..}=>true,
            Self::VecRGBAf32{..}=>true,
            Self::VecRu8{..}=>true,
            Self::VecRGu8{..}=>true,
            Self::VecRf32{..}=>true,
            _=>false
        }
    }
    
    pub fn is_render(&self)->bool{
        match self{
            Self::RenderBGRAu8{..}=>true,
            Self::RenderRGBAf16{..}=>true,
            Self::RenderRGBAf32{..}=>true,
            _=>false
        }
    }
        
    pub fn is_depth(&self)->bool{
        match self{
            Self::DepthD32{..}=>true,
            _=>false
        }
    }

    pub fn is_video(&self) -> bool {
        #[cfg(any(target_os = "android", target_os = "linux"))]
        if let Self::VideoRGB = self {
            return true;
        }
        false
    }
    
    pub fn vec_width_height(&self)->Option<(usize,usize)>{
        match self{
            Self::VecBGRAu8_32{width, height, .. }=>Some((*width,*height)),
            Self::VecMipBGRAu8_32{width, height, ..}=>Some((*width,*height)),
            Self::VecRGBAf32{width, height, ..}=>Some((*width,*height)),
            Self::VecRu8{width, height, ..}=>Some((*width,*height)),
            Self::VecRGu8{width, height, ..}=>Some((*width,*height)),
            Self::VecRf32{width, height,..}=>Some((*width,*height)),
            _=>None
        }
    }
    
    pub(crate) fn as_vec_alloc(&self)->Option<TextureAlloc>{
        match self{
            Self::VecBGRAu8_32{width,height,..}=>Some(TextureAlloc{
                width:*width,
                height:*height,
                pixel:TexturePixel::BGRAu8,
                category: TextureCategory::Vec,
            }),
            Self::VecMipBGRAu8_32{width,height,..}=>Some(TextureAlloc{
                width:*width,
                height:*height,
                pixel:TexturePixel::BGRAu8,
                category: TextureCategory::Vec,
            }),
            Self::VecRGBAf32{width,height,..}=>Some(TextureAlloc{
                width:*width,
                height:*height,
                pixel:TexturePixel::RGBAf32,
                category: TextureCategory::Vec,
            }),
            Self::VecRu8{width,height,..}=>Some(TextureAlloc{
                width:*width,
                height:*height,
                pixel:TexturePixel::Ru8,
                category: TextureCategory::Vec,
            }),
            Self::VecRGu8{width,height,..}=>Some(TextureAlloc{
                width:*width,
                height:*height,
                pixel:TexturePixel::RGu8,
                category: TextureCategory::Vec,
            }),
            Self::VecRf32{width,height,..}=>Some(TextureAlloc{
                width:*width,
                height:*height,
                pixel:TexturePixel::Rf32,
                category: TextureCategory::Vec,
            }),
            _=>None
        }
    }
    #[allow(unused)]    
    pub(crate) fn as_render_alloc(&self, width:usize, height:usize)->Option<TextureAlloc>{
        match self{
            Self::RenderBGRAu8{size,..}=>{
                let (width,height) = size.width_height(width, height);
                Some(TextureAlloc{
                    width,
                    height,
                    pixel:TexturePixel::BGRAu8,
                    category: TextureCategory::Render,
                })
            }
            Self::RenderRGBAf16{size,..}=>{
                let (width,height) = size.width_height(width, height);
                Some(TextureAlloc{
                    width,
                    height,
                    pixel:TexturePixel::RGBAf16,
                    category: TextureCategory::Render,
                })
            }
            Self::RenderRGBAf32{size,..}=>{
                let (width,height) = size.width_height(width, height);
                Some(TextureAlloc{
                    width,
                    height,
                    pixel:TexturePixel::RGBAf32,
                    category: TextureCategory::Render,
                })
            }
            _=>None
        }
    }
        
    pub(crate) fn as_depth_alloc(&self, width:usize, height:usize)->Option<TextureAlloc>{
        match self{
            Self::DepthD32{size,..}=>{
                let (width,height) = size.width_height(width, height);
                Some(TextureAlloc{
                    width,
                    height,
                    pixel:TexturePixel::D32,
                    category: TextureCategory::DepthBuffer,
                })
            },
            _=>None
        }
    }

    #[cfg(any(target_os = "android", target_os = "linux"))]
    #[allow(unused)]
    pub(crate) fn as_video_alloc(&self)->Option<TextureAlloc>{
        match self{
            Self::VideoRGB => {
                Some(TextureAlloc{
                    width: 0,
                    height: 0,
                    pixel:TexturePixel::VideoRGB,
                    category: TextureCategory::Video,
                })
            },
            _ => None
        }
    }
    
    #[allow(unused)]
    pub(crate) fn as_shared_alloc(&self)->Option<TextureAlloc>{
        match self{
            Self::SharedBGRAu8{width, height, ..}=>{
                Some(TextureAlloc{
                    width:*width,
                    height:*height,
                    pixel:TexturePixel::BGRAu8,
                    category: TextureCategory::Shared,
                })
            }
            _=>None
        }
    }

    #[allow(unused)]
    fn is_compatible_with(&self, other: &Self) -> bool {
        #[cfg(any(target_os = "android", target_os = "linux"))]
        {
            return !(self.is_video() ^ other.is_video());
        }
        true
    }
}

impl Default for TextureFormat {
    fn default() -> Self {
        TextureFormat::Unknown
    }
}

impl Texture {
    pub fn new(cx: &mut Cx) -> Self {
        cx.null_texture()
    }

    pub fn new_with_format(cx: &mut Cx, format: TextureFormat) -> Self {
        let texture = cx.textures.alloc(format);
        texture
    }
    
    pub fn set_animation(&self, cx: &mut Cx, animation: Option<TextureAnimation>) {
        cx.textures[self.texture_id()].animation = animation;
    }
        
    pub fn animation<'a>(&self,cx: &'a mut Cx) -> &'a Option<TextureAnimation> {
        &cx.textures[self.texture_id()].animation
    }
        
    pub fn get_format<'a>(&self, cx: &'a mut Cx) -> &'a mut TextureFormat {
        &mut cx.textures[self.texture_id()].format
    }

    pub fn take_vec_u32(&self, cx: &mut Cx) -> Vec<u32> {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let data = match &mut cx_texture.format {
            TextureFormat::VecBGRAu8_32 { data, .. } => data,
            _ => panic!("incorrect texture format for u32 image data"),
        };
        data.take().expect("image data already taken")
    }
    
    pub fn swap_vec_u32(&self, cx: &mut Cx, vec: &mut Vec<u32>) {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let (data, updated) = match &mut cx_texture.format {
            TextureFormat::VecBGRAu8_32 { data, updated, .. } => (data, updated),
            _ => panic!("incorrect texture format for u32 image data"),
        };
        if data.is_none(){
            *data = Some(vec![]);
        }
        if let Some(data) = data{
            std::mem::swap(data, vec)
        }
        *updated = updated.update(None);
    }

    pub fn put_back_vec_u32(&self, cx: &mut Cx, new_data: Vec<u32>, dirty_rect: Option<RectUsize>) {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let (data, updated) = match &mut cx_texture.format {
            TextureFormat::VecBGRAu8_32 { data, updated, .. } => (data, updated),
            _ => panic!("incorrect texture format for u32 image data"),
        };
        //assert!(data.is_none(), "image data not taken or already put back");
        *data = Some(new_data);
        *updated = updated.update(dirty_rect);
    }

    pub fn take_vec_u8(&self, cx: &mut Cx) -> Vec<u8> {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let data = match &mut cx_texture.format {
            TextureFormat::VecRu8 { data, .. } => data,
            TextureFormat::VecRGu8 { data, .. } => data,
            _ => panic!("incorrect texture format for u32 image data"),
        };
        data.take().expect("image data already taken")
    }

    pub fn put_back_vec_u8(&self, cx: &mut Cx, new_data: Vec<u8>, dirty_rect: Option<RectUsize>) {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let (data, updated) = match &mut cx_texture.format {
            TextureFormat::VecRu8 { data, updated, .. } => (data, updated),
            TextureFormat::VecRGu8 { data,updated, .. } => (data, updated),
            _ => panic!("incorrect texture format for u8 image data"),
        };
        assert!(data.is_none(), "image data not taken or already put back");
        *data = Some(new_data);
        *updated = updated.update(dirty_rect);
    }
            
    pub fn take_vec_f32(&self, cx: &mut Cx) -> Vec<f32> {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let data = match &mut cx_texture.format{
            TextureFormat::VecRf32 { data, .. } => data,
            TextureFormat::VecRGBAf32{data, .. } => data,
            _ => panic!("Not the correct texture desc for f32 image data"),
        };
        data.take().expect("image data already taken")
    }

    pub fn put_back_vec_f32(&self, cx: &mut Cx, new_data: Vec<f32>, dirty_rect: Option<RectUsize>) {
        let cx_texture = &mut cx.textures[self.texture_id()];
        let (data, updated) = match &mut cx_texture.format {
            TextureFormat::VecRf32 { data, updated, .. } => (data, updated),
            TextureFormat::VecRGBAf32 { data, updated, .. } => (data, updated),
            _ => panic!("incorrect texture format for f32 image data"),
        };
        assert!(data.is_none(), "image data not taken or already put back");
        *data = Some(new_data);
        *updated = updated.update(dirty_rect);
    }
}

#[derive(Default)]
pub struct CxTexture {
    pub (crate) format: TextureFormat,
    pub (crate) alloc: Option<TextureAlloc>,
    pub (crate) animation: Option<TextureAnimation>,
    pub os: CxOsTexture,
    pub previous_platform_resource: Option<CxOsTexture>,
}