blob: 905fbe2e5fe426d888d3831421d41a6b1434134c [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]4b1146bc2012-07-10 18:46:0326#include "third_party/WebKit/Source/WebKit/chromium/public/WebHelperPlugin.h"
[email protected]a7547fb2012-03-08 04:43:4427#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3728#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
29#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0130#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3731#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4132#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
33#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]e6e90dc2011-12-03 00:01:3734#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
35#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]d353541f2012-05-03 22:45:4136#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1337#include "ui/base/ui_base_switches.h"
[email protected]08397d52011-02-05 01:53:3838#include "ui/gfx/point.h"
39#include "ui/gfx/size.h"
[email protected]1835b9e2012-02-28 13:12:4840#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2741#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4142#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3443#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5044#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4645#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4846
47#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4948#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5249#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4150#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4851#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4452
[email protected]8bd0fe62011-01-17 06:44:3753#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2954
[email protected]fa7b1dc2010-06-23 17:53:0455using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3056using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2257using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0658using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2859using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4460using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0061using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2862using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5963using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1864using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0165using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5266using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1967using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5268using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2869using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2470using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0471using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1872using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2773using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2274
[email protected]faec7b12012-06-19 14:42:1375static const float kStandardDPI = 160;
[email protected]f1cccb32012-06-06 18:29:5976
[email protected]6fd35b72012-03-01 19:46:4177RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:0478 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:4479 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:2980 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:5681 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:1182 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2983 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4284 host_window_(0),
[email protected]05a980d7a2012-02-07 22:16:4285 host_window_set_(false),
[email protected]b4d08452010-10-05 17:34:3586 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2987 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0688 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0589 update_reply_pending_(false),
[email protected]ea3ee0a2012-05-15 03:43:0990 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:2491 using_asynchronous_swapbuffers_(false),
92 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2993 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2994 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0995 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2996 needs_repainting_on_restore_(false),
97 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4998 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4899 closing_(false),
[email protected]14392a52012-05-02 20:28:44100 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04101 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45102 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
103 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12104 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48105 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12106 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50107 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21108 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07109 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04110 screen_info_(screen_info),
[email protected]75bfbf92012-06-12 19:12:02111 device_scale_factor_(1) {
[email protected]8b3f0eb2012-05-03 19:15:05112 if (!swapped_out)
113 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27114 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18115 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kDisableGpuVsync);
[email protected]424820962012-06-08 15:33:19117#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
[email protected]faec7b12012-06-19 14:42:13118 device_scale_factor_ = screen_info.verticalDPI / kStandardDPI;
119 // Unless an explicit scale factor was provided for testing, ensure the scale
120 // is integral.
121 if (!CommandLine::ForCurrentProcess()->HasSwitch(
122 switches::kForceDeviceScaleFactor))
123 device_scale_factor_ = static_cast<int>(device_scale_factor_);
124 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
[email protected]f1cccb32012-06-06 18:29:59125#endif
initial.commit09911bf2008-07-26 23:55:29126}
127
128RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11129 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21130 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35131 if (current_paint_buf_) {
132 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
133 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29134 }
[email protected]992db4c2011-05-12 15:37:15135 // If we are swapped out, we have released already.
136 if (!is_swapped_out_)
137 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29138}
139
[email protected]484955942010-08-19 16:13:18140// static
[email protected]8085dbc82008-09-26 22:53:44141RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04142 WebKit::WebPopupType popup_type,
143 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29144 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41145 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04146 new RenderWidget(popup_type, screen_info, false));
initial.commit09911bf2008-07-26 23:55:29147 widget->Init(opener_id); // adds reference
148 return widget;
149}
150
[email protected]484955942010-08-19 16:13:18151// static
152WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
153 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03154 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18155 break;
156 case WebKit::WebPopupTypeSelect:
157 case WebKit::WebPopupTypeSuggestion:
158 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44159 case WebKit::WebPopupTypePage:
160 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03161 case WebKit::WebPopupTypeHelperPlugin:
162 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18163 default:
164 NOTREACHED();
165 }
166 return NULL;
167}
168
initial.commit09911bf2008-07-26 23:55:29169void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18170 DoInit(opener_id,
171 RenderWidget::CreateWebWidget(this),
[email protected]9f4f3322012-01-18 22:29:56172 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
173 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18174}
175
[email protected]484955942010-08-19 16:13:18176void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06177 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18178 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29179 DCHECK(!webwidget_);
180
181 if (opener_id != MSG_ROUTING_NONE)
182 opener_id_ = opener_id;
183
[email protected]484955942010-08-19 16:13:18184 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29185
[email protected]380244092011-10-07 17:26:27186 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29187 if (result) {
[email protected]380244092011-10-07 17:26:27188 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29189 // Take a reference on behalf of the RenderThread. This will be balanced
190 // when we receive ViewMsg_Close.
191 AddRef();
192 } else {
193 DCHECK(false);
194 }
195}
196
197// This is used to complete pending inits and non-pending inits. For non-
198// pending cases, the parent will be the same as the current parent. This
199// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55200void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29201 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29202
203 host_window_ = parent_hwnd;
[email protected]05a980d7a2012-02-07 22:16:42204 host_window_set_ = true;
205
[email protected]f1d5de42012-04-20 01:47:14206#if WEBWIDGET_HAS_SETCOMPOSITORSURFACEREADY
207 if (webwidget_)
208 webwidget_->setCompositorSurfaceReady();
209#endif
[email protected]05a980d7a2012-02-07 22:16:42210 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29211
[email protected]6de74452009-02-25 18:04:59212 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29213}
214
[email protected]992db4c2011-05-12 15:37:15215void RenderWidget::SetSwappedOut(bool is_swapped_out) {
216 // We should only toggle between states.
217 DCHECK(is_swapped_out_ != is_swapped_out);
218 is_swapped_out_ = is_swapped_out;
219
220 // If we are swapping out, we will call ReleaseProcess, allowing the process
221 // to exit if all of its RenderViews are swapped out. We wait until the
222 // WasSwappedOut call to do this, to avoid showing the sad tab.
223 // If we are swapping in, we call AddRefProcess to prevent the process from
224 // exiting.
225 if (!is_swapped_out)
226 RenderProcess::current()->AddRefProcess();
227}
228
[email protected]a95986a82010-12-24 06:19:28229bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
230 bool handled = true;
231 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
232 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
233 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
234 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54235 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28236 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
237 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15238 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28239 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]a7266a92012-06-28 02:11:08240 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, OnSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28241 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
242 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
243 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
244 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
245 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
246 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
247 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
248 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
[email protected]fea38fc2012-06-13 17:38:37249 IPC_MESSAGE_HANDLER(ViewMsg_SetDeviceScaleFactor, OnSetDeviceScaleFactor)
[email protected]a95986a82010-12-24 06:19:28250 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
251 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53252 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]a95986a82010-12-24 06:19:28253 IPC_MESSAGE_UNHANDLED(handled = false)
254 IPC_END_MESSAGE_MAP()
255 return handled;
256}
initial.commit09911bf2008-07-26 23:55:29257
258bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15259 // Don't send any messages after the browser has told us to close, and filter
260 // most outgoing messages while swapped out.
261 if ((is_swapped_out_ &&
262 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11263 closing_) {
initial.commit09911bf2008-07-26 23:55:29264 delete message;
265 return false;
266 }
267
268 // If given a messsage without a routing ID, then assign our routing ID.
269 if (message->routing_id() == MSG_ROUTING_NONE)
270 message->set_routing_id(routing_id_);
271
[email protected]380244092011-10-07 17:26:27272 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44273}
274
[email protected]61e2b3cc2012-03-02 16:13:34275void RenderWidget::Resize(const gfx::Size& new_size,
276 const gfx::Rect& resizer_rect,
277 bool is_fullscreen,
278 ResizeAck resize_ack) {
279 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
280 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
281 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29282
[email protected]61e2b3cc2012-03-02 16:13:34283 // Ignore this during shutdown.
284 if (!webwidget_)
285 return;
286
287 // Remember the rect where the resize corner will be drawn.
288 resizer_rect_ = resizer_rect;
289
290 // NOTE: We may have entered fullscreen mode without changing our size.
291 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
292 if (fullscreen_change)
293 WillToggleFullscreen();
294 is_fullscreen_ = is_fullscreen;
295
296 if (size_ != new_size) {
297 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34298 needs_repainting_on_restore_ = false;
299
300 size_ = new_size;
301
302 paint_aggregator_.ClearPendingUpdate();
303
304 // When resizing, we want to wait to paint before ACK'ing the resize. This
305 // ensures that we only resize as fast as we can paint. We only need to
306 // send an ACK if we are resized to a non-empty rect.
307 webwidget_->resize(new_size);
308 if (!new_size.IsEmpty()) {
309 if (!is_accelerated_compositing_active_) {
310 // Resize should have caused an invalidation of the entire view.
311 DCHECK(paint_aggregator_.HasPendingUpdate());
312 }
313
314 // Send the Resize_ACK flag once we paint again if requested.
315 if (resize_ack == SEND_RESIZE_ACK)
316 set_next_paint_is_resize_ack();
317 }
[email protected]ff475a322012-03-14 00:05:35318 } else {
319 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34320 }
321
322 if (fullscreen_change)
323 DidToggleFullscreen();
324
325 // If a resize ack is requested and it isn't set-up, then no more resizes will
326 // come in and in general things will go wrong.
327 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
328 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29329}
330
331void RenderWidget::OnClose() {
332 if (closing_)
333 return;
334 closing_ = true;
335
336 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03337 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27338 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03339 SetHidden(false);
340 }
initial.commit09911bf2008-07-26 23:55:29341
initial.commit09911bf2008-07-26 23:55:29342 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25343 // now. Post a task that only gets invoked when there are no nested message
344 // loops.
[email protected]32876ae2011-11-15 22:25:21345 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29346 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25347
348 // Balances the AddRef taken when we called AddRoute.
349 Release();
initial.commit09911bf2008-07-26 23:55:29350}
351
[email protected]61e2b3cc2012-03-02 16:13:34352// Got a response from the browser after the renderer decided to create a new
353// view.
354void RenderWidget::OnCreatingNewAck(
355 gfx::NativeViewId parent) {
356 DCHECK(routing_id_ != MSG_ROUTING_NONE);
357
358 CompleteInit(parent);
359}
360
[email protected]f21c613a2009-02-12 14:46:17361void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09362 const gfx::Rect& resizer_rect,
363 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34364 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29365}
366
[email protected]b5913d72012-02-07 22:26:54367void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
368 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59369 gfx::Rect view_rect(size_);
370
371 gfx::Rect old_damage_rect = view_rect.Intersect(resizer_rect_);
372 if (!old_damage_rect.IsEmpty())
373 paint_aggregator_.InvalidateRect(old_damage_rect);
374
375 gfx::Rect new_damage_rect = view_rect.Intersect(resizer_rect);
376 if (!new_damage_rect.IsEmpty())
377 paint_aggregator_.InvalidateRect(new_damage_rect);
378
[email protected]b5913d72012-02-07 22:26:54379 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59380
[email protected]b5913d72012-02-07 22:26:54381 if (webwidget_)
382 webwidget_->didChangeWindowResizerRect();
383 }
384}
385
initial.commit09911bf2008-07-26 23:55:29386void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31387 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29388 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03389 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29390}
391
392void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31393 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29394 // During shutdown we can just ignore this message.
395 if (!webwidget_)
396 return;
397
398 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03399 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29400
401 if (!needs_repainting && !needs_repainting_on_restore_)
402 return;
403 needs_repainting_on_restore_ = false;
404
[email protected]d65adb12010-04-28 17:26:49405 // Tag the next paint as a restore ack, which is picked up by
406 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29407 set_next_paint_is_restore_ack();
408
409 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56410 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46411 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
412 } else {
413 scheduleComposite();
414 }
initial.commit09911bf2008-07-26 23:55:29415}
416
[email protected]992db4c2011-05-12 15:37:15417void RenderWidget::OnWasSwappedOut() {
418 // If we have been swapped out and no one else is using this process,
419 // it's safe to exit now. If we get swapped back in, we will call
420 // AddRefProcess in SetSwappedOut.
421 if (is_swapped_out_)
422 RenderProcess::current()->ReleaseProcess();
423}
424
[email protected]53d3f302009-12-21 04:42:05425void RenderWidget::OnRequestMoveAck() {
426 DCHECK(pending_window_rect_count_);
427 pending_window_rect_count_--;
428}
429
430void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58431 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21432 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05433 update_reply_pending_ = false;
434
[email protected]b4d08452010-10-05 17:34:35435 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
436 // have no current paint buffer.
437 if (current_paint_buf_) {
438 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
439 current_paint_buf_ = NULL;
440 }
441
[email protected]65225772011-05-12 21:10:24442 // If swapbuffers is still pending, then defer the update until the
443 // swapbuffers occurs.
444 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
445 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
446 return;
447 }
448
[email protected]29ed96a2012-02-04 18:12:16449 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18450 if (!is_accelerated_compositing_active_) {
451 DidFlushPaint();
452 }
[email protected]a2f6bc112009-06-27 16:27:25453
initial.commit09911bf2008-07-26 23:55:29454 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24455 DoDeferredUpdateAndSendInputAck();
456}
457
[email protected]d0be63772011-12-20 23:18:04458bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24459 return false;
460}
461
[email protected]d0be63772011-12-20 23:18:04462void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24463 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21464 while (!updates_pending_swap_.empty()) {
465 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
466 updates_pending_swap_.pop_front();
467 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
468 // compositing pass, hence doesn't require an UpdateRect message.
469 if (msg)
470 Send(msg);
471 }
[email protected]65225772011-05-12 21:10:24472 num_swapbuffers_complete_pending_ = 0;
473 using_asynchronous_swapbuffers_ = false;
474 // Schedule another frame so the compositor learns about it.
475 scheduleComposite();
476}
477
[email protected]37a6f302011-07-11 23:43:08478void RenderWidget::OnSwapBuffersPosted() {
479 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21480
481 if (using_asynchronous_swapbuffers_) {
482 ViewHostMsg_UpdateRect* msg = NULL;
483 // pending_update_params_ can be NULL if the swap doesn't correspond to an
484 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
485 // message.
486 if (pending_update_params_.get()) {
487 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
488 pending_update_params_.reset();
489 }
490 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08491 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21492 }
[email protected]37a6f302011-07-11 23:43:08493}
494
495void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24496 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16497
[email protected]404939f2012-06-01 04:06:18498 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16499 DidFlushPaint();
500
[email protected]65225772011-05-12 21:10:24501 // When compositing deactivates, we reset the swapbuffers pending count. The
502 // swapbuffers acks may still arrive, however.
503 if (num_swapbuffers_complete_pending_ == 0) {
504 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
505 return;
506 }
[email protected]aa4117f2011-12-09 22:19:21507 DCHECK(!updates_pending_swap_.empty());
508 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
509 updates_pending_swap_.pop_front();
510 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
511 // compositing pass, hence doesn't require an UpdateRect message.
512 if (msg)
513 Send(msg);
[email protected]65225772011-05-12 21:10:24514 num_swapbuffers_complete_pending_--;
515
516 // If update reply is still pending, then defer the update until that reply
517 // occurs.
[email protected]d0be63772011-12-20 23:18:04518 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24519 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
520 return;
521 }
522
523 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06524 // when we were previously rendering. However, if an invalidation task is not
525 // posted, there may be software rendering work pending. In that case, don't
526 // early out.
527 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24528 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
529 return;
530 }
531
[email protected]65225772011-05-12 21:10:24532 // Continue painting if necessary...
533 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29534}
535
initial.commit09911bf2008-07-26 23:55:29536void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24537 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
[email protected]ce208f872012-03-07 20:42:56538 PickleIterator iter(message);
initial.commit09911bf2008-07-26 23:55:29539
540 const char* data;
541 int data_length;
[email protected]5dd768212009-08-13 23:34:49542 handling_input_event_ = true;
543 if (!message.ReadData(&iter, &data, &data_length)) {
544 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29545 return;
[email protected]5dd768212009-08-13 23:34:49546 }
initial.commit09911bf2008-07-26 23:55:29547
548 const WebInputEvent* input_event =
549 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48550
[email protected]b68a0e52011-12-08 15:11:12551 bool is_keyboard_shortcut = false;
552 // is_keyboard_shortcut flag is only available for RawKeyDown events.
553 if (input_event->type == WebInputEvent::RawKeyDown)
554 message.ReadBool(&iter, &is_keyboard_shortcut);
555
[email protected]67bfb83f2011-09-22 03:36:37556 bool prevent_default = false;
557 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26558 const WebMouseEvent& mouse_event =
559 *static_cast<const WebMouseEvent*>(input_event);
560 TRACE_EVENT2("renderer", "HandleMouseMove",
561 "x", mouse_event.x, "y", mouse_event.y);
562 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37563 }
564
565 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12566 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
567 suppress_next_char_events_ = false;
568 if (!processed && webwidget_)
569 processed = webwidget_->handleInputEvent(*input_event);
570 }
571
572 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
573 // it's not processed by webkit, then we need to suppress the upcoming Char
574 // events.
575 if (!processed && is_keyboard_shortcut)
576 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29577
[email protected]a9fb30aa2011-10-06 06:58:46578 IPC::Message* response =
579 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
580 processed);
[email protected]3391a0772012-03-28 00:32:07581 bool event_type_gets_rate_limited =
582 input_event->type == WebInputEvent::MouseMove ||
583 input_event->type == WebInputEvent::MouseWheel ||
584 WebInputEvent::isTouchEventType(input_event->type);
585 bool is_input_throttled =
586 webwidget_->isInputThrottled() ||
587 paint_aggregator_.HasPendingUpdate();
[email protected]e2824412009-02-27 01:57:05588
[email protected]f8868d72012-04-27 19:13:03589 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24590 // We want to rate limit the input events in this case, so we'll wait for
591 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17592 if (pending_input_event_ack_.get()) {
593 // As two different kinds of events could cause us to postpone an ack
594 // we send it now, if we have one pending. The Browser should never
595 // send us the same kind of event we are delaying the ack for.
596 Send(pending_input_event_ack_.release());
597 }
[email protected]12fbad812009-09-01 18:21:24598 pending_input_event_ack_.reset(response);
599 } else {
600 Send(response);
601 }
602
[email protected]5dd768212009-08-13 23:34:49603 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48604
[email protected]67bfb83f2011-09-22 03:36:37605 if (!prevent_default) {
606 if (WebInputEvent::isKeyboardEventType(input_event->type))
607 DidHandleKeyEvent();
608 if (WebInputEvent::isMouseEventType(input_event->type))
609 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24610 if (WebInputEvent::isTouchEventType(input_event->type))
611 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37612 }
initial.commit09911bf2008-07-26 23:55:29613}
614
615void RenderWidget::OnMouseCaptureLost() {
616 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28617 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29618}
619
620void RenderWidget::OnSetFocus(bool enable) {
621 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33622 if (webwidget_)
623 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29624}
625
626void RenderWidget::ClearFocus() {
627 // We may have got the focus from the browser before this gets processed, in
628 // which case we do not want to unfocus ourself.
629 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28630 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29631}
632
[email protected]2d5d09d52009-06-15 14:29:21633void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00634 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21635 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06636 TRACE_EVENT2("renderer", "PaintRect",
637 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00638 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21639
640 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00641 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
642 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03643
[email protected]699ab0d2009-04-23 23:19:14644 // If there is a custom background, tile it.
645 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14646 SkPaint paint;
647 SkShader* shader = SkShader::CreateBitmapShader(background_,
648 SkShader::kRepeat_TileMode,
649 SkShader::kRepeat_TileMode);
650 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20651
652 // Use kSrc_Mode to handle background_ transparency properly.
653 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
654
655 // Canvas could contain multiple update rects. Clip to given rect so that
656 // we don't accidentally clear other update rects.
657 canvas->save();
[email protected]1835b9e2012-02-28 13:12:48658 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14659 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20660 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14661 }
662
[email protected]719b36f2010-12-22 20:36:46663 // First see if this rect is a plugin that can paint itself faster.
664 TransportDIB* optimized_dib = NULL;
665 gfx::Rect optimized_copy_rect, optimized_copy_location;
666 webkit::ppapi::PluginInstance* optimized_instance =
667 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
668 &optimized_copy_location,
669 &optimized_copy_rect);
670 if (optimized_instance) {
671 // This plugin can be optimize-painted and we can just ask it to paint
672 // itself. We don't actually need the TransportDIB in this case.
673 //
674 // This is an optimization for PPAPI plugins that know they're on top of
675 // the page content. If this rect is inside such a plugin, we can save some
676 // time and avoid re-rendering the page content which we know will be
677 // covered by the plugin later (this time can be significant, especially
678 // for a playing movie that is invalidating a lot).
679 //
680 // In the plugin movie case, hopefully the similar call to
681 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
682 // painting, because that avoids copying the plugin image to a different
683 // paint rect. Unfortunately, if anything on the page is animating other
684 // than the movie, it break this optimization since the union of the
685 // invalid regions will be larger than the plugin.
686 //
687 // This code optimizes that case, where we can still avoid painting in
688 // WebKit and filling the background (which can be slow) and just painting
689 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
690 // required.
691 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27692 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46693 } else {
694 // Normal painting case.
695 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
696
697 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35698 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46699 }
initial.commit09911bf2008-07-26 23:55:29700
[email protected]4fb66842009-12-04 21:41:00701 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00702 canvas->restore();
703}
704
705void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
706 skia::PlatformCanvas* canvas) {
707 static bool kPaintBorder =
708 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
709 if (!kPaintBorder)
710 return;
711
[email protected]53d3f302009-12-21 04:42:05712 // Cycle through these colors to help distinguish new paint rects.
713 const SkColor colors[] = {
714 SkColorSetARGB(0x3F, 0xFF, 0, 0),
715 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
716 SkColorSetARGB(0x3F, 0, 0, 0xFF),
717 };
718 static int color_selector = 0;
719
[email protected]4fb66842009-12-04 21:41:00720 SkPaint paint;
721 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05722 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00723 paint.setStrokeWidth(1);
724
725 SkIRect irect;
726 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
727 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29728}
729
[email protected]52ccd0ea2011-02-16 01:09:05730void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30731 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30732 if (!animation_update_pending_) {
733 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59734 return;
[email protected]921244e42011-07-20 16:36:30735 }
[email protected]bd37ae252011-06-03 01:28:18736 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59737 // Record when we fired (according to base::Time::Now()) relative to when
738 // we posted the task to quantify how much the base::Time/base::TimeTicks
739 // skew is affecting animations.
740 base::TimeDelta animation_callback_delay = base::Time::Now() -
741 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
742 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
743 animation_callback_delay,
744 base::TimeDelta::FromMilliseconds(0),
745 base::TimeDelta::FromMilliseconds(30),
746 25);
747 }
[email protected]65225772011-05-12 21:10:24748 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24749}
750
[email protected]52ccd0ea2011-02-16 01:09:05751void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59752 if (!animation_update_pending_)
753 return;
[email protected]bd37ae252011-06-03 01:28:18754
755 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33756 base::TimeDelta animationInterval = IsRenderingVSynced() ?
757 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18758
[email protected]7c4329e2011-02-18 22:02:59759 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45760
761 // animation_floor_time_ is the earliest time that we should animate when
762 // using the dead reckoning software scheduler. If we're using swapbuffers
763 // complete callbacks to rate limit, we can ignore this floor.
764 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30765 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33766 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18767 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59768 // running animation callbacks so that if a callback requests another
769 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38770 animation_timer_.Stop();
771 animation_timer_.Start(FROM_HERE, animationInterval, this,
772 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59773 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30774 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59775 return;
[email protected]5f8b1022011-01-21 23:34:50776 }
[email protected]bd37ae252011-06-03 01:28:18777 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38778 if (!animation_timer_.IsRunning()) {
779 // This code uses base::Time::Now() to calculate the floor and next fire
780 // time because javascript's Date object uses base::Time::Now(). The
781 // message loop uses base::TimeTicks, which on windows can have a
782 // different granularity than base::Time.
783 // The upshot of all this is that this function might be called before
784 // base::Time::Now() has advanced past the animation_floor_time_. To
785 // avoid exposing this delay to javascript, we keep posting delayed
786 // tasks until base::Time::Now() has advanced far enough.
787 base::TimeDelta delay = animation_floor_time_ - now;
788 animation_timer_.Start(FROM_HERE, delay, this,
789 &RenderWidget::AnimationCallback);
790 }
[email protected]5f8b1022011-01-21 23:34:50791}
792
[email protected]bd37ae252011-06-03 01:28:18793bool RenderWidget::IsRenderingVSynced() {
794 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
795 // not caught by this check. This will lead to artificially low frame rates
796 // for people who force vsync off at a driver level and expect Chrome to speed
797 // up.
798 return !has_disable_gpu_vsync_switch_;
799}
800
[email protected]65225772011-05-12 21:10:24801void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06802 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24803 invalidation_task_posted_ = false;
804 DoDeferredUpdateAndSendInputAck();
805}
806
807void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05808 DoDeferredUpdate();
809
810 if (pending_input_event_ack_.get())
811 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21812}
813
[email protected]552e6002009-11-19 05:24:57814void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58815 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08816
[email protected]65225772011-05-12 21:10:24817 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29818 return;
[email protected]05a980d7a2012-02-07 22:16:42819
820 if (!host_window_set_) {
821 TRACE_EVENT0("renderer", "EarlyOut_NoHostWindow");
822 return;
823 }
[email protected]aa4117f2011-12-09 22:19:21824 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24825 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
826 return;
827 }
[email protected]9ca84622011-06-02 23:46:39828 if (is_accelerated_compositing_active_ &&
829 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24830 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
831 return;
832 }
initial.commit09911bf2008-07-26 23:55:29833
[email protected]552e6002009-11-19 05:24:57834 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29835 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57836 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29837 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24838 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29839 return;
840 }
841
[email protected]05a980d7a2012-02-07 22:16:42842 if (is_accelerated_compositing_active_)
843 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
844
[email protected]0fb93f52011-05-18 23:13:56845 // Tracking of frame rate jitter
846 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]541dcd52012-03-15 15:57:51847 webwidget_->instrumentBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:05848 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50849
[email protected]f98d7e3c2010-09-13 22:30:46850 // Layout may generate more invalidation. It may also enable the
851 // GPU acceleration, so make sure to run layout before we send the
852 // GpuRenderingActivated message.
853 webwidget_->layout();
854
[email protected]dcca3aa92012-02-17 23:03:37855 // The following two can result in further layout and possibly
856 // enable GPU acceleration so they need to be called before any painting
857 // is done.
858 UpdateTextInputState();
859 UpdateSelectionBounds();
860
[email protected]5f8b1022011-01-21 23:34:50861 // Suppress painting if nothing is dirty. This has to be done after updating
862 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24863 if (!paint_aggregator_.HasPendingUpdate()) {
864 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]541dcd52012-03-15 15:57:51865 webwidget_->instrumentCancelFrame();
[email protected]5f8b1022011-01-21 23:34:50866 return;
[email protected]65225772011-05-12 21:10:24867 }
[email protected]5f8b1022011-01-21 23:34:50868
[email protected]872ae5b2011-05-26 20:20:50869 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56870 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04871 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56872 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
873 delay,
874 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41875 base::TimeDelta::FromMilliseconds(120),
876 60);
[email protected]d0be63772011-12-20 23:18:04877 } else {
[email protected]0fb93f52011-05-18 23:13:56878 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
879 delay,
880 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41881 base::TimeDelta::FromMilliseconds(120),
882 60);
[email protected]d0be63772011-12-20 23:18:04883 }
[email protected]872ae5b2011-05-26 20:20:50884
885 // Calculate filtered time per frame:
886 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
887 filtered_time_per_frame_ =
888 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56889 }
890 last_do_deferred_update_time_ = frame_begin_ticks;
891
[email protected]552e6002009-11-19 05:24:57892 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29893 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30894 PaintAggregator::PendingUpdate update;
895 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29896
[email protected]53d3f302009-12-21 04:42:05897 gfx::Rect scroll_damage = update.GetScrollDamage();
898 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29899
[email protected]29ed96a2012-02-04 18:12:16900 // Notify derived classes that we're about to initiate a paint.
901 WillInitiatePaint();
902
[email protected]ca4847f2010-09-24 05:39:15903 // A plugin may be able to do an optimized paint. First check this, in which
904 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46905 // This optimization allows PPAPI plugins that declare themselves on top of
906 // the page (like a traditional windowed plugin) to be able to animate (think
907 // movie playing) without repeatedly re-painting the page underneath, or
908 // copying the plugin backing store (since we can send the plugin's backing
909 // store directly to the browser).
910 //
911 // This optimization only works when the entire invalid region is contained
912 // within the plugin. There is a related optimization in PaintRect for the
913 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15914 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15915 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]aa4117f2011-12-09 22:19:21916 DCHECK(!pending_update_params_.get());
917 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
918 pending_update_params_->dx = update.scroll_delta.x();
919 pending_update_params_->dy = update.scroll_delta.y();
920 pending_update_params_->scroll_rect = update.scroll_rect;
921 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:21922 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
923 pending_update_params_->flags = next_paint_flags_;
924 pending_update_params_->scroll_offset = GetScrollOffset();
925 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:09926 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:21927 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:09928 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:21929
[email protected]ca4847f2010-09-24 05:39:15930 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56931 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15932 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
933 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27934 // Only update the part of the plugin that actually changed.
935 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21936 pending_update_params_->bitmap = dib->id();
937 pending_update_params_->bitmap_rect = optimized_copy_location;
938 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]a79d8a632010-11-18 22:35:56939 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46940 // Compute a buffer for painting and cache it.
[email protected]f1cccb32012-06-06 18:29:59941 gfx::Rect pixel_bounds = bounds.Scale(device_scale_factor_);
[email protected]ca4847f2010-09-24 05:39:15942 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35943 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:59944 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:46945 if (!canvas.get()) {
946 NOTREACHED();
947 return;
948 }
[email protected]cef3362f2009-12-21 17:48:45949
[email protected]f98d7e3c2010-09-13 22:30:46950 // We may get back a smaller canvas than we asked for.
951 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:59952 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
953 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
954 pixel_bounds.set_width(canvas->getDevice()->width());
955 pixel_bounds.set_height(canvas->getDevice()->height());
956 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
957 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:05958
[email protected]f98d7e3c2010-09-13 22:30:46959 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
960
[email protected]aa4117f2011-12-09 22:19:21961 pending_update_params_->bitmap = current_paint_buf_->id();
962 pending_update_params_->bitmap_rect = bounds;
963
964 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:46965 // The scroll damage is just another rectangle to paint and copy.
966 copy_rects.swap(update.paint_rects);
967 if (!scroll_damage.IsEmpty())
968 copy_rects.push_back(scroll_damage);
969
970 for (size_t i = 0; i < copy_rects.size(); ++i)
[email protected]f1cccb32012-06-06 18:29:59971 PaintRect(copy_rects[i], pixel_bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:35972
973 // Software FPS tick for performance tests. The accelerated path traces the
974 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
975 // NOTE: Tests may break if this event is renamed or moved.
976 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:46977 } else { // Accelerated compositing path
978 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:21979 // If painting is done via the gpu process then we don't set any damage
980 // rects to save the browser process from doing unecessary work.
981 pending_update_params_->bitmap_rect = bounds;
982 pending_update_params_->scroll_rect = gfx::Rect();
983 // We don't need an ack, because we're not sharing a DIB with the browser.
984 // If it needs to (e.g. composited UI), the GPU process does its own ACK
985 // with the browser for the GPU surface.
986 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:42987 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46988 }
989
[email protected]936c6f52011-12-13 01:35:26990 // If we're holding a pending input event ACK, send the ACK before sending the
991 // UpdateReply message so we can receive another input event before the
992 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
993 // the UpdateRect IPC message handler.
994 if (pending_input_event_ack_.get())
995 Send(pending_input_event_ack_.release());
996
[email protected]aa4117f2011-12-09 22:19:21997 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
998 // OnSwapBuffersPosted), meaning a message has been added to the
999 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1000 // the message now.
1001 if (pending_update_params_.get()) {
1002 // sending an ack to browser process that the paint is complete...
1003 update_reply_pending_ = pending_update_params_->needs_ack;
1004 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1005 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341006 }
[email protected]53d3f302009-12-21 04:42:051007
[email protected]29ed96a2012-02-04 18:12:161008 // If we're software rendering then we're done initiating the paint.
1009 if (!is_accelerated_compositing_active_)
1010 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291011}
1012
1013///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461014// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291015
[email protected]4873c7d2009-07-16 06:36:281016void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571017 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481018 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571019 gfx::Rect damaged_rect = view_rect.Intersect(rect);
1020 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291021 return;
1022
[email protected]552e6002009-11-19 05:24:571023 paint_aggregator_.InvalidateRect(damaged_rect);
1024
1025 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241026 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571027 return;
1028 if (!paint_aggregator_.HasPendingUpdate())
1029 return;
[email protected]aa4117f2011-12-09 22:19:211030 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241031 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1032 return;
1033
1034 // When GPU rendering, combine pending animations and invalidations into
1035 // a single update.
[email protected]816edc62012-03-17 01:27:221036 if (is_accelerated_compositing_active_ &&
1037 animation_update_pending_ &&
1038 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571039 return;
1040
1041 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291042 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1043 // on the call stack.
1044 // 2) Allows us to collect more damage rects before painting to help coalesce
1045 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241046 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211047 MessageLoop::current()->PostTask(
1048 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291049}
1050
[email protected]4873c7d2009-07-16 06:36:281051void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461052 // Drop scrolls on the floor when we are in compositing mode.
1053 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561054 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461055 return;
1056
[email protected]552e6002009-11-19 05:24:571057 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481058 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571059 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
1060 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291061 return;
1062
[email protected]552e6002009-11-19 05:24:571063 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
1064
1065 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241066 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571067 return;
1068 if (!paint_aggregator_.HasPendingUpdate())
1069 return;
[email protected]aa4117f2011-12-09 22:19:211070 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241071 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1072 return;
1073
1074 // When GPU rendering, combine pending animations and invalidations into
1075 // a single update.
[email protected]816edc62012-03-17 01:27:221076 if (is_accelerated_compositing_active_ &&
1077 animation_update_pending_ &&
1078 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571079 return;
1080
1081 // Perform updating asynchronously. This serves two purposes:
1082 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1083 // on the call stack.
1084 // 2) Allows us to collect more damage rects before painting to help coalesce
1085 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241086 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211087 MessageLoop::current()->PostTask(
1088 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291089}
1090
[email protected]244ac1892011-12-02 17:04:471091void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091092 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1093 size_ = new_size;
1094 need_update_rect_for_auto_resize_ = true;
1095 }
[email protected]244ac1892011-12-02 17:04:471096}
1097
[email protected]91acd1c2012-03-14 08:32:391098void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221099 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1100
[email protected]c63b4d42012-04-26 01:01:071101#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211102 if (!is_accelerated_compositing_active_) {
1103 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1104 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1105 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1106 // going to switch to accelerated compositing, the GPU process may need
1107 // round-trips to the browser's UI thread before finishing the frame,
1108 // causing deadlocks if we delay the UpdateRect until we receive the
1109 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071110 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1111 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211112 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1113 }
[email protected]c63b4d42012-04-26 01:01:071114#endif
[email protected]aa4117f2011-12-09 22:19:211115
[email protected]ea162f92011-10-04 23:08:221116 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421117 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241118 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221119}
1120
1121void RenderWidget::didDeactivateCompositor() {
1122 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1123
1124 is_accelerated_compositing_active_ = false;
1125 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1126 routing_id_, is_accelerated_compositing_active_));
1127
[email protected]ea162f92011-10-04 23:08:221128 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241129 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561130}
1131
[email protected]9cd43a62012-03-26 08:03:561132void RenderWidget::willBeginCompositorFrame() {
1133 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371134
1135 DCHECK(RenderThreadImpl::current()->compositor_thread());
1136
1137 // The following two can result in further layout and possibly
1138 // enable GPU acceleration so they need to be called before any painting
1139 // is done.
1140 UpdateTextInputState();
1141 UpdateSelectionBounds();
1142
[email protected]9cd43a62012-03-26 08:03:561143 WillInitiatePaint();
1144}
1145
[email protected]3391a0772012-03-28 00:32:071146void RenderWidget::didBecomeReadyForAdditionalInput() {
1147 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1148 if (pending_input_event_ack_.get())
1149 Send(pending_input_event_ack_.release());
1150}
1151
[email protected]58264a32011-11-17 23:36:151152void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501153 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351154 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1155 // NOTE: Tests may break if this event is renamed or moved.
1156 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161157 // Notify subclasses that we initiated the paint operation.
1158 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151159}
1160
1161void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181162 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1163
1164 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561165 DidFlushPaint();
1166
[email protected]aa4117f2011-12-09 22:19:211167 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151168 return;
1169
[email protected]ea3ee0a2012-05-15 03:43:091170 if (!next_paint_flags_ &&
1171 !need_update_rect_for_auto_resize_ &&
1172 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151173 return;
[email protected]ea3ee0a2012-05-15 03:43:091174 }
[email protected]58264a32011-11-17 23:36:151175
1176 ViewHostMsg_UpdateRect_Params params;
1177 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151178 params.plugin_window_moves.swap(plugin_window_moves_);
1179 params.flags = next_paint_flags_;
1180 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121181 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091182 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151183
1184 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1185 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091186 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151187}
1188
[email protected]f98d7e3c2010-09-13 22:30:461189void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041190 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401191 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041192 } else {
[email protected]c3d45532011-10-07 19:20:401193 // TODO(nduca): replace with something a little less hacky. The reason this
1194 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1195 // contains a lot of host-renderer synchronization logic that is still
1196 // important for the accelerated compositing case. The option of simply
1197 // duplicating all that code is less desirable than "faking out" the
1198 // invalidation path using a magical damage rect.
1199 didInvalidateRect(WebRect(0, 0, 1, 1));
1200 }
[email protected]f98d7e3c2010-09-13 22:30:461201}
1202
[email protected]5f8b1022011-01-21 23:34:501203void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201204 if (animation_update_pending_)
1205 return;
1206
[email protected]921244e42011-07-20 16:36:301207 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201208 animation_update_pending_ = true;
1209 if (!animation_timer_.IsRunning()) {
1210 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1211 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211212 }
[email protected]5f8b1022011-01-21 23:34:501213}
1214
[email protected]4873c7d2009-07-16 06:36:281215void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301216 // TODO(darin): Eliminate this temporary.
1217 WebCursor cursor(cursor_info);
1218
initial.commit09911bf2008-07-26 23:55:291219 // Only send a SetCursor message if we need to make a change.
1220 if (!current_cursor_.IsEqual(cursor)) {
1221 current_cursor_ = cursor;
1222 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1223 }
1224}
1225
1226// We are supposed to get a single call to Show for a newly created RenderWidget
1227// that was created via RenderWidget::CreateWebView. So, we wait until this
1228// point to dispatch the ShowWidget message.
1229//
1230// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281231// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291232//
[email protected]4873c7d2009-07-16 06:36:281233void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291234 DCHECK(!did_show_) << "received extraneous Show call";
1235 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1236 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1237
[email protected]8de12d942010-11-17 20:42:441238 if (did_show_)
1239 return;
1240
1241 did_show_ = true;
1242 // NOTE: initial_pos_ may still have its default values at this point, but
1243 // that's okay. It'll be ignored if as_popup is false, or the browser
1244 // process will impose a default position otherwise.
1245 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1246 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291247}
1248
[email protected]4873c7d2009-07-16 06:36:281249void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291250}
1251
[email protected]4873c7d2009-07-16 06:36:281252void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291253}
1254
[email protected]2533ce12009-05-09 00:02:241255void RenderWidget::DoDeferredClose() {
1256 Send(new ViewHostMsg_Close(routing_id_));
1257}
1258
[email protected]4873c7d2009-07-16 06:36:281259void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321260 if (is_swapped_out_) {
1261 // This widget is currently swapped out, and the active widget is in a
1262 // different process. Have the browser route the close request to the
1263 // active widget instead, so that the correct unload handlers are run.
1264 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1265 return;
1266 }
1267
initial.commit09911bf2008-07-26 23:55:291268 // If a page calls window.close() twice, we'll end up here twice, but that's
1269 // OK. It is safe to send multiple Close messages.
1270
[email protected]2533ce12009-05-09 00:02:241271 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1272 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1273 // could be closed before the JS finishes executing. So instead, post a
1274 // message back to the message loop, which won't run until the JS is
1275 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211276 MessageLoop::current()->PostTask(
1277 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291278}
1279
1280void RenderWidget::Close() {
1281 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281282 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291283 webwidget_ = NULL;
1284 }
1285}
1286
[email protected]4873c7d2009-07-16 06:36:281287WebRect RenderWidget::windowRect() {
1288 if (pending_window_rect_count_)
1289 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241290
[email protected]b3f2b912009-04-09 16:18:521291 gfx::Rect rect;
1292 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281293 return rect;
initial.commit09911bf2008-07-26 23:55:291294}
1295
[email protected]8a9d6ca32011-06-06 20:11:301296void RenderWidget::setToolTipText(const WebKit::WebString& text,
1297 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541298 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301299}
1300
[email protected]4873c7d2009-07-16 06:36:281301void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291302 if (did_show_) {
1303 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241304 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291305 } else {
1306 initial_pos_ = pos;
1307 }
1308}
1309
[email protected]2533ce12009-05-09 00:02:241310void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1311 pending_window_rect_ = rect;
1312 pending_window_rect_count_++;
1313}
1314
[email protected]4873c7d2009-07-16 06:36:281315WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241316 if (pending_window_rect_count_) {
1317 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1318 // the RootWindowRect is probably going to return wrong results since the
1319 // browser may not have processed the Move yet. There isn't really anything
1320 // good to do in this case, and it shouldn't happen - since this size is
1321 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281322 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241323 }
1324
[email protected]b3f2b912009-04-09 16:18:521325 gfx::Rect rect;
1326 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281327 return rect;
[email protected]d4547452008-08-28 18:36:371328}
1329
[email protected]4873c7d2009-07-16 06:36:281330WebRect RenderWidget::windowResizerRect() {
1331 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191332}
1333
[email protected]fa7b1dc2010-06-23 17:53:041334void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031335 // To prevent this renderer process from sending unnecessary IPC messages to
1336 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041337 // only during the input method attached to the browser process is active.
1338 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291339}
1340
[email protected]58b48a0d2012-06-13 07:01:351341void RenderWidget::UpdateCompositionInfo(
1342 const ui::Range& range,
1343 const std::vector<gfx::Rect>& character_bounds) {
1344 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1345 return;
1346 composition_character_bounds_ = character_bounds;
1347 composition_range_ = range;
1348 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1349 routing_id(), composition_range_, composition_character_bounds_));
1350}
1351
[email protected]fa7b1dc2010-06-23 17:53:041352void RenderWidget::OnImeSetComposition(
1353 const string16& text,
1354 const std::vector<WebCompositionUnderline>& underlines,
1355 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281356 if (!webwidget_)
1357 return;
[email protected]d4cff272011-05-02 15:46:011358 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041359 text, WebVector<WebCompositionUnderline>(underlines),
1360 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011361 // Setting the IME composition was successful. Send the new composition
1362 // range to the browser.
1363 ui::Range range(ui::Range::InvalidRange());
1364 size_t location, length;
1365 if (webwidget_->compositionRange(&location, &length)) {
1366 range.set_start(location);
1367 range.set_end(location + length);
1368 }
1369 // The IME was cancelled via the Esc key, so just send back the caret.
1370 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1371 range.set_start(location);
1372 range.set_end(location + length);
1373 }
[email protected]58b48a0d2012-06-13 07:01:351374 std::vector<gfx::Rect> character_bounds;
1375 GetCompositionCharacterBounds(&character_bounds);
1376 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011377 } else {
[email protected]fa7b1dc2010-06-23 17:53:041378 // If we failed to set the composition text, then we need to let the browser
1379 // process to cancel the input method's ongoing composition session, to make
1380 // sure we are in a consistent state.
1381 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011382
1383 // Send an updated IME range with just the caret range.
1384 ui::Range range(ui::Range::InvalidRange());
1385 size_t location, length;
1386 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1387 range.set_start(location);
1388 range.set_end(location + length);
1389 }
[email protected]58b48a0d2012-06-13 07:01:351390 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261391 }
[email protected]fa7b1dc2010-06-23 17:53:041392}
1393
[email protected]4de6d1692011-10-12 08:45:441394void RenderWidget::OnImeConfirmComposition(
1395 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041396 if (!webwidget_)
1397 return;
1398
1399 handling_input_event_ = true;
1400 webwidget_->confirmComposition(text);
1401 handling_input_event_ = false;
1402
[email protected]d4cff272011-05-02 15:46:011403 // Send an updated IME range with just the caret range.
1404 ui::Range range(ui::Range::InvalidRange());
1405 size_t location, length;
1406 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1407 range.set_start(location);
1408 range.set_end(location + length);
1409 }
[email protected]58b48a0d2012-06-13 07:01:351410 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
initial.commit09911bf2008-07-26 23:55:291411}
1412
[email protected]948f7ab72010-05-28 23:48:081413// This message causes the renderer to render an image of the
1414// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491415void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091416 int tag,
[email protected]948f7ab72010-05-28 23:48:081417 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491418 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001419 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1420 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251421 // Close our unused handle.
1422#if defined(OS_WIN)
1423 ::CloseHandle(dib_handle);
1424#elif defined(OS_MACOSX)
1425 base::SharedMemory::CloseHandle(dib_handle);
1426#endif
1427 }
[email protected]d65adb12010-04-28 17:26:491428 return;
[email protected]45c6aad32010-11-11 04:46:251429 }
[email protected]d65adb12010-04-28 17:26:491430
[email protected]948f7ab72010-05-28 23:48:081431 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491432 // If one of these is empty, then we just return the dib we were
1433 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091434 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491435 return;
1436 }
1437
1438 // Map the given DIB ID into this process, and unmap it at the end
1439 // of this function.
[email protected]45c6aad32010-11-11 04:46:251440 scoped_ptr<TransportDIB> paint_at_size_buffer(
1441 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301442
1443 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491444 float x_scale = static_cast<float>(desired_size.width()) /
1445 static_cast<float>(canvas_size.width());
1446 float y_scale = static_cast<float>(desired_size.height()) /
1447 static_cast<float>(canvas_size.height());
1448
[email protected]ee8d6fd2010-05-26 17:05:481449 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491450 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1451 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481452 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491453
[email protected]36808ad2010-10-20 19:18:301454 scoped_ptr<skia::PlatformCanvas> canvas(
1455 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1456 canvas_size.height()));
1457 if (!canvas.get()) {
1458 NOTREACHED();
1459 return;
1460 }
1461
[email protected]d65adb12010-04-28 17:26:491462 // Reset bounds to what we actually received, but they should be the
1463 // same.
1464 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1465 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1466 bounds.set_width(canvas->getDevice()->width());
1467 bounds.set_height(canvas->getDevice()->height());
1468
1469 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081470 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491471 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1472
[email protected]948f7ab72010-05-28 23:48:081473 // Have to make sure we're laid out at the right size before
1474 // rendering.
1475 gfx::Size old_size = webwidget_->size();
1476 webwidget_->resize(page_size);
1477 webwidget_->layout();
1478
[email protected]d65adb12010-04-28 17:26:491479 // Paint the entire thing (using original bounds, not scaled bounds).
1480 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1481 canvas->restore();
1482
[email protected]948f7ab72010-05-28 23:48:081483 // Return the widget to its previous size.
1484 webwidget_->resize(old_size);
1485
[email protected]c88c9442010-07-19 18:55:091486 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491487}
1488
[email protected]ec7dc112008-08-06 05:30:121489void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1490 // During shutdown we can just ignore this message.
1491 if (!webwidget_)
1492 return;
1493
1494 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561495 if (is_accelerated_compositing_active_) {
[email protected]8c49fa982012-02-10 14:37:041496 webwidget_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461497 scheduleComposite();
1498 } else {
1499 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1500 didInvalidateRect(repaint_rect);
1501 }
[email protected]ec7dc112008-08-06 05:30:121502}
1503
[email protected]fea38fc2012-06-13 17:38:371504void RenderWidget::OnSetDeviceScaleFactor(float device_scale_factor) {
[email protected]dea5e7682012-06-14 05:21:241505 if (device_scale_factor_ == device_scale_factor)
1506 return;
1507
1508 device_scale_factor_ = device_scale_factor;
1509
1510 if (!is_accelerated_compositing_active_) {
1511 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1512 } else {
1513 scheduleComposite();
1514 }
[email protected]fea38fc2012-06-13 17:38:371515}
1516
[email protected]4873c7d2009-07-16 06:36:281517void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111518 if (!webwidget_)
1519 return;
[email protected]4873c7d2009-07-16 06:36:281520 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111521}
1522
[email protected]6131a642012-06-15 23:26:531523void RenderWidget::OnScreenInfoChanged(
1524 const WebKit::WebScreenInfo& screen_info) {
1525 screen_info_ = screen_info;
1526}
1527
[email protected]719b36f2010-12-22 20:36:461528webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151529 const gfx::Rect& paint_bounds,
1530 TransportDIB** dib,
1531 gfx::Rect* location,
1532 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461533 // Bare RenderWidgets don't support optimized plugin painting.
1534 return NULL;
[email protected]ca4847f2010-09-24 05:39:151535}
1536
[email protected]bcaf2272011-02-15 15:29:431537gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521538 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431539 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521540}
1541
[email protected]bee16aab2009-08-26 15:55:031542void RenderWidget::SetHidden(bool hidden) {
1543 if (is_hidden_ == hidden)
1544 return;
1545
1546 // The status has changed. Tell the RenderThread about it.
1547 is_hidden_ = hidden;
1548 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271549 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031550 else
[email protected]380244092011-10-07 17:26:271551 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031552}
1553
[email protected]2b624c562011-10-27 22:58:261554void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261555 if (!webwidget_)
1556 return;
1557
1558 if (is_fullscreen_) {
1559 webwidget_->willExitFullScreen();
1560 } else {
1561 webwidget_->willEnterFullScreen();
1562 }
[email protected]2b624c562011-10-27 22:58:261563}
1564
1565void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261566 if (!webwidget_)
1567 return;
1568
1569 if (is_fullscreen_) {
1570 webwidget_->didEnterFullScreen();
1571 } else {
1572 webwidget_->didExitFullScreen();
1573 }
[email protected]2b624c562011-10-27 22:58:261574}
1575
[email protected]699ab0d2009-04-23 23:19:141576void RenderWidget::SetBackground(const SkBitmap& background) {
1577 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461578
[email protected]699ab0d2009-04-23 23:19:141579 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281580 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141581}
1582
[email protected]674741932009-02-04 23:44:461583bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051584 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461585}
1586
1587bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051588 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461589}
1590
1591void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051592 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461593}
1594
1595void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051596 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461597}
1598
1599void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051600 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461601}
1602
[email protected]e99ef6f2011-10-16 01:13:001603void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041604 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291605 return;
[email protected]fa7b1dc2010-06-23 17:53:041606
[email protected]ad26ef42011-06-17 07:59:451607 ui::TextInputType new_type = GetTextInputType();
1608 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001609 // Only sends text input type and compose inline to the browser process if
1610 // they are changed.
1611 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451612 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041613 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451614 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001615 Send(new ViewHostMsg_TextInputStateChanged(
1616 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291617 }
initial.commit09911bf2008-07-26 23:55:291618}
1619
[email protected]3f783362011-10-21 22:40:501620void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1621 WebRect start_webrect;
1622 WebRect end_webrect;
1623 webwidget_->selectionBounds(start_webrect, end_webrect);
1624 *start = start_webrect;
1625 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321626}
1627
[email protected]e99ef6f2011-10-16 01:13:001628void RenderWidget::UpdateSelectionBounds() {
1629 if (!webwidget_)
1630 return;
1631
[email protected]3f783362011-10-21 22:40:501632 gfx::Rect start_rect;
1633 gfx::Rect end_rect;
1634 GetSelectionBounds(&start_rect, &end_rect);
[email protected]58b48a0d2012-06-13 07:01:351635 if (selection_start_rect_ != start_rect || selection_end_rect_ != end_rect) {
1636 selection_start_rect_ = start_rect;
1637 selection_end_rect_ = end_rect;
1638 Send(new ViewHostMsg_SelectionBoundsChanged(
1639 routing_id_, selection_start_rect_, selection_end_rect_));
1640 }
[email protected]e99ef6f2011-10-16 01:13:001641
[email protected]58b48a0d2012-06-13 07:01:351642 std::vector<gfx::Rect> character_bounds;
1643 GetCompositionCharacterBounds(&character_bounds);
1644 UpdateCompositionInfo(composition_range_, character_bounds);
1645}
1646
1647bool RenderWidget::ShouldUpdateCompositionInfo(
1648 const ui::Range& range,
1649 const std::vector<gfx::Rect>& bounds) {
1650 if (composition_range_ != range)
1651 return true;
1652 if (bounds.size() != composition_character_bounds_.size())
1653 return true;
1654 for (size_t i = 0; i < bounds.size(); ++i) {
1655 if (bounds[i] != composition_character_bounds_[i])
1656 return true;
1657 }
1658 return false;
[email protected]e99ef6f2011-10-16 01:13:001659}
1660
[email protected]73bf95812011-10-12 11:38:321661// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451662COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1663 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1664COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1665 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1666COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1667 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181668COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1669 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1670COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1671 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1672COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1673 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1674COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1675 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1676COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1677 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001678COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1679 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1680COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1681 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1682COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1683 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1684COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1685 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1686COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1687 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1688COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1689 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]ad26ef42011-06-17 07:59:451690
1691ui::TextInputType RenderWidget::GetTextInputType() {
1692 if (webwidget_) {
1693 int type = webwidget_->textInputType();
1694 // Check the type is in the range representable by ui::TextInputType.
[email protected]feb8cf752012-06-08 04:48:001695 DCHECK_LE(type, ui::TEXT_INPUT_TYPE_WEEK) <<
[email protected]ad26ef42011-06-17 07:59:451696 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1697 return static_cast<ui::TextInputType>(type);
1698 }
1699 return ui::TEXT_INPUT_TYPE_NONE;
1700}
1701
[email protected]58b48a0d2012-06-13 07:01:351702void RenderWidget::GetCompositionCharacterBounds(
1703 std::vector<gfx::Rect>* bounds) {
1704 DCHECK(bounds);
1705 bounds->clear();
1706}
1707
[email protected]ad26ef42011-06-17 07:59:451708bool RenderWidget::CanComposeInline() {
1709 return true;
[email protected]56ea1a62011-05-30 07:05:571710}
1711
[email protected]4873c7d2009-07-16 06:36:281712WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:041713 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:281714}
1715
[email protected]f660d9c2012-06-06 18:31:211716float RenderWidget::deviceScaleFactor() {
1717 return device_scale_factor_;
1718}
1719
[email protected]fa7b1dc2010-06-23 17:53:041720void RenderWidget::resetInputMethod() {
1721 if (!input_method_is_active_)
1722 return;
1723
1724 // If the last text input type is not None, then we should finish any
1725 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451726 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041727 // If a composition text exists, then we need to let the browser process
1728 // to cancel the input method's ongoing composition session.
1729 if (webwidget_->confirmComposition())
1730 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1731 }
[email protected]d4cff272011-05-02 15:46:011732
1733 // Send an updated IME range with the current caret rect.
1734 ui::Range range(ui::Range::InvalidRange());
1735 size_t location, length;
1736 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1737 range.set_start(location);
1738 range.set_end(location + length);
1739 }
[email protected]58b48a0d2012-06-13 07:01:351740
1741 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:041742}
1743
[email protected]f103ab72009-09-02 17:10:591744void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501745 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291746 size_t i = 0;
1747 for (; i < plugin_window_moves_.size(); ++i) {
1748 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581749 if (move.rects_valid) {
1750 plugin_window_moves_[i] = move;
1751 } else {
1752 plugin_window_moves_[i].visible = move.visible;
1753 }
initial.commit09911bf2008-07-26 23:55:291754 break;
1755 }
1756 }
1757
1758 if (i == plugin_window_moves_.size())
1759 plugin_window_moves_.push_back(move);
1760}
[email protected]268654772009-08-06 23:02:041761
1762void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1763 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1764 i != plugin_window_moves_.end(); ++i) {
1765 if (i->window == window) {
1766 plugin_window_moves_.erase(i);
1767 break;
1768 }
1769 }
1770}
[email protected]67bfb83f2011-09-22 03:36:371771
[email protected]a39ca1652012-07-13 21:30:581772void RenderWidget::BeginSmoothScroll(bool down, bool scroll_far) {
1773 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, down, scroll_far));
1774}
1775
[email protected]67bfb83f2011-09-22 03:36:371776bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1777 return false;
1778}
[email protected]c3d45532011-10-07 19:20:401779
1780bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1781 return false;
1782}