summaryrefslogtreecommitdiffstats
path: root/gfx/layers/ipc/CompositorVsyncScheduler.h
blob: 8363450043739a0037647a24b253b596aadcfe85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_layers_CompositorVsyncScheduler_h
#define mozilla_layers_CompositorVsyncScheduler_h

#include <stdint.h>  // for uint64_t

#include "mozilla/Attributes.h"  // for override
#include "mozilla/Monitor.h"     // for Monitor
#include "mozilla/RefPtr.h"      // for RefPtr
#include "mozilla/TimeStamp.h"   // for TimeStamp
#include "mozilla/gfx/Point.h"   // for IntSize
#include "mozilla/layers/SampleTime.h"
#include "mozilla/webrender/webrender_ffi.h"
#include "mozilla/VsyncDispatcher.h"
#include "mozilla/widget/CompositorWidget.h"
#include "nsISupportsImpl.h"

namespace mozilla {

class CancelableRunnable;
class Runnable;

namespace gfx {
class DrawTarget;
}  // namespace gfx

namespace layers {

class CompositorVsyncSchedulerOwner;

/**
 * Manages the vsync (de)registration and tracking on behalf of the
 * compositor when it need to paint.
 * Turns vsync notifications into scheduled composites.
 **/
class CompositorVsyncScheduler {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorVsyncScheduler)

 public:
  CompositorVsyncScheduler(CompositorVsyncSchedulerOwner* aVsyncSchedulerOwner,
                           widget::CompositorWidget* aWidget);

  /**
   * Notify this class of a vsync. This will trigger a composite if one is
   * needed. This must be called from the vsync dispatch thread.
   */
  void NotifyVsync(const VsyncEvent& aVsync);

  /**
   * Do cleanup. This must be called on the compositor thread.
   */
  void Destroy();

  /**
   * Notify this class that a composition is needed. This will trigger a
   * composition soon (likely at the next vsync). This must be called on the
   * compositor thread.
   */
  void ScheduleComposition(wr::RenderReasons aReasons);

  /**
   * Cancel any composite task that has been scheduled but hasn't run yet.
   *
   * Returns the render reasons of the canceled task if any.
   */
  wr::RenderReasons CancelCurrentCompositeTask();

  /**
   * Check if a composite is pending. This is generally true between a call
   * to ScheduleComposition() and the time the composite happens.
   */
  bool NeedsComposite();

  /**
   * Force a composite to happen right away, without waiting for the next vsync.
   * This must be called on the compositor thread.
   */
  void ForceComposeToTarget(wr::RenderReasons aReasons,
                            gfx::DrawTarget* aTarget,
                            const gfx::IntRect* aRect);

  /**
   * If a composite is pending, force it to trigger right away. This must be
   * called on the compositor thread. Returns true if there was a composite
   * flushed.
   */
  bool FlushPendingComposite();

  /**
   * Return the vsync timestamp of the last or ongoing composite. Must be called
   * on the compositor thread.
   */
  const SampleTime& GetLastComposeTime() const;

  /**
   * Return the vsync timestamp and id of the most recently received
   * vsync event. Must be called on the compositor thread.
   */
  const TimeStamp& GetLastVsyncTime() const;
  const TimeStamp& GetLastVsyncOutputTime() const;
  const VsyncId& GetLastVsyncId() const;

  /**
   * Update LastCompose TimeStamp to current timestamp.
   * The function is typically used when composition is handled outside the
   * CompositorVsyncScheduler.
   */
  void UpdateLastComposeTime();

 private:
  virtual ~CompositorVsyncScheduler();

  // Post a task to run Composite() on the compositor thread, if there isn't
  // such a task already queued. Can be called from any thread.
  void PostCompositeTask(const VsyncEvent& aVsyncEvent,
                         wr::RenderReasons aReasons);

  // Post a task to run DispatchVREvents() on the VR thread, if there isn't
  // such a task already queued. Can be called from any thread.
  void PostVRTask(TimeStamp aTimestamp);

  /**
   * Cancel any VR task that has been scheduled but hasn't run yet.
   */
  void CancelCurrentVRTask();

  // This gets run at vsync time and "does" a composite (which really means
  // update internal state and call the owner to do the composite).
  void Composite(const VsyncEvent& aVsyncEvent, wr::RenderReasons aReasons);

  void ObserveVsync();
  void UnobserveVsync();

  void DispatchVREvents(TimeStamp aVsyncTimestamp);

  class Observer final : public VsyncObserver {
    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorVsyncScheduler::Observer,
                                          override)

   public:
    explicit Observer(CompositorVsyncScheduler* aOwner);
    void NotifyVsync(const VsyncEvent& aVsync) override;
    void Destroy();

   private:
    virtual ~Observer();

    Mutex mMutex MOZ_UNANNOTATED;
    // Hold raw pointer to avoid mutual reference.
    CompositorVsyncScheduler* mOwner;
  };

  CompositorVsyncSchedulerOwner* mVsyncSchedulerOwner;
  SampleTime mLastComposeTime;
  TimeStamp mLastVsyncTime;
  TimeStamp mLastVsyncOutputTime;
  VsyncId mLastVsyncId;

  bool mAsapScheduling;
  bool mIsObservingVsync;
  // Accessed on the compositor thread.
  wr::RenderReasons mRendersDelayedByVsyncReasons;
  TimeStamp mCompositeRequestedAt;
  int32_t mVsyncNotificationsSkipped;
  widget::CompositorWidget* mWidget;
  RefPtr<CompositorVsyncScheduler::Observer> mVsyncObserver;

  mozilla::Monitor mCurrentCompositeTaskMonitor MOZ_UNANNOTATED;
  RefPtr<CancelableRunnable> mCurrentCompositeTask;
  // Accessed on multiple threads, guarded by mCurrentCompositeTaskMonitor.
  wr::RenderReasons mCurrentCompositeTaskReasons;

  mozilla::Monitor mCurrentVRTaskMonitor MOZ_UNANNOTATED;
  RefPtr<CancelableRunnable> mCurrentVRTask;
};

}  // namespace layers
}  // namespace mozilla

#endif  // mozilla_layers_CompositorVsyncScheduler_h