tex_packer_core/
runtime_atlas.rs

1use crate::config::PackerConfig;
2use crate::error::{Result, TexPackerError};
3use crate::model::Frame;
4use crate::runtime::{AtlasSession, RuntimeStats, RuntimeStrategy};
5use image::{Rgba, RgbaImage};
6
7/// Region that needs to be updated on GPU texture.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct UpdateRegion {
10    /// Page ID that needs updating.
11    pub page_id: usize,
12    /// X coordinate of the region.
13    pub x: u32,
14    /// Y coordinate of the region.
15    pub y: u32,
16    /// Width of the region.
17    pub width: u32,
18    /// Height of the region.
19    pub height: u32,
20}
21
22impl UpdateRegion {
23    /// Create an empty update region.
24    pub fn empty() -> Self {
25        Self {
26            page_id: 0,
27            x: 0,
28            y: 0,
29            width: 0,
30            height: 0,
31        }
32    }
33
34    /// Check if this region is empty.
35    pub fn is_empty(&self) -> bool {
36        self.width == 0 || self.height == 0
37    }
38
39    /// Get the area of this region in pixels.
40    pub fn area(&self) -> u64 {
41        (self.width as u64) * (self.height as u64)
42    }
43}
44
45/// Runtime atlas with pixel data management.
46///
47/// This extends `AtlasSession` by managing actual pixel data in addition to geometry.
48/// Useful for game engines that need to dynamically update GPU textures.
49pub struct RuntimeAtlas {
50    session: AtlasSession,
51    pages: Vec<RgbaImage>,
52    background_color: Rgba<u8>,
53}
54
55impl RuntimeAtlas {
56    /// Create a new runtime atlas with pixel data management.
57    pub fn new(cfg: PackerConfig, strategy: RuntimeStrategy) -> Self {
58        Self {
59            session: AtlasSession::new(cfg, strategy),
60            pages: Vec::new(),
61            background_color: Rgba([0, 0, 0, 0]), // Transparent by default
62        }
63    }
64
65    /// Set the background color for new pages.
66    pub fn with_background_color(mut self, color: Rgba<u8>) -> Self {
67        self.background_color = color;
68        self
69    }
70
71    /// Append a texture with its pixel data.
72    /// Returns (page_id, frame, update_region).
73    pub fn append_with_image(
74        &mut self,
75        key: String,
76        image: &RgbaImage,
77    ) -> Result<(usize, Frame<String>, UpdateRegion)> {
78        let (w, h) = image.dimensions();
79        let (page_id, frame) = self.session.append(key, w, h)?;
80
81        // Ensure page exists
82        self.ensure_page(page_id);
83
84        // Blit image to page
85        let update_region = self.blit_to_page(page_id, &frame, image)?;
86
87        Ok((page_id, frame, update_region))
88    }
89
90    /// Append a texture by dimensions only (no pixel data).
91    /// Returns (page_id, frame).
92    pub fn append(&mut self, key: String, w: u32, h: u32) -> Result<(usize, Frame<String>)> {
93        self.session.append(key, w, h)
94    }
95
96    /// Evict a texture and optionally clear its region.
97    /// Returns the region that was cleared (if clear=true).
98    pub fn evict_with_clear(
99        &mut self,
100        page_id: usize,
101        key: &str,
102        clear: bool,
103    ) -> Option<UpdateRegion> {
104        // Get reserved slot before evicting (covers padding/extrude)
105        let slot_region = if clear {
106            self.session
107                .get_reserved_slot(key)
108                .map(|(pid, slot)| UpdateRegion {
109                    page_id: pid,
110                    x: slot.x,
111                    y: slot.y,
112                    width: slot.w,
113                    height: slot.h,
114                })
115        } else {
116            None
117        };
118
119        // Evict from session
120        if self.session.evict(page_id, key) {
121            // Clear pixels if requested
122            if clear {
123                if let Some(region) = slot_region {
124                    self.clear_region(region);
125                    return Some(region);
126                }
127            }
128            Some(UpdateRegion::empty())
129        } else {
130            None
131        }
132    }
133
134    /// Evict a texture by key and optionally clear its region.
135    pub fn evict_by_key_with_clear(&mut self, key: &str, clear: bool) -> Option<UpdateRegion> {
136        // Get reserved slot before evicting
137        let slot_region = if clear {
138            self.session
139                .get_reserved_slot(key)
140                .map(|(page_id, slot)| UpdateRegion {
141                    page_id,
142                    x: slot.x,
143                    y: slot.y,
144                    width: slot.w,
145                    height: slot.h,
146                })
147        } else {
148            None
149        };
150
151        if self.session.evict_by_key(key) {
152            if clear {
153                if let Some(region) = slot_region {
154                    self.clear_region(region);
155                    return Some(region);
156                }
157            }
158            Some(UpdateRegion::empty())
159        } else {
160            None
161        }
162    }
163
164    /// Get a reference to the pixel data of a page.
165    pub fn get_page_image(&self, page_id: usize) -> Option<&RgbaImage> {
166        self.pages.get(page_id)
167    }
168
169    /// Get a mutable reference to the pixel data of a page.
170    pub fn get_page_image_mut(&mut self, page_id: usize) -> Option<&mut RgbaImage> {
171        self.pages.get_mut(page_id)
172    }
173
174    /// Get the number of pages with pixel data.
175    pub fn num_pages(&self) -> usize {
176        self.pages.len()
177    }
178
179    // Delegate query methods to session
180    pub fn get_frame(&self, key: &str) -> Option<(usize, &Frame<String>)> {
181        self.session.get_frame(key)
182    }
183
184    pub fn contains(&self, key: &str) -> bool {
185        self.session.contains(key)
186    }
187
188    pub fn keys(&self) -> Vec<&str> {
189        self.session.keys()
190    }
191
192    pub fn texture_count(&self) -> usize {
193        self.session.texture_count()
194    }
195
196    pub fn stats(&self) -> RuntimeStats {
197        self.session.stats()
198    }
199
200    pub fn snapshot_atlas(&self) -> crate::model::Atlas<String> {
201        self.session.snapshot_atlas()
202    }
203
204    /// Ensure a page exists, creating it if necessary.
205    fn ensure_page(&mut self, page_id: usize) {
206        while self.pages.len() <= page_id {
207            let page_img = RgbaImage::from_pixel(
208                self.session.cfg.max_width,
209                self.session.cfg.max_height,
210                self.background_color,
211            );
212            self.pages.push(page_img);
213        }
214    }
215
216    /// Blit an image to a page at the frame's position.
217    fn blit_to_page(
218        &mut self,
219        page_id: usize,
220        frame: &Frame<String>,
221        image: &RgbaImage,
222    ) -> Result<UpdateRegion> {
223        let page = self
224            .pages
225            .get_mut(page_id)
226            .ok_or_else(|| TexPackerError::InvalidConfig("Page not found".into()))?;
227
228        let (src_w, src_h) = image.dimensions();
229        let dst_x = frame.frame.x;
230        let dst_y = frame.frame.y;
231
232        // Reuse core compositing (with extrusion and optional outlines)
233        let extrude = self.session.cfg.texture_extrusion;
234        let outlines = self.session.cfg.texture_outlines;
235        crate::compositing::blit_rgba(
236            image,
237            page,
238            dst_x,
239            dst_y,
240            0,
241            0,
242            src_w,
243            src_h,
244            frame.rotated,
245            extrude,
246            outlines,
247        );
248
249        // Return the minimal update region including extrusion
250        let start_x = dst_x.saturating_sub(extrude);
251        let start_y = dst_y.saturating_sub(extrude);
252        let mut width = frame.frame.w + extrude.saturating_mul(2);
253        let mut height = frame.frame.h + extrude.saturating_mul(2);
254        // Clamp to page bounds
255        if start_x + width > page.width() {
256            width = page.width() - start_x;
257        }
258        if start_y + height > page.height() {
259            height = page.height() - start_y;
260        }
261
262        Ok(UpdateRegion {
263            page_id,
264            x: start_x,
265            y: start_y,
266            width,
267            height,
268        })
269    }
270
271    /// Clear a region on a page.
272    fn clear_region(&mut self, region: UpdateRegion) {
273        if let Some(page) = self.pages.get_mut(region.page_id) {
274            for y in region.y..(region.y + region.height).min(page.height()) {
275                for x in region.x..(region.x + region.width).min(page.width()) {
276                    page.put_pixel(x, y, self.background_color);
277                }
278            }
279        }
280    }
281}