|
| 1 | +//! An example to test transparent rendering. |
| 2 | +//! |
| 3 | +//! Press `o`, `i`, `m` or `t` to change the alpha mode to `Opaque`, `Ignored`, `Premultiplied` and |
| 4 | +//! `Postmultiplied` respectively. |
| 5 | +//! |
| 6 | +//! This should render 6 rectangular areas. For details on the terminology, see: |
| 7 | +//! <https://html.spec.whatwg.org/multipage/canvas.html#premultiplied-alpha-and-the-2d-rendering-context> |
| 8 | +//! |
| 9 | +//! (255, 127, 0, 255): |
| 10 | +//! - Opaque/Ignored: Completely-opaque orange. |
| 11 | +//! - Postmultiplied: Completely-opaque orange. |
| 12 | +//! - Premultiplied: Completely-opaque orange. |
| 13 | +//! |
| 14 | +//! (255, 255, 0, 127): |
| 15 | +//! - Opaque/Ignored: Completely-opaque yellow. |
| 16 | +//! - Postmultiplied: Halfway-opaque yellow. |
| 17 | +//! - Premultiplied: Additive halfway-opaque yellow. |
| 18 | +//! |
| 19 | +//! (127, 127, 0, 127): |
| 20 | +//! - Opaque/Ignored: Completely-opaque dark yellow. |
| 21 | +//! - Postmultiplied: Halfway-opaque dark yellow. |
| 22 | +//! - Premultiplied: Halfway-opaque yellow. |
| 23 | +//! |
| 24 | +//! (255, 127, 0, 127): |
| 25 | +//! - Opaque/Ignored: Completely-opaque orange. |
| 26 | +//! - Postmultiplied: Halfway-opaque orange. |
| 27 | +//! - Premultiplied: Additive halfway-opaque orange. |
| 28 | +//! |
| 29 | +//! (255, 127, 0, 0): |
| 30 | +//! - Opaque/Ignored: Completely-opaque orange. |
| 31 | +//! - Postmultiplied: Fully-transparent orange. |
| 32 | +//! - Premultiplied: Additive fully-transparent orange. |
| 33 | +//! |
| 34 | +//! (0, 0, 0, 0): |
| 35 | +//! - Opaque/Ignored: Completely-opaque black. |
| 36 | +//! - Postmultiplied: Fully-transparent. |
| 37 | +//! - Premultiplied: Fully-transparent. |
| 38 | +use softbuffer::{AlphaMode, Context, Pixel, Surface}; |
| 39 | +use std::num::NonZeroU32; |
| 40 | +use winit::event::{ElementState, KeyEvent, WindowEvent}; |
| 41 | +use winit::event_loop::{ControlFlow, EventLoop}; |
| 42 | +use winit::keyboard::{Key, NamedKey}; |
| 43 | + |
| 44 | +mod util; |
| 45 | + |
| 46 | +fn main() { |
| 47 | + util::setup(); |
| 48 | + |
| 49 | + let event_loop = EventLoop::new().unwrap(); |
| 50 | + |
| 51 | + let context = Context::new(event_loop.owned_display_handle()).unwrap(); |
| 52 | + |
| 53 | + let app = util::WinitAppBuilder::with_init( |
| 54 | + |elwt| util::make_window(elwt, |w| w), |
| 55 | + move |_elwt, window| Surface::new(&context, window.clone()).unwrap(), |
| 56 | + ) |
| 57 | + .with_event_handler(|window, surface, window_id, event, elwt| { |
| 58 | + elwt.set_control_flow(ControlFlow::Wait); |
| 59 | + |
| 60 | + if window_id != window.id() { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + match event { |
| 65 | + WindowEvent::Resized(size) => { |
| 66 | + let Some(surface) = surface else { |
| 67 | + tracing::error!("Resized fired before Resumed or after Suspended"); |
| 68 | + return; |
| 69 | + }; |
| 70 | + |
| 71 | + if let (Some(width), Some(height)) = |
| 72 | + (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) |
| 73 | + { |
| 74 | + surface.resize(width, height).unwrap(); |
| 75 | + } |
| 76 | + } |
| 77 | + WindowEvent::RedrawRequested => { |
| 78 | + let Some(surface) = surface else { |
| 79 | + tracing::error!("RedrawRequested fired before Resumed or after Suspended"); |
| 80 | + return; |
| 81 | + }; |
| 82 | + |
| 83 | + tracing::info!(alpha_mode = ?surface.alpha_mode(), "redraw"); |
| 84 | + |
| 85 | + let alpha_mode = surface.alpha_mode(); |
| 86 | + let mut buffer = surface.buffer_mut().unwrap(); |
| 87 | + let width = buffer.width().get(); |
| 88 | + for (x, _, pixel) in buffer.pixels_iter() { |
| 89 | + let rectangle_number = (x * 6) / width; |
| 90 | + *pixel = match rectangle_number { |
| 91 | + 0 => Pixel::new_rgba(255, 127, 0, 255), |
| 92 | + 1 => Pixel::new_rgba(255, 255, 0, 127), |
| 93 | + 2 => Pixel::new_rgba(127, 127, 0, 127), |
| 94 | + 3 => Pixel::new_rgba(255, 127, 0, 127), |
| 95 | + 4 => Pixel::new_rgba(255, 127, 0, 0), |
| 96 | + _ => Pixel::new_rgba(0, 0, 0, 0), |
| 97 | + }; |
| 98 | + |
| 99 | + // Convert `AlphaMode::Opaque` -> `AlphaMode::Ignored`. |
| 100 | + if alpha_mode == AlphaMode::Opaque { |
| 101 | + pixel.a = 255; |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + buffer.present().unwrap(); |
| 106 | + } |
| 107 | + WindowEvent::CloseRequested |
| 108 | + | WindowEvent::KeyboardInput { |
| 109 | + event: |
| 110 | + KeyEvent { |
| 111 | + logical_key: Key::Named(NamedKey::Escape), |
| 112 | + repeat: false, |
| 113 | + .. |
| 114 | + }, |
| 115 | + .. |
| 116 | + } => { |
| 117 | + elwt.exit(); |
| 118 | + } |
| 119 | + WindowEvent::KeyboardInput { |
| 120 | + event: |
| 121 | + KeyEvent { |
| 122 | + logical_key, |
| 123 | + repeat: false, |
| 124 | + state: ElementState::Pressed, |
| 125 | + .. |
| 126 | + }, |
| 127 | + .. |
| 128 | + } => { |
| 129 | + let Some(surface) = surface else { |
| 130 | + tracing::error!("KeyboardInput fired before Resumed or after Suspended"); |
| 131 | + return; |
| 132 | + }; |
| 133 | + |
| 134 | + let alpha_mode = match logical_key.to_text() { |
| 135 | + Some("o") => AlphaMode::Opaque, |
| 136 | + Some("i") => AlphaMode::Ignored, |
| 137 | + Some("m") => AlphaMode::Premultiplied, |
| 138 | + Some("t") => AlphaMode::Postmultiplied, |
| 139 | + _ => return, |
| 140 | + }; |
| 141 | + |
| 142 | + if !surface.supports_alpha_mode(alpha_mode) { |
| 143 | + tracing::warn!(?alpha_mode, "not supported by the backend"); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + tracing::info!(?alpha_mode, "set alpha"); |
| 148 | + let size = window.inner_size(); |
| 149 | + let width = NonZeroU32::new(size.width).unwrap(); |
| 150 | + let height = NonZeroU32::new(size.height).unwrap(); |
| 151 | + surface.configure(width, height, alpha_mode).unwrap(); |
| 152 | + assert_eq!(surface.alpha_mode(), alpha_mode); |
| 153 | + |
| 154 | + window.set_transparent(matches!( |
| 155 | + alpha_mode, |
| 156 | + AlphaMode::Premultiplied | AlphaMode::Postmultiplied |
| 157 | + )); |
| 158 | + |
| 159 | + window.request_redraw(); |
| 160 | + } |
| 161 | + _ => {} |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + util::run_app(event_loop, app); |
| 166 | +} |
0 commit comments