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