-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.rs
More file actions
727 lines (641 loc) · 28.5 KB
/
graph.rs
File metadata and controls
727 lines (641 loc) · 28.5 KB
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
use crate::core::context::alpha::AlphaMethod;
use crate::core::context::line_context::LineContext;
use crate::core::context::{AlphaContext, BezierContext, WindowContext};
use crate::draw::tools::brush::Brush;
use crate::utils::math::rng::XorShiftRng;
use std::ptr;
use crate::core::context::gpu::GpuContext;
use crate::core::default_colors::TRANSPARENT_BLACK;
#[derive(Debug)]
pub enum FrameBufferStatus {
/// An error occurred while trying to set the active frame buffer
ErrorBadBufferIndex = 100,
/// An error occurred while trying to copy frame buffers
ErrorPixelOutOfBounds = 200,
/// Removing the active buffer is not allowed!
ErrorCannotRemoveActiveFrameBuf = 300,
/*
/// Attempt to perform an indirect operation on the active buffer
ErrorBufferIsActive = 300,
*/
/// Warning: the buffer was resized to zero pixels.
WarningResizedToZero = 1000,
/// Warning: the source and destination frame buffers are the same.
WarningSameSourceAndDestination = 1001,
/// Warning: Too many buffers requested to be removed.
/// The number of removed buffers was capped so that at least one buffer remains.
WarningBuffersRemovalWasCapped = 1100,
}
#[derive(Debug)]
/// A struct that holds an immutable reference to a frame buffer
pub struct ImmutableFrameBuffer<'a> {
/// The index of the frame buffer in the vector of frame buffers
pub frame_buf_index: usize,
/// A reference to the frame buffer itself
pub frame_buf: &'a Vec<u32>,
}
#[derive(Debug)]
/// A struct that holds a mutable reference to the currently active frame buffer
/// and a vector of immutable references to other frame buffers.
pub struct MultipleFrameBuffers<'a> {
/// A mutable reference to the currently active frame buffer
pub active: &'a mut Vec<u32>,
/// A vector of immutable references to other frame buffers
pub immut: Vec<ImmutableFrameBuffer<'a>>,
}
#[derive(Debug)]
pub struct GraphContext<UserData = Vec<i32>> {
/// Reference to the window context
pub win: WindowContext,
/* start FRAME BUFFER related fields */
/// The currently active renderable buffer
pub frame_buf: Vec<u32>,
/// A vector of initialized frame buffers
frame_bufs: Vec<Vec<u32>>,
/// The index of the currently active frame buffer
active_frame_buf_index: usize,
/* end FRAME BUFFER related fields */
/// A vector of user-defined data. Store any information here that needs to be passed around with the context
pub user_data: Box<UserData>,
/// Settings for rendering controls for Bezier curves
pub bezier: BezierContext,
/// Current frame in animation. If no animation is needed, can be set to `0`
pub frame_count: usize,
/// Configurations for alpha blending (where applicable)
pub alpha: AlphaContext,
/// Some operations in Graph1 can be sped up using multiple CPUs / CPU cores.
/// If the target hardware has multiple cores and `num_threads > 1`,
/// Graph1 will attempt to create `num_threads` threads to parallelize some computations.
/// If `1`, Graph1 will perform calculations only on the main thread.
/// If `0`, Graph1 will not perform those operations, that support multithreading. Not recommended for usage.
pub num_threads: usize,
/// A random number generator (XorShiftRng) seeded with default values.
/// If needed, reseed using:
/// - `rng.set_seed_32(seed_u32)`
/// - `rng.set_seed_64(seed_u64)`
pub rng: XorShiftRng,
/// Settings for line drawing
pub line: LineContext,
/// The brush used for paint-brush operations.
pub brush: Brush,
/// Context for very experimental GPU rendering.
/// **NB:** Use only if you know what you are doing!
pub gpu_context: GpuContext,
}
impl<UserData: Default> GraphContext<UserData> {
/// Instantiates a new `GraphContext`.
/// The advanced settings like `alpha` and `bezier` are set to default,
/// please configure them manually via your context instance.
/// # Arguments
/// * `win` - The window context
/// * `use_alpha` - Whether to enable alpha blending
/// * `num_frame_bufs` - How many frame buffers to create (including the active one).
/// * `user_data` - Optional user-defined data
/// * `num_threads` - How many threads to use for rendering.
/// * `0` - skip operations that support multithreading (rather should not be used).
/// * `1` - use main thread only.
/// * `2` - and more - use that many threads.
/// * `line` - Optional line context. If `None` given, default settings will be used.
/// # Returns
/// A new `GraphContext` instance
pub fn new(
win: WindowContext,
use_alpha: bool,
num_frame_bufs: usize,
user_data: Option<UserData>,
num_threads: usize,
line: Option<LineContext>,
) -> GraphContext<UserData> {
// How many pixels are in the frame buffer
let num_pixels = win.get_num_pixels();
let bg_color = win.background_color;
// Ensure at least one frame buffer will be created
let num_frame_bufs = num_frame_bufs.max(1);
// TODO: write a unit test that ensures that the number of frame buffers is always at least 1.
let frame_buf: Vec<u32>;
let mut frame_bufs: Vec<Vec<u32>> = Vec::new();
// Only one frame buffer is needed, so it is created directly as the active frame buffer.
if num_frame_bufs == 1 {
frame_buf = vec![bg_color; num_pixels];
} else {
// Initialize the active frame buffer with a dummy.
// TODO: check if we can actually avoid allocating memory for the dummy and still be able to use
// TODO: `std::mem::swap` (or maybe something like `std::mem::take`?)
// TODO: TRY TO USE `Vec::new()` (an empty vector) FOR THE DUMMY!
// TODO: Chances are, everything will keep working, because the dummy is never actually used for rendering,
// TODO: and the overall memory footprint may become one frame_buf smaller!
frame_buf = vec![TRANSPARENT_BLACK; num_pixels];
// Create additional frame buffers
for _ in 1..=num_frame_bufs {
frame_bufs.push(vec![bg_color; num_pixels]);
}
}
let mut ctx = GraphContext {
win,
frame_buf,
frame_bufs,
active_frame_buf_index: 0, // Index of the currently active frame buffer
// Use the provided `user_data` or default to `UserData::default()`
user_data: Box::new(user_data.unwrap_or_default()),
bezier: BezierContext::default(),
frame_count: 0,
alpha: AlphaContext {
enabled: use_alpha,
method: AlphaMethod::Int,
},
num_threads,
rng: XorShiftRng::default(),
line: line.unwrap_or_default(),
brush: Brush::default(),
gpu_context: GpuContext::create(),
};
// Set buffer `0` to be the active frame buffer
if num_frame_bufs > 1 {
std::mem::swap(&mut ctx.frame_buf, &mut ctx.frame_bufs[0]);
}
ctx
}
}
impl<UserData> GraphContext<UserData> {
/// Resizes the window context, the frame buffer, and the back frame buffers, if any.
/// If the new size is zero, `FrameBufferError::ResizedToZero` is returned.
/// # Arguments
/// * `w` - The new width of the window
/// * `h` - The new height of the window
pub fn resize(&mut self, w: u32, h: u32) -> Result<(), FrameBufferStatus> {
// resize the window (which also resizes the quadrants)
self.win.resize(w, h);
// resize the frame buffer
let num_pixels = self.win.get_num_pixels();
self.frame_buf.resize(num_pixels, self.win.background_color);
if self.frame_bufs.len() > 0 {
// resize all the existing frame buffers
for frame_buf in &mut self.frame_bufs {
frame_buf.resize(num_pixels, self.win.background_color);
}
}
if num_pixels == 0 {
return Err(FrameBufferStatus::WarningResizedToZero);
}
Ok(())
}
/*
// FIXME:
// FIXME: This function is a part of an effort for adding and removing frame buffers dynamically.
// FIXME: Instead of initializing them statically at the creation of the contex
// FIXME: This function is not implemented and not tested yet!
// FIXME:
// FIXME:
/// NB!: DON'T USE THIS FUNCTION!
/// TODO: 1. FIX THE DOCS! Too long, too detailed.
/// TODO: 2. Test the function!
/// TODO: 3. See the TODO above `frame_buf = vec![TRANSPARENT_BLACK; num_pixels];`
/// Resizes the window context, the active frame buffer, and the back frame buffers, if any.
///
/// This function guarantees that all *real* frame buffers are resized to the new resolution.
/// The "dummy" buffer (which lives in the slot of the currently active buffer inside
/// `self.frame_bufs`) does not represent an actual framebuffer and therefore should not
/// consume memory proportional to the window size. To ensure all *real* buffers are resized
/// uniformly without special-case logic, we temporarily swap the dummy into `self.frame_buf`
/// and put the real active buffer back into its slot in `self.frame_bufs`. That way, every
/// element of `self.frame_bufs` contains an actual framebuffer during the resize loop.
///
/// After resizing, we swap again to restore the original invariant:
/// - `self.frame_buf` contains the currently active framebuffer.
/// - `self.frame_bufs[self.active_frame_buf_index]` contains the dummy placeholder.
///
/// # Arguments
/// * `w` - The new width of the window.
/// * `h` - The new height of the window.
///
/// # Returns
/// * `Ok(())` on success.
/// * `Err(FrameBufferStatus::WarningResizedToZero)` if the new resolution is zero
/// (in which case the buffers are still resized, but to zero pixels).
pub fn resize__new(&mut self, w: u32, h: u32) -> Result<(), FrameBufferStatus> {
panic!("Not implemented / tested!");
// Resize the window (this updates width, height, and quadrants).
self.win.resize(w, h);
// Compute new total number of pixels.
let num_pixels = self.win.get_num_pixels();
// --- Step A: Swap dummy into self.frame_buf ---
// This ensures that all entries in self.frame_bufs are "real" buffers
// that should be resized, including the one that was active.
std::mem::swap(
&mut self.frame_buf,
&mut self.frame_bufs[self.active_frame_buf_index],
);
// --- Step B: Resize all real buffers uniformly ---
for buf in &mut self.frame_bufs {
buf.resize(num_pixels, self.win.background_color);
}
// --- Step C: Swap back to restore the invariant ---
std::mem::swap(
&mut self.frame_buf,
&mut self.frame_bufs[self.active_frame_buf_index],
);
// If the resolution is zero, signal a warning (but still resized correctly).
if num_pixels == 0 {
return Err(FrameBufferStatus::WarningResizedToZero);
}
Ok(())
}
*/
/// Sets the pixel at (x, y) in the frame buffer to the specified color.
/// If the coordinates are out of bounds, the pixel will not be set,
/// and `FrameBufferError::PixelOutOfBounds` is returned.
/// # Arguments
/// * `x` - The x-coordinate of the pixel
/// * `y` - The y-coordinate of the pixel
/// * `color` - The color to set the pixel to, in RGBA format (0xRRGGBBAA)
/// * `back_buf_index` - Optional index of the back buffer to set the pixel in. If `None`, sets the pixel in the active frame buffer.
pub fn set_pixel(&mut self, x: u32, y: u32, color: u32) -> Result<(), FrameBufferStatus> {
if x < self.win.w && y < self.win.h {
self.frame_buf[(x + y * self.win.w) as usize] = color;
return Ok(());
}
Err(FrameBufferStatus::ErrorPixelOutOfBounds)
}
/// Reads the pixel color from the frame buffer at (x, y).
/// If the coordinates are out of bounds, returns `None`,
/// and `FrameBufferError::PixelOutOfBounds` is returned.
/// # Arguments
/// * `x` - The x-coordinate of the pixel
/// * `y` - The y-coordinate of the pixel
pub fn get_pixel(&mut self, x: u32, y: u32) -> Result<u32, FrameBufferStatus> {
if x < self.win.w && y < self.win.h {
return Ok(self.frame_buf[(x + y * self.win.w) as usize]);
}
Err(FrameBufferStatus::ErrorPixelOutOfBounds)
}
/// Returns index of a frame buffer that is currently active.
pub fn get_active_frame_buf_index(&self) -> usize {
self.active_frame_buf_index
}
/// Sets the active frame buffer to the one specified by `frame_buf_index`.
/// If the specified index is the same as the currently active one, no operation is performed
/// and `FrameBufferError::BadBufferIndex` is returned.
/// # Arguments
/// * `frame_buf_index` - The index of the frame buffer to set as active.
pub fn set_active_frame_buf(
&mut self,
frame_buf_index: usize,
) -> Result<(), FrameBufferStatus> {
if frame_buf_index == self.active_frame_buf_index {
// No operation needed, the requested buffer is already active
return Err(FrameBufferStatus::WarningSameSourceAndDestination);
}
if frame_buf_index < self.frame_bufs.len() {
// Return the currently active buffer to its place
std::mem::swap(
&mut self.frame_buf,
&mut self.frame_bufs[self.active_frame_buf_index],
);
// Set the requested frame buffer as the active one
std::mem::swap(&mut self.frame_buf, &mut self.frame_bufs[frame_buf_index]);
self.active_frame_buf_index = frame_buf_index;
return Ok(());
}
Err(FrameBufferStatus::ErrorBadBufferIndex)
}
// pub fn add_frame_bufs(&mut self, bg_colors:Vec<u32>) -> usize {
// let num_pixels = self.win.get_num_pixels();
// let bg_color = self.win.background_color;
// self.frame_bufs.push(vec![bg_color; num_pixels]);
// 234
// }
// pub fn remove_frame_bufs(&mut self, something:usize) {
// let num_pixels = self.win.get_num_pixels();
// let bg_color = self.win.background_color;
// self.frame_bufs.push(vec![bg_color; num_pixels]);
// }
/// Copies the contents of the source frame buffer to the destination frame buffer.
/// If the source and destination indices are the same, no operation is performed,
/// and `FrameBufferError::BadBufferIndex` is returned.
///
/// # Arguments
/// * `src_index` - The index of the source frame buffer to copy from.
/// * `dst_index` - The index of the destination frame buffer to copy to.
///
pub fn frame_buf_copy(
&mut self,
src_index: usize,
dst_index: usize,
) -> Result<(), FrameBufferStatus> {
if src_index == dst_index {
// Can't copy from a buffer to itself, no operation needed
return Err(FrameBufferStatus::WarningSameSourceAndDestination);
}
// Ensure the source and destination indices are within bounds and not the same
if src_index < self.frame_bufs.len() && dst_index < self.frame_bufs.len() {
// Return the currently active buffer to its place in the vector for simplicity of indexing
// Effectively, at this point `frame_buf` must reference the dummy filled with `default_colors::TRANSPARENT_BLACK`
std::mem::swap(
&mut self.frame_buf,
&mut self.frame_bufs[self.active_frame_buf_index],
);
unsafe {
ptr::copy_nonoverlapping(
self.frame_bufs[src_index].as_ptr(),
self.frame_bufs[dst_index].as_mut_ptr(),
self.frame_buf.len(),
);
}
// Restore the active buffer reference
std::mem::swap(
&mut self.frame_buf,
&mut self.frame_bufs[self.active_frame_buf_index],
);
return Ok(());
}
Err(FrameBufferStatus::ErrorBadBufferIndex)
}
/// Copies the contents of the currently active frame buffer to the frame buffer specified by `frame_buf_index`.
/// If the destination frame buffer and the active one are the same, no operation is performed,
/// and `FrameBufferError::BadBufferIndex` is returned.
/// # Arguments
/// * `frame_buf_index` - The index of the frame buffer to copy to.
pub fn copy_to_active_frame_buf_from(
&mut self,
frame_buf_index: usize,
) -> Result<(), FrameBufferStatus> {
if frame_buf_index == self.active_frame_buf_index {
// Can't copy from a buffer to itself, no operation needed
return Err(FrameBufferStatus::WarningSameSourceAndDestination);
}
if frame_buf_index < self.frame_bufs.len() {
unsafe {
ptr::copy_nonoverlapping(
self.frame_bufs[frame_buf_index].as_ptr(),
self.frame_buf.as_mut_ptr(),
self.frame_buf.len(),
);
}
return Ok(());
}
Err(FrameBufferStatus::ErrorBadBufferIndex)
}
/// Copies the contents of the specified frame buffer to the currently active frame buffer.
/// If the source frame buffer is the active one, no operation is performed,
/// and `FrameBufferError::BadBufferIndex` is returned.
/// # Arguments
/// * `frame_buf_index` - The index of the frame buffer to copy from.
pub fn copy_from_active_frame_buf_to(
&mut self,
frame_buf_index: usize,
) -> Result<(), FrameBufferStatus> {
if frame_buf_index == self.active_frame_buf_index {
// Can't copy from a buffer to itself, no operation needed
return Err(FrameBufferStatus::WarningSameSourceAndDestination);
}
if frame_buf_index < self.frame_bufs.len() {
unsafe {
ptr::copy_nonoverlapping(
self.frame_buf.as_ptr(),
self.frame_bufs[frame_buf_index].as_mut_ptr(),
self.frame_buf.len(),
);
}
return Ok(());
}
Err(FrameBufferStatus::ErrorBadBufferIndex)
}
/// Returns a mutable reference to the currently active frame buffer and
/// a vector of immutable references to frame buffers specified by the caller.
/// # Arguments
/// * `frame_buf_indices` - Indices of the frame buffers to return as immutable.
/// # Returns
/// A `MultipleFrameBuffers` struct containing:
/// * `active` - A mutable reference to the currently active frame buffer.
/// * `immut` - A vector of immutable references to the frame buffers specified by `frame_buf_indices`.
/// # Note
/// - **NB:** If you want `immut` to maintain the order of the frame buffers specified in `frame_buf_indices`,
/// **do not** include the active frame buffer index in `frame_buf_indices`.
/// - The active frame buffer will not be included in the `immut` vector as it is returned as mutable in `active`.
pub fn get_multi_frame_bufs(
&mut self,
frame_buf_indices: &[usize],
) -> Result<MultipleFrameBuffers<'_>, FrameBufferStatus> {
let mut immut_frame_bufs: Vec<ImmutableFrameBuffer> = Vec::new();
for buf_index in frame_buf_indices {
let buf_index = *buf_index;
if buf_index >= self.frame_bufs.len() {
return Err(FrameBufferStatus::ErrorBadBufferIndex);
}
// Skip the active frame buffer as it'll be returned as mutable.
if buf_index == self.active_frame_buf_index {
continue;
}
immut_frame_bufs.push(ImmutableFrameBuffer {
frame_buf_index: buf_index,
frame_buf: &self.frame_bufs[buf_index],
});
}
Ok(MultipleFrameBuffers {
active: &mut self.frame_buf,
immut: immut_frame_bufs,
})
}
}
/***************************************************************************************************
----------------------------------------------------------------------------------------------------
[████████] [██████] [██████] [████████] [██████]
[██] [██] [██] [██] [██]
[██] [████] [██████] [██] [██████]
[██] [██] [██] [██] [██]
[██] [██████] [██████] [██] [██████]
....................................................................................................
****************************************************************************************************/
#[cfg(test)]
mod tests {
use super::*;
fn make_ctx(width: u32, height: u32, num_bufs: usize) -> GraphContext<()> {
let background = Some(0xFF0000FF); // Red, fully opaque
let foreground = Some(0xFFFFFFFF); // White, fully opaque
let win = WindowContext::new(width, height, background, foreground);
GraphContext::new(win, false, num_bufs, None, 1, None)
}
#[test]
fn test_new_single_buffer() {
let width = 4;
let height = 3;
let ctx = make_ctx(width, height, 1);
assert_eq!(ctx.frame_buf.len(), (width * height) as usize);
assert_eq!(ctx.get_active_frame_buf_index(), 0);
// Check that the second buffer is initialized with the background color
assert!(ctx.frame_buf.iter().all(|&c| c == ctx.win.background_color));
}
#[test]
fn test_new_multi_buffer() {
let width = 2;
let height = 2;
let ctx = make_ctx(width, height, 2);
assert_eq!(ctx.frame_buf.len(), 4);
assert_eq!(ctx.get_active_frame_buf_index(), 0);
assert_eq!(ctx.frame_bufs.len(), 2);
// Check that the second buffer is initialized
assert_eq!(ctx.frame_bufs[1].len(), 4);
// Check that the second buffer is initialized with the background color
assert!(ctx.frame_bufs[1]
.iter()
.all(|&c| c == ctx.win.background_color));
}
#[test]
fn test_resize() {
let mut ctx = make_ctx(2, 2, 1);
assert_eq!(ctx.frame_buf.len(), 4);
ctx.resize(3, 6).unwrap();
assert_eq!(ctx.frame_buf.len(), 3 * 6);
assert_eq!(ctx.win.w, 3);
assert_eq!(ctx.win.h, 6);
}
#[test]
fn test_resize_to_zero() {
let mut ctx = make_ctx(2, 2, 1);
let res = ctx.resize(10, 0);
assert!(matches!(res, Err(FrameBufferStatus::WarningResizedToZero)));
let res = ctx.resize(4, 4);
assert!(matches!(res, Ok(())));
assert_eq!(ctx.frame_buf.len(), 4 * 4);
let res = ctx.resize(0, 10);
assert!(matches!(res, Err(FrameBufferStatus::WarningResizedToZero)));
}
#[test]
fn test_set_and_get_pixel() {
let mut ctx = make_ctx(3, 2, 1);
let color = 0xAABBCCDD;
assert!(ctx.set_pixel(1, 1, color).is_ok());
assert_eq!(ctx.get_pixel(1, 1).unwrap(), color);
// Out of bounds
assert!(ctx.set_pixel(10, 10, color).is_err());
assert!(ctx.get_pixel(10, 10).is_err());
}
#[test]
fn test_active_frame_buf_index() {
let ctx = make_ctx(2, 2, 2);
assert_eq!(ctx.get_active_frame_buf_index(), 0);
}
#[test]
fn test_set_active_frame_buf_and_swap() {
let mut ctx = make_ctx(2, 2, 3);
// Write unique values to each buffer in turn
for buf_index in 1..3 {
ctx.set_active_frame_buf(buf_index).unwrap();
for i in 0..ctx.frame_buf.len() {
ctx.frame_buf[i] = (buf_index as u32) * 0x11111111;
}
}
// Now verify that values persist after swaps
for buf_index in 1..3 {
ctx.set_active_frame_buf(buf_index).unwrap();
assert!(ctx
.frame_buf
.iter()
.all(|&v| v == (buf_index as u32) * 0x11111111));
}
// Out of bounds should error
let res = ctx.set_active_frame_buf(100);
// check that the error is `FrameBufferStatus::ErrorBadBufferIndex`
assert!(matches!(res, Err(FrameBufferStatus::ErrorBadBufferIndex)));
}
#[test]
fn test_frame_buf_copy() {
let mut ctx = make_ctx(2, 2, 2);
// Fill buffer 0 with a pattern
for i in 0..ctx.frame_buf.len() {
ctx.frame_buf[i] = 0x12345678;
}
// Copy from buffer 0 to buffer 1
assert!(ctx.frame_buf_copy(0, 1).is_ok());
// Switch to buffer 1 and check values
ctx.set_active_frame_buf(1).unwrap();
assert!(ctx.frame_buf.iter().all(|&v| v == 0x12345678));
// Should fail on same src/dst
let res = ctx.frame_buf_copy(0, 0);
assert!(matches!(
res,
Err(FrameBufferStatus::WarningSameSourceAndDestination)
));
}
#[test]
fn test_copy_to_and_from_active_frame_buf() {
let mut ctx = make_ctx(2, 2, 2);
// Write distinct data to buffer 0 and 1
let res = ctx.set_active_frame_buf(0);
// this is expected, and it's just a warning
assert!(matches!(
res,
Err(FrameBufferStatus::WarningSameSourceAndDestination)
));
for i in 0..ctx.frame_buf.len() {
ctx.frame_buf[i] = 0xCAFEBABE;
}
ctx.set_active_frame_buf(1).unwrap();
for i in 0..ctx.frame_buf.len() {
ctx.frame_buf[i] = 0xDEADBEEF;
}
// Copy buffer 1 into buffer 0 (active <- from)
assert!(ctx.copy_to_active_frame_buf_from(0).is_ok());
assert!(ctx.frame_buf.iter().all(|&v| v == 0xCAFEBABE));
// Copy active buffer (1) into buffer 0 (from active -> to)
ctx.set_active_frame_buf(0).unwrap();
assert!(ctx.copy_from_active_frame_buf_to(1).is_ok());
ctx.set_active_frame_buf(1).unwrap();
assert!(ctx.frame_buf.iter().all(|&v| v == 0xCAFEBABE));
}
////////////////////////////////////////////////////////////////////////////////////////////////
#[test]
fn returns_active_and_immut_refs_when_indices_valid() {
let mut ctx = make_ctx(2, 2, 3);
let indices = [0, 1, 2];
let res = ctx.get_multi_frame_bufs(&indices);
assert!(res.is_ok());
let bufs = res.unwrap();
// Should skip index 0 in immut (active), so only [1, 2]
let immut_indices: Vec<_> = bufs.immut.iter().map(|f| f.frame_buf_index).collect();
assert_eq!(immut_indices, vec![1, 2]);
// Active buffer should be mutable
bufs.active[0] = 0x22222222;
// The active buffer is 0 by default
assert_eq!(ctx.get_active_frame_buf_index(), 0);
}
#[test]
fn order_is_preserved_and_active_is_skipped() {
let mut ctx = make_ctx(2, 2, 4);
ctx.set_active_frame_buf(2).unwrap();
let indices = [3, 0, 2, 1];
let res = ctx.get_multi_frame_bufs(&indices);
assert!(res.is_ok());
let bufs = res.unwrap();
let immut_indices: Vec<_> = bufs.immut.iter().map(|f| f.frame_buf_index).collect();
// 2 is active, so only [3,0,1] in order
assert_eq!(immut_indices, vec![3, 0, 1]);
}
#[test]
fn returns_error_for_any_oob_index() {
let mut ctx = make_ctx(2, 2, 2);
// Buffers: 0,1; index 2 is invalid
let indices = [0, 2];
let res = ctx.get_multi_frame_bufs(&indices);
assert!(matches!(res, Err(FrameBufferStatus::ErrorBadBufferIndex)));
}
#[test]
fn returns_empty_immut_if_only_active_requested() {
let mut ctx = make_ctx(2, 2, 2);
let indices = [0]; // 0 is active
let res = ctx.get_multi_frame_bufs(&indices);
assert!(res.is_ok());
let bufs = res.unwrap();
assert!(bufs.immut.is_empty());
}
#[test]
fn active_mutation_reflects_on_context() {
let mut ctx = make_ctx(2, 2, 2);
let indices = [0, 1];
let res = ctx.get_multi_frame_bufs(&indices).unwrap();
for px in res.active.iter_mut() {
*px = 0xAABBCCDD;
}
assert!(ctx.frame_buf.iter().all(|&px| px == 0xAABBCCDD));
}
}