Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / render_widget_resize_helper_mac.h
blob132fb07f5f3e022315c6924e5738cc6fc8a3a323
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_
8 #include "base/lazy_instance.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/synchronization/lock.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "ipc/ipc_message.h"
14 namespace content {
16 // RenderWidgetResizeHelper is used to make resize appear smooth. That is to
17 // say, make sure that the window size and the size of the content being drawn
18 // in that window are resized in lock-step. This is accomplished by waiting
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.
23 // The function of waiting for a frame of the correct size is done inside
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 // 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
34 // (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
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
41 // WrappedTask) to make sure that the messages are only executed once.
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.
49 class RenderWidgetResizeHelper {
50 public:
51 static RenderWidgetResizeHelper* Get();
52 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const;
54 // UI THREAD ONLY -----------------------------------------------------------
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);
60 // IO THREAD ONLY -----------------------------------------------------------
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);
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);
72 private:
73 friend struct base::DefaultLazyInstanceTraits<RenderWidgetResizeHelper>;
74 RenderWidgetResizeHelper();
75 ~RenderWidgetResizeHelper();
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);
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_;
85 DISALLOW_COPY_AND_ASSIGN(RenderWidgetResizeHelper);
88 } // namespace content
90 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_