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 #ifndef ANDROID_SF_COMPOSER_HAL_H 18 #define ANDROID_SF_COMPOSER_HAL_H 19 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <utility> 24 #include <vector> 25 26 #include <android/frameworks/vr/composer/1.0/IVrComposerClient.h> 27 #include <android/hardware/graphics/common/1.1/types.h> 28 #include <android/hardware/graphics/composer/2.2/IComposer.h> 29 #include <android/hardware/graphics/composer/2.2/IComposerClient.h> 30 #include <composer-command-buffer/2.2/ComposerCommandBuffer.h> 31 #include <gui/HdrMetadata.h> 32 #include <math/mat4.h> 33 #include <ui/GraphicBuffer.h> 34 #include <utils/StrongPointer.h> 35 36 namespace android { 37 38 namespace Hwc2 { 39 40 using frameworks::vr::composer::V1_0::IVrComposerClient; 41 42 namespace types = hardware::graphics::common; 43 44 namespace V2_1 = hardware::graphics::composer::V2_1; 45 namespace V2_2 = hardware::graphics::composer::V2_2; 46 47 using types::V1_0::ColorTransform; 48 using types::V1_0::Hdr; 49 using types::V1_0::Transform; 50 51 using types::V1_1::ColorMode; 52 using types::V1_1::Dataspace; 53 using types::V1_1::PixelFormat; 54 using types::V1_1::RenderIntent; 55 56 using V2_1::Config; 57 using V2_1::Display; 58 using V2_1::Error; 59 using V2_1::IComposerCallback; 60 using V2_1::Layer; 61 62 using V2_2::CommandReaderBase; 63 using V2_2::CommandWriterBase; 64 using V2_2::IComposer; 65 using V2_2::IComposerClient; 66 67 using PerFrameMetadata = IComposerClient::PerFrameMetadata; 68 using PerFrameMetadataKey = IComposerClient::PerFrameMetadataKey; 69 70 class Composer { 71 public: 72 virtual ~Composer() = 0; 73 74 virtual std::vector<IComposer::Capability> getCapabilities() = 0; 75 virtual std::string dumpDebugInfo() = 0; 76 77 virtual void registerCallback(const sp<IComposerCallback>& callback) = 0; 78 79 // Returns true if the connected composer service is running in a remote 80 // process, false otherwise. This will return false if the service is 81 // configured in passthrough mode, for example. 82 virtual bool isRemote() = 0; 83 84 // Reset all pending commands in the command buffer. Useful if you want to 85 // skip a frame but have already queued some commands. 86 virtual void resetCommands() = 0; 87 88 // Explicitly flush all pending commands in the command buffer. 89 virtual Error executeCommands() = 0; 90 91 virtual uint32_t getMaxVirtualDisplayCount() = 0; 92 virtual bool isUsingVrComposer() const = 0; 93 virtual Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format, 94 Display* outDisplay) = 0; 95 virtual Error destroyVirtualDisplay(Display display) = 0; 96 97 virtual Error acceptDisplayChanges(Display display) = 0; 98 99 virtual Error createLayer(Display display, Layer* outLayer) = 0; 100 virtual Error destroyLayer(Display display, Layer layer) = 0; 101 102 virtual Error getActiveConfig(Display display, Config* outConfig) = 0; 103 virtual Error getChangedCompositionTypes( 104 Display display, std::vector<Layer>* outLayers, 105 std::vector<IComposerClient::Composition>* outTypes) = 0; 106 virtual Error getColorModes(Display display, std::vector<ColorMode>* outModes) = 0; 107 virtual Error getDisplayAttribute(Display display, Config config, 108 IComposerClient::Attribute attribute, int32_t* outValue) = 0; 109 virtual Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs) = 0; 110 virtual Error getDisplayName(Display display, std::string* outName) = 0; 111 112 virtual Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, 113 std::vector<Layer>* outLayers, 114 std::vector<uint32_t>* outLayerRequestMasks) = 0; 115 116 virtual Error getDisplayType(Display display, IComposerClient::DisplayType* outType) = 0; 117 virtual Error getDozeSupport(Display display, bool* outSupport) = 0; 118 virtual Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes, 119 float* outMaxLuminance, float* outMaxAverageLuminance, 120 float* outMinLuminance) = 0; 121 122 virtual Error getReleaseFences(Display display, std::vector<Layer>* outLayers, 123 std::vector<int>* outReleaseFences) = 0; 124 125 virtual Error presentDisplay(Display display, int* outPresentFence) = 0; 126 127 virtual Error setActiveConfig(Display display, Config config) = 0; 128 129 /* 130 * The composer caches client targets internally. When target is nullptr, 131 * the composer uses slot to look up the client target from its cache. 132 * When target is not nullptr, the cache is updated with the new target. 133 */ 134 virtual Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target, 135 int acquireFence, Dataspace dataspace, 136 const std::vector<IComposerClient::Rect>& damage) = 0; 137 virtual Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) = 0; 138 virtual Error setColorTransform(Display display, const float* matrix, ColorTransform hint) = 0; 139 virtual Error setOutputBuffer(Display display, const native_handle_t* buffer, 140 int releaseFence) = 0; 141 virtual Error setPowerMode(Display display, IComposerClient::PowerMode mode) = 0; 142 virtual Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) = 0; 143 144 virtual Error setClientTargetSlotCount(Display display) = 0; 145 146 virtual Error validateDisplay(Display display, uint32_t* outNumTypes, 147 uint32_t* outNumRequests) = 0; 148 149 virtual Error presentOrValidateDisplay(Display display, uint32_t* outNumTypes, 150 uint32_t* outNumRequests, int* outPresentFence, 151 uint32_t* state) = 0; 152 153 virtual Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) = 0; 154 /* see setClientTarget for the purpose of slot */ 155 virtual Error setLayerBuffer(Display display, Layer layer, uint32_t slot, 156 const sp<GraphicBuffer>& buffer, int acquireFence) = 0; 157 virtual Error setLayerSurfaceDamage(Display display, Layer layer, 158 const std::vector<IComposerClient::Rect>& damage) = 0; 159 virtual Error setLayerBlendMode(Display display, Layer layer, 160 IComposerClient::BlendMode mode) = 0; 161 virtual Error setLayerColor(Display display, Layer layer, 162 const IComposerClient::Color& color) = 0; 163 virtual Error setLayerCompositionType(Display display, Layer layer, 164 IComposerClient::Composition type) = 0; 165 virtual Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) = 0; 166 virtual Error setLayerDisplayFrame(Display display, Layer layer, 167 const IComposerClient::Rect& frame) = 0; 168 virtual Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) = 0; 169 virtual Error setLayerSidebandStream(Display display, Layer layer, 170 const native_handle_t* stream) = 0; 171 virtual Error setLayerSourceCrop(Display display, Layer layer, 172 const IComposerClient::FRect& crop) = 0; 173 virtual Error setLayerTransform(Display display, Layer layer, Transform transform) = 0; 174 virtual Error setLayerVisibleRegion(Display display, Layer layer, 175 const std::vector<IComposerClient::Rect>& visible) = 0; 176 virtual Error setLayerZOrder(Display display, Layer layer, uint32_t z) = 0; 177 virtual Error setLayerInfo(Display display, Layer layer, uint32_t type, uint32_t appId) = 0; 178 179 // Composer HAL 2.2 180 virtual Error setLayerPerFrameMetadata( 181 Display display, Layer layer, 182 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) = 0; 183 virtual Error getPerFrameMetadataKeys( 184 Display display, std::vector<IComposerClient::PerFrameMetadataKey>* outKeys) = 0; 185 virtual Error getRenderIntents(Display display, ColorMode colorMode, 186 std::vector<RenderIntent>* outRenderIntents) = 0; 187 virtual Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) = 0; 188 }; 189 190 namespace impl { 191 192 class CommandReader : public CommandReaderBase { 193 public: 194 ~CommandReader(); 195 196 // Parse and execute commands from the command queue. The commands are 197 // actually return values from the server and will be saved in ReturnData. 198 Error parse(); 199 200 // Get and clear saved errors. 201 struct CommandError { 202 uint32_t location; 203 Error error; 204 }; 205 std::vector<CommandError> takeErrors(); 206 207 bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes, 208 uint32_t* outNumLayerRequestMasks) const; 209 210 // Get and clear saved changed composition types. 211 void takeChangedCompositionTypes(Display display, 212 std::vector<Layer>* outLayers, 213 std::vector<IComposerClient::Composition>* outTypes); 214 215 // Get and clear saved display requests. 216 void takeDisplayRequests(Display display, 217 uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers, 218 std::vector<uint32_t>* outLayerRequestMasks); 219 220 // Get and clear saved release fences. 221 void takeReleaseFences(Display display, std::vector<Layer>* outLayers, 222 std::vector<int>* outReleaseFences); 223 224 // Get and clear saved present fence. 225 void takePresentFence(Display display, int* outPresentFence); 226 227 // Get what stage succeeded during PresentOrValidate: Present or Validate 228 void takePresentOrValidateStage(Display display, uint32_t * state); 229 230 private: 231 void resetData(); 232 233 bool parseSelectDisplay(uint16_t length); 234 bool parseSetError(uint16_t length); 235 bool parseSetChangedCompositionTypes(uint16_t length); 236 bool parseSetDisplayRequests(uint16_t length); 237 bool parseSetPresentFence(uint16_t length); 238 bool parseSetReleaseFences(uint16_t length); 239 bool parseSetPresentOrValidateDisplayResult(uint16_t length); 240 241 struct ReturnData { 242 uint32_t displayRequests = 0; 243 244 std::vector<Layer> changedLayers; 245 std::vector<IComposerClient::Composition> compositionTypes; 246 247 std::vector<Layer> requestedLayers; 248 std::vector<uint32_t> requestMasks; 249 250 int presentFence = -1; 251 252 std::vector<Layer> releasedLayers; 253 std::vector<int> releaseFences; 254 255 uint32_t presentOrValidateState; 256 }; 257 258 std::vector<CommandError> mErrors; 259 std::unordered_map<Display, ReturnData> mReturnData; 260 261 // When SELECT_DISPLAY is parsed, this is updated to point to the 262 // display's return data in mReturnData. We use it to avoid repeated 263 // map lookups. 264 ReturnData* mCurrentReturnData; 265 }; 266 267 // Composer is a wrapper to IComposer, a proxy to server-side composer. 268 class Composer final : public Hwc2::Composer { 269 public: 270 Composer(const std::string& serviceName); 271 ~Composer() override; 272 273 std::vector<IComposer::Capability> getCapabilities() override; 274 std::string dumpDebugInfo() override; 275 276 void registerCallback(const sp<IComposerCallback>& callback) override; 277 278 // Returns true if the connected composer service is running in a remote 279 // process, false otherwise. This will return false if the service is 280 // configured in passthrough mode, for example. 281 bool isRemote() override; 282 283 // Reset all pending commands in the command buffer. Useful if you want to 284 // skip a frame but have already queued some commands. 285 void resetCommands() override; 286 287 // Explicitly flush all pending commands in the command buffer. 288 Error executeCommands() override; 289 290 uint32_t getMaxVirtualDisplayCount() override; isUsingVrComposer()291 bool isUsingVrComposer() const override { return mIsUsingVrComposer; } 292 Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format, 293 Display* outDisplay) override; 294 Error destroyVirtualDisplay(Display display) override; 295 296 Error acceptDisplayChanges(Display display) override; 297 298 Error createLayer(Display display, Layer* outLayer) override; 299 Error destroyLayer(Display display, Layer layer) override; 300 301 Error getActiveConfig(Display display, Config* outConfig) override; 302 Error getChangedCompositionTypes(Display display, std::vector<Layer>* outLayers, 303 std::vector<IComposerClient::Composition>* outTypes) override; 304 Error getColorModes(Display display, std::vector<ColorMode>* outModes) override; 305 Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute, 306 int32_t* outValue) override; 307 Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs); 308 Error getDisplayName(Display display, std::string* outName) override; 309 310 Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask, 311 std::vector<Layer>* outLayers, 312 std::vector<uint32_t>* outLayerRequestMasks) override; 313 314 Error getDisplayType(Display display, IComposerClient::DisplayType* outType) override; 315 Error getDozeSupport(Display display, bool* outSupport) override; 316 Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes, float* outMaxLuminance, 317 float* outMaxAverageLuminance, float* outMinLuminance) override; 318 319 Error getReleaseFences(Display display, std::vector<Layer>* outLayers, 320 std::vector<int>* outReleaseFences) override; 321 322 Error presentDisplay(Display display, int* outPresentFence) override; 323 324 Error setActiveConfig(Display display, Config config) override; 325 326 /* 327 * The composer caches client targets internally. When target is nullptr, 328 * the composer uses slot to look up the client target from its cache. 329 * When target is not nullptr, the cache is updated with the new target. 330 */ 331 Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target, 332 int acquireFence, Dataspace dataspace, 333 const std::vector<IComposerClient::Rect>& damage) override; 334 Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) override; 335 Error setColorTransform(Display display, const float* matrix, ColorTransform hint) override; 336 Error setOutputBuffer(Display display, const native_handle_t* buffer, 337 int releaseFence) override; 338 Error setPowerMode(Display display, IComposerClient::PowerMode mode) override; 339 Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override; 340 341 Error setClientTargetSlotCount(Display display) override; 342 343 Error validateDisplay(Display display, uint32_t* outNumTypes, 344 uint32_t* outNumRequests) override; 345 346 Error presentOrValidateDisplay(Display display, uint32_t* outNumTypes, uint32_t* outNumRequests, 347 int* outPresentFence, uint32_t* state) override; 348 349 Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override; 350 /* see setClientTarget for the purpose of slot */ 351 Error setLayerBuffer(Display display, Layer layer, uint32_t slot, 352 const sp<GraphicBuffer>& buffer, int acquireFence) override; 353 Error setLayerSurfaceDamage(Display display, Layer layer, 354 const std::vector<IComposerClient::Rect>& damage) override; 355 Error setLayerBlendMode(Display display, Layer layer, IComposerClient::BlendMode mode) override; 356 Error setLayerColor(Display display, Layer layer, const IComposerClient::Color& color) override; 357 Error setLayerCompositionType(Display display, Layer layer, 358 IComposerClient::Composition type) override; 359 Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) override; 360 Error setLayerDisplayFrame(Display display, Layer layer, 361 const IComposerClient::Rect& frame) override; 362 Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override; 363 Error setLayerSidebandStream(Display display, Layer layer, 364 const native_handle_t* stream) override; 365 Error setLayerSourceCrop(Display display, Layer layer, 366 const IComposerClient::FRect& crop) override; 367 Error setLayerTransform(Display display, Layer layer, Transform transform) override; 368 Error setLayerVisibleRegion(Display display, Layer layer, 369 const std::vector<IComposerClient::Rect>& visible) override; 370 Error setLayerZOrder(Display display, Layer layer, uint32_t z) override; 371 Error setLayerInfo(Display display, Layer layer, uint32_t type, uint32_t appId) override; 372 373 // Composer HAL 2.2 374 Error setLayerPerFrameMetadata( 375 Display display, Layer layer, 376 const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) override; 377 Error getPerFrameMetadataKeys( 378 Display display, std::vector<IComposerClient::PerFrameMetadataKey>* outKeys) override; 379 Error getRenderIntents(Display display, ColorMode colorMode, 380 std::vector<RenderIntent>* outRenderIntents) override; 381 Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) override; 382 383 private: 384 class CommandWriter : public CommandWriterBase { 385 public: 386 CommandWriter(uint32_t initialMaxSize); 387 ~CommandWriter() override; 388 389 void setLayerInfo(uint32_t type, uint32_t appId); 390 void setClientTargetMetadata( 391 const IVrComposerClient::BufferMetadata& metadata); 392 void setLayerBufferMetadata( 393 const IVrComposerClient::BufferMetadata& metadata); 394 395 private: 396 void writeBufferMetadata( 397 const IVrComposerClient::BufferMetadata& metadata); 398 }; 399 400 // Many public functions above simply write a command into the command 401 // queue to batch the calls. validateDisplay and presentDisplay will call 402 // this function to execute the command queue. 403 Error execute(); 404 405 sp<V2_1::IComposer> mComposer; 406 407 sp<V2_1::IComposerClient> mClient; 408 sp<IComposerClient> mClient_2_2; 409 410 // 64KiB minus a small space for metadata such as read/write pointers 411 static constexpr size_t kWriterInitialSize = 412 64 * 1024 / sizeof(uint32_t) - 16; 413 CommandWriter mWriter; 414 CommandReader mReader; 415 416 // When true, the we attach to the vr_hwcomposer service instead of the 417 // hwcomposer. This allows us to redirect surfaces to 3d surfaces in vr. 418 const bool mIsUsingVrComposer; 419 }; 420 421 } // namespace impl 422 423 } // namespace Hwc2 424 425 } // namespace android 426 427 #endif // ANDROID_SF_COMPOSER_HAL_H 428