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

Commit fd668629 authored by Marissa Wall's avatar Marissa Wall
Browse files

blast: Factor out BufferQueueLayer

Remove all BufferQueue related items from BufferLayer and
Layer into a new BufferQueueLayer class.

Bug: 80477568
Test: Transaction_test.cpp
Change-Id: I7fcc49768e2723b4991d4566c5124451e328bdf0
parent aac3a45b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ filegroup {
    srcs: [
        "BufferLayer.cpp",
        "BufferLayerConsumer.cpp",
        "BufferQueueLayer.cpp",
        "Client.cpp",
        "ColorLayer.cpp",
        "ContainerLayer.cpp",
+162 −486

File changed.

Preview size limit exceeded, changes collapsed.

+82 −91
Original line number Diff line number Diff line
@@ -47,152 +47,143 @@

namespace android {

/*
 * A new BufferQueue and a new BufferLayerConsumer are created when the
 * BufferLayer is first referenced.
 *
 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
 * that new data has arrived.
 */
class BufferLayer : public Layer, public BufferLayerConsumer::ContentsChangedListener {
class BufferLayer : public Layer {
public:
    BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
                uint32_t h, uint32_t flags);

    ~BufferLayer() override;

    // If we have received a new buffer this frame, we will pass its surface
    // damage down to hardware composer. Otherwise, we must send a region with
    // one empty rect.
    void useSurfaceDamage();
    void useEmptyDamage();

    // -----------------------------------------------------------------------
    // Overriden from Layer
    // -----------------------------------------------------------------------
public:
    // If we have received a new buffer this frame, we will pass its surface
    // damage down to hardware composer. Otherwise, we must send a region with
    // one empty rect.
    void useSurfaceDamage() override;
    void useEmptyDamage() override;

    /*
     * getTypeId - Provide unique string for each class type in the Layer
     * hierarchy
     */
    // getTypeId - Provide unique string for each class type in the Layer
    // hierarchy
    const char* getTypeId() const override { return "BufferLayer"; }

    /*
     * isProtected - true if the layer may contain protected content in the
     * GRALLOC_USAGE_PROTECTED sense.
     */
    bool isProtected() const;
    bool isOpaque(const Layer::State& s) const override;

    /*
     * isVisible - true if this layer is visible, false otherwise
     */
    // isVisible - true if this layer is visible, false otherwise
    bool isVisible() const override;

    /*
     * isFixedSize - true if content has a fixed size
     */
    // isFixedSize - true if content has a fixed size
    bool isFixedSize() const override;

    // the this layer's size and format
    status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);

    /*
     * onDraw - draws the surface.
     */
    // onDraw - draws the surface.
    void onDraw(const RenderArea& renderArea, const Region& clip,
                bool useIdentityTransform) const override;
    void drawNow(const RenderArea& renderArea, bool useIdentityTransform) const;

    void onLayerDisplayed(const sp<Fence>& releaseFence) override;
    bool isHdrY410() const override;

    void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override;

    void abandon() override;
    bool shouldPresentNow(const DispSync& dispSync) const override;
    void setTransformHint(uint32_t orientation) const override;
    bool onPreComposition(nsecs_t refreshStartTime) override;
    bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
                           const std::shared_ptr<FenceTime>& presentFence,
                           const CompositorTiming& compositorTiming) override;
    std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
    bool getTransformToDisplayInverse() const override;

public:
    bool onPreComposition(nsecs_t refreshStartTime) override;

    // If a buffer was replaced this frame, release the former buffer
    void releasePendingBuffer(nsecs_t dequeueReadyTime);

    /*
     * latchBuffer - called each time the screen is redrawn and returns whether
     * the visible regions need to be recomputed (this is a fairly heavy
     * operation, so this should be set only if needed). Typically this is used
     * to figure out if the content or size of a surface has changed.
     */
    // latchBuffer - called each time the screen is redrawn and returns whether
    // the visible regions need to be recomputed (this is a fairly heavy
    // operation, so this should be set only if needed). Typically this is used
    // to figure out if the content or size of a surface has changed.
    Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;

    bool isBufferLatched() const override { return mRefreshPending; }
    void setDefaultBufferSize(uint32_t w, uint32_t h) override;

    bool isHdrY410() const override;
    void notifyAvailableFrames() override;

    void setPerFrameData(const sp<const DisplayDevice>& display) override;
    bool hasReadyFrame() const override;

    bool isOpaque(const Layer::State& s) const override;
private:
    // Returns the current scaling mode, unless mOverrideScalingMode
    // is set, in which case, it returns mOverrideScalingMode
    uint32_t getEffectiveScalingMode() const override;
    // -----------------------------------------------------------------------

    // -----------------------------------------------------------------------
    // Functions that must be implemented by derived classes
    // -----------------------------------------------------------------------
private:
    void onFirstRef() override;
    virtual bool fenceHasSignaled() const = 0;

    // Interface implementation for
    // BufferLayerConsumer::ContentsChangedListener
    void onFrameAvailable(const BufferItem& item) override;
    void onFrameReplaced(const BufferItem& item) override;
    void onSidebandStreamChanged() override;
    virtual nsecs_t getDesiredPresentTime() = 0;
    virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;

    // needsLinearFiltering - true if this surface's state requires filtering
    bool needsFiltering(const RenderArea& renderArea) const;
    virtual void getDrawingTransformMatrix(float matrix[16]) const = 0;
    virtual uint32_t getDrawingTransform() const = 0;
    virtual ui::Dataspace getDrawingDataSpace() const = 0;
    virtual Rect getDrawingCrop() const = 0;
    virtual uint32_t getDrawingScalingMode() const = 0;
    virtual Region getDrawingSurfaceDamage() const = 0;
    virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
    virtual int getDrawingApi() const = 0;
    virtual PixelFormat getPixelFormat() const = 0;

    static bool getOpacityForFormat(uint32_t format);
    virtual uint64_t getFrameNumber() const = 0;

    // drawing
    void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
    virtual bool getAutoRefresh() const = 0;
    virtual bool getSidebandStreamChanged() const = 0;

    // Temporary - Used only for LEGACY camera mode.
    uint32_t getProducerStickyTransform() const;
    virtual std::optional<Region> latchSidebandStream(bool& recomputeVisibleRegions) = 0;

    // Loads the corresponding system property once per process
    static bool latchUnsignaledBuffers();
    virtual bool hasDrawingBuffer() const = 0;

    uint64_t getHeadFrameNumber() const;
    bool headFenceHasSignaled() const;
    virtual void setFilteringEnabled(bool enabled) const = 0;

    // Returns the current scaling mode, unless mOverrideScalingMode
    // is set, in which case, it returns mOverrideScalingMode
    uint32_t getEffectiveScalingMode() const override;
    virtual status_t bindTextureImage() const = 0;
    virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) = 0;

public:
    void notifyAvailableFrames() override;
    virtual status_t updateActiveBuffer() = 0;
    virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;

    PixelFormat getPixelFormat() const override { return mFormat; }
    sp<IGraphicBufferProducer> getProducer() const;
    virtual void setHwcLayerBuffer(const sp<const DisplayDevice>& display) = 0;

private:
    sp<BufferLayerConsumer> mConsumer;
    // -----------------------------------------------------------------------

public:
    // isProtected - true if the layer may contain protected content in the
    // GRALLOC_USAGE_PROTECTED sense.
    bool isProtected() const;

protected:
    // Loads the corresponding system property once per process
    static bool latchUnsignaledBuffers();

    // Check all of the local sync points to ensure that all transactions
    // which need to have been applied prior to the frame which is about to
    // be latched have signaled
    bool allTransactionsSignaled();
    sp<IGraphicBufferProducer> mProducer;

    // constants
    uint32_t mTextureName; // from GLES
    PixelFormat mFormat;
    static bool getOpacityForFormat(uint32_t format);

    // from GLES
    const uint32_t mTextureName;

private:
    // needsLinearFiltering - true if this surface's state requires filtering
    bool needsFiltering(const RenderArea& renderArea) const;

    // drawing
    void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;

    uint64_t getHeadFrameNumber() const;

    // main thread
    uint32_t mCurrentScalingMode;
    bool mBufferLatched = false;   // TODO: Use mActiveBuffer?
    uint64_t mPreviousFrameNumber; // Only accessed on the main thread.

    // main thread.
    bool mBufferLatched; // TODO: Use mActiveBuffer?

    // The texture used to draw the layer in GLES composition mode
    mutable Texture mTexture;

    bool mUpdateTexImageFailed; // This is only accessed on the main thread.
    bool mRefreshPending;
};

