/* * Copyright (C) 2009 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 #include #include #include #include #include #include #include #include #include "EventThread.h" #include "TracedOrdinal.h" #include "VSyncDispatch.h" namespace android { class SurfaceFlinger; template class Task : public MessageHandler { template friend auto makeTask(G&&); explicit Task(F&& f) : mTask(std::move(f)) {} void handleMessage(const Message&) override { mTask(); } using T = std::invoke_result_t; std::packaged_task mTask; }; template inline auto makeTask(F&& f) { sp> task = new Task(std::move(f)); return std::make_pair(task, task->mTask.get_future()); } class MessageQueue { public: enum { INVALIDATE = 0, REFRESH = 1, }; virtual ~MessageQueue() = default; virtual void init(const sp& flinger) = 0; virtual void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&, std::chrono::nanoseconds workDuration) = 0; virtual void setDuration(std::chrono::nanoseconds workDuration) = 0; virtual void setInjector(sp) = 0; virtual void waitMessage() = 0; virtual void postMessage(sp&&) = 0; virtual void invalidate() = 0; virtual void refresh() = 0; virtual std::optional nextExpectedInvalidate() = 0; }; // --------------------------------------------------------------------------- namespace impl { class MessageQueue : public android::MessageQueue { protected: class Handler : public MessageHandler { enum : uint32_t { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 }; MessageQueue& mQueue; std::atomic mEventMask; std::atomic mVsyncId; std::atomic mExpectedVSyncTime; public: explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {} void handleMessage(const Message& message) override; virtual void dispatchRefresh(); virtual void dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp); virtual bool invalidatePending(); }; friend class Handler; sp mFlinger; sp mLooper; struct Vsync { frametimeline::TokenManager* tokenManager = nullptr; std::unique_ptr registration; std::mutex mutex; TracedOrdinal workDuration GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)}; std::chrono::nanoseconds lastCallbackTime GUARDED_BY(mutex) = std::chrono::nanoseconds{0}; bool scheduled GUARDED_BY(mutex) = false; std::optional expectedWakeupTime GUARDED_BY(mutex); TracedOrdinal value = {"VSYNC-sf", 0}; }; struct Injector { gui::BitTube tube; std::mutex mutex; sp connection GUARDED_BY(mutex); }; Vsync mVsync; Injector mInjector; sp mHandler; void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime); void injectorCallback(); public: ~MessageQueue() override = default; void init(const sp& flinger) override; void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&, std::chrono::nanoseconds workDuration) override; void setDuration(std::chrono::nanoseconds workDuration) override; void setInjector(sp) override; void waitMessage() override; void postMessage(sp&&) override; // sends INVALIDATE message at next VSYNC void invalidate() override; // sends REFRESH message at next VSYNC void refresh() override; std::optional nextExpectedInvalidate() override; }; } // namespace impl } // namespace android