diff options
Diffstat (limited to 'services')
58 files changed, 2541 insertions, 307 deletions
diff --git a/services/automotive/display/AutomotiveDisplayProxyService.cpp b/services/automotive/display/AutomotiveDisplayProxyService.cpp index afa623352b..56c3b7d31f 100644 --- a/services/automotive/display/AutomotiveDisplayProxyService.cpp +++ b/services/automotive/display/AutomotiveDisplayProxyService.cpp @@ -65,7 +65,7 @@ AutomotiveDisplayProxyService::getIGraphicBufferProducer(uint64_t id) { std::swap(displayWidth, displayHeight); } - sp<android::SurfaceComposerClient> surfaceClient = new SurfaceComposerClient(); + sp<android::SurfaceComposerClient> surfaceClient = sp<SurfaceComposerClient>::make(); err = surfaceClient->initCheck(); if (err != NO_ERROR) { ALOGE("SurfaceComposerClient::initCheck error: %#x", err); diff --git a/services/inputflinger/dispatcher/Entry.h b/services/inputflinger/dispatcher/Entry.h index becfb05f67..9cd76c7d11 100644 --- a/services/inputflinger/dispatcher/Entry.h +++ b/services/inputflinger/dispatcher/Entry.h @@ -125,10 +125,17 @@ struct KeyEntry : EventEntry { bool syntheticRepeat; // set to true for synthetic key repeats enum class InterceptKeyResult { + // The interception result is unknown. UNKNOWN, + // The event should be skipped and not sent to the application. SKIP, + // The event should be sent to the application. CONTINUE, + // The event should eventually be sent to the application, after a delay. TRY_AGAIN_LATER, + // The event should not be initially sent to the application, but instead go through + // post-processing to generate a fallback key event and then sent to the application. + FALLBACK, }; // These are special fields that may need to be modified while the event is being dispatched. mutable InterceptKeyResult interceptKeyResult; // set based on the interception result diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp index 9f3b2c1ffe..2908c61182 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.cpp +++ b/services/inputflinger/dispatcher/InputDispatcher.cpp @@ -952,7 +952,7 @@ InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy, new LatencyAggregatorWithHistograms())) : std::move(std::unique_ptr<InputEventTimelineProcessor>( new LatencyAggregator()))), - mLatencyTracker(*mInputEventTimelineProcessor) { + mLatencyTracker(*mInputEventTimelineProcessor, mInputDevices) { mReporter = createInputReporter(); mWindowInfoListener = sp<DispatcherWindowListener>::make(*this); @@ -1961,11 +1961,74 @@ bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<con } } + if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::FALLBACK) { + findAndDispatchFallbackEvent(currentTime, entry, inputTargets); + // Drop the key. + return true; + } + // Dispatch the key. dispatchEventLocked(currentTime, entry, inputTargets); return true; } +void InputDispatcher::findAndDispatchFallbackEvent(nsecs_t currentTime, + std::shared_ptr<const KeyEntry> entry, + std::vector<InputTarget>& inputTargets) { + // Find the fallback associated with the incoming key event and dispatch it. + KeyEvent event = createKeyEvent(*entry); + const int32_t originalKeyCode = entry->keyCode; + + // Fetch the fallback event. + KeyCharacterMap::FallbackAction fallback; + for (const InputDeviceInfo& deviceInfo : mInputDevices) { + if (deviceInfo.getId() == entry->deviceId) { + const KeyCharacterMap* map = deviceInfo.getKeyCharacterMap(); + + LOG_ALWAYS_FATAL_IF(map == nullptr, "No KeyCharacterMap for device %d", + entry->deviceId); + map->getFallbackAction(entry->keyCode, entry->metaState, &fallback); + break; + } + } + + if (fallback.keyCode == AKEYCODE_UNKNOWN) { + // No fallback detected. + return; + } + + std::unique_ptr<KeyEntry> fallbackKeyEntry = + std::make_unique<KeyEntry>(mIdGenerator.nextId(), entry->injectionState, + event.getEventTime(), event.getDeviceId(), event.getSource(), + event.getDisplayId(), entry->policyFlags, entry->action, + event.getFlags() | AKEY_EVENT_FLAG_FALLBACK, + fallback.keyCode, event.getScanCode(), /*metaState=*/0, + event.getRepeatCount(), event.getDownTime()); + + if (mTracer) { + fallbackKeyEntry->traceTracker = + mTracer->traceDerivedEvent(*fallbackKeyEntry, *entry->traceTracker); + } + + for (const InputTarget& inputTarget : inputTargets) { + std::shared_ptr<Connection> connection = inputTarget.connection; + if (!connection->responsive || (connection->status != Connection::Status::NORMAL)) { + return; + } + + connection->inputState.setFallbackKey(originalKeyCode, fallback.keyCode); + if (entry->action == AKEY_EVENT_ACTION_UP) { + connection->inputState.removeFallbackKey(originalKeyCode); + } + + if (mTracer) { + mTracer->dispatchToTargetHint(*fallbackKeyEntry->traceTracker, inputTarget); + } + enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, + std::move(fallbackKeyEntry), inputTarget); + } +} + void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { LOG_IF(INFO, DEBUG_OUTBOUND_EVENT_DETAILS) << prefix << "eventTime=" << entry.eventTime << ", deviceId=" << entry.deviceId @@ -4367,7 +4430,7 @@ void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedA std::scoped_lock _l(mLock); // Reset key repeating in case a keyboard device was added or removed or something. resetKeyRepeatLocked(); - mLatencyTracker.setInputDevices(args.inputDeviceInfos); + mInputDevices = args.inputDeviceInfos; } void InputDispatcher::notifyKey(const NotifyKeyArgs& args) { diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h index b2ab711cc2..2e8f2ce04e 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.h +++ b/services/inputflinger/dispatcher/InputDispatcher.h @@ -919,10 +919,14 @@ private: std::unique_ptr<const KeyEntry> afterKeyEventLockedInterruptable( const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, bool handled) REQUIRES(mLock); + void findAndDispatchFallbackEvent(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, + std::vector<InputTarget>& inputTargets) REQUIRES(mLock); // Statistics gathering. nsecs_t mLastStatisticPushTime = 0; std::unique_ptr<InputEventTimelineProcessor> mInputEventTimelineProcessor GUARDED_BY(mLock); + // Must outlive `mLatencyTracker`. + std::vector<InputDeviceInfo> mInputDevices; LatencyTracker mLatencyTracker GUARDED_BY(mLock); void traceInboundQueueLengthLocked() REQUIRES(mLock); void traceOutboundQueueLength(const Connection& connection); diff --git a/services/inputflinger/dispatcher/LatencyTracker.cpp b/services/inputflinger/dispatcher/LatencyTracker.cpp index 0921e37d03..7c23694fbd 100644 --- a/services/inputflinger/dispatcher/LatencyTracker.cpp +++ b/services/inputflinger/dispatcher/LatencyTracker.cpp @@ -67,8 +67,9 @@ static void eraseByValue(std::multimap<K, V>& map, const V& value) { } // namespace -LatencyTracker::LatencyTracker(InputEventTimelineProcessor& processor) - : mTimelineProcessor(&processor) {} +LatencyTracker::LatencyTracker(InputEventTimelineProcessor& processor, + std::vector<InputDeviceInfo>& inputDevices) + : mTimelineProcessor(&processor), mInputDevices(inputDevices) {} void LatencyTracker::trackListener(const NotifyArgs& args) { if (const NotifyKeyArgs* keyArgs = std::get_if<NotifyKeyArgs>(&args)) { @@ -248,8 +249,4 @@ std::string LatencyTracker::dump(const char* prefix) const { StringPrintf("%s mEventTimes.size() = %zu\n", prefix, mEventTimes.size()); } -void LatencyTracker::setInputDevices(const std::vector<InputDeviceInfo>& inputDevices) { - mInputDevices = inputDevices; -} - } // namespace android::inputdispatcher diff --git a/services/inputflinger/dispatcher/LatencyTracker.h b/services/inputflinger/dispatcher/LatencyTracker.h index 79ea14c4fb..7e2cc79249 100644 --- a/services/inputflinger/dispatcher/LatencyTracker.h +++ b/services/inputflinger/dispatcher/LatencyTracker.h @@ -20,9 +20,11 @@ #include <map> #include <unordered_map> +#include <vector> #include <binder/IBinder.h> #include <input/Input.h> +#include <input/InputDevice.h> #include "InputEventTimeline.h" #include "NotifyArgs.h" @@ -41,8 +43,10 @@ public: /** * Create a LatencyTracker. * param reportingFunction: the function that will be called in order to report full latency. + * param inputDevices: input devices relevant for tracking. */ - LatencyTracker(InputEventTimelineProcessor& processor); + LatencyTracker(InputEventTimelineProcessor& processor, + std::vector<InputDeviceInfo>& inputDevices); /** * Start keeping track of an event identified by the args. This must be called first. * If duplicate events are encountered (events that have the same eventId), none of them will be @@ -60,7 +64,6 @@ public: std::array<nsecs_t, GraphicsTimeline::SIZE> timeline); std::string dump(const char* prefix) const; - void setInputDevices(const std::vector<InputDeviceInfo>& inputDevices); private: /** @@ -81,7 +84,7 @@ private: std::multimap<nsecs_t /*eventTime*/, int32_t /*inputEventId*/> mEventTimes; InputEventTimelineProcessor* mTimelineProcessor; - std::vector<InputDeviceInfo> mInputDevices; + std::vector<InputDeviceInfo>& mInputDevices; void trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime, DeviceId deviceId, const std::set<InputDeviceUsageSource>& sources, int32_t inputEventAction, diff --git a/services/inputflinger/reader/mapper/TouchpadInputMapper.cpp b/services/inputflinger/reader/mapper/TouchpadInputMapper.cpp index c982dab019..63eb357bdb 100644 --- a/services/inputflinger/reader/mapper/TouchpadInputMapper.cpp +++ b/services/inputflinger/reader/mapper/TouchpadInputMapper.cpp @@ -144,28 +144,39 @@ public: // records it if so. void processGesture(const TouchpadInputMapper::MetricsIdentifier& id, const Gesture& gesture) { std::scoped_lock lock(mLock); + Counters& counters = mCounters[id]; switch (gesture.type) { case kGestureTypeFling: if (gesture.details.fling.fling_state == GESTURES_FLING_START) { // Indicates the end of a two-finger scroll gesture. - mCounters[id].twoFingerSwipeGestures++; + counters.twoFingerSwipeGestures++; } break; case kGestureTypeSwipeLift: - mCounters[id].threeFingerSwipeGestures++; + // The Gestures library occasionally outputs two lift gestures in a row, which can + // cause inaccurate metrics reporting. To work around this, deduplicate successive + // lift gestures. + // TODO(b/404529050): fix the Gestures library, and remove this check. + if (counters.lastGestureType != kGestureTypeSwipeLift) { + counters.threeFingerSwipeGestures++; + } break; case kGestureTypeFourFingerSwipeLift: - mCounters[id].fourFingerSwipeGestures++; + // TODO(b/404529050): fix the Gestures library, and remove this check. + if (counters.lastGestureType != kGestureTypeFourFingerSwipeLift) { + counters.fourFingerSwipeGestures++; + } break; case kGestureTypePinch: if (gesture.details.pinch.zoom_state == GESTURES_ZOOM_END) { - mCounters[id].pinchGestures++; + counters.pinchGestures++; } break; default: // We're not interested in any other gestures. break; } + counters.lastGestureType = gesture.type; } private: @@ -214,6 +225,10 @@ private: int32_t threeFingerSwipeGestures = 0; int32_t fourFingerSwipeGestures = 0; int32_t pinchGestures = 0; + + // Records the last type of gesture received for this device, for deduplication purposes. + // TODO(b/404529050): fix the Gestures library and remove this field. + GestureType lastGestureType = kGestureTypeContactInitiated; }; // Metrics are aggregated by device model and version, so if two devices of the same model and diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp index 43b4bd8335..298ba4209a 100644 --- a/services/inputflinger/tests/InputDispatcher_test.cpp +++ b/services/inputflinger/tests/InputDispatcher_test.cpp @@ -37,6 +37,7 @@ #include <input/BlockingQueue.h> #include <input/Input.h> #include <input/InputConsumer.h> +#include <input/KeyCharacterMap.h> #include <input/PrintTools.h> #include <linux/input.h> #include <sys/epoll.h> @@ -139,6 +140,30 @@ static KeyEvent getTestKeyEvent() { return event; } +InputDeviceInfo generateTestDeviceInfo(uint16_t vendorId, uint16_t productId, DeviceId deviceId) { + InputDeviceIdentifier identifier; + identifier.vendor = vendorId; + identifier.product = productId; + auto info = InputDeviceInfo(); + info.initialize(deviceId, /*generation=*/1, /*controllerNumber=*/1, identifier, "Test Device", + /*isExternal=*/false, /*hasMic=*/false, ui::LogicalDisplayId::INVALID); + return info; +} + +std::unique_ptr<KeyCharacterMap> loadKeyCharacterMap(const char* name) { + InputDeviceIdentifier identifier; + identifier.name = name; + std::string path = getInputDeviceConfigurationFilePathByName(identifier.getCanonicalName(), + InputDeviceConfigurationFileType:: + KEY_CHARACTER_MAP); + + if (path.empty()) { + return nullptr; + } + + return *KeyCharacterMap::load(path, KeyCharacterMap::Format::BASE); +} + } // namespace // --- InputDispatcherTest --- @@ -7629,6 +7654,50 @@ TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserAct mFakePolicy->assertUserActivityPoked(); } +TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyFallbackKey) { +#if !defined(__ANDROID__) + GTEST_SKIP() << "b/253299089 Generic files are currently read directly from device."; +#endif + InputDeviceInfo testDevice = generateTestDeviceInfo(/*vendorId=*/0, + /*productId=*/0, /*deviceId=*/1); + std::unique_ptr<KeyCharacterMap> kcm = loadKeyCharacterMap("Generic"); + ASSERT_NE(nullptr, kcm); + + testDevice.setKeyCharacterMap(std::move(kcm)); + mDispatcher->notifyInputDevicesChanged(NotifyInputDevicesChangedArgs(/*id=*/1, {testDevice})); + + std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); + sp<FakeWindowHandle> window = + sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", + ui::LogicalDisplayId::DEFAULT); + + window->setFocusable(true); + mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); + setFocusedWindow(window); + + window->consumeFocusEvent(true); + + mFakePolicy->setInterceptKeyBeforeDispatchingResult( + inputdispatcher::KeyEntry::InterceptKeyResult::FALLBACK); + + // In the Generic KCM fallbacks, Meta + Space => SEARCH. + mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) + .keyCode(AKEYCODE_SPACE) + .metaState(AMETA_META_ON) + .build()); + mDispatcher->waitForIdle(); + + // Should have poked user activity + mFakePolicy->assertUserActivityPoked(); + + // Fallback is generated and sent instead. + std::unique_ptr<KeyEvent> consumedEvent = window->consumeKey(/*handled=*/false); + ASSERT_NE(nullptr, consumedEvent); + ASSERT_THAT(*consumedEvent, + AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_SEARCH), + WithFlags(AKEY_EVENT_FLAG_FALLBACK))); +} + class DisableUserActivityInputDispatcherTest : public InputDispatcherTest, public ::testing::WithParamInterface<bool> {}; diff --git a/services/inputflinger/tests/LatencyTracker_test.cpp b/services/inputflinger/tests/LatencyTracker_test.cpp index d8c5eac3c4..b7ca24bb61 100644 --- a/services/inputflinger/tests/LatencyTracker_test.cpp +++ b/services/inputflinger/tests/LatencyTracker_test.cpp @@ -19,10 +19,13 @@ #include "NotifyArgsBuilders.h" #include "android/input.h" +#include <vector> + #include <android-base/logging.h> #include <android-base/properties.h> #include <binder/Binder.h> #include <gtest/gtest.h> +#include <input/InputDevice.h> #include <input/PrintTools.h> #include <inttypes.h> #include <linux/input.h> @@ -51,11 +54,6 @@ static InputDeviceInfo generateTestDeviceInfo(uint16_t vendorId, uint16_t produc return info; } -void setDefaultInputDeviceInfo(LatencyTracker& tracker) { - InputDeviceInfo deviceInfo = generateTestDeviceInfo(/*vendorId=*/0, /*productId=*/0, DEVICE_ID); - tracker.setInputDevices({deviceInfo}); -} - const auto FIRST_TOUCH_POINTER = PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200); const auto FIRST_MOUSE_POINTER = PointerBuilder(/*id=*/1, ToolType::MOUSE); @@ -120,13 +118,14 @@ protected: std::unique_ptr<LatencyTracker> mTracker; sp<IBinder> connection1; sp<IBinder> connection2; + std::vector<InputDeviceInfo> inputDevices; void SetUp() override { connection1 = sp<BBinder>::make(); connection2 = sp<BBinder>::make(); - mTracker = std::make_unique<LatencyTracker>(*this); - setDefaultInputDeviceInfo(*mTracker); + inputDevices.push_back(generateTestDeviceInfo(/*vendorId=*/0, /*productId=*/0, DEVICE_ID)); + mTracker = std::make_unique<LatencyTracker>(*this, inputDevices); } void TearDown() override {} @@ -140,6 +139,10 @@ protected: */ void assertReceivedTimelines(const std::vector<InputEventTimeline>& timelines); + void updateInputDevices(const std::vector<InputDeviceInfo>& inputDevicesUpdated) { + inputDevices = inputDevicesUpdated; + } + private: void processTimeline(const InputEventTimeline& timeline) override { mReceivedTimelines.push_back(timeline); @@ -448,7 +451,7 @@ TEST_F(LatencyTrackerTest, TrackListenerCheck_DeviceInfoFieldsInputEventTimeline deviceInfo2.addSource(AINPUT_SOURCE_TOUCHSCREEN); deviceInfo2.addSource(AINPUT_SOURCE_STYLUS); - mTracker->setInputDevices({deviceInfo1, deviceInfo2}); + updateInputDevices({deviceInfo1, deviceInfo2}); mTracker->trackListener( MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, inputEventId) diff --git a/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp b/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp index 157a3338da..9c027fa00c 100644 --- a/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp +++ b/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp @@ -17,6 +17,8 @@ #include <fuzzer/FuzzedDataProvider.h> #include <linux/input.h> +#include <vector> + #include "../../InputDeviceMetricsSource.h" #include "../InputEventTimeline.h" #include "NotifyArgsBuilders.h" @@ -58,7 +60,8 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { FuzzedDataProvider fdp(data, size); EmptyProcessor emptyProcessor; - LatencyTracker tracker(emptyProcessor); + std::vector<InputDeviceInfo> emptyDevices; + LatencyTracker tracker(emptyProcessor, emptyDevices); // Make some pre-defined tokens to ensure that some timelines are complete. std::array<sp<IBinder> /*token*/, 10> predefinedTokens; diff --git a/services/powermanager/Android.bp b/services/powermanager/Android.bp index 4f65e77462..78f8f7bd96 100644 --- a/services/powermanager/Android.bp +++ b/services/powermanager/Android.bp @@ -52,6 +52,7 @@ cc_library_shared { ], whole_static_libs: [ + "android.adpf.sessionmanager_aidl-ndk", "android.os.hintmanager_aidl-ndk", ], diff --git a/services/powermanager/PowerHalController.cpp b/services/powermanager/PowerHalController.cpp index 0ba1909a44..a817a7bf2e 100644 --- a/services/powermanager/PowerHalController.cpp +++ b/services/powermanager/PowerHalController.cpp @@ -173,6 +173,21 @@ HalResult<aidl::android::hardware::power::SupportInfo> PowerHalController::getSu return CACHE_SUPPORT(6, processHalResult(handle->getSupportInfo(), "getSupportInfo")); } +HalResult<void> PowerHalController::sendCompositionData( + const std::vector<hal::CompositionData>& data) { + std::shared_ptr<HalWrapper> handle = initHal(); + return CACHE_SUPPORT(6, + processHalResult(handle->sendCompositionData(data), + "sendCompositionData")); +} + +HalResult<void> PowerHalController::sendCompositionUpdate(const hal::CompositionUpdate& update) { + std::shared_ptr<HalWrapper> handle = initHal(); + return CACHE_SUPPORT(6, + processHalResult(handle->sendCompositionUpdate(update), + "sendCompositionUpdate")); +} + } // namespace power } // namespace android diff --git a/services/powermanager/PowerHalWrapper.cpp b/services/powermanager/PowerHalWrapper.cpp index 068c23f94a..9c83bf5af3 100644 --- a/services/powermanager/PowerHalWrapper.cpp +++ b/services/powermanager/PowerHalWrapper.cpp @@ -79,6 +79,16 @@ HalResult<Aidl::SupportInfo> EmptyHalWrapper::getSupportInfo() { return HalResult<Aidl::SupportInfo>::unsupported(); } +HalResult<void> EmptyHalWrapper::sendCompositionData(const std::vector<hal::CompositionData>&) { + ALOGV("Skipped sendCompositionData because %s", getUnsupportedMessage()); + return HalResult<void>::unsupported(); +} + +HalResult<void> EmptyHalWrapper::sendCompositionUpdate(const hal::CompositionUpdate&) { + ALOGV("Skipped sendCompositionUpdate because %s", getUnsupportedMessage()); + return HalResult<void>::unsupported(); +} + const char* EmptyHalWrapper::getUnsupportedMessage() { return "Power HAL is not supported"; } @@ -292,6 +302,14 @@ HalResult<Aidl::SupportInfo> AidlHalWrapper::getSupportInfo() { return HalResult<Aidl::SupportInfo>::fromStatus(result, std::move(support)); } +HalResult<void> AidlHalWrapper::sendCompositionData(const std::vector<hal::CompositionData>& data) { + return HalResult<void>::fromStatus(mHandle->sendCompositionData(data)); +} + +HalResult<void> AidlHalWrapper::sendCompositionUpdate(const hal::CompositionUpdate& update) { + return HalResult<void>::fromStatus(mHandle->sendCompositionUpdate(update)); +} + const char* AidlHalWrapper::getUnsupportedMessage() { return "Power HAL doesn't support it"; } diff --git a/services/powermanager/tests/PowerHalWrapperAidlTest.cpp b/services/powermanager/tests/PowerHalWrapperAidlTest.cpp index 682d1f44dc..1c53496644 100644 --- a/services/powermanager/tests/PowerHalWrapperAidlTest.cpp +++ b/services/powermanager/tests/PowerHalWrapperAidlTest.cpp @@ -26,9 +26,9 @@ #include <utils/Log.h> #include <unistd.h> +#include <memory> #include <thread> - using android::binder::Status; using namespace aidl::android::hardware::power; @@ -347,3 +347,50 @@ TEST_F(PowerHalWrapperAidlTest, TestCreateHintSessionWithConfigUnsupported) { result = mWrapper->createHintSessionWithConfig(tgid, uid, threadIds, durationNanos, tag, &out); ASSERT_TRUE(result.isUnsupported()); } + +TEST_F(PowerHalWrapperAidlTest, TestSendingCompositionData) { + int32_t tgid = 999; + int32_t uid = 1001; + std::vector<hal::CompositionData> dataOut; + dataOut.emplace_back(hal::CompositionData{ + .timestampNanos = 0L, + .scheduledPresentTimestampsNanos = {100}, + .latchTimestampNanos = 50, + .outputIds = {0}, + }); + dataOut.emplace_back(hal::CompositionData{ + .timestampNanos = 200L, + .scheduledPresentTimestampsNanos = {300}, + .latchTimestampNanos = 250, + .outputIds = {0}, + }); + EXPECT_CALL(*mMockHal.get(), sendCompositionData(_)) + .Times(Exactly(1)) + .WillOnce([&](const std::vector<hal::CompositionData>& passedData) { + if (!std::equal(passedData.begin(), passedData.end(), dataOut.begin())) { + ADD_FAILURE() << "Passed composition data not the same"; + } + return ndk::ScopedAStatus::ok(); + }); + + ASSERT_TRUE(mWrapper->sendCompositionData(dataOut).isOk()); +} + +TEST_F(PowerHalWrapperAidlTest, TestSendingCompositionUpdate) { + int32_t tgid = 999; + int32_t uid = 1001; + hal::CompositionUpdate dataOut{ + .timestampNanos = 123, + .deadOutputIds = {1, 2, 3}, + }; + EXPECT_CALL(*mMockHal.get(), sendCompositionUpdate(_)) + .Times(Exactly(1)) + .WillOnce([&](const hal::CompositionUpdate& passedData) { + if (passedData != dataOut) { + ADD_FAILURE() << "Passed composition update data not the same"; + } + return ndk::ScopedAStatus::ok(); + }); + + ASSERT_TRUE(mWrapper->sendCompositionUpdate(dataOut).isOk()); +}
\ No newline at end of file diff --git a/services/surfaceflinger/Display/VirtualDisplaySnapshot.h b/services/surfaceflinger/Display/VirtualDisplaySnapshot.h index c68020ce51..71d9f2e468 100644 --- a/services/surfaceflinger/Display/VirtualDisplaySnapshot.h +++ b/services/surfaceflinger/Display/VirtualDisplaySnapshot.h @@ -35,6 +35,7 @@ public: VirtualDisplayId displayId() const { return mVirtualId; } bool isGpu() const { return mIsGpu; } + const std::string& uniqueId() const { return mUniqueId; } void dump(utils::Dumper& dumper) const { using namespace std::string_view_literals; diff --git a/services/surfaceflinger/FrameTimeline/FrameTimeline.cpp b/services/surfaceflinger/FrameTimeline/FrameTimeline.cpp index 008b0571c3..51d4078987 100644 --- a/services/surfaceflinger/FrameTimeline/FrameTimeline.cpp +++ b/services/surfaceflinger/FrameTimeline/FrameTimeline.cpp @@ -29,7 +29,6 @@ #include <cinttypes> #include <numeric> #include <unordered_set> -#include <vector> #include "../Jank/JankTracker.h" @@ -1005,11 +1004,6 @@ void FrameTimeline::setSfPresent(nsecs_t sfPresentTime, finalizeCurrentDisplayFrame(); } -const std::vector<std::shared_ptr<frametimeline::SurfaceFrame>>& FrameTimeline::getPresentFrames() - const { - return mPresentFrames; -} - void FrameTimeline::onCommitNotComposited() { SFTRACE_CALL(); std::scoped_lock lock(mMutex); @@ -1530,7 +1524,6 @@ void FrameTimeline::flushPendingPresentFences() { mPendingPresentFences.erase(mPendingPresentFences.begin()); } - mPresentFrames.clear(); for (size_t i = 0; i < mPendingPresentFences.size(); i++) { const auto& pendingPresentFence = mPendingPresentFences[i]; nsecs_t signalTime = Fence::SIGNAL_TIME_INVALID; @@ -1544,12 +1537,6 @@ void FrameTimeline::flushPendingPresentFences() { auto& displayFrame = pendingPresentFence.second; displayFrame->onPresent(signalTime, mPreviousActualPresentTime); - // Surface frames have been jank classified and can be provided to caller - // to detect if buffer stuffing is occurring. - for (const auto& frame : displayFrame->getSurfaceFrames()) { - mPresentFrames.push_back(frame); - } - mPreviousPredictionPresentTime = displayFrame->trace(mSurfaceFlingerPid, monoBootOffset, mPreviousPredictionPresentTime, mFilterFramesBeforeTraceStarts); diff --git a/services/surfaceflinger/FrameTimeline/FrameTimeline.h b/services/surfaceflinger/FrameTimeline/FrameTimeline.h index 9fedb57aca..fa83cd8523 100644 --- a/services/surfaceflinger/FrameTimeline/FrameTimeline.h +++ b/services/surfaceflinger/FrameTimeline/FrameTimeline.h @@ -331,11 +331,6 @@ public: virtual void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence, const std::shared_ptr<FenceTime>& gpuFence) = 0; - // Provides surface frames that have already been jank classified in the most recent - // flush of pending present fences. This allows buffer stuffing detection from SF. - virtual const std::vector<std::shared_ptr<frametimeline::SurfaceFrame>>& getPresentFrames() - const = 0; - // Tells FrameTimeline that a frame was committed but not composited. This is used to flush // all the associated surface frames. virtual void onCommitNotComposited() = 0; @@ -513,8 +508,6 @@ public: void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate, Fps renderRate) override; void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence, const std::shared_ptr<FenceTime>& gpuFence = FenceTime::NO_FENCE) override; - const std::vector<std::shared_ptr<frametimeline::SurfaceFrame>>& getPresentFrames() - const override; void onCommitNotComposited() override; void parseArgs(const Vector<String16>& args, std::string& result) override; void setMaxDisplayFrames(uint32_t size) override; @@ -562,9 +555,6 @@ private: // display frame, this is a good starting size for the vector so that we can avoid the // internal vector resizing that happens with push_back. static constexpr uint32_t kNumSurfaceFramesInitial = 10; - // Presented surface frames that have been jank classified and can - // indicate of potential buffer stuffing. - std::vector<std::shared_ptr<frametimeline::SurfaceFrame>> mPresentFrames; }; } // namespace impl diff --git a/services/surfaceflinger/QueuedTransactionState.h b/services/surfaceflinger/QueuedTransactionState.h index 6a17a0d0cb..86683da26c 100644 --- a/services/surfaceflinger/QueuedTransactionState.h +++ b/services/surfaceflinger/QueuedTransactionState.h @@ -25,7 +25,6 @@ #include <common/FlagManager.h> #include <ftl/flags.h> #include <gui/LayerState.h> -#include <gui/TransactionState.h> #include <system/window.h> namespace android { @@ -51,26 +50,33 @@ public: struct QueuedTransactionState { QueuedTransactionState() = default; - QueuedTransactionState(TransactionState&& transactionState, - std::vector<ResolvedComposerState>&& composerStates, - std::vector<uint64_t>&& uncacheBufferIds, int64_t postTime, - int originPid, int originUid) - : frameTimelineInfo(std::move(transactionState.mFrameTimelineInfo)), - states(composerStates), - displays(std::move(transactionState.mDisplayStates)), - flags(transactionState.mFlags), - applyToken(transactionState.mApplyToken), - inputWindowCommands(std::move(transactionState.mInputWindowCommands)), - desiredPresentTime(transactionState.mDesiredPresentTime), - isAutoTimestamp(transactionState.mIsAutoTimestamp), + QueuedTransactionState(const FrameTimelineInfo& frameTimelineInfo, + std::vector<ResolvedComposerState>& composerStates, + const Vector<DisplayState>& displayStates, uint32_t transactionFlags, + const sp<IBinder>& applyToken, + const InputWindowCommands& inputWindowCommands, + int64_t desiredPresentTime, bool isAutoTimestamp, + std::vector<uint64_t> uncacheBufferIds, int64_t postTime, + bool hasListenerCallbacks, + std::vector<ListenerCallbacks> listenerCallbacks, int originPid, + int originUid, uint64_t transactionId, + std::vector<uint64_t> mergedTransactionIds) + : frameTimelineInfo(frameTimelineInfo), + states(std::move(composerStates)), + displays(displayStates), + flags(transactionFlags), + applyToken(applyToken), + inputWindowCommands(inputWindowCommands), + desiredPresentTime(desiredPresentTime), + isAutoTimestamp(isAutoTimestamp), uncacheBufferIds(std::move(uncacheBufferIds)), postTime(postTime), - hasListenerCallbacks(transactionState.mHasListenerCallbacks), - listenerCallbacks(std::move(transactionState.mListenerCallbacks)), + hasListenerCallbacks(hasListenerCallbacks), + listenerCallbacks(listenerCallbacks), originPid(originPid), originUid(originUid), - id(transactionState.getId()), - mergedTransactionIds(std::move(transactionState.mMergedTransactionIds)) {} + id(transactionId), + mergedTransactionIds(std::move(mergedTransactionIds)) {} // Invokes `void(const layer_state_t&)` visitor for matching layers. template <typename Visitor> @@ -129,7 +135,7 @@ struct QueuedTransactionState { FrameTimelineInfo frameTimelineInfo; std::vector<ResolvedComposerState> states; - std::vector<DisplayState> displays; + Vector<DisplayState> displays; uint32_t flags; sp<IBinder> applyToken; InputWindowCommands inputWindowCommands; diff --git a/services/surfaceflinger/Scheduler/EventThread.cpp b/services/surfaceflinger/Scheduler/EventThread.cpp index c37b9653cb..5390295e9a 100644 --- a/services/surfaceflinger/Scheduler/EventThread.cpp +++ b/services/surfaceflinger/Scheduler/EventThread.cpp @@ -512,14 +512,6 @@ void EventThread::onModeRejected(PhysicalDisplayId displayId, DisplayModeId mode mCondition.notify_all(); } -// Merge lists of buffer stuffed Uids -void EventThread::addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) { - std::lock_guard<std::mutex> lock(mMutex); - for (auto& [uid, count] : bufferStuffedUids) { - mBufferStuffedUids.emplace_or_replace(uid, count); - } -} - void EventThread::threadMain(std::unique_lock<std::mutex>& lock) { DisplayEventConsumers consumers; @@ -761,10 +753,6 @@ void EventThread::generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event, const DisplayEventConsumers& consumers) { - // List of Uids that have been sent vsync data with queued buffer count. - // Used to keep track of which Uids can be removed from the map of - // buffer stuffed clients. - ftl::SmallVector<uid_t, 10> uidsPostedQueuedBuffers; for (const auto& consumer : consumers) { DisplayEventReceiver::Event copy = event; if (event.header.type == DisplayEventType::DISPLAY_EVENT_VSYNC) { @@ -774,13 +762,6 @@ void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event, event.vsync.vsyncData.preferredExpectedPresentationTime(), event.vsync.vsyncData.preferredDeadlineTimestamp()); } - auto it = mBufferStuffedUids.find(consumer->mOwnerUid); - if (it != mBufferStuffedUids.end()) { - copy.vsync.vsyncData.numberQueuedBuffers = it->second; - uidsPostedQueuedBuffers.emplace_back(consumer->mOwnerUid); - } else { - copy.vsync.vsyncData.numberQueuedBuffers = 0; - } switch (consumer->postEvent(copy)) { case NO_ERROR: break; @@ -796,12 +777,6 @@ void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event, removeDisplayEventConnectionLocked(consumer); } } - // The clients that have already received the queued buffer count - // can be removed from the buffer stuffed Uid list to avoid - // being sent duplicate messages. - for (auto uid : uidsPostedQueuedBuffers) { - mBufferStuffedUids.erase(uid); - } if (event.header.type == DisplayEventType::DISPLAY_EVENT_VSYNC && FlagManager::getInstance().vrr_config()) { mLastCommittedVsyncTime = diff --git a/services/surfaceflinger/Scheduler/EventThread.h b/services/surfaceflinger/Scheduler/EventThread.h index a91dde7430..612883a88b 100644 --- a/services/surfaceflinger/Scheduler/EventThread.h +++ b/services/surfaceflinger/Scheduler/EventThread.h @@ -56,7 +56,6 @@ using gui::VsyncEventData; // --------------------------------------------------------------------------- using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride; -using BufferStuffingMap = ftl::SmallMap<uid_t, uint32_t, 10>; enum class VSyncRequest { None = -2, @@ -141,10 +140,6 @@ public: virtual void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel, int32_t maxLevel) = 0; - - // An elevated number of queued buffers in the server is detected. This propagates a - // flag to Choreographer indicating that buffer stuffing recovery should begin. - virtual void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) = 0; }; struct IEventThreadCallback { @@ -199,8 +194,6 @@ public: void onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel, int32_t maxLevel) override; - void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) override; - private: friend EventThreadTest; @@ -241,10 +234,6 @@ private: scheduler::VSyncCallbackRegistration mVsyncRegistration GUARDED_BY(mMutex); frametimeline::TokenManager* const mTokenManager; - // All consumers that need to recover from buffer stuffing and the number - // of their queued buffers. - BufferStuffingMap mBufferStuffedUids GUARDED_BY(mMutex); - IEventThreadCallback& mCallback; std::thread mThread; diff --git a/services/surfaceflinger/Scheduler/Scheduler.cpp b/services/surfaceflinger/Scheduler/Scheduler.cpp index 911d4894ce..c9d3b31061 100644 --- a/services/surfaceflinger/Scheduler/Scheduler.cpp +++ b/services/surfaceflinger/Scheduler/Scheduler.cpp @@ -960,11 +960,6 @@ bool Scheduler::updateFrameRateOverridesLocked(GlobalSignals consideredSignals, return mFrameRateOverrideMappings.updateFrameRateOverridesByContent(frameRateOverrides); } -void Scheduler::addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) { - if (!mRenderEventThread) return; - mRenderEventThread->addBufferStuffedUids(std::move(bufferStuffedUids)); -} - void Scheduler::promotePacesetterDisplay(PhysicalDisplayId pacesetterId, PromotionParams params) { std::shared_ptr<VsyncSchedule> pacesetterVsyncSchedule; { diff --git a/services/surfaceflinger/Scheduler/Scheduler.h b/services/surfaceflinger/Scheduler/Scheduler.h index 81389e7362..61469c1b46 100644 --- a/services/surfaceflinger/Scheduler/Scheduler.h +++ b/services/surfaceflinger/Scheduler/Scheduler.h @@ -337,10 +337,6 @@ public: mPacesetterFrameDurationFractionToSkip = frameDurationFraction; } - // Propagates a flag to the EventThread indicating that buffer stuffing - // recovery should begin. - void addBufferStuffedUids(BufferStuffingMap bufferStuffedUids); - void setDebugPresentDelay(TimePoint delay) { mDebugPresentDelay = delay; } private: diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index aa933ee8a7..ce7a720714 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -66,13 +66,11 @@ #include <ftl/concat.h> #include <ftl/fake_guard.h> #include <ftl/future.h> -#include <ftl/small_map.h> #include <ftl/unit.h> #include <gui/AidlUtil.h> #include <gui/BufferQueue.h> #include <gui/DebugEGLImageTracker.h> #include <gui/IProducerListener.h> -#include <gui/JankInfo.h> #include <gui/LayerMetadata.h> #include <gui/LayerState.h> #include <gui/Surface.h> @@ -3315,40 +3313,12 @@ void SurfaceFlinger::onCompositionPresented(PhysicalDisplayId pacesetterId, const TimePoint presentTime = TimePoint::now(); - // The Uids of layer owners that are in buffer stuffing mode, and their elevated - // buffer counts. Messages to start recovery are sent exclusively to these Uids. - BufferStuffingMap bufferStuffedUids; - // Set presentation information before calling Layer::releasePendingBuffer, such that jank // information from previous' frame classification is already available when sending jank info // to clients, so they get jank classification as early as possible. mFrameTimeline->setSfPresent(presentTime.ns(), pacesetterPresentFenceTime, pacesetterGpuCompositionDoneFenceTime); - // Find and register any layers that are in buffer stuffing mode - const auto& presentFrames = mFrameTimeline->getPresentFrames(); - - for (const auto& frame : presentFrames) { - const auto& layer = mLayerLifecycleManager.getLayerFromId(frame->getLayerId()); - if (!layer) continue; - uint32_t numberQueuedBuffers = layer->pendingBuffers ? layer->pendingBuffers->load() : 0; - int32_t jankType = frame->getJankType().value_or(JankType::None); - if (jankType & JankType::BufferStuffing && - layer->flags & layer_state_t::eRecoverableFromBufferStuffing) { - auto [it, wasEmplaced] = - bufferStuffedUids.try_emplace(layer->ownerUid.val(), numberQueuedBuffers); - // Update with maximum number of queued buffers, allows clients drawing - // multiple windows to account for the most severely stuffed window - if (!wasEmplaced && it->second < numberQueuedBuffers) { - it->second = numberQueuedBuffers; - } - } - } - - if (!bufferStuffedUids.empty()) { - mScheduler->addBufferStuffedUids(std::move(bufferStuffedUids)); - } - // We use the CompositionEngine::getLastFrameRefreshTimestamp() which might // be sampled a little later than when we started doing work for this frame, // but that should be okay since CompositorTiming has snapping logic. @@ -5028,7 +4998,13 @@ bool SurfaceFlinger::shouldLatchUnsignaled(const layer_state_t& state, size_t nu return true; } -status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState) { +status_t SurfaceFlinger::setTransactionState( + const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states, + Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, + InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, bool isAutoTimestamp, + const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks, + const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId, + const std::vector<uint64_t>& mergedTransactionIds) { SFTRACE_CALL(); IPCThreadState* ipc = IPCThreadState::self(); @@ -5036,7 +5012,7 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState const int originUid = ipc->getCallingUid(); uint32_t permissions = LayerStatePermissions::getTransactionPermissions(originPid, originUid); ftl::Flags<adpf::Workload> queuedWorkload; - for (auto& composerState : transactionState.mComposerStates) { + for (auto& composerState : states) { composerState.state.sanitize(permissions); if (composerState.state.what & layer_state_t::COMPOSITION_EFFECTS) { queuedWorkload |= adpf::Workload::EFFECTS; @@ -5046,27 +5022,27 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState } } - for (DisplayState& display : transactionState.mDisplayStates) { + for (DisplayState& display : displays) { display.sanitize(permissions); } - if (!transactionState.mInputWindowCommands.empty() && + if (!inputWindowCommands.empty() && (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) == 0) { ALOGE("Only privileged callers are allowed to send input commands."); - transactionState.mInputWindowCommands.clear(); + inputWindowCommands.clear(); } - if (transactionState.mFlags & (eEarlyWakeupStart | eEarlyWakeupEnd)) { + if (flags & (eEarlyWakeupStart | eEarlyWakeupEnd)) { const bool hasPermission = (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) || callingThreadHasPermission(sWakeupSurfaceFlinger); if (!hasPermission) { ALOGE("Caller needs permission android.permission.WAKEUP_SURFACE_FLINGER to use " "eEarlyWakeup[Start|End] flags"); - transactionState.mFlags &= ~(eEarlyWakeupStart | eEarlyWakeupEnd); + flags &= ~(eEarlyWakeupStart | eEarlyWakeupEnd); } } - if (transactionState.mFlags & eEarlyWakeupStart) { + if (flags & eEarlyWakeupStart) { queuedWorkload |= adpf::Workload::WAKEUP; } mPowerAdvisor->setQueuedWorkload(queuedWorkload); @@ -5074,8 +5050,8 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState const int64_t postTime = systemTime(); std::vector<uint64_t> uncacheBufferIds; - uncacheBufferIds.reserve(transactionState.mUncacheBuffers.size()); - for (const auto& uncacheBuffer : transactionState.mUncacheBuffers) { + uncacheBufferIds.reserve(uncacheBuffers.size()); + for (const auto& uncacheBuffer : uncacheBuffers) { sp<GraphicBuffer> buffer = ClientCache::getInstance().erase(uncacheBuffer); if (buffer != nullptr) { uncacheBufferIds.push_back(buffer->getId()); @@ -5083,8 +5059,8 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState } std::vector<ResolvedComposerState> resolvedStates; - resolvedStates.reserve(transactionState.mComposerStates.size()); - for (auto& state : transactionState.mComposerStates) { + resolvedStates.reserve(states.size()); + for (auto& state : states) { resolvedStates.emplace_back(std::move(state)); auto& resolvedState = resolvedStates.back(); resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface); @@ -5095,7 +5071,7 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState layer->getDebugName() : std::to_string(resolvedState.state.layerId); resolvedState.externalTexture = getExternalTextureFromBufferData(*resolvedState.state.bufferData, - layerName.c_str(), transactionState.getId()); + layerName.c_str(), transactionId); if (resolvedState.externalTexture) { resolvedState.state.bufferData->buffer = resolvedState.externalTexture->getBuffer(); if (FlagManager::getInstance().monitor_buffer_fences()) { @@ -5123,12 +5099,22 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState } } - QueuedTransactionState state{std::move(transactionState), - std::move(resolvedStates), + QueuedTransactionState state{frameTimelineInfo, + resolvedStates, + displays, + flags, + applyToken, + std::move(inputWindowCommands), + desiredPresentTime, + isAutoTimestamp, std::move(uncacheBufferIds), postTime, + hasListenerCallbacks, + listenerCallbacks, originPid, - originUid}; + originUid, + transactionId, + mergedTransactionIds}; state.workloadHint = queuedWorkload; if (mTransactionTracing) { @@ -5151,16 +5137,16 @@ status_t SurfaceFlinger::setTransactionState(TransactionState&& transactionState for (const auto& [displayId, data] : mNotifyExpectedPresentMap) { if (data.hintStatus.load() == NotifyExpectedPresentHintStatus::ScheduleOnTx) { - scheduleNotifyExpectedPresentHint(displayId, VsyncId{state.frameTimelineInfo.vsyncId}); + scheduleNotifyExpectedPresentHint(displayId, VsyncId{frameTimelineInfo.vsyncId}); } } - setTransactionFlags(eTransactionFlushNeeded, schedule, state.applyToken, frameHint); + setTransactionFlags(eTransactionFlushNeeded, schedule, applyToken, frameHint); return NO_ERROR; } bool SurfaceFlinger::applyTransactionState( const FrameTimelineInfo& frameTimelineInfo, std::vector<ResolvedComposerState>& states, - std::span<DisplayState> displays, uint32_t flags, + Vector<DisplayState>& displays, uint32_t flags, const InputWindowCommands& inputWindowCommands, const int64_t desiredPresentTime, bool isAutoTimestamp, const std::vector<uint64_t>& uncacheBufferIds, const int64_t postTime, bool hasListenerCallbacks, const std::vector<ListenerCallbacks>& listenerCallbacks, @@ -5650,8 +5636,7 @@ void SurfaceFlinger::initializeDisplays() { auto layerStack = ui::DEFAULT_LAYER_STACK.id; for (const auto& [id, display] : FTL_FAKE_GUARD(mStateLock, mPhysicalDisplays)) { - state.displays.emplace_back( - DisplayState(display.token(), ui::LayerStack::fromValue(layerStack++))); + state.displays.push(DisplayState(display.token(), ui::LayerStack::fromValue(layerStack++))); } std::vector<QueuedTransactionState> transactions; @@ -6123,9 +6108,8 @@ void SurfaceFlinger::dumpDisplays(std::string& result) const { display->dump(dumper); std::lock_guard lock(mVirtualDisplaysMutex); - const auto virtualSnapshotIt = mVirtualDisplays.find(virtualId); - if (virtualSnapshotIt != mVirtualDisplays.end()) { - virtualSnapshotIt->second.dump(dumper); + if (const auto snapshotOpt = mVirtualDisplays.get(virtualId)) { + snapshotOpt->get().dump(dumper); } } } @@ -6137,6 +6121,7 @@ void SurfaceFlinger::dumpDisplayIdentificationData(std::string& result) const { if (!displayId) { continue; } + const auto hwcDisplayId = getHwComposer().fromPhysicalDisplayId(*displayId); if (!hwcDisplayId) { continue; @@ -6145,6 +6130,7 @@ void SurfaceFlinger::dumpDisplayIdentificationData(std::string& result) const { StringAppendF(&result, "Display %s (HWC display %" PRIu64 "): ", to_string(*displayId).c_str(), *hwcDisplayId); + uint8_t port; DisplayIdentificationData data; if (!getHwComposer().getDisplayIdentificationData(*hwcDisplayId, &port, &data)) { @@ -6172,6 +6158,19 @@ void SurfaceFlinger::dumpDisplayIdentificationData(std::string& result) const { result.append(edid->displayName.data(), edid->displayName.length()); result.append("\"\n"); } + + for (const auto& [token, display] : mDisplays) { + const auto virtualDisplayId = asVirtualDisplayId(display->getDisplayIdVariant()); + if (virtualDisplayId) { + StringAppendF(&result, "Display %s (Virtual display): displayName=\"%s\"", + to_string(*virtualDisplayId).c_str(), display->getDisplayName().c_str()); + std::lock_guard lock(mVirtualDisplaysMutex); + if (const auto snapshotOpt = mVirtualDisplays.get(*virtualDisplayId)) { + StringAppendF(&result, " uniqueId=\"%s\"", snapshotOpt->get().uniqueId().c_str()); + } + result.append("\n"); + } + } } void SurfaceFlinger::dumpRawDisplayIdentificationData(const DumpArgs& args, diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index f61214cc65..c472c4c6d4 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -545,7 +545,13 @@ private: } sp<IBinder> getPhysicalDisplayToken(PhysicalDisplayId displayId) const; - status_t setTransactionState(TransactionState&&) override; + status_t setTransactionState( + const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& state, + Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, + InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, + bool isAutoTimestamp, const std::vector<client_cache_t>& uncacheBuffers, + bool hasListenerCallbacks, const std::vector<ListenerCallbacks>& listenerCallbacks, + uint64_t transactionId, const std::vector<uint64_t>& mergedTransactionIds) override; void bootFinished(); status_t getSupportedFrameTimestamps(std::vector<FrameEvent>* outSupported) const; sp<IDisplayEventConnection> createDisplayEventConnection( @@ -787,7 +793,7 @@ private: */ bool applyTransactionState(const FrameTimelineInfo& info, std::vector<ResolvedComposerState>& state, - std::span<DisplayState> displays, uint32_t flags, + Vector<DisplayState>& displays, uint32_t flags, const InputWindowCommands& inputWindowCommands, const int64_t desiredPresentTime, bool isAutoTimestamp, const std::vector<uint64_t>& uncacheBufferIds, @@ -1453,8 +1459,6 @@ private: // Flag used to set override desired display mode from backdoor bool mDebugDisplayModeSetByBackdoor = false; - // Tracks the number of maximum queued buffers by layer owner Uid. - using BufferStuffingMap = ftl::SmallMap<uid_t, uint32_t, 10>; BufferCountTracker mBufferCountTracker; std::unordered_map<DisplayId, sp<HdrLayerInfoReporter>> mHdrLayerInfoListeners diff --git a/services/surfaceflinger/Tracing/TransactionProtoParser.cpp b/services/surfaceflinger/Tracing/TransactionProtoParser.cpp index 6bbc04cf6f..3297c16113 100644 --- a/services/surfaceflinger/Tracing/TransactionProtoParser.cpp +++ b/services/surfaceflinger/Tracing/TransactionProtoParser.cpp @@ -321,7 +321,7 @@ QueuedTransactionState TransactionProtoParser::fromProto( int32_t displayCount = proto.display_changes_size(); t.displays.reserve(static_cast<size_t>(displayCount)); for (int i = 0; i < displayCount; i++) { - t.displays.emplace_back(fromProto(proto.display_changes(i))); + t.displays.add(fromProto(proto.display_changes(i))); } return t; } diff --git a/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp b/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp index 192602d7d2..46b98f9193 100644 --- a/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp +++ b/services/surfaceflinger/tests/DereferenceSurfaceControl_test.cpp @@ -52,8 +52,6 @@ protected: }; TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) { - // Last strong pointer is removed, the layer is destroyed and is removed - // from compostion. fgLayer = nullptr; { SCOPED_TRACE("after setting null"); @@ -63,9 +61,7 @@ TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) { } TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) { - Transaction transaction; - transaction.show(fgLayer); - // |transaction| retains a strong pointer, so layer is retained. + auto transaction = Transaction().show(fgLayer); fgLayer = nullptr; { SCOPED_TRACE("after setting null"); diff --git a/services/surfaceflinger/tests/IPC_test.cpp b/services/surfaceflinger/tests/IPC_test.cpp index 94cb878311..18bd3b92d2 100644 --- a/services/surfaceflinger/tests/IPC_test.cpp +++ b/services/surfaceflinger/tests/IPC_test.cpp @@ -42,22 +42,14 @@ using CallbackInfo = SurfaceComposerClient::CallbackInfo; using TCLHash = SurfaceComposerClient::TCLHash; using android::hardware::graphics::common::V1_1::BufferUsage; -class TransactionHelper : public Transaction, public Parcelable { +class TransactionHelper : public Transaction { public: - TransactionHelper() : Transaction() {} size_t getNumListeners() { return mListenerCallbacks.size(); } std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash> getListenerCallbacks() { return mListenerCallbacks; } - status_t writeToParcel(Parcel* parcel) const override { - return Transaction::writeToParcel(parcel); - } - - status_t readFromParcel(const Parcel* parcel) override { - return Transaction::readFromParcel(parcel); - } }; class IPCTestUtils { diff --git a/services/surfaceflinger/tests/end2end/.clang-format b/services/surfaceflinger/tests/end2end/.clang-format new file mode 120000 index 0000000000..5e8e20be26 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/.clang-format @@ -0,0 +1 @@ +../../../../../../build/soong/scripts/system-clang-format
\ No newline at end of file diff --git a/services/surfaceflinger/tests/end2end/.clang-tidy b/services/surfaceflinger/tests/end2end/.clang-tidy new file mode 100644 index 0000000000..29f3b4721c --- /dev/null +++ b/services/surfaceflinger/tests/end2end/.clang-tidy @@ -0,0 +1,380 @@ +# Copyright 2025 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. + +FormatStyle: file +InheritParentConfig: true + +# Please add checks explicitly rather than using wildcards like "modernize-*". +# These check names are current as of LLVM 20.0.0, as reported by "clang-tidy --list-checks --checks=*" +# For more information on each check, see https://clang.llvm.org/extra/clang-tidy/checks/list.html. +Checks: + # from android-* + - android-cloexec-accept + - android-cloexec-accept4 + - android-cloexec-creat + - android-cloexec-dup + - android-cloexec-epoll-create + - android-cloexec-epoll-create1 + - android-cloexec-fopen + - android-cloexec-inotify-init + - android-cloexec-inotify-init1 + - android-cloexec-memfd-create + - android-cloexec-open + - android-cloexec-pipe + - android-cloexec-pipe2 + - android-cloexec-socket + - android-comparison-in-temp-failure-retry + + # from bugprone-* + - bugprone-argument-comment + - bugprone-assert-side-effect + - bugprone-assignment-in-if-condition + - bugprone-bad-signal-to-kill-thread + - bugprone-bool-pointer-implicit-conversion + - bugprone-branch-clone + - bugprone-casting-through-void + - bugprone-chained-comparison + - bugprone-compare-pointer-to-member-virtual-function + - bugprone-copy-constructor-init + - bugprone-crtp-constructor-accessibility + - bugprone-dangling-handle + - bugprone-dynamic-static-initializers + - bugprone-easily-swappable-parameters + - bugprone-empty-catch + - bugprone-exception-escape + - bugprone-fold-init-type + - bugprone-forward-declaration-namespace + - bugprone-forwarding-reference-overload + - bugprone-implicit-widening-of-multiplication-result + - bugprone-inaccurate-erase + - bugprone-inc-dec-in-conditions + - bugprone-incorrect-enable-if + - bugprone-incorrect-roundings + - bugprone-infinite-loop + - bugprone-integer-division + - bugprone-lambda-function-name + - bugprone-macro-parentheses + - bugprone-macro-repeated-side-effects + - bugprone-misplaced-operator-in-strlen-in-alloc + - bugprone-misplaced-pointer-arithmetic-in-alloc + - bugprone-misplaced-widening-cast + - bugprone-move-forwarding-reference + - bugprone-multi-level-implicit-pointer-conversion + - bugprone-multiple-new-in-one-expression + - bugprone-multiple-statement-macro + - bugprone-narrowing-conversions + - bugprone-no-escape + - bugprone-non-zero-enum-to-bool-conversion + - bugprone-not-null-terminated-result + - bugprone-optional-value-conversion + - bugprone-parent-virtual-call + - bugprone-pointer-arithmetic-on-polymorphic-object + - bugprone-posix-return + - bugprone-redundant-branch-condition + - bugprone-reserved-identifier + - bugprone-return-const-ref-from-parameter + - bugprone-shared-ptr-array-mismatch + - bugprone-signal-handler + - bugprone-signed-char-misuse + - bugprone-sizeof-container + - bugprone-sizeof-expression + - bugprone-spuriously-wake-up-functions + - bugprone-standalone-empty + - bugprone-string-constructor + - bugprone-string-integer-assignment + - bugprone-string-literal-with-embedded-nul + - bugprone-stringview-nullptr + - bugprone-suspicious-enum-usage + - bugprone-suspicious-include + - bugprone-suspicious-memory-comparison + - bugprone-suspicious-memset-usage + - bugprone-suspicious-missing-comma + - bugprone-suspicious-realloc-usage + - bugprone-suspicious-semicolon + - bugprone-suspicious-string-compare + - bugprone-suspicious-stringview-data-usage + - bugprone-swapped-arguments + - bugprone-switch-missing-default-case + - bugprone-terminating-continue + - bugprone-throw-keyword-missing + - bugprone-too-small-loop-variable + - bugprone-unchecked-optional-access + - bugprone-undefined-memory-manipulation + - bugprone-undelegated-constructor + - bugprone-unhandled-exception-at-new + - bugprone-unhandled-self-assignment + - bugprone-unique-ptr-array-mismatch + - bugprone-unsafe-functions + - bugprone-unused-local-non-trivial-variable + - bugprone-unused-raii + - bugprone-unused-return-value + - bugprone-use-after-move + - bugprone-virtual-near-miss + + # from cert-* + - cert-con36-c + - cert-con54-cpp + - cert-ctr56-cpp + - cert-dcl03-c + - cert-dcl16-c + - cert-dcl37-c + - cert-dcl50-cpp + - cert-dcl51-cpp + - cert-dcl54-cpp + - cert-dcl58-cpp + - cert-dcl59-cpp + - cert-env33-c + - cert-err09-cpp + - cert-err33-c + - cert-err34-c + - cert-err52-cpp + - cert-err58-cpp + - cert-err60-cpp + - cert-err61-cpp + - cert-exp42-c + - cert-fio38-c + - cert-flp30-c + - cert-flp37-c + - cert-int09-c + - cert-mem57-cpp + - cert-msc24-c + - cert-msc30-c + - cert-msc32-c + - cert-msc33-c + - cert-msc50-cpp + - cert-msc51-cpp + - cert-msc54-cpp + - cert-oop11-cpp + - cert-oop54-cpp + - cert-oop57-cpp + - cert-oop58-cpp + - cert-pos44-c + - cert-pos47-c + - cert-sig30-c + - cert-str34-c + + # from concurrency-* + - concurrency-mt-unsafe + - concurrency-thread-canceltype-asynchronous + + # from cppcoreguidelines-* + - cppcoreguidelines-avoid-c-arrays + - cppcoreguidelines-avoid-capturing-lambda-coroutines + - cppcoreguidelines-avoid-const-or-ref-data-members + - cppcoreguidelines-avoid-do-while + - cppcoreguidelines-avoid-goto + - cppcoreguidelines-avoid-magic-numbers + - cppcoreguidelines-avoid-non-const-global-variables + - cppcoreguidelines-avoid-reference-coroutine-parameters + - cppcoreguidelines-c-copy-assignment-signature + - cppcoreguidelines-explicit-virtual-functions + - cppcoreguidelines-init-variables + - cppcoreguidelines-interfaces-global-init + - cppcoreguidelines-macro-to-enum + - cppcoreguidelines-macro-usage + - cppcoreguidelines-misleading-capture-default-by-value + - cppcoreguidelines-missing-std-forward + - cppcoreguidelines-narrowing-conversions + - cppcoreguidelines-no-malloc + - cppcoreguidelines-no-suspend-with-lock + - cppcoreguidelines-noexcept-destructor + - cppcoreguidelines-noexcept-move-operations + - cppcoreguidelines-noexcept-swap + - cppcoreguidelines-non-private-member-variables-in-classes + - cppcoreguidelines-owning-memory + - cppcoreguidelines-prefer-member-initializer + - cppcoreguidelines-pro-bounds-array-to-pointer-decay + - cppcoreguidelines-pro-bounds-constant-array-index + - cppcoreguidelines-pro-bounds-pointer-arithmetic + - cppcoreguidelines-pro-type-const-cast + - cppcoreguidelines-pro-type-cstyle-cast + - cppcoreguidelines-pro-type-member-init + - cppcoreguidelines-pro-type-reinterpret-cast + - cppcoreguidelines-pro-type-static-cast-downcast + - cppcoreguidelines-pro-type-union-access + - cppcoreguidelines-pro-type-vararg + - cppcoreguidelines-rvalue-reference-param-not-moved + - cppcoreguidelines-slicing + - cppcoreguidelines-special-member-functions + - cppcoreguidelines-use-default-member-init + - cppcoreguidelines-virtual-class-destructor + + # from google-* + - google-build-explicit-make-pair + - google-build-namespaces + - google-build-using-namespace + - google-default-arguments + - google-explicit-constructor + - google-global-names-in-headers + - google-objc-avoid-nsobject-new + - google-objc-avoid-throwing-exception + - google-objc-function-naming + - google-objc-global-variable-declaration + - google-readability-avoid-underscore-in-googletest-name + - google-readability-braces-around-statements + - google-readability-casting + - google-readability-function-size + - google-readability-namespace-comments + - google-readability-todo + - google-runtime-int + - google-runtime-operator + - google-upgrade-googletest-case + + # from misc-* + - misc-confusable-identifiers + - misc-const-correctness + - misc-coroutine-hostile-raii + - misc-definitions-in-headers + - misc-header-include-cycle + - misc-include-cleaner + - misc-misleading-bidirectional + - misc-misleading-identifier + - misc-misplaced-const + - misc-new-delete-overloads + - misc-no-recursion + - misc-non-copyable-objects + - misc-non-private-member-variables-in-classes + - misc-redundant-expression + - misc-static-assert + - misc-throw-by-value-catch-by-reference + - misc-unconventional-assign-operator + - misc-uniqueptr-reset-release + - misc-unused-alias-decls + - misc-unused-parameters + - misc-unused-using-decls + - misc-use-anonymous-namespace + - misc-use-internal-linkage + + # from modernize-* + - modernize-avoid-bind + - modernize-avoid-c-arrays + - modernize-concat-nested-namespaces + - modernize-deprecated-headers + - modernize-deprecated-ios-base-aliases + - modernize-loop-convert + - modernize-macro-to-enum + - modernize-make-shared + - modernize-make-unique + - modernize-min-max-use-initializer-list + - modernize-pass-by-value + - modernize-raw-string-literal + - modernize-redundant-void-arg + - modernize-replace-auto-ptr + - modernize-replace-disallow-copy-and-assign-macro + - modernize-replace-random-shuffle + - modernize-return-braced-init-list + - modernize-shrink-to-fit + - modernize-type-traits + - modernize-unary-static-assert + - modernize-use-auto + - modernize-use-bool-literals + - modernize-use-constraints + - modernize-use-default-member-init + - modernize-use-designated-initializers + - modernize-use-emplace + - modernize-use-equals-default + - modernize-use-equals-delete + - modernize-use-nodiscard + - modernize-use-noexcept + - modernize-use-nullptr + - modernize-use-override + - modernize-use-ranges + - modernize-use-starts-ends-with + - modernize-use-std-format + - modernize-use-std-numbers + - modernize-use-std-print + - modernize-use-trailing-return-type + - modernize-use-transparent-functors + - modernize-use-uncaught-exceptions + - modernize-use-using + + # from performance-* + - performance-avoid-endl + - performance-enum-size + - performance-faster-string-find + - performance-for-range-copy + - performance-implicit-conversion-in-loop + - performance-inefficient-algorithm + - performance-inefficient-string-concatenation + - performance-inefficient-vector-operation + - performance-move-const-arg + - performance-move-constructor-init + - performance-no-automatic-move + - performance-no-int-to-ptr + - performance-noexcept-destructor + - performance-noexcept-move-constructor + - performance-noexcept-swap + - performance-trivially-destructible + - performance-type-promotion-in-math-fn + - performance-unnecessary-copy-initialization + - performance-unnecessary-value-param + + # from portability-* + - portability-restrict-system-includes + - portability-simd-intrinsics + - portability-std-allocator-const + + # from readability-* + - readability-avoid-const-params-in-decls + - readability-avoid-nested-conditional-operator + - readability-avoid-return-with-void-value + - readability-avoid-unconditional-preprocessor-if + - readability-braces-around-statements + - readability-const-return-type + - readability-container-contains + - readability-container-data-pointer + - readability-container-size-empty + - readability-convert-member-functions-to-static + - readability-delete-null-pointer + - readability-duplicate-include + - readability-else-after-return + - readability-enum-initial-value + - readability-function-cognitive-complexity + - readability-function-size + - readability-identifier-length + - readability-identifier-naming + - readability-implicit-bool-conversion + - readability-inconsistent-declaration-parameter-name + - readability-isolate-declaration + - readability-magic-numbers + - readability-make-member-function-const + - readability-math-missing-parentheses + - readability-misleading-indentation + - readability-misplaced-array-index + - readability-named-parameter + - readability-non-const-parameter + - readability-operators-representation + - readability-qualified-auto + - readability-redundant-access-specifiers + - readability-redundant-casting + - readability-redundant-control-flow + - readability-redundant-declaration + - readability-redundant-function-ptr-dereference + - readability-redundant-inline-specifier + - readability-redundant-member-init + - readability-redundant-preprocessor + - readability-redundant-smartptr-get + - readability-redundant-string-cstr + - readability-redundant-string-init + - readability-reference-to-constructed-temporary + - readability-simplify-boolean-expr + - readability-simplify-subscript-expr + - readability-static-accessed-through-instance + - readability-static-definition-in-anonymous-namespace + - readability-string-compare + - readability-suspicious-call-argument + - readability-uniqueptr-delete-release + - readability-uppercase-literal-suffix + - readability-use-anyofallof + - readability-use-std-min-max diff --git a/services/surfaceflinger/tests/end2end/.clangd b/services/surfaceflinger/tests/end2end/.clangd new file mode 100644 index 0000000000..d64d2f8a55 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/.clangd @@ -0,0 +1,20 @@ +# Copyright 2025 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. + +Diagnostics: + UnusedIncludes: Strict + MissingIncludes: Strict + ClangTidy: + FastCheckFilter: None + # See the .clang-tidy files for additional configuration diff --git a/services/surfaceflinger/tests/end2end/Android.bp b/services/surfaceflinger/tests/end2end/Android.bp new file mode 100644 index 0000000000..8810330179 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/Android.bp @@ -0,0 +1,68 @@ +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_native_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_native_license"], +} + +cc_test { + name: "surfaceflinger_end2end_tests", + test_suites: ["device-tests"], + require_root: true, + + cpp_std: "experimental", + cflags: [ + "-DANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION", + "-DNODISCARD_EXPECTED", + "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS", + "-Wall", + "-Wconversion", + "-Werror", + "-Wextra", + "-Wformat", + "-Wno-non-virtual-dtor", + "-Wno-sign-compare", + "-Wno-sign-conversion", + "-Wshadow", + "-Wthread-safety", + "-Wunreachable-code", + "-Wunused", + ], + srcs: [ + "main.cpp", + "test_framework/core/TestService.cpp", + "test_framework/fake_hwc3/Hwc3Composer.cpp", + "test_framework/fake_hwc3/Hwc3Controller.cpp", + "test_framework/surfaceflinger/SFController.cpp", + "tests/Placeholder_test.cpp", + ], + tidy: true, + tidy_flags: [ + "--config=", // Use the .clang-tidy closest to each source file for the configuration + ], + tidy_checks_as_errors: [ + "*", + ], + include_dirs: [ + "frameworks/native/include", + ], + local_include_dirs: ["."], + shared_libs: [ + "libbase", + "libbinder", + "libbinder_ndk", + "libcutils", + "libgui", + "libsync", + "libui", + "libutils", + ], + static_libs: [ + "android.hardware.common-V2-ndk", + "android.hardware.graphics.common-V6-ndk", + "android.hardware.graphics.composer3-V3-ndk", + "libgtest", + ], +} diff --git a/services/surfaceflinger/tests/end2end/AndroidTest.xml b/services/surfaceflinger/tests/end2end/AndroidTest.xml new file mode 100644 index 0000000000..99cb7b3fee --- /dev/null +++ b/services/surfaceflinger/tests/end2end/AndroidTest.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright 2025 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. +--> +<configuration description="Configuration for surfaceflinger_end2end_tests"> + <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer" /> + <target_preparer class="com.android.tradefed.targetprep.DisableSELinuxTargetPreparer" /> + <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer"> + <!-- Stop everything to run SurfaceFlinger in isolation, with relaxed SELinux permissions --> + <option name="teardown-command" value="stop" /> + <option name="teardown-command" value="setprop debug.sf.nobootanimation 1" /> + + <!-- Restart everything with normal settings after the test finishes. --> + <option name="teardown-command" value="stop" /> + <option name="teardown-command" value="setprop debug.sf.nobootanimation 0" /> + <option name="teardown-command" value="setprop debug.sf.hwc_service_name default" /> + <option name="teardown-command" value="start" /> + </target_preparer> + <target_preparer class="com.android.compatibility.common.tradefed.targetprep.FilePusher"> + <option name="cleanup" value="true" /> + <option name="push" value="surfaceflinger_end2end_tests->/data/local/tests/surfaceflinger_end2end_tests" /> + </target_preparer> + <test class="com.android.tradefed.testtype.GTest" > + <option name="native-test-device-path" value="/data/local/tests" /> + <option name="module-name" value="surfaceflinger_end2end_tests" /> + <option name="native-test-timeout" value="15m"/> + </test> +</configuration> diff --git a/services/surfaceflinger/tests/end2end/README.md b/services/surfaceflinger/tests/end2end/README.md new file mode 100644 index 0000000000..2f58cec5c7 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/README.md @@ -0,0 +1,75 @@ +# `surfaceflinger_end2end_tests` + +Tests to cover end to end testing of SurfaceFlinger. + +In particular the test framework allows you to simulate various display +configurations, so the test can confirm displays are handled correctly. + +## Quick Useful info + +### Running the tests + +At present the tests should run on any target, though the typical target would +be a [Cuttlefish](https://source.android.com/docs/devices/cuttlefish) VM +target such as `aosp_cf_x86_64_phone`. + +At some future time the test may be rewritten to require +[`vkms`](https://dri.freedesktop.org/docs/drm/gpu/vkms.html) and +[`drm_hwcomposer`](https://gitlab.freedesktop.org/drm-hwcomposer/drm-hwcomposer) + +``` +atest surfaceflinger_end2end_tests +``` + +You can also run the google test binary directly. However you will also need +to run a few other set-up and tear-down commands that are part of the +AndroidTest.xml configuration, so that SurfaceFlinger can be used run isolation +from the rest of the system. + +``` +# Set-up +adb root +adb shell stop +adb shell setenforce 0 +adb shell setprop debug.sf.nobootanimation 1 + +# Sync and run the test +adb sync data +adb shell data/nativetest64/surfaceflinger_end2end_tests/surfaceflinger_end2end_tests + +# Tear-down +adb shell stop +adb shell setenforce 1 +adb shell setprop debug.sf.nobootanimation 0 +adb shell setprop debug.sf.hwc_service_name default +``` + +### Manual clang-tidy checks via Soong + +At present Android does not run the clang-tidy checks as part of its +presubmit checks. + +You can run them through the build system by using phony target that are +automatically created for each source subdirectory. + +For the code under `frameworks/native/services/surfaceflinger/tests/end2end`, +you would build: + +``` +m tidy-frameworks-native-services-surfaceflinger-tests-end2end +``` + +For more information see the build documentation: + +* <https://android.googlesource.com/platform/build/soong/+/main/docs/tidy.md#the-tidy_directory-targets> + +### Seeing clang-tidy checks in your editor + +If your editor supports using [`clangd`](https://clangd.llvm.org/) as a +C++ language server, you can build and export a compilation database using +Soong. With the local `.clangd` configuration file, you should see the same +checks in editor, along with all the other checks `clangd` runs. + +See the build documentation for the compilation database instructions: + +https://android.googlesource.com/platform/build/soong/+/main/docs/compdb.md diff --git a/services/surfaceflinger/tests/end2end/main.cpp b/services/surfaceflinger/tests/end2end/main.cpp new file mode 100644 index 0000000000..ddf690021d --- /dev/null +++ b/services/surfaceflinger/tests/end2end/main.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2025 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. + */ + +#include <cstdlib> +#include <string_view> + +#include <android-base/logging.h> +#include <android/binder_process.h> +#include <gtest/gtest.h> + +namespace { + +void init(int argc, char** argv) { + using namespace std::string_view_literals; + + ::testing::InitGoogleTest(&argc, argv); + ::android::base::InitLogging(argv, android::base::StderrLogger); + + auto minimumSeverity = android::base::INFO; + for (int i = 1; i < argc; i++) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + const std::string_view arg = argv[i]; + + if (arg == "-v"sv) { + minimumSeverity = android::base::DEBUG; + } else if (arg == "-vv"sv) { + minimumSeverity = android::base::VERBOSE; + } + } + ::android::base::SetMinimumLogSeverity(minimumSeverity); +} + +} // namespace + +auto main(int argc, char** argv) -> int { + init(argc, argv); + + ABinderProcess_setThreadPoolMaxThreadCount(1); + ABinderProcess_startThreadPool(); + + return RUN_ALL_TESTS(); +}
\ No newline at end of file diff --git a/services/surfaceflinger/tests/end2end/test_framework/core/DisplayConfiguration.h b/services/surfaceflinger/tests/end2end/test_framework/core/DisplayConfiguration.h new file mode 100644 index 0000000000..c3a535e10f --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/core/DisplayConfiguration.h @@ -0,0 +1,29 @@ +/* + * Copyright 2025 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. + */ + +#pragma once + +#include <cstdint> + +namespace android::surfaceflinger::tests::end2end::test_framework::core { + +struct DisplayConfiguration final { + using Id = int64_t; + + Id id{}; +}; + +} // namespace android::surfaceflinger::tests::end2end::test_framework::core diff --git a/services/surfaceflinger/tests/end2end/test_framework/core/TestService.cpp b/services/surfaceflinger/tests/end2end/test_framework/core/TestService.cpp new file mode 100644 index 0000000000..0531f18527 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/core/TestService.cpp @@ -0,0 +1,82 @@ +/* + * Copyright 2025 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. + */ + +#include <memory> +#include <span> +#include <string> +#include <utility> +#include <vector> + +#include <android-base/expected.h> +#include <ftl/ignore.h> + +#include "test_framework/core/DisplayConfiguration.h" +#include "test_framework/core/TestService.h" +#include "test_framework/fake_hwc3/Hwc3Controller.h" +#include "test_framework/surfaceflinger/SFController.h" + +namespace android::surfaceflinger::tests::end2end::test_framework::core { + +struct TestService::Passkey final {}; + +auto TestService::startWithDisplays(const std::vector<DisplayConfiguration>& displays) + -> base::expected<std::unique_ptr<TestService>, std::string> { + using namespace std::string_literals; + + auto service = std::make_unique<TestService>(TestService::Passkey{}); + if (service == nullptr) { + return base::unexpected("Failed to construct the TestService instance."s); + } + + if (auto result = service->init(displays); !result) { + return base::unexpected("Failed to init the TestService instance: "s + result.error()); + } + + return service; +} + +TestService::TestService(Passkey passkey) { + ftl::ignore(passkey); +} + +auto TestService::init(std::span<const DisplayConfiguration> displays) + -> base::expected<void, std::string> { + using namespace std::string_literals; + + auto hwcResult = fake_hwc3::Hwc3Controller::make(displays); + if (!hwcResult) { + return base::unexpected(std::move(hwcResult).error()); + } + auto hwc = *std::move(hwcResult); + + auto flingerResult = surfaceflinger::SFController::make(); + if (!flingerResult) { + return base::unexpected(std::move(flingerResult).error()); + } + auto flinger = *std::move(flingerResult); + + surfaceflinger::SFController::useHwcService(fake_hwc3::Hwc3Controller::getServiceName()); + + if (auto result = flinger->startAndConnect(); !result) { + return base::unexpected(std::move(result).error()); + } + + mHwc = std::move(hwc); + mFlinger = std::move(flinger); + return {}; +} + +} // namespace android::surfaceflinger::tests::end2end::test_framework::core diff --git a/services/surfaceflinger/tests/end2end/test_framework/core/TestService.h b/services/surfaceflinger/tests/end2end/test_framework/core/TestService.h new file mode 100644 index 0000000000..21e6426406 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/core/TestService.h @@ -0,0 +1,76 @@ +/* + * Copyright 2025 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. + */ + +#pragma once + +#include <memory> +#include <span> +#include <string> +#include <vector> + +#include <android-base/expected.h> +#include <android-base/logging.h> + +#include "test_framework/core/DisplayConfiguration.h" + +namespace android::surfaceflinger::tests::end2end::test_framework { + +namespace surfaceflinger { + +class SFController; + +} // namespace surfaceflinger + +namespace fake_hwc3 { + +class Hwc3Controller; + +} // namespace fake_hwc3 + +namespace core { + +class TestService final { + struct Passkey; // Uses the passkey idiom to restrict construction. + + public: + // Constructs the test service, and starts it with the given displays as connected at boot. + [[nodiscard]] static auto startWithDisplays(const std::vector<DisplayConfiguration>& displays) + -> base::expected<std::unique_ptr<TestService>, std::string>; + + explicit TestService(Passkey passkey); + + // Obtains the HWC3 back-end controller + [[nodiscard]] auto hwc() -> fake_hwc3::Hwc3Controller& { + CHECK(mHwc); + return *mHwc; + } + + // Obtains the SurfaceFlinger front-end controller + [[nodiscard]] auto flinger() -> surfaceflinger::SFController& { + CHECK(mFlinger); + return *mFlinger; + } + + private: + [[nodiscard]] auto init(std::span<const DisplayConfiguration> displays) + -> base::expected<void, std::string>; + + std::shared_ptr<fake_hwc3::Hwc3Controller> mHwc; + std::shared_ptr<surfaceflinger::SFController> mFlinger; +}; + +} // namespace core +} // namespace android::surfaceflinger::tests::end2end::test_framework diff --git a/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Composer.cpp b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Composer.cpp new file mode 100644 index 0000000000..5349ef0b58 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Composer.cpp @@ -0,0 +1,123 @@ +/* + * Copyright 2025 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. + */ + +#include <cstdint> +#include <memory> +#include <string> +#include <string_view> +#include <utility> +#include <vector> + +#include <aidl/android/hardware/graphics/composer3/BnComposer.h> +#include <aidl/android/hardware/graphics/composer3/Capability.h> +#include <aidl/android/hardware/graphics/composer3/IComposer.h> +#include <aidl/android/hardware/graphics/composer3/PowerMode.h> + +#include <android-base/expected.h> +#include <android-base/logging.h> +#include <android/binder_auto_utils.h> +#include <android/binder_interface_utils.h> +#include <android/binder_status.h> +#include <ftl/ignore.h> + +#include "test_framework/core/DisplayConfiguration.h" +#include "test_framework/fake_hwc3/Hwc3Composer.h" + +namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 { + +class Hwc3Composer::Hwc3ComposerImpl final + : public aidl::android::hardware::graphics::composer3::BnComposer { + using Capability = aidl::android::hardware::graphics::composer3::Capability; + using IComposerClient = aidl::android::hardware::graphics::composer3::IComposerClient; + using Hwc3PowerMode = aidl::android::hardware::graphics::composer3::PowerMode; + + // begin IComposer overrides + + auto dump(int dumpFd, const char** args, uint32_t num_args) -> binder_status_t override { + UNIMPLEMENTED(WARNING); + ftl::ignore(dumpFd, args, num_args); + return static_cast<binder_status_t>(STATUS_NO_MEMORY); + } + + auto createClient(std::shared_ptr<IComposerClient>* out_client) -> ndk::ScopedAStatus override { + UNIMPLEMENTED(WARNING); + ftl::ignore(out_client); + return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage( + IComposer::EX_NO_RESOURCES, "Client failed to initialize"); + } + + auto getCapabilities(std::vector<Capability>* out_capabilities) -> ndk::ScopedAStatus override { + UNIMPLEMENTED(WARNING); + ftl::ignore(out_capabilities); + return ndk::ScopedAStatus::ok(); + } + + // end IComposer overrides +}; + +struct Hwc3Composer::Passkey final {}; + +auto Hwc3Composer::getServiceName(std::string_view baseServiceName) -> std::string { + return Hwc3ComposerImpl::makeServiceName(baseServiceName); +} + +auto Hwc3Composer::make() -> base::expected<std::shared_ptr<Hwc3Composer>, std::string> { + using namespace std::string_literals; + + auto composer = std::make_shared<Hwc3Composer>(Passkey{}); + if (composer == nullptr) { + return base::unexpected("Failed to construct the Hwc3Composer instance."s); + } + + if (auto result = composer->init(); !result) { + return base::unexpected("Failed to init the Hwc3Composer instance: "s + result.error()); + } + + return composer; +} + +Hwc3Composer::Hwc3Composer(Hwc3Composer::Passkey passkey) { + ftl::ignore(passkey); +} + +auto Hwc3Composer::init() -> base::expected<void, std::string> { + using namespace std::string_literals; + + auto impl = ndk::SharedRefBase::make<Hwc3ComposerImpl>(); + if (!impl) { + return base::unexpected("Failed to construct the Hwc3ComposerImpl instance."s); + } + + mImpl = std::move(impl); + + return {}; +} + +auto Hwc3Composer::getComposer() -> std::shared_ptr<Hwc3IComposer> { + return mImpl; +} + +void Hwc3Composer::addDisplay(const core::DisplayConfiguration& display) { + UNIMPLEMENTED(WARNING); + ftl::ignore(display, mImpl); +} + +void Hwc3Composer::removeDisplay(core::DisplayConfiguration::Id displayId) { + UNIMPLEMENTED(WARNING); + ftl::ignore(displayId, mImpl); +} + +} // namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 diff --git a/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Composer.h b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Composer.h new file mode 100644 index 0000000000..6d6b7374c6 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Composer.h @@ -0,0 +1,61 @@ +/* + * Copyright 2025 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. + */ + +#pragma once + +#include <memory> +#include <string> +#include <string_view> + +#include <aidl/android/hardware/graphics/composer3/IComposer.h> +#include <android-base/expected.h> + +#include "test_framework/core/DisplayConfiguration.h" + +namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 { + +class Hwc3Composer final { + struct Passkey; // Uses the passkey idiom to restrict construction. + + class Hwc3ComposerImpl; // An internal class implements the AIDL interface. + + public: + using Hwc3IComposer = aidl::android::hardware::graphics::composer3::IComposer; + + // Gets the full qualified service name given a base name for the service. + [[nodiscard]] static auto getServiceName(std::string_view baseServiceName) -> std::string; + + // Constructs a Hwc3Composer instance. + [[nodiscard]] static auto make() -> base::expected<std::shared_ptr<Hwc3Composer>, std::string>; + + explicit Hwc3Composer(Passkey passkey); + + // Obtains the AIDL composer3::IComposer interface for the internal instance. + [[nodiscard]] auto getComposer() -> std::shared_ptr<Hwc3IComposer>; + + // Adds a display to the composer. This will sent a hotplug connect event. + void addDisplay(const core::DisplayConfiguration& display); + + // Removes a display from the composer. This will sent a hotplug disconnect event. + void removeDisplay(core::DisplayConfiguration::Id displayId); + + private: + [[nodiscard]] auto init() -> base::expected<void, std::string>; + + std::shared_ptr<Hwc3ComposerImpl> mImpl; +}; + +} // namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 diff --git a/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Controller.cpp b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Controller.cpp new file mode 100644 index 0000000000..ea985c09b4 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Controller.cpp @@ -0,0 +1,106 @@ +/* + * Copyright 2025 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. + */ + +#include <memory> +#include <span> +#include <string> +#include <utility> + +#include <android-base/expected.h> +#include <android-base/logging.h> +#include <android/binder_manager.h> +#include <android/binder_stability.h> +#include <android/binder_status.h> +#include <fmt/format.h> +#include <ftl/ignore.h> + +#include "test_framework/core/DisplayConfiguration.h" +#include "test_framework/fake_hwc3/Hwc3Composer.h" +#include "test_framework/fake_hwc3/Hwc3Controller.h" + +namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 { + +struct Hwc3Controller::Passkey final {}; + +auto Hwc3Controller::make(std::span<const core::DisplayConfiguration> displays) + -> base::expected<std::shared_ptr<fake_hwc3::Hwc3Controller>, std::string> { + using namespace std::string_literals; + + auto controller = std::make_unique<Hwc3Controller>(Passkey{}); + if (controller == nullptr) { + return base::unexpected("Failed to construct the Hwc3Controller instance"s); + } + + if (auto result = controller->init(displays); !result) { + return base::unexpected("Failed to construct the Hwc3Controller instance: "s + + result.error()); + } + + return controller; +} + +Hwc3Controller::Hwc3Controller(Passkey passkey) { + ftl::ignore(passkey); +} + +auto Hwc3Controller::init(const std::span<const core::DisplayConfiguration> displays) + -> base::expected<void, std::string> { + using namespace std::string_literals; + + auto qualifiedServiceName = Hwc3Composer::getServiceName(baseServiceName); + + auto composerResult = Hwc3Composer::make(); + if (!composerResult) { + return base::unexpected(std::move(composerResult).error()); + } + auto composer = *std::move(composerResult); + + for (const auto& display : displays) { + composer->addDisplay(display); + } + + auto binder = composer->getComposer()->asBinder(); + + // This downgrade allows us to use the fake service name without it being defined in the + // VINTF manifest. + AIBinder_forceDowngradeToLocalStability(binder.get()); + + auto status = AServiceManager_addService(binder.get(), qualifiedServiceName.c_str()); + if (status != STATUS_OK) { + return base::unexpected(fmt::format("Failed to register service {}. Error {}.", + qualifiedServiceName, status)); + } + LOG(INFO) << "Registered service " << qualifiedServiceName << ". Error: " << status; + + mComposer = std::move(composer); + return {}; +} + +auto Hwc3Controller::getServiceName() -> std::string { + return Hwc3Composer::getServiceName(baseServiceName); +} + +void Hwc3Controller::addDisplay(const core::DisplayConfiguration& config) { + CHECK(mComposer); + mComposer->addDisplay(config); +} + +void Hwc3Controller::removeDisplay(core::DisplayConfiguration::Id displayId) { + CHECK(mComposer); + mComposer->removeDisplay(displayId); +} + +} // namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 diff --git a/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Controller.h b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Controller.h new file mode 100644 index 0000000000..e53d2cfc48 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/fake_hwc3/Hwc3Controller.h @@ -0,0 +1,59 @@ +/* + * Copyright 2025 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. + */ + +#pragma once + +#include <memory> +#include <span> +#include <string> + +#include <android-base/expected.h> + +#include "test_framework/core/DisplayConfiguration.h" + +namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 { + +class Hwc3Composer; + +class Hwc3Controller final { + struct Passkey; // Uses the passkey idiom to restrict construction. + + public: + // Gets the service name for the HWC3 instance that will be created and registered + [[nodiscard]] static auto getServiceName() -> std::string; + + // Makes the HWC3 controller instance. + [[nodiscard]] static auto make(std::span<const core::DisplayConfiguration> displays) + -> base::expected<std::shared_ptr<fake_hwc3::Hwc3Controller>, std::string>; + + explicit Hwc3Controller(Passkey passkey); + + // Adds a new display to the HWC3, which will become a hotplug connect event. + void addDisplay(const core::DisplayConfiguration& config); + + // Removes a new display from the HWC3, which will become a hotplug disconnect event. + void removeDisplay(core::DisplayConfiguration::Id displayId); + + private: + static constexpr std::string baseServiceName = "fake"; + + [[nodiscard]] auto init(std::span<const core::DisplayConfiguration> displays) + -> base::expected<void, std::string>; + + std::shared_ptr<Hwc3Composer> mComposer; +}; + +} // namespace android::surfaceflinger::tests::end2end::test_framework::fake_hwc3 diff --git a/services/surfaceflinger/tests/end2end/test_framework/surfaceflinger/SFController.cpp b/services/surfaceflinger/tests/end2end/test_framework/surfaceflinger/SFController.cpp new file mode 100644 index 0000000000..1cf49c5750 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/surfaceflinger/SFController.cpp @@ -0,0 +1,181 @@ +/* + * Copyright 2025 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. + */ + +#include <chrono> +#include <cstdlib> +#include <memory> +#include <string> +#include <string_view> +#include <thread> +#include <utility> + +#include <android-base/expected.h> +#include <android-base/logging.h> +#include <android-base/properties.h> +#include <android/gui/ISurfaceComposer.h> +#include <binder/IBinder.h> +#include <binder/IInterface.h> +#include <binder/IServiceManager.h> +#include <binder/Status.h> +#include <ftl/finalizer.h> +#include <ftl/ignore.h> +#include <gui/Surface.h> +#include <gui/SurfaceComposerClient.h> +#include <utils/String16.h> +#include <utils/String8.h> +#include <utils/StrongPointer.h> + +#include "test_framework/surfaceflinger/SFController.h" + +namespace android::surfaceflinger::tests::end2end::test_framework::surfaceflinger { + +namespace { + +auto waitForSurfaceFlingerAIDL() -> sp<gui::ISurfaceComposer> { + constexpr auto kTimeout = std::chrono::seconds(30); + constexpr auto kSurfaceFlingerServiceName = "SurfaceFlingerAIDL"; + const sp<android::IServiceManager> serviceManager(android::defaultServiceManager()); + const auto kTimeoutAfter = std::chrono::steady_clock::now() + kTimeout; + + LOG(INFO) << "Waiting " << kTimeout << " for service manager registration...."; + sp<android::IBinder> flingerService; + while (flingerService == nullptr) { + if (std::chrono::steady_clock::now() > kTimeoutAfter) { + LOG(INFO) << "... Timeout!"; + return nullptr; + } + + constexpr auto sleepTime = std::chrono::milliseconds(10); + std::this_thread::sleep_for(sleepTime); + flingerService = serviceManager->checkService(String16(kSurfaceFlingerServiceName)); + } + LOG(INFO) << "Obtained surfaceflinger interface from service manager."; + + return interface_cast<gui::ISurfaceComposer>(flingerService); +} + +} // namespace + +struct SFController::Passkey final {}; + +void SFController::useHwcService(std::string_view fqn) { + base::SetProperty("debug.sf.hwc_service_name", std::string(fqn)); +} + +auto SFController::make() -> base::expected<std::shared_ptr<SFController>, std::string> { + using namespace std::string_literals; + + auto controller = std::make_unique<SFController>(Passkey{}); + if (controller == nullptr) { + return base::unexpected("Failed to construct the SFController instance."s); + } + + if (auto result = controller->init(); !result) { + return base::unexpected("Failed to init the SFController instance: "s + result.error()); + } + + return controller; +} + +SFController::SFController(Passkey passkey) { + ftl::ignore(passkey); +} + +auto SFController::init() -> base::expected<void, std::string> { + LOG(INFO) << "Stopping everything to prepare for tests"; + // NOLINTBEGIN(cert-env33-c) + system("stop"); + // NOLINTEND(cert-env33-c) + + mCleanup = ftl::Finalizer([this]() { stop(); }); + + return {}; +} + +auto SFController::startAndConnect() -> base::expected<void, std::string> { + using namespace std::string_literals; + + start(); + + LOG(VERBOSE) << "Getting ISurfaceComposer...."; + auto surfaceComposerAidl = waitForSurfaceFlingerAIDL(); + if (surfaceComposerAidl == nullptr) { + return base::unexpected("Failed to obtain the surfaceComposerAidl interface."s); + } + LOG(VERBOSE) << "Getting ISurfaceComposerClient...."; + sp<gui::ISurfaceComposerClient> surfaceComposerClientAidl; + if (!surfaceComposerAidl->createConnection(&surfaceComposerClientAidl).isOk()) { + return base::unexpected("Failed to obtain the surfaceComposerClientAidl interface."s); + } + if (surfaceComposerClientAidl == nullptr) { + return base::unexpected("Failed to obtain a valid surfaceComposerClientAidl interface."s); + } + auto surfaceComposerClient = sp<SurfaceComposerClient>::make(surfaceComposerClientAidl); + if (surfaceComposerClient == nullptr) { + return base::unexpected( + "Failed to construct a surfaceComposerClient around the aidl interface."s); + } + + mSurfaceComposerAidl = std::move(surfaceComposerAidl); + mSurfaceComposerClientAidl = std::move(surfaceComposerClientAidl); + mSurfaceComposerClient = std::move(surfaceComposerClient); + + LOG(INFO) << "Connected to surfaceflinger"; + return {}; +} + +void SFController::start() { + LOG(INFO) << "Starting surfaceflinger"; + // NOLINTBEGIN(cert-env33-c) + system("start surfaceflinger"); + // NOLINTEND(cert-env33-c) +} + +void SFController::stop() { + LOG(INFO) << "Stopping surfaceflinger"; + // NOLINTBEGIN(cert-env33-c) + system("stop surfaceflinger"); + // NOLINTEND(cert-env33-c) + + if (mSurfaceComposerAidl != nullptr) { + LOG(INFO) << "Waiting for SF AIDL interface to die"; + + constexpr auto kTimeout = std::chrono::seconds(30); + const auto binder = android::gui::ISurfaceComposer::asBinder(mSurfaceComposerAidl); + const auto kTimeoutAfter = std::chrono::steady_clock::now() + kTimeout; + + while (binder->isBinderAlive()) { + if (std::chrono::steady_clock::now() > kTimeoutAfter) { + LOG(INFO) << "... Timeout!"; + break; + } + + ftl::ignore = binder->pingBinder(); + + constexpr auto kPollInterval = std::chrono::milliseconds(10); + std::this_thread::sleep_for(kPollInterval); + } + + constexpr auto kShutdownWait = std::chrono::milliseconds(500); + std::this_thread::sleep_for(kShutdownWait); + } + + mSurfaceComposerClient = nullptr; + mSurfaceComposerClientAidl = nullptr; + mSurfaceComposerAidl = nullptr; +} + +} // namespace android::surfaceflinger::tests::end2end::test_framework::surfaceflinger diff --git a/services/surfaceflinger/tests/end2end/test_framework/surfaceflinger/SFController.h b/services/surfaceflinger/tests/end2end/test_framework/surfaceflinger/SFController.h new file mode 100644 index 0000000000..58bac9197f --- /dev/null +++ b/services/surfaceflinger/tests/end2end/test_framework/surfaceflinger/SFController.h @@ -0,0 +1,70 @@ +/* + * Copyright 2025 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. + */ + +#pragma once + +#include <memory> +#include <string> +#include <string_view> + +#include <android-base/expected.h> +#include <ftl/finalizer.h> +#include <utils/StrongPointer.h> + +namespace android::gui { + +class ISurfaceComposer; +class ISurfaceComposerClient; + +} // namespace android::gui + +namespace android { + +class SurfaceComposerClient; + +} // namespace android + +namespace android::surfaceflinger::tests::end2end::test_framework::surfaceflinger { + +class SFController final { + struct Passkey; // Uses the passkey idiom to restrict construction. + + public: + // Sets a property so that SurfaceFlinger uses the named HWC service. + static void useHwcService(std::string_view fqn); + + // Makes an instance of the SFController. + [[nodiscard]] static auto make() -> base::expected<std::shared_ptr<SFController>, std::string>; + + explicit SFController(Passkey pass); + + // Starts SurfaceFlinger and establishes the AIDL interface connections. + [[nodiscard]] auto startAndConnect() -> base::expected<void, std::string>; + + private: + [[nodiscard]] auto init() -> base::expected<void, std::string>; + static void start(); + void stop(); + + sp<gui::ISurfaceComposer> mSurfaceComposerAidl; + sp<gui::ISurfaceComposerClient> mSurfaceComposerClientAidl; + sp<SurfaceComposerClient> mSurfaceComposerClient; + + // Finalizers should be last so their destructors are invoked first. + ftl::FinalizerFtl mCleanup; +}; + +} // namespace android::surfaceflinger::tests::end2end::test_framework::surfaceflinger diff --git a/services/surfaceflinger/tests/end2end/tests/.clang-tidy b/services/surfaceflinger/tests/end2end/tests/.clang-tidy new file mode 100644 index 0000000000..4924c466c0 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/tests/.clang-tidy @@ -0,0 +1,32 @@ +# Copyright 2025 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. + +FormatStyle: file +InheritParentConfig: true + +# Note: For tests, we are actually turning off certain checks enabled for the +# non-test code in the parent .clang-tidy file. +Checks: + - -cppcoreguidelines-avoid-magic-numbers # Allow tests to use magic numbers. + - -cppcoreguidelines-avoid-goto # Google Test macros use goto. + - -cppcoreguidelines-avoid-non-const-global-variables # Google Test macros define global variables. + - -cppcoreguidelines-macro-usage # Google Benchmark defines function-like macros. + - -cppcoreguidelines-owning-memory # Google Test macros use operator new directly. + - -google-runtime-int # Tests might intentionally use the base "short"/"long" types and not want to use "int16"/"int64". + - -misc-use-anonymous-namespace # Google Test macros declare some static global variables to not export them. + - -modernize-use-trailing-return-type # Google Test macros use non-trailing return types. + - -performance-move-const-arg # Tests might std::move() a trivially copyable value as part of testing that moving works. + - -readability-function-cognitive-complexity # Assertions turn into extra branches, increasing apparent complexity. + - -readability-magic-numbers # Allow tests to use magic numbers + diff --git a/services/surfaceflinger/tests/end2end/tests/Placeholder_test.cpp b/services/surfaceflinger/tests/end2end/tests/Placeholder_test.cpp new file mode 100644 index 0000000000..3c4277f8a0 --- /dev/null +++ b/services/surfaceflinger/tests/end2end/tests/Placeholder_test.cpp @@ -0,0 +1,39 @@ +/* + * Copyright 2025 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. + */ + +#include <gtest/gtest.h> + +#include <android-base/logging.h> + +#include "test_framework/core/TestService.h" + +namespace android::surfaceflinger::tests::end2end { +namespace { + +struct Placeholder : public ::testing::Test {}; + +TEST_F(Placeholder, Bringup) { + auto serviceResult = test_framework::core::TestService::startWithDisplays({ + {.id = 123}, + }); + if (!serviceResult) { + LOG(WARNING) << "End2End service not available. " << serviceResult.error(); + GTEST_SKIP() << "End2End service not available. " << serviceResult.error(); + } +} + +} // namespace +} // namespace android::surfaceflinger::tests::end2end diff --git a/services/surfaceflinger/tests/unittests/EventThreadTest.cpp b/services/surfaceflinger/tests/unittests/EventThreadTest.cpp index 6f15db8beb..76e01a6e7d 100644 --- a/services/surfaceflinger/tests/unittests/EventThreadTest.cpp +++ b/services/surfaceflinger/tests/unittests/EventThreadTest.cpp @@ -23,7 +23,6 @@ #include <gmock/gmock.h> #include <gtest/gtest.h> -#include <gui/DisplayEventReceiver.h> #include <log/log.h> #include <scheduler/VsyncConfig.h> #include <utils/Errors.h> @@ -112,8 +111,6 @@ protected: void expectOnExpectedPresentTimePosted(nsecs_t expectedPresentTime); void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride>); - void expectQueuedBufferCountReceivedByConnection( - ConnectionEventRecorder& connectionEventRecorder, uint32_t expectedBufferCount); void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime, nsecs_t deadlineTimestamp) { @@ -147,7 +144,6 @@ protected: sp<MockEventThreadConnection> mConnection; sp<MockEventThreadConnection> mThrottledConnection; std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager; - std::vector<ConnectionEventRecorder*> mBufferStuffedConnectionRecorders; std::chrono::nanoseconds mVsyncPeriod; @@ -380,14 +376,6 @@ void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection( EXPECT_EQ(expectedDisplayId, event.header.displayId); } -void EventThreadTest::expectQueuedBufferCountReceivedByConnection( - ConnectionEventRecorder& connectionEventRecorder, uint32_t expectedBufferCount) { - auto args = connectionEventRecorder.waitForCall(); - ASSERT_TRUE(args.has_value()); - const auto& event = std::get<0>(args.value()); - EXPECT_EQ(expectedBufferCount, event.vsync.vsyncData.numberQueuedBuffers); -} - namespace { using namespace testing; @@ -880,63 +868,6 @@ TEST_F(EventThreadTest, postHcpLevelsChanged) { EXPECT_EQ(HDCP_V2, event.hdcpLevelsChange.maxLevel); } -TEST_F(EventThreadTest, connectionReceivesBufferStuffing) { - setupEventThread(); - - // Create a connection that will experience buffer stuffing. - ConnectionEventRecorder stuffedConnectionEventRecorder{0}; - sp<MockEventThreadConnection> stuffedConnection = - createConnection(stuffedConnectionEventRecorder, - gui::ISurfaceComposer::EventRegistration::modeChanged | - gui::ISurfaceComposer::EventRegistration::frameRateOverride, - 111); - - // Add a connection and buffer count to the list of stuffed Uids that will receive - // data in the next vsync event. - BufferStuffingMap bufferStuffedUids; - bufferStuffedUids.try_emplace(stuffedConnection->mOwnerUid, 3); - mThread->addBufferStuffedUids(bufferStuffedUids); - mBufferStuffedConnectionRecorders.emplace_back(&stuffedConnectionEventRecorder); - - // Signal that we want the next vsync event to be posted to two connections. - mThread->requestNextVsync(mConnection); - mThread->requestNextVsync(stuffedConnection); - onVSyncEvent(123, 456, 789); - - // Vsync event data contains number of queued buffers. - expectQueuedBufferCountReceivedByConnection(mConnectionEventCallRecorder, 0); - expectQueuedBufferCountReceivedByConnection(stuffedConnectionEventRecorder, 3); -} - -TEST_F(EventThreadTest, connectionsWithSameUidReceiveBufferStuffing) { - setupEventThread(); - - // Create a connection with the same Uid as another connection. - ConnectionEventRecorder secondConnectionEventRecorder{0}; - sp<MockEventThreadConnection> secondConnection = - createConnection(secondConnectionEventRecorder, - gui::ISurfaceComposer::EventRegistration::modeChanged | - gui::ISurfaceComposer::EventRegistration::frameRateOverride, - mConnectionUid); - - // Add connection Uid and buffer count to the list of stuffed Uids that will receive - // data in the next vsync event. - BufferStuffingMap bufferStuffedUids; - bufferStuffedUids.try_emplace(mConnectionUid, 3); - mThread->addBufferStuffedUids(bufferStuffedUids); - mBufferStuffedConnectionRecorders.emplace_back(&mConnectionEventCallRecorder); - mBufferStuffedConnectionRecorders.emplace_back(&secondConnectionEventRecorder); - - // Signal that we want the next vsync event to be posted to two connections. - mThread->requestNextVsync(mConnection); - mThread->requestNextVsync(secondConnection); - onVSyncEvent(123, 456, 789); - - // Vsync event data contains number of queued buffers. - expectQueuedBufferCountReceivedByConnection(mConnectionEventCallRecorder, 3); - expectQueuedBufferCountReceivedByConnection(secondConnectionEventRecorder, 3); -} - } // namespace } // namespace android diff --git a/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp b/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp index 9c143fdd41..6cc6322bfc 100644 --- a/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp +++ b/services/surfaceflinger/tests/unittests/SurfaceFlinger_NotifyExpectedPresentTest.cpp @@ -45,15 +45,28 @@ protected: void setTransactionState() { ASSERT_TRUE(mFlinger.getTransactionQueue().isEmpty()); TransactionInfo transaction; - mFlinger.setTransactionState(std::move(transaction)); + mFlinger.setTransactionState(FrameTimelineInfo{}, transaction.states, transaction.displays, + transaction.flags, transaction.applyToken, + transaction.inputWindowCommands, + TimePoint::now().ns() + s2ns(1), transaction.isAutoTimestamp, + transaction.unCachedBuffers, + /*HasListenerCallbacks=*/false, transaction.callbacks, + transaction.id, transaction.mergedTransactionIds); } - struct TransactionInfo : public TransactionState { - TransactionInfo() { - mApplyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); - mIsAutoTimestamp = false; - mId = static_cast<uint64_t>(-1); - } + struct TransactionInfo { + Vector<ComposerState> states; + Vector<DisplayState> displays; + uint32_t flags = 0; + sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); + InputWindowCommands inputWindowCommands; + int64_t desiredPresentTime = 0; + bool isAutoTimestamp = false; + FrameTimelineInfo frameTimelineInfo{}; + std::vector<client_cache_t> unCachedBuffers; + uint64_t id = static_cast<uint64_t>(-1); + std::vector<uint64_t> mergedTransactionIds; + std::vector<ListenerCallbacks> callbacks; }; struct Compositor final : ICompositor { @@ -370,4 +383,4 @@ TEST_F(NotifyExpectedPresentTest, notifyExpectedPresentRenderRateChanged) { } } } -} // namespace android +} // namespace android
\ No newline at end of file diff --git a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h index 13c32bdf08..c5973db109 100644 --- a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h +++ b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h @@ -519,8 +519,18 @@ public: return mFlinger->mTransactionHandler.mPendingTransactionCount.load(); } - auto setTransactionState(TransactionState&& state) { - return mFlinger->setTransactionState(std::move(state)); + auto setTransactionState( + const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states, + Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken, + const InputWindowCommands& inputWindowCommands, int64_t desiredPresentTime, + bool isAutoTimestamp, const std::vector<client_cache_t>& uncacheBuffers, + bool hasListenerCallbacks, std::vector<ListenerCallbacks>& listenerCallbacks, + uint64_t transactionId, const std::vector<uint64_t>& mergedTransactionIds) { + return mFlinger->setTransactionState(frameTimelineInfo, states, displays, flags, applyToken, + inputWindowCommands, desiredPresentTime, + isAutoTimestamp, uncacheBuffers, hasListenerCallbacks, + listenerCallbacks, transactionId, + mergedTransactionIds); } auto setTransactionStateInternal(QueuedTransactionState& transaction) { diff --git a/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp b/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp index 1395fb6af3..69dfcc4a8f 100644 --- a/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp +++ b/services/surfaceflinger/tests/unittests/TransactionApplicationTest.cpp @@ -17,8 +17,6 @@ #undef LOG_TAG #define LOG_TAG "TransactionApplicationTest" -#include <cstdint> - #include <binder/Binder.h> #include <common/test/FlagUtils.h> #include <compositionengine/Display.h> @@ -71,32 +69,38 @@ public: TestableSurfaceFlinger mFlinger; renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine(); - struct TransactionInfo : public TransactionState { - TransactionInfo() { - mApplyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); - mId = static_cast<uint64_t>(-1); - } + struct TransactionInfo { + Vector<ComposerState> states; + Vector<DisplayState> displays; + uint32_t flags = 0; + sp<IBinder> applyToken = IInterface::asBinder(TransactionCompletedListener::getIInstance()); + InputWindowCommands inputWindowCommands; + int64_t desiredPresentTime = 0; + bool isAutoTimestamp = true; + FrameTimelineInfo frameTimelineInfo; + std::vector<client_cache_t> uncacheBuffers; + uint64_t id = static_cast<uint64_t>(-1); + std::vector<uint64_t> mergedTransactionIds; + static_assert(0xffffffffffffffff == static_cast<uint64_t>(-1)); }; - void checkEqual(const TransactionInfo& info, const QueuedTransactionState& state) { - EXPECT_EQ(0u, info.mComposerStates.size()); + void checkEqual(TransactionInfo info, QueuedTransactionState state) { + EXPECT_EQ(0u, info.states.size()); EXPECT_EQ(0u, state.states.size()); - EXPECT_EQ(0u, info.mDisplayStates.size()); + EXPECT_EQ(0u, info.displays.size()); EXPECT_EQ(0u, state.displays.size()); - EXPECT_EQ(info.mFlags, state.flags); - EXPECT_EQ(info.mDesiredPresentTime, state.desiredPresentTime); + EXPECT_EQ(info.flags, state.flags); + EXPECT_EQ(info.desiredPresentTime, state.desiredPresentTime); } void setupSingle(TransactionInfo& transaction, uint32_t flags, int64_t desiredPresentTime, bool isAutoTimestamp, const FrameTimelineInfo& frameTimelineInfo) { mTransactionNumber++; - transaction.mFlags |= flags; - transaction.mDesiredPresentTime = desiredPresentTime; - transaction.mIsAutoTimestamp = isAutoTimestamp; - transaction.mFrameTimelineInfo = frameTimelineInfo; - transaction.mHasListenerCallbacks = mHasListenerCallbacks; - transaction.mListenerCallbacks = mCallbacks; + transaction.flags |= flags; + transaction.desiredPresentTime = desiredPresentTime; + transaction.isAutoTimestamp = isAutoTimestamp; + transaction.frameTimelineInfo = frameTimelineInfo; } void NotPlacedOnTransactionQueue(uint32_t flags) { @@ -107,7 +111,12 @@ public: /*desiredPresentTime*/ systemTime(), /*isAutoTimestamp*/ true, FrameTimelineInfo{}); nsecs_t applicationTime = systemTime(); - mFlinger.setTransactionState(std::move(transaction)); + mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states, + transaction.displays, transaction.flags, + transaction.applyToken, transaction.inputWindowCommands, + transaction.desiredPresentTime, transaction.isAutoTimestamp, + transaction.uncacheBuffers, mHasListenerCallbacks, mCallbacks, + transaction.id, transaction.mergedTransactionIds); // If transaction is synchronous, SF applyTransactionState should time out (5s) wating for // SF to commit the transaction. If this is animation, it should not time out waiting. @@ -129,7 +138,12 @@ public: setupSingle(transaction, flags, /*desiredPresentTime*/ time + s2ns(1), false, FrameTimelineInfo{}); nsecs_t applicationSentTime = systemTime(); - mFlinger.setTransactionState(std::move(transaction)); + mFlinger.setTransactionState(transaction.frameTimelineInfo, transaction.states, + transaction.displays, transaction.flags, + transaction.applyToken, transaction.inputWindowCommands, + transaction.desiredPresentTime, transaction.isAutoTimestamp, + transaction.uncacheBuffers, mHasListenerCallbacks, mCallbacks, + transaction.id, transaction.mergedTransactionIds); nsecs_t returnedTime = systemTime(); EXPECT_LE(returnedTime, applicationSentTime + TRANSACTION_TIMEOUT); @@ -155,7 +169,12 @@ public: /*isAutoTimestamp*/ true, FrameTimelineInfo{}); nsecs_t applicationSentTime = systemTime(); - mFlinger.setTransactionState(std::move(transactionA)); + mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states, + transactionA.displays, transactionA.flags, + transactionA.applyToken, transactionA.inputWindowCommands, + transactionA.desiredPresentTime, transactionA.isAutoTimestamp, + transactionA.uncacheBuffers, mHasListenerCallbacks, mCallbacks, + transactionA.id, transactionA.mergedTransactionIds); // This thread should not have been blocked by the above transaction // (5s is the timeout period that applyTransactionState waits for SF to @@ -165,7 +184,12 @@ public: mFlinger.flushTransactionQueues(); applicationSentTime = systemTime(); - mFlinger.setTransactionState(std::move(transactionB)); + mFlinger.setTransactionState(transactionB.frameTimelineInfo, transactionB.states, + transactionB.displays, transactionB.flags, + transactionB.applyToken, transactionB.inputWindowCommands, + transactionB.desiredPresentTime, transactionB.isAutoTimestamp, + transactionB.uncacheBuffers, mHasListenerCallbacks, mCallbacks, + transactionB.id, transactionB.mergedTransactionIds); // this thread should have been blocked by the above transaction // if this is an animation, this thread should be blocked for 5s @@ -198,7 +222,12 @@ TEST_F(TransactionApplicationTest, AddToPendingQueue) { TransactionInfo transactionA; // transaction to go on pending queue setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ s2ns(1), false, FrameTimelineInfo{}); - mFlinger.setTransactionState(std::move(transactionA)); + mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states, + transactionA.displays, transactionA.flags, transactionA.applyToken, + transactionA.inputWindowCommands, transactionA.desiredPresentTime, + transactionA.isAutoTimestamp, transactionA.uncacheBuffers, + mHasListenerCallbacks, mCallbacks, transactionA.id, + transactionA.mergedTransactionIds); auto& transactionQueue = mFlinger.getTransactionQueue(); ASSERT_FALSE(transactionQueue.isEmpty()); @@ -214,7 +243,12 @@ TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) { TransactionInfo transactionA; // transaction to go on pending queue setupSingle(transactionA, /*flags*/ 0, /*desiredPresentTime*/ s2ns(1), false, FrameTimelineInfo{}); - mFlinger.setTransactionState(std::move(transactionA)); + mFlinger.setTransactionState(transactionA.frameTimelineInfo, transactionA.states, + transactionA.displays, transactionA.flags, transactionA.applyToken, + transactionA.inputWindowCommands, transactionA.desiredPresentTime, + transactionA.isAutoTimestamp, transactionA.uncacheBuffers, + mHasListenerCallbacks, mCallbacks, transactionA.id, + transactionA.mergedTransactionIds); auto& transactionQueue = mFlinger.getTransactionQueue(); ASSERT_FALSE(transactionQueue.isEmpty()); @@ -223,10 +257,12 @@ TEST_F(TransactionApplicationTest, Flush_RemovesFromQueue) { // transaction here (sending a null applyToken to fake it as from a // different process) to re-query and reset the cached expected present time TransactionInfo empty; - empty.mApplyToken = sp<IBinder>(); - empty.mHasListenerCallbacks = mHasListenerCallbacks; - empty.mListenerCallbacks = mCallbacks; - mFlinger.setTransactionState(std::move(empty)); + empty.applyToken = sp<IBinder>(); + mFlinger.setTransactionState(empty.frameTimelineInfo, empty.states, empty.displays, empty.flags, + empty.applyToken, empty.inputWindowCommands, + empty.desiredPresentTime, empty.isAutoTimestamp, + empty.uncacheBuffers, mHasListenerCallbacks, mCallbacks, empty.id, + empty.mergedTransactionIds); // flush transaction queue should flush as desiredPresentTime has // passed @@ -370,9 +406,9 @@ public: const auto kFrameTimelineInfo = FrameTimelineInfo{}; setupSingle(transaction, kFlags, kDesiredPresentTime, kIsAutoTimestamp, kFrameTimelineInfo); - transaction.mApplyToken = applyToken; + transaction.applyToken = applyToken; for (const auto& state : states) { - transaction.mComposerStates.push_back(state); + transaction.states.push_back(state); } return transaction; @@ -383,8 +419,8 @@ public: EXPECT_TRUE(mFlinger.getTransactionQueue().isEmpty()); EXPECT_EQ(0u, mFlinger.getPendingTransactionQueue().size()); std::unordered_set<uint32_t> createdLayers; - for (auto& transaction : transactions) { - for (auto& state : transaction.mComposerStates) { + for (auto transaction : transactions) { + for (auto& state : transaction.states) { auto layerId = static_cast<uint32_t>(state.state.layerId); if (createdLayers.find(layerId) == createdLayers.end()) { mFlinger.addLayer(layerId); @@ -398,8 +434,8 @@ public: for (auto transaction : transactions) { std::vector<ResolvedComposerState> resolvedStates; - resolvedStates.reserve(transaction.mComposerStates.size()); - for (auto& state : transaction.mComposerStates) { + resolvedStates.reserve(transaction.states.size()); + for (auto& state : transaction.states) { ResolvedComposerState resolvedState; resolvedState.state = std::move(state.state); resolvedState.externalTexture = @@ -410,9 +446,15 @@ public: resolvedStates.emplace_back(resolvedState); } - QueuedTransactionState transactionState(std::move(transaction), - std::move(resolvedStates), {}, systemTime(), - getpid(), static_cast<int>(getuid())); + QueuedTransactionState transactionState(transaction.frameTimelineInfo, resolvedStates, + transaction.displays, transaction.flags, + transaction.applyToken, + transaction.inputWindowCommands, + transaction.desiredPresentTime, + transaction.isAutoTimestamp, {}, systemTime(), + mHasListenerCallbacks, mCallbacks, getpid(), + static_cast<int>(getuid()), transaction.id, + transaction.mergedTransactionIds); mFlinger.setTransactionStateInternal(transactionState); } mFlinger.flushTransactionQueues(); diff --git a/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp b/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp index b36ad213c8..d3eec5c6f3 100644 --- a/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp +++ b/services/surfaceflinger/tests/unittests/TransactionProtoParserTest.cpp @@ -66,7 +66,7 @@ TEST(TransactionProtoParserTest, parse) { display.token = nullptr; } display.width = 85; - t1.displays.push_back(display); + t1.displays.add(display); } class TestMapper : public TransactionProtoParser::FlingerDataMapper { diff --git a/services/surfaceflinger/tests/unittests/mock/MockEventThread.h b/services/surfaceflinger/tests/unittests/mock/MockEventThread.h index 3036fec456..cce4d2aba8 100644 --- a/services/surfaceflinger/tests/unittests/mock/MockEventThread.h +++ b/services/surfaceflinger/tests/unittests/mock/MockEventThread.h @@ -55,7 +55,6 @@ public: MOCK_METHOD(void, onHdcpLevelsChanged, (PhysicalDisplayId displayId, int32_t connectedLevel, int32_t maxLevel), (override)); - MOCK_METHOD(void, addBufferStuffedUids, (BufferStuffingMap), (override)); }; } // namespace android::mock diff --git a/services/vibratorservice/Android.bp b/services/vibratorservice/Android.bp index ed03cfc3f5..4b12cc20a4 100644 --- a/services/vibratorservice/Android.bp +++ b/services/vibratorservice/Android.bp @@ -26,6 +26,7 @@ cc_library_shared { srcs: [ "VibratorCallbackScheduler.cpp", + "VibratorController.cpp", "VibratorHalController.cpp", "VibratorHalWrapper.cpp", "VibratorManagerHalController.cpp", @@ -41,17 +42,17 @@ cc_library_shared { }, shared_libs: [ + "android.hardware.vibrator-V3-ndk", "libbinder_ndk", "liblog", "libutils", - "android.hardware.vibrator-V3-ndk", ], cflags: [ "-Wall", "-Werror", - "-Wunused", "-Wunreachable-code", + "-Wunused", ], local_include_dirs: ["include"], diff --git a/services/vibratorservice/VibratorController.cpp b/services/vibratorservice/VibratorController.cpp new file mode 100644 index 0000000000..21924e9f13 --- /dev/null +++ b/services/vibratorservice/VibratorController.cpp @@ -0,0 +1,196 @@ +/* + * Copyright (C) 2025 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. + */ + +#define LOG_TAG "VibratorController" + +#ifndef qDoWithRetries +#define qDoWithRetries(op) doWithRetries(op, __FUNCTION__) +#endif + +#include <aidl/android/hardware/vibrator/IVibrator.h> +#include <android/binder_manager.h> +#include <binder/IServiceManager.h> + +#include <utils/Log.h> + +#include <vibratorservice/VibratorController.h> + +using ::aidl::android::hardware::vibrator::Effect; +using ::aidl::android::hardware::vibrator::EffectStrength; +using ::aidl::android::hardware::vibrator::IVibrator; + +using Status = ::ndk::ScopedAStatus; + +using namespace std::placeholders; + +namespace android { + +namespace vibrator { + +// ------------------------------------------------------------------------------------------------- + +inline bool isStatusUnsupported(const Status& status) { + // STATUS_UNKNOWN_TRANSACTION means the HAL is an older version, so operation is unsupported. + return status.getStatus() == STATUS_UNKNOWN_TRANSACTION || + status.getExceptionCode() == EX_UNSUPPORTED_OPERATION; +} + +inline bool isStatusTransactionFailed(const Status& status) { + // STATUS_UNKNOWN_TRANSACTION means the HAL is an older version, so operation is unsupported. + return status.getStatus() != STATUS_UNKNOWN_TRANSACTION && + status.getExceptionCode() == EX_TRANSACTION_FAILED; +} + +// ------------------------------------------------------------------------------------------------- + +bool VibratorProvider::isDeclared() { + std::lock_guard<std::mutex> lock(mMutex); + if (mIsDeclared.has_value()) { + return *mIsDeclared; + } + + bool isDeclared = AServiceManager_isDeclared(mServiceName.c_str()); + if (!isDeclared) { + ALOGV("Vibrator HAL service not declared."); + } + + mIsDeclared.emplace(isDeclared); + return isDeclared; +} + +std::shared_ptr<IVibrator> VibratorProvider::waitForVibrator() { + if (!isDeclared()) { + return nullptr; + } + + auto vibrator = IVibrator::fromBinder( + ndk::SpAIBinder(AServiceManager_waitForService(mServiceName.c_str()))); + if (vibrator) { + ALOGV("Successfully connected to Vibrator HAL service."); + } else { + ALOGE("Error connecting to declared Vibrator HAL service."); + } + + return vibrator; +} + +std::shared_ptr<IVibrator> VibratorProvider::checkForVibrator() { + if (!isDeclared()) { + return nullptr; + } + + auto vibrator = IVibrator::fromBinder( + ndk::SpAIBinder(AServiceManager_checkService(mServiceName.c_str()))); + if (vibrator) { + ALOGV("Successfully reconnected to Vibrator HAL service."); + } else { + ALOGE("Error reconnecting to declared Vibrator HAL service."); + } + + return vibrator; +} + +// ------------------------------------------------------------------------------------------------- + +bool VibratorController::init() { + if (!mVibratorProvider->isDeclared()) { + return false; + } + std::lock_guard<std::mutex> lock(mMutex); + if (mVibrator == nullptr) { + mVibrator = mVibratorProvider->waitForVibrator(); + } + return mVibratorProvider->isDeclared(); +} + +Status VibratorController::off() { + return qDoWithRetries(std::bind(&IVibrator::off, _1)); +} + +Status VibratorController::setAmplitude(float amplitude) { + return qDoWithRetries(std::bind(&IVibrator::setAmplitude, _1, amplitude)); +} + +Status VibratorController::setExternalControl(bool enabled) { + return qDoWithRetries(std::bind(&IVibrator::setExternalControl, _1, enabled)); +} + +Status VibratorController::alwaysOnEnable(int32_t id, const Effect& effect, + const EffectStrength& strength) { + return qDoWithRetries(std::bind(&IVibrator::alwaysOnEnable, _1, id, effect, strength)); +} + +Status VibratorController::alwaysOnDisable(int32_t id) { + return qDoWithRetries(std::bind(&IVibrator::alwaysOnDisable, _1, id)); +} + +// ------------------------------------------------------------------------------------------------- + +std::shared_ptr<IVibrator> VibratorController::reconnectToVibrator() { + std::lock_guard<std::mutex> lock(mMutex); + mVibrator = mVibratorProvider->checkForVibrator(); + return mVibrator; +} + +Status VibratorController::doWithRetries(const VibratorController::VibratorOp& op, + const char* logLabel) { + if (!init()) { + ALOGV("Skipped %s because Vibrator HAL is not declared", logLabel); + return Status::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE, "IVibrator not declared"); + } + std::shared_ptr<IVibrator> vibrator; + { + std::lock_guard<std::mutex> lock(mMutex); + vibrator = mVibrator; + } + + if (!vibrator) { + ALOGE("Skipped %s because Vibrator HAL is declared but failed to load", logLabel); + return Status::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE, + "IVibrator declared but failed to load"); + } + + auto status = doOnce(vibrator.get(), op, logLabel); + for (int i = 1; i < MAX_ATTEMPTS && isStatusTransactionFailed(status); i++) { + vibrator = reconnectToVibrator(); + if (!vibrator) { + // Failed to reconnect to vibrator HAL after a transaction failed, skip retries. + break; + } + status = doOnce(vibrator.get(), op, logLabel); + } + + return status; +} + +Status VibratorController::doOnce(IVibrator* vibrator, const VibratorController::VibratorOp& op, + const char* logLabel) { + auto status = op(vibrator); + if (!status.isOk()) { + if (isStatusUnsupported(status)) { + ALOGV("Vibrator HAL %s is unsupported: %s", logLabel, status.getMessage()); + } else { + ALOGE("Vibrator HAL %s failed: %s", logLabel, status.getMessage()); + } + } + return status; +} + +// ------------------------------------------------------------------------------------------------- + +}; // namespace vibrator + +}; // namespace android diff --git a/services/vibratorservice/include/vibratorservice/VibratorController.h b/services/vibratorservice/include/vibratorservice/VibratorController.h new file mode 100644 index 0000000000..691c8ae518 --- /dev/null +++ b/services/vibratorservice/include/vibratorservice/VibratorController.h @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2025 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. + */ + +#ifndef ANDROID_OS_VIBRATOR_CONTROLLER_H +#define ANDROID_OS_VIBRATOR_CONTROLLER_H + +#include <aidl/android/hardware/vibrator/IVibrator.h> + +#include <android-base/thread_annotations.h> + +namespace android { + +namespace vibrator { + +// ------------------------------------------------------------------------------------------------- + +/* Provider for IVibrator HAL service instances. */ +class VibratorProvider { +public: + using IVibrator = ::aidl::android::hardware::vibrator::IVibrator; + + VibratorProvider() : mServiceName(std::string(IVibrator::descriptor) + "/default") {} + virtual ~VibratorProvider() = default; + + /* Returns true if vibrator HAL service is declared in the device, false otherwise. */ + virtual bool isDeclared(); + + /* Connects to vibrator HAL, possibly waiting for the declared service to become available. */ + virtual std::shared_ptr<IVibrator> waitForVibrator(); + + /* Connects to vibrator HAL if declared and available, without waiting. */ + virtual std::shared_ptr<IVibrator> checkForVibrator(); + +private: + std::mutex mMutex; + const std::string mServiceName; + std::optional<bool> mIsDeclared GUARDED_BY(mMutex); +}; + +// ------------------------------------------------------------------------------------------------- + +/* Controller for Vibrator HAL handle. + * This relies on VibratorProvider to connect to the underlying Vibrator HAL service and reconnects + * after each transaction failed call. This also ensures connecting to the service is thread-safe. + */ +class VibratorController { +public: + using Effect = ::aidl::android::hardware::vibrator::Effect; + using EffectStrength = ::aidl::android::hardware::vibrator::EffectStrength; + using IVibrator = ::aidl::android::hardware::vibrator::IVibrator; + using Status = ::ndk::ScopedAStatus; + using VibratorOp = std::function<Status(IVibrator*)>; + + VibratorController() : VibratorController(std::make_shared<VibratorProvider>()) {} + VibratorController(std::shared_ptr<VibratorProvider> vibratorProvider) + : mVibratorProvider(std::move(vibratorProvider)), mVibrator(nullptr) {} + virtual ~VibratorController() = default; + + /* Connects HAL service, possibly waiting for the declared service to become available. + * This will automatically be called at the first API usage if it was not manually called + * beforehand. Call this manually during the setup phase to avoid slowing the first API call. + * Returns true if HAL service is declared, false otherwise. + */ + bool init(); + + /* Turn vibrator off. */ + Status off(); + + /* Set vibration amplitude in [0,1]. */ + Status setAmplitude(float amplitude); + + /* Enable/disable external control. */ + Status setExternalControl(bool enabled); + + /* Enable always-on for given id, with given effect and strength. */ + Status alwaysOnEnable(int32_t id, const Effect& effect, const EffectStrength& strength); + + /* Disable always-on for given id. */ + Status alwaysOnDisable(int32_t id); + +private: + /* Max number of attempts to perform an operation when it fails with transaction error. */ + static constexpr int MAX_ATTEMPTS = 2; + + std::mutex mMutex; + std::shared_ptr<VibratorProvider> mVibratorProvider; + std::shared_ptr<IVibrator> mVibrator GUARDED_BY(mMutex); + + /* Reconnects HAL service without waiting for the service to become available. */ + std::shared_ptr<IVibrator> reconnectToVibrator(); + + /* Perform given operation on HAL with retries on transaction failures. */ + Status doWithRetries(const VibratorOp& op, const char* logLabel); + + /* Perform given operation on HAL with logs for error/unsupported results. */ + static Status doOnce(IVibrator* vibrator, const VibratorOp& op, const char* logLabel); +}; + +// ------------------------------------------------------------------------------------------------- + +}; // namespace vibrator + +}; // namespace android + +#endif // ANDROID_OS_VIBRATOR_CONTROLLER_H diff --git a/services/vibratorservice/include/vibratorservice/VibratorHalController.h b/services/vibratorservice/include/vibratorservice/VibratorHalController.h index a1cb3fad35..9b3202bc60 100644 --- a/services/vibratorservice/include/vibratorservice/VibratorHalController.h +++ b/services/vibratorservice/include/vibratorservice/VibratorHalController.h @@ -14,6 +14,8 @@ * limitations under the License. */ +// TODO(b/308452413): remove this file once android.os.vibrator.remove_hidl_support is removed + #ifndef ANDROID_OS_VIBRATORHALCONTROLLER_H #define ANDROID_OS_VIBRATORHALCONTROLLER_H diff --git a/services/vibratorservice/include/vibratorservice/VibratorHalWrapper.h b/services/vibratorservice/include/vibratorservice/VibratorHalWrapper.h index 065227861d..68568d4163 100644 --- a/services/vibratorservice/include/vibratorservice/VibratorHalWrapper.h +++ b/services/vibratorservice/include/vibratorservice/VibratorHalWrapper.h @@ -14,6 +14,8 @@ * limitations under the License. */ +// TODO(b/308452413): remove this file once android.os.vibrator.remove_hidl_support is removed + #ifndef ANDROID_OS_VIBRATORHALWRAPPER_H #define ANDROID_OS_VIBRATORHALWRAPPER_H diff --git a/services/vibratorservice/test/Android.bp b/services/vibratorservice/test/Android.bp index 038248e636..92c6286513 100644 --- a/services/vibratorservice/test/Android.bp +++ b/services/vibratorservice/test/Android.bp @@ -27,6 +27,7 @@ cc_test { test_suites: ["device-tests"], srcs: [ "VibratorCallbackSchedulerTest.cpp", + "VibratorControllerTest.cpp", "VibratorHalControllerTest.cpp", "VibratorHalWrapperAidlTest.cpp", "VibratorManagerHalControllerTest.cpp", @@ -39,12 +40,12 @@ cc_test { "-Wextra", ], shared_libs: [ + "android.hardware.vibrator-V3-ndk", "libbase", "libbinder_ndk", "liblog", - "libvibratorservice", "libutils", - "android.hardware.vibrator-V3-ndk", + "libvibratorservice", ], static_libs: [ "libgmock", diff --git a/services/vibratorservice/test/VibratorControllerTest.cpp b/services/vibratorservice/test/VibratorControllerTest.cpp new file mode 100644 index 0000000000..11ec75bc36 --- /dev/null +++ b/services/vibratorservice/test/VibratorControllerTest.cpp @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2025 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. + */ + +#define LOG_TAG "VibratorControllerTest" + +#include <aidl/android/hardware/vibrator/IVibrator.h> + +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +#include <utils/Log.h> +#include <thread> + +#include <vibratorservice/VibratorController.h> + +#include "test_mocks.h" +#include "test_utils.h" + +using ::aidl::android::hardware::vibrator::Effect; +using ::aidl::android::hardware::vibrator::EffectStrength; +using ::aidl::android::hardware::vibrator::IVibrator; + +using namespace android; +using namespace testing; + +const auto kReturnOk = []() { return ndk::ScopedAStatus::ok(); }; +const auto kReturnUnsupported = []() { + return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); +}; +const auto kReturnTransactionFailed = []() { + return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); +}; +const auto kReturnUnknownTransaction = []() { + return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_TRANSACTION); +}; +const auto kReturnIllegalArgument = []() { + return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); +}; + +// ------------------------------------------------------------------------------------------------- + +/* Provides mock IVibrator instance for testing. */ +class FakeVibratorProvider : public vibrator::VibratorProvider { +public: + FakeVibratorProvider() + : mIsDeclared(true), + mMockVibrator(ndk::SharedRefBase::make<StrictMock<vibrator::MockIVibrator>>()), + mConnectCount(0), + mReconnectCount(0) {} + virtual ~FakeVibratorProvider() = default; + + bool isDeclared() override { return mIsDeclared; } + + std::shared_ptr<IVibrator> waitForVibrator() override { + mConnectCount++; + return mIsDeclared ? mMockVibrator : nullptr; + } + + std::shared_ptr<IVibrator> checkForVibrator() override { + mReconnectCount++; + return mIsDeclared ? mMockVibrator : nullptr; + } + + void setDeclared(bool isDeclared) { mIsDeclared = isDeclared; } + + int32_t getConnectCount() { return mConnectCount; } + + int32_t getReconnectCount() { return mReconnectCount; } + + std::shared_ptr<StrictMock<vibrator::MockIVibrator>> getMockVibrator() { return mMockVibrator; } + +private: + bool mIsDeclared; + std::shared_ptr<StrictMock<vibrator::MockIVibrator>> mMockVibrator; + int32_t mConnectCount; + int32_t mReconnectCount; +}; + +// ------------------------------------------------------------------------------------------------- + +class VibratorControllerTest : public Test { +public: + void SetUp() override { + mProvider = std::make_shared<FakeVibratorProvider>(); + mController = std::make_unique<vibrator::VibratorController>(mProvider); + ASSERT_NE(mController, nullptr); + } + +protected: + std::shared_ptr<FakeVibratorProvider> mProvider = nullptr; + std::unique_ptr<vibrator::VibratorController> mController = nullptr; +}; + +// ------------------------------------------------------------------------------------------------- + +TEST_F(VibratorControllerTest, TestInitServiceDeclared) { + ASSERT_TRUE(mController->init()); + ASSERT_EQ(1, mProvider->getConnectCount()); + ASSERT_EQ(0, mProvider->getReconnectCount()); + + // Noop when wrapper was already initialized. + ASSERT_TRUE(mController->init()); + ASSERT_EQ(1, mProvider->getConnectCount()); + ASSERT_EQ(0, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestInitServiceNotDeclared) { + mProvider->setDeclared(false); + + ASSERT_FALSE(mController->init()); + ASSERT_EQ(0, mProvider->getConnectCount()); + ASSERT_EQ(0, mProvider->getReconnectCount()); + + ASSERT_FALSE(mController->init()); + ASSERT_EQ(0, mProvider->getConnectCount()); + ASSERT_EQ(0, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestFirstCallTriggersInit) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(1)) + .WillRepeatedly(kReturnOk); + + auto status = mController->off(); + ASSERT_TRUE(status.isOk()); + ASSERT_EQ(1, mProvider->getConnectCount()); +} + +TEST_F(VibratorControllerTest, TestSuccessfulResultDoesNotRetry) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(1)) + .WillRepeatedly(kReturnOk); + + auto status = mController->off(); + ASSERT_TRUE(status.isOk()); + ASSERT_EQ(0, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestUnsupportedOperationResultDoesNotRetry) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(1)) + .WillRepeatedly(kReturnUnsupported); + + auto status = mController->off(); + ASSERT_FALSE(status.isOk()); + ASSERT_EQ(0, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestUnknownTransactionResultDoesNotRetry) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(1)) + .WillRepeatedly(kReturnUnknownTransaction); + + auto status = mController->off(); + ASSERT_FALSE(status.isOk()); + ASSERT_EQ(0, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestOperationFailedDoesNotRetry) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(1)) + .WillRepeatedly(kReturnIllegalArgument); + + auto status = mController->off(); + ASSERT_FALSE(status.isOk()); + ASSERT_EQ(0, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestTransactionFailedRetriesOnlyOnce) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(2)) + .WillRepeatedly(kReturnTransactionFailed); + + auto status = mController->off(); + ASSERT_FALSE(status.isOk()); + ASSERT_EQ(1, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestTransactionFailedThenSucceedsReturnsSuccessAfterRetries) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(2)) + .WillOnce(kReturnTransactionFailed) + .WillRepeatedly(kReturnOk); + + auto status = mController->off(); + ASSERT_TRUE(status.isOk()); + ASSERT_EQ(1, mProvider->getReconnectCount()); +} + +TEST_F(VibratorControllerTest, TestOff) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), off()) + .Times(Exactly(1)) + .WillRepeatedly(kReturnOk); + + auto status = mController->off(); + ASSERT_TRUE(status.isOk()); +} + +TEST_F(VibratorControllerTest, TestSetAmplitude) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), setAmplitude(Eq(0.1f))) + .Times(Exactly(1)) + .WillRepeatedly(kReturnOk); + EXPECT_CALL(*mProvider->getMockVibrator().get(), setAmplitude(Eq(0.2f))) + .Times(Exactly(1)) + .WillRepeatedly(kReturnIllegalArgument); + + ASSERT_TRUE(mController->setAmplitude(0.1f).isOk()); + ASSERT_FALSE(mController->setAmplitude(0.2f).isOk()); +} + +TEST_F(VibratorControllerTest, TestSetExternalControl) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), setExternalControl(Eq(false))) + .Times(Exactly(1)) + .WillRepeatedly(kReturnOk); + EXPECT_CALL(*mProvider->getMockVibrator().get(), setExternalControl(Eq(true))) + .Times(Exactly(1)) + .WillRepeatedly(kReturnIllegalArgument); + + ASSERT_TRUE(mController->setExternalControl(false).isOk()); + ASSERT_FALSE(mController->setExternalControl(true).isOk()); +} + +TEST_F(VibratorControllerTest, TestAlwaysOnEnable) { + EXPECT_CALL(*mProvider->getMockVibrator().get(), + alwaysOnEnable(Eq(1), Eq(Effect::CLICK), Eq(EffectStrength::LIGHT))) + .Times(Exactly(1)) + .WillRepeatedly(kReturnOk); + EXPECT_CALL(*mProvider->getMockVibrator().get(), + alwaysOnEnable(Eq(2), Eq(Effect::TICK), Eq(EffectStrength::MEDIUM))) + .Times(Exactly(1)) + .WillRepeatedly(kReturnIllegalArgument); + + ASSERT_TRUE(mController->alwaysOnEnable(1, Effect::CLICK, EffectStrength::LIGHT).isOk()); + ASSERT_FALSE(mController->alwaysOnEnable(2, Effect::TICK, EffectStrength::MEDIUM).isOk()); +} |