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

ash / utility / occlusion_tracker_pauser.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_UTILITY_OCCLUSION_TRACKER_PAUSER_H_
#define ASH_UTILITY_OCCLUSION_TRACKER_PAUSER_H_

#include <memory>

#include "ash/ash_export.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/aura/window_occlusion_tracker.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/compositor_observer.h"

namespace ash {

// A utility class to pause aura's WindowOcclusionTracker until animations are
// finished on all compositors.
class ASH_EXPORT OcclusionTrackerPauser : public ui::CompositorObserver {
 public:
  OcclusionTrackerPauser();
  OcclusionTrackerPauser(const OcclusionTrackerPauser&) = delete;
  OcclusionTrackerPauser& operator=(const OcclusionTrackerPauser&) = delete;
  ~OcclusionTrackerPauser() override;

  // Pause the occlusion tracker until all new animations added after this are
  // finished. If the timeout is elapsed before all new animations are finished,
  // the pause will be unpaused. This function requires there to be at least one
  // animation running, or at least one animation about to run as a
  // precondition.
  void PauseUntilAnimationsEnd(base::TimeDelta timeout);

  // ui::CompositorObserver:
  void OnFirstAnimationStarted(ui::Compositor* compositor) override;
  void OnFirstNonAnimatedFrameStarted(ui::Compositor* compositor) override;
  void OnCompositingShuttingDown(ui::Compositor* compositor) override;

 private:
  void Pause(ui::Compositor* compositor);
  void OnFinish(ui::Compositor* compositor);
  void Shutdown(bool timed_out);

  base::OneShotTimer timer_;
  base::ScopedMultiSourceObservation<ui::Compositor, ui::CompositorObserver>
      observations_{this};

  // Keeps track of compositors that are animating. We can unpause when this is
  // empty.
  base::flat_set<raw_ptr<ui::Compositor, CtnExperimental>>
      animating_compositors_;

  std::unique_ptr<aura::WindowOcclusionTracker::ScopedPause> scoped_pause_;
};

}  // namespace ash

#endif  // ASH_UTILITY_OCCLUSION_TRACKER_PAUSER_H_