FakeComposerClient.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2017 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. #pragma once
  17. #define HWC2_USE_CPP11
  18. #define HWC2_INCLUDE_STRINGIFICATION
  19. #include <composer-hal/2.1/ComposerClient.h>
  20. #undef HWC2_USE_CPP11
  21. #undef HWC2_INCLUDE_STRINGIFICATION
  22. #include "RenderState.h"
  23. // Needed for display type/ID enums
  24. #include <hardware/hwcomposer_defs.h>
  25. #include <utils/Condition.h>
  26. #include <chrono>
  27. using namespace android::hardware::graphics::composer::V2_1;
  28. using namespace android::hardware::graphics::composer::V2_1::hal;
  29. using namespace android::hardware;
  30. using namespace std::chrono_literals;
  31. namespace {
  32. class LayerImpl;
  33. class Frame;
  34. class DelayedEventGenerator;
  35. } // namespace
  36. namespace android {
  37. class SurfaceComposerClient;
  38. } // namespace android
  39. namespace sftest {
  40. // NOTE: The ID's need to be exactly these. VR composer and parts of
  41. // the SurfaceFlinger assume the display IDs to have these values
  42. // despite the enum being documented as a display type.
  43. // TODO: Reference to actual documentation
  44. constexpr Display PRIMARY_DISPLAY = static_cast<Display>(HWC_DISPLAY_PRIMARY);
  45. constexpr Display EXTERNAL_DISPLAY = static_cast<Display>(HWC_DISPLAY_EXTERNAL);
  46. class FakeComposerClient : public ComposerHal {
  47. public:
  48. FakeComposerClient();
  49. virtual ~FakeComposerClient();
  50. bool hasCapability(hwc2_capability_t capability) override;
  51. std::string dumpDebugInfo() override;
  52. void registerEventCallback(EventCallback* callback) override;
  53. void unregisterEventCallback() override;
  54. uint32_t getMaxVirtualDisplayCount() override;
  55. Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
  56. Display* outDisplay) override;
  57. Error destroyVirtualDisplay(Display display) override;
  58. Error createLayer(Display display, Layer* outLayer) override;
  59. Error destroyLayer(Display display, Layer layer) override;
  60. Error getActiveConfig(Display display, Config* outConfig) override;
  61. Error getClientTargetSupport(Display display, uint32_t width, uint32_t height,
  62. PixelFormat format, Dataspace dataspace) override;
  63. Error getColorModes(Display display, hidl_vec<ColorMode>* outModes) override;
  64. Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute,
  65. int32_t* outValue) override;
  66. Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override;
  67. Error getDisplayName(Display display, hidl_string* outName) override;
  68. Error getDisplayType(Display display, IComposerClient::DisplayType* outType) override;
  69. Error getDozeSupport(Display display, bool* outSupport) override;
  70. Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes, float* outMaxLuminance,
  71. float* outMaxAverageLuminance, float* outMinLuminance) override;
  72. Error setActiveConfig(Display display, Config config) override;
  73. Error setColorMode(Display display, ColorMode mode) override;
  74. Error setPowerMode(Display display, IComposerClient::PowerMode mode) override;
  75. Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
  76. Error setColorTransform(Display display, const float* matrix, int32_t hint) override;
  77. Error setClientTarget(Display display, buffer_handle_t target, int32_t acquireFence,
  78. int32_t dataspace, const std::vector<hwc_rect_t>& damage) override;
  79. Error setOutputBuffer(Display display, buffer_handle_t buffer, int32_t releaseFence) override;
  80. Error validateDisplay(Display display, std::vector<Layer>* outChangedLayers,
  81. std::vector<IComposerClient::Composition>* outCompositionTypes,
  82. uint32_t* outDisplayRequestMask, std::vector<Layer>* outRequestedLayers,
  83. std::vector<uint32_t>* outRequestMasks) override;
  84. Error acceptDisplayChanges(Display display) override;
  85. Error presentDisplay(Display display, int32_t* outPresentFence, std::vector<Layer>* outLayers,
  86. std::vector<int32_t>* outReleaseFences) override;
  87. Error setLayerCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override;
  88. Error setLayerBuffer(Display display, Layer layer, buffer_handle_t buffer,
  89. int32_t acquireFence) override;
  90. Error setLayerSurfaceDamage(Display display, Layer layer,
  91. const std::vector<hwc_rect_t>& damage) override;
  92. Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override;
  93. Error setLayerColor(Display display, Layer layer, IComposerClient::Color color) override;
  94. Error setLayerCompositionType(Display display, Layer layer, int32_t type) override;
  95. Error setLayerDataspace(Display display, Layer layer, int32_t dataspace) override;
  96. Error setLayerDisplayFrame(Display display, Layer layer, const hwc_rect_t& frame) override;
  97. Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
  98. Error setLayerSidebandStream(Display display, Layer layer, buffer_handle_t stream) override;
  99. Error setLayerSourceCrop(Display display, Layer layer, const hwc_frect_t& crop) override;
  100. Error setLayerTransform(Display display, Layer layer, int32_t transform) override;
  101. Error setLayerVisibleRegion(Display display, Layer layer,
  102. const std::vector<hwc_rect_t>& visible) override;
  103. Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
  104. void setClient(ComposerClient* client);
  105. void requestVSync(uint64_t vsyncTime = 0);
  106. // We don't want tests hanging, so always use a timeout. Remember
  107. // to always check the number of frames with test ASSERT_!
  108. // Wait until next frame is rendered after requesting vsync.
  109. void runVSyncAndWait(std::chrono::nanoseconds maxWait = 100ms);
  110. void runVSyncAfter(std::chrono::nanoseconds wait);
  111. int getFrameCount() const;
  112. // We don't want tests hanging, so always use a timeout. Remember
  113. // to always check the number of frames with test ASSERT_!
  114. void waitUntilFrame(int targetFrame, std::chrono::nanoseconds maxWait = 100ms) const;
  115. std::vector<RenderState> getFrameRects(int frame) const;
  116. std::vector<RenderState> getLatestFrame() const;
  117. void clearFrames();
  118. void onSurfaceFlingerStart();
  119. void onSurfaceFlingerStop();
  120. int getLayerCount() const;
  121. Layer getLayer(size_t index) const;
  122. void hotplugDisplay(Display display, IComposerCallback::Connection state);
  123. void refreshDisplay(Display display);
  124. private:
  125. LayerImpl& getLayerImpl(Layer handle);
  126. EventCallback* mEventCallback;
  127. Config mCurrentConfig;
  128. bool mVsyncEnabled;
  129. std::vector<std::unique_ptr<LayerImpl>> mLayers;
  130. std::vector<std::unique_ptr<Frame>> mFrames;
  131. // Using a pointer to hide the implementation into the CPP file.
  132. std::unique_ptr<DelayedEventGenerator> mDelayedEventGenerator;
  133. android::sp<android::SurfaceComposerClient> mSurfaceComposer; // For VSync injections
  134. mutable android::Mutex mStateMutex;
  135. mutable android::Condition mFramesAvailable;
  136. };
  137. } // namespace sftest