Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android刷新频率

Android13实时刷新频率的实现代码(完整代码)

作者:龙之叶

文章介绍了Android 13中如何通过设置开发者选项显示屏幕刷新频率,具体涉及到Settings应用中的代码和SurfaceFlinger服务的实现,感兴趣的朋友一起看看吧

设置->开发者选项->显示刷新频率
packages/apps/Settings/src/com/android/settings/development/ShowRefreshRatePreferenceController.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();
}

到此这篇关于Android13实时刷新频率的实现代码的文章就介绍到这了,更多相关Android刷新频率内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文