Android 11 触摸小圆点显示流程

news2024/9/22 10:03:37

在开发者选项中,打开 “显示点按操作反馈” 开关,当我们在触摸屏幕时,会显示一个小圆点,来分析下小圆点的显示流程。
操作这个开关时,其实就是操作Settings数据库中的 SHOW_TOUCHES

//packages\apps\Settings\src\com\android\settings\development\ShowTapsPreferenceController.java
@Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final boolean isEnabled = (Boolean) newValue;
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SHOW_TOUCHES, isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
        return true;
    }

而在IMS中,会对 SHOW_TOUCHES 这个key进行监听

//frameworks\base\services\core\java\com\android\server\input\InputManagerService.java
private void registerShowTouchesSettingObserver() {
        mContext.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
                new ContentObserver(mHandler) {
                    @Override
                    public void onChange(boolean selfChange) {
                        updateShowTouchesFromSettings();
                    }
                }, UserHandle.USER_ALL);
    }

当SHOW_TOUCHES 这个key的值有改变时,调用updateShowTouchesFromSettings方法,在updateShowTouchesFromSettings方法中,是调用nativeSetShowTouches这个native方法,直接来看下这个方法

//frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
static void nativeSetShowTouches(JNIEnv* /* env */,
        jclass /* clazz */, jlong ptr, jboolean enabled) {
    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);

    im->setShowTouches(enabled);//调用NativeInputManager的setShowTouches方法
}

void NativeInputManager::setShowTouches(bool enabled) {
    { // acquire lock
        AutoMutex _l(mLock);

        if (mLocked.showTouches == enabled) {
            return;
        }

        ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
        mLocked.showTouches = enabled;//1
    } // release lock

    mInputManager->getReader()->requestRefreshConfiguration(
            InputReaderConfiguration::CHANGE_SHOW_TOUCHES);//2
}

注释1处mLocked.showTouches就为传进来的enabled(打开为true,关闭为false),注释2处调用InputReader的requestRefreshConfiguration继续处理

//frameworks\native\services\inputflinger\reader\InputReader.cpp
void InputReader::requestRefreshConfiguration(uint32_t changes) {
    AutoMutex _l(mLock);

    if (changes) {
        bool needWake = !mConfigurationChangesToRefresh;
        mConfigurationChangesToRefresh |= changes;//changes为InputReaderConfiguration::CHANGE_SHOW_TOUCHES

        if (needWake) {
            mEventHub->wake();//唤醒InputReader线程
        }
    }
}

InputReader线程被唤醒,loopOnce继续执行

//frameworks\native\services\inputflinger\reader\InputReader.cpp
void InputReader::loopOnce() {
    //省略
    { // acquire lock
        AutoMutex _l(mLock);
        
        uint32_t changes = mConfigurationChangesToRefresh;
        if (changes) {
            mConfigurationChangesToRefresh = 0;
            timeoutMillis = 0;
            refreshConfigurationLocked(changes);//1
        } else if (mNextTimeout != LLONG_MAX) {
           //省略
        }
    } // release lock

	//省略

注释1处,调用refreshConfigurationLocked继续处理

//frameworks\native\services\inputflinger\reader\InputReader.cpp
void InputReader::refreshConfigurationLocked(uint32_t changes) {
    mPolicy->getReaderConfiguration(&mConfig);//1
    mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);

    if (changes) {
       //省略
        if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
            mEventHub->requestReopenDevices();
        } else {
            for (auto& devicePair : mDevices) {
                std::shared_ptr<InputDevice>& device = devicePair.second;
                device->configure(now, &mConfig, changes);//2
            }
        }
    }
}

注释1处主要是将前面的mLocked.showTouches又赋值给mConfig的showTouches,注释2处调用InputDevice的configure继续处理。
1,getReaderConfiguration

//frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
	//省略

	{ // acquire lock
        AutoMutex _l(mLock);
		//省略

        outConfig->showTouches = mLocked.showTouches;//赋值
		
		//省略
    } //  release lock
}

2,InputDevice:configure

