Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 63fcbac3 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I693c514a,Ibfdf403d,Ie260fb09,If07953b4

* changes:
  SF: Pass timestamps into layer tracing
  SF: Commit created layers without using transactions
  SF: Only create a starting entry if there are starting states
  SF: Allow privileged clients to create layers offscreen
parents 582c98b9 b64a3b46
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -136,7 +136,7 @@ struct layer_state_t {
        eLayerStackChanged = 0x00000080,
        /* unused 0x00000400, */
        eShadowRadiusChanged = 0x00000800,
        eLayerCreated = 0x00001000,
        /* unused 0x00001000, */
        eBufferCropChanged = 0x00002000,
        eRelativeLayerChanged = 0x00004000,
        eReparent = 0x00008000,
+1 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ struct LayerCreationArgs {
    uid_t callingUid;
    uint32_t textureName;
    std::optional<uint32_t> sequence = std::nullopt;
    bool addToRoot = true;
};

class Layer : public virtual RefBase, compositionengine::LayerFE {
+56 −81
Original line number Diff line number Diff line
@@ -2139,7 +2139,8 @@ bool SurfaceFlinger::commit(nsecs_t frameTime, int64_t vsyncId, nsecs_t expected

        bool needsTraversal = false;
        if (clearTransactionFlags(eTransactionFlushNeeded)) {
            needsTraversal = flushTransactionQueues(vsyncId);
            needsTraversal |= commitCreatedLayers();
            needsTraversal |= flushTransactionQueues(vsyncId);
        }

        const bool shouldCommit =
@@ -2181,6 +2182,11 @@ bool SurfaceFlinger::commit(nsecs_t frameTime, int64_t vsyncId, nsecs_t expected
    updateCursorAsync();
    updateInputFlinger();

    if (mLayerTracingEnabled && !mLayerTracing.flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
        // This will block and tracing should only be enabled for debugging.
        mLayerTracing.notify(mVisibleRegionsDirty, frameTime);
    }

    MAIN_THREAD_GUARD(persistDisplayBrightness(mustComposite));

    return mustComposite && CC_LIKELY(mBootStage != BootStage::BOOTLOADER);
@@ -2282,13 +2288,9 @@ void SurfaceFlinger::composite(nsecs_t frameTime) {
    modulateVsync(&VsyncModulator::onDisplayRefresh, usedGpuComposition);

    mLayersWithQueuedFrames.clear();
    if (mLayerTracingEnabled) {
    if (mLayerTracingEnabled && mLayerTracing.flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
        // This will block and should only be used for debugging.
        if (mVisibleRegionsDirty) {
            mLayerTracing.notify("visibleRegionsDirty");
        } else if (mLayerTracing.flagIsSet(LayerTracing::TRACE_BUFFERS)) {
            mLayerTracing.notify("bufferLatched");
        }
        mLayerTracing.notify(mVisibleRegionsDirty, frameTime);
    }

    mVisibleRegionsWereDirtyThisFrame = mVisibleRegionsDirty; // Cache value for use in post-comp
@@ -3605,7 +3607,7 @@ bool SurfaceFlinger::latchBuffers() {
}

status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, const sp<IBinder>& handle,
                                        const sp<Layer>& lbc, const wp<Layer>& parent,
                                        const sp<Layer>& layer, const wp<Layer>& parent,
                                        bool addToRoot, uint32_t* outTransformHint) {
    if (mNumLayers >= ISurfaceComposer::MAX_LAYERS) {
        ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers.load(),
@@ -3613,31 +3615,22 @@ status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, const sp<IBind
        return NO_MEMORY;
    }

    setLayerCreatedState(handle, lbc, parent, addToRoot);

    // Create a transaction includes the initial parent and producer.
    Vector<ComposerState> states;
    Vector<DisplayState> displays;

    ComposerState composerState;
    composerState.state.what = layer_state_t::eLayerCreated;
    composerState.state.surface = handle;
    states.add(composerState);
    {
        std::scoped_lock<std::mutex> lock(mCreatedLayersLock);
        mCreatedLayers.emplace_back(layer, parent, addToRoot);
    }

    lbc->updateTransformHint(mActiveDisplayTransformHint);
    layer->updateTransformHint(mActiveDisplayTransformHint);
    if (outTransformHint) {
        *outTransformHint = mActiveDisplayTransformHint;
    }
    // attach this layer to the client
    if (client != nullptr) {
        client->attachLayer(handle, lbc);
        client->attachLayer(handle, layer);
    }

    int64_t transactionId = (((int64_t)mPid) << 32) | mUniqueTransactionId++;
    return setTransactionState(FrameTimelineInfo{}, states, displays, 0 /* flags */, nullptr,
                               InputWindowCommands{}, -1 /* desiredPresentTime */,
                               true /* isAutoTimestamp */, {}, false /* hasListenerCallbacks */, {},
                               transactionId);
    setTransactionFlags(eTransactionNeeded);
    return NO_ERROR;
}

uint32_t SurfaceFlinger::getTransactionFlags() const {
@@ -4222,15 +4215,7 @@ uint32_t SurfaceFlinger::setClientStateLocked(const FrameTimelineInfo& frameTime
    uint32_t flags = 0;
    sp<Layer> layer = nullptr;
    if (s.surface) {
        if (what & layer_state_t::eLayerCreated) {
            layer = handleLayerCreatedLocked(s.surface);
            if (layer) {
                flags |= eTransactionNeeded | eTraversalNeeded;
                mLayersAdded = true;
            }
        } else {
        layer = fromHandle(s.surface).promote();
        }
    } else {
        // The client may provide us a null handle. Treat it as if the layer was removed.
        ALOGW("Attempt to set client state with a null layer handle");
@@ -4574,7 +4559,7 @@ status_t SurfaceFlinger::mirrorLayer(const LayerCreationArgs& args,
                                                args.name, mirrorFrom->sequence);
    }
    return addClientLayer(args.client, *outHandle, mirrorLayer /* layer */, nullptr /* parent */,
                          false /* addAsRoot */, nullptr /* outTransformHint */);
                          false /* addToRoot */, nullptr /* outTransformHint */);
}

status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, sp<IBinder>* outHandle,
@@ -4614,7 +4599,7 @@ status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, sp<IBinder>* outHa
        return result;
    }

    bool addToRoot = callingThreadHasUnscopedSurfaceFlingerAccess();
    bool addToRoot = args.addToRoot && callingThreadHasUnscopedSurfaceFlingerAccess();
    wp<Layer> parent(parentHandle != nullptr ? fromHandle(parentHandle) : parentLayer);
    if (parentHandle != nullptr && parent == nullptr) {
        ALOGE("Invalid parent handle %p.", parentHandle.get());
@@ -4641,7 +4626,6 @@ status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, sp<IBinder>* outHa
        return result;
    }

    setTransactionFlags(eTransactionNeeded);
    *outLayerId = layer->sequence;
    return result;
}
@@ -5794,12 +5778,18 @@ status_t SurfaceFlinger::onTransact(uint32_t code, const Parcel& data, Parcel* r
            }
            case 1025: { // Set layer tracing
                n = data.readInt32();
                int64_t fixedStartingTime = data.readInt64();
                bool tracingEnabledChanged;
                if (n) {
                    ALOGD("LayerTracing enabled");
                    tracingEnabledChanged = mLayerTracing.enable();
                    if (tracingEnabledChanged) {
                        mScheduler->schedule([&]() MAIN_THREAD { mLayerTracing.notify("start"); })
                        int64_t startingTime =
                                (fixedStartingTime) ? fixedStartingTime : systemTime();
                        mScheduler
                                ->schedule([&]() MAIN_THREAD {
                                    mLayerTracing.notify("start", startingTime);
                                })
                                .wait();
                    }
                } else {
@@ -7100,53 +7090,19 @@ void TransactionState::traverseStatesWithBuffers(
    }
}

void SurfaceFlinger::setLayerCreatedState(const sp<IBinder>& handle, const wp<Layer>& layer,
                                          const wp<Layer> parent, bool addToRoot) {
    Mutex::Autolock lock(mCreatedLayersLock);
    mCreatedLayers[handle->localBinder()] =
            std::make_unique<LayerCreatedState>(layer, parent, addToRoot);
}

auto SurfaceFlinger::getLayerCreatedState(const sp<IBinder>& handle) {
    Mutex::Autolock lock(mCreatedLayersLock);
    BBinder* b = nullptr;
    if (handle) {
        b = handle->localBinder();
    }

    if (b == nullptr) {
        return std::unique_ptr<LayerCreatedState>(nullptr);
    }

    auto it = mCreatedLayers.find(b);
    if (it == mCreatedLayers.end()) {
        ALOGE("Can't find layer from handle %p", handle.get());
        return std::unique_ptr<LayerCreatedState>(nullptr);
    }

    auto state = std::move(it->second);
    mCreatedLayers.erase(it);
    return state;
}

sp<Layer> SurfaceFlinger::handleLayerCreatedLocked(const sp<IBinder>& handle) {
    const auto& state = getLayerCreatedState(handle);
    if (!state) {
        return nullptr;
    }

    sp<Layer> layer = state->layer.promote();
void SurfaceFlinger::handleLayerCreatedLocked(const LayerCreatedState& state) {
    sp<Layer> layer = state.layer.promote();
    if (!layer) {
        ALOGE("Invalid layer %p", state->layer.unsafe_get());
        return nullptr;
        ALOGD("Layer was destroyed soon after creation %p", state.layer.unsafe_get());
        return;
    }

    sp<Layer> parent;
    bool addToRoot = state->addToRoot;
    if (state->initialParent != nullptr) {
        parent = state->initialParent.promote();
    bool addToRoot = state.addToRoot;
    if (state.initialParent != nullptr) {
        parent = state.initialParent.promote();
        if (parent == nullptr) {
            ALOGE("Invalid parent %p", state->initialParent.unsafe_get());
            ALOGD("Parent was destroyed soon after creation %p", state.initialParent.unsafe_get());
            addToRoot = false;
        }
    }
@@ -7166,7 +7122,6 @@ sp<Layer> SurfaceFlinger::handleLayerCreatedLocked(const sp<IBinder>& handle) {
    layer->updateTransformHint(mActiveDisplayTransformHint);

    mInterceptor->saveSurfaceCreation(layer);
    return layer;
}

void SurfaceFlinger::sample() {
@@ -7248,6 +7203,26 @@ std::shared_ptr<renderengine::ExternalTexture> SurfaceFlinger::getExternalTextur
             layerName);
    return buffer;
}

bool SurfaceFlinger::commitCreatedLayers() {
    std::vector<LayerCreatedState> createdLayers;
    {
        std::scoped_lock<std::mutex> lock(mCreatedLayersLock);
        createdLayers = std::move(mCreatedLayers);
        mCreatedLayers.clear();
        if (createdLayers.size() == 0) {
            return false;
        }
    }

    Mutex::Autolock _l(mStateLock);
    for (const auto& createdLayer : createdLayers) {
        handleLayerCreatedLocked(createdLayer);
    }
    createdLayers.clear();
    mLayersAdded = true;
    return true;
}
} // namespace android

#if defined(__gl_h_)
+4 −6
Original line number Diff line number Diff line
@@ -1327,7 +1327,7 @@ private:

    std::unordered_map<DisplayId, sp<HdrLayerInfoReporter>> mHdrLayerInfoListeners
            GUARDED_BY(mStateLock);
    mutable Mutex mCreatedLayersLock;
    mutable std::mutex mCreatedLayersLock;
    struct LayerCreatedState {
        LayerCreatedState(const wp<Layer>& layer, const wp<Layer> parent, bool addToRoot)
              : layer(layer), initialParent(parent), addToRoot(addToRoot) {}
@@ -1343,11 +1343,9 @@ private:

    // A temporay pool that store the created layers and will be added to current state in main
    // thread.
    std::unordered_map<BBinder*, std::unique_ptr<LayerCreatedState>> mCreatedLayers;
    void setLayerCreatedState(const sp<IBinder>& handle, const wp<Layer>& layer,
                              const wp<Layer> parent, bool addToRoot);
    auto getLayerCreatedState(const sp<IBinder>& handle);
    sp<Layer> handleLayerCreatedLocked(const sp<IBinder>& handle) REQUIRES(mStateLock);
    std::vector<LayerCreatedState> mCreatedLayers GUARDED_BY(mCreatedLayersLock);
    bool commitCreatedLayers();
    void handleLayerCreatedLocked(const LayerCreatedState& state) REQUIRES(mStateLock);

    std::atomic<ui::Transform::RotationFlags> mActiveDisplayTransformHint;

+7 −3
Original line number Diff line number Diff line
@@ -98,16 +98,20 @@ void LayerTracing::dump(std::string& result) const {
    mBuffer->dump(result);
}

void LayerTracing::notify(const char* where) {
    ATRACE_CALL();
void LayerTracing::notify(bool visibleRegionDirty, int64_t time) {
    std::scoped_lock lock(mTraceLock);
    if (!mEnabled) {
        return;
    }

    if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
        return;
    }

    ATRACE_CALL();
    LayersTraceProto entry;
    entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
    entry.set_elapsed_realtime_nanos(time);
    const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
    entry.set_where(where);
    LayersProto layers(mFlinger.dumpDrawingStateProto(mFlags));

Loading