Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.4 開機即橫屏:SurfaceFlinger 與 Input 系統的改法記錄

編輯:關於 Android 程式設計

軟體環境:Android 4.4;硬體平台:Marvell。

之前在 Android 4.0 上做過開機螢幕旋轉 90 度,當時的切入點很好找——只要設定系統屬性 ro.sf.hwrotation = 90,SurfaceFlinger 在計算顯示時會讀取這個值並據以旋轉畫面,改動非常小。但到了 4.4,翻遍 SurfaceFlinger 相關程式碼都找不到對這個屬性的處理判斷,機制顯然被重寫過,於是決定把 4.0 那套判斷邏輯移植回來。

一、DisplayDevice.cpp:處理旋轉與投影

先在 orientationToTransfrom 中攔截 orientation,若屬性為 90 就強制指定為 eOrientation90:

--- a/services/surfaceflinger/DisplayDevice.cpp
+++ b/services/surfaceflinger/DisplayDevice.cpp
@@ status_t DisplayDevice::orientationToTransfrom(
         int orientation, int w, int h, Transform* tr)
 {
     uint32_t flags = 0;
+    char property[PROPERTY_VALUE_MAX];
+    if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
+        if (atoi(property) == 90)
+            orientation = DisplayState::eOrientation90;
+    }
     switch (orientation) {
     case DisplayState::eOrientationDefault:
         flags = Transform::ROT_0;

接著在 setProjection 中,當目標 frame 尚未設定而需要退回「整個螢幕區域」時,必須把寬高對調,否則畫面會被裁掉一半:

@@ void DisplayDevice::setProjection(int orientation, ...)
     if (!frame.isValid()) {
-        frame = Rect(w, h);
+        if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
+            if (atoi(property) == 90)
+                frame = Rect(h, w);
+            else
+                frame = Rect(w, h);
+        } else {
+            frame = Rect(w, h);
+        }
     }

二、SurfaceFlinger.cpp:修正 DisplayInfo 的寬高

上層查詢螢幕資訊時讀的是 hwc 回報的物理寬高,橫屏後必須交換:

--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ status_t SurfaceFlinger::getDisplayInfo(...)
-    info->w = hwc.getWidth(type);
-    info->h = hwc.getHeight(type);
+    if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
+        if (atoi(property) == 90) {
+            info->w = hwc.getHeight(type);
+            info->h = hwc.getWidth(type);
+        }
+    } else {
+        info->w = hwc.getWidth(type);
+        info->h = hwc.getHeight(type);
+    }

三、問題來了:畫面橫了,觸控沒橫

上述兩個檔案改完,開機與後續顯示確實進入了橫屏模式,但觸控座標仍舊按豎屏邏輯處理——點左上角反應的是別的位置。先嘗試在 Surface 相關程式碼中做座標映射,多次嘗試都失敗,最後選擇改 Input 系統的處理,這雖未必是最優解,但確實能解決問題。

首先在 TouchInputMapper::configureSurface 中強制指定視圖朝向:

--- a/services/input/InputReader.cpp
+++ b/services/input/InputReader.cpp
@@ #include "InputReader.h"
+#include <cutils/properties.h>
@@ void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
+    char property[PROPERTY_VALUE_MAX];
+    if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
+        if (atoi(property) == 90)
+            mViewport.orientation = DISPLAY_ORIENTATION_90;
+    }
     switch (mViewport.orientation) {
     case DISPLAY_ORIENTATION_90:
         naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;

接著在 cookPointerData 中同步表面朝向,讓單點座標的旋轉與顯示一致:

@@ void TouchInputMapper::cookPointerData() {
     float x, y, left, top, right, bottom;
+    char property[PROPERTY_VALUE_MAX];
+    if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
+        if (atoi(property) == 90)
+            mSurfaceOrientation = DISPLAY_ORIENTATION_90;
+    }
     switch (mSurfaceOrientation) {
     case DISPLAY_ORIENTATION_90:

至此,觸控方向與顯示方向一致,橫屏模式完整可用。

思路回顧與提醒

整個改動可以概括為三層:顯示層負責旋轉畫面與交換寬高,資訊層修正回報給上層的螢幕尺寸,輸入層把觸控座標同步旋轉。任何一層漏掉,都會表現為「畫面正常但點不準」或「畫面被裁切」這類看似不相關的現象。

需要坦白說明的是,這個方案是遇到問題解決問題的產物,4.4 也許存在類似 4.0 那種一行屬性就能搞定的入口,只是暫時沒有找到。上述改動屬於系統源碼層面的侵入式修改,穩定性未經完整測試,跨平台移植時也需要重新驗證。如果已經有人找到更乾淨的做法,歡迎交流。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved