summaryrefslogtreecommitdiffstats
path: root/gfx/wr/wrench/src/test_invalidation.rs
blob: 4befcb9980aea00986d0427b720ec31221b2059b (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
129
/* 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/. */

use crate::NotifierEvent;
use crate::WindowWrapper;
use std::path::PathBuf;
use std::sync::mpsc::Receiver;
use crate::wrench::{Wrench, WrenchThing};
use crate::yaml_frame_reader::YamlFrameReader;
use webrender::{PictureCacheDebugInfo, TileDebugInfo};
use webrender::api::units::*;

pub struct TestHarness<'a> {
    wrench: &'a mut Wrench,
    window: &'a mut WindowWrapper,
    rx: Receiver<NotifierEvent>,
}

struct RenderResult {
    pc_debug: PictureCacheDebugInfo,
    composite_needed: bool,
}

// Convenience method to build a picture rect
fn pr(x: f32, y: f32, w: f32, h: f32) -> PictureRect {
    PictureRect::from_origin_and_size(
        PicturePoint::new(x, y),
        PictureSize::new(w, h),
    )
}

impl<'a> TestHarness<'a> {
    pub fn new(
        wrench: &'a mut Wrench,
        window: &'a mut WindowWrapper,
        rx: Receiver<NotifierEvent>
    ) -> Self {
        TestHarness {
            wrench,
            window,
            rx,
        }
    }

    /// Main entry point for invalidation tests
    pub fn run(
        mut self,
    ) {
        // List all invalidation tests here
        self.test_basic();
        self.test_composite_nop();
    }

    /// Simple validation / proof of concept of invalidation testing
    fn test_basic(
        &mut self,
    ) {
        // Render basic.yaml, ensure that the valid/dirty rects are as expected
        let results = self.render_yaml("basic");
        let tile_info = results.pc_debug.slice(0).tile(0, 0).as_dirty();
        assert_eq!(
            tile_info.local_valid_rect,
            pr(100.0, 100.0, 500.0, 100.0),
        );
        assert_eq!(
            tile_info.local_dirty_rect,
            pr(100.0, 100.0, 500.0, 100.0),
        );

        // Render it again and ensure the tile was considered valid (no rasterization was done)
        let results = self.render_yaml("basic");
        assert_eq!(*results.pc_debug.slice(0).tile(0, 0), TileDebugInfo::Valid);
    }

    /// Ensure WR detects composites are needed for position changes within a single tile.
    fn test_composite_nop(
        &mut self,
    ) {
        // Render composite_nop_1.yaml, ensure that the valid/dirty rects are as expected
        let results = self.render_yaml("composite_nop_1");
        let tile_info = results.pc_debug.slice(0).tile(0, 0).as_dirty();
        assert_eq!(
            tile_info.local_valid_rect,
            pr(100.0, 100.0, 100.0, 100.0),
        );
        assert_eq!(
            tile_info.local_dirty_rect,
            pr(100.0, 100.0, 100.0, 100.0),
        );

        // Render composite_nop_2.yaml, ensure that the valid/dirty rects are as expected
        let results = self.render_yaml("composite_nop_2");
        let tile_info = results.pc_debug.slice(0).tile(0, 0).as_dirty();
        assert_eq!(
            tile_info.local_valid_rect,
            pr(100.0, 120.0, 100.0, 100.0),
        );
        assert_eq!(
            tile_info.local_dirty_rect,
            pr(100.0, 120.0, 100.0, 100.0),
        );

        // Main part of this test - ensure WR detects a composite is required in this case
        assert!(results.composite_needed);
    }

    /// Render a YAML file, and return the picture cache debug info
    fn render_yaml(
        &mut self,
        filename: &str,
    ) -> RenderResult {
        let path = format!("invalidation/{}.yaml", filename);
        let mut reader = YamlFrameReader::new(&PathBuf::from(path));

        reader.do_frame(self.wrench);
        let composite_needed = match self.rx.recv().unwrap() {
            NotifierEvent::WakeUp { composite_needed } => composite_needed,
            NotifierEvent::ShutDown => unreachable!(),
        };
        let results = self.wrench.render();
        self.window.swap_buffers();

        RenderResult {
            pc_debug: results.picture_cache_debug,
            composite_needed,
        }
    }
}