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

Commit 07e2a48b authored by Vishnu Nair's avatar Vishnu Nair
Browse files

SF: Carve out LayerHandle

Move LayerHandle outside of layer so it can
be used with the new LayerLifecycleManager.

Make Handle fields like layer private and provide
access functions so callers have to safely cast
from a binder instead of directly casting it.

Add layerid to LayerHandle since we want to
expose the layer class in fewer places.

Finally fold LayerCleaner class into LayerHandler.

Bug: 238781169
Test: presubmit

Change-Id: I86e08050cfcc89d68e6ed8fa0e8ff30063cf3603
parent e23e6bb2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -156,6 +156,7 @@ filegroup {
        "Effects/Daltonizer.cpp",
        "EventLog/EventLog.cpp",
        "FrontEnd/LayerCreationArgs.cpp",
        "FrontEnd/LayerHandle.cpp",
        "FrontEnd/TransactionHandler.cpp",
        "FlagManager.cpp",
        "FpsReporter.cpp",
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "LayerHandle.h"
#include <cstdint>
#include "Layer.h"
#include "LayerCreationArgs.h"
#include "SurfaceFlinger.h"

namespace android::surfaceflinger {

LayerHandle::LayerHandle(const sp<android::SurfaceFlinger>& flinger,
                         const sp<android::Layer>& layer)
      : mFlinger(flinger), mLayer(layer), mLayerId(static_cast<uint32_t>(layer->getSequence())) {}

LayerHandle::~LayerHandle() {
    if (mFlinger) {
        mFlinger->onHandleDestroyed(this, mLayer, mLayerId);
    }
}

const String16 LayerHandle::kDescriptor = String16("android.Layer.LayerHandle");

sp<LayerHandle> LayerHandle::fromIBinder(const sp<IBinder>& binder) {
    if (binder == nullptr) {
        return nullptr;
    }

    BBinder* b = binder->localBinder();
    if (b == nullptr || b->getInterfaceDescriptor() != LayerHandle::kDescriptor) {
        ALOGD("handle does not have a valid descriptor");
        return nullptr;
    }

    // We can safely cast this binder since its local and we verified its interface descriptor.
    return sp<LayerHandle>::cast(binder);
}

sp<android::Layer> LayerHandle::getLayer(const sp<IBinder>& binder) {
    sp<LayerHandle> handle = LayerHandle::fromIBinder(binder);
    return handle ? handle->mLayer : nullptr;
}

uint32_t LayerHandle::getLayerId(const sp<IBinder>& binder) {
    sp<LayerHandle> handle = LayerHandle::fromIBinder(binder);
    return handle ? handle->mLayerId : UNASSIGNED_LAYER_ID;
}

} // namespace android::surfaceflinger
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <binder/Binder.h>
#include <utils/StrongPointer.h>

namespace android {
class SurfaceFlinger;
class Layer;
} // namespace android

namespace android::surfaceflinger {

/*
 * The layer handle is just a BBinder object passed to the client
 * (remote process) -- we don't keep any reference on our side such that
 * the dtor is called when the remote side let go of its reference.
 *
 * ~LayerHandle ensures that mFlinger->onLayerDestroyed() is called for
 * this layer when the handle is destroyed.
 */
class LayerHandle : public BBinder {
public:
    LayerHandle(const sp<android::SurfaceFlinger>& flinger, const sp<android::Layer>& layer);
    // for testing
    LayerHandle(uint32_t layerId) : mFlinger(nullptr), mLayer(nullptr), mLayerId(layerId) {}
    ~LayerHandle();

    // Static functions to access the layer and layer id safely from an incoming binder.
    static sp<LayerHandle> fromIBinder(const sp<IBinder>& handle);
    static sp<android::Layer> getLayer(const sp<IBinder>& handle);
    static uint32_t getLayerId(const sp<IBinder>& handle);
    static const String16 kDescriptor;

    const String16& getInterfaceDescriptor() const override { return kDescriptor; }

private:
    sp<android::SurfaceFlinger> mFlinger;
    sp<android::Layer> mLayer;
    const uint32_t mLayerId;
};

} // namespace android::surfaceflinger
+6 −21
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@
#include "FrameTimeline.h"
#include "FrameTracer/FrameTracer.h"
#include "FrontEnd/LayerCreationArgs.h"
#include "FrontEnd/LayerHandle.h"
#include "LayerProtoHelper.h"
#include "SurfaceFlinger.h"
#include "TimeStats/TimeStats.h"
@@ -332,7 +333,7 @@ sp<IBinder> Layer::getHandle() {
        return nullptr;
    }
    mGetHandleCalled = true;
    return sp<Handle>::make(mFlinger, sp<Layer>::fromExisting(this));
    return sp<LayerHandle>::make(mFlinger, sp<Layer>::fromExisting(this));
}

