blob: 12b90a938d5224d8dc6bb3d38e89ccdd7acdcaab [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]32876ae2011-11-15 22:25:217#include "base/bind.h"
[email protected]4fb66842009-12-04 21:41:008#include "base/command_line.h"
[email protected]366ae242011-05-10 02:23:589#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2114#include "base/stl_util.h"
[email protected]8a9d6ca32011-06-06 20:11:3015#include "base/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4816#include "build/build_config.h"
[email protected]992db4c2011-05-12 15:37:1517#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5018#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2919#include "content/public/common/content_switches.h"
[email protected]8704f89b2011-04-15 00:30:0520#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4421#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1922#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1823#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4824#include "skia/ext/platform_canvas.h"
[email protected]8bd0fe62011-01-17 06:44:3725#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]4b1146bc2012-07-10 18:46:0326#include "third_party/WebKit/Source/WebKit/chromium/public/WebHelperPlugin.h"
[email protected]a7547fb2012-03-08 04:43:4427#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3728#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
29#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0130#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3731#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4132#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
33#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]e6e90dc2011-12-03 00:01:3734#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
35#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]d353541f2012-05-03 22:45:4136#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1337#include "ui/base/ui_base_switches.h"
[email protected]08397d52011-02-05 01:53:3838#include "ui/gfx/point.h"
39#include "ui/gfx/size.h"
[email protected]1835b9e2012-02-28 13:12:4840#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2741#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4142#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3443#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5044#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4645#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4846
47#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4948#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5249#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4150#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4851#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4452
[email protected]8bd0fe62011-01-17 06:44:3753#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2954
[email protected]fa7b1dc2010-06-23 17:53:0455using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3056using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2257using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0658using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2859using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4460using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0061using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2862using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5963using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1864using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0165using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5266using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1967using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5268using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2869using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2470using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0471using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1872using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2773using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2274
[email protected]faec7b12012-06-19 14:42:1375static const float kStandardDPI = 160;
[email protected]f1cccb32012-06-06 18:29:5976
[email protected]6fd35b72012-03-01 19:46:4177RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:0478 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:4479 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:2980 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:5681 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:1182 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2983 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4284 host_window_(0),
[email protected]05a980d7a2012-02-07 22:16:4285 host_window_set_(false),
[email protected]b4d08452010-10-05 17:34:3586 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2987 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0688 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0589 update_reply_pending_(false),
[email protected]ea3ee0a2012-05-15 03:43:0990 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:2491 using_asynchronous_swapbuffers_(false),
92 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2993 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2994 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0995 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2996 needs_repainting_on_restore_(false),
97 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4998 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4899 closing_(false),
[email protected]14392a52012-05-02 20:28:44100 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04101 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45102 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
103 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12104 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48105 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12106 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50107 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21108 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07109 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04110 screen_info_(screen_info),
[email protected]ce2b28e2012-08-09 15:53:57111 device_scale_factor_(1),
112 throttle_input_events_(true) {
[email protected]8b3f0eb2012-05-03 19:15:05113 if (!swapped_out)
114 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27115 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18116 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
117 switches::kDisableGpuVsync);
[email protected]424820962012-06-08 15:33:19118#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
[email protected]faec7b12012-06-19 14:42:13119 device_scale_factor_ = screen_info.verticalDPI / kStandardDPI;
120 // Unless an explicit scale factor was provided for testing, ensure the scale
121 // is integral.
122 if (!CommandLine::ForCurrentProcess()->HasSwitch(
123 switches::kForceDeviceScaleFactor))
124 device_scale_factor_ = static_cast<int>(device_scale_factor_);
125 device_scale_factor_ = std::max(1.0f, device_scale_factor_);
[email protected]f1cccb32012-06-06 18:29:59126#endif
initial.commit09911bf2008-07-26 23:55:29127}
128
129RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11130 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21131 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35132 if (current_paint_buf_) {
133 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
134 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29135 }
[email protected]992db4c2011-05-12 15:37:15136 // If we are swapped out, we have released already.
137 if (!is_swapped_out_)
138 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29139}
140
[email protected]484955942010-08-19 16:13:18141// static
[email protected]8085dbc82008-09-26 22:53:44142RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04143 WebKit::WebPopupType popup_type,
144 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29145 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41146 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04147 new RenderWidget(popup_type, screen_info, false));
initial.commit09911bf2008-07-26 23:55:29148 widget->Init(opener_id); // adds reference
149 return widget;
150}
151
[email protected]484955942010-08-19 16:13:18152// static
153WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
154 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03155 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18156 break;
157 case WebKit::WebPopupTypeSelect:
158 case WebKit::WebPopupTypeSuggestion:
159 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44160 case WebKit::WebPopupTypePage:
161 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03162 case WebKit::WebPopupTypeHelperPlugin:
163 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18164 default:
165 NOTREACHED();
166 }
167 return NULL;
168}
169
initial.commit09911bf2008-07-26 23:55:29170void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18171 DoInit(opener_id,
172 RenderWidget::CreateWebWidget(this),
[email protected]9f4f3322012-01-18 22:29:56173 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
174 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18175}
176
[email protected]484955942010-08-19 16:13:18177void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06178 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18179 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29180 DCHECK(!webwidget_);
181
182 if (opener_id != MSG_ROUTING_NONE)
183 opener_id_ = opener_id;
184
[email protected]484955942010-08-19 16:13:18185 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29186
[email protected]380244092011-10-07 17:26:27187 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29188 if (result) {
[email protected]380244092011-10-07 17:26:27189 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29190 // Take a reference on behalf of the RenderThread. This will be balanced
191 // when we receive ViewMsg_Close.
192 AddRef();
193 } else {
194 DCHECK(false);
195 }
196}
197
198// This is used to complete pending inits and non-pending inits. For non-
199// pending cases, the parent will be the same as the current parent. This
200// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55201void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29202 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29203
204 host_window_ = parent_hwnd;
[email protected]05a980d7a2012-02-07 22:16:42205 host_window_set_ = true;
206
[email protected]f1d5de42012-04-20 01:47:14207#if WEBWIDGET_HAS_SETCOMPOSITORSURFACEREADY
208 if (webwidget_)
209 webwidget_->setCompositorSurfaceReady();
210#endif
[email protected]05a980d7a2012-02-07 22:16:42211 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29212
[email protected]6de74452009-02-25 18:04:59213 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29214}
215
[email protected]992db4c2011-05-12 15:37:15216void RenderWidget::SetSwappedOut(bool is_swapped_out) {
217 // We should only toggle between states.
218 DCHECK(is_swapped_out_ != is_swapped_out);
219 is_swapped_out_ = is_swapped_out;
220
221 // If we are swapping out, we will call ReleaseProcess, allowing the process
222 // to exit if all of its RenderViews are swapped out. We wait until the
223 // WasSwappedOut call to do this, to avoid showing the sad tab.
224 // If we are swapping in, we call AddRefProcess to prevent the process from
225 // exiting.
226 if (!is_swapped_out)
227 RenderProcess::current()->AddRefProcess();
228}
229
[email protected]a95986a82010-12-24 06:19:28230bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
231 bool handled = true;
232 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
233 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
234 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
235 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54236 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28237 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41238 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15239 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28240 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]a7266a92012-06-28 02:11:08241 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, OnSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28242 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
243 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
244 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
245 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
246 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
247 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
248 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
249 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
[email protected]fea38fc2012-06-13 17:38:37250 IPC_MESSAGE_HANDLER(ViewMsg_SetDeviceScaleFactor, OnSetDeviceScaleFactor)
[email protected]a95986a82010-12-24 06:19:28251 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
252 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53253 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]a95986a82010-12-24 06:19:28254 IPC_MESSAGE_UNHANDLED(handled = false)
255 IPC_END_MESSAGE_MAP()
256 return handled;
257}
initial.commit09911bf2008-07-26 23:55:29258
259bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15260 // Don't send any messages after the browser has told us to close, and filter
261 // most outgoing messages while swapped out.
262 if ((is_swapped_out_ &&
263 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11264 closing_) {
initial.commit09911bf2008-07-26 23:55:29265 delete message;
266 return false;
267 }
268
269 // If given a messsage without a routing ID, then assign our routing ID.
270 if (message->routing_id() == MSG_ROUTING_NONE)
271 message->set_routing_id(routing_id_);
272
[email protected]380244092011-10-07 17:26:27273 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44274}
275
[email protected]61e2b3cc2012-03-02 16:13:34276void RenderWidget::Resize(const gfx::Size& new_size,
277 const gfx::Rect& resizer_rect,
278 bool is_fullscreen,
279 ResizeAck resize_ack) {
280 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
281 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
282 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29283
[email protected]61e2b3cc2012-03-02 16:13:34284 // Ignore this during shutdown.
285 if (!webwidget_)
286 return;
287
288 // Remember the rect where the resize corner will be drawn.
289 resizer_rect_ = resizer_rect;
290
291 // NOTE: We may have entered fullscreen mode without changing our size.
292 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
293 if (fullscreen_change)
294 WillToggleFullscreen();
295 is_fullscreen_ = is_fullscreen;
296
297 if (size_ != new_size) {
298 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34299 needs_repainting_on_restore_ = false;
300
301 size_ = new_size;
302
303 paint_aggregator_.ClearPendingUpdate();
304
305 // When resizing, we want to wait to paint before ACK'ing the resize. This
306 // ensures that we only resize as fast as we can paint. We only need to
307 // send an ACK if we are resized to a non-empty rect.
308 webwidget_->resize(new_size);
309 if (!new_size.IsEmpty()) {
310 if (!is_accelerated_compositing_active_) {
311 // Resize should have caused an invalidation of the entire view.
312 DCHECK(paint_aggregator_.HasPendingUpdate());
313 }
314
315 // Send the Resize_ACK flag once we paint again if requested.
316 if (resize_ack == SEND_RESIZE_ACK)
317 set_next_paint_is_resize_ack();
318 }
[email protected]ff475a322012-03-14 00:05:35319 } else {
320 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34321 }
322
323 if (fullscreen_change)
324 DidToggleFullscreen();
325
326 // If a resize ack is requested and it isn't set-up, then no more resizes will
327 // come in and in general things will go wrong.
328 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
329 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29330}
331
332void RenderWidget::OnClose() {
333 if (closing_)
334 return;
335 closing_ = true;
336
337 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03338 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27339 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03340 SetHidden(false);
341 }
initial.commit09911bf2008-07-26 23:55:29342
initial.commit09911bf2008-07-26 23:55:29343 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25344 // now. Post a task that only gets invoked when there are no nested message
345 // loops.
[email protected]32876ae2011-11-15 22:25:21346 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29347 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25348
349 // Balances the AddRef taken when we called AddRoute.
350 Release();
initial.commit09911bf2008-07-26 23:55:29351}
352
[email protected]61e2b3cc2012-03-02 16:13:34353// Got a response from the browser after the renderer decided to create a new
354// view.
355void RenderWidget::OnCreatingNewAck(
356 gfx::NativeViewId parent) {
357 DCHECK(routing_id_ != MSG_ROUTING_NONE);
358
359 CompleteInit(parent);
360}
361
[email protected]f21c613a2009-02-12 14:46:17362void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09363 const gfx::Rect& resizer_rect,
364 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34365 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29366}
367
[email protected]b5913d72012-02-07 22:26:54368void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
369 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59370 gfx::Rect view_rect(size_);
371
372 gfx::Rect old_damage_rect = view_rect.Intersect(resizer_rect_);
373 if (!old_damage_rect.IsEmpty())
374 paint_aggregator_.InvalidateRect(old_damage_rect);
375
376 gfx::Rect new_damage_rect = view_rect.Intersect(resizer_rect);
377 if (!new_damage_rect.IsEmpty())
378 paint_aggregator_.InvalidateRect(new_damage_rect);
379
[email protected]b5913d72012-02-07 22:26:54380 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59381
[email protected]b5913d72012-02-07 22:26:54382 if (webwidget_)
383 webwidget_->didChangeWindowResizerRect();
384 }
385}
386
initial.commit09911bf2008-07-26 23:55:29387void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31388 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29389 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03390 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29391}
392
[email protected]9e2e4632012-07-27 16:38:41393void RenderWidget::OnWasShown(bool needs_repainting) {
394 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29395 // During shutdown we can just ignore this message.
396 if (!webwidget_)
397 return;
398
399 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03400 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29401
402 if (!needs_repainting && !needs_repainting_on_restore_)
403 return;
404 needs_repainting_on_restore_ = false;
405
[email protected]d65adb12010-04-28 17:26:49406 // Tag the next paint as a restore ack, which is picked up by
407 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29408 set_next_paint_is_restore_ack();
409
410 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56411 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46412 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
413 } else {
414 scheduleComposite();
415 }
initial.commit09911bf2008-07-26 23:55:29416}
417
[email protected]992db4c2011-05-12 15:37:15418void RenderWidget::OnWasSwappedOut() {
419 // If we have been swapped out and no one else is using this process,
420 // it's safe to exit now. If we get swapped back in, we will call
421 // AddRefProcess in SetSwappedOut.
422 if (is_swapped_out_)
423 RenderProcess::current()->ReleaseProcess();
424}
425
[email protected]53d3f302009-12-21 04:42:05426void RenderWidget::OnRequestMoveAck() {
427 DCHECK(pending_window_rect_count_);
428 pending_window_rect_count_--;
429}
430
431void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58432 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21433 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05434 update_reply_pending_ = false;
435
[email protected]b4d08452010-10-05 17:34:35436 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
437 // have no current paint buffer.
438 if (current_paint_buf_) {
439 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
440 current_paint_buf_ = NULL;
441 }
442
[email protected]65225772011-05-12 21:10:24443 // If swapbuffers is still pending, then defer the update until the
444 // swapbuffers occurs.
445 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
446 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
447 return;
448 }
449
[email protected]29ed96a2012-02-04 18:12:16450 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18451 if (!is_accelerated_compositing_active_) {
452 DidFlushPaint();
453 }
[email protected]a2f6bc112009-06-27 16:27:25454
initial.commit09911bf2008-07-26 23:55:29455 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24456 DoDeferredUpdateAndSendInputAck();
457}
458
[email protected]d0be63772011-12-20 23:18:04459bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24460 return false;
461}
462
[email protected]d0be63772011-12-20 23:18:04463void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24464 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21465 while (!updates_pending_swap_.empty()) {
466 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
467 updates_pending_swap_.pop_front();
468 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
469 // compositing pass, hence doesn't require an UpdateRect message.
470 if (msg)
471 Send(msg);
472 }
[email protected]65225772011-05-12 21:10:24473 num_swapbuffers_complete_pending_ = 0;
474 using_asynchronous_swapbuffers_ = false;
475 // Schedule another frame so the compositor learns about it.
476 scheduleComposite();
477}
478
[email protected]37a6f302011-07-11 23:43:08479void RenderWidget::OnSwapBuffersPosted() {
480 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21481
482 if (using_asynchronous_swapbuffers_) {
483 ViewHostMsg_UpdateRect* msg = NULL;
484 // pending_update_params_ can be NULL if the swap doesn't correspond to an
485 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
486 // message.
487 if (pending_update_params_.get()) {
488 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
489 pending_update_params_.reset();
490 }
491 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08492 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21493 }
[email protected]37a6f302011-07-11 23:43:08494}
495
496void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24497 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16498
[email protected]404939f2012-06-01 04:06:18499 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16500 DidFlushPaint();
501
[email protected]65225772011-05-12 21:10:24502 // When compositing deactivates, we reset the swapbuffers pending count. The
503 // swapbuffers acks may still arrive, however.
504 if (num_swapbuffers_complete_pending_ == 0) {
505 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
506 return;
507 }
[email protected]aa4117f2011-12-09 22:19:21508 DCHECK(!updates_pending_swap_.empty());
509 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
510 updates_pending_swap_.pop_front();
511 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
512 // compositing pass, hence doesn't require an UpdateRect message.
513 if (msg)
514 Send(msg);
[email protected]65225772011-05-12 21:10:24515 num_swapbuffers_complete_pending_--;
516
517 // If update reply is still pending, then defer the update until that reply
518 // occurs.
[email protected]d0be63772011-12-20 23:18:04519 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24520 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
521 return;
522 }
523
524 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06525 // when we were previously rendering. However, if an invalidation task is not
526 // posted, there may be software rendering work pending. In that case, don't
527 // early out.
528 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24529 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
530 return;
531 }
532
[email protected]65225772011-05-12 21:10:24533 // Continue painting if necessary...
534 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29535}
536
initial.commit09911bf2008-07-26 23:55:29537void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24538 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
[email protected]ce208f872012-03-07 20:42:56539 PickleIterator iter(message);
initial.commit09911bf2008-07-26 23:55:29540
541 const char* data;
542 int data_length;
[email protected]5dd768212009-08-13 23:34:49543 handling_input_event_ = true;
544 if (!message.ReadData(&iter, &data, &data_length)) {
545 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29546 return;
[email protected]5dd768212009-08-13 23:34:49547 }
initial.commit09911bf2008-07-26 23:55:29548
549 const WebInputEvent* input_event =
550 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48551
[email protected]b68a0e52011-12-08 15:11:12552 bool is_keyboard_shortcut = false;
553 // is_keyboard_shortcut flag is only available for RawKeyDown events.
554 if (input_event->type == WebInputEvent::RawKeyDown)
555 message.ReadBool(&iter, &is_keyboard_shortcut);
556
[email protected]67bfb83f2011-09-22 03:36:37557 bool prevent_default = false;
558 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26559 const WebMouseEvent& mouse_event =
560 *static_cast<const WebMouseEvent*>(input_event);
561 TRACE_EVENT2("renderer", "HandleMouseMove",
562 "x", mouse_event.x, "y", mouse_event.y);
563 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37564 }
565
566 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12567 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
568 suppress_next_char_events_ = false;
569 if (!processed && webwidget_)
570 processed = webwidget_->handleInputEvent(*input_event);
571 }
572
573 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
574 // it's not processed by webkit, then we need to suppress the upcoming Char
575 // events.
576 if (!processed && is_keyboard_shortcut)
577 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29578
[email protected]a9fb30aa2011-10-06 06:58:46579 IPC::Message* response =
580 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
581 processed);
[email protected]3391a0772012-03-28 00:32:07582 bool event_type_gets_rate_limited =
583 input_event->type == WebInputEvent::MouseMove ||
584 input_event->type == WebInputEvent::MouseWheel ||
585 WebInputEvent::isTouchEventType(input_event->type);
586 bool is_input_throttled =
[email protected]ce2b28e2012-08-09 15:53:57587 throttle_input_events_ &&
588 ((webwidget_ ? webwidget_->isInputThrottled() : false) ||
589 paint_aggregator_.HasPendingUpdate());
[email protected]e2824412009-02-27 01:57:05590
[email protected]f8868d72012-04-27 19:13:03591 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24592 // We want to rate limit the input events in this case, so we'll wait for
593 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17594 if (pending_input_event_ack_.get()) {
595 // As two different kinds of events could cause us to postpone an ack
596 // we send it now, if we have one pending. The Browser should never
597 // send us the same kind of event we are delaying the ack for.
598 Send(pending_input_event_ack_.release());
599 }
[email protected]12fbad812009-09-01 18:21:24600 pending_input_event_ack_.reset(response);
601 } else {
602 Send(response);
603 }
604
[email protected]5dd768212009-08-13 23:34:49605 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48606
[email protected]67bfb83f2011-09-22 03:36:37607 if (!prevent_default) {
608 if (WebInputEvent::isKeyboardEventType(input_event->type))
609 DidHandleKeyEvent();
610 if (WebInputEvent::isMouseEventType(input_event->type))
611 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24612 if (WebInputEvent::isTouchEventType(input_event->type))
613 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37614 }
initial.commit09911bf2008-07-26 23:55:29615}
616
617void RenderWidget::OnMouseCaptureLost() {
618 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28619 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29620}
621
622void RenderWidget::OnSetFocus(bool enable) {
623 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33624 if (webwidget_)
625 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29626}
627
628void RenderWidget::ClearFocus() {
629 // We may have got the focus from the browser before this gets processed, in
630 // which case we do not want to unfocus ourself.
631 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28632 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29633}
634
[email protected]2d5d09d52009-06-15 14:29:21635void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00636 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21637 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06638 TRACE_EVENT2("renderer", "PaintRect",
639 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00640 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21641
642 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00643 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
644 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03645
[email protected]699ab0d2009-04-23 23:19:14646 // If there is a custom background, tile it.
647 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14648 SkPaint paint;
649 SkShader* shader = SkShader::CreateBitmapShader(background_,
650 SkShader::kRepeat_TileMode,
651 SkShader::kRepeat_TileMode);
652 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20653
654 // Use kSrc_Mode to handle background_ transparency properly.
655 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
656
657 // Canvas could contain multiple update rects. Clip to given rect so that
658 // we don't accidentally clear other update rects.
659 canvas->save();
[email protected]1835b9e2012-02-28 13:12:48660 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14661 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20662 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14663 }
664
[email protected]719b36f2010-12-22 20:36:46665 // First see if this rect is a plugin that can paint itself faster.
666 TransportDIB* optimized_dib = NULL;
667 gfx::Rect optimized_copy_rect, optimized_copy_location;
668 webkit::ppapi::PluginInstance* optimized_instance =
669 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
670 &optimized_copy_location,
671 &optimized_copy_rect);
672 if (optimized_instance) {
673 // This plugin can be optimize-painted and we can just ask it to paint
674 // itself. We don't actually need the TransportDIB in this case.
675 //
676 // This is an optimization for PPAPI plugins that know they're on top of
677 // the page content. If this rect is inside such a plugin, we can save some
678 // time and avoid re-rendering the page content which we know will be
679 // covered by the plugin later (this time can be significant, especially
680 // for a playing movie that is invalidating a lot).
681 //
682 // In the plugin movie case, hopefully the similar call to
683 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
684 // painting, because that avoids copying the plugin image to a different
685 // paint rect. Unfortunately, if anything on the page is animating other
686 // than the movie, it break this optimization since the union of the
687 // invalid regions will be larger than the plugin.
688 //
689 // This code optimizes that case, where we can still avoid painting in
690 // WebKit and filling the background (which can be slow) and just painting
691 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
692 // required.
[email protected]fef5e3972012-08-07 03:59:47693 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]719b36f2010-12-22 20:36:46694 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27695 optimized_copy_location, rect);
[email protected]fef5e3972012-08-07 03:59:47696 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
697 if (!is_accelerated_compositing_active_)
698 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46699 } else {
700 // Normal painting case.
[email protected]fef5e3972012-08-07 03:59:47701 base::TimeTicks paint_begin_ticks = base::TimeTicks::Now();
[email protected]719b36f2010-12-22 20:36:46702 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]fef5e3972012-08-07 03:59:47703 base::TimeDelta paint_time = base::TimeTicks::Now() - paint_begin_ticks;
704 if (!is_accelerated_compositing_active_)
705 software_stats_.totalPaintTimeInSeconds += paint_time.InSecondsF();
[email protected]719b36f2010-12-22 20:36:46706
707 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35708 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46709 }
initial.commit09911bf2008-07-26 23:55:29710
[email protected]4fb66842009-12-04 21:41:00711 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00712 canvas->restore();
713}
714
715void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
716 skia::PlatformCanvas* canvas) {
717 static bool kPaintBorder =
718 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
719 if (!kPaintBorder)
720 return;
721
[email protected]53d3f302009-12-21 04:42:05722 // Cycle through these colors to help distinguish new paint rects.
723 const SkColor colors[] = {
724 SkColorSetARGB(0x3F, 0xFF, 0, 0),
725 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
726 SkColorSetARGB(0x3F, 0, 0, 0xFF),
727 };
728 static int color_selector = 0;
729
[email protected]4fb66842009-12-04 21:41:00730 SkPaint paint;
731 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05732 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00733 paint.setStrokeWidth(1);
734
735 SkIRect irect;
736 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
737 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29738}
739
[email protected]52ccd0ea2011-02-16 01:09:05740void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30741 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30742 if (!animation_update_pending_) {
743 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59744 return;
[email protected]921244e42011-07-20 16:36:30745 }
[email protected]bd37ae252011-06-03 01:28:18746 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59747 // Record when we fired (according to base::Time::Now()) relative to when
748 // we posted the task to quantify how much the base::Time/base::TimeTicks
749 // skew is affecting animations.
750 base::TimeDelta animation_callback_delay = base::Time::Now() -
751 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
752 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
753 animation_callback_delay,
754 base::TimeDelta::FromMilliseconds(0),
755 base::TimeDelta::FromMilliseconds(30),
756 25);
757 }
[email protected]65225772011-05-12 21:10:24758 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24759}
760
[email protected]52ccd0ea2011-02-16 01:09:05761void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59762 if (!animation_update_pending_)
763 return;
[email protected]bd37ae252011-06-03 01:28:18764
765 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33766 base::TimeDelta animationInterval = IsRenderingVSynced() ?
767 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18768
[email protected]7c4329e2011-02-18 22:02:59769 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45770
771 // animation_floor_time_ is the earliest time that we should animate when
772 // using the dead reckoning software scheduler. If we're using swapbuffers
773 // complete callbacks to rate limit, we can ignore this floor.
774 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30775 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33776 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18777 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59778 // running animation callbacks so that if a callback requests another
779 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38780 animation_timer_.Stop();
781 animation_timer_.Start(FROM_HERE, animationInterval, this,
782 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59783 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30784 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59785 return;
[email protected]5f8b1022011-01-21 23:34:50786 }
[email protected]bd37ae252011-06-03 01:28:18787 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38788 if (!animation_timer_.IsRunning()) {
789 // This code uses base::Time::Now() to calculate the floor and next fire
790 // time because javascript's Date object uses base::Time::Now(). The
791 // message loop uses base::TimeTicks, which on windows can have a
792 // different granularity than base::Time.
793 // The upshot of all this is that this function might be called before
794 // base::Time::Now() has advanced past the animation_floor_time_. To
795 // avoid exposing this delay to javascript, we keep posting delayed
796 // tasks until base::Time::Now() has advanced far enough.
797 base::TimeDelta delay = animation_floor_time_ - now;
798 animation_timer_.Start(FROM_HERE, delay, this,
799 &RenderWidget::AnimationCallback);
800 }
[email protected]5f8b1022011-01-21 23:34:50801}
802
[email protected]bd37ae252011-06-03 01:28:18803bool RenderWidget::IsRenderingVSynced() {
804 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
805 // not caught by this check. This will lead to artificially low frame rates
806 // for people who force vsync off at a driver level and expect Chrome to speed
807 // up.
808 return !has_disable_gpu_vsync_switch_;
809}
810
[email protected]65225772011-05-12 21:10:24811void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06812 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24813 invalidation_task_posted_ = false;
814 DoDeferredUpdateAndSendInputAck();
815}
816
817void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05818 DoDeferredUpdate();
819
820 if (pending_input_event_ack_.get())
821 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21822}
823
[email protected]552e6002009-11-19 05:24:57824void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58825 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08826
[email protected]65225772011-05-12 21:10:24827 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29828 return;
[email protected]05a980d7a2012-02-07 22:16:42829
830 if (!host_window_set_) {
831 TRACE_EVENT0("renderer", "EarlyOut_NoHostWindow");
832 return;
833 }
[email protected]aa4117f2011-12-09 22:19:21834 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24835 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
836 return;
837 }
[email protected]9ca84622011-06-02 23:46:39838 if (is_accelerated_compositing_active_ &&
839 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24840 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
841 return;
842 }
initial.commit09911bf2008-07-26 23:55:29843
[email protected]552e6002009-11-19 05:24:57844 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29845 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57846 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29847 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24848 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29849 return;
850 }
851
[email protected]05a980d7a2012-02-07 22:16:42852 if (is_accelerated_compositing_active_)
853 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
854
[email protected]0fb93f52011-05-18 23:13:56855 // Tracking of frame rate jitter
856 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]541dcd52012-03-15 15:57:51857 webwidget_->instrumentBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:05858 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50859
[email protected]f98d7e3c2010-09-13 22:30:46860 // Layout may generate more invalidation. It may also enable the
861 // GPU acceleration, so make sure to run layout before we send the
862 // GpuRenderingActivated message.
863 webwidget_->layout();
864
[email protected]dcca3aa92012-02-17 23:03:37865 // The following two can result in further layout and possibly
866 // enable GPU acceleration so they need to be called before any painting
867 // is done.
868 UpdateTextInputState();
869 UpdateSelectionBounds();
870
[email protected]5f8b1022011-01-21 23:34:50871 // Suppress painting if nothing is dirty. This has to be done after updating
872 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24873 if (!paint_aggregator_.HasPendingUpdate()) {
874 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]541dcd52012-03-15 15:57:51875 webwidget_->instrumentCancelFrame();
[email protected]5f8b1022011-01-21 23:34:50876 return;
[email protected]65225772011-05-12 21:10:24877 }
[email protected]5f8b1022011-01-21 23:34:50878
[email protected]872ae5b2011-05-26 20:20:50879 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56880 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:04881 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:56882 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
883 delay,
884 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41885 base::TimeDelta::FromMilliseconds(120),
886 60);
[email protected]d0be63772011-12-20 23:18:04887 } else {
[email protected]0fb93f52011-05-18 23:13:56888 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
889 delay,
890 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:41891 base::TimeDelta::FromMilliseconds(120),
892 60);
[email protected]d0be63772011-12-20 23:18:04893 }
[email protected]872ae5b2011-05-26 20:20:50894
895 // Calculate filtered time per frame:
896 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
897 filtered_time_per_frame_ =
898 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56899 }
900 last_do_deferred_update_time_ = frame_begin_ticks;
901
[email protected]fef5e3972012-08-07 03:59:47902 if (!is_accelerated_compositing_active_) {
903 software_stats_.numAnimationFrames++;
904 software_stats_.numFramesSentToScreen++;
905 }
906
[email protected]552e6002009-11-19 05:24:57907 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29908 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30909 PaintAggregator::PendingUpdate update;
910 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29911
[email protected]53d3f302009-12-21 04:42:05912 gfx::Rect scroll_damage = update.GetScrollDamage();
913 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29914
[email protected]29ed96a2012-02-04 18:12:16915 // Notify derived classes that we're about to initiate a paint.
916 WillInitiatePaint();
917
[email protected]ca4847f2010-09-24 05:39:15918 // A plugin may be able to do an optimized paint. First check this, in which
919 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46920 // This optimization allows PPAPI plugins that declare themselves on top of
921 // the page (like a traditional windowed plugin) to be able to animate (think
922 // movie playing) without repeatedly re-painting the page underneath, or
923 // copying the plugin backing store (since we can send the plugin's backing
924 // store directly to the browser).
925 //
926 // This optimization only works when the entire invalid region is contained
927 // within the plugin. There is a related optimization in PaintRect for the
928 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15929 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15930 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]aa4117f2011-12-09 22:19:21931 DCHECK(!pending_update_params_.get());
932 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
933 pending_update_params_->dx = update.scroll_delta.x();
934 pending_update_params_->dy = update.scroll_delta.y();
935 pending_update_params_->scroll_rect = update.scroll_rect;
936 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:21937 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
938 pending_update_params_->flags = next_paint_flags_;
939 pending_update_params_->scroll_offset = GetScrollOffset();
940 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:09941 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:21942 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:09943 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:21944
[email protected]ca4847f2010-09-24 05:39:15945 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56946 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15947 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
948 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27949 // Only update the part of the plugin that actually changed.
950 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21951 pending_update_params_->bitmap = dib->id();
952 pending_update_params_->bitmap_rect = optimized_copy_location;
953 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]a79d8a632010-11-18 22:35:56954 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46955 // Compute a buffer for painting and cache it.
[email protected]f1cccb32012-06-06 18:29:59956 gfx::Rect pixel_bounds = bounds.Scale(device_scale_factor_);
[email protected]ca4847f2010-09-24 05:39:15957 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35958 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:59959 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:46960 if (!canvas.get()) {
961 NOTREACHED();
962 return;
963 }
[email protected]cef3362f2009-12-21 17:48:45964
[email protected]f98d7e3c2010-09-13 22:30:46965 // We may get back a smaller canvas than we asked for.
966 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:59967 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
968 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
969 pixel_bounds.set_width(canvas->getDevice()->width());
970 pixel_bounds.set_height(canvas->getDevice()->height());
971 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
972 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:05973
[email protected]f98d7e3c2010-09-13 22:30:46974 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
975
[email protected]aa4117f2011-12-09 22:19:21976 pending_update_params_->bitmap = current_paint_buf_->id();
977 pending_update_params_->bitmap_rect = bounds;
978
979 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:46980 // The scroll damage is just another rectangle to paint and copy.
981 copy_rects.swap(update.paint_rects);
982 if (!scroll_damage.IsEmpty())
983 copy_rects.push_back(scroll_damage);
984
985 for (size_t i = 0; i < copy_rects.size(); ++i)
[email protected]f1cccb32012-06-06 18:29:59986 PaintRect(copy_rects[i], pixel_bounds.origin(), canvas.get());
[email protected]60a50072012-01-11 02:05:35987
988 // Software FPS tick for performance tests. The accelerated path traces the
989 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
990 // NOTE: Tests may break if this event is renamed or moved.
991 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:46992 } else { // Accelerated compositing path
993 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:21994 // If painting is done via the gpu process then we don't set any damage
995 // rects to save the browser process from doing unecessary work.
996 pending_update_params_->bitmap_rect = bounds;
997 pending_update_params_->scroll_rect = gfx::Rect();
998 // We don't need an ack, because we're not sharing a DIB with the browser.
999 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1000 // with the browser for the GPU surface.
1001 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:421002 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:461003 }
1004
[email protected]936c6f52011-12-13 01:35:261005 // If we're holding a pending input event ACK, send the ACK before sending the
1006 // UpdateReply message so we can receive another input event before the
1007 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1008 // the UpdateRect IPC message handler.
1009 if (pending_input_event_ack_.get())
1010 Send(pending_input_event_ack_.release());
1011
[email protected]aa4117f2011-12-09 22:19:211012 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
1013 // OnSwapBuffersPosted), meaning a message has been added to the
1014 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1015 // the message now.
1016 if (pending_update_params_.get()) {
1017 // sending an ack to browser process that the paint is complete...
1018 update_reply_pending_ = pending_update_params_->needs_ack;
1019 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1020 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341021 }
[email protected]53d3f302009-12-21 04:42:051022
[email protected]29ed96a2012-02-04 18:12:161023 // If we're software rendering then we're done initiating the paint.
1024 if (!is_accelerated_compositing_active_)
1025 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291026}
1027
1028///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461029// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291030
[email protected]4873c7d2009-07-16 06:36:281031void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571032 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481033 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571034 gfx::Rect damaged_rect = view_rect.Intersect(rect);
1035 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291036 return;
1037
[email protected]552e6002009-11-19 05:24:571038 paint_aggregator_.InvalidateRect(damaged_rect);
1039
1040 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241041 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571042 return;
1043 if (!paint_aggregator_.HasPendingUpdate())
1044 return;
[email protected]aa4117f2011-12-09 22:19:211045 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241046 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1047 return;
1048
1049 // When GPU rendering, combine pending animations and invalidations into
1050 // a single update.
[email protected]816edc62012-03-17 01:27:221051 if (is_accelerated_compositing_active_ &&
1052 animation_update_pending_ &&
1053 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571054 return;
1055
1056 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291057 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1058 // on the call stack.
1059 // 2) Allows us to collect more damage rects before painting to help coalesce
1060 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241061 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211062 MessageLoop::current()->PostTask(
1063 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291064}
1065
[email protected]4873c7d2009-07-16 06:36:281066void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461067 // Drop scrolls on the floor when we are in compositing mode.
1068 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561069 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461070 return;
1071
[email protected]552e6002009-11-19 05:24:571072 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481073 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:571074 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
1075 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291076 return;
1077
[email protected]552e6002009-11-19 05:24:571078 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
1079
1080 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241081 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571082 return;
1083 if (!paint_aggregator_.HasPendingUpdate())
1084 return;
[email protected]aa4117f2011-12-09 22:19:211085 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241086 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1087 return;
1088
1089 // When GPU rendering, combine pending animations and invalidations into
1090 // a single update.
[email protected]816edc62012-03-17 01:27:221091 if (is_accelerated_compositing_active_ &&
1092 animation_update_pending_ &&
1093 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571094 return;
1095
1096 // Perform updating asynchronously. This serves two purposes:
1097 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1098 // on the call stack.
1099 // 2) Allows us to collect more damage rects before painting to help coalesce
1100 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241101 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211102 MessageLoop::current()->PostTask(
1103 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291104}
1105
[email protected]244ac1892011-12-02 17:04:471106void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091107 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1108 size_ = new_size;
1109 need_update_rect_for_auto_resize_ = true;
1110 }
[email protected]244ac1892011-12-02 17:04:471111}
1112
[email protected]91acd1c2012-03-14 08:32:391113void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221114 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1115
[email protected]c63b4d42012-04-26 01:01:071116#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211117 if (!is_accelerated_compositing_active_) {
1118 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1119 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1120 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1121 // going to switch to accelerated compositing, the GPU process may need
1122 // round-trips to the browser's UI thread before finishing the frame,
1123 // causing deadlocks if we delay the UpdateRect until we receive the
1124 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071125 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1126 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211127 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1128 }
[email protected]c63b4d42012-04-26 01:01:071129#endif
[email protected]aa4117f2011-12-09 22:19:211130
[email protected]ea162f92011-10-04 23:08:221131 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421132 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241133 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221134}
1135
1136void RenderWidget::didDeactivateCompositor() {
1137 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1138
1139 is_accelerated_compositing_active_ = false;
1140 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1141 routing_id_, is_accelerated_compositing_active_));
1142
[email protected]ea162f92011-10-04 23:08:221143 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241144 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561145}
1146
[email protected]9cd43a62012-03-26 08:03:561147void RenderWidget::willBeginCompositorFrame() {
1148 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371149
1150 DCHECK(RenderThreadImpl::current()->compositor_thread());
1151
1152 // The following two can result in further layout and possibly
1153 // enable GPU acceleration so they need to be called before any painting
1154 // is done.
1155 UpdateTextInputState();
1156 UpdateSelectionBounds();
1157
[email protected]9cd43a62012-03-26 08:03:561158 WillInitiatePaint();
1159}
1160
[email protected]3391a0772012-03-28 00:32:071161void RenderWidget::didBecomeReadyForAdditionalInput() {
1162 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1163 if (pending_input_event_ack_.get())
1164 Send(pending_input_event_ack_.release());
1165}
1166
[email protected]58264a32011-11-17 23:36:151167void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501168 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351169 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1170 // NOTE: Tests may break if this event is renamed or moved.
1171 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161172 // Notify subclasses that we initiated the paint operation.
1173 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151174}
1175
1176void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181177 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1178
1179 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561180 DidFlushPaint();
1181
[email protected]aa4117f2011-12-09 22:19:211182 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151183 return;
1184
[email protected]ea3ee0a2012-05-15 03:43:091185 if (!next_paint_flags_ &&
1186 !need_update_rect_for_auto_resize_ &&
1187 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151188 return;
[email protected]ea3ee0a2012-05-15 03:43:091189 }
[email protected]58264a32011-11-17 23:36:151190
1191 ViewHostMsg_UpdateRect_Params params;
1192 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151193 params.plugin_window_moves.swap(plugin_window_moves_);
1194 params.flags = next_paint_flags_;
1195 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121196 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091197 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151198
1199 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1200 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091201 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151202}
1203
[email protected]f98d7e3c2010-09-13 22:30:461204void RenderWidget::scheduleComposite() {
[email protected]d0be63772011-12-20 23:18:041205 if (WebWidgetHandlesCompositorScheduling()) {
[email protected]c3d45532011-10-07 19:20:401206 webwidget_->composite(false);
[email protected]d0be63772011-12-20 23:18:041207 } else {
[email protected]c3d45532011-10-07 19:20:401208 // TODO(nduca): replace with something a little less hacky. The reason this
1209 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1210 // contains a lot of host-renderer synchronization logic that is still
1211 // important for the accelerated compositing case. The option of simply
1212 // duplicating all that code is less desirable than "faking out" the
1213 // invalidation path using a magical damage rect.
1214 didInvalidateRect(WebRect(0, 0, 1, 1));
1215 }
[email protected]f98d7e3c2010-09-13 22:30:461216}
1217
[email protected]5f8b1022011-01-21 23:34:501218void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201219 if (animation_update_pending_)
1220 return;
1221
[email protected]921244e42011-07-20 16:36:301222 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201223 animation_update_pending_ = true;
1224 if (!animation_timer_.IsRunning()) {
1225 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1226 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211227 }
[email protected]5f8b1022011-01-21 23:34:501228}
1229
[email protected]4873c7d2009-07-16 06:36:281230void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301231 // TODO(darin): Eliminate this temporary.
1232 WebCursor cursor(cursor_info);
1233
initial.commit09911bf2008-07-26 23:55:291234 // Only send a SetCursor message if we need to make a change.
1235 if (!current_cursor_.IsEqual(cursor)) {
1236 current_cursor_ = cursor;
1237 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1238 }
1239}
1240
1241// We are supposed to get a single call to Show for a newly created RenderWidget
1242// that was created via RenderWidget::CreateWebView. So, we wait until this
1243// point to dispatch the ShowWidget message.
1244//
1245// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281246// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291247//
[email protected]4873c7d2009-07-16 06:36:281248void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291249 DCHECK(!did_show_) << "received extraneous Show call";
1250 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1251 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1252
[email protected]8de12d942010-11-17 20:42:441253 if (did_show_)
1254 return;
1255
1256 did_show_ = true;
1257 // NOTE: initial_pos_ may still have its default values at this point, but
1258 // that's okay. It'll be ignored if as_popup is false, or the browser
1259 // process will impose a default position otherwise.
1260 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1261 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291262}
1263
[email protected]4873c7d2009-07-16 06:36:281264void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291265}
1266
[email protected]4873c7d2009-07-16 06:36:281267void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291268}
1269
[email protected]2533ce12009-05-09 00:02:241270void RenderWidget::DoDeferredClose() {
1271 Send(new ViewHostMsg_Close(routing_id_));
1272}
1273
[email protected]4873c7d2009-07-16 06:36:281274void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321275 if (is_swapped_out_) {
1276 // This widget is currently swapped out, and the active widget is in a
1277 // different process. Have the browser route the close request to the
1278 // active widget instead, so that the correct unload handlers are run.
1279 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1280 return;
1281 }
1282
initial.commit09911bf2008-07-26 23:55:291283 // If a page calls window.close() twice, we'll end up here twice, but that's
1284 // OK. It is safe to send multiple Close messages.
1285
[email protected]2533ce12009-05-09 00:02:241286 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1287 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1288 // could be closed before the JS finishes executing. So instead, post a
1289 // message back to the message loop, which won't run until the JS is
1290 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211291 MessageLoop::current()->PostTask(
1292 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291293}
1294
1295void RenderWidget::Close() {
1296 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281297 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291298 webwidget_ = NULL;
1299 }
1300}
1301
[email protected]4873c7d2009-07-16 06:36:281302WebRect RenderWidget::windowRect() {
1303 if (pending_window_rect_count_)
1304 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241305
[email protected]b3f2b912009-04-09 16:18:521306 gfx::Rect rect;
1307 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281308 return rect;
initial.commit09911bf2008-07-26 23:55:291309}
1310
[email protected]8a9d6ca32011-06-06 20:11:301311void RenderWidget::setToolTipText(const WebKit::WebString& text,
1312 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541313 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301314}
1315
[email protected]4873c7d2009-07-16 06:36:281316void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291317 if (did_show_) {
1318 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241319 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291320 } else {
1321 initial_pos_ = pos;
1322 }
1323}
1324
[email protected]2533ce12009-05-09 00:02:241325void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1326 pending_window_rect_ = rect;
1327 pending_window_rect_count_++;
1328}
1329
[email protected]4873c7d2009-07-16 06:36:281330WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241331 if (pending_window_rect_count_) {
1332 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1333 // the RootWindowRect is probably going to return wrong results since the
1334 // browser may not have processed the Move yet. There isn't really anything
1335 // good to do in this case, and it shouldn't happen - since this size is
1336 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281337 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241338 }
1339
[email protected]b3f2b912009-04-09 16:18:521340 gfx::Rect rect;
1341 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281342 return rect;
[email protected]d4547452008-08-28 18:36:371343}
1344
[email protected]4873c7d2009-07-16 06:36:281345WebRect RenderWidget::windowResizerRect() {
1346 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191347}
1348
[email protected]fa7b1dc2010-06-23 17:53:041349void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031350 // To prevent this renderer process from sending unnecessary IPC messages to
1351 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041352 // only during the input method attached to the browser process is active.
1353 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291354}
1355
[email protected]58b48a0d2012-06-13 07:01:351356void RenderWidget::UpdateCompositionInfo(
1357 const ui::Range& range,
1358 const std::vector<gfx::Rect>& character_bounds) {
1359 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1360 return;
1361 composition_character_bounds_ = character_bounds;
1362 composition_range_ = range;
1363 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1364 routing_id(), composition_range_, composition_character_bounds_));
1365}
1366
[email protected]fa7b1dc2010-06-23 17:53:041367void RenderWidget::OnImeSetComposition(
1368 const string16& text,
1369 const std::vector<WebCompositionUnderline>& underlines,
1370 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281371 if (!webwidget_)
1372 return;
[email protected]d4cff272011-05-02 15:46:011373 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041374 text, WebVector<WebCompositionUnderline>(underlines),
1375 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011376 // Setting the IME composition was successful. Send the new composition
1377 // range to the browser.
1378 ui::Range range(ui::Range::InvalidRange());
1379 size_t location, length;
1380 if (webwidget_->compositionRange(&location, &length)) {
1381 range.set_start(location);
1382 range.set_end(location + length);
1383 }
1384 // The IME was cancelled via the Esc key, so just send back the caret.
1385 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1386 range.set_start(location);
1387 range.set_end(location + length);
1388 }
[email protected]58b48a0d2012-06-13 07:01:351389 std::vector<gfx::Rect> character_bounds;
1390 GetCompositionCharacterBounds(&character_bounds);
1391 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011392 } else {
[email protected]fa7b1dc2010-06-23 17:53:041393 // If we failed to set the composition text, then we need to let the browser
1394 // process to cancel the input method's ongoing composition session, to make
1395 // sure we are in a consistent state.
1396 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011397
1398 // Send an updated IME range with just the caret range.
1399 ui::Range range(ui::Range::InvalidRange());
1400 size_t location, length;
1401 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1402 range.set_start(location);
1403 range.set_end(location + length);
1404 }
[email protected]58b48a0d2012-06-13 07:01:351405 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261406 }
[email protected]fa7b1dc2010-06-23 17:53:041407}
1408
[email protected]4de6d1692011-10-12 08:45:441409void RenderWidget::OnImeConfirmComposition(
1410 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041411 if (!webwidget_)
1412 return;
1413
1414 handling_input_event_ = true;
1415 webwidget_->confirmComposition(text);
1416 handling_input_event_ = false;
1417
[email protected]d4cff272011-05-02 15:46:011418 // Send an updated IME range with just the caret range.
1419 ui::Range range(ui::Range::InvalidRange());
1420 size_t location, length;
1421 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1422 range.set_start(location);
1423 range.set_end(location + length);
1424 }
[email protected]58b48a0d2012-06-13 07:01:351425 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
initial.commit09911bf2008-07-26 23:55:291426}
1427
[email protected]948f7ab72010-05-28 23:48:081428// This message causes the renderer to render an image of the
1429// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491430void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091431 int tag,
[email protected]948f7ab72010-05-28 23:48:081432 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491433 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001434 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1435 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251436 // Close our unused handle.
1437#if defined(OS_WIN)
1438 ::CloseHandle(dib_handle);
1439#elif defined(OS_MACOSX)
1440 base::SharedMemory::CloseHandle(dib_handle);
1441#endif
1442 }
[email protected]d65adb12010-04-28 17:26:491443 return;
[email protected]45c6aad32010-11-11 04:46:251444 }
[email protected]d65adb12010-04-28 17:26:491445
[email protected]948f7ab72010-05-28 23:48:081446 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491447 // If one of these is empty, then we just return the dib we were
1448 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091449 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491450 return;
1451 }
1452
1453 // Map the given DIB ID into this process, and unmap it at the end
1454 // of this function.
[email protected]45c6aad32010-11-11 04:46:251455 scoped_ptr<TransportDIB> paint_at_size_buffer(
1456 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301457
[email protected]8f640512012-08-07 23:52:511458 gfx::Size page_size_in_pixel = page_size.Scale(device_scale_factor_);
1459 gfx::Size desired_size_in_pixel = desired_size.Scale(device_scale_factor_);
1460 gfx::Size canvas_size = page_size_in_pixel;
1461 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491462 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511463 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491464 static_cast<float>(canvas_size.height());
1465
[email protected]ee8d6fd2010-05-26 17:05:481466 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491467 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1468 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481469 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491470
[email protected]36808ad2010-10-20 19:18:301471 scoped_ptr<skia::PlatformCanvas> canvas(
1472 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1473 canvas_size.height()));
1474 if (!canvas.get()) {
1475 NOTREACHED();
1476 return;
1477 }
1478
[email protected]d65adb12010-04-28 17:26:491479 // Reset bounds to what we actually received, but they should be the
1480 // same.
1481 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1482 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1483 bounds.set_width(canvas->getDevice()->width());
1484 bounds.set_height(canvas->getDevice()->height());
1485
1486 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081487 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491488 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1489
[email protected]948f7ab72010-05-28 23:48:081490 // Have to make sure we're laid out at the right size before
1491 // rendering.
1492 gfx::Size old_size = webwidget_->size();
1493 webwidget_->resize(page_size);
1494 webwidget_->layout();
1495
[email protected]d65adb12010-04-28 17:26:491496 // Paint the entire thing (using original bounds, not scaled bounds).
1497 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1498 canvas->restore();
1499
[email protected]948f7ab72010-05-28 23:48:081500 // Return the widget to its previous size.
1501 webwidget_->resize(old_size);
1502
[email protected]c88c9442010-07-19 18:55:091503 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491504}
1505
[email protected]ec7dc112008-08-06 05:30:121506void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1507 // During shutdown we can just ignore this message.
1508 if (!webwidget_)
1509 return;
1510
1511 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561512 if (is_accelerated_compositing_active_) {
[email protected]8c49fa982012-02-10 14:37:041513 webwidget_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461514 scheduleComposite();
1515 } else {
1516 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1517 didInvalidateRect(repaint_rect);
1518 }
[email protected]ec7dc112008-08-06 05:30:121519}
1520
[email protected]fea38fc2012-06-13 17:38:371521void RenderWidget::OnSetDeviceScaleFactor(float device_scale_factor) {
[email protected]dea5e7682012-06-14 05:21:241522 if (device_scale_factor_ == device_scale_factor)
1523 return;
1524
1525 device_scale_factor_ = device_scale_factor;
1526
1527 if (!is_accelerated_compositing_active_) {
1528 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1529 } else {
1530 scheduleComposite();
1531 }
[email protected]fea38fc2012-06-13 17:38:371532}
1533
[email protected]4873c7d2009-07-16 06:36:281534void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111535 if (!webwidget_)
1536 return;
[email protected]4873c7d2009-07-16 06:36:281537 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111538}
1539
[email protected]6131a642012-06-15 23:26:531540void RenderWidget::OnScreenInfoChanged(
1541 const WebKit::WebScreenInfo& screen_info) {
1542 screen_info_ = screen_info;
1543}
1544
[email protected]719b36f2010-12-22 20:36:461545webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151546 const gfx::Rect& paint_bounds,
1547 TransportDIB** dib,
1548 gfx::Rect* location,
1549 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461550 // Bare RenderWidgets don't support optimized plugin painting.
1551 return NULL;
[email protected]ca4847f2010-09-24 05:39:151552}
1553
[email protected]bcaf2272011-02-15 15:29:431554gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521555 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431556 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521557}
1558
[email protected]bee16aab2009-08-26 15:55:031559void RenderWidget::SetHidden(bool hidden) {
1560 if (is_hidden_ == hidden)
1561 return;
1562
1563 // The status has changed. Tell the RenderThread about it.
1564 is_hidden_ = hidden;
1565 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271566 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031567 else
[email protected]380244092011-10-07 17:26:271568 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031569}
1570
[email protected]2b624c562011-10-27 22:58:261571void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261572 if (!webwidget_)
1573 return;
1574
1575 if (is_fullscreen_) {
1576 webwidget_->willExitFullScreen();
1577 } else {
1578 webwidget_->willEnterFullScreen();
1579 }
[email protected]2b624c562011-10-27 22:58:261580}
1581
1582void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261583 if (!webwidget_)
1584 return;
1585
1586 if (is_fullscreen_) {
1587 webwidget_->didEnterFullScreen();
1588 } else {
1589 webwidget_->didExitFullScreen();
1590 }
[email protected]2b624c562011-10-27 22:58:261591}
1592
[email protected]699ab0d2009-04-23 23:19:141593void RenderWidget::SetBackground(const SkBitmap& background) {
1594 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461595
[email protected]699ab0d2009-04-23 23:19:141596 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281597 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141598}
1599
[email protected]674741932009-02-04 23:44:461600bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051601 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461602}
1603
1604bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051605 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461606}
1607
1608void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051609 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461610}
1611
1612void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051613 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461614}
1615
1616void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051617 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461618}
1619
[email protected]e99ef6f2011-10-16 01:13:001620void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041621 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291622 return;
[email protected]fa7b1dc2010-06-23 17:53:041623
[email protected]ad26ef42011-06-17 07:59:451624 ui::TextInputType new_type = GetTextInputType();
1625 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001626 // Only sends text input type and compose inline to the browser process if
1627 // they are changed.
1628 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451629 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041630 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451631 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001632 Send(new ViewHostMsg_TextInputStateChanged(
1633 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291634 }
initial.commit09911bf2008-07-26 23:55:291635}
1636
[email protected]3f783362011-10-21 22:40:501637void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1638 WebRect start_webrect;
1639 WebRect end_webrect;
1640 webwidget_->selectionBounds(start_webrect, end_webrect);
1641 *start = start_webrect;
1642 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321643}
1644
[email protected]e99ef6f2011-10-16 01:13:001645void RenderWidget::UpdateSelectionBounds() {
1646 if (!webwidget_)
1647 return;
1648
[email protected]3f783362011-10-21 22:40:501649 gfx::Rect start_rect;
1650 gfx::Rect end_rect;
1651 GetSelectionBounds(&start_rect, &end_rect);
[email protected]58b48a0d2012-06-13 07:01:351652 if (selection_start_rect_ != start_rect || selection_end_rect_ != end_rect) {
1653 selection_start_rect_ = start_rect;
1654 selection_end_rect_ = end_rect;
1655 Send(new ViewHostMsg_SelectionBoundsChanged(
1656 routing_id_, selection_start_rect_, selection_end_rect_));
1657 }
[email protected]e99ef6f2011-10-16 01:13:001658
[email protected]58b48a0d2012-06-13 07:01:351659 std::vector<gfx::Rect> character_bounds;
1660 GetCompositionCharacterBounds(&character_bounds);
1661 UpdateCompositionInfo(composition_range_, character_bounds);
1662}
1663
1664bool RenderWidget::ShouldUpdateCompositionInfo(
1665 const ui::Range& range,
1666 const std::vector<gfx::Rect>& bounds) {
1667 if (composition_range_ != range)
1668 return true;
1669 if (bounds.size() != composition_character_bounds_.size())
1670 return true;
1671 for (size_t i = 0; i < bounds.size(); ++i) {
1672 if (bounds[i] != composition_character_bounds_[i])
1673 return true;
1674 }
1675 return false;
[email protected]e99ef6f2011-10-16 01:13:001676}
1677
[email protected]73bf95812011-10-12 11:38:321678// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451679COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1680 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1681COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1682 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1683COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1684 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181685COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1686 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1687COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1688 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1689COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1690 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1691COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1692 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1693COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1694 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001695COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1696 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1697COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1698 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1699COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1700 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1701COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1702 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1703COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1704 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1705COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1706 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]ad26ef42011-06-17 07:59:451707
1708ui::TextInputType RenderWidget::GetTextInputType() {
1709 if (webwidget_) {
1710 int type = webwidget_->textInputType();
1711 // Check the type is in the range representable by ui::TextInputType.
[email protected]65de3d92012-07-21 01:54:191712 DCHECK_LE(type, ui::TEXT_INPUT_TYPE_MAX) <<
[email protected]ad26ef42011-06-17 07:59:451713 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1714 return static_cast<ui::TextInputType>(type);
1715 }
1716 return ui::TEXT_INPUT_TYPE_NONE;
1717}
1718
[email protected]58b48a0d2012-06-13 07:01:351719void RenderWidget::GetCompositionCharacterBounds(
1720 std::vector<gfx::Rect>* bounds) {
1721 DCHECK(bounds);
1722 bounds->clear();
1723}
1724
[email protected]ad26ef42011-06-17 07:59:451725bool RenderWidget::CanComposeInline() {
1726 return true;
[email protected]56ea1a62011-05-30 07:05:571727}
1728
[email protected]4873c7d2009-07-16 06:36:281729WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:041730 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:281731}
1732
[email protected]f660d9c2012-06-06 18:31:211733float RenderWidget::deviceScaleFactor() {
1734 return device_scale_factor_;
1735}
1736
[email protected]fa7b1dc2010-06-23 17:53:041737void RenderWidget::resetInputMethod() {
1738 if (!input_method_is_active_)
1739 return;
1740
1741 // If the last text input type is not None, then we should finish any
1742 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451743 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041744 // If a composition text exists, then we need to let the browser process
1745 // to cancel the input method's ongoing composition session.
1746 if (webwidget_->confirmComposition())
1747 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1748 }
[email protected]d4cff272011-05-02 15:46:011749
1750 // Send an updated IME range with the current caret rect.
1751 ui::Range range(ui::Range::InvalidRange());
1752 size_t location, length;
1753 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1754 range.set_start(location);
1755 range.set_end(location + length);
1756 }
[email protected]58b48a0d2012-06-13 07:01:351757
1758 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:041759}
1760
[email protected]f103ab72009-09-02 17:10:591761void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501762 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291763 size_t i = 0;
1764 for (; i < plugin_window_moves_.size(); ++i) {
1765 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581766 if (move.rects_valid) {
1767 plugin_window_moves_[i] = move;
1768 } else {
1769 plugin_window_moves_[i].visible = move.visible;
1770 }
initial.commit09911bf2008-07-26 23:55:291771 break;
1772 }
1773 }
1774
1775 if (i == plugin_window_moves_.size())
1776 plugin_window_moves_.push_back(move);
1777}
[email protected]268654772009-08-06 23:02:041778
1779void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1780 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1781 i != plugin_window_moves_.end(); ++i) {
1782 if (i->window == window) {
1783 plugin_window_moves_.erase(i);
1784 break;
1785 }
1786 }
1787}
[email protected]67bfb83f2011-09-22 03:36:371788
[email protected]fef5e3972012-08-07 03:59:471789void RenderWidget::GetRenderingStats(WebKit::WebRenderingStats& stats) const {
1790 webwidget()->renderingStats(stats);
1791 stats.numAnimationFrames += software_stats_.numAnimationFrames;
1792 stats.numFramesSentToScreen += software_stats_.numFramesSentToScreen;
1793 stats.totalPaintTimeInSeconds += software_stats_.totalPaintTimeInSeconds;
1794}
1795
[email protected]a39ca1652012-07-13 21:30:581796void RenderWidget::BeginSmoothScroll(bool down, bool scroll_far) {
1797 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, down, scroll_far));
1798}
1799
[email protected]67bfb83f2011-09-22 03:36:371800bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1801 return false;
1802}
[email protected]c3d45532011-10-07 19:20:401803
1804bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1805 return false;
1806}