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