weston: light sleep function [1/1]

PD#SWPL-177395

Problem:
lightsleep wakeup failed

Solution:
add light sleep function

Verify:
ah212

Change-Id: Iabde450a63037f382084ae819d7e22d738fbcf67
Signed-off-by: leng.fang <leng.fang@amlogic.com>
diff --git a/aml-weston/aml-backend.c b/aml-weston/aml-backend.c
index 5b05b08..d0c2ffd 100644
--- a/aml-weston/aml-backend.c
+++ b/aml-weston/aml-backend.c
@@ -51,6 +51,7 @@
 		[WESTON_MODE_PIC_AR_64_27] = " 64:27",
 		[WESTON_MODE_PIC_AR_256_135] = " 256:135",
 };
+drm_cmd *g_cmd = NULL;
 #endif
 
 #ifdef BUILD_AML_TV
@@ -225,6 +226,17 @@
 	return rc;
 }
 
+static int drm_get_scaling()
+{
+	int ret = 100;
+
+#ifdef ENABLE_MODE_POLICY
+	ret = mode_policy_get_scaling();
+#endif
+
+	return ret;
+}
+
 void drm_output_render_aml_config(struct drm_output *output, struct drm_plane_state *scanout_state)
 {
 	if (output && scanout_state) {
@@ -233,7 +245,7 @@
 #endif
 
 #ifdef MESON_DRM_FIX_UI_SIZE
-		int scaling = mode_policy_get_scaling();
+		int scaling = drm_get_scaling();
 		if (scaling > 40 && scaling < 100) {
 			int x_margin = output->display_size.width * (100 - scaling) / 100;
 			int y_margin = output->display_size.height * (100 - scaling) / 100;
@@ -364,13 +376,17 @@
 
 #ifdef ENABLE_DRM_HELP
 		struct drm_head *head = to_drm_head(weston_output_get_first_head(base));
+		drm_cmd *ctx;
 		if (head && head->connector.conn)
 		{
 			if (head->connector.conn->connector_type == DRM_MODE_CONNECTOR_HDMIA)
 			{
+				ctx = drm_get_cmd();
+				ctx->b = b;
 				help_set_switch_mode_function((output_ctx)base, drm_output_switch_mode_helper);
 				help_set_force_refresh_function(drm_output_force_refresh);
 				help_set_property_function(drm_set_property);
+				help_set_execute_command_function(drm_execute_command);
 			}
 		}
 #endif
@@ -948,6 +964,138 @@
 	}
 	return 0;
 }
+
+drm_cmd *drm_get_cmd()
+{
+	if (!g_cmd) {
+		g_cmd = (drm_cmd *)calloc(1, sizeof(*g_cmd));
+	}
+	return g_cmd;
+}
+
+static bool compare_string(const char *str, const char *const_str)
+{
+	int str_len, const_len;
+
+	if (!str || !const_str)
+		return false;
+
+	str_len = strlen(str);
+	const_len = strlen(const_str);
+
+	if (str_len != const_len)
+		return false;
+
+	if (!strncmp(str, const_str, str_len))
+		return true;
+	return false;
+}
+
+static bool drm_get_bestmode()
+{
+	bool ret = false;
+
+#ifdef ENABLE_MODE_POLICY
+	ret = WestonGetUbootIsBestmode();
+#endif
+
+	return ret;
+}
+
+static void drm_set_display_enable(drm_cmd *info, int enable)
+{
+	struct drm_backend *b = info->b;
+	struct weston_compositor *compositor;
+	struct drm_output *output;
+	struct weston_mode *wst_mode;
+
+	if (!b || !b->compositor)
+		return;
+
+	if (b->display_enable == enable)
+		return;
+
+	compositor = b->compositor;
+	b->display_enable = enable;
+
+	if (enable == 1) {
+		mode_policy_resume();
+		wst_mode = mode_policy_choose_mode(NULL);
+		wl_list_for_each(output, &compositor->output_list, base.link) {
+			weston_output_mode_set_native(&output->base,
+						wst_mode, output->base.current_scale);
+			if (output->base.start_repaint_loop)
+				output->base.start_repaint_loop(&output->base);
+		}
+	} else if (enable == 0) {
+		drm_change_to_dummy_mode(compositor);
+	}
+}
+
+char *drm_execute_command(char *cmd)
+{
+	char *tok, *ctx = NULL;
+	drm_cmd *info = drm_get_cmd();
+
+	if (cmd && strlen(cmd) > 0) {
+		weston_log("%s[%d]: cmd: %s\n", __func__, __LINE__, cmd);
+		tok = strtok_r(cmd, " ", &ctx);
+		do {
+			if (tok) {
+				if (compare_string(tok, "get")) {
+					tok = strtok_r(0, " ", &ctx);
+					if (tok) {
+						if (compare_string(tok, "display")) {
+							tok = strtok_r(0, " ", &ctx);
+							if (tok) {
+								if (compare_string(tok, "enable")) {
+									int enabled = info->b ? info->b->display_enable : 0;
+									sprintf(info->response, "%d: display enable %d", 0, enabled);
+								}
+							} else {
+								sprintf(info->response, "%d: %s", -1, "get display missing argument(s)");
+							}
+						} else if (compare_string(tok, "bestmode")) {
+							sprintf(info->response, "%d: bestmode %d", 0, drm_get_bestmode() ? 1 : 0);
+						} else if (compare_string( tok, "scaling")) {
+							sprintf(info->response, "%d: scaling %d", 0, drm_get_scaling());
+						}
+					}
+				} else if (compare_string(tok, "set")) {
+					tok = strtok_r(0, " ", &ctx);
+					if (tok) {
+						if (compare_string(tok, "display")) {
+							tok = strtok_r(0, " ", &ctx);
+							if (tok) {
+								if (compare_string(tok, "enable")) {
+									tok = strtok_r(0, " ", &ctx);
+									if (tok) {
+										int value = -1;
+										if (compare_string(tok, "0"))
+											value = 0;
+										else if (compare_string(tok, "1"))
+											value = 1;
+										if (value >= 0) {
+											drm_set_display_enable(info, value);
+											sprintf(info->response, "%d: display set enable %d", 0, value);
+										}
+									}
+								}
+							} else {
+								sprintf(info->response, "%d: %s", -1, "set display missing argument(s)");
+							}
+						}
+					}
+				} else {
+					weston_log("%s[%d]: %d: %s", __func__, __LINE__, -1, "unknown cmd" );
+					break;
+				}
+			}
+			tok = strtok_r(0, " ", &ctx);
+		} while (tok);
+	}
+	return info->response;
+}
 #endif
 
 void drm_add_aml_callback(struct drm_backend *b)
diff --git a/aml-weston/aml-backend.h b/aml-weston/aml-backend.h
index 83dab6f..c5eb2d3 100644
--- a/aml-weston/aml-backend.h
+++ b/aml-weston/aml-backend.h
@@ -19,9 +19,16 @@
 #include "compositor-drm-help.h"
 #include "shared/file-util.h"
 
+typedef struct _drm_cmd {
+	struct drm_backend *b;
+	char response[256 + 3];
+} drm_cmd;
+
+drm_cmd *drm_get_cmd();
 void weston_print_info(struct weston_compositor* ec, int count);
 int drm_output_switch_mode_helper(output_ctx ctx, drm_helper_mode* mode);
 int drm_output_force_refresh(output_ctx ctx);
+char *drm_execute_command(char *cmd);
 #endif
 
 void drm_output_render_aml_config(struct drm_output *output, struct drm_plane_state *scanout_state);
diff --git a/libweston/backend-drm/drm-internal.h b/libweston/backend-drm/drm-internal.h
index 6cb1d5a..b9ee319 100644
--- a/libweston/backend-drm/drm-internal.h
+++ b/libweston/backend-drm/drm-internal.h
@@ -321,6 +321,7 @@
 	int vdin_detect_fd;
 #endif
 	bool allow_modeset;
+	int display_enable;
 };
 
 struct drm_mode {
diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
index 5f53c7c..d69da58 100644
--- a/libweston/backend-drm/drm.c
+++ b/libweston/backend-drm/drm.c
@@ -2564,7 +2564,7 @@
 
 	event = udev_monitor_receive_device(b->udev_monitor);
 
-	if (udev_event_is_hotplug(b, event)) {
+	if (udev_event_is_hotplug(b, event) && b->display_enable) {
 #ifdef ENABLE_MODE_POLICY
 		mode_policy_set_hotplug(AML_WESTON_HOTPLUG_PLUG | AML_WESTON_HOTPLUG_UNPLUG, true);
 #endif
@@ -2657,6 +2657,7 @@
 		weston_compositor_damage_all(compositor);
 		b->state_invalid = true;
 		b->allow_modeset = true;
+		b->display_enable = 1;
 		udev_input_enable(&b->input);
 		weston_compositor_resume_focus(compositor);
 	} else {
@@ -3055,6 +3056,7 @@
 	b->state_invalid = true;
 	b->allow_modeset = true;
 	b->drm.fd = -1;
+	b->display_enable = 1;
 
 #ifdef BUILD_AML_TV
 	b->vdin_detect_fd = -1;
diff --git a/libweston/modepolicy/ModePolicy.cpp b/libweston/modepolicy/ModePolicy.cpp
index 1266a48..cea77ad 100644
--- a/libweston/modepolicy/ModePolicy.cpp
+++ b/libweston/modepolicy/ModePolicy.cpp
@@ -2424,7 +2424,7 @@
             }
         }
 
-        if (attr_change || modeChange || frac_rate_policy_change)
+        if (attr_change || modeChange || frac_rate_policy_change || force)
             mAdapter->setColorAttribute(final_deepcolor);
 
         //apply hdr priority to driver sysfs
diff --git a/libweston/modepolicy/modepolicy_aml.cpp b/libweston/modepolicy/modepolicy_aml.cpp
index a2ec5da..aed8f73 100644
--- a/libweston/modepolicy/modepolicy_aml.cpp
+++ b/libweston/modepolicy/modepolicy_aml.cpp
@@ -155,7 +155,7 @@
 	return get_mode_name_for_weston_mode(ctx, wmode, mode);
 }
 
-static bool WestonGetUbootIsBestmode()
+bool WestonGetUbootIsBestmode()
 {
 	const char *isbestmode = bootenv_get("is.bestmode");
 	if ( isbestmode != NULL ) {
diff --git a/libweston/modepolicy/modepolicy_aml.h b/libweston/modepolicy/modepolicy_aml.h
index b7b17c8..c372e14 100644
--- a/libweston/modepolicy/modepolicy_aml.h
+++ b/libweston/modepolicy/modepolicy_aml.h
@@ -47,6 +47,7 @@
 
 void init_mode_policy_without_mode(struct weston_head *head, int fd, drmModeConnector *conn);
 void weston_start_update_env_thread();
+bool WestonGetUbootIsBestmode();
 int mode_policy_add_prop(drmModeAtomicReq *req, bool mode_changed);
 void mode_policy_set_hotplug(int plug, bool force);
 struct weston_mode *mode_policy_choose_mode(struct weston_mode *mode);