OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ |
| 7 |
| 8 #include <deque> |
| 9 |
| 10 #include "base/lazy_instance.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "ipc/ipc_message.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 // RenderWidgetResizeHelper is used to make resize appear smooth. That is to |
| 18 // say, make sure that the window size and the size of the content being drawn |
| 19 // in that window are resized in lock-step. This is accomplished by waiting |
| 20 // inside -[RenderWidgetHostViewCocoa setFrameSize:] for the renderer (and |
| 21 // potentially browser compositor as well) to produce a frame of same size |
| 22 // as the RenderWidgetHostViewCocoa. |
| 23 // |
| 24 // The function of waiting for a frame of the correct size is done inside |
| 25 // RenderWidgetHostImpl::WaitForSurface. That function will call |
| 26 // RenderWidgetResizeHelper::WaitForSingleTaskToRun until a timeout occurs, |
| 27 // or the corresponding RenderWidgetHostViewCocoa has a renderer frame of the |
| 28 // same size as its NSView. |
| 29 // |
| 30 // This is somewhat complicated because waiting for frames requires that |
| 31 // 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 |
| 33 // (ignoring RenderWidgetResizeHelper), those IPCs arrive on the IO thread |
| 34 // and are posted as tasks to the UI thread either by the RenderMessageFilter |
| 35 // (for renderer processes) or the GpuProcessHostUIShim (for the GPU process). |
| 36 // The IPCs that are required to create new frames for smooth resize are sent |
| 37 // to the RenderWidgetResizeHelper using the PostRendererProcessMsg and |
| 38 // PostGpuProcessMsg methods. These functions will post them as tasks to the UI |
| 39 // thread (as usual), and will also enqueue them into a queue which will be |
| 40 // read and run in RenderWidgetResizeHelper::WaitForSingleTaskToRun, potentially |
| 41 // before the task posted to the UI thread is run. Some care is taken (see |
| 42 // EnqueuedTask) to make sure that the messages are only executed once. |
| 43 // |
| 44 // TODO(ccameron): This does not support smooth resize when using the |
| 45 // ui::Compositor yet. To support this, it will be necessary that the |
| 46 // RenderWidgetResizeHelper have a base::TaskRunner to send to the |
| 47 // cc::ThreadProxy. The tasks that cc then posts can be pumped in |
| 48 // WaitForSingleTaskToRun in a way similar to the one in which IPCs are handled. |
| 49 // |
| 50 class RenderWidgetResizeHelper { |
| 51 public: |
| 52 static RenderWidgetResizeHelper* Get(); |
| 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 ----------------------------------------------------------- |
| 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: |
| 73 friend struct base::DefaultLazyInstanceTraits<RenderWidgetResizeHelper>; |
| 74 RenderWidgetResizeHelper(); |
| 75 ~RenderWidgetResizeHelper(); |
| 76 |
| 77 // A classed used to wrap an IPC or a task. |
| 78 class EnqueuedTask; |
| 79 friend class EnqueuedTask; |
| 80 |
| 81 // Called on the IO thread to add a task to the queue. |
| 82 void PostEnqueuedTask(EnqueuedTask* proxy); |
| 83 |
| 84 // Called on the UI to remove the task from the queue when it is run. |
| 85 void WillRunEnqueuedTask(EnqueuedTask* proxy); |
| 86 |
| 87 // A queue of live messages. Must hold |task_queue_lock_| to access. |
| 88 // The EnqueuedTask objects are removed from the front of the queue when |
| 89 // they are run (either by TaskRunner they were posted to or by a call to |
| 90 // WaitForSingleTaskToRun pulling them off of the queue). |
| 91 typedef std::deque<EnqueuedTask*> EnqueuedTaskQueue; |
| 92 EnqueuedTaskQueue task_queue_; |
| 93 base::Lock task_queue_lock_; |
| 94 |
| 95 // Event used to wake up the UI thread if it is sleeping in |
| 96 // WaitForSingleTaskToRun. |
| 97 base::WaitableEvent event_; |
| 98 }; |
| 99 |
| 100 } // namespace content |
| 101 |
| 102 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_MAC_H_ |
OLD | NEW |