blob: d14aea8f2cbd6789cba6164d6b7434f05d1a6e63 [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]479b0172012-10-29 19:27:0919#include "content/public/common/compositor_util.h"
[email protected]c08950d22011-10-13 22:20:2920#include "content/public/common/content_switches.h"
[email protected]8704f89b2011-04-15 00:30:0521#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4422#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1923#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1824#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4825#include "skia/ext/platform_canvas.h"
[email protected]8bd0fe62011-01-17 06:44:3726#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]4b1146bc2012-07-10 18:46:0327#include "third_party/WebKit/Source/WebKit/chromium/public/WebHelperPlugin.h"
[email protected]a7547fb2012-03-08 04:43:4428#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3729#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
30#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0131#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3732#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4133#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
34#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]e6e90dc2011-12-03 00:01:3735#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
36#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]d353541f2012-05-03 22:45:4137#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1338#include "ui/base/ui_base_switches.h"
[email protected]a25e25b2012-09-28 14:32:3739#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3540#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4841#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2742#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4143#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3444#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5045#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4646#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4847
48#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4949#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5250#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4151#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4852#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4453
[email protected]8bd0fe62011-01-17 06:44:3754#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2955
[email protected]fa7b1dc2010-06-23 17:53:0456using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3057using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2258using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0659using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2860using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4461using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0062using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2863using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5964using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1865using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0166using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5267using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1968using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5269using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2870using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2471using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0472using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1873using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2674
75namespace content {
[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]479b0172012-10-29 19:27:09210 if (webwidget_) {
211 webwidget_->setCompositorSurfaceReady();
212 if (IsThreadedCompositingEnabled())
213 webwidget_->enterForceCompositingMode(true);
214 }
[email protected]05a980d7a2012-02-07 22:16:42215 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29216
[email protected]6de74452009-02-25 18:04:59217 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29218}
219
[email protected]992db4c2011-05-12 15:37:15220void RenderWidget::SetSwappedOut(bool is_swapped_out) {
221 // We should only toggle between states.
222 DCHECK(is_swapped_out_ != is_swapped_out);
223 is_swapped_out_ = is_swapped_out;
224
225 // If we are swapping out, we will call ReleaseProcess, allowing the process
226 // to exit if all of its RenderViews are swapped out. We wait until the
227 // WasSwappedOut call to do this, to avoid showing the sad tab.
228 // If we are swapping in, we call AddRefProcess to prevent the process from
229 // exiting.
230 if (!is_swapped_out)
231 RenderProcess::current()->AddRefProcess();
232}
233
[email protected]a95986a82010-12-24 06:19:28234bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
235 bool handled = true;
236 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
237 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
238 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
239 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54240 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28241 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41242 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15243 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28244 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]a7266a92012-06-28 02:11:08245 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, OnSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28246 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
247 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
248 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
249 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
250 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
251 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
252 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
253 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
[email protected]0e241b4b2012-08-18 09:06:27254 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted,
255 OnMsgSmoothScrollCompleted)
[email protected]fea38fc2012-06-13 17:38:37256 IPC_MESSAGE_HANDLER(ViewMsg_SetDeviceScaleFactor, OnSetDeviceScaleFactor)
[email protected]a95986a82010-12-24 06:19:28257 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
258 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53259 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]a95986a82010-12-24 06:19:28260 IPC_MESSAGE_UNHANDLED(handled = false)
261 IPC_END_MESSAGE_MAP()
262 return handled;
263}
initial.commit09911bf2008-07-26 23:55:29264
265bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15266 // Don't send any messages after the browser has told us to close, and filter
267 // most outgoing messages while swapped out.
268 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26269 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11270 closing_) {
initial.commit09911bf2008-07-26 23:55:29271 delete message;
272 return false;
273 }
274
275 // If given a messsage without a routing ID, then assign our routing ID.
276 if (message->routing_id() == MSG_ROUTING_NONE)
277 message->set_routing_id(routing_id_);
278
[email protected]380244092011-10-07 17:26:27279 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44280}
281
[email protected]61e2b3cc2012-03-02 16:13:34282void RenderWidget::Resize(const gfx::Size& new_size,
283 const gfx::Rect& resizer_rect,
284 bool is_fullscreen,
285 ResizeAck resize_ack) {
286 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
287 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
288 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29289
[email protected]61e2b3cc2012-03-02 16:13:34290 // Ignore this during shutdown.
291 if (!webwidget_)
292 return;
293
294 // Remember the rect where the resize corner will be drawn.
295 resizer_rect_ = resizer_rect;
296
297 // NOTE: We may have entered fullscreen mode without changing our size.
298 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
299 if (fullscreen_change)
300 WillToggleFullscreen();
301 is_fullscreen_ = is_fullscreen;
302
303 if (size_ != new_size) {
304 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34305 needs_repainting_on_restore_ = false;
306
307 size_ = new_size;
308
309 paint_aggregator_.ClearPendingUpdate();
310
311 // When resizing, we want to wait to paint before ACK'ing the resize. This
312 // ensures that we only resize as fast as we can paint. We only need to
313 // send an ACK if we are resized to a non-empty rect.
314 webwidget_->resize(new_size);
315 if (!new_size.IsEmpty()) {
316 if (!is_accelerated_compositing_active_) {
317 // Resize should have caused an invalidation of the entire view.
318 DCHECK(paint_aggregator_.HasPendingUpdate());
319 }
320
321 // Send the Resize_ACK flag once we paint again if requested.
322 if (resize_ack == SEND_RESIZE_ACK)
323 set_next_paint_is_resize_ack();
324 }
[email protected]ff475a322012-03-14 00:05:35325 } else {
326 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34327 }
328
329 if (fullscreen_change)
330 DidToggleFullscreen();
331
332 // If a resize ack is requested and it isn't set-up, then no more resizes will
333 // come in and in general things will go wrong.
334 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
335 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29336}
337
338void RenderWidget::OnClose() {
339 if (closing_)
340 return;
341 closing_ = true;
342
343 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03344 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27345 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03346 SetHidden(false);
347 }
initial.commit09911bf2008-07-26 23:55:29348
initial.commit09911bf2008-07-26 23:55:29349 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25350 // now. Post a task that only gets invoked when there are no nested message
351 // loops.
[email protected]32876ae2011-11-15 22:25:21352 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29353 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25354
355 // Balances the AddRef taken when we called AddRoute.
356 Release();
initial.commit09911bf2008-07-26 23:55:29357}
358
[email protected]61e2b3cc2012-03-02 16:13:34359// Got a response from the browser after the renderer decided to create a new
360// view.
361void RenderWidget::OnCreatingNewAck(
362 gfx::NativeViewId parent) {
363 DCHECK(routing_id_ != MSG_ROUTING_NONE);
364
365 CompleteInit(parent);
366}
367
[email protected]f21c613a2009-02-12 14:46:17368void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09369 const gfx::Rect& resizer_rect,
370 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34371 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29372}
373
[email protected]b5913d72012-02-07 22:26:54374void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
375 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59376 gfx::Rect view_rect(size_);
377
[email protected]ce112fe2012-10-29 22:52:18378 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59379 if (!old_damage_rect.IsEmpty())
380 paint_aggregator_.InvalidateRect(old_damage_rect);
381
[email protected]ce112fe2012-10-29 22:52:18382 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59383 if (!new_damage_rect.IsEmpty())
384 paint_aggregator_.InvalidateRect(new_damage_rect);
385
[email protected]b5913d72012-02-07 22:26:54386 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59387
[email protected]b5913d72012-02-07 22:26:54388 if (webwidget_)
389 webwidget_->didChangeWindowResizerRect();
390 }
391}
392
initial.commit09911bf2008-07-26 23:55:29393void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31394 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29395 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03396 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29397}
398
[email protected]9e2e4632012-07-27 16:38:41399void RenderWidget::OnWasShown(bool needs_repainting) {
400 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29401 // During shutdown we can just ignore this message.
402 if (!webwidget_)
403 return;
404
405 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03406 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29407
408 if (!needs_repainting && !needs_repainting_on_restore_)
409 return;
410 needs_repainting_on_restore_ = false;
411
[email protected]d65adb12010-04-28 17:26:49412 // Tag the next paint as a restore ack, which is picked up by
413 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29414 set_next_paint_is_restore_ack();
415
416 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56417 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46418 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
419 } else {
420 scheduleComposite();
421 }
initial.commit09911bf2008-07-26 23:55:29422}
423
[email protected]992db4c2011-05-12 15:37:15424void RenderWidget::OnWasSwappedOut() {
425 // If we have been swapped out and no one else is using this process,
426 // it's safe to exit now. If we get swapped back in, we will call
427 // AddRefProcess in SetSwappedOut.
428 if (is_swapped_out_)
429 RenderProcess::current()->ReleaseProcess();
430}
431
[email protected]53d3f302009-12-21 04:42:05432void RenderWidget::OnRequestMoveAck() {
433 DCHECK(pending_window_rect_count_);
434 pending_window_rect_count_--;
435}
436
437void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58438 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21439 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05440 update_reply_pending_ = false;
441
[email protected]b4d08452010-10-05 17:34:35442 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
443 // have no current paint buffer.
444 if (current_paint_buf_) {
445 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
446 current_paint_buf_ = NULL;
447 }
448
[email protected]65225772011-05-12 21:10:24449 // If swapbuffers is still pending, then defer the update until the
450 // swapbuffers occurs.
451 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
452 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
453 return;
454 }
455
[email protected]29ed96a2012-02-04 18:12:16456 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18457 if (!is_accelerated_compositing_active_) {
458 DidFlushPaint();
459 }
[email protected]a2f6bc112009-06-27 16:27:25460
initial.commit09911bf2008-07-26 23:55:29461 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24462 DoDeferredUpdateAndSendInputAck();
463}
464
[email protected]d0be63772011-12-20 23:18:04465bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24466 return false;
467}
468
[email protected]479b0172012-10-29 19:27:09469bool RenderWidget::ForceCompositingModeEnabled() {
470 return false;
471}
472
[email protected]d0be63772011-12-20 23:18:04473void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24474 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21475 while (!updates_pending_swap_.empty()) {
476 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
477 updates_pending_swap_.pop_front();
478 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
479 // compositing pass, hence doesn't require an UpdateRect message.
480 if (msg)
481 Send(msg);
482 }
[email protected]65225772011-05-12 21:10:24483 num_swapbuffers_complete_pending_ = 0;
484 using_asynchronous_swapbuffers_ = false;
485 // Schedule another frame so the compositor learns about it.
486 scheduleComposite();
487}
488
[email protected]37a6f302011-07-11 23:43:08489void RenderWidget::OnSwapBuffersPosted() {
490 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21491
492 if (using_asynchronous_swapbuffers_) {
493 ViewHostMsg_UpdateRect* msg = NULL;
494 // pending_update_params_ can be NULL if the swap doesn't correspond to an
495 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
496 // message.
497 if (pending_update_params_.get()) {
498 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
499 pending_update_params_.reset();
500 }
501 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08502 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21503 }
[email protected]37a6f302011-07-11 23:43:08504}
505
506void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24507 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16508
[email protected]404939f2012-06-01 04:06:18509 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16510 DidFlushPaint();
511
[email protected]65225772011-05-12 21:10:24512 // When compositing deactivates, we reset the swapbuffers pending count. The
513 // swapbuffers acks may still arrive, however.
514 if (num_swapbuffers_complete_pending_ == 0) {
515 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
516 return;
517 }
[email protected]aa4117f2011-12-09 22:19:21518 DCHECK(!updates_pending_swap_.empty());
519 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
520 updates_pending_swap_.pop_front();
521 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
522 // compositing pass, hence doesn't require an UpdateRect message.
523 if (msg)
524 Send(msg);
[email protected]65225772011-05-12 21:10:24525 num_swapbuffers_complete_pending_--;
526
527 // If update reply is still pending, then defer the update until that reply
528 // occurs.
[email protected]d0be63772011-12-20 23:18:04529 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24530 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
531 return;
532 }
533
534 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06535 // when we were previously rendering. However, if an invalidation task is not
536 // posted, there may be software rendering work pending. In that case, don't
537 // early out.
538 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24539 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
540 return;
541 }
542
[email protected]cc66e682012-10-02 06:48:18543 // Do not call DoDeferredUpdate unless there's animation work to be done or
544 // a real invalidation. This prevents rendering in response to a swapbuffers
545 // callback coming back after we've navigated away from the page that
546 // generated it.
547 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
548 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
549 return;
550 }
551
[email protected]65225772011-05-12 21:10:24552 // Continue painting if necessary...
553 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29554}
555
initial.commit09911bf2008-07-26 23:55:29556void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24557 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
[email protected]ce208f872012-03-07 20:42:56558 PickleIterator iter(message);
initial.commit09911bf2008-07-26 23:55:29559
560 const char* data;
561 int data_length;
[email protected]5dd768212009-08-13 23:34:49562 handling_input_event_ = true;
563 if (!message.ReadData(&iter, &data, &data_length)) {
564 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29565 return;
[email protected]5dd768212009-08-13 23:34:49566 }
initial.commit09911bf2008-07-26 23:55:29567
568 const WebInputEvent* input_event =
569 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48570
[email protected]b68a0e52011-12-08 15:11:12571 bool is_keyboard_shortcut = false;
572 // is_keyboard_shortcut flag is only available for RawKeyDown events.
573 if (input_event->type == WebInputEvent::RawKeyDown)
574 message.ReadBool(&iter, &is_keyboard_shortcut);
575
[email protected]67bfb83f2011-09-22 03:36:37576 bool prevent_default = false;
577 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26578 const WebMouseEvent& mouse_event =
579 *static_cast<const WebMouseEvent*>(input_event);
580 TRACE_EVENT2("renderer", "HandleMouseMove",
581 "x", mouse_event.x, "y", mouse_event.y);
582 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37583 }
584
585 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12586 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
587 suppress_next_char_events_ = false;
588 if (!processed && webwidget_)
589 processed = webwidget_->handleInputEvent(*input_event);
590 }
591
592 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
593 // it's not processed by webkit, then we need to suppress the upcoming Char
594 // events.
595 if (!processed && is_keyboard_shortcut)
596 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29597
[email protected]a9fb30aa2011-10-06 06:58:46598 IPC::Message* response =
599 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
600 processed);
[email protected]3391a0772012-03-28 00:32:07601 bool event_type_gets_rate_limited =
602 input_event->type == WebInputEvent::MouseMove ||
603 input_event->type == WebInputEvent::MouseWheel ||
604 WebInputEvent::isTouchEventType(input_event->type);
605 bool is_input_throttled =
[email protected]ce2b28e2012-08-09 15:53:57606 throttle_input_events_ &&
607 ((webwidget_ ? webwidget_->isInputThrottled() : false) ||
608 paint_aggregator_.HasPendingUpdate());
[email protected]e2824412009-02-27 01:57:05609
[email protected]f8868d72012-04-27 19:13:03610 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24611 // We want to rate limit the input events in this case, so we'll wait for
612 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17613 if (pending_input_event_ack_.get()) {
614 // As two different kinds of events could cause us to postpone an ack
615 // we send it now, if we have one pending. The Browser should never
616 // send us the same kind of event we are delaying the ack for.
617 Send(pending_input_event_ack_.release());
618 }
[email protected]12fbad812009-09-01 18:21:24619 pending_input_event_ack_.reset(response);
620 } else {
621 Send(response);
622 }
623
[email protected]3306f262012-09-21 19:20:42624#if defined(OS_ANDROID)
625 // Allow the IME to be shown when the focus changes as a consequence
626 // of a processed touch end event.
627 if (input_event->type == WebInputEvent::TouchEnd && processed)
628 UpdateTextInputState(SHOW_IME_IF_NEEDED);
629#endif
630
[email protected]5dd768212009-08-13 23:34:49631 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48632
[email protected]67bfb83f2011-09-22 03:36:37633 if (!prevent_default) {
634 if (WebInputEvent::isKeyboardEventType(input_event->type))
635 DidHandleKeyEvent();
636 if (WebInputEvent::isMouseEventType(input_event->type))
637 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24638 if (WebInputEvent::isTouchEventType(input_event->type))
639 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37640 }
initial.commit09911bf2008-07-26 23:55:29641}
642
643void RenderWidget::OnMouseCaptureLost() {
644 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28645 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29646}
647
648void RenderWidget::OnSetFocus(bool enable) {
649 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33650 if (webwidget_)
651 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29652}
653
654void RenderWidget::ClearFocus() {
655 // We may have got the focus from the browser before this gets processed, in
656 // which case we do not want to unfocus ourself.
657 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28658 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29659}
660
[email protected]2d5d09d52009-06-15 14:29:21661void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00662 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21663 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06664 TRACE_EVENT2("renderer", "PaintRect",
665 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00666 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21667
668 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00669 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
670 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03671
[email protected]699ab0d2009-04-23 23:19:14672 // If there is a custom background, tile it.
673 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14674 SkPaint paint;
675 SkShader* shader = SkShader::CreateBitmapShader(background_,
676 SkShader::kRepeat_TileMode,
677 SkShader::kRepeat_TileMode);
678 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20679
680 // Use kSrc_Mode to handle background_ transparency properly.
681 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
682
683 // Canvas could contain multiple update rects. Clip to given rect so that
684 // we don't accidentally clear other update rects.
685 canvas->save();
[email protected]1835b9e2012-02-28 13:12:48686 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14687 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20688 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14689 }
690
[email protected]719b36f2010-12-22 20:36:46691 // First see if this rect is a plugin that can paint itself faster.
692 TransportDIB* optimized_dib = NULL;
693 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20694 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46695 webkit::ppapi::PluginInstance* optimized_instance =
696 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
697 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20698 &optimized_copy_rect,
699 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46700 if (optimized_instance) {
701 // This plugin can be optimize-painted and we can just ask it to paint
702 // itself. We don't actually need the TransportDIB in this case.
703 //
704 // This is an optimization for PPAPI plugins that know they're on top of
705 // the page content. If this rect is inside such a plugin, we can save some
706 // time and avoid re-rendering the page content which we know will be
707 // covered by the plugin later (this time can be significant, especially
708 // for a playing movie that is invalidating a lot).
709 //
710 // In the plugin movie case, hopefully the similar call to
711 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
712 // painting, because that avoids copying the plugin image to a different
713 // paint rect. Unfortunately, if anything on the page is animating other
714 // than the movie, it break this optimization since the union of the
715 // invalid regions will be larger than the plugin.
716 //
717 // This code optimizes that case, where we can still avoid painting in
718 // WebKit and filling the background (which can be slow) and just painting
719 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
720 // required.
[email protected]fef5e3972012-08-07 03:59:47721 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]df59dd42012-09-14 22:56:30722 SkAutoCanvasRestore auto_restore(canvas, true);
723 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:46724 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27725 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:21726 canvas->restore();
[email protected]fef5e3972012-08-07 03:59:47727 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
728 if (!is_accelerated_compositing_active_)
729 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46730 } else {
731 // Normal painting case.
[email protected]fef5e3972012-08-07 03:59:47732 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]719b36f2010-12-22 20:36:46733 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]fef5e3972012-08-07 03:59:47734 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
735 if (!is_accelerated_compositing_active_)
736 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46737
738 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35739 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46740 }
initial.commit09911bf2008-07-26 23:55:29741
[email protected]4fb66842009-12-04 21:41:00742 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00743 canvas->restore();
744}
745
746void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
747 skia::PlatformCanvas* canvas) {
748 static bool kPaintBorder =
749 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
750 if (!kPaintBorder)
751 return;
752
[email protected]53d3f302009-12-21 04:42:05753 // Cycle through these colors to help distinguish new paint rects.
754 const SkColor colors[] = {
755 SkColorSetARGB(0x3F, 0xFF, 0, 0),
756 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
757 SkColorSetARGB(0x3F, 0, 0, 0xFF),
758 };
759 static int color_selector = 0;
760
[email protected]4fb66842009-12-04 21:41:00761 SkPaint paint;
762 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05763 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00764 paint.setStrokeWidth(1);
765
766 SkIRect irect;
767 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
768 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29769}
770
[email protected]52ccd0ea2011-02-16 01:09:05771void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30772 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30773 if (!animation_update_pending_) {
774 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59775 return;
[email protected]921244e42011-07-20 16:36:30776 }
[email protected]bd37ae252011-06-03 01:28:18777 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59778 // Record when we fired (according to base::Time::Now()) relative to when
779 // we posted the task to quantify how much the base::Time/base::TimeTicks
780 // skew is affecting animations.
781 base::TimeDelta animation_callback_delay = base::Time::Now() -
782 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
783 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
784 animation_callback_delay,
785 base::TimeDelta::FromMilliseconds(0),
786 base::TimeDelta::FromMilliseconds(30),
787 25);
788 }
[email protected]65225772011-05-12 21:10:24789 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24790}
791
[email protected]52ccd0ea2011-02-16 01:09:05792void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59793 if (!animation_update_pending_)
794 return;
[email protected]bd37ae252011-06-03 01:28:18795
796 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33797 base::TimeDelta animationInterval = IsRenderingVSynced() ?
798 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18799
[email protected]7c4329e2011-02-18 22:02:59800 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45801
802 // animation_floor_time_ is the earliest time that we should animate when
803 // using the dead reckoning software scheduler. If we're using swapbuffers
804 // complete callbacks to rate limit, we can ignore this floor.
805 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30806 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33807 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18808 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59809 // running animation callbacks so that if a callback requests another
810 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38811 animation_timer_.Stop();
812 animation_timer_.Start(FROM_HERE, animationInterval, this,
813 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59814 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30815 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59816 return;
[email protected]5f8b1022011-01-21 23:34:50817 }
[email protected]bd37ae252011-06-03 01:28:18818 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38819 if (!animation_timer_.IsRunning()) {
820 // This code uses base::Time::Now() to calculate the floor and next fire
821 // time because javascript's Date object uses base::Time::Now(). The
822 // message loop uses base::TimeTicks, which on windows can have a
823 // different granularity than base::Time.
824 // The upshot of all this is that this function might be called before
825 // base::Time::Now() has advanced past the animation_floor_time_. To
826 // avoid exposing this delay to javascript, we keep posting delayed
827 // tasks until base::Time::Now() has advanced far enough.
828 base::TimeDelta delay = animation_floor_time_ - now;
829 animation_timer_.Start(FROM_HERE, delay, this,
830 &RenderWidget::AnimationCallback);
831 }
[email protected]5f8b1022011-01-21 23:34:50832}
833
[email protected]bd37ae252011-06-03 01:28:18834bool RenderWidget::IsRenderingVSynced() {
835 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
836 // not caught by this check. This will lead to artificially low frame rates
837 // for people who force vsync off at a driver level and expect Chrome to speed
838 // up.
839 return !has_disable_gpu_vsync_switch_;
840}
841
[email protected]65225772011-05-12 21:10:24842void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06843 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24844 invalidation_task_posted_ = false;
845 DoDeferredUpdateAndSendInputAck();
846}
847
848void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05849 DoDeferredUpdate();
850
851 if (pending_input_event_ack_.get())
852 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21853}
854
[email protected]552e6002009-11-19 05:24:57855void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58856 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08857
[email protected]65225772011-05-12 21:10:24858 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29859 return;
[email protected]05a980d7a2012-02-07 22:16:42860
861 if (!host_window_set_) {
862 TRACE_EVENT0("renderer", "EarlyOut_NoHostWindow");
863 return;
864 }
[email protected]aa4117f2011-12-09 22:19:21865 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24866 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
867 return;
868 }
[email protected]9ca84622011-06-02 23:46:39869 if (is_accelerated_compositing_active_ &&
870 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24871 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
872 return;
873 }
initial.commit09911bf2008-07-26 23:55:29874
[email protected]552e6002009-11-19 05:24:57875 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29876 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57877 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29878 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24879 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29880 return;
881 }
882
[email protected]05a980d7a2012-02-07 22:16:42883 if (is_accelerated_compositing_active_)
884 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
885
[email protected]0fb93f52011-05-18 23:13:56886 // Tracking of frame rate jitter
887 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]541dcd52012-03-15 15:57:51888 webwidget_->instrumentBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:05889 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50890
[email protected]f98d7e3c2010-09-13 22:30:46891 // Layout may generate more invalidation. It may also enable the
892 // GPU acceleration, so make sure to run layout before we send the
893 // GpuRenderingActivated message.
894 webwidget_->layout();
895
[email protected]dcca3aa92012-02-17 23:03:37896 // The following two can result in further layout and possibly
897 // enable GPU acceleration so they need to be called before any painting
898 // is done.
[email protected]3306f262012-09-21 19:20:42899 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]dcca3aa92012-02-17 23:03:37900 UpdateSelectionBounds();
901
[email protected]5f8b1022011-01-21 23:34:50902 // Suppress painting if nothing is dirty. This has to be done after updating
903 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24904 if (!paint_aggregator_.HasPendingUpdate()) {
905 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]541dcd52012-03-15 15:57:51906 webwidget_->instrumentCancelFrame();
[email protected]5f8b1022011-01-21 23:34:50907 return;
[email protected]65225772011-05-12 21:10:24908 }
[email protected]5f8b1022011-01-21 23:34:50909
[email protected]479b0172012-10-29 19:27:09910 if (!is_accelerated_compositing_active_ &&
911 !IsThreadedCompositingEnabled() &&
912 ForceCompositingModeEnabled()) {
913 webwidget_->enterForceCompositingMode(true);
914 }
915
[email protected]872ae5b2011-05-26 20:20:50916 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56917 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04918 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56919 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
920 delay,
921 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41922 base::TimeDelta::FromMilliseconds(120),
923 60);
[email protected]d0be63772011-12-20 23:18:04924 } else {
[email protected]0fb93f52011-05-18 23:13:56925 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
926 delay,
927 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41928 base::TimeDelta::FromMilliseconds(120),
929 60);
[email protected]d0be63772011-12-20 23:18:04930 }
[email protected]872ae5b2011-05-26 20:20:50931
932 // Calculate filtered time per frame:
933 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
934 filtered_time_per_frame_ =
935 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56936 }
937 last_do_deferred_update_time_ = frame_begin_ticks;
938
[email protected]fef5e3972012-08-07 03:59:47939 if (!is_accelerated_compositing_active_) {
940 software_stats_.numAnimationFrames++;
941 software_stats_.numFramesSentToScreen++;
942 }
943
[email protected]552e6002009-11-19 05:24:57944 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29945 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30946 PaintAggregator::PendingUpdate update;
947 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29948
[email protected]53d3f302009-12-21 04:42:05949 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:18950 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:29951
[email protected]29ed96a2012-02-04 18:12:16952 // Notify derived classes that we're about to initiate a paint.
953 WillInitiatePaint();
954
[email protected]ca4847f2010-09-24 05:39:15955 // A plugin may be able to do an optimized paint. First check this, in which
956 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46957 // This optimization allows PPAPI plugins that declare themselves on top of
958 // the page (like a traditional windowed plugin) to be able to animate (think
959 // movie playing) without repeatedly re-painting the page underneath, or
960 // copying the plugin backing store (since we can send the plugin's backing
961 // store directly to the browser).
962 //
963 // This optimization only works when the entire invalid region is contained
964 // within the plugin. There is a related optimization in PaintRect for the
965 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15966 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15967 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20968 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:21969 DCHECK(!pending_update_params_.get());
970 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
971 pending_update_params_->dx = update.scroll_delta.x();
972 pending_update_params_->dy = update.scroll_delta.y();
973 pending_update_params_->scroll_rect = update.scroll_rect;
974 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:21975 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
976 pending_update_params_->flags = next_paint_flags_;
977 pending_update_params_->scroll_offset = GetScrollOffset();
978 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:09979 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:21980 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:09981 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:21982
[email protected]ca4847f2010-09-24 05:39:15983 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56984 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15985 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20986 &optimized_copy_rect,
987 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:27988 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:47989 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21990 pending_update_params_->bitmap = dib->id();
991 pending_update_params_->bitmap_rect = optimized_copy_location;
992 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:20993 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:56994 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46995 // Compute a buffer for painting and cache it.
[email protected]ce112fe2012-10-29 22:52:18996 gfx::Rect pixel_bounds = gfx::ToFlooredRectDeprecated(
997 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]ca4847f2010-09-24 05:39:15998 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35999 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591000 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:461001 if (!canvas.get()) {
1002 NOTREACHED();
1003 return;
1004 }
[email protected]cef3362f2009-12-21 17:48:451005
[email protected]f98d7e3c2010-09-13 22:30:461006 // We may get back a smaller canvas than we asked for.
1007 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591008 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1009 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1010 pixel_bounds.set_width(canvas->getDevice()->width());
1011 pixel_bounds.set_height(canvas->getDevice()->height());
1012 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1013 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051014
[email protected]f98d7e3c2010-09-13 22:30:461015 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1016
[email protected]aa4117f2011-12-09 22:19:211017 pending_update_params_->bitmap = current_paint_buf_->id();
1018 pending_update_params_->bitmap_rect = bounds;
1019
1020 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461021 // The scroll damage is just another rectangle to paint and copy.
1022 copy_rects.swap(update.paint_rects);
1023 if (!scroll_damage.IsEmpty())
1024 copy_rects.push_back(scroll_damage);
1025
1026 for (size_t i = 0; i < copy_rects.size(); ++i)
[email protected]f1cccb32012-06-06 18:29:591027 PaintRect(copy_rects[i], pixel_bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:351028
1029 // Software FPS tick for performance tests. The accelerated path traces the
1030 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1031 // NOTE: Tests may break if this event is renamed or moved.
1032 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:461033 } else { // Accelerated compositing path
1034 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211035 // If painting is done via the gpu process then we don't set any damage
1036 // rects to save the browser process from doing unecessary work.
1037 pending_update_params_->bitmap_rect = bounds;
1038 pending_update_params_->scroll_rect = gfx::Rect();
1039 // We don't need an ack, because we're not sharing a DIB with the browser.
1040 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1041 // with the browser for the GPU surface.
1042 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:421043 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:461044 }
1045
[email protected]936c6f52011-12-13 01:35:261046 // If we're holding a pending input event ACK, send the ACK before sending the
1047 // UpdateReply message so we can receive another input event before the
1048 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1049 // the UpdateRect IPC message handler.
1050 if (pending_input_event_ack_.get())
1051 Send(pending_input_event_ack_.release());
1052
[email protected]aa4117f2011-12-09 22:19:211053 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
1054 // OnSwapBuffersPosted), meaning a message has been added to the
1055 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1056 // the message now.
1057 if (pending_update_params_.get()) {
1058 // sending an ack to browser process that the paint is complete...
1059 update_reply_pending_ = pending_update_params_->needs_ack;
1060 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1061 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341062 }
[email protected]53d3f302009-12-21 04:42:051063
[email protected]29ed96a2012-02-04 18:12:161064 // If we're software rendering then we're done initiating the paint.
1065 if (!is_accelerated_compositing_active_)
1066 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291067}
1068
1069///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461070// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291071
[email protected]4873c7d2009-07-16 06:36:281072void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]479b0172012-10-29 19:27:091073 TRACE_EVENT2("renderer", "RenderWidget::didInvalidateRect",
1074 "width", rect.width, "height", rect.height);
[email protected]552e6002009-11-19 05:24:571075 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481076 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181077 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571078 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291079 return;
1080
[email protected]552e6002009-11-19 05:24:571081 paint_aggregator_.InvalidateRect(damaged_rect);
1082
1083 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241084 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571085 return;
1086 if (!paint_aggregator_.HasPendingUpdate())
1087 return;
[email protected]aa4117f2011-12-09 22:19:211088 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241089 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1090 return;
1091
1092 // When GPU rendering, combine pending animations and invalidations into
1093 // a single update.
[email protected]816edc62012-03-17 01:27:221094 if (is_accelerated_compositing_active_ &&
1095 animation_update_pending_ &&
1096 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571097 return;
1098
1099 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291100 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1101 // on the call stack.
1102 // 2) Allows us to collect more damage rects before painting to help coalesce
1103 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241104 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211105 MessageLoop::current()->PostTask(
1106 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291107}
1108
[email protected]4873c7d2009-07-16 06:36:281109void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461110 // Drop scrolls on the floor when we are in compositing mode.
1111 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561112 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461113 return;
1114
[email protected]552e6002009-11-19 05:24:571115 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481116 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181117 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571118 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291119 return;
1120
[email protected]552e6002009-11-19 05:24:571121 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
1122
1123 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241124 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571125 return;
1126 if (!paint_aggregator_.HasPendingUpdate())
1127 return;
[email protected]aa4117f2011-12-09 22:19:211128 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241129 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1130 return;
1131
1132 // When GPU rendering, combine pending animations and invalidations into
1133 // a single update.
[email protected]816edc62012-03-17 01:27:221134 if (is_accelerated_compositing_active_ &&
1135 animation_update_pending_ &&
1136 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571137 return;
1138
1139 // Perform updating asynchronously. This serves two purposes:
1140 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1141 // on the call stack.
1142 // 2) Allows us to collect more damage rects before painting to help coalesce
1143 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241144 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211145 MessageLoop::current()->PostTask(
1146 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291147}
1148
[email protected]244ac1892011-12-02 17:04:471149void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091150 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1151 size_ = new_size;
1152 need_update_rect_for_auto_resize_ = true;
1153 }
[email protected]244ac1892011-12-02 17:04:471154}
1155
[email protected]91acd1c2012-03-14 08:32:391156void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221157 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1158
[email protected]c63b4d42012-04-26 01:01:071159#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211160 if (!is_accelerated_compositing_active_) {
1161 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1162 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1163 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1164 // going to switch to accelerated compositing, the GPU process may need
1165 // round-trips to the browser's UI thread before finishing the frame,
1166 // causing deadlocks if we delay the UpdateRect until we receive the
1167 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071168 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1169 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211170 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1171 }
[email protected]c63b4d42012-04-26 01:01:071172#endif
[email protected]aa4117f2011-12-09 22:19:211173
[email protected]ea162f92011-10-04 23:08:221174 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421175 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241176 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221177}
1178
1179void RenderWidget::didDeactivateCompositor() {
1180 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1181
1182 is_accelerated_compositing_active_ = false;
1183 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1184 routing_id_, is_accelerated_compositing_active_));
1185
[email protected]ea162f92011-10-04 23:08:221186 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241187 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091188
1189 // In single-threaded mode, we exit force compositing mode and re-enter in
1190 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1191 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1192 // entering force compositing mode at the appropriate times.
1193 if (!IsThreadedCompositingEnabled())
1194 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561195}
1196
[email protected]9cd43a62012-03-26 08:03:561197void RenderWidget::willBeginCompositorFrame() {
1198 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371199
1200 DCHECK(RenderThreadImpl::current()->compositor_thread());
1201
1202 // The following two can result in further layout and possibly
1203 // enable GPU acceleration so they need to be called before any painting
1204 // is done.
[email protected]3306f262012-09-21 19:20:421205 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]abe8b3a2012-03-28 21:19:371206 UpdateSelectionBounds();
1207
[email protected]9cd43a62012-03-26 08:03:561208 WillInitiatePaint();
1209}
1210
[email protected]3391a0772012-03-28 00:32:071211void RenderWidget::didBecomeReadyForAdditionalInput() {
1212 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1213 if (pending_input_event_ack_.get())
1214 Send(pending_input_event_ack_.release());
1215}
1216
[email protected]58264a32011-11-17 23:36:151217void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501218 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351219 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1220 // NOTE: Tests may break if this event is renamed or moved.
1221 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161222 // Notify subclasses that we initiated the paint operation.
1223 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151224}
1225
1226void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181227 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1228
1229 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561230 DidFlushPaint();
1231
[email protected]aa4117f2011-12-09 22:19:211232 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151233 return;
1234
[email protected]ea3ee0a2012-05-15 03:43:091235 if (!next_paint_flags_ &&
1236 !need_update_rect_for_auto_resize_ &&
1237 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151238 return;
[email protected]ea3ee0a2012-05-15 03:43:091239 }
[email protected]58264a32011-11-17 23:36:151240
1241 ViewHostMsg_UpdateRect_Params params;
1242 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151243 params.plugin_window_moves.swap(plugin_window_moves_);
1244 params.flags = next_paint_flags_;
1245 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121246 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091247 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151248
1249 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1250 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091251 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151252}
1253
[email protected]f98d7e3c2010-09-13 22:30:461254void RenderWidget::scheduleComposite() {
[email protected]479b0172012-10-29 19:27:091255 TRACE_EVENT0("gpu", "RenderWidget::scheduleComposite");
[email protected]d0be63772011-12-20 23:18:041256 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401257 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041258 } else {
[email protected]c3d45532011-10-07 19:20:401259 // TODO(nduca): replace with something a little less hacky. The reason this
1260 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1261 // contains a lot of host-renderer synchronization logic that is still
1262 // important for the accelerated compositing case. The option of simply
1263 // duplicating all that code is less desirable than "faking out" the
1264 // invalidation path using a magical damage rect.
1265 didInvalidateRect(WebRect(0, 0, 1, 1));
1266 }
[email protected]f98d7e3c2010-09-13 22:30:461267}
1268
[email protected]5f8b1022011-01-21 23:34:501269void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201270 if (animation_update_pending_)
1271 return;
1272
[email protected]921244e42011-07-20 16:36:301273 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201274 animation_update_pending_ = true;
1275 if (!animation_timer_.IsRunning()) {
1276 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1277 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211278 }
[email protected]5f8b1022011-01-21 23:34:501279}
1280
[email protected]4873c7d2009-07-16 06:36:281281void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301282 // TODO(darin): Eliminate this temporary.
1283 WebCursor cursor(cursor_info);
1284
initial.commit09911bf2008-07-26 23:55:291285 // Only send a SetCursor message if we need to make a change.
1286 if (!current_cursor_.IsEqual(cursor)) {
1287 current_cursor_ = cursor;
1288 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1289 }
1290}
1291
1292// We are supposed to get a single call to Show for a newly created RenderWidget
1293// that was created via RenderWidget::CreateWebView. So, we wait until this
1294// point to dispatch the ShowWidget message.
1295//
1296// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281297// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291298//
[email protected]4873c7d2009-07-16 06:36:281299void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291300 DCHECK(!did_show_) << "received extraneous Show call";
1301 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1302 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1303
[email protected]8de12d942010-11-17 20:42:441304 if (did_show_)
1305 return;
1306
1307 did_show_ = true;
1308 // NOTE: initial_pos_ may still have its default values at this point, but
1309 // that's okay. It'll be ignored if as_popup is false, or the browser
1310 // process will impose a default position otherwise.
1311 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1312 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291313}
1314
[email protected]4873c7d2009-07-16 06:36:281315void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291316}
1317
[email protected]4873c7d2009-07-16 06:36:281318void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291319}
1320
[email protected]2533ce12009-05-09 00:02:241321void RenderWidget::DoDeferredClose() {
1322 Send(new ViewHostMsg_Close(routing_id_));
1323}
1324
[email protected]4873c7d2009-07-16 06:36:281325void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321326 if (is_swapped_out_) {
1327 // This widget is currently swapped out, and the active widget is in a
1328 // different process. Have the browser route the close request to the
1329 // active widget instead, so that the correct unload handlers are run.
1330 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1331 return;
1332 }
1333
initial.commit09911bf2008-07-26 23:55:291334 // If a page calls window.close() twice, we'll end up here twice, but that's
1335 // OK. It is safe to send multiple Close messages.
1336
[email protected]2533ce12009-05-09 00:02:241337 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1338 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1339 // could be closed before the JS finishes executing. So instead, post a
1340 // message back to the message loop, which won't run until the JS is
1341 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211342 MessageLoop::current()->PostTask(
1343 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291344}
1345
1346void RenderWidget::Close() {
1347 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281348 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291349 webwidget_ = NULL;
1350 }
1351}
1352
[email protected]4873c7d2009-07-16 06:36:281353WebRect RenderWidget::windowRect() {
1354 if (pending_window_rect_count_)
1355 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241356
[email protected]2bcd5402012-09-25 22:25:591357#if defined(OS_ANDROID)
1358 // Short circuit of the sync RPC call.
1359 return gfx::Rect(size_);
1360#else
[email protected]b3f2b912009-04-09 16:18:521361 gfx::Rect rect;
1362 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281363 return rect;
[email protected]2bcd5402012-09-25 22:25:591364#endif
initial.commit09911bf2008-07-26 23:55:291365}
1366
[email protected]8a9d6ca32011-06-06 20:11:301367void RenderWidget::setToolTipText(const WebKit::WebString& text,
1368 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541369 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301370}
1371
[email protected]4873c7d2009-07-16 06:36:281372void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291373 if (did_show_) {
1374 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241375 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291376 } else {
1377 initial_pos_ = pos;
1378 }
1379}
1380
[email protected]2533ce12009-05-09 00:02:241381void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1382 pending_window_rect_ = rect;
1383 pending_window_rect_count_++;
1384}
1385
[email protected]4873c7d2009-07-16 06:36:281386WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241387 if (pending_window_rect_count_) {
1388 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1389 // the RootWindowRect is probably going to return wrong results since the
1390 // browser may not have processed the Move yet. There isn't really anything
1391 // good to do in this case, and it shouldn't happen - since this size is
1392 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281393 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241394 }
1395
[email protected]2bcd5402012-09-25 22:25:591396#if defined(OS_ANDROID)
1397 // Short circuit of the sync RPC call.
1398 return gfx::Rect(size_);
1399#else
[email protected]b3f2b912009-04-09 16:18:521400 gfx::Rect rect;
1401 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281402 return rect;
[email protected]2bcd5402012-09-25 22:25:591403#endif
[email protected]d4547452008-08-28 18:36:371404}
1405
[email protected]4873c7d2009-07-16 06:36:281406WebRect RenderWidget::windowResizerRect() {
1407 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191408}
1409
[email protected]fa7b1dc2010-06-23 17:53:041410void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031411 // To prevent this renderer process from sending unnecessary IPC messages to
1412 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041413 // only during the input method attached to the browser process is active.
1414 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291415}
1416
[email protected]58b48a0d2012-06-13 07:01:351417void RenderWidget::UpdateCompositionInfo(
1418 const ui::Range& range,
1419 const std::vector<gfx::Rect>& character_bounds) {
1420 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1421 return;
1422 composition_character_bounds_ = character_bounds;
1423 composition_range_ = range;
1424 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1425 routing_id(), composition_range_, composition_character_bounds_));
1426}
1427
[email protected]fa7b1dc2010-06-23 17:53:041428void RenderWidget::OnImeSetComposition(
1429 const string16& text,
1430 const std::vector<WebCompositionUnderline>& underlines,
1431 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281432 if (!webwidget_)
1433 return;
[email protected]d4cff272011-05-02 15:46:011434 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041435 text, WebVector<WebCompositionUnderline>(underlines),
1436 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011437 // Setting the IME composition was successful. Send the new composition
1438 // range to the browser.
1439 ui::Range range(ui::Range::InvalidRange());
1440 size_t location, length;
1441 if (webwidget_->compositionRange(&location, &length)) {
1442 range.set_start(location);
1443 range.set_end(location + length);
1444 }
1445 // The IME was cancelled via the Esc key, so just send back the caret.
1446 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1447 range.set_start(location);
1448 range.set_end(location + length);
1449 }
[email protected]58b48a0d2012-06-13 07:01:351450 std::vector<gfx::Rect> character_bounds;
1451 GetCompositionCharacterBounds(&character_bounds);
1452 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011453 } else {
[email protected]fa7b1dc2010-06-23 17:53:041454 // If we failed to set the composition text, then we need to let the browser
1455 // process to cancel the input method's ongoing composition session, to make
1456 // sure we are in a consistent state.
1457 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011458
1459 // Send an updated IME range with just the caret range.
1460 ui::Range range(ui::Range::InvalidRange());
1461 size_t location, length;
1462 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1463 range.set_start(location);
1464 range.set_end(location + length);
1465 }
[email protected]58b48a0d2012-06-13 07:01:351466 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261467 }
[email protected]fa7b1dc2010-06-23 17:53:041468}
1469
[email protected]4de6d1692011-10-12 08:45:441470void RenderWidget::OnImeConfirmComposition(
1471 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041472 if (!webwidget_)
1473 return;
1474
1475 handling_input_event_ = true;
1476 webwidget_->confirmComposition(text);
1477 handling_input_event_ = false;
1478
[email protected]d4cff272011-05-02 15:46:011479 // Send an updated IME range with just the caret range.
1480 ui::Range range(ui::Range::InvalidRange());
1481 size_t location, length;
1482 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1483 range.set_start(location);
1484 range.set_end(location + length);
1485 }
[email protected]58b48a0d2012-06-13 07:01:351486 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
initial.commit09911bf2008-07-26 23:55:291487}
1488
[email protected]948f7ab72010-05-28 23:48:081489// This message causes the renderer to render an image of the
1490// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491491void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091492 int tag,
[email protected]948f7ab72010-05-28 23:48:081493 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491494 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001495 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1496 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251497 // Close our unused handle.
1498#if defined(OS_WIN)
1499 ::CloseHandle(dib_handle);
1500#elif defined(OS_MACOSX)
1501 base::SharedMemory::CloseHandle(dib_handle);
1502#endif
1503 }
[email protected]d65adb12010-04-28 17:26:491504 return;
[email protected]45c6aad32010-11-11 04:46:251505 }
[email protected]d65adb12010-04-28 17:26:491506
[email protected]948f7ab72010-05-28 23:48:081507 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491508 // If one of these is empty, then we just return the dib we were
1509 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091510 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491511 return;
1512 }
1513
1514 // Map the given DIB ID into this process, and unmap it at the end
1515 // of this function.
[email protected]45c6aad32010-11-11 04:46:251516 scoped_ptr<TransportDIB> paint_at_size_buffer(
1517 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301518
[email protected]4b01b962012-10-09 23:17:351519 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
1520 page_size.Scale(device_scale_factor_));
1521 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
1522 desired_size.Scale(device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511523 gfx::Size canvas_size = page_size_in_pixel;
1524 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491525 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511526 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491527 static_cast<float>(canvas_size.height());
1528
[email protected]ee8d6fd2010-05-26 17:05:481529 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491530 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1531 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481532 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491533
[email protected]36808ad2010-10-20 19:18:301534 scoped_ptr<skia::PlatformCanvas> canvas(
1535 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1536 canvas_size.height()));
1537 if (!canvas.get()) {
1538 NOTREACHED();
1539 return;
1540 }
1541
[email protected]d65adb12010-04-28 17:26:491542 // Reset bounds to what we actually received, but they should be the
1543 // same.
1544 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1545 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1546 bounds.set_width(canvas->getDevice()->width());
1547 bounds.set_height(canvas->getDevice()->height());
1548
1549 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081550 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491551 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1552
[email protected]948f7ab72010-05-28 23:48:081553 // Have to make sure we're laid out at the right size before
1554 // rendering.
1555 gfx::Size old_size = webwidget_->size();
1556 webwidget_->resize(page_size);
1557 webwidget_->layout();
1558
[email protected]d65adb12010-04-28 17:26:491559 // Paint the entire thing (using original bounds, not scaled bounds).
1560 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1561 canvas->restore();
1562
[email protected]948f7ab72010-05-28 23:48:081563 // Return the widget to its previous size.
1564 webwidget_->resize(old_size);
1565
[email protected]c88c9442010-07-19 18:55:091566 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491567}
1568
[email protected]ec7dc112008-08-06 05:30:121569void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1570 // During shutdown we can just ignore this message.
1571 if (!webwidget_)
1572 return;
1573
1574 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561575 if (is_accelerated_compositing_active_) {
[email protected]8c49fa982012-02-10 14:37:041576 webwidget_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461577 scheduleComposite();
1578 } else {
1579 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1580 didInvalidateRect(repaint_rect);
1581 }
[email protected]ec7dc112008-08-06 05:30:121582}
1583
[email protected]fea38fc2012-06-13 17:38:371584void RenderWidget::OnSetDeviceScaleFactor(float device_scale_factor) {
[email protected]dea5e7682012-06-14 05:21:241585 if (device_scale_factor_ == device_scale_factor)
1586 return;
1587
1588 device_scale_factor_ = device_scale_factor;
1589
1590 if (!is_accelerated_compositing_active_) {
1591 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1592 } else {
1593 scheduleComposite();
1594 }
[email protected]fea38fc2012-06-13 17:38:371595}
1596
[email protected]0e241b4b2012-08-18 09:06:271597void RenderWidget::OnMsgSmoothScrollCompleted(int gesture_id) {
1598 PendingSmoothScrollGestureMap::iterator it =
1599 pending_smooth_scroll_gestures_.find(gesture_id);
1600 DCHECK(it != pending_smooth_scroll_gestures_.end());
1601 it->second.Run();
1602 pending_smooth_scroll_gestures_.erase(it);
1603}
1604
[email protected]4873c7d2009-07-16 06:36:281605void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111606 if (!webwidget_)
1607 return;
[email protected]4873c7d2009-07-16 06:36:281608 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111609}
1610
[email protected]6131a642012-06-15 23:26:531611void RenderWidget::OnScreenInfoChanged(
1612 const WebKit::WebScreenInfo& screen_info) {
1613 screen_info_ = screen_info;
1614}
1615
[email protected]719b36f2010-12-22 20:36:461616webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151617 const gfx::Rect& paint_bounds,
1618 TransportDIB** dib,
1619 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:201620 gfx::Rect* clip,
1621 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:461622 // Bare RenderWidgets don't support optimized plugin painting.
1623 return NULL;
[email protected]ca4847f2010-09-24 05:39:151624}
1625
[email protected]bcaf2272011-02-15 15:29:431626gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521627 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431628 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521629}
1630
[email protected]bee16aab2009-08-26 15:55:031631void RenderWidget::SetHidden(bool hidden) {
1632 if (is_hidden_ == hidden)
1633 return;
1634
1635 // The status has changed. Tell the RenderThread about it.
1636 is_hidden_ = hidden;
1637 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271638 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031639 else
[email protected]380244092011-10-07 17:26:271640 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031641}
1642
[email protected]2b624c562011-10-27 22:58:261643void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261644 if (!webwidget_)
1645 return;
1646
1647 if (is_fullscreen_) {
1648 webwidget_->willExitFullScreen();
1649 } else {
1650 webwidget_->willEnterFullScreen();
1651 }
[email protected]2b624c562011-10-27 22:58:261652}
1653
1654void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261655 if (!webwidget_)
1656 return;
1657
1658 if (is_fullscreen_) {
1659 webwidget_->didEnterFullScreen();
1660 } else {
1661 webwidget_->didExitFullScreen();
1662 }
[email protected]2b624c562011-10-27 22:58:261663}
1664
[email protected]699ab0d2009-04-23 23:19:141665void RenderWidget::SetBackground(const SkBitmap& background) {
1666 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461667
[email protected]699ab0d2009-04-23 23:19:141668 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281669 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141670}
1671
[email protected]674741932009-02-04 23:44:461672bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051673 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461674}
1675
1676bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051677 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461678}
1679
1680void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051681 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461682}
1683
1684void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051685 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461686}
1687
1688void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051689 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461690}
1691
[email protected]3306f262012-09-21 19:20:421692void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
1693 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
1694 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291695 return;
[email protected]ad26ef42011-06-17 07:59:451696 ui::TextInputType new_type = GetTextInputType();
[email protected]5b739cb2012-08-21 20:35:211697 WebKit::WebTextInputInfo new_info;
1698 if (webwidget_)
1699 new_info = webwidget_->textInputInfo();
1700
[email protected]ad26ef42011-06-17 07:59:451701 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:211702
[email protected]3306f262012-09-21 19:20:421703 // Only sends text input params if they are changed or if the ime should be
1704 // shown.
1705 if (show_ime_if_needed || (text_input_type_ != new_type
1706 || text_input_info_ != new_info
1707 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:211708 ViewHostMsg_TextInputState_Params p;
1709 p.type = new_type;
1710 p.value = new_info.value.utf8();
1711 p.selection_start = new_info.selectionStart;
1712 p.selection_end = new_info.selectionEnd;
1713 p.composition_start = new_info.compositionStart;
1714 p.composition_end = new_info.compositionEnd;
1715 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:421716 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:211717 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
1718
1719 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:041720 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451721 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:291722 }
initial.commit09911bf2008-07-26 23:55:291723}
1724
[email protected]3f783362011-10-21 22:40:501725void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1726 WebRect start_webrect;
1727 WebRect end_webrect;
1728 webwidget_->selectionBounds(start_webrect, end_webrect);
1729 *start = start_webrect;
1730 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321731}
1732
[email protected]e99ef6f2011-10-16 01:13:001733void RenderWidget::UpdateSelectionBounds() {
1734 if (!webwidget_)
1735 return;
1736
[email protected]3f783362011-10-21 22:40:501737 gfx::Rect start_rect;
1738 gfx::Rect end_rect;
1739 GetSelectionBounds(&start_rect, &end_rect);
[email protected]58b48a0d2012-06-13 07:01:351740 if (selection_start_rect_ != start_rect || selection_end_rect_ != end_rect) {
1741 selection_start_rect_ = start_rect;
1742 selection_end_rect_ = end_rect;
[email protected]b556c2e2012-08-10 22:18:271743 WebTextDirection start_dir = WebKit::WebTextDirectionLeftToRight;
1744 WebTextDirection end_dir = WebKit::WebTextDirectionLeftToRight;
1745 webwidget_->selectionTextDirection(start_dir, end_dir);
1746 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_,
1747 selection_start_rect_, start_dir, selection_end_rect_, end_dir));
[email protected]58b48a0d2012-06-13 07:01:351748 }
[email protected]e99ef6f2011-10-16 01:13:001749
[email protected]58b48a0d2012-06-13 07:01:351750 std::vector<gfx::Rect> character_bounds;
1751 GetCompositionCharacterBounds(&character_bounds);
1752 UpdateCompositionInfo(composition_range_, character_bounds);
1753}
1754
1755bool RenderWidget::ShouldUpdateCompositionInfo(
1756 const ui::Range& range,
1757 const std::vector<gfx::Rect>& bounds) {
1758 if (composition_range_ != range)
1759 return true;
1760 if (bounds.size() != composition_character_bounds_.size())
1761 return true;
1762 for (size_t i = 0; i < bounds.size(); ++i) {
1763 if (bounds[i] != composition_character_bounds_[i])
1764 return true;
1765 }
1766 return false;
[email protected]e99ef6f2011-10-16 01:13:001767}
1768
[email protected]73bf95812011-10-12 11:38:321769// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451770COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1771 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1772COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1773 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1774COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1775 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181776COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1777 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1778COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1779 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1780COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1781 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1782COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1783 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1784COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1785 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001786COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1787 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1788COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1789 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1790COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1791 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1792COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1793 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1794COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1795 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1796COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1797 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]ad26ef42011-06-17 07:59:451798
[email protected]5b739cb2012-08-21 20:35:211799ui::TextInputType RenderWidget::WebKitToUiTextInputType(
1800 WebKit::WebTextInputType type) {
1801 // Check the type is in the range representable by ui::TextInputType.
1802 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
1803 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1804 return static_cast<ui::TextInputType>(type);
1805}
1806
[email protected]ad26ef42011-06-17 07:59:451807ui::TextInputType RenderWidget::GetTextInputType() {
1808 if (webwidget_) {
[email protected]5b739cb2012-08-21 20:35:211809 WebKit::WebTextInputType type = webwidget_->textInputType();
1810 return WebKitToUiTextInputType(type);
[email protected]ad26ef42011-06-17 07:59:451811 }
1812 return ui::TEXT_INPUT_TYPE_NONE;
1813}
1814
[email protected]58b48a0d2012-06-13 07:01:351815void RenderWidget::GetCompositionCharacterBounds(
1816 std::vector<gfx::Rect>* bounds) {
1817 DCHECK(bounds);
1818 bounds->clear();
1819}
1820
[email protected]ad26ef42011-06-17 07:59:451821bool RenderWidget::CanComposeInline() {
1822 return true;
[email protected]56ea1a62011-05-30 07:05:571823}
1824
[email protected]4873c7d2009-07-16 06:36:281825WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:041826 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:281827}
1828
[email protected]f660d9c2012-06-06 18:31:211829float RenderWidget::deviceScaleFactor() {
1830 return device_scale_factor_;
1831}
1832
[email protected]fa7b1dc2010-06-23 17:53:041833void RenderWidget::resetInputMethod() {
1834 if (!input_method_is_active_)
1835 return;
1836
1837 // If the last text input type is not None, then we should finish any
1838 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451839 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041840 // If a composition text exists, then we need to let the browser process
1841 // to cancel the input method's ongoing composition session.
1842 if (webwidget_->confirmComposition())
1843 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1844 }
[email protected]d4cff272011-05-02 15:46:011845
1846 // Send an updated IME range with the current caret rect.
1847 ui::Range range(ui::Range::InvalidRange());
1848 size_t location, length;
1849 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1850 range.set_start(location);
1851 range.set_end(location + length);
1852 }
[email protected]58b48a0d2012-06-13 07:01:351853
1854 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:041855}
1856
[email protected]f103ab72009-09-02 17:10:591857void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501858 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291859 size_t i = 0;
1860 for (; i < plugin_window_moves_.size(); ++i) {
1861 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581862 if (move.rects_valid) {
1863 plugin_window_moves_[i] = move;
1864 } else {
1865 plugin_window_moves_[i].visible = move.visible;
1866 }
initial.commit09911bf2008-07-26 23:55:291867 break;
1868 }
1869 }
1870
1871 if (i == plugin_window_moves_.size())
1872 plugin_window_moves_.push_back(move);
1873}
[email protected]268654772009-08-06 23:02:041874
1875void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1876 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1877 i != plugin_window_moves_.end(); ++i) {
1878 if (i->window == window) {
1879 plugin_window_moves_.erase(i);
1880 break;
1881 }
1882 }
1883}
[email protected]67bfb83f2011-09-22 03:36:371884
[email protected]fef5e3972012-08-07 03:59:471885void RenderWidget::GetRenderingStats(WebKit::WebRenderingStats& stats) const {
1886 webwidget()->renderingStats(stats);
1887 stats.numAnimationFrames += software_stats_.numAnimationFrames;
1888 stats.numFramesSentToScreen += software_stats_.numFramesSentToScreen;
1889 stats.totalPaintTimeInSeconds += software_stats_.totalPaintTimeInSeconds;
1890}
1891
[email protected]e9ff79c2012-10-19 21:31:261892bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:521893 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
1894 if (!gpu_channel)
1895 return false;
1896
1897 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
1898}
1899
[email protected]0e241b4b2012-08-18 09:06:271900void RenderWidget::BeginSmoothScroll(
1901 bool down,
[email protected]ebd8b562012-10-09 14:44:291902 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:191903 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:291904 int mouse_event_x,
1905 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:271906 DCHECK(!callback.is_null());
1907 int id = next_smooth_scroll_gesture_id_++;
[email protected]267909d2012-10-20 04:36:191908
1909 ViewHostMsg_BeginSmoothScroll_Params params;
1910 params.scroll_down = down;
1911 params.pixels_to_scroll = pixels_to_scroll;
1912 params.mouse_event_x = mouse_event_x;
1913 params.mouse_event_y = mouse_event_y;
1914
1915 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, id, params));
[email protected]0e241b4b2012-08-18 09:06:271916 pending_smooth_scroll_gestures_.insert(std::make_pair(id, callback));
[email protected]a39ca1652012-07-13 21:30:581917}
1918
[email protected]67bfb83f2011-09-22 03:36:371919bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1920 return false;
1921}
[email protected]c3d45532011-10-07 19:20:401922
1923bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1924 return false;
1925}
[email protected]e9ff79c2012-10-19 21:31:261926
1927} // namespace content