Skip to content

Commit b83252f

Browse files
committed
doc: Add efficiency note on reading from the buffer's data
Serves as documentation for why we don't expose different `pixels` and `pixels_mut` methods.
1 parent 551a5d6 commit b83252f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,36 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> HasWindowHandle for Surface<D, W>
173173
///
174174
/// This trusts the display server not to mutate the buffer, which could otherwise be unsound.
175175
///
176+
/// # Reading buffer data
177+
///
178+
/// Reading from buffer data may perform very poorly, as the underlying storage of zero-copy
179+
/// buffers, where implemented, may set options optimized for CPU writes, that allows them to bypass
180+
/// certain caches and avoid cache pollution.
181+
///
182+
/// As such, when rendering, you should always set the pixel in its entirety:
183+
///
184+
/// ```
185+
/// # let pixel = &mut 0x00ffffff;
186+
/// # let (blue, green, red) = (0x11, 0x22, 0x33);
187+
/// *pixel = blue | (green << 8) | (red << 16);
188+
/// # assert_eq!(*pixel, 0x00332211);
189+
/// ```
190+
///
191+
/// Instead of e.g. something like:
192+
///
193+
/// ```
194+
/// # let pixel = &mut 0x00ffffff;
195+
/// # let (blue, green, red) = (0x11, 0x22, 0x33);
196+
/// // DISCOURAGED!
197+
/// *pixel &= 0x00000000; // Clear
198+
/// *pixel |= blue; // Set blue pixel
199+
/// *pixel |= green << 8; // Set green pixel
200+
/// *pixel |= red << 16; // Set red pixel
201+
/// # assert_eq!(*pixel, 0x00332211);
202+
/// ```
203+
///
204+
/// To discourage reading from the buffer, `&self -> &[u8]` methods are intentionally not provided.
205+
///
176206
/// # Data representation
177207
///
178208
/// The format of the buffer is as follows. There is one `u32` in the buffer for each pixel in

0 commit comments

Comments
 (0)