Android显示系统——基本概念梳理

DisplayState

struct DisplayState {
    // 这里定义了Display变更类型,说明Display可能发生的变化类型
    enum {
        eSurfaceChanged = 0x01,
        eLayerStackChanged = 0x02,
        eDisplayProjectionChanged = 0x04,
        eDisplaySizeChanged = 0x08,
        eFlagsChanged = 0x10
    };

    DisplayState();
    void merge(const DisplayState& other);
    void sanitize(int32_t permissions); // 清除无效(无权限)的变换标记,即若无权限进行某些变更,则拦截对应的变更操作

    uint32_t what = 0;  // 变化标记,表示发生了什么变化
    uint32_t flags = 0;
    sp<IBinder> token;
    sp<IGraphicBufferProducer> surface;

    ui::LayerStack layerStack = ui::DEFAULT_LAYER_STACK;    // 属于此display的layer栈,即需要显示在此display上的所有图层
    ui::Rotation orientation = ui::ROTATION_0;              // display的显示方向
    Rect layerStackSpaceRect = Rect::EMPTY_RECT;            // 表示原始layers区域大小,即图层的原始大小和位置
    Rect orientedDisplaySpaceRect = Rect::EMPTY_RECT;       // 表示进行一系列变换后,图层应该显示在的区域(大小、位置),即图层应该显示在屏幕的哪个位置及大小

    uint32_t width = 0;  // display宽度
    uint32_t height = 0; // display高度

    status_t write(Parcel& output) const;
    status_t read(const Parcel& input);
};
  • orientation表示屏幕显示的方向,一般为0/90/180/270。
  • layerStackSpaceRect定义的矩形区域用于表示原始layers区域大小,即图层的原始大小和位置。
  • orientedDisplaySpaceRect定义的矩形区域用于表示进行一系列变换后,图层应该显示在的区域(大小、位置),即图层应该显示在屏幕的哪个位置及大小

DisplayState指定了如何将图层显示到物理屏幕上,首先要先将从layerslayerStackSpaceRect平移(translate)和缩放到orientedDisplaySpaceRect区域,然后再根据orientation/width/height进行旋转,将图层显示到物理屏上指定的位置。

例如,假设定义了一个Rect(0, 0, 200, 100)的layerStackSpaceRect区域(大小为200x100),而orientedDisplaySpaceRect区域为Rect(20, 10, 420, 210),其大小为400x200,物理屏display的尺寸为WxH。当orientation为0时,首先需要将layers进行放大2倍以匹配目标显示区域的大小,然后将其从(0,0)点移动到(20,10)点。当orientation为1时,在进行完缩放和平衡动作后,还需要顺时钟旋转90度,再平衡(W, 0)后,就变换到了指定的物理屏上的显示位置。

ComposerState

ComposeState的定义如下,其中最关键的是layer_state_t结构体:

struct ComposerState {
    layer_state_t state;
    status_t write(Parcel& output) const;
    status_t read(const Parcel& input);
};

layer_state_t结构体内容较多,拆开来看
首先,定义了三个权限,含义比较简单

enum Permission {
    ACCESS_SURFACE_FLINGER = 0x1, // 访问权限
    ROTATE_SURFACE_FLINGER = 0x2, // 旋转权限
    INTERNAL_SYSTEM_WINDOW = 0x4, // 系统窗口权限
};

接口定义一组layer相关类型,一般用于创建图层时指定layer的类型

enum {
    eLayerHidden = 0x01,         // 隐藏此图层
    eLayerOpaque = 0x02,         // Layer不透明
    eLayerSkipScreenshot = 0x40, // 截图(屏)时跳过此layer,即截屏截不到此图层
    eLayerSecure = 0x80,         // 是否为安全图层,安全图层投屏时不会被投到外接屏
    // Queue up BufferStateLayer buffers instead of dropping the oldest buffer when this flag is
    // set. This blocks the client until all the buffers have been presented. If the buffers
    // have presentation timestamps, then we may drop buffers.
    eEnableBackpressure = 0x100,       // ENABLE_BACKPRESSURE
    eLayerIsDisplayDecoration = 0x200, // 装饰图层,如屏幕顶部和底部的圆角图层,隐私指示器图层等
    // Ignore any destination frame set on the layer. This is used when the buffer scaling mode
    // is freeze and the destination frame is applied asynchronously with the buffer submission.
    // This is needed to maintain compatibility for SurfaceView scaling behavior.
    // See SurfaceView scaling behavior for more details.
    eIgnoreDestinationFrame = 0x400,
};

这一组定义含义比较明确,用于表示layer发生了什么变化,一般配合transaction的各种setXXX方法进行使用,当transaction提交到SurfaceFlinger时,SurfaceFlinger于依据不同类型的变化进行相应的处理。

