summaryrefslogtreecommitdiffstats
path: root/gfx/wr/examples/image_resize.rs
blob: 4456b00cafe031f65cfb740224ac54555f61cf80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate gleam;
extern crate glutin;
extern crate webrender;
extern crate winit;

#[path = "common/boilerplate.rs"]
mod boilerplate;
#[path = "common/image_helper.rs"]
mod image_helper;

use crate::boilerplate::{Example, HandyDandyRectBuilder};
use webrender::api::*;
use webrender::render_api::*;
use webrender::api::units::*;

struct App {
    image_key: ImageKey,
}

impl Example for App {
    fn render(
        &mut self,
        _api: &mut RenderApi,
        builder: &mut DisplayListBuilder,
        txn: &mut Transaction,
        _device_size: DeviceIntSize,
        pipeline_id: PipelineId,
        _document_id: DocumentId,
    ) {
        let (image_descriptor, image_data) = image_helper::make_checkerboard(32, 32);
        txn.add_image(
            self.image_key,
            image_descriptor,
            image_data,
            None,
        );

        let bounds = (0, 0).to(512, 512);
        let space_and_clip = SpaceAndClipInfo::root_scroll(pipeline_id);

        builder.push_simple_stacking_context(
            bounds.min,
            space_and_clip.spatial_id,
            PrimitiveFlags::IS_BACKFACE_VISIBLE,
        );

        let image_size = LayoutSize::new(100.0, 100.0);

        builder.push_image(
            &CommonItemProperties::new(
                LayoutRect::from_origin_and_size(LayoutPoint::new(100.0, 100.0), image_size),
                space_and_clip,
            ),
            bounds,
            ImageRendering::Auto,
            AlphaType::PremultipliedAlpha,
            self.image_key,
            ColorF::WHITE,
        );

        builder.push_image(
            &CommonItemProperties::new(
                LayoutRect::from_origin_and_size(LayoutPoint::new(250.0, 100.0), image_size),
                space_and_clip,
            ),
            bounds,
            ImageRendering::Pixelated,
            AlphaType::PremultipliedAlpha,
            self.image_key,
            ColorF::WHITE,
        );

        builder.pop_stacking_context();
    }

    fn on_event(
        &mut self,
        event: winit::event::WindowEvent,
        _window: &winit::window::Window,
        api: &mut RenderApi,
        document_id: DocumentId,
    ) -> bool {
        match event {
            winit::event::WindowEvent::KeyboardInput {
                input: winit::event::KeyboardInput {
                    state: winit::event::ElementState::Pressed,
                    virtual_keycode: Some(winit::event::VirtualKeyCode::Space),
                    ..
                },
                ..
            } => {
                let mut image_data = Vec::new();
                for y in 0 .. 64 {
                    for x in 0 .. 64 {
                        let r = 255 * ((y & 32) == 0) as u8;
                        let g = 255 * ((x & 32) == 0) as u8;
                        image_data.extend_from_slice(&[0, g, r, 0xff]);
                    }
                }

                let mut txn = Transaction::new();
                txn.update_image(
                    self.image_key,
                    ImageDescriptor::new(64, 64, ImageFormat::BGRA8, ImageDescriptorFlags::IS_OPAQUE),
                    ImageData::new(image_data),
                    &DirtyRect::All,
                );
                let mut txn = Transaction::new();
                txn.generate_frame(0, RenderReasons::empty());
                api.send_transaction(document_id, txn);
            }
            _ => {}
        }

        false
    }
}

fn main() {
    let mut app = App {
        image_key: ImageKey(IdNamespace(0), 0),
    };
    boilerplate::main_wrapper(&mut app, None);
}