mirror of
				https://github.com/labwc/labwc.git
				synced 2025-11-03 09:01:51 -05:00 
			
		
		
		
	scaled_scene_buffer: make dropping the buffer optional
In preparation to also use the scaled_scene_buffer for theme components like rounded corner images and button icons. No functional change intended.
This commit is contained in:
		
							parent
							
								
									7aa540b8ab
								
							
						
					
					
						commit
						8d17ab2d60
					
				
					 4 changed files with 51 additions and 10 deletions
				
			
		| 
						 | 
					@ -6,7 +6,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct wlr_scene_tree;
 | 
					struct wlr_scene_tree;
 | 
				
			||||||
struct wlr_scene_buffer;
 | 
					struct wlr_scene_buffer;
 | 
				
			||||||
struct scaled_scene_buffere;
 | 
					struct scaled_scene_buffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct scaled_font_buffer {
 | 
					struct scaled_font_buffer {
 | 
				
			||||||
	struct wlr_scene_buffer *scene_buffer;
 | 
						struct wlr_scene_buffer *scene_buffer;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,6 +26,7 @@ struct scaled_scene_buffer {
 | 
				
			||||||
	void *data;  /* opaque user data */
 | 
						void *data;  /* opaque user data */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Private */
 | 
						/* Private */
 | 
				
			||||||
 | 
						bool drop_buffer;
 | 
				
			||||||
	double active_scale;
 | 
						double active_scale;
 | 
				
			||||||
	struct wl_list cache;  /* struct scaled_buffer_cache_entry.link */
 | 
						struct wl_list cache;  /* struct scaled_buffer_cache_entry.link */
 | 
				
			||||||
	struct wl_listener destroy;
 | 
						struct wl_listener destroy;
 | 
				
			||||||
| 
						 | 
					@ -51,10 +52,34 @@ struct scaled_scene_buffer {
 | 
				
			||||||
 * wlr_scene_buffer is being destroyed. If implementation->destroy is set
 | 
					 * wlr_scene_buffer is being destroyed. If implementation->destroy is set
 | 
				
			||||||
 * it will also get called so a consumer of this API may clean up its own
 | 
					 * it will also get called so a consumer of this API may clean up its own
 | 
				
			||||||
 * allocations.
 | 
					 * allocations.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * All requested lab_data_buffers via impl->create_buffer() will be locked
 | 
				
			||||||
 | 
					 * during the lifetime of the buffer in the internal cache and unlocked
 | 
				
			||||||
 | 
					 * when being evacuated from the cache (due to LAB_SCALED_BUFFER_MAX_CACHE
 | 
				
			||||||
 | 
					 * or the internal wlr_scene_buffer being destroyed).
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * If drop_buffer was set during creation of the scaled_scene_buffer, the
 | 
				
			||||||
 | 
					 * backing wlr_buffer behind a lab_data_buffer will also get dropped
 | 
				
			||||||
 | 
					 * (via wlr_buffer_drop). If there are no more locks (consumers) of the
 | 
				
			||||||
 | 
					 * respective buffer this will then cause the lab_data_buffer to be free'd.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * In the case of the buffer provider dropping the buffer itself (due to
 | 
				
			||||||
 | 
					 * for example a Reconfigure event) the lock prevents the buffer from being
 | 
				
			||||||
 | 
					 * destroyed until the buffer is evacuated from the internal cache and thus
 | 
				
			||||||
 | 
					 * unlocked.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This allows using scaled_scene_buffer for an autoscaling font_buffer
 | 
				
			||||||
 | 
					 * (which gets free'd automatically) and also for theme components like
 | 
				
			||||||
 | 
					 * rounded corner images or button icons whose buffers only exist once but
 | 
				
			||||||
 | 
					 * are references by multiple windows with their own scaled_scene_buffers.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * The rough idea is: use drop_buffer = true for one-shot buffers and false
 | 
				
			||||||
 | 
					 * for buffers that should outlive the scaled_scene_buffer instance itself.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
struct scaled_scene_buffer *scaled_scene_buffer_create(
 | 
					struct scaled_scene_buffer *scaled_scene_buffer_create(
 | 
				
			||||||
	struct wlr_scene_tree *parent,
 | 
						struct wlr_scene_tree *parent,
 | 
				
			||||||
	const struct scaled_scene_buffer_impl *implementation);
 | 
						const struct scaled_scene_buffer_impl *implementation,
 | 
				
			||||||
 | 
						bool drop_buffer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Clear the cache of existing buffers, useful in case the content changes */
 | 
					/* Clear the cache of existing buffers, useful in case the content changes */
 | 
				
			||||||
void scaled_scene_buffer_invalidate_cache(struct scaled_scene_buffer *self);
 | 
					void scaled_scene_buffer_invalidate_cache(struct scaled_scene_buffer *self);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,10 +30,12 @@ static void
 | 
				
			||||||
_destroy(struct scaled_scene_buffer *scaled_buffer)
 | 
					_destroy(struct scaled_scene_buffer *scaled_buffer)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct scaled_font_buffer *self = scaled_buffer->data;
 | 
						struct scaled_font_buffer *self = scaled_buffer->data;
 | 
				
			||||||
 | 
						scaled_buffer->data = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	zfree(self->text);
 | 
						zfree(self->text);
 | 
				
			||||||
	zfree(self->font.name);
 | 
						zfree(self->font.name);
 | 
				
			||||||
	zfree(self->arrow);
 | 
						zfree(self->arrow);
 | 
				
			||||||
	zfree(scaled_buffer->data);
 | 
						free(self);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const struct scaled_scene_buffer_impl impl = {
 | 
					static const struct scaled_scene_buffer_impl impl = {
 | 
				
			||||||
| 
						 | 
					@ -48,7 +50,7 @@ scaled_font_buffer_create(struct wlr_scene_tree *parent)
 | 
				
			||||||
	assert(parent);
 | 
						assert(parent);
 | 
				
			||||||
	struct scaled_font_buffer *self = znew(*self);
 | 
						struct scaled_font_buffer *self = znew(*self);
 | 
				
			||||||
	struct scaled_scene_buffer *scaled_buffer =
 | 
						struct scaled_scene_buffer *scaled_buffer =
 | 
				
			||||||
		scaled_scene_buffer_create(parent, &impl);
 | 
							scaled_scene_buffer_create(parent, &impl, /* drop_buffer */ true);
 | 
				
			||||||
	if (!scaled_buffer) {
 | 
						if (!scaled_buffer) {
 | 
				
			||||||
		free(self);
 | 
							free(self);
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,11 +36,15 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Internal API */
 | 
					/* Internal API */
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
_cache_entry_destroy(struct scaled_scene_buffer_cache_entry *cache_entry)
 | 
					_cache_entry_destroy(struct scaled_scene_buffer_cache_entry *cache_entry, bool drop_buffer)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	wl_list_remove(&cache_entry->link);
 | 
						wl_list_remove(&cache_entry->link);
 | 
				
			||||||
	if (cache_entry->buffer) {
 | 
						if (cache_entry->buffer) {
 | 
				
			||||||
		wlr_buffer_drop(cache_entry->buffer);
 | 
							/* Allow the buffer to get dropped if there are no further consumers */
 | 
				
			||||||
 | 
							wlr_buffer_unlock(cache_entry->buffer);
 | 
				
			||||||
 | 
							if (drop_buffer) {
 | 
				
			||||||
 | 
								wlr_buffer_drop(cache_entry->buffer);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	free(cache_entry);
 | 
						free(cache_entry);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -64,6 +68,10 @@ _update_buffer(struct scaled_scene_buffer *self, double scale)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Create new buffer, will get destroyed along the backing wlr_buffer */
 | 
						/* Create new buffer, will get destroyed along the backing wlr_buffer */
 | 
				
			||||||
	struct lab_data_buffer *buffer = self->impl->create_buffer(self, scale);
 | 
						struct lab_data_buffer *buffer = self->impl->create_buffer(self, scale);
 | 
				
			||||||
 | 
						if (buffer) {
 | 
				
			||||||
 | 
							/* Ensure the buffer doesn't get deleted behind our back */
 | 
				
			||||||
 | 
							wlr_buffer_lock(&buffer->base);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	self->width = buffer ? buffer->unscaled_width : 0;
 | 
						self->width = buffer ? buffer->unscaled_width : 0;
 | 
				
			||||||
	self->height = buffer ? buffer->unscaled_height : 0;
 | 
						self->height = buffer ? buffer->unscaled_height : 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,7 +81,11 @@ _update_buffer(struct scaled_scene_buffer *self, double scale)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		cache_entry = wl_container_of(self->cache.prev, cache_entry, link);
 | 
							cache_entry = wl_container_of(self->cache.prev, cache_entry, link);
 | 
				
			||||||
		if (cache_entry->buffer) {
 | 
							if (cache_entry->buffer) {
 | 
				
			||||||
			wlr_buffer_drop(cache_entry->buffer);
 | 
								/* Allow the old buffer to get dropped if there are no further consumers */
 | 
				
			||||||
 | 
								wlr_buffer_unlock(cache_entry->buffer);
 | 
				
			||||||
 | 
								if (self->drop_buffer) {
 | 
				
			||||||
 | 
									wlr_buffer_drop(cache_entry->buffer);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		wl_list_remove(&cache_entry->link);
 | 
							wl_list_remove(&cache_entry->link);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -100,7 +112,7 @@ _handle_node_destroy(struct wl_listener *listener, void *data)
 | 
				
			||||||
	wl_list_remove(&self->output_leave.link);
 | 
						wl_list_remove(&self->output_leave.link);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wl_list_for_each_safe(cache_entry, cache_entry_tmp, &self->cache, link) {
 | 
						wl_list_for_each_safe(cache_entry, cache_entry_tmp, &self->cache, link) {
 | 
				
			||||||
		_cache_entry_destroy(cache_entry);
 | 
							_cache_entry_destroy(cache_entry, self->drop_buffer);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	assert(wl_list_empty(&self->cache));
 | 
						assert(wl_list_empty(&self->cache));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -146,7 +158,8 @@ _handle_output_leave(struct wl_listener *listener, void *data)
 | 
				
			||||||
/* Public API */
 | 
					/* Public API */
 | 
				
			||||||
struct scaled_scene_buffer *
 | 
					struct scaled_scene_buffer *
 | 
				
			||||||
scaled_scene_buffer_create(struct wlr_scene_tree *parent,
 | 
					scaled_scene_buffer_create(struct wlr_scene_tree *parent,
 | 
				
			||||||
		const struct scaled_scene_buffer_impl *impl)
 | 
							const struct scaled_scene_buffer_impl *impl,
 | 
				
			||||||
 | 
							bool drop_buffer)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	assert(parent);
 | 
						assert(parent);
 | 
				
			||||||
	assert(impl);
 | 
						assert(impl);
 | 
				
			||||||
| 
						 | 
					@ -162,6 +175,7 @@ scaled_scene_buffer_create(struct wlr_scene_tree *parent,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	self->impl = impl;
 | 
						self->impl = impl;
 | 
				
			||||||
	self->active_scale = 1;
 | 
						self->active_scale = 1;
 | 
				
			||||||
 | 
						self->drop_buffer = drop_buffer;
 | 
				
			||||||
	wl_list_init(&self->cache);
 | 
						wl_list_init(&self->cache);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Listen to output enter/leave so we get notified about scale changes */
 | 
						/* Listen to output enter/leave so we get notified about scale changes */
 | 
				
			||||||
| 
						 | 
					@ -183,7 +197,7 @@ scaled_scene_buffer_invalidate_cache(struct scaled_scene_buffer *self)
 | 
				
			||||||
	assert(self);
 | 
						assert(self);
 | 
				
			||||||
	struct scaled_scene_buffer_cache_entry *cache_entry, *cache_entry_tmp;
 | 
						struct scaled_scene_buffer_cache_entry *cache_entry, *cache_entry_tmp;
 | 
				
			||||||
	wl_list_for_each_safe(cache_entry, cache_entry_tmp, &self->cache, link) {
 | 
						wl_list_for_each_safe(cache_entry, cache_entry_tmp, &self->cache, link) {
 | 
				
			||||||
		_cache_entry_destroy(cache_entry);
 | 
							_cache_entry_destroy(cache_entry, self->drop_buffer);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	assert(wl_list_empty(&self->cache));
 | 
						assert(wl_list_empty(&self->cache));
 | 
				
			||||||
	_update_buffer(self, self->active_scale);
 | 
						_update_buffer(self, self->active_scale);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue