| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BLIMP_ENGINE_RENDERER_FRAME_SCHEDULER_H_ | |
| 6 #define BLIMP_ENGINE_RENDERER_FRAME_SCHEDULER_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/timer/timer.h" | |
| 13 | |
| 14 namespace blimp { | |
| 15 namespace engine { | |
| 16 | |
| 17 class FrameSchedulerClient { | |
| 18 public: | |
| 19 virtual ~FrameSchedulerClient() {} | |
| 20 | |
| 21 // Used to notify the SchedulerClient to start the requested frame update. | |
| 22 // Calling this method marks the completion of the frame update request made | |
| 23 // to the scheduler. Any calls to schedule a frame after this will result in | |
| 24 // a new frame update. | |
| 25 // The frame update must be run synchronously, and if it results in any | |
| 26 // content changes sent to the blimp client, the scheduler must be informed | |
| 27 // using DidSendFrameUpdateToClient. | |
| 28 virtual void StartFrameUpdate() = 0; | |
| 29 }; | |
| 30 | |
| 31 // Responsible for scheduling frame updates sent to the client. | |
| 32 // The Scheduler uses the state of frames sent to the client and the | |
| 33 // acknowledgements for these frames from the client to make decisions regarding | |
| 34 // when a frame should be produced on the engine. | |
| 35 // The caller provides a FrameSchedulerClient to be notified when a frame update | |
| 36 // should be produced. The FrameSchedulerClient must outlive this class. | |
| 37 class FrameScheduler { | |
| 38 public: | |
| 39 FrameScheduler(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 40 FrameSchedulerClient* client); | |
| 41 virtual ~FrameScheduler(); | |
| 42 | |
| 43 // Called when the |client| wants to start a frame update on the engine. | |
| 44 // |client->StartFrameUpdate() will be called back asynchronously when the | |
| 45 // scheduler is ready for the next frame to be started. | |
| 46 void ScheduleFrameUpdate(); | |
| 47 | |
| 48 // Called when a frame update is sent to the client. This must be called only | |
| 49 // when the |client| is producing a frame update in | |
| 50 // FrameSchedulerClient::StartFrameUpdate and must be followed with a | |
| 51 // DidReceiveFrameUpdateAck when the frame sent is ack-ed by the client. | |
| 52 void DidSendFrameUpdateToClient(); | |
| 53 | |
| 54 // Called when an Ack is received for a frame sent to the client. | |
| 55 void DidReceiveFrameUpdateAck(); | |
| 56 | |
| 57 bool needs_frame_update() const { return needs_frame_update_; } | |
| 58 | |
| 59 void set_frame_delay_for_testing(base::TimeDelta frame_delay) { | |
| 60 frame_delay_ = frame_delay; | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 void ScheduleFrameUpdateIfNecessary(); | |
| 65 | |
| 66 // Returns true if a frame update can be started. The Scheduler can not | |
| 67 // produce new frames either if there is no request for frames pending or we | |
| 68 // are waiting for an ack for a frame previously sent to the client. | |
| 69 bool ShouldProduceFrameUpdates() const; | |
| 70 | |
| 71 void StartFrameUpdate(); | |
| 72 | |
| 73 // Set to true if the |client_| has requested us to schedule frames. | |
| 74 bool needs_frame_update_ = false; | |
| 75 | |
| 76 // Set to true if a frame update was sent to the client and the ack is | |
| 77 // pending. | |
| 78 bool frame_ack_pending_ = false; | |
| 79 | |
| 80 bool in_frame_update_ = false; | |
| 81 | |
| 82 // The time at which the next main frame update can be run. | |
| 83 base::TimeTicks next_frame_time_; | |
| 84 | |
| 85 // The delay to use between consecutive frames. | |
| 86 base::TimeDelta frame_delay_; | |
| 87 | |
| 88 base::OneShotTimer frame_tick_timer_; | |
| 89 | |
| 90 FrameSchedulerClient* client_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(FrameScheduler); | |
| 93 }; | |
| 94 | |
| 95 } // namespace engine | |
| 96 } // namespace blimp | |
| 97 | |
| 98 #endif // BLIMP_ENGINE_RENDERER_FRAME_SCHEDULER_H_ | |
| OLD | NEW |