blob: f17b665935541efca4430b3b43a8c30420b83b5a [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]8704f89b2011-04-15 00:30:0520#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4421#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1922#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1823#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4824#include "skia/ext/platform_canvas.h"
[email protected]8bd0fe62011-01-17 06:44:3725#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]a7547fb2012-03-08 04:43:4426#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3727#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
28#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0129#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3730#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4131#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
32#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]e6e90dc2011-12-03 00:01:3733#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
34#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]d353541f2012-05-03 22:45:4135#include "third_party/skia/include/core/SkShader.h"
[email protected]08397d52011-02-05 01:53:3836#include "ui/gfx/point.h"
37#include "ui/gfx/size.h"
[email protected]1835b9e2012-02-28 13:12:4838#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2739#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4140#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3441#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5042#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4643#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4844
45#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4946#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5247#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4148#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4849#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4450
[email protected]8bd0fe62011-01-17 06:44:3751#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2952
[email protected]fa7b1dc2010-06-23 17:53:0453using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3054using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2255using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0656using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2857using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4458using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0059using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2860using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5961using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1862using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0163using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5264using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1965using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5266using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2867using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2468using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0469using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1870using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2771using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2272
[email protected]f1cccb32012-06-06 18:29:5973static const int kStandardDPI = 160;
74
[email protected]6fd35b72012-03-01 19:46:4175RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:0476 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:4477 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:2978 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:5679 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:1180 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2981 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4282 host_window_(0),
[email protected]05a980d7a2012-02-07 22:16:4283 host_window_set_(false),
[email protected]b4d08452010-10-05 17:34:3584 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2985 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0686 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0587 update_reply_pending_(false),
[email protected]ea3ee0a2012-05-15 03:43:0988 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:2489 using_asynchronous_swapbuffers_(false),
90 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2991 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2992 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0993 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2994 needs_repainting_on_restore_(false),
95 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4996 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4897 closing_(false),
[email protected]14392a52012-05-02 20:28:4498 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:0499 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45100 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
101 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12102 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48103 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12104 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50105 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21106 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07107 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04108 screen_info_(screen_info),
[email protected]75bfbf92012-06-12 19:12:02109 device_scale_factor_(1) {
[email protected]8b3f0eb2012-05-03 19:15:05110 if (!swapped_out)
111 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27112 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18113 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
114 switches::kDisableGpuVsync);
[email protected]424820962012-06-08 15:33:19115#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
[email protected]60051ec2012-06-08 22:40:57116 device_scale_factor_ = std::max(1, screen_info.verticalDPI / kStandardDPI);
[email protected]f1cccb32012-06-06 18:29:59117#endif
initial.commit09911bf2008-07-26 23:55:29118}
119
120RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11121 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21122 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35123 if (current_paint_buf_) {
124 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
125 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29126 }
[email protected]992db4c2011-05-12 15:37:15127 // If we are swapped out, we have released already.
128 if (!is_swapped_out_)
129 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29130}
131
[email protected]484955942010-08-19 16:13:18132// static
[email protected]8085dbc82008-09-26 22:53:44133RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04134 WebKit::WebPopupType popup_type,
135 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29136 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41137 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04138 new RenderWidget(popup_type, screen_info, false));
initial.commit09911bf2008-07-26 23:55:29139 widget->Init(opener_id); // adds reference
140 return widget;
141}
142
[email protected]484955942010-08-19 16:13:18143// static
144WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
145 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03146 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18147 break;
148 case WebKit::WebPopupTypeSelect:
149 case WebKit::WebPopupTypeSuggestion:
150 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44151 case WebKit::WebPopupTypePage:
152 return WebPagePopup::create(render_widget);
[email protected]484955942010-08-19 16:13:18153 default:
154 NOTREACHED();
155 }
156 return NULL;
157}
158
initial.commit09911bf2008-07-26 23:55:29159void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18160 DoInit(opener_id,
161 RenderWidget::CreateWebWidget(this),
[email protected]9f4f3322012-01-18 22:29:56162 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
163 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18164}
165
[email protected]484955942010-08-19 16:13:18166void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06167 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18168 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29169 DCHECK(!webwidget_);
170
171 if (opener_id != MSG_ROUTING_NONE)
172 opener_id_ = opener_id;
173
[email protected]484955942010-08-19 16:13:18174 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29175
[email protected]380244092011-10-07 17:26:27176 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29177 if (result) {
[email protected]380244092011-10-07 17:26:27178 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29179 // Take a reference on behalf of the RenderThread. This will be balanced
180 // when we receive ViewMsg_Close.
181 AddRef();
182 } else {
183 DCHECK(false);
184 }
185}
186
187// This is used to complete pending inits and non-pending inits. For non-
188// pending cases, the parent will be the same as the current parent. This
189// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55190void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29191 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29192
193 host_window_ = parent_hwnd;
[email protected]05a980d7a2012-02-07 22:16:42194 host_window_set_ = true;
195
[email protected]f1d5de42012-04-20 01:47:14196#if WEBWIDGET_HAS_SETCOMPOSITORSURFACEREADY
197 if (webwidget_)
198 webwidget_->setCompositorSurfaceReady();
199#endif
[email protected]05a980d7a2012-02-07 22:16:42200 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29201
[email protected]6de74452009-02-25 18:04:59202 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29203}
204
[email protected]992db4c2011-05-12 15:37:15205void RenderWidget::SetSwappedOut(bool is_swapped_out) {
206 // We should only toggle between states.
207 DCHECK(is_swapped_out_ != is_swapped_out);
208 is_swapped_out_ = is_swapped_out;
209
210 // If we are swapping out, we will call ReleaseProcess, allowing the process
211 // to exit if all of its RenderViews are swapped out. We wait until the
212 // WasSwappedOut call to do this, to avoid showing the sad tab.
213 // If we are swapping in, we call AddRefProcess to prevent the process from
214 // exiting.
215 if (!is_swapped_out)
216 RenderProcess::current()->AddRefProcess();
217}
218
[email protected]a95986a82010-12-24 06:19:28219bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
220 bool handled = true;
221 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
222 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
223 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
224 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54225 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28226 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
227 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15228 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28229 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
230 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
231 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
232 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
233 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
234 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
235 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
236 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
237 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
238 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
239 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]a95986a82010-12-24 06:19:28240 IPC_MESSAGE_UNHANDLED(handled = false)
241 IPC_END_MESSAGE_MAP()
242 return handled;
243}
initial.commit09911bf2008-07-26 23:55:29244
245bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15246 // Don't send any messages after the browser has told us to close, and filter
247 // most outgoing messages while swapped out.
248 if ((is_swapped_out_ &&
249 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11250 closing_) {
initial.commit09911bf2008-07-26 23:55:29251 delete message;
252 return false;
253 }
254
255 // If given a messsage without a routing ID, then assign our routing ID.
256 if (message->routing_id() == MSG_ROUTING_NONE)
257 message->set_routing_id(routing_id_);
258
[email protected]380244092011-10-07 17:26:27259 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44260}
261
[email protected]61e2b3cc2012-03-02 16:13:34262void RenderWidget::Resize(const gfx::Size& new_size,
263 const gfx::Rect& resizer_rect,
264 bool is_fullscreen,
265 ResizeAck resize_ack) {
266 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
267 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
268 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29269
[email protected]61e2b3cc2012-03-02 16:13:34270 // Ignore this during shutdown.
271 if (!webwidget_)
272 return;
273
274 // Remember the rect where the resize corner will be drawn.
275 resizer_rect_ = resizer_rect;
276
277 // NOTE: We may have entered fullscreen mode without changing our size.
278 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
279 if (fullscreen_change)
280 WillToggleFullscreen();
281 is_fullscreen_ = is_fullscreen;
282
283 if (size_ != new_size) {
284 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34285 needs_repainting_on_restore_ = false;
286
287 size_ = new_size;
288
289 paint_aggregator_.ClearPendingUpdate();
290
291 // When resizing, we want to wait to paint before ACK'ing the resize. This
292 // ensures that we only resize as fast as we can paint. We only need to
293 // send an ACK if we are resized to a non-empty rect.
294 webwidget_->resize(new_size);
295 if (!new_size.IsEmpty()) {
296 if (!is_accelerated_compositing_active_) {
297 // Resize should have caused an invalidation of the entire view.
298 DCHECK(paint_aggregator_.HasPendingUpdate());
299 }
300
301 // Send the Resize_ACK flag once we paint again if requested.
302 if (resize_ack == SEND_RESIZE_ACK)
303 set_next_paint_is_resize_ack();
304 }
[email protected]ff475a322012-03-14 00:05:35305 } else {
306 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34307 }
308
309 if (fullscreen_change)
310 DidToggleFullscreen();
311
312 // If a resize ack is requested and it isn't set-up, then no more resizes will
313 // come in and in general things will go wrong.
314 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
315 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29316}
317
318void RenderWidget::OnClose() {
319 if (closing_)
320 return;
321 closing_ = true;
322
323 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03324 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27325 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03326 SetHidden(false);
327 }
initial.commit09911bf2008-07-26 23:55:29328
initial.commit09911bf2008-07-26 23:55:29329 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25330 // now. Post a task that only gets invoked when there are no nested message
331 // loops.
[email protected]32876ae2011-11-15 22:25:21332 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29333 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25334
335 // Balances the AddRef taken when we called AddRoute.
336 Release();
initial.commit09911bf2008-07-26 23:55:29337}
338
[email protected]61e2b3cc2012-03-02 16:13:34339// Got a response from the browser after the renderer decided to create a new
340// view.
341void RenderWidget::OnCreatingNewAck(
342 gfx::NativeViewId parent) {
343 DCHECK(routing_id_ != MSG_ROUTING_NONE);
344
345 CompleteInit(parent);
346}
347
[email protected]f21c613a2009-02-12 14:46:17348void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09349 const gfx::Rect& resizer_rect,
350 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34351 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29352}
353
[email protected]b5913d72012-02-07 22:26:54354void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
355 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59356 gfx::Rect view_rect(size_);
357
358 gfx::Rect old_damage_rect = view_rect.Intersect(resizer_rect_);
359 if (!old_damage_rect.IsEmpty())
360 paint_aggregator_.InvalidateRect(old_damage_rect);
361
362 gfx::Rect new_damage_rect = view_rect.Intersect(resizer_rect);
363 if (!new_damage_rect.IsEmpty())
364 paint_aggregator_.InvalidateRect(new_damage_rect);
365
[email protected]b5913d72012-02-07 22:26:54366 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59367
[email protected]b5913d72012-02-07 22:26:54368 if (webwidget_)
369 webwidget_->didChangeWindowResizerRect();
370 }
371}
372
initial.commit09911bf2008-07-26 23:55:29373void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31374 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29375 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03376 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29377}
378
379void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31380 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29381 // During shutdown we can just ignore this message.
382 if (!webwidget_)
383 return;
384
385 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03386 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29387
388 if (!needs_repainting && !needs_repainting_on_restore_)
389 return;
390 needs_repainting_on_restore_ = false;
391
[email protected]d65adb12010-04-28 17:26:49392 // Tag the next paint as a restore ack, which is picked up by
393 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29394 set_next_paint_is_restore_ack();
395
396 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56397 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46398 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
399 } else {
400 scheduleComposite();
401 }
initial.commit09911bf2008-07-26 23:55:29402}
403
[email protected]992db4c2011-05-12 15:37:15404void RenderWidget::OnWasSwappedOut() {
405 // If we have been swapped out and no one else is using this process,
406 // it's safe to exit now. If we get swapped back in, we will call
407 // AddRefProcess in SetSwappedOut.
408 if (is_swapped_out_)
409 RenderProcess::current()->ReleaseProcess();
410}
411
[email protected]53d3f302009-12-21 04:42:05412void RenderWidget::OnRequestMoveAck() {
413 DCHECK(pending_window_rect_count_);
414 pending_window_rect_count_--;
415}
416
417void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58418 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21419 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05420 update_reply_pending_ = false;
421
[email protected]b4d08452010-10-05 17:34:35422 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
423 // have no current paint buffer.
424 if (current_paint_buf_) {
425 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
426 current_paint_buf_ = NULL;
427 }
428
[email protected]65225772011-05-12 21:10:24429 // If swapbuffers is still pending, then defer the update until the
430 // swapbuffers occurs.
431 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
432 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
433 return;
434 }
435
[email protected]29ed96a2012-02-04 18:12:16436 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18437 if (!is_accelerated_compositing_active_) {
438 DidFlushPaint();
439 }
[email protected]a2f6bc112009-06-27 16:27:25440
initial.commit09911bf2008-07-26 23:55:29441 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24442 DoDeferredUpdateAndSendInputAck();
443}
444
[email protected]d0be63772011-12-20 23:18:04445bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24446 return false;
447}
448
[email protected]d0be63772011-12-20 23:18:04449void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24450 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21451 while (!updates_pending_swap_.empty()) {
452 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
453 updates_pending_swap_.pop_front();
454 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
455 // compositing pass, hence doesn't require an UpdateRect message.
456 if (msg)
457 Send(msg);
458 }
[email protected]65225772011-05-12 21:10:24459 num_swapbuffers_complete_pending_ = 0;
460 using_asynchronous_swapbuffers_ = false;
461 // Schedule another frame so the compositor learns about it.
462 scheduleComposite();
463}
464
[email protected]37a6f302011-07-11 23:43:08465void RenderWidget::OnSwapBuffersPosted() {
466 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21467
468 if (using_asynchronous_swapbuffers_) {
469 ViewHostMsg_UpdateRect* msg = NULL;
470 // pending_update_params_ can be NULL if the swap doesn't correspond to an
471 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
472 // message.
473 if (pending_update_params_.get()) {
474 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
475 pending_update_params_.reset();
476 }
477 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08478 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21479 }
[email protected]37a6f302011-07-11 23:43:08480}
481
482void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24483 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16484
[email protected]404939f2012-06-01 04:06:18485 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16486 DidFlushPaint();
487
[email protected]65225772011-05-12 21:10:24488 // When compositing deactivates, we reset the swapbuffers pending count. The
489 // swapbuffers acks may still arrive, however.
490 if (num_swapbuffers_complete_pending_ == 0) {
491 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
492 return;
493 }
[email protected]aa4117f2011-12-09 22:19:21494 DCHECK(!updates_pending_swap_.empty());
495 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
496 updates_pending_swap_.pop_front();
497 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
498 // compositing pass, hence doesn't require an UpdateRect message.
499 if (msg)
500 Send(msg);
[email protected]65225772011-05-12 21:10:24501 num_swapbuffers_complete_pending_--;
502
503 // If update reply is still pending, then defer the update until that reply
504 // occurs.
[email protected]d0be63772011-12-20 23:18:04505 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24506 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
507 return;
508 }
509
510 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06511 // when we were previously rendering. However, if an invalidation task is not
512 // posted, there may be software rendering work pending. In that case, don't
513 // early out.
514 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24515 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
516 return;
517 }
518
[email protected]65225772011-05-12 21:10:24519 // Continue painting if necessary...
520 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29521}
522
initial.commit09911bf2008-07-26 23:55:29523void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24524 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
[email protected]ce208f872012-03-07 20:42:56525 PickleIterator iter(message);
initial.commit09911bf2008-07-26 23:55:29526
527 const char* data;
528 int data_length;
[email protected]5dd768212009-08-13 23:34:49529 handling_input_event_ = true;
530 if (!message.ReadData(&iter, &data, &data_length)) {
531 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29532 return;
[email protected]5dd768212009-08-13 23:34:49533 }
initial.commit09911bf2008-07-26 23:55:29534
535 const WebInputEvent* input_event =
536 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48537
[email protected]b68a0e52011-12-08 15:11:12538 bool is_keyboard_shortcut = false;
539 // is_keyboard_shortcut flag is only available for RawKeyDown events.
540 if (input_event->type == WebInputEvent::RawKeyDown)
541 message.ReadBool(&iter, &is_keyboard_shortcut);
542
[email protected]67bfb83f2011-09-22 03:36:37543 bool prevent_default = false;
544 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26545 const WebMouseEvent& mouse_event =
546 *static_cast<const WebMouseEvent*>(input_event);
547 TRACE_EVENT2("renderer", "HandleMouseMove",
548 "x", mouse_event.x, "y", mouse_event.y);
549 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37550 }
551
552 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12553 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
554 suppress_next_char_events_ = false;
555 if (!processed && webwidget_)
556 processed = webwidget_->handleInputEvent(*input_event);
557 }
558
559 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
560 // it's not processed by webkit, then we need to suppress the upcoming Char
561 // events.
562 if (!processed && is_keyboard_shortcut)
563 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29564
[email protected]a9fb30aa2011-10-06 06:58:46565 IPC::Message* response =
566 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
567 processed);
[email protected]3391a0772012-03-28 00:32:07568 bool event_type_gets_rate_limited =
569 input_event->type == WebInputEvent::MouseMove ||
570 input_event->type == WebInputEvent::MouseWheel ||
571 WebInputEvent::isTouchEventType(input_event->type);
572 bool is_input_throttled =
573 webwidget_->isInputThrottled() ||
574 paint_aggregator_.HasPendingUpdate();
[email protected]e2824412009-02-27 01:57:05575
[email protected]f8868d72012-04-27 19:13:03576 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24577 // We want to rate limit the input events in this case, so we'll wait for
578 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17579 if (pending_input_event_ack_.get()) {
580 // As two different kinds of events could cause us to postpone an ack
581 // we send it now, if we have one pending. The Browser should never
582 // send us the same kind of event we are delaying the ack for.
583 Send(pending_input_event_ack_.release());
584 }
[email protected]12fbad812009-09-01 18:21:24585 pending_input_event_ack_.reset(response);
586 } else {
587 Send(response);
588 }
589
[email protected]5dd768212009-08-13 23:34:49590 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48591
[email protected]67bfb83f2011-09-22 03:36:37592 if (!prevent_default) {
593 if (WebInputEvent::isKeyboardEventType(input_event->type))
594 DidHandleKeyEvent();
595 if (WebInputEvent::isMouseEventType(input_event->type))
596 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24597 if (WebInputEvent::isTouchEventType(input_event->type))
598 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37599 }
initial.commit09911bf2008-07-26 23:55:29600}
601
602void RenderWidget::OnMouseCaptureLost() {
603 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28604 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29605}
606
607void RenderWidget::OnSetFocus(bool enable) {
608 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33609 if (webwidget_)
610 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29611}
612
613void RenderWidget::ClearFocus() {
614 // We may have got the focus from the browser before this gets processed, in
615 // which case we do not want to unfocus ourself.
616 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28617 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29618}
619
[email protected]2d5d09d52009-06-15 14:29:21620void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00621 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21622 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06623 TRACE_EVENT2("renderer", "PaintRect",
624 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00625 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21626
627 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00628 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
629 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03630
[email protected]699ab0d2009-04-23 23:19:14631 // If there is a custom background, tile it.
632 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14633 SkPaint paint;
634 SkShader* shader = SkShader::CreateBitmapShader(background_,
635 SkShader::kRepeat_TileMode,
636 SkShader::kRepeat_TileMode);
637 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20638
639 // Use kSrc_Mode to handle background_ transparency properly.
640 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
641
642 // Canvas could contain multiple update rects. Clip to given rect so that
643 // we don't accidentally clear other update rects.
644 canvas->save();
[email protected]1835b9e2012-02-28 13:12:48645 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14646 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20647 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14648 }
649
[email protected]719b36f2010-12-22 20:36:46650 // First see if this rect is a plugin that can paint itself faster.
651 TransportDIB* optimized_dib = NULL;
652 gfx::Rect optimized_copy_rect, optimized_copy_location;
653 webkit::ppapi::PluginInstance* optimized_instance =
654 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
655 &optimized_copy_location,
656 &optimized_copy_rect);
657 if (optimized_instance) {
658 // This plugin can be optimize-painted and we can just ask it to paint
659 // itself. We don't actually need the TransportDIB in this case.
660 //
661 // This is an optimization for PPAPI plugins that know they're on top of
662 // the page content. If this rect is inside such a plugin, we can save some
663 // time and avoid re-rendering the page content which we know will be
664 // covered by the plugin later (this time can be significant, especially
665 // for a playing movie that is invalidating a lot).
666 //
667 // In the plugin movie case, hopefully the similar call to
668 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
669 // painting, because that avoids copying the plugin image to a different
670 // paint rect. Unfortunately, if anything on the page is animating other
671 // than the movie, it break this optimization since the union of the
672 // invalid regions will be larger than the plugin.
673 //
674 // This code optimizes that case, where we can still avoid painting in
675 // WebKit and filling the background (which can be slow) and just painting
676 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
677 // required.
678 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27679 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46680 } else {
681 // Normal painting case.
682 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
683
684 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35685 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46686 }
initial.commit09911bf2008-07-26 23:55:29687
[email protected]4fb66842009-12-04 21:41:00688 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00689 canvas->restore();
690}
691
692void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
693 skia::PlatformCanvas* canvas) {
694 static bool kPaintBorder =
695 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
696 if (!kPaintBorder)
697 return;
698
[email protected]53d3f302009-12-21 04:42:05699 // Cycle through these colors to help distinguish new paint rects.
700 const SkColor colors[] = {
701 SkColorSetARGB(0x3F, 0xFF, 0, 0),
702 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
703 SkColorSetARGB(0x3F, 0, 0, 0xFF),
704 };
705 static int color_selector = 0;
706
[email protected]4fb66842009-12-04 21:41:00707 SkPaint paint;
708 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05709 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00710 paint.setStrokeWidth(1);
711
712 SkIRect irect;
713 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
714 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29715}
716
[email protected]52ccd0ea2011-02-16 01:09:05717void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30718 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30719 if (!animation_update_pending_) {
720 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59721 return;
[email protected]921244e42011-07-20 16:36:30722 }
[email protected]bd37ae252011-06-03 01:28:18723 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59724 // Record when we fired (according to base::Time::Now()) relative to when
725 // we posted the task to quantify how much the base::Time/base::TimeTicks
726 // skew is affecting animations.
727 base::TimeDelta animation_callback_delay = base::Time::Now() -
728 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
729 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
730 animation_callback_delay,
731 base::TimeDelta::FromMilliseconds(0),
732 base::TimeDelta::FromMilliseconds(30),
733 25);
734 }
[email protected]65225772011-05-12 21:10:24735 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24736}
737
[email protected]52ccd0ea2011-02-16 01:09:05738void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59739 if (!animation_update_pending_)
740 return;
[email protected]bd37ae252011-06-03 01:28:18741
742 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33743 base::TimeDelta animationInterval = IsRenderingVSynced() ?
744 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18745
[email protected]7c4329e2011-02-18 22:02:59746 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45747
748 // animation_floor_time_ is the earliest time that we should animate when
749 // using the dead reckoning software scheduler. If we're using swapbuffers
750 // complete callbacks to rate limit, we can ignore this floor.
751 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30752 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33753 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18754 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59755 // running animation callbacks so that if a callback requests another
756 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38757 animation_timer_.Stop();
758 animation_timer_.Start(FROM_HERE, animationInterval, this,
759 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59760 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30761 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59762 return;
[email protected]5f8b1022011-01-21 23:34:50763 }
[email protected]bd37ae252011-06-03 01:28:18764 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38765 if (!animation_timer_.IsRunning()) {
766 // This code uses base::Time::Now() to calculate the floor and next fire
767 // time because javascript's Date object uses base::Time::Now(). The
768 // message loop uses base::TimeTicks, which on windows can have a
769 // different granularity than base::Time.
770 // The upshot of all this is that this function might be called before
771 // base::Time::Now() has advanced past the animation_floor_time_. To
772 // avoid exposing this delay to javascript, we keep posting delayed
773 // tasks until base::Time::Now() has advanced far enough.
774 base::TimeDelta delay = animation_floor_time_ - now;
775 animation_timer_.Start(FROM_HERE, delay, this,
776 &RenderWidget::AnimationCallback);
777 }
[email protected]5f8b1022011-01-21 23:34:50778}
779
[email protected]bd37ae252011-06-03 01:28:18780bool RenderWidget::IsRenderingVSynced() {
781 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
782 // not caught by this check. This will lead to artificially low frame rates
783 // for people who force vsync off at a driver level and expect Chrome to speed
784 // up.
785 return !has_disable_gpu_vsync_switch_;
786}
787
[email protected]65225772011-05-12 21:10:24788void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06789 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24790 invalidation_task_posted_ = false;
791 DoDeferredUpdateAndSendInputAck();
792}
793
794void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05795 DoDeferredUpdate();
796
797 if (pending_input_event_ack_.get())
798 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21799}
800
[email protected]552e6002009-11-19 05:24:57801void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58802 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08803
[email protected]65225772011-05-12 21:10:24804 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29805 return;
[email protected]05a980d7a2012-02-07 22:16:42806
807 if (!host_window_set_) {
808 TRACE_EVENT0("renderer", "EarlyOut_NoHostWindow");
809 return;
810 }
[email protected]aa4117f2011-12-09 22:19:21811 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24812 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
813 return;
814 }
[email protected]9ca84622011-06-02 23:46:39815 if (is_accelerated_compositing_active_ &&
816 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24817 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
818 return;
819 }
initial.commit09911bf2008-07-26 23:55:29820
[email protected]552e6002009-11-19 05:24:57821 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29822 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57823 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29824 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24825 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29826 return;
827 }
828
[email protected]05a980d7a2012-02-07 22:16:42829 if (is_accelerated_compositing_active_)
830 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
831
[email protected]0fb93f52011-05-18 23:13:56832 // Tracking of frame rate jitter
833 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]541dcd52012-03-15 15:57:51834 webwidget_->instrumentBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:05835 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50836
[email protected]f98d7e3c2010-09-13 22:30:46837 // Layout may generate more invalidation. It may also enable the
838 // GPU acceleration, so make sure to run layout before we send the
839 // GpuRenderingActivated message.
840 webwidget_->layout();
841
[email protected]dcca3aa92012-02-17 23:03:37842 // The following two can result in further layout and possibly
843 // enable GPU acceleration so they need to be called before any painting
844 // is done.
845 UpdateTextInputState();
846 UpdateSelectionBounds();
847
[email protected]5f8b1022011-01-21 23:34:50848 // Suppress painting if nothing is dirty. This has to be done after updating
849 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24850 if (!paint_aggregator_.HasPendingUpdate()) {
851 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]541dcd52012-03-15 15:57:51852 webwidget_->instrumentCancelFrame();
[email protected]5f8b1022011-01-21 23:34:50853 return;
[email protected]65225772011-05-12 21:10:24854 }
[email protected]5f8b1022011-01-21 23:34:50855
[email protected]872ae5b2011-05-26 20:20:50856 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56857 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04858 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56859 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
860 delay,
861 base::TimeDelta::FromMilliseconds(1),
862 base::TimeDelta::FromMilliseconds(60),
863 30);
[email protected]d0be63772011-12-20 23:18:04864 } else {
[email protected]0fb93f52011-05-18 23:13:56865 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
866 delay,
867 base::TimeDelta::FromMilliseconds(1),
868 base::TimeDelta::FromMilliseconds(60),
869 30);
[email protected]d0be63772011-12-20 23:18:04870 }
[email protected]872ae5b2011-05-26 20:20:50871
872 // Calculate filtered time per frame:
873 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
874 filtered_time_per_frame_ =
875 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56876 }
877 last_do_deferred_update_time_ = frame_begin_ticks;
878
[email protected]552e6002009-11-19 05:24:57879 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29880 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30881 PaintAggregator::PendingUpdate update;
882 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29883
[email protected]53d3f302009-12-21 04:42:05884 gfx::Rect scroll_damage = update.GetScrollDamage();
885 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29886
[email protected]29ed96a2012-02-04 18:12:16887 // Notify derived classes that we're about to initiate a paint.
888 WillInitiatePaint();
889
[email protected]ca4847f2010-09-24 05:39:15890 // A plugin may be able to do an optimized paint. First check this, in which
891 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46892 // This optimization allows PPAPI plugins that declare themselves on top of
893 // the page (like a traditional windowed plugin) to be able to animate (think
894 // movie playing) without repeatedly re-painting the page underneath, or
895 // copying the plugin backing store (since we can send the plugin's backing
896 // store directly to the browser).
897 //
898 // This optimization only works when the entire invalid region is contained
899 // within the plugin. There is a related optimization in PaintRect for the
900 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15901 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15902 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]aa4117f2011-12-09 22:19:21903 DCHECK(!pending_update_params_.get());
904 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
905 pending_update_params_->dx = update.scroll_delta.x();
906 pending_update_params_->dy = update.scroll_delta.y();
907 pending_update_params_->scroll_rect = update.scroll_rect;
908 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:21909 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
910 pending_update_params_->flags = next_paint_flags_;
911 pending_update_params_->scroll_offset = GetScrollOffset();
912 pending_update_params_->needs_ack = true;
913 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:09914 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:21915
[email protected]ca4847f2010-09-24 05:39:15916 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56917 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15918 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
919 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27920 // Only update the part of the plugin that actually changed.
921 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21922 pending_update_params_->bitmap = dib->id();
923 pending_update_params_->bitmap_rect = optimized_copy_location;
924 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]a79d8a632010-11-18 22:35:56925 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46926 // Compute a buffer for painting and cache it.
[email protected]f1cccb32012-06-06 18:29:59927 gfx::Rect pixel_bounds = bounds.Scale(device_scale_factor_);
[email protected]ca4847f2010-09-24 05:39:15928 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35929 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:59930 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:46931 if (!canvas.get()) {
932 NOTREACHED();
933 return;
934 }
[email protected]cef3362f2009-12-21 17:48:45935
[email protected]f98d7e3c2010-09-13 22:30:46936 // We may get back a smaller canvas than we asked for.
937 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:59938 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
939 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
940 pixel_bounds.set_width(canvas->getDevice()->width());
941 pixel_bounds.set_height(canvas->getDevice()->height());
942 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
943 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:05944
[email protected]f98d7e3c2010-09-13 22:30:46945 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
946
[email protected]aa4117f2011-12-09 22:19:21947 pending_update_params_->bitmap = current_paint_buf_->id();
948 pending_update_params_->bitmap_rect = bounds;
949
950 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:46951 // The scroll damage is just another rectangle to paint and copy.
952 copy_rects.swap(update.paint_rects);
953 if (!scroll_damage.IsEmpty())
954 copy_rects.push_back(scroll_damage);
955
956 for (size_t i = 0; i < copy_rects.size(); ++i)
[email protected]f1cccb32012-06-06 18:29:59957 PaintRect(copy_rects[i], pixel_bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:35958
959 // Software FPS tick for performance tests. The accelerated path traces the
960 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
961 // NOTE: Tests may break if this event is renamed or moved.
962 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:46963 } else { // Accelerated compositing path
964 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:21965 // If painting is done via the gpu process then we don't set any damage
966 // rects to save the browser process from doing unecessary work.
967 pending_update_params_->bitmap_rect = bounds;
968 pending_update_params_->scroll_rect = gfx::Rect();
969 // We don't need an ack, because we're not sharing a DIB with the browser.
970 // If it needs to (e.g. composited UI), the GPU process does its own ACK
971 // with the browser for the GPU surface.
972 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:42973 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46974 }
975
[email protected]936c6f52011-12-13 01:35:26976 // If we're holding a pending input event ACK, send the ACK before sending the
977 // UpdateReply message so we can receive another input event before the
978 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
979 // the UpdateRect IPC message handler.
980 if (pending_input_event_ack_.get())
981 Send(pending_input_event_ack_.release());
982
[email protected]aa4117f2011-12-09 22:19:21983 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
984 // OnSwapBuffersPosted), meaning a message has been added to the
985 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
986 // the message now.
987 if (pending_update_params_.get()) {
988 // sending an ack to browser process that the paint is complete...
989 update_reply_pending_ = pending_update_params_->needs_ack;
990 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
991 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:34992 }
[email protected]53d3f302009-12-21 04:42:05993
[email protected]29ed96a2012-02-04 18:12:16994 // If we're software rendering then we're done initiating the paint.
995 if (!is_accelerated_compositing_active_)
996 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29997}
998
999///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461000// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291001
[email protected]4873c7d2009-07-16 06:36:281002void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571003 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481004 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571005 gfx::Rect damaged_rect = view_rect.Intersect(rect);
1006 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291007 return;
1008
[email protected]552e6002009-11-19 05:24:571009 paint_aggregator_.InvalidateRect(damaged_rect);
1010
1011 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241012 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571013 return;
1014 if (!paint_aggregator_.HasPendingUpdate())
1015 return;
[email protected]aa4117f2011-12-09 22:19:211016 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241017 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1018 return;
1019
1020 // When GPU rendering, combine pending animations and invalidations into
1021 // a single update.
[email protected]816edc62012-03-17 01:27:221022 if (is_accelerated_compositing_active_ &&
1023 animation_update_pending_ &&
1024 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571025 return;
1026
1027 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291028 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1029 // on the call stack.
1030 // 2) Allows us to collect more damage rects before painting to help coalesce
1031 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241032 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211033 MessageLoop::current()->PostTask(
1034 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291035}
1036
[email protected]4873c7d2009-07-16 06:36:281037void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461038 // Drop scrolls on the floor when we are in compositing mode.
1039 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561040 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461041 return;
1042
[email protected]552e6002009-11-19 05:24:571043 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481044 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571045 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
1046 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291047 return;
1048
[email protected]552e6002009-11-19 05:24:571049 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
1050
1051 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241052 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571053 return;
1054 if (!paint_aggregator_.HasPendingUpdate())
1055 return;
[email protected]aa4117f2011-12-09 22:19:211056 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241057 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1058 return;
1059
1060 // When GPU rendering, combine pending animations and invalidations into
1061 // a single update.
[email protected]816edc62012-03-17 01:27:221062 if (is_accelerated_compositing_active_ &&
1063 animation_update_pending_ &&
1064 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571065 return;
1066
1067 // Perform updating asynchronously. This serves two purposes:
1068 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1069 // on the call stack.
1070 // 2) Allows us to collect more damage rects before painting to help coalesce
1071 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241072 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211073 MessageLoop::current()->PostTask(
1074 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291075}
1076
[email protected]244ac1892011-12-02 17:04:471077void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091078 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1079 size_ = new_size;
1080 need_update_rect_for_auto_resize_ = true;
1081 }
[email protected]244ac1892011-12-02 17:04:471082}
1083
[email protected]91acd1c2012-03-14 08:32:391084void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221085 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1086
[email protected]c63b4d42012-04-26 01:01:071087#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211088 if (!is_accelerated_compositing_active_) {
1089 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1090 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1091 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1092 // going to switch to accelerated compositing, the GPU process may need
1093 // round-trips to the browser's UI thread before finishing the frame,
1094 // causing deadlocks if we delay the UpdateRect until we receive the
1095 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071096 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1097 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211098 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1099 }
[email protected]c63b4d42012-04-26 01:01:071100#endif
[email protected]aa4117f2011-12-09 22:19:211101
[email protected]ea162f92011-10-04 23:08:221102 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421103 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241104 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221105}
1106
1107void RenderWidget::didDeactivateCompositor() {
1108 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1109
1110 is_accelerated_compositing_active_ = false;
1111 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1112 routing_id_, is_accelerated_compositing_active_));
1113
[email protected]ea162f92011-10-04 23:08:221114 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241115 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561116}
1117
[email protected]9cd43a62012-03-26 08:03:561118void RenderWidget::willBeginCompositorFrame() {
1119 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371120
1121 DCHECK(RenderThreadImpl::current()->compositor_thread());
1122
1123 // The following two can result in further layout and possibly
1124 // enable GPU acceleration so they need to be called before any painting
1125 // is done.
1126 UpdateTextInputState();
1127 UpdateSelectionBounds();
1128
[email protected]9cd43a62012-03-26 08:03:561129 WillInitiatePaint();
1130}
1131
[email protected]3391a0772012-03-28 00:32:071132void RenderWidget::didBecomeReadyForAdditionalInput() {
1133 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1134 if (pending_input_event_ack_.get())
1135 Send(pending_input_event_ack_.release());
1136}
1137
[email protected]58264a32011-11-17 23:36:151138void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501139 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351140 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1141 // NOTE: Tests may break if this event is renamed or moved.
1142 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161143 // Notify subclasses that we initiated the paint operation.
1144 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151145}
1146
1147void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181148 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1149
1150 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561151 DidFlushPaint();
1152
[email protected]aa4117f2011-12-09 22:19:211153 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151154 return;
1155
[email protected]ea3ee0a2012-05-15 03:43:091156 if (!next_paint_flags_ &&
1157 !need_update_rect_for_auto_resize_ &&
1158 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151159 return;
[email protected]ea3ee0a2012-05-15 03:43:091160 }
[email protected]58264a32011-11-17 23:36:151161
1162 ViewHostMsg_UpdateRect_Params params;
1163 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151164 params.plugin_window_moves.swap(plugin_window_moves_);
1165 params.flags = next_paint_flags_;
1166 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121167 params.needs_ack = false;
[email protected]58264a32011-11-17 23:36:151168
1169 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1170 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091171 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151172}
1173
[email protected]f98d7e3c2010-09-13 22:30:461174void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041175 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401176 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041177 } else {
[email protected]c3d45532011-10-07 19:20:401178 // TODO(nduca): replace with something a little less hacky. The reason this
1179 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1180 // contains a lot of host-renderer synchronization logic that is still
1181 // important for the accelerated compositing case. The option of simply
1182 // duplicating all that code is less desirable than "faking out" the
1183 // invalidation path using a magical damage rect.
1184 didInvalidateRect(WebRect(0, 0, 1, 1));
1185 }
[email protected]f98d7e3c2010-09-13 22:30:461186}
1187
[email protected]5f8b1022011-01-21 23:34:501188void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201189 if (animation_update_pending_)
1190 return;
1191
[email protected]921244e42011-07-20 16:36:301192 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201193 animation_update_pending_ = true;
1194 if (!animation_timer_.IsRunning()) {
1195 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1196 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211197 }
[email protected]5f8b1022011-01-21 23:34:501198}
1199
[email protected]4873c7d2009-07-16 06:36:281200void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301201 // TODO(darin): Eliminate this temporary.
1202 WebCursor cursor(cursor_info);
1203
initial.commit09911bf2008-07-26 23:55:291204 // Only send a SetCursor message if we need to make a change.
1205 if (!current_cursor_.IsEqual(cursor)) {
1206 current_cursor_ = cursor;
1207 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1208 }
1209}
1210
1211// We are supposed to get a single call to Show for a newly created RenderWidget
1212// that was created via RenderWidget::CreateWebView. So, we wait until this
1213// point to dispatch the ShowWidget message.
1214//
1215// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281216// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291217//
[email protected]4873c7d2009-07-16 06:36:281218void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291219 DCHECK(!did_show_) << "received extraneous Show call";
1220 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1221 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1222
[email protected]8de12d942010-11-17 20:42:441223 if (did_show_)
1224 return;
1225
1226 did_show_ = true;
1227 // NOTE: initial_pos_ may still have its default values at this point, but
1228 // that's okay. It'll be ignored if as_popup is false, or the browser
1229 // process will impose a default position otherwise.
1230 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1231 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291232}
1233
[email protected]4873c7d2009-07-16 06:36:281234void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291235}
1236
[email protected]4873c7d2009-07-16 06:36:281237void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291238}
1239
[email protected]2533ce12009-05-09 00:02:241240void RenderWidget::DoDeferredClose() {
1241 Send(new ViewHostMsg_Close(routing_id_));
1242}
1243
[email protected]4873c7d2009-07-16 06:36:281244void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321245 if (is_swapped_out_) {
1246 // This widget is currently swapped out, and the active widget is in a
1247 // different process. Have the browser route the close request to the
1248 // active widget instead, so that the correct unload handlers are run.
1249 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1250 return;
1251 }
1252
initial.commit09911bf2008-07-26 23:55:291253 // If a page calls window.close() twice, we'll end up here twice, but that's
1254 // OK. It is safe to send multiple Close messages.
1255
[email protected]2533ce12009-05-09 00:02:241256 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1257 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1258 // could be closed before the JS finishes executing. So instead, post a
1259 // message back to the message loop, which won't run until the JS is
1260 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211261 MessageLoop::current()->PostTask(
1262 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291263}
1264
1265void RenderWidget::Close() {
1266 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281267 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291268 webwidget_ = NULL;
1269 }
1270}
1271
[email protected]4873c7d2009-07-16 06:36:281272WebRect RenderWidget::windowRect() {
1273 if (pending_window_rect_count_)
1274 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241275
[email protected]b3f2b912009-04-09 16:18:521276 gfx::Rect rect;
1277 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281278 return rect;
initial.commit09911bf2008-07-26 23:55:291279}
1280
[email protected]8a9d6ca32011-06-06 20:11:301281void RenderWidget::setToolTipText(const WebKit::WebString& text,
1282 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541283 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301284}
1285
[email protected]4873c7d2009-07-16 06:36:281286void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291287 if (did_show_) {
1288 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241289 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291290 } else {
1291 initial_pos_ = pos;
1292 }
1293}
1294
[email protected]2533ce12009-05-09 00:02:241295void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1296 pending_window_rect_ = rect;
1297 pending_window_rect_count_++;
1298}
1299
[email protected]4873c7d2009-07-16 06:36:281300WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241301 if (pending_window_rect_count_) {
1302 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1303 // the RootWindowRect is probably going to return wrong results since the
1304 // browser may not have processed the Move yet. There isn't really anything
1305 // good to do in this case, and it shouldn't happen - since this size is
1306 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281307 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241308 }
1309
[email protected]b3f2b912009-04-09 16:18:521310 gfx::Rect rect;
1311 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281312 return rect;
[email protected]d4547452008-08-28 18:36:371313}
1314
[email protected]4873c7d2009-07-16 06:36:281315WebRect RenderWidget::windowResizerRect() {
1316 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191317}
1318
[email protected]fa7b1dc2010-06-23 17:53:041319void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031320 // To prevent this renderer process from sending unnecessary IPC messages to
1321 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041322 // only during the input method attached to the browser process is active.
1323 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291324}
1325
[email protected]58b48a0d2012-06-13 07:01:351326void RenderWidget::UpdateCompositionInfo(
1327 const ui::Range& range,
1328 const std::vector<gfx::Rect>& character_bounds) {
1329 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1330 return;
1331 composition_character_bounds_ = character_bounds;
1332 composition_range_ = range;
1333 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1334 routing_id(), composition_range_, composition_character_bounds_));
1335}
1336
[email protected]fa7b1dc2010-06-23 17:53:041337void RenderWidget::OnImeSetComposition(
1338 const string16& text,
1339 const std::vector<WebCompositionUnderline>& underlines,
1340 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281341 if (!webwidget_)
1342 return;
[email protected]d4cff272011-05-02 15:46:011343 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041344 text, WebVector<WebCompositionUnderline>(underlines),
1345 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011346 // Setting the IME composition was successful. Send the new composition
1347 // range to the browser.
1348 ui::Range range(ui::Range::InvalidRange());
1349 size_t location, length;
1350 if (webwidget_->compositionRange(&location, &length)) {
1351 range.set_start(location);
1352 range.set_end(location + length);
1353 }
1354 // The IME was cancelled via the Esc key, so just send back the caret.
1355 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1356 range.set_start(location);
1357 range.set_end(location + length);
1358 }
[email protected]58b48a0d2012-06-13 07:01:351359 std::vector<gfx::Rect> character_bounds;
1360 GetCompositionCharacterBounds(&character_bounds);
1361 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011362 } else {
[email protected]fa7b1dc2010-06-23 17:53:041363 // If we failed to set the composition text, then we need to let the browser
1364 // process to cancel the input method's ongoing composition session, to make
1365 // sure we are in a consistent state.
1366 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011367
1368 // Send an updated IME range with just the caret range.
1369 ui::Range range(ui::Range::InvalidRange());
1370 size_t location, length;
1371 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1372 range.set_start(location);
1373 range.set_end(location + length);
1374 }
[email protected]58b48a0d2012-06-13 07:01:351375 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261376 }
[email protected]fa7b1dc2010-06-23 17:53:041377}
1378
[email protected]4de6d1692011-10-12 08:45:441379void RenderWidget::OnImeConfirmComposition(
1380 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041381 if (!webwidget_)
1382 return;
1383
1384 handling_input_event_ = true;
1385 webwidget_->confirmComposition(text);
1386 handling_input_event_ = false;
1387
[email protected]d4cff272011-05-02 15:46:011388 // Send an updated IME range with just the caret range.
1389 ui::Range range(ui::Range::InvalidRange());
1390 size_t location, length;
1391 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1392 range.set_start(location);
1393 range.set_end(location + length);
1394 }
[email protected]58b48a0d2012-06-13 07:01:351395 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
initial.commit09911bf2008-07-26 23:55:291396}
1397
[email protected]948f7ab72010-05-28 23:48:081398// This message causes the renderer to render an image of the
1399// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491400void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091401 int tag,
[email protected]948f7ab72010-05-28 23:48:081402 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491403 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001404 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1405 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251406 // Close our unused handle.
1407#if defined(OS_WIN)
1408 ::CloseHandle(dib_handle);
1409#elif defined(OS_MACOSX)
1410 base::SharedMemory::CloseHandle(dib_handle);
1411#endif
1412 }
[email protected]d65adb12010-04-28 17:26:491413 return;
[email protected]45c6aad32010-11-11 04:46:251414 }
[email protected]d65adb12010-04-28 17:26:491415
[email protected]948f7ab72010-05-28 23:48:081416 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491417 // If one of these is empty, then we just return the dib we were
1418 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091419 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491420 return;
1421 }
1422
1423 // Map the given DIB ID into this process, and unmap it at the end
1424 // of this function.
[email protected]45c6aad32010-11-11 04:46:251425 scoped_ptr<TransportDIB> paint_at_size_buffer(
1426 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301427
1428 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491429 float x_scale = static_cast<float>(desired_size.width()) /
1430 static_cast<float>(canvas_size.width());
1431 float y_scale = static_cast<float>(desired_size.height()) /
1432 static_cast<float>(canvas_size.height());
1433
[email protected]ee8d6fd2010-05-26 17:05:481434 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491435 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1436 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481437 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491438
[email protected]36808ad2010-10-20 19:18:301439 scoped_ptr<skia::PlatformCanvas> canvas(
1440 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1441 canvas_size.height()));
1442 if (!canvas.get()) {
1443 NOTREACHED();
1444 return;
1445 }
1446
[email protected]d65adb12010-04-28 17:26:491447 // Reset bounds to what we actually received, but they should be the
1448 // same.
1449 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1450 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1451 bounds.set_width(canvas->getDevice()->width());
1452 bounds.set_height(canvas->getDevice()->height());
1453
1454 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081455 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491456 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1457
[email protected]948f7ab72010-05-28 23:48:081458 // Have to make sure we're laid out at the right size before
1459 // rendering.
1460 gfx::Size old_size = webwidget_->size();
1461 webwidget_->resize(page_size);
1462 webwidget_->layout();
1463
[email protected]d65adb12010-04-28 17:26:491464 // Paint the entire thing (using original bounds, not scaled bounds).
1465 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1466 canvas->restore();
1467
[email protected]948f7ab72010-05-28 23:48:081468 // Return the widget to its previous size.
1469 webwidget_->resize(old_size);
1470
[email protected]c88c9442010-07-19 18:55:091471 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491472}
1473
[email protected]ec7dc112008-08-06 05:30:121474void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1475 // During shutdown we can just ignore this message.
1476 if (!webwidget_)
1477 return;
1478
1479 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561480 if (is_accelerated_compositing_active_) {
[email protected]8c49fa982012-02-10 14:37:041481 webwidget_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461482 scheduleComposite();
1483 } else {
1484 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1485 didInvalidateRect(repaint_rect);
1486 }
[email protected]ec7dc112008-08-06 05:30:121487}
1488
[email protected]4873c7d2009-07-16 06:36:281489void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111490 if (!webwidget_)
1491 return;
[email protected]4873c7d2009-07-16 06:36:281492 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111493}
1494
[email protected]719b36f2010-12-22 20:36:461495webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151496 const gfx::Rect& paint_bounds,
1497 TransportDIB** dib,
1498 gfx::Rect* location,
1499 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461500 // Bare RenderWidgets don't support optimized plugin painting.
1501 return NULL;
[email protected]ca4847f2010-09-24 05:39:151502}
1503
[email protected]bcaf2272011-02-15 15:29:431504gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521505 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431506 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521507}
1508
[email protected]bee16aab2009-08-26 15:55:031509void RenderWidget::SetHidden(bool hidden) {
1510 if (is_hidden_ == hidden)
1511 return;
1512
1513 // The status has changed. Tell the RenderThread about it.
1514 is_hidden_ = hidden;
1515 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271516 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031517 else
[email protected]380244092011-10-07 17:26:271518 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031519}
1520
[email protected]2b624c562011-10-27 22:58:261521void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261522 if (!webwidget_)
1523 return;
1524
1525 if (is_fullscreen_) {
1526 webwidget_->willExitFullScreen();
1527 } else {
1528 webwidget_->willEnterFullScreen();
1529 }
[email protected]2b624c562011-10-27 22:58:261530}
1531
1532void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261533 if (!webwidget_)
1534 return;
1535
1536 if (is_fullscreen_) {
1537 webwidget_->didEnterFullScreen();
1538 } else {
1539 webwidget_->didExitFullScreen();
1540 }
[email protected]2b624c562011-10-27 22:58:261541}
1542
[email protected]699ab0d2009-04-23 23:19:141543void RenderWidget::SetBackground(const SkBitmap& background) {
1544 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461545
[email protected]699ab0d2009-04-23 23:19:141546 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281547 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141548}
1549
[email protected]674741932009-02-04 23:44:461550bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051551 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461552}
1553
1554bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051555 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461556}
1557
1558void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051559 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461560}
1561
1562void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051563 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461564}
1565
1566void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051567 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461568}
1569
[email protected]e99ef6f2011-10-16 01:13:001570void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041571 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291572 return;
[email protected]fa7b1dc2010-06-23 17:53:041573
[email protected]ad26ef42011-06-17 07:59:451574 ui::TextInputType new_type = GetTextInputType();
1575 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001576 // Only sends text input type and compose inline to the browser process if
1577 // they are changed.
1578 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451579 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041580 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451581 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001582 Send(new ViewHostMsg_TextInputStateChanged(
1583 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291584 }
initial.commit09911bf2008-07-26 23:55:291585}
1586
[email protected]3f783362011-10-21 22:40:501587void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1588 WebRect start_webrect;
1589 WebRect end_webrect;
1590 webwidget_->selectionBounds(start_webrect, end_webrect);
1591 *start = start_webrect;
1592 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321593}
1594
[email protected]e99ef6f2011-10-16 01:13:001595void RenderWidget::UpdateSelectionBounds() {
1596 if (!webwidget_)
1597 return;
1598
[email protected]3f783362011-10-21 22:40:501599 gfx::Rect start_rect;
1600 gfx::Rect end_rect;
1601 GetSelectionBounds(&start_rect, &end_rect);
[email protected]58b48a0d2012-06-13 07:01:351602 if (selection_start_rect_ != start_rect || selection_end_rect_ != end_rect) {
1603 selection_start_rect_ = start_rect;
1604 selection_end_rect_ = end_rect;
1605 Send(new ViewHostMsg_SelectionBoundsChanged(
1606 routing_id_, selection_start_rect_, selection_end_rect_));
1607 }
[email protected]e99ef6f2011-10-16 01:13:001608
[email protected]58b48a0d2012-06-13 07:01:351609 std::vector<gfx::Rect> character_bounds;
1610 GetCompositionCharacterBounds(&character_bounds);
1611 UpdateCompositionInfo(composition_range_, character_bounds);
1612}
1613
1614bool RenderWidget::ShouldUpdateCompositionInfo(
1615 const ui::Range& range,
1616 const std::vector<gfx::Rect>& bounds) {
1617 if (composition_range_ != range)
1618 return true;
1619 if (bounds.size() != composition_character_bounds_.size())
1620 return true;
1621 for (size_t i = 0; i < bounds.size(); ++i) {
1622 if (bounds[i] != composition_character_bounds_[i])
1623 return true;
1624 }
1625 return false;
[email protected]e99ef6f2011-10-16 01:13:001626}
1627
[email protected]73bf95812011-10-12 11:38:321628// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451629COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1630 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1631COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1632 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1633COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1634 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181635COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1636 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1637COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1638 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1639COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1640 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1641COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1642 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1643COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1644 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001645COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1646 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1647COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1648 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1649COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1650 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1651COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1652 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1653COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1654 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1655COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1656 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]ad26ef42011-06-17 07:59:451657
1658ui::TextInputType RenderWidget::GetTextInputType() {
1659 if (webwidget_) {
1660 int type = webwidget_->textInputType();
1661 // Check the type is in the range representable by ui::TextInputType.
[email protected]feb8cf752012-06-08 04:48:001662 DCHECK_LE(type, ui::TEXT_INPUT_TYPE_WEEK) <<
[email protected]ad26ef42011-06-17 07:59:451663 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1664 return static_cast<ui::TextInputType>(type);
1665 }
1666 return ui::TEXT_INPUT_TYPE_NONE;
1667}
1668
[email protected]58b48a0d2012-06-13 07:01:351669void RenderWidget::GetCompositionCharacterBounds(
1670 std::vector<gfx::Rect>* bounds) {
1671 DCHECK(bounds);
1672 bounds->clear();
1673}
1674
[email protected]ad26ef42011-06-17 07:59:451675bool RenderWidget::CanComposeInline() {
1676 return true;
[email protected]56ea1a62011-05-30 07:05:571677}
1678
[email protected]4873c7d2009-07-16 06:36:281679WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:041680 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:281681}
1682
[email protected]f660d9c2012-06-06 18:31:211683float RenderWidget::deviceScaleFactor() {
1684 return device_scale_factor_;
1685}
1686
[email protected]fa7b1dc2010-06-23 17:53:041687void RenderWidget::resetInputMethod() {
1688 if (!input_method_is_active_)
1689 return;
1690
1691 // If the last text input type is not None, then we should finish any
1692 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451693 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041694 // If a composition text exists, then we need to let the browser process
1695 // to cancel the input method's ongoing composition session.
1696 if (webwidget_->confirmComposition())
1697 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1698 }
[email protected]d4cff272011-05-02 15:46:011699
1700 // Send an updated IME range with the current caret rect.
1701 ui::Range range(ui::Range::InvalidRange());
1702 size_t location, length;
1703 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1704 range.set_start(location);
1705 range.set_end(location + length);
1706 }
[email protected]58b48a0d2012-06-13 07:01:351707
1708 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:041709}
1710
[email protected]f103ab72009-09-02 17:10:591711void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501712 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291713 size_t i = 0;
1714 for (; i < plugin_window_moves_.size(); ++i) {
1715 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581716 if (move.rects_valid) {
1717 plugin_window_moves_[i] = move;
1718 } else {
1719 plugin_window_moves_[i].visible = move.visible;
1720 }
initial.commit09911bf2008-07-26 23:55:291721 break;
1722 }
1723 }
1724
1725 if (i == plugin_window_moves_.size())
1726 plugin_window_moves_.push_back(move);
1727}
[email protected]268654772009-08-06 23:02:041728
1729void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1730 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1731 i != plugin_window_moves_.end(); ++i) {
1732 if (i->window == window) {
1733 plugin_window_moves_.erase(i);
1734 break;
1735 }
1736 }
1737}
[email protected]67bfb83f2011-09-22 03:36:371738
1739bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1740 return false;
1741}
[email protected]c3d45532011-10-07 19:20:401742
1743bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1744 return false;
1745}