blob: ff139dd5d5104f61dabd98f1f641e1741a56a181 [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 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]9f4f3322012-01-18 22:29:5673 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:1174 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2975 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73fa2009-10-13 13:43:4276 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3577 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2978 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0679 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0580 update_reply_pending_(false),
[email protected]65225772011-05-12 21:10:2481 using_asynchronous_swapbuffers_(false),
82 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2983 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2984 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0985 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2986 needs_repainting_on_restore_(false),
87 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4988 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4889 closing_(false),
[email protected]992db4c2011-05-12 15:37:1590 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0491 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:4592 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
93 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:1294 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4895 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:1296 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5097 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2198 animation_update_pending_(false),
[email protected]65225772011-05-12 21:10:2499 animation_task_posted_(false),
100 invalidation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:28101 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27102 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18103 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
104 switches::kDisableGpuVsync);
initial.commit09911bf2008-07-26 23:55:29105}
106
107RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11108 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21109 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35110 if (current_paint_buf_) {
111 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
112 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29113 }
[email protected]992db4c2011-05-12 15:37:15114 // If we are swapped out, we have released already.
115 if (!is_swapped_out_)
116 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29117}
118
[email protected]484955942010-08-19 16:13:18119// static
[email protected]8085dbc82008-09-26 22:53:44120RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]3e2b375b2010-04-07 17:03:12121 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29122 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27123 scoped_refptr<RenderWidget> widget(new RenderWidget(popup_type));
initial.commit09911bf2008-07-26 23:55:29124 widget->Init(opener_id); // adds reference
125 return widget;
126}
127
[email protected]484955942010-08-19 16:13:18128// static
129WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
130 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03131 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18132 break;
133 case WebKit::WebPopupTypeSelect:
134 case WebKit::WebPopupTypeSuggestion:
135 return WebPopupMenu::create(render_widget);
136 default:
137 NOTREACHED();
138 }
139 return NULL;
140}
141
initial.commit09911bf2008-07-26 23:55:29142void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18143 DoInit(opener_id,
144 RenderWidget::CreateWebWidget(this),
[email protected]9f4f3322012-01-18 22:29:56145 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
146 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18147}
148
[email protected]484955942010-08-19 16:13:18149void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06150 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18151 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29152 DCHECK(!webwidget_);
153
154 if (opener_id != MSG_ROUTING_NONE)
155 opener_id_ = opener_id;
156
[email protected]484955942010-08-19 16:13:18157 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29158
[email protected]380244092011-10-07 17:26:27159 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29160 if (result) {
[email protected]380244092011-10-07 17:26:27161 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29162 // Take a reference on behalf of the RenderThread. This will be balanced
163 // when we receive ViewMsg_Close.
164 AddRef();
165 } else {
166 DCHECK(false);
167 }
168}
169
170// This is used to complete pending inits and non-pending inits. For non-
171// pending cases, the parent will be the same as the current parent. This
172// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55173void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29174 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29175
176 host_window_ = parent_hwnd;
177
[email protected]6de74452009-02-25 18:04:59178 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29179}
180
[email protected]992db4c2011-05-12 15:37:15181void RenderWidget::SetSwappedOut(bool is_swapped_out) {
182 // We should only toggle between states.
183 DCHECK(is_swapped_out_ != is_swapped_out);
184 is_swapped_out_ = is_swapped_out;
185
186 // If we are swapping out, we will call ReleaseProcess, allowing the process
187 // to exit if all of its RenderViews are swapped out. We wait until the
188 // WasSwappedOut call to do this, to avoid showing the sad tab.
189 // If we are swapping in, we call AddRefProcess to prevent the process from
190 // exiting.
191 if (!is_swapped_out)
192 RenderProcess::current()->AddRefProcess();
193}
194
[email protected]a95986a82010-12-24 06:19:28195bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
196 bool handled = true;
197 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
198 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
199 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
200 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
201 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
202 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15203 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28204 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
205 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
206 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
207 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
208 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
209 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
210 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
211 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
212 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
213 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
214 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
215 IPC_MESSAGE_UNHANDLED(handled = false)
216 IPC_END_MESSAGE_MAP()
217 return handled;
218}
initial.commit09911bf2008-07-26 23:55:29219
220bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15221 // Don't send any messages after the browser has told us to close, and filter
222 // most outgoing messages while swapped out.
223 if ((is_swapped_out_ &&
224 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
225 closing_) {
initial.commit09911bf2008-07-26 23:55:29226 delete message;
227 return false;
228 }
229
230 // If given a messsage without a routing ID, then assign our routing ID.
231 if (message->routing_id() == MSG_ROUTING_NONE)
232 message->set_routing_id(routing_id_);
233
[email protected]380244092011-10-07 17:26:27234 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44235}
236
initial.commit09911bf2008-07-26 23:55:29237// Got a response from the browser after the renderer decided to create a new
238// view.
[email protected]c03a6e72011-04-19 21:42:06239void RenderWidget::OnCreatingNewAck(
[email protected]2d7c8552011-06-27 19:21:55240 gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29241 DCHECK(routing_id_ != MSG_ROUTING_NONE);
242
[email protected]2d7c8552011-06-27 19:21:55243 CompleteInit(parent);
initial.commit09911bf2008-07-26 23:55:29244}
245
246void RenderWidget::OnClose() {
247 if (closing_)
248 return;
249 closing_ = true;
250
251 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03252 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27253 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03254 SetHidden(false);
255 }
initial.commit09911bf2008-07-26 23:55:29256
initial.commit09911bf2008-07-26 23:55:29257 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25258 // now. Post a task that only gets invoked when there are no nested message
259 // loops.
[email protected]32876ae2011-11-15 22:25:21260 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29261 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25262
263 // Balances the AddRef taken when we called AddRoute.
264 Release();
initial.commit09911bf2008-07-26 23:55:29265}
266
[email protected]f21c613a2009-02-12 14:46:17267void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09268 const gfx::Rect& resizer_rect,
269 bool is_fullscreen) {
initial.commit09911bf2008-07-26 23:55:29270 // During shutdown we can just ignore this message.
271 if (!webwidget_)
272 return;
273
[email protected]e2356242010-11-16 22:33:03274 // We shouldn't be asked to resize to our current size.
275 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
276
[email protected]f21c613a2009-02-12 14:46:17277 // Remember the rect where the resize corner will be drawn.
278 resizer_rect_ = resizer_rect;
279
[email protected]2b624c562011-10-27 22:58:26280 // NOTE: We may have entered fullscreen mode without changing our size.
281 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
282 if (fullscreen_change)
283 WillToggleFullscreen();
[email protected]ee41e7d22011-10-14 19:34:09284 is_fullscreen_ = is_fullscreen;
initial.commit09911bf2008-07-26 23:55:29285
[email protected]2b624c562011-10-27 22:58:26286 if (size_ != new_size) {
287 // TODO(darin): We should not need to reset this here.
288 SetHidden(false);
289 needs_repainting_on_restore_ = false;
initial.commit09911bf2008-07-26 23:55:29290
[email protected]2b624c562011-10-27 22:58:26291 size_ = new_size;
[email protected]552e6002009-11-19 05:24:57292
[email protected]2b624c562011-10-27 22:58:26293 // We should not be sent a Resize message if we have not ACK'd the previous
294 DCHECK(!next_paint_is_resize_ack());
295
296 paint_aggregator_.ClearPendingUpdate();
297
298 // When resizing, we want to wait to paint before ACK'ing the resize. This
299 // ensures that we only resize as fast as we can paint. We only need to
300 // send an ACK if we are resized to a non-empty rect.
301 webwidget_->resize(new_size);
302 if (!new_size.IsEmpty()) {
303 if (!is_accelerated_compositing_active_) {
304 // Resize should have caused an invalidation of the entire view.
305 DCHECK(paint_aggregator_.HasPendingUpdate());
306 }
307
308 // We will send the Resize_ACK flag once we paint again.
309 set_next_paint_is_resize_ack();
[email protected]f98d7e3c2010-09-13 22:30:46310 }
initial.commit09911bf2008-07-26 23:55:29311 }
[email protected]2b624c562011-10-27 22:58:26312
313 if (fullscreen_change)
314 DidToggleFullscreen();
initial.commit09911bf2008-07-26 23:55:29315}
316
317void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31318 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29319 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03320 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29321}
322
323void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31324 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29325 // During shutdown we can just ignore this message.
326 if (!webwidget_)
327 return;
328
329 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03330 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29331
332 if (!needs_repainting && !needs_repainting_on_restore_)
333 return;
334 needs_repainting_on_restore_ = false;
335
[email protected]d65adb12010-04-28 17:26:49336 // Tag the next paint as a restore ack, which is picked up by
337 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29338 set_next_paint_is_restore_ack();
339
340 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56341 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46342 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
343 } else {
344 scheduleComposite();
345 }
initial.commit09911bf2008-07-26 23:55:29346}
347
[email protected]992db4c2011-05-12 15:37:15348void RenderWidget::OnWasSwappedOut() {
349 // If we have been swapped out and no one else is using this process,
350 // it's safe to exit now. If we get swapped back in, we will call
351 // AddRefProcess in SetSwappedOut.
352 if (is_swapped_out_)
353 RenderProcess::current()->ReleaseProcess();
354}
355
[email protected]53d3f302009-12-21 04:42:05356void RenderWidget::OnRequestMoveAck() {
357 DCHECK(pending_window_rect_count_);
358 pending_window_rect_count_--;
359}
360
361void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58362 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21363 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05364 update_reply_pending_ = false;
365
[email protected]b4d08452010-10-05 17:34:35366 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
367 // have no current paint buffer.
368 if (current_paint_buf_) {
369 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
370 current_paint_buf_ = NULL;
371 }
372
[email protected]65225772011-05-12 21:10:24373 // If swapbuffers is still pending, then defer the update until the
374 // swapbuffers occurs.
375 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
376 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
377 return;
378 }
379
[email protected]00c39612010-03-06 02:53:28380 // Notify subclasses.
[email protected]b5db7eb2011-11-29 09:11:50381 if (!is_accelerated_compositing_active_)
382 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25383
initial.commit09911bf2008-07-26 23:55:29384 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24385 DoDeferredUpdateAndSendInputAck();
386}
387
[email protected]d0be63772011-12-20 23:18:04388bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24389 return false;
390}
391
[email protected]d0be63772011-12-20 23:18:04392void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24393 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21394 while (!updates_pending_swap_.empty()) {
395 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
396 updates_pending_swap_.pop_front();
397 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
398 // compositing pass, hence doesn't require an UpdateRect message.
399 if (msg)
400 Send(msg);
401 }
[email protected]65225772011-05-12 21:10:24402 num_swapbuffers_complete_pending_ = 0;
403 using_asynchronous_swapbuffers_ = false;
404 // Schedule another frame so the compositor learns about it.
405 scheduleComposite();
406}
407
[email protected]37a6f302011-07-11 23:43:08408void RenderWidget::OnSwapBuffersPosted() {
409 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21410
411 if (using_asynchronous_swapbuffers_) {
412 ViewHostMsg_UpdateRect* msg = NULL;
413 // pending_update_params_ can be NULL if the swap doesn't correspond to an
414 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
415 // message.
416 if (pending_update_params_.get()) {
417 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
418 pending_update_params_.reset();
419 }
420 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08421 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21422 }
[email protected]37a6f302011-07-11 23:43:08423}
424
425void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24426 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
427 // When compositing deactivates, we reset the swapbuffers pending count. The
428 // swapbuffers acks may still arrive, however.
429 if (num_swapbuffers_complete_pending_ == 0) {
430 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
431 return;
432 }
[email protected]aa4117f2011-12-09 22:19:21433 DCHECK(!updates_pending_swap_.empty());
434 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
435 updates_pending_swap_.pop_front();
436 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
437 // compositing pass, hence doesn't require an UpdateRect message.
438 if (msg)
439 Send(msg);
[email protected]65225772011-05-12 21:10:24440 num_swapbuffers_complete_pending_--;
441
442 // If update reply is still pending, then defer the update until that reply
443 // occurs.
[email protected]d0be63772011-12-20 23:18:04444 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24445 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
446 return;
447 }
448
449 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06450 // when we were previously rendering. However, if an invalidation task is not
451 // posted, there may be software rendering work pending. In that case, don't
452 // early out.
453 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24454 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
455 return;
456 }
457
[email protected]65225772011-05-12 21:10:24458 // Continue painting if necessary...
459 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29460}
461
initial.commit09911bf2008-07-26 23:55:29462void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24463 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29464 void* iter = NULL;
465
466 const char* data;
467 int data_length;
[email protected]5dd768212009-08-13 23:34:49468 handling_input_event_ = true;
469 if (!message.ReadData(&iter, &data, &data_length)) {
470 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29471 return;
[email protected]5dd768212009-08-13 23:34:49472 }
initial.commit09911bf2008-07-26 23:55:29473
474 const WebInputEvent* input_event =
475 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48476
[email protected]b68a0e52011-12-08 15:11:12477 bool is_keyboard_shortcut = false;
478 // is_keyboard_shortcut flag is only available for RawKeyDown events.
479 if (input_event->type == WebInputEvent::RawKeyDown)
480 message.ReadBool(&iter, &is_keyboard_shortcut);
481
[email protected]67bfb83f2011-09-22 03:36:37482 bool prevent_default = false;
483 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26484 const WebMouseEvent& mouse_event =
485 *static_cast<const WebMouseEvent*>(input_event);
486 TRACE_EVENT2("renderer", "HandleMouseMove",
487 "x", mouse_event.x, "y", mouse_event.y);
488 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37489 }
490
491 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12492 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
493 suppress_next_char_events_ = false;
494 if (!processed && webwidget_)
495 processed = webwidget_->handleInputEvent(*input_event);
496 }
497
498 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
499 // it's not processed by webkit, then we need to suppress the upcoming Char
500 // events.
501 if (!processed && is_keyboard_shortcut)
502 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29503
[email protected]a9fb30aa2011-10-06 06:58:46504 IPC::Message* response =
505 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
506 processed);
[email protected]e2824412009-02-27 01:57:05507
[email protected]353a34c2010-05-28 23:35:17508 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03509 input_event->type == WebInputEvent::MouseWheel ||
510 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57511 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24512 // We want to rate limit the input events in this case, so we'll wait for
513 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17514 if (pending_input_event_ack_.get()) {
515 // As two different kinds of events could cause us to postpone an ack
516 // we send it now, if we have one pending. The Browser should never
517 // send us the same kind of event we are delaying the ack for.
518 Send(pending_input_event_ack_.release());
519 }
[email protected]12fbad812009-09-01 18:21:24520 pending_input_event_ack_.reset(response);
521 } else {
522 Send(response);
523 }
524
[email protected]5dd768212009-08-13 23:34:49525 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48526
[email protected]67bfb83f2011-09-22 03:36:37527 if (!prevent_default) {
528 if (WebInputEvent::isKeyboardEventType(input_event->type))
529 DidHandleKeyEvent();
530 if (WebInputEvent::isMouseEventType(input_event->type))
531 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24532 if (WebInputEvent::isTouchEventType(input_event->type))
533 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37534 }
initial.commit09911bf2008-07-26 23:55:29535}
536
537void RenderWidget::OnMouseCaptureLost() {
538 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28539 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29540}
541
542void RenderWidget::OnSetFocus(bool enable) {
543 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33544 if (webwidget_)
545 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29546}
547
548void RenderWidget::ClearFocus() {
549 // We may have got the focus from the browser before this gets processed, in
550 // which case we do not want to unfocus ourself.
551 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28552 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29553}
554
[email protected]2d5d09d52009-06-15 14:29:21555void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00556 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21557 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06558 TRACE_EVENT2("renderer", "PaintRect",
559 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00560 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21561
562 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00563 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
564 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03565
[email protected]699ab0d2009-04-23 23:19:14566 // If there is a custom background, tile it.
567 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14568 SkPaint paint;
569 SkShader* shader = SkShader::CreateBitmapShader(background_,
570 SkShader::kRepeat_TileMode,
571 SkShader::kRepeat_TileMode);
572 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20573
574 // Use kSrc_Mode to handle background_ transparency properly.
575 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
576
577 // Canvas could contain multiple update rects. Clip to given rect so that
578 // we don't accidentally clear other update rects.
579 canvas->save();
580 SkRect clip;
581 clip.set(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
582 SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()));
583 canvas->clipRect(clip);
[email protected]699ab0d2009-04-23 23:19:14584 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20585 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14586 }
587
[email protected]719b36f2010-12-22 20:36:46588 // First see if this rect is a plugin that can paint itself faster.
589 TransportDIB* optimized_dib = NULL;
590 gfx::Rect optimized_copy_rect, optimized_copy_location;
591 webkit::ppapi::PluginInstance* optimized_instance =
592 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
593 &optimized_copy_location,
594 &optimized_copy_rect);
595 if (optimized_instance) {
596 // This plugin can be optimize-painted and we can just ask it to paint
597 // itself. We don't actually need the TransportDIB in this case.
598 //
599 // This is an optimization for PPAPI plugins that know they're on top of
600 // the page content. If this rect is inside such a plugin, we can save some
601 // time and avoid re-rendering the page content which we know will be
602 // covered by the plugin later (this time can be significant, especially
603 // for a playing movie that is invalidating a lot).
604 //
605 // In the plugin movie case, hopefully the similar call to
606 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
607 // painting, because that avoids copying the plugin image to a different
608 // paint rect. Unfortunately, if anything on the page is animating other
609 // than the movie, it break this optimization since the union of the
610 // invalid regions will be larger than the plugin.
611 //
612 // This code optimizes that case, where we can still avoid painting in
613 // WebKit and filling the background (which can be slow) and just painting
614 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
615 // required.
616 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27617 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46618 } else {
619 // Normal painting case.
620 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
621
622 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35623 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46624 }
initial.commit09911bf2008-07-26 23:55:29625
[email protected]4fb66842009-12-04 21:41:00626 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00627 canvas->restore();
628}
629
630void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
631 skia::PlatformCanvas* canvas) {
632 static bool kPaintBorder =
633 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
634 if (!kPaintBorder)
635 return;
636
[email protected]53d3f302009-12-21 04:42:05637 // Cycle through these colors to help distinguish new paint rects.
638 const SkColor colors[] = {
639 SkColorSetARGB(0x3F, 0xFF, 0, 0),
640 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
641 SkColorSetARGB(0x3F, 0, 0, 0xFF),
642 };
643 static int color_selector = 0;
644
[email protected]4fb66842009-12-04 21:41:00645 SkPaint paint;
646 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05647 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00648 paint.setStrokeWidth(1);
649
650 SkIRect irect;
651 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
652 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29653}
654
[email protected]52ccd0ea2011-02-16 01:09:05655void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30656 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]52ccd0ea2011-02-16 01:09:05657 animation_task_posted_ = false;
[email protected]921244e42011-07-20 16:36:30658 if (!animation_update_pending_) {
659 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59660 return;
[email protected]921244e42011-07-20 16:36:30661 }
[email protected]bd37ae252011-06-03 01:28:18662 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59663 // Record when we fired (according to base::Time::Now()) relative to when
664 // we posted the task to quantify how much the base::Time/base::TimeTicks
665 // skew is affecting animations.
666 base::TimeDelta animation_callback_delay = base::Time::Now() -
667 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
668 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
669 animation_callback_delay,
670 base::TimeDelta::FromMilliseconds(0),
671 base::TimeDelta::FromMilliseconds(30),
672 25);
673 }
[email protected]65225772011-05-12 21:10:24674 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24675}
676
[email protected]52ccd0ea2011-02-16 01:09:05677void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59678 if (!animation_update_pending_)
679 return;
[email protected]bd37ae252011-06-03 01:28:18680
681 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]4f67d172012-01-16 02:38:51682 int animationInterval = IsRenderingVSynced() ? 16 : 0;
[email protected]bd37ae252011-06-03 01:28:18683
[email protected]7c4329e2011-02-18 22:02:59684 base::Time now = base::Time::Now();
[email protected]46d8c8292011-12-12 18:15:05685 if (now >= animation_floor_time_ || is_accelerated_compositing_active_) {
[email protected]921244e42011-07-20 16:36:30686 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]4f67d172012-01-16 02:38:51687 animation_floor_time_ = now +
688 base::TimeDelta::FromMilliseconds(animationInterval);
[email protected]bd37ae252011-06-03 01:28:18689 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59690 // running animation callbacks so that if a callback requests another
691 // we'll be sure to run it at the proper time.
[email protected]32876ae2011-11-15 22:25:21692 MessageLoop::current()->PostDelayedTask(
693 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this),
694 animationInterval);
[email protected]7c4329e2011-02-18 22:02:59695 animation_task_posted_ = true;
696 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30697 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59698 return;
[email protected]5f8b1022011-01-21 23:34:50699 }
[email protected]bd37ae252011-06-03 01:28:18700 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]7c4329e2011-02-18 22:02:59701 if (animation_task_posted_)
702 return;
703 // This code uses base::Time::Now() to calculate the floor and next fire
704 // time because javascript's Date object uses base::Time::Now(). The
705 // message loop uses base::TimeTicks, which on windows can have a
706 // different granularity than base::Time.
707 // The upshot of all this is that this function might be called before
708 // base::Time::Now() has advanced past the animation_floor_time_. To
709 // avoid exposing this delay to javascript, we keep posting delayed
710 // tasks until base::Time::Now() has advanced far enough.
[email protected]4f67d172012-01-16 02:38:51711 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
[email protected]7c4329e2011-02-18 22:02:59712 animation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21713 MessageLoop::current()->PostDelayedTask(
714 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this), delay);
[email protected]5f8b1022011-01-21 23:34:50715}
716
[email protected]bd37ae252011-06-03 01:28:18717bool RenderWidget::IsRenderingVSynced() {
718 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
719 // not caught by this check. This will lead to artificially low frame rates
720 // for people who force vsync off at a driver level and expect Chrome to speed
721 // up.
722 return !has_disable_gpu_vsync_switch_;
723}
724
[email protected]65225772011-05-12 21:10:24725void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06726 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24727 invalidation_task_posted_ = false;
728 DoDeferredUpdateAndSendInputAck();
729}
730
731void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05732 DoDeferredUpdate();
733
734 if (pending_input_event_ack_.get())
735 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21736}
737
[email protected]552e6002009-11-19 05:24:57738void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58739 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08740
[email protected]65225772011-05-12 21:10:24741 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29742 return;
[email protected]aa4117f2011-12-09 22:19:21743 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24744 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
745 return;
746 }
[email protected]9ca84622011-06-02 23:46:39747 if (is_accelerated_compositing_active_ &&
748 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24749 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
750 return;
751 }
initial.commit09911bf2008-07-26 23:55:29752
[email protected]552e6002009-11-19 05:24:57753 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29754 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57755 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29756 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24757 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29758 return;
759 }
760
[email protected]0fb93f52011-05-18 23:13:56761 // Tracking of frame rate jitter
762 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]52ccd0ea2011-02-16 01:09:05763 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50764
[email protected]f98d7e3c2010-09-13 22:30:46765 // Layout may generate more invalidation. It may also enable the
766 // GPU acceleration, so make sure to run layout before we send the
767 // GpuRenderingActivated message.
768 webwidget_->layout();
769
[email protected]5f8b1022011-01-21 23:34:50770 // Suppress painting if nothing is dirty. This has to be done after updating
771 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24772 if (!paint_aggregator_.HasPendingUpdate()) {
773 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]5f8b1022011-01-21 23:34:50774 return;
[email protected]65225772011-05-12 21:10:24775 }
[email protected]5f8b1022011-01-21 23:34:50776
[email protected]872ae5b2011-05-26 20:20:50777 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56778 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04779 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56780 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
781 delay,
782 base::TimeDelta::FromMilliseconds(1),
783 base::TimeDelta::FromMilliseconds(60),
784 30);
[email protected]d0be63772011-12-20 23:18:04785 } else {
[email protected]0fb93f52011-05-18 23:13:56786 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
787 delay,
788 base::TimeDelta::FromMilliseconds(1),
789 base::TimeDelta::FromMilliseconds(60),
790 30);
[email protected]d0be63772011-12-20 23:18:04791 }
[email protected]872ae5b2011-05-26 20:20:50792
793 // Calculate filtered time per frame:
794 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
795 filtered_time_per_frame_ =
796 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56797 }
798 last_do_deferred_update_time_ = frame_begin_ticks;
799
[email protected]552e6002009-11-19 05:24:57800 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29801 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30802 PaintAggregator::PendingUpdate update;
803 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29804
[email protected]53d3f302009-12-21 04:42:05805 gfx::Rect scroll_damage = update.GetScrollDamage();
806 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29807
[email protected]ca4847f2010-09-24 05:39:15808 // A plugin may be able to do an optimized paint. First check this, in which
809 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46810 // This optimization allows PPAPI plugins that declare themselves on top of
811 // the page (like a traditional windowed plugin) to be able to animate (think
812 // movie playing) without repeatedly re-painting the page underneath, or
813 // copying the plugin backing store (since we can send the plugin's backing
814 // store directly to the browser).
815 //
816 // This optimization only works when the entire invalid region is contained
817 // within the plugin. There is a related optimization in PaintRect for the
818 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15819 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15820 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]aa4117f2011-12-09 22:19:21821 DCHECK(!pending_update_params_.get());
822 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
823 pending_update_params_->dx = update.scroll_delta.x();
824 pending_update_params_->dy = update.scroll_delta.y();
825 pending_update_params_->scroll_rect = update.scroll_rect;
826 pending_update_params_->view_size = size_;
827 pending_update_params_->resizer_rect = resizer_rect_;
828 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
829 pending_update_params_->flags = next_paint_flags_;
830 pending_update_params_->scroll_offset = GetScrollOffset();
831 pending_update_params_->needs_ack = true;
832 next_paint_flags_ = 0;
833
[email protected]ca4847f2010-09-24 05:39:15834 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56835 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15836 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
837 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27838 // Only update the part of the plugin that actually changed.
839 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21840 pending_update_params_->bitmap = dib->id();
841 pending_update_params_->bitmap_rect = optimized_copy_location;
842 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]a79d8a632010-11-18 22:35:56843 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46844 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15845 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35846 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
847 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46848 if (!canvas.get()) {
849 NOTREACHED();
850 return;
851 }
[email protected]cef3362f2009-12-21 17:48:45852
[email protected]f98d7e3c2010-09-13 22:30:46853 // We may get back a smaller canvas than we asked for.
854 // TODO(darin): This seems like it could cause painting problems!
855 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
856 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
857 bounds.set_width(canvas->getDevice()->width());
858 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05859
[email protected]f98d7e3c2010-09-13 22:30:46860 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
861
[email protected]aa4117f2011-12-09 22:19:21862 pending_update_params_->bitmap = current_paint_buf_->id();
863 pending_update_params_->bitmap_rect = bounds;
864
865 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:46866 // The scroll damage is just another rectangle to paint and copy.
867 copy_rects.swap(update.paint_rects);
868 if (!scroll_damage.IsEmpty())
869 copy_rects.push_back(scroll_damage);
870
871 for (size_t i = 0; i < copy_rects.size(); ++i)
872 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:35873
874 // Software FPS tick for performance tests. The accelerated path traces the
875 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
876 // NOTE: Tests may break if this event is renamed or moved.
877 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:46878 } else { // Accelerated compositing path
879 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:21880 // If painting is done via the gpu process then we don't set any damage
881 // rects to save the browser process from doing unecessary work.
882 pending_update_params_->bitmap_rect = bounds;
883 pending_update_params_->scroll_rect = gfx::Rect();
884 // We don't need an ack, because we're not sharing a DIB with the browser.
885 // If it needs to (e.g. composited UI), the GPU process does its own ACK
886 // with the browser for the GPU surface.
887 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:42888 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46889 }
890
[email protected]936c6f52011-12-13 01:35:26891 // If we're holding a pending input event ACK, send the ACK before sending the
892 // UpdateReply message so we can receive another input event before the
893 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
894 // the UpdateRect IPC message handler.
895 if (pending_input_event_ack_.get())
896 Send(pending_input_event_ack_.release());
897
[email protected]aa4117f2011-12-09 22:19:21898 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
899 // OnSwapBuffersPosted), meaning a message has been added to the
900 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
901 // the message now.
902 if (pending_update_params_.get()) {
903 // sending an ack to browser process that the paint is complete...
904 update_reply_pending_ = pending_update_params_->needs_ack;
905 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
906 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:34907 }
[email protected]53d3f302009-12-21 04:42:05908
[email protected]e99ef6f2011-10-16 01:13:00909 UpdateTextInputState();
910 UpdateSelectionBounds();
[email protected]00c39612010-03-06 02:53:28911
912 // Let derived classes know we've painted.
913 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29914}
915
916///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46917// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29918
[email protected]4873c7d2009-07-16 06:36:28919void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57920 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48921 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57922 gfx::Rect damaged_rect = view_rect.Intersect(rect);
923 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29924 return;
925
[email protected]552e6002009-11-19 05:24:57926 paint_aggregator_.InvalidateRect(damaged_rect);
927
928 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24929 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57930 return;
931 if (!paint_aggregator_.HasPendingUpdate())
932 return;
[email protected]aa4117f2011-12-09 22:19:21933 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:24934 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
935 return;
936
937 // When GPU rendering, combine pending animations and invalidations into
938 // a single update.
939 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57940 return;
941
942 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29943 // 1) Ensures that we call WebView::Paint without a bunch of other junk
944 // on the call stack.
945 // 2) Allows us to collect more damage rects before painting to help coalesce
946 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24947 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21948 MessageLoop::current()->PostTask(
949 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:29950}
951
[email protected]4873c7d2009-07-16 06:36:28952void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46953 // Drop scrolls on the floor when we are in compositing mode.
954 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56955 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46956 return;
957
[email protected]552e6002009-11-19 05:24:57958 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48959 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57960 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
961 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29962 return;
963
[email protected]552e6002009-11-19 05:24:57964 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
965
966 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24967 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57968 return;
969 if (!paint_aggregator_.HasPendingUpdate())
970 return;
[email protected]aa4117f2011-12-09 22:19:21971 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:24972 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
973 return;
974
975 // When GPU rendering, combine pending animations and invalidations into
976 // a single update.
977 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57978 return;
979
980 // Perform updating asynchronously. This serves two purposes:
981 // 1) Ensures that we call WebView::Paint without a bunch of other junk
982 // on the call stack.
983 // 2) Allows us to collect more damage rects before painting to help coalesce
984 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24985 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21986 MessageLoop::current()->PostTask(
987 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:29988}
989
[email protected]244ac1892011-12-02 17:04:47990void RenderWidget::didAutoResize(const WebSize& new_size) {
991 size_ = new_size;
992}
993
[email protected]3da12cb2011-10-08 02:25:04994void RenderWidget::didActivateCompositor(int compositor_identifier) {
[email protected]ea162f92011-10-04 23:08:22995 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
996
[email protected]f3150172011-10-22 02:28:45997 CompositorThread* compositor_thread =
998 RenderThreadImpl::current()->compositor_thread();
999 if (compositor_thread)
1000 compositor_thread->AddCompositor(routing_id_, compositor_identifier);
[email protected]3da12cb2011-10-08 02:25:041001
[email protected]aa4117f2011-12-09 22:19:211002 if (!is_accelerated_compositing_active_) {
1003 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1004 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1005 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1006 // going to switch to accelerated compositing, the GPU process may need
1007 // round-trips to the browser's UI thread before finishing the frame,
1008 // causing deadlocks if we delay the UpdateRect until we receive the
1009 // OnSwapBuffersComplete. So send a dummy message that will unblock the
1010 // browser's UI thread.
1011 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1012 }
1013
[email protected]ea162f92011-10-04 23:08:221014 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421015 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241016 routing_id_, is_accelerated_compositing_active_));
1017
[email protected]c3d45532011-10-07 19:20:401018 // Note: asynchronous swapbuffer support currently only matters if
1019 // compositing scheduling happens on the RenderWidget.
[email protected]ea162f92011-10-04 23:08:221020 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
[email protected]ea162f92011-10-04 23:08:221021}
1022
1023void RenderWidget::didDeactivateCompositor() {
1024 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1025
1026 is_accelerated_compositing_active_ = false;
1027 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1028 routing_id_, is_accelerated_compositing_active_));
1029
[email protected]ea162f92011-10-04 23:08:221030 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241031 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561032}
1033
[email protected]58264a32011-11-17 23:36:151034void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501035 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351036 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1037 // NOTE: Tests may break if this event is renamed or moved.
1038 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]b5db7eb2011-11-29 09:11:501039 // Notify subclasses.
1040 DidFlushPaint();
[email protected]58264a32011-11-17 23:36:151041}
1042
1043void RenderWidget::didCompleteSwapBuffers() {
[email protected]aa4117f2011-12-09 22:19:211044 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151045 return;
1046
1047 if (!next_paint_flags_ && !plugin_window_moves_.size())
1048 return;
1049
1050 ViewHostMsg_UpdateRect_Params params;
1051 params.view_size = size_;
1052 params.resizer_rect = resizer_rect_;
1053 params.plugin_window_moves.swap(plugin_window_moves_);
1054 params.flags = next_paint_flags_;
1055 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121056 params.needs_ack = false;
[email protected]58264a32011-11-17 23:36:151057
1058 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1059 next_paint_flags_ = 0;
1060}
1061
[email protected]f98d7e3c2010-09-13 22:30:461062void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041063 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401064 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041065 } else {
[email protected]c3d45532011-10-07 19:20:401066 // TODO(nduca): replace with something a little less hacky. The reason this
1067 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1068 // contains a lot of host-renderer synchronization logic that is still
1069 // important for the accelerated compositing case. The option of simply
1070 // duplicating all that code is less desirable than "faking out" the
1071 // invalidation path using a magical damage rect.
1072 didInvalidateRect(WebRect(0, 0, 1, 1));
1073 }
[email protected]f98d7e3c2010-09-13 22:30:461074}
1075
[email protected]5f8b1022011-01-21 23:34:501076void RenderWidget::scheduleAnimation() {
[email protected]921244e42011-07-20 16:36:301077 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ee3d3ad2011-02-04 00:42:211078 if (!animation_update_pending_) {
1079 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:051080 if (!animation_task_posted_) {
1081 animation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211082 MessageLoop::current()->PostTask(
1083 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this));
[email protected]52ccd0ea2011-02-16 01:09:051084 }
[email protected]ee3d3ad2011-02-04 00:42:211085 }
[email protected]5f8b1022011-01-21 23:34:501086}
1087
[email protected]4873c7d2009-07-16 06:36:281088void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301089 // TODO(darin): Eliminate this temporary.
1090 WebCursor cursor(cursor_info);
1091
initial.commit09911bf2008-07-26 23:55:291092 // Only send a SetCursor message if we need to make a change.
1093 if (!current_cursor_.IsEqual(cursor)) {
1094 current_cursor_ = cursor;
1095 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1096 }
1097}
1098
1099// We are supposed to get a single call to Show for a newly created RenderWidget
1100// that was created via RenderWidget::CreateWebView. So, we wait until this
1101// point to dispatch the ShowWidget message.
1102//
1103// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281104// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291105//
[email protected]4873c7d2009-07-16 06:36:281106void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291107 DCHECK(!did_show_) << "received extraneous Show call";
1108 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1109 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1110
[email protected]8de12d942010-11-17 20:42:441111 if (did_show_)
1112 return;
1113
1114 did_show_ = true;
1115 // NOTE: initial_pos_ may still have its default values at this point, but
1116 // that's okay. It'll be ignored if as_popup is false, or the browser
1117 // process will impose a default position otherwise.
1118 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1119 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291120}
1121
[email protected]4873c7d2009-07-16 06:36:281122void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291123}
1124
[email protected]4873c7d2009-07-16 06:36:281125void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291126}
1127
[email protected]2533ce12009-05-09 00:02:241128void RenderWidget::DoDeferredClose() {
1129 Send(new ViewHostMsg_Close(routing_id_));
1130}
1131
[email protected]4873c7d2009-07-16 06:36:281132void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:291133 // If a page calls window.close() twice, we'll end up here twice, but that's
1134 // OK. It is safe to send multiple Close messages.
1135
[email protected]2533ce12009-05-09 00:02:241136 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1137 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1138 // could be closed before the JS finishes executing. So instead, post a
1139 // message back to the message loop, which won't run until the JS is
1140 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211141 MessageLoop::current()->PostTask(
1142 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291143}
1144
1145void RenderWidget::Close() {
1146 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281147 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291148 webwidget_ = NULL;
1149 }
1150}
1151
[email protected]4873c7d2009-07-16 06:36:281152WebRect RenderWidget::windowRect() {
1153 if (pending_window_rect_count_)
1154 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241155
[email protected]b3f2b912009-04-09 16:18:521156 gfx::Rect rect;
1157 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281158 return rect;
initial.commit09911bf2008-07-26 23:55:291159}
1160
[email protected]8a9d6ca32011-06-06 20:11:301161void RenderWidget::setToolTipText(const WebKit::WebString& text,
1162 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541163 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301164}
1165
[email protected]4873c7d2009-07-16 06:36:281166void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291167 if (did_show_) {
1168 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241169 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291170 } else {
1171 initial_pos_ = pos;
1172 }
1173}
1174
[email protected]2533ce12009-05-09 00:02:241175void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1176 pending_window_rect_ = rect;
1177 pending_window_rect_count_++;
1178}
1179
[email protected]4873c7d2009-07-16 06:36:281180WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241181 if (pending_window_rect_count_) {
1182 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1183 // the RootWindowRect is probably going to return wrong results since the
1184 // browser may not have processed the Move yet. There isn't really anything
1185 // good to do in this case, and it shouldn't happen - since this size is
1186 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281187 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241188 }
1189
[email protected]b3f2b912009-04-09 16:18:521190 gfx::Rect rect;
1191 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281192 return rect;
[email protected]d4547452008-08-28 18:36:371193}
1194
[email protected]4873c7d2009-07-16 06:36:281195WebRect RenderWidget::windowResizerRect() {
1196 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191197}
1198
[email protected]fa7b1dc2010-06-23 17:53:041199void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031200 // To prevent this renderer process from sending unnecessary IPC messages to
1201 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041202 // only during the input method attached to the browser process is active.
1203 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291204}
1205
[email protected]fa7b1dc2010-06-23 17:53:041206void RenderWidget::OnImeSetComposition(
1207 const string16& text,
1208 const std::vector<WebCompositionUnderline>& underlines,
1209 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281210 if (!webwidget_)
1211 return;
[email protected]d4cff272011-05-02 15:46:011212 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041213 text, WebVector<WebCompositionUnderline>(underlines),
1214 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011215 // Setting the IME composition was successful. Send the new composition
1216 // range to the browser.
1217 ui::Range range(ui::Range::InvalidRange());
1218 size_t location, length;
1219 if (webwidget_->compositionRange(&location, &length)) {
1220 range.set_start(location);
1221 range.set_end(location + length);
1222 }
1223 // The IME was cancelled via the Esc key, so just send back the caret.
1224 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1225 range.set_start(location);
1226 range.set_end(location + length);
1227 }
1228 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
1229 } else {
[email protected]fa7b1dc2010-06-23 17:53:041230 // If we failed to set the composition text, then we need to let the browser
1231 // process to cancel the input method's ongoing composition session, to make
1232 // sure we are in a consistent state.
1233 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011234
1235 // Send an updated IME range with just the caret range.
1236 ui::Range range(ui::Range::InvalidRange());
1237 size_t location, length;
1238 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1239 range.set_start(location);
1240 range.set_end(location + length);
1241 }
1242 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:261243 }
[email protected]fa7b1dc2010-06-23 17:53:041244}
1245
[email protected]4de6d1692011-10-12 08:45:441246void RenderWidget::OnImeConfirmComposition(
1247 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041248 if (!webwidget_)
1249 return;
1250
1251 handling_input_event_ = true;
1252 webwidget_->confirmComposition(text);
1253 handling_input_event_ = false;
1254
[email protected]d4cff272011-05-02 15:46:011255 // Send an updated IME range with just the caret range.
1256 ui::Range range(ui::Range::InvalidRange());
1257 size_t location, length;
1258 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1259 range.set_start(location);
1260 range.set_end(location + length);
1261 }
1262 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:291263}
1264
[email protected]948f7ab72010-05-28 23:48:081265// This message causes the renderer to render an image of the
1266// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491267void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091268 int tag,
[email protected]948f7ab72010-05-28 23:48:081269 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491270 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001271 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1272 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251273 // Close our unused handle.
1274#if defined(OS_WIN)
1275 ::CloseHandle(dib_handle);
1276#elif defined(OS_MACOSX)
1277 base::SharedMemory::CloseHandle(dib_handle);
1278#endif
1279 }
[email protected]d65adb12010-04-28 17:26:491280 return;
[email protected]45c6aad32010-11-11 04:46:251281 }
[email protected]d65adb12010-04-28 17:26:491282
[email protected]948f7ab72010-05-28 23:48:081283 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491284 // If one of these is empty, then we just return the dib we were
1285 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091286 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491287 return;
1288 }
1289
1290 // Map the given DIB ID into this process, and unmap it at the end
1291 // of this function.
[email protected]45c6aad32010-11-11 04:46:251292 scoped_ptr<TransportDIB> paint_at_size_buffer(
1293 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301294
1295 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491296 float x_scale = static_cast<float>(desired_size.width()) /
1297 static_cast<float>(canvas_size.width());
1298 float y_scale = static_cast<float>(desired_size.height()) /
1299 static_cast<float>(canvas_size.height());
1300
[email protected]ee8d6fd2010-05-26 17:05:481301 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491302 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1303 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481304 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491305
[email protected]36808ad2010-10-20 19:18:301306 scoped_ptr<skia::PlatformCanvas> canvas(
1307 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1308 canvas_size.height()));
1309 if (!canvas.get()) {
1310 NOTREACHED();
1311 return;
1312 }
1313
[email protected]d65adb12010-04-28 17:26:491314 // Reset bounds to what we actually received, but they should be the
1315 // same.
1316 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1317 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1318 bounds.set_width(canvas->getDevice()->width());
1319 bounds.set_height(canvas->getDevice()->height());
1320
1321 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081322 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491323 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1324
[email protected]948f7ab72010-05-28 23:48:081325 // Have to make sure we're laid out at the right size before
1326 // rendering.
1327 gfx::Size old_size = webwidget_->size();
1328 webwidget_->resize(page_size);
1329 webwidget_->layout();
1330
[email protected]d65adb12010-04-28 17:26:491331 // Paint the entire thing (using original bounds, not scaled bounds).
1332 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1333 canvas->restore();
1334
[email protected]948f7ab72010-05-28 23:48:081335 // Return the widget to its previous size.
1336 webwidget_->resize(old_size);
1337
[email protected]c88c9442010-07-19 18:55:091338 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491339}
1340
[email protected]ec7dc112008-08-06 05:30:121341void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1342 // During shutdown we can just ignore this message.
1343 if (!webwidget_)
1344 return;
1345
1346 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561347 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461348 scheduleComposite();
1349 } else {
1350 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1351 didInvalidateRect(repaint_rect);
1352 }
[email protected]ec7dc112008-08-06 05:30:121353}
1354
[email protected]4873c7d2009-07-16 06:36:281355void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111356 if (!webwidget_)
1357 return;
[email protected]4873c7d2009-07-16 06:36:281358 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111359}
1360
[email protected]719b36f2010-12-22 20:36:461361webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151362 const gfx::Rect& paint_bounds,
1363 TransportDIB** dib,
1364 gfx::Rect* location,
1365 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461366 // Bare RenderWidgets don't support optimized plugin painting.
1367 return NULL;
[email protected]ca4847f2010-09-24 05:39:151368}
1369
[email protected]bcaf2272011-02-15 15:29:431370gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521371 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431372 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521373}
1374
[email protected]bee16aab2009-08-26 15:55:031375void RenderWidget::SetHidden(bool hidden) {
1376 if (is_hidden_ == hidden)
1377 return;
1378
1379 // The status has changed. Tell the RenderThread about it.
1380 is_hidden_ = hidden;
1381 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271382 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031383 else
[email protected]380244092011-10-07 17:26:271384 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031385}
1386
[email protected]2b624c562011-10-27 22:58:261387void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261388 if (!webwidget_)
1389 return;
1390
1391 if (is_fullscreen_) {
1392 webwidget_->willExitFullScreen();
1393 } else {
1394 webwidget_->willEnterFullScreen();
1395 }
[email protected]2b624c562011-10-27 22:58:261396}
1397
1398void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261399 if (!webwidget_)
1400 return;
1401
1402 if (is_fullscreen_) {
1403 webwidget_->didEnterFullScreen();
1404 } else {
1405 webwidget_->didExitFullScreen();
1406 }
[email protected]2b624c562011-10-27 22:58:261407}
1408
[email protected]699ab0d2009-04-23 23:19:141409void RenderWidget::SetBackground(const SkBitmap& background) {
1410 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461411
[email protected]699ab0d2009-04-23 23:19:141412 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281413 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141414}
1415
[email protected]674741932009-02-04 23:44:461416bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051417 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461418}
1419
1420bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051421 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461422}
1423
1424void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051425 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461426}
1427
1428void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051429 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461430}
1431
1432void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051433 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461434}
1435
[email protected]e99ef6f2011-10-16 01:13:001436void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041437 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291438 return;
[email protected]fa7b1dc2010-06-23 17:53:041439
[email protected]ad26ef42011-06-17 07:59:451440 ui::TextInputType new_type = GetTextInputType();
1441 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001442 // Only sends text input type and compose inline to the browser process if
1443 // they are changed.
1444 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451445 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041446 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451447 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001448 Send(new ViewHostMsg_TextInputStateChanged(
1449 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291450 }
initial.commit09911bf2008-07-26 23:55:291451}
1452
[email protected]3f783362011-10-21 22:40:501453void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1454 WebRect start_webrect;
1455 WebRect end_webrect;
1456 webwidget_->selectionBounds(start_webrect, end_webrect);
1457 *start = start_webrect;
1458 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321459}
1460
[email protected]e99ef6f2011-10-16 01:13:001461void RenderWidget::UpdateSelectionBounds() {
1462 if (!webwidget_)
1463 return;
1464
[email protected]3f783362011-10-21 22:40:501465 gfx::Rect start_rect;
1466 gfx::Rect end_rect;
1467 GetSelectionBounds(&start_rect, &end_rect);
[email protected]e99ef6f2011-10-16 01:13:001468 if (selection_start_rect_ == start_rect && selection_end_rect_ == end_rect)
1469 return;
1470
1471 selection_start_rect_ = start_rect;
1472 selection_end_rect_ = end_rect;
1473 Send(new ViewHostMsg_SelectionBoundsChanged(
1474 routing_id_, selection_start_rect_, selection_end_rect_));
1475}
1476
[email protected]73bf95812011-10-12 11:38:321477// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451478COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1479 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1480COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1481 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1482COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1483 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181484COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1485 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1486COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1487 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1488COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1489 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1490COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1491 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1492COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1493 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451494
1495ui::TextInputType RenderWidget::GetTextInputType() {
1496 if (webwidget_) {
1497 int type = webwidget_->textInputType();
1498 // Check the type is in the range representable by ui::TextInputType.
[email protected]caf38ed2011-07-28 13:15:181499 DCHECK(type <= ui::TEXT_INPUT_TYPE_URL) <<
[email protected]ad26ef42011-06-17 07:59:451500 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1501 return static_cast<ui::TextInputType>(type);
1502 }
1503 return ui::TEXT_INPUT_TYPE_NONE;
1504}
1505
1506bool RenderWidget::CanComposeInline() {
1507 return true;
[email protected]56ea1a62011-05-30 07:05:571508}
1509
[email protected]4873c7d2009-07-16 06:36:281510WebScreenInfo RenderWidget::screenInfo() {
1511 WebScreenInfo results;
1512 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1513 return results;
1514}
1515
[email protected]fa7b1dc2010-06-23 17:53:041516void RenderWidget::resetInputMethod() {
1517 if (!input_method_is_active_)
1518 return;
1519
1520 // If the last text input type is not None, then we should finish any
1521 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451522 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041523 // If a composition text exists, then we need to let the browser process
1524 // to cancel the input method's ongoing composition session.
1525 if (webwidget_->confirmComposition())
1526 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1527 }
[email protected]d4cff272011-05-02 15:46:011528
1529 // Send an updated IME range with the current caret rect.
1530 ui::Range range(ui::Range::InvalidRange());
1531 size_t location, length;
1532 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1533 range.set_start(location);
1534 range.set_end(location + length);
1535 }
1536 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041537}
1538
[email protected]f103ab72009-09-02 17:10:591539void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501540 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291541 size_t i = 0;
1542 for (; i < plugin_window_moves_.size(); ++i) {
1543 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581544 if (move.rects_valid) {
1545 plugin_window_moves_[i] = move;
1546 } else {
1547 plugin_window_moves_[i].visible = move.visible;
1548 }
initial.commit09911bf2008-07-26 23:55:291549 break;
1550 }
1551 }
1552
1553 if (i == plugin_window_moves_.size())
1554 plugin_window_moves_.push_back(move);
1555}
[email protected]268654772009-08-06 23:02:041556
1557void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1558 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1559 i != plugin_window_moves_.end(); ++i) {
1560 if (i->window == window) {
1561 plugin_window_moves_.erase(i);
1562 break;
1563 }
1564 }
1565}
[email protected]67bfb83f2011-09-22 03:36:371566
1567bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1568 return false;
1569}
[email protected]c3d45532011-10-07 19:20:401570
1571bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1572 return false;
1573}