+482 −0

File added.

Preview size limit exceeded, changes collapsed.

+144 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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 "BufferLayer.h"

#include <utils/String8.h>

namespace android {

/*
 * A new BufferQueue and a new BufferLayerConsumer are created when the
 * BufferLayer is first referenced.
 *
 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
 * that new data has arrived.
 */
class BufferQueueLayer : public BufferLayer, public BufferLayerConsumer::ContentsChangedListener {
public:
    BufferQueueLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name,
                     uint32_t w, uint32_t h, uint32_t flags);

    // -----------------------------------------------------------------------
    // Interface implementation for Layer
    // -----------------------------------------------------------------------
public:
    void onLayerDisplayed(const sp<Fence>& releaseFence) override;

    void abandon() override;

    void setTransformHint(uint32_t orientation) const override;

    std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;

    bool getTransformToDisplayInverse() const override;

    // If a buffer was replaced this frame, release the former buffer
    void releasePendingBuffer(nsecs_t dequeueReadyTime) override;

    void setDefaultBufferSize(uint32_t w, uint32_t h) override;

    int32_t getQueuedFrameCount() const override;

    bool shouldPresentNow(const DispSync& dispSync) const override;
    // -----------------------------------------------------------------------

    // -----------------------------------------------------------------------
    // Interface implementation for BufferLayer
    // -----------------------------------------------------------------------
public:
    bool fenceHasSignaled() const override;

private:
    nsecs_t getDesiredPresentTime() override;
    std::shared_ptr<FenceTime> getCurrentFenceTime() const override;

