composer: Don't allow power mode switch during TUI end

Change-Id: Ic36cd513886a5e513c8c7e1d83f4174545827d49
CRs-Fixed: 3561149
diff --git a/composer/hwc_session.cpp b/composer/hwc_session.cpp
index 4f2b3dd..b33216c 100644
--- a/composer/hwc_session.cpp
+++ b/composer/hwc_session.cpp
@@ -1208,6 +1208,15 @@
     }
   }
 
+  if (tui_start_success_ && is_builtin && is_power_off) {
+    DLOGI("TUI session in progress, defer power state change");
+    SCOPE_LOCK(locker_[display]);
+    if (hwc_display_[display]) {
+      hwc_display_[display]->SetPendingPowerMode(mode);
+      return HWC2_ERROR_NONE;
+    }
+  }
+
   if (secure_session_active_ && is_builtin && is_power_off) {
     if (GetActiveBuiltinDisplay() != HWCCallbacks::kNumDisplays) {
       DLOGI("Secure session in progress, defer power state change");
@@ -4271,6 +4280,7 @@
     }
   }
 
+  tui_start_success_ = true;
   return 0;
 }
 android::status_t HWCSession::TUITransitionEnd(int disp_id) {
@@ -4295,10 +4305,12 @@
     if (hwc_display_[target_display]) {
       if (hwc_display_[target_display]->HandleSecureEvent(kTUITransitionEnd, &needs_refresh,
                                                           false) != kErrorNone) {
+        tui_start_success_ = false;
         return -EINVAL;
       }
     } else {
       DLOGW("Target display %d is not ready", disp_id);
+      tui_start_success_ = false;
       return -ENODEV;
     }
   }
@@ -4311,6 +4323,7 @@
         DLOGE("Device unassign failed with error %d", ret);
       }
       TUITransitionUnPrepare(disp_id);
+      tui_start_success_ = false;
       return 0;
     }
   }
@@ -4377,6 +4390,7 @@
   }
   // Reset tui session state variable.
   DLOGI("End of TUI session on display %d", disp_id);
+  tui_start_success_ = false;
   return 0;
 }
 
diff --git a/composer/hwc_session.h b/composer/hwc_session.h
index 8c32a01..c4edded 100644
--- a/composer/hwc_session.h
+++ b/composer/hwc_session.h
@@ -666,6 +666,7 @@
   bool async_vds_creation_ = false;
   std::bitset<HWCCallbacks::kNumDisplays> display_ready_;
   bool secure_session_active_ = false;
+  bool tui_start_success_ = false;
   bool is_client_up_ = false;
   std::shared_ptr<IPCIntf> ipc_intf_ = nullptr;
   bool primary_pending_ = true;