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 <optional>
21 #include <string>
22 #include <vector>
23 
24 #include <ui/PictureProfileHandle.h>
25 #include <ui/Transform.h>
26 #include <utils/StrongPointer.h>
27 
28 // TODO(b/129481165): remove the #pragma below and fix conversion issues
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wconversion"
31 #pragma clang diagnostic ignored "-Wextra"
32 
33 #include <ui/DisplayIdentification.h>
34 #include "DisplayHardware/ComposerHal.h"
35 
36 #include "LayerFE.h"
37 
38 #include <aidl/android/hardware/graphics/composer3/Composition.h>
39 
40 // TODO(b/129481165): remove the #pragma below and fix conversion issues
41 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
42 
43 namespace android {
44 
45 namespace HWC2 {
46 class Layer;
47 } // namespace HWC2
48 
49 namespace compositionengine {
50 
51 class CompositionEngine;
52 class Output;
53 
54 namespace impl {
55 struct OutputLayerCompositionState;
56 } // namespace impl
57 
58 /**
59  * An output layer contains the output-dependent composition state for a layer
60  */
61 class OutputLayer {
62 public:
63     virtual ~OutputLayer();
64 
65     // Sets the HWC2::Layer associated with this layer
66     virtual void setHwcLayer(std::shared_ptr<HWC2::Layer>) = 0;
67 
68     // Gets the output which owns this output layer
69     virtual const Output& getOutput() const = 0;
70 
71     // Gets the front-end layer interface this output layer represents
72     virtual LayerFE& getLayerFE() const = 0;
73 
74     using CompositionState = compositionengine::impl::OutputLayerCompositionState;
75 
76     // Gets the raw composition state data for the layer
77     // TODO(lpique): Make this protected once it is only internally called.
78     virtual const CompositionState& getState() const = 0;
79 
80     // Allows mutable access to the raw composition state data for the layer.
81     // This is meant to be used by the various functions that are part of the
82     // composition process.
83     // TODO(lpique): Make this protected once it is only internally called.
84     virtual CompositionState& editState() = 0;
85 
86     // Clear the cache entries for a set of buffers that SurfaceFlinger no
87     // longer cares about.
88     virtual void uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) = 0;
89 
90     // Get the relative priority of the layer's picture profile with respect to the importance of
91     // the visual content to the user experience. Lower is higher priority.
92     virtual int64_t getPictureProfilePriority() const = 0;
93 
94     // The picture profile handle for the layer.
95     virtual const PictureProfileHandle& getPictureProfileHandle() const = 0;
96 
97     // Commit the picture profile to the composition state.
98     virtual void commitPictureProfileToCompositionState() = 0;
99 
100     // Recalculates the state of the output layer from the output-independent
101     // layer. If includeGeometry is false, the geometry state can be skipped.
102     // internalDisplayRotationFlags must be set to the rotation flags for the
103     // internal display, and is used to properly compute the inverse-display
104     // transform, if needed.
105     virtual void updateCompositionState(
106             bool includeGeometry, bool forceClientComposition,
107             ui::Transform::RotationFlags internalDisplayRotationFlags,
108             const std::optional<std::vector<
109                     std::optional<aidl::android::hardware::graphics::composer3::LutProperties>>>
110                     properties) = 0;
111 
112     // Writes the geometry state to the HWC, or does nothing if this layer does
113     // not use the HWC. If includeGeometry is false, the geometry state can be
114     // skipped. If skipLayer is true, then the alpha of the layer is forced to
115     // 0 so that HWC will ignore it. z specifies the order to draw the layer in
116     // (starting with 0 for the back layer, and increasing for each following
117     // layer). zIsOverridden specifies whether the layer has been reordered.
118     // isPeekingThrough specifies whether this layer will be shown through a
119     // hole punch in a layer above it.
120     virtual void writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
121                                  bool zIsOverridden, bool isPeekingThrough) = 0;
122 
123     // Updates the cursor position with the HWC
124     virtual void writeCursorPositionToHWC() const = 0;
125 
126     // Returns the HWC2::Layer associated with this layer, if it exists
127     virtual HWC2::Layer* getHwcLayer() const = 0;
128 
129     // Returns true if the current layer state requires client composition
130     virtual bool requiresClientComposition() const = 0;
131 
132     // Returns true if the current layer should be treated as a cursor layer
133     virtual bool isHardwareCursor() const = 0;
134 
135     // Applies a HWC device requested composition type change
136     virtual void applyDeviceCompositionTypeChange(
137             aidl::android::hardware::graphics::composer3::Composition) = 0;
138 
139     // Prepares to apply any HWC device layer requests
140     virtual void prepareForDeviceLayerRequests() = 0;
141 
142     // Applies a HWC device layer request
143     virtual void applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) = 0;
144 
145     // Applies a HWC device layer lut
146     virtual void applyDeviceLayerLut(
147             ndk::ScopedFileDescriptor,
148             std::vector<std::pair<
149                     int, aidl::android::hardware::graphics::composer3::LutProperties>>) = 0;
150 
151     // Returns true if the composition settings scale pixels
152     virtual bool needsFiltering() const = 0;
153 
154     // Returns LayerSettings to be used by RenderEngine if the layer has been overridden
155     // during the composition process
156     virtual std::optional<LayerFE::LayerSettings> getOverrideCompositionSettings() const = 0;
157 
158     // Debugging
159     virtual void dump(std::string& result) const = 0;
160 };
161 
162 } // namespace compositionengine
163 } // namespace android
164