    void getDrawingTransformMatrix(float matrix[16]) const override;
    uint32_t getDrawingTransform() const override;
    ui::Dataspace getDrawingDataSpace() const override;
    Rect getDrawingCrop() const override;
    uint32_t getDrawingScalingMode() const override;
    Region getDrawingSurfaceDamage() const override;
    const HdrMetadata& getDrawingHdrMetadata() const override;
    int getDrawingApi() const override;
    PixelFormat getPixelFormat() const override;

    uint64_t getFrameNumber() const override;

    bool getAutoRefresh() const override;
    bool getSidebandStreamChanged() const override;

    std::optional<Region> latchSidebandStream(bool& recomputeVisibleRegions) override;

    bool hasDrawingBuffer() const override;

    void setFilteringEnabled(bool enabled) const override;

    status_t bindTextureImage() const override;
    status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) override;

    status_t updateActiveBuffer() override;
    status_t updateFrameNumber(nsecs_t latchTime) override;

    void setHwcLayerBuffer(const sp<const DisplayDevice>& display) override;
    // -----------------------------------------------------------------------

    // -----------------------------------------------------------------------
    // Interface implementation for BufferLayerConsumer::ContentsChangedListener
    // -----------------------------------------------------------------------
protected:
    void onFrameAvailable(const BufferItem& item) override;
    void onFrameReplaced(const BufferItem& item) override;
    void onSidebandStreamChanged() override;
    // -----------------------------------------------------------------------

public:
    status_t setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format);

    sp<IGraphicBufferProducer> getProducer() const;

private:
    // Temporary - Used only for LEGACY camera mode.
    uint32_t getProducerStickyTransform() const;

    void onFirstRef() override;

    sp<BufferLayerConsumer> mConsumer;
    sp<IGraphicBufferProducer> mProducer;

    PixelFormat mFormat;

    // Only accessed on the main thread.
    uint64_t mPreviousFrameNumber;
    bool mUpdateTexImageFailed;

    // Local copy of the queued contents of the incoming BufferQueue
    mutable Mutex mQueueItemLock;
    Condition mQueueItemCondition;
    Vector<BufferItem> mQueueItems;
    std::atomic<uint64_t> mLastFrameNumberReceived;

    bool mAutoRefresh;
    int mActiveBufferSlot;

    // thread-safe
    volatile int32_t mQueuedFrames;
    volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
};

} // namespace android
Loading