1 /*
2  * Copyright 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 #ifndef ANDROID_HWC_DISPLAY_H
18 #define ANDROID_HWC_DISPLAY_H
19 
20 #include <aidl/android/hardware/graphics/common/DisplayDecorationSupport.h>
21 #include <aidl/android/hardware/graphics/composer3/ColorMode.h>
22 #include <aidl/android/hardware/graphics/composer3/ContentType.h>
23 #include <aidl/android/hardware/graphics/composer3/DisplayAttribute.h>
24 #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h>
25 #include <aidl/android/hardware/graphics/composer3/DisplayConnectionType.h>
26 #include <aidl/android/hardware/graphics/composer3/DisplayContentSample.h>
27 #include <aidl/android/hardware/graphics/composer3/DisplayIdentification.h>
28 #include <aidl/android/hardware/graphics/composer3/HdrCapabilities.h>
29 #include <aidl/android/hardware/graphics/composer3/PerFrameMetadataKey.h>
30 #include <aidl/android/hardware/graphics/composer3/PowerMode.h>
31 #include <aidl/android/hardware/graphics/composer3/ReadbackBufferAttributes.h>
32 #include <aidl/android/hardware/graphics/composer3/RenderIntent.h>
33 #include <aidl/android/hardware/graphics/composer3/VsyncPeriodChangeConstraints.h>
34 #include <aidl/android/hardware/graphics/composer3/VsyncPeriodChangeTimeline.h>
35 #include <android-base/unique_fd.h>
36 
37 #include <array>
38 #include <mutex>
39 #include <optional>
40 #include <thread>
41 #include <unordered_map>
42 #include <unordered_set>
43 #include <vector>
44 
45 #include "Common.h"
46 #include "DisplayChanges.h"
47 #include "DisplayConfig.h"
48 #include "DisplayFinder.h"
49 #include "FencedBuffer.h"
50 #include "FrameComposer.h"
51 #include "Layer.h"
52 #include "Time.h"
53 #include "VsyncThread.h"
54 
55 namespace aidl::android::hardware::graphics::composer3::impl {
56 
57 class FrameComposer;
58 
59 class Display {
60    public:
61     Display(FrameComposer* composer, int64_t id);
62     ~Display();
63 
64     Display(const Display& display) = delete;
65     Display& operator=(const Display& display) = delete;
66 
67     Display(Display&& display) = delete;
68     Display& operator=(Display&& display) = delete;
69 
70     HWC3::Error init(const std::vector<DisplayConfig>& configs, int32_t activeConfigId,
71                      const std::optional<std::vector<uint8_t>>& edid = std::nullopt);
72 
73     HWC3::Error updateParameters(uint32_t width, uint32_t height, uint32_t dpiX, uint32_t dpiY,
74                                  uint32_t refreshRateHz,
75                                  const std::optional<std::vector<uint8_t>>& edid = std::nullopt);
76 
77     // HWComposer3 interface.
78     HWC3::Error createLayer(int64_t* outLayerId);
79     HWC3::Error destroyLayer(int64_t layerId);
80     HWC3::Error getActiveConfig(int32_t* outConfigId);
81     HWC3::Error getDisplayAttribute(int32_t configId, DisplayAttribute attribute,
82                                     int32_t* outValue);
83     HWC3::Error getColorModes(std::vector<ColorMode>* outColorModes);
84     HWC3::Error getDisplayCapabilities(std::vector<DisplayCapability>* caps);
85     HWC3::Error getDisplayConfigs(std::vector<int32_t>* configs);
86     HWC3::Error getDisplayConfigurations(std::vector<DisplayConfiguration>* outConfigs);
87     HWC3::Error getDisplayConnectionType(DisplayConnectionType* outType);
88     HWC3::Error getDisplayIdentificationData(DisplayIdentification* outIdentification);
89     HWC3::Error getDisplayName(std::string* outName);
90     HWC3::Error getDisplayVsyncPeriod(int32_t* outVsyncPeriod);
91     HWC3::Error getDisplayedContentSample(int64_t maxFrames, int64_t timestamp,
92                                           DisplayContentSample* samples);
93     HWC3::Error getDisplayedContentSamplingAttributes(
94         DisplayContentSamplingAttributes* outAttributes);
95     HWC3::Error getDisplayPhysicalOrientation(common::Transform* outOrientation);
96     HWC3::Error getHdrCapabilities(HdrCapabilities* outCapabilities);
97     HWC3::Error getPerFrameMetadataKeys(std::vector<PerFrameMetadataKey>* outKeys);
98     HWC3::Error getReadbackBufferAttributes(ReadbackBufferAttributes* attrs);
99     HWC3::Error getReadbackBufferFence(ndk::ScopedFileDescriptor* acquireFence);
100     HWC3::Error getRenderIntents(ColorMode mode, std::vector<RenderIntent>* intents);
101     HWC3::Error getSupportedContentTypes(std::vector<ContentType>* types);
102     HWC3::Error getDecorationSupport(std::optional<common::DisplayDecorationSupport>* support);
103     HWC3::Error registerCallback(const std::shared_ptr<IComposerCallback>& callback);
104     HWC3::Error setActiveConfig(int32_t configId);
105     HWC3::Error setActiveConfigWithConstraints(int32_t config,
106                                                const VsyncPeriodChangeConstraints& constraints,
107                                                VsyncPeriodChangeTimeline* outTimeline);
108     HWC3::Error setBootConfig(int32_t configId);
109     HWC3::Error clearBootConfig();
110     HWC3::Error getPreferredBootConfig(int32_t* outConfigId);
111     HWC3::Error setAutoLowLatencyMode(bool on);
112     HWC3::Error setColorMode(ColorMode mode, RenderIntent intent);
113     HWC3::Error setContentType(ContentType contentType);
114     HWC3::Error setDisplayedContentSamplingEnabled(bool enable, FormatColorComponent componentMask,
115                                                    int64_t maxFrames);
116     HWC3::Error setPowerMode(PowerMode mode);
117     HWC3::Error setReadbackBuffer(const buffer_handle_t buffer,
118                                   const ndk::ScopedFileDescriptor& releaseFence);
119     HWC3::Error setVsyncEnabled(bool enabled);
120     HWC3::Error setIdleTimerEnabled(int32_t timeoutMs);
121     HWC3::Error setColorTransform(const std::vector<float>& transform);
122     HWC3::Error setBrightness(float brightness);
123     HWC3::Error setClientTarget(buffer_handle_t buffer, const ndk::ScopedFileDescriptor& fence,
124                                 common::Dataspace dataspace,
125                                 const std::vector<common::Rect>& damage);
126     HWC3::Error setOutputBuffer(buffer_handle_t buffer, const ndk::ScopedFileDescriptor& fence);
127     HWC3::Error setExpectedPresentTime(
128         const std::optional<ClockMonotonicTimestamp>& expectedPresentTime);
129     HWC3::Error validate(DisplayChanges* outChanges);
130     HWC3::Error acceptChanges();
131     HWC3::Error present(::android::base::unique_fd* outDisplayFence,
132                         std::unordered_map<int64_t, ::android::base::unique_fd>* outLayerFences);
133 
134     // Non HWCComposer3 interface.
getId()135     int64_t getId() const { return mId; }
136 
137     Layer* getLayer(int64_t layerHandle);
138 
139     HWC3::Error setEdid(std::vector<uint8_t> edid);
140 
hasColorTransform()141     bool hasColorTransform() const { return mColorTransform.has_value(); }
getColorTransform()142     std::array<float, 16> getColorTransform() const { return *mColorTransform; }
143 
getClientTarget()144     FencedBuffer& getClientTarget() { return mClientTarget; }
145     buffer_handle_t waitAndGetClientTargetBuffer();
146 
getOrderedLayers()147     const std::vector<Layer*>& getOrderedLayers() { return mOrderedLayers; }
148 
149    private:
150     bool hasConfig(int32_t configId) const;
151     DisplayConfig* getConfig(int32_t configId);
152 
153     std::optional<int32_t> getBootConfigId();
154 
155     void setLegacyEdid();
156 
157     // The state of this display should only be modified from
158     // SurfaceFlinger's main loop, with the exception of when dump is
159     // called. To prevent a bad state from crashing us during a dump
160     // call, all public calls into Display must acquire this mutex.
161     mutable std::recursive_mutex mStateMutex;
162 
163     FrameComposer* mComposer = nullptr;
164     const int64_t mId;
165     std::string mName;
166     PowerMode mPowerMode = PowerMode::OFF;
167     VsyncThread mVsyncThread;
168     FencedBuffer mClientTarget;
169     FencedBuffer mReadbackBuffer;
170     // Will only be non-null after the Display has been validated and
171     // before it has been accepted.
172     enum class PresentFlowState {
173         WAITING_FOR_VALIDATE,
174         WAITING_FOR_ACCEPT,
175         WAITING_FOR_PRESENT,
176     };
177     PresentFlowState mPresentFlowState = PresentFlowState::WAITING_FOR_VALIDATE;
178     DisplayChanges mPendingChanges;
179     std::optional<TimePoint> mExpectedPresentTime;
180     std::unordered_map<int64_t, std::unique_ptr<Layer>> mLayers;
181     // Ordered layers available after validate().
182     std::vector<Layer*> mOrderedLayers;
183     std::optional<int32_t> mActiveConfigId;
184     std::unordered_map<int32_t, DisplayConfig> mConfigs;
185     std::unordered_set<ColorMode> mColorModes = {ColorMode::NATIVE};
186     ColorMode mActiveColorMode = ColorMode::NATIVE;
187     std::optional<std::array<float, 16>> mColorTransform;
188     std::vector<uint8_t> mEdid;
189 };
190 
191 }  // namespace aidl::android::hardware::graphics::composer3::impl
192 
193 #endif
194