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
use core::ops;
use crate::buf::{atomic_buf, buf, cell_buf, AtomicBuffer, Buffer, CellBuffer};
use crate::layout::{Decay, DynLayout, Layout, SliceLayout, Take};
use crate::{BufferReuseError, TexelBuffer};
/// Inner buffer implementation.
///
/// Not exposed to avoid leaking the implementation detail of the `Buf` type parameter. This allows
/// a single implementation for borrowed and owned buffers while keeping `buf`, `Cog` etc. private.
#[derive(Default, Clone, PartialEq, Eq)]
pub(crate) struct RawImage<Buf, Layout> {
buffer: Buf,
layout: Layout,
}
pub(crate) trait BufferLike {
/// Get the length of the buffer in bytes.
fn byte_len(&self) -> usize;
/// Convert the bytes into a normalized buffer allocation.
fn into_owned(self) -> Buffer;
/// Transfer all bytes to a new instance.
fn take(&mut self) -> Self
where
Self: Sized;
}
pub(crate) trait BufferRef: BufferLike + ops::Deref<Target = buf> {}
pub(crate) trait BufferMut: BufferRef + ops::DerefMut {}
pub(crate) trait Growable: BufferLike {
fn grow_to(&mut self, _: usize);
}
/// Layout oblivious methods that can allocate and change to another buffer.
impl<B: Growable, L> RawImage<B, L> {
/// Grow the buffer, preparing for another layout.
///
/// This may allocate a new buffer and thus disassociate the image from the currently borrowed
/// underlying buffer.
///
/// # Panics
/// This function will panic if an allocation is necessary but fails.
pub(crate) fn grow(&mut self, layout: &impl Layout) {
Growable::grow_to(&mut self.buffer, layout.byte_len());
}
/// Convert the inner layout.
///
/// This method expects that the converted layout is compatible with the current layout.
///
/// # Panics
/// This method panics if the new layout requires more bytes and allocation fails.
pub(crate) fn decay<Other>(mut self) -> RawImage<B, Other>
where
Other: Decay<L>,
{
let layout = Other::decay(self.layout);
Growable::grow_to(&mut self.buffer, layout.byte_len());
RawImage {
buffer: self.buffer,
layout,
}
}
/// Change the layout, reusing and growing the buffer.
///
/// # Panics
/// This method panics if the new layout requires more bytes and allocation fails.
pub(crate) fn with_layout<Other: Layout>(mut self, layout: Other) -> RawImage<B, Other> {
Growable::grow_to(&mut self.buffer, layout.byte_len());
RawImage {
buffer: self.buffer,
layout,
}
}
/// Mutably borrow this image with another arbitrary layout.
///
/// The other layout could be completely incompatible and perform arbitrary mutations. This
/// seems counter intuitive at first, but recall that these mutations are not unsound as they
/// can not invalidate the bytes themselves and only write unexpected values. This provides
/// more flexibility for 'transmutes' than easily expressible in the type system.
///
/// # Panics
/// This method panics if the new layout requires more bytes and allocation fails.
pub(crate) fn as_reinterpreted<Other>(&mut self, other: Other) -> RawImage<&'_ mut buf, Other>
where
B: BufferMut,
Other: Layout,
{
self.grow(&other);
RawImage {
buffer: &mut self.buffer,
layout: other,
}
}
/// Change the layout and then resize the buffer so that it still fits.
pub(crate) fn mutate_layout<T>(&mut self, f: impl FnOnce(&mut L) -> T) -> T
where
L: Layout,
{
let t = f(&mut self.layout);
self.buffer.grow_to(self.layout.byte_len());
t
}
}
/// Layout oblivious methods, these also never allocate or panic.
impl<B: BufferLike, L> RawImage<B, L> {
/// Take ownership of the image's bytes.
///
/// # Panics
/// This method panics if allocation fails.
pub(crate) fn into_owned(self) -> RawImage<Buffer, L> {
RawImage {
buffer: BufferLike::into_owned(self.buffer),
layout: self.layout,
}
}
}
/// Methods specifically with a dynamic layout.
impl<B> RawImage<B, DynLayout> {
pub(crate) fn try_from_dynamic<Other>(self, layout: Other) -> Result<RawImage<B, Other>, Self>
where
Other: Into<DynLayout> + Clone,
{
let reference = layout.clone().into();
if self.layout == reference {
Ok(RawImage {
buffer: self.buffer,
layout,
})
} else {
Err(self)
}
}
}
impl<B, L> RawImage<B, L> {
/// Allocate a buffer for a particular layout.
pub(crate) fn new(layout: L) -> Self
where
L: Layout,
B: From<Buffer>,
{
let bytes = layout.byte_len();
RawImage {
buffer: Buffer::new(bytes).into(),
layout,
}
}
/// Create a image from a byte slice specifying the contents.
///
/// If the layout requires more bytes then the remaining bytes are zero initialized.
pub(crate) fn with_contents(buffer: &[u8], layout: L) -> Self
where
L: Layout,
B: From<Buffer>,
{
let mut buffer = Buffer::from(buffer);
buffer.grow_to(layout.byte_len());
RawImage {
buffer: buffer.into(),
layout,
}
}
/// Create a new raw image from layout and buffer.
///
/// # Panics
///
/// Panics if the buffer does not have enough space for the layout.
pub fn from_buffer(layout: L, buffer: B) -> Self
where
B: BufferLike,
L: Layout,
{
assert!(<dyn Layout>::fits_buffer(&layout, &buffer));
RawImage { buffer, layout }
}
pub(crate) fn with_buffer_unchecked(layout: L, buffer: B) -> Self
where
B: ops::Deref<Target = buf>,
{
RawImage { buffer, layout }
}
pub(crate) fn into_parts(self) -> (B, L) {
(self.buffer, self.layout)
}
/// Get a reference to the layout.
pub(crate) fn layout(&self) -> &L {
&self.layout
}
/// Get a mutable reference to the layout.
///
/// Be mindful not to modify the layout to exceed the allocated size.
pub(crate) fn layout_mut_unguarded(&mut self) -> &mut L {
&mut self.layout
}
/// Get a reference to the unstructured bytes of the image.
///
/// Note that this may return more bytes than required for the specific layout for various
/// reasons. See also [`as_layout_bytes`].
///
/// [`as_layout_bytes`]: #method.as_layout_bytes
pub(crate) fn as_capacity_bytes(&self) -> &[u8]
where
B: ops::Deref<Target = buf>,
{
self.buffer.as_bytes()
}
/// Get a reference to the aligned unstructured bytes of the image.
///
/// Note that this may return more bytes than required for the specific layout for various
/// reasons. See also [`as_layout_bytes`].
///
/// [`as_layout_bytes`]: #method.as_layout_bytes
pub(crate) fn as_capacity_buf(&self) -> &buf
where
B: ops::Deref<Target = buf>,
{
&self.buffer
}
/// Get a mutable reference to the unstructured bytes of the image.
///
/// Note that this may return more bytes than required for the specific layout for various
/// reasons. See also [`as_layout_bytes_mut`].
///
/// [`as_layout_bytes_mut`]: #method.as_layout_bytes_mut
pub(crate) fn as_capacity_bytes_mut(&mut self) -> &mut [u8]
where
B: ops::DerefMut<Target = buf>,
{
self.buffer.as_bytes_mut()
}
/// Get a mutable reference to the unstructured bytes of the image.
///
/// Note that this may return more bytes than required for the specific layout for various
/// reasons. See also [`as_layout_bytes_mut`].
///
/// [`as_layout_bytes_mut`]: #method.as_layout_bytes_mut
pub(crate) fn as_capacity_buf_mut(&mut self) -> &mut buf
where
B: ops::DerefMut<Target = buf>,
{
&mut self.buffer
}
/// Get a reference to the full allocated underlying atomic buffer.
pub(crate) fn as_capacity_atomic_buf(&self) -> &atomic_buf
where
B: ops::Deref<Target = atomic_buf>,
{
&self.buffer
}
/// Get a reference to the full allocated underlying cell buffer.
pub(crate) fn as_capacity_cell_buf(&self) -> &cell_buf
where
B: ops::Deref<Target = cell_buf>,
{
&self.buffer
}
/// Get a reference to those bytes used by the layout.
pub(crate) fn as_bytes(&self) -> &[u8]
where
B: ops::Deref<Target = buf>,
L: Layout,
{
&self.as_capacity_bytes()[..self.layout.byte_len()]
}
pub fn as_buf(&self) -> &buf
where
B: ops::Deref<Target = buf>,
L: Layout,
{
let byte_len = self.layout.byte_len();
self.buffer.truncate(byte_len)
}
pub fn as_mut_buf(&mut self) -> &mut buf
where
B: ops::DerefMut<Target = buf>,
L: Layout,
{
let byte_len = self.layout.byte_len();
self.buffer.truncate_mut(byte_len)
}
pub fn as_cell_buf(&self) -> &cell_buf
where
B: ops::Deref<Target = cell_buf>,
L: Layout,
{
let byte_len = self.layout.byte_len();
self.as_capacity_cell_buf().truncate(byte_len)
}
pub(crate) fn as_slice(&self) -> &[L::Sample]
where
B: ops::Deref<Target = buf>,
L: SliceLayout,
{
let texel = self.layout.sample();
texel.cast_buf(self.as_buf())
}
/// Get mutable reference to the buffer.
pub(crate) fn get(&self) -> &B {
&self.buffer
}
/// Get a mutable reference to the buffer. It is inadvisible to modify the buffer in a way that
/// it can no longer hold the layout.
pub(crate) fn get_mut(&mut self) -> &mut B {
&mut self.buffer
}
/// Borrow the buffer with the same layout.
pub(crate) fn as_deref(&self) -> RawImage<&'_ B::Target, &'_ L>
where
B: ops::Deref,
{
RawImage {
buffer: &self.buffer,
layout: &self.layout,
}
}
/// Borrow the buffer mutably with the same layout.
pub(crate) fn as_deref_mut(&mut self) -> RawImage<&'_ mut B::Target, &'_ mut L>
where
B: ops::DerefMut,
{
RawImage {
buffer: &mut self.buffer,
layout: &mut self.layout,
}
}
/// Check if the buffer is enough for another layout.
pub(crate) fn fits(&self, other: &impl Layout) -> bool
where
B: BufferLike,
{
<dyn Layout>::fits_buffer(other, &self.buffer)
}
/// Convert the inner layout.
///
/// This method drops the image if the new layout requires more bytes than the current buffer.
/// It's recommended you call this only on reference-type buffers.
pub(crate) fn checked_decay<Other>(self) -> Option<RawImage<B, Other>>
where
B: BufferLike,
Other: Decay<L>,
{
let layout = Other::decay(self.layout);
if <dyn Layout>::fits_buffer(&layout, &self.buffer) {
Some(RawImage {
buffer: self.buffer,
layout,
})
} else {
None
}
}
/// Change the layout without checking the buffer.
pub(crate) fn mogrify_layout<Other: Layout>(
self,
layout: impl FnOnce(L) -> Other,
) -> RawImage<B, Other> {
let layout = layout(self.layout);
RawImage {
buffer: self.buffer,
layout,
}
}
/// Reinterpret the bits in another layout.
///
/// This method fails if the layout requires more bytes than are currently allocated.
pub(crate) fn try_reinterpret<Other>(self, layout: Other) -> Result<RawImage<B, Other>, Self>
where
B: BufferLike,
Other: Layout,
{
if self.buffer.byte_len() < layout.byte_len() {
Err(self)
} else {
Ok(RawImage {
buffer: self.buffer,
layout,
})
}
}
}
/// Methods for all `Layouts` (the trait).
impl<B: BufferLike, L: Layout> RawImage<B, L> {
/// Get a mutable reference to those bytes used by the layout.
pub(crate) fn as_bytes_mut(&mut self) -> &mut [u8]
where
B: BufferMut,
{
let len = self.layout.byte_len();
&mut self.as_capacity_bytes_mut()[..len]
}
/// Reuse the buffer for a new image layout of the same type.
pub(crate) fn try_reuse(&mut self, layout: L) -> Result<(), BufferReuseError> {
if self.buffer.byte_len() >= layout.byte_len() {
self.layout = layout;
Ok(())
} else {
Err(BufferReuseError {
capacity: self.buffer.byte_len(),
requested: Some(layout.byte_len()),
})
}
}
/// Change the layout but require that the new layout fits the buffer, never reallocate.
pub(crate) fn mutate_inplace<T>(&mut self, f: impl FnOnce(&mut L) -> T) -> T
where
L: Layout,
{
let t = f(&mut self.layout);
assert!(
self.layout.byte_len() <= self.buffer.byte_len(),
"Modification required buffer allocation, was not in-place"
);
t
}
/// Take the buffer and layout from this image, moving content into a new instance.
///
/// Asserts that the moved-from container can hold the emptied layout.
pub(crate) fn take(&mut self) -> Self
where
L: Take,
{
let buffer = self.buffer.take();
let layout = self.mutate_inplace(Take::take);
RawImage::from_buffer(layout, buffer)
}
}
/// Methods for layouts that are slices of individual samples.
impl<B: BufferLike, L: SliceLayout> RawImage<B, L> {
/// Interpret an existing buffer as a pixel image.
///
/// The data already contained within the buffer is not modified so that prior initialization
/// can be performed or one array of samples reinterpreted for an image of other sample type.
/// However, the `TexelBuffer` will be logically resized which will zero-initialize missing elements if
/// the current buffer is too short.
///
/// # Panics
///
/// This function will panic if resizing causes a reallocation that fails.
pub(crate) fn from_texel_buffer(buffer: TexelBuffer<L::Sample>, layout: L) -> Self
where
B: From<Buffer>,
{
let buffer = buffer.into_inner();
assert!(buffer.len() >= layout.byte_len());
Self {
buffer: buffer.into(),
layout,
}
}
pub(crate) fn as_mut_slice(&mut self) -> &mut [L::Sample]
where
B: BufferMut,
{
self.layout.sample().cast_mut_buf(self.as_mut_buf())
}
/// Convert back into an vector-like of sample types.
pub(crate) fn into_buffer(self) -> TexelBuffer<L::Sample>
where
B: BufferRef,
{
let sample = self.layout.sample();
// Avoid calling any method of `Layout` after this. Not relevant for safety but might be in
// the future, if we want to avoid the extra check in `resize`.
let count = self.as_slice().len();
let buffer = self.buffer.into_owned();
let mut rec = TexelBuffer::from_buffer(buffer, sample);
// This should never reallocate at this point but we don't really know or care.
rec.resize(count);
rec
}
}
impl BufferLike for Buffer {
fn byte_len(&self) -> usize {
self.as_bytes().len()
}
fn into_owned(self) -> Self {
self
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl BufferLike for CellBuffer {
fn byte_len(&self) -> usize {
core::mem::size_of_val(&**self)
}
fn into_owned(self) -> Buffer {
self.to_owned()
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl BufferLike for AtomicBuffer {
fn byte_len(&self) -> usize {
core::mem::size_of_val(&**self)
}
fn into_owned(self) -> Buffer {
self.to_owned()
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl BufferLike for &'_ buf {
fn byte_len(&self) -> usize {
self.as_bytes().len()
}
fn into_owned(self) -> Buffer {
Buffer::from(self.as_bytes())
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl BufferLike for &'_ mut buf {
fn byte_len(&self) -> usize {
self.as_bytes().len()
}
fn into_owned(self) -> Buffer {
Buffer::from(self.as_bytes())
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl BufferLike for &'_ cell_buf {
fn byte_len(&self) -> usize {
self.as_texels(crate::texels::U8).as_slice_of_cells().len()
}
fn into_owned(self) -> Buffer {
let mut target = Buffer::new(self.byte_len());
let texels = self.as_texels(crate::texels::U8).as_slice_of_cells();
crate::texels::U8.load_cell_slice(texels, target.as_bytes_mut());
target
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl BufferLike for &'_ atomic_buf {
fn byte_len(&self) -> usize {
self.as_texels(crate::texels::U8).len()
}
fn into_owned(self) -> Buffer {
let source = self.as_texels(crate::texels::U8);
let mut target = Buffer::new(source.len());
source.write_to_slice(target.as_bytes_mut());
target
}
fn take(&mut self) -> Self {
core::mem::take(self)
}
}
impl dyn Layout + '_ {
fn fits_buffer(&self, buffer: &dyn BufferLike) -> bool {
self.byte_len() <= buffer.byte_len()
}
}
impl Growable for Buffer {
fn grow_to(&mut self, bytes: usize) {
Buffer::grow_to(self, bytes);
}
}
impl BufferRef for Buffer {}
impl BufferMut for Buffer {}
impl BufferRef for &'_ mut buf {}
impl BufferMut for &'_ mut buf {}