File: ca_transaction_observer.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 (99 lines) | stat: -rw-r--r-- 3,753 bytes parent folder | download | duplicates (9)
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
// Copyright 2018 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_ACCELERATED_WIDGET_MAC_CA_TRANSACTION_OBSERVER_H_
#define UI_ACCELERATED_WIDGET_MAC_CA_TRANSACTION_OBSERVER_H_

#include <set>

#include "base/functional/callback.h"
#include "base/no_destructor.h"
#include "base/observer_list.h"
#include "base/time/time.h"

#include "ui/accelerated_widget_mac/accelerated_widget_mac_export.h"

namespace ui {

// CATransactionCoordinator is an interface to undocumented macOS APIs which
// run callbacks at different stages of committing a CATransaction to the
// window server. There is no guarantee that it will call registered observers
// at all.
//
// - Pre-commit: After all outstanding CATransactions have committed and after
//   layout, but before the new layer tree has been sent to the window server.
//   Safe to block here waiting for drawing/layout in other processes (but
//   you're on the main thread, so be reasonable).
//
// - Post-commit: After the new layer tree has been sent to the server but
//   before the transaction has been finalized. In post-commit, the screen area
//   occupied by the window and its shadow are frozen, so it's important to
//   block as briefly as possible (well under a frame) or else artifacts will
//   be visible around affected windows if screen content is changing behind
//   them (think resizing a browser window while a video plays in a second
//   window behind it). This is a great place to call -[CATransaction commit]
//   (or otherwise flush pending changes to the screen) in other processes,
//   because their updates will appear atomically.
//
// It has been observed that committing a CATransaction in the GPU process
// which changes which IOSurfaces are assigned to layers' contents is *faster*
// if done during the browser's post-commit phase vs. its pre-commit phase.

class ACCELERATED_WIDGET_MAC_EXPORT CATransactionCoordinator {
 public:
  class PreCommitObserver {
   public:
    virtual bool ShouldWaitInPreCommit() = 0;
    virtual base::TimeDelta PreCommitTimeout() = 0;
  };

  // PostCommitObserver sub-classes must communicate with the IO thread. The
  // CATransactionCoordinator will retain registered observers to ensure that
  // they are not deleted while registered.
  class PostCommitObserver
      : public base::RefCountedThreadSafe<PostCommitObserver> {
   public:
    virtual void OnActivateForTransaction() = 0;
    virtual void OnEnterPostCommit() = 0;
    virtual bool ShouldWaitInPostCommit() = 0;

   protected:
    virtual ~PostCommitObserver() = default;

   private:
    friend class base::RefCountedThreadSafe<PostCommitObserver>;
  };

  static CATransactionCoordinator& Get();

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

  void Synchronize();
  void DisableForTesting() { disabled_for_testing_ = true; }

  void AddPreCommitObserver(PreCommitObserver*);
  void RemovePreCommitObserver(PreCommitObserver*);

  void AddPostCommitObserver(scoped_refptr<PostCommitObserver>);
  void RemovePostCommitObserver(scoped_refptr<PostCommitObserver>);

 private:
  friend class base::NoDestructor<CATransactionCoordinator>;
  CATransactionCoordinator();
  ~CATransactionCoordinator();

  void SynchronizeImpl();
  void PreCommitHandler();
  void PostCommitHandler();

  bool active_ = false;
  bool disabled_for_testing_ = false;
  base::ObserverList<PreCommitObserver>::Unchecked pre_commit_observers_;
  std::set<scoped_refptr<PostCommitObserver>> post_commit_observers_;
};

}  // namespace ui

#endif  // UI_ACCELERATED_WIDGET_MAC_CA_TRANSACTION_OBSERVER_H_