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