blob: 0c24f9ff09363814a2714c6fedd981c8ce98fdca [file] [log] [blame]
[email protected]c5218762011-03-24 17:48:511// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]32876ae2011-11-15 22:25:217#include "base/bind.h"
[email protected]4fb66842009-12-04 21:41:008#include "base/command_line.h"
[email protected]366ae242011-05-10 02:23:589#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2114#include "base/stl_util.h"
[email protected]8a9d6ca32011-06-06 20:11:3015#include "base/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4816#include "build/build_config.h"
[email protected]992db4c2011-05-12 15:37:1517#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5018#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2919#include "content/public/common/content_switches.h"
[email protected]3da12cb2011-10-08 02:25:0420#include "content/renderer/gpu/compositor_thread.h"
[email protected]8704f89b2011-04-15 00:30:0521#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4422#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1923#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1824#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4825#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5226#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3727#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]e6e90dc2011-12-03 00:01:3728#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
[email protected]8bd0fe62011-01-17 06:44:3729#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
30#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0131#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]e6e90dc2011-12-03 00:01:3732#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]8bd0fe62011-01-17 06:44:3733#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]e6e90dc2011-12-03 00:01:3734#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
35#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]08397d52011-02-05 01:53:3836#include "ui/gfx/point.h"
37#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1238#include "ui/gfx/surface/transport_dib.h"
[email protected]bd37ae252011-06-03 01:28:1839#include "ui/gfx/gl/gl_switches.h"
[email protected]8c89e7792009-08-19 21:18:3440#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5041#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4642#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4843
44#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4945#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5246#include "third_party/skia/include/core/SkPixelRef.h"
47#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4848#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4449
[email protected]8bd0fe62011-01-17 06:44:3750#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2951
[email protected]fa7b1dc2010-06-23 17:53:0452using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3053using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2254using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0655using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2856using WebKit::WebNavigationPolicy;
[email protected]e99ef6f2011-10-16 01:13:0057using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2858using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5959using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1860using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0161using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5262using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1963using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5264using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2865using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2466using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0467using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1868using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2769using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2270
[email protected]380244092011-10-07 17:26:2771RenderWidget::RenderWidget(WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2972 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1173 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2974 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4275 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3576 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2977 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0678 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0579 update_reply_pending_(false),
[email protected]65225772011-05-12 21:10:2480 using_asynchronous_swapbuffers_(false),
81 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2982 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2983 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0984 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2985 needs_repainting_on_restore_(false),
86 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4987 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4888 closing_(false),
[email protected]992db4c2011-05-12 15:37:1589 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0490 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:4591 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
92 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:1293 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4894 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:1295 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5096 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2197 animation_update_pending_(false),
[email protected]65225772011-05-12 21:10:2498 animation_task_posted_(false),
99 invalidation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:28100 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27101 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18102 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
103 switches::kDisableGpuVsync);
initial.commit09911bf2008-07-26 23:55:29104}
105
106RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11107 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21108 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35109 if (current_paint_buf_) {
110 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
111 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29112 }
[email protected]992db4c2011-05-12 15:37:15113 // If we are swapped out, we have released already.
114 if (!is_swapped_out_)
115 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29116}
117
[email protected]484955942010-08-19 16:13:18118// static
[email protected]8085dbc82008-09-26 22:53:44119RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]3e2b375b2010-04-07 17:03:12120 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29121 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27122 scoped_refptr<RenderWidget> widget(new RenderWidget(popup_type));
initial.commit09911bf2008-07-26 23:55:29123 widget->Init(opener_id); // adds reference
124 return widget;
125}
126
[email protected]484955942010-08-19 16:13:18127// static
128WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
129 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03130 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18131 break;
132 case WebKit::WebPopupTypeSelect:
133 case WebKit::WebPopupTypeSuggestion:
134 return WebPopupMenu::create(render_widget);
135 default:
136 NOTREACHED();
137 }
138 return NULL;
139}
140
initial.commit09911bf2008-07-26 23:55:29141void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18142 DoInit(opener_id,
143 RenderWidget::CreateWebWidget(this),
144 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
145}
146
[email protected]484955942010-08-19 16:13:18147void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06148 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18149 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29150 DCHECK(!webwidget_);
151
152 if (opener_id != MSG_ROUTING_NONE)
153 opener_id_ = opener_id;
154
[email protected]484955942010-08-19 16:13:18155 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29156
[email protected]380244092011-10-07 17:26:27157 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29158 if (result) {
[email protected]380244092011-10-07 17:26:27159 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29160 // Take a reference on behalf of the RenderThread. This will be balanced
161 // when we receive ViewMsg_Close.
162 AddRef();
163 } else {
164 DCHECK(false);
165 }
166}
167
168// This is used to complete pending inits and non-pending inits. For non-
169// pending cases, the parent will be the same as the current parent. This
170// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55171void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29172 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29173
174 host_window_ = parent_hwnd;
175
[email protected]6de74452009-02-25 18:04:59176 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29177}
178
[email protected]992db4c2011-05-12 15:37:15179void RenderWidget::SetSwappedOut(bool is_swapped_out) {
180 // We should only toggle between states.
181 DCHECK(is_swapped_out_ != is_swapped_out);
182 is_swapped_out_ = is_swapped_out;
183
184 // If we are swapping out, we will call ReleaseProcess, allowing the process
185 // to exit if all of its RenderViews are swapped out. We wait until the
186 // WasSwappedOut call to do this, to avoid showing the sad tab.
187 // If we are swapping in, we call AddRefProcess to prevent the process from
188 // exiting.
189 if (!is_swapped_out)
190 RenderProcess::current()->AddRefProcess();
191}
192
[email protected]a95986a82010-12-24 06:19:28193bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
194 bool handled = true;
195 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
196 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
197 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
198 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
199 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
200 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15201 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28202 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
203 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
204 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
205 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
206 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
207 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
208 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
209 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
210 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
211 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
212 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
213 IPC_MESSAGE_UNHANDLED(handled = false)
214 IPC_END_MESSAGE_MAP()
215 return handled;
216}
initial.commit09911bf2008-07-26 23:55:29217
218bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15219 // Don't send any messages after the browser has told us to close, and filter
220 // most outgoing messages while swapped out.
221 if ((is_swapped_out_ &&
222 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
223 closing_) {
initial.commit09911bf2008-07-26 23:55:29224 delete message;
225 return false;
226 }
227
228 // If given a messsage without a routing ID, then assign our routing ID.
229 if (message->routing_id() == MSG_ROUTING_NONE)
230 message->set_routing_id(routing_id_);
231
[email protected]380244092011-10-07 17:26:27232 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44233}
234
initial.commit09911bf2008-07-26 23:55:29235// Got a response from the browser after the renderer decided to create a new
236// view.
[email protected]c03a6e72011-04-19 21:42:06237void RenderWidget::OnCreatingNewAck(
[email protected]2d7c8552011-06-27 19:21:55238 gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29239 DCHECK(routing_id_ != MSG_ROUTING_NONE);
240
[email protected]2d7c8552011-06-27 19:21:55241 CompleteInit(parent);
initial.commit09911bf2008-07-26 23:55:29242}
243
244void RenderWidget::OnClose() {
245 if (closing_)
246 return;
247 closing_ = true;
248
249 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03250 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27251 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03252 SetHidden(false);
253 }
initial.commit09911bf2008-07-26 23:55:29254
initial.commit09911bf2008-07-26 23:55:29255 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25256 // now. Post a task that only gets invoked when there are no nested message
257 // loops.
[email protected]32876ae2011-11-15 22:25:21258 MessageLoop::current()->PostNonNestableTask(
259 FROM_HERE, NewRunnableMethod(this, &RenderWidget::Close));
[email protected]d3fc25652009-02-24 22:31:25260
261 // Balances the AddRef taken when we called AddRoute.
262 Release();
initial.commit09911bf2008-07-26 23:55:29263}
264
[email protected]f21c613a2009-02-12 14:46:17265void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09266 const gfx::Rect& resizer_rect,
267 bool is_fullscreen) {
initial.commit09911bf2008-07-26 23:55:29268 // During shutdown we can just ignore this message.
269 if (!webwidget_)
270 return;
271
[email protected]e2356242010-11-16 22:33:03272 // We shouldn't be asked to resize to our current size.
273 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
274
[email protected]f21c613a2009-02-12 14:46:17275 // Remember the rect where the resize corner will be drawn.
276 resizer_rect_ = resizer_rect;
277
[email protected]2b624c562011-10-27 22:58:26278 // NOTE: We may have entered fullscreen mode without changing our size.
279 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
280 if (fullscreen_change)
281 WillToggleFullscreen();
[email protected]ee41e7d22011-10-14 19:34:09282 is_fullscreen_ = is_fullscreen;
initial.commit09911bf2008-07-26 23:55:29283
[email protected]2b624c562011-10-27 22:58:26284 if (size_ != new_size) {
285 // TODO(darin): We should not need to reset this here.
286 SetHidden(false);
287 needs_repainting_on_restore_ = false;
initial.commit09911bf2008-07-26 23:55:29288
[email protected]2b624c562011-10-27 22:58:26289 size_ = new_size;
[email protected]552e6002009-11-19 05:24:57290
[email protected]2b624c562011-10-27 22:58:26291 // We should not be sent a Resize message if we have not ACK'd the previous
292 DCHECK(!next_paint_is_resize_ack());
293
294 paint_aggregator_.ClearPendingUpdate();
295
296 // When resizing, we want to wait to paint before ACK'ing the resize. This
297 // ensures that we only resize as fast as we can paint. We only need to
298 // send an ACK if we are resized to a non-empty rect.
299 webwidget_->resize(new_size);
300 if (!new_size.IsEmpty()) {
301 if (!is_accelerated_compositing_active_) {
302 // Resize should have caused an invalidation of the entire view.
303 DCHECK(paint_aggregator_.HasPendingUpdate());
304 }
305
306 // We will send the Resize_ACK flag once we paint again.
307 set_next_paint_is_resize_ack();
[email protected]f98d7e3c2010-09-13 22:30:46308 }
initial.commit09911bf2008-07-26 23:55:29309 }
[email protected]2b624c562011-10-27 22:58:26310
311 if (fullscreen_change)
312 DidToggleFullscreen();
initial.commit09911bf2008-07-26 23:55:29313}
314
315void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31316 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29317 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03318 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29319}
320
321void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31322 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29323 // During shutdown we can just ignore this message.
324 if (!webwidget_)
325 return;
326
327 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03328 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29329
330 if (!needs_repainting && !needs_repainting_on_restore_)
331 return;
332 needs_repainting_on_restore_ = false;
333
[email protected]d65adb12010-04-28 17:26:49334 // Tag the next paint as a restore ack, which is picked up by
335 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29336 set_next_paint_is_restore_ack();
337
338 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56339 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46340 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
341 } else {
342 scheduleComposite();
343 }
initial.commit09911bf2008-07-26 23:55:29344}
345
[email protected]992db4c2011-05-12 15:37:15346void RenderWidget::OnWasSwappedOut() {
347 // If we have been swapped out and no one else is using this process,
348 // it's safe to exit now. If we get swapped back in, we will call
349 // AddRefProcess in SetSwappedOut.
350 if (is_swapped_out_)
351 RenderProcess::current()->ReleaseProcess();
352}
353
[email protected]53d3f302009-12-21 04:42:05354void RenderWidget::OnRequestMoveAck() {
355 DCHECK(pending_window_rect_count_);
356 pending_window_rect_count_--;
357}
358
359void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58360 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21361 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05362 update_reply_pending_ = false;
363
[email protected]b4d08452010-10-05 17:34:35364 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
365 // have no current paint buffer.
366 if (current_paint_buf_) {
367 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
368 current_paint_buf_ = NULL;
369 }
370
[email protected]65225772011-05-12 21:10:24371 // If swapbuffers is still pending, then defer the update until the
372 // swapbuffers occurs.
373 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
374 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
375 return;
376 }
377
[email protected]00c39612010-03-06 02:53:28378 // Notify subclasses.
[email protected]b5db7eb2011-11-29 09:11:50379 if (!is_accelerated_compositing_active_)
380 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25381
initial.commit09911bf2008-07-26 23:55:29382 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24383 DoDeferredUpdateAndSendInputAck();
384}
385
[email protected]d0be63772011-12-20 23:18:04386bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24387 return false;
388}
389
[email protected]d0be63772011-12-20 23:18:04390void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24391 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21392 while (!updates_pending_swap_.empty()) {
393 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
394 updates_pending_swap_.pop_front();
395 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
396 // compositing pass, hence doesn't require an UpdateRect message.
397 if (msg)
398 Send(msg);
399 }
[email protected]65225772011-05-12 21:10:24400 num_swapbuffers_complete_pending_ = 0;
401 using_asynchronous_swapbuffers_ = false;
402 // Schedule another frame so the compositor learns about it.
403 scheduleComposite();
404}
405
[email protected]37a6f302011-07-11 23:43:08406void RenderWidget::OnSwapBuffersPosted() {
407 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21408
409 if (using_asynchronous_swapbuffers_) {
410 ViewHostMsg_UpdateRect* msg = NULL;
411 // pending_update_params_ can be NULL if the swap doesn't correspond to an
412 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
413 // message.
414 if (pending_update_params_.get()) {
415 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
416 pending_update_params_.reset();
417 }
418 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08419 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21420 }
[email protected]37a6f302011-07-11 23:43:08421}
422
423void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24424 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
425 // When compositing deactivates, we reset the swapbuffers pending count. The
426 // swapbuffers acks may still arrive, however.
427 if (num_swapbuffers_complete_pending_ == 0) {
428 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
429 return;
430 }
[email protected]aa4117f2011-12-09 22:19:21431 DCHECK(!updates_pending_swap_.empty());
432 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
433 updates_pending_swap_.pop_front();
434 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
435 // compositing pass, hence doesn't require an UpdateRect message.
436 if (msg)
437 Send(msg);
[email protected]65225772011-05-12 21:10:24438 num_swapbuffers_complete_pending_--;
439
440 // If update reply is still pending, then defer the update until that reply
441 // occurs.
[email protected]d0be63772011-12-20 23:18:04442 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24443 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
444 return;
445 }
446
447 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06448 // when we were previously rendering. However, if an invalidation task is not
449 // posted, there may be software rendering work pending. In that case, don't
450 // early out.
451 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24452 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
453 return;
454 }
455
[email protected]65225772011-05-12 21:10:24456 // Continue painting if necessary...
457 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29458}
459
initial.commit09911bf2008-07-26 23:55:29460void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24461 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29462 void* iter = NULL;
463
464 const char* data;
465 int data_length;
[email protected]5dd768212009-08-13 23:34:49466 handling_input_event_ = true;
467 if (!message.ReadData(&iter, &data, &data_length)) {
468 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29469 return;
[email protected]5dd768212009-08-13 23:34:49470 }
initial.commit09911bf2008-07-26 23:55:29471
472 const WebInputEvent* input_event =
473 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48474
[email protected]b68a0e52011-12-08 15:11:12475 bool is_keyboard_shortcut = false;
476 // is_keyboard_shortcut flag is only available for RawKeyDown events.
477 if (input_event->type == WebInputEvent::RawKeyDown)
478 message.ReadBool(&iter, &is_keyboard_shortcut);
479
[email protected]67bfb83f2011-09-22 03:36:37480 bool prevent_default = false;
481 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26482 const WebMouseEvent& mouse_event =
483 *static_cast<const WebMouseEvent*>(input_event);
484 TRACE_EVENT2("renderer", "HandleMouseMove",
485 "x", mouse_event.x, "y", mouse_event.y);
486 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37487 }
488
489 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12490 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
491 suppress_next_char_events_ = false;
492 if (!processed && webwidget_)
493 processed = webwidget_->handleInputEvent(*input_event);
494 }
495
496 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
497 // it's not processed by webkit, then we need to suppress the upcoming Char
498 // events.
499 if (!processed && is_keyboard_shortcut)
500 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29501
[email protected]a9fb30aa2011-10-06 06:58:46502 IPC::Message* response =
503 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
504 processed);
[email protected]e2824412009-02-27 01:57:05505
[email protected]353a34c2010-05-28 23:35:17506 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03507 input_event->type == WebInputEvent::MouseWheel ||
508 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57509 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24510 // We want to rate limit the input events in this case, so we'll wait for
511 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17512 if (pending_input_event_ack_.get()) {
513 // As two different kinds of events could cause us to postpone an ack
514 // we send it now, if we have one pending. The Browser should never
515 // send us the same kind of event we are delaying the ack for.
516 Send(pending_input_event_ack_.release());
517 }
[email protected]12fbad812009-09-01 18:21:24518 pending_input_event_ack_.reset(response);
519 } else {
520 Send(response);
521 }
522
[email protected]5dd768212009-08-13 23:34:49523 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48524
[email protected]67bfb83f2011-09-22 03:36:37525 if (!prevent_default) {
526 if (WebInputEvent::isKeyboardEventType(input_event->type))
527 DidHandleKeyEvent();
528 if (WebInputEvent::isMouseEventType(input_event->type))
529 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24530 if (WebInputEvent::isTouchEventType(input_event->type))
531 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37532 }
initial.commit09911bf2008-07-26 23:55:29533}
534
535void RenderWidget::OnMouseCaptureLost() {
536 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28537 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29538}
539
540void RenderWidget::OnSetFocus(bool enable) {
541 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33542 if (webwidget_)
543 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29544}
545
546void RenderWidget::ClearFocus() {
547 // We may have got the focus from the browser before this gets processed, in
548 // which case we do not want to unfocus ourself.
549 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28550 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29551}
552
[email protected]2d5d09d52009-06-15 14:29:21553void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00554 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21555 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06556 TRACE_EVENT2("renderer", "PaintRect",
557 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00558 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21559
560 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00561 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
562 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03563
[email protected]699ab0d2009-04-23 23:19:14564 // If there is a custom background, tile it.
565 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14566 SkPaint paint;
567 SkShader* shader = SkShader::CreateBitmapShader(background_,
568 SkShader::kRepeat_TileMode,
569 SkShader::kRepeat_TileMode);
570 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20571
572 // Use kSrc_Mode to handle background_ transparency properly.
573 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
574
575 // Canvas could contain multiple update rects. Clip to given rect so that
576 // we don't accidentally clear other update rects.
577 canvas->save();
578 SkRect clip;
579 clip.set(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
580 SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()));
581 canvas->clipRect(clip);
[email protected]699ab0d2009-04-23 23:19:14582 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20583 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14584 }
585
[email protected]719b36f2010-12-22 20:36:46586 // First see if this rect is a plugin that can paint itself faster.
587 TransportDIB* optimized_dib = NULL;
588 gfx::Rect optimized_copy_rect, optimized_copy_location;
589 webkit::ppapi::PluginInstance* optimized_instance =
590 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
591 &optimized_copy_location,
592 &optimized_copy_rect);
593 if (optimized_instance) {
594 // This plugin can be optimize-painted and we can just ask it to paint
595 // itself. We don't actually need the TransportDIB in this case.
596 //
597 // This is an optimization for PPAPI plugins that know they're on top of
598 // the page content. If this rect is inside such a plugin, we can save some
599 // time and avoid re-rendering the page content which we know will be
600 // covered by the plugin later (this time can be significant, especially
601 // for a playing movie that is invalidating a lot).
602 //
603 // In the plugin movie case, hopefully the similar call to
604 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
605 // painting, because that avoids copying the plugin image to a different
606 // paint rect. Unfortunately, if anything on the page is animating other
607 // than the movie, it break this optimization since the union of the
608 // invalid regions will be larger than the plugin.
609 //
610 // This code optimizes that case, where we can still avoid painting in
611 // WebKit and filling the background (which can be slow) and just painting
612 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
613 // required.
614 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27615 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46616 } else {
617 // Normal painting case.
618 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
619
620 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35621 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46622 }
initial.commit09911bf2008-07-26 23:55:29623
[email protected]4fb66842009-12-04 21:41:00624 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00625 canvas->restore();
626}
627
628void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
629 skia::PlatformCanvas* canvas) {
630 static bool kPaintBorder =
631 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
632 if (!kPaintBorder)
633 return;
634
[email protected]53d3f302009-12-21 04:42:05635 // Cycle through these colors to help distinguish new paint rects.
636 const SkColor colors[] = {
637 SkColorSetARGB(0x3F, 0xFF, 0, 0),
638 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
639 SkColorSetARGB(0x3F, 0, 0, 0xFF),
640 };
641 static int color_selector = 0;
642
[email protected]4fb66842009-12-04 21:41:00643 SkPaint paint;
644 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05645 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00646 paint.setStrokeWidth(1);
647
648 SkIRect irect;
649 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
650 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29651}
652
[email protected]52ccd0ea2011-02-16 01:09:05653void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30654 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]52ccd0ea2011-02-16 01:09:05655 animation_task_posted_ = false;
[email protected]921244e42011-07-20 16:36:30656 if (!animation_update_pending_) {
657 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59658 return;
[email protected]921244e42011-07-20 16:36:30659 }
[email protected]bd37ae252011-06-03 01:28:18660 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59661 // Record when we fired (according to base::Time::Now()) relative to when
662 // we posted the task to quantify how much the base::Time/base::TimeTicks
663 // skew is affecting animations.
664 base::TimeDelta animation_callback_delay = base::Time::Now() -
665 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
666 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
667 animation_callback_delay,
668 base::TimeDelta::FromMilliseconds(0),
669 base::TimeDelta::FromMilliseconds(30),
670 25);
671 }
[email protected]65225772011-05-12 21:10:24672 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24673}
674
[email protected]52ccd0ea2011-02-16 01:09:05675void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59676 if (!animation_update_pending_)
677 return;
[email protected]bd37ae252011-06-03 01:28:18678
679 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
680 int animationInterval = IsRenderingVSynced() ? 16 : 0;
681
[email protected]7c4329e2011-02-18 22:02:59682 base::Time now = base::Time::Now();
[email protected]46d8c8292011-12-12 18:15:05683 if (now >= animation_floor_time_ || is_accelerated_compositing_active_) {
[email protected]921244e42011-07-20 16:36:30684 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]bd37ae252011-06-03 01:28:18685 animation_floor_time_ = now +
686 base::TimeDelta::FromMilliseconds(animationInterval);
687 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59688 // running animation callbacks so that if a callback requests another
689 // we'll be sure to run it at the proper time.
[email protected]32876ae2011-11-15 22:25:21690 MessageLoop::current()->PostDelayedTask(
691 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this),
692 animationInterval);
[email protected]7c4329e2011-02-18 22:02:59693 animation_task_posted_ = true;
694 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30695 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59696 return;
[email protected]5f8b1022011-01-21 23:34:50697 }
[email protected]bd37ae252011-06-03 01:28:18698 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]7c4329e2011-02-18 22:02:59699 if (animation_task_posted_)
700 return;
701 // This code uses base::Time::Now() to calculate the floor and next fire
702 // time because javascript's Date object uses base::Time::Now(). The
703 // message loop uses base::TimeTicks, which on windows can have a
704 // different granularity than base::Time.
705 // The upshot of all this is that this function might be called before
706 // base::Time::Now() has advanced past the animation_floor_time_. To
707 // avoid exposing this delay to javascript, we keep posting delayed
708 // tasks until base::Time::Now() has advanced far enough.
709 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
710 animation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21711 MessageLoop::current()->PostDelayedTask(
712 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this), delay);
[email protected]5f8b1022011-01-21 23:34:50713}
714
[email protected]bd37ae252011-06-03 01:28:18715bool RenderWidget::IsRenderingVSynced() {
716 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
717 // not caught by this check. This will lead to artificially low frame rates
718 // for people who force vsync off at a driver level and expect Chrome to speed
719 // up.
720 return !has_disable_gpu_vsync_switch_;
721}
722
[email protected]65225772011-05-12 21:10:24723void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06724 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24725 invalidation_task_posted_ = false;
726 DoDeferredUpdateAndSendInputAck();
727}
728
729void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05730 DoDeferredUpdate();
731
732 if (pending_input_event_ack_.get())
733 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21734}
735
[email protected]552e6002009-11-19 05:24:57736void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58737 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08738
[email protected]65225772011-05-12 21:10:24739 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29740 return;
[email protected]aa4117f2011-12-09 22:19:21741 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24742 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
743 return;
744 }
[email protected]9ca84622011-06-02 23:46:39745 if (is_accelerated_compositing_active_ &&
746 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24747 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
748 return;
749 }
initial.commit09911bf2008-07-26 23:55:29750
[email protected]552e6002009-11-19 05:24:57751 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29752 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57753 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29754 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24755 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29756 return;
757 }
758
[email protected]0fb93f52011-05-18 23:13:56759 // Tracking of frame rate jitter
760 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]52ccd0ea2011-02-16 01:09:05761 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50762
[email protected]f98d7e3c2010-09-13 22:30:46763 // Layout may generate more invalidation. It may also enable the
764 // GPU acceleration, so make sure to run layout before we send the
765 // GpuRenderingActivated message.
766 webwidget_->layout();
767
[email protected]5f8b1022011-01-21 23:34:50768 // Suppress painting if nothing is dirty. This has to be done after updating
769 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24770 if (!paint_aggregator_.HasPendingUpdate()) {
771 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]5f8b1022011-01-21 23:34:50772 return;
[email protected]65225772011-05-12 21:10:24773 }
[email protected]5f8b1022011-01-21 23:34:50774
[email protected]872ae5b2011-05-26 20:20:50775 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56776 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04777 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56778 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
779 delay,
780 base::TimeDelta::FromMilliseconds(1),
781 base::TimeDelta::FromMilliseconds(60),
782 30);
[email protected]d0be63772011-12-20 23:18:04783 } else {
[email protected]0fb93f52011-05-18 23:13:56784 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
785 delay,
786 base::TimeDelta::FromMilliseconds(1),
787 base::TimeDelta::FromMilliseconds(60),
788 30);
[email protected]d0be63772011-12-20 23:18:04789 }
[email protected]872ae5b2011-05-26 20:20:50790
791 // Calculate filtered time per frame:
792 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
793 filtered_time_per_frame_ =
794 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56795 }
796 last_do_deferred_update_time_ = frame_begin_ticks;
797
[email protected]552e6002009-11-19 05:24:57798 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29799 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30800 PaintAggregator::PendingUpdate update;
801 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29802
[email protected]53d3f302009-12-21 04:42:05803 gfx::Rect scroll_damage = update.GetScrollDamage();
804 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29805
[email protected]ca4847f2010-09-24 05:39:15806 // A plugin may be able to do an optimized paint. First check this, in which
807 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46808 // This optimization allows PPAPI plugins that declare themselves on top of
809 // the page (like a traditional windowed plugin) to be able to animate (think
810 // movie playing) without repeatedly re-painting the page underneath, or
811 // copying the plugin backing store (since we can send the plugin's backing
812 // store directly to the browser).
813 //
814 // This optimization only works when the entire invalid region is contained
815 // within the plugin. There is a related optimization in PaintRect for the
816 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15817 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15818 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]aa4117f2011-12-09 22:19:21819 DCHECK(!pending_update_params_.get());
820 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
821 pending_update_params_->dx = update.scroll_delta.x();
822 pending_update_params_->dy = update.scroll_delta.y();
823 pending_update_params_->scroll_rect = update.scroll_rect;
824 pending_update_params_->view_size = size_;
825 pending_update_params_->resizer_rect = resizer_rect_;
826 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
827 pending_update_params_->flags = next_paint_flags_;
828 pending_update_params_->scroll_offset = GetScrollOffset();
829 pending_update_params_->needs_ack = true;
830 next_paint_flags_ = 0;
831
[email protected]ca4847f2010-09-24 05:39:15832 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56833 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15834 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
835 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27836 // Only update the part of the plugin that actually changed.
837 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21838 pending_update_params_->bitmap = dib->id();
839 pending_update_params_->bitmap_rect = optimized_copy_location;
840 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]a79d8a632010-11-18 22:35:56841 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46842 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15843 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35844 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
845 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46846 if (!canvas.get()) {
847 NOTREACHED();
848 return;
849 }
[email protected]cef3362f2009-12-21 17:48:45850
[email protected]f98d7e3c2010-09-13 22:30:46851 // We may get back a smaller canvas than we asked for.
852 // TODO(darin): This seems like it could cause painting problems!
853 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
854 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
855 bounds.set_width(canvas->getDevice()->width());
856 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05857
[email protected]f98d7e3c2010-09-13 22:30:46858 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
859
[email protected]aa4117f2011-12-09 22:19:21860 pending_update_params_->bitmap = current_paint_buf_->id();
861 pending_update_params_->bitmap_rect = bounds;
862
863 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:46864 // The scroll damage is just another rectangle to paint and copy.
865 copy_rects.swap(update.paint_rects);
866 if (!scroll_damage.IsEmpty())
867 copy_rects.push_back(scroll_damage);
868
869 for (size_t i = 0; i < copy_rects.size(); ++i)
870 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
871 } else { // Accelerated compositing path
872 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:21873 // If painting is done via the gpu process then we don't set any damage
874 // rects to save the browser process from doing unecessary work.
875 pending_update_params_->bitmap_rect = bounds;
876 pending_update_params_->scroll_rect = gfx::Rect();
877 // We don't need an ack, because we're not sharing a DIB with the browser.
878 // If it needs to (e.g. composited UI), the GPU process does its own ACK
879 // with the browser for the GPU surface.
880 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:42881 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46882 }
883
[email protected]936c6f52011-12-13 01:35:26884 // If we're holding a pending input event ACK, send the ACK before sending the
885 // UpdateReply message so we can receive another input event before the
886 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
887 // the UpdateRect IPC message handler.
888 if (pending_input_event_ack_.get())
889 Send(pending_input_event_ack_.release());
890
[email protected]aa4117f2011-12-09 22:19:21891 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
892 // OnSwapBuffersPosted), meaning a message has been added to the
893 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
894 // the message now.
895 if (pending_update_params_.get()) {
896 // sending an ack to browser process that the paint is complete...
897 update_reply_pending_ = pending_update_params_->needs_ack;
898 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
899 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:34900 }
[email protected]53d3f302009-12-21 04:42:05901
[email protected]e99ef6f2011-10-16 01:13:00902 UpdateTextInputState();
903 UpdateSelectionBounds();
[email protected]00c39612010-03-06 02:53:28904
905 // Let derived classes know we've painted.
906 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29907}
908
909///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46910// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29911
[email protected]4873c7d2009-07-16 06:36:28912void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57913 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48914 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57915 gfx::Rect damaged_rect = view_rect.Intersect(rect);
916 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29917 return;
918
[email protected]552e6002009-11-19 05:24:57919 paint_aggregator_.InvalidateRect(damaged_rect);
920
921 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24922 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57923 return;
924 if (!paint_aggregator_.HasPendingUpdate())
925 return;
[email protected]aa4117f2011-12-09 22:19:21926 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:24927 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
928 return;
929
930 // When GPU rendering, combine pending animations and invalidations into
931 // a single update.
932 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57933 return;
934
935 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29936 // 1) Ensures that we call WebView::Paint without a bunch of other junk
937 // on the call stack.
938 // 2) Allows us to collect more damage rects before painting to help coalesce
939 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24940 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21941 MessageLoop::current()->PostTask(
942 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:29943}
944
[email protected]4873c7d2009-07-16 06:36:28945void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46946 // Drop scrolls on the floor when we are in compositing mode.
947 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56948 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46949 return;
950
[email protected]552e6002009-11-19 05:24:57951 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48952 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57953 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
954 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29955 return;
956
[email protected]552e6002009-11-19 05:24:57957 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
958
959 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24960 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57961 return;
962 if (!paint_aggregator_.HasPendingUpdate())
963 return;
[email protected]aa4117f2011-12-09 22:19:21964 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:24965 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
966 return;
967
968 // When GPU rendering, combine pending animations and invalidations into
969 // a single update.
970 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57971 return;
972
973 // Perform updating asynchronously. This serves two purposes:
974 // 1) Ensures that we call WebView::Paint without a bunch of other junk
975 // on the call stack.
976 // 2) Allows us to collect more damage rects before painting to help coalesce
977 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24978 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21979 MessageLoop::current()->PostTask(
980 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:29981}
982
[email protected]244ac1892011-12-02 17:04:47983void RenderWidget::didAutoResize(const WebSize& new_size) {
984 size_ = new_size;
985}
986
[email protected]3da12cb2011-10-08 02:25:04987void RenderWidget::didActivateCompositor(int compositor_identifier) {
[email protected]ea162f92011-10-04 23:08:22988 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
989
[email protected]f3150172011-10-22 02:28:45990 CompositorThread* compositor_thread =
991 RenderThreadImpl::current()->compositor_thread();
992 if (compositor_thread)
993 compositor_thread->AddCompositor(routing_id_, compositor_identifier);
[email protected]3da12cb2011-10-08 02:25:04994
[email protected]aa4117f2011-12-09 22:19:21995 if (!is_accelerated_compositing_active_) {
996 // When not in accelerated compositing mode, in certain cases (e.g. waiting
997 // for a resize or if no backing store) the RenderWidgetHost is blocking the
998 // browser's UI thread for some time, waiting for an UpdateRect. If we are
999 // going to switch to accelerated compositing, the GPU process may need
1000 // round-trips to the browser's UI thread before finishing the frame,
1001 // causing deadlocks if we delay the UpdateRect until we receive the
1002 // OnSwapBuffersComplete. So send a dummy message that will unblock the
1003 // browser's UI thread.
1004 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1005 }
1006
[email protected]ea162f92011-10-04 23:08:221007 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421008 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241009 routing_id_, is_accelerated_compositing_active_));
1010
[email protected]c3d45532011-10-07 19:20:401011 // Note: asynchronous swapbuffer support currently only matters if
1012 // compositing scheduling happens on the RenderWidget.
[email protected]ea162f92011-10-04 23:08:221013 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
[email protected]ea162f92011-10-04 23:08:221014}
1015
1016void RenderWidget::didDeactivateCompositor() {
1017 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1018
1019 is_accelerated_compositing_active_ = false;
1020 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1021 routing_id_, is_accelerated_compositing_active_));
1022
[email protected]ea162f92011-10-04 23:08:221023 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241024 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561025}
1026
[email protected]58264a32011-11-17 23:36:151027void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501028 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
1029 // Notify subclasses.
1030 DidFlushPaint();
[email protected]58264a32011-11-17 23:36:151031}
1032
1033void RenderWidget::didCompleteSwapBuffers() {
[email protected]aa4117f2011-12-09 22:19:211034 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151035 return;
1036
1037 if (!next_paint_flags_ && !plugin_window_moves_.size())
1038 return;
1039
1040 ViewHostMsg_UpdateRect_Params params;
1041 params.view_size = size_;
1042 params.resizer_rect = resizer_rect_;
1043 params.plugin_window_moves.swap(plugin_window_moves_);
1044 params.flags = next_paint_flags_;
1045 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121046 params.needs_ack = false;
[email protected]58264a32011-11-17 23:36:151047
1048 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1049 next_paint_flags_ = 0;
1050}
1051
[email protected]f98d7e3c2010-09-13 22:30:461052void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041053 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401054 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041055 } else {
[email protected]c3d45532011-10-07 19:20:401056 // TODO(nduca): replace with something a little less hacky. The reason this
1057 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1058 // contains a lot of host-renderer synchronization logic that is still
1059 // important for the accelerated compositing case. The option of simply
1060 // duplicating all that code is less desirable than "faking out" the
1061 // invalidation path using a magical damage rect.
1062 didInvalidateRect(WebRect(0, 0, 1, 1));
1063 }
[email protected]f98d7e3c2010-09-13 22:30:461064}
1065
[email protected]5f8b1022011-01-21 23:34:501066void RenderWidget::scheduleAnimation() {
[email protected]921244e42011-07-20 16:36:301067 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ee3d3ad2011-02-04 00:42:211068 if (!animation_update_pending_) {
1069 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:051070 if (!animation_task_posted_) {
1071 animation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211072 MessageLoop::current()->PostTask(
1073 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this));
[email protected]52ccd0ea2011-02-16 01:09:051074 }
[email protected]ee3d3ad2011-02-04 00:42:211075 }
[email protected]5f8b1022011-01-21 23:34:501076}
1077
[email protected]4873c7d2009-07-16 06:36:281078void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301079 // TODO(darin): Eliminate this temporary.
1080 WebCursor cursor(cursor_info);
1081
initial.commit09911bf2008-07-26 23:55:291082 // Only send a SetCursor message if we need to make a change.
1083 if (!current_cursor_.IsEqual(cursor)) {
1084 current_cursor_ = cursor;
1085 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1086 }
1087}
1088
1089// We are supposed to get a single call to Show for a newly created RenderWidget
1090// that was created via RenderWidget::CreateWebView. So, we wait until this
1091// point to dispatch the ShowWidget message.
1092//
1093// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281094// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291095//
[email protected]4873c7d2009-07-16 06:36:281096void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291097 DCHECK(!did_show_) << "received extraneous Show call";
1098 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1099 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1100
[email protected]8de12d942010-11-17 20:42:441101 if (did_show_)
1102 return;
1103
1104 did_show_ = true;
1105 // NOTE: initial_pos_ may still have its default values at this point, but
1106 // that's okay. It'll be ignored if as_popup is false, or the browser
1107 // process will impose a default position otherwise.
1108 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1109 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291110}
1111
[email protected]4873c7d2009-07-16 06:36:281112void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291113}
1114
[email protected]4873c7d2009-07-16 06:36:281115void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291116}
1117
[email protected]2533ce12009-05-09 00:02:241118void RenderWidget::DoDeferredClose() {
1119 Send(new ViewHostMsg_Close(routing_id_));
1120}
1121
[email protected]4873c7d2009-07-16 06:36:281122void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:291123 // If a page calls window.close() twice, we'll end up here twice, but that's
1124 // OK. It is safe to send multiple Close messages.
1125
[email protected]2533ce12009-05-09 00:02:241126 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1127 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1128 // could be closed before the JS finishes executing. So instead, post a
1129 // message back to the message loop, which won't run until the JS is
1130 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211131 MessageLoop::current()->PostTask(
1132 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291133}
1134
1135void RenderWidget::Close() {
1136 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281137 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291138 webwidget_ = NULL;
1139 }
1140}
1141
[email protected]4873c7d2009-07-16 06:36:281142WebRect RenderWidget::windowRect() {
1143 if (pending_window_rect_count_)
1144 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241145
[email protected]b3f2b912009-04-09 16:18:521146 gfx::Rect rect;
1147 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281148 return rect;
initial.commit09911bf2008-07-26 23:55:291149}
1150
[email protected]8a9d6ca32011-06-06 20:11:301151void RenderWidget::setToolTipText(const WebKit::WebString& text,
1152 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541153 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301154}
1155
[email protected]4873c7d2009-07-16 06:36:281156void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291157 if (did_show_) {
1158 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241159 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291160 } else {
1161 initial_pos_ = pos;
1162 }
1163}
1164
[email protected]2533ce12009-05-09 00:02:241165void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1166 pending_window_rect_ = rect;
1167 pending_window_rect_count_++;
1168}
1169
[email protected]4873c7d2009-07-16 06:36:281170WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241171 if (pending_window_rect_count_) {
1172 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1173 // the RootWindowRect is probably going to return wrong results since the
1174 // browser may not have processed the Move yet. There isn't really anything
1175 // good to do in this case, and it shouldn't happen - since this size is
1176 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281177 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241178 }
1179
[email protected]b3f2b912009-04-09 16:18:521180 gfx::Rect rect;
1181 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281182 return rect;
[email protected]d4547452008-08-28 18:36:371183}
1184
[email protected]4873c7d2009-07-16 06:36:281185WebRect RenderWidget::windowResizerRect() {
1186 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191187}
1188
[email protected]fa7b1dc2010-06-23 17:53:041189void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031190 // To prevent this renderer process from sending unnecessary IPC messages to
1191 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041192 // only during the input method attached to the browser process is active.
1193 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291194}
1195
[email protected]fa7b1dc2010-06-23 17:53:041196void RenderWidget::OnImeSetComposition(
1197 const string16& text,
1198 const std::vector<WebCompositionUnderline>& underlines,
1199 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281200 if (!webwidget_)
1201 return;
[email protected]d4cff272011-05-02 15:46:011202 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041203 text, WebVector<WebCompositionUnderline>(underlines),
1204 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011205 // Setting the IME composition was successful. Send the new composition
1206 // range to the browser.
1207 ui::Range range(ui::Range::InvalidRange());
1208 size_t location, length;
1209 if (webwidget_->compositionRange(&location, &length)) {
1210 range.set_start(location);
1211 range.set_end(location + length);
1212 }
1213 // The IME was cancelled via the Esc key, so just send back the caret.
1214 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1215 range.set_start(location);
1216 range.set_end(location + length);
1217 }
1218 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
1219 } else {
[email protected]fa7b1dc2010-06-23 17:53:041220 // If we failed to set the composition text, then we need to let the browser
1221 // process to cancel the input method's ongoing composition session, to make
1222 // sure we are in a consistent state.
1223 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011224
1225 // Send an updated IME range with just the caret range.
1226 ui::Range range(ui::Range::InvalidRange());
1227 size_t location, length;
1228 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1229 range.set_start(location);
1230 range.set_end(location + length);
1231 }
1232 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:261233 }
[email protected]fa7b1dc2010-06-23 17:53:041234}
1235
[email protected]4de6d1692011-10-12 08:45:441236void RenderWidget::OnImeConfirmComposition(
1237 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041238 if (!webwidget_)
1239 return;
1240
1241 handling_input_event_ = true;
1242 webwidget_->confirmComposition(text);
1243 handling_input_event_ = false;
1244
[email protected]d4cff272011-05-02 15:46:011245 // Send an updated IME range with just the caret range.
1246 ui::Range range(ui::Range::InvalidRange());
1247 size_t location, length;
1248 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1249 range.set_start(location);
1250 range.set_end(location + length);
1251 }
1252 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:291253}
1254
[email protected]948f7ab72010-05-28 23:48:081255// This message causes the renderer to render an image of the
1256// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491257void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091258 int tag,
[email protected]948f7ab72010-05-28 23:48:081259 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491260 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001261 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1262 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251263 // Close our unused handle.
1264#if defined(OS_WIN)
1265 ::CloseHandle(dib_handle);
1266#elif defined(OS_MACOSX)
1267 base::SharedMemory::CloseHandle(dib_handle);
1268#endif
1269 }
[email protected]d65adb12010-04-28 17:26:491270 return;
[email protected]45c6aad32010-11-11 04:46:251271 }
[email protected]d65adb12010-04-28 17:26:491272
[email protected]948f7ab72010-05-28 23:48:081273 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491274 // If one of these is empty, then we just return the dib we were
1275 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091276 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491277 return;
1278 }
1279
1280 // Map the given DIB ID into this process, and unmap it at the end
1281 // of this function.
[email protected]45c6aad32010-11-11 04:46:251282 scoped_ptr<TransportDIB> paint_at_size_buffer(
1283 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301284
1285 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491286 float x_scale = static_cast<float>(desired_size.width()) /
1287 static_cast<float>(canvas_size.width());
1288 float y_scale = static_cast<float>(desired_size.height()) /
1289 static_cast<float>(canvas_size.height());
1290
[email protected]ee8d6fd2010-05-26 17:05:481291 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491292 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1293 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481294 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491295
[email protected]36808ad2010-10-20 19:18:301296 scoped_ptr<skia::PlatformCanvas> canvas(
1297 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1298 canvas_size.height()));
1299 if (!canvas.get()) {
1300 NOTREACHED();
1301 return;
1302 }
1303
[email protected]d65adb12010-04-28 17:26:491304 // Reset bounds to what we actually received, but they should be the
1305 // same.
1306 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1307 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1308 bounds.set_width(canvas->getDevice()->width());
1309 bounds.set_height(canvas->getDevice()->height());
1310
1311 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081312 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491313 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1314
[email protected]948f7ab72010-05-28 23:48:081315 // Have to make sure we're laid out at the right size before
1316 // rendering.
1317 gfx::Size old_size = webwidget_->size();
1318 webwidget_->resize(page_size);
1319 webwidget_->layout();
1320
[email protected]d65adb12010-04-28 17:26:491321 // Paint the entire thing (using original bounds, not scaled bounds).
1322 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1323 canvas->restore();
1324
[email protected]948f7ab72010-05-28 23:48:081325 // Return the widget to its previous size.
1326 webwidget_->resize(old_size);
1327
[email protected]c88c9442010-07-19 18:55:091328 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491329}
1330
[email protected]ec7dc112008-08-06 05:30:121331void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1332 // During shutdown we can just ignore this message.
1333 if (!webwidget_)
1334 return;
1335
1336 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561337 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461338 scheduleComposite();
1339 } else {
1340 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1341 didInvalidateRect(repaint_rect);
1342 }
[email protected]ec7dc112008-08-06 05:30:121343}
1344
[email protected]4873c7d2009-07-16 06:36:281345void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111346 if (!webwidget_)
1347 return;
[email protected]4873c7d2009-07-16 06:36:281348 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111349}
1350
[email protected]719b36f2010-12-22 20:36:461351webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151352 const gfx::Rect& paint_bounds,
1353 TransportDIB** dib,
1354 gfx::Rect* location,
1355 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461356 // Bare RenderWidgets don't support optimized plugin painting.
1357 return NULL;
[email protected]ca4847f2010-09-24 05:39:151358}
1359
[email protected]bcaf2272011-02-15 15:29:431360gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521361 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431362 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521363}
1364
[email protected]bee16aab2009-08-26 15:55:031365void RenderWidget::SetHidden(bool hidden) {
1366 if (is_hidden_ == hidden)
1367 return;
1368
1369 // The status has changed. Tell the RenderThread about it.
1370 is_hidden_ = hidden;
1371 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271372 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031373 else
[email protected]380244092011-10-07 17:26:271374 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031375}
1376
[email protected]2b624c562011-10-27 22:58:261377void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261378 if (!webwidget_)
1379 return;
1380
1381 if (is_fullscreen_) {
1382 webwidget_->willExitFullScreen();
1383 } else {
1384 webwidget_->willEnterFullScreen();
1385 }
[email protected]2b624c562011-10-27 22:58:261386}
1387
1388void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261389 if (!webwidget_)
1390 return;
1391
1392 if (is_fullscreen_) {
1393 webwidget_->didEnterFullScreen();
1394 } else {
1395 webwidget_->didExitFullScreen();
1396 }
[email protected]2b624c562011-10-27 22:58:261397}
1398
[email protected]699ab0d2009-04-23 23:19:141399void RenderWidget::SetBackground(const SkBitmap& background) {
1400 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461401
[email protected]699ab0d2009-04-23 23:19:141402 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281403 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141404}
1405
[email protected]674741932009-02-04 23:44:461406bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051407 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461408}
1409
1410bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051411 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461412}
1413
1414void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051415 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461416}
1417
1418void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051419 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461420}
1421
1422void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051423 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461424}
1425
[email protected]e99ef6f2011-10-16 01:13:001426void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041427 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291428 return;
[email protected]fa7b1dc2010-06-23 17:53:041429
[email protected]ad26ef42011-06-17 07:59:451430 ui::TextInputType new_type = GetTextInputType();
1431 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001432 // Only sends text input type and compose inline to the browser process if
1433 // they are changed.
1434 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451435 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041436 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451437 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001438 Send(new ViewHostMsg_TextInputStateChanged(
1439 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291440 }
initial.commit09911bf2008-07-26 23:55:291441}
1442
[email protected]3f783362011-10-21 22:40:501443void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1444 WebRect start_webrect;
1445 WebRect end_webrect;
1446 webwidget_->selectionBounds(start_webrect, end_webrect);
1447 *start = start_webrect;
1448 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321449}
1450
[email protected]e99ef6f2011-10-16 01:13:001451void RenderWidget::UpdateSelectionBounds() {
1452 if (!webwidget_)
1453 return;
1454
[email protected]3f783362011-10-21 22:40:501455 gfx::Rect start_rect;
1456 gfx::Rect end_rect;
1457 GetSelectionBounds(&start_rect, &end_rect);
[email protected]e99ef6f2011-10-16 01:13:001458 if (selection_start_rect_ == start_rect && selection_end_rect_ == end_rect)
1459 return;
1460
1461 selection_start_rect_ = start_rect;
1462 selection_end_rect_ = end_rect;
1463 Send(new ViewHostMsg_SelectionBoundsChanged(
1464 routing_id_, selection_start_rect_, selection_end_rect_));
1465}
1466
[email protected]73bf95812011-10-12 11:38:321467// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451468COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1469 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1470COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1471 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1472COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1473 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181474COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1475 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1476COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1477 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1478COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1479 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1480COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1481 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1482COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1483 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451484
1485ui::TextInputType RenderWidget::GetTextInputType() {
1486 if (webwidget_) {
1487 int type = webwidget_->textInputType();
1488 // Check the type is in the range representable by ui::TextInputType.
[email protected]caf38ed2011-07-28 13:15:181489 DCHECK(type <= ui::TEXT_INPUT_TYPE_URL) <<
[email protected]ad26ef42011-06-17 07:59:451490 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1491 return static_cast<ui::TextInputType>(type);
1492 }
1493 return ui::TEXT_INPUT_TYPE_NONE;
1494}
1495
1496bool RenderWidget::CanComposeInline() {
1497 return true;
[email protected]56ea1a62011-05-30 07:05:571498}
1499
[email protected]4873c7d2009-07-16 06:36:281500WebScreenInfo RenderWidget::screenInfo() {
1501 WebScreenInfo results;
1502 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1503 return results;
1504}
1505
[email protected]fa7b1dc2010-06-23 17:53:041506void RenderWidget::resetInputMethod() {
1507 if (!input_method_is_active_)
1508 return;
1509
1510 // If the last text input type is not None, then we should finish any
1511 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451512 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041513 // If a composition text exists, then we need to let the browser process
1514 // to cancel the input method's ongoing composition session.
1515 if (webwidget_->confirmComposition())
1516 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1517 }
[email protected]d4cff272011-05-02 15:46:011518
1519 // Send an updated IME range with the current caret rect.
1520 ui::Range range(ui::Range::InvalidRange());
1521 size_t location, length;
1522 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1523 range.set_start(location);
1524 range.set_end(location + length);
1525 }
1526 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041527}
1528
[email protected]f103ab72009-09-02 17:10:591529void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501530 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291531 size_t i = 0;
1532 for (; i < plugin_window_moves_.size(); ++i) {
1533 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581534 if (move.rects_valid) {
1535 plugin_window_moves_[i] = move;
1536 } else {
1537 plugin_window_moves_[i].visible = move.visible;
1538 }
initial.commit09911bf2008-07-26 23:55:291539 break;
1540 }
1541 }
1542
1543 if (i == plugin_window_moves_.size())
1544 plugin_window_moves_.push_back(move);
1545}
[email protected]268654772009-08-06 23:02:041546
1547void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1548 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1549 i != plugin_window_moves_.end(); ++i) {
1550 if (i->window == window) {
1551 plugin_window_moves_.erase(i);
1552 break;
1553 }
1554 }
1555}
[email protected]67bfb83f2011-09-22 03:36:371556
1557bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1558 return false;
1559}
[email protected]c3d45532011-10-07 19:20:401560
1561bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1562 return false;
1563}