解决android4.0 触摸屏分辨率映射不准

来源:互联网 发布:天际网络网关登录器 编辑:程序博客网 时间:2024/05/01 11:38

转载自:http://blog.csdn.net/a181622974/article/details/8192334

前言 

  几个月以前升级了android4.0,在触摸屏这块一直有个令人头疼的问题,通常触摸屏的分辨率应该是根据当前屏幕分辨率而定。

但android4.0上就有点奇怪,不管怎么换屏幕的分辨率,触摸屏始终有不准的情况。最近着手研究了这块,通过修改inputreader.cpp 

及 com_android_server_InputManager.cpp终于解决的触摸屏不准的问题。


正文

  由于驱动是直接从android2.3的linux内核中移植过来,而android2.3上又可以正常使用且没有任何区域触摸无反应的情况,因此可以

排除linux-->android层之间的接口问题。


分析

  触摸屏属于输入设备,而且在整个输入过程中肯定是有坐标转换的,否则系统是无法获取触摸屏的准确点击位置。而在android中输入

事件可以到input相关代码中查找。


源码追踪

  仔细分析了下framework代码,可以发现framework//base/services/input/InputReader.cpp 中对触摸事件设置了一个分辨率映射值:

[cpp] view plaincopy
  1. void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t width, int32_t height, int32_t orientation) {  
  3.     if (displayId == 0) {  
  4.         DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;  
  5.         info.width = width;  
  6.         info.height = height;  
  7.         info.orientation = orientation;  
  8.     }  
  9. }  

此处DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;说明该函数可能有两次调用,用于设置两种情况的输入设备。


而附近又对应了获取显示器信息函数

[cpp] view plaincopy
  1. bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t* width, int32_t* height, int32_t* orientation) const {  
  3.   
  4. }  

并且在该文件中有多处调用了该函数,其中以下位置比较可疑

[cpp] view plaincopy
  1. void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {  
  2.     int32_t oldDeviceMode = mDeviceMode;  
  3. ......  
  4.  // Get associated display dimensions.  
  5.     if (mParameters.associatedDisplayId >= 0) {  
  6.         if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,  
  7.                 mParameters.associatedDisplayIsExternal,  
  8.                 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,  
  9.                 &mAssociatedDisplayOrientation)) {  
  10.             LOGI(INDENT "Touch device '%s' could not query the properties of its associated "  
  11.                     "display %d.  The device will be inoperable until the display size "  
  12.                     "becomes available.",  
  13.                     getDeviceName().string(), mParameters.associatedDisplayId);  
  14.             //这块打印消息直接暴露了 触摸屏设备获取屏幕分辨率的踪迹,看来触摸屏确实是通过获取  
  15.             //当前屏幕分辨率来映射坐标值的  
  16.             mDeviceMode = DEVICE_MODE_DISABLED;  
  17.             return;  
  18.         }  
  19.     }  
  20.   
  21.     // Configure dimensions.  
  22.     int32_t width, height, orientation;  
  23.       
  24.     if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {  
  25.         width = mAssociatedDisplayWidth;  //此处保留了之前获取的宽度  
  26.         height = mAssociatedDisplayHeight;//此处保留了之前获取的高度  
  27.         orientation = mParameters.orientationAware ? //此处保留了屏幕的方向  
  28.                 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;  
  29.     } else {  
  30.         width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;  
  31.         height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;  
  32.         orientation = DISPLAY_ORIENTATION_0;  
  33.          
  34.     }  
看来是有必要追踪是谁调用了setDisplayInfo

通过source insight搜索发现

com_android_server_InputManager.cpp中对InputReaderConfiguration::setDisplayInfo调用了两次,代码如下:

[cpp] view plaincopy
  1. void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {  
  2.     JNIEnv* env = jniEnv();  
  3.    ......  
  4.     { // acquire lock  
  5.         AutoMutex _l(mLock);  
  6.   
  7.         outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed  
  8.                 * POINTER_SPEED_EXPONENT);  
  9.         outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;  
  10.   
  11.         outConfig->showTouches = mLocked.showTouches;  
  12.   
  13.         outConfig->setDisplayInfo(0, false /*external*/,  
  14.                 mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);  
  15.         outConfig->setDisplayInfo(0, true /*external*/,  
  16.                 mLocked.displayExternalWidth, mLocked.displayExternalHeight,  
  17.                 mLocked.displayOrientation);  
  18.     }   
  19. }  

立刻在setDisplayInfo函数中增加打印消息

[cpp] view plaincopy
  1. void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t width, int32_t height, int32_t orientation) {  
  3.   
  4.     LOGI("[inputreader] [setDisplayInfo] : width=%d height=%d orientation=%d",  
  5.             width, height, orientation);  
  6.     if (displayId == 0) {  
  7.         DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;  
  8.         info.width = width;  
  9.         info.height = height;  
  10.         info.orientation = orientation;  
  11.     }  
  12. }  
重新制作固件烧录后重启发现:

两次setDisplayInfo分别设置了连个不同分辨率的值下来

笔者用的是lvds输出,分辨率设置为1920*1080

第一次:

[cpp] view plaincopy
  1. [inputreader] [setDisplayInfo] : width=1920 height=1080 orientation=0  
第二次
[cpp] view plaincopy
  1. [inputreader] [setDisplayInfo] : width=1280 height=800 orientation=0  
这一结果也解释了触摸屏不准的情况,原来是将1280*800的分辨率映射到1920*1080的显示屏上。



解决方法

通过测试结果可以发现第一次获取的屏幕分辨率是准确的,因此于是通过以下方式修改,解决了触摸不准的情况

1.注释getReaderConfiguration中第二次调用setDisplayInfo

[cpp] view plaincopy
  1. void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {  
  2.     JNIEnv* env = jniEnv();  
  3.    ......  
  4.         outConfig->setDisplayInfo(0, false /*external*/,  
  5.                 mLocked.displayWidth, mLocked.displayHeight, mLocked.displayOrientation);  
  6.         //outConfig->setDisplayInfo(0, true /*external*/,  
  7.         //       mLocked.displayExternalWidth, mLocked.displayExternalHeight,  
  8.         //        mLocked.displayOrientation);  
  9.     }   
  10. }  

2.setDisplayInfo中修改如下:

[cpp] view plaincopy
  1. void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,  
  2.         int32_t width, int32_t height, int32_t orientation) {  
  3.     LOGI("[inputreader] [setDisplayInfo] : width=%d height=%d orientation=%d",  
  4.             width, height, orientation);  
  5.     if (displayId == 0) {  
  6.         DisplayInfo& info = mExternalDisplay;  
  7.         info.width = width;  
  8.         info.height = height;  
  9.         info.orientation = orientation;  
  10.   
  11.         DisplayInfo& minfo =  mInternalDisplay;  
  12.         minfo.width = width;  
  13.         minfo.height = height;  
  14.         minfo.orientation = orientation;          
  15.     }  
  16. }  

这样修改就将第一次传来的屏幕分辨率保存到两个不同变量中供不同情况使用。至此android4.0的屏幕校准问题解决
原创粉丝点击