-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathshowrepaint.cpp
More file actions
202 lines (177 loc) · 7.54 KB
/
Copy pathshowrepaint.cpp
File metadata and controls
202 lines (177 loc) · 7.54 KB
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Scott Moreau
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <wayfire/plugin.hpp>
#include <wayfire/output.hpp>
#include <wayfire/opengl.hpp>
#include <wayfire/render-manager.hpp>
#include <wayfire/per-output-plugin.hpp>
#include <wayfire/util/log.hpp>
extern "C"
{
#include <EGL/egl.h>
}
class wayfire_showrepaint : public wf::per_output_plugin_instance_t
{
wf::option_wrapper_t<wf::activatorbinding_t> toggle_binding{"showrepaint/toggle"};
wf::option_wrapper_t<bool> reduce_flicker{"showrepaint/reduce_flicker"};
bool active, egl_swap_buffers_with_damage;
wf::auxilliary_buffer_t last_buffer;
public:
void init() override
{
active = false;
egl_swap_buffers_with_damage =
egl_extension_supported("EGL_KHR_swap_buffers_with_damage") ||
egl_extension_supported("EGL_EXT_swap_buffers_with_damage");
output->add_activator(toggle_binding, &toggle_cb);
reduce_flicker.set_callback(option_changed);
}
wf::config::option_base_t::updated_callback_t option_changed = [=] ()
{
output->render->damage_whole();
};
void set_active_status(bool status)
{
if (this->active == status)
{
return;
}
if (status)
{
output->render->add_effect(&overlay_hook, wf::OUTPUT_EFFECT_OVERLAY);
output->render->add_effect(&on_main_pass_done, wf::OUTPUT_EFFECT_PASS_DONE);
} else
{
output->render->rem_effect(&overlay_hook);
output->render->rem_effect(&on_main_pass_done);
}
this->active = status;
}
wf::activator_callback toggle_cb = [=] (auto)
{
set_active_status(!active);
output->render->damage_whole();
return true;
};
bool egl_extension_supported(std::string ext)
{
if (!wf::get_core().is_gles2())
{
return false;
}
std::string extensions;
wf::gles::run_in_context([&]
{
EGLDisplay egl_display = eglGetCurrentDisplay();
extensions = std::string(eglQueryString(egl_display, EGL_EXTENSIONS));
});
size_t pos = extensions.find(ext);
if (pos == std::string::npos)
{
return false;
}
return true;
}
void get_random_color(wf::color_t& color)
{
color.r = 0.15 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / 0.25));
color.g = 0.15 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / 0.25));
color.b = 0.15 + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / 0.25));
color.a = 0.25;
}
wf::effect_hook_t overlay_hook = [=] ()
{
auto target_fb = output->render->get_target_framebuffer();
wf::region_t swap_damage = target_fb.geometry_region_from_framebuffer_region(
output->render->get_swap_damage());
wf::region_t scheduled_damage = output->render->get_scheduled_damage();
wf::region_t output_region{target_fb.geometry};
wf::region_t inverted_damage;
wf::region_t damage;
/* Show scheduled client damage. Scheduled damage is the client damage
* in union with last frame client damage. If this region is empty, we
* use swap damage, which is the same as scheduled damage unless something
* is rendering the entire frame buffer, in which case it is the whole
* output region. The reason for this is because we want to display both
* scheduled client damage region and the swap damage region, in contrast.
*/
wf::color_t color;
get_random_color(color);
damage = scheduled_damage.empty() ? swap_damage : scheduled_damage;
inverted_damage = output_region ^ damage;
auto rpass = output->render->get_current_pass();
rpass->add_rect(color, target_fb, target_fb.geometry, damage);
if (reduce_flicker)
{
/* Show swap damage. It might be possible that we blit right over this but in the case of cube
* and expo, it shows client and swap damage in contrast. This makes sense since the idea is to
* show damage as colored regions. We don't do this if the reduce_flicker option isn't set because
* we don't repaint the inverted damage from the last buffer in this case, so we would keep
* painting it with different colors until it is white. */
get_random_color(color);
rpass->add_rect(color, target_fb, target_fb.geometry, inverted_damage);
}
/* If swap_buffers_with_damage is supported, we do not need the following to be executed. */
if (egl_swap_buffers_with_damage || !reduce_flicker)
{
return;
}
/* Repaint the inverted damage region with the last buffer contents.
* We only want to see what actually changed on screen. If we don't do this, things like mouse and
* keyboard input cause buffer swaps which only make the screen flicker between buffers, without
* showing any actual damage changes. If swap_buffers_with_damage is supported, we do not need to do
* this since the damage region that is passed to swap is only repainted. If it isn't supported, the
* entire buffer is repainted.
*/
if (last_buffer.get_size().width > 0)
{
std::shared_ptr<wf::texture_t> texture = wf::texture_t::from_aux(last_buffer);
texture->set_transform(target_fb.wl_transform);
rpass->add_texture(texture, target_fb, target_fb.geometry, inverted_damage);
}
};
wf::effect_hook_t on_main_pass_done = [=] ()
{
if (!reduce_flicker || egl_swap_buffers_with_damage)
{
return;
}
/*
* Save the current buffer to last buffer so we can render the
* inverted damage from the last buffer to the current buffer
* on next frame. We have to save the entire buffer because we
* don't know what the next frame damage will be.
*/
auto target_fb = output->render->get_target_framebuffer();
last_buffer.allocate(target_fb.get_size());
wlr_box full = wf::construct_box({0, 0}, target_fb.get_size());
last_buffer.get_renderbuffer().blit(target_fb, wf::geometry_to_fbox(full), full);
};
void fini() override
{
output->rem_binding(&toggle_cb);
set_active_status(false);
}
};
DECLARE_WAYFIRE_PLUGIN(wf::per_output_plugin_t<wayfire_showrepaint>);