blob: 9c3872e6d138a3b56029870ce0644613ff0ff532 [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]32876ae2011-11-15 22:25:217#include "base/bind.h"
[email protected]4fb66842009-12-04 21:41:008#include "base/command_line.h"
[email protected]366ae242011-05-10 02:23:589#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2114#include "base/stl_util.h"
[email protected]8a9d6ca32011-06-06 20:11:3015#include "base/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4816#include "build/build_config.h"
[email protected]992db4c2011-05-12 15:37:1517#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5018#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2919#include "content/public/common/content_switches.h"
[email protected]8704f89b2011-04-15 00:30:0520#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4421#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1922#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1823#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4824#include "skia/ext/platform_canvas.h"
[email protected]8bd0fe62011-01-17 06:44:3725#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]4b1146bc2012-07-10 18:46:0326#include "third_party/WebKit/Source/WebKit/chromium/public/WebHelperPlugin.h"
[email protected]a7547fb2012-03-08 04:43:4427#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3728#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
29#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0130#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3731#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4132#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
33#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]e6e90dc2011-12-03 00:01:3734#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
35#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]d353541f2012-05-03 22:45:4136#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1337#include "ui/base/ui_base_switches.h"
[email protected]a25e25b2012-09-28 14:32:3738#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3539#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4840#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2741#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4142#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3443#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5044#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4645#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4846
47#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4948#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5249#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4150#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4851#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4452
[email protected]8bd0fe62011-01-17 06:44:3753#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2954
[email protected]fa7b1dc2010-06-23 17:53:0455using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3056using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2257using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0658using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2859using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4460using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0061using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2862using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5963using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1864using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0165using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5266using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1967using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5268using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2869using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2470using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0471using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1872using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2673
74namespace content {
[email protected]62cb33cae2009-03-27 23:30:2275
[email protected]faec7b12012-06-19 14:42:1376static const float kStandardDPI = 160;
[email protected]f1cccb32012-06-06 18:29:5977
[email protected]6fd35b72012-03-01 19:46:4178RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:0479 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:4480 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:2981 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:5682 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:1183 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2984 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4285 host_window_(0),
[email protected]05a980d7a2012-02-07 22:16:4286 host_window_set_(false),
[email protected]b4d08452010-10-05 17:34:3587 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2988 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0689 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0590 update_reply_pending_(false),
[email protected]ea3ee0a2012-05-15 03:43:0991 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:2492 using_asynchronous_swapbuffers_(false),
93 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2994 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2995 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0996 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2997 needs_repainting_on_restore_(false),
98 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4999 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:48100 closing_(false),
[email protected]14392a52012-05-02 20:28:44101 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04102 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45103 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
104 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12105 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48106 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12107 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50108 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21109 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07110 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04111 screen_info_(screen_info),
[email protected]ce2b28e2012-08-09 15:53:57112 device_scale_factor_(1),
[email protected]0e241b4b2012-08-18 09:06:27113 throttle_input_events_(true),
114 next_smooth_scroll_gesture_id_(0) {
[email protected]8b3f0eb2012-05-03 19:15:05115 if (!swapped_out)
116 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27117 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18118 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kDisableGpuVsync);
[email protected]424820962012-06-08 15:33:19120#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
[email protected]faec7b12012-06-19 14:42:13121 device_scale_factor_ = screen_info.verticalDPI / kStandardDPI;
122 // Unless an explicit scale factor was provided for testing, ensure the scale
123 // is integral.
124 if (!CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kForceDeviceScaleFactor))
126 device_scale_factor_ = static_cast<int>(device_scale_factor_);
127 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
[email protected]f1cccb32012-06-06 18:29:59128#endif
initial.commit09911bf2008-07-26 23:55:29129}
130
131RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11132 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21133 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35134 if (current_paint_buf_) {
135 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
136 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29137 }
[email protected]992db4c2011-05-12 15:37:15138 // If we are swapped out, we have released already.
139 if (!is_swapped_out_)
140 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29141}
142
[email protected]484955942010-08-19 16:13:18143// static
[email protected]8085dbc82008-09-26 22:53:44144RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04145 WebKit::WebPopupType popup_type,
146 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29147 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41148 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04149 new RenderWidget(popup_type, screen_info, false));
initial.commit09911bf2008-07-26 23:55:29150 widget->Init(opener_id); // adds reference
151 return widget;
152}
153
[email protected]484955942010-08-19 16:13:18154// static
155WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
156 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03157 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18158 break;
159 case WebKit::WebPopupTypeSelect:
160 case WebKit::WebPopupTypeSuggestion:
161 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44162 case WebKit::WebPopupTypePage:
163 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03164 case WebKit::WebPopupTypeHelperPlugin:
165 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18166 default:
167 NOTREACHED();
168 }
169 return NULL;
170}
171
initial.commit09911bf2008-07-26 23:55:29172void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18173 DoInit(opener_id,
174 RenderWidget::CreateWebWidget(this),
[email protected]9f4f3322012-01-18 22:29:56175 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
176 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18177}
178
[email protected]484955942010-08-19 16:13:18179void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06180 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18181 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29182 DCHECK(!webwidget_);
183
184 if (opener_id != MSG_ROUTING_NONE)
185 opener_id_ = opener_id;
186
[email protected]484955942010-08-19 16:13:18187 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29188
[email protected]380244092011-10-07 17:26:27189 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29190 if (result) {
[email protected]380244092011-10-07 17:26:27191 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29192 // Take a reference on behalf of the RenderThread. This will be balanced
193 // when we receive ViewMsg_Close.
194 AddRef();
195 } else {
196 DCHECK(false);
197 }
198}
199
200// This is used to complete pending inits and non-pending inits. For non-
201// pending cases, the parent will be the same as the current parent. This
202// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55203void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29204 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29205
206 host_window_ = parent_hwnd;
[email protected]05a980d7a2012-02-07 22:16:42207 host_window_set_ = true;
208
[email protected]f1d5de42012-04-20 01:47:14209#if WEBWIDGET_HAS_SETCOMPOSITORSURFACEREADY
210 if (webwidget_)
211 webwidget_->setCompositorSurfaceReady();
212#endif
[email protected]05a980d7a2012-02-07 22:16:42213 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29214
[email protected]6de74452009-02-25 18:04:59215 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29216}
217
[email protected]992db4c2011-05-12 15:37:15218void RenderWidget::SetSwappedOut(bool is_swapped_out) {
219 // We should only toggle between states.
220 DCHECK(is_swapped_out_ != is_swapped_out);
221 is_swapped_out_ = is_swapped_out;
222
223 // If we are swapping out, we will call ReleaseProcess, allowing the process
224 // to exit if all of its RenderViews are swapped out. We wait until the
225 // WasSwappedOut call to do this, to avoid showing the sad tab.
226 // If we are swapping in, we call AddRefProcess to prevent the process from
227 // exiting.
228 if (!is_swapped_out)
229 RenderProcess::current()->AddRefProcess();
230}
231
[email protected]a95986a82010-12-24 06:19:28232bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
233 bool handled = true;
234 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
235 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
236 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
237 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54238 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28239 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41240 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15241 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28242 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]a7266a92012-06-28 02:11:08243 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, OnSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28244 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
245 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
246 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
247 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
248 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
249 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
250 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
251 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
[email protected]0e241b4b2012-08-18 09:06:27252 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted,
253 OnMsgSmoothScrollCompleted)
[email protected]fea38fc2012-06-13 17:38:37254 IPC_MESSAGE_HANDLER(ViewMsg_SetDeviceScaleFactor, OnSetDeviceScaleFactor)
[email protected]a95986a82010-12-24 06:19:28255 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
256 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53257 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]a95986a82010-12-24 06:19:28258 IPC_MESSAGE_UNHANDLED(handled = false)
259 IPC_END_MESSAGE_MAP()
260 return handled;
261}
initial.commit09911bf2008-07-26 23:55:29262
263bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15264 // Don't send any messages after the browser has told us to close, and filter
265 // most outgoing messages while swapped out.
266 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26267 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11268 closing_) {
initial.commit09911bf2008-07-26 23:55:29269 delete message;
270 return false;
271 }
272
273 // If given a messsage without a routing ID, then assign our routing ID.
274 if (message->routing_id() == MSG_ROUTING_NONE)
275 message->set_routing_id(routing_id_);
276
[email protected]380244092011-10-07 17:26:27277 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44278}
279
[email protected]61e2b3cc2012-03-02 16:13:34280void RenderWidget::Resize(const gfx::Size& new_size,
281 const gfx::Rect& resizer_rect,
282 bool is_fullscreen,
283 ResizeAck resize_ack) {
284 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
285 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
286 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29287
[email protected]61e2b3cc2012-03-02 16:13:34288 // Ignore this during shutdown.
289 if (!webwidget_)
290 return;
291
292 // Remember the rect where the resize corner will be drawn.
293 resizer_rect_ = resizer_rect;
294
295 // NOTE: We may have entered fullscreen mode without changing our size.
296 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
297 if (fullscreen_change)
298 WillToggleFullscreen();
299 is_fullscreen_ = is_fullscreen;
300
301 if (size_ != new_size) {
302 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34303 needs_repainting_on_restore_ = false;
304
305 size_ = new_size;
306
307 paint_aggregator_.ClearPendingUpdate();
308
309 // When resizing, we want to wait to paint before ACK'ing the resize. This
310 // ensures that we only resize as fast as we can paint. We only need to
311 // send an ACK if we are resized to a non-empty rect.
312 webwidget_->resize(new_size);
313 if (!new_size.IsEmpty()) {
314 if (!is_accelerated_compositing_active_) {
315 // Resize should have caused an invalidation of the entire view.
316 DCHECK(paint_aggregator_.HasPendingUpdate());
317 }
318
319 // Send the Resize_ACK flag once we paint again if requested.
320 if (resize_ack == SEND_RESIZE_ACK)
321 set_next_paint_is_resize_ack();
322 }
[email protected]ff475a322012-03-14 00:05:35323 } else {
324 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34325 }
326
327 if (fullscreen_change)
328 DidToggleFullscreen();
329
330 // If a resize ack is requested and it isn't set-up, then no more resizes will
331 // come in and in general things will go wrong.
332 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
333 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29334}
335
336void RenderWidget::OnClose() {
337 if (closing_)
338 return;
339 closing_ = true;
340
341 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03342 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27343 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03344 SetHidden(false);
345 }
initial.commit09911bf2008-07-26 23:55:29346
initial.commit09911bf2008-07-26 23:55:29347 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25348 // now. Post a task that only gets invoked when there are no nested message
349 // loops.
[email protected]32876ae2011-11-15 22:25:21350 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29351 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25352
353 // Balances the AddRef taken when we called AddRoute.
354 Release();
initial.commit09911bf2008-07-26 23:55:29355}
356
[email protected]61e2b3cc2012-03-02 16:13:34357// Got a response from the browser after the renderer decided to create a new
358// view.
359void RenderWidget::OnCreatingNewAck(
360 gfx::NativeViewId parent) {
361 DCHECK(routing_id_ != MSG_ROUTING_NONE);
362
363 CompleteInit(parent);
364}
365
[email protected]f21c613a2009-02-12 14:46:17366void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09367 const gfx::Rect& resizer_rect,
368 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34369 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29370}
371
[email protected]b5913d72012-02-07 22:26:54372void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
373 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59374 gfx::Rect view_rect(size_);
375
[email protected]d4030502012-10-23 16:51:47376 gfx::Rect old_damage_rect = view_rect;
377 old_damage_rect.Intersect(resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59378 if (!old_damage_rect.IsEmpty())
379 paint_aggregator_.InvalidateRect(old_damage_rect);
380
[email protected]d4030502012-10-23 16:51:47381 gfx::Rect new_damage_rect = view_rect;
382 new_damage_rect.Intersect(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]d0be63772011-12-20 23:18:04469void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24470 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21471 while (!updates_pending_swap_.empty()) {
472 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
473 updates_pending_swap_.pop_front();
474 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
475 // compositing pass, hence doesn't require an UpdateRect message.
476 if (msg)
477 Send(msg);
478 }
[email protected]65225772011-05-12 21:10:24479 num_swapbuffers_complete_pending_ = 0;
480 using_asynchronous_swapbuffers_ = false;
481 // Schedule another frame so the compositor learns about it.
482 scheduleComposite();
483}
484
[email protected]37a6f302011-07-11 23:43:08485void RenderWidget::OnSwapBuffersPosted() {
486 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21487
488 if (using_asynchronous_swapbuffers_) {
489 ViewHostMsg_UpdateRect* msg = NULL;
490 // pending_update_params_ can be NULL if the swap doesn't correspond to an
491 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
492 // message.
493 if (pending_update_params_.get()) {
494 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
495 pending_update_params_.reset();
496 }
497 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08498 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21499 }
[email protected]37a6f302011-07-11 23:43:08500}
501
502void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24503 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16504
[email protected]404939f2012-06-01 04:06:18505 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16506 DidFlushPaint();
507
[email protected]65225772011-05-12 21:10:24508 // When compositing deactivates, we reset the swapbuffers pending count. The
509 // swapbuffers acks may still arrive, however.
510 if (num_swapbuffers_complete_pending_ == 0) {
511 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
512 return;
513 }
[email protected]aa4117f2011-12-09 22:19:21514 DCHECK(!updates_pending_swap_.empty());
515 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
516 updates_pending_swap_.pop_front();
517 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
518 // compositing pass, hence doesn't require an UpdateRect message.
519 if (msg)
520 Send(msg);
[email protected]65225772011-05-12 21:10:24521 num_swapbuffers_complete_pending_--;
522
523 // If update reply is still pending, then defer the update until that reply
524 // occurs.
[email protected]d0be63772011-12-20 23:18:04525 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24526 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
527 return;
528 }
529
530 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06531 // when we were previously rendering. However, if an invalidation task is not
532 // posted, there may be software rendering work pending. In that case, don't
533 // early out.
534 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24535 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
536 return;
537 }
538
[email protected]cc66e682012-10-02 06:48:18539 // Do not call DoDeferredUpdate unless there's animation work to be done or
540 // a real invalidation. This prevents rendering in response to a swapbuffers
541 // callback coming back after we've navigated away from the page that
542 // generated it.
543 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
544 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
545 return;
546 }
547
[email protected]65225772011-05-12 21:10:24548 // Continue painting if necessary...
549 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29550}
551
initial.commit09911bf2008-07-26 23:55:29552void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24553 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
[email protected]ce208f872012-03-07 20:42:56554 PickleIterator iter(message);
initial.commit09911bf2008-07-26 23:55:29555
556 const char* data;
557 int data_length;
[email protected]5dd768212009-08-13 23:34:49558 handling_input_event_ = true;
559 if (!message.ReadData(&iter, &data, &data_length)) {
560 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29561 return;
[email protected]5dd768212009-08-13 23:34:49562 }
initial.commit09911bf2008-07-26 23:55:29563
564 const WebInputEvent* input_event =
565 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48566
[email protected]b68a0e52011-12-08 15:11:12567 bool is_keyboard_shortcut = false;
568 // is_keyboard_shortcut flag is only available for RawKeyDown events.
569 if (input_event->type == WebInputEvent::RawKeyDown)
570 message.ReadBool(&iter, &is_keyboard_shortcut);
571
[email protected]67bfb83f2011-09-22 03:36:37572 bool prevent_default = false;
573 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26574 const WebMouseEvent& mouse_event =
575 *static_cast<const WebMouseEvent*>(input_event);
576 TRACE_EVENT2("renderer", "HandleMouseMove",
577 "x", mouse_event.x, "y", mouse_event.y);
578 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37579 }
580
581 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12582 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
583 suppress_next_char_events_ = false;
584 if (!processed && webwidget_)
585 processed = webwidget_->handleInputEvent(*input_event);
586 }
587
588 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
589 // it's not processed by webkit, then we need to suppress the upcoming Char
590 // events.
591 if (!processed && is_keyboard_shortcut)
592 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29593
[email protected]a9fb30aa2011-10-06 06:58:46594 IPC::Message* response =
595 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
596 processed);
[email protected]3391a0772012-03-28 00:32:07597 bool event_type_gets_rate_limited =
598 input_event->type == WebInputEvent::MouseMove ||
599 input_event->type == WebInputEvent::MouseWheel ||
600 WebInputEvent::isTouchEventType(input_event->type);
601 bool is_input_throttled =
[email protected]ce2b28e2012-08-09 15:53:57602 throttle_input_events_ &&
603 ((webwidget_ ? webwidget_->isInputThrottled() : false) ||
604 paint_aggregator_.HasPendingUpdate());
[email protected]e2824412009-02-27 01:57:05605
[email protected]f8868d72012-04-27 19:13:03606 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24607 // We want to rate limit the input events in this case, so we'll wait for
608 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17609 if (pending_input_event_ack_.get()) {
610 // As two different kinds of events could cause us to postpone an ack
611 // we send it now, if we have one pending. The Browser should never
612 // send us the same kind of event we are delaying the ack for.
613 Send(pending_input_event_ack_.release());
614 }
[email protected]12fbad812009-09-01 18:21:24615 pending_input_event_ack_.reset(response);
616 } else {
617 Send(response);
618 }
619
[email protected]3306f262012-09-21 19:20:42620#if defined(OS_ANDROID)
621 // Allow the IME to be shown when the focus changes as a consequence
622 // of a processed touch end event.
623 if (input_event->type == WebInputEvent::TouchEnd && processed)
624 UpdateTextInputState(SHOW_IME_IF_NEEDED);
625#endif
626
[email protected]5dd768212009-08-13 23:34:49627 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48628
[email protected]67bfb83f2011-09-22 03:36:37629 if (!prevent_default) {
630 if (WebInputEvent::isKeyboardEventType(input_event->type))
631 DidHandleKeyEvent();
632 if (WebInputEvent::isMouseEventType(input_event->type))
633 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24634 if (WebInputEvent::isTouchEventType(input_event->type))
635 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37636 }
initial.commit09911bf2008-07-26 23:55:29637}
638
639void RenderWidget::OnMouseCaptureLost() {
640 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28641 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29642}
643
644void RenderWidget::OnSetFocus(bool enable) {
645 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33646 if (webwidget_)
647 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29648}
649
650void RenderWidget::ClearFocus() {
651 // We may have got the focus from the browser before this gets processed, in
652 // which case we do not want to unfocus ourself.
653 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28654 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29655}
656
[email protected]2d5d09d52009-06-15 14:29:21657void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00658 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21659 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06660 TRACE_EVENT2("renderer", "PaintRect",
661 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00662 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21663
664 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00665 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
666 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03667
[email protected]699ab0d2009-04-23 23:19:14668 // If there is a custom background, tile it.
669 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14670 SkPaint paint;
671 SkShader* shader = SkShader::CreateBitmapShader(background_,
672 SkShader::kRepeat_TileMode,
673 SkShader::kRepeat_TileMode);
674 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20675
676 // Use kSrc_Mode to handle background_ transparency properly.
677 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
678
679 // Canvas could contain multiple update rects. Clip to given rect so that
680 // we don't accidentally clear other update rects.
681 canvas->save();
[email protected]1835b9e2012-02-28 13:12:48682 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14683 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20684 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14685 }
686
[email protected]719b36f2010-12-22 20:36:46687 // First see if this rect is a plugin that can paint itself faster.
688 TransportDIB* optimized_dib = NULL;
689 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20690 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46691 webkit::ppapi::PluginInstance* optimized_instance =
692 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
693 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20694 &optimized_copy_rect,
695 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46696 if (optimized_instance) {
697 // This plugin can be optimize-painted and we can just ask it to paint
698 // itself. We don't actually need the TransportDIB in this case.
699 //
700 // This is an optimization for PPAPI plugins that know they're on top of
701 // the page content. If this rect is inside such a plugin, we can save some
702 // time and avoid re-rendering the page content which we know will be
703 // covered by the plugin later (this time can be significant, especially
704 // for a playing movie that is invalidating a lot).
705 //
706 // In the plugin movie case, hopefully the similar call to
707 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
708 // painting, because that avoids copying the plugin image to a different
709 // paint rect. Unfortunately, if anything on the page is animating other
710 // than the movie, it break this optimization since the union of the
711 // invalid regions will be larger than the plugin.
712 //
713 // This code optimizes that case, where we can still avoid painting in
714 // WebKit and filling the background (which can be slow) and just painting
715 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
716 // required.
[email protected]fef5e3972012-08-07 03:59:47717 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]df59dd42012-09-14 22:56:30718 SkAutoCanvasRestore auto_restore(canvas, true);
719 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:46720 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27721 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:21722 canvas->restore();
[email protected]fef5e3972012-08-07 03:59:47723 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
724 if (!is_accelerated_compositing_active_)
725 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46726 } else {
727 // Normal painting case.
[email protected]fef5e3972012-08-07 03:59:47728 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]719b36f2010-12-22 20:36:46729 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]fef5e3972012-08-07 03:59:47730 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
731 if (!is_accelerated_compositing_active_)
732 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46733
734 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35735 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46736 }
initial.commit09911bf2008-07-26 23:55:29737
[email protected]4fb66842009-12-04 21:41:00738 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00739 canvas->restore();
740}
741
742void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
743 skia::PlatformCanvas* canvas) {
744 static bool kPaintBorder =
745 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
746 if (!kPaintBorder)
747 return;
748
[email protected]53d3f302009-12-21 04:42:05749 // Cycle through these colors to help distinguish new paint rects.
750 const SkColor colors[] = {
751 SkColorSetARGB(0x3F, 0xFF, 0, 0),
752 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
753 SkColorSetARGB(0x3F, 0, 0, 0xFF),
754 };
755 static int color_selector = 0;
756
[email protected]4fb66842009-12-04 21:41:00757 SkPaint paint;
758 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05759 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00760 paint.setStrokeWidth(1);
761
762 SkIRect irect;
763 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
764 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29765}
766
[email protected]52ccd0ea2011-02-16 01:09:05767void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30768 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30769 if (!animation_update_pending_) {
770 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59771 return;
[email protected]921244e42011-07-20 16:36:30772 }
[email protected]bd37ae252011-06-03 01:28:18773 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59774 // Record when we fired (according to base::Time::Now()) relative to when
775 // we posted the task to quantify how much the base::Time/base::TimeTicks
776 // skew is affecting animations.
777 base::TimeDelta animation_callback_delay = base::Time::Now() -
778 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
779 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
780 animation_callback_delay,
781 base::TimeDelta::FromMilliseconds(0),
782 base::TimeDelta::FromMilliseconds(30),
783 25);
784 }
[email protected]65225772011-05-12 21:10:24785 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24786}
787
[email protected]52ccd0ea2011-02-16 01:09:05788void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59789 if (!animation_update_pending_)
790 return;
[email protected]bd37ae252011-06-03 01:28:18791
792 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33793 base::TimeDelta animationInterval = IsRenderingVSynced() ?
794 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18795
[email protected]7c4329e2011-02-18 22:02:59796 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45797
798 // animation_floor_time_ is the earliest time that we should animate when
799 // using the dead reckoning software scheduler. If we're using swapbuffers
800 // complete callbacks to rate limit, we can ignore this floor.
801 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30802 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33803 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18804 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59805 // running animation callbacks so that if a callback requests another
806 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38807 animation_timer_.Stop();
808 animation_timer_.Start(FROM_HERE, animationInterval, this,
809 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59810 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30811 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59812 return;
[email protected]5f8b1022011-01-21 23:34:50813 }
[email protected]bd37ae252011-06-03 01:28:18814 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38815 if (!animation_timer_.IsRunning()) {
816 // This code uses base::Time::Now() to calculate the floor and next fire
817 // time because javascript's Date object uses base::Time::Now(). The
818 // message loop uses base::TimeTicks, which on windows can have a
819 // different granularity than base::Time.
820 // The upshot of all this is that this function might be called before
821 // base::Time::Now() has advanced past the animation_floor_time_. To
822 // avoid exposing this delay to javascript, we keep posting delayed
823 // tasks until base::Time::Now() has advanced far enough.
824 base::TimeDelta delay = animation_floor_time_ - now;
825 animation_timer_.Start(FROM_HERE, delay, this,
826 &RenderWidget::AnimationCallback);
827 }
[email protected]5f8b1022011-01-21 23:34:50828}
829
[email protected]bd37ae252011-06-03 01:28:18830bool RenderWidget::IsRenderingVSynced() {
831 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
832 // not caught by this check. This will lead to artificially low frame rates
833 // for people who force vsync off at a driver level and expect Chrome to speed
834 // up.
835 return !has_disable_gpu_vsync_switch_;
836}
837
[email protected]65225772011-05-12 21:10:24838void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06839 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24840 invalidation_task_posted_ = false;
841 DoDeferredUpdateAndSendInputAck();
842}
843
844void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05845 DoDeferredUpdate();
846
847 if (pending_input_event_ack_.get())
848 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21849}
850
[email protected]552e6002009-11-19 05:24:57851void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58852 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08853
[email protected]65225772011-05-12 21:10:24854 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29855 return;
[email protected]05a980d7a2012-02-07 22:16:42856
857 if (!host_window_set_) {
858 TRACE_EVENT0("renderer", "EarlyOut_NoHostWindow");
859 return;
860 }
[email protected]aa4117f2011-12-09 22:19:21861 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24862 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
863 return;
864 }
[email protected]9ca84622011-06-02 23:46:39865 if (is_accelerated_compositing_active_ &&
866 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24867 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
868 return;
869 }
initial.commit09911bf2008-07-26 23:55:29870
[email protected]552e6002009-11-19 05:24:57871 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29872 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57873 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29874 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24875 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29876 return;
877 }
878
[email protected]05a980d7a2012-02-07 22:16:42879 if (is_accelerated_compositing_active_)
880 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
881
[email protected]0fb93f52011-05-18 23:13:56882 // Tracking of frame rate jitter
883 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]541dcd52012-03-15 15:57:51884 webwidget_->instrumentBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:05885 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50886
[email protected]f98d7e3c2010-09-13 22:30:46887 // Layout may generate more invalidation. It may also enable the
888 // GPU acceleration, so make sure to run layout before we send the
889 // GpuRenderingActivated message.
890 webwidget_->layout();
891
[email protected]dcca3aa92012-02-17 23:03:37892 // The following two can result in further layout and possibly
893 // enable GPU acceleration so they need to be called before any painting
894 // is done.
[email protected]3306f262012-09-21 19:20:42895 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]dcca3aa92012-02-17 23:03:37896 UpdateSelectionBounds();
897
[email protected]5f8b1022011-01-21 23:34:50898 // Suppress painting if nothing is dirty. This has to be done after updating
899 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24900 if (!paint_aggregator_.HasPendingUpdate()) {
901 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]541dcd52012-03-15 15:57:51902 webwidget_->instrumentCancelFrame();
[email protected]5f8b1022011-01-21 23:34:50903 return;
[email protected]65225772011-05-12 21:10:24904 }
[email protected]5f8b1022011-01-21 23:34:50905
[email protected]872ae5b2011-05-26 20:20:50906 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56907 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04908 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56909 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
910 delay,
911 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41912 base::TimeDelta::FromMilliseconds(120),
913 60);
[email protected]d0be63772011-12-20 23:18:04914 } else {
[email protected]0fb93f52011-05-18 23:13:56915 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
916 delay,
917 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41918 base::TimeDelta::FromMilliseconds(120),
919 60);
[email protected]d0be63772011-12-20 23:18:04920 }
[email protected]872ae5b2011-05-26 20:20:50921
922 // Calculate filtered time per frame:
923 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
924 filtered_time_per_frame_ =
925 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56926 }
927 last_do_deferred_update_time_ = frame_begin_ticks;
928
[email protected]fef5e3972012-08-07 03:59:47929 if (!is_accelerated_compositing_active_) {
930 software_stats_.numAnimationFrames++;
931 software_stats_.numFramesSentToScreen++;
932 }
933
[email protected]552e6002009-11-19 05:24:57934 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29935 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30936 PaintAggregator::PendingUpdate update;
937 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29938
[email protected]53d3f302009-12-21 04:42:05939 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]d4030502012-10-23 16:51:47940 gfx::Rect bounds = update.GetPaintBounds();
941 bounds.Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29942
[email protected]29ed96a2012-02-04 18:12:16943 // Notify derived classes that we're about to initiate a paint.
944 WillInitiatePaint();
945
[email protected]ca4847f2010-09-24 05:39:15946 // A plugin may be able to do an optimized paint. First check this, in which
947 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46948 // This optimization allows PPAPI plugins that declare themselves on top of
949 // the page (like a traditional windowed plugin) to be able to animate (think
950 // movie playing) without repeatedly re-painting the page underneath, or
951 // copying the plugin backing store (since we can send the plugin's backing
952 // store directly to the browser).
953 //
954 // This optimization only works when the entire invalid region is contained
955 // within the plugin. There is a related optimization in PaintRect for the
956 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15957 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15958 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20959 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:21960 DCHECK(!pending_update_params_.get());
961 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
962 pending_update_params_->dx = update.scroll_delta.x();
963 pending_update_params_->dy = update.scroll_delta.y();
964 pending_update_params_->scroll_rect = update.scroll_rect;
965 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:21966 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
967 pending_update_params_->flags = next_paint_flags_;
968 pending_update_params_->scroll_offset = GetScrollOffset();
969 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:09970 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:21971 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:09972 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:21973
[email protected]ca4847f2010-09-24 05:39:15974 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56975 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15976 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20977 &optimized_copy_rect,
978 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:27979 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:47980 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21981 pending_update_params_->bitmap = dib->id();
982 pending_update_params_->bitmap_rect = optimized_copy_location;
983 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:20984 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:56985 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46986 // Compute a buffer for painting and cache it.
[email protected]d4030502012-10-23 16:51:47987 gfx::RectF scaled_bounds = bounds;
988 scaled_bounds.Scale(device_scale_factor_);
989 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(scaled_bounds);
[email protected]ca4847f2010-09-24 05:39:15990 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35991 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:59992 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:46993 if (!canvas.get()) {
994 NOTREACHED();
995 return;
996 }
[email protected]cef3362f2009-12-21 17:48:45997
[email protected]f98d7e3c2010-09-13 22:30:46998 // We may get back a smaller canvas than we asked for.
999 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591000 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1001 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1002 pixel_bounds.set_width(canvas->getDevice()->width());
1003 pixel_bounds.set_height(canvas->getDevice()->height());
1004 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1005 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051006
[email protected]f98d7e3c2010-09-13 22:30:461007 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1008
[email protected]aa4117f2011-12-09 22:19:211009 pending_update_params_->bitmap = current_paint_buf_->id();
1010 pending_update_params_->bitmap_rect = bounds;
1011
1012 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461013 // The scroll damage is just another rectangle to paint and copy.
1014 copy_rects.swap(update.paint_rects);
1015 if (!scroll_damage.IsEmpty())
1016 copy_rects.push_back(scroll_damage);
1017
1018 for (size_t i = 0; i < copy_rects.size(); ++i)
[email protected]f1cccb32012-06-06 18:29:591019 PaintRect(copy_rects[i], pixel_bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:351020
1021 // Software FPS tick for performance tests. The accelerated path traces the
1022 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1023 // NOTE: Tests may break if this event is renamed or moved.
1024 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:461025 } else { // Accelerated compositing path
1026 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211027 // If painting is done via the gpu process then we don't set any damage
1028 // rects to save the browser process from doing unecessary work.
1029 pending_update_params_->bitmap_rect = bounds;
1030 pending_update_params_->scroll_rect = gfx::Rect();
1031 // We don't need an ack, because we're not sharing a DIB with the browser.
1032 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1033 // with the browser for the GPU surface.
1034 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:421035 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:461036 }
1037
[email protected]936c6f52011-12-13 01:35:261038 // If we're holding a pending input event ACK, send the ACK before sending the
1039 // UpdateReply message so we can receive another input event before the
1040 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1041 // the UpdateRect IPC message handler.
1042 if (pending_input_event_ack_.get())
1043 Send(pending_input_event_ack_.release());
1044
[email protected]aa4117f2011-12-09 22:19:211045 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
1046 // OnSwapBuffersPosted), meaning a message has been added to the
1047 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1048 // the message now.
1049 if (pending_update_params_.get()) {
1050 // sending an ack to browser process that the paint is complete...
1051 update_reply_pending_ = pending_update_params_->needs_ack;
1052 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1053 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341054 }
[email protected]53d3f302009-12-21 04:42:051055
[email protected]29ed96a2012-02-04 18:12:161056 // If we're software rendering then we're done initiating the paint.
1057 if (!is_accelerated_compositing_active_)
1058 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291059}
1060
1061///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461062// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291063
[email protected]4873c7d2009-07-16 06:36:281064void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571065 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481066 gfx::Rect view_rect(size_);
[email protected]d4030502012-10-23 16:51:471067 gfx::Rect damaged_rect = view_rect;
1068 damaged_rect.Intersect(rect);
[email protected]552e6002009-11-19 05:24:571069 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291070 return;
1071
[email protected]552e6002009-11-19 05:24:571072 paint_aggregator_.InvalidateRect(damaged_rect);
1073
1074 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241075 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571076 return;
1077 if (!paint_aggregator_.HasPendingUpdate())
1078 return;
[email protected]aa4117f2011-12-09 22:19:211079 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241080 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1081 return;
1082
1083 // When GPU rendering, combine pending animations and invalidations into
1084 // a single update.
[email protected]816edc62012-03-17 01:27:221085 if (is_accelerated_compositing_active_ &&
1086 animation_update_pending_ &&
1087 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571088 return;
1089
1090 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291091 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1092 // on the call stack.
1093 // 2) Allows us to collect more damage rects before painting to help coalesce
1094 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241095 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211096 MessageLoop::current()->PostTask(
1097 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291098}
1099
[email protected]4873c7d2009-07-16 06:36:281100void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461101 // Drop scrolls on the floor when we are in compositing mode.
1102 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561103 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461104 return;
1105
[email protected]552e6002009-11-19 05:24:571106 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481107 gfx::Rect view_rect(size_);
[email protected]d4030502012-10-23 16:51:471108 gfx::Rect damaged_rect = view_rect;
1109 damaged_rect.Intersect(clip_rect);
[email protected]552e6002009-11-19 05:24:571110 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291111 return;
1112
[email protected]552e6002009-11-19 05:24:571113 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
1114
1115 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241116 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571117 return;
1118 if (!paint_aggregator_.HasPendingUpdate())
1119 return;
[email protected]aa4117f2011-12-09 22:19:211120 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241121 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1122 return;
1123
1124 // When GPU rendering, combine pending animations and invalidations into
1125 // a single update.
[email protected]816edc62012-03-17 01:27:221126 if (is_accelerated_compositing_active_ &&
1127 animation_update_pending_ &&
1128 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571129 return;
1130
1131 // Perform updating asynchronously. This serves two purposes:
1132 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1133 // on the call stack.
1134 // 2) Allows us to collect more damage rects before painting to help coalesce
1135 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241136 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211137 MessageLoop::current()->PostTask(
1138 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291139}
1140
[email protected]244ac1892011-12-02 17:04:471141void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091142 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1143 size_ = new_size;
1144 need_update_rect_for_auto_resize_ = true;
1145 }
[email protected]244ac1892011-12-02 17:04:471146}
1147
[email protected]91acd1c2012-03-14 08:32:391148void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221149 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1150
[email protected]c63b4d42012-04-26 01:01:071151#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211152 if (!is_accelerated_compositing_active_) {
1153 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1154 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1155 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1156 // going to switch to accelerated compositing, the GPU process may need
1157 // round-trips to the browser's UI thread before finishing the frame,
1158 // causing deadlocks if we delay the UpdateRect until we receive the
1159 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071160 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1161 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211162 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1163 }
[email protected]c63b4d42012-04-26 01:01:071164#endif
[email protected]aa4117f2011-12-09 22:19:211165
[email protected]ea162f92011-10-04 23:08:221166 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421167 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241168 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221169}
1170
1171void RenderWidget::didDeactivateCompositor() {
1172 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1173
1174 is_accelerated_compositing_active_ = false;
1175 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1176 routing_id_, is_accelerated_compositing_active_));
1177
[email protected]ea162f92011-10-04 23:08:221178 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241179 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561180}
1181
[email protected]9cd43a62012-03-26 08:03:561182void RenderWidget::willBeginCompositorFrame() {
1183 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371184
1185 DCHECK(RenderThreadImpl::current()->compositor_thread());
1186
1187 // The following two can result in further layout and possibly
1188 // enable GPU acceleration so they need to be called before any painting
1189 // is done.
[email protected]3306f262012-09-21 19:20:421190 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]abe8b3a2012-03-28 21:19:371191 UpdateSelectionBounds();
1192
[email protected]9cd43a62012-03-26 08:03:561193 WillInitiatePaint();
1194}
1195
[email protected]3391a0772012-03-28 00:32:071196void RenderWidget::didBecomeReadyForAdditionalInput() {
1197 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1198 if (pending_input_event_ack_.get())
1199 Send(pending_input_event_ack_.release());
1200}
1201
[email protected]58264a32011-11-17 23:36:151202void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501203 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351204 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1205 // NOTE: Tests may break if this event is renamed or moved.
1206 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161207 // Notify subclasses that we initiated the paint operation.
1208 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151209}
1210
1211void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181212 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1213
1214 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561215 DidFlushPaint();
1216
[email protected]aa4117f2011-12-09 22:19:211217 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151218 return;
1219
[email protected]ea3ee0a2012-05-15 03:43:091220 if (!next_paint_flags_ &&
1221 !need_update_rect_for_auto_resize_ &&
1222 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151223 return;
[email protected]ea3ee0a2012-05-15 03:43:091224 }
[email protected]58264a32011-11-17 23:36:151225
1226 ViewHostMsg_UpdateRect_Params params;
1227 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151228 params.plugin_window_moves.swap(plugin_window_moves_);
1229 params.flags = next_paint_flags_;
1230 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121231 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091232 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151233
1234 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1235 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091236 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151237}
1238
[email protected]f98d7e3c2010-09-13 22:30:461239void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041240 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401241 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041242 } else {
[email protected]c3d45532011-10-07 19:20:401243 // TODO(nduca): replace with something a little less hacky. The reason this
1244 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1245 // contains a lot of host-renderer synchronization logic that is still
1246 // important for the accelerated compositing case. The option of simply
1247 // duplicating all that code is less desirable than "faking out" the
1248 // invalidation path using a magical damage rect.
1249 didInvalidateRect(WebRect(0, 0, 1, 1));
1250 }
[email protected]f98d7e3c2010-09-13 22:30:461251}
1252
[email protected]5f8b1022011-01-21 23:34:501253void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201254 if (animation_update_pending_)
1255 return;
1256
[email protected]921244e42011-07-20 16:36:301257 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201258 animation_update_pending_ = true;
1259 if (!animation_timer_.IsRunning()) {
1260 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1261 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211262 }
[email protected]5f8b1022011-01-21 23:34:501263}
1264
[email protected]4873c7d2009-07-16 06:36:281265void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301266 // TODO(darin): Eliminate this temporary.
1267 WebCursor cursor(cursor_info);
1268
initial.commit09911bf2008-07-26 23:55:291269 // Only send a SetCursor message if we need to make a change.
1270 if (!current_cursor_.IsEqual(cursor)) {
1271 current_cursor_ = cursor;
1272 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1273 }
1274}
1275
1276// We are supposed to get a single call to Show for a newly created RenderWidget
1277// that was created via RenderWidget::CreateWebView. So, we wait until this
1278// point to dispatch the ShowWidget message.
1279//
1280// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281281// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291282//
[email protected]4873c7d2009-07-16 06:36:281283void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291284 DCHECK(!did_show_) << "received extraneous Show call";
1285 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1286 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1287
[email protected]8de12d942010-11-17 20:42:441288 if (did_show_)
1289 return;
1290
1291 did_show_ = true;
1292 // NOTE: initial_pos_ may still have its default values at this point, but
1293 // that's okay. It'll be ignored if as_popup is false, or the browser
1294 // process will impose a default position otherwise.
1295 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1296 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291297}
1298
[email protected]4873c7d2009-07-16 06:36:281299void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291300}
1301
[email protected]4873c7d2009-07-16 06:36:281302void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291303}
1304
[email protected]2533ce12009-05-09 00:02:241305void RenderWidget::DoDeferredClose() {
1306 Send(new ViewHostMsg_Close(routing_id_));
1307}
1308
[email protected]4873c7d2009-07-16 06:36:281309void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321310 if (is_swapped_out_) {
1311 // This widget is currently swapped out, and the active widget is in a
1312 // different process. Have the browser route the close request to the
1313 // active widget instead, so that the correct unload handlers are run.
1314 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1315 return;
1316 }
1317
initial.commit09911bf2008-07-26 23:55:291318 // If a page calls window.close() twice, we'll end up here twice, but that's
1319 // OK. It is safe to send multiple Close messages.
1320
[email protected]2533ce12009-05-09 00:02:241321 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1322 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1323 // could be closed before the JS finishes executing. So instead, post a
1324 // message back to the message loop, which won't run until the JS is
1325 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211326 MessageLoop::current()->PostTask(
1327 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291328}
1329
1330void RenderWidget::Close() {
1331 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281332 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291333 webwidget_ = NULL;
1334 }
1335}
1336
[email protected]4873c7d2009-07-16 06:36:281337WebRect RenderWidget::windowRect() {
1338 if (pending_window_rect_count_)
1339 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241340
[email protected]2bcd5402012-09-25 22:25:591341#if defined(OS_ANDROID)
1342 // Short circuit of the sync RPC call.
1343 return gfx::Rect(size_);
1344#else
[email protected]b3f2b912009-04-09 16:18:521345 gfx::Rect rect;
1346 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281347 return rect;
[email protected]2bcd5402012-09-25 22:25:591348#endif
initial.commit09911bf2008-07-26 23:55:291349}
1350
[email protected]8a9d6ca32011-06-06 20:11:301351void RenderWidget::setToolTipText(const WebKit::WebString& text,
1352 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541353 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301354}
1355
[email protected]4873c7d2009-07-16 06:36:281356void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291357 if (did_show_) {
1358 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241359 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291360 } else {
1361 initial_pos_ = pos;
1362 }
1363}
1364
[email protected]2533ce12009-05-09 00:02:241365void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1366 pending_window_rect_ = rect;
1367 pending_window_rect_count_++;
1368}
1369
[email protected]4873c7d2009-07-16 06:36:281370WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241371 if (pending_window_rect_count_) {
1372 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1373 // the RootWindowRect is probably going to return wrong results since the
1374 // browser may not have processed the Move yet. There isn't really anything
1375 // good to do in this case, and it shouldn't happen - since this size is
1376 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281377 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241378 }
1379
[email protected]2bcd5402012-09-25 22:25:591380#if defined(OS_ANDROID)
1381 // Short circuit of the sync RPC call.
1382 return gfx::Rect(size_);
1383#else
[email protected]b3f2b912009-04-09 16:18:521384 gfx::Rect rect;
1385 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281386 return rect;
[email protected]2bcd5402012-09-25 22:25:591387#endif
[email protected]d4547452008-08-28 18:36:371388}
1389
[email protected]4873c7d2009-07-16 06:36:281390WebRect RenderWidget::windowResizerRect() {
1391 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191392}
1393
[email protected]fa7b1dc2010-06-23 17:53:041394void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031395 // To prevent this renderer process from sending unnecessary IPC messages to
1396 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041397 // only during the input method attached to the browser process is active.
1398 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291399}
1400
[email protected]58b48a0d2012-06-13 07:01:351401void RenderWidget::UpdateCompositionInfo(
1402 const ui::Range& range,
1403 const std::vector<gfx::Rect>& character_bounds) {
1404 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1405 return;
1406 composition_character_bounds_ = character_bounds;
1407 composition_range_ = range;
1408 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1409 routing_id(), composition_range_, composition_character_bounds_));
1410}
1411
[email protected]fa7b1dc2010-06-23 17:53:041412void RenderWidget::OnImeSetComposition(
1413 const string16& text,
1414 const std::vector<WebCompositionUnderline>& underlines,
1415 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281416 if (!webwidget_)
1417 return;
[email protected]d4cff272011-05-02 15:46:011418 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041419 text, WebVector<WebCompositionUnderline>(underlines),
1420 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011421 // Setting the IME composition was successful. Send the new composition
1422 // range to the browser.
1423 ui::Range range(ui::Range::InvalidRange());
1424 size_t location, length;
1425 if (webwidget_->compositionRange(&location, &length)) {
1426 range.set_start(location);
1427 range.set_end(location + length);
1428 }
1429 // The IME was cancelled via the Esc key, so just send back the caret.
1430 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1431 range.set_start(location);
1432 range.set_end(location + length);
1433 }
[email protected]58b48a0d2012-06-13 07:01:351434 std::vector<gfx::Rect> character_bounds;
1435 GetCompositionCharacterBounds(&character_bounds);
1436 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011437 } else {
[email protected]fa7b1dc2010-06-23 17:53:041438 // If we failed to set the composition text, then we need to let the browser
1439 // process to cancel the input method's ongoing composition session, to make
1440 // sure we are in a consistent state.
1441 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011442
1443 // Send an updated IME range with just the caret range.
1444 ui::Range range(ui::Range::InvalidRange());
1445 size_t location, length;
1446 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 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261451 }
[email protected]fa7b1dc2010-06-23 17:53:041452}
1453
[email protected]4de6d1692011-10-12 08:45:441454void RenderWidget::OnImeConfirmComposition(
1455 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041456 if (!webwidget_)
1457 return;
1458
1459 handling_input_event_ = true;
1460 webwidget_->confirmComposition(text);
1461 handling_input_event_ = false;
1462
[email protected]d4cff272011-05-02 15:46:011463 // Send an updated IME range with just the caret range.
1464 ui::Range range(ui::Range::InvalidRange());
1465 size_t location, length;
1466 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1467 range.set_start(location);
1468 range.set_end(location + length);
1469 }
[email protected]58b48a0d2012-06-13 07:01:351470 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
initial.commit09911bf2008-07-26 23:55:291471}
1472
[email protected]948f7ab72010-05-28 23:48:081473// This message causes the renderer to render an image of the
1474// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491475void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091476 int tag,
[email protected]948f7ab72010-05-28 23:48:081477 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491478 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001479 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1480 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251481 // Close our unused handle.
1482#if defined(OS_WIN)
1483 ::CloseHandle(dib_handle);
1484#elif defined(OS_MACOSX)
1485 base::SharedMemory::CloseHandle(dib_handle);
1486#endif
1487 }
[email protected]d65adb12010-04-28 17:26:491488 return;
[email protected]45c6aad32010-11-11 04:46:251489 }
[email protected]d65adb12010-04-28 17:26:491490
[email protected]948f7ab72010-05-28 23:48:081491 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491492 // If one of these is empty, then we just return the dib we were
1493 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091494 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491495 return;
1496 }
1497
1498 // Map the given DIB ID into this process, and unmap it at the end
1499 // of this function.
[email protected]45c6aad32010-11-11 04:46:251500 scoped_ptr<TransportDIB> paint_at_size_buffer(
1501 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301502
[email protected]4b01b962012-10-09 23:17:351503 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
1504 page_size.Scale(device_scale_factor_));
1505 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
1506 desired_size.Scale(device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511507 gfx::Size canvas_size = page_size_in_pixel;
1508 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491509 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511510 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491511 static_cast<float>(canvas_size.height());
1512
[email protected]ee8d6fd2010-05-26 17:05:481513 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491514 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1515 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481516 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491517
[email protected]36808ad2010-10-20 19:18:301518 scoped_ptr<skia::PlatformCanvas> canvas(
1519 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1520 canvas_size.height()));
1521 if (!canvas.get()) {
1522 NOTREACHED();
1523 return;
1524 }
1525
[email protected]d65adb12010-04-28 17:26:491526 // Reset bounds to what we actually received, but they should be the
1527 // same.
1528 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1529 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1530 bounds.set_width(canvas->getDevice()->width());
1531 bounds.set_height(canvas->getDevice()->height());
1532
1533 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081534 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491535 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1536
[email protected]948f7ab72010-05-28 23:48:081537 // Have to make sure we're laid out at the right size before
1538 // rendering.
1539 gfx::Size old_size = webwidget_->size();
1540 webwidget_->resize(page_size);
1541 webwidget_->layout();
1542
[email protected]d65adb12010-04-28 17:26:491543 // Paint the entire thing (using original bounds, not scaled bounds).
1544 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1545 canvas->restore();
1546
[email protected]948f7ab72010-05-28 23:48:081547 // Return the widget to its previous size.
1548 webwidget_->resize(old_size);
1549
[email protected]c88c9442010-07-19 18:55:091550 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491551}
1552
[email protected]ec7dc112008-08-06 05:30:121553void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1554 // During shutdown we can just ignore this message.
1555 if (!webwidget_)
1556 return;
1557
1558 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561559 if (is_accelerated_compositing_active_) {
[email protected]8c49fa982012-02-10 14:37:041560 webwidget_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461561 scheduleComposite();
1562 } else {
1563 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1564 didInvalidateRect(repaint_rect);
1565 }
[email protected]ec7dc112008-08-06 05:30:121566}
1567
[email protected]fea38fc2012-06-13 17:38:371568void RenderWidget::OnSetDeviceScaleFactor(float device_scale_factor) {
[email protected]dea5e7682012-06-14 05:21:241569 if (device_scale_factor_ == device_scale_factor)
1570 return;
1571
1572 device_scale_factor_ = device_scale_factor;
1573
1574 if (!is_accelerated_compositing_active_) {
1575 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1576 } else {
1577 scheduleComposite();
1578 }
[email protected]fea38fc2012-06-13 17:38:371579}
1580
[email protected]0e241b4b2012-08-18 09:06:271581void RenderWidget::OnMsgSmoothScrollCompleted(int gesture_id) {
1582 PendingSmoothScrollGestureMap::iterator it =
1583 pending_smooth_scroll_gestures_.find(gesture_id);
1584 DCHECK(it != pending_smooth_scroll_gestures_.end());
1585 it->second.Run();
1586 pending_smooth_scroll_gestures_.erase(it);
1587}
1588
[email protected]4873c7d2009-07-16 06:36:281589void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111590 if (!webwidget_)
1591 return;
[email protected]4873c7d2009-07-16 06:36:281592 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111593}
1594
[email protected]6131a642012-06-15 23:26:531595void RenderWidget::OnScreenInfoChanged(
1596 const WebKit::WebScreenInfo& screen_info) {
1597 screen_info_ = screen_info;
1598}
1599
[email protected]719b36f2010-12-22 20:36:461600webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151601 const gfx::Rect& paint_bounds,
1602 TransportDIB** dib,
1603 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:201604 gfx::Rect* clip,
1605 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:461606 // Bare RenderWidgets don't support optimized plugin painting.
1607 return NULL;
[email protected]ca4847f2010-09-24 05:39:151608}
1609
[email protected]bcaf2272011-02-15 15:29:431610gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521611 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431612 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521613}
1614
[email protected]bee16aab2009-08-26 15:55:031615void RenderWidget::SetHidden(bool hidden) {
1616 if (is_hidden_ == hidden)
1617 return;
1618
1619 // The status has changed. Tell the RenderThread about it.
1620 is_hidden_ = hidden;
1621 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271622 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031623 else
[email protected]380244092011-10-07 17:26:271624 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031625}
1626
[email protected]2b624c562011-10-27 22:58:261627void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261628 if (!webwidget_)
1629 return;
1630
1631 if (is_fullscreen_) {
1632 webwidget_->willExitFullScreen();
1633 } else {
1634 webwidget_->willEnterFullScreen();
1635 }
[email protected]2b624c562011-10-27 22:58:261636}
1637
1638void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261639 if (!webwidget_)
1640 return;
1641
1642 if (is_fullscreen_) {
1643 webwidget_->didEnterFullScreen();
1644 } else {
1645 webwidget_->didExitFullScreen();
1646 }
[email protected]2b624c562011-10-27 22:58:261647}
1648
[email protected]699ab0d2009-04-23 23:19:141649void RenderWidget::SetBackground(const SkBitmap& background) {
1650 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461651
[email protected]699ab0d2009-04-23 23:19:141652 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281653 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141654}
1655
[email protected]674741932009-02-04 23:44:461656bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051657 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461658}
1659
1660bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051661 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461662}
1663
1664void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051665 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461666}
1667
1668void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051669 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461670}
1671
1672void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051673 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461674}
1675
[email protected]3306f262012-09-21 19:20:421676void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
1677 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
1678 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291679 return;
[email protected]ad26ef42011-06-17 07:59:451680 ui::TextInputType new_type = GetTextInputType();
[email protected]5b739cb2012-08-21 20:35:211681 WebKit::WebTextInputInfo new_info;
1682 if (webwidget_)
1683 new_info = webwidget_->textInputInfo();
1684
[email protected]ad26ef42011-06-17 07:59:451685 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:211686
[email protected]3306f262012-09-21 19:20:421687 // Only sends text input params if they are changed or if the ime should be
1688 // shown.
1689 if (show_ime_if_needed || (text_input_type_ != new_type
1690 || text_input_info_ != new_info
1691 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:211692 ViewHostMsg_TextInputState_Params p;
1693 p.type = new_type;
1694 p.value = new_info.value.utf8();
1695 p.selection_start = new_info.selectionStart;
1696 p.selection_end = new_info.selectionEnd;
1697 p.composition_start = new_info.compositionStart;
1698 p.composition_end = new_info.compositionEnd;
1699 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:421700 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:211701 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
1702
1703 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:041704 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451705 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:291706 }
initial.commit09911bf2008-07-26 23:55:291707}
1708
[email protected]3f783362011-10-21 22:40:501709void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1710 WebRect start_webrect;
1711 WebRect end_webrect;
1712 webwidget_->selectionBounds(start_webrect, end_webrect);
1713 *start = start_webrect;
1714 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321715}
1716
[email protected]e99ef6f2011-10-16 01:13:001717void RenderWidget::UpdateSelectionBounds() {
1718 if (!webwidget_)
1719 return;
1720
[email protected]3f783362011-10-21 22:40:501721 gfx::Rect start_rect;
1722 gfx::Rect end_rect;
1723 GetSelectionBounds(&start_rect, &end_rect);
[email protected]58b48a0d2012-06-13 07:01:351724 if (selection_start_rect_ != start_rect || selection_end_rect_ != end_rect) {
1725 selection_start_rect_ = start_rect;
1726 selection_end_rect_ = end_rect;
[email protected]b556c2e2012-08-10 22:18:271727 WebTextDirection start_dir = WebKit::WebTextDirectionLeftToRight;
1728 WebTextDirection end_dir = WebKit::WebTextDirectionLeftToRight;
1729 webwidget_->selectionTextDirection(start_dir, end_dir);
1730 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_,
1731 selection_start_rect_, start_dir, selection_end_rect_, end_dir));
[email protected]58b48a0d2012-06-13 07:01:351732 }
[email protected]e99ef6f2011-10-16 01:13:001733
[email protected]58b48a0d2012-06-13 07:01:351734 std::vector<gfx::Rect> character_bounds;
1735 GetCompositionCharacterBounds(&character_bounds);
1736 UpdateCompositionInfo(composition_range_, character_bounds);
1737}
1738
1739bool RenderWidget::ShouldUpdateCompositionInfo(
1740 const ui::Range& range,
1741 const std::vector<gfx::Rect>& bounds) {
1742 if (composition_range_ != range)
1743 return true;
1744 if (bounds.size() != composition_character_bounds_.size())
1745 return true;
1746 for (size_t i = 0; i < bounds.size(); ++i) {
1747 if (bounds[i] != composition_character_bounds_[i])
1748 return true;
1749 }
1750 return false;
[email protected]e99ef6f2011-10-16 01:13:001751}
1752
[email protected]73bf95812011-10-12 11:38:321753// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451754COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1755 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1756COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1757 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1758COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1759 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181760COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1761 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1762COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1763 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1764COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1765 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1766COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1767 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1768COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1769 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001770COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1771 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1772COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1773 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1774COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1775 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1776COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1777 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1778COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1779 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1780COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1781 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]ad26ef42011-06-17 07:59:451782
[email protected]5b739cb2012-08-21 20:35:211783ui::TextInputType RenderWidget::WebKitToUiTextInputType(
1784 WebKit::WebTextInputType type) {
1785 // Check the type is in the range representable by ui::TextInputType.
1786 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
1787 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1788 return static_cast<ui::TextInputType>(type);
1789}
1790
[email protected]ad26ef42011-06-17 07:59:451791ui::TextInputType RenderWidget::GetTextInputType() {
1792 if (webwidget_) {
[email protected]5b739cb2012-08-21 20:35:211793 WebKit::WebTextInputType type = webwidget_->textInputType();
1794 return WebKitToUiTextInputType(type);
[email protected]ad26ef42011-06-17 07:59:451795 }
1796 return ui::TEXT_INPUT_TYPE_NONE;
1797}
1798
[email protected]58b48a0d2012-06-13 07:01:351799void RenderWidget::GetCompositionCharacterBounds(
1800 std::vector<gfx::Rect>* bounds) {
1801 DCHECK(bounds);
1802 bounds->clear();
1803}
1804
[email protected]ad26ef42011-06-17 07:59:451805bool RenderWidget::CanComposeInline() {
1806 return true;
[email protected]56ea1a62011-05-30 07:05:571807}
1808
[email protected]4873c7d2009-07-16 06:36:281809WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:041810 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:281811}
1812
[email protected]f660d9c2012-06-06 18:31:211813float RenderWidget::deviceScaleFactor() {
1814 return device_scale_factor_;
1815}
1816
[email protected]fa7b1dc2010-06-23 17:53:041817void RenderWidget::resetInputMethod() {
1818 if (!input_method_is_active_)
1819 return;
1820
1821 // If the last text input type is not None, then we should finish any
1822 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451823 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041824 // If a composition text exists, then we need to let the browser process
1825 // to cancel the input method's ongoing composition session.
1826 if (webwidget_->confirmComposition())
1827 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1828 }
[email protected]d4cff272011-05-02 15:46:011829
1830 // Send an updated IME range with the current caret rect.
1831 ui::Range range(ui::Range::InvalidRange());
1832 size_t location, length;
1833 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1834 range.set_start(location);
1835 range.set_end(location + length);
1836 }
[email protected]58b48a0d2012-06-13 07:01:351837
1838 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:041839}
1840
[email protected]f103ab72009-09-02 17:10:591841void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501842 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291843 size_t i = 0;
1844 for (; i < plugin_window_moves_.size(); ++i) {
1845 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581846 if (move.rects_valid) {
1847 plugin_window_moves_[i] = move;
1848 } else {
1849 plugin_window_moves_[i].visible = move.visible;
1850 }
initial.commit09911bf2008-07-26 23:55:291851 break;
1852 }
1853 }
1854
1855 if (i == plugin_window_moves_.size())
1856 plugin_window_moves_.push_back(move);
1857}
[email protected]268654772009-08-06 23:02:041858
1859void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1860 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1861 i != plugin_window_moves_.end(); ++i) {
1862 if (i->window == window) {
1863 plugin_window_moves_.erase(i);
1864 break;
1865 }
1866 }
1867}
[email protected]67bfb83f2011-09-22 03:36:371868
[email protected]fef5e3972012-08-07 03:59:471869void RenderWidget::GetRenderingStats(WebKit::WebRenderingStats& stats) const {
1870 webwidget()->renderingStats(stats);
1871 stats.numAnimationFrames += software_stats_.numAnimationFrames;
1872 stats.numFramesSentToScreen += software_stats_.numFramesSentToScreen;
1873 stats.totalPaintTimeInSeconds += software_stats_.totalPaintTimeInSeconds;
1874}
1875
[email protected]e9ff79c2012-10-19 21:31:261876bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:521877 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
1878 if (!gpu_channel)
1879 return false;
1880
1881 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
1882}
1883
[email protected]0e241b4b2012-08-18 09:06:271884void RenderWidget::BeginSmoothScroll(
1885 bool down,
[email protected]ebd8b562012-10-09 14:44:291886 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:191887 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:291888 int mouse_event_x,
1889 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:271890 DCHECK(!callback.is_null());
1891 int id = next_smooth_scroll_gesture_id_++;
[email protected]267909d2012-10-20 04:36:191892
1893 ViewHostMsg_BeginSmoothScroll_Params params;
1894 params.scroll_down = down;
1895 params.pixels_to_scroll = pixels_to_scroll;
1896 params.mouse_event_x = mouse_event_x;
1897 params.mouse_event_y = mouse_event_y;
1898
1899 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, id, params));
[email protected]0e241b4b2012-08-18 09:06:271900 pending_smooth_scroll_gestures_.insert(std::make_pair(id, callback));
[email protected]a39ca1652012-07-13 21:30:581901}
1902
[email protected]67bfb83f2011-09-22 03:36:371903bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1904 return false;
1905}
[email protected]c3d45532011-10-07 19:20:401906
1907bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1908 return false;
1909}
[email protected]e9ff79c2012-10-19 21:31:261910
1911} // namespace content