enum {
    ePositionChanged = 0x00000001,
    eLayerChanged = 0x00000002,
    eSizeChanged = 0x00000004,
    eAlphaChanged = 0x00000008,
    eMatrixChanged = 0x00000010,
    eTransparentRegionChanged = 0x00000020,
    eFlagsChanged = 0x00000040,
    eLayerStackChanged = 0x00000080,
    eDimmingEnabledChanged = 0x00000400,
    eShadowRadiusChanged = 0x00000800,
    /* unused 0x00001000, */
    eBufferCropChanged = 0x00002000,
    eRelativeLayerChanged = 0x00004000,
    eReparent = 0x00008000,
    eColorChanged = 0x00010000,
    eDestroySurface = 0x00020000,
    eTransformChanged = 0x00040000,
    eTransformToDisplayInverseChanged = 0x00080000,
    eCropChanged = 0x00100000,
    eBufferChanged = 0x00200000,
    /* unused 0x00400000, */
    eDataspaceChanged = 0x00800000,
    eHdrMetadataChanged = 0x01000000,
    eSurfaceDamageRegionChanged = 0x02000000,
    eApiChanged = 0x04000000,
    eSidebandStreamChanged = 0x08000000,
    eColorTransformChanged = 0x10000000,
    eHasListenerCallbacksChanged = 0x20000000,
    eInputInfoChanged = 0x40000000,
    eCornerRadiusChanged = 0x80000000,
    eDestinationFrameChanged = 0x1'00000000,
    /* unused = 0x2'00000000, */
    eBackgroundColorChanged = 0x4'00000000,
    eMetadataChanged = 0x8'00000000,
    eColorSpaceAgnosticChanged = 0x10'00000000,
    eFrameRateSelectionPriority = 0x20'00000000,
    eFrameRateChanged = 0x40'00000000,
    eBackgroundBlurRadiusChanged = 0x80'00000000,
    eProducerDisconnect = 0x100'00000000,
    eFixedTransformHintChanged = 0x200'00000000,
    /* unused 0x400'00000000, */
    eBlurRegionsChanged = 0x800'00000000,
    eAutoRefreshChanged = 0x1000'00000000,
    eStretchChanged = 0x2000'00000000,
    eTrustedOverlayChanged = 0x4000'00000000,
    eDropInputModeChanged = 0x8000'00000000
};
struct layer_state_t {
    // ......
    layer_state_t();

    void merge(const layer_state_t& other);
    status_t write(Parcel& output) const;
    status_t read(const Parcel& input);
    bool hasBufferChanges() const;
    bool hasValidBuffer() const;
    void sanitize(int32_t permissions);

    struct matrix22_t {
        float dsdx{0};
        float dtdx{0};
        float dtdy{0};
        float dsdy{0};
        status_t write(Parcel& output) const;
        status_t read(const Parcel& input);
    };
    sp<IBinder> surface;
    int32_t layerId;
    uint64_t what;
    float x;    // layer位置的x坐标
    float y;    // layer位置的y坐标
    int32_t z;  // layer的z轴顺序
    uint32_t w; // layer的宽度w
    uint32_t h; // layer的高度h
    ui::LayerStack layerStack = ui::DEFAULT_LAYER_STACK;
    float alpha;    // layer的透明度
    uint32_t flags; // layer发生变化的flags标记,参考前面layer发生变化的enum定义
    uint32_t mask;
    uint8_t reserved;
    matrix22_t matrix;
    float cornerRadius;  // layer的圆角半径
    uint32_t backgroundBlurRadius;
    sp<SurfaceControl> reparentSurfaceControl;

    sp<SurfaceControl> relativeLayerSurfaceControl;

    sp<SurfaceControl> parentSurfaceControlForChild;

    half3 color;

    Region transparentRegion; // non POD must be last. see write/read

    uint32_t transform;
    bool transformToDisplayInverse;
    Rect crop;
    std::shared_ptr<BufferData> bufferData = nullptr;
    ui::Dataspace dataspace;
    HdrMetadata hdrMetadata;
    Region surfaceDamageRegion;
    int32_t api;
    sp<NativeHandle> sidebandStream;
    mat4 colorTransform;
    std::vector<BlurRegion> blurRegions;
    sp<gui::WindowInfoHandle> windowInfoHandle = new gui::WindowInfoHandle();
    LayerMetadata metadata;

    float bgColorAlpha; // The following refer to the alpha, and dataspace, respectively of the background color layer
    ui::Dataspace bgColorDataspace;

    bool colorSpaceAgnostic;  // A color space agnostic layer means the color of this layer can be interpreted in any color space.

    std::vector<ListenerCallbacks> listeners;

    float shadowRadius; // 此layer的阴影半径
    int32_t frameRateSelectionPriority;
    
    float frameRate; // 此layer设定的预期刷新率
    int8_t frameRateCompatibility;
    int8_t changeFrameRateStrategy;

    // Set by window manager indicating the layer and all its children are
    // in a different orientation than the display. The hint suggests that
    // the graphic producers should receive a transform hint as if the
    // display was in this orientation. When the display changes to match
    // the layer orientation, the graphic producer may not need to allocate
    // a buffer of a different size. -1 means the transform hint is not set,
    // otherwise the value will be a valid ui::Rotation.
    ui::Transform::RotationFlags fixedTransformHint;

    // Indicates that the consumer should acquire the next frame as soon as it
    // can and not wait for a frame to become available. This is only relevant
    // in shared buffer mode.
    bool autoRefresh;

    // An inherited state that indicates that this surface control and its children
    // should be trusted for input occlusion detection purposes
    bool isTrustedOverlay;

    StretchEffect stretchEffect; // 此layer的拉伸效果
    Rect bufferCrop;
    Rect destinationFrame;

    gui::DropInputMode dropInputMode; // 强制inputflinger忽略所有此图层及其子图层接收到的input事件,即此图层不处理input事件。

    bool dimmingEnabled;
};
posted @ 2023-06-24 17:41  TPrime.A  阅读(691)  评论(0编辑  收藏  举报