commit cbef704d166bea3ef8884990928593dcbd712bfe
parent d6cfcbff26ad3807d814825d8b024b608bc91428
Author: Michael Forney <mforney@mforney.org>
Date: Thu, 16 Jan 2014 02:55:16 -0800
Use common buffer struct
Diffstat:
10 files changed, 316 insertions(+), 140 deletions(-)
diff --git a/libswc/buffer.c b/libswc/buffer.c
@@ -0,0 +1,41 @@
+/* swc: libswc/buffer.c
+ *
+ * Copyright (c) 2013 Michael Forney
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "buffer.h"
+
+#include <wld/wld.h>
+
+bool swc_buffer_initialize(struct swc_buffer * buffer, struct wld_buffer * wld)
+{
+ buffer->wld = wld;
+ wl_signal_init(&buffer->destroy_signal);
+
+ return true;
+}
+
+void swc_buffer_finalize(struct swc_buffer * buffer)
+{
+ wl_signal_emit(&buffer->destroy_signal, buffer);
+ wld_destroy_buffer(buffer->wld);
+}
+
diff --git a/libswc/buffer.h b/libswc/buffer.h
@@ -0,0 +1,42 @@
+/* swc: libswc/buffer.h
+ *
+ * Copyright (c) 2013 Michael Forney
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef SWC_BUFFER_H
+#define SWC_BUFFER_H
+
+#include <stdbool.h>
+#include <wayland-server.h>
+
+struct swc_buffer
+{
+ struct wld_buffer * wld;
+ struct wl_signal destroy_signal;
+};
+
+bool swc_buffer_initialize(struct swc_buffer * buffer,
+ struct wld_buffer * wld);
+
+void swc_buffer_finalize(struct swc_buffer * buffer);
+
+#endif
+
diff --git a/libswc/compositor.c b/libswc/compositor.c
@@ -1,8 +1,8 @@
#include "swc.h"
+#include "buffer.h"
#include "compositor.h"
#include "data_device_manager.h"
#include "drm.h"
-#include "drm_buffer.h"
#include "internal.h"
#include "output.h"
#include "pointer.h"
@@ -11,6 +11,7 @@
#include "surface.h"
#include "util.h"
#include "view.h"
+#include "wayland_buffer.h"
#include <stdlib.h>
#include <stdio.h>
@@ -168,7 +169,7 @@ static void renderer_attach(struct swc_surface * surface,
struct wl_resource * resource)
{
struct buffer_state * state;
- struct swc_drm_buffer * drm_buffer;
+ struct swc_buffer * drm_buffer;
if (!resource)
return;
@@ -180,7 +181,7 @@ static void renderer_attach(struct swc_surface * surface,
if (!(state = malloc(sizeof *state)))
return;
- if ((drm_buffer = swc_drm_buffer_get(resource)))
+ if ((drm_buffer = swc_wayland_buffer_get(resource)))
{
if (!(state = malloc(sizeof *state)))
return;
diff --git a/libswc/drm.c b/libswc/drm.c
@@ -22,10 +22,10 @@
*/
#include "drm.h"
-#include "drm_buffer.h"
#include "event.h"
#include "internal.h"
#include "output.h"
+#include "wayland_buffer.h"
#include <stdio.h>
#include <stdlib.h>
@@ -73,7 +73,7 @@ static void create_buffer(struct wl_client * client,
uint32_t stride, uint32_t format)
{
struct wld_buffer * wld;
- struct swc_drm_buffer * buffer;
+ struct swc_buffer * buffer;
union wld_object object = { .u32 = name };
wld = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_GEM_NAME, object,
@@ -82,7 +82,7 @@ static void create_buffer(struct wl_client * client,
if (!wld)
goto error0;
- buffer = swc_drm_buffer_new(client, id, wld);
+ buffer = swc_wayland_buffer_new(client, id, wld);
if (!buffer)
goto error1;
@@ -116,7 +116,7 @@ static void create_prime_buffer(struct wl_client * client,
int32_t offset2, int32_t stride2)
{
struct wld_buffer * wld;
- struct swc_drm_buffer * buffer;
+ struct swc_buffer * buffer;
union wld_object object = { .i = fd };
wld = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_PRIME_FD, object,
@@ -126,7 +126,7 @@ static void create_prime_buffer(struct wl_client * client,
if (!wld)
goto error0;
- buffer = swc_drm_buffer_new(client, id, wld);
+ buffer = swc_wayland_buffer_new(client, id, wld);
if (!buffer)
goto error1;
diff --git a/libswc/drm_buffer.c b/libswc/drm_buffer.c
@@ -1,75 +0,0 @@
-/* swc: drm_buffer.c
- *
- * Copyright (c) 2013 Michael Forney
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-#include "drm_buffer.h"
-
-#include <stdlib.h>
-#include <wayland-server.h>
-#include <wld/wld.h>
-#include "protocol/wayland-drm-server-protocol.h"
-
-static void destroy(struct wl_client * client, struct wl_resource * resource)
-{
- wl_resource_destroy(resource);
-}
-
-static const struct wl_buffer_interface drm_buffer_implementation = {
- .destroy = &destroy
-};
-
-static void buffer_destroy(struct wl_resource * resource)
-{
- struct swc_drm_buffer * buffer = wl_resource_get_user_data(resource);
-
- free(buffer);
-}
-
-struct swc_drm_buffer * swc_drm_buffer_new
- (struct wl_client * client, uint32_t id, struct wld_buffer * wld)
-{
- struct swc_drm_buffer * buffer;
-
- buffer = malloc(sizeof *buffer);
-
- if (!buffer)
- return NULL;
-
- buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
- wl_resource_set_implementation(buffer->resource, &drm_buffer_implementation,
- buffer, &buffer_destroy);
- buffer->wld = wld;
-
- return buffer;
-}
-
-struct swc_drm_buffer * swc_drm_buffer_get(struct wl_resource * resource)
-{
- if (wl_resource_instance_of(resource, &wl_buffer_interface,
- &drm_buffer_implementation))
- {
- return wl_resource_get_user_data(resource);
- }
-
- return NULL;
-}
-
diff --git a/libswc/drm_buffer.h b/libswc/drm_buffer.h
@@ -1,43 +0,0 @@
-/* swc: drm_buffer.h
- *
- * Copyright (c) 2013 Michael Forney
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-#ifndef SWC_DRM_BUFFER_H
-#define SWC_DRM_BUFFER_H
-
-#include <stdint.h>
-
-struct wl_client;
-
-struct swc_drm_buffer
-{
- struct wl_resource * resource;
- struct wld_buffer * wld;
-};
-
-struct swc_drm_buffer * swc_drm_buffer_new
- (struct wl_client * client, uint32_t id, struct wld_buffer * wld);
-
-struct swc_drm_buffer * swc_drm_buffer_get(struct wl_resource * resource);
-
-#endif
-
diff --git a/libswc/local.mk b/libswc/local.mk
@@ -31,6 +31,8 @@ $(dir)_PACKAGES = \
SWC_SOURCES = \
libswc/compositor.c \
libswc/view.c \
+ libswc/buffer.c \
+ libswc/wayland_buffer.c \
libswc/util.c \
libswc/output.c \
libswc/plane.c \
@@ -48,7 +50,6 @@ SWC_SOURCES = \
libswc/xkb.c \
libswc/shm.c \
libswc/drm.c \
- libswc/drm_buffer.c \
protocol/wayland-drm-protocol.c \
launch/protocol.c
diff --git a/libswc/surface.c b/libswc/surface.c
@@ -22,11 +22,12 @@
*/
#include "surface.h"
-#include "drm_buffer.h"
+#include "buffer.h"
#include "event.h"
#include "region.h"
#include "util.h"
#include "view.h"
+#include "wayland_buffer.h"
#include <stdlib.h>
#include <stdio.h>
@@ -234,8 +235,7 @@ static void commit(struct wl_client * client, struct wl_resource * resource)
/* Attach */
if (surface->pending.commit & SWC_SURFACE_COMMIT_ATTACH)
{
- struct wl_shm_buffer * shm_buffer;
- struct swc_drm_buffer * drm_buffer;
+ struct swc_buffer * buffer;
if (surface->state.buffer
&& surface->state.buffer != surface->pending.state.buffer)
@@ -248,16 +248,8 @@ static void commit(struct wl_client * client, struct wl_resource * resource)
/* Determine size of buffer. */
if (surface->state.buffer)
{
- if ((shm_buffer = wl_shm_buffer_get(surface->state.buffer)))
- {
- set_size(surface, wl_shm_buffer_get_width(shm_buffer),
- wl_shm_buffer_get_height(shm_buffer));
- }
- else if ((drm_buffer = swc_drm_buffer_get(surface->state.buffer)))
- {
- set_size(surface,
- drm_buffer->wld->width, drm_buffer->wld->height);
- }
+ if ((buffer = swc_wayland_buffer_get(surface->state.buffer)))
+ set_size(surface, buffer->wld->width, buffer->wld->height);
else
WARNING("Unknown buffer type attached\n");
}
diff --git a/libswc/wayland_buffer.c b/libswc/wayland_buffer.c
@@ -0,0 +1,176 @@
+/* swc: libswc/wayland_buffer.c
+ *
+ * Copyright (c) 2013 Michael Forney
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "wayland_buffer.h"
+#include "buffer.h"
+#include "internal.h"
+#include "shm.h"
+#include "util.h"
+
+#include <wld/wld.h>
+#include <wld/pixman.h>
+
+struct wayland_buffer
+{
+ struct swc_buffer base;
+ struct wl_resource * resource;
+ struct wl_listener destroy_listener;
+};
+
+static void destroy(struct wl_client * client, struct wl_resource * resource)
+{
+ wl_resource_destroy(resource);
+}
+
+static const struct wl_buffer_interface buffer_implementation = {
+ .destroy = &destroy
+};
+
+/* NOTE: Needed because the implementation for SHM buffers comes from
+ * libwayland-server. */
+static void handle_buffer_destroy(struct wl_listener * listener, void * data)
+{
+ struct wayland_buffer * buffer
+ = CONTAINER_OF(listener, typeof(*buffer), destroy_listener);
+
+ swc_buffer_finalize(&buffer->base);
+}
+
+static inline uint32_t format_shm_to_wld(uint32_t format)
+{
+ switch (format)
+ {
+ case WL_SHM_FORMAT_ARGB8888:
+ return WLD_FORMAT_ARGB8888;
+ case WL_SHM_FORMAT_XRGB8888:
+ return WLD_FORMAT_XRGB8888;
+ default:
+ return format;
+ }
+}
+
+struct swc_buffer * swc_wayland_buffer_get(struct wl_resource * resource)
+{
+ struct wayland_buffer * buffer;
+
+ if (wl_resource_instance_of(resource, &wl_buffer_interface,
+ &buffer_implementation))
+ {
+ buffer = wl_resource_get_user_data(resource);
+ }
+ else
+ {
+ struct wl_listener * listener;
+
+ listener = wl_resource_get_destroy_listener(resource,
+ &handle_buffer_destroy);
+
+ if (listener)
+ buffer = CONTAINER_OF(listener, typeof(*buffer), destroy_listener);
+ else
+ {
+ struct wl_shm_buffer * shm_buffer;
+ struct wld_buffer * wld = NULL;
+
+ if ((shm_buffer = wl_shm_buffer_get(resource)))
+ {
+ union wld_object object = {
+ .ptr = wl_shm_buffer_get_data(shm_buffer)
+ };
+
+ wld = wld_import_buffer
+ (swc.shm->context, WLD_OBJECT_DATA, object,
+ wl_shm_buffer_get_width(shm_buffer),
+ wl_shm_buffer_get_height(shm_buffer),
+ format_shm_to_wld(wl_shm_buffer_get_format(shm_buffer)),
+ wl_shm_buffer_get_stride(shm_buffer));
+ }
+
+ if (!wld)
+ goto error0;
+
+ if (!(buffer = malloc(sizeof *buffer)))
+ goto error0;
+
+ if (!swc_buffer_initialize(&buffer->base, wld))
+ goto error1;
+
+ buffer->resource = resource;
+ buffer->destroy_listener.notify = &handle_buffer_destroy;
+ wl_resource_add_destroy_listener(resource,
+ &buffer->destroy_listener);
+ }
+ }
+
+ return &buffer->base;
+
+ error1:
+ free(buffer);
+ error0:
+ return NULL;
+}
+
+static void destroy_buffer(struct wl_resource * resource)
+{
+ struct wayland_buffer * buffer = wl_resource_get_user_data(resource);
+
+ swc_buffer_finalize(&buffer->base);
+}
+
+struct swc_buffer * swc_wayland_buffer_new
+ (struct wl_client * client, uint32_t id, struct wld_buffer * wld)
+{
+ struct wayland_buffer * buffer;
+
+ buffer = malloc(sizeof *buffer);
+
+ if (!buffer)
+ goto error0;
+
+ if (!swc_buffer_initialize(&buffer->base, wld))
+ goto error1;
+
+ buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
+
+ if (!buffer->resource)
+ goto error2;
+
+ wl_resource_set_implementation(buffer->resource, &buffer_implementation,
+ buffer, &destroy_buffer);
+
+ return &buffer->base;
+
+ error2:
+ wl_client_post_no_memory(client);
+ swc_buffer_finalize(&buffer->base);
+ error1:
+ free(buffer);
+ error0:
+ return NULL;
+}
+
+void swc_wayland_buffer_release(struct swc_buffer * buffer)
+{
+ wl_buffer_send_release(((struct wayland_buffer *) buffer)->resource);
+}
+
diff --git a/libswc/wayland_buffer.h b/libswc/wayland_buffer.h
@@ -0,0 +1,41 @@
+/* swc: libswc/wayland_buffer.h
+ *
+ * Copyright (c) 2013 Michael Forney
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef SWC_WAYLAND_BUFFER_H
+#define SWC_WAYLAND_BUFFER_H
+
+#include <stdint.h>
+
+struct wl_client;
+struct wl_resource;
+struct wld_buffer;
+
+struct swc_buffer * swc_wayland_buffer_get(struct wl_resource * resource);
+
+struct swc_buffer * swc_wayland_buffer_new
+ (struct wl_client * client, uint32_t id, struct wld_buffer * buffer);
+
+void swc_wayland_buffer_release(struct swc_buffer * buffer);
+
+#endif
+