//frameworks\native\services\inputflinger\reader\InputDevice.cpp
void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config,
                            uint32_t changes) {
	//省略
	for_each_mapper([this, when, config, changes](InputMapper& mapper) {
            mapper.configure(when, config, changes);
            mSources |= mapper.getSources();
        });
	//省略

}

对于InputReaderConfiguration::CHANGE_SHOW_TOUCHES,又调用对应mapper的configure处理。多指触摸的话mapper为MultiTouchInputMapper,MultiTouchInputMapper没有实现configure方法,在其父类TouchInputMapper中实现

//frameworks\native\services\inputflinger\reader\mapper\TouchInputMapper.cpp
void TouchInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
                                 uint32_t changes) {
    //省略
    if (!changes ||
        (changes &
         (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
          InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
          InputReaderConfiguration::CHANGE_SHOW_TOUCHES |
          InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE))) {
        // Configure device sources, surface dimensions, orientation and
        // scaling factors.
        configureSurface(when, &resetNeeded);
    }
	//省略

调用configureSurface继续处理

//frameworks\native\services\inputflinger\reader\mapper\TouchInputMapper.cpp
void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
	//省略
	// Create pointer controller if needed.
    if (mDeviceMode == DEVICE_MODE_POINTER ||
        (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
        if (mPointerController == nullptr) {
            mPointerController = getContext()->getPointerController(getDeviceId());
        }
    } else {
        mPointerController.clear();
    }
	//省略
}

getPointerController方法最终调用InputReader的getPointerControllerLocked方法

//frameworks\native\services\inputflinger\reader\InputReader.cpp
sp<PointerControllerInterface> InputReader::getPointerControllerLocked(int32_t deviceId) {
    sp<PointerControllerInterface> controller = mPointerController.promote();
    if (controller == nullptr) {
        controller = mPolicy->obtainPointerController(deviceId);//1
        mPointerController = controller;
        updatePointerDisplayLocked();//2
    }
    return controller;
}

注释1处创建PointerController对象,注释2处调用updatePointerDisplayLocked继续处理
1,obtainPointerController

//frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
    ATRACE_CALL();
    AutoMutex _l(mLock);

    sp<PointerController> controller = mLocked.pointerController.promote();
    if (controller == nullptr) {
        ensureSpriteControllerLocked();//创建SpriteController对象

        controller = new PointerController(this, mLooper, mLocked.spriteController);//创建PointerController对象
        mLocked.pointerController = controller;
        updateInactivityTimeoutLocked();
    }

    return controller;
}

2,updatePointerDisplayLocked

//frameworks\native\services\inputflinger\reader\InputReader.cpp
void InputReader::updatePointerDisplayLocked() {
    sp<PointerControllerInterface> controller = mPointerController.promote();//取出前面创建的PointerController对象
    if (controller == nullptr) {
        return;
    }

   //省略

    controller->setDisplayViewport(*viewport);
}

来看一下PointerController的setDisplayViewport方法

frameworks\base\libs\input\PointerController.cpp
void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
	//省略
		loadResourcesLocked();//加载小圆点图片资源
	//省略

	updatePointerLocked();//保存资源信息到SpriteController中,其中包含了小圆点图片的bitmap

}

loadResourcesLocked加载图片信息,主要是通过JNI调用,调用到PointerIcon中的getSystemIcon方法中

//frameworks\base\core\java\android\view\PointerIcon.java
public static PointerIcon getSystemIcon(@NonNull Context context, int type) {
       //省略
        int typeIndex = getSystemIconTypeIndex(type);
        if (typeIndex == 0) {
            typeIndex = getSystemIconTypeIndex(TYPE_DEFAULT);
        }

        int defStyle = sUseLargeIcons ?
                com.android.internal.R.style.LargePointer : com.android.internal.R.style.Pointer;
        TypedArray a = context.obtainStyledAttributes(null,
                com.android.internal.R.styleable.Pointer,
                0, defStyle);
        int resourceId = a.getResourceId(typeIndex, -1);
        a.recycle();
        
        icon = new PointerIcon(type);
        if ((resourceId & 0xff000000) == 0x01000000) {
            icon.mSystemIconResourceId = resourceId;//设置ID
        } else {
            icon.loadResource(context, context.getResources(), resourceId);
        }
        systemIcons.append(type, icon);
        return icon;
    }

