image_resize.rs (3840B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 extern crate gleam; 6 extern crate glutin; 7 extern crate webrender; 8 extern crate winit; 9 10 #[path = "common/boilerplate.rs"] 11 mod boilerplate; 12 #[path = "common/image_helper.rs"] 13 mod image_helper; 14 15 use crate::boilerplate::{Example, HandyDandyRectBuilder}; 16 use webrender::api::*; 17 use webrender::render_api::*; 18 use webrender::api::units::*; 19 20 struct App { 21 image_key: ImageKey, 22 } 23 24 impl Example for App { 25 fn render( 26 &mut self, 27 _api: &mut RenderApi, 28 builder: &mut DisplayListBuilder, 29 txn: &mut Transaction, 30 _device_size: DeviceIntSize, 31 pipeline_id: PipelineId, 32 _document_id: DocumentId, 33 ) { 34 let (image_descriptor, image_data) = image_helper::make_checkerboard(32, 32); 35 txn.add_image( 36 self.image_key, 37 image_descriptor, 38 image_data, 39 None, 40 ); 41 42 let bounds = (0, 0).to(512, 512); 43 let space_and_clip = SpaceAndClipInfo::root_scroll(pipeline_id); 44 45 builder.push_simple_stacking_context( 46 bounds.min, 47 space_and_clip.spatial_id, 48 PrimitiveFlags::IS_BACKFACE_VISIBLE, 49 ); 50 51 let image_size = LayoutSize::new(100.0, 100.0); 52 53 builder.push_image( 54 &CommonItemProperties::new( 55 LayoutRect::from_origin_and_size(LayoutPoint::new(100.0, 100.0), image_size), 56 space_and_clip, 57 ), 58 bounds, 59 ImageRendering::Auto, 60 AlphaType::PremultipliedAlpha, 61 self.image_key, 62 ColorF::WHITE, 63 ); 64 65 builder.push_image( 66 &CommonItemProperties::new( 67 LayoutRect::from_origin_and_size(LayoutPoint::new(250.0, 100.0), image_size), 68 space_and_clip, 69 ), 70 bounds, 71 ImageRendering::Pixelated, 72 AlphaType::PremultipliedAlpha, 73 self.image_key, 74 ColorF::WHITE, 75 ); 76 77 builder.pop_stacking_context(); 78 } 79 80 fn on_event( 81 &mut self, 82 event: winit::event::WindowEvent, 83 _window: &winit::window::Window, 84 api: &mut RenderApi, 85 document_id: DocumentId, 86 ) -> bool { 87 match event { 88 winit::event::WindowEvent::KeyboardInput { 89 input: winit::event::KeyboardInput { 90 state: winit::event::ElementState::Pressed, 91 virtual_keycode: Some(winit::event::VirtualKeyCode::Space), 92 .. 93 }, 94 .. 95 } => { 96 let mut image_data = Vec::new(); 97 for y in 0 .. 64 { 98 for x in 0 .. 64 { 99 let r = 255 * ((y & 32) == 0) as u8; 100 let g = 255 * ((x & 32) == 0) as u8; 101 image_data.extend_from_slice(&[0, g, r, 0xff]); 102 } 103 } 104 105 let mut txn = Transaction::new(); 106 txn.update_image( 107 self.image_key, 108 ImageDescriptor::new(64, 64, ImageFormat::BGRA8, ImageDescriptorFlags::IS_OPAQUE), 109 ImageData::new(image_data), 110 &DirtyRect::All, 111 ); 112 let mut txn = Transaction::new(); 113 txn.generate_frame(0, true, false, RenderReasons::empty()); 114 api.send_transaction(document_id, txn); 115 } 116 _ => {} 117 } 118 119 false 120 } 121 } 122 123 fn main() { 124 let mut app = App { 125 image_key: ImageKey(IdNamespace(0), 0), 126 }; 127 boilerplate::main_wrapper(&mut app, None); 128 }