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