DisplayTest.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2019 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. #include <cmath>
  17. #include <compositionengine/DisplayColorProfileCreationArgs.h>
  18. #include <compositionengine/DisplayCreationArgs.h>
  19. #include <compositionengine/DisplaySurface.h>
  20. #include <compositionengine/RenderSurfaceCreationArgs.h>
  21. #include <compositionengine/impl/Display.h>
  22. #include <compositionengine/mock/CompositionEngine.h>
  23. #include <compositionengine/mock/NativeWindow.h>
  24. #include <compositionengine/mock/RenderSurface.h>
  25. #include <gtest/gtest.h>
  26. #include "MockHWComposer.h"
  27. namespace android::compositionengine {
  28. namespace {
  29. using testing::Return;
  30. using testing::ReturnRef;
  31. using testing::StrictMock;
  32. constexpr DisplayId DEFAULT_DISPLAY_ID = DisplayId{42};
  33. class DisplayTest : public testing::Test {
  34. public:
  35. ~DisplayTest() override = default;
  36. StrictMock<android::mock::HWComposer> mHwComposer;
  37. StrictMock<mock::CompositionEngine> mCompositionEngine;
  38. sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
  39. impl::Display mDisplay{mCompositionEngine,
  40. DisplayCreationArgsBuilder().setDisplayId(DEFAULT_DISPLAY_ID).build()};
  41. };
  42. /* ------------------------------------------------------------------------
  43. * Basic construction
  44. */
  45. TEST_F(DisplayTest, canInstantiateDisplay) {
  46. {
  47. constexpr DisplayId display1 = DisplayId{123u};
  48. auto display =
  49. impl::createDisplay(mCompositionEngine,
  50. DisplayCreationArgsBuilder().setDisplayId(display1).build());
  51. EXPECT_FALSE(display->isSecure());
  52. EXPECT_FALSE(display->isVirtual());
  53. EXPECT_EQ(display1, display->getId());
  54. }
  55. {
  56. constexpr DisplayId display2 = DisplayId{546u};
  57. auto display = impl::createDisplay(mCompositionEngine,
  58. DisplayCreationArgsBuilder()
  59. .setIsSecure(true)
  60. .setDisplayId(display2)
  61. .build());
  62. EXPECT_TRUE(display->isSecure());
  63. EXPECT_FALSE(display->isVirtual());
  64. EXPECT_EQ(display2, display->getId());
  65. }
  66. {
  67. constexpr DisplayId display3 = DisplayId{789u};
  68. auto display = impl::createDisplay(mCompositionEngine,
  69. DisplayCreationArgsBuilder()
  70. .setIsVirtual(true)
  71. .setDisplayId(display3)
  72. .build());
  73. EXPECT_FALSE(display->isSecure());
  74. EXPECT_TRUE(display->isVirtual());
  75. EXPECT_EQ(display3, display->getId());
  76. }
  77. }
  78. /* ------------------------------------------------------------------------
  79. * Display::disconnect()
  80. */
  81. TEST_F(DisplayTest, disconnectDisconnectsDisplay) {
  82. EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
  83. // The first call to disconnect will disconnect the display with the HWC and
  84. // set mHwcId to -1.
  85. EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(1);
  86. mDisplay.disconnect();
  87. EXPECT_FALSE(mDisplay.getId());
  88. // Subsequent calls will do nothing,
  89. EXPECT_CALL(mHwComposer, disconnectDisplay(DEFAULT_DISPLAY_ID)).Times(0);
  90. mDisplay.disconnect();
  91. EXPECT_FALSE(mDisplay.getId());
  92. }
  93. /* ------------------------------------------------------------------------
  94. * Display::setColorTransform()
  95. */
  96. TEST_F(DisplayTest, setColorTransformSetsTransform) {
  97. // Identity matrix sets an identity state value
  98. const mat4 identity;
  99. EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
  100. EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, identity)).Times(1);
  101. mDisplay.setColorTransform(identity);
  102. EXPECT_EQ(HAL_COLOR_TRANSFORM_IDENTITY, mDisplay.getState().colorTransform);
  103. // Non-identity matrix sets a non-identity state value
  104. const mat4 nonIdentity = mat4() * 2;
  105. EXPECT_CALL(mHwComposer, setColorTransform(DEFAULT_DISPLAY_ID, nonIdentity)).Times(1);
  106. mDisplay.setColorTransform(nonIdentity);
  107. EXPECT_EQ(HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX, mDisplay.getState().colorTransform);
  108. }
  109. /* ------------------------------------------------------------------------
  110. * Display::setColorMode()
  111. */
  112. TEST_F(DisplayTest, setColorModeSetsModeUnlessNoChange) {
  113. mock::RenderSurface* renderSurface = new StrictMock<mock::RenderSurface>();
  114. mDisplay.setRenderSurfaceForTest(std::unique_ptr<RenderSurface>(renderSurface));
  115. EXPECT_CALL(mCompositionEngine, getHwComposer()).WillRepeatedly(ReturnRef(mHwComposer));
  116. // These values are expected to be the initial state.
  117. ASSERT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode);
  118. ASSERT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace);
  119. ASSERT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent);
  120. // Otherwise if the values are unchanged, nothing happens
  121. mDisplay.setColorMode(ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN,
  122. ui::RenderIntent::COLORIMETRIC);
  123. EXPECT_EQ(ui::ColorMode::NATIVE, mDisplay.getState().colorMode);
  124. EXPECT_EQ(ui::Dataspace::UNKNOWN, mDisplay.getState().dataspace);
  125. EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, mDisplay.getState().renderIntent);
  126. // Otherwise if the values are different, updates happen
  127. EXPECT_CALL(*renderSurface, setBufferDataspace(ui::Dataspace::DISPLAY_P3)).Times(1);
  128. EXPECT_CALL(mHwComposer,
  129. setActiveColorMode(DEFAULT_DISPLAY_ID, ui::ColorMode::DISPLAY_P3,
  130. ui::RenderIntent::TONE_MAP_COLORIMETRIC))
  131. .Times(1);
  132. mDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
  133. ui::RenderIntent::TONE_MAP_COLORIMETRIC);
  134. EXPECT_EQ(ui::ColorMode::DISPLAY_P3, mDisplay.getState().colorMode);
  135. EXPECT_EQ(ui::Dataspace::DISPLAY_P3, mDisplay.getState().dataspace);
  136. EXPECT_EQ(ui::RenderIntent::TONE_MAP_COLORIMETRIC, mDisplay.getState().renderIntent);
  137. }
  138. TEST_F(DisplayTest, setColorModeDoesNothingForVirtualDisplay) {
  139. impl::Display virtualDisplay{mCompositionEngine,
  140. DisplayCreationArgs{false, true, DEFAULT_DISPLAY_ID}};
  141. virtualDisplay.setColorMode(ui::ColorMode::DISPLAY_P3, ui::Dataspace::DISPLAY_P3,
  142. ui::RenderIntent::TONE_MAP_COLORIMETRIC);
  143. EXPECT_EQ(ui::ColorMode::NATIVE, virtualDisplay.getState().colorMode);
  144. EXPECT_EQ(ui::Dataspace::UNKNOWN, virtualDisplay.getState().dataspace);
  145. EXPECT_EQ(ui::RenderIntent::COLORIMETRIC, virtualDisplay.getState().renderIntent);
  146. }
  147. /* ------------------------------------------------------------------------
  148. * Display::createDisplayColorProfile()
  149. */
  150. TEST_F(DisplayTest, createDisplayColorProfileSetsDisplayColorProfile) {
  151. EXPECT_TRUE(mDisplay.getDisplayColorProfile() == nullptr);
  152. mDisplay.createDisplayColorProfile(
  153. DisplayColorProfileCreationArgs{false, HdrCapabilities(), 0,
  154. DisplayColorProfileCreationArgs::HwcColorModes()});
  155. EXPECT_TRUE(mDisplay.getDisplayColorProfile() != nullptr);
  156. }
  157. /* ------------------------------------------------------------------------
  158. * Display::createRenderSurface()
  159. */
  160. TEST_F(DisplayTest, createRenderSurfaceSetsRenderSurface) {
  161. EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL)).WillRepeatedly(Return(NO_ERROR));
  162. EXPECT_TRUE(mDisplay.getRenderSurface() == nullptr);
  163. mDisplay.createRenderSurface(RenderSurfaceCreationArgs{640, 480, mNativeWindow, nullptr});
  164. EXPECT_TRUE(mDisplay.getRenderSurface() != nullptr);
  165. }
  166. } // namespace
  167. } // namespace android::compositionengine