Alexandros Frantzis | 27d7c39 | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2018 Collabora, Ltd. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <inttypes.h> |
| 29 | |
| 30 | #include "compositor.h" |
| 31 | #include "linux-explicit-synchronization.h" |
| 32 | #include "linux-explicit-synchronization-unstable-v1-server-protocol.h" |
| 33 | |
| 34 | static void |
| 35 | destroy_linux_surface_synchronization(struct wl_resource *resource) |
| 36 | { |
| 37 | struct weston_surface *surface = |
| 38 | wl_resource_get_user_data(resource); |
| 39 | |
| 40 | if (surface) |
| 41 | surface->synchronization_resource = NULL; |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | linux_surface_synchronization_destroy(struct wl_client *client, |
| 46 | struct wl_resource *resource) |
| 47 | { |
| 48 | wl_resource_destroy(resource); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | linux_surface_synchronization_set_acquire_fence(struct wl_client *client, |
| 53 | struct wl_resource *resource, |
| 54 | int32_t fd) |
| 55 | { |
| 56 | wl_client_post_no_memory(client); |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | linux_surface_synchronization_get_release(struct wl_client *client, |
| 61 | struct wl_resource *resource, |
| 62 | uint32_t id) |
| 63 | { |
| 64 | wl_client_post_no_memory(client); |
| 65 | } |
| 66 | |
| 67 | const struct zwp_linux_surface_synchronization_v1_interface |
| 68 | linux_surface_synchronization_implementation = { |
| 69 | linux_surface_synchronization_destroy, |
| 70 | linux_surface_synchronization_set_acquire_fence, |
| 71 | linux_surface_synchronization_get_release, |
| 72 | }; |
| 73 | |
| 74 | static void |
| 75 | linux_explicit_synchronization_destroy(struct wl_client *client, |
| 76 | struct wl_resource *resource) |
| 77 | { |
| 78 | wl_resource_destroy(resource); |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | linux_explicit_synchronization_get_synchronization(struct wl_client *client, |
| 83 | struct wl_resource *resource, |
| 84 | uint32_t id, |
| 85 | struct wl_resource *surface_resource) |
| 86 | { |
| 87 | struct weston_surface *surface = |
| 88 | wl_resource_get_user_data(surface_resource); |
| 89 | |
| 90 | if (surface->synchronization_resource) { |
| 91 | wl_resource_post_error( |
| 92 | resource, |
| 93 | ZWP_LINUX_EXPLICIT_SYNCHRONIZATION_V1_ERROR_SYNCHRONIZATION_EXISTS, |
| 94 | "wl_surface@%"PRIu32" already has a synchronization object", |
| 95 | wl_resource_get_id(surface_resource)); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | surface->synchronization_resource = |
| 100 | wl_resource_create(client, |
| 101 | &zwp_linux_surface_synchronization_v1_interface, |
| 102 | wl_resource_get_version(resource), id); |
| 103 | if (!surface->synchronization_resource) { |
| 104 | wl_client_post_no_memory(client); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | wl_resource_set_implementation(surface->synchronization_resource, |
| 109 | &linux_surface_synchronization_implementation, |
| 110 | surface, |
| 111 | destroy_linux_surface_synchronization); |
| 112 | } |
| 113 | |
| 114 | static const struct zwp_linux_explicit_synchronization_v1_interface |
| 115 | linux_explicit_synchronization_implementation = { |
| 116 | linux_explicit_synchronization_destroy, |
| 117 | linux_explicit_synchronization_get_synchronization |
| 118 | }; |
| 119 | |
| 120 | static void |
| 121 | bind_linux_explicit_synchronization(struct wl_client *client, |
| 122 | void *data, uint32_t version, |
| 123 | uint32_t id) |
| 124 | { |
| 125 | struct weston_compositor *compositor = data; |
| 126 | struct wl_resource *resource; |
| 127 | |
| 128 | resource = wl_resource_create(client, |
| 129 | &zwp_linux_explicit_synchronization_v1_interface, |
| 130 | version, id); |
| 131 | if (resource == NULL) { |
| 132 | wl_client_post_no_memory(client); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | wl_resource_set_implementation(resource, |
| 137 | &linux_explicit_synchronization_implementation, |
| 138 | compositor, NULL); |
| 139 | } |
| 140 | |
| 141 | /** Advertise linux_explicit_synchronization support |
| 142 | * |
| 143 | * Calling this initializes the zwp_linux_explicit_synchronization_v1 |
| 144 | * protocol support, so that the interface will be advertised to clients. |
| 145 | * Essentially it creates a global. Do not call this function multiple times |
| 146 | * in the compositor's lifetime. There is no way to deinit explicitly, globals |
| 147 | * will be reaped when the wl_display gets destroyed. |
| 148 | * |
| 149 | * \param compositor The compositor to init for. |
| 150 | * \return Zero on success, -1 on failure. |
| 151 | */ |
| 152 | WL_EXPORT int |
| 153 | linux_explicit_synchronization_setup(struct weston_compositor *compositor) |
| 154 | { |
| 155 | /* TODO: Update to minor version 2 when the next version of |
| 156 | * wayland-protocols that contains it is released. */ |
| 157 | if (!wl_global_create(compositor->wl_display, |
| 158 | &zwp_linux_explicit_synchronization_v1_interface, |
| 159 | 1, compositor, |
| 160 | bind_linux_explicit_synchronization)) |
| 161 | return -1; |
| 162 | |
| 163 | return 0; |
| 164 | } |