@@ -236,28 +236,49 @@ impl Surface {
236
236
}
237
237
238
238
/// Shows the given buffer with the given width and height on the window corresponding to this
239
- /// graphics context. Panics if buffer.len() ≠ width*height. If the size of the buffer does
240
- /// not match the size of the window, the buffer is drawn in the upper-left corner of the window.
239
+ /// surface.
240
+ ///
241
241
/// It is recommended in most production use cases to have the buffer fill the entire window.
242
- /// Use your windowing library to find the size of the window.
242
+ /// Use your windowing library to determine the size of the window.
243
+ ///
244
+ /// # Panics
245
+ ///
246
+ /// - If `buffer.len() ≠ width * height`
243
247
///
244
- /// The format of the buffer is as follows. There is one u32 in the buffer for each pixel in
245
- /// the area to draw. The first entry is the upper-left most pixel. The second is one to the right
246
- /// etc. (Row-major top to bottom left to right one u32 per pixel). Within each u32 the highest
247
- /// order 8 bits are to be set to 0. The next highest order 8 bits are the red channel, then the
248
- /// green channel, and then the blue channel in the lowest-order 8 bits. See the examples for
249
- /// one way to build this format using bitwise operations.
248
+ /// # Pixel format
250
249
///
251
- /// --------
250
+ /// The buffer layout is in a row-major layout. This means that index `0` is at the top-left corner of the
251
+ /// window and as the index increases, the position on the x axis grows to the right. When the number of
252
+ /// entries for the row is equal to the width of the surface, then the next row is started.
252
253
///
253
- /// Pixel format (u32):
254
+ /// Each pixel is represented in memory as a [`u32`]. Starting with the most significant byte (MSB)
255
+ /// going to the least significant byte (LSB), the byte is ignored but best set to `0`. Then there is
256
+ /// a byte for the red, green and blue channel.
254
257
///
255
- /// 00000000RRRRRRRRGGGGGGGGBBBBBBBB
258
+ /// ```text
259
+ /// XXXXXXXXRRRRRRRRGGGGGGGGBBBBBBBB
260
+ /// ^ ^
261
+ /// MSB LSB
262
+ /// ```
256
263
///
257
- /// 0: Bit is 0
258
- /// R: Red channel
259
- /// G: Green channel
260
- /// B: Blue channel
264
+ /// | Bit | Channel |
265
+ /// |------|---------------------|
266
+ /// | X | Ignored[^x-channel] |
267
+ /// | R | Red channel |
268
+ /// | G | Green channel |
269
+ /// | B | Blue channel |
270
+ ///
271
+ /// The function shown below may be used to create a color from a 24-bit RGB color using bitwise\
272
+ /// operations:
273
+ ///
274
+ /// ```
275
+ /// fn pixel(red: u8, green: u8, blue: u8) -> u32 {
276
+ /// let color = blue as u32
277
+ /// | ((green as u32) << 8)
278
+ /// | ((red as u32) << 16);
279
+ /// color
280
+ /// }
281
+ /// ```
261
282
///
262
283
/// # Platform dependent behavior
263
284
///
@@ -272,6 +293,8 @@ impl Surface {
272
293
///
273
294
/// If the caller wishes to synchronize other surface/window changes, such requests must be sent to the
274
295
/// Wayland compositor before calling this function.
296
+ ///
297
+ /// [^x-channel]: On some backends setting these bits to anything other than `0` may cause weird behavior.
275
298
#[ inline]
276
299
pub fn set_buffer ( & mut self , buffer : & [ u32 ] , width : u16 , height : u16 ) {
277
300
if ( width as usize ) * ( height as usize ) != buffer. len ( ) {
0 commit comments