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 
9 #[path = "common/boilerplate.rs"]
10 mod boilerplate;
11 #[path = "common/image_helper.rs"]
12 mod image_helper;
13 
14 use boilerplate::{Example, HandyDandyRectBuilder};
15 use webrender::api::*;
16 
17 struct App {
18     image_key: ImageKey,
19 }
20 
21 impl Example for App {
render( &mut self, _api: &RenderApi, builder: &mut DisplayListBuilder, resources: &mut ResourceUpdates, _framebuffer_size: DeviceUintSize, _pipeline_id: PipelineId, _document_id: DocumentId, )22     fn render(
23         &mut self,
24         _api: &RenderApi,
25         builder: &mut DisplayListBuilder,
26         resources: &mut ResourceUpdates,
27         _framebuffer_size: DeviceUintSize,
28         _pipeline_id: PipelineId,
29         _document_id: DocumentId,
30     ) {
31         let (image_descriptor, image_data) = image_helper::make_checkerboard(32, 32);
32         resources.add_image(
33             self.image_key,
34             image_descriptor,
35             image_data,
36             None,
37         );
38 
39         let bounds = (0, 0).to(512, 512);
40         let info = LayoutPrimitiveInfo::new(bounds);
41         builder.push_stacking_context(
42             &info,
43             ScrollPolicy::Scrollable,
44             None,
45             TransformStyle::Flat,
46             None,
47             MixBlendMode::Normal,
48             Vec::new(),
49         );
50 
51         let image_size = LayoutSize::new(100.0, 100.0);
52 
53         let info = LayoutPrimitiveInfo::with_clip_rect(
54             LayoutRect::new(LayoutPoint::new(100.0, 100.0), image_size),
55             bounds,
56         );
57         builder.push_image(
58             &info,
59             image_size,
60             LayoutSize::zero(),
61             ImageRendering::Auto,
62             AlphaType::PremultipliedAlpha,
63             self.image_key,
64         );
65 
66         let info = LayoutPrimitiveInfo::with_clip_rect(
67             LayoutRect::new(LayoutPoint::new(250.0, 100.0), image_size),
68             bounds,
69         );
70         builder.push_image(
71             &info,
72             image_size,
73             LayoutSize::zero(),
74             ImageRendering::Pixelated,
75             AlphaType::PremultipliedAlpha,
76             self.image_key,
77         );
78 
79         builder.pop_stacking_context();
80     }
81 
on_event(&mut self, event: glutin::WindowEvent, api: &RenderApi, document_id: DocumentId) -> bool82     fn on_event(&mut self, event: glutin::WindowEvent, api: &RenderApi, document_id: DocumentId) -> bool {
83         match event {
84             glutin::WindowEvent::KeyboardInput {
85                 input: glutin::KeyboardInput {
86                     state: glutin::ElementState::Pressed,
87                     virtual_keycode: Some(glutin::VirtualKeyCode::Space),
88                     ..
89                 },
90                 ..
91             } => {
92                 let mut image_data = Vec::new();
93                 for y in 0 .. 64 {
94                     for x in 0 .. 64 {
95                         let r = 255 * ((y & 32) == 0) as u8;
96                         let g = 255 * ((x & 32) == 0) as u8;
97                         image_data.extend_from_slice(&[0, g, r, 0xff]);
98                     }
99                 }
100 
101                 let mut updates = ResourceUpdates::new();
102                 updates.update_image(
103                     self.image_key,
104                     ImageDescriptor::new(64, 64, ImageFormat::BGRA8, true),
105                     ImageData::new(image_data),
106                     None,
107                 );
108                 let mut txn = Transaction::new();
109                 txn.update_resources(updates);
110                 txn.generate_frame();
111                 api.send_transaction(document_id, txn);
112             }
113             _ => {}
114         }
115 
116         false
117     }
118 }
119 
main()120 fn main() {
121     let mut app = App {
122         image_key: ImageKey(IdNamespace(0), 0),
123     };
124     boilerplate::main_wrapper(&mut app, None);
125 }
126