private static int getSystemIconTypeIndex(int type) {
        switch (type) {
            case TYPE_ARROW:
                return com.android.internal.R.styleable.Pointer_pointerIconArrow;
            case TYPE_SPOT_HOVER:
                return com.android.internal.R.styleable.Pointer_pointerIconSpotHover;
            case TYPE_SPOT_TOUCH:
                return com.android.internal.R.styleable.Pointer_pointerIconSpotTouch;
	//省略
}

可以看出,加载的资源ID为pointer_spot_touch_icon

//frameworks\base\core\res\res\drawable\pointer_spot_touch_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<pointer-icon xmlns:android="http://schemas.android.com/apk/res/android"
    android:bitmap="@drawable/pointer_spot_touch"
    android:hotSpotX="16dp"
    android:hotSpotY="16dp" />

小圆点的图片资源已经被加载,并将其保存在相应的变量中了,接下来就需要将其显示出来了。当有触摸数据产生时,InputReader在读取到数据,然后会调用到cookAndDispatch来处理原始数据

//frameworks\native\services\inputflinger\reader\mapper\TouchInputMapper.cpp
void TouchInputMapper::cookAndDispatch(nsecs_t when) {
	//省略
	if (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches &&
            mPointerController != nullptr) {//如果需要显示小圆点
            mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
            mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);

            mPointerController->setButtonState(mCurrentRawState.buttonState);
            mPointerController->setSpots(mCurrentCookedState.cookedPointerData.pointerCoords,
                                         mCurrentCookedState.cookedPointerData.idToIndex,
                                         mCurrentCookedState.cookedPointerData.touchingIdBits,
                                         mViewport.displayId);
        }
	//省略
}

主要是通过调用setSpots去处理,在setSpots函数中,主要是先创建一个Spot对象,然后调用其updateSprite方法去请求更新小圆点

//frameworks\base\libs\input\PointerController.cpp
void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y,
        int32_t displayId) {
    sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
    sprite->setAlpha(alpha);
    sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
    sprite->setPosition(x, y);
    sprite->setDisplayId(displayId);
    this->x = x;
    this->y = y;

    if (icon != lastIcon) {
        lastIcon = icon;
        if (icon) {
            sprite->setIcon(*icon);
            sprite->setVisible(true);//显示
        } else {
            sprite->setVisible(false);
        }
    }
}

接下来的流程,主要是通过发送发送MSG_UPDATE_SPRITES消息,然后调用doUpdateSprites来更新绘制小圆点

