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