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

Commit 1f6d6d5d authored by Evan Rosky's avatar Evan Rosky
Browse files

Add metadata store to surfaces

This adds a key/value metadata storage mechanism to
surfaces that allows the windowmanager to pass information
to the surfaceflinger frontend.

This then moves the existing metadata (window type and
ownerUID) into this metadata structure.

Bug: 122925737
Test: Phone boots and surfaces fling. Some unittests
Change-Id: I72c574737b7f75be2311a341812b15d385f507ed
parent 57ba2f19
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -108,6 +108,7 @@ cc_library_shared {
        "ISurfaceComposerClient.cpp",
        "ISurfaceComposerClient.cpp",
        "ITransactionCompletedListener.cpp",
        "ITransactionCompletedListener.cpp",
        "LayerDebugInfo.cpp",
        "LayerDebugInfo.cpp",
        "LayerMetadata.cpp",
        "LayerState.cpp",
        "LayerState.cpp",
        "OccupancyTracker.cpp",
        "OccupancyTracker.cpp",
        "StreamSplitter.cpp",
        "StreamSplitter.cpp",
+8 −8
Original line number Original line Diff line number Diff line
@@ -47,26 +47,26 @@ public:
    ~BpSurfaceComposerClient() override;
    ~BpSurfaceComposerClient() override;


    status_t createSurface(const String8& name, uint32_t width, uint32_t height, PixelFormat format,
    status_t createSurface(const String8& name, uint32_t width, uint32_t height, PixelFormat format,
                           uint32_t flags, const sp<IBinder>& parent, int32_t windowType,
                           uint32_t flags, const sp<IBinder>& parent, LayerMetadata metadata,
                           int32_t ownerUid, sp<IBinder>* handle,
                           sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp) override {
                           sp<IGraphicBufferProducer>* gbp) override {
        return callRemote<decltype(&ISurfaceComposerClient::createSurface)>(Tag::CREATE_SURFACE,
        return callRemote<decltype(&ISurfaceComposerClient::createSurface)>(Tag::CREATE_SURFACE,
                                                                            name, width, height,
                                                                            name, width, height,
                                                                            format, flags, parent,
                                                                            format, flags, parent,
                                                                            windowType, ownerUid,
                                                                            std::move(metadata),
                                                                            handle, gbp);
                                                                            handle, gbp);
    }
    }


    status_t createWithSurfaceParent(const String8& name, uint32_t width, uint32_t height,
    status_t createWithSurfaceParent(const String8& name, uint32_t width, uint32_t height,
                                     PixelFormat format, uint32_t flags,
                                     PixelFormat format, uint32_t flags,
                                     const sp<IGraphicBufferProducer>& parent, int32_t windowType,
                                     const sp<IGraphicBufferProducer>& parent,
                                     int32_t ownerUid, sp<IBinder>* handle,
                                     LayerMetadata metadata, sp<IBinder>* handle,
                                     sp<IGraphicBufferProducer>* gbp) override {
                                     sp<IGraphicBufferProducer>* gbp) override {
        return callRemote<decltype(
        return callRemote<decltype(
                &ISurfaceComposerClient::createWithSurfaceParent)>(Tag::CREATE_WITH_SURFACE_PARENT,
                &ISurfaceComposerClient::createWithSurfaceParent)>(Tag::CREATE_WITH_SURFACE_PARENT,
                                                                   name, width, height, format,
                                                                   name, width, height, format,
                                                                   flags, parent, windowType,
                                                                   flags, parent,
                                                                   ownerUid, handle, gbp);
                                                                   std::move(metadata), handle,
                                                                   gbp);
    }
    }


    status_t clearLayerFrameStats(const sp<IBinder>& handle) const override {
    status_t clearLayerFrameStats(const sp<IBinder>& handle) const override {
+114 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 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 <android-base/stringprintf.h>
#include <binder/Parcel.h>
#include <gui/LayerMetadata.h>

using android::base::StringPrintf;

namespace android {

LayerMetadata::LayerMetadata() = default;

LayerMetadata::LayerMetadata(std::unordered_map<uint32_t, std::vector<uint8_t>> map)
      : mMap(std::move(map)) {}

LayerMetadata::LayerMetadata(const LayerMetadata& other) = default;

LayerMetadata::LayerMetadata(LayerMetadata&& other) = default;

void LayerMetadata::merge(const LayerMetadata& other) {
    for (const auto& entry : other.mMap) {
        mMap[entry.first] = entry.second;
    }
}

status_t LayerMetadata::writeToParcel(Parcel* parcel) const {
    parcel->writeInt32(static_cast<int>(mMap.size()));
    status_t status = OK;
    for (const auto& entry : mMap) {
        status = parcel->writeUint32(entry.first);
        if (status != OK) {
            break;
        }
        status = parcel->writeByteVector(entry.second);
        if (status != OK) {
            break;
        }
    }
    return status;
}

status_t LayerMetadata::readFromParcel(const Parcel* parcel) {
    int size = parcel->readInt32();
    status_t status = OK;
    mMap.clear();
    for (int i = 0; i < size; ++i) {
        uint32_t key = parcel->readUint32();
        status = parcel->readByteVector(&mMap[key]);
        if (status != OK) {
            break;
        }
    }
    return status;
}

LayerMetadata& LayerMetadata::operator=(const LayerMetadata& other) {
    mMap = other.mMap;
    return *this;
}

LayerMetadata& LayerMetadata::operator=(LayerMetadata&& other) {
    mMap = std::move(other.mMap);
    return *this;
}

bool LayerMetadata::has(uint32_t key) const {
    return mMap.count(key);
}

int32_t LayerMetadata::getInt32(uint32_t key, int32_t fallback) const {
    if (!has(key)) return fallback;
    const std::vector<uint8_t>& data = mMap.at(key);
    if (data.size() < sizeof(uint32_t)) return fallback;
    Parcel p;
    p.setData(data.data(), data.size());
    return p.readInt32();
}

void LayerMetadata::setInt32(uint32_t key, int32_t value) {
    std::vector<uint8_t>& data = mMap[key];
    Parcel p;
    p.writeInt32(value);
    data.resize(p.dataSize());
    memcpy(data.data(), p.data(), p.dataSize());
}

std::string LayerMetadata::itemToString(uint32_t key, const char* separator) const {
    if (!has(key)) return std::string();
    switch (key) {
        case METADATA_OWNER_UID:
            return StringPrintf("ownerUID%s%d", separator, getInt32(key, 0));
        case METADATA_WINDOW_TYPE:
            return StringPrintf("windowType%s%d", separator, getInt32(key, 0));
        default:
            return StringPrintf("%d%s%dbytes", key, separator,
                                static_cast<int>(mMap.at(key).size()));
    }
}

} // namespace android
+6 −0
Original line number Original line Diff line number Diff line
@@ -96,6 +96,7 @@ status_t layer_state_t::write(Parcel& output) const


    output.writeStrongBinder(cachedBuffer.token);
    output.writeStrongBinder(cachedBuffer.token);
    output.writeInt32(cachedBuffer.bufferId);
    output.writeInt32(cachedBuffer.bufferId);
    output.writeParcelable(metadata);


    output.writeFloat(colorAlpha);
    output.writeFloat(colorAlpha);
    output.writeUint32(static_cast<uint32_t>(colorDataspace));
    output.writeUint32(static_cast<uint32_t>(colorDataspace));
@@ -172,6 +173,7 @@ status_t layer_state_t::read(const Parcel& input)


    cachedBuffer.token = input.readStrongBinder();
    cachedBuffer.token = input.readStrongBinder();
    cachedBuffer.bufferId = input.readInt32();
    cachedBuffer.bufferId = input.readInt32();
    input.readParcelable(&metadata);


    colorAlpha = input.readFloat();
    colorAlpha = input.readFloat();
    colorDataspace = static_cast<ui::Dataspace>(input.readUint32());
    colorDataspace = static_cast<ui::Dataspace>(input.readUint32());
@@ -396,6 +398,10 @@ void layer_state_t::merge(const layer_state_t& other) {
        what |= eColorDataspaceChanged;
        what |= eColorDataspaceChanged;
        colorDataspace = other.colorDataspace;
        colorDataspace = other.colorDataspace;
    }
    }
    if (other.what & eMetadataChanged) {
        what |= eMetadataChanged;
        metadata.merge(other.metadata);
    }
    if ((other.what & what) != other.what) {
    if ((other.what & what) != other.what) {
        ALOGE("Unmerged SurfaceComposer Transaction properties. LayerState::merge needs updating? "
        ALOGE("Unmerged SurfaceComposer Transaction properties. LayerState::merge needs updating? "
              "other.what=0x%" PRIu64 " what=0x%" PRIu64,
              "other.what=0x%" PRIu64 " what=0x%" PRIu64,
+30 −28
Original line number Original line Diff line number Diff line
@@ -539,6 +539,20 @@ SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer
    return *this;
    return *this;
}
}


SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMetadata(
        const sp<SurfaceControl>& sc, uint32_t key, std::vector<uint8_t> data) {
    layer_state_t* s = getLayerState(sc);
    if (!s) {
        mStatus = BAD_INDEX;
        return *this;
    }
    s->what |= layer_state_t::eMetadataChanged;
    s->metadata.mMap[key] = std::move(data);

    registerSurfaceControlForCallback(sc);
    return *this;
}

SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setMatrix(
        const sp<SurfaceControl>& sc, float dsdx, float dtdx,
        const sp<SurfaceControl>& sc, float dsdx, float dtdx,
        float dtdy, float dsdy) {
        float dtdy, float dsdy) {
@@ -1141,26 +1155,19 @@ void SurfaceComposerClient::dispose() {
    mStatus = NO_INIT;
    mStatus = NO_INIT;
}
}


sp<SurfaceControl> SurfaceComposerClient::createSurface(
sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
        const String8& name,
                                                        PixelFormat format, uint32_t flags,
        uint32_t w,
        uint32_t h,
        PixelFormat format,
        uint32_t flags,
                                                        SurfaceControl* parent,
                                                        SurfaceControl* parent,
        int32_t windowType,
                                                        LayerMetadata metadata) {
        int32_t ownerUid)
{
    sp<SurfaceControl> s;
    sp<SurfaceControl> s;
    createSurfaceChecked(name, w, h, format, &s, flags, parent, windowType, ownerUid);
    createSurfaceChecked(name, w, h, format, &s, flags, parent, std::move(metadata));
    return s;
    return s;
}
}


sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8& name, uint32_t w,
                                                                  uint32_t h, PixelFormat format,
                                                                  uint32_t h, PixelFormat format,
                                                                  uint32_t flags, Surface* parent,
                                                                  uint32_t flags, Surface* parent,
                                                                  int32_t windowType,
                                                                  LayerMetadata metadata) {
                                                                  int32_t ownerUid) {
    sp<SurfaceControl> sur;
    sp<SurfaceControl> sur;
    status_t err = mStatus;
    status_t err = mStatus;


@@ -1169,8 +1176,8 @@ sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8&
        sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
        sp<IGraphicBufferProducer> parentGbp = parent->getIGraphicBufferProducer();
        sp<IGraphicBufferProducer> gbp;
        sp<IGraphicBufferProducer> gbp;


        err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp, windowType,
        err = mClient->createWithSurfaceParent(name, w, h, format, flags, parentGbp,
                                               ownerUid, &handle, &gbp);
                                               std::move(metadata), &handle, &gbp);
        ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
        ALOGE_IF(err, "SurfaceComposerClient::createWithSurfaceParent error %s", strerror(-err));
        if (err == NO_ERROR) {
        if (err == NO_ERROR) {
            return new SurfaceControl(this, handle, gbp, true /* owned */);
            return new SurfaceControl(this, handle, gbp, true /* owned */);
@@ -1179,17 +1186,11 @@ sp<SurfaceControl> SurfaceComposerClient::createWithSurfaceParent(const String8&
    return nullptr;
    return nullptr;
}
}


status_t SurfaceComposerClient::createSurfaceChecked(
status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
        const String8& name,
        uint32_t w,
        uint32_t h,
                                                     PixelFormat format,
                                                     PixelFormat format,
        sp<SurfaceControl>* outSurface,
                                                     sp<SurfaceControl>* outSurface, uint32_t flags,
        uint32_t flags,
                                                     SurfaceControl* parent,
                                                     SurfaceControl* parent,
        int32_t windowType,
                                                     LayerMetadata metadata) {
        int32_t ownerUid)
{
    sp<SurfaceControl> sur;
    sp<SurfaceControl> sur;
    status_t err = mStatus;
    status_t err = mStatus;


@@ -1201,8 +1202,9 @@ status_t SurfaceComposerClient::createSurfaceChecked(
        if (parent != nullptr) {
        if (parent != nullptr) {
            parentHandle = parent->getHandle();
            parentHandle = parent->getHandle();
        }
        }
        err = mClient->createSurface(name, w, h, format, flags, parentHandle,

                windowType, ownerUid, &handle, &gbp);
        err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
                                     &handle, &gbp);
        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
        if (err == NO_ERROR) {
        if (err == NO_ERROR) {
            *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
            *outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
Loading