OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ |
7 | 7 |
8 #include "base/lazy_instance.h" | 8 #include "base/macros.h" |
9 #include "base/single_thread_task_runner.h" | 9 |
10 #include "base/synchronization/lock.h" | 10 namespace IPC { |
11 #include "base/synchronization/waitable_event.h" | 11 class Message; |
12 #include "ipc/ipc_message.h" | 12 } |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 // RenderWidgetResizeHelper is used to make resize appear smooth. That is to | 16 // Provides functions for content to synchronize with window resize tasks being |
17 // say, make sure that the window size and the size of the content being drawn | 17 // managed by ui::WindowResizeHelperMac. WindowResizeHelperMac must be |
18 // in that window are resized in lock-step. This is accomplished by waiting | 18 // initialized before calling these functions. |
19 // inside -[RenderWidgetHostViewCocoa setFrameSize:] for the renderer (and | |
20 // potentially browser compositor as well) to produce a frame of same size | |
21 // as the RenderWidgetHostViewCocoa. | |
22 // | 19 // |
23 // The function of waiting for a frame of the correct size is done inside | 20 // The IPCs that are required to create new frames for smooth resize are sent |
24 // RenderWidgetHostImpl::WaitForSurface. That function will call | 21 // to the ui::WindowResizeHelperMac singleton using the PostRendererProcessMsg() |
25 // RenderWidgetResizeHelper::WaitForSingleTaskToRun until a timeout occurs, | 22 // and PostGpuProcessMsg() methods. Usually those IPCs arrive on the IO thread |
26 // or the corresponding RenderWidgetHostViewCocoa has a renderer frame of the | |
27 // same size as its NSView. | |
28 // | |
29 // This is somewhat complicated because waiting for frames requires that | |
30 // that the browser handle the IPCs (from the renderer and the GPU processes) | |
31 // that are required to pick up a new frame. In the ordinary run of things | |
32 // (ignoring RenderWidgetResizeHelper), those IPCs arrive on the IO thread | |
33 // and are posted as tasks to the UI thread either by the RenderMessageFilter | 23 // and are posted as tasks to the UI thread either by the RenderMessageFilter |
34 // (for renderer processes) or the GpuProcessHostUIShim (for the GPU process). | 24 // (for renderer processes) or the GpuProcessHostUIShim (for the GPU process). |
35 // The IPCs that are required to create new frames for smooth resize are sent | 25 // By posting them via RenderWidgetResizeHelper instead, it allows a smooth |
36 // to the RenderWidgetResizeHelper using the PostRendererProcessMsg and | 26 // window resize to be controlled by WindowResizeHelperMac. |
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 | |
41 // WrappedTask) to make sure that the messages are only executed once. | |
42 // | |
43 // 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 | |
45 // RenderWidgetResizeHelper provides a base::SingleThreadTaskRunner which, | |
46 // when a task is posted to it, enqueues the task in the aforementioned queue, | |
47 // which may be pumped by RenderWidgetResizeHelper::WaitForSingleTaskToRun. | |
48 // | |
49 class RenderWidgetResizeHelper { | 27 class RenderWidgetResizeHelper { |
50 public: | 28 public: |
51 static RenderWidgetResizeHelper* Get(); | |
52 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const; | |
53 | |
54 // UI THREAD ONLY ----------------------------------------------------------- | |
55 | |
56 // Waits at most |max_delay| for a task to run. Returns true if a task ran, | |
57 // false if no task ran. | |
58 bool WaitForSingleTaskToRun(const base::TimeDelta& max_delay); | |
59 | |
60 // IO THREAD ONLY ----------------------------------------------------------- | 29 // IO THREAD ONLY ----------------------------------------------------------- |
61 | 30 |
62 // This will cause |msg| to be handled by the RenderProcessHost corresponding | 31 // 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 | 32 // 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 | 33 // ordinary message loop would run it, or potentially earlier in a call to |
65 // WaitForSingleTaskToRun . | 34 // WindowResizeHelperMac::WaitForSingleTaskToRun(). |
66 void PostRendererProcessMsg(int render_process_id, const IPC::Message& msg); | 35 static void PostRendererProcessMsg(int render_process_id, |
| 36 const IPC::Message& msg); |
67 | 37 |
68 // This is similar to PostRendererProcessMsg, but will handle the message in | 38 // This is similar to PostRendererProcessMsg, but will handle the message in |
69 // the GpuProcessHostUIShim corresponding to |gpu_host_id|. | 39 // the GpuProcessHostUIShim corresponding to |gpu_host_id|. |
70 void PostGpuProcessMsg(int gpu_host_id, const IPC::Message& msg); | 40 static void PostGpuProcessMsg(int gpu_host_id, const IPC::Message& msg); |
71 | 41 |
72 private: | 42 private: |
73 friend struct base::DefaultLazyInstanceTraits<RenderWidgetResizeHelper>; | 43 DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWidgetResizeHelper); |
74 RenderWidgetResizeHelper(); | |
75 ~RenderWidgetResizeHelper(); | |
76 | |
77 // This helper is needed to create a ScopedAllowWait inside the scope of a | |
78 // class where it is allowed. | |
79 static void EventTimedWait(base::WaitableEvent* event, base::TimeDelta delay); | |
80 | |
81 // The task runner to which the helper will post tasks. This also maintains | |
82 // the task queue and does the actual work for WaitForSingleTaskToRun. | |
83 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(RenderWidgetResizeHelper); | |
86 }; | 44 }; |
87 | 45 |
88 } // namespace content | 46 } // namespace content |
89 | 47 |
90 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ | 48 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ |
OLD | NEW |