// ---------------------------------------------------------------------------
@@ -774,7 +775,7 @@ void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
}

bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
    sp<Layer> relative = fromHandle(relativeToHandle).promote();
    sp<Layer> relative = LayerHandle::getLayer(relativeToHandle);
    if (relative == nullptr) {
        return false;
    }
@@ -1543,7 +1544,7 @@ void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
bool Layer::reparent(const sp<IBinder>& newParentHandle) {
    sp<Layer> newParent;
    if (newParentHandle != nullptr) {
        newParent = fromHandle(newParentHandle).promote();
        newParent = LayerHandle::getLayer(newParentHandle);
        if (newParent == nullptr) {
            ALOGE("Unable to promote Layer handle");
            return false;
@@ -1936,7 +1937,8 @@ void Layer::commitChildList() {

void Layer::setInputInfo(const WindowInfo& info) {
    mDrawingState.inputInfo = info;
    mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
    mDrawingState.touchableRegionCrop =
            LayerHandle::getLayer(info.touchableRegionCropHandle.promote());
    mDrawingState.modified = true;
    mFlinger->mUpdateInputInfo = true;
    setTransactionFlags(eTransactionNeeded);
@@ -2583,23 +2585,6 @@ void Layer::setClonedChild(const sp<Layer>& clonedChild) {
    mFlinger->mNumClones++;
}

const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");

wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
    if (handleBinder == nullptr) {
        return nullptr;
    }

    BBinder* b = handleBinder->localBinder();
    if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
        return nullptr;
    }

    // We can safely cast this binder since its local and we verified its interface descriptor.
    sp<Handle> handle = sp<Handle>::cast(handleBinder);
    return handle->owner;
}

bool Layer::setDropInputMode(gui::DropInputMode mode) {
    if (mDrawingState.dropInputMode == mode) {
        return false;
+0 −40
Original line number Diff line number Diff line
@@ -226,46 +226,6 @@ public:
        bool dimmingEnabled = true;
    };

    /*
     * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
     * is called.
     */
    class LayerCleaner {
        sp<SurfaceFlinger> mFlinger;
        sp<Layer> mLayer;
        BBinder* mHandle;

    protected:
        ~LayerCleaner() {
            // destroy client resources
            mFlinger->onHandleDestroyed(mHandle, mLayer);
        }

    public:
        LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer, BBinder* handle)
              : mFlinger(flinger), mLayer(layer), mHandle(handle) {}
    };

    /*
     * The layer handle is just a BBinder object passed to the client
     * (remote process) -- we don't keep any reference on our side such that
     * the dtor is called when the remote side let go of its reference.
     *
     * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
     * this layer when the handle is destroyed.
     */
    class Handle : public BBinder, public LayerCleaner {
    public:
        Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
              : LayerCleaner(flinger, layer, this), owner(layer) {}
        const String16& getInterfaceDescriptor() const override { return kDescriptor; }

        static const String16 kDescriptor;
        wp<Layer> owner;
    };

    static wp<Layer> fromHandle(const sp<IBinder>& handle);

    explicit Layer(const LayerCreationArgs& args);
    virtual ~Layer();

Loading