壁纸作为显示的最底层窗口它是怎么显示的
1. SurfaceFlinger layer层级
锁屏状态dump SurfaceFlinger ,adb shell dumpsys SurfaceFlinger
Display 0 (active) HWC layers:
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Layer name
Z | Window Type | Layer Type | Comp Type | Transform | Disp Frame (LTRB) | Source Crop (LTRB) | Frame Rate (Explicit) (Seamlessness) [Focused]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Wallpaper BBQ wrapper#58
rel 0 | 0 | 0 | CLIENT | 0 | 0 0 1080 2408 | 0.0 0.0 1080.0 2408.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
StatusBar#105
rel 0 | 2000 | 21 | CLIENT | 0 | 0 0 1080 76 | 0.0 0.0 1080.0 76.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
NotificationShade#656
rel 0 | 2040 | 17 | CLIENT | 0 | 0 0 1080 2408 | 0.0 0.0 1080.0 2408.0 | [*]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
NavigationBar0#101
rel 0 | 2019 | 21 | CLIENT | 0 | 0 2364 1080 2408 | 0.0 0.0 1080.0 44.0 | [ ]
可以看到壁纸layer显示在最底层,其上依次是状态栏layer,锁屏layer,导航栏窗口layer。
void SurfaceFlinger::dumpHwcLayersMinidumpLockedLegacy(std::string& result) const {
for (const auto& [token, display] : mDisplays) {
const auto displayId = HalDisplayId::tryCast(display->getId());
if (!displayId) {
continue;
}
StringAppendF(&result, "Display %s (%s) HWC layers:\n", to_string(*displayId).c_str(),
displayId == mActiveDisplayId ? "active" : "inactive");
Layer::miniDumpHeader(result);
const DisplayDevice& ref = *display;
mDrawingState.traverseInZOrder([&](Layer* layer) { layer->miniDumpLegacy(result, ref); });
result.append("\n");
}
}
mDrawingState是按照Layer.z属性排序的
frameworks/native/services/surfaceflinger/LayerVector.cpp
int LayerVector::do_compare(const void* lhs, const void* rhs) const
{
// sort layers per layer-stack, then by z-order and finally by sequence
const auto& l = *reinterpret_cast<const sp<Layer>*>(lhs);
const auto& r = *reinterpret_cast<const sp<Layer>*>(rhs);
const auto& lState = l->getDrawingState();
const auto& rState = r->getDrawingState();
const auto ls = lState.layerStack;
const auto rs = rState.layerStack;
if (ls != rs)
return (ls > rs) ? 1 : -1;
int32_t lz = lState.z;
int32_t rz = rState.z;
if (lz != rz)
return (lz > rz) ? 1 : -1;
}
2. system_server窗口层级
通常我们看到的窗口层级如下
frameworks/base/services/core/java/com/android/server/policy/WindowManagerPolicy.java
default int getWindowLayerFromTypeLw(int type, boolean canAddInternalSystemWindow,
boolean roundedCornerOverlay) {
// Always put the rounded corner layer to the top most.
if (roundedCornerOverlay && canAddInternalSystemWindow) {
return getMaxWindowLayer();
}
if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
return APPLICATION_LAYER;
}
switch (type) {
case TYPE_WALLPAPER:
// wallpaper is at the bottom, though the window manager may move it.
return 1;
}
/frameworks/base/services/core/java/com/android/server/wm/WindowState.java
mBaseLayer = mPolicy.getWindowLayerLw(this)
* TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
大概层级
DisplayArea添加到DisplayContent
frameworks/base/services/core/java/com/android/server/wm/DisplayAreaPolicyBuilder.java
void instantiateChildren(DisplayArea<DisplayArea> parent, DisplayArea.Tokens[] areaForLayer,
int level, Map<Feature, List<DisplayArea<WindowContainer>>> areas) {
mChildren.sort(Comparator.comparingInt(pendingArea -> pendingArea.mMinLayer));
for (int i = 0; i < mChildren.size(); i++) {
final PendingArea child = mChildren.get(i);
final DisplayArea area = child.createArea(parent, areaForLayer);
if (area == null) {
// TaskDisplayArea and ImeContainer can be set at different hierarchy, so it can
// be null.
continue;
}
parent.addChild(area, WindowContainer.POSITION_TOP);
if (child.mFeature != null) {
areas.get(child.mFeature).add(area);
}
child.instantiateChildren(area, areaForLayer, level + 1, areas);
}
}
创建37个layer并添加进displayContent中
frameworks/base/services/core/java/com/android/server/policy/WindowManagerPolicy.java
default int getMaxWindowLayer() {
return 36;
}
frameworks/base/services/core/java/com/android/server/wm/DisplayAreaPolicyBuilder.java
private void build(@Nullable List<HierarchyBuilder> displayAreaGroupHierarchyBuilders) {
final WindowManagerPolicy policy = mRoot.mWmService.mPolicy;
final int maxWindowLayerCount = policy.getMaxWindowLayer() + 1;
final DisplayArea.Tokens[] displayAreaForLayer =
new DisplayArea.Tokens[maxWindowLayerCount];
.
.
.
root.instantiateChildren(mRoot, displayAreaForLayer, 0, featureAreas);
// Notify the root that we have finished attaching all the DisplayAreas. Cache all the
// feature related collections there for fast access.
mRoot.onHierarchyBuilt(mFeatures, displayAreaForLayer, featureAreas);
}
通过assignLayer来设置surfacecontrol的layer属性及(LayerState的z属性)
frameworks/base/services/core/java/com/android/server/wm/WindowContainer.java
void assignChildLayers(Transaction t) {
int layer = 0;
// We use two passes as a way to promote children which
// need Z-boosting to the end of the list.
for (int j = 0; j < mChildren.size(); ++j) {
final WindowContainer wc = mChildren.get(j);
wc.assignChildLayers(t);
if (!wc.needsZBoost()) {
wc.assignLayer(t, layer++);
}
}
for (int j = 0; j < mChildren.size(); ++j) {
final WindowContainer wc = mChildren.get(j);
if (wc.needsZBoost()) {
wc.assignLayer(t, layer++);
}
}
if (mOverlayHost != null) {
mOverlayHost.setLayer(t, layer++);
}
}
3. 通过SurfaceComposerClient 传递到SurfaceFlinger ( sf->setTransactionState)
frameworks/native/libs/gui/SurfaceComposerClient.cpp
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
const sp<SurfaceControl>& sc, int32_t z) {
layer_state_t* s = getLayerState(sc);
if (!s) {
mStatus = BAD_INDEX;
return *this;
}
s->what |= layer_state_t::eLayerChanged;
s->what &= ~layer_state_t::eRelativeLayerChanged;
s->z = z;
registerSurfaceControlForCallback(sc);
return *this;
}