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