diff options
author | 2024-08-28 15:42:17 -0700 | |
---|---|---|
committer | 2024-08-28 15:42:17 -0700 | |
commit | a12c5c7fb8f09dc6cf61cd30c854025dbb679040 (patch) | |
tree | 3ba396424e0f1017823a9c25b353e33b766c90f9 | |
parent | d3dae29bbac2d6e63a6690d98c9b3569ecdbfa25 (diff) |
Remove legacy layer hierarchy
Legacy layer objects are now all stored in the mLegacyLayer map. They
are created on the binder thread and destroyed on the main thread.
Cloned layers do not have a legacy layer.
This change also keeps the create layer alive until main thread wakes up
to make the lifetime a bit simpler to track.
Flag: EXEMPT flag removal
Bug: 330785038
Test: presubmit
Change-Id: I4132e7a7c373c823ad914209955f7b0f55af008d
-rw-r--r-- | services/surfaceflinger/FrontEnd/Update.h | 17 | ||||
-rw-r--r-- | services/surfaceflinger/Layer.cpp | 375 | ||||
-rw-r--r-- | services/surfaceflinger/Layer.h | 83 | ||||
-rw-r--r-- | services/surfaceflinger/LayerVector.cpp | 29 | ||||
-rw-r--r-- | services/surfaceflinger/LayerVector.h | 5 | ||||
-rw-r--r-- | services/surfaceflinger/SurfaceFlinger.cpp | 113 | ||||
-rw-r--r-- | services/surfaceflinger/SurfaceFlinger.h | 20 | ||||
-rw-r--r-- | services/surfaceflinger/tests/unittests/SchedulerTest.cpp | 1 | ||||
-rw-r--r-- | services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h | 2 | ||||
-rw-r--r-- | services/surfaceflinger/tests/unittests/TransactionTracingTest.cpp | 1 | ||||
-rw-r--r-- | services/surfaceflinger/tests/unittests/TunnelModeEnabledReporterTest.cpp | 5 |
11 files changed, 17 insertions, 634 deletions
diff --git a/services/surfaceflinger/FrontEnd/Update.h b/services/surfaceflinger/FrontEnd/Update.h index e5cca8fa95..4af27ab84d 100644 --- a/services/surfaceflinger/FrontEnd/Update.h +++ b/services/surfaceflinger/FrontEnd/Update.h @@ -22,28 +22,13 @@ #include "RequestedLayerState.h" #include "TransactionState.h" -namespace android { -struct LayerCreatedState { - LayerCreatedState(const wp<Layer>& layer, const wp<Layer>& parent, bool addToRoot) - : layer(layer), initialParent(parent), addToRoot(addToRoot) {} - wp<Layer> layer; - // Indicates the initial parent of the created layer, only used for creating layer in - // SurfaceFlinger. If nullptr, it may add the created layer into the current root layers. - wp<Layer> initialParent; - // Indicates whether the layer getting created should be added at root if there's no parent - // and has permission ACCESS_SURFACE_FLINGER. If set to false and no parent, the layer will - // be added offscreen. - bool addToRoot; -}; -} // namespace android - namespace android::surfaceflinger::frontend { // Atomic set of changes affecting layer state. These changes are queued in binder threads and // applied every vsync. struct Update { std::vector<TransactionState> transactions; - std::vector<LayerCreatedState> layerCreatedStates; + std::vector<sp<Layer>> legacyLayers; std::vector<std::unique_ptr<frontend::RequestedLayerState>> newLayers; std::vector<LayerCreationArgs> layerCreationArgs; std::vector<std::pair<uint32_t, std::string /* debugName */>> destroyedHandles; diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp index 636f7bdabf..9e2c22a174 100644 --- a/services/surfaceflinger/Layer.cpp +++ b/services/surfaceflinger/Layer.cpp @@ -282,48 +282,6 @@ void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) { } } -void Layer::removeFromCurrentState() { - if (!mRemovedFromDrawingState) { - mRemovedFromDrawingState = true; - mFlinger->mScheduler->deregisterLayer(this); - } - updateTrustedPresentationState(nullptr, nullptr, -1 /* time_in_ms */, true /* leaveState*/); - - mFlinger->markLayerPendingRemovalLocked(sp<Layer>::fromExisting(this)); -} - -sp<Layer> Layer::getRootLayer() { - sp<Layer> parent = getParent(); - if (parent == nullptr) { - return sp<Layer>::fromExisting(this); - } - return parent->getRootLayer(); -} - -void Layer::onRemovedFromCurrentState() { - // Use the root layer since we want to maintain the hierarchy for the entire subtree. - auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current); - std::sort(layersInTree.begin(), layersInTree.end()); - - REQUIRE_MUTEX(mFlinger->mStateLock); - traverse(LayerVector::StateSet::Current, - [&](Layer* layer) REQUIRES(layer->mFlinger->mStateLock) { - layer->removeFromCurrentState(); - layer->removeRelativeZ(layersInTree); - }); -} - -void Layer::addToCurrentState() { - if (mRemovedFromDrawingState) { - mRemovedFromDrawingState = false; - mFlinger->mScheduler->registerLayer(this, FrameRateCompatibility::Default); - } - - for (const auto& child : mCurrentChildren) { - child->addToCurrentState(); - } -} - // --------------------------------------------------------------------------- // set-up // --------------------------------------------------------------------------- @@ -592,7 +550,6 @@ void Layer::prepareBasicGeometryCompositionState() { snapshot->alpha = alpha; snapshot->backgroundBlurRadius = getBackgroundBlurRadius(); snapshot->blurRegions = getBlurRegions(); - snapshot->stretchEffect = getStretchEffect(); } void Layer::prepareGeometryCompositionState() { @@ -826,33 +783,6 @@ void Layer::setTransactionFlags(uint32_t mask) { mTransactionFlags |= mask; } -bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) { - ssize_t idx = mCurrentChildren.indexOf(childLayer); - if (idx < 0) { - return false; - } - if (childLayer->setLayer(z)) { - mCurrentChildren.removeAt(idx); - mCurrentChildren.add(childLayer); - return true; - } - return false; -} - -bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer, - const sp<IBinder>& relativeToHandle, int32_t relativeZ) { - ssize_t idx = mCurrentChildren.indexOf(childLayer); - if (idx < 0) { - return false; - } - if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) { - mCurrentChildren.removeAt(idx); - mCurrentChildren.add(childLayer); - return true; - } - return false; -} - bool Layer::setLayer(int32_t z) { if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false; mDrawingState.sequence++; @@ -959,46 +889,6 @@ bool Layer::setAlpha(float alpha) { return true; } -bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) { - if (!mDrawingState.bgColorLayer && alpha == 0) { - return false; - } - mDrawingState.sequence++; - mDrawingState.modified = true; - setTransactionFlags(eTransactionNeeded); - - if (!mDrawingState.bgColorLayer && alpha != 0) { - // create background color layer if one does not yet exist - uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect; - std::string name = mName + "BackgroundColorLayer"; - mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer( - surfaceflinger::LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), flags, - LayerMetadata())); - - // add to child list - addChild(mDrawingState.bgColorLayer); - mFlinger->mLayersAdded = true; - // set up SF to handle added color layer - if (isRemovedFromCurrentState()) { - MUTEX_ALIAS(mFlinger->mStateLock, mDrawingState.bgColorLayer->mFlinger->mStateLock); - mDrawingState.bgColorLayer->onRemovedFromCurrentState(); - } - mFlinger->setTransactionFlags(eTransactionNeeded); - } else if (mDrawingState.bgColorLayer && alpha == 0) { - MUTEX_ALIAS(mFlinger->mStateLock, mDrawingState.bgColorLayer->mFlinger->mStateLock); - mDrawingState.bgColorLayer->reparent(nullptr); - mDrawingState.bgColorLayer = nullptr; - return true; - } - - mDrawingState.bgColorLayer->setColor(color); - mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min()); - mDrawingState.bgColorLayer->setAlpha(alpha); - mDrawingState.bgColorLayer->setDataspace(dataspace); - - return true; -} - bool Layer::setCornerRadius(float cornerRadius) { if (mDrawingState.cornerRadius == cornerRadius) return false; @@ -1149,22 +1039,6 @@ bool Layer::setStretchEffect(const StretchEffect& effect) { return true; } -StretchEffect Layer::getStretchEffect() const { - if (mDrawingState.stretchEffect.hasEffect()) { - return mDrawingState.stretchEffect; - } - - sp<Layer> parent = getParent(); - if (parent != nullptr) { - auto effect = parent->getStretchEffect(); - if (effect.hasEffect()) { - // TODO(b/179047472): Map it? Or do we make the effect be in global space? - return effect; - } - } - return StretchEffect{}; -} - void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info, nsecs_t postTime, gui::GameMode gameMode) { mDrawingState.postTime = postTime; @@ -1439,32 +1313,6 @@ void Layer::onDisconnect() { mFlinger->mFrameTracer->onDestroy(layerId); } -size_t Layer::getDescendantCount() const { - size_t count = 0; - for (const sp<Layer>& child : mDrawingChildren) { - count += 1 + child->getChildrenCount(); - } - return count; -} - -void Layer::addChild(const sp<Layer>& layer) { - mFlinger->mSomeChildrenChanged = true; - setTransactionFlags(eTransactionNeeded); - - mCurrentChildren.add(layer); - layer->setParent(sp<Layer>::fromExisting(this)); -} - -ssize_t Layer::removeChild(const sp<Layer>& layer) { - mFlinger->mSomeChildrenChanged = true; - setTransactionFlags(eTransactionNeeded); - - layer->setParent(nullptr); - const auto removeResult = mCurrentChildren.remove(layer); - - return removeResult; -} - void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) { for (const sp<Layer>& child : mDrawingChildren) { child->mDrawingParent = newParent; @@ -1475,39 +1323,6 @@ void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) { } } -bool Layer::reparent(const sp<IBinder>& newParentHandle) { - sp<Layer> newParent; - if (newParentHandle != nullptr) { - newParent = LayerHandle::getLayer(newParentHandle); - if (newParent == nullptr) { - ALOGE("Unable to promote Layer handle"); - return false; - } - if (newParent == this) { - ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str()); - return false; - } - } - - sp<Layer> parent = getParent(); - if (parent != nullptr) { - parent->removeChild(sp<Layer>::fromExisting(this)); - } - - if (newParentHandle != nullptr) { - newParent->addChild(sp<Layer>::fromExisting(this)); - if (!newParent->isRemovedFromCurrentState()) { - addToCurrentState(); - } else { - onRemovedFromCurrentState(); - } - } else { - onRemovedFromCurrentState(); - } - - return true; -} - bool Layer::setColorTransform(const mat4& matrix) { static const mat4 identityMatrix = mat4(); @@ -1590,167 +1405,6 @@ __attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::mak return traverse; } -/** - * Negatively signed relatives are before 'this' in Z-order. - */ -void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) { - // In the case we have other layers who are using a relative Z to us, makeTraversalList will - // produce a new list for traversing, including our relatives, and not including our children - // who are relatives of another surface. In the case that there are no relative Z, - // makeTraversalList returns our children directly to avoid significant overhead. - // However in this case we need to take the responsibility for filtering children which - // are relatives of another surface here. - bool skipRelativeZUsers = false; - const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers); - - size_t i = 0; - for (; i < list.size(); i++) { - const auto& relative = list[i]; - if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) { - continue; - } - - if (relative->getZ(stateSet) >= 0) { - break; - } - relative->traverseInZOrder(stateSet, visitor); - } - - visitor(this); - for (; i < list.size(); i++) { - const auto& relative = list[i]; - - if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) { - continue; - } - relative->traverseInZOrder(stateSet, visitor); - } -} - -/** - * Positively signed relatives are before 'this' in reverse Z-order. - */ -void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet, - const LayerVector::Visitor& visitor) { - // See traverseInZOrder for documentation. - bool skipRelativeZUsers = false; - LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers); - - int32_t i = 0; - for (i = int32_t(list.size()) - 1; i >= 0; i--) { - const auto& relative = list[i]; - - if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) { - continue; - } - - if (relative->getZ(stateSet) < 0) { - break; - } - relative->traverseInReverseZOrder(stateSet, visitor); - } - visitor(this); - for (; i >= 0; i--) { - const auto& relative = list[i]; - - if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) { - continue; - } - - relative->traverseInReverseZOrder(stateSet, visitor); - } -} - -void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) { - visitor(this); - const LayerVector& children = - state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren; - for (const sp<Layer>& child : children) { - child->traverse(state, visitor); - } -} - -void Layer::traverseChildren(const LayerVector::Visitor& visitor) { - for (const sp<Layer>& child : mDrawingChildren) { - visitor(child.get()); - } -} - -LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet, - const std::vector<Layer*>& layersInTree) { - LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid, - "makeTraversalList received invalid stateSet"); - const bool useDrawing = stateSet == LayerVector::StateSet::Drawing; - const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren; - const State& state = useDrawing ? mDrawingState : mDrawingState; - - LayerVector traverse(stateSet); - for (const wp<Layer>& weakRelative : state.zOrderRelatives) { - sp<Layer> strongRelative = weakRelative.promote(); - // Only add relative layers that are also descendents of the top most parent of the tree. - // If a relative layer is not a descendent, then it should be ignored. - if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) { - traverse.add(strongRelative); - } - } - - for (const sp<Layer>& child : children) { - const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState; - // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a - // descendent of the top most parent of the tree. If it's not a descendent, then just add - // the child here since it won't be added later as a relative. - if (std::binary_search(layersInTree.begin(), layersInTree.end(), - childState.zOrderRelativeOf.promote().get())) { - continue; - } - traverse.add(child); - } - - return traverse; -} - -void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree, - LayerVector::StateSet stateSet, - const LayerVector::Visitor& visitor) { - const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree); - - size_t i = 0; - for (; i < list.size(); i++) { - const auto& relative = list[i]; - if (relative->getZ(stateSet) >= 0) { - break; - } - relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor); - } - - visitor(this); - for (; i < list.size(); i++) { - const auto& relative = list[i]; - relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor); - } -} - -std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) { - const bool useDrawing = stateSet == LayerVector::StateSet::Drawing; - const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren; - - std::vector<Layer*> layersInTree = {this}; - for (size_t i = 0; i < children.size(); i++) { - const auto& child = children[i]; - std::vector<Layer*> childLayers = child->getLayersInTree(stateSet); - layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend()); - } - - return layersInTree; -} - -void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet, - const LayerVector::Visitor& visitor) { - std::vector<Layer*> layersInTree = getLayersInTree(stateSet); - std::sort(layersInTree.begin(), layersInTree.end()); - traverseChildrenInZOrderInner(layersInTree, stateSet, visitor); -} - ui::Transform Layer::getTransform() const { return mEffectiveTransform; } @@ -1861,27 +1515,6 @@ bool Layer::findInHierarchy(const sp<Layer>& l) { return false; } -void Layer::commitChildList() { - for (size_t i = 0; i < mCurrentChildren.size(); i++) { - const auto& child = mCurrentChildren[i]; - child->commitChildList(); - } - mDrawingChildren = mCurrentChildren; - mDrawingParent = mCurrentParent; - if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) { - auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote(); - if (zOrderRelativeOf == nullptr) return; - if (findInHierarchy(zOrderRelativeOf)) { - ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(), - zOrderRelativeOf->mName.c_str()); - ALOGE("Severing rel Z loop, potentially dangerous"); - mDrawingState.isRelativeOf = false; - zOrderRelativeOf->removeZOrderRelative(wp<Layer>::fromExisting(this)); - } - } -} - - void Layer::setInputInfo(const WindowInfo& info) { mDrawingState.inputInfo = info; mDrawingState.touchableRegionCrop = @@ -2048,10 +1681,6 @@ void Layer::writeToProtoCommonState(perfetto::protos::LayerProto* layerInfo, [&]() { return layerInfo->mutable_destination_frame(); }); } -bool Layer::isRemovedFromCurrentState() const { - return mRemovedFromDrawingState; -} - // Applies the given transform to the region, while protecting against overflows caused by any // offsets. If applying the offset in the transform to any of the Rects in the region would result // in an overflow, they are not added to the output Region. @@ -2375,10 +2004,6 @@ bool Layer::updateMirrorInfo(const std::deque<Layer*>& cloneRootsPendingUpdates) // There's no need to remove from drawingState when the layer is offscreen since currentState is // copied to drawingState for the root layer. So the clonedChild is always removed from // drawingState and then needs to be added back each traversal. - if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) { - addChildToDrawing(mClonedChild); - } - mClonedChild->updateClonedDrawingState(clonedLayersMap); mClonedChild->updateClonedChildren(sp<Layer>::fromExisting(this), clonedLayersMap); mClonedChild->updateClonedRelatives(clonedLayersMap); diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h index f6eed6332b..e1792b5b2f 100644 --- a/services/surfaceflinger/Layer.h +++ b/services/surfaceflinger/Layer.h @@ -297,7 +297,6 @@ public: virtual bool setMetadata(const LayerMetadata& data); virtual void setChildrenDrawingParent(const sp<Layer>&); - virtual bool reparent(const sp<IBinder>& newParentHandle) REQUIRES(mFlinger->mStateLock); virtual bool setColorTransform(const mat4& matrix); virtual mat4 getColorTransform() const; virtual bool hasColorTransform() const; @@ -326,8 +325,6 @@ public: gui::GameMode gameMode); bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& /*handles*/, bool willPresent); - virtual bool setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) - REQUIRES(mFlinger->mStateLock); virtual bool setColorSpaceAgnostic(const bool agnostic); virtual bool setDimmingEnabled(const bool dimmingEnabled); virtual bool setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint); @@ -638,8 +635,6 @@ public: return {getLayerStack(), isInternalDisplayOverlay()}; } - bool isRemovedFromCurrentState() const; - perfetto::protos::LayerProto* writeToProto(perfetto::protos::LayersProto& layersProto, uint32_t traceFlags); void writeCompositionStateToProto(perfetto::protos::LayerProto* layerProto, @@ -671,22 +666,6 @@ public: */ void removeRelativeZ(const std::vector<Layer*>& layersInTree); - /* - * Remove from current state and mark for removal. - */ - void removeFromCurrentState() REQUIRES(mFlinger->mStateLock); - - /* - * called with the state lock from a binder thread when the layer is - * removed from the current list to the pending removal list - */ - void onRemovedFromCurrentState() REQUIRES(mFlinger->mStateLock); - - /* - * Called when the layer is added back to the current state list. - */ - void addToCurrentState(); - inline const State& getDrawingState() const { return mDrawingState; } inline State& getDrawingState() { return mDrawingState; } @@ -715,57 +694,9 @@ public: // is ready to acquire a buffer. ui::Transform::RotationFlags getFixedTransformHint() const; - /** - * Traverse this layer and it's hierarchy of children directly. Unlike traverseInZOrder - * which will not emit children who have relativeZOrder to another layer, this method - * just directly emits all children. It also emits them in no particular order. - * So this method is not suitable for graphical operations, as it doesn't represent - * the scene state, but it's also more efficient than traverseInZOrder and so useful for - * book-keeping. - */ - void traverse(LayerVector::StateSet, const LayerVector::Visitor&); - void traverseInReverseZOrder(LayerVector::StateSet, const LayerVector::Visitor&); - void traverseInZOrder(LayerVector::StateSet, const LayerVector::Visitor&); - void traverseChildren(const LayerVector::Visitor&); - - /** - * Traverse only children in z order, ignoring relative layers that are not children of the - * parent. - */ - void traverseChildrenInZOrder(LayerVector::StateSet, const LayerVector::Visitor&); - - size_t getDescendantCount() const; - size_t getChildrenCount() const { return mDrawingChildren.size(); } bool isHandleAlive() const { return mHandleAlive; } bool onHandleDestroyed() { return mHandleAlive = false; } - - // ONLY CALL THIS FROM THE LAYER DTOR! - // See b/141111965. We need to add current children to offscreen layers in - // the layer dtor so as not to dangle layers. Since the layer has not - // committed its transaction when the layer is destroyed, we must add - // current children. This is safe in the dtor as we will no longer update - // the current state, but should not be called anywhere else! - LayerVector& getCurrentChildren() { return mCurrentChildren; } - - void addChild(const sp<Layer>&); - // Returns index if removed, or negative value otherwise - // for symmetry with Vector::remove - ssize_t removeChild(const sp<Layer>& layer); - sp<Layer> getParent() const { return mCurrentParent.promote(); } - - // Should be called with the surfaceflinger statelock held - bool isAtRoot() const { return mIsAtRoot; } - void setIsAtRoot(bool isAtRoot) { mIsAtRoot = isAtRoot; } - - bool hasParent() const { return getParent() != nullptr; } Rect getScreenBounds(bool reduceTransparentRegion = true) const; - bool setChildLayer(const sp<Layer>& childLayer, int32_t z); - bool setChildRelativeLayer(const sp<Layer>& childLayer, - const sp<IBinder>& relativeToHandle, int32_t relativeZ); - - // Copy the current list of children to the drawing state. Called by - // SurfaceFlinger to complete a transaction. - void commitChildList(); int32_t getZ(LayerVector::StateSet) const; /** @@ -855,7 +786,6 @@ public: } bool setStretchEffect(const StretchEffect& effect); - StretchEffect getStretchEffect() const; bool setBufferCrop(const Rect& /* bufferCrop */); bool setDestinationFrame(const Rect& /* destinationFrame */); @@ -1086,19 +1016,6 @@ private: const DisplayDevice&) const; aidl::android::hardware::graphics::composer3::Composition getCompositionType( const compositionengine::OutputLayer*) const; - /** - * Returns an unsorted vector of all layers that are part of this tree. - * That includes the current layer and all its descendants. - */ - std::vector<Layer*> getLayersInTree(LayerVector::StateSet); - /** - * Traverses layers that are part of this tree in the correct z order. - * layersInTree must be sorted before calling this method. - */ - void traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree, - LayerVector::StateSet, const LayerVector::Visitor&); - LayerVector makeChildrenTraversalList(LayerVector::StateSet, - const std::vector<Layer*>& layersInTree); bool propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool overrideChildren, bool* transactionNeeded); diff --git a/services/surfaceflinger/LayerVector.cpp b/services/surfaceflinger/LayerVector.cpp index f52e60deda..ff0a955ad6 100644 --- a/services/surfaceflinger/LayerVector.cpp +++ b/services/surfaceflinger/LayerVector.cpp @@ -61,35 +61,6 @@ int LayerVector::do_compare(const void* lhs, const void* rhs) const return (l->sequence > r->sequence) ? 1 : -1; } -void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const { - for (size_t i = 0; i < size(); i++) { - const auto& layer = (*this)[i]; - auto& state = layer->getDrawingState(); - if (state.isRelativeOf) { - continue; - } - layer->traverseInZOrder(stateSet, visitor); - } -} - -void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const { - for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) { - const auto& layer = (*this)[i]; - auto& state = layer->getDrawingState(); - if (state.isRelativeOf) { - continue; - } - layer->traverseInReverseZOrder(stateSet, visitor); - } -} - -void LayerVector::traverse(const Visitor& visitor) const { - for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) { - const auto& layer = (*this)[i]; - layer->traverse(mStateSet, visitor); - } -} - } // namespace android // TODO(b/129481165): remove the #pragma below and fix conversion issues diff --git a/services/surfaceflinger/LayerVector.h b/services/surfaceflinger/LayerVector.h index a531f4fd95..38dc11d3bc 100644 --- a/services/surfaceflinger/LayerVector.h +++ b/services/surfaceflinger/LayerVector.h @@ -46,11 +46,8 @@ public: // Sorts layer by layer-stack, Z order, and finally creation order (sequence). int do_compare(const void* lhs, const void* rhs) const override; - using Visitor = std::function<void(Layer*)>; - void traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const; - void traverseInZOrder(StateSet stateSet, const Visitor& visitor) const; - void traverse(const Visitor& visitor) const; + private: const StateSet mStateSet; }; diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index 71344fdbe3..56f9d95f61 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -2371,7 +2371,7 @@ bool SurfaceFlinger::updateLayerSnapshots(VsyncId vsyncId, nsecs_t frameTimeNs, { // TODO(b/238781169) lockless queue this and keep order. std::scoped_lock<std::mutex> lock(mCreatedLayersLock); - update.layerCreatedStates = std::move(mCreatedLayers); + update.legacyLayers = std::move(mCreatedLayers); mCreatedLayers.clear(); update.newLayers = std::move(mNewLayers); mNewLayers.clear(); @@ -2390,11 +2390,8 @@ bool SurfaceFlinger::updateLayerSnapshots(VsyncId vsyncId, nsecs_t frameTimeNs, } mLayerLifecycleManager.applyTransactions(update.transactions); mLayerLifecycleManager.onHandlesDestroyed(update.destroyedHandles); - for (auto& legacyLayer : update.layerCreatedStates) { - sp<Layer> layer = legacyLayer.layer.promote(); - if (layer) { - mLegacyLayers[layer->sequence] = layer; - } + for (auto& legacyLayer : update.legacyLayers) { + mLegacyLayers[legacyLayer->sequence] = legacyLayer; } mLayerHierarchyBuilder.update(mLayerLifecycleManager); } @@ -4336,36 +4333,9 @@ void SurfaceFlinger::initScheduler(const sp<const DisplayDevice>& display) { void SurfaceFlinger::doCommitTransactions() { SFTRACE_CALL(); - - if (!mLayersPendingRemoval.isEmpty()) { - // Notify removed layers now that they can't be drawn from - for (const auto& l : mLayersPendingRemoval) { - // Ensure any buffers set to display on any children are released. - if (l->isRemovedFromCurrentState()) { - l->latchAndReleaseBuffer(); - } - - // If a layer has a parent, we allow it to out-live it's handle - // with the idea that the parent holds a reference and will eventually - // be cleaned up. However no one cleans up the top-level so we do so - // here. - if (l->isAtRoot()) { - l->setIsAtRoot(false); - mCurrentState.layersSortedByZ.remove(l); - } - } - mLayersPendingRemoval.clear(); - } - mDrawingState = mCurrentState; mCurrentState.colorMatrixChanged = false; - if (mVisibleRegionsDirty) { - for (const auto& rootLayer : mDrawingState.layersSortedByZ) { - rootLayer->commitChildList(); - } - } - if (mLayerMirrorRoots.size() > 0) { std::deque<Layer*> pendingUpdates; pendingUpdates.insert(pendingUpdates.end(), mLayerMirrorRoots.begin(), @@ -4373,9 +4343,6 @@ void SurfaceFlinger::doCommitTransactions() { std::vector<Layer*> needsUpdating; for (Layer* cloneRoot : mLayerMirrorRoots) { pendingUpdates.pop_front(); - if (cloneRoot->isRemovedFromCurrentState()) { - continue; - } if (cloneRoot->updateMirrorInfo(pendingUpdates)) { } else { needsUpdating.push_back(cloneRoot); @@ -4427,7 +4394,7 @@ status_t SurfaceFlinger::addClientLayer(LayerCreationArgs& args, const sp<IBinde args.layerIdToMirror = LayerHandle::getLayerId(args.mirrorLayerHandle.promote()); { std::scoped_lock<std::mutex> lock(mCreatedLayersLock); - mCreatedLayers.emplace_back(layer, parent, args.addToRoot); + mCreatedLayers.emplace_back(layer); mNewLayers.emplace_back(std::make_unique<frontend::RequestedLayerState>(args)); args.mirrorLayerHandle.clear(); args.parentHandle.clear(); @@ -5268,33 +5235,22 @@ status_t SurfaceFlinger::createEffectLayer(const LayerCreationArgs& args, sp<IBi return NO_ERROR; } -void SurfaceFlinger::markLayerPendingRemovalLocked(const sp<Layer>& layer) { - mLayersPendingRemoval.add(layer); - mLayersRemoved = true; - setTransactionFlags(eTransactionNeeded); -} - void SurfaceFlinger::onHandleDestroyed(BBinder* handle, sp<Layer>& layer, uint32_t layerId) { { - std::scoped_lock<std::mutex> lock(mCreatedLayersLock); - mDestroyedHandles.emplace_back(layerId, layer->getDebugName()); - } - - { // Used to remove stalled transactions which uses an internal lock. ftl::FakeGuard guard(kMainThreadContext); mTransactionHandler.onLayerDestroyed(layerId); } - JankTracker::flushJankData(layerId); - Mutex::Autolock lock(mStateLock); - markLayerPendingRemovalLocked(layer); + std::scoped_lock<std::mutex> lock(mCreatedLayersLock); + mDestroyedHandles.emplace_back(layerId, layer->getDebugName()); + + Mutex::Autolock stateLock(mStateLock); layer->onHandleDestroyed(); mBufferCountTracker.remove(handle); layer.clear(); - - setTransactionFlags(eTransactionFlushNeeded); + setTransactionFlags(eTransactionFlushNeeded | eTransactionNeeded); } void SurfaceFlinger::initializeDisplays() { @@ -7692,18 +7648,6 @@ void SurfaceFlinger::traverseLegacyLayers(const LayerVector::Visitor& visitor) c // --------------------------------------------------------------------------- -void SurfaceFlinger::State::traverse(const LayerVector::Visitor& visitor) const { - layersSortedByZ.traverse(visitor); -} - -void SurfaceFlinger::State::traverseInZOrder(const LayerVector::Visitor& visitor) const { - layersSortedByZ.traverseInZOrder(stateSet, visitor); -} - -void SurfaceFlinger::State::traverseInReverseZOrder(const LayerVector::Visitor& visitor) const { - layersSortedByZ.traverseInReverseZOrder(stateSet, visitor); -} - ftl::Optional<scheduler::FrameRateMode> SurfaceFlinger::getPreferredDisplayMode( PhysicalDisplayId displayId, DisplayModeId defaultModeId) const { if (const auto schedulerMode = mScheduler->getPreferredDisplayMode(); @@ -7872,16 +7816,12 @@ status_t SurfaceFlinger::getDesiredDisplayModeSpecs(const sp<IBinder>& displayTo void SurfaceFlinger::onLayerFirstRef(Layer* layer) { mNumLayers++; - if (!layer->isRemovedFromCurrentState()) { - mScheduler->registerLayer(layer, scheduler::FrameRateCompatibility::Default); - } + mScheduler->registerLayer(layer, scheduler::FrameRateCompatibility::Default); } void SurfaceFlinger::onLayerDestroyed(Layer* layer) { mNumLayers--; - if (!layer->isRemovedFromCurrentState()) { - mScheduler->deregisterLayer(layer); - } + mScheduler->deregisterLayer(layer); if (mTransactionTracing) { mTransactionTracing->onLayerRemoved(layer->getSequence()); } @@ -8034,37 +7974,6 @@ int SurfaceFlinger::getMaxAcquiredBufferCountForRefreshRate(Fps refreshRate) con return calculateMaxAcquiredBufferCount(refreshRate, presentLatency); } -void SurfaceFlinger::handleLayerCreatedLocked(const LayerCreatedState& state, VsyncId vsyncId) { - sp<Layer> layer = state.layer.promote(); - if (!layer) { - ALOGD("Layer was destroyed soon after creation %p", state.layer.unsafe_get()); - return; - } - MUTEX_ALIAS(mStateLock, layer->mFlinger->mStateLock); - - sp<Layer> parent; - bool addToRoot = state.addToRoot; - if (state.initialParent != nullptr) { - parent = state.initialParent.promote(); - if (parent == nullptr) { - ALOGD("Parent was destroyed soon after creation %p", state.initialParent.unsafe_get()); - addToRoot = false; - } - } - - if (parent == nullptr && addToRoot) { - layer->setIsAtRoot(true); - mCurrentState.layersSortedByZ.add(layer); - } else if (parent == nullptr) { - layer->onRemovedFromCurrentState(); - } else if (parent->isRemovedFromCurrentState()) { - parent->addChild(layer); - layer->onRemovedFromCurrentState(); - } else { - parent->addChild(layer); - } -} - void SurfaceFlinger::sample() { if (!mLumaSampling || !mRegionSamplingThread) { return; diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index 282c8cf803..cee9160e61 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -389,11 +389,10 @@ private: class State { public: - explicit State(LayerVector::StateSet set) : stateSet(set), layersSortedByZ(set) {} + explicit State(LayerVector::StateSet set) : stateSet(set) {} State& operator=(const State& other) { // We explicitly don't copy stateSet so that, e.g., mDrawingState // always uses the Drawing StateSet. - layersSortedByZ = other.layersSortedByZ; displays = other.displays; colorMatrixChanged = other.colorMatrixChanged; if (colorMatrixChanged) { @@ -405,7 +404,6 @@ private: } const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid; - LayerVector layersSortedByZ; // TODO(b/241285876): Replace deprecated DefaultKeyedVector with ftl::SmallMap. DefaultKeyedVector<wp<IBinder>, DisplayDeviceState> displays; @@ -425,10 +423,6 @@ private: mat4 colorMatrix; ShadowSettings globalShadowSettings; - - void traverse(const LayerVector::Visitor& visitor) const; - void traverseInZOrder(const LayerVector::Visitor& visitor) const; - void traverseInReverseZOrder(const LayerVector::Visitor& visitor) const; }; // Keeps track of pending buffers per layer handle in the transaction queue or current/drawing @@ -847,8 +841,6 @@ private: status_t mirrorDisplay(DisplayId displayId, const LayerCreationArgs& args, gui::CreateSurfaceResult& outResult); - void markLayerPendingRemovalLocked(const sp<Layer>& layer) REQUIRES(mStateLock); - // add a layer to SurfaceFlinger status_t addClientLayer(LayerCreationArgs& args, const sp<IBinder>& handle, const sp<Layer>& layer, const wp<Layer>& parentLayer, @@ -1202,7 +1194,6 @@ private: State mCurrentState{LayerVector::StateSet::Current}; std::atomic<int32_t> mTransactionFlags = 0; std::atomic<uint32_t> mUniqueTransactionId = 1; - SortedVector<sp<Layer>> mLayersPendingRemoval; // Buffers that have been discarded by clients and need to be evicted from per-layer caches so // the graphics memory can be immediately freed. @@ -1392,13 +1383,6 @@ private: std::unordered_map<DisplayId, sp<HdrLayerInfoReporter>> mHdrLayerInfoListeners GUARDED_BY(mStateLock); - mutable std::mutex mCreatedLayersLock; - - // A temporay pool that store the created layers and will be added to current state in main - // thread. - std::vector<LayerCreatedState> mCreatedLayers GUARDED_BY(mCreatedLayersLock); - void handleLayerCreatedLocked(const LayerCreatedState&, VsyncId) REQUIRES(mStateLock); - std::atomic<ui::Transform::RotationFlags> mActiveDisplayTransformHint; // Must only be accessed on the main thread. @@ -1439,6 +1423,8 @@ private: frontend::LayerHierarchyBuilder mLayerHierarchyBuilder GUARDED_BY(kMainThreadContext); frontend::LayerSnapshotBuilder mLayerSnapshotBuilder GUARDED_BY(kMainThreadContext); + mutable std::mutex mCreatedLayersLock; + std::vector<sp<Layer>> mCreatedLayers GUARDED_BY(mCreatedLayersLock); std::vector<std::pair<uint32_t, std::string>> mDestroyedHandles GUARDED_BY(mCreatedLayersLock); std::vector<std::unique_ptr<frontend::RequestedLayerState>> mNewLayers GUARDED_BY(mCreatedLayersLock); diff --git a/services/surfaceflinger/tests/unittests/SchedulerTest.cpp b/services/surfaceflinger/tests/unittests/SchedulerTest.cpp index 358f6b0cf5..45ca7e2f59 100644 --- a/services/surfaceflinger/tests/unittests/SchedulerTest.cpp +++ b/services/surfaceflinger/tests/unittests/SchedulerTest.cpp @@ -884,7 +884,6 @@ TEST_F(AttachedChoreographerTest, removedWhenLayerIsGone) { mScheduler->createDisplayEventConnection(Cycle::Render, {}, layer->getHandle()); layer.clear(); - mFlinger.mutableLayersPendingRemoval().clear(); EXPECT_TRUE(mScheduler->mutableAttachedChoreographers().empty()); } diff --git a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h index a6a275804d..710b5cc623 100644 --- a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h +++ b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h @@ -721,7 +721,6 @@ public: } auto& mutableMinAcquiredBuffers() { return SurfaceFlinger::minAcquiredBuffers; } - auto& mutableLayersPendingRemoval() { return mFlinger->mLayersPendingRemoval; } auto& mutableLayerSnapshotBuilder() { return mFlinger->mLayerSnapshotBuilder; }; auto fromHandle(const sp<IBinder>& handle) { return LayerHandle::getLayer(handle); } @@ -789,7 +788,6 @@ public: mutableDisplays().clear(); mutableCurrentState().displays.clear(); mutableDrawingState().displays.clear(); - mFlinger->mLayersPendingRemoval.clear(); mFlinger->mScheduler.reset(); mFlinger->mCompositionEngine->setHwComposer(std::unique_ptr<HWComposer>()); mFlinger->mRenderEngine = std::unique_ptr<renderengine::RenderEngine>(); diff --git a/services/surfaceflinger/tests/unittests/TransactionTracingTest.cpp b/services/surfaceflinger/tests/unittests/TransactionTracingTest.cpp index 7bf167498b..f8f08c78fd 100644 --- a/services/surfaceflinger/tests/unittests/TransactionTracingTest.cpp +++ b/services/surfaceflinger/tests/unittests/TransactionTracingTest.cpp @@ -20,6 +20,7 @@ #include <gui/SurfaceComposerClient.h> #include <cstdint> #include "Client.h" +#include "Layer.h" #include <layerproto/LayerProtoHeader.h> #include "FrontEnd/LayerCreationArgs.h" diff --git a/services/surfaceflinger/tests/unittests/TunnelModeEnabledReporterTest.cpp b/services/surfaceflinger/tests/unittests/TunnelModeEnabledReporterTest.cpp index 9f6065b683..e27af0e62c 100644 --- a/services/surfaceflinger/tests/unittests/TunnelModeEnabledReporterTest.cpp +++ b/services/surfaceflinger/tests/unittests/TunnelModeEnabledReporterTest.cpp @@ -128,12 +128,10 @@ TEST_F(TunnelModeEnabledReporterTest, callsNewListenerWithFreshInformation) { NativeHandle::create(reinterpret_cast<native_handle_t*>(DEFAULT_SIDEBAND_STREAM), false); layer->setSidebandStream(stream, FrameTimelineInfo{}, 20, gui::GameMode::Unsupported); - mFlinger.mutableCurrentState().layersSortedByZ.add(layer); mTunnelModeEnabledReporter->updateTunnelModeStatus(); mTunnelModeEnabledReporter->addListener(mTunnelModeEnabledListener); EXPECT_EQ(true, mTunnelModeEnabledListener->mTunnelModeEnabled); mTunnelModeEnabledReporter->removeListener(mTunnelModeEnabledListener); - mFlinger.mutableCurrentState().layersSortedByZ.remove(layer); layer = nullptr; mTunnelModeEnabledReporter->updateTunnelModeStatus(); @@ -154,12 +152,9 @@ TEST_F(TunnelModeEnabledReporterTest, layerWithSidebandStreamTriggersUpdate) { layerWithSidebandStream->setSidebandStream(stream, FrameTimelineInfo{}, 20, gui::GameMode::Unsupported); - mFlinger.mutableCurrentState().layersSortedByZ.add(simpleLayer); - mFlinger.mutableCurrentState().layersSortedByZ.add(layerWithSidebandStream); mTunnelModeEnabledReporter->updateTunnelModeStatus(); EXPECT_EQ(true, mTunnelModeEnabledListener->mTunnelModeEnabled); - mFlinger.mutableCurrentState().layersSortedByZ.remove(layerWithSidebandStream); layerWithSidebandStream = nullptr; mTunnelModeEnabledReporter->updateTunnelModeStatus(); EXPECT_EQ(false, mTunnelModeEnabledListener->mTunnelModeEnabled); |