1 /*
2  * Copyright 2019 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 <cstdint>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <vector>
24 
25 #include <compositionengine/LayerFE.h>
26 #include <compositionengine/OutputLayer.h>
27 #include <ui/FloatRect.h>
28 #include <ui/PictureProfileHandle.h>
29 #include <ui/Rect.h>
30 
31 #include <ui/DisplayIdentification.h>
32 
33 #include <aidl/android/hardware/graphics/composer3/Composition.h>
34 
35 using aidl::android::hardware::graphics::composer3::LutProperties;
36 
37 namespace android::compositionengine {
38 
39 struct LayerFECompositionState;
40 
41 namespace impl {
42 
43 // The implementation class contains the common implementation, but does not
44 // actually contain the final layer state.
45 class OutputLayer : public virtual compositionengine::OutputLayer {
46 public:
47     ~OutputLayer() override;
48 
49     void setHwcLayer(std::shared_ptr<HWC2::Layer>) override;
50 
51     void uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) override;
52     int64_t getPictureProfilePriority() const override;
53     const PictureProfileHandle& getPictureProfileHandle() const override;
54     void commitPictureProfileToCompositionState() override;
55 
56     void updateCompositionState(bool includeGeometry, bool forceClientComposition,
57                                 ui::Transform::RotationFlags,
58                                 const std::optional<std::vector<std::optional<LutProperties>>>
59                                         properties = std::nullopt) override;
60     void writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z, bool zIsOverridden,
61                          bool isPeekingThrough) override;
62     void writeCursorPositionToHWC() const override;
63 
64     HWC2::Layer* getHwcLayer() const override;
65     bool requiresClientComposition() const override;
66     bool isHardwareCursor() const override;
67     void applyDeviceCompositionTypeChange(
68             aidl::android::hardware::graphics::composer3::Composition) override;
69     void prepareForDeviceLayerRequests() override;
70     void applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) override;
71     void applyDeviceLayerLut(ndk::ScopedFileDescriptor,
72                              std::vector<std::pair<int, LutProperties>>) override;
73     bool needsFiltering() const override;
74     std::optional<LayerFE::LayerSettings> getOverrideCompositionSettings() const override;
75 
76     void dump(std::string&) const override;
77     virtual FloatRect calculateOutputSourceCrop(uint32_t internalDisplayRotationFlags) const;
78     virtual Rect calculateOutputDisplayFrame() const;
79     virtual uint32_t calculateOutputRelativeBufferTransform(
80             uint32_t internalDisplayRotationFlags) const;
81 
82 protected:
83     // Implemented by the final implementation for the final state it uses.
84     virtual void dumpState(std::string&) const = 0;
85 
86 private:
87     Rect calculateInitialCrop() const;
88     void writeOutputDependentGeometryStateToHWC(
89             HWC2::Layer*, aidl::android::hardware::graphics::composer3::Composition, uint32_t z);
90     void writeOutputIndependentGeometryStateToHWC(HWC2::Layer*, const LayerFECompositionState&,
91                                                   bool skipLayer);
92     void writeOutputDependentPerFrameStateToHWC(HWC2::Layer*);
93     void writeOutputIndependentPerFrameStateToHWC(
94             HWC2::Layer*, const LayerFECompositionState&,
95             aidl::android::hardware::graphics::composer3::Composition compositionType,
96             bool skipLayer);
97     void writeSolidColorStateToHWC(HWC2::Layer*, const LayerFECompositionState&);
98     void writeSidebandStateToHWC(HWC2::Layer*, const LayerFECompositionState&);
99     void writeBufferStateToHWC(HWC2::Layer*, const LayerFECompositionState&, bool skipLayer);
100     void writeCompositionTypeToHWC(HWC2::Layer*,
101                                    aidl::android::hardware::graphics::composer3::Composition,
102                                    bool isPeekingThrough, bool skipLayer);
103     void writeLutToHWC(HWC2::Layer*, const LayerFECompositionState&);
104     void detectDisallowedCompositionTypeChange(
105             aidl::android::hardware::graphics::composer3::Composition from,
106             aidl::android::hardware::graphics::composer3::Composition to) const;
107     bool isClientCompositionForced(bool isPeekingThrough) const;
108     void updateLuts(const LayerFECompositionState&,
109                     const std::optional<std::vector<std::optional<LutProperties>>>& properties);
110 };
111 
112 // This template factory function standardizes the implementation details of the
113 // final class using the types actually required by the implementation. This is
114 // not possible to do in the base class as those types may not even be visible
115 // to the base code.
116 template <typename BaseOutputLayer>
createOutputLayerTemplated(const Output & output,sp<LayerFE> layerFE)117 std::unique_ptr<BaseOutputLayer> createOutputLayerTemplated(const Output& output,
118                                                             sp<LayerFE> layerFE) {
119     class OutputLayer final : public BaseOutputLayer {
120     public:
121 // Clang incorrectly complains that these are unused.
122 #pragma clang diagnostic push
123 #pragma clang diagnostic ignored "-Wunused-local-typedef"
124 
125         using OutputLayerCompositionState = std::remove_const_t<
126                 std::remove_reference_t<decltype(std::declval<BaseOutputLayer>().getState())>>;
127         using Output = std::remove_const_t<
128                 std::remove_reference_t<decltype(std::declval<BaseOutputLayer>().getOutput())>>;
129         using LayerFE =
130                 std::remove_reference_t<decltype(std::declval<BaseOutputLayer>().getLayerFE())>;
131 
132 #pragma clang diagnostic pop
133 
134         OutputLayer(const Output& output, const sp<LayerFE>& layerFE)
135               : mOutput(output), mLayerFE(layerFE) {}
136         ~OutputLayer() override = default;
137 
138     private:
139         // compositionengine::OutputLayer overrides
140         const Output& getOutput() const override { return mOutput; }
141         LayerFE& getLayerFE() const override { return *mLayerFE; }
142         const OutputLayerCompositionState& getState() const override { return mState; }
143         OutputLayerCompositionState& editState() override { return mState; }
144 
145         // compositionengine::impl::OutputLayer overrides
146         void dumpState(std::string& out) const override { mState.dump(out); }
147 
148         const Output& mOutput;
149         const sp<LayerFE> mLayerFE;
150         OutputLayerCompositionState mState;
151     };
152 
153     return std::make_unique<OutputLayer>(output, layerFE);
154 }
155 
156 std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output&,
157                                                const sp<LayerFE>&);
158 
159 } // namespace impl
160 } // namespace android::compositionengine
161