RenderSurfaceTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <cstdarg>
  17. #include <cstdint>
  18. #include <compositionengine/RenderSurfaceCreationArgs.h>
  19. #include <compositionengine/impl/RenderSurface.h>
  20. #include <compositionengine/mock/CompositionEngine.h>
  21. #include <compositionengine/mock/Display.h>
  22. #include <compositionengine/mock/DisplaySurface.h>
  23. #include <compositionengine/mock/NativeWindow.h>
  24. #include <compositionengine/mock/OutputLayer.h>
  25. #include <gtest/gtest.h>
  26. #include <renderengine/mock/RenderEngine.h>
  27. #include "MockHWComposer.h"
  28. namespace android::compositionengine {
  29. namespace {
  30. /* ------------------------------------------------------------------------
  31. * RenderSurfaceTest
  32. */
  33. constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1920;
  34. constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1080;
  35. constexpr std::optional<DisplayId> DEFAULT_DISPLAY_ID = std::make_optional(DisplayId{123u});
  36. const std::string DEFAULT_DISPLAY_NAME = "Mock Display";
  37. using testing::_;
  38. using testing::ByMove;
  39. using testing::DoAll;
  40. using testing::Ref;
  41. using testing::Return;
  42. using testing::ReturnRef;
  43. using testing::SetArgPointee;
  44. using testing::StrictMock;
  45. class RenderSurfaceTest : public testing::Test {
  46. public:
  47. RenderSurfaceTest() {
  48. EXPECT_CALL(mDisplay, getId()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_ID));
  49. EXPECT_CALL(mDisplay, getName()).WillRepeatedly(ReturnRef(DEFAULT_DISPLAY_NAME));
  50. EXPECT_CALL(mCompositionEngine, getHwComposer).WillRepeatedly(ReturnRef(mHwComposer));
  51. EXPECT_CALL(mCompositionEngine, getRenderEngine).WillRepeatedly(ReturnRef(mRenderEngine));
  52. EXPECT_CALL(*mNativeWindow, disconnect(NATIVE_WINDOW_API_EGL))
  53. .WillRepeatedly(Return(NO_ERROR));
  54. }
  55. ~RenderSurfaceTest() override = default;
  56. StrictMock<android::mock::HWComposer> mHwComposer;
  57. StrictMock<renderengine::mock::RenderEngine> mRenderEngine;
  58. StrictMock<mock::CompositionEngine> mCompositionEngine;
  59. StrictMock<mock::Display> mDisplay;
  60. sp<mock::NativeWindow> mNativeWindow = new StrictMock<mock::NativeWindow>();
  61. sp<mock::DisplaySurface> mDisplaySurface = new StrictMock<mock::DisplaySurface>();
  62. impl::RenderSurface mSurface{mCompositionEngine, mDisplay,
  63. RenderSurfaceCreationArgs{DEFAULT_DISPLAY_WIDTH,
  64. DEFAULT_DISPLAY_HEIGHT, mNativeWindow,
  65. mDisplaySurface}};
  66. };
  67. /* ------------------------------------------------------------------------
  68. * Basic construction
  69. */
  70. TEST_F(RenderSurfaceTest, canInstantiate) {
  71. EXPECT_TRUE(mSurface.isValid());
  72. }
  73. /* ------------------------------------------------------------------------
  74. * RenderSurface::initialize()
  75. */
  76. TEST_F(RenderSurfaceTest, initializeConfiguresNativeWindow) {
  77. EXPECT_CALL(*mNativeWindow, connect(NATIVE_WINDOW_API_EGL)).WillOnce(Return(NO_ERROR));
  78. EXPECT_CALL(*mNativeWindow, setBuffersFormat(HAL_PIXEL_FORMAT_RGBA_8888))
  79. .WillOnce(Return(NO_ERROR));
  80. EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
  81. mSurface.initialize();
  82. }
  83. /* ------------------------------------------------------------------------
  84. * RenderSurface::getSize()
  85. */
  86. TEST_F(RenderSurfaceTest, sizeReturnsConstructedSize) {
  87. const ui::Size expected{DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT};
  88. EXPECT_EQ(expected, mSurface.getSize());
  89. }
  90. /* ------------------------------------------------------------------------
  91. * RenderSurface::getClientTargetAcquireFence()
  92. */
  93. TEST_F(RenderSurfaceTest, getClientTargetAcquireFenceForwardsCall) {
  94. sp<Fence> fence = new Fence();
  95. EXPECT_CALL(*mDisplaySurface, getClientTargetAcquireFence()).WillOnce(ReturnRef(fence));
  96. EXPECT_EQ(fence.get(), mSurface.getClientTargetAcquireFence().get());
  97. }
  98. /* ------------------------------------------------------------------------
  99. * RenderSurface::setDisplaySize()
  100. */
  101. TEST_F(RenderSurfaceTest, setDisplaySizeAppliesChange) {
  102. EXPECT_CALL(*mDisplaySurface, resizeBuffers(640, 480)).Times(1);
  103. mSurface.setDisplaySize(ui::Size(640, 480));
  104. }
  105. /* ------------------------------------------------------------------------
  106. * RenderSurface::setBufferDataspace()
  107. */
  108. TEST_F(RenderSurfaceTest, setBufferDataspaceAppliesChange) {
  109. EXPECT_CALL(*mNativeWindow, setBuffersDataSpace(ui::Dataspace::DISPLAY_P3))
  110. .WillOnce(Return(NO_ERROR));
  111. mSurface.setBufferDataspace(ui::Dataspace::DISPLAY_P3);
  112. }
  113. /* ------------------------------------------------------------------------
  114. * RenderSurface::setProtected()
  115. */
  116. TEST_F(RenderSurfaceTest, setProtectedTrueEnablesProtection) {
  117. EXPECT_FALSE(mSurface.isProtected());
  118. EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
  119. .WillOnce(Return(NO_ERROR));
  120. mSurface.setProtected(true);
  121. EXPECT_TRUE(mSurface.isProtected());
  122. }
  123. TEST_F(RenderSurfaceTest, setProtectedFalseDisablesProtection) {
  124. EXPECT_FALSE(mSurface.isProtected());
  125. EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
  126. mSurface.setProtected(false);
  127. EXPECT_FALSE(mSurface.isProtected());
  128. }
  129. TEST_F(RenderSurfaceTest, setProtectedEnableAndDisable) {
  130. EXPECT_FALSE(mSurface.isProtected());
  131. EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
  132. .WillOnce(Return(NO_ERROR));
  133. EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER)).WillOnce(Return(NO_ERROR));
  134. mSurface.setProtected(true);
  135. EXPECT_TRUE(mSurface.isProtected());
  136. mSurface.setProtected(false);
  137. EXPECT_FALSE(mSurface.isProtected());
  138. }
  139. TEST_F(RenderSurfaceTest, setProtectedEnableWithError) {
  140. EXPECT_FALSE(mSurface.isProtected());
  141. EXPECT_CALL(*mNativeWindow, setUsage(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_PROTECTED))
  142. .WillOnce(Return(INVALID_OPERATION));
  143. mSurface.setProtected(true);
  144. EXPECT_FALSE(mSurface.isProtected());
  145. }
  146. /* ------------------------------------------------------------------------
  147. * RenderSurface::beginFrame()
  148. */
  149. TEST_F(RenderSurfaceTest, beginFrameAppliesChange) {
  150. EXPECT_CALL(*mDisplaySurface, beginFrame(true)).WillOnce(Return(NO_ERROR));
  151. EXPECT_EQ(NO_ERROR, mSurface.beginFrame(true));
  152. }
  153. /* ------------------------------------------------------------------------
  154. * RenderSurface::prepareFrame()
  155. */
  156. TEST_F(RenderSurfaceTest, prepareFramePassesOutputLayersToHwc) {
  157. EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
  158. .WillOnce(Return(INVALID_OPERATION));
  159. EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
  160. }
  161. TEST_F(RenderSurfaceTest, prepareFrameTakesEarlyOutOnHwcError) {
  162. EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
  163. .WillOnce(Return(INVALID_OPERATION));
  164. EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
  165. }
  166. TEST_F(RenderSurfaceTest, prepareFrameHandlesMixedComposition) {
  167. EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
  168. .WillOnce(Return(NO_ERROR));
  169. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  170. EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  171. EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_MIXED))
  172. .WillOnce(Return(INVALID_OPERATION));
  173. EXPECT_EQ(INVALID_OPERATION, mSurface.prepareFrame());
  174. }
  175. TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyGlesComposition) {
  176. EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
  177. .WillOnce(Return(NO_ERROR));
  178. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  179. EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  180. EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_GLES))
  181. .WillOnce(Return(NO_ERROR));
  182. EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
  183. }
  184. TEST_F(RenderSurfaceTest, prepareFrameHandlesOnlyHwcComposition) {
  185. EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
  186. .WillOnce(Return(NO_ERROR));
  187. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  188. EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  189. EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
  190. .WillOnce(Return(NO_ERROR));
  191. EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
  192. }
  193. TEST_F(RenderSurfaceTest, prepareFrameHandlesNoComposition) {
  194. EXPECT_CALL(mHwComposer, prepare(*DEFAULT_DISPLAY_ID, Ref(mDisplay)))
  195. .WillOnce(Return(NO_ERROR));
  196. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  197. EXPECT_CALL(mHwComposer, hasDeviceComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  198. EXPECT_CALL(*mDisplaySurface, prepareFrame(DisplaySurface::COMPOSITION_HWC))
  199. .WillOnce(Return(NO_ERROR));
  200. EXPECT_EQ(NO_ERROR, mSurface.prepareFrame());
  201. }
  202. /* ------------------------------------------------------------------------
  203. * RenderSurface::dequeueBuffer()
  204. */
  205. TEST_F(RenderSurfaceTest, dequeueBufferObtainsABuffer) {
  206. sp<GraphicBuffer> buffer = new GraphicBuffer();
  207. EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
  208. .WillOnce(
  209. DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
  210. base::unique_fd fence;
  211. EXPECT_EQ(buffer.get(), mSurface.dequeueBuffer(&fence).get());
  212. EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
  213. }
  214. /* ------------------------------------------------------------------------
  215. * RenderSurface::queueBuffer()
  216. */
  217. TEST_F(RenderSurfaceTest, queueBufferHandlesNoClientComposition) {
  218. sp<GraphicBuffer> buffer = new GraphicBuffer();
  219. mSurface.mutableGraphicBufferForTest() = buffer;
  220. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  221. EXPECT_CALL(mHwComposer, hasFlipClientTargetRequest(DEFAULT_DISPLAY_ID))
  222. .WillOnce(Return(false));
  223. EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
  224. mSurface.queueBuffer(base::unique_fd());
  225. EXPECT_EQ(buffer.get(), mSurface.mutableGraphicBufferForTest().get());
  226. }
  227. TEST_F(RenderSurfaceTest, queueBufferHandlesClientComposition) {
  228. sp<GraphicBuffer> buffer = new GraphicBuffer();
  229. mSurface.mutableGraphicBufferForTest() = buffer;
  230. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  231. EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
  232. .WillOnce(Return(NO_ERROR));
  233. EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
  234. mSurface.queueBuffer(base::unique_fd());
  235. EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
  236. }
  237. TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequest) {
  238. sp<GraphicBuffer> buffer = new GraphicBuffer();
  239. mSurface.mutableGraphicBufferForTest() = buffer;
  240. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  241. EXPECT_CALL(mHwComposer, hasFlipClientTargetRequest(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  242. EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
  243. .WillOnce(Return(NO_ERROR));
  244. EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
  245. mSurface.queueBuffer(base::unique_fd());
  246. EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
  247. }
  248. TEST_F(RenderSurfaceTest, queueBufferHandlesFlipClientTargetRequestWithNoBufferYetDequeued) {
  249. sp<GraphicBuffer> buffer = new GraphicBuffer();
  250. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(false));
  251. EXPECT_CALL(mHwComposer, hasFlipClientTargetRequest(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  252. EXPECT_CALL(*mNativeWindow, dequeueBuffer(_, _))
  253. .WillOnce(
  254. DoAll(SetArgPointee<0>(buffer.get()), SetArgPointee<1>(-1), Return(NO_ERROR)));
  255. EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
  256. .WillOnce(Return(NO_ERROR));
  257. EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
  258. mSurface.queueBuffer(base::unique_fd());
  259. EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
  260. }
  261. TEST_F(RenderSurfaceTest, queueBufferHandlesNativeWindowQueueBufferFailureOnVirtualDisplay) {
  262. sp<GraphicBuffer> buffer = new GraphicBuffer();
  263. mSurface.mutableGraphicBufferForTest() = buffer;
  264. EXPECT_CALL(mHwComposer, hasClientComposition(DEFAULT_DISPLAY_ID)).WillOnce(Return(true));
  265. EXPECT_CALL(*mNativeWindow, queueBuffer(buffer->getNativeBuffer(), -1))
  266. .WillOnce(Return(INVALID_OPERATION));
  267. EXPECT_CALL(mDisplay, isVirtual()).WillOnce(Return(true));
  268. EXPECT_CALL(*mNativeWindow, cancelBuffer(buffer->getNativeBuffer(), -1))
  269. .WillOnce(Return(NO_ERROR));
  270. EXPECT_CALL(*mDisplaySurface, advanceFrame()).Times(1);
  271. mSurface.queueBuffer(base::unique_fd());
  272. EXPECT_EQ(nullptr, mSurface.mutableGraphicBufferForTest().get());
  273. }
  274. /* ------------------------------------------------------------------------
  275. * RenderSurface::onPresentDisplayCompleted()
  276. */
  277. TEST_F(RenderSurfaceTest, onPresentDisplayCompletedForwardsSignal) {
  278. EXPECT_CALL(*mDisplaySurface, onFrameCommitted()).Times(1);
  279. mSurface.onPresentDisplayCompleted();
  280. }
  281. /* ------------------------------------------------------------------------
  282. * RenderSurface::setViewportAndProjection()
  283. */
  284. TEST_F(RenderSurfaceTest, setViewportAndProjectionAppliesChang) {
  285. mSurface.setSizeForTest(ui::Size(100, 200));
  286. EXPECT_CALL(mRenderEngine,
  287. setViewportAndProjection(100, 200, Rect(100, 200), ui::Transform::ROT_0))
  288. .Times(1);
  289. mSurface.setViewportAndProjection();
  290. }
  291. /* ------------------------------------------------------------------------
  292. * RenderSurface::flip()
  293. */
  294. TEST_F(RenderSurfaceTest, flipForwardsSignal) {
  295. mSurface.setPageFlipCountForTest(500);
  296. mSurface.flip();
  297. EXPECT_EQ(501, mSurface.getPageFlipCount());
  298. }
  299. } // namespace
  300. } // namespace android::compositionengine