summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/tests/fakehwc/FakeComposerUtils.cpp
blob: 1cea25a80fa3398930295fb8db661ff9cc44f1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"

#define LOG_NDEBUG 0
#undef LOG_TAG
#define LOG_TAG "FakeHwcUtil"
#include <log/log.h>

#include "FakeComposerUtils.h"
#include "RenderState.h"

#include "SurfaceFlinger.h" // Get the name of the service...

#include <binder/IServiceManager.h>
#include <cutils/properties.h>
#include <hidl/ServiceManagement.h>

#include <iomanip>
#include <thread>

using android::String16;
using android::sp;
using namespace std::chrono_literals;
using namespace sftest;
using std::setw;

namespace sftest {

// clang-format off
inline void printSourceRectAligned(::std::ostream& os, const hwc_frect_t& sourceRect, int align) {
    os << std::fixed << std::setprecision(1) << "("
       << setw(align) << sourceRect.left << setw(0) << ","
       << setw(align) << sourceRect.top << setw(0) << ","
       << setw(align) << sourceRect.right << setw(0) << ","
       << setw(align) << sourceRect.bottom << setw(0) << ")";
}

inline void printDisplayRectAligned(::std::ostream& os, const hwc_rect_t& displayRect, int align) {
    os << "("
       << setw(align) << displayRect.left << setw(0) << ","
       << setw(align) << displayRect.top << setw(0) << ","
       << setw(align) << displayRect.right << setw(0) << ","
       << setw(align) << displayRect.bottom << setw(0) << ")";
}
// clang-format on

inline ::std::ostream& operator<<(::std::ostream& os, const sftest::RenderState& state) {
    printSourceRectAligned(os, state.mSourceCrop, 7);
    os << "->";
    printDisplayRectAligned(os, state.mDisplayFrame, 5);
    return os << " Swaps:" << state.mSwapCount << " Alpha:" << std::setprecision(3)
              << state.mPlaneAlpha << " Xform:" << state.mTransform;
}

// Helper for verifying the parts of the RenderState
template <typename T>
bool valuesMatch(::testing::AssertionResult& message, const T& ref, const T& val,
                 const char* name) {
    if (ref != val) {
        message = message << "Expected " << name << ":" << ref << ", got:" << val << ".";
        return false;
    }
    return true;
}

::testing::AssertionResult rectsAreSame(const RenderState& ref, const RenderState& val) {
    // TODO: Message could start as success and be assigned as failure.
    // Only problem is that utility assumes it to be failure and just adds stuff. Would
    // need still special case the initial failure in the utility?
    // TODO: ... or would it be possible to break this back to gtest primitives?
    ::testing::AssertionResult message = ::testing::AssertionFailure();
    bool passes = true;

    // The work here is mostly about providing good log strings for differences
    passes &= valuesMatch(message, ref.mDisplayFrame, val.mDisplayFrame, "display frame");
    passes &= valuesMatch(message, ref.mPlaneAlpha, val.mPlaneAlpha, "alpha");
    passes &= valuesMatch(message, ref.mSwapCount, val.mSwapCount, "swap count");
    passes &= valuesMatch(message, ref.mSourceCrop, val.mSourceCrop, "source crop");
    // ... add more
    if (passes) {
        return ::testing::AssertionSuccess();
    }
    return message;
}

::testing::AssertionResult framesAreSame(const std::vector<RenderState>& ref,
                                         const std::vector<RenderState>& val) {
    ::testing::AssertionResult message = ::testing::AssertionFailure();
    bool passed = true;
    if (ref.size() != val.size()) {
        message << "Expected " << ref.size() << " rects, got " << val.size() << ".";
        passed = false;
    }
    for (size_t rectIndex = 0; rectIndex < std::min(ref.size(), val.size()); rectIndex++) {
        ::testing::AssertionResult rectResult = rectsAreSame(ref[rectIndex], val[rectIndex]);
        if (rectResult == false) {
            message << "First different rect at " << rectIndex << ": " << rectResult.message();
            passed = false;
            break;
        }
    }

    if (passed) {
        return ::testing::AssertionSuccess();
    } else {
        message << "\nReference:";
        for (auto state = ref.begin(); state != ref.end(); ++state) {
            message << "\n" << *state;
        }
        message << "\nActual:";
        for (auto state = val.begin(); state != val.end(); ++state) {
            message << "\n" << *state;
        }
    }
    return message;
}

void startSurfaceFlinger() {
    ALOGI("Start SurfaceFlinger");
    system("start surfaceflinger");

    sp<android::IServiceManager> sm(android::defaultServiceManager());
    sp<android::IBinder> sf;
    while (sf == nullptr) {
        std::this_thread::sleep_for(10ms);
        sf = sm->checkService(String16(android::SurfaceFlinger::getServiceName()));
    }
    ALOGV("SurfaceFlinger running");
}

void stopSurfaceFlinger() {
    ALOGI("Stop SurfaceFlinger");
    system("stop surfaceflinger");
    sp<android::IServiceManager> sm(android::defaultServiceManager());
    sp<android::IBinder> sf;
    while (sf != nullptr) {
        std::this_thread::sleep_for(10ms);
        sf = sm->checkService(String16(android::SurfaceFlinger::getServiceName()));
    }
    ALOGV("SurfaceFlinger stopped");
}

////////////////////////////////////////////////

void FakeHwcEnvironment::SetUp() {
    ALOGI("Test env setup");
    system("setenforce 0");
    system("stop");
    property_set("debug.sf.nobootanimation", "1");
    {
        char value[PROPERTY_VALUE_MAX];
        property_get("debug.sf.nobootanimation", value, "0");
        LOG_FATAL_IF(atoi(value) != 1, "boot skip not set");
    }
    // TODO: Try registering the mock as the default service instead.
    property_set("debug.sf.hwc_service_name", "mock");

    // This allows tests/SF to register/load a HIDL service not listed in manifest files.
    android::hardware::details::setTrebleTestingOverride(true);
    property_set("debug.sf.treble_testing_override", "true");
}

void FakeHwcEnvironment::TearDown() {
    ALOGI("Test env tear down");
    system("stop");
    // Wait for mock call signaling teardown?
    property_set("debug.sf.nobootanimation", "0");
    property_set("debug.sf.hwc_service_name", "default");
    system("setenforce 1");
    ALOGI("Test env tear down - done");
}

} // namespace sftest

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic pop // ignored "-Wconversion"