Implement atomic layout updates for tree operations

This implements atomic layout updates for when views map, reparent or
unmap.
This commit is contained in:
Ryan Dwyer 2018-06-23 16:24:11 +10:00
parent 1c89f32533
commit 38398e2d77
18 changed files with 545 additions and 389 deletions

View file

@ -9,6 +9,7 @@
#include "sway/output.h"
#include "sway/tree/container.h"
#include "sway/tree/view.h"
#include "sway/tree/workspace.h"
#include "list.h"
#include "log.h"
@ -18,6 +19,13 @@
*/
#define TIMEOUT_MS 200
/**
* If enabled, sway will always wait for the transaction timeout before
* applying it, rather than applying it when the views are ready. This allows us
* to observe the rendered state while a transaction is in progress.
*/
#define TRANSACTION_DEBUG false
struct sway_transaction {
struct wl_event_source *timer;
list_t *instructions; // struct sway_transaction_instruction *
@ -29,7 +37,9 @@ struct sway_transaction_instruction {
struct sway_transaction *transaction;
struct sway_container *container;
struct sway_container_state state;
struct wlr_buffer *saved_buffer;
uint32_t serial;
bool ready;
};
struct sway_transaction *transaction_create() {
@ -40,44 +50,55 @@ struct sway_transaction *transaction_create() {
return transaction;
}
static void remove_saved_view_buffer(
struct sway_transaction_instruction *instruction) {
if (instruction->saved_buffer) {
wlr_buffer_unref(instruction->saved_buffer);
instruction->saved_buffer = NULL;
}
}
static void save_view_buffer(struct sway_view *view,
struct sway_transaction_instruction *instruction) {
if (!sway_assert(instruction->saved_buffer == NULL,
"Didn't expect instruction to have a saved buffer already")) {
remove_saved_view_buffer(instruction);
}
if (view->surface && wlr_surface_has_buffer(view->surface)) {
wlr_buffer_ref(view->surface->buffer);
instruction->saved_buffer = view->surface->buffer;
}
}
static void transaction_destroy(struct sway_transaction *transaction) {
int i;
// Free instructions
for (i = 0; i < transaction->instructions->length; ++i) {
for (int i = 0; i < transaction->instructions->length; ++i) {
struct sway_transaction_instruction *instruction =
transaction->instructions->items[i];
if (instruction->container->type == C_VIEW) {
struct sway_view *view = instruction->container->sway_view;
for (int j = 0; j < view->instructions->length; ++j) {
if (view->instructions->items[j] == instruction) {
list_del(view->instructions, j);
break;
}
struct sway_container *con = instruction->container;
for (int j = 0; j < con->instructions->length; ++j) {
if (con->instructions->items[j] == instruction) {
list_del(con->instructions, j);
break;
}
}
if (con->destroying && !con->instructions->length) {
container_free(con);
}
remove_saved_view_buffer(instruction);
free(instruction);
}
list_free(transaction->instructions);
// Free damage
for (i = 0; i < transaction->damage->length; ++i) {
struct wlr_box *box = transaction->damage->items[i];
free(box);
}
list_foreach(transaction->damage, free);
list_free(transaction->damage);
free(transaction);
}
void transaction_add_container(struct sway_transaction *transaction,
struct sway_container *container) {
struct sway_transaction_instruction *instruction =
calloc(1, sizeof(struct sway_transaction_instruction));
instruction->transaction = transaction;
instruction->container = container;
// Copy the container's main (pending) properties into the instruction state
struct sway_container_state *state = &instruction->state;
static void copy_pending_state(struct sway_container *container,
struct sway_container_state *state) {
state->layout = container->layout;
state->swayc_x = container->x;
state->swayc_y = container->y;
@ -87,6 +108,7 @@ void transaction_add_container(struct sway_transaction *transaction,
state->current_gaps = container->current_gaps;
state->gaps_inner = container->gaps_inner;
state->gaps_outer = container->gaps_outer;
state->parent = container->parent;
if (container->type == C_VIEW) {
struct sway_view *view = container->sway_view;
@ -101,8 +123,44 @@ void transaction_add_container(struct sway_transaction *transaction,
state->border_left = view->border_left;
state->border_right = view->border_right;
state->border_bottom = view->border_bottom;
} else if (container->type == C_WORKSPACE) {
state->ws_fullscreen = container->sway_workspace->fullscreen;
state->ws_floating = container->sway_workspace->floating;
state->children = create_list();
list_cat(state->children, container->children);
} else {
state->children = create_list();
list_cat(state->children, container->children);
}
}
static bool transaction_has_container(struct sway_transaction *transaction,
struct sway_container *container) {
for (int i = 0; i < transaction->instructions->length; ++i) {
struct sway_transaction_instruction *instruction =
transaction->instructions->items[i];
if (instruction->container == container) {
return true;
}
}
return false;
}
void transaction_add_container(struct sway_transaction *transaction,
struct sway_container *container) {
if (transaction_has_container(transaction, container)) {
return;
}
struct sway_transaction_instruction *instruction =
calloc(1, sizeof(struct sway_transaction_instruction));
instruction->transaction = transaction;
instruction->container = container;
copy_pending_state(container, &instruction->state);
if (container->type == C_VIEW) {
save_view_buffer(container->sway_view, instruction);
}
list_add(transaction->instructions, instruction);
}
@ -113,47 +171,29 @@ void transaction_add_damage(struct sway_transaction *transaction,
list_add(transaction->damage, box);
}
static void save_view_buffer(struct sway_view *view) {
if (view->saved_buffer) {
wlr_buffer_unref(view->saved_buffer);
}
wlr_buffer_ref(view->surface->buffer);
view->saved_buffer = view->surface->buffer;
view->saved_surface_width = view->surface->current->width;
view->saved_surface_height = view->surface->current->height;
}
static void remove_saved_view_buffer(struct sway_view *view) {
if (view->saved_buffer) {
wlr_buffer_unref(view->saved_buffer);
view->saved_buffer = NULL;
view->saved_surface_width = 0;
view->saved_surface_height = 0;
}
}
/**
* Apply a transaction to the "current" state of the tree.
*
* This is mostly copying stuff from the pending state into the main swayc
* properties, but also includes reparenting and deleting containers.
*/
static void transaction_apply(struct sway_transaction *transaction) {
int i;
// Apply the instruction state to the container's current state
for (i = 0; i < transaction->instructions->length; ++i) {
struct sway_transaction_instruction *instruction =
transaction->instructions->items[i];
struct sway_container *container = instruction->container;
memcpy(&instruction->container->current, &instruction->state,
sizeof(struct sway_container_state));
// There are separate children lists for each instruction state, the
// container's current state and the container's pending state
// (ie. con->children). The list itself needs to be freed here.
// Any child containers which are being deleted will be cleaned up in
// transaction_destroy().
list_free(container->current.children);
if (container->type == C_VIEW) {
remove_saved_view_buffer(container->sway_view);
}
memcpy(&container->current, &instruction->state,
sizeof(struct sway_container_state));
}
// Damage
// Apply damage
for (i = 0; i < transaction->damage->length; ++i) {
struct wlr_box *box = transaction->damage->items[i];
for (int j = 0; j < root_container.children->length; ++j) {
@ -161,8 +201,6 @@ static void transaction_apply(struct sway_transaction *transaction) {
output_damage_box(output->sway_output, box);
}
}
update_debug_tree();
}
static int handle_timeout(void *data) {
@ -182,7 +220,7 @@ void transaction_commit(struct sway_transaction *transaction) {
struct sway_transaction_instruction *instruction =
transaction->instructions->items[i];
struct sway_container *con = instruction->container;
if (con->type == C_VIEW &&
if (con->type == C_VIEW && !con->destroying &&
(con->current.view_width != instruction->state.view_width ||
con->current.view_height != instruction->state.view_height)) {
instruction->serial = view_configure(con->sway_view,
@ -191,14 +229,12 @@ void transaction_commit(struct sway_transaction *transaction) {
instruction->state.view_width,
instruction->state.view_height);
if (instruction->serial) {
save_view_buffer(con->sway_view);
list_add(con->sway_view->instructions, instruction);
++transaction->num_waiting;
}
}
list_add(con->instructions, instruction);
}
if (!transaction->num_waiting) {
// This can happen if the transaction only contains xwayland views
wlr_log(L_DEBUG, "Transaction %p has nothing to wait for, applying",
transaction);
transaction_apply(transaction);
@ -210,31 +246,47 @@ void transaction_commit(struct sway_transaction *transaction) {
transaction->timer = wl_event_loop_add_timer(server.wl_event_loop,
handle_timeout, transaction);
wl_event_source_timer_update(transaction->timer, TIMEOUT_MS);
// The debug tree shows the pending/live tree. Here is a good place to
// update it, because we make a transaction every time we change the pending
// tree.
update_debug_tree();
}
void transaction_notify_view_ready(struct sway_view *view, uint32_t serial) {
// Find the instruction
struct sway_transaction_instruction *instruction = NULL;
for (int i = 0; i < view->instructions->length; ++i) {
for (int i = 0; i < view->swayc->instructions->length; ++i) {
struct sway_transaction_instruction *tmp_instruction =
view->instructions->items[i];
if (tmp_instruction->serial == serial) {
view->swayc->instructions->items[i];
if (tmp_instruction->serial == serial && !tmp_instruction->ready) {
instruction = tmp_instruction;
list_del(view->instructions, i);
break;
}
}
if (!instruction) {
// This can happen if the view acknowledges the configure after the
// transaction has timed out and applied.
return;
}
instruction->ready = true;
// If all views are ready, apply the transaction
struct sway_transaction *transaction = instruction->transaction;
if (--transaction->num_waiting == 0) {
#if !TRANSACTION_DEBUG
wlr_log(L_DEBUG, "Transaction %p is ready, applying", transaction);
wl_event_source_timer_update(transaction->timer, 0);
transaction_apply(transaction);
transaction_destroy(transaction);
#endif
}
}
struct wlr_texture *transaction_get_texture(struct sway_view *view) {
if (!view->swayc || !view->swayc->instructions->length) {
return view->surface->buffer->texture;
}
struct sway_transaction_instruction *instruction =
view->swayc->instructions->items[0];
return instruction->saved_buffer ?
instruction->saved_buffer->texture : NULL;
}