//frameworks\base\libs\input\SpriteController.cpp
void SpriteController::doUpdateSprites() {
	//省略
	if (update.state.surfaceControl != NULL && !update.state.surfaceDrawn
                && update.state.wantSurfaceVisible()) {
            sp<Surface> surface = update.state.surfaceControl->getSurface();
            ANativeWindow_Buffer outBuffer;
            status_t status = surface->lock(&outBuffer, NULL);
            if (status) {
                ALOGE("Error %d locking sprite surface before drawing.", status);
            } else {
                graphics::Paint paint;
                paint.setBlendMode(ABLEND_MODE_SRC);

                graphics::Canvas canvas(outBuffer, (int32_t) surface->getBuffersDataSpace());
                canvas.drawBitmap(update.state.icon.bitmap, 0, 0, &paint);

                const int iconWidth = update.state.icon.bitmap.getInfo().width;
                const int iconHeight = update.state.icon.bitmap.getInfo().height;

                if (outBuffer.width > iconWidth) {
                    paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
                    canvas.drawRect({iconWidth, 0, outBuffer.width, iconHeight}, paint);
                }
                if (outBuffer.height > iconHeight) {
                    paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
                    canvas.drawRect({0, iconHeight, outBuffer.width, outBuffer.height}, paint);
                }

                status = surface->unlockAndPost();

		//省略
}

总结
触摸圆点的显示逻辑都是在InputReader线程中完成。以下为显示的流程图
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1707086.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

HTTP请求拦截器链

文章目录 HTTP请求拦截器链需求定义写一个Controller方法接口写三个http请求拦截器把拦截器加入到配置中&#xff0c;并且配置拦截规则在postman里面发送请求&#xff0c;看下测试结果是否正确 HTTP请求拦截器链 需求定义 我们写一个包含三个HTTP请求拦截器的拦截器链&#x…

如何让UE4.26使用VS2022【Windows,源码下载】

使用UE5一直用的是VS2022&#xff0c;都是因为团队需要&#xff0c;只能用UE4&#xff0c;而我电脑中拥有的UE4的版本是UE4.26以及VS2022&#xff0c;我不可能去下载VS2019来为这么一个项目&#xff0c;所以就研究了一下是哪里阻止了UE4.26不让我使用VS2022. 首先下载UE4.26源码…

Langchain-Chatchat之pdf转markdown格式

文章目录 背景开发环境loader文本解析步骤markdown格式的文本为什么选择markdown格式测试markdown格式提取表格原pdf表格markdown格式的表格 测试markdown格式的知识库运行项目修改文件加载器loader 其他问题运行项目报错查看系统当前的max_user_watches修改sysctl.conf配置 图…

【Linux】Linux下centos更换国内yum源

&#x1f331;博客主页&#xff1a;青竹雾色间 &#x1f331;系列专栏&#xff1a;Linux &#x1f618;博客制作不易欢迎各位&#x1f44d;点赞⭐收藏➕关注 目录 1. 备份旧的 YUM 源文件2. 下载国内的 YUM 源文件阿里云&#xff1a;网易&#xff1a; 3. 清理 YUM 缓存4. 更新…

【c++leetcode】69. Sqrt(x)

问题入口 二分搜索 最困难的是能否意识到用二分搜索法解题。 算术平方根的区间在[1, x] 。代码如下&#xff1a; class Solution { public:int mySqrt(int x) {if (x 1 || x 0){return x;}int64_t start 1;int64_t end x;while (start < x){int64_t mid start (en…

开箱元宇宙| 探索家乐福如何在The Sandbox 中重新定义零售和可持续发展

有没有想过 The Sandbox 如何与世界上最具代表性的品牌和名人的战略保持一致&#xff1f;在本期的 "开箱元宇宙 "系列中&#xff0c;我们与家乐福团队进行了对话&#xff0c;这家法国巨头率先采用web3技术重新定义零售和可持续发展。 家乐福的用户平均游玩时间为 57 …

06_知识点总结(JS高级)

一、进程与线程 1. 进程(process)&#xff1a;程序的一次执行, 它占有一片独有的内存空间 2. 线程(thread)&#xff1a; 是进程内的一个独立执行单元&#xff0c;CPU的基本调度单元, 是程序执行的一个完整流程 3. 进程与线程 * 应用程序必须运行在某个进程的某个线程上 * 一个…

tinyrenderer-渲染器着色

整理了代码&#xff0c;创建了一个相机类&#xff0c;控制镜头 class Camera { public:Camera(Vec3f cameraPos, Vec3f target, Vec3f up):cameraPos_(cameraPos), target_(target), up_(up) {}Matrix getView();Matrix getProjection(); private:Vec3f cameraPos_;Vec3f targ…

来自学术界的知识库 RAG 调优方案实践(一)

背景介绍 在之前的文章详细梳理过工业界的 RAG 方案 QAnything 和 RagFlow&#xff0c;这次主要整理下来自学术界的一系列 RAG 优化方案。 主要关注优化方案对应的设计思想以及相关的实现&#xff0c;希望可以对大家的 RAG 服务效果提升有所帮助。 基础介绍 在综述论文 Ret…

【ARM+Codesys案例】T3/RK3568/树莓派+Codesys锂电池测试设备控制解决方案

锂电池诞生于上世纪60年代&#xff0c;90年代开始由日本索尼公司实现商业化。锂离子电池凭借快速充放电、长循环寿命、无记忆效应等众多优点&#xff0c;成为当今数码产品及电动汽车大规模应用的第一选择。与镍氢电池、铅酸电池相比&#xff0c;锂电池可以存储更多电能。现在&a…

Word如何绘制三线表及设置磅值

插入表格&#xff0c; 开始 边框 边框和低温 设置磅值 先全部设置为无边框 上边 1.5 0.5 以上内容未完善&#xff0c;请等待作者更新

春秋云境CVE-2018-20604

简介 雷风影视CMS是一款采用PHP基于THINKPHP3.2.3框架开发&#xff0c;适合各类视频、影视网站的影视内容管理程序&#xff0c;该CMS存在缺陷&#xff0c;可以通过 admin.php?s/Template/edit/path/*web*..*..*..*..*1.txt 的方式读取任意文件。 正文 1.进入靶场 2./admin…

机器学习(五) -- 监督学习(4) -- 集成学习方法-随机森林

系列文章目录及链接 上篇&#xff1a;机器学习&#xff08;五&#xff09; -- 监督学习&#xff08;3&#xff09; -- 决策树 下篇&#xff1a;机器学习&#xff08;五&#xff09; -- 监督学习&#xff08;5&#xff09; -- 线性回归1 前言 tips&#xff1a;标题前有“***”…

pip安装软件包提示“没有那个文件或目录”问题的处理

文章目录 一、Python.h&#xff1a;没有那个文件或目录二、lber.h&#xff1a;没有那个文件或目录 一、Python.h&#xff1a;没有那个文件或目录 pip install -I python-ldap3.0.0b1 #异常提示In file included from Modules/LDAPObject.c:3:0:Modules/common.h:9:20: 致命错…

zabbix配置自动发现规则模版-snmp

配置-->模版-->创建模版 找到刚才创建的模版进行编辑 创建一个自动发现规则&#xff08;SNMP OID 一般配置表中较有代表性的字段的OID&#xff09; 修改监控项原型 创建一台主机并链接模版 测试&#xff0c;获得预计自动发现的监控项的值 上述测试不为空&#xff0c…

OWASP十大API漏洞解析:如何抵御Bot攻击?

新型数字经济中&#xff0c;API是物联网设备、Web和移动应用以及业务合作伙伴流程的入口点。然而&#xff0c;API也是犯罪分子的前门&#xff0c;许多人依靠Bot来发动攻击。对于安全团队来说&#xff0c;保护API并缓解Bot攻击至关重要。那么Bot在API攻击中处于怎样的地位&#…

动手学操作系统(三、通过IO接口直接控制显卡)

动手学操作系统&#xff08;三、通过IO接口直接控制显卡&#xff09; 在之前的学习内容中&#xff0c;我们成功编写了MBR主引导记录&#xff0c;在终端上进行了打印显示&#xff0c;在这一节我们使用MBR通过IO接口来直接控制显卡输出字符。 文章目录 动手学操作系统&#xff0…

物联网在IT/OT融合中的作用有哪些?——青创智通

工业物联网解决方案-工业IOT-青创智通 制造业等重工业正处于大规模数字化转型之中。这种转变的一部分是一种称为IT/OT融合的做法&#xff0c;这种做法带来了一些独特的安全风险。企业需要物联网来解决这些问题。 什么是IT/OT融合&#xff1f; IT/OT融合是信息技术&#xff08…

ue引擎游戏开发笔记(47)——设置状态机解决跳跃问题

1.问题分析&#xff1a; 目前当角色起跳时&#xff0c;只是简单的上下移动&#xff0c;空中仍然保持行走动作&#xff0c;并没有设置跳跃动作&#xff0c;因此&#xff0c;给角色设置新的跳跃动作&#xff0c;并优化新的动作动画。 2.操作实现&#xff1a; 1.实现跳跃不复杂&…

vr数字成果展在线展示突破用户传统认知

想要轻松搭建一个充满互动与创意的3D数字展厅吗?vr互动数字展厅搭建编辑器将是您的不二之选!华锐视点3D云展平台提供的vr互动数字展厅搭建编辑器将空间重建与互动制作完美结合&#xff0c;让您轻松实现3D空间的搭建与互动营销制作。 在vr互动数字展厅搭建编辑器的帮助下&#…