Android顯示系統(tǒng)SurfaceFlinger詳解
一、Android系統(tǒng)啟動
Android設(shè)備從按下開機(jī)鍵到桌面顯示畫面,大致過程如下圖流程:

開機(jī)顯示桌面、從桌面點(diǎn)擊 App 圖標(biāo)到 Activity顯示在屏幕上的過程又是怎樣的呢?下面介紹Android系統(tǒng)中的“畫家” - SurfaceFlinger.
SurfaceFlinger 啟動過程:

二、SurfaceFlinger代碼剖析[Android 11]
代碼路徑:/frameworks/native/services/surfaceflinger/
SurfaceFlinger二進(jìn)制分成surfaceflinger可執(zhí)行文件(main入口)和libsurfaceflinger.so庫文件(功能實(shí)現(xiàn)),由main_surfaceflinger.cpp文件編譯而成,Android.bp代碼模塊編譯配置如下:
1.【執(zhí)行文件-surfaceflinger】
...cc_binary {
name: "surfaceflinger",
defaults: ["surfaceflinger_defaults"],
init_rc: ["surfaceflinger.rc"],
srcs: ["main_surfaceflinger.cpp"],
whole_static_libs: [
"libsigchain",
],
shared_libs: [
"android.frameworks.displayservice@1.0",
"android.hardware.configstore-utils",
"android.hardware.configstore@1.0",
"android.hardware.graphics.allocator@2.0",
"libbinder",
"libcutils",
"libdisplayservicehidl",
"libhidlbase",
"libhidltransport",
"liblayers_proto",
"liblog",
"libsurfaceflinger",
"libtimestats_proto",
"libutils",
],
static_libs: [
"libserviceutils",
"libtrace_proto",
],
ldflags: ["-Wl,--export-dynamic"],
// TODO(b/71715793): These version-scripts are required due to the use of
// whole_static_libs to pull in libsigchain. To work, the files had to be
// locally duplicated from their original location
// $ANDROID_ROOT/art/sigchainlib/
multilib: {
lib32: {
version_script: "version-script32.txt",
},
lib64: {
version_script: "version-script64.txt",
},
},
}...
SurfaceFlinger可執(zhí)行二進(jìn)制文件surfaceflinger由main_surfaceflinger.cpp文件獨(dú)立編譯而成,主要負(fù)責(zé)搭建進(jìn)程啟動環(huán)境:
int main(int, char**) {
signal(SIGPIPE, SIG_IGN);
// 從8.0開始,Android提供了hidl機(jī)制,將原先直接由JNI->Native->HAL的接口調(diào)用形式,統(tǒng)一規(guī)范成hidl service/client交互形式。
// 該方式從一方面規(guī)范和統(tǒng)一了Android Framework和HAL的調(diào)用機(jī)制,但實(shí)際從項(xiàng)目維度,這種調(diào)用方式對性能上開銷,將比直接調(diào)用的方式要花費(fèi)更多的時(shí)間。
hardware::configureRpcThreadpool(1 /* maxThreads */,
false /* callerWillJoin */);
startGraphicsAllocatorService();
// When SF is launched in its own process, limit the number of
// binder threads to 4.
ProcessState::self()->setThreadPoolMaxThreadCount(4);
// start the thread pool
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
// 創(chuàng)建SurfaceFlinger對象,由強(qiáng)指針指向。
// SurfaceFlinger繼承RefBase類,所以此處一旦new出對象賦給sp指針后,將立刻出發(fā)SurfaceFlinger類的onFirstRef方法的調(diào)用。
// instantiate surfaceflinger
sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger();
setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
set_sched_policy(0, SP_FOREGROUND);
// Put most SurfaceFlinger threads in the system-background cpuset
// Keeps us from unnecessarily using big cores
// Do this after the binder thread pool init
if (cpusets_enabled()) set_cpuset_policy(0, SP_SYSTEM);
// SurfaceFlinger類正式初始化
// initialize before clients can connect
flinger->init();
// SurfaceFlinger向ServiceManager注冊Binder服務(wù),
// 這樣在其他進(jìn)程中可以通過getService+SERVICE_NAME來獲取SurfaceFlinger服務(wù),繼而可以和SurfaceFlinger類進(jìn)行Binder通信。
// publish surface flinger
sp<IServiceManager> sm(defaultServiceManager());
sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
//里面的new DisplayService()方法調(diào)用HIDL定義接口 Return<sp<IDisplayEventReceiver >> getEventReceiver() override;
startDisplayService(); // dependency on SF getting registered above
if (SurfaceFlinger::setSchedFifo(true) != NO_ERROR) {
ALOGW("Couldn't set to SCHED_FIFO: %s", strerror(errno));
}
// SurfaceFlinger類進(jìn)入主循環(huán)(此處注意SurfaceFlinger類未繼承Threads類,不遵循Threads類的接口執(zhí)行順序)
// run surface flinger in this thread
flinger->run();
return 0;
}
HIDL接口介紹可以參考:https://source.android.google.cn/reference/hidl/
2.【動態(tài)庫-libsurfaceflinger.so】
Android.bp代碼模塊編譯配置如下:
...cc_library_shared {
name: "libsurfaceflinger",
defaults: ["libsurfaceflinger_defaults"],
cflags: [
"-fvisibility=hidden",
"-Werror=format",
"-DREDUCE_VIDEO_WORKLOAD",
"-DUSE_AML_HW_ACTIVE_MODE",
],
srcs: [
":libsurfaceflinger_sources",
],
logtags: ["EventLog/EventLogTags.logtags"],
include_dirs: [
"frameworks/native/vulkan/vkjson",
"frameworks/native/vulkan/include",
"hardware/amlogic/gralloc/amlogic",
"hardware/amlogic/hwcomposer/tvp",
"hardware/amlogic/gralloc",
],
static_libs: [
"libomxutils_static@2",
"libamgralloc_ext_static@2",
],
cppflags: [
"-fwhole-program-vtables", // requires ThinLTO
],
lto: {
thin: true,
},
}...
上面提到的createSurfaceFlinger()中會調(diào)用new SurfaceFlinger(),然后會執(zhí)行到:onFirstRef():
void SurfaceFlinger::onFirstRef()
{
mEventQueue.init(this);
}
onFirstRef() 中會創(chuàng)建 Handler 并初始化: /frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
//MessageQueue.cpp
void MessageQueue::init(const sp<SurfaceFlinger>& flinger)
{
mFlinger = flinger;
mLooper = new Looper(true);
mHandler = new Handler(*this);
}
然后會執(zhí)行到 SurfaceFlinger::init(),該方法主要功能是:
- 初始化 EGL
- 創(chuàng)建 HWComposer
- 初始化非虛擬顯示屏
- 啟動 EventThread 線程
- 啟動開機(jī)動畫
// Do not call property_set on main thread which will be blocked by init
// Use StartPropertySetThread instead.
void SurfaceFlinger::init() {
ALOGI( "SurfaceFlinger's main thread ready to run. "
"Initializing graphics H/W...");
Mutex::Autolock _l(mStateLock);
//?對于CompositionEngine 屬性進(jìn)行設(shè)置, 創(chuàng)建RenderEngine對象
// Get a RenderEngine for the given display / config (can't fail)
// TODO(b/77156734): We need to stop casting and use HAL types when possible.
// Sending maxFrameBufferAcquiredBuffers as the cache size is tightly tuned to single-display.
mCompositionEngine->setRenderEngine(renderengine::RenderEngine::create(
renderengine::RenderEngineCreationArgs::Builder()
.setPixelFormat(static_cast<int32_t>(defaultCompositionPixelFormat))
.setImageCacheSize(maxFrameBufferAcquiredBuffers)
.setUseColorManagerment(useColorManagement)
.setEnableProtectedContext(enable_protected_contents(false))
.setPrecacheToneMapperShaderOnly(false)
.setSupportsBackgroundBlur(mSupportsBlur)
.setContextPriority(useContextPriority
? renderengine::RenderEngine::ContextPriority::HIGH
: renderengine::RenderEngine::ContextPriority::MEDIUM)
.build()));
mCompositionEngine->setTimeStats(mTimeStats);
LOG_ALWAYS_FATAL_IF(mVrFlingerRequestsDisplay,
"Starting with vr flinger active is not currently supported."); //創(chuàng)建HWComposer對象并傳入一個(gè)name屬性,再通過mCompositionEngine->setHwComposer設(shè)置對象屬性。
mCompositionEngine->setHwComposer(getFactory().createHWComposer(getBE().mHwcServiceName));
mCompositionEngine->getHwComposer().setConfiguration(this, getBE().mComposerSequenceId); //processDisplayHotplugEventsLocked(); 處理 任何初始熱插拔和顯示更改的結(jié)果 //在此方法中主要有調(diào)用 initScheduler(displayId);
// Process any initial hotplug and resulting display changes.
processDisplayHotplugEventsLocked();
const auto display = getDefaultDisplayDeviceLocked();
LOG_ALWAYS_FATAL_IF(!display, "Missing internal display after registering composer callback.");
LOG_ALWAYS_FATAL_IF(!getHwComposer().isConnected(*display->getId()),
"Internal display is disconnected.");
if (useVrFlinger) {
auto vrFlingerRequestDisplayCallback = [this](bool requestDisplay) {
// This callback is called from the vr flinger dispatch thread. We
// need to call signalTransaction(), which requires holding
// mStateLock when we're not on the main thread. Acquiring
// mStateLock from the vr flinger dispatch thread might trigger a
// deadlock in surface flinger (see b/66916578), so post a message
// to be handled on the main thread instead.
static_cast<void>(schedule([=] {
ALOGI("VR request display mode: requestDisplay=%d", requestDisplay);
mVrFlingerRequestsDisplay = requestDisplay;
signalTransaction();
}));
};
mVrFlinger = dvr::VrFlinger::Create(getHwComposer().getComposer(),
getHwComposer()
.fromPhysicalDisplayId(*display->getId())
.value_or(0),
vrFlingerRequestDisplayCallback);
if (!mVrFlinger) {
ALOGE("Failed to start vrflinger");
}
}
// initialize our drawing state
mDrawingState = mCurrentState;
// set initial conditions (e.g. unblank default device)
initializeDisplays();
char primeShaderCache[PROPERTY_VALUE_MAX];
property_get("service.sf.prime_shader_cache", primeShaderCache, "1");
if (atoi(primeShaderCache)) {
getRenderEngine().primeCache();
}
// Inform native graphics APIs whether the present timestamp is supported:
const bool presentFenceReliable =
!getHwComposer().hasCapability(hal::Capability::PRESENT_FENCE_IS_NOT_RELIABLE);
mStartPropertySetThread = getFactory().createStartPropertySetThread(presentFenceReliable);
if (mStartPropertySetThread->Start() != NO_ERROR) {
ALOGE("Run StartPropertySetThread failed!");
}
ALOGV("Done initializing");
}
首先看下如何創(chuàng)建 HWComposer:frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
//make_unique 相當(dāng)于 new,(能夠取代new 而且無需 delete pointer,有助于代碼管理)。
std::unique_ptr<HWComposer> DefaultFactory::createHWComposer(const std::string& serviceName) {
return std::make_unique<android::impl::HWComposer>(serviceName);
}
對于CompositionEngine進(jìn)行初始化:
std::unique_ptr<compositionengine::CompositionEngine> DefaultFactory::createCompositionEngine() {
return compositionengine::impl::createCompositionEngine();
}
同樣是通過make_unique創(chuàng)建了 CompositionEngine對象:
std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() {
return std::make_unique<CompositionEngine>();
}
再回到flinger->init()中processDisplayHotplugEventsLocked(); 處理任何初始熱插拔和顯示更改的結(jié)果,在此方法中主要有調(diào)用 initScheduler(displayId):
void SurfaceFlinger::initScheduler(DisplayId primaryDisplayId) {
if (mScheduler) {
// In practice it's not allowed to hotplug in/out the primary display once it's been
// connected during startup, but some tests do it, so just warn and return.
ALOGW("Can't re-init scheduler");
return;
}
auto currentConfig = HwcConfigIndexType(getHwComposer().getActiveConfigIndex(primaryDisplayId));
mRefreshRateConfigs =
std::make_unique<scheduler::RefreshRateConfigs>(getHwComposer().getConfigs(
primaryDisplayId),
currentConfig);
mRefreshRateStats =
std::make_unique<scheduler::RefreshRateStats>(*mRefreshRateConfigs, *mTimeStats,
currentConfig, hal::PowerMode::OFF);
mRefreshRateStats->setConfigMode(currentConfig);
mPhaseConfiguration = getFactory().createPhaseConfiguration(*mRefreshRateConfigs);
// 處創(chuàng)建Scheduler對象
// start the EventThread
mScheduler =
getFactory().createScheduler([this](bool enabled) { setPrimaryVsyncEnabled(enabled); },
*mRefreshRateConfigs, *this);
//創(chuàng)建app鏈接 和 sf鏈接 mAppConnectionHandle =
mScheduler->createConnection("app", mPhaseConfiguration->getCurrentOffsets().late.app,
impl::EventThread::InterceptVSyncsCallback());
mSfConnectionHandle =
mScheduler->createConnection("sf", mPhaseConfiguration->getCurrentOffsets().late.sf,
[this](nsecs_t timestamp) {
mInterceptor->saveVSyncEvent(timestamp);
});
mEventQueue->setEventConnection(mScheduler->getEventConnection(mSfConnectionHandle));
mVSyncModulator.emplace(*mScheduler, mAppConnectionHandle, mSfConnectionHandle,
mPhaseConfiguration->getCurrentOffsets());
mRegionSamplingThread =
new RegionSamplingThread(*this, *mScheduler,
RegionSamplingThread::EnvironmentTimingTunables());
// Dispatch a config change request for the primary display on scheduler
// initialization, so that the EventThreads always contain a reference to a
// prior configuration.
//
// This is a bit hacky, but this avoids a back-pointer into the main SF
// classes from EventThread, and there should be no run-time binder cost
// anyway since there are no connected apps at this point.
const nsecs_t vsyncPeriod =
mRefreshRateConfigs->getRefreshRateFromConfigId(currentConfig).getVsyncPeriod();
mScheduler->onPrimaryDisplayConfigChanged(mAppConnectionHandle, primaryDisplayId.value,
currentConfig, vsyncPeriod);
}
詳細(xì)看下app、sf的鏈接:
Scheduler::ConnectionHandle Scheduler::createConnection(
const char* connectionName, nsecs_t phaseOffsetNs,
impl::EventThread::InterceptVSyncsCallback interceptCallback) {
auto vsyncSource = makePrimaryDispSyncSource(connectionName, phaseOffsetNs);
auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource), std::move(interceptCallback));
return createConnection(std::move(eventThread));
}
可以看到創(chuàng)建了DispSyncSource對象,且構(gòu)造方法傳入了四個(gè)值,dispSync對象,phaseOffset偏移量,traceVsync為true,name就是 app或 sf
DispSyncSource::DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
const char* name)
: mName(name),
mValue(base::StringPrintf("VSYNC-%s", name), 0), //對mValue進(jìn)行了賦值,systrace上我們看到的 VSYNC-app VSYNC-sf 標(biāo)簽就是它
mTraceVsync(traceVsync), //mTraceVsync為true,在onDispSyncEvent方法中
mVsyncOnLabel(base::StringPrintf("VsyncOn-%s", name)),
mDispSync(dispSync),
mPhaseOffset(base::StringPrintf("VsyncOffset-%s", name), phaseOffset) //對mPhaseOffset進(jìn)行初始化 vsync信號到來時(shí)候,sf、app的偏移量
所以我們在systrace上面看到的 VSYNC-app/VSYNC-sf 駝峰 0 1變化,來源于這個(gè)。

