OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ | 5 #ifndef UI_ACCELERATED_WIDGET_MAC_WINDOW_RESIZE_HELPER_MAC_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ | 6 #define UI_ACCELERATED_WIDGET_MAC_WINDOW_RESIZE_HELPER_MAC_H_ |
7 | 7 |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/macros.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "ui/accelerated_widget_mac/accelerated_widget_mac_export.h" |
12 #include "ipc/ipc_message.h" | |
13 | 12 |
14 namespace content { | 13 namespace base { |
| 14 class SingleThreadTaskRunner; |
| 15 class TimeDelta; |
| 16 class WaitableEvent; |
| 17 } |
15 | 18 |
16 // RenderWidgetResizeHelper is used to make resize appear smooth. That is to | 19 namespace ui { |
17 // say, make sure that the window size and the size of the content being drawn | 20 |
| 21 // WindowResizeHelperMac is used to make resize appear smooth. That is to |
| 22 // say, make sure that the window size and the size of the contents being drawn |
18 // in that window are resized in lock-step. This is accomplished by waiting | 23 // in that window are resized in lock-step. This is accomplished by waiting |
19 // inside -[RenderWidgetHostViewCocoa setFrameSize:] for the renderer (and | 24 // inside AppKit drawing routines on the UI thread for the compositor to produce |
20 // potentially browser compositor as well) to produce a frame of same size | 25 // a frame of same size as the NSView that hosts an AcceleratedWidgetMac. When a |
21 // as the RenderWidgetHostViewCocoa. | 26 // resize occurs, the view controller can wait for a frame of the correct size |
22 // | 27 // by calling WindowResizeHelperMac::WaitForSingleTaskToRun() until a timeout |
23 // The function of waiting for a frame of the correct size is done inside | 28 // occurs, or the corresponding AcceleratedWidgetMac has a renderer frame of the |
24 // RenderWidgetHostImpl::WaitForSurface. That function will call | |
25 // RenderWidgetResizeHelper::WaitForSingleTaskToRun until a timeout occurs, | |
26 // or the corresponding RenderWidgetHostViewCocoa has a renderer frame of the | |
27 // same size as its NSView. | 29 // same size as its NSView. |
28 // | 30 // |
29 // This is somewhat complicated because waiting for frames requires that | 31 // By posting tasks to the custom task_runner(), other threads indicate tasks |
30 // that the browser handle the IPCs (from the renderer and the GPU processes) | 32 // that are required to pick up a new frame. In the ordinary run of things these |
31 // that are required to pick up a new frame. In the ordinary run of things | 33 // would be posted to the |target_task_runner|; the UI thread given as an |
32 // (ignoring RenderWidgetResizeHelper), those IPCs arrive on the IO thread | 34 // argument to Init(). Posting instead to task_runner() will cause the tasks to |
33 // and are posted as tasks to the UI thread either by the RenderMessageFilter | 35 // be posted to the UI thread (as usual), and will also enqueue them into a |
34 // (for renderer processes) or the GpuProcessHostUIShim (for the GPU process). | 36 // queue which will be read and run in WaitForSingleTaskToRun(), potentially |
35 // The IPCs that are required to create new frames for smooth resize are sent | |
36 // to the RenderWidgetResizeHelper using the PostRendererProcessMsg and | |
37 // PostGpuProcessMsg methods. These functions will post them as tasks to the UI | |
38 // thread (as usual), and will also enqueue them into a queue which will be | |
39 // read and run in RenderWidgetResizeHelper::WaitForSingleTaskToRun, potentially | |
40 // before the task posted to the UI thread is run. Some care is taken (see | 37 // before the task posted to the UI thread is run. Some care is taken (see |
41 // WrappedTask) to make sure that the messages are only executed once. | 38 // WrappedTask) to make sure that the messages are only executed once. |
42 // | 39 // |
43 // This is further complicated because, in order for a frame to appear, it is | 40 // This is further complicated because, in order for a frame to appear, it is |
44 // necessary to run tasks posted by the ui::Compositor. To accomplish this, the | 41 // also necessary to run tasks posted by the ui::Compositor. To accomplish this, |
45 // RenderWidgetResizeHelper provides a base::SingleThreadTaskRunner which, | 42 // the task_runner() that WindowResizeHelperMac provides can be used to |
46 // when a task is posted to it, enqueues the task in the aforementioned queue, | 43 // construct a ui::Compositor. When the Compositor posts tasks to it, they are |
47 // which may be pumped by RenderWidgetResizeHelper::WaitForSingleTaskToRun. | 44 // enqueued in the aforementioned queue, which may be pumped by |
48 // | 45 // WindowResizeHelperMac::WaitForSingleTaskToRun(). |
49 class RenderWidgetResizeHelper { | 46 class ACCELERATED_WIDGET_MAC_EXPORT WindowResizeHelperMac { |
50 public: | 47 public: |
51 static RenderWidgetResizeHelper* Get(); | 48 static WindowResizeHelperMac* Get(); |
| 49 |
| 50 // Initializes the pumpable task_runner(), providing it with the task runner |
| 51 // for UI thread tasks. task_runner() will be null before Init() is called, |
| 52 // and WaitForSingleTaskToRun() will immediately return false. |
| 53 void Init( |
| 54 const scoped_refptr<base::SingleThreadTaskRunner>& target_task_runner); |
| 55 |
52 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const; | 56 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const; |
53 | 57 |
54 // UI THREAD ONLY ----------------------------------------------------------- | 58 // UI THREAD ONLY ----------------------------------------------------------- |
55 | 59 |
56 // Waits at most |max_delay| for a task to run. Returns true if a task ran, | 60 // Waits at most |max_delay| for a task to run. Returns true if a task ran, |
57 // false if no task ran. | 61 // false if no task ran. |
58 bool WaitForSingleTaskToRun(const base::TimeDelta& max_delay); | 62 bool WaitForSingleTaskToRun(const base::TimeDelta& max_delay); |
59 | 63 |
60 // IO THREAD ONLY ----------------------------------------------------------- | |
61 | |
62 // This will cause |msg| to be handled by the RenderProcessHost corresponding | |
63 // to |render_process_id|, on the UI thread. This will either happen when the | |
64 // ordinary message loop would run it, or potentially earlier in a call to | |
65 // WaitForSingleTaskToRun . | |
66 void PostRendererProcessMsg(int render_process_id, const IPC::Message& msg); | |
67 | |
68 // This is similar to PostRendererProcessMsg, but will handle the message in | |
69 // the GpuProcessHostUIShim corresponding to |gpu_host_id|. | |
70 void PostGpuProcessMsg(int gpu_host_id, const IPC::Message& msg); | |
71 | |
72 private: | 64 private: |
73 friend struct base::DefaultLazyInstanceTraits<RenderWidgetResizeHelper>; | 65 friend struct base::DefaultLazyInstanceTraits<WindowResizeHelperMac>; |
74 RenderWidgetResizeHelper(); | 66 WindowResizeHelperMac(); |
75 ~RenderWidgetResizeHelper(); | 67 ~WindowResizeHelperMac(); |
76 | 68 |
77 // This helper is needed to create a ScopedAllowWait inside the scope of a | 69 // This helper is needed to create a ScopedAllowWait inside the scope of a |
78 // class where it is allowed. | 70 // class where it is allowed. |
79 static void EventTimedWait(base::WaitableEvent* event, base::TimeDelta delay); | 71 static void EventTimedWait(base::WaitableEvent* event, base::TimeDelta delay); |
80 | 72 |
81 // The task runner to which the helper will post tasks. This also maintains | 73 // The task runner to which the helper will post tasks. This also maintains |
82 // the task queue and does the actual work for WaitForSingleTaskToRun. | 74 // the task queue and does the actual work for WaitForSingleTaskToRun. |
83 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 75 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
84 | 76 |
85 DISALLOW_COPY_AND_ASSIGN(RenderWidgetResizeHelper); | 77 DISALLOW_COPY_AND_ASSIGN(WindowResizeHelperMac); |
86 }; | 78 }; |
87 | 79 |
88 } // namespace content | 80 } // namespace ui |
89 | 81 |
90 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ | 82 #endif // UI_ACCELERATED_WIDGET_MAC_WINDOW_RESIZE_HELPER_MAC_H_ |
OLD | NEW |