Marissa Wall | 4d60005 | 2016-12-15 12:16:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include <array> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <dlfcn.h> |
| 20 | #include <hardware/hardware.h> |
| 21 | |
| 22 | #define HWC2_INCLUDE_STRINGIFICATION |
| 23 | #define HWC2_USE_CPP11 |
| 24 | #include <hardware/hwcomposer2.h> |
| 25 | #undef HWC2_INCLUDE_STRINGIFICATION |
| 26 | #undef HWC2_USE_CPP11 |
| 27 | |
| 28 | class Hwc2Test : public testing::Test { |
| 29 | public: |
| 30 | |
| 31 | virtual void SetUp() |
| 32 | { |
| 33 | hw_module_t const* hwc2Module; |
| 34 | |
| 35 | int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &hwc2Module); |
| 36 | ASSERT_GE(err, 0) << "failed to get hwc hardware module: " |
| 37 | << strerror(-err); |
| 38 | |
| 39 | /* The following method will fail if you have not run |
| 40 | * "adb shell stop" */ |
| 41 | err = hwc2_open(hwc2Module, &mHwc2Device); |
| 42 | ASSERT_GE(err, 0) << "failed to open hwc hardware module: " |
| 43 | << strerror(-err); |
| 44 | } |
| 45 | |
| 46 | virtual void TearDown() |
| 47 | { |
| 48 | if (mHwc2Device) |
| 49 | hwc2_close(mHwc2Device); |
| 50 | } |
| 51 | |
Marissa Wall | a4b0148 | 2017-02-17 20:52:03 -0800 | [diff] [blame^] | 52 | void registerCallback(hwc2_callback_descriptor_t descriptor, |
| 53 | hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer, |
| 54 | hwc2_error_t* outErr = nullptr) |
| 55 | { |
| 56 | auto pfn = reinterpret_cast<HWC2_PFN_REGISTER_CALLBACK>( |
| 57 | getFunction(HWC2_FUNCTION_REGISTER_CALLBACK)); |
| 58 | ASSERT_TRUE(pfn) << "failed to get function"; |
| 59 | |
| 60 | auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, descriptor, |
| 61 | callbackData, pointer)); |
| 62 | if (outErr) { |
| 63 | *outErr = err; |
| 64 | } else { |
| 65 | ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to register callback"; |
| 66 | } |
| 67 | } |
| 68 | |
Marissa Wall | 4d60005 | 2016-12-15 12:16:01 -0800 | [diff] [blame] | 69 | protected: |
| 70 | hwc2_function_pointer_t getFunction(hwc2_function_descriptor_t descriptor) |
| 71 | { |
| 72 | return mHwc2Device->getFunction(mHwc2Device, descriptor); |
| 73 | } |
| 74 | |
| 75 | void getCapabilities(std::vector<hwc2_capability_t>* outCapabilities) |
| 76 | { |
| 77 | uint32_t num = 0; |
| 78 | |
| 79 | mHwc2Device->getCapabilities(mHwc2Device, &num, nullptr); |
| 80 | |
| 81 | outCapabilities->resize(num); |
| 82 | |
| 83 | mHwc2Device->getCapabilities(mHwc2Device, &num, |
| 84 | reinterpret_cast<int32_t*>(outCapabilities->data())); |
| 85 | } |
| 86 | |
| 87 | hwc2_device_t* mHwc2Device = nullptr; |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | static const std::array<hwc2_function_descriptor_t, 42> requiredFunctions = {{ |
| 92 | HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES, |
| 93 | HWC2_FUNCTION_CREATE_LAYER, |
| 94 | HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY, |
| 95 | HWC2_FUNCTION_DESTROY_LAYER, |
| 96 | HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY, |
| 97 | HWC2_FUNCTION_DUMP, |
| 98 | HWC2_FUNCTION_GET_ACTIVE_CONFIG, |
| 99 | HWC2_FUNCTION_GET_CHANGED_COMPOSITION_TYPES, |
| 100 | HWC2_FUNCTION_GET_CLIENT_TARGET_SUPPORT, |
| 101 | HWC2_FUNCTION_GET_COLOR_MODES, |
| 102 | HWC2_FUNCTION_GET_DISPLAY_ATTRIBUTE, |
| 103 | HWC2_FUNCTION_GET_DISPLAY_CONFIGS, |
| 104 | HWC2_FUNCTION_GET_DISPLAY_NAME, |
| 105 | HWC2_FUNCTION_GET_DISPLAY_REQUESTS, |
| 106 | HWC2_FUNCTION_GET_DISPLAY_TYPE, |
| 107 | HWC2_FUNCTION_GET_DOZE_SUPPORT, |
| 108 | HWC2_FUNCTION_GET_HDR_CAPABILITIES, |
| 109 | HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT, |
| 110 | HWC2_FUNCTION_GET_RELEASE_FENCES, |
| 111 | HWC2_FUNCTION_PRESENT_DISPLAY, |
| 112 | HWC2_FUNCTION_REGISTER_CALLBACK, |
| 113 | HWC2_FUNCTION_SET_ACTIVE_CONFIG, |
| 114 | HWC2_FUNCTION_SET_CLIENT_TARGET, |
| 115 | HWC2_FUNCTION_SET_COLOR_MODE, |
| 116 | HWC2_FUNCTION_SET_COLOR_TRANSFORM, |
| 117 | HWC2_FUNCTION_SET_CURSOR_POSITION, |
| 118 | HWC2_FUNCTION_SET_LAYER_BLEND_MODE, |
| 119 | HWC2_FUNCTION_SET_LAYER_BUFFER, |
| 120 | HWC2_FUNCTION_SET_LAYER_COLOR, |
| 121 | HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE, |
| 122 | HWC2_FUNCTION_SET_LAYER_DATASPACE, |
| 123 | HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME, |
| 124 | HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA, |
| 125 | HWC2_FUNCTION_SET_LAYER_SOURCE_CROP, |
| 126 | HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE, |
| 127 | HWC2_FUNCTION_SET_LAYER_TRANSFORM, |
| 128 | HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION, |
| 129 | HWC2_FUNCTION_SET_LAYER_Z_ORDER, |
| 130 | HWC2_FUNCTION_SET_OUTPUT_BUFFER, |
| 131 | HWC2_FUNCTION_SET_POWER_MODE, |
| 132 | HWC2_FUNCTION_SET_VSYNC_ENABLED, |
| 133 | HWC2_FUNCTION_VALIDATE_DISPLAY, |
| 134 | }}; |
| 135 | |
| 136 | /* TESTCASE: Tests that the HWC2 supports all required functions. */ |
| 137 | TEST_F(Hwc2Test, GET_FUNCTION) |
| 138 | { |
| 139 | for (hwc2_function_descriptor_t descriptor : requiredFunctions) { |
| 140 | hwc2_function_pointer_t pfn = getFunction(descriptor); |
| 141 | EXPECT_TRUE(pfn) << "failed to get function " |
| 142 | << getFunctionDescriptorName(descriptor); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* TESTCASE: Tests that the HWC2 fails to retrieve and invalid function. */ |
| 147 | TEST_F(Hwc2Test, GET_FUNCTION_invalid_function) |
| 148 | { |
| 149 | hwc2_function_pointer_t pfn = getFunction(HWC2_FUNCTION_INVALID); |
| 150 | EXPECT_FALSE(pfn) << "failed to get invalid function"; |
| 151 | } |
| 152 | |
| 153 | /* TESTCASE: Tests that the HWC2 does not return an invalid capability. */ |
| 154 | TEST_F(Hwc2Test, GET_CAPABILITIES) |
| 155 | { |
| 156 | std::vector<hwc2_capability_t> capabilities; |
| 157 | |
| 158 | getCapabilities(&capabilities); |
| 159 | |
| 160 | EXPECT_EQ(std::count(capabilities.begin(), capabilities.end(), |
| 161 | HWC2_CAPABILITY_INVALID), 0); |
| 162 | } |
Marissa Wall | a4b0148 | 2017-02-17 20:52:03 -0800 | [diff] [blame^] | 163 | |
| 164 | static const std::array<hwc2_callback_descriptor_t, 3> callbackDescriptors = {{ |
| 165 | HWC2_CALLBACK_HOTPLUG, |
| 166 | HWC2_CALLBACK_REFRESH, |
| 167 | HWC2_CALLBACK_VSYNC, |
| 168 | }}; |
| 169 | |
| 170 | /* TESTCASE: Tests that the HWC2 can successfully register all required |
| 171 | * callback functions. */ |
| 172 | TEST_F(Hwc2Test, REGISTER_CALLBACK) |
| 173 | { |
| 174 | hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>( |
| 175 | const_cast<char*>("data")); |
| 176 | |
| 177 | for (auto descriptor : callbackDescriptors) { |
| 178 | ASSERT_NO_FATAL_FAILURE(registerCallback(descriptor, data, |
| 179 | []() { return; })); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* TESTCASE: Test that the HWC2 fails to register invalid callbacks. */ |
| 184 | TEST_F(Hwc2Test, REGISTER_CALLBACK_bad_parameter) |
| 185 | { |
| 186 | hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>( |
| 187 | const_cast<char*>("data")); |
| 188 | hwc2_error_t err = HWC2_ERROR_NONE; |
| 189 | |
| 190 | ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_INVALID, data, |
| 191 | []() { return; }, &err)); |
| 192 | EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER) << "returned wrong error code"; |
| 193 | } |
| 194 | |
| 195 | /* TESTCASE: Tests that the HWC2 can register a callback with null data. */ |
| 196 | TEST_F(Hwc2Test, REGISTER_CALLBACK_null_data) |
| 197 | { |
| 198 | hwc2_callback_data_t data = nullptr; |
| 199 | |
| 200 | for (auto descriptor : callbackDescriptors) { |
| 201 | ASSERT_NO_FATAL_FAILURE(registerCallback(descriptor, data, |
| 202 | []() { return; })); |
| 203 | } |
| 204 | } |