1 /* 2 * Copyright 2021 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 "ComposerHal.h" 20 21 #include <ftl/shared_mutex.h> 22 #include <ui/DisplayMap.h> 23 24 #include <functional> 25 #include <optional> 26 #include <string> 27 #include <utility> 28 #include <vector> 29 30 // TODO(b/129481165): remove the #pragma below and fix conversion issues 31 #pragma clang diagnostic push 32 #pragma clang diagnostic ignored "-Wconversion" 33 #pragma clang diagnostic ignored "-Wextra" 34 35 #include <android/hardware/graphics/composer/2.4/IComposer.h> 36 #include <android/hardware/graphics/composer/2.4/IComposerClient.h> 37 38 #include <aidl/android/hardware/graphics/composer3/IComposer.h> 39 #include <aidl/android/hardware/graphics/composer3/IComposerClient.h> 40 #include <android/hardware/graphics/composer3/ComposerClientReader.h> 41 #include <android/hardware/graphics/composer3/ComposerClientWriter.h> 42 43 #include <aidl/android/hardware/graphics/composer3/Composition.h> 44 #include <aidl/android/hardware/graphics/composer3/DisplayCapability.h> 45 46 // TODO(b/129481165): remove the #pragma below and fix conversion issues 47 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" 48 49 namespace android::Hwc2 { 50 51 using aidl::android::hardware::graphics::common::DisplayDecorationSupport; 52 using aidl::android::hardware::graphics::common::HdrConversionCapability; 53 using aidl::android::hardware::graphics::common::HdrConversionStrategy; 54 using aidl::android::hardware::graphics::composer3::ComposerClientReader; 55 using aidl::android::hardware::graphics::composer3::ComposerClientWriter; 56 using aidl::android::hardware::graphics::composer3::OverlayProperties; 57 58 class AidlIComposerCallbackWrapper; 59 60 // Composer is a wrapper to IComposer, a proxy to server-side composer. 61 class AidlComposer final : public Hwc2::Composer { 62 public: 63 static bool isDeclared(const std::string& serviceName); 64 65 explicit AidlComposer(const std::string& serviceName); 66 ~AidlComposer() override; 67 68 bool isSupported(OptionalFeature) const; 69 bool isVrrSupported() const; 70 71 std::vector<aidl::android::hardware::graphics::composer3::Capability> getCapabilities() 72 override; 73 std::string dumpDebugInfo() override; 74 75 void registerCallback(HWC2::ComposerCallback& callback) override; 76 77 // Explicitly flush all pending commands in the command buffer. 78 Error executeCommands(Display) override; 79 80 uint32_t getMaxVirtualDisplayCount() override; 81 Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format, 82 Display* outDisplay) override; 83 Error destroyVirtualDisplay(Display display) override; 84 85 Error acceptDisplayChanges(Display display) override; 86 87 Error createLayer(Display display, Layer* outLayer) override; 88 Error destroyLayer(Display display, Layer layer) override; 89 90 Error getActiveConfig(Display display, Config* outConfig) override; 91 Error getChangedCompositionTypes( 92 Display display, std::vector<Layer>* outLayers, 93 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) 94 override; 95 Error getColorModes(Display display, std::vector<ColorMode>* outModes) override; 96 Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute, 97 int32_t* outValue) override; 98 Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs); 99 Error getDisplayConfigurations(Display, int32_t maxFrameIntervalNs, 100 std::vector<DisplayConfiguration>*); 101 Error getDisplayName(Display display, std::string* outName) override; 102 103 Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, 104 std::vector<Layer>* outLayers, 105 std::vector<uint32_t>* outLayerRequestMasks) override; 106 107 Error getDozeSupport(Display display, bool* outSupport) override; 108 Error hasDisplayIdleTimerCapability(Display display, bool* outSupport) override; 109 Error getHdrCapabilities(Display display, std::vector<Hdr>* outHdrTypes, float* outMaxLuminance, 110 float* outMaxAverageLuminance, float* outMinLuminance) override; 111 Error getOverlaySupport(OverlayProperties* outProperties) override; 112 113 Error getReleaseFences(Display display, std::vector<Layer>* outLayers, 114 std::vector<int>* outReleaseFences) override; 115 116 Error presentDisplay(Display display, int* outPresentFence) override; 117 118 Error setActiveConfig(Display display, Config config) override; 119 120 /* 121 * The composer caches client targets internally. When target is nullptr, 122 * the composer uses slot to look up the client target from its cache. 123 * When target is not nullptr, the cache is updated with the new target. 124 */ 125 Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target, 126 int acquireFence, Dataspace dataspace, 127 const std::vector<IComposerClient::Rect>& damage, 128 float hdrSdrRatio) override; 129 Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) override; 130 Error setColorTransform(Display display, const float* matrix) override; 131 Error setOutputBuffer(Display display, const native_handle_t* buffer, 132 int releaseFence) override; 133 Error setPowerMode(Display display, IComposerClient::PowerMode mode) override; 134 Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override; 135 136 Error setClientTargetSlotCount(Display display) override; 137 138 Error validateDisplay(Display display, nsecs_t expectedPresentTime, int32_t frameIntervalNs, 139 uint32_t* outNumTypes, uint32_t* outNumRequests) override; 140 141 Error presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime, 142 int32_t frameIntervalNs, uint32_t* outNumTypes, 143 uint32_t* outNumRequests, int* outPresentFence, 144 uint32_t* state) override; 145 146 Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override; 147 /* see setClientTarget for the purpose of slot */ 148 Error setLayerBuffer(Display display, Layer layer, uint32_t slot, 149 const sp<GraphicBuffer>& buffer, int acquireFence) override; 150 Error setLayerBufferSlotsToClear(Display display, Layer layer, 151 const std::vector<uint32_t>& slotsToClear, 152 uint32_t activeBufferSlot) override; 153 Error setLayerSurfaceDamage(Display display, Layer layer, 154 const std::vector<IComposerClient::Rect>& damage) override; 155 Error setLayerBlendMode(Display display, Layer layer, IComposerClient::BlendMode mode) override; 156 Error setLayerColor(Display display, Layer layer, const Color& color) override; 157 Error setLayerCompositionType( 158 Display display, Layer layer, 159 aidl::android::hardware::graphics::composer3::Composition type) override; 160 Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) override; 161 Error setLayerDisplayFrame(Display display, Layer layer, 162 const IComposerClient::Rect& frame) override; 163 Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override; 164 Error setLayerSidebandStream(Display display, Layer layer, 165 const native_handle_t* stream) override; 166 Error setLayerSourceCrop(Display display, Layer layer, 167 const IComposerClient::FRect& crop) override; 168 Error setLayerTransform(Display display, Layer layer, Transform transform) override; 169 Error setLayerVisibleRegion(Display display, Layer layer, 170 const std::vector<IComposerClient::Rect>& visible) override; 171 Error setLayerZOrder(Display display, Layer layer, uint32_t z) override; 172 173 // Composer HAL 2.2 174 Error setLayerPerFrameMetadata( 175 Display display, Layer layer, 176 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) override; 177 std::vector<IComposerClient::PerFrameMetadataKey> getPerFrameMetadataKeys( 178 Display display) override; 179 Error getRenderIntents(Display display, ColorMode colorMode, 180 std::vector<RenderIntent>* outRenderIntents) override; 181 Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) override; 182 183 // Composer HAL 2.3 184 Error getDisplayIdentificationData(Display display, uint8_t* outPort, 185 std::vector<uint8_t>* outData) override; 186 Error setLayerColorTransform(Display display, Layer layer, const float* matrix) override; 187 Error getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat, 188 Dataspace* outDataspace, 189 uint8_t* outComponentMask) override; 190 Error setDisplayContentSamplingEnabled(Display display, bool enabled, uint8_t componentMask, 191 uint64_t maxFrames) override; 192 Error getDisplayedContentSample(Display display, uint64_t maxFrames, uint64_t timestamp, 193 DisplayedFrameStats* outStats) override; 194 Error setLayerPerFrameMetadataBlobs( 195 Display display, Layer layer, 196 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) override; 197 Error setDisplayBrightness(Display display, float brightness, float brightnessNits, 198 const DisplayBrightnessOptions& options) override; 199 200 // Composer HAL 2.4 201 Error getDisplayCapabilities( 202 Display display, 203 std::vector<aidl::android::hardware::graphics::composer3::DisplayCapability>* 204 outCapabilities) override; 205 V2_4::Error getDisplayConnectionType(Display display, 206 IComposerClient::DisplayConnectionType* outType) override; 207 V2_4::Error getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) override; 208 V2_4::Error setActiveConfigWithConstraints( 209 Display display, Config config, 210 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints, 211 VsyncPeriodChangeTimeline* outTimeline) override; 212 V2_4::Error setAutoLowLatencyMode(Display displayId, bool on) override; 213 V2_4::Error getSupportedContentTypes( 214 Display displayId, 215 std::vector<IComposerClient::ContentType>* outSupportedContentTypes) override; 216 V2_4::Error setContentType(Display displayId, 217 IComposerClient::ContentType contentType) override; 218 V2_4::Error setLayerGenericMetadata(Display display, Layer layer, const std::string& key, 219 bool mandatory, const std::vector<uint8_t>& value) override; 220 V2_4::Error getLayerGenericMetadataKeys( 221 std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) override; 222 Error getClientTargetProperty( 223 Display display, 224 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness* 225 outClientTargetProperty) override; 226 227 // AIDL Composer HAL 228 Error setLayerBrightness(Display display, Layer layer, float brightness) override; 229 Error setLayerBlockingRegion(Display display, Layer layer, 230 const std::vector<IComposerClient::Rect>& blocking) override; 231 Error setBootDisplayConfig(Display displayId, Config) override; 232 Error clearBootDisplayConfig(Display displayId) override; 233 Error getPreferredBootDisplayConfig(Display displayId, Config*) override; 234 Error getDisplayDecorationSupport(Display display, 235 std::optional<DisplayDecorationSupport>* support) override; 236 Error setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) override; 237 238 Error getPhysicalDisplayOrientation(Display displayId, 239 AidlTransform* outDisplayOrientation) override; 240 void onHotplugConnect(Display) override; 241 void onHotplugDisconnect(Display) override; 242 Error getHdrConversionCapabilities(std::vector<HdrConversionCapability>*) override; 243 Error setHdrConversionStrategy(HdrConversionStrategy, Hdr*) override; 244 Error setRefreshRateChangedCallbackDebugEnabled(Display, bool) override; 245 Error notifyExpectedPresent(Display, nsecs_t expectedPresentTime, 246 int32_t frameIntervalNs) override; 247 248 private: 249 // Many public functions above simply write a command into the command 250 // queue to batch the calls. validateDisplay and presentDisplay will call 251 // this function to execute the command queue. 252 Error execute(Display) REQUIRES_SHARED(mMutex); 253 254 // returns the default instance name for the given service 255 static std::string instance(const std::string& serviceName); 256 257 ftl::Optional<std::reference_wrapper<ComposerClientWriter>> getWriter(Display) 258 REQUIRES_SHARED(mMutex); 259 ftl::Optional<std::reference_wrapper<ComposerClientReader>> getReader(Display) 260 REQUIRES_SHARED(mMutex); 261 void addDisplay(Display) EXCLUDES(mMutex); 262 void removeDisplay(Display) EXCLUDES(mMutex); 263 void addReader(Display) REQUIRES(mMutex); 264 void removeReader(Display) REQUIRES(mMutex); 265 bool getLayerLifecycleBatchCommand(); 266 bool hasMultiThreadedPresentSupport(Display); 267 268 // 64KiB minus a small space for metadata such as read/write pointers 269 static constexpr size_t kWriterInitialSize = 64 * 1024 / sizeof(uint32_t) - 16; 270 // Max number of buffers that may be cached for a given layer 271 // We obtain this number by: 272 // 1. Tightly coupling this cache to the max size of BufferQueue 273 // 2. Adding an additional slot for the layer caching feature in SurfaceFlinger (see: Planner.h) 274 static const constexpr uint32_t kMaxLayerBufferCount = BufferQueue::NUM_BUFFER_SLOTS + 1; 275 276 // Without DisplayCapability::MULTI_THREADED_PRESENT, we use a single reader 277 // for all displays. With the capability, we use a separate reader for each 278 // display. 279 bool mSingleReader = true; 280 // Invalid displayId used as a key to mReaders when mSingleReader is true. 281 static constexpr int64_t kSingleReaderKey = 0; 282 283 ui::PhysicalDisplayMap<Display, ComposerClientWriter> mWriters GUARDED_BY(mMutex); 284 ui::PhysicalDisplayMap<Display, ComposerClientReader> mReaders GUARDED_BY(mMutex); 285 286 // Protect access to mWriters and mReaders with a shared_mutex. Adding and 287 // removing a display require exclusive access, since the iterator or the 288 // writer/reader may be invalidated. Other calls need shared access while 289 // using the writer/reader, so they can use their display's writer/reader 290 // without it being deleted or the iterator being invalidated. 291 // TODO (b/257958323): Use std::shared_mutex and RAII once they support 292 // threading annotations. 293 ftl::SharedMutex mMutex; 294 295 int32_t mComposerInterfaceVersion = 1; 296 bool mEnableLayerCommandBatchingFlag = false; 297 std::atomic<int64_t> mLayerID = 1; 298 299 // Buffer slots for layers are cleared by setting the slot buffer to this buffer. 300 sp<GraphicBuffer> mClearSlotBuffer; 301 302 // Aidl interface 303 using AidlIComposer = aidl::android::hardware::graphics::composer3::IComposer; 304 using AidlIComposerClient = aidl::android::hardware::graphics::composer3::IComposerClient; 305 std::shared_ptr<AidlIComposer> mAidlComposer; 306 std::shared_ptr<AidlIComposerClient> mAidlComposerClient; 307 std::shared_ptr<AidlIComposerCallbackWrapper> mAidlComposerCallback; 308 }; 309 310 } // namespace android::Hwc2 311