blob: 406b53181c3ddd9c32fd442ff1de60a44ec3aa7c [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"
[email protected]a25e25b2012-09-28 14:32:3739#include "ui/gfx/rect_conversions.h"
[email protected]08397d52011-02-05 01:53:3840#include "ui/gfx/size.h"
[email protected]4b01b962012-10-09 23:17:3541#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4842#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2743#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4144#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3445#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5046#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4647#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4848
49#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4950#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5251#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4152#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4853#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4454
[email protected]8bd0fe62011-01-17 06:44:3755#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2956
[email protected]fa7b1dc2010-06-23 17:53:0457using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3058using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2259using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0660using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2861using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4462using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0063using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2864using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5965using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1866using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0167using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5268using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1969using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5270using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2871using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2472using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0473using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1874using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2775using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2276
[email protected]faec7b12012-06-19 14:42:1377static const float kStandardDPI = 160;
[email protected]f1cccb32012-06-06 18:29:5978
[email protected]6fd35b72012-03-01 19:46:4179RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:0480 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:4481 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:2982 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:5683 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:1184 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2985 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4286 host_window_(0),
[email protected]05a980d7a2012-02-07 22:16:4287 host_window_set_(false),
[email protected]b4d08452010-10-05 17:34:3588 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2989 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0690 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0591 update_reply_pending_(false),
[email protected]ea3ee0a2012-05-15 03:43:0992 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:2493 using_asynchronous_swapbuffers_(false),
94 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2995 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2996 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0997 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2998 needs_repainting_on_restore_(false),
99 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49100 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:48101 closing_(false),
[email protected]14392a52012-05-02 20:28:44102 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04103 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45104 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
105 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12106 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48107 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12108 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50109 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21110 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07111 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04112 screen_info_(screen_info),
[email protected]ce2b28e2012-08-09 15:53:57113 device_scale_factor_(1),
[email protected]0e241b4b2012-08-18 09:06:27114 throttle_input_events_(true),
115 next_smooth_scroll_gesture_id_(0) {
[email protected]8b3f0eb2012-05-03 19:15:05116 if (!swapped_out)
117 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27118 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18119 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
120 switches::kDisableGpuVsync);
[email protected]424820962012-06-08 15:33:19121#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
[email protected]faec7b12012-06-19 14:42:13122 device_scale_factor_ = screen_info.verticalDPI / kStandardDPI;
123 // Unless an explicit scale factor was provided for testing, ensure the scale
124 // is integral.
125 if (!CommandLine::ForCurrentProcess()->HasSwitch(
126 switches::kForceDeviceScaleFactor))
127 device_scale_factor_ = static_cast<int>(device_scale_factor_);
128 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
[email protected]f1cccb32012-06-06 18:29:59129#endif
initial.commit09911bf2008-07-26 23:55:29130}
131
132RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11133 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21134 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35135 if (current_paint_buf_) {
136 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
137 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29138 }
[email protected]992db4c2011-05-12 15:37:15139 // If we are swapped out, we have released already.
140 if (!is_swapped_out_)
141 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29142}
143
[email protected]484955942010-08-19 16:13:18144// static
[email protected]8085dbc82008-09-26 22:53:44145RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04146 WebKit::WebPopupType popup_type,
147 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29148 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41149 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04150 new RenderWidget(popup_type, screen_info, false));
initial.commit09911bf2008-07-26 23:55:29151 widget->Init(opener_id); // adds reference
152 return widget;
153}
154
[email protected]484955942010-08-19 16:13:18155// static
156WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
157 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03158 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18159 break;
160 case WebKit::WebPopupTypeSelect:
161 case WebKit::WebPopupTypeSuggestion:
162 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44163 case WebKit::WebPopupTypePage:
164 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03165 case WebKit::WebPopupTypeHelperPlugin:
166 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18167 default:
168 NOTREACHED();
169 }
170 return NULL;
171}
172
initial.commit09911bf2008-07-26 23:55:29173void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18174 DoInit(opener_id,
175 RenderWidget::CreateWebWidget(this),
[email protected]9f4f3322012-01-18 22:29:56176 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
177 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18178}
179
[email protected]484955942010-08-19 16:13:18180void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06181 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18182 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29183 DCHECK(!webwidget_);
184
185 if (opener_id != MSG_ROUTING_NONE)
186 opener_id_ = opener_id;
187
[email protected]484955942010-08-19 16:13:18188 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29189
[email protected]380244092011-10-07 17:26:27190 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29191 if (result) {
[email protected]380244092011-10-07 17:26:27192 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29193 // Take a reference on behalf of the RenderThread. This will be balanced
194 // when we receive ViewMsg_Close.
195 AddRef();
196 } else {
197 DCHECK(false);
198 }
199}
200
201// This is used to complete pending inits and non-pending inits. For non-
202// pending cases, the parent will be the same as the current parent. This
203// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55204void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29205 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29206
207 host_window_ = parent_hwnd;
[email protected]05a980d7a2012-02-07 22:16:42208 host_window_set_ = true;
209
[email protected]f1d5de42012-04-20 01:47:14210#if WEBWIDGET_HAS_SETCOMPOSITORSURFACEREADY
211 if (webwidget_)
212 webwidget_->setCompositorSurfaceReady();
213#endif
[email protected]05a980d7a2012-02-07 22:16:42214 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29215
[email protected]6de74452009-02-25 18:04:59216 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29217}
218
[email protected]992db4c2011-05-12 15:37:15219void RenderWidget::SetSwappedOut(bool is_swapped_out) {
220 // We should only toggle between states.
221 DCHECK(is_swapped_out_ != is_swapped_out);
222 is_swapped_out_ = is_swapped_out;
223
224 // If we are swapping out, we will call ReleaseProcess, allowing the process
225 // to exit if all of its RenderViews are swapped out. We wait until the
226 // WasSwappedOut call to do this, to avoid showing the sad tab.
227 // If we are swapping in, we call AddRefProcess to prevent the process from
228 // exiting.
229 if (!is_swapped_out)
230 RenderProcess::current()->AddRefProcess();
231}
232
[email protected]a95986a82010-12-24 06:19:28233bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
234 bool handled = true;
235 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
236 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
237 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
238 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54239 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28240 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41241 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15242 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28243 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]a7266a92012-06-28 02:11:08244 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, OnSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28245 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
246 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
247 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
248 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
249 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
250 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
251 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
252 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
[email protected]0e241b4b2012-08-18 09:06:27253 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted,
254 OnMsgSmoothScrollCompleted)
[email protected]fea38fc2012-06-13 17:38:37255 IPC_MESSAGE_HANDLER(ViewMsg_SetDeviceScaleFactor, OnSetDeviceScaleFactor)
[email protected]a95986a82010-12-24 06:19:28256 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
257 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53258 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]a95986a82010-12-24 06:19:28259 IPC_MESSAGE_UNHANDLED(handled = false)
260 IPC_END_MESSAGE_MAP()
261 return handled;
262}
initial.commit09911bf2008-07-26 23:55:29263
264bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15265 // Don't send any messages after the browser has told us to close, and filter
266 // most outgoing messages while swapped out.
267 if ((is_swapped_out_ &&
268 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11269 closing_) {
initial.commit09911bf2008-07-26 23:55:29270 delete message;
271 return false;
272 }
273
274 // If given a messsage without a routing ID, then assign our routing ID.
275 if (message->routing_id() == MSG_ROUTING_NONE)
276 message->set_routing_id(routing_id_);
277
[email protected]380244092011-10-07 17:26:27278 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44279}
280
[email protected]61e2b3cc2012-03-02 16:13:34281void RenderWidget::Resize(const gfx::Size& new_size,
282 const gfx::Rect& resizer_rect,
283 bool is_fullscreen,
284 ResizeAck resize_ack) {
285 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
286 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
287 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29288
[email protected]61e2b3cc2012-03-02 16:13:34289 // Ignore this during shutdown.
290 if (!webwidget_)
291 return;
292
293 // Remember the rect where the resize corner will be drawn.
294 resizer_rect_ = resizer_rect;
295
296 // NOTE: We may have entered fullscreen mode without changing our size.
297 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
298 if (fullscreen_change)
299 WillToggleFullscreen();
300 is_fullscreen_ = is_fullscreen;
301
302 if (size_ != new_size) {
303 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34304 needs_repainting_on_restore_ = false;
305
306 size_ = new_size;
307
308 paint_aggregator_.ClearPendingUpdate();
309
310 // When resizing, we want to wait to paint before ACK'ing the resize. This
311 // ensures that we only resize as fast as we can paint. We only need to
312 // send an ACK if we are resized to a non-empty rect.
313 webwidget_->resize(new_size);
314 if (!new_size.IsEmpty()) {
315 if (!is_accelerated_compositing_active_) {
316 // Resize should have caused an invalidation of the entire view.
317 DCHECK(paint_aggregator_.HasPendingUpdate());
318 }
319
320 // Send the Resize_ACK flag once we paint again if requested.
321 if (resize_ack == SEND_RESIZE_ACK)
322 set_next_paint_is_resize_ack();
323 }
[email protected]ff475a322012-03-14 00:05:35324 } else {
325 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34326 }
327
328 if (fullscreen_change)
329 DidToggleFullscreen();
330
331 // If a resize ack is requested and it isn't set-up, then no more resizes will
332 // come in and in general things will go wrong.
333 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
334 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29335}
336
337void RenderWidget::OnClose() {
338 if (closing_)
339 return;
340 closing_ = true;
341
342 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03343 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27344 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03345 SetHidden(false);
346 }
initial.commit09911bf2008-07-26 23:55:29347
initial.commit09911bf2008-07-26 23:55:29348 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25349 // now. Post a task that only gets invoked when there are no nested message
350 // loops.
[email protected]32876ae2011-11-15 22:25:21351 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29352 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25353
354 // Balances the AddRef taken when we called AddRoute.
355 Release();
initial.commit09911bf2008-07-26 23:55:29356}
357
[email protected]61e2b3cc2012-03-02 16:13:34358// Got a response from the browser after the renderer decided to create a new
359// view.
360void RenderWidget::OnCreatingNewAck(
361 gfx::NativeViewId parent) {
362 DCHECK(routing_id_ != MSG_ROUTING_NONE);
363
364 CompleteInit(parent);
365}
366
[email protected]f21c613a2009-02-12 14:46:17367void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09368 const gfx::Rect& resizer_rect,
369 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34370 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29371}
372
[email protected]b5913d72012-02-07 22:26:54373void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
374 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59375 gfx::Rect view_rect(size_);
376
377 gfx::Rect old_damage_rect = view_rect.Intersect(resizer_rect_);
378 if (!old_damage_rect.IsEmpty())
379 paint_aggregator_.InvalidateRect(old_damage_rect);
380
381 gfx::Rect new_damage_rect = view_rect.Intersect(resizer_rect);
382 if (!new_damage_rect.IsEmpty())
383 paint_aggregator_.InvalidateRect(new_damage_rect);
384
[email protected]b5913d72012-02-07 22:26:54385 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59386
[email protected]b5913d72012-02-07 22:26:54387 if (webwidget_)
388 webwidget_->didChangeWindowResizerRect();
389 }
390}
391
initial.commit09911bf2008-07-26 23:55:29392void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31393 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29394 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03395 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29396}
397
[email protected]9e2e4632012-07-27 16:38:41398void RenderWidget::OnWasShown(bool needs_repainting) {
399 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29400 // During shutdown we can just ignore this message.
401 if (!webwidget_)
402 return;
403
404 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03405 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29406
407 if (!needs_repainting && !needs_repainting_on_restore_)
408 return;
409 needs_repainting_on_restore_ = false;
410
[email protected]d65adb12010-04-28 17:26:49411 // Tag the next paint as a restore ack, which is picked up by
412 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29413 set_next_paint_is_restore_ack();
414
415 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56416 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46417 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
418 } else {
419 scheduleComposite();
420 }
initial.commit09911bf2008-07-26 23:55:29421}
422
[email protected]992db4c2011-05-12 15:37:15423void RenderWidget::OnWasSwappedOut() {
424 // If we have been swapped out and no one else is using this process,
425 // it's safe to exit now. If we get swapped back in, we will call
426 // AddRefProcess in SetSwappedOut.
427 if (is_swapped_out_)
428 RenderProcess::current()->ReleaseProcess();
429}
430
[email protected]53d3f302009-12-21 04:42:05431void RenderWidget::OnRequestMoveAck() {
432 DCHECK(pending_window_rect_count_);
433 pending_window_rect_count_--;
434}
435
436void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58437 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21438 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05439 update_reply_pending_ = false;
440
[email protected]b4d08452010-10-05 17:34:35441 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
442 // have no current paint buffer.
443 if (current_paint_buf_) {
444 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
445 current_paint_buf_ = NULL;
446 }
447
[email protected]65225772011-05-12 21:10:24448 // If swapbuffers is still pending, then defer the update until the
449 // swapbuffers occurs.
450 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
451 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
452 return;
453 }
454
[email protected]29ed96a2012-02-04 18:12:16455 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18456 if (!is_accelerated_compositing_active_) {
457 DidFlushPaint();
458 }
[email protected]a2f6bc112009-06-27 16:27:25459
initial.commit09911bf2008-07-26 23:55:29460 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24461 DoDeferredUpdateAndSendInputAck();
462}
463
[email protected]d0be63772011-12-20 23:18:04464bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24465 return false;
466}
467
[email protected]d0be63772011-12-20 23:18:04468void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24469 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21470 while (!updates_pending_swap_.empty()) {
471 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
472 updates_pending_swap_.pop_front();
473 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
474 // compositing pass, hence doesn't require an UpdateRect message.
475 if (msg)
476 Send(msg);
477 }
[email protected]65225772011-05-12 21:10:24478 num_swapbuffers_complete_pending_ = 0;
479 using_asynchronous_swapbuffers_ = false;
480 // Schedule another frame so the compositor learns about it.
481 scheduleComposite();
482}
483
[email protected]37a6f302011-07-11 23:43:08484void RenderWidget::OnSwapBuffersPosted() {
485 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21486
487 if (using_asynchronous_swapbuffers_) {
488 ViewHostMsg_UpdateRect* msg = NULL;
489 // pending_update_params_ can be NULL if the swap doesn't correspond to an
490 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
491 // message.
492 if (pending_update_params_.get()) {
493 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
494 pending_update_params_.reset();
495 }
496 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08497 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21498 }
[email protected]37a6f302011-07-11 23:43:08499}
500
501void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24502 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16503
[email protected]404939f2012-06-01 04:06:18504 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16505 DidFlushPaint();
506
[email protected]65225772011-05-12 21:10:24507 // When compositing deactivates, we reset the swapbuffers pending count. The
508 // swapbuffers acks may still arrive, however.
509 if (num_swapbuffers_complete_pending_ == 0) {
510 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
511 return;
512 }
[email protected]aa4117f2011-12-09 22:19:21513 DCHECK(!updates_pending_swap_.empty());
514 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
515 updates_pending_swap_.pop_front();
516 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
517 // compositing pass, hence doesn't require an UpdateRect message.
518 if (msg)
519 Send(msg);
[email protected]65225772011-05-12 21:10:24520 num_swapbuffers_complete_pending_--;
521
522 // If update reply is still pending, then defer the update until that reply
523 // occurs.
[email protected]d0be63772011-12-20 23:18:04524 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24525 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
526 return;
527 }
528
529 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06530 // when we were previously rendering. However, if an invalidation task is not
531 // posted, there may be software rendering work pending. In that case, don't
532 // early out.
533 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24534 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
535 return;
536 }
537
[email protected]cc66e682012-10-02 06:48:18538 // Do not call DoDeferredUpdate unless there's animation work to be done or
539 // a real invalidation. This prevents rendering in response to a swapbuffers
540 // callback coming back after we've navigated away from the page that
541 // generated it.
542 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
543 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
544 return;
545 }
546
[email protected]65225772011-05-12 21:10:24547 // Continue painting if necessary...
548 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29549}
550
initial.commit09911bf2008-07-26 23:55:29551void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24552 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
[email protected]ce208f872012-03-07 20:42:56553 PickleIterator iter(message);
initial.commit09911bf2008-07-26 23:55:29554
555 const char* data;
556 int data_length;
[email protected]5dd768212009-08-13 23:34:49557 handling_input_event_ = true;
558 if (!message.ReadData(&iter, &data, &data_length)) {
559 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29560 return;
[email protected]5dd768212009-08-13 23:34:49561 }
initial.commit09911bf2008-07-26 23:55:29562
563 const WebInputEvent* input_event =
564 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48565
[email protected]b68a0e52011-12-08 15:11:12566 bool is_keyboard_shortcut = false;
567 // is_keyboard_shortcut flag is only available for RawKeyDown events.
568 if (input_event->type == WebInputEvent::RawKeyDown)
569 message.ReadBool(&iter, &is_keyboard_shortcut);
570
[email protected]67bfb83f2011-09-22 03:36:37571 bool prevent_default = false;
572 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26573 const WebMouseEvent& mouse_event =
574 *static_cast<const WebMouseEvent*>(input_event);
575 TRACE_EVENT2("renderer", "HandleMouseMove",
576 "x", mouse_event.x, "y", mouse_event.y);
577 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37578 }
579
580 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12581 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
582 suppress_next_char_events_ = false;
583 if (!processed && webwidget_)
584 processed = webwidget_->handleInputEvent(*input_event);
585 }
586
587 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
588 // it's not processed by webkit, then we need to suppress the upcoming Char
589 // events.
590 if (!processed && is_keyboard_shortcut)
591 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29592
[email protected]a9fb30aa2011-10-06 06:58:46593 IPC::Message* response =
594 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
595 processed);
[email protected]3391a0772012-03-28 00:32:07596 bool event_type_gets_rate_limited =
597 input_event->type == WebInputEvent::MouseMove ||
598 input_event->type == WebInputEvent::MouseWheel ||
599 WebInputEvent::isTouchEventType(input_event->type);
600 bool is_input_throttled =
[email protected]ce2b28e2012-08-09 15:53:57601 throttle_input_events_ &&
602 ((webwidget_ ? webwidget_->isInputThrottled() : false) ||
603 paint_aggregator_.HasPendingUpdate());
[email protected]e2824412009-02-27 01:57:05604
[email protected]f8868d72012-04-27 19:13:03605 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24606 // We want to rate limit the input events in this case, so we'll wait for
607 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17608 if (pending_input_event_ack_.get()) {
609 // As two different kinds of events could cause us to postpone an ack
610 // we send it now, if we have one pending. The Browser should never
611 // send us the same kind of event we are delaying the ack for.
612 Send(pending_input_event_ack_.release());
613 }
[email protected]12fbad812009-09-01 18:21:24614 pending_input_event_ack_.reset(response);
615 } else {
616 Send(response);
617 }
618
[email protected]3306f262012-09-21 19:20:42619#if defined(OS_ANDROID)
620 // Allow the IME to be shown when the focus changes as a consequence
621 // of a processed touch end event.
622 if (input_event->type == WebInputEvent::TouchEnd && processed)
623 UpdateTextInputState(SHOW_IME_IF_NEEDED);
624#endif
625
[email protected]5dd768212009-08-13 23:34:49626 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48627
[email protected]67bfb83f2011-09-22 03:36:37628 if (!prevent_default) {
629 if (WebInputEvent::isKeyboardEventType(input_event->type))
630 DidHandleKeyEvent();
631 if (WebInputEvent::isMouseEventType(input_event->type))
632 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24633 if (WebInputEvent::isTouchEventType(input_event->type))
634 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37635 }
initial.commit09911bf2008-07-26 23:55:29636}
637
638void RenderWidget::OnMouseCaptureLost() {
639 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28640 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29641}
642
643void RenderWidget::OnSetFocus(bool enable) {
644 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33645 if (webwidget_)
646 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29647}
648
649void RenderWidget::ClearFocus() {
650 // We may have got the focus from the browser before this gets processed, in
651 // which case we do not want to unfocus ourself.
652 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28653 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29654}
655
[email protected]2d5d09d52009-06-15 14:29:21656void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00657 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21658 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06659 TRACE_EVENT2("renderer", "PaintRect",
660 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00661 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21662
663 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00664 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
665 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03666
[email protected]699ab0d2009-04-23 23:19:14667 // If there is a custom background, tile it.
668 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14669 SkPaint paint;
670 SkShader* shader = SkShader::CreateBitmapShader(background_,
671 SkShader::kRepeat_TileMode,
672 SkShader::kRepeat_TileMode);
673 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20674
675 // Use kSrc_Mode to handle background_ transparency properly.
676 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
677
678 // Canvas could contain multiple update rects. Clip to given rect so that
679 // we don't accidentally clear other update rects.
680 canvas->save();
[email protected]1835b9e2012-02-28 13:12:48681 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14682 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20683 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14684 }
685
[email protected]719b36f2010-12-22 20:36:46686 // First see if this rect is a plugin that can paint itself faster.
687 TransportDIB* optimized_dib = NULL;
688 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20689 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46690 webkit::ppapi::PluginInstance* optimized_instance =
691 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
692 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20693 &optimized_copy_rect,
694 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46695 if (optimized_instance) {
696 // This plugin can be optimize-painted and we can just ask it to paint
697 // itself. We don't actually need the TransportDIB in this case.
698 //
699 // This is an optimization for PPAPI plugins that know they're on top of
700 // the page content. If this rect is inside such a plugin, we can save some
701 // time and avoid re-rendering the page content which we know will be
702 // covered by the plugin later (this time can be significant, especially
703 // for a playing movie that is invalidating a lot).
704 //
705 // In the plugin movie case, hopefully the similar call to
706 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
707 // painting, because that avoids copying the plugin image to a different
708 // paint rect. Unfortunately, if anything on the page is animating other
709 // than the movie, it break this optimization since the union of the
710 // invalid regions will be larger than the plugin.
711 //
712 // This code optimizes that case, where we can still avoid painting in
713 // WebKit and filling the background (which can be slow) and just painting
714 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
715 // required.
[email protected]fef5e3972012-08-07 03:59:47716 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]df59dd42012-09-14 22:56:30717 SkAutoCanvasRestore auto_restore(canvas, true);
718 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:46719 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27720 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:21721 canvas->restore();
[email protected]fef5e3972012-08-07 03:59:47722 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
723 if (!is_accelerated_compositing_active_)
724 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46725 } else {
726 // Normal painting case.
[email protected]fef5e3972012-08-07 03:59:47727 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]719b36f2010-12-22 20:36:46728 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]fef5e3972012-08-07 03:59:47729 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
730 if (!is_accelerated_compositing_active_)
731 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46732
733 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35734 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46735 }
initial.commit09911bf2008-07-26 23:55:29736
[email protected]4fb66842009-12-04 21:41:00737 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00738 canvas->restore();
739}
740
741void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
742 skia::PlatformCanvas* canvas) {
743 static bool kPaintBorder =
744 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
745 if (!kPaintBorder)
746 return;
747
[email protected]53d3f302009-12-21 04:42:05748 // Cycle through these colors to help distinguish new paint rects.
749 const SkColor colors[] = {
750 SkColorSetARGB(0x3F, 0xFF, 0, 0),
751 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
752 SkColorSetARGB(0x3F, 0, 0, 0xFF),
753 };
754 static int color_selector = 0;
755
[email protected]4fb66842009-12-04 21:41:00756 SkPaint paint;
757 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05758 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00759 paint.setStrokeWidth(1);
760
761 SkIRect irect;
762 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
763 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29764}
765
[email protected]52ccd0ea2011-02-16 01:09:05766void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30767 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30768 if (!animation_update_pending_) {
769 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59770 return;
[email protected]921244e42011-07-20 16:36:30771 }
[email protected]bd37ae252011-06-03 01:28:18772 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59773 // Record when we fired (according to base::Time::Now()) relative to when
774 // we posted the task to quantify how much the base::Time/base::TimeTicks
775 // skew is affecting animations.
776 base::TimeDelta animation_callback_delay = base::Time::Now() -
777 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
778 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
779 animation_callback_delay,
780 base::TimeDelta::FromMilliseconds(0),
781 base::TimeDelta::FromMilliseconds(30),
782 25);
783 }
[email protected]65225772011-05-12 21:10:24784 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24785}
786
[email protected]52ccd0ea2011-02-16 01:09:05787void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59788 if (!animation_update_pending_)
789 return;
[email protected]bd37ae252011-06-03 01:28:18790
791 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33792 base::TimeDelta animationInterval = IsRenderingVSynced() ?
793 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18794
[email protected]7c4329e2011-02-18 22:02:59795 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45796
797 // animation_floor_time_ is the earliest time that we should animate when
798 // using the dead reckoning software scheduler. If we're using swapbuffers
799 // complete callbacks to rate limit, we can ignore this floor.
800 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30801 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33802 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18803 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59804 // running animation callbacks so that if a callback requests another
805 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38806 animation_timer_.Stop();
807 animation_timer_.Start(FROM_HERE, animationInterval, this,
808 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59809 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30810 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59811 return;
[email protected]5f8b1022011-01-21 23:34:50812 }
[email protected]bd37ae252011-06-03 01:28:18813 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38814 if (!animation_timer_.IsRunning()) {
815 // This code uses base::Time::Now() to calculate the floor and next fire
816 // time because javascript's Date object uses base::Time::Now(). The
817 // message loop uses base::TimeTicks, which on windows can have a
818 // different granularity than base::Time.
819 // The upshot of all this is that this function might be called before
820 // base::Time::Now() has advanced past the animation_floor_time_. To
821 // avoid exposing this delay to javascript, we keep posting delayed
822 // tasks until base::Time::Now() has advanced far enough.
823 base::TimeDelta delay = animation_floor_time_ - now;
824 animation_timer_.Start(FROM_HERE, delay, this,
825 &RenderWidget::AnimationCallback);
826 }
[email protected]5f8b1022011-01-21 23:34:50827}
828
[email protected]bd37ae252011-06-03 01:28:18829bool RenderWidget::IsRenderingVSynced() {
830 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
831 // not caught by this check. This will lead to artificially low frame rates
832 // for people who force vsync off at a driver level and expect Chrome to speed
833 // up.
834 return !has_disable_gpu_vsync_switch_;
835}
836
[email protected]65225772011-05-12 21:10:24837void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06838 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24839 invalidation_task_posted_ = false;
840 DoDeferredUpdateAndSendInputAck();
841}
842
843void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05844 DoDeferredUpdate();
845
846 if (pending_input_event_ack_.get())
847 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21848}
849
[email protected]552e6002009-11-19 05:24:57850void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58851 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08852
[email protected]65225772011-05-12 21:10:24853 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29854 return;
[email protected]05a980d7a2012-02-07 22:16:42855
856 if (!host_window_set_) {
857 TRACE_EVENT0("renderer", "EarlyOut_NoHostWindow");
858 return;
859 }
[email protected]aa4117f2011-12-09 22:19:21860 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24861 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
862 return;
863 }
[email protected]9ca84622011-06-02 23:46:39864 if (is_accelerated_compositing_active_ &&
865 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24866 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
867 return;
868 }
initial.commit09911bf2008-07-26 23:55:29869
[email protected]552e6002009-11-19 05:24:57870 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29871 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57872 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29873 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24874 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29875 return;
876 }
877
[email protected]05a980d7a2012-02-07 22:16:42878 if (is_accelerated_compositing_active_)
879 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
880
[email protected]0fb93f52011-05-18 23:13:56881 // Tracking of frame rate jitter
882 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]541dcd52012-03-15 15:57:51883 webwidget_->instrumentBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:05884 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50885
[email protected]f98d7e3c2010-09-13 22:30:46886 // Layout may generate more invalidation. It may also enable the
887 // GPU acceleration, so make sure to run layout before we send the
888 // GpuRenderingActivated message.
889 webwidget_->layout();
890
[email protected]dcca3aa92012-02-17 23:03:37891 // The following two can result in further layout and possibly
892 // enable GPU acceleration so they need to be called before any painting
893 // is done.
[email protected]3306f262012-09-21 19:20:42894 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]dcca3aa92012-02-17 23:03:37895 UpdateSelectionBounds();
896
[email protected]5f8b1022011-01-21 23:34:50897 // Suppress painting if nothing is dirty. This has to be done after updating
898 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24899 if (!paint_aggregator_.HasPendingUpdate()) {
900 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]541dcd52012-03-15 15:57:51901 webwidget_->instrumentCancelFrame();
[email protected]5f8b1022011-01-21 23:34:50902 return;
[email protected]65225772011-05-12 21:10:24903 }
[email protected]5f8b1022011-01-21 23:34:50904
[email protected]872ae5b2011-05-26 20:20:50905 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56906 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04907 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56908 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
909 delay,
910 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41911 base::TimeDelta::FromMilliseconds(120),
912 60);
[email protected]d0be63772011-12-20 23:18:04913 } else {
[email protected]0fb93f52011-05-18 23:13:56914 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
915 delay,
916 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41917 base::TimeDelta::FromMilliseconds(120),
918 60);
[email protected]d0be63772011-12-20 23:18:04919 }
[email protected]872ae5b2011-05-26 20:20:50920
921 // Calculate filtered time per frame:
922 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
923 filtered_time_per_frame_ =
924 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56925 }
926 last_do_deferred_update_time_ = frame_begin_ticks;
927
[email protected]fef5e3972012-08-07 03:59:47928 if (!is_accelerated_compositing_active_) {
929 software_stats_.numAnimationFrames++;
930 software_stats_.numFramesSentToScreen++;
931 }
932
[email protected]552e6002009-11-19 05:24:57933 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29934 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30935 PaintAggregator::PendingUpdate update;
936 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29937
[email protected]53d3f302009-12-21 04:42:05938 gfx::Rect scroll_damage = update.GetScrollDamage();
939 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29940
[email protected]29ed96a2012-02-04 18:12:16941 // Notify derived classes that we're about to initiate a paint.
942 WillInitiatePaint();
943
[email protected]ca4847f2010-09-24 05:39:15944 // A plugin may be able to do an optimized paint. First check this, in which
945 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46946 // This optimization allows PPAPI plugins that declare themselves on top of
947 // the page (like a traditional windowed plugin) to be able to animate (think
948 // movie playing) without repeatedly re-painting the page underneath, or
949 // copying the plugin backing store (since we can send the plugin's backing
950 // store directly to the browser).
951 //
952 // This optimization only works when the entire invalid region is contained
953 // within the plugin. There is a related optimization in PaintRect for the
954 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15955 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15956 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20957 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:21958 DCHECK(!pending_update_params_.get());
959 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
960 pending_update_params_->dx = update.scroll_delta.x();
961 pending_update_params_->dy = update.scroll_delta.y();
962 pending_update_params_->scroll_rect = update.scroll_rect;
963 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:21964 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
965 pending_update_params_->flags = next_paint_flags_;
966 pending_update_params_->scroll_offset = GetScrollOffset();
967 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:09968 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:21969 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:09970 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:21971
[email protected]ca4847f2010-09-24 05:39:15972 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56973 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15974 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20975 &optimized_copy_rect,
976 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:27977 // Only update the part of the plugin that actually changed.
978 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21979 pending_update_params_->bitmap = dib->id();
980 pending_update_params_->bitmap_rect = optimized_copy_location;
981 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:20982 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:56983 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46984 // Compute a buffer for painting and cache it.
[email protected]a25e25b2012-09-28 14:32:37985 gfx::Rect pixel_bounds =
986 gfx::ToEnclosingRect(bounds.Scale(device_scale_factor_));
[email protected]ca4847f2010-09-24 05:39:15987 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35988 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:59989 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:46990 if (!canvas.get()) {
991 NOTREACHED();
992 return;
993 }
[email protected]cef3362f2009-12-21 17:48:45994
[email protected]f98d7e3c2010-09-13 22:30:46995 // We may get back a smaller canvas than we asked for.
996 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:59997 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
998 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
999 pixel_bounds.set_width(canvas->getDevice()->width());
1000 pixel_bounds.set_height(canvas->getDevice()->height());
1001 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1002 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051003
[email protected]f98d7e3c2010-09-13 22:30:461004 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1005
[email protected]aa4117f2011-12-09 22:19:211006 pending_update_params_->bitmap = current_paint_buf_->id();
1007 pending_update_params_->bitmap_rect = bounds;
1008
1009 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461010 // The scroll damage is just another rectangle to paint and copy.
1011 copy_rects.swap(update.paint_rects);
1012 if (!scroll_damage.IsEmpty())
1013 copy_rects.push_back(scroll_damage);
1014
1015 for (size_t i = 0; i < copy_rects.size(); ++i)
[email protected]f1cccb32012-06-06 18:29:591016 PaintRect(copy_rects[i], pixel_bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:351017
1018 // Software FPS tick for performance tests. The accelerated path traces the
1019 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1020 // NOTE: Tests may break if this event is renamed or moved.
1021 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:461022 } else { // Accelerated compositing path
1023 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211024 // If painting is done via the gpu process then we don't set any damage
1025 // rects to save the browser process from doing unecessary work.
1026 pending_update_params_->bitmap_rect = bounds;
1027 pending_update_params_->scroll_rect = gfx::Rect();
1028 // We don't need an ack, because we're not sharing a DIB with the browser.
1029 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1030 // with the browser for the GPU surface.
1031 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:421032 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:461033 }
1034
[email protected]936c6f52011-12-13 01:35:261035 // If we're holding a pending input event ACK, send the ACK before sending the
1036 // UpdateReply message so we can receive another input event before the
1037 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1038 // the UpdateRect IPC message handler.
1039 if (pending_input_event_ack_.get())
1040 Send(pending_input_event_ack_.release());
1041
[email protected]aa4117f2011-12-09 22:19:211042 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
1043 // OnSwapBuffersPosted), meaning a message has been added to the
1044 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1045 // the message now.
1046 if (pending_update_params_.get()) {
1047 // sending an ack to browser process that the paint is complete...
1048 update_reply_pending_ = pending_update_params_->needs_ack;
1049 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1050 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341051 }
[email protected]53d3f302009-12-21 04:42:051052
[email protected]29ed96a2012-02-04 18:12:161053 // If we're software rendering then we're done initiating the paint.
1054 if (!is_accelerated_compositing_active_)
1055 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291056}
1057
1058///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461059// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291060
[email protected]4873c7d2009-07-16 06:36:281061void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571062 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481063 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571064 gfx::Rect damaged_rect = view_rect.Intersect(rect);
1065 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291066 return;
1067
[email protected]552e6002009-11-19 05:24:571068 paint_aggregator_.InvalidateRect(damaged_rect);
1069
1070 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241071 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571072 return;
1073 if (!paint_aggregator_.HasPendingUpdate())
1074 return;
[email protected]aa4117f2011-12-09 22:19:211075 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241076 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1077 return;
1078
1079 // When GPU rendering, combine pending animations and invalidations into
1080 // a single update.
[email protected]816edc62012-03-17 01:27:221081 if (is_accelerated_compositing_active_ &&
1082 animation_update_pending_ &&
1083 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571084 return;
1085
1086 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291087 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1088 // on the call stack.
1089 // 2) Allows us to collect more damage rects before painting to help coalesce
1090 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241091 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211092 MessageLoop::current()->PostTask(
1093 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291094}
1095
[email protected]4873c7d2009-07-16 06:36:281096void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461097 // Drop scrolls on the floor when we are in compositing mode.
1098 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561099 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461100 return;
1101
[email protected]552e6002009-11-19 05:24:571102 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481103 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571104 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
1105 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291106 return;
1107
[email protected]552e6002009-11-19 05:24:571108 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
1109
1110 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241111 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571112 return;
1113 if (!paint_aggregator_.HasPendingUpdate())
1114 return;
[email protected]aa4117f2011-12-09 22:19:211115 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241116 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1117 return;
1118
1119 // When GPU rendering, combine pending animations and invalidations into
1120 // a single update.
[email protected]816edc62012-03-17 01:27:221121 if (is_accelerated_compositing_active_ &&
1122 animation_update_pending_ &&
1123 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571124 return;
1125
1126 // Perform updating asynchronously. This serves two purposes:
1127 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1128 // on the call stack.
1129 // 2) Allows us to collect more damage rects before painting to help coalesce
1130 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241131 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211132 MessageLoop::current()->PostTask(
1133 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291134}
1135
[email protected]244ac1892011-12-02 17:04:471136void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091137 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1138 size_ = new_size;
1139 need_update_rect_for_auto_resize_ = true;
1140 }
[email protected]244ac1892011-12-02 17:04:471141}
1142
[email protected]91acd1c2012-03-14 08:32:391143void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221144 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1145
[email protected]c63b4d42012-04-26 01:01:071146#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211147 if (!is_accelerated_compositing_active_) {
1148 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1149 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1150 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1151 // going to switch to accelerated compositing, the GPU process may need
1152 // round-trips to the browser's UI thread before finishing the frame,
1153 // causing deadlocks if we delay the UpdateRect until we receive the
1154 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071155 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1156 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211157 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1158 }
[email protected]c63b4d42012-04-26 01:01:071159#endif
[email protected]aa4117f2011-12-09 22:19:211160
[email protected]ea162f92011-10-04 23:08:221161 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421162 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241163 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221164}
1165
1166void RenderWidget::didDeactivateCompositor() {
1167 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1168
1169 is_accelerated_compositing_active_ = false;
1170 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1171 routing_id_, is_accelerated_compositing_active_));
1172
[email protected]ea162f92011-10-04 23:08:221173 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241174 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561175}
1176
[email protected]9cd43a62012-03-26 08:03:561177void RenderWidget::willBeginCompositorFrame() {
1178 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371179
1180 DCHECK(RenderThreadImpl::current()->compositor_thread());
1181
1182 // The following two can result in further layout and possibly
1183 // enable GPU acceleration so they need to be called before any painting
1184 // is done.
[email protected]3306f262012-09-21 19:20:421185 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]abe8b3a2012-03-28 21:19:371186 UpdateSelectionBounds();
1187
[email protected]9cd43a62012-03-26 08:03:561188 WillInitiatePaint();
1189}
1190
[email protected]3391a0772012-03-28 00:32:071191void RenderWidget::didBecomeReadyForAdditionalInput() {
1192 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1193 if (pending_input_event_ack_.get())
1194 Send(pending_input_event_ack_.release());
1195}
1196
[email protected]58264a32011-11-17 23:36:151197void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501198 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351199 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1200 // NOTE: Tests may break if this event is renamed or moved.
1201 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161202 // Notify subclasses that we initiated the paint operation.
1203 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151204}
1205
1206void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181207 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1208
1209 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561210 DidFlushPaint();
1211
[email protected]aa4117f2011-12-09 22:19:211212 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151213 return;
1214
[email protected]ea3ee0a2012-05-15 03:43:091215 if (!next_paint_flags_ &&
1216 !need_update_rect_for_auto_resize_ &&
1217 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151218 return;
[email protected]ea3ee0a2012-05-15 03:43:091219 }
[email protected]58264a32011-11-17 23:36:151220
1221 ViewHostMsg_UpdateRect_Params params;
1222 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151223 params.plugin_window_moves.swap(plugin_window_moves_);
1224 params.flags = next_paint_flags_;
1225 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121226 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091227 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151228
1229 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1230 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091231 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151232}
1233
[email protected]f98d7e3c2010-09-13 22:30:461234void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041235 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401236 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041237 } else {
[email protected]c3d45532011-10-07 19:20:401238 // TODO(nduca): replace with something a little less hacky. The reason this
1239 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1240 // contains a lot of host-renderer synchronization logic that is still
1241 // important for the accelerated compositing case. The option of simply
1242 // duplicating all that code is less desirable than "faking out" the
1243 // invalidation path using a magical damage rect.
1244 didInvalidateRect(WebRect(0, 0, 1, 1));
1245 }
[email protected]f98d7e3c2010-09-13 22:30:461246}
1247
[email protected]5f8b1022011-01-21 23:34:501248void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201249 if (animation_update_pending_)
1250 return;
1251
[email protected]921244e42011-07-20 16:36:301252 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201253 animation_update_pending_ = true;
1254 if (!animation_timer_.IsRunning()) {
1255 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1256 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211257 }
[email protected]5f8b1022011-01-21 23:34:501258}
1259
[email protected]4873c7d2009-07-16 06:36:281260void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301261 // TODO(darin): Eliminate this temporary.
1262 WebCursor cursor(cursor_info);
1263
initial.commit09911bf2008-07-26 23:55:291264 // Only send a SetCursor message if we need to make a change.
1265 if (!current_cursor_.IsEqual(cursor)) {
1266 current_cursor_ = cursor;
1267 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1268 }
1269}
1270
1271// We are supposed to get a single call to Show for a newly created RenderWidget
1272// that was created via RenderWidget::CreateWebView. So, we wait until this
1273// point to dispatch the ShowWidget message.
1274//
1275// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281276// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291277//
[email protected]4873c7d2009-07-16 06:36:281278void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291279 DCHECK(!did_show_) << "received extraneous Show call";
1280 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1281 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1282
[email protected]8de12d942010-11-17 20:42:441283 if (did_show_)
1284 return;
1285
1286 did_show_ = true;
1287 // NOTE: initial_pos_ may still have its default values at this point, but
1288 // that's okay. It'll be ignored if as_popup is false, or the browser
1289 // process will impose a default position otherwise.
1290 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1291 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291292}
1293
[email protected]4873c7d2009-07-16 06:36:281294void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291295}
1296
[email protected]4873c7d2009-07-16 06:36:281297void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291298}
1299
[email protected]2533ce12009-05-09 00:02:241300void RenderWidget::DoDeferredClose() {
1301 Send(new ViewHostMsg_Close(routing_id_));
1302}
1303
[email protected]4873c7d2009-07-16 06:36:281304void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321305 if (is_swapped_out_) {
1306 // This widget is currently swapped out, and the active widget is in a
1307 // different process. Have the browser route the close request to the
1308 // active widget instead, so that the correct unload handlers are run.
1309 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1310 return;
1311 }
1312
initial.commit09911bf2008-07-26 23:55:291313 // If a page calls window.close() twice, we'll end up here twice, but that's
1314 // OK. It is safe to send multiple Close messages.
1315
[email protected]2533ce12009-05-09 00:02:241316 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1317 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1318 // could be closed before the JS finishes executing. So instead, post a
1319 // message back to the message loop, which won't run until the JS is
1320 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211321 MessageLoop::current()->PostTask(
1322 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291323}
1324
1325void RenderWidget::Close() {
1326 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281327 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291328 webwidget_ = NULL;
1329 }
1330}
1331
[email protected]4873c7d2009-07-16 06:36:281332WebRect RenderWidget::windowRect() {
1333 if (pending_window_rect_count_)
1334 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241335
[email protected]2bcd5402012-09-25 22:25:591336#if defined(OS_ANDROID)
1337 // Short circuit of the sync RPC call.
1338 return gfx::Rect(size_);
1339#else
[email protected]b3f2b912009-04-09 16:18:521340 gfx::Rect rect;
1341 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281342 return rect;
[email protected]2bcd5402012-09-25 22:25:591343#endif
initial.commit09911bf2008-07-26 23:55:291344}
1345
[email protected]8a9d6ca32011-06-06 20:11:301346void RenderWidget::setToolTipText(const WebKit::WebString& text,
1347 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541348 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301349}
1350
[email protected]4873c7d2009-07-16 06:36:281351void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291352 if (did_show_) {
1353 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241354 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291355 } else {
1356 initial_pos_ = pos;
1357 }
1358}
1359
[email protected]2533ce12009-05-09 00:02:241360void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1361 pending_window_rect_ = rect;
1362 pending_window_rect_count_++;
1363}
1364
[email protected]4873c7d2009-07-16 06:36:281365WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241366 if (pending_window_rect_count_) {
1367 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1368 // the RootWindowRect is probably going to return wrong results since the
1369 // browser may not have processed the Move yet. There isn't really anything
1370 // good to do in this case, and it shouldn't happen - since this size is
1371 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281372 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241373 }
1374
[email protected]2bcd5402012-09-25 22:25:591375#if defined(OS_ANDROID)
1376 // Short circuit of the sync RPC call.
1377 return gfx::Rect(size_);
1378#else
[email protected]b3f2b912009-04-09 16:18:521379 gfx::Rect rect;
1380 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281381 return rect;
[email protected]2bcd5402012-09-25 22:25:591382#endif
[email protected]d4547452008-08-28 18:36:371383}
1384
[email protected]4873c7d2009-07-16 06:36:281385WebRect RenderWidget::windowResizerRect() {
1386 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191387}
1388
[email protected]fa7b1dc2010-06-23 17:53:041389void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031390 // To prevent this renderer process from sending unnecessary IPC messages to
1391 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041392 // only during the input method attached to the browser process is active.
1393 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291394}
1395
[email protected]58b48a0d2012-06-13 07:01:351396void RenderWidget::UpdateCompositionInfo(
1397 const ui::Range& range,
1398 const std::vector<gfx::Rect>& character_bounds) {
1399 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1400 return;
1401 composition_character_bounds_ = character_bounds;
1402 composition_range_ = range;
1403 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1404 routing_id(), composition_range_, composition_character_bounds_));
1405}
1406
[email protected]fa7b1dc2010-06-23 17:53:041407void RenderWidget::OnImeSetComposition(
1408 const string16& text,
1409 const std::vector<WebCompositionUnderline>& underlines,
1410 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281411 if (!webwidget_)
1412 return;
[email protected]d4cff272011-05-02 15:46:011413 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041414 text, WebVector<WebCompositionUnderline>(underlines),
1415 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011416 // Setting the IME composition was successful. Send the new composition
1417 // range to the browser.
1418 ui::Range range(ui::Range::InvalidRange());
1419 size_t location, length;
1420 if (webwidget_->compositionRange(&location, &length)) {
1421 range.set_start(location);
1422 range.set_end(location + length);
1423 }
1424 // The IME was cancelled via the Esc key, so just send back the caret.
1425 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1426 range.set_start(location);
1427 range.set_end(location + length);
1428 }
[email protected]58b48a0d2012-06-13 07:01:351429 std::vector<gfx::Rect> character_bounds;
1430 GetCompositionCharacterBounds(&character_bounds);
1431 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011432 } else {
[email protected]fa7b1dc2010-06-23 17:53:041433 // If we failed to set the composition text, then we need to let the browser
1434 // process to cancel the input method's ongoing composition session, to make
1435 // sure we are in a consistent state.
1436 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011437
1438 // Send an updated IME range with just the caret range.
1439 ui::Range range(ui::Range::InvalidRange());
1440 size_t location, length;
1441 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1442 range.set_start(location);
1443 range.set_end(location + length);
1444 }
[email protected]58b48a0d2012-06-13 07:01:351445 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261446 }
[email protected]fa7b1dc2010-06-23 17:53:041447}
1448
[email protected]4de6d1692011-10-12 08:45:441449void RenderWidget::OnImeConfirmComposition(
1450 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041451 if (!webwidget_)
1452 return;
1453
1454 handling_input_event_ = true;
1455 webwidget_->confirmComposition(text);
1456 handling_input_event_ = false;
1457
[email protected]d4cff272011-05-02 15:46:011458 // Send an updated IME range with just the caret range.
1459 ui::Range range(ui::Range::InvalidRange());
1460 size_t location, length;
1461 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1462 range.set_start(location);
1463 range.set_end(location + length);
1464 }
[email protected]58b48a0d2012-06-13 07:01:351465 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
initial.commit09911bf2008-07-26 23:55:291466}
1467
[email protected]948f7ab72010-05-28 23:48:081468// This message causes the renderer to render an image of the
1469// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491470void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091471 int tag,
[email protected]948f7ab72010-05-28 23:48:081472 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491473 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001474 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1475 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251476 // Close our unused handle.
1477#if defined(OS_WIN)
1478 ::CloseHandle(dib_handle);
1479#elif defined(OS_MACOSX)
1480 base::SharedMemory::CloseHandle(dib_handle);
1481#endif
1482 }
[email protected]d65adb12010-04-28 17:26:491483 return;
[email protected]45c6aad32010-11-11 04:46:251484 }
[email protected]d65adb12010-04-28 17:26:491485
[email protected]948f7ab72010-05-28 23:48:081486 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491487 // If one of these is empty, then we just return the dib we were
1488 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091489 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491490 return;
1491 }
1492
1493 // Map the given DIB ID into this process, and unmap it at the end
1494 // of this function.
[email protected]45c6aad32010-11-11 04:46:251495 scoped_ptr<TransportDIB> paint_at_size_buffer(
1496 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301497
[email protected]4b01b962012-10-09 23:17:351498 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
1499 page_size.Scale(device_scale_factor_));
1500 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
1501 desired_size.Scale(device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511502 gfx::Size canvas_size = page_size_in_pixel;
1503 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491504 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511505 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491506 static_cast<float>(canvas_size.height());
1507
[email protected]ee8d6fd2010-05-26 17:05:481508 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491509 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1510 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481511 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491512
[email protected]36808ad2010-10-20 19:18:301513 scoped_ptr<skia::PlatformCanvas> canvas(
1514 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1515 canvas_size.height()));
1516 if (!canvas.get()) {
1517 NOTREACHED();
1518 return;
1519 }
1520
[email protected]d65adb12010-04-28 17:26:491521 // Reset bounds to what we actually received, but they should be the
1522 // same.
1523 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1524 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1525 bounds.set_width(canvas->getDevice()->width());
1526 bounds.set_height(canvas->getDevice()->height());
1527
1528 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081529 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491530 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1531
[email protected]948f7ab72010-05-28 23:48:081532 // Have to make sure we're laid out at the right size before
1533 // rendering.
1534 gfx::Size old_size = webwidget_->size();
1535 webwidget_->resize(page_size);
1536 webwidget_->layout();
1537
[email protected]d65adb12010-04-28 17:26:491538 // Paint the entire thing (using original bounds, not scaled bounds).
1539 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1540 canvas->restore();
1541
[email protected]948f7ab72010-05-28 23:48:081542 // Return the widget to its previous size.
1543 webwidget_->resize(old_size);
1544
[email protected]c88c9442010-07-19 18:55:091545 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491546}
1547
[email protected]ec7dc112008-08-06 05:30:121548void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1549 // During shutdown we can just ignore this message.
1550 if (!webwidget_)
1551 return;
1552
1553 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561554 if (is_accelerated_compositing_active_) {
[email protected]8c49fa982012-02-10 14:37:041555 webwidget_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461556 scheduleComposite();
1557 } else {
1558 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1559 didInvalidateRect(repaint_rect);
1560 }
[email protected]ec7dc112008-08-06 05:30:121561}
1562
[email protected]fea38fc2012-06-13 17:38:371563void RenderWidget::OnSetDeviceScaleFactor(float device_scale_factor) {
[email protected]dea5e7682012-06-14 05:21:241564 if (device_scale_factor_ == device_scale_factor)
1565 return;
1566
1567 device_scale_factor_ = device_scale_factor;
1568
1569 if (!is_accelerated_compositing_active_) {
1570 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1571 } else {
1572 scheduleComposite();
1573 }
[email protected]fea38fc2012-06-13 17:38:371574}
1575
[email protected]0e241b4b2012-08-18 09:06:271576void RenderWidget::OnMsgSmoothScrollCompleted(int gesture_id) {
1577 PendingSmoothScrollGestureMap::iterator it =
1578 pending_smooth_scroll_gestures_.find(gesture_id);
1579 DCHECK(it != pending_smooth_scroll_gestures_.end());
1580 it->second.Run();
1581 pending_smooth_scroll_gestures_.erase(it);
1582}
1583
[email protected]4873c7d2009-07-16 06:36:281584void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111585 if (!webwidget_)
1586 return;
[email protected]4873c7d2009-07-16 06:36:281587 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111588}
1589
[email protected]6131a642012-06-15 23:26:531590void RenderWidget::OnScreenInfoChanged(
1591 const WebKit::WebScreenInfo& screen_info) {
1592 screen_info_ = screen_info;
1593}
1594
[email protected]719b36f2010-12-22 20:36:461595webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151596 const gfx::Rect& paint_bounds,
1597 TransportDIB** dib,
1598 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:201599 gfx::Rect* clip,
1600 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:461601 // Bare RenderWidgets don't support optimized plugin painting.
1602 return NULL;
[email protected]ca4847f2010-09-24 05:39:151603}
1604
[email protected]bcaf2272011-02-15 15:29:431605gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521606 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431607 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521608}
1609
[email protected]bee16aab2009-08-26 15:55:031610void RenderWidget::SetHidden(bool hidden) {
1611 if (is_hidden_ == hidden)
1612 return;
1613
1614 // The status has changed. Tell the RenderThread about it.
1615 is_hidden_ = hidden;
1616 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271617 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031618 else
[email protected]380244092011-10-07 17:26:271619 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031620}
1621
[email protected]2b624c562011-10-27 22:58:261622void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261623 if (!webwidget_)
1624 return;
1625
1626 if (is_fullscreen_) {
1627 webwidget_->willExitFullScreen();
1628 } else {
1629 webwidget_->willEnterFullScreen();
1630 }
[email protected]2b624c562011-10-27 22:58:261631}
1632
1633void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261634 if (!webwidget_)
1635 return;
1636
1637 if (is_fullscreen_) {
1638 webwidget_->didEnterFullScreen();
1639 } else {
1640 webwidget_->didExitFullScreen();
1641 }
[email protected]2b624c562011-10-27 22:58:261642}
1643
[email protected]699ab0d2009-04-23 23:19:141644void RenderWidget::SetBackground(const SkBitmap& background) {
1645 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461646
[email protected]699ab0d2009-04-23 23:19:141647 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281648 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141649}
1650
[email protected]674741932009-02-04 23:44:461651bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051652 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461653}
1654
1655bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051656 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461657}
1658
1659void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051660 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461661}
1662
1663void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051664 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461665}
1666
1667void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051668 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461669}
1670
[email protected]3306f262012-09-21 19:20:421671void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
1672 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
1673 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291674 return;
[email protected]ad26ef42011-06-17 07:59:451675 ui::TextInputType new_type = GetTextInputType();
[email protected]5b739cb2012-08-21 20:35:211676 WebKit::WebTextInputInfo new_info;
1677 if (webwidget_)
1678 new_info = webwidget_->textInputInfo();
1679
[email protected]ad26ef42011-06-17 07:59:451680 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:211681
[email protected]3306f262012-09-21 19:20:421682 // Only sends text input params if they are changed or if the ime should be
1683 // shown.
1684 if (show_ime_if_needed || (text_input_type_ != new_type
1685 || text_input_info_ != new_info
1686 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:211687 ViewHostMsg_TextInputState_Params p;
1688 p.type = new_type;
1689 p.value = new_info.value.utf8();
1690 p.selection_start = new_info.selectionStart;
1691 p.selection_end = new_info.selectionEnd;
1692 p.composition_start = new_info.compositionStart;
1693 p.composition_end = new_info.compositionEnd;
1694 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:421695 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:211696 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
1697
1698 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:041699 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451700 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:291701 }
initial.commit09911bf2008-07-26 23:55:291702}
1703
[email protected]3f783362011-10-21 22:40:501704void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1705 WebRect start_webrect;
1706 WebRect end_webrect;
1707 webwidget_->selectionBounds(start_webrect, end_webrect);
1708 *start = start_webrect;
1709 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321710}
1711
[email protected]e99ef6f2011-10-16 01:13:001712void RenderWidget::UpdateSelectionBounds() {
1713 if (!webwidget_)
1714 return;
1715
[email protected]3f783362011-10-21 22:40:501716 gfx::Rect start_rect;
1717 gfx::Rect end_rect;
1718 GetSelectionBounds(&start_rect, &end_rect);
[email protected]58b48a0d2012-06-13 07:01:351719 if (selection_start_rect_ != start_rect || selection_end_rect_ != end_rect) {
1720 selection_start_rect_ = start_rect;
1721 selection_end_rect_ = end_rect;
[email protected]b556c2e2012-08-10 22:18:271722 WebTextDirection start_dir = WebKit::WebTextDirectionLeftToRight;
1723 WebTextDirection end_dir = WebKit::WebTextDirectionLeftToRight;
1724 webwidget_->selectionTextDirection(start_dir, end_dir);
1725 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_,
1726 selection_start_rect_, start_dir, selection_end_rect_, end_dir));
[email protected]58b48a0d2012-06-13 07:01:351727 }
[email protected]e99ef6f2011-10-16 01:13:001728
[email protected]58b48a0d2012-06-13 07:01:351729 std::vector<gfx::Rect> character_bounds;
1730 GetCompositionCharacterBounds(&character_bounds);
1731 UpdateCompositionInfo(composition_range_, character_bounds);
1732}
1733
1734bool RenderWidget::ShouldUpdateCompositionInfo(
1735 const ui::Range& range,
1736 const std::vector<gfx::Rect>& bounds) {
1737 if (composition_range_ != range)
1738 return true;
1739 if (bounds.size() != composition_character_bounds_.size())
1740 return true;
1741 for (size_t i = 0; i < bounds.size(); ++i) {
1742 if (bounds[i] != composition_character_bounds_[i])
1743 return true;
1744 }
1745 return false;
[email protected]e99ef6f2011-10-16 01:13:001746}
1747
[email protected]73bf95812011-10-12 11:38:321748// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451749COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1750 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1751COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1752 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1753COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1754 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181755COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1756 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1757COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1758 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1759COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1760 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1761COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1762 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1763COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1764 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001765COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1766 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1767COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1768 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1769COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1770 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1771COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1772 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1773COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1774 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1775COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1776 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]ad26ef42011-06-17 07:59:451777
[email protected]5b739cb2012-08-21 20:35:211778ui::TextInputType RenderWidget::WebKitToUiTextInputType(
1779 WebKit::WebTextInputType type) {
1780 // Check the type is in the range representable by ui::TextInputType.
1781 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
1782 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1783 return static_cast<ui::TextInputType>(type);
1784}
1785
[email protected]ad26ef42011-06-17 07:59:451786ui::TextInputType RenderWidget::GetTextInputType() {
1787 if (webwidget_) {
[email protected]5b739cb2012-08-21 20:35:211788 WebKit::WebTextInputType type = webwidget_->textInputType();
1789 return WebKitToUiTextInputType(type);
[email protected]ad26ef42011-06-17 07:59:451790 }
1791 return ui::TEXT_INPUT_TYPE_NONE;
1792}
1793
[email protected]58b48a0d2012-06-13 07:01:351794void RenderWidget::GetCompositionCharacterBounds(
1795 std::vector<gfx::Rect>* bounds) {
1796 DCHECK(bounds);
1797 bounds->clear();
1798}
1799
[email protected]ad26ef42011-06-17 07:59:451800bool RenderWidget::CanComposeInline() {
1801 return true;
[email protected]56ea1a62011-05-30 07:05:571802}
1803
[email protected]4873c7d2009-07-16 06:36:281804WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:041805 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:281806}
1807
[email protected]f660d9c2012-06-06 18:31:211808float RenderWidget::deviceScaleFactor() {
1809 return device_scale_factor_;
1810}
1811
[email protected]fa7b1dc2010-06-23 17:53:041812void RenderWidget::resetInputMethod() {
1813 if (!input_method_is_active_)
1814 return;
1815
1816 // If the last text input type is not None, then we should finish any
1817 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451818 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041819 // If a composition text exists, then we need to let the browser process
1820 // to cancel the input method's ongoing composition session.
1821 if (webwidget_->confirmComposition())
1822 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1823 }
[email protected]d4cff272011-05-02 15:46:011824
1825 // Send an updated IME range with the current caret rect.
1826 ui::Range range(ui::Range::InvalidRange());
1827 size_t location, length;
1828 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1829 range.set_start(location);
1830 range.set_end(location + length);
1831 }
[email protected]58b48a0d2012-06-13 07:01:351832
1833 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:041834}
1835
[email protected]f103ab72009-09-02 17:10:591836void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501837 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291838 size_t i = 0;
1839 for (; i < plugin_window_moves_.size(); ++i) {
1840 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581841 if (move.rects_valid) {
1842 plugin_window_moves_[i] = move;
1843 } else {
1844 plugin_window_moves_[i].visible = move.visible;
1845 }
initial.commit09911bf2008-07-26 23:55:291846 break;
1847 }
1848 }
1849
1850 if (i == plugin_window_moves_.size())
1851 plugin_window_moves_.push_back(move);
1852}
[email protected]268654772009-08-06 23:02:041853
1854void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1855 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1856 i != plugin_window_moves_.end(); ++i) {
1857 if (i->window == window) {
1858 plugin_window_moves_.erase(i);
1859 break;
1860 }
1861 }
1862}
[email protected]67bfb83f2011-09-22 03:36:371863
[email protected]fef5e3972012-08-07 03:59:471864void RenderWidget::GetRenderingStats(WebKit::WebRenderingStats& stats) const {
1865 webwidget()->renderingStats(stats);
1866 stats.numAnimationFrames += software_stats_.numAnimationFrames;
1867 stats.numFramesSentToScreen += software_stats_.numFramesSentToScreen;
1868 stats.totalPaintTimeInSeconds += software_stats_.totalPaintTimeInSeconds;
1869}
1870
[email protected]63b465922012-09-06 02:04:521871bool RenderWidget::GetGpuRenderingStats(
1872 content::GpuRenderingStats* stats) const {
1873 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
1874 if (!gpu_channel)
1875 return false;
1876
1877 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
1878}
1879
[email protected]0e241b4b2012-08-18 09:06:271880void RenderWidget::BeginSmoothScroll(
1881 bool down,
1882 bool scroll_far,
[email protected]ebd8b562012-10-09 14:44:291883 const SmoothScrollCompletionCallback& callback,
1884 int mouse_event_x,
1885 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:271886 DCHECK(!callback.is_null());
1887 int id = next_smooth_scroll_gesture_id_++;
[email protected]ebd8b562012-10-09 14:44:291888 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, id, down, scroll_far,
1889 mouse_event_x, mouse_event_y));
[email protected]0e241b4b2012-08-18 09:06:271890 pending_smooth_scroll_gestures_.insert(std::make_pair(id, callback));
[email protected]a39ca1652012-07-13 21:30:581891}
1892
[email protected]67bfb83f2011-09-22 03:36:371893bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1894 return false;
1895}
[email protected]c3d45532011-10-07 19:20:401896
1897bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1898 return false;
1899}