FakeComposerUtils.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "FakeComposerClient.h"
  18. #include <gui/SurfaceComposerClient.h>
  19. #include <hardware/hwcomposer_defs.h>
  20. #include <log/log.h>
  21. #include <gtest/gtest.h>
  22. // clang-format off
  23. // Note: This needs to reside in the global namespace for the GTest to use it
  24. inline ::std::ostream& operator<<(::std::ostream& os, const hwc_rect_t& rect) {
  25. return os << "(" << rect.left << ","
  26. << rect.top << ","
  27. << rect.right << ","
  28. << rect.bottom << ")";
  29. }
  30. inline ::std::ostream& operator<<(::std::ostream& os, const hwc_frect_t& rect) {
  31. return os << "(" << rect.left << ","
  32. << rect.top << ","
  33. << rect.right << ","
  34. << rect.bottom << ")";
  35. }
  36. // clang-format on
  37. namespace sftest {
  38. class RenderState;
  39. // clang-format off
  40. inline bool operator==(const hwc_rect_t& a, const hwc_rect_t& b) {
  41. return a.top == b.top &&
  42. a.left == b.left &&
  43. a.bottom == b.bottom &&
  44. a.right == b.right;
  45. }
  46. inline bool operator==(const hwc_frect_t& a, const hwc_frect_t& b) {
  47. return a.top == b.top &&
  48. a.left == b.left &&
  49. a.bottom == b.bottom &&
  50. a.right == b.right;
  51. }
  52. // clang-format on
  53. inline bool operator!=(const hwc_rect_t& a, const hwc_rect_t& b) {
  54. return !(a == b);
  55. }
  56. inline bool operator!=(const hwc_frect_t& a, const hwc_frect_t& b) {
  57. return !(a == b);
  58. }
  59. ::testing::AssertionResult rectsAreSame(const RenderState& ref, const RenderState& val);
  60. ::testing::AssertionResult framesAreSame(const std::vector<RenderState>& ref,
  61. const std::vector<RenderState>& val);
  62. void startSurfaceFlinger();
  63. void stopSurfaceFlinger();
  64. class FakeHwcEnvironment : public ::testing::Environment {
  65. public:
  66. virtual ~FakeHwcEnvironment() {}
  67. void SetUp() override;
  68. void TearDown() override;
  69. };
  70. /*
  71. * All surface state changes are supposed to happen inside a global
  72. * transaction. TransactionScope object at the beginning of
  73. * scope automates the process. The resulting scope gives a visual cue
  74. * on the span of the transaction as well.
  75. *
  76. * Closing the transaction is synchronous, i.e., it waits for
  77. * SurfaceFlinger to composite one frame. Now, the FakeComposerClient
  78. * is built to explicitly request vsyncs one at the time. A delayed
  79. * request must be made before closing the transaction or the test
  80. * thread stalls until SurfaceFlinger does an emergency vsync by
  81. * itself. TransactionScope encapsulates this vsync magic.
  82. */
  83. class TransactionScope : public android::SurfaceComposerClient::Transaction {
  84. public:
  85. explicit TransactionScope(FakeComposerClient& composer) : Transaction(), mComposer(composer) {}
  86. ~TransactionScope() {
  87. int frameCount = mComposer.getFrameCount();
  88. mComposer.runVSyncAfter(1ms);
  89. LOG_ALWAYS_FATAL_IF(android::NO_ERROR != apply());
  90. // Make sure that exactly one frame has been rendered.
  91. mComposer.waitUntilFrame(frameCount + 1);
  92. LOG_ALWAYS_FATAL_IF(frameCount + 1 != mComposer.getFrameCount(),
  93. "Unexpected frame advance. Delta: %d",
  94. mComposer.getFrameCount() - frameCount);
  95. }
  96. FakeComposerClient& mComposer;
  97. };
  98. } // namespace sftest