創(chuàng)建EventThread對象,傳入sf 或 app 相關(guān)聯(lián)的vsyncSource對象:
auto eventThread = std::make_unique<impl::EventThread>(std::move(vsyncSource), std::move(interceptCallback));
說明:
- 1)每個(gè)ConnectionHandle 對象里有個(gè) id,作為 Scheduler 對象中 mConnections 屬性(map<id, Connection>)的鍵值,Connection 對象中又包含 ConnectionHandle、EventThreadConnection、EventThread 3個(gè)屬性。
- 2)mScheduler->getEventConnection(mSfConnectionHandle) 中,以 mSfConnectionHandle 的 id 為鍵值,在 Scheduler 的 mConnections(unordered_map<int64_t, Connection>)中找到對應(yīng)的Connection,并返回其 EventThreadConnection 成員屬性。
- 3)getHwComposer().registerCallback() 中,依次調(diào)用 HwComposer、Device 的registerCallback() 方法,并在 Device 中 將 SurfaceFlinger 對象封裝到 ComposerCallbackBridge 中;對于封裝后的對象,依次調(diào)用 Composer、IComposerClient 的 registerCallback() 方法,注入到 IComposerClient 的實(shí)現(xiàn)類中。
相關(guān)問題:
- ① 屏幕刷新速率比系統(tǒng)幀速率快:
此時(shí),在前緩沖區(qū)內(nèi)容全部映射到屏幕上之后,后緩沖區(qū)尚未準(zhǔn)備好下一幀,屏幕將無法讀取下一幀,所以只能繼續(xù)顯示當(dāng)前一幀的圖形,造成一幀顯示多次,也就是卡頓。
- ② 系統(tǒng)幀速率比屏幕刷新率快
此時(shí),屏幕未完全把前緩沖區(qū)的一幀映射到屏幕,而系統(tǒng)已經(jīng)在后緩沖區(qū)準(zhǔn)備好了下一幀,并要求讀取下一幀到屏幕,將會導(dǎo)致屏幕上半部分是上一幀的圖形,而下半部分是下一幀的圖形,造成屏幕上顯示多幀,也就是屏幕撕裂。
為了解決上述問題,Android顯示系統(tǒng)一般會有多級緩沖,即在屏幕刷新的同時(shí)在另外一個(gè)buffer準(zhǔn)備下一幀數(shù)據(jù),以此提高性能:

