xref: /aosp_15_r20/external/drm_hwcomposer/hwc2_device/HwcLayer.h (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <aidl/android/hardware/graphics/common/Transform.h>
20 #include <hardware/hwcomposer2.h>
21 
22 #include "bufferinfo/BufferInfoGetter.h"
23 #include "compositor/LayerData.h"
24 
25 namespace android {
26 
27 class HwcDisplay;
28 
29 class HwcLayer {
30  public:
31   struct Buffer {
32     buffer_handle_t buffer_handle;
33     SharedFd acquire_fence;
34   };
35   // A set of properties to be validated.
36   struct LayerProperties {
37     std::optional<Buffer> buffer;
38     std::optional<BufferBlendMode> blend_mode;
39     std::optional<BufferColorSpace> color_space;
40     std::optional<BufferSampleRange> sample_range;
41     std::optional<HWC2::Composition> composition_type;
42     std::optional<hwc_rect_t> display_frame;
43     std::optional<float> alpha;
44     std::optional<hwc_frect_t> source_crop;
45     std::optional<LayerTransform> transform;
46     std::optional<uint32_t> z_order;
47   };
48 
HwcLayer(HwcDisplay * parent_display)49   explicit HwcLayer(HwcDisplay *parent_display) : parent_(parent_display){};
50 
GetSfType()51   HWC2::Composition GetSfType() const {
52     return sf_type_;
53   }
GetValidatedType()54   HWC2::Composition GetValidatedType() const {
55     return validated_type_;
56   }
AcceptTypeChange()57   void AcceptTypeChange() {
58     sf_type_ = validated_type_;
59   }
SetValidatedType(HWC2::Composition type)60   void SetValidatedType(HWC2::Composition type) {
61     validated_type_ = type;
62   }
IsTypeChanged()63   bool IsTypeChanged() const {
64     return sf_type_ != validated_type_;
65   }
66 
GetPriorBufferScanOutFlag()67   bool GetPriorBufferScanOutFlag() const {
68     return prior_buffer_scanout_flag_;
69   }
70 
SetPriorBufferScanOutFlag(bool state)71   void SetPriorBufferScanOutFlag(bool state) {
72     prior_buffer_scanout_flag_ = state;
73   }
74 
GetZOrder()75   uint32_t GetZOrder() const {
76     return z_order_;
77   }
78 
GetLayerData()79   auto &GetLayerData() {
80     return layer_data_;
81   }
82 
83   void SetLayerProperties(const LayerProperties &layer_properties);
84 
85   // HWC2 Layer hooks
86   HWC2::Error SetCursorPosition(int32_t /*x*/, int32_t /*y*/);
87   HWC2::Error SetLayerBlendMode(int32_t mode);
88   HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
89   HWC2::Error SetLayerColor(hwc_color_t /*color*/);
90   HWC2::Error SetLayerCompositionType(int32_t type);
91   HWC2::Error SetLayerDataspace(int32_t dataspace);
92   HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
93   HWC2::Error SetLayerPlaneAlpha(float alpha);
94   HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
95   HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
96   HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
97   HWC2::Error SetLayerTransform(int32_t transform);
98   HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
99   HWC2::Error SetLayerZOrder(uint32_t order);
100 
101  private:
102   // sf_type_ stores the initial type given to us by surfaceflinger,
103   // validated_type_ stores the type after running ValidateDisplay
104   HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
105   HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
106 
107   uint32_t z_order_ = 0;
108   LayerData layer_data_;
109 
110   /* The following buffer data can have 2 sources:
111    * 1 - Mapper@4 metadata API
112    * 2 - HWC@2 API
113    * We keep ability to have 2 sources in drm_hwc. It may be useful for CLIENT
114    * layer, at this moment HWC@2 API can't specify blending mode for this layer,
115    * but Mapper@4 can do that
116    */
117   BufferColorSpace color_space_{};
118   BufferSampleRange sample_range_{};
119   BufferBlendMode blend_mode_{};
120   buffer_handle_t buffer_handle_{};
121   bool buffer_handle_updated_{};
122 
123   bool prior_buffer_scanout_flag_{};
124 
125   HwcDisplay *const parent_;
126 
127   /* Layer state */
128  public:
129   void PopulateLayerData();
130 
IsLayerUsableAsDevice()131   bool IsLayerUsableAsDevice() const {
132     return !bi_get_failed_ && !fb_import_failed_ && buffer_handle_ != nullptr;
133   }
134 
135  private:
136   void ImportFb();
137   bool bi_get_failed_{};
138   bool fb_import_failed_{};
139 
140   /* SwapChain Cache */
141  public:
142   void SwChainClearCache();
143 
144  private:
145   struct SwapChainElement {
146     std::optional<BufferInfo> bi;
147     std::shared_ptr<DrmFbIdHandle> fb;
148   };
149 
150   bool SwChainGetBufferFromCache(BufferUniqueId unique_id);
151   void SwChainReassemble(BufferUniqueId unique_id);
152   void SwChainAddCurrentBuffer(BufferUniqueId unique_id);
153 
154   std::map<int /*seq_no*/, SwapChainElement> swchain_cache_;
155   std::map<BufferUniqueId, int /*seq_no*/> swchain_lookup_table_;
156   bool swchain_reassembled_{};
157 };
158 
159 }  // namespace android
160