/* * 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 #include #include #include "EventThread.h" #include "TracedOrdinal.h" #include "VSyncDispatch.h" namespace android { struct ICompositor; template class Task : public MessageHandler { template friend auto makeTask(G&&); template friend sp> sp>::make(Args&&... args); 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 = sp>::make(std::forward(f)); return std::make_pair(task, task->mTask.get_future()); } class MessageQueue { public: virtual ~MessageQueue() = default; virtual void initVsyncInternal(std::shared_ptr, frametimeline::TokenManager&, std::chrono::nanoseconds workDuration) = 0; virtual void destroyVsync() = 0; virtual void setDuration(std::chrono::nanoseconds workDuration) = 0; virtual void waitMessage() = 0; virtual void postMessage(sp&&) = 0; virtual void postMessageDelayed(sp&&, nsecs_t uptimeDelay) = 0; virtual void scheduleConfigure() = 0; virtual void scheduleFrame() = 0; virtual std::optional getScheduledFrameResult() const = 0; }; namespace impl { class MessageQueue : public android::MessageQueue { protected: class Handler : public MessageHandler { MessageQueue& mQueue; std::atomic_bool mFramePending = false; std::atomic mVsyncId; std::atomic mExpectedVsyncTime; public: explicit Handler(MessageQueue& queue) : mQueue(queue) {} void handleMessage(const Message& message) override; virtual TimePoint getExpectedVsyncTime() const { return mExpectedVsyncTime.load(); } virtual bool isFramePending() const; virtual void dispatchFrame(VsyncId, TimePoint expectedVsyncTime); }; friend class Handler; // For tests. MessageQueue(ICompositor&, sp); void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime); void onNewVsyncSchedule(std::shared_ptr) EXCLUDES(mVsync.mutex); private: virtual void onFrameSignal(ICompositor&, VsyncId, TimePoint expectedVsyncTime) = 0; ICompositor& mCompositor; const sp mLooper; const sp mHandler; struct Vsync { frametimeline::TokenManager* tokenManager = nullptr; mutable std::mutex mutex; std::unique_ptr registration GUARDED_BY(mutex); TracedOrdinal workDuration GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)}; TimePoint lastCallbackTime GUARDED_BY(mutex); std::optional scheduledFrameTimeOpt GUARDED_BY(mutex); TracedOrdinal value = {"VSYNC-sf", 0}; }; Vsync mVsync; // Returns the old registration so it can be destructed outside the lock to // avoid deadlock. std::unique_ptr onNewVsyncScheduleLocked( std::shared_ptr) REQUIRES(mVsync.mutex); public: explicit MessageQueue(ICompositor&); void initVsyncInternal(std::shared_ptr, frametimeline::TokenManager&, std::chrono::nanoseconds workDuration) override; void destroyVsync() override; void setDuration(std::chrono::nanoseconds workDuration) override; void waitMessage() override; void postMessage(sp&&) override; void postMessageDelayed(sp&&, nsecs_t uptimeDelay) override; void scheduleConfigure() override; void scheduleFrame() override; std::optional getScheduledFrameResult() const override; }; } // namespace impl } // namespace android