-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrounded-corners.cpp
178 lines (147 loc) · 4.42 KB
/
rounded-corners.cpp
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
#include <wayfire/view-transform.hpp>
#include <wayfire/opengl.hpp>
#include <wayfire/plugin.hpp>
#include <wayfire/signal-definitions.hpp>
#include <wayfire/util/log.hpp>
namespace
{
const std::string vertex_source = R"(
#version 100
attribute mediump vec2 position;
varying mediump vec2 fposition;
uniform mat4 matrix;
void main() {
gl_Position = matrix * vec4(position, 0.0, 1.0);
fposition = position;
})";
const std::string frag_source = R"(
#version 100
@builtin_ext@
varying mediump vec2 fposition;
@builtin@
// Top left corner
uniform mediump vec2 top_left;
// Top left corner with shadows included
uniform mediump vec2 full_top_left;
// Bottom right corner
uniform mediump vec2 bottom_right;
// Bottom right corner with shadows included
uniform mediump vec2 full_bottom_right;
// Rounding radius
const mediump float radius = 20.0;
void main()
{
mediump vec2 corner_dist = min(fposition - top_left, bottom_right - fposition);
if (max(corner_dist.x, corner_dist.y) < radius)
{
if (distance(corner_dist, vec2(radius, radius)) > radius)
{
discard;
}
}
highp vec2 uv = (fposition - full_top_left) / (full_bottom_right - full_top_left);
uv.y = 1.0 - uv.y;
gl_FragColor = get_pixel(uv);
})";
}
class rounded_corners_transformer_t : public wf::view_transformer_t
{
public:
uint32_t get_z_order() override
{
return wf::TRANSFORMER_2D - 1;
}
wf::region_t transform_opaque_region(
wf::geometry_t box, wf::region_t region) override
{
(void)box;
(void)region;
return {};
}
wf::pointf_t transform_point(
wf::geometry_t view, wf::pointf_t point) override
{
(void)view;
return point;
}
wf::pointf_t untransform_point(
wf::geometry_t view, wf::pointf_t point) override
{
(void)view;
return point;
}
wf::geometry_t get_bounding_box(wf::geometry_t bbox, wlr_box region) override
{
(void)bbox;
return wf::geometry_intersection(this->view->get_wm_geometry(), region);
}
std::vector<GLfloat> vertex_data;
void upload_data(wlr_box src_box)
{
auto geometry = view->get_wm_geometry();
float x = geometry.x, y = geometry.y,
w = geometry.width, h = geometry.height;
vertex_data = {
x, y + h,
x + w, y + h,
x + w, y,
x, y,
};
program.attrib_pointer("position", 2, 0, vertex_data.data(), GL_FLOAT);
program.uniform2f("top_left", x, y);
program.uniform2f("bottom_right", x + w, y + h);
program.uniform2f("full_top_left", src_box.x, src_box.y);
program.uniform2f("full_bottom_right",
src_box.x + src_box.width, src_box.y + src_box.height);
}
void render_with_damage(wf::texture_t src_tex, wlr_box src_box,
const wf::region_t& damage, const wf::framebuffer_t& target_fb) override
{
OpenGL::render_begin(target_fb);
program.use(src_tex.type);
program.set_active_texture(src_tex);
upload_data(src_box);
program.uniformMatrix4f("matrix", target_fb.get_orthographic_projection());
GL_CALL(glEnable(GL_BLEND));
GL_CALL(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
for (const auto& box : damage)
{
target_fb.logic_scissor(wlr_box_from_pixman_box(box));
GL_CALL(glDrawArrays(GL_TRIANGLE_FAN, 0, 4));
}
GL_CALL(glDisable(GL_BLEND));
program.deactivate();
OpenGL::render_end();
}
rounded_corners_transformer_t(wayfire_view view)
{
this->view = view;
OpenGL::render_begin();
program.compile(vertex_source, frag_source);
OpenGL::render_end();
}
virtual ~rounded_corners_transformer_t()
{}
private:
wayfire_view view;
OpenGL::program_t program;
};
class wayfire_rounded_corners_t : public wf::plugin_interface_t
{
public:
void init() override
{
output->connect_signal("view-mapped", &on_map);
}
wf::signal_connection_t on_map = [=] (wf::signal_data_t *data)
{
auto view = wf::get_signaled_view(data);
// Ignore panels, backgrounds, etc.
if (view->role == wf::VIEW_ROLE_TOPLEVEL)
{
view->add_transformer(
std::make_unique<rounded_corners_transformer_t>(view));
}
};
};
DECLARE_WAYFIRE_PLUGIN(wayfire_rounded_corners_t);