1 /* 2 * Copyright 2016 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 <optional> 22 #include <string> 23 #include <unordered_map> 24 #include <utility> 25 #include <vector> 26 27 // TODO(b/129481165): remove the #pragma below and fix conversion issues 28 #pragma clang diagnostic push 29 #pragma clang diagnostic ignored "-Wconversion" 30 #pragma clang diagnostic ignored "-Wextra" 31 32 #include <composer-command-buffer/2.4/ComposerCommandBuffer.h> 33 #include <gui/BufferQueue.h> 34 #include <gui/HdrMetadata.h> 35 #include <math/mat4.h> 36 #include <ui/DisplayedFrameStats.h> 37 #include <ui/GraphicBuffer.h> 38 #include <utils/StrongPointer.h> 39 40 #include <aidl/android/hardware/graphics/composer3/Composition.h> 41 42 // TODO(b/129481165): remove the #pragma below and fix conversion issues 43 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" 44 45 namespace android::Hwc2 { 46 47 namespace types = hardware::graphics::common; 48 49 namespace V2_1 = hardware::graphics::composer::V2_1; 50 namespace V2_2 = hardware::graphics::composer::V2_2; 51 namespace V2_3 = hardware::graphics::composer::V2_3; 52 namespace V2_4 = hardware::graphics::composer::V2_4; 53 54 using types::V1_0::ColorTransform; 55 using types::V1_0::Transform; 56 using types::V1_1::RenderIntent; 57 using types::V1_2::ColorMode; 58 using types::V1_2::Dataspace; 59 using types::V1_2::PixelFormat; 60 61 using V2_1::Config; 62 using V2_1::Display; 63 using V2_1::Error; 64 using V2_1::Layer; 65 using V2_4::CommandReaderBase; 66 using V2_4::CommandWriterBase; 67 using V2_4::IComposer; 68 using V2_4::IComposerCallback; 69 using V2_4::IComposerClient; 70 using V2_4::VsyncPeriodChangeTimeline; 71 using V2_4::VsyncPeriodNanos; 72 using PerFrameMetadata = IComposerClient::PerFrameMetadata; 73 using PerFrameMetadataKey = IComposerClient::PerFrameMetadataKey; 74 using PerFrameMetadataBlob = IComposerClient::PerFrameMetadataBlob; 75 76 class CommandReader : public CommandReaderBase { 77 public: 78 ~CommandReader(); 79 80 // Parse and execute commands from the command queue. The commands are 81 // actually return values from the server and will be saved in ReturnData. 82 Error parse(); 83 84 // Get and clear saved errors. 85 struct CommandError { 86 uint32_t location; 87 Error error; 88 }; 89 std::vector<CommandError> takeErrors(); 90 91 bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes, 92 uint32_t* outNumLayerRequestMasks) const; 93 94 // Get and clear saved changed composition types. 95 void takeChangedCompositionTypes( 96 Display display, std::vector<Layer>* outLayers, 97 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes); 98 99 // Get and clear saved display requests. 100 void takeDisplayRequests(Display display, uint32_t* outDisplayRequestMask, 101 std::vector<Layer>* outLayers, 102 std::vector<uint32_t>* outLayerRequestMasks); 103 104 // Get and clear saved release fences. 105 void takeReleaseFences(Display display, std::vector<Layer>* outLayers, 106 std::vector<int>* outReleaseFences); 107 108 // Get and clear saved present fence. 109 void takePresentFence(Display display, int* outPresentFence); 110 111 // Get what stage succeeded during PresentOrValidate: Present or Validate 112 void takePresentOrValidateStage(Display display, uint32_t* state); 113 114 // Get the client target properties requested by hardware composer. 115 void takeClientTargetProperty(Display display, 116 IComposerClient::ClientTargetProperty* outClientTargetProperty); 117 118 private: 119 void resetData(); 120 121 bool parseSelectDisplay(uint16_t length); 122 bool parseSetError(uint16_t length); 123 bool parseSetChangedCompositionTypes(uint16_t length); 124 bool parseSetDisplayRequests(uint16_t length); 125 bool parseSetPresentFence(uint16_t length); 126 bool parseSetReleaseFences(uint16_t length); 127 bool parseSetPresentOrValidateDisplayResult(uint16_t length); 128 bool parseSetClientTargetProperty(uint16_t length); 129 130 struct ReturnData { 131 uint32_t displayRequests = 0; 132 133 std::vector<Layer> changedLayers; 134 std::vector<aidl::android::hardware::graphics::composer3::Composition> compositionTypes; 135 136 std::vector<Layer> requestedLayers; 137 std::vector<uint32_t> requestMasks; 138 139 int presentFence = -1; 140 141 std::vector<Layer> releasedLayers; 142 std::vector<int> releaseFences; 143 144 uint32_t presentOrValidateState; 145 146 // Composer 2.4 implementation can return a client target property 147 // structure to indicate the client target properties that hardware 148 // composer requests. The composer client must change the client target 149 // properties to match this request. 150 IComposerClient::ClientTargetProperty clientTargetProperty{PixelFormat::RGBA_8888, 151 Dataspace::UNKNOWN}; 152 }; 153 154 std::vector<CommandError> mErrors; 155 std::unordered_map<Display, ReturnData> mReturnData; 156 157 // When SELECT_DISPLAY is parsed, this is updated to point to the 158 // display's return data in mReturnData. We use it to avoid repeated 159 // map lookups. 160 ReturnData* mCurrentReturnData; 161 }; 162 163 // Composer is a wrapper to IComposer, a proxy to server-side composer. 164 class HidlComposer final : public Composer { 165 public: 166 explicit HidlComposer(const std::string& serviceName); 167 ~HidlComposer() override; 168 169 bool isSupported(OptionalFeature) const; 170 bool isVrrSupported() const; 171 172 std::vector<aidl::android::hardware::graphics::composer3::Capability> getCapabilities() 173 override; 174 std::string dumpDebugInfo() override; 175 176 void registerCallback(HWC2::ComposerCallback& callback) override; 177 178 // Explicitly flush all pending commands in the command buffer. 179 Error executeCommands(Display) override; 180 181 uint32_t getMaxVirtualDisplayCount() override; 182 Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format, 183 Display* outDisplay) override; 184 Error destroyVirtualDisplay(Display display) override; 185 186 Error acceptDisplayChanges(Display display) override; 187 188 Error createLayer(Display display, Layer* outLayer) override; 189 Error destroyLayer(Display display, Layer layer) override; 190 191 Error getActiveConfig(Display display, Config* outConfig) override; 192 Error getChangedCompositionTypes( 193 Display display, std::vector<Layer>* outLayers, 194 std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) 195 override; 196 Error getColorModes(Display display, std::vector<ColorMode>* outModes) override; 197 Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute, 198 int32_t* outValue) override; 199 Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs); 200 Error getDisplayConfigurations(Display, int32_t maxFrameIntervalNs, 201 std::vector<DisplayConfiguration>*); 202 Error getDisplayName(Display display, std::string* outName) override; 203 204 Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, 205 std::vector<Layer>* outLayers, 206 std::vector<uint32_t>* outLayerRequestMasks) override; 207 208 Error getDozeSupport(Display display, bool* outSupport) override; 209 Error hasDisplayIdleTimerCapability(Display display, bool* outSupport) override; 210 Error getHdrCapabilities(Display display, std::vector<Hdr>* outHdrTypes, float* outMaxLuminance, 211 float* outMaxAverageLuminance, float* outMinLuminance) override; 212 Error getOverlaySupport(aidl::android::hardware::graphics::composer3::OverlayProperties* 213 outProperties) override; 214 215 Error getReleaseFences(Display display, std::vector<Layer>* outLayers, 216 std::vector<int>* outReleaseFences) override; 217 218 Error presentDisplay(Display display, int* outPresentFence) override; 219 220 Error setActiveConfig(Display display, Config config) override; 221 222 /* 223 * The composer caches client targets internally. When target is nullptr, 224 * the composer uses slot to look up the client target from its cache. 225 * When target is not nullptr, the cache is updated with the new target. 226 */ 227 Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target, 228 int acquireFence, Dataspace dataspace, 229 const std::vector<IComposerClient::Rect>& damage, 230 float hdrSdrRatio) override; 231 Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) override; 232 Error setColorTransform(Display display, const float* matrix) override; 233 Error setOutputBuffer(Display display, const native_handle_t* buffer, 234 int releaseFence) override; 235 Error setPowerMode(Display display, IComposerClient::PowerMode mode) override; 236 Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override; 237 238 Error setClientTargetSlotCount(Display display) override; 239 240 Error validateDisplay(Display display, nsecs_t expectedPresentTime, int32_t frameIntervalNs, 241 uint32_t* outNumTypes, uint32_t* outNumRequests) override; 242 243 Error presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime, 244 int32_t frameIntervalNs, uint32_t* outNumTypes, 245 uint32_t* outNumRequests, int* outPresentFence, 246 uint32_t* state) override; 247 248 Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override; 249 /* see setClientTarget for the purpose of slot */ 250 Error setLayerBuffer(Display display, Layer layer, uint32_t slot, 251 const sp<GraphicBuffer>& buffer, int acquireFence) override; 252 Error setLayerBufferSlotsToClear(Display display, Layer layer, 253 const std::vector<uint32_t>& slotsToClear, 254 uint32_t activeBufferSlot) override; 255 Error setLayerSurfaceDamage(Display display, Layer layer, 256 const std::vector<IComposerClient::Rect>& damage) override; 257 Error setLayerBlendMode(Display display, Layer layer, IComposerClient::BlendMode mode) override; 258 Error setLayerColor(Display display, Layer layer, 259 const aidl::android::hardware::graphics::composer3::Color& color) override; 260 Error setLayerCompositionType( 261 Display display, Layer layer, 262 aidl::android::hardware::graphics::composer3::Composition type) override; 263 Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) override; 264 Error setLayerDisplayFrame(Display display, Layer layer, 265 const IComposerClient::Rect& frame) override; 266 Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override; 267 Error setLayerSidebandStream(Display display, Layer layer, 268 const native_handle_t* stream) override; 269 Error setLayerSourceCrop(Display display, Layer layer, 270 const IComposerClient::FRect& crop) override; 271 Error setLayerTransform(Display display, Layer layer, Transform transform) override; 272 Error setLayerVisibleRegion(Display display, Layer layer, 273 const std::vector<IComposerClient::Rect>& visible) override; 274 Error setLayerZOrder(Display display, Layer layer, uint32_t z) override; 275 276 // Composer HAL 2.2 277 Error setLayerPerFrameMetadata( 278 Display display, Layer layer, 279 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) override; 280 std::vector<IComposerClient::PerFrameMetadataKey> getPerFrameMetadataKeys( 281 Display display) override; 282 Error getRenderIntents(Display display, ColorMode colorMode, 283 std::vector<RenderIntent>* outRenderIntents) override; 284 Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) override; 285 286 // Composer HAL 2.3 287 Error getDisplayIdentificationData(Display display, uint8_t* outPort, 288 std::vector<uint8_t>* outData) override; 289 Error setLayerColorTransform(Display display, Layer layer, const float* matrix) override; 290 Error getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat, 291 Dataspace* outDataspace, 292 uint8_t* outComponentMask) override; 293 Error setDisplayContentSamplingEnabled(Display display, bool enabled, uint8_t componentMask, 294 uint64_t maxFrames) override; 295 Error getDisplayedContentSample(Display display, uint64_t maxFrames, uint64_t timestamp, 296 DisplayedFrameStats* outStats) override; 297 Error setLayerPerFrameMetadataBlobs( 298 Display display, Layer layer, 299 const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) override; 300 Error setDisplayBrightness(Display display, float brightness, float brightnessNits, 301 const DisplayBrightnessOptions& options) override; 302 303 // Composer HAL 2.4 304 Error getDisplayCapabilities( 305 Display display, 306 std::vector<aidl::android::hardware::graphics::composer3::DisplayCapability>* 307 outCapabilities) override; 308 V2_4::Error getDisplayConnectionType(Display display, 309 IComposerClient::DisplayConnectionType* outType) override; 310 V2_4::Error getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) override; 311 V2_4::Error setActiveConfigWithConstraints( 312 Display display, Config config, 313 const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints, 314 VsyncPeriodChangeTimeline* outTimeline) override; 315 V2_4::Error setAutoLowLatencyMode(Display displayId, bool on) override; 316 V2_4::Error getSupportedContentTypes( 317 Display displayId, 318 std::vector<IComposerClient::ContentType>* outSupportedContentTypes) override; 319 V2_4::Error setContentType(Display displayId, 320 IComposerClient::ContentType contentType) override; 321 V2_4::Error setLayerGenericMetadata(Display display, Layer layer, const std::string& key, 322 bool mandatory, const std::vector<uint8_t>& value) override; 323 V2_4::Error getLayerGenericMetadataKeys( 324 std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) override; 325 Error getClientTargetProperty( 326 Display display, 327 aidl::android::hardware::graphics::composer3::ClientTargetPropertyWithBrightness* 328 outClientTargetProperty) override; 329 330 // AIDL Composer HAL 331 Error setLayerBrightness(Display display, Layer layer, float brightness) override; 332 Error setLayerBlockingRegion(Display display, Layer layer, 333 const std::vector<IComposerClient::Rect>& blocking) override; 334 Error setBootDisplayConfig(Display displayId, Config) override; 335 Error clearBootDisplayConfig(Display displayId) override; 336 Error getPreferredBootDisplayConfig(Display displayId, Config*) override; 337 Error getDisplayDecorationSupport( 338 Display display, 339 std::optional<aidl::android::hardware::graphics::common::DisplayDecorationSupport>* 340 support) override; 341 Error setIdleTimerEnabled(Display displayId, std::chrono::milliseconds timeout) override; 342 343 Error getPhysicalDisplayOrientation(Display displayId, 344 AidlTransform* outDisplayOrientation) override; 345 void onHotplugConnect(Display) override; 346 void onHotplugDisconnect(Display) override; 347 Error getHdrConversionCapabilities( 348 std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>*) 349 override; 350 Error setHdrConversionStrategy(aidl::android::hardware::graphics::common::HdrConversionStrategy, 351 Hdr*) override; 352 Error setRefreshRateChangedCallbackDebugEnabled(Display, bool) override; 353 Error notifyExpectedPresent(Display, nsecs_t, int32_t) override; 354 355 private: 356 class CommandWriter : public CommandWriterBase { 357 public: CommandWriter(uint32_t initialMaxSize)358 explicit CommandWriter(uint32_t initialMaxSize) : CommandWriterBase(initialMaxSize) {} ~CommandWriter()359 ~CommandWriter() override {} 360 }; 361 362 void registerCallback(const sp<IComposerCallback>& callback); 363 364 // Many public functions above simply write a command into the command 365 // queue to batch the calls. validateDisplay and presentDisplay will call 366 // this function to execute the command queue. 367 Error execute(); 368 369 sp<V2_1::IComposer> mComposer; 370 371 sp<V2_1::IComposerClient> mClient; 372 sp<V2_2::IComposerClient> mClient_2_2; 373 sp<V2_3::IComposerClient> mClient_2_3; 374 sp<IComposerClient> mClient_2_4; 375 376 // Buffer slots for layers are cleared by setting the slot buffer to this buffer. 377 sp<GraphicBuffer> mClearSlotBuffer; 378 379 // 64KiB minus a small space for metadata such as read/write pointers 380 static constexpr size_t kWriterInitialSize = 64 * 1024 / sizeof(uint32_t) - 16; 381 // Max number of buffers that may be cached for a given layer 382 // We obtain this number by: 383 // 1. Tightly coupling this cache to the max size of BufferQueue 384 // 2. Adding an additional slot for the layer caching feature in SurfaceFlinger (see: Planner.h) 385 static const constexpr uint32_t kMaxLayerBufferCount = BufferQueue::NUM_BUFFER_SLOTS + 1; 386 CommandWriter mWriter; 387 CommandReader mReader; 388 }; 389 390 } // namespace android::Hwc2 391