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
//! Locking the texture for read/write.

use std::ffi::c_void;
use std::ptr::NonNull;

use crate::{
    as_raw,
    geo::{Rect, Size},
};
use crate::{bind, Sdl};

use super::Texture;

/// A lock of the texture, ready to read/write as the raw pixels.
pub struct Lock<'texture> {
    texture: &'texture mut Texture<'texture>,
    pixels: NonNull<c_void>,
    len: usize,
}

impl std::fmt::Debug for Lock<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Lock")
            .field("texture", &self.texture)
            .field("len", &self.len)
            .finish_non_exhaustive()
    }
}

impl<'texture> Lock<'texture> {
    pub(super) fn new(texture: &'texture mut Texture<'texture>, area: Option<Rect>) -> Self {
        let area = area.map(Into::into);
        let (mut pixels, mut pitch) = (std::ptr::null_mut(), 0);
        let ret = unsafe {
            bind::SDL_LockTexture(texture.as_ptr(), as_raw(&area), &mut pixels, &mut pitch)
        };
        if ret != 0 {
            Sdl::error_then_panic("Obtaining texture lock");
        }

        use super::QueryExt;
        let Size { height, .. } = texture.size();
        Self {
            texture,
            pixels: NonNull::new(pixels).unwrap(),
            len: (pitch as u32 * height) as usize,
        }
    }

    /// Returns bytes of the pixels.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.pixels.as_ptr().cast(), self.len) }
    }

    /// Returns mutable bytes of the pixels.
    pub fn as_bytes_mut(&mut self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.pixels.as_ptr().cast(), self.len) }
    }
}

impl Drop for Lock<'_> {
    fn drop(&mut self) {
        unsafe { bind::SDL_UnlockTexture(self.texture.as_ptr()) }
    }
}