commit 682473b

Michael Forney  ·  2013-12-23 22:59:13 +0000 UTC
parent 7cde7c2
Move surface state into compositor view

Now, each composited surface has its own view.
5 files changed,  +95, -119
+89, -105
  1@@ -19,8 +19,9 @@
  2 #include <wld/drm.h>
  3 #include <xkbcommon/xkbcommon-keysyms.h>
  4 
  5-struct surface_state
  6+struct view
  7 {
  8+    struct swc_view base;
  9     struct swc_compositor * compositor;
 10 
 11     /* The box that the surface covers (including it's border). */
 12@@ -39,7 +40,7 @@ struct surface_state
 13 
 14     bool mapped;
 15 
 16-    struct wl_listener event_listener;
 17+    struct wl_listener surface_event_listener;
 18 };
 19 
 20 /* Rendering {{{ */
 21@@ -115,7 +116,7 @@ static void repaint_surface(struct render_target * target,
 22     pixman_region32_t border_damage;
 23     pixman_region32_t surface_region;
 24     struct buffer_state * state;
 25-    struct surface_state * surface_state = surface->view_state;
 26+    struct view * view = (void *) surface->view;
 27 
 28     if (!surface->state.buffer)
 29         return;
 30@@ -123,15 +124,14 @@ static void repaint_surface(struct render_target * target,
 31     state = buffer_state(surface->state.buffer);
 32     assert(state);
 33 
 34-    pixman_region32_init_with_extents(&surface_damage, &surface_state->extents);
 35+    pixman_region32_init_with_extents(&surface_damage, &view->extents);
 36     pixman_region32_init(&border_damage);
 37     pixman_region32_init_rect
 38         (&surface_region, surface->geometry.x, surface->geometry.y,
 39          surface->geometry.width, surface->geometry.height);
 40 
 41     pixman_region32_intersect(&surface_damage, &surface_damage, damage);
 42-    pixman_region32_subtract(&surface_damage, &surface_damage,
 43-                             &surface_state->clip);
 44+    pixman_region32_subtract(&surface_damage, &surface_damage, &view->clip);
 45     pixman_region32_subtract(&border_damage, &surface_damage, &surface_region);
 46     pixman_region32_intersect(&surface_damage, &surface_damage,
 47                               &surface_region);
 48@@ -161,8 +161,7 @@ static void repaint_surface(struct render_target * target,
 49 
 50         pixman_region32_translate(&border_damage,
 51                                   -target->geometry.x, -target->geometry.y);
 52-        wld_fill_region(target->drawable, surface_state->border.color,
 53-                        &border_damage);
 54+        wld_fill_region(target->drawable, view->border.color, &border_damage);
 55     }
 56 
 57     pixman_region32_fini(&border_damage);
 58@@ -274,13 +273,12 @@ static void renderer_flush_surface(struct swc_surface * surface)
 59  */
 60 static void damage_below_surface(struct swc_surface * surface)
 61 {
 62-    struct swc_compositor * compositor = CONTAINER_OF
 63-        (surface->view, typeof(*compositor), compositor_view);
 64-    struct surface_state * state = surface->view_state;
 65+    struct view * view = (void *) surface->view;
 66+    struct swc_compositor * compositor = view->compositor;
 67     pixman_region32_t damage_below;
 68 
 69-    pixman_region32_init_with_extents(&damage_below, &state->extents);
 70-    pixman_region32_subtract(&damage_below, &damage_below, &state->clip);
 71+    pixman_region32_init_with_extents(&damage_below, &view->extents);
 72+    pixman_region32_subtract(&damage_below, &damage_below, &view->clip);
 73     pixman_region32_union(&compositor->damage, &compositor->damage,
 74                           &damage_below);
 75     pixman_region32_fini(&damage_below);
 76@@ -291,43 +289,42 @@ static void damage_below_surface(struct swc_surface * surface)
 77  */
 78 static void damage_surface(struct swc_surface * surface)
 79 {
 80-    struct surface_state * state = surface->view_state;
 81+    struct view * view = (void *) surface->view;
 82     printf("damaging surface\n");
 83 
 84     pixman_region32_fini(&surface->state.damage);
 85     pixman_region32_init_rect(&surface->state.damage, 0, 0,
 86                               surface->geometry.width,
 87                               surface->geometry.height);
 88-    state->border.damaged = true;
 89+    view->border.damaged = true;
 90 }
 91 
 92 static void update_extents(struct swc_surface * surface)
 93 {
 94-    struct surface_state * state = surface->view_state;
 95+    struct view * view = (void *) surface->view;
 96 
 97-    state->extents.x1 = surface->geometry.x - state->border.width;
 98-    state->extents.y1 = surface->geometry.y - state->border.width;
 99-    state->extents.x2 = surface->geometry.x + surface->geometry.width
100-        + state->border.width;
101-    state->extents.y2 = surface->geometry.y + surface->geometry.height
102-        + state->border.width;
103+    view->extents.x1 = surface->geometry.x - view->border.width;
104+    view->extents.y1 = surface->geometry.y - view->border.width;
105+    view->extents.x2 = surface->geometry.x + surface->geometry.width
106+        + view->border.width;
107+    view->extents.y2 = surface->geometry.y + surface->geometry.height
108+        + view->border.width;
109 
110     /* Damage border. */
111-    state->border.damaged = true;
112+    view->border.damaged = true;
113 }
114 
115 static void update_outputs(struct swc_surface * surface)
116 {
117-    struct swc_compositor * compositor = CONTAINER_OF
118-        (surface->view, typeof(*compositor), compositor_view);
119-    struct surface_state * state = surface->view_state;
120+    struct view * view = (void *) surface->view;
121+    struct swc_compositor * compositor = view->compositor;
122     uint32_t old_outputs = surface->outputs, new_outputs = 0,
123              entered_outputs, left_outputs, changed_outputs;
124     struct swc_output * output;
125     struct wl_client * client;
126     struct wl_resource * resource;
127 
128-    if (state->mapped)
129+    if (view->mapped)
130     {
131         wl_list_for_each(output, &compositor->outputs, link)
132         {
133@@ -367,8 +364,8 @@ static void update(struct swc_surface * surface);
134 
135 static void handle_surface_event(struct wl_listener * listener, void * data)
136 {
137-    struct surface_state * state
138-        = CONTAINER_OF(listener, typeof(*state), event_listener);
139+    struct view * view
140+        = CONTAINER_OF(listener, typeof(*view), surface_event_listener);
141     struct swc_event * event = data;
142     struct swc_surface_event_data * event_data = event->data;
143     struct swc_surface * surface = event_data->surface;
144@@ -386,49 +383,16 @@ static void handle_surface_event(struct wl_listener * listener, void * data)
145     }
146 }
147 
148-static bool add(struct swc_surface * surface)
149-{
150-    struct swc_compositor * compositor = CONTAINER_OF
151-        (surface->view, typeof(*compositor), compositor_view);
152-    struct surface_state * state;
153-
154-    state = malloc(sizeof *state);
155-
156-    if (!state)
157-        return false;
158-
159-    state->compositor = compositor;
160-    state->extents.x1 = surface->geometry.x;
161-    state->extents.y1 = surface->geometry.y;
162-    state->extents.x2 = surface->geometry.x + surface->geometry.width;
163-    state->extents.y2 = surface->geometry.y + surface->geometry.height;
164-    state->border.width = 0;
165-    state->border.color = 0x000000;
166-    state->border.damaged = false;
167-    state->mapped = false;
168-    state->event_listener.notify = &handle_surface_event;
169-
170-    wl_signal_add(&surface->event_signal, &state->event_listener);
171-
172-    pixman_region32_init(&state->clip);
173-
174-    surface->view_state = state;
175-
176-    return true;
177-}
178-
179 static void remove_(struct swc_surface * surface)
180 {
181-    struct swc_compositor * compositor = CONTAINER_OF
182-        (surface->view, typeof(*compositor), compositor_view);
183-    struct surface_state * state = surface->view_state;
184+    struct view * view = (void *) surface->view;
185 
186     swc_compositor_surface_hide(surface);
187 
188-    wl_list_remove(&state->event_listener.link);
189-    pixman_region32_fini(&state->clip);
190+    wl_list_remove(&view->surface_event_listener.link);
191+    pixman_region32_fini(&view->clip);
192 
193-    free(state);
194+    free(view);
195 }
196 
197 static void attach(struct swc_surface * surface, struct wl_resource * resource)
198@@ -438,12 +402,11 @@ static void attach(struct swc_surface * surface, struct wl_resource * resource)
199 
200 static void update(struct swc_surface * surface)
201 {
202-    struct swc_compositor * compositor = CONTAINER_OF
203-        (surface->view, typeof(*compositor), compositor_view);
204-    struct surface_state * state = surface->view_state;
205+    struct view * view = (void *) surface->view;
206+    struct swc_compositor * compositor = view->compositor;
207     struct swc_output * output;
208 
209-    if (!state->mapped)
210+    if (!view->mapped)
211         return;
212 
213     wl_list_for_each(output, &compositor->outputs, link)
214@@ -455,14 +418,12 @@ static void update(struct swc_surface * surface)
215 
216 static void move(struct swc_surface * surface, int32_t x, int32_t y)
217 {
218-    struct swc_compositor * compositor = CONTAINER_OF
219-        (surface->view, typeof(*compositor), compositor_view);
220-    struct surface_state * state = surface->view_state;
221+    struct view * view = (void *) surface->view;
222 
223     if (x == surface->geometry.x && y == surface->geometry.y)
224         return;
225 
226-    if (state->mapped)
227+    if (view->mapped)
228         damage_below_surface(surface);
229 
230     surface->geometry.x = x;
231@@ -470,11 +431,11 @@ static void move(struct swc_surface * surface, int32_t x, int32_t y)
232 
233     update_extents(surface);
234 
235-    if (state->mapped)
236+    if (view->mapped)
237     {
238         /* Assume worst-case no clipping until we draw the next frame (in case
239          * the surface gets moved again before that). */
240-        pixman_region32_init(&state->clip);
241+        pixman_region32_init(&view->clip);
242 
243         damage_below_surface(surface);
244         update(surface);
245@@ -484,32 +445,58 @@ static void move(struct swc_surface * surface, int32_t x, int32_t y)
246 }
247 
248 const struct swc_view_impl view_impl = {
249-    .add = &add,
250     .remove = &remove_,
251     .attach = &attach,
252     .update = &update,
253     .move = &move
254 };
255 
256+bool swc_compositor_add_surface(struct swc_compositor * compositor,
257+                                struct swc_surface * surface)
258+{
259+    struct view * view;
260+
261+    view = malloc(sizeof *view);
262+
263+    if (!view)
264+        return false;
265+
266+    swc_view_initialize(&view->base, &view_impl);
267+    view->compositor = compositor;
268+    view->mapped = false;
269+    view->extents.x1 = surface->geometry.x;
270+    view->extents.y1 = surface->geometry.y;
271+    view->extents.x2 = surface->geometry.x + surface->geometry.width;
272+    view->extents.y2 = surface->geometry.y + surface->geometry.height;
273+    view->border.width = 0;
274+    view->border.color = 0x000000;
275+    view->border.damaged = false;
276+    view->surface_event_listener.notify = &handle_surface_event;
277+    wl_signal_add(&surface->event_signal, &view->surface_event_listener);
278+    pixman_region32_init(&view->clip);
279+    swc_surface_set_view(surface, &view->base);
280+
281+    return true;
282+}
283+
284 void swc_compositor_surface_show(struct swc_surface * surface)
285 {
286-    struct swc_compositor * compositor = CONTAINER_OF
287-        (surface->view, typeof(*compositor), compositor_view);
288-    struct surface_state * state = surface->view_state;
289+    struct view * view = (void *) surface->view;
290+    struct swc_compositor * compositor = view->compositor;
291 
292-    if (surface->view->impl != &view_impl)
293+    if (view->base.impl != &view_impl)
294         return;
295 
296-    if (state->mapped)
297+    if (view->mapped)
298         return;
299 
300     printf("showing surface %u\n", wl_resource_get_id(surface->resource));
301 
302-    state->mapped = true;
303+    view->mapped = true;
304 
305     /* Assume worst-case no clipping until we draw the next frame (in case the
306      * surface gets moved before that. */
307-    pixman_region32_clear(&state->clip);
308+    pixman_region32_clear(&view->clip);
309 
310     damage_surface(surface);
311     update_outputs(surface);
312@@ -519,20 +506,18 @@ void swc_compositor_surface_show(struct swc_surface * surface)
313 
314 void swc_compositor_surface_hide(struct swc_surface * surface)
315 {
316-    struct swc_compositor * compositor = CONTAINER_OF
317-        (surface->view, typeof(*compositor), compositor_view);
318-    struct surface_state * state = surface->view_state;
319+    struct view * view = (void *) surface->view;
320 
321-    if (surface->view->impl != &view_impl)
322+    if (view->base.impl != &view_impl)
323         return;
324 
325-    if (!state->mapped)
326+    if (!view->mapped)
327         return;
328 
329     /* Update all the outputs the surface was on. */
330     update(surface);
331 
332-    state->mapped = false;
333+    view->mapped = false;
334 
335     damage_below_surface(surface);
336     update_outputs(surface);
337@@ -542,13 +527,13 @@ void swc_compositor_surface_hide(struct swc_surface * surface)
338 void swc_compositor_surface_set_border_width(struct swc_surface * surface,
339                                              uint32_t width)
340 {
341-    struct surface_state * state = surface->view_state;
342+    struct view * view = (void *) surface->view;
343 
344-    if (state->border.width == width)
345+    if (view->border.width == width)
346         return;
347 
348-    state->border.width = width;
349-    state->border.damaged = true;
350+    view->border.width = width;
351+    view->border.damaged = true;
352 
353     /* XXX: Damage above surface for transparent surfaces? */
354 
355@@ -559,13 +544,13 @@ void swc_compositor_surface_set_border_width(struct swc_surface * surface,
356 void swc_compositor_surface_set_border_color(struct swc_surface * surface,
357                                              uint32_t color)
358 {
359-    struct surface_state * state = surface->view_state;
360+    struct view * view = (void *) surface->view;
361 
362-    if (state->border.color == color)
363+    if (view->border.color == color)
364         return;
365 
366-    state->border.color = color;
367-    state->border.damaged = true;
368+    view->border.color = color;
369+    view->border.damaged = true;
370 
371     /* XXX: Damage above surface for transparent surfaces? */
372 
373@@ -577,7 +562,7 @@ void swc_compositor_surface_set_border_color(struct swc_surface * surface,
374 static void calculate_damage(struct swc_compositor * compositor)
375 {
376     struct swc_surface * surface;
377-    struct surface_state * state;
378+    struct view * view;
379     pixman_region32_t surface_opaque;
380 
381     pixman_region32_clear(&compositor->opaque);
382@@ -586,10 +571,10 @@ static void calculate_damage(struct swc_compositor * compositor)
383     /* Go through surfaces top-down to calculate clipping regions. */
384     wl_list_for_each(surface, &compositor->surfaces, link)
385     {
386-        state = surface->view_state;
387+        view = (void *) surface->view;
388 
389         /* Clip the surface by the opaque region covering it. */
390-        pixman_region32_copy(&state->clip, &compositor->opaque);
391+        pixman_region32_copy(&view->clip, &compositor->opaque);
392 
393         /* Translate the opaque region to global coordinates. */
394         pixman_region32_copy(&surface_opaque, &surface->state.opaque);
395@@ -616,11 +601,11 @@ static void calculate_damage(struct swc_compositor * compositor)
396             pixman_region32_clear(&surface->state.damage);
397         }
398 
399-        if (state->border.damaged)
400+        if (view->border.damaged)
401         {
402             pixman_region32_t border_region, surface_region;
403 
404-            pixman_region32_init_with_extents(&border_region, &state->extents);
405+            pixman_region32_init_with_extents(&border_region, &view->extents);
406             pixman_region32_init_rect
407                 (&surface_region, surface->geometry.x, surface->geometry.y,
408                  surface->geometry.width, surface->geometry.height);
409@@ -634,7 +619,7 @@ static void calculate_damage(struct swc_compositor * compositor)
410             pixman_region32_fini(&border_region);
411             pixman_region32_fini(&surface_region);
412 
413-            state->border.damaged = false;
414+            view->border.damaged = false;
415         }
416     }
417 
418@@ -891,7 +876,6 @@ bool swc_compositor_initialize(struct swc_compositor * compositor,
419     compositor->pointer_listener.notify = &handle_pointer_event;
420     compositor->scheduled_updates = 0;
421     compositor->pending_flips = 0;
422-    swc_view_initialize(&compositor->compositor_view, &view_impl);
423     swc_view_initialize(&compositor->cursor_view, &swc_cursor_view_impl);
424     compositor->pointer_handler = (struct swc_pointer_handler) {
425         .focus = &handle_focus,
+5, -2
 1@@ -2,7 +2,6 @@
 2 #define SWC_COMPOSITOR_H
 3 
 4 #include "pointer.h"
 5-#include "view.h"
 6 
 7 #include <wayland-server.h>
 8 
 9@@ -30,7 +29,6 @@ struct swc_compositor
10         uint32_t scheduled_updates;
11     };
12 
13-    struct swc_view compositor_view;
14     struct swc_view cursor_view;
15 
16     struct swc_pointer_handler pointer_handler;
17@@ -51,6 +49,11 @@ void swc_compositor_add_globals(struct swc_compositor * compositor,
18 void swc_compositor_schedule_update(struct swc_compositor * compositor,
19                                     struct swc_output * output);
20 
21+bool swc_compositor_add_surface(struct swc_compositor * compositor,
22+                                struct swc_surface * surface);
23+bool swc_compositor_remove_surface(struct swc_compositor * compositor,
24+                                   struct swc_surface * surface);
25+
26 void swc_compositor_surface_show(struct swc_surface * surface);
27 void swc_compositor_surface_hide(struct swc_surface * surface);
28 void swc_compositor_surface_set_border_color(struct swc_surface * surface,
+0, -8
 1@@ -419,14 +419,6 @@ void swc_surface_set_view(struct swc_surface * surface,
 2 
 3     if (surface->view)
 4     {
 5-        if (surface->view->impl->add
 6-            && !surface->view->impl->add(surface))
 7-        {
 8-            surface->view = NULL;
 9-            return;
10-        }
11-
12-
13         surface->view->impl->attach(surface, surface->state.buffer);
14         surface->view->impl->update(surface);
15     }
+0, -3
 1@@ -42,9 +42,6 @@ struct swc_view
 2  */
 3 struct swc_view_impl
 4 {
 5-    /* Called when a surface is added to the view. */
 6-    bool (* add)(struct swc_surface * surface);
 7-
 8     /* Called when a surface is removed from the view. */
 9     void (* remove)(struct swc_surface * surface);
10 
+1, -1
1@@ -124,7 +124,7 @@ bool swc_window_initialize(struct swc_window * window,
2     INTERNAL(window)->impl = impl;
3 
4     surface->window = window;
5-    swc_surface_set_view(surface, &swc.compositor->compositor_view);
6+    swc_compositor_add_surface(swc.compositor, surface);
7 
8     swc.manager->new_window(window);
9