compositor: set surface->plane from destroyed plane to NULL

In drm backend, the cursor_surface->plane point to
drm_output->cursor_plane.when this output is removed,
drm_output->cursor_plane is destroyed, butcursor_surface->plane
still point to destroyed plane. So once mouse move to this
cursor_surface and system will repaint this cursor_surface,
segment fault will generate in weston_surface_damage_below() function.

V2:
-set surface->plane to NULL whose plane point to unplugged output,
 then change weston_surface_damage_below() to do nothing if
 surface->plane is NULL (Kristian)
-set surface->plane to NULL in weston_surface_unmap(),
 so that all surfaces that have a non-NULL plane pointer wil be
 on compositor->surface_list (Kristian).

bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=69777

Signed-off-by: Xiong Zhang <xiong.y.zhang@intel.com>
diff --git a/src/compositor.c b/src/compositor.c
index 11f582e..632bbe7 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -371,7 +371,7 @@
 	wl_list_init(&view->link);
 	wl_list_init(&view->layer_link);
 
-	view->plane = &surface->compositor->primary_plane;
+	view->plane = NULL;
 
 	pixman_region32_init(&view->clip);
 
@@ -610,8 +610,9 @@
 	pixman_region32_init(&damage);
 	pixman_region32_subtract(&damage, &view->transform.boundingbox,
 				 &view->clip);
-	pixman_region32_union(&view->plane->damage,
-			      &view->plane->damage, &damage);
+	if (view->plane)
+		pixman_region32_union(&view->plane->damage,
+				      &view->plane->damage, &damage);
 	pixman_region32_fini(&damage);
 }
 
@@ -1147,6 +1148,7 @@
 
 	weston_view_damage_below(view);
 	view->output = NULL;
+	view->plane = NULL;
 	wl_list_remove(&view->layer_link);
 	wl_list_init(&view->layer_link);
 	wl_list_remove(&view->link);
@@ -2844,12 +2846,15 @@
 }
 
 WL_EXPORT void
-weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
+weston_plane_init(struct weston_plane *plane,
+			struct weston_compositor *ec,
+			int32_t x, int32_t y)
 {
 	pixman_region32_init(&plane->damage);
 	pixman_region32_init(&plane->clip);
 	plane->x = x;
 	plane->y = y;
+	plane->compositor = ec;
 
 	/* Init the link so that the call to wl_list_remove() when releasing
 	 * the plane without ever stacking doesn't lead to a crash */
@@ -2859,9 +2864,16 @@
 WL_EXPORT void
 weston_plane_release(struct weston_plane *plane)
 {
+	struct weston_view *view;
+
 	pixman_region32_fini(&plane->damage);
 	pixman_region32_fini(&plane->clip);
 
+	wl_list_for_each(view, &plane->compositor->view_list, link) {
+		if (view->plane == plane)
+			view->plane = NULL;
+	}
+
 	wl_list_remove(&plane->link);
 }
 
@@ -3256,7 +3268,7 @@
 	wl_list_init(&ec->axis_binding_list);
 	wl_list_init(&ec->debug_binding_list);
 
-	weston_plane_init(&ec->primary_plane, 0, 0);
+	weston_plane_init(&ec->primary_plane, ec, 0, 0);
 	weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
 
 	s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);