commit cbef704
Michael Forney
·
2014-01-16 10:55:16 +0000 UTC
parent d6cfcbf
Use common buffer struct
9 files changed,
+284,
-108
+41,
-0
1@@ -0,0 +1,41 @@
2+/* swc: libswc/buffer.c
3+ *
4+ * Copyright (c) 2013 Michael Forney
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
25+#include "buffer.h"
26+
27+#include <wld/wld.h>
28+
29+bool swc_buffer_initialize(struct swc_buffer * buffer, struct wld_buffer * wld)
30+{
31+ buffer->wld = wld;
32+ wl_signal_init(&buffer->destroy_signal);
33+
34+ return true;
35+}
36+
37+void swc_buffer_finalize(struct swc_buffer * buffer)
38+{
39+ wl_signal_emit(&buffer->destroy_signal, buffer);
40+ wld_destroy_buffer(buffer->wld);
41+}
42+
+42,
-0
1@@ -0,0 +1,42 @@
2+/* swc: libswc/buffer.h
3+ *
4+ * Copyright (c) 2013 Michael Forney
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
25+#ifndef SWC_BUFFER_H
26+#define SWC_BUFFER_H
27+
28+#include <stdbool.h>
29+#include <wayland-server.h>
30+
31+struct swc_buffer
32+{
33+ struct wld_buffer * wld;
34+ struct wl_signal destroy_signal;
35+};
36+
37+bool swc_buffer_initialize(struct swc_buffer * buffer,
38+ struct wld_buffer * wld);
39+
40+void swc_buffer_finalize(struct swc_buffer * buffer);
41+
42+#endif
43+
+4,
-3
1@@ -1,8 +1,8 @@
2 #include "swc.h"
3+#include "buffer.h"
4 #include "compositor.h"
5 #include "data_device_manager.h"
6 #include "drm.h"
7-#include "drm_buffer.h"
8 #include "internal.h"
9 #include "output.h"
10 #include "pointer.h"
11@@ -11,6 +11,7 @@
12 #include "surface.h"
13 #include "util.h"
14 #include "view.h"
15+#include "wayland_buffer.h"
16
17 #include <stdlib.h>
18 #include <stdio.h>
19@@ -168,7 +169,7 @@ static void renderer_attach(struct swc_surface * surface,
20 struct wl_resource * resource)
21 {
22 struct buffer_state * state;
23- struct swc_drm_buffer * drm_buffer;
24+ struct swc_buffer * drm_buffer;
25
26 if (!resource)
27 return;
28@@ -180,7 +181,7 @@ static void renderer_attach(struct swc_surface * surface,
29 if (!(state = malloc(sizeof *state)))
30 return;
31
32- if ((drm_buffer = swc_drm_buffer_get(resource)))
33+ if ((drm_buffer = swc_wayland_buffer_get(resource)))
34 {
35 if (!(state = malloc(sizeof *state)))
36 return;
+5,
-5
1@@ -22,10 +22,10 @@
2 */
3
4 #include "drm.h"
5-#include "drm_buffer.h"
6 #include "event.h"
7 #include "internal.h"
8 #include "output.h"
9+#include "wayland_buffer.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13@@ -73,7 +73,7 @@ static void create_buffer(struct wl_client * client,
14 uint32_t stride, uint32_t format)
15 {
16 struct wld_buffer * wld;
17- struct swc_drm_buffer * buffer;
18+ struct swc_buffer * buffer;
19 union wld_object object = { .u32 = name };
20
21 wld = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_GEM_NAME, object,
22@@ -82,7 +82,7 @@ static void create_buffer(struct wl_client * client,
23 if (!wld)
24 goto error0;
25
26- buffer = swc_drm_buffer_new(client, id, wld);
27+ buffer = swc_wayland_buffer_new(client, id, wld);
28
29 if (!buffer)
30 goto error1;
31@@ -116,7 +116,7 @@ static void create_prime_buffer(struct wl_client * client,
32 int32_t offset2, int32_t stride2)
33 {
34 struct wld_buffer * wld;
35- struct swc_drm_buffer * buffer;
36+ struct swc_buffer * buffer;
37 union wld_object object = { .i = fd };
38
39 wld = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_PRIME_FD, object,
40@@ -126,7 +126,7 @@ static void create_prime_buffer(struct wl_client * client,
41 if (!wld)
42 goto error0;
43
44- buffer = swc_drm_buffer_new(client, id, wld);
45+ buffer = swc_wayland_buffer_new(client, id, wld);
46
47 if (!buffer)
48 goto error1;
+0,
-75
1@@ -1,75 +0,0 @@
2-/* swc: drm_buffer.c
3- *
4- * Copyright (c) 2013 Michael Forney
5- *
6- * Permission is hereby granted, free of charge, to any person obtaining a copy
7- * of this software and associated documentation files (the "Software"), to deal
8- * in the Software without restriction, including without limitation the rights
9- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10- * copies of the Software, and to permit persons to whom the Software is
11- * furnished to do so, subject to the following conditions:
12- *
13- * The above copyright notice and this permission notice shall be included in
14- * all copies or substantial portions of the Software.
15- *
16- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22- * SOFTWARE.
23- */
24-
25-#include "drm_buffer.h"
26-
27-#include <stdlib.h>
28-#include <wayland-server.h>
29-#include <wld/wld.h>
30-#include "protocol/wayland-drm-server-protocol.h"
31-
32-static void destroy(struct wl_client * client, struct wl_resource * resource)
33-{
34- wl_resource_destroy(resource);
35-}
36-
37-static const struct wl_buffer_interface drm_buffer_implementation = {
38- .destroy = &destroy
39-};
40-
41-static void buffer_destroy(struct wl_resource * resource)
42-{
43- struct swc_drm_buffer * buffer = wl_resource_get_user_data(resource);
44-
45- free(buffer);
46-}
47-
48-struct swc_drm_buffer * swc_drm_buffer_new
49- (struct wl_client * client, uint32_t id, struct wld_buffer * wld)
50-{
51- struct swc_drm_buffer * buffer;
52-
53- buffer = malloc(sizeof *buffer);
54-
55- if (!buffer)
56- return NULL;
57-
58- buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
59- wl_resource_set_implementation(buffer->resource, &drm_buffer_implementation,
60- buffer, &buffer_destroy);
61- buffer->wld = wld;
62-
63- return buffer;
64-}
65-
66-struct swc_drm_buffer * swc_drm_buffer_get(struct wl_resource * resource)
67-{
68- if (wl_resource_instance_of(resource, &wl_buffer_interface,
69- &drm_buffer_implementation))
70- {
71- return wl_resource_get_user_data(resource);
72- }
73-
74- return NULL;
75-}
76-
+2,
-1
1@@ -31,6 +31,8 @@ $(dir)_PACKAGES = \
2 SWC_SOURCES = \
3 libswc/compositor.c \
4 libswc/view.c \
5+ libswc/buffer.c \
6+ libswc/wayland_buffer.c \
7 libswc/util.c \
8 libswc/output.c \
9 libswc/plane.c \
10@@ -48,7 +50,6 @@ SWC_SOURCES = \
11 libswc/xkb.c \
12 libswc/shm.c \
13 libswc/drm.c \
14- libswc/drm_buffer.c \
15 protocol/wayland-drm-protocol.c \
16 launch/protocol.c
17
+5,
-13
1@@ -22,11 +22,12 @@
2 */
3
4 #include "surface.h"
5-#include "drm_buffer.h"
6+#include "buffer.h"
7 #include "event.h"
8 #include "region.h"
9 #include "util.h"
10 #include "view.h"
11+#include "wayland_buffer.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15@@ -234,8 +235,7 @@ static void commit(struct wl_client * client, struct wl_resource * resource)
16 /* Attach */
17 if (surface->pending.commit & SWC_SURFACE_COMMIT_ATTACH)
18 {
19- struct wl_shm_buffer * shm_buffer;
20- struct swc_drm_buffer * drm_buffer;
21+ struct swc_buffer * buffer;
22
23 if (surface->state.buffer
24 && surface->state.buffer != surface->pending.state.buffer)
25@@ -248,16 +248,8 @@ static void commit(struct wl_client * client, struct wl_resource * resource)
26 /* Determine size of buffer. */
27 if (surface->state.buffer)
28 {
29- if ((shm_buffer = wl_shm_buffer_get(surface->state.buffer)))
30- {
31- set_size(surface, wl_shm_buffer_get_width(shm_buffer),
32- wl_shm_buffer_get_height(shm_buffer));
33- }
34- else if ((drm_buffer = swc_drm_buffer_get(surface->state.buffer)))
35- {
36- set_size(surface,
37- drm_buffer->wld->width, drm_buffer->wld->height);
38- }
39+ if ((buffer = swc_wayland_buffer_get(surface->state.buffer)))
40+ set_size(surface, buffer->wld->width, buffer->wld->height);
41 else
42 WARNING("Unknown buffer type attached\n");
43 }
+176,
-0
1@@ -0,0 +1,176 @@
2+/* swc: libswc/wayland_buffer.c
3+ *
4+ * Copyright (c) 2013 Michael Forney
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
25+#include "wayland_buffer.h"
26+#include "buffer.h"
27+#include "internal.h"
28+#include "shm.h"
29+#include "util.h"
30+
31+#include <wld/wld.h>
32+#include <wld/pixman.h>
33+
34+struct wayland_buffer
35+{
36+ struct swc_buffer base;
37+ struct wl_resource * resource;
38+ struct wl_listener destroy_listener;
39+};
40+
41+static void destroy(struct wl_client * client, struct wl_resource * resource)
42+{
43+ wl_resource_destroy(resource);
44+}
45+
46+static const struct wl_buffer_interface buffer_implementation = {
47+ .destroy = &destroy
48+};
49+
50+/* NOTE: Needed because the implementation for SHM buffers comes from
51+ * libwayland-server. */
52+static void handle_buffer_destroy(struct wl_listener * listener, void * data)
53+{
54+ struct wayland_buffer * buffer
55+ = CONTAINER_OF(listener, typeof(*buffer), destroy_listener);
56+
57+ swc_buffer_finalize(&buffer->base);
58+}
59+
60+static inline uint32_t format_shm_to_wld(uint32_t format)
61+{
62+ switch (format)
63+ {
64+ case WL_SHM_FORMAT_ARGB8888:
65+ return WLD_FORMAT_ARGB8888;
66+ case WL_SHM_FORMAT_XRGB8888:
67+ return WLD_FORMAT_XRGB8888;
68+ default:
69+ return format;
70+ }
71+}
72+
73+struct swc_buffer * swc_wayland_buffer_get(struct wl_resource * resource)
74+{
75+ struct wayland_buffer * buffer;
76+
77+ if (wl_resource_instance_of(resource, &wl_buffer_interface,
78+ &buffer_implementation))
79+ {
80+ buffer = wl_resource_get_user_data(resource);
81+ }
82+ else
83+ {
84+ struct wl_listener * listener;
85+
86+ listener = wl_resource_get_destroy_listener(resource,
87+ &handle_buffer_destroy);
88+
89+ if (listener)
90+ buffer = CONTAINER_OF(listener, typeof(*buffer), destroy_listener);
91+ else
92+ {
93+ struct wl_shm_buffer * shm_buffer;
94+ struct wld_buffer * wld = NULL;
95+
96+ if ((shm_buffer = wl_shm_buffer_get(resource)))
97+ {
98+ union wld_object object = {
99+ .ptr = wl_shm_buffer_get_data(shm_buffer)
100+ };
101+
102+ wld = wld_import_buffer
103+ (swc.shm->context, WLD_OBJECT_DATA, object,
104+ wl_shm_buffer_get_width(shm_buffer),
105+ wl_shm_buffer_get_height(shm_buffer),
106+ format_shm_to_wld(wl_shm_buffer_get_format(shm_buffer)),
107+ wl_shm_buffer_get_stride(shm_buffer));
108+ }
109+
110+ if (!wld)
111+ goto error0;
112+
113+ if (!(buffer = malloc(sizeof *buffer)))
114+ goto error0;
115+
116+ if (!swc_buffer_initialize(&buffer->base, wld))
117+ goto error1;
118+
119+ buffer->resource = resource;
120+ buffer->destroy_listener.notify = &handle_buffer_destroy;
121+ wl_resource_add_destroy_listener(resource,
122+ &buffer->destroy_listener);
123+ }
124+ }
125+
126+ return &buffer->base;
127+
128+ error1:
129+ free(buffer);
130+ error0:
131+ return NULL;
132+}
133+
134+static void destroy_buffer(struct wl_resource * resource)
135+{
136+ struct wayland_buffer * buffer = wl_resource_get_user_data(resource);
137+
138+ swc_buffer_finalize(&buffer->base);
139+}
140+
141+struct swc_buffer * swc_wayland_buffer_new
142+ (struct wl_client * client, uint32_t id, struct wld_buffer * wld)
143+{
144+ struct wayland_buffer * buffer;
145+
146+ buffer = malloc(sizeof *buffer);
147+
148+ if (!buffer)
149+ goto error0;
150+
151+ if (!swc_buffer_initialize(&buffer->base, wld))
152+ goto error1;
153+
154+ buffer->resource = wl_resource_create(client, &wl_buffer_interface, 1, id);
155+
156+ if (!buffer->resource)
157+ goto error2;
158+
159+ wl_resource_set_implementation(buffer->resource, &buffer_implementation,
160+ buffer, &destroy_buffer);
161+
162+ return &buffer->base;
163+
164+ error2:
165+ wl_client_post_no_memory(client);
166+ swc_buffer_finalize(&buffer->base);
167+ error1:
168+ free(buffer);
169+ error0:
170+ return NULL;
171+}
172+
173+void swc_wayland_buffer_release(struct swc_buffer * buffer)
174+{
175+ wl_buffer_send_release(((struct wayland_buffer *) buffer)->resource);
176+}
177+
R libswc/drm_buffer.h =>
libswc/wayland_buffer.h
+9,
-11
1@@ -1,4 +1,4 @@
2-/* swc: drm_buffer.h
3+/* swc: libswc/wayland_buffer.h
4 *
5 * Copyright (c) 2013 Michael Forney
6 *
7@@ -21,23 +21,21 @@
8 * SOFTWARE.
9 */
10
11-#ifndef SWC_DRM_BUFFER_H
12-#define SWC_DRM_BUFFER_H
13+#ifndef SWC_WAYLAND_BUFFER_H
14+#define SWC_WAYLAND_BUFFER_H
15
16 #include <stdint.h>
17
18 struct wl_client;
19+struct wl_resource;
20+struct wld_buffer;
21
22-struct swc_drm_buffer
23-{
24- struct wl_resource * resource;
25- struct wld_buffer * wld;
26-};
27+struct swc_buffer * swc_wayland_buffer_get(struct wl_resource * resource);
28
29-struct swc_drm_buffer * swc_drm_buffer_new
30- (struct wl_client * client, uint32_t id, struct wld_buffer * wld);
31+struct swc_buffer * swc_wayland_buffer_new
32+ (struct wl_client * client, uint32_t id, struct wld_buffer * buffer);
33
34-struct swc_drm_buffer * swc_drm_buffer_get(struct wl_resource * resource);
35+void swc_wayland_buffer_release(struct swc_buffer * buffer);
36
37 #endif
38