• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <memory>
22 
23 #include "bufferinfo/BufferInfo.h"
24 #include "bufferinfo/BufferInfoGetter.h"
25 #include "compositor/LayerData.h"
26 #include "utils/fd.h"
27 
28 namespace android {
29 
30 class HwcDisplay;
31 
32 class FrontendLayerBase {
33  public:
34   virtual ~FrontendLayerBase() = default;
35 };
36 
37 class HwcLayer {
38  public:
39   struct Buffer {
40     int32_t slot_id;
41     std::optional<BufferInfo> bi;
42   };
43   struct Slot {
44     int32_t slot_id;
45     SharedFd fence;
46   };
47   // A set of properties to be validated.
48   struct LayerProperties {
49     std::optional<Buffer> slot_buffer;
50     std::optional<Slot> active_slot;
51     std::optional<BufferBlendMode> blend_mode;
52     std::optional<BufferColorSpace> color_space;
53     std::optional<BufferSampleRange> sample_range;
54     std::optional<HWC2::Composition> composition_type;
55     std::optional<DstRectInfo> display_frame;
56     std::optional<float> alpha;
57     std::optional<SrcRectInfo> source_crop;
58     std::optional<LayerTransform> transform;
59     std::optional<uint32_t> z_order;
60   };
61 
HwcLayer(HwcDisplay * parent_display)62   explicit HwcLayer(HwcDisplay *parent_display) : parent_(parent_display){};
63 
GetSfType()64   HWC2::Composition GetSfType() const {
65     return sf_type_;
66   }
GetValidatedType()67   HWC2::Composition GetValidatedType() const {
68     return validated_type_;
69   }
AcceptTypeChange()70   void AcceptTypeChange() {
71     sf_type_ = validated_type_;
72   }
SetValidatedType(HWC2::Composition type)73   void SetValidatedType(HWC2::Composition type) {
74     validated_type_ = type;
75   }
IsTypeChanged()76   bool IsTypeChanged() const {
77     return sf_type_ != validated_type_;
78   }
79 
GetPriorBufferScanOutFlag()80   bool GetPriorBufferScanOutFlag() const {
81     return prior_buffer_scanout_flag_;
82   }
83 
SetPriorBufferScanOutFlag(bool state)84   void SetPriorBufferScanOutFlag(bool state) {
85     prior_buffer_scanout_flag_ = state;
86   }
87 
GetZOrder()88   uint32_t GetZOrder() const {
89     return z_order_;
90   }
91 
GetLayerData()92   auto &GetLayerData() {
93     return layer_data_;
94   }
95 
96   void SetLayerProperties(const LayerProperties &layer_properties);
97 
98   auto GetFrontendPrivateData() -> std::shared_ptr<FrontendLayerBase> {
99     return frontend_private_data_;
100   }
101 
SetFrontendPrivateData(std::shared_ptr<FrontendLayerBase> data)102   auto SetFrontendPrivateData(std::shared_ptr<FrontendLayerBase> data) {
103     frontend_private_data_ = std::move(data);
104   }
105 
106  private:
107   // sf_type_ stores the initial type given to us by surfaceflinger,
108   // validated_type_ stores the type after running ValidateDisplay
109   HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
110   HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
111 
112   uint32_t z_order_ = 0;
113   LayerData layer_data_;
114 
115   /* The following buffer data can have 2 sources:
116    * 1 - Mapper@4 metadata API
117    * 2 - HWC@2 API
118    * We keep ability to have 2 sources in drm_hwc. It may be useful for CLIENT
119    * layer, at this moment HWC@2 API can't specify blending mode for this layer,
120    * but Mapper@4 can do that
121    */
122   BufferColorSpace color_space_{};
123   BufferSampleRange sample_range_{};
124   BufferBlendMode blend_mode_{};
125   bool buffer_updated_{};
126 
127   bool prior_buffer_scanout_flag_{};
128 
129   HwcDisplay *const parent_;
130 
131   std::shared_ptr<FrontendLayerBase> frontend_private_data_;
132 
133   std::optional<int32_t> active_slot_id_;
134   struct BufferSlot {
135     BufferInfo bi;
136     std::shared_ptr<DrmFbIdHandle> fb;
137   };
138   std::map<int32_t /*slot*/, BufferSlot> slots_;
139 
140   void ImportFb();
141   bool fb_import_failed_{};
142 
143  public:
144   void PopulateLayerData();
145   void ClearSlots();
146 
IsLayerUsableAsDevice()147   bool IsLayerUsableAsDevice() const {
148     return !fb_import_failed_ && active_slot_id_.has_value() &&
149            slots_.count(*active_slot_id_) > 0;
150   }
151 };
152 
153 }  // namespace android
154