|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Red Hat Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License as |
| 6 | + * published by the Free Software Foundation; either version 2 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but |
| 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 17 | + * 02111-1307, USA. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "config.h" |
| 22 | + |
| 23 | +#include "wayland/meta-wayland-single-pixel-buffer.h" |
| 24 | + |
| 25 | +#include "backends/meta-backend-private.h" |
| 26 | +#include "wayland/meta-wayland-buffer.h" |
| 27 | +#include "wayland/meta-wayland-private.h" |
| 28 | + |
| 29 | +#include "single-pixel-buffer-v1-server-protocol.h" |
| 30 | + |
| 31 | +struct _MetaWaylandSinglePixelBuffer |
| 32 | +{ |
| 33 | + uint32_t r; |
| 34 | + uint32_t g; |
| 35 | + uint32_t b; |
| 36 | + uint32_t a; |
| 37 | +}; |
| 38 | + |
| 39 | +static void |
| 40 | +buffer_destroy (struct wl_client *client, |
| 41 | + struct wl_resource *resource) |
| 42 | +{ |
| 43 | + wl_resource_destroy (resource); |
| 44 | +} |
| 45 | + |
| 46 | +static const struct wl_buffer_interface single_pixel_buffer_implementation = |
| 47 | +{ |
| 48 | + buffer_destroy, |
| 49 | +}; |
| 50 | + |
| 51 | +static void |
| 52 | +single_pixel_buffer_manager_destroy (struct wl_client *client, |
| 53 | + struct wl_resource *resource) |
| 54 | +{ |
| 55 | + wl_resource_destroy (resource); |
| 56 | +} |
| 57 | + |
| 58 | +static void |
| 59 | +single_pixel_buffer_manager_create_1px_rgba32_buffer (struct wl_client *client, |
| 60 | + struct wl_resource *resource, |
| 61 | + uint32_t buffer_id, |
| 62 | + uint32_t r, |
| 63 | + uint32_t g, |
| 64 | + uint32_t b, |
| 65 | + uint32_t a) |
| 66 | +{ |
| 67 | + MetaWaylandSinglePixelBuffer *single_pixel_buffer; |
| 68 | + struct wl_resource *buffer_resource; |
| 69 | + |
| 70 | + single_pixel_buffer = g_new0 (MetaWaylandSinglePixelBuffer, 1); |
| 71 | + single_pixel_buffer->r = r; |
| 72 | + single_pixel_buffer->g = g; |
| 73 | + single_pixel_buffer->b = b; |
| 74 | + single_pixel_buffer->a = a; |
| 75 | + |
| 76 | + buffer_resource = |
| 77 | + wl_resource_create (client, &wl_buffer_interface, 1, buffer_id); |
| 78 | + wl_resource_set_implementation (buffer_resource, |
| 79 | + &single_pixel_buffer_implementation, |
| 80 | + single_pixel_buffer, NULL); |
| 81 | + meta_wayland_buffer_from_resource (buffer_resource); |
| 82 | +} |
| 83 | + |
| 84 | +static const struct wp_single_pixel_buffer_manager_v1_interface |
| 85 | + single_pixel_buffer_manager_implementation = |
| 86 | +{ |
| 87 | + single_pixel_buffer_manager_destroy, |
| 88 | + single_pixel_buffer_manager_create_1px_rgba32_buffer, |
| 89 | +}; |
| 90 | + |
| 91 | +static void |
| 92 | +single_pixel_buffer_manager_bind (struct wl_client *client, |
| 93 | + void *user_data, |
| 94 | + uint32_t version, |
| 95 | + uint32_t id) |
| 96 | +{ |
| 97 | + MetaWaylandCompositor *compositor = user_data; |
| 98 | + struct wl_resource *resource; |
| 99 | + |
| 100 | + resource = wl_resource_create (client, |
| 101 | + &wp_single_pixel_buffer_manager_v1_interface, |
| 102 | + version, id); |
| 103 | + wl_resource_set_implementation (resource, |
| 104 | + &single_pixel_buffer_manager_implementation, |
| 105 | + compositor, NULL); |
| 106 | +} |
| 107 | + |
| 108 | +gboolean |
| 109 | +meta_wayland_single_pixel_buffer_attach (MetaWaylandBuffer *buffer, |
| 110 | + CoglTexture **texture, |
| 111 | + GError **error) |
| 112 | +{ |
| 113 | + MetaBackend *backend = meta_get_backend (); |
| 114 | + ClutterBackend *clutter_backend = meta_backend_get_clutter_backend (backend); |
| 115 | + CoglContext *cogl_context = |
| 116 | + clutter_backend_get_cogl_context (clutter_backend); |
| 117 | + MetaWaylandSinglePixelBuffer *single_pixel_buffer = |
| 118 | + wl_resource_get_user_data (buffer->resource); |
| 119 | + uint8_t data[4]; |
| 120 | + CoglPixelFormat pixel_format; |
| 121 | + CoglTexture2D *tex_2d; |
| 122 | + |
| 123 | + if (buffer->single_pixel.texture) |
| 124 | + { |
| 125 | + *texture = g_object_ref (buffer->single_pixel.texture); |
| 126 | + return TRUE; |
| 127 | + } |
| 128 | + |
| 129 | + data[0] = single_pixel_buffer->b / (UINT32_MAX / 0xff); |
| 130 | + data[1] = single_pixel_buffer->g / (UINT32_MAX / 0xff); |
| 131 | + data[2] = single_pixel_buffer->r / (UINT32_MAX / 0xff); |
| 132 | + data[3] = single_pixel_buffer->a / (UINT32_MAX / 0xff); |
| 133 | + |
| 134 | + if (data[3] == UINT8_MAX) |
| 135 | + pixel_format = COGL_PIXEL_FORMAT_BGR_888; |
| 136 | + else |
| 137 | + pixel_format = COGL_PIXEL_FORMAT_BGRA_8888_PRE; |
| 138 | + |
| 139 | + tex_2d = cogl_texture_2d_new_from_data (cogl_context, |
| 140 | + 1, 1, |
| 141 | + pixel_format, |
| 142 | + 4, data, |
| 143 | + error); |
| 144 | + if (!tex_2d) |
| 145 | + return FALSE; |
| 146 | + |
| 147 | + buffer->single_pixel.texture = COGL_TEXTURE (tex_2d); |
| 148 | + |
| 149 | + cogl_clear_object (texture); |
| 150 | + *texture = cogl_object_ref (buffer->single_pixel.texture); |
| 151 | + return TRUE; |
| 152 | +} |
| 153 | + |
| 154 | +MetaWaylandSinglePixelBuffer * |
| 155 | +meta_wayland_single_pixel_buffer_from_buffer (MetaWaylandBuffer *buffer) |
| 156 | +{ |
| 157 | + if (!buffer->resource) |
| 158 | + return NULL; |
| 159 | + |
| 160 | + if (wl_resource_instance_of (buffer->resource, &wl_buffer_interface, |
| 161 | + &single_pixel_buffer_implementation)) |
| 162 | + return wl_resource_get_user_data (buffer->resource); |
| 163 | + |
| 164 | + return NULL; |
| 165 | +} |
| 166 | + |
| 167 | +void |
| 168 | +meta_wayland_single_pixel_buffer_free (MetaWaylandSinglePixelBuffer *single_pixel_buffer) |
| 169 | +{ |
| 170 | + g_free (single_pixel_buffer); |
| 171 | +} |
| 172 | + |
| 173 | +void |
| 174 | +meta_wayland_init_single_pixel_buffer_manager (MetaWaylandCompositor *compositor) |
| 175 | +{ |
| 176 | + if (!wl_global_create (compositor->wayland_display, |
| 177 | + &wp_single_pixel_buffer_manager_v1_interface, |
| 178 | + META_WP_SINGLE_PIXEL_BUFFER_V1_VERSION, |
| 179 | + compositor, |
| 180 | + single_pixel_buffer_manager_bind)) |
| 181 | + g_warning ("Failed to create wp_single_pixel_buffer_manager_v1 global"); |
| 182 | +} |
0 commit comments