<commit_before>#include <assert.h>
#include <stdlib.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_buffer.h>
#include <wlr/types/wlr_linux_dmabuf_v1.h>
#include <wlr/util/log.h>

bool wlr_resource_is_buffer(struct wl_resource *resource) {
	return strcmp(wl_resource_get_class(resource), wl_buffer_interface.name) == 0;
}

bool wlr_buffer_get_resource_size(struct wl_resource *resource,
		struct wlr_renderer *renderer, int *width, int *height) {
	assert(wlr_resource_is_buffer(resource));

	struct wl_shm_buffer *shm_buf = wl_shm_buffer_get(resource);
	if (shm_buf != NULL) {
		*width = wl_shm_buffer_get_width(shm_buf);
		*height = wl_shm_buffer_get_height(shm_buf);
	} else if (wlr_renderer_resource_is_wl_drm_buffer(renderer,
			resource)) {
		wlr_renderer_wl_drm_buffer_get_size(renderer, resource,
			width, height);
	} else if (wlr_dmabuf_v1_resource_is_buffer(resource)) {
		struct wlr_dmabuf_v1_buffer *dmabuf =
			wlr_dmabuf_v1_buffer_from_buffer_resource(resource);
		*width = dmabuf->attributes.width;
		*height = dmabuf->attributes.height;
	} else {
		*width = *height = 0;
		return false;
	}

	return true;
}


static void buffer_resource_handle_destroy(struct wl_listener *listener,
		void *data) {
	struct wlr_buffer *buffer =
		wl_container_of(listener, buffer, resource_destroy);
	wl_list_remove(&buffer->resource_destroy.link);
	wl_list_init(&buffer->resource_destroy.link);
	buffer->resource = NULL;

	// At this point, if the wl_buffer comes from linux-dmabuf or wl_drm, we
	// still haven't released it (ie. we'll read it in the future) but the
	// client destroyed it. Reading the texture itself should be fine because
	// we still hold a reference to the DMA-BUF via the texture. However the
	// client could decide to re-use the same DMA-BUF for something else, in
	// which case we'll read garbage. We decide to accept this risk.
}

struct wlr_buffer *wlr_buffer_create(struct wlr_renderer *renderer,
		struct wl_resource *resource) {
	assert(wlr_resource_is_buffer(resource));

	struct wlr_texture *texture = NULL;
	bool released = false;

	struct wl_shm_buffer *shm_buf = wl_shm_buffer_get(resource);
	if (shm_buf != NULL) {
		enum wl_shm_format fmt = wl_shm_buffer_get_format(shm_buf);
		int32_t stride = wl_shm_buffer_get_stride(shm_buf);
		int32_t width = wl_shm_buffer_get_width(shm_buf);
		int32_t height = wl_shm_buffer_get_height(shm_buf);

		wl_shm_buffer_begin_access(shm_buf);
		void *data = wl_shm_buffer_get_data(shm_buf);
		texture = wlr_texture_from_pixels(renderer, fmt, stride,
			width, height, data);
		wl_shm_buffer_end_access(shm_buf);

		// We have uploaded the data, we don't need to access the wl_buffer
		// anymore
		wl_buffer_send_release(resource);
		released = true;
	} else if (wlr_renderer_resource_is_wl_drm_buffer(renderer, resource)) {
		texture = wlr_texture_from_wl_drm(renderer, resource);
	} else if (wlr_dmabuf_v1_resource_is_buffer(resource)) {
		struct wlr_dmabuf_v1_buffer *dmabuf =
			wlr_dmabuf_v1_buffer_from_buffer_resource(resource);
		texture = wlr_texture_from_dmabuf(renderer, &dmabuf->attributes);

		// We have imported the DMA-BUF, but we need to prevent the client from
		// re-using the same DMA-BUF for the next frames, so we don't release
		// the buffer yet.
	} else {
		wlr_log(WLR_ERROR, "Cannot upload texture: unknown buffer type");
		return NULL;
	}

	if (texture == NULL) {
		wlr_log(WLR_ERROR, "Failed to upload texture");
		return NULL;
	}

	struct wlr_buffer *buffer = calloc(1, sizeof(struct wlr_buffer));
	if (buffer == NULL) {
		wlr_texture_destroy(texture);
		return NULL;
	}
	buffer->resource = resource;
	buffer->texture = texture;
	buffer->released = released;
	buffer->n_refs = 1;

	wl_resource_add_destroy_listener(resource, &buffer->resource_destroy);
	buffer->resource_destroy.notify = buffer_resource_handle_destroy;

	return buffer;
}

struct wlr_buffer *wlr_buffer_ref(struct wlr_buffer *buffer) {
	buffer->n_refs++;
	return buffer;
}
<commit_msg>buffer: disconnect clients that commit an unknown buffer type<commit_after>#include <assert.h>
#include <stdlib.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_buffer.h>
#include <wlr/types/wlr_linux_dmabuf_v1.h>
#include <wlr/util/log.h>

