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