blob: d732b1657952d56625cd8835894e2d0cb6e33f37 [file] [log] [blame]
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +05301/*
2 * Copyright © 2019 Intel Corporation
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#include <stdint.h>
28#include <stdlib.h>
29#include <assert.h>
30#include <signal.h>
31#include <unistd.h>
32#include <string.h>
33#include <libweston/libweston.h>
Marius Vladc901e892019-06-21 22:49:18 +030034#include <libweston/weston-log.h>
Guillaume Champagnef1e8fc92020-01-27 20:14:29 -050035#include "libweston-internal.h"
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +053036#include "weston-content-protection-server-protocol.h"
37#include "shared/helpers.h"
38#include "shared/timespec-util.h"
39
40#define content_protection_log(cp, ...) \
41 weston_log_scope_printf((cp)->debug, __VA_ARGS__)
42
43static const char * const content_type_name [] = {
44 [WESTON_PROTECTED_SURFACE_TYPE_UNPROTECTED] = "UNPROTECTED",
45 [WESTON_PROTECTED_SURFACE_TYPE_HDCP_0] = "TYPE-0",
46 [WESTON_PROTECTED_SURFACE_TYPE_HDCP_1] = "TYPE-1",
47};
48
49void
50weston_protected_surface_send_event(struct protected_surface *psurface,
51 enum weston_hdcp_protection protection)
52{
53 struct wl_resource *p_resource;
54 enum weston_protected_surface_type protection_type;
55 struct content_protection *cp;
56 struct wl_resource *surface_resource;
57
58 p_resource = psurface->protection_resource;
59 if (!p_resource)
60 return;
61 /* No event to be sent to client, in case of enforced mode */
62 if (psurface->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED)
63 return;
64 protection_type = (enum weston_protected_surface_type) protection;
65 weston_protected_surface_send_status(p_resource, protection_type);
66
67 cp = psurface->cp_backptr;
68 surface_resource = psurface->surface->resource;
69 content_protection_log(cp, "wl_surface@%"PRIu32" Protection type set to %s\n",
70 wl_resource_get_id(surface_resource),
71 content_type_name[protection_type]);
72}
73
74static void
75set_type(struct wl_client *client, struct wl_resource *resource,
76 enum weston_protected_surface_type content_type)
77{
78 struct content_protection *cp;
79 struct protected_surface *psurface;
80 enum weston_hdcp_protection weston_cp;
81 struct wl_resource *surface_resource;
82
83 psurface = wl_resource_get_user_data(resource);
84 if (!psurface)
85 return;
86 cp = psurface->cp_backptr;
87 surface_resource = psurface->surface->resource;
88
89 if (content_type < WESTON_PROTECTED_SURFACE_TYPE_UNPROTECTED ||
90 content_type > WESTON_PROTECTED_SURFACE_TYPE_HDCP_1) {
91 wl_resource_post_error(resource,
92 WESTON_PROTECTED_SURFACE_ERROR_INVALID_TYPE,
93 "wl_surface@%"PRIu32" Invalid content-type %d for request:set_type\n",
94 wl_resource_get_id(surface_resource), content_type);
95
Maxime Roussin-Bélanger1d009c22020-12-17 17:10:13 -050096 content_protection_log(cp, "wl_surface@%"PRIu32" Invalid content-type %d for request:set_type\n",
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +053097 wl_resource_get_id(surface_resource), content_type);
98 return;
99 }
100
101 content_protection_log(cp, "wl_surface@%"PRIu32" Request: Enable Content-Protection Type: %s\n",
102 wl_resource_get_id(surface_resource),
103 content_type_name[content_type]);
104
105 weston_cp = (enum weston_hdcp_protection) content_type;
106 psurface->surface->pending.desired_protection = weston_cp;
107}
108
109static void
110protected_surface_destroy(struct wl_client *client, struct wl_resource *resource)
111{
112 struct protected_surface *psurface;
113
114 psurface = wl_resource_get_user_data(resource);
115 if (!psurface)
116 return;
117 psurface->surface->pending.desired_protection = WESTON_HDCP_DISABLE;
118}
119
120static void
121set_enforce_mode(struct wl_client *client, struct wl_resource *resource)
122{
123 /*
124 * Enforce Censored-Visibility. Compositor censors the protected
125 * surface on an unsecured output.
126 * In case of a surface, being shown on an unprotected output, the
127 * compositor hides the surface, not allowing it to be displayed on
128 * the unprotected output, without bothering the client. No difference
129 * for the protected outputs.
130 *
131 * The member 'protection_mode' is "double-buffered", so setting it in
132 * pending_state will cause the setting of the corresponding
133 * 'protection_mode' in weston_surface, after the commit.
134 *
135 * This function sets the 'protection_mode' of the weston_surface_state
136 * to 'enfoced'. The renderers inspect the flag and compare the
137 * desired_protection of the surface, to the current_protection of the
138 * output, based on that the real surface or a place-holder content,
139 * (e.g. solid color) are shown.
140 */
141
142 struct protected_surface *psurface;
143
144 psurface = wl_resource_get_user_data(resource);
145 if (!psurface)
146 return;
147
148 psurface->surface->pending.protection_mode =
149 WESTON_SURFACE_PROTECTION_MODE_ENFORCED;
150}
151
152static void
153set_relax_mode(struct wl_client *client, struct wl_resource *resource)
154{
155 /*
156 * Relaxed mode. By default this mode will be activated.
157 * In case of a surface, being shown in unprotected output,
158 * compositor just sends the event for protection status changed.
159 *
160 * On setting the relaxed mode, the 'protection_mode' member is queued
161 * to be set to 'relax' from the existing 'enforce' mode.
162 */
163
164 struct protected_surface *psurface;
165
166 psurface = wl_resource_get_user_data(resource);
167 if (!psurface)
168 return;
169
170 psurface->surface->pending.protection_mode =
171 WESTON_SURFACE_PROTECTION_MODE_RELAXED;
172}
173
174static const struct weston_protected_surface_interface protected_surface_implementation = {
175 protected_surface_destroy,
176 set_type,
177 set_enforce_mode,
178 set_relax_mode,
179};
180
181static void
182cp_destroy_listener(struct wl_listener *listener, void *data)
183{
184 struct content_protection *cp;
185
186 cp = container_of(listener, struct content_protection,
187 destroy_listener);
188 wl_list_remove(&cp->destroy_listener.link);
189 wl_list_remove(&cp->protected_list);
Leandro Ribeirof0149642019-12-18 15:52:18 -0300190 weston_log_scope_destroy(cp->debug);
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530191 cp->debug = NULL;
Daniel Stone61851f52021-12-06 16:59:26 +0000192 if (cp->surface_protection_update)
193 wl_event_source_remove(cp->surface_protection_update);
Ankit Nautiyalf74c35b2019-04-03 19:21:58 +0530194 cp->surface_protection_update = NULL;
Marius Vladf722dc62021-05-12 17:04:28 +0300195 cp->compositor->content_protection = NULL;
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530196 free(cp);
197}
198
199static void
200free_protected_surface(struct protected_surface *psurface)
201{
202 psurface->surface->pending.desired_protection = WESTON_HDCP_DISABLE;
203 wl_resource_set_user_data(psurface->protection_resource, NULL);
204 wl_list_remove(&psurface->surface_destroy_listener.link);
205 wl_list_remove(&psurface->link);
206 free(psurface);
207}
208
209static void
210surface_destroyed(struct wl_listener *listener, void *data)
211{
212 struct protected_surface *psurface;
213
214 psurface = container_of(listener, struct protected_surface,
215 surface_destroy_listener);
216 free_protected_surface(psurface);
217}
218
219static void
220destroy_protected_surface(struct wl_resource *resource)
221{
222 struct protected_surface *psurface;
223
224 psurface = wl_resource_get_user_data(resource);
225 if (!psurface)
226 return;
227 free_protected_surface(psurface);
228}
229
230static void
231get_protection(struct wl_client *client, struct wl_resource *cp_resource,
232 uint32_t id, struct wl_resource *surface_resource)
233{
234 struct wl_resource *resource;
235 struct weston_surface *surface;
236 struct content_protection *cp;
237 struct protected_surface *psurface;
238 struct wl_listener *listener;
239
240 surface = wl_resource_get_user_data(surface_resource);
241 assert(surface);
242 cp = wl_resource_get_user_data(cp_resource);
243 assert(cp);
244
245 /*
246 * Check if this client has a corresponding protected-surface
247 */
248
249 listener = wl_resource_get_destroy_listener(surface->resource,
250 surface_destroyed);
251
252 if (listener) {
253 wl_resource_post_error(cp_resource,
254 WESTON_CONTENT_PROTECTION_ERROR_SURFACE_EXISTS,
255 "wl_surface@%"PRIu32" Protection already exists",
256 wl_resource_get_id(surface_resource));
257 return;
258 }
259
260 psurface = zalloc(sizeof(struct protected_surface));
261 if (!psurface) {
262 wl_client_post_no_memory(client);
263 return;
264 }
265 psurface->cp_backptr = cp;
266 resource = wl_resource_create(client, &weston_protected_surface_interface,
267 1, id);
268 if (!resource) {
269 free(psurface);
270 wl_client_post_no_memory(client);
271 return;
272 }
273
274 wl_list_insert(&cp->protected_list, &psurface->link);
275 wl_resource_set_implementation(resource, &protected_surface_implementation,
276 psurface,
277 destroy_protected_surface);
278
279 psurface->protection_resource = resource;
280 psurface->surface = surface;
281 psurface->surface_destroy_listener.notify = surface_destroyed;
282 wl_resource_add_destroy_listener(surface->resource,
283 &psurface->surface_destroy_listener);
284 weston_protected_surface_send_event(psurface,
285 psurface->surface->current_protection);
286}
287
288static void
289destroy_protection(struct wl_client *client, struct wl_resource *cp_resource)
290{
291 wl_resource_destroy(cp_resource);
292}
293
294static const
295struct weston_content_protection_interface content_protection_implementation = {
296 destroy_protection,
297 get_protection,
298};
299
300static void
301bind_weston_content_protection(struct wl_client *client, void *data,
302 uint32_t version, uint32_t id)
303{
304 struct content_protection *cp = data;
305 struct wl_resource *resource;
306
307 resource = wl_resource_create(client,
308 &weston_content_protection_interface,
309 1, id);
310 if (!resource) {
311 wl_client_post_no_memory(client);
312 return;
313 }
314
315 wl_resource_set_implementation(resource,
316 &content_protection_implementation,
317 cp, NULL);
318}
319/* Advertise the content-protection support.
320 *
321 * Calling this function sets up the content-protection support via HDCP.
322 * This exposes the global interface, visible to the client, enabling them to
323 * request for content-protection for their surfaces according to the type of
324 * content.
325 */
326
327WL_EXPORT int
328weston_compositor_enable_content_protection(struct weston_compositor *compositor)
329{
330 struct content_protection *cp;
331
332 cp = zalloc(sizeof(*cp));
333 if (cp == NULL)
334 return -1;
335 cp->compositor = compositor;
336
337 compositor->content_protection = cp;
338 wl_list_init(&cp->protected_list);
339 if (wl_global_create(compositor->wl_display,
340 &weston_content_protection_interface, 1, cp,
341 bind_weston_content_protection) == NULL)
342 return -1;
343
344 cp->destroy_listener.notify = cp_destroy_listener;
345 wl_signal_add(&compositor->destroy_signal, &cp->destroy_listener);
Leandro Ribeirofa505c52019-12-26 16:41:09 -0300346 cp->debug = weston_compositor_add_log_scope(compositor, "content-protection-debug",
347 "debug-logs for content-protection",
348 NULL, NULL, NULL);
Ankit Nautiyal5cfe03c2019-03-28 15:05:42 +0530349 return 0;
350}