- 前緩沖區(qū):用來顯示內(nèi)容到屏幕的幀緩沖區(qū)
- 后緩沖區(qū):用于后臺合成下一幀圖形的幀緩沖區(qū)
- 垂直同步(VSync):當(dāng)屏幕從緩沖區(qū)掃描完一幀到屏幕上之后,開始掃描下一幀之前,發(fā)出的一個(gè)同步信號,該信號用來切換前緩沖區(qū)和后緩沖區(qū)。
- 屏幕刷新率(HZ):代表屏幕在一秒內(nèi)刷新屏幕的次數(shù),Android手機(jī)一般為60HZ(也就是1秒刷新60幀,大約16.67毫秒刷新1幀)
- 系統(tǒng)幀速率(FPS):代表了系統(tǒng)在一秒內(nèi)合成的幀數(shù),該值的大小由系統(tǒng)算法和硬件決定。
3. 服務(wù)啟動配置文件:/frameworks/native/services/surfaceflinger/surfaceflinger.rc
上面發(fā)現(xiàn)服務(wù)配置文件也在Android.mk中被加載:LOCAL_INIT_RC := surfaceflinger.rc
service surfaceflinger /system/bin/surfaceflinger
class core animation
user system
group graphics drmrpc readproc
onrestart restart zygote
writepid /dev/stune/foreground/tasks
socket pdx/system/vr/display/client stream 0666 system graphics u:object_r:pdx_display_client_endpoint_socket:s0
socket pdx/system/vr/display/manager stream 0666 system graphics u:object_r:pdx_display_manager_endpoint_socket:s0
socket pdx/system/vr/display/vsync stream 0666 system graphics u:object_r:pdx_display_vsync_endpoint_socket:s0
4. Surface 創(chuàng)建過程

