Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 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 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 40 | #include <aidl/android/hardware/graphics/composer3/Composition.h> |
| 41 | |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 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::Hdr; |
| 60 | using types::V1_2::PixelFormat; |
| 61 | |
| 62 | using V2_1::Config; |
| 63 | using V2_1::Display; |
| 64 | using V2_1::Error; |
| 65 | using V2_1::Layer; |
| 66 | using V2_4::CommandReaderBase; |
| 67 | using V2_4::CommandWriterBase; |
| 68 | using V2_4::IComposer; |
| 69 | using V2_4::IComposerCallback; |
| 70 | using V2_4::IComposerClient; |
| 71 | using V2_4::VsyncPeriodChangeTimeline; |
| 72 | using V2_4::VsyncPeriodNanos; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 73 | using PerFrameMetadata = IComposerClient::PerFrameMetadata; |
| 74 | using PerFrameMetadataKey = IComposerClient::PerFrameMetadataKey; |
| 75 | using PerFrameMetadataBlob = IComposerClient::PerFrameMetadataBlob; |
| 76 | |
| 77 | class CommandReader : public CommandReaderBase { |
| 78 | public: |
| 79 | ~CommandReader(); |
| 80 | |
| 81 | // Parse and execute commands from the command queue. The commands are |
| 82 | // actually return values from the server and will be saved in ReturnData. |
| 83 | Error parse(); |
| 84 | |
| 85 | // Get and clear saved errors. |
| 86 | struct CommandError { |
| 87 | uint32_t location; |
| 88 | Error error; |
| 89 | }; |
| 90 | std::vector<CommandError> takeErrors(); |
| 91 | |
| 92 | bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes, |
| 93 | uint32_t* outNumLayerRequestMasks) const; |
| 94 | |
| 95 | // Get and clear saved changed composition types. |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 96 | void takeChangedCompositionTypes( |
| 97 | Display display, std::vector<Layer>* outLayers, |
| 98 | std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes); |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 99 | |
| 100 | // Get and clear saved display requests. |
| 101 | void takeDisplayRequests(Display display, uint32_t* outDisplayRequestMask, |
| 102 | std::vector<Layer>* outLayers, |
| 103 | std::vector<uint32_t>* outLayerRequestMasks); |
| 104 | |
| 105 | // Get and clear saved release fences. |
| 106 | void takeReleaseFences(Display display, std::vector<Layer>* outLayers, |
| 107 | std::vector<int>* outReleaseFences); |
| 108 | |
| 109 | // Get and clear saved present fence. |
| 110 | void takePresentFence(Display display, int* outPresentFence); |
| 111 | |
| 112 | // Get what stage succeeded during PresentOrValidate: Present or Validate |
| 113 | void takePresentOrValidateStage(Display display, uint32_t* state); |
| 114 | |
| 115 | // Get the client target properties requested by hardware composer. |
| 116 | void takeClientTargetProperty(Display display, |
| 117 | IComposerClient::ClientTargetProperty* outClientTargetProperty); |
| 118 | |
| 119 | private: |
| 120 | void resetData(); |
| 121 | |
| 122 | bool parseSelectDisplay(uint16_t length); |
| 123 | bool parseSetError(uint16_t length); |
| 124 | bool parseSetChangedCompositionTypes(uint16_t length); |
| 125 | bool parseSetDisplayRequests(uint16_t length); |
| 126 | bool parseSetPresentFence(uint16_t length); |
| 127 | bool parseSetReleaseFences(uint16_t length); |
| 128 | bool parseSetPresentOrValidateDisplayResult(uint16_t length); |
| 129 | bool parseSetClientTargetProperty(uint16_t length); |
| 130 | |
| 131 | struct ReturnData { |
| 132 | uint32_t displayRequests = 0; |
| 133 | |
| 134 | std::vector<Layer> changedLayers; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 135 | std::vector<aidl::android::hardware::graphics::composer3::Composition> compositionTypes; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 136 | |
| 137 | std::vector<Layer> requestedLayers; |
| 138 | std::vector<uint32_t> requestMasks; |
| 139 | |
| 140 | int presentFence = -1; |
| 141 | |
| 142 | std::vector<Layer> releasedLayers; |
| 143 | std::vector<int> releaseFences; |
| 144 | |
| 145 | uint32_t presentOrValidateState; |
| 146 | |
| 147 | // Composer 2.4 implementation can return a client target property |
| 148 | // structure to indicate the client target properties that hardware |
| 149 | // composer requests. The composer client must change the client target |
| 150 | // properties to match this request. |
| 151 | IComposerClient::ClientTargetProperty clientTargetProperty{PixelFormat::RGBA_8888, |
| 152 | Dataspace::UNKNOWN}; |
| 153 | }; |
| 154 | |
| 155 | std::vector<CommandError> mErrors; |
| 156 | std::unordered_map<Display, ReturnData> mReturnData; |
| 157 | |
| 158 | // When SELECT_DISPLAY is parsed, this is updated to point to the |
| 159 | // display's return data in mReturnData. We use it to avoid repeated |
| 160 | // map lookups. |
| 161 | ReturnData* mCurrentReturnData; |
| 162 | }; |
| 163 | |
| 164 | // Composer is a wrapper to IComposer, a proxy to server-side composer. |
| 165 | class HidlComposer final : public Composer { |
| 166 | public: |
| 167 | explicit HidlComposer(const std::string& serviceName); |
| 168 | ~HidlComposer() override; |
| 169 | |
Ady Abraham | 4d211cf | 2021-12-14 16:19:03 -0800 | [diff] [blame] | 170 | bool isSupported(OptionalFeature) const; |
| 171 | |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 172 | std::vector<IComposer::Capability> getCapabilities() override; |
| 173 | std::string dumpDebugInfo() override; |
| 174 | |
| 175 | void registerCallback(const sp<IComposerCallback>& callback) override; |
| 176 | |
| 177 | // Reset all pending commands in the command buffer. Useful if you want to |
| 178 | // skip a frame but have already queued some commands. |
| 179 | void resetCommands() override; |
| 180 | |
| 181 | // Explicitly flush all pending commands in the command buffer. |
| 182 | Error executeCommands() override; |
| 183 | |
| 184 | uint32_t getMaxVirtualDisplayCount() override; |
| 185 | Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format, |
| 186 | Display* outDisplay) override; |
| 187 | Error destroyVirtualDisplay(Display display) override; |
| 188 | |
| 189 | Error acceptDisplayChanges(Display display) override; |
| 190 | |
| 191 | Error createLayer(Display display, Layer* outLayer) override; |
| 192 | Error destroyLayer(Display display, Layer layer) override; |
| 193 | |
| 194 | Error getActiveConfig(Display display, Config* outConfig) override; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 195 | Error getChangedCompositionTypes( |
| 196 | Display display, std::vector<Layer>* outLayers, |
| 197 | std::vector<aidl::android::hardware::graphics::composer3::Composition>* outTypes) |
| 198 | override; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 199 | Error getColorModes(Display display, std::vector<ColorMode>* outModes) override; |
| 200 | Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute, |
| 201 | int32_t* outValue) override; |
| 202 | Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs); |
| 203 | Error getDisplayName(Display display, std::string* outName) override; |
| 204 | |
| 205 | Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, |
| 206 | std::vector<Layer>* outLayers, |
| 207 | std::vector<uint32_t>* outLayerRequestMasks) override; |
| 208 | |
| 209 | Error getDozeSupport(Display display, bool* outSupport) override; |
| 210 | Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes, float* outMaxLuminance, |
| 211 | float* outMaxAverageLuminance, float* outMinLuminance) override; |
| 212 | |
| 213 | Error getReleaseFences(Display display, std::vector<Layer>* outLayers, |
| 214 | std::vector<int>* outReleaseFences) override; |
| 215 | |
| 216 | Error presentDisplay(Display display, int* outPresentFence) override; |
| 217 | |
| 218 | Error setActiveConfig(Display display, Config config) override; |
| 219 | |
| 220 | /* |
| 221 | * The composer caches client targets internally. When target is nullptr, |
| 222 | * the composer uses slot to look up the client target from its cache. |
| 223 | * When target is not nullptr, the cache is updated with the new target. |
| 224 | */ |
| 225 | Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target, |
| 226 | int acquireFence, Dataspace dataspace, |
| 227 | const std::vector<IComposerClient::Rect>& damage) override; |
| 228 | Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) override; |
Ady Abraham | dc011a9 | 2021-12-21 14:06:44 -0800 | [diff] [blame] | 229 | Error setColorTransform(Display display, const float* matrix) override; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 230 | Error setOutputBuffer(Display display, const native_handle_t* buffer, |
| 231 | int releaseFence) override; |
| 232 | Error setPowerMode(Display display, IComposerClient::PowerMode mode) override; |
| 233 | Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override; |
| 234 | |
| 235 | Error setClientTargetSlotCount(Display display) override; |
| 236 | |
Ady Abraham | 43065bd | 2021-12-10 17:22:15 -0800 | [diff] [blame] | 237 | Error validateDisplay(Display display, nsecs_t expectedPresentTime, uint32_t* outNumTypes, |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 238 | uint32_t* outNumRequests) override; |
| 239 | |
Ady Abraham | 43065bd | 2021-12-10 17:22:15 -0800 | [diff] [blame] | 240 | Error presentOrValidateDisplay(Display display, nsecs_t expectedPresentTime, |
| 241 | uint32_t* outNumTypes, uint32_t* outNumRequests, |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 242 | int* outPresentFence, uint32_t* state) override; |
| 243 | |
| 244 | Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override; |
| 245 | /* see setClientTarget for the purpose of slot */ |
| 246 | Error setLayerBuffer(Display display, Layer layer, uint32_t slot, |
| 247 | const sp<GraphicBuffer>& buffer, int acquireFence) override; |
| 248 | Error setLayerSurfaceDamage(Display display, Layer layer, |
| 249 | const std::vector<IComposerClient::Rect>& damage) override; |
| 250 | Error setLayerBlendMode(Display display, Layer layer, IComposerClient::BlendMode mode) override; |
| 251 | Error setLayerColor(Display display, Layer layer, const IComposerClient::Color& color) override; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 252 | Error setLayerCompositionType( |
| 253 | Display display, Layer layer, |
| 254 | aidl::android::hardware::graphics::composer3::Composition type) override; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 255 | Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) override; |
| 256 | Error setLayerDisplayFrame(Display display, Layer layer, |
| 257 | const IComposerClient::Rect& frame) override; |
| 258 | Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override; |
| 259 | Error setLayerSidebandStream(Display display, Layer layer, |
| 260 | const native_handle_t* stream) override; |
| 261 | Error setLayerSourceCrop(Display display, Layer layer, |
| 262 | const IComposerClient::FRect& crop) override; |
| 263 | Error setLayerTransform(Display display, Layer layer, Transform transform) override; |
| 264 | Error setLayerVisibleRegion(Display display, Layer layer, |
| 265 | const std::vector<IComposerClient::Rect>& visible) override; |
| 266 | Error setLayerZOrder(Display display, Layer layer, uint32_t z) override; |
| 267 | |
| 268 | // Composer HAL 2.2 |
| 269 | Error setLayerPerFrameMetadata( |
| 270 | Display display, Layer layer, |
| 271 | const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) override; |
| 272 | std::vector<IComposerClient::PerFrameMetadataKey> getPerFrameMetadataKeys( |
| 273 | Display display) override; |
| 274 | Error getRenderIntents(Display display, ColorMode colorMode, |
| 275 | std::vector<RenderIntent>* outRenderIntents) override; |
| 276 | Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) override; |
| 277 | |
| 278 | // Composer HAL 2.3 |
| 279 | Error getDisplayIdentificationData(Display display, uint8_t* outPort, |
| 280 | std::vector<uint8_t>* outData) override; |
| 281 | Error setLayerColorTransform(Display display, Layer layer, const float* matrix) override; |
| 282 | Error getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat, |
| 283 | Dataspace* outDataspace, |
| 284 | uint8_t* outComponentMask) override; |
| 285 | Error setDisplayContentSamplingEnabled(Display display, bool enabled, uint8_t componentMask, |
| 286 | uint64_t maxFrames) override; |
| 287 | Error getDisplayedContentSample(Display display, uint64_t maxFrames, uint64_t timestamp, |
| 288 | DisplayedFrameStats* outStats) override; |
| 289 | Error setLayerPerFrameMetadataBlobs( |
| 290 | Display display, Layer layer, |
| 291 | const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) override; |
| 292 | Error setDisplayBrightness(Display display, float brightness) override; |
| 293 | |
| 294 | // Composer HAL 2.4 |
Leon Scroggins III | 5967aec | 2021-12-29 11:14:22 -0500 | [diff] [blame^] | 295 | Error getDisplayCapabilities( |
| 296 | Display display, |
| 297 | std::vector<aidl::android::hardware::graphics::composer3::DisplayCapability>* |
| 298 | outCapabilities) override; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 299 | V2_4::Error getDisplayConnectionType(Display display, |
| 300 | IComposerClient::DisplayConnectionType* outType) override; |
| 301 | V2_4::Error getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) override; |
| 302 | V2_4::Error setActiveConfigWithConstraints( |
| 303 | Display display, Config config, |
| 304 | const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints, |
| 305 | VsyncPeriodChangeTimeline* outTimeline) override; |
| 306 | V2_4::Error setAutoLowLatencyMode(Display displayId, bool on) override; |
| 307 | V2_4::Error getSupportedContentTypes( |
| 308 | Display displayId, |
| 309 | std::vector<IComposerClient::ContentType>* outSupportedContentTypes) override; |
| 310 | V2_4::Error setContentType(Display displayId, |
| 311 | IComposerClient::ContentType contentType) override; |
| 312 | V2_4::Error setLayerGenericMetadata(Display display, Layer layer, const std::string& key, |
| 313 | bool mandatory, const std::vector<uint8_t>& value) override; |
| 314 | V2_4::Error getLayerGenericMetadataKeys( |
| 315 | std::vector<IComposerClient::LayerGenericMetadataKey>* outKeys) override; |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 316 | Error getClientTargetProperty(Display display, |
| 317 | IComposerClient::ClientTargetProperty* outClientTargetProperty, |
| 318 | float* outWhitePointNits) override; |
| 319 | Error setLayerWhitePointNits(Display display, Layer layer, float whitePointNits) override; |
Ady Abraham | 9fc2805 | 2021-10-14 17:21:38 -0700 | [diff] [blame] | 320 | |
| 321 | private: |
| 322 | class CommandWriter : public CommandWriterBase { |
| 323 | public: |
| 324 | explicit CommandWriter(uint32_t initialMaxSize) : CommandWriterBase(initialMaxSize) {} |
| 325 | ~CommandWriter() override {} |
| 326 | }; |
| 327 | |
| 328 | // Many public functions above simply write a command into the command |
| 329 | // queue to batch the calls. validateDisplay and presentDisplay will call |
| 330 | // this function to execute the command queue. |
| 331 | Error execute(); |
| 332 | |
| 333 | sp<V2_1::IComposer> mComposer; |
| 334 | |
| 335 | sp<V2_1::IComposerClient> mClient; |
| 336 | sp<V2_2::IComposerClient> mClient_2_2; |
| 337 | sp<V2_3::IComposerClient> mClient_2_3; |
| 338 | sp<IComposerClient> mClient_2_4; |
| 339 | |
| 340 | // 64KiB minus a small space for metadata such as read/write pointers |
| 341 | static constexpr size_t kWriterInitialSize = 64 * 1024 / sizeof(uint32_t) - 16; |
| 342 | // Max number of buffers that may be cached for a given layer |
| 343 | // We obtain this number by: |
| 344 | // 1. Tightly coupling this cache to the max size of BufferQueue |
| 345 | // 2. Adding an additional slot for the layer caching feature in SurfaceFlinger (see: Planner.h) |
| 346 | static const constexpr uint32_t kMaxLayerBufferCount = BufferQueue::NUM_BUFFER_SLOTS + 1; |
| 347 | CommandWriter mWriter; |
| 348 | CommandReader mReader; |
| 349 | }; |
| 350 | |
| 351 | } // namespace android::Hwc2 |