Android13实时刷新频率的实现代码(完整代码)
设置->开发者选项->显示刷新频率
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刷新频率内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android性能优化之利用Rxlifecycle解决RxJava内存泄漏详解
RxJava作为一种响应式编程框架,是目前编程界网红,可谓是家喻户晓,其简洁的编码风格、易用易读的链式方法调用、强大的异步支持等使得RxJava被广泛使用。2017-01-01Android基于OpenGL的GLSurfaceView创建一个Activity实现方法
这篇文章主要介绍了Android基于OpenGL的GLSurfaceView创建一个Activity实现方法,结合实例形式分析了Android中OpenGL图形操作类GLSurfaceView的功能、用法及相关使用技巧,需要的朋友可以参考下2016-10-10
最新评论