-
Notifications
You must be signed in to change notification settings - Fork 6
/
wakefield-surface.c
283 lines (238 loc) · 8.97 KB
/
wakefield-surface.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (C) 2015 Endless Mobile
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* Written by:
* Jasper St. Pierre <[email protected]>
*/
#define COMPOSITOR_VERSION 3
struct WakefieldRegion
{
struct wl_resource *resource;
cairo_region_t *region;
};
static void
wl_region_add (struct wl_client *client,
struct wl_resource *resource,
gint32 x,
gint32 y,
gint32 width,
gint32 height)
{
struct WakefieldRegion *region = wl_resource_get_user_data (resource);
cairo_rectangle_int_t rectangle = { x, y, width, height };
cairo_region_union_rectangle (region->region, &rectangle);
}
static void
wl_region_subtract (struct wl_client *client,
struct wl_resource *resource,
gint32 x,
gint32 y,
gint32 width,
gint32 height)
{
struct WakefieldRegion *region = wl_resource_get_user_data (resource);
cairo_rectangle_int_t rectangle = { x, y, width, height };
cairo_region_subtract_rectangle (region->region, &rectangle);
}
static const struct wl_region_interface region_interface = {
resource_release,
wl_region_add,
wl_region_subtract
};
static void
wl_region_destructor (struct wl_resource *resource)
{
struct WakefieldRegion *region = wl_resource_get_user_data (resource);
cairo_region_destroy (region->region);
g_slice_free (struct WakefieldRegion, region);
}
static void
wl_compositor_create_region (struct wl_client *client,
struct wl_resource *compositor_resource,
uint32_t id)
{
struct WakefieldRegion *region = g_slice_new0 (struct WakefieldRegion);
region->resource = wl_resource_create (client, &wl_region_interface, wl_resource_get_version (compositor_resource), id);
wl_resource_set_implementation (region->resource, ®ion_interface, region, wl_region_destructor);
region->region = cairo_region_create ();
}
static void
wl_surface_attach (struct wl_client *client,
struct wl_resource *surface_resource,
struct wl_resource *buffer_resource,
gint32 dx, gint32 dy)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (surface_resource);
/* Ignore dx/dy in our case */
surface->pending.buffer = buffer_resource;
}
static void
wl_surface_damage (struct wl_client *client,
struct wl_resource *surface_resource,
int32_t x, int32_t y, int32_t width, int32_t height)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (surface_resource);
cairo_rectangle_int_t rectangle = { x, y, width, height };
cairo_region_union_rectangle (surface->damage, &rectangle);
}
#define WL_CALLBACK_VERSION 1
static void
wl_surface_frame (struct wl_client *client,
struct wl_resource *surface_resource,
uint32_t callback_id)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (surface_resource);
struct wl_resource *callback = wl_resource_create (client, &wl_callback_interface,
WL_CALLBACK_VERSION, callback_id);
wl_resource_set_destructor (callback, unbind_resource);
wl_list_insert (&surface->pending.frame_callbacks, wl_resource_get_link (callback));
}
static void
wl_surface_set_opaque_region (struct wl_client *client,
struct wl_resource *surface_resource,
struct wl_resource *region_resource)
{
/* XXX: Do we need this? */
}
static void
wl_surface_set_input_region (struct wl_client *client,
struct wl_resource *surface_resource,
struct wl_resource *region_resource)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (surface_resource);
g_clear_pointer (&surface->pending.input_region, cairo_region_destroy);
if (region_resource)
{
struct WakefieldRegion *region = wl_resource_get_user_data (region_resource);
surface->pending.input_region = cairo_region_copy (region->region);
}
}
static void
wl_surface_commit (struct wl_client *client,
struct wl_resource *resource)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (resource);
if (surface->pending.buffer)
surface->current.buffer = surface->pending.buffer;
/* XXX: Should we reallocate / redraw the entire region if the buffer
* scale changes? */
if (surface->pending.scale > 0)
surface->current.scale = surface->pending.scale;
wl_list_insert_list (&surface->current.frame_callbacks,
&surface->pending.frame_callbacks);
wl_list_init (&surface->pending.frame_callbacks);
/* process damage */
gtk_widget_queue_draw_region (GTK_WIDGET (surface->compositor), surface->damage);
/* ... and then empty it */
{
cairo_rectangle_int_t nothing = { 0, 0, 0, 0 };
cairo_region_intersect_rectangle (surface->damage, ¬hing);
}
/* XXX: Stop leak when we start using the input region. */
surface->pending.input_region = NULL;
surface->pending.buffer = NULL;
surface->pending.scale = 0;
}
static void
wl_surface_set_buffer_transform (struct wl_client *client,
struct wl_resource *resource,
int32_t transform)
{
/* TODO */
}
static void
wl_surface_set_buffer_scale (struct wl_client *client,
struct wl_resource *resource,
int32_t scale)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (resource);
surface->pending.scale = scale;
}
static void
destroy_pending_state (struct WakefieldSurfacePendingState *state)
{
g_clear_pointer (&state->input_region, cairo_region_destroy);
}
static void
wl_surface_destructor (struct wl_resource *resource)
{
struct WakefieldSurface *surface = wl_resource_get_user_data (resource);
destroy_pending_state (&surface->pending);
destroy_pending_state (&surface->current);
/* XXX */
{
WakefieldCompositor *compositor = surface->compositor;
WakefieldCompositorPrivate *priv = wakefield_compositor_get_instance_private (compositor);
priv->surface = NULL;
}
}
static const struct wl_surface_interface surface_interface = {
resource_release,
wl_surface_attach,
wl_surface_damage,
wl_surface_frame,
wl_surface_set_opaque_region,
wl_surface_set_input_region,
wl_surface_commit,
wl_surface_set_buffer_transform,
wl_surface_set_buffer_scale
};
static void
wl_compositor_create_surface (struct wl_client *client,
struct wl_resource *compositor_resource,
uint32_t id)
{
WakefieldCompositor *compositor = wl_resource_get_user_data (compositor_resource);
WakefieldCompositorPrivate *priv = wakefield_compositor_get_instance_private (compositor);
struct WakefieldSurface *surface;
/* XXX: For now, treat the first surface created as the proper
* preview surface and leak all the rest until we get a
* special Wakefield extension... */
if (priv->surface)
return;
surface = g_slice_new0 (struct WakefieldSurface);
surface->compositor = compositor;
surface->damage = cairo_region_create ();
surface->resource = wl_resource_create (client, &wl_surface_interface, wl_resource_get_version (compositor_resource), id);
wl_resource_set_implementation (surface->resource, &surface_interface, surface, wl_surface_destructor);
wl_list_init (&surface->pending.frame_callbacks);
wl_list_init (&surface->current.frame_callbacks);
surface->current.scale = 1;
priv->surface = surface;
}
const static struct wl_compositor_interface compositor_interface = {
wl_compositor_create_surface,
wl_compositor_create_region
};
static void
bind_compositor (struct wl_client *client,
void *data,
uint32_t version,
uint32_t id)
{
WakefieldCompositor *compositor = data;
struct wl_resource *cr;
cr = wl_resource_create (client, &wl_compositor_interface, version, id);
wl_resource_set_implementation (cr, &compositor_interface, compositor, NULL);
}
static void
wakefield_surface_init (WakefieldCompositor *compositor)
{
WakefieldCompositorPrivate *priv = wakefield_compositor_get_instance_private (compositor);
wl_global_create (priv->wl_display, &wl_compositor_interface, COMPOSITOR_VERSION, compositor, bind_compositor);
}