From 5cfe03c8630074a9fac3646dc774ac668c0206e9 Mon Sep 17 00:00:00 2001 From: Ankit Nautiyal Date: Thu, 28 Mar 2019 15:05:42 +0530 Subject: [PATCH] libweston: Add content-protection protocol implementation This patch adds the content-protection protocol implementation, to enable a weston client application to request for content-protection for its content via HDCP. Signed-off-by: Ankit Nautiyal --- include/libweston/libweston.h | 35 +++- libweston/compositor.c | 27 +++ libweston/content-protection.c | 347 +++++++++++++++++++++++++++++++++ libweston/meson.build | 3 + 4 files changed, 411 insertions(+), 1 deletion(-) create mode 100644 libweston/content-protection.c diff --git a/include/libweston/libweston.h b/include/libweston/libweston.h index 620b66c99..04a179185 100644 --- a/include/libweston/libweston.h +++ b/include/libweston/libweston.h @@ -111,7 +111,10 @@ enum weston_mode_aspect_ratio { WESTON_MODE_PIC_AR_256_135 = 4, /* DRM_MODE_PICTURE_ASPECT_256_135*/ }; - +enum weston_surface_protection_mode { + WESTON_SURFACE_PROTECTION_MODE_RELAXED, + WESTON_SURFACE_PROTECTION_MODE_ENFORCED +}; struct weston_mode { uint32_t flags; @@ -1218,6 +1221,8 @@ struct weston_compositor { struct weston_log_context *weston_log_ctx; struct weston_log_scope *debug_scene; + + struct content_protection *content_protection; }; struct weston_buffer { @@ -1442,6 +1447,9 @@ struct weston_surface_state { /* weston_protected_surface.set_type */ enum weston_hdcp_protection desired_protection; + + /* weston_protected_surface.enforced/relaxed */ + enum weston_surface_protection_mode protection_mode; }; struct weston_surface_activation_data { @@ -1573,6 +1581,8 @@ struct weston_surface { struct weston_buffer_release_reference buffer_release_ref; enum weston_hdcp_protection desired_protection; + enum weston_hdcp_protection current_protection; + enum weston_surface_protection_mode protection_mode; }; struct weston_subsurface { @@ -1607,6 +1617,22 @@ struct weston_subsurface { struct wl_list unused_views; }; +struct protected_surface { + struct weston_surface *surface; + struct wl_listener surface_destroy_listener; + struct wl_list link; + struct wl_resource *protection_resource; + struct content_protection *cp_backptr; +}; + +struct content_protection { + struct weston_compositor *compositor; + struct wl_listener destroy_listener; + struct weston_log_scope *debug; + struct wl_list protected_list; +}; + + enum weston_key_state_update { STATE_UPDATE_AUTOMATIC, STATE_UPDATE_NONE, @@ -2449,6 +2475,13 @@ void weston_buffer_send_server_error(struct weston_buffer *buffer, const char *msg); +int +weston_compositor_enable_content_protection(struct weston_compositor *compositor); + +void +weston_protected_surface_send_event(struct protected_surface *psurface, + enum weston_hdcp_protection protection); + #ifdef __cplusplus } #endif diff --git a/libweston/compositor.c b/libweston/compositor.c index ad78eaabf..fc11ae5c0 100644 --- a/libweston/compositor.c +++ b/libweston/compositor.c @@ -481,6 +481,7 @@ weston_surface_state_init(struct weston_surface_state *state) state->acquire_fence_fd = -1; state->desired_protection = WESTON_HDCP_DISABLE; + state->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED; } static void @@ -564,6 +565,8 @@ weston_surface_create(struct weston_compositor *compositor) surface->acquire_fence_fd = -1; surface->desired_protection = WESTON_HDCP_DISABLE; + surface->current_protection = WESTON_HDCP_DISABLE; + surface->protection_mode = WESTON_SURFACE_PROTECTION_MODE_RELAXED; return surface; } @@ -3360,6 +3363,22 @@ weston_surface_set_desired_protection(struct weston_surface *surface, weston_surface_damage(surface); } +static void +weston_surface_set_protection_mode(struct weston_surface *surface, + enum weston_surface_protection_mode p_mode) +{ + struct content_protection *cp = surface->compositor->content_protection; + struct protected_surface *psurface; + + surface->protection_mode = p_mode; + wl_list_for_each(psurface, &cp->protected_list, link) { + if (!psurface || psurface->surface != surface) + continue; + weston_protected_surface_send_event(psurface, + surface->current_protection); + } +} + static void weston_surface_commit_state(struct weston_surface *surface, struct weston_surface_state *state) @@ -3450,6 +3469,11 @@ weston_surface_commit_state(struct weston_surface *surface, &state->feedback_list); wl_list_init(&state->feedback_list); + /* weston_protected_surface.enforced/relaxed */ + if (surface->protection_mode != state->protection_mode) + weston_surface_set_protection_mode(surface, + state->protection_mode); + /* weston_protected_surface.set_type */ weston_surface_set_desired_protection(surface, state->desired_protection); @@ -3753,6 +3777,7 @@ weston_subsurface_commit_to_cache(struct weston_subsurface *sub) &surface->pending.buffer_release_ref); } sub->cached.desired_protection = surface->pending.desired_protection; + sub->cached.protection_mode = surface->pending.protection_mode; assert(surface->pending.acquire_fence_fd == -1); assert(surface->pending.buffer_release_ref.buffer_release == NULL); sub->cached.sx += surface->pending.sx; @@ -7087,6 +7112,8 @@ weston_compositor_create(struct wl_display *display, ec->touch_mode = WESTON_TOUCH_MODE_NORMAL; + ec->content_protection = NULL; + if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4, ec, compositor_bind)) goto fail; diff --git a/libweston/content-protection.c b/libweston/content-protection.c new file mode 100644 index 000000000..72865a51b --- /dev/null +++ b/libweston/content-protection.c @@ -0,0 +1,347 @@ +/* + * Copyright © 2019 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "compositor/weston.h" +#include "weston-content-protection-server-protocol.h" +#include "shared/helpers.h" +#include "shared/timespec-util.h" + +#define content_protection_log(cp, ...) \ + weston_log_scope_printf((cp)->debug, __VA_ARGS__) + +static const char * const content_type_name [] = { + [WESTON_PROTECTED_SURFACE_TYPE_UNPROTECTED] = "UNPROTECTED", + [WESTON_PROTECTED_SURFACE_TYPE_HDCP_0] = "TYPE-0", + [WESTON_PROTECTED_SURFACE_TYPE_HDCP_1] = "TYPE-1", +}; + +void +weston_protected_surface_send_event(struct protected_surface *psurface, + enum weston_hdcp_protection protection) +{ + struct wl_resource *p_resource; + enum weston_protected_surface_type protection_type; + struct content_protection *cp; + struct wl_resource *surface_resource; + + p_resource = psurface->protection_resource; + if (!p_resource) + return; + /* No event to be sent to client, in case of enforced mode */ + if (psurface->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED) + return; + protection_type = (enum weston_protected_surface_type) protection; + weston_protected_surface_send_status(p_resource, protection_type); + + cp = psurface->cp_backptr; + surface_resource = psurface->surface->resource; + content_protection_log(cp, "wl_surface@%"PRIu32" Protection type set to %s\n", + wl_resource_get_id(surface_resource), + content_type_name[protection_type]); +} + +static void +set_type(struct wl_client *client, struct wl_resource *resource, + enum weston_protected_surface_type content_type) +{ + struct content_protection *cp; + struct protected_surface *psurface; + enum weston_hdcp_protection weston_cp; + struct wl_resource *surface_resource; + + psurface = wl_resource_get_user_data(resource); + if (!psurface) + return; + cp = psurface->cp_backptr; + surface_resource = psurface->surface->resource; + + if (content_type < WESTON_PROTECTED_SURFACE_TYPE_UNPROTECTED || + content_type > WESTON_PROTECTED_SURFACE_TYPE_HDCP_1) { + wl_resource_post_error(resource, + WESTON_PROTECTED_SURFACE_ERROR_INVALID_TYPE, + "wl_surface@%"PRIu32" Invalid content-type %d for request:set_type\n", + wl_resource_get_id(surface_resource), content_type); + + content_protection_log(cp, "wl_surface@%"PRIu32" Invalid content-type %d for resquest:set_type\n", + wl_resource_get_id(surface_resource), content_type); + return; + } + + content_protection_log(cp, "wl_surface@%"PRIu32" Request: Enable Content-Protection Type: %s\n", + wl_resource_get_id(surface_resource), + content_type_name[content_type]); + + weston_cp = (enum weston_hdcp_protection) content_type; + psurface->surface->pending.desired_protection = weston_cp; +} + +static void +protected_surface_destroy(struct wl_client *client, struct wl_resource *resource) +{ + struct protected_surface *psurface; + + psurface = wl_resource_get_user_data(resource); + if (!psurface) + return; + psurface->surface->pending.desired_protection = WESTON_HDCP_DISABLE; +} + +static void +set_enforce_mode(struct wl_client *client, struct wl_resource *resource) +{ + /* + * Enforce Censored-Visibility. Compositor censors the protected + * surface on an unsecured output. + * In case of a surface, being shown on an unprotected output, the + * compositor hides the surface, not allowing it to be displayed on + * the unprotected output, without bothering the client. No difference + * for the protected outputs. + * + * The member 'protection_mode' is "double-buffered", so setting it in + * pending_state will cause the setting of the corresponding + * 'protection_mode' in weston_surface, after the commit. + * + * This function sets the 'protection_mode' of the weston_surface_state + * to 'enfoced'. The renderers inspect the flag and compare the + * desired_protection of the surface, to the current_protection of the + * output, based on that the real surface or a place-holder content, + * (e.g. solid color) are shown. + */ + + struct protected_surface *psurface; + + psurface = wl_resource_get_user_data(resource); + if (!psurface) + return; + + psurface->surface->pending.protection_mode = + WESTON_SURFACE_PROTECTION_MODE_ENFORCED; +} + +static void +set_relax_mode(struct wl_client *client, struct wl_resource *resource) +{ + /* + * Relaxed mode. By default this mode will be activated. + * In case of a surface, being shown in unprotected output, + * compositor just sends the event for protection status changed. + * + * On setting the relaxed mode, the 'protection_mode' member is queued + * to be set to 'relax' from the existing 'enforce' mode. + */ + + struct protected_surface *psurface; + + psurface = wl_resource_get_user_data(resource); + if (!psurface) + return; + + psurface->surface->pending.protection_mode = + WESTON_SURFACE_PROTECTION_MODE_RELAXED; +} + +static const struct weston_protected_surface_interface protected_surface_implementation = { + protected_surface_destroy, + set_type, + set_enforce_mode, + set_relax_mode, +}; + +static void +cp_destroy_listener(struct wl_listener *listener, void *data) +{ + struct content_protection *cp; + + cp = container_of(listener, struct content_protection, + destroy_listener); + wl_list_remove(&cp->destroy_listener.link); + wl_list_remove(&cp->protected_list); + weston_compositor_log_scope_destroy(cp->debug); + cp->debug = NULL; + free(cp); +} + +static void +free_protected_surface(struct protected_surface *psurface) +{ + psurface->surface->pending.desired_protection = WESTON_HDCP_DISABLE; + wl_resource_set_user_data(psurface->protection_resource, NULL); + wl_list_remove(&psurface->surface_destroy_listener.link); + wl_list_remove(&psurface->link); + free(psurface); +} + +static void +surface_destroyed(struct wl_listener *listener, void *data) +{ + struct protected_surface *psurface; + + psurface = container_of(listener, struct protected_surface, + surface_destroy_listener); + free_protected_surface(psurface); +} + +static void +destroy_protected_surface(struct wl_resource *resource) +{ + struct protected_surface *psurface; + + psurface = wl_resource_get_user_data(resource); + if (!psurface) + return; + free_protected_surface(psurface); +} + +static void +get_protection(struct wl_client *client, struct wl_resource *cp_resource, + uint32_t id, struct wl_resource *surface_resource) +{ + struct wl_resource *resource; + struct weston_surface *surface; + struct content_protection *cp; + struct protected_surface *psurface; + struct wl_listener *listener; + + surface = wl_resource_get_user_data(surface_resource); + assert(surface); + cp = wl_resource_get_user_data(cp_resource); + assert(cp); + + /* + * Check if this client has a corresponding protected-surface + */ + + listener = wl_resource_get_destroy_listener(surface->resource, + surface_destroyed); + + if (listener) { + wl_resource_post_error(cp_resource, + WESTON_CONTENT_PROTECTION_ERROR_SURFACE_EXISTS, + "wl_surface@%"PRIu32" Protection already exists", + wl_resource_get_id(surface_resource)); + return; + } + + psurface = zalloc(sizeof(struct protected_surface)); + if (!psurface) { + wl_client_post_no_memory(client); + return; + } + psurface->cp_backptr = cp; + resource = wl_resource_create(client, &weston_protected_surface_interface, + 1, id); + if (!resource) { + free(psurface); + wl_client_post_no_memory(client); + return; + } + + wl_list_insert(&cp->protected_list, &psurface->link); + wl_resource_set_implementation(resource, &protected_surface_implementation, + psurface, + destroy_protected_surface); + + psurface->protection_resource = resource; + psurface->surface = surface; + psurface->surface_destroy_listener.notify = surface_destroyed; + wl_resource_add_destroy_listener(surface->resource, + &psurface->surface_destroy_listener); + weston_protected_surface_send_event(psurface, + psurface->surface->current_protection); +} + +static void +destroy_protection(struct wl_client *client, struct wl_resource *cp_resource) +{ + wl_resource_destroy(cp_resource); +} + +static const +struct weston_content_protection_interface content_protection_implementation = { + destroy_protection, + get_protection, +}; + +static void +bind_weston_content_protection(struct wl_client *client, void *data, + uint32_t version, uint32_t id) +{ + struct content_protection *cp = data; + struct wl_resource *resource; + + resource = wl_resource_create(client, + &weston_content_protection_interface, + 1, id); + if (!resource) { + wl_client_post_no_memory(client); + return; + } + + wl_resource_set_implementation(resource, + &content_protection_implementation, + cp, NULL); +} +/* Advertise the content-protection support. + * + * Calling this function sets up the content-protection support via HDCP. + * This exposes the global interface, visible to the client, enabling them to + * request for content-protection for their surfaces according to the type of + * content. + */ + +WL_EXPORT int +weston_compositor_enable_content_protection(struct weston_compositor *compositor) +{ + struct content_protection *cp; + + cp = zalloc(sizeof(*cp)); + if (cp == NULL) + return -1; + cp->compositor = compositor; + + compositor->content_protection = cp; + wl_list_init(&cp->protected_list); + if (wl_global_create(compositor->wl_display, + &weston_content_protection_interface, 1, cp, + bind_weston_content_protection) == NULL) + return -1; + + cp->destroy_listener.notify = cp_destroy_listener; + wl_signal_add(&compositor->destroy_signal, &cp->destroy_listener); + cp->debug = weston_compositor_add_log_scope(compositor->weston_log_ctx, + "content-protection-debug", + "debug-logs for content-protection", + NULL, NULL); + return 0; +} diff --git a/libweston/meson.build b/libweston/meson.build index 42d227cd4..f5b67fe54 100644 --- a/libweston/meson.build +++ b/libweston/meson.build @@ -12,6 +12,7 @@ srcs_libweston = [ 'bindings.c', 'clipboard.c', 'compositor.c', + 'content-protection.c', 'data-device.c', 'input.c', 'linux-dmabuf.c', @@ -50,6 +51,8 @@ srcs_libweston = [ text_input_unstable_v1_server_protocol_h, weston_touch_calibration_protocol_c, weston_touch_calibration_server_protocol_h, + weston_content_protection_protocol_c, + weston_content_protection_server_protocol_h, viewporter_protocol_c, viewporter_server_protocol_h, xdg_output_unstable_v1_protocol_c,