Surface 創(chuàng)建的過程就是 Activity 顯示的過程,在 ActivityThread.handleResumeActivity() 中調(diào)用了 Activity.makeVisible()具體實(shí)現(xiàn):
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();//此處 getWindowManager 獲取的是 WindowManagerImpl 對象
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
WindowManagerImpl.java:
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mDisplay, mParentWindow);
}
WindowManagerGlobal.java:
public void addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) {
...
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
//創(chuàng)建 ViewRootImpl
ViewRootImpl root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
//設(shè)置 View
root.setView(view, wparams, panelParentView);
...
}
創(chuàng)建 ViewRootImpl:
public ViewRootImpl(Context context, Display display) {
//獲取 IWindowSession的代理類
this(context, display, WindowManagerGlobal.getWindowSession(),
false /* useSfChoreographer */);
}
WindowManagerGlobal.java:
@UnsupportedAppUsage
public static IWindowSession getWindowSession() {
synchronized (WindowManagerGlobal.class) {
if (sWindowSession == null) {
try {
// Emulate the legacy behavior. The global instance of InputMethodManager
// was instantiated here.
// TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
//獲取 IMS 的代理類
InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
IWindowManager windowManager = getWindowManagerService();
//經(jīng)過 Binder 調(diào)用,最終調(diào)用 WMS
sWindowSession = windowManager.openSession(
new IWindowSessionCallback.Stub() {
@Override
public void onAnimatorScaleChanged(float scale) {
ValueAnimator.setDurationScale(scale);
}
});
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
return sWindowSession;
}
}
WindowManagerService.openSession:
// -------------------------------------------------------------
// IWindowManager API
// -------------------------------------------------------------
@Override
public IWindowSession openSession(IWindowSessionCallback callback) {
//創(chuàng)建session對象
return new Session(this, callback);
}
再次經(jīng)過 Binder 將數(shù)據(jù)寫回 app 進(jìn)程,則獲取的便是 Session 的代理對象 IWindowSession。
創(chuàng)建完 ViewRootImpl 對象后,接下來調(diào)用該對象的 setView() 方法:
ViewRootImpl:
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
synchronized (this) {
requestLayout(); //詳見下面分析
...
//通過 Binder調(diào)用,進(jìn)入 system 進(jìn)程的 Session
res = mWindowSession.addToDisplayAsUser(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(), userId, mTmpFrame, mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, mAttachInfo.mDisplayCutout, inputChannel, mTempInsets, mTempControls);
...
}
}
Session.java
@Override
public int addToDisplayAsUser(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, int userId, Rect outFrame,
Rect outContentInsets, Rect outStableInsets,
DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel,
InsetsState outInsetsState, InsetsSourceControl[] outActiveControls) {
//調(diào)用WMS的addWindow方法
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outFrame,
outContentInsets, outStableInsets, outDisplayCutout, outInputChannel,
outInsetsState, outActiveControls, userId);
}
WindowManagerService.java:
public int addWindow(Session session, IWindow client, int seq,
LayoutParams attrs, int viewVisibility, int displayId, Rect outFrame,
Rect outContentInsets, Rect outStableInsets,
DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel,
InsetsState outInsetsState, InsetsSourceControl[] outActiveControls,
int requestUserId) {
Arrays.fill(outActiveControls, null);
int[] appOp = new int[1];
final boolean isRoundedCornerOverlay = (attrs.privateFlags
& PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY) != 0;
int res = mPolicy.checkAddPermission(attrs.type, isRoundedCornerOverlay, attrs.packageName,
appOp);
if (res != WindowManagerGlobal.ADD_OKAY) {
return res;
}
WindowState parentWindow = null;
final int callingUid = Binder.getCallingUid();
final int callingPid = Binder.getCallingPid();
final long origId = Binder.clearCallingIdentity();
final int type = attrs.type;
synchronized (mGlobalLock) {
if (!mDisplayReady) {
throw new IllegalStateException("Display has not been initialialized");
}
...
//創(chuàng)建 WindowState
final WindowState win = new WindowState(this, session, client, token, parentWindow,
appOp[0], seq, attrs, viewVisibility, session.mUid, userId,
session.mCanAddInternalSystemWindow);
if (win.mDeathRecipient == null) {
// Client has apparently died, so there is no reason to
// continue.
ProtoLog.w(WM_ERROR, "Adding window client %s"
+ " that is dead, aborting.", client.asBinder());
return WindowManagerGlobal.ADD_APP_EXITING;
}
if (win.getDisplayContent() == null) {
ProtoLog.w(WM_ERROR, "Adding window to Display that has been removed.");
return WindowManagerGlobal.ADD_INVALID_DISPLAY;
}
// 調(diào)整 WindowManager的LayoutParams 參數(shù)
final DisplayPolicy displayPolicy = displayContent.getDisplayPolicy();
displayPolicy.adjustWindowParamsLw(win, win.mAttrs, callingPid, callingUid);
res = displayPolicy.validateAddingWindowLw(attrs, callingPid, callingUid);
if (res != WindowManagerGlobal.ADD_OKAY) {
return res;
}
// 打開輸入通道
final boolean openInputChannels = (outInputChannel != null
&& (attrs.inputFeatures & INPUT_FEATURE_NO_INPUT_CHANNEL) == 0);
if (openInputChannels) {
win.openInputChannel(outInputChannel);
}
...
displayContent.getInputMonitor().setUpdateInputWindowsNeededLw();
boolean focusChanged = false;
//當(dāng)該窗口能接收按鍵事件,則更新聚焦窗口
if (win.canReceiveKeys()) {
focusChanged = updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS,
false /*updateInputWindows*/);
if (focusChanged) {
imMayMove = false;
}
}
if (imMayMove) {
displayContent.computeImeTarget(true /* updateImeTarget */);
}
...
}
Binder.restoreCallingIdentity(origId);
return res;
}
創(chuàng)建 SurfaceSession 對象,并將當(dāng)前 Session 添加到 WMS.mSessions 成員變量。
Session.java:
void windowAddedLocked(String packageName) {
mPackageName = packageName;
mRelayoutTag = "relayoutWindow: " + mPackageName;
if (mSurfaceSession == null) {
if (DEBUG) {
Slog.v(TAG_WM, "First window added to " + this + ", creating SurfaceSession");
}
mSurfaceSession = new SurfaceSession();
ProtoLog.i(WM_SHOW_TRANSACTIONS, " NEW SURFACE SESSION %s", mSurfaceSession);
mService.mSessions.add(this);
if (mLastReportedAnimatorScale != mService.getCurrentAnimatorScale()) {
mService.dispatchNewAnimatorScaleLocked(this);
}
}
mNumWindow++;
}
SurfaceSession 的創(chuàng)建會調(diào)用 JNI,在 JNI 調(diào)用 nativeCreate()。
android_view_SurfaceSession.cpp:
static jlong nativeCreate(JNIEnv* env, jclass clazz) {
SurfaceComposerClient* client = new SurfaceComposerClient();
client->incStrong((void*)nativeCreate);
return reinterpret_cast<jlong>(client);
}
static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
jstring nameStr, jint w, jint h, jint format, jint flags, jlong parentObject,
jobject metadataParcel) {
ScopedUtfChars name(env, nameStr);
sp<SurfaceComposerClient> client;
if (sessionObj != NULL) {
client = android_view_SurfaceSession_getClient(env, sessionObj);
} else {
client = SurfaceComposerClient::getDefault();
}
SurfaceControl *parent = reinterpret_cast<SurfaceControl*>(parentObject);
sp<SurfaceControl> surface;
LayerMetadata metadata;
Parcel* parcel = parcelForJavaObject(env, metadataParcel);
if (parcel && !parcel->objectsCount()) {
status_t err = metadata.readFromParcel(parcel);
if (err != NO_ERROR) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"Metadata parcel has wrong format");
}
}
status_t err = client->createSurfaceChecked(
String8(name.c_str()), w, h, format, &surface, flags, parent, std::move(metadata));
if (err == NAME_NOT_FOUND) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return 0;
} else if (err != NO_ERROR) {
jniThrowException(env, OutOfResourcesException, NULL);
return 0;
}
surface->incStrong((void *)nativeCreate);
return reinterpret_cast<jlong>(surface.get());
}
通過以上JNI接口獲取SurfaceComposerClient 對象,作為跟 SurfaceFlinger 通信的代理對象。
void SurfaceComposerClient::onFirstRef() {
//getComposerService() 將返回 SF 的 Binder 代理端的 BpSurfaceFlinger 對象
sp<ISurfaceComposer> sf(ComposerService::getComposerService());
if (sf != nullptr && mStatus == NO_INIT) {
sp<ISurfaceComposerClient> conn;
//調(diào)用 SF 的 createConnection()
conn = sf->createConnection();
if (conn != nullptr) {
mClient = conn;
mStatus = NO_ERROR;
}
}
}
比如截屏接口就會通過SurfaceControl調(diào)用到其中的capture 接口:
status_t ScreenshotClient::capture(const sp<IBinder>& display, ui::Dataspace reqDataSpace,
ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
ui::Rotation rotation, bool captureSecureLayers,
sp<GraphicBuffer>* outBuffer, bool& outCapturedSecureLayers) {
sp<ISurfaceComposer> s(ComposerService::getComposerService());
if (s == nullptr) return NO_INIT;
status_t ret = s->captureScreen(display, outBuffer, outCapturedSecureLayers, reqDataSpace,
reqPixelFormat, sourceCrop, reqWidth, reqHeight,
useIdentityTransform, rotation, captureSecureLayers);
if (ret != NO_ERROR) {
return ret;
}
return ret;
}
然后具體看下核心的SurfaceFlinger實(shí)現(xiàn):
SurfaceFlinger.cpp:
sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() {
//創(chuàng)建一個(gè)Client
const sp<Client> client = new Client(this);
return client->initCheck() == NO_ERROR ? client : nullptr;
}
回到之前,創(chuàng)建完 ViewRootImpl 對象后,接下來調(diào)用該對象的 setView() 方法。在 setView() 中調(diào)用了 requestLayout() 方法,現(xiàn)在具體來看下這個(gè)方法調(diào)用流程:
@Override
public void requestLayout() {
if (!mHandlingLayoutInLayoutRequest) {
checkThread();
mLayoutRequested = true;
scheduleTraversals();
}
}
@UnsupportedAppUsage
void scheduleTraversals() {
if (!mTraversalScheduled) {
mTraversalScheduled = true;
mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); //啟動TraversalRunnable
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
notifyRendererOfFramePending();
pokeDrawLockIfNeeded();
}
}
final class TraversalRunnable implements Runnable {
@Override
public void run() {
doTraversal();
}
}
void doTraversal() {
if (mTraversalScheduled) {
mTraversalScheduled = false;
mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
if (mProfile) {
Debug.startMethodTracing("ViewAncestor");
}
//調(diào)用performTraversals performTraversals();
if (mProfile) {
Debug.stopMethodTracing();
mProfile = false;
}
}
}
private void performTraversals() {
// cache mView since it is used so much below...
final View host = mView; //它就是 DecorView
...
if (mFirst || windowShouldResize || viewVisibilityChanged || cutoutChanged || params != null
|| mForceNextWindowRelayout) {
mForceNextWindowRelayout = false;
if (isViewVisible) {
// If this window is giving internal insets to the window
// manager, and it is being added or changing its visibility,
// then we want to first give the window manager "fake"
// insets to cause it to effectively ignore the content of
// the window during layout. This avoids it briefly causing
// other windows to resize/move based on the raw frame of the
// window, waiting until we can finish laying out this window
// and get back to the window manager with the ultimately
// computed insets.
insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
}
...
try {
if (DEBUG_LAYOUT) {
Log.i(mTag, "host=w:" + host.getMeasuredWidth() + ", h:" +
host.getMeasuredHeight() + ", params=" + params);
}
if (mAttachInfo.mThreadedRenderer != null) {
// relayoutWindow may decide to destroy mSurface. As that decision
// happens in WindowManager service, we need to be defensive here
// and stop using the surface in case it gets destroyed.
if (mAttachInfo.mThreadedRenderer.pause()) {
// Animations were running so we need to push a frame
// to resume them
mDirty.set(0, 0, mWidth, mHeight);
}
mChoreographer.mFrameInfo.addFlags(FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED);
}
// 關(guān)鍵函數(shù)relayoutWindow
relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
if (DEBUG_LAYOUT) Log.v(mTag, "relayout: frame=" + frame.toShortString()
+ " cutout=" + mPendingDisplayCutout.get().toString()
+ " surface=" + mSurface);
// If the pending {@link MergedConfiguration} handed back from
// {@link #relayoutWindow} does not match the one last reported,
// WindowManagerService has reported back a frame from a configuration not yet
// handled by the client. In this case, we need to accept the configuration so we
// do not lay out and draw with the wrong configuration.
if (!mPendingMergedConfiguration.equals(mLastReportedMergedConfiguration)) {
if (DEBUG_CONFIGURATION) Log.v(mTag, "Visible with new config: "
+ mPendingMergedConfiguration.getMergedConfiguration());
performConfigurationChange(mPendingMergedConfiguration, !mFirst,
INVALID_DISPLAY /* same display */);
updatedConfiguration = true;
}
} catch (RemoteException e) {
}
...
}
boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible;
if (!cancelDraw) {
if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
for (int i = 0; i < mPendingTransitions.size(); ++i) {
mPendingTransitions.get(i).startChangingAnimations();
}
mPendingTransitions.clear();
}
//開始繪制,其中調(diào)用了draw(fullRedrawNeeded);
performDraw();
} else {
if (isViewVisible) {
// Try again
scheduleTraversals();
} else if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
for (int i = 0; i < mPendingTransitions.size(); ++i) {
mPendingTransitions.get(i).endChangingAnimations();
}
mPendingTransitions.clear();
}
}
if (mAttachInfo.mContentCaptureEvents != null) {
notifyContentCatpureEvents();
}
mIsInTraversal = false;
}
再看下relayoutWindow的實(shí)現(xiàn):
... if (mSurfaceControl.isValid()) {
if (!useBLAST()) {
//先創(chuàng)建一個(gè)本地Surface,然后調(diào)用copyFrom 將SurfaceControl信息拷貝到Surface中
mSurface.copyFrom(mSurfaceControl);
} else {
final Surface blastSurface = getOrCreateBLASTSurface(mSurfaceSize.x,
mSurfaceSize.y);
// If blastSurface == null that means it hasn't changed since the last time we
// called. In this situation, avoid calling transferFrom as we would then
// inc the generation ID and cause EGL resources to be recreated.
if (blastSurface != null) {
mSurface.transferFrom(blastSurface);
}
}
} else {
destroySurface();
}....
SurfaceControl 類可以看作是一個(gè) wrapper 類,最后會執(zhí)行 copyFrom() 將其返回給 App 客戶端:
@UnsupportedAppUsage
public void copyFrom(SurfaceControl other) {
if (other == null) {
throw new IllegalArgumentException("other must not be null");
}
long surfaceControlPtr = other.mNativeObject;
if (surfaceControlPtr == 0) {
throw new NullPointerException(
"null SurfaceControl native object. Are you using a released SurfaceControl?");
}
//通過JNI獲取源SurfaceControl
long newNativeObject = nativeGetFromSurfaceControl(mNativeObject, surfaceControlPtr);
synchronized (mLock) {
if (newNativeObject == mNativeObject) {
return;
}
if (mNativeObject != 0) {
nativeRelease(mNativeObject);
}
//保存到全局mNativeObject用于外部調(diào)用
setNativeObjectLocked(newNativeObject);
}
}
Surface 顯示過程總結(jié):
在 App 進(jìn)程中創(chuàng)建 PhoneWindow 后會創(chuàng)建 ViewRoot。ViewRoot 的創(chuàng)建會創(chuàng)建一個(gè) Surface,這個(gè) Surface 其實(shí)是空的,通過與 WindowManagerService 通信 copyFrom() 一個(gè)NativeSurface 與 SurfaceFlinger 通信時(shí)。
關(guān)于Native Window:
Native Window是OpenGL與本地窗口系統(tǒng)之間搭建了橋梁。整個(gè)GUI系統(tǒng)至少需要兩種本地窗口:
- (1)面向管理者(SurfaceFlinger)
SurfaceFlinger是系統(tǒng)中所有UI界面的管理者,需要直接或間接的持有“本地窗口”,此本地窗口是FramebufferNativeWindow。
- (2)面向應(yīng)用程序
這類本地窗口是Surface。
正常情況按照SDK向?qū)葾PK應(yīng)用程序,是采用Skia等第三方圖形庫,而對于希望使用OpenGL ES來完成復(fù)雜界面渲染的應(yīng)用開發(fā)者來說,Android也提供封裝的GLSurfaceView(或其他方式)來實(shí)現(xiàn)圖形顯示。
①FramebufferNativeWindow
EGL需要通過本地窗口來為OpenGL/OpenGL ES創(chuàng)建環(huán)境。由于OpenGL/ES對多平臺支持,考慮到兼容性和移植性。不同平臺的本地窗口EGLNativeWindowType數(shù)據(jù)類型不同。
Android平臺的數(shù)據(jù)類型是ANativeWindow,像是一份“協(xié)議”,規(guī)定了一個(gè)本地窗口的形態(tài)和功能。ANativeWindow是FramebufferNativeWindow的父類。
Android中,由于多緩沖技術(shù),EGLNativeWindowType所管理的緩沖區(qū)最少2個(gè),最大3個(gè)。
FramebufferNativeWindow初始化需要Golloc支持,步驟如下:
- 加載GRALLOC_HARDWARE_MODULE_ID模塊,參見上節(jié)。
- 分別打開fb和gralloc設(shè)備,打開后的設(shè)備由全局變量fbDev和grDev管理。
- 根據(jù)設(shè)備的屬性來給FramebufferNativeWindow賦初值。
- 根據(jù)FramebufferNativeWindow的實(shí)現(xiàn)來填充ANativeWindow中的“協(xié)議”
- 其他一些必要的初始化
②應(yīng)用程序的本地窗口 - Surface
Surface也繼承了ANativeWindow
class Surface : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
Surface是面向Android系統(tǒng)中所有UI應(yīng)用程序的,即它承擔(dān)著應(yīng)用進(jìn)程中的UI顯示需求。
需要面向上層實(shí)現(xiàn)(主要是Java層)提供繪制圖像的畫板。SurfaceFlinger需要收集系統(tǒng)中所有應(yīng)用程序繪制的圖像數(shù)據(jù),然后集中顯示到物理屏幕上。Surface需要扮演相應(yīng)角色,本質(zhì)上還是由SurfaceFlinger服務(wù)統(tǒng)一管理的,涉及到很多跨進(jìn)程的通信細(xì)節(jié)。
③Surface的創(chuàng)建
Surface將通過mGraphicBufferProducer來獲取buffer,這些緩沖區(qū)會被記錄在mSlots中數(shù)據(jù)中。mGraphicBufferProducer這一核心成員的初始化流程如下:
ViewRootImpl持有一個(gè)Java層的Surface對象(mSurface)。
ViewRootImpl向WindowManagerService發(fā)起relayout請求,此時(shí)mSurface被賦予真正的有效值,將輾轉(zhuǎn)生成的SurfaceControl通過Surface.copyFrom()函數(shù)復(fù)制到mSurface中。
由此,Surface由SurfaceControl管理,SurfaceControl由SurfaceComposerClient創(chuàng)建。SurfaceComposerClient獲得的匿名Binder是ISurfaceComposer,其服務(wù)端實(shí)現(xiàn)是SurfaceFlinger。而Surface依賴的IGraphicBufferProducer對象在Service端的實(shí)現(xiàn)是BufferQueue。
class SurfaceFlinger : public BinderService<SurfaceFlinger>, //在ServiceManager中注冊為SurfaceFlinger public BnSurfaceComposer,//實(shí)現(xiàn)的接口卻叫ISurfaceComposer
④SurfaceFlinger服務(wù)框架:
Buffer,Consumer,Producer是“生產(chǎn)者-消費(fèi)者”模型中的3個(gè)參與對象,如何協(xié)調(diào)好它們的工作是應(yīng)用程序能否正常顯示UI的關(guān)鍵。
Buffer是BufferQueue,Producer是應(yīng)用程序,Consumer是SurfaceFlinger。

