File: window_resize_helper_mac.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 (86 lines) | stat: -rw-r--r-- 3,922 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
// Copyright 2015 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_WINDOW_RESIZE_HELPER_MAC_H_
#define UI_ACCELERATED_WIDGET_MAC_WINDOW_RESIZE_HELPER_MAC_H_

#include "base/lazy_instance.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/accelerated_widget_mac/accelerated_widget_mac_export.h"

namespace base {
class TimeDelta;
class WaitableEvent;
}  // namespace base

namespace ui {

// WindowResizeHelperMac is used to make resize appear smooth. That is to
// say, make sure that the window size and the size of the contents being drawn
// in that window are resized in lock-step. This is accomplished by waiting
// inside AppKit drawing routines on the UI thread for the compositor to produce
// a frame of same size as the NSView that hosts an AcceleratedWidgetMac. When a
// resize occurs, the view controller can wait for a frame of the correct size
// by calling WindowResizeHelperMac::WaitForSingleTaskToRun() until a timeout
// occurs, or the corresponding AcceleratedWidgetMac has a renderer frame of the
// same size as its NSView.
//
// By posting tasks to the custom task_runner(), other threads indicate tasks
// that are required to pick up a new frame. In the ordinary run of things these
// would be posted to the |target_task_runner|; the UI thread given as an
// argument to Init(). Posting instead to task_runner() will cause the tasks to
// be posted to the UI thread (as usual), and will also enqueue them into a
// queue which will be read and run in WaitForSingleTaskToRun(), potentially
// before the task posted to the UI thread is run. Some care is taken (see
// WrappedTask) to make sure that the messages are only executed once.
//
// This is further complicated because, in order for a frame to appear, it is
// also necessary to run tasks posted by the ui::Compositor. To accomplish this,
// the task_runner() that WindowResizeHelperMac provides can be used to
// construct a ui::Compositor. When the Compositor posts tasks to it, they are
// enqueued in the aforementioned queue, which may be pumped by
// WindowResizeHelperMac::WaitForSingleTaskToRun().
class ACCELERATED_WIDGET_MAC_EXPORT WindowResizeHelperMac {
 public:
  static WindowResizeHelperMac* Get();

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

  // Initializes the pumpable task_runner(), providing it with the task runner
  // for UI thread tasks. task_runner() will be null before Init() is called,
  // and WaitForSingleTaskToRun() will immediately return false.
  void Init(
      const scoped_refptr<base::SingleThreadTaskRunner>& target_task_runner);

  // Because this class is global, many tests may want to do this setup
  // repeatedly, so need some way to uninitialize as well.
  void ShutdownForTests();

  scoped_refptr<base::SingleThreadTaskRunner> task_runner() const;

  // UI THREAD ONLY -----------------------------------------------------------

  // Waits at most |max_delay| for a task to run. Returns true if a task ran,
  // false if no task ran.
  bool WaitForSingleTaskToRun(const base::TimeDelta& max_delay);

 private:
  friend struct base::LazyInstanceTraitsBase<WindowResizeHelperMac>;
  WindowResizeHelperMac();
  ~WindowResizeHelperMac();

  // This helper is needed to create a ScopedAllowWait inside the scope of a
  // class where it is allowed.
  static void EventTimedWait(base::WaitableEvent* event, base::TimeDelta delay);

  // The task runner to which the helper will post tasks. This also maintains
  // the task queue and does the actual work for WaitForSingleTaskToRun.
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
};

}  // namespace ui

#endif  // UI_ACCELERATED_WIDGET_MAC_WINDOW_RESIZE_HELPER_MAC_H_