File: compositor_lock.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,207 bytes parent folder | download | duplicates (8)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_COMPOSITOR_COMPOSITOR_LOCK_H_
#define UI_COMPOSITOR_COMPOSITOR_LOCK_H_

#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "ui/compositor/compositor_export.h"

namespace ui {

class Compositor;
class CompositorLock;

// Implemented by clients which take compositor lock. Used to notify the client
// when their lock times out.
class CompositorLockClient {
 public:
  virtual ~CompositorLockClient() {}

  // Called if the CompositorLock ends before being destroyed due to timeout.
  virtual void CompositorLockTimedOut() = 0;
};

// A helper class used to manage compositor locks. Should be created/used by
// classes which want to provide out compositor locking.
class COMPOSITOR_EXPORT CompositorLockManager {
 public:
  CompositorLockManager(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  ~CompositorLockManager();

  // Creates a compositor lock. If the timeout is null, then no timeout is used.
  // Runs `release_callback` on timeout or when the returned `CompositorLock`
  // is destroyed.
  std::unique_ptr<CompositorLock> GetCompositorLock(
      CompositorLockClient* client,
      base::TimeDelta timeout,
      base::OnceClosure release_callback);

  void set_allow_locks_to_extend_timeout(bool allowed) {
    allow_locks_to_extend_timeout_ = allowed;
  }

  bool IsLocked() const { return !active_locks_.empty(); }

  void TimeoutLocksForTesting() { TimeoutLocks(); }

 private:
  friend class CompositorLock;

  // Causes all active CompositorLocks to be timed out.
  void TimeoutLocks();

  // Called to perform the unlock operation.
  void RemoveCompositorLock(CompositorLock*);

  // The TaskRunner on which timeouts are run.
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  // The estimated time that the locks will timeout.
  base::TimeTicks scheduled_timeout_;
  // If true, the |scheduled_timeout_| might be recalculated and extended.
  bool allow_locks_to_extend_timeout_ = false;
  // The set of locks that are held externally.
  std::vector<raw_ptr<CompositorLock, VectorExperimental>> active_locks_;

  base::WeakPtrFactory<CompositorLockManager> weak_ptr_factory_{this};
  base::WeakPtrFactory<CompositorLockManager> lock_timeout_weak_ptr_factory_{
      this};
};

// This class represents a lock on the compositor, that can be used to prevent
// commits to the compositor tree while we're waiting for an asynchronous
// event. The typical use case is when waiting for a renderer to produce a frame
// at the right size. The caller keeps a reference on this object, and drops the
// reference once it desires to release the lock.
// By default, the lock will be cancelled after a short timeout to ensure
// responsiveness of the UI, so the compositor tree should be kept in a
// "reasonable" state while the lock is held. The timeout length, or no timeout,
// can be requested at the time the lock is created.
// Don't instantiate this class directly, use Compositor::GetCompositorLock.
class COMPOSITOR_EXPORT CompositorLock {
 public:
  // The |client| is informed about events from the CompositorLock. The
  // |delegate| is used to perform actual unlocking. If |timeout| is zero then
  // no timeout is scheduled, else a timeout is scheduled on the |task_runner|.
  explicit CompositorLock(CompositorLockClient* client,
                          base::WeakPtr<CompositorLockManager> manager,
                          base::OnceClosure release_callback);

  CompositorLock(const CompositorLock&) = delete;
  CompositorLock& operator=(const CompositorLock&) = delete;

  ~CompositorLock();

 private:
  friend class CompositorLockManager;

  // Causes the CompositorLock to end due to a timeout.
  void TimeoutLock();

  const raw_ptr<CompositorLockClient> client_;
  base::OnceClosure release_callback_;
  base::WeakPtr<CompositorLockManager> manager_;
};

}  // namespace ui

#endif  // UI_COMPOSITOR_COMPOSITOR_LOCK_H_