Android13实时刷新频率的实现代码

设置->开发者选项->显示刷新频率
packages/apps/Settings/src/com/android/settings/development/ShowRefreshRatePreferenceController.java

java 复制代码
public class ShowRefreshRatePreferenceController extends DeveloperOptionsPreferenceController
        implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {

    ......

    private static final int SETTING_VALUE_QUERY = 2;
    private static final int SETTING_VALUE_ON = 1;
    private static final int SETTING_VALUE_OFF = 0;

    @VisibleForTesting
    static final String SURFACE_FLINGER_SERVICE_KEY = "SurfaceFlinger";
    @VisibleForTesting
    static final int SURFACE_FLINGER_CODE = 1034;

    private static final String SURFACE_COMPOSER_INTERFACE_KEY = "android.ui.ISurfaceComposer";

    private final IBinder mSurfaceFlinger;

    ......

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final boolean isEnabled = (Boolean) newValue;
        writeShowRefreshRateSetting(isEnabled);
        return true;
    }

    @Override
    public void updateState(Preference preference) {
        updateShowRefreshRateSetting();
    }

    ......

    @VisibleForTesting
    void updateShowRefreshRateSetting() {
        // magic communication with surface flinger.
        try {
            if (mSurfaceFlinger != null) {
                final Parcel data = Parcel.obtain();
                final Parcel reply = Parcel.obtain();
                data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
                data.writeInt(SETTING_VALUE_QUERY);
                mSurfaceFlinger.transact(SURFACE_FLINGER_CODE, data, reply, 0 /* flags */); // 调用 SurfaceFlinger::onTransact 中 1034 方法
                final boolean enabled = reply.readBoolean();    // 通过 SurfaceFlinger 获取 mRefreshRateOverlay != null判断显示是否打开
                ((SwitchPreference) mPreference).setChecked(enabled);
                reply.recycle();
                data.recycle();
            }
        } catch (RemoteException ex) {
            // intentional no-op
        }
    }

    @VisibleForTesting
    void writeShowRefreshRateSetting(boolean isEnabled) {   // 设置打开显示刷新频率
        try {
            if (mSurfaceFlinger != null) {
                final Parcel data = Parcel.obtain();
                data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
                final int showRefreshRate = isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF;
                data.writeInt(showRefreshRate);
                mSurfaceFlinger.transact(SURFACE_FLINGER_CODE, data,
                        null /* reply */, 0 /* flags */);
                data.recycle();
            }
        } catch (RemoteException ex) {
            // intentional no-op
        }
        updateShowRefreshRateSetting();
    }
}

RefreshRateOverlay实际Layer显示刷新频率
frameworks\native\services\surfaceflinger\SurfaceFlinger.cpp

namespace {
    ......
status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
                                    uint32_t flags) {
    if (const status_t error = CheckTransactCodeCredentials(code); error != OK) {
        return error;
    }

    status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
    if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
        CHECK_INTERFACE(ISurfaceComposer, data, reply);
        IPCThreadState* ipc = IPCThreadState::self();
        const int uid = ipc->getCallingUid();
        if(code == 8002) {
            reply->writeInt32(mScheduler->getDfrcFps());
            return NO_ERROR;
        }
        if (CC_UNLIKELY(uid != AID_SYSTEM
                && !PermissionCache::checkCallingPermission(sHardwareTest))) {
            const int pid = ipc->getCallingPid();
            ALOGE("Permission Denial: "
                    "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
            return PERMISSION_DENIED;
        }
        int n;
        switch (code) {
            ......
            case 1034: {
                auto future = mScheduler->schedule([&] {
                    switch (n = data.readInt32()) {
                        case 0:
                        case 1:
                            FTL_FAKE_GUARD(mStateLock,
                                           enableRefreshRateOverlay(static_cast<bool>(n)));
                            break;
                        default: {
                            reply->writeBool(
                                    FTL_FAKE_GUARD(mStateLock, isRefreshRateOverlayEnabled()));
                        }
                    }
                });

                future.wait();
                return NO_ERROR;
            }
            ......
void SurfaceFlinger::enableRefreshRateOverlay(bool enable)
{
    for (const auto& [ignored, display] : mDisplays) {
        if (display->isInternal() && !display->isVirtual()) {
            if (enable) {
                auto fpsRange = display->refreshRateConfigs().getSupportedRefreshRateRange();
                mRefreshRateOverlay = std::make_shared<RefreshRateOverlay>(fpsRange, mRefreshRateOverlaySpinner);
            } else {
                mRefreshRateOverlay.reset(); //disable RefreshRateOverlay
            }
            display->enableRefreshRateOverlay(mRefreshRateOverlay);
        }
    }
}
            ......

初始化RefreshRateOverlay
frameworks\native\services\surfaceflinger\RefreshRateOverlay.cpp

RefreshRateOverlay::RefreshRateOverlay(FpsRange fpsRange, bool showSpinner)
      : mFpsRange(fpsRange),
        mShowSpinner(showSpinner),
        mSurfaceControl(SurfaceComposerClient::getDefault()
                                ->createSurface(String8("RefreshRateOverlay"), kBufferWidth,
                                                kBufferHeight, PIXEL_FORMAT_RGBA_8888,
                                                ISurfaceComposerClient::eFXSurfaceBufferState)) {
    if (!mSurfaceControl) {
        ALOGE("%s: Failed to create buffer state layer", __func__);
        return;
    }

    createTransaction(mSurfaceControl)
            .setLayer(mSurfaceControl, INT32_MAX - 2)
            .setTrustedOverlay(mSurfaceControl, true)
            .apply();
}
相关推荐
SoulKuyan几秒前
Android客制化------7.0设置壁纸存在的一些问题
android·开发语言·python
timer_0171 分钟前
Swoole v6 正式发布
android
lph0097 分钟前
android compose 串口通信
android
华年源码16 分钟前
基于SpringBoot的旅游网站的设计与实现(源码+数据库+文档)
java·毕业设计·源码·springboot·旅游
程序猿零零漆21 分钟前
SpringCloud系列教程:微服务的未来(十)服务调用、注册中心原理、Nacos注册中心
java·spring cloud·微服务
就爱学编程28 分钟前
力扣刷题:数组OJ篇(上)
java·算法·leetcode
一条小小yu33 分钟前
从零手写缓存框架(二)redis expire 过期原理
java·redis·缓存
XiaoH23343 分钟前
培训机构Day25
java
秋の花1 小时前
【Java】List一部分常见问题
java·windows·list
风月歌1 小时前
java项目之旅游网站的设计与实现(源码+文档)
java·mysql·vue·源码·springboot