bool wlr_resource_is_buffer(struct wl_resource *resource) {
	return strcmp(wl_resource_get_class(resource), wl_buffer_interface.name) == 0;
}

bool wlr_buffer_get_resource_size(struct wl_resource *resource,
		struct wlr_renderer *renderer, int *width, int *height) {
	assert(wlr_resource_is_buffer(resource));

	struct wl_shm_buffer *shm_buf = wl_shm_buffer_get(resource);
	if (shm_buf != NULL) {
		*width = wl_shm_buffer_get_width(shm_buf);
		*height = wl_shm_buffer_get_height(shm_buf);
	} else if (wlr_renderer_resource_is_wl_drm_buffer(renderer,
			resource)) {
		wlr_renderer_wl_drm_buffer_get_size(renderer, resource,
			width, height);
	} else if (wlr_dmabuf_v1_resource_is_buffer(resource)) {
		struct wlr_dmabuf_v1_buffer *dmabuf =
			wlr_dmabuf_v1_buffer_from_buffer_resource(resource);
		*width = dmabuf->attributes.width;
		*height = dmabuf->attributes.height;
	} else {
		*width = *height = 0;
		return false;
	}

	return true;
}


static void buffer_resource_handle_destroy(struct wl_listener *listener,
		void *data) {
	struct wlr_buffer *buffer =
		wl_container_of(listener, buffer, resource_destroy);
	wl_list_remove(&buffer->resource_destroy.link);
	wl_list_init(&buffer->resource_destroy.link);
	buffer->resource = NULL;

	// At this point, if the wl_buffer comes from linux-dmabuf or wl_drm, we
	// still haven't released it (ie. we'll read it in the future) but the
	// client destroyed it. Reading the texture itself should be fine because
	// we still hold a reference to the DMA-BUF via the texture. However the
	// client could decide to re-use the same DMA-BUF for something else, in
	// which case we'll read garbage. We decide to accept this risk.
}

struct wlr_buffer *wlr_buffer_create(struct wlr_renderer *renderer,
		struct wl_resource *resource) {
	assert(wlr_resource_is_buffer(resource));

	struct wlr_texture *texture = NULL;
	bool released = false;

	struct wl_shm_buffer *shm_buf = wl_shm_buffer_get(resource);
	if (shm_buf != NULL) {
		enum wl_shm_format fmt = wl_shm_buffer_get_format(shm_buf);
		int32_t stride = wl_shm_buffer_get_stride(shm_buf);
		int32_t width = wl_shm_buffer_get_width(shm_buf);
		int32_t height = wl_shm_buffer_get_height(shm_buf);

		wl_shm_buffer_begin_access(shm_buf);
		void *data = wl_shm_buffer_get_data(shm_buf);
		texture = wlr_texture_from_pixels(renderer, fmt, stride,
			width, height, data);
		wl_shm_buffer_end_access(shm_buf);

		// We have uploaded the data, we don't need to access the wl_buffer
		// anymore
		wl_buffer_send_release(resource);
		released = true;
	} else if (wlr_renderer_resource_is_wl_drm_buffer(renderer, resource)) {
		texture = wlr_texture_from_wl_drm(renderer, resource);
	} else if (wlr_dmabuf_v1_resource_is_buffer(resource)) {
		struct wlr_dmabuf_v1_buffer *dmabuf =
			wlr_dmabuf_v1_buffer_from_buffer_resource(resource);
		texture = wlr_texture_from_dmabuf(renderer, &dmabuf->attributes);

		// We have imported the DMA-BUF, but we need to prevent the client from
		// re-using the same DMA-BUF for the next frames, so we don't release
		// the buffer yet.
	} else {
		wlr_log(WLR_ERROR, "Cannot upload texture: unknown buffer type");

		// Instead of just logging the error, also disconnect the client with a
		// fatal protocol error so that it's clear something went wrong.
		wl_resource_post_error(resource, 0, "unknown buffer type");
		return NULL;
	}

	if (texture == NULL) {
		wlr_log(WLR_ERROR, "Failed to upload texture");
		return NULL;
	}

	struct wlr_buffer *buffer = calloc(1, sizeof(struct wlr_buffer));
	if (buffer == NULL) {
		wlr_texture_destroy(texture);
		return NULL;
	}
	buffer->resource = resource;
	buffer->texture = texture;
	buffer->released = released;
	buffer->n_refs = 1;

	wl_resource_add_destroy_listener(resource, &buffer->resource_destroy);
	buffer->resource_destroy.notify = buffer_resource_handle_destroy;

	return buffer;
}

struct wlr_buffer *wlr_buffer_ref(struct wlr_buffer *buffer) {
	buffer->n_refs++;
	return buffer;
}

{'fraction_non_alphanumeric': 0.09799853551379058, 'fraction_numerical': 0.004271418110812789, 'mean_word_length': 5.432496075353218, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6843568', 'n_tokens_mistral': 3108, 'n_tokens_neox': 2959, 'n_words': 681}