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 {
render( &mut self, _api: &mut RenderApi, builder: &mut DisplayListBuilder, txn: &mut Transaction, _device_size: DeviceIntSize, pipeline_id: PipelineId, _document_id: DocumentId, )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 
on_event(&mut self, event: winit::WindowEvent, api: &mut RenderApi, document_id: DocumentId) -> bool80     fn on_event(&mut self, event: winit::WindowEvent, api: &mut RenderApi, document_id: DocumentId) -> bool {
81         match event {
82             winit::WindowEvent::KeyboardInput {
83                 input: winit::KeyboardInput {
84                     state: winit::ElementState::Pressed,
85                     virtual_keycode: Some(winit::VirtualKeyCode::Space),
86                     ..
87                 },
88                 ..
89             } => {
90                 let mut image_data = Vec::new();
91                 for y in 0 .. 64 {
92                     for x in 0 .. 64 {
93                         let r = 255 * ((y & 32) == 0) as u8;
94                         let g = 255 * ((x & 32) == 0) as u8;
95                         image_data.extend_from_slice(&[0, g, r, 0xff]);
96                     }
97                 }
98 
99                 let mut txn = Transaction::new();
100                 txn.update_image(
101                     self.image_key,
102                     ImageDescriptor::new(64, 64, ImageFormat::BGRA8, ImageDescriptorFlags::IS_OPAQUE),
103                     ImageData::new(image_data),
104                     &DirtyRect::All,
105                 );
106                 let mut txn = Transaction::new();
107                 txn.generate_frame(0);
108                 api.send_transaction(document_id, txn);
109             }
110             _ => {}
111         }
112 
113         false
114     }
115 }
116 
main()117 fn main() {
118     let mut app = App {
119         image_key: ImageKey(IdNamespace(0), 0),
120     };
121     boilerplate::main_wrapper(&mut app, None);
122 }
123