Surface內(nèi)部提供一個(gè)BufferQueue,與上層和SurfaceFlinger形成一個(gè)生產(chǎn)者消費(fèi)者模型,上層對應(yīng)Producer,SurfaceFlinger對應(yīng)Consumer。三者通過Buffer產(chǎn)生聯(lián)系,每個(gè)Buffer都有四種狀態(tài):
- Free:可被上層使用;
- Dequeued:出列,正在被上層使用;
- Queued:入列,已完成上層繪制,等待SurfaceFlinger合成;
- Acquired:被獲取,SurfaceFlinger正持有該Buffer進(jìn)行合成;
如此循環(huán),形成一個(gè)Buffer被循環(huán)使用的過程(FREE-> DEQUEUED->QUEUED->ACQUIRED->FREE)。
BufferQueue中的mSlots數(shù)組用于管理期內(nèi)的緩沖區(qū),最大容器是32。數(shù)據(jù)緩沖區(qū)的空間是動態(tài)分配的,應(yīng)用程序與SurfaceFlinger都是使用OpenGL ES來完成UI顯示。Layer類在SurfaceFlinger中表示“層”,通俗地講就是代表了一個(gè)“畫面”,最終物理屏幕上的顯示結(jié)果就是通過對系統(tǒng)中同時(shí)存在的所有“畫面”進(jìn)行處理疊加而成。
到此這篇關(guān)于Android顯示系統(tǒng)SurfaceFlinger詳解的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Android SDK-在64位Linux中使用需要注意的問題
本篇文章是對Android SDK-在64位Linux中使用需要注意的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android實(shí)現(xiàn)滑動加載數(shù)據(jù)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)滑動加載數(shù)據(jù)的方法,實(shí)例分析了Android通過滑動實(shí)現(xiàn)動態(tài)加載數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Android控件之ImageView用法實(shí)例分析
這篇文章主要介紹了Android控件之ImageView用法,以實(shí)例形式較為詳細(xì)的分析了ImageView控件用于顯示圖片的使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Android Studio自動排版的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Android Studio自動排版的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架功能
這篇文章主要介紹了Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Android項(xiàng)目實(shí)戰(zhàn)之百度地圖地點(diǎn)簽到功能
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)之百度地圖地點(diǎn)簽到功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android實(shí)現(xiàn)listview動態(tài)加載數(shù)據(jù)分頁的兩種方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)listview動態(tài)加載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06

