File: test_occlusion.cpp

package info (click to toggle)
mir 2.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,080 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (205 lines) | stat: -rw-r--r-- 8,073 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <mir/geometry/rectangle.h>
#include "src/server/compositor/occlusion.h"
#include <mir/test/doubles/fake_renderable.h>
#include <mir/test/doubles/stub_scene_element.h>

#include <gtest/gtest.h>
#include <memory>

using namespace testing;
using namespace mir::geometry;
using namespace mir::compositor;
namespace mg = mir::graphics;
namespace mtd = mir::test::doubles;

namespace
{
struct OcclusionFilterTest : public Test
{
    OcclusionFilterTest()
    {
        monitor_rect.top_left = {0, 0};
        monitor_rect.size = {1920, 1200};
    }

    SceneElementSequence scene_elements_from(
        std::vector<std::shared_ptr<mg::Renderable>> renderables)
    {
        SceneElementSequence elements;
        for (auto const& renderable : renderables)
            elements.push_back(std::make_shared<mtd::StubSceneElement>(renderable));

        return elements;
    }

    mg::RenderableList renderables_from(SceneElementSequence const& elements)
    {
        mg::RenderableList renderables;
        for (auto const& element : elements)
            renderables.push_back(element->renderable());

        return renderables;
    }

    Rectangle monitor_rect;
};

}

TEST_F(OcclusionFilterTest, single_window_not_occluded)
{
    auto window = std::make_shared<mtd::FakeRenderable>(12, 34, 56, 78);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({window}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), IsEmpty());
    EXPECT_THAT(renderables_from(elements), ElementsAre(window));
}

TEST_F(OcclusionFilterTest, partially_offscreen_still_visible)
{ // Regression test for LP: #1301115
    auto left =   std::make_shared<mtd::FakeRenderable>(-10,   10, 100, 100);
    auto right =  std::make_shared<mtd::FakeRenderable>(1900,  10, 100, 100);
    auto top =    std::make_shared<mtd::FakeRenderable>(500,   -1, 100, 100);
    auto bottom = std::make_shared<mtd::FakeRenderable>(200, 1000, 100, 1000);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({left, right, top, bottom}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), IsEmpty());
    EXPECT_THAT(renderables_from(elements), ElementsAre(left, right, top, bottom));
}

TEST_F(OcclusionFilterTest, smaller_window_occluded)
{
    auto top = std::make_shared<mtd::FakeRenderable>(10, 10, 10, 10);
    auto bottom = std::make_shared<mtd::FakeRenderable>(12, 12, 5, 5);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({bottom, top}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), ElementsAre(bottom));
    EXPECT_THAT(renderables_from(elements), ElementsAre(top));
}

TEST_F(OcclusionFilterTest, translucent_window_occludes_nothing)
{
    auto top = std::make_shared<mtd::FakeRenderable>(Rectangle{{10, 10}, {10, 10}}, 0.5f);
    auto bottom = std::make_shared<mtd::FakeRenderable>(Rectangle{{12, 12}, {5, 5}}, 1.0f);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({bottom, top}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), IsEmpty());
    EXPECT_THAT(renderables_from(elements), ElementsAre(bottom, top));
}

TEST_F(OcclusionFilterTest, shaped_window_without_opaque_region_occludes_nothing)
{
    auto top = std::make_shared<mtd::FakeRenderable>(Rectangle{{10, 10}, {10, 10}}, 1.0f, false, std::nullopt);
    auto bottom = std::make_shared<mtd::FakeRenderable>(12, 12, 5, 5);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({bottom, top}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), IsEmpty());
    EXPECT_THAT(renderables_from(elements), ElementsAre(bottom, top));
}

TEST_F(OcclusionFilterTest, shaped_window_with_opaque_region_occludes_something)
{
    // Simulate a window with transparent edges 1px wide and an opaque center
    auto top = std::make_shared<mtd::FakeRenderable>(
        Rectangle{{10, 10}, {10, 10}}, 1.0f, false, Rectangles{Rectangle{{11, 11}, {9, 9}}});
    auto bottom = std::make_shared<mtd::FakeRenderable>(12, 12, 5, 5);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({bottom, top}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), ElementsAre(bottom));
    EXPECT_THAT(renderables_from(elements), ElementsAre(top));
}

TEST_F(OcclusionFilterTest, identical_window_occluded)
{
    auto top = std::make_shared<mtd::FakeRenderable>(10, 10, 10, 10);
    auto bottom = std::make_shared<mtd::FakeRenderable>(10, 10, 10, 10);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({bottom, top}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), ElementsAre(bottom));
    EXPECT_THAT(renderables_from(elements), ElementsAre(top));
}

TEST_F(OcclusionFilterTest, larger_window_never_occluded)
{
    auto top = std::make_shared<mtd::FakeRenderable>(10, 10, 10, 10);
    auto bottom = std::make_shared<mtd::FakeRenderable>(9, 9, 12, 12);

    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from({bottom, top}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), IsEmpty());
    EXPECT_THAT(renderables_from(elements), ElementsAre(bottom, top));
}

TEST_F(OcclusionFilterTest, cascaded_windows_never_occluded)
{
    std::vector<std::shared_ptr<mg::Renderable>> renderables;
    unsigned int const num_windows{10u};
    for (auto x = 0u; x < num_windows; x++)
        renderables.push_back(std::make_shared<mtd::FakeRenderable>(x, x, 200, 100));


    auto const& [occlusions, elements] = split_occluded_and_visible(scene_elements_from(renderables), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), IsEmpty());
    EXPECT_THAT(renderables_from(elements), ElementsAreArray(renderables));
}

TEST_F(OcclusionFilterTest, some_occluded_and_some_not)
{
    auto window0 = std::make_shared<mtd::FakeRenderable>(10, 20, 400, 300);
    auto window1 = std::make_shared<mtd::FakeRenderable>(10, 20, 5, 5);
    auto window2 = std::make_shared<mtd::FakeRenderable>(100, 100, 20, 20);
    auto window3 = std::make_shared<mtd::FakeRenderable>(200, 200, 50, 50);
    auto window4 = std::make_shared<mtd::FakeRenderable>(500, 600, 34, 56);
    auto window5 = std::make_shared<mtd::FakeRenderable>(200, 200, 1000, 1000);

    auto const& [occlusions, elements] = split_occluded_and_visible(
        scene_elements_from({
            window5, // not occluded
            window4, // not occluded
            window3, // occluded
            window2, // occluded
            window1, // occluded
            window0  // not occluded
        }),
        monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), ElementsAre(window3, window2, window1));
    EXPECT_THAT(renderables_from(elements), ElementsAre(window5, window4, window0));
}

TEST_F(OcclusionFilterTest,
       occludes_partially_onscreen_window_when_onscreen_part_is_covered_by_another_window)
{
    auto const partially_onscreen = std::make_shared<mtd::FakeRenderable>(-50, 100, 150, 100);
    auto const covering = std::make_shared<mtd::FakeRenderable>(0, 100, 100, 100);

    auto const& [occlusions, elements] =
        split_occluded_and_visible(scene_elements_from({partially_onscreen, covering}), monitor_rect);

    EXPECT_THAT(renderables_from(occlusions), ElementsAre(partially_onscreen));
    EXPECT_THAT(renderables_from(elements), ElementsAre(covering));
}