blob: 6dcf933684ed64faa339c9b5b1ab365b692d7af3 [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]681ccff2013-03-18 06:13:5217#include "cc/base/switches.h"
18#include "cc/base/thread.h"
19#include "cc/base/thread_impl.h"
[email protected]7f0d825f2013-03-18 07:24:3020#include "cc/output/output_surface.h"
[email protected]556fd292013-03-18 08:03:0421#include "cc/trees/layer_tree_host.h"
[email protected]ed7defa2013-03-12 21:29:5922#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]992db4c2011-05-12 15:37:1523#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5024#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2925#include "content/public/common/content_switches.h"
[email protected]ed7defa2013-03-12 21:29:5926#include "content/renderer/gpu/compositor_output_surface.h"
27#include "content/renderer/gpu/compositor_software_output_device_gl_adapter.h"
[email protected]ea5f70a2013-03-07 12:30:3628#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5929#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2830#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]8704f89b2011-04-15 00:30:0531#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4432#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1933#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1834#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4835#include "skia/ext/platform_canvas.h"
[email protected]ed7defa2013-03-12 21:29:5936#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
[email protected]debc8e52013-01-15 03:43:1237#include "third_party/WebKit/Source/Platform/chromium/public/WebPoint.h"
38#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
39#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
40#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
[email protected]8bd0fe62011-01-17 06:44:3741#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]4b1146bc2012-07-10 18:46:0342#include "third_party/WebKit/Source/WebKit/chromium/public/WebHelperPlugin.h"
[email protected]a7547fb2012-03-08 04:43:4443#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3744#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
45#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0146#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3747#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4148#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1349#include "ui/base/ui_base_switches.h"
[email protected]a25e25b2012-09-28 14:32:3750#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3551#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4852#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2753#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4154#include "ui/surface/transport_dib.h"
[email protected]b63d58d2012-11-26 22:37:4455#include "webkit/compositor_bindings/web_rendering_stats_impl.h"
[email protected]8c89e7792009-08-19 21:18:3456#include "webkit/glue/webkit_glue.h"
[email protected]ed7defa2013-03-12 21:29:5957#include "webkit/gpu/webgraphicscontext3d_in_process_impl.h"
[email protected]191eb3f72010-12-21 06:27:5058#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4659#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4860
61#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4962#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5263#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4164#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4865#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4466
[email protected]8bd0fe62011-01-17 06:44:3767#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2968
[email protected]fa7b1dc2010-06-23 17:53:0469using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3070using WebKit::WebCursorInfo;
[email protected]41d86852012-11-07 12:23:2471using WebKit::WebGestureEvent;
[email protected]62cb33cae2009-03-27 23:30:2272using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0673using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2874using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4475using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0076using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2877using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5978using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1879using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0180using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5281using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1982using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5283using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2884using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2485using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0486using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1887using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2688
[email protected]6a4d7f62013-01-07 21:32:1389namespace {
90const char* GetEventName(WebInputEvent::Type type) {
91#define CASE_TYPE(t) case WebInputEvent::t: return #t
92 switch(type) {
93 CASE_TYPE(Undefined);
94 CASE_TYPE(MouseDown);
95 CASE_TYPE(MouseUp);
96 CASE_TYPE(MouseMove);
97 CASE_TYPE(MouseEnter);
98 CASE_TYPE(MouseLeave);
99 CASE_TYPE(ContextMenu);
100 CASE_TYPE(MouseWheel);
101 CASE_TYPE(RawKeyDown);
102 CASE_TYPE(KeyDown);
103 CASE_TYPE(KeyUp);
104 CASE_TYPE(Char);
105 CASE_TYPE(GestureScrollBegin);
106 CASE_TYPE(GestureScrollEnd);
107 CASE_TYPE(GestureScrollUpdate);
108 CASE_TYPE(GestureFlingStart);
109 CASE_TYPE(GestureFlingCancel);
110 CASE_TYPE(GestureTap);
111 CASE_TYPE(GestureTapDown);
112 CASE_TYPE(GestureTapCancel);
113 CASE_TYPE(GestureDoubleTap);
114 CASE_TYPE(GestureTwoFingerTap);
115 CASE_TYPE(GestureLongPress);
116 CASE_TYPE(GestureLongTap);
117 CASE_TYPE(GesturePinchBegin);
118 CASE_TYPE(GesturePinchEnd);
119 CASE_TYPE(GesturePinchUpdate);
120 CASE_TYPE(TouchStart);
121 CASE_TYPE(TouchMove);
122 CASE_TYPE(TouchEnd);
123 CASE_TYPE(TouchCancel);
[email protected]3ec08ed2013-01-11 15:59:57124 default:
125 // Must include default to let WebKit::WebInputEvent add new event types
126 // before they're added here.
127 DLOG(WARNING) << "Unhandled WebInputEvent type in GetEventName.\n";
128 break;
[email protected]6a4d7f62013-01-07 21:32:13129 }
130#undef CASE_TYPE
[email protected]3ec08ed2013-01-11 15:59:57131 return "";
[email protected]6a4d7f62013-01-07 21:32:13132}
133}
[email protected]e9ff79c2012-10-19 21:31:26134namespace content {
[email protected]62cb33cae2009-03-27 23:30:22135
[email protected]6fd35b72012-03-01 19:46:41136RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:04137 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:44138 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:29139 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56140 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11141 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29142 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30143 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35144 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:29145 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06146 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05147 update_reply_pending_(false),
[email protected]847a2582013-03-09 02:29:51148 auto_resize_mode_(false),
[email protected]ea3ee0a2012-05-15 03:43:09149 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24150 using_asynchronous_swapbuffers_(false),
151 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29152 did_show_(false),
initial.commit09911bf2008-07-26 23:55:29153 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:09154 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29155 needs_repainting_on_restore_(false),
156 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49157 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50158 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48159 closing_(false),
[email protected]14392a52012-05-02 20:28:44160 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04161 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45162 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
163 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12164 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48165 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12166 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50167 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21168 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07169 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04170 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52171 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]0e241b4b2012-08-18 09:06:27172 throttle_input_events_(true),
[email protected]cb6430932012-10-31 00:53:36173 next_smooth_scroll_gesture_id_(0),
[email protected]ed7defa2013-03-12 21:29:59174 is_threaded_compositing_enabled_(false),
175 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
[email protected]8b3f0eb2012-05-03 19:15:05176 if (!swapped_out)
177 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27178 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18179 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
180 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36181 is_threaded_compositing_enabled_ =
182 CommandLine::ForCurrentProcess()->HasSwitch(
183 switches::kEnableThreadedCompositing);
initial.commit09911bf2008-07-26 23:55:29184}
185
186RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11187 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21188 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35189 if (current_paint_buf_) {
190 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
191 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29192 }
[email protected]992db4c2011-05-12 15:37:15193 // If we are swapped out, we have released already.
194 if (!is_swapped_out_)
195 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29196}
197
[email protected]484955942010-08-19 16:13:18198// static
[email protected]8085dbc82008-09-26 22:53:44199RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04200 WebKit::WebPopupType popup_type,
201 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29202 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41203 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04204 new RenderWidget(popup_type, screen_info, false));
[email protected]a635f942012-12-07 10:34:29205 if (widget->Init(opener_id)) { // adds reference on success.
206 return widget;
207 }
208 return NULL;
initial.commit09911bf2008-07-26 23:55:29209}
210
[email protected]484955942010-08-19 16:13:18211// static
212WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
213 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03214 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18215 break;
216 case WebKit::WebPopupTypeSelect:
217 case WebKit::WebPopupTypeSuggestion:
218 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44219 case WebKit::WebPopupTypePage:
220 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03221 case WebKit::WebPopupTypeHelperPlugin:
222 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18223 default:
224 NOTREACHED();
225 }
226 return NULL;
227}
228
[email protected]a635f942012-12-07 10:34:29229bool RenderWidget::Init(int32 opener_id) {
230 return DoInit(opener_id,
231 RenderWidget::CreateWebWidget(this),
232 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
233 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18234}
235
[email protected]a635f942012-12-07 10:34:29236bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06237 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18238 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29239 DCHECK(!webwidget_);
240
241 if (opener_id != MSG_ROUTING_NONE)
242 opener_id_ = opener_id;
243
[email protected]484955942010-08-19 16:13:18244 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29245
[email protected]380244092011-10-07 17:26:27246 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29247 if (result) {
[email protected]380244092011-10-07 17:26:27248 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29249 // Take a reference on behalf of the RenderThread. This will be balanced
250 // when we receive ViewMsg_Close.
251 AddRef();
[email protected]a635f942012-12-07 10:34:29252 return true;
initial.commit09911bf2008-07-26 23:55:29253 } else {
[email protected]a635f942012-12-07 10:34:29254 // The above Send can fail when the tab is closing.
255 return false;
initial.commit09911bf2008-07-26 23:55:29256 }
257}
258
[email protected]fc4404d2012-11-07 19:53:30259// This is used to complete pending inits and non-pending inits.
260void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29261 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29262
[email protected]fc4404d2012-11-07 19:53:30263 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42264
[email protected]8926c602013-01-23 05:32:06265 if (webwidget_ && is_threaded_compositing_enabled_) {
266 webwidget_->enterForceCompositingMode(true);
267 }
[email protected]ba91a792013-02-06 09:48:28268 if (compositor_) {
269 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09270 }
[email protected]05a980d7a2012-02-07 22:16:42271 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29272
[email protected]6de74452009-02-25 18:04:59273 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29274}
275
[email protected]992db4c2011-05-12 15:37:15276void RenderWidget::SetSwappedOut(bool is_swapped_out) {
277 // We should only toggle between states.
278 DCHECK(is_swapped_out_ != is_swapped_out);
279 is_swapped_out_ = is_swapped_out;
280
281 // If we are swapping out, we will call ReleaseProcess, allowing the process
282 // to exit if all of its RenderViews are swapped out. We wait until the
283 // WasSwappedOut call to do this, to avoid showing the sad tab.
284 // If we are swapping in, we call AddRefProcess to prevent the process from
285 // exiting.
286 if (!is_swapped_out)
287 RenderProcess::current()->AddRefProcess();
288}
289
[email protected]34bb3ac2013-03-08 02:41:28290bool RenderWidget::AllowPartialSwap() const {
291 return true;
292}
293
[email protected]a95986a82010-12-24 06:19:28294bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
295 bool handled = true;
296 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
297 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
298 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
299 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54300 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28301 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41302 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15303 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28304 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59305 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
306 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28307 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
308 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
309 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
310 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
311 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
312 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25313 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
314 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
315 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted, OnSmoothScrollCompleted)
[email protected]a95986a82010-12-24 06:19:28316 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
317 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53318 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]80ad8622012-11-07 16:33:03319 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21320#if defined(OS_ANDROID)
321 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
[email protected]2384b6c2013-02-28 23:58:51322 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]105dffb42013-02-20 03:46:21323#endif
[email protected]a95986a82010-12-24 06:19:28324 IPC_MESSAGE_UNHANDLED(handled = false)
325 IPC_END_MESSAGE_MAP()
326 return handled;
327}
initial.commit09911bf2008-07-26 23:55:29328
329bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15330 // Don't send any messages after the browser has told us to close, and filter
331 // most outgoing messages while swapped out.
332 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26333 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11334 closing_) {
initial.commit09911bf2008-07-26 23:55:29335 delete message;
336 return false;
337 }
338
339 // If given a messsage without a routing ID, then assign our routing ID.
340 if (message->routing_id() == MSG_ROUTING_NONE)
341 message->set_routing_id(routing_id_);
342
[email protected]380244092011-10-07 17:26:27343 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44344}
345
[email protected]61e2b3cc2012-03-02 16:13:34346void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44347 const gfx::Size& physical_backing_size,
[email protected]61e2b3cc2012-03-02 16:13:34348 const gfx::Rect& resizer_rect,
349 bool is_fullscreen,
350 ResizeAck resize_ack) {
351 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
352 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
353 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29354
[email protected]61e2b3cc2012-03-02 16:13:34355 // Ignore this during shutdown.
356 if (!webwidget_)
357 return;
358
[email protected]60d47ac2013-03-01 23:42:44359 if (compositor_)
360 compositor_->setViewportSize(new_size, physical_backing_size);
361
[email protected]dade8992013-03-04 07:34:34362 physical_backing_size_ = physical_backing_size;
[email protected]61e2b3cc2012-03-02 16:13:34363 resizer_rect_ = resizer_rect;
364
365 // NOTE: We may have entered fullscreen mode without changing our size.
366 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
367 if (fullscreen_change)
368 WillToggleFullscreen();
369 is_fullscreen_ = is_fullscreen;
370
371 if (size_ != new_size) {
372 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34373 needs_repainting_on_restore_ = false;
374
375 size_ = new_size;
376
377 paint_aggregator_.ClearPendingUpdate();
378
379 // When resizing, we want to wait to paint before ACK'ing the resize. This
380 // ensures that we only resize as fast as we can paint. We only need to
381 // send an ACK if we are resized to a non-empty rect.
382 webwidget_->resize(new_size);
383 if (!new_size.IsEmpty()) {
384 if (!is_accelerated_compositing_active_) {
385 // Resize should have caused an invalidation of the entire view.
386 DCHECK(paint_aggregator_.HasPendingUpdate());
387 }
388
389 // Send the Resize_ACK flag once we paint again if requested.
390 if (resize_ack == SEND_RESIZE_ACK)
391 set_next_paint_is_resize_ack();
392 }
[email protected]ff475a322012-03-14 00:05:35393 } else {
394 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34395 }
396
397 if (fullscreen_change)
398 DidToggleFullscreen();
399
400 // If a resize ack is requested and it isn't set-up, then no more resizes will
401 // come in and in general things will go wrong.
402 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
403 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29404}
405
406void RenderWidget::OnClose() {
407 if (closing_)
408 return;
409 closing_ = true;
410
411 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03412 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27413 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03414 SetHidden(false);
415 }
initial.commit09911bf2008-07-26 23:55:29416
initial.commit09911bf2008-07-26 23:55:29417 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25418 // now. Post a task that only gets invoked when there are no nested message
419 // loops.
[email protected]32876ae2011-11-15 22:25:21420 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29421 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25422
423 // Balances the AddRef taken when we called AddRoute.
424 Release();
initial.commit09911bf2008-07-26 23:55:29425}
426
[email protected]61e2b3cc2012-03-02 16:13:34427// Got a response from the browser after the renderer decided to create a new
428// view.
[email protected]fc4404d2012-11-07 19:53:30429void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34430 DCHECK(routing_id_ != MSG_ROUTING_NONE);
431
[email protected]fc4404d2012-11-07 19:53:30432 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34433}
434
[email protected]f21c613a2009-02-12 14:46:17435void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44436 const gfx::Size& physical_backing_size,
[email protected]ee41e7d22011-10-14 19:34:09437 const gfx::Rect& resizer_rect,
438 bool is_fullscreen) {
[email protected]60d47ac2013-03-01 23:42:44439 Resize(new_size, physical_backing_size, resizer_rect, is_fullscreen,
440 SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29441}
442
[email protected]b5913d72012-02-07 22:26:54443void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
444 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59445 gfx::Rect view_rect(size_);
446
[email protected]ce112fe2012-10-29 22:52:18447 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59448 if (!old_damage_rect.IsEmpty())
449 paint_aggregator_.InvalidateRect(old_damage_rect);
450
[email protected]ce112fe2012-10-29 22:52:18451 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59452 if (!new_damage_rect.IsEmpty())
453 paint_aggregator_.InvalidateRect(new_damage_rect);
454
[email protected]b5913d72012-02-07 22:26:54455 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59456
[email protected]b5913d72012-02-07 22:26:54457 if (webwidget_)
458 webwidget_->didChangeWindowResizerRect();
459 }
460}
461
initial.commit09911bf2008-07-26 23:55:29462void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31463 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29464 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03465 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29466}
467
[email protected]9e2e4632012-07-27 16:38:41468void RenderWidget::OnWasShown(bool needs_repainting) {
469 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29470 // During shutdown we can just ignore this message.
471 if (!webwidget_)
472 return;
473
474 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03475 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29476
477 if (!needs_repainting && !needs_repainting_on_restore_)
478 return;
479 needs_repainting_on_restore_ = false;
480
[email protected]d65adb12010-04-28 17:26:49481 // Tag the next paint as a restore ack, which is picked up by
482 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29483 set_next_paint_is_restore_ack();
484
485 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56486 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46487 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
488 } else {
489 scheduleComposite();
490 }
initial.commit09911bf2008-07-26 23:55:29491}
492
[email protected]992db4c2011-05-12 15:37:15493void RenderWidget::OnWasSwappedOut() {
494 // If we have been swapped out and no one else is using this process,
495 // it's safe to exit now. If we get swapped back in, we will call
496 // AddRefProcess in SetSwappedOut.
497 if (is_swapped_out_)
498 RenderProcess::current()->ReleaseProcess();
499}
500
[email protected]53d3f302009-12-21 04:42:05501void RenderWidget::OnRequestMoveAck() {
502 DCHECK(pending_window_rect_count_);
503 pending_window_rect_count_--;
504}
505
506void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58507 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21508 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05509 update_reply_pending_ = false;
510
[email protected]b4d08452010-10-05 17:34:35511 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
512 // have no current paint buffer.
513 if (current_paint_buf_) {
514 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
515 current_paint_buf_ = NULL;
516 }
517
[email protected]65225772011-05-12 21:10:24518 // If swapbuffers is still pending, then defer the update until the
519 // swapbuffers occurs.
520 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
521 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
522 return;
523 }
524
[email protected]29ed96a2012-02-04 18:12:16525 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18526 if (!is_accelerated_compositing_active_) {
527 DidFlushPaint();
528 }
[email protected]a2f6bc112009-06-27 16:27:25529
initial.commit09911bf2008-07-26 23:55:29530 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24531 DoDeferredUpdateAndSendInputAck();
532}
533
[email protected]d0be63772011-12-20 23:18:04534bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59535 // Contexts using the command buffer support asynchronous swapbuffers.
536 // See RenderWidget::CreateOutputSurface().
537 if (RenderThreadImpl::current()->compositor_message_loop_proxy() ||
538 CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessWebGL))
539 return false;
540
541 return true;
542}
543
544GURL RenderWidget::GetURLForGraphicsContext3D() {
545 return GURL();
[email protected]65225772011-05-12 21:10:24546}
547
[email protected]479b0172012-10-29 19:27:09548bool RenderWidget::ForceCompositingModeEnabled() {
549 return false;
550}
551
[email protected]ba91a792013-02-06 09:48:28552scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface() {
[email protected]ed7defa2013-03-12 21:29:59553 // Explicitly disable antialiasing for the compositor. As of the time of
554 // this writing, the only platform that supported antialiasing for the
555 // compositor was Mac OS X, because the on-screen OpenGL context creation
556 // code paths on Windows and Linux didn't yet have multisampling support.
557 // Mac OS X essentially always behaves as though it's rendering offscreen.
558 // Multisampling has a heavy cost especially on devices with relatively low
559 // fill rate like most notebooks, and the Mac implementation would need to
560 // be optimized to resolve directly into the IOSurface shared between the
561 // GPU and browser processes. For these reasons and to avoid platform
562 // disparities we explicitly disable antialiasing.
563 WebKit::WebGraphicsContext3D::Attributes attributes;
564 attributes.antialias = false;
565 attributes.shareResources = true;
566 attributes.noAutomaticFlushes = true;
567 WebGraphicsContext3D* context = CreateGraphicsContext3D(attributes);
568 if (!context)
569 return scoped_ptr<cc::OutputSurface>();
570
571 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
572 if (command_line.HasSwitch(switches::kEnableSoftwareCompositingGLAdapter)) {
573 // In the absence of a software-based delegating renderer, use this
574 // stopgap adapter class to present the software renderer output using a
575 // 3d context.
576 return scoped_ptr<cc::OutputSurface>(
577 new CompositorOutputSurface(routing_id(), NULL,
578 new CompositorSoftwareOutputDeviceGLAdapter(context)));
579 } else {
580 bool composite_to_mailbox =
581 command_line.HasSwitch(cc::switches::kCompositeToMailbox);
582 DCHECK(!composite_to_mailbox || command_line.HasSwitch(
583 cc::switches::kEnableCompositorFrameMessage));
584 // No swap throttling yet when compositing on the main thread.
585 DCHECK(!composite_to_mailbox || is_threaded_compositing_enabled_);
586 return scoped_ptr<cc::OutputSurface>(composite_to_mailbox ?
587 new MailboxOutputSurface(routing_id(), context, NULL) :
588 new CompositorOutputSurface(routing_id(), context, NULL));
589 }
[email protected]ba91a792013-02-06 09:48:28590}
591
[email protected]ed7defa2013-03-12 21:29:59592void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24593 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21594 while (!updates_pending_swap_.empty()) {
595 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
596 updates_pending_swap_.pop_front();
597 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
598 // compositing pass, hence doesn't require an UpdateRect message.
599 if (msg)
600 Send(msg);
601 }
[email protected]65225772011-05-12 21:10:24602 num_swapbuffers_complete_pending_ = 0;
603 using_asynchronous_swapbuffers_ = false;
604 // Schedule another frame so the compositor learns about it.
605 scheduleComposite();
606}
607
[email protected]ed7defa2013-03-12 21:29:59608void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08609 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21610
611 if (using_asynchronous_swapbuffers_) {
612 ViewHostMsg_UpdateRect* msg = NULL;
613 // pending_update_params_ can be NULL if the swap doesn't correspond to an
614 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
615 // message.
616 if (pending_update_params_.get()) {
617 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
618 pending_update_params_.reset();
619 }
620 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08621 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21622 }
[email protected]37a6f302011-07-11 23:43:08623}
624
[email protected]ed7defa2013-03-12 21:29:59625void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24626 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16627
[email protected]404939f2012-06-01 04:06:18628 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16629 DidFlushPaint();
630
[email protected]65225772011-05-12 21:10:24631 // When compositing deactivates, we reset the swapbuffers pending count. The
632 // swapbuffers acks may still arrive, however.
633 if (num_swapbuffers_complete_pending_ == 0) {
634 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
635 return;
636 }
[email protected]aa4117f2011-12-09 22:19:21637 DCHECK(!updates_pending_swap_.empty());
638 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
639 updates_pending_swap_.pop_front();
640 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
641 // compositing pass, hence doesn't require an UpdateRect message.
642 if (msg)
643 Send(msg);
[email protected]65225772011-05-12 21:10:24644 num_swapbuffers_complete_pending_--;
645
646 // If update reply is still pending, then defer the update until that reply
647 // occurs.
[email protected]d0be63772011-12-20 23:18:04648 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24649 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
650 return;
651 }
652
653 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06654 // when we were previously rendering. However, if an invalidation task is not
655 // posted, there may be software rendering work pending. In that case, don't
656 // early out.
657 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24658 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
659 return;
660 }
661
[email protected]cc66e682012-10-02 06:48:18662 // Do not call DoDeferredUpdate unless there's animation work to be done or
663 // a real invalidation. This prevents rendering in response to a swapbuffers
664 // callback coming back after we've navigated away from the page that
665 // generated it.
666 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
667 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
668 return;
669 }
670
[email protected]65225772011-05-12 21:10:24671 // Continue painting if necessary...
672 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29673}
674
[email protected]0dea1652012-12-14 00:09:09675void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
676 bool is_keyboard_shortcut) {
[email protected]65225772011-05-12 21:10:24677 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29678
[email protected]5dd768212009-08-13 23:34:49679 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09680 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49681 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29682 return;
[email protected]5dd768212009-08-13 23:34:49683 }
initial.commit09911bf2008-07-26 23:55:29684
[email protected]6a4d7f62013-01-07 21:32:13685 base::TimeDelta now = base::TimeDelta::FromInternalValue(
686 base::TimeTicks::Now().ToInternalValue());
687
688 int64 delta = static_cast<int64>(
689 (now.InSecondsF() - input_event->timeStampSeconds) *
690 base::Time::kMicrosecondsPerSecond);
691 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
692 std::string name_for_event =
693 base::StringPrintf("Event.Latency.Renderer.%s",
694 GetEventName(input_event->type));
[email protected]de415552013-01-23 04:12:17695 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:48696 base::Histogram::FactoryGet(
[email protected]6a4d7f62013-01-07 21:32:13697 name_for_event,
[email protected]bafdc5d52013-02-27 18:18:48698 0,
699 1000000,
[email protected]6a4d7f62013-01-07 21:32:13700 100,
[email protected]de415552013-01-23 04:12:17701 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:48702 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:13703
[email protected]67bfb83f2011-09-22 03:36:37704 bool prevent_default = false;
705 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26706 const WebMouseEvent& mouse_event =
707 *static_cast<const WebMouseEvent*>(input_event);
708 TRACE_EVENT2("renderer", "HandleMouseMove",
709 "x", mouse_event.x, "y", mouse_event.y);
710 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37711 }
712
[email protected]41d86852012-11-07 12:23:24713 if (WebInputEvent::isGestureEventType(input_event->type)) {
714 const WebGestureEvent& gesture_event =
715 *static_cast<const WebGestureEvent*>(input_event);
716 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
717 }
718
[email protected]3ebcc7c2013-01-09 05:34:46719 if (input_event->type == WebInputEvent::GestureTap ||
720 input_event->type == WebInputEvent::GestureLongPress)
721 resetInputMethod();
722
[email protected]67bfb83f2011-09-22 03:36:37723 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12724 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
725 suppress_next_char_events_ = false;
726 if (!processed && webwidget_)
727 processed = webwidget_->handleInputEvent(*input_event);
728 }
729
730 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
731 // it's not processed by webkit, then we need to suppress the upcoming Char
732 // events.
733 if (!processed && is_keyboard_shortcut)
734 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29735
[email protected]3d5c243b2012-11-30 00:26:01736 InputEventAckState ack_result = processed ?
737 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
738 if (!processed && input_event->type == WebInputEvent::TouchStart) {
739 const WebTouchEvent& touch_event =
740 *static_cast<const WebTouchEvent*>(input_event);
741 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
742 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
743 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
744 }
745
[email protected]a9fb30aa2011-10-06 06:58:46746 IPC::Message* response =
747 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
[email protected]3d5c243b2012-11-30 00:26:01748 ack_result);
[email protected]3391a0772012-03-28 00:32:07749 bool event_type_gets_rate_limited =
750 input_event->type == WebInputEvent::MouseMove ||
751 input_event->type == WebInputEvent::MouseWheel ||
752 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06753
754 bool frame_pending = paint_aggregator_.HasPendingUpdate();
755 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28756 frame_pending = compositor_ &&
757 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06758 }
759
[email protected]3391a0772012-03-28 00:32:07760 bool is_input_throttled =
[email protected]ce2b28e2012-08-09 15:53:57761 throttle_input_events_ &&
[email protected]8926c602013-01-23 05:32:06762 frame_pending;
[email protected]e2824412009-02-27 01:57:05763
[email protected]f8868d72012-04-27 19:13:03764 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24765 // We want to rate limit the input events in this case, so we'll wait for
766 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17767 if (pending_input_event_ack_.get()) {
768 // As two different kinds of events could cause us to postpone an ack
769 // we send it now, if we have one pending. The Browser should never
770 // send us the same kind of event we are delaying the ack for.
771 Send(pending_input_event_ack_.release());
772 }
[email protected]12fbad812009-09-01 18:21:24773 pending_input_event_ack_.reset(response);
774 } else {
775 Send(response);
776 }
777
[email protected]3306f262012-09-21 19:20:42778#if defined(OS_ANDROID)
779 // Allow the IME to be shown when the focus changes as a consequence
780 // of a processed touch end event.
781 if (input_event->type == WebInputEvent::TouchEnd && processed)
782 UpdateTextInputState(SHOW_IME_IF_NEEDED);
783#endif
784
[email protected]5dd768212009-08-13 23:34:49785 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48786
[email protected]67bfb83f2011-09-22 03:36:37787 if (!prevent_default) {
788 if (WebInputEvent::isKeyboardEventType(input_event->type))
789 DidHandleKeyEvent();
790 if (WebInputEvent::isMouseEventType(input_event->type))
791 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24792 if (WebInputEvent::isTouchEventType(input_event->type))
793 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37794 }
initial.commit09911bf2008-07-26 23:55:29795}
796
797void RenderWidget::OnMouseCaptureLost() {
798 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28799 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29800}
801
802void RenderWidget::OnSetFocus(bool enable) {
803 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33804 if (webwidget_)
805 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29806}
807
808void RenderWidget::ClearFocus() {
809 // We may have got the focus from the browser before this gets processed, in
810 // which case we do not want to unfocus ourself.
811 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28812 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]2d5d09d52009-06-15 14:29:21815void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00816 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21817 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06818 TRACE_EVENT2("renderer", "PaintRect",
819 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45820
821 const bool kEnableGpuBenchmarking =
822 CommandLine::ForCurrentProcess()->HasSwitch(
823 switches::kEnableGpuBenchmarking);
[email protected]4fb66842009-12-04 21:41:00824 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21825
826 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00827 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
828 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03829
[email protected]699ab0d2009-04-23 23:19:14830 // If there is a custom background, tile it.
831 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14832 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11833 skia::RefPtr<SkShader> shader = skia::AdoptRef(
834 SkShader::CreateBitmapShader(background_,
835 SkShader::kRepeat_TileMode,
836 SkShader::kRepeat_TileMode));
837 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20838
839 // Use kSrc_Mode to handle background_ transparency properly.
840 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
841
842 // Canvas could contain multiple update rects. Clip to given rect so that
843 // we don't accidentally clear other update rects.
844 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44845 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48846 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14847 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20848 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14849 }
850
[email protected]719b36f2010-12-22 20:36:46851 // First see if this rect is a plugin that can paint itself faster.
852 TransportDIB* optimized_dib = NULL;
853 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20854 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46855 webkit::ppapi::PluginInstance* optimized_instance =
856 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
857 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20858 &optimized_copy_rect,
859 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46860 if (optimized_instance) {
861 // This plugin can be optimize-painted and we can just ask it to paint
862 // itself. We don't actually need the TransportDIB in this case.
863 //
864 // This is an optimization for PPAPI plugins that know they're on top of
865 // the page content. If this rect is inside such a plugin, we can save some
866 // time and avoid re-rendering the page content which we know will be
867 // covered by the plugin later (this time can be significant, especially
868 // for a playing movie that is invalidating a lot).
869 //
870 // In the plugin movie case, hopefully the similar call to
871 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
872 // painting, because that avoids copying the plugin image to a different
873 // paint rect. Unfortunately, if anything on the page is animating other
874 // than the movie, it break this optimization since the union of the
875 // invalid regions will be larger than the plugin.
876 //
877 // This code optimizes that case, where we can still avoid painting in
878 // WebKit and filling the background (which can be slow) and just painting
879 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
880 // required.
[email protected]63ab54262012-11-09 15:58:45881 base::TimeTicks paint_begin_ticks;
882 if (kEnableGpuBenchmarking)
883 paint_begin_ticks = base::TimeTicks::HighResNow();
884
[email protected]df59dd42012-09-14 22:56:30885 SkAutoCanvasRestore auto_restore(canvas, true);
886 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:46887 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27888 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:21889 canvas->restore();
[email protected]63ab54262012-11-09 15:58:45890 if (kEnableGpuBenchmarking) {
891 base::TimeDelta paint_time =
892 base::TimeTicks::HighResNow() - paint_begin_ticks;
893 if (!is_accelerated_compositing_active_)
[email protected]121cc752013-01-16 09:12:45894 software_stats_.totalPaintTime += paint_time;
[email protected]63ab54262012-11-09 15:58:45895 }
[email protected]719b36f2010-12-22 20:36:46896 } else {
897 // Normal painting case.
[email protected]63ab54262012-11-09 15:58:45898 base::TimeTicks paint_begin_ticks;
899 if (kEnableGpuBenchmarking)
900 paint_begin_ticks = base::TimeTicks::HighResNow();
901
[email protected]719b36f2010-12-22 20:36:46902 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]63ab54262012-11-09 15:58:45903
904 if (kEnableGpuBenchmarking) {
905 base::TimeDelta paint_time =
906 base::TimeTicks::HighResNow() - paint_begin_ticks;
907 if (!is_accelerated_compositing_active_)
[email protected]121cc752013-01-16 09:12:45908 software_stats_.totalPaintTime += paint_time;
[email protected]63ab54262012-11-09 15:58:45909 }
[email protected]719b36f2010-12-22 20:36:46910
911 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35912 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46913 }
initial.commit09911bf2008-07-26 23:55:29914
[email protected]4fb66842009-12-04 21:41:00915 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00916 canvas->restore();
[email protected]63ab54262012-11-09 15:58:45917
918 if (kEnableGpuBenchmarking) {
[email protected]63ab54262012-11-09 15:58:45919 int64 num_pixels_processed = rect.width() * rect.height();
920 software_stats_.totalPixelsPainted += num_pixels_processed;
921 software_stats_.totalPixelsRasterized += num_pixels_processed;
922 }
[email protected]4fb66842009-12-04 21:41:00923}
924
925void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
926 skia::PlatformCanvas* canvas) {
927 static bool kPaintBorder =
928 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
929 if (!kPaintBorder)
930 return;
931
[email protected]53d3f302009-12-21 04:42:05932 // Cycle through these colors to help distinguish new paint rects.
933 const SkColor colors[] = {
934 SkColorSetARGB(0x3F, 0xFF, 0, 0),
935 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
936 SkColorSetARGB(0x3F, 0, 0, 0xFF),
937 };
938 static int color_selector = 0;
939
[email protected]4fb66842009-12-04 21:41:00940 SkPaint paint;
941 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05942 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00943 paint.setStrokeWidth(1);
944
945 SkIRect irect;
946 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
947 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29948}
949
[email protected]52ccd0ea2011-02-16 01:09:05950void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30951 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30952 if (!animation_update_pending_) {
953 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59954 return;
[email protected]921244e42011-07-20 16:36:30955 }
[email protected]bd37ae252011-06-03 01:28:18956 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59957 // Record when we fired (according to base::Time::Now()) relative to when
958 // we posted the task to quantify how much the base::Time/base::TimeTicks
959 // skew is affecting animations.
960 base::TimeDelta animation_callback_delay = base::Time::Now() -
961 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
962 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
963 animation_callback_delay,
964 base::TimeDelta::FromMilliseconds(0),
965 base::TimeDelta::FromMilliseconds(30),
966 25);
967 }
[email protected]65225772011-05-12 21:10:24968 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24969}
970
[email protected]52ccd0ea2011-02-16 01:09:05971void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59972 if (!animation_update_pending_)
973 return;
[email protected]bd37ae252011-06-03 01:28:18974
975 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33976 base::TimeDelta animationInterval = IsRenderingVSynced() ?
977 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18978
[email protected]7c4329e2011-02-18 22:02:59979 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45980
981 // animation_floor_time_ is the earliest time that we should animate when
982 // using the dead reckoning software scheduler. If we're using swapbuffers
983 // complete callbacks to rate limit, we can ignore this floor.
984 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30985 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33986 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18987 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59988 // running animation callbacks so that if a callback requests another
989 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38990 animation_timer_.Stop();
991 animation_timer_.Start(FROM_HERE, animationInterval, this,
992 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59993 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:28994 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:20995 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:06996 } else {
[email protected]635353c2013-03-06 09:11:20997 double frame_begin_time =
998 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
999 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061000 }
[email protected]7c4329e2011-02-18 22:02:591001 return;
[email protected]5f8b1022011-01-21 23:34:501002 }
[email protected]bd37ae252011-06-03 01:28:181003 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381004 if (!animation_timer_.IsRunning()) {
1005 // This code uses base::Time::Now() to calculate the floor and next fire
1006 // time because javascript's Date object uses base::Time::Now(). The
1007 // message loop uses base::TimeTicks, which on windows can have a
1008 // different granularity than base::Time.
1009 // The upshot of all this is that this function might be called before
1010 // base::Time::Now() has advanced past the animation_floor_time_. To
1011 // avoid exposing this delay to javascript, we keep posting delayed
1012 // tasks until base::Time::Now() has advanced far enough.
1013 base::TimeDelta delay = animation_floor_time_ - now;
1014 animation_timer_.Start(FROM_HERE, delay, this,
1015 &RenderWidget::AnimationCallback);
1016 }
[email protected]5f8b1022011-01-21 23:34:501017}
1018
[email protected]bd37ae252011-06-03 01:28:181019bool RenderWidget::IsRenderingVSynced() {
1020 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1021 // not caught by this check. This will lead to artificially low frame rates
1022 // for people who force vsync off at a driver level and expect Chrome to speed
1023 // up.
1024 return !has_disable_gpu_vsync_switch_;
1025}
1026
[email protected]65225772011-05-12 21:10:241027void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061028 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241029 invalidation_task_posted_ = false;
1030 DoDeferredUpdateAndSendInputAck();
1031}
1032
1033void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051034 DoDeferredUpdate();
1035
1036 if (pending_input_event_ack_.get())
1037 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211038}
1039
[email protected]552e6002009-11-19 05:24:571040void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581041 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:081042
[email protected]65225772011-05-12 21:10:241043 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291044 return;
[email protected]05a980d7a2012-02-07 22:16:421045
[email protected]fc4404d2012-11-07 19:53:301046 if (!init_complete_) {
1047 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421048 return;
1049 }
[email protected]aa4117f2011-12-09 22:19:211050 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241051 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1052 return;
1053 }
[email protected]9ca84622011-06-02 23:46:391054 if (is_accelerated_compositing_active_ &&
1055 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241056 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1057 return;
1058 }
initial.commit09911bf2008-07-26 23:55:291059
[email protected]552e6002009-11-19 05:24:571060 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051061 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571062 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291063 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241064 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291065 return;
1066 }
1067
[email protected]05a980d7a2012-02-07 22:16:421068 if (is_accelerated_compositing_active_)
1069 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1070
[email protected]0fb93f52011-05-18 23:13:561071 // Tracking of frame rate jitter
1072 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371073 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051074 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501075
[email protected]f98d7e3c2010-09-13 22:30:461076 // Layout may generate more invalidation. It may also enable the
1077 // GPU acceleration, so make sure to run layout before we send the
1078 // GpuRenderingActivated message.
1079 webwidget_->layout();
1080
[email protected]dcca3aa92012-02-17 23:03:371081 // The following two can result in further layout and possibly
1082 // enable GPU acceleration so they need to be called before any painting
1083 // is done.
[email protected]3306f262012-09-21 19:20:421084 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]dcca3aa92012-02-17 23:03:371085 UpdateSelectionBounds();
1086
[email protected]5f8b1022011-01-21 23:34:501087 // Suppress painting if nothing is dirty. This has to be done after updating
1088 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241089 if (!paint_aggregator_.HasPendingUpdate()) {
1090 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371091 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501092 return;
[email protected]65225772011-05-12 21:10:241093 }
[email protected]5f8b1022011-01-21 23:34:501094
[email protected]479b0172012-10-29 19:27:091095 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361096 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091097 ForceCompositingModeEnabled()) {
1098 webwidget_->enterForceCompositingMode(true);
1099 }
1100
[email protected]872ae5b2011-05-26 20:20:501101 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561102 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041103 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561104 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1105 delay,
1106 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411107 base::TimeDelta::FromMilliseconds(120),
1108 60);
[email protected]d0be63772011-12-20 23:18:041109 } else {
[email protected]0fb93f52011-05-18 23:13:561110 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1111 delay,
1112 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411113 base::TimeDelta::FromMilliseconds(120),
1114 60);
[email protected]d0be63772011-12-20 23:18:041115 }
[email protected]872ae5b2011-05-26 20:20:501116
1117 // Calculate filtered time per frame:
1118 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1119 filtered_time_per_frame_ =
1120 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561121 }
1122 last_do_deferred_update_time_ = frame_begin_ticks;
1123
[email protected]fef5e3972012-08-07 03:59:471124 if (!is_accelerated_compositing_active_) {
1125 software_stats_.numAnimationFrames++;
1126 software_stats_.numFramesSentToScreen++;
1127 }
1128
[email protected]552e6002009-11-19 05:24:571129 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291130 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301131 PaintAggregator::PendingUpdate update;
1132 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291133
[email protected]53d3f302009-12-21 04:42:051134 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181135 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291136
[email protected]29ed96a2012-02-04 18:12:161137 // Notify derived classes that we're about to initiate a paint.
1138 WillInitiatePaint();
1139
[email protected]ca4847f2010-09-24 05:39:151140 // A plugin may be able to do an optimized paint. First check this, in which
1141 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461142 // This optimization allows PPAPI plugins that declare themselves on top of
1143 // the page (like a traditional windowed plugin) to be able to animate (think
1144 // movie playing) without repeatedly re-painting the page underneath, or
1145 // copying the plugin backing store (since we can send the plugin's backing
1146 // store directly to the browser).
1147 //
1148 // This optimization only works when the entire invalid region is contained
1149 // within the plugin. There is a related optimization in PaintRect for the
1150 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151151 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151152 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201153 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211154 DCHECK(!pending_update_params_.get());
1155 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551156 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211157 pending_update_params_->scroll_rect = update.scroll_rect;
1158 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211159 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1160 pending_update_params_->flags = next_paint_flags_;
1161 pending_update_params_->scroll_offset = GetScrollOffset();
1162 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091163 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211164 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091165 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211166
[email protected]ca4847f2010-09-24 05:39:151167 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561168 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151169 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201170 &optimized_copy_rect,
1171 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271172 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471173 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211174 pending_update_params_->bitmap = dib->id();
1175 pending_update_params_->bitmap_rect = optimized_copy_location;
1176 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201177 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561178 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461179 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101180
1181 bool fractional_scale = device_scale_factor_ -
1182 static_cast<int>(device_scale_factor_) != 0;
1183 if (fractional_scale) {
1184 // Damage might not be DIP aligned. Inflate damage to compensate.
1185 bounds.Inset(-1, -1);
1186 bounds.Intersect(gfx::Rect(size_));
1187 }
1188
1189 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181190 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101191
[email protected]ca4847f2010-09-24 05:39:151192 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351193 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591194 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:461195 if (!canvas.get()) {
1196 NOTREACHED();
1197 return;
1198 }
[email protected]cef3362f2009-12-21 17:48:451199
[email protected]f98d7e3c2010-09-13 22:30:461200 // We may get back a smaller canvas than we asked for.
1201 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591202 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1203 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1204 pixel_bounds.set_width(canvas->getDevice()->width());
1205 pixel_bounds.set_height(canvas->getDevice()->height());
1206 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1207 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051208
[email protected]f98d7e3c2010-09-13 22:30:461209 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1210
[email protected]aa4117f2011-12-09 22:19:211211 pending_update_params_->bitmap = current_paint_buf_->id();
1212 pending_update_params_->bitmap_rect = bounds;
1213
1214 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461215 // The scroll damage is just another rectangle to paint and copy.
1216 copy_rects.swap(update.paint_rects);
1217 if (!scroll_damage.IsEmpty())
1218 copy_rects.push_back(scroll_damage);
1219
[email protected]4889bd212013-02-11 22:23:101220 for (size_t i = 0; i < copy_rects.size(); ++i) {
1221 gfx::Rect rect = copy_rects[i];
1222 if (fractional_scale) {
1223 // Damage might not be DPI aligned. Inflate rect to compensate.
1224 rect.Inset(-1, -1);
1225 }
1226 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1227 }
[email protected]60a50072012-01-11 02:05:351228
1229 // Software FPS tick for performance tests. The accelerated path traces the
1230 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1231 // NOTE: Tests may break if this event is renamed or moved.
1232 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:461233 } else { // Accelerated compositing path
1234 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211235 // If painting is done via the gpu process then we don't set any damage
1236 // rects to save the browser process from doing unecessary work.
1237 pending_update_params_->bitmap_rect = bounds;
1238 pending_update_params_->scroll_rect = gfx::Rect();
1239 // We don't need an ack, because we're not sharing a DIB with the browser.
1240 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1241 // with the browser for the GPU surface.
1242 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521243 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461244 }
1245
[email protected]936c6f52011-12-13 01:35:261246 // If we're holding a pending input event ACK, send the ACK before sending the
1247 // UpdateReply message so we can receive another input event before the
1248 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1249 // the UpdateRect IPC message handler.
1250 if (pending_input_event_ack_.get())
1251 Send(pending_input_event_ack_.release());
1252
[email protected]ab543072013-01-25 04:38:151253 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211254 // OnSwapBuffersPosted), meaning a message has been added to the
1255 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1256 // the message now.
1257 if (pending_update_params_.get()) {
1258 // sending an ack to browser process that the paint is complete...
1259 update_reply_pending_ = pending_update_params_->needs_ack;
1260 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1261 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341262 }
[email protected]53d3f302009-12-21 04:42:051263
[email protected]29ed96a2012-02-04 18:12:161264 // If we're software rendering then we're done initiating the paint.
1265 if (!is_accelerated_compositing_active_)
1266 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291267}
1268
[email protected]f0c2a242013-03-15 19:34:521269void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151270 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281271 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521272 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151273}
1274
initial.commit09911bf2008-07-26 23:55:291275///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461276// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291277
[email protected]4873c7d2009-07-16 06:36:281278void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]479b0172012-10-29 19:27:091279 TRACE_EVENT2("renderer", "RenderWidget::didInvalidateRect",
1280 "width", rect.width, "height", rect.height);
[email protected]552e6002009-11-19 05:24:571281 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481282 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181283 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571284 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291285 return;
1286
[email protected]552e6002009-11-19 05:24:571287 paint_aggregator_.InvalidateRect(damaged_rect);
1288
1289 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241290 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571291 return;
1292 if (!paint_aggregator_.HasPendingUpdate())
1293 return;
[email protected]aa4117f2011-12-09 22:19:211294 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241295 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1296 return;
1297
1298 // When GPU rendering, combine pending animations and invalidations into
1299 // a single update.
[email protected]816edc62012-03-17 01:27:221300 if (is_accelerated_compositing_active_ &&
1301 animation_update_pending_ &&
1302 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571303 return;
1304
1305 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291306 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1307 // on the call stack.
1308 // 2) Allows us to collect more damage rects before painting to help coalesce
1309 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241310 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211311 MessageLoop::current()->PostTask(
1312 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291313}
1314
[email protected]990278ff2012-11-13 02:12:551315void RenderWidget::didScrollRect(int dx, int dy,
1316 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461317 // Drop scrolls on the floor when we are in compositing mode.
1318 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561319 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461320 return;
1321
[email protected]552e6002009-11-19 05:24:571322 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481323 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181324 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571325 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291326 return;
1327
[email protected]990278ff2012-11-13 02:12:551328 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571329
1330 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241331 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571332 return;
1333 if (!paint_aggregator_.HasPendingUpdate())
1334 return;
[email protected]aa4117f2011-12-09 22:19:211335 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241336 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1337 return;
1338
1339 // When GPU rendering, combine pending animations and invalidations into
1340 // a single update.
[email protected]816edc62012-03-17 01:27:221341 if (is_accelerated_compositing_active_ &&
1342 animation_update_pending_ &&
1343 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571344 return;
1345
1346 // Perform updating asynchronously. This serves two purposes:
1347 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1348 // on the call stack.
1349 // 2) Allows us to collect more damage rects before painting to help coalesce
1350 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241351 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211352 MessageLoop::current()->PostTask(
1353 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291354}
1355
[email protected]244ac1892011-12-02 17:04:471356void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091357 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1358 size_ = new_size;
[email protected]240b5c32012-11-09 19:17:181359 // If we don't clear PaintAggregator after changing autoResize state, then
1360 // we might end up in a situation where bitmap_rect is larger than the
1361 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1362 // with invalid damage rects.
1363 paint_aggregator_.ClearPendingUpdate();
[email protected]8be1c582013-03-06 00:55:031364
[email protected]97e1bf72013-03-06 14:06:051365 AutoResizeCompositor();
1366
[email protected]8be1c582013-03-06 00:55:031367 if (RenderThreadImpl::current()->short_circuit_size_updates()) {
1368 setWindowRect(WebRect(rootWindowRect().x,
1369 rootWindowRect().y,
1370 new_size.width,
1371 new_size.height));
1372 } else {
1373 need_update_rect_for_auto_resize_ = true;
1374 }
[email protected]ea3ee0a2012-05-15 03:43:091375 }
[email protected]244ac1892011-12-02 17:04:471376}
1377
[email protected]97e1bf72013-03-06 14:06:051378void RenderWidget::AutoResizeCompositor() {
[email protected]847a2582013-03-09 02:29:511379 if (!auto_resize_mode_)
1380 return;
1381
[email protected]97e1bf72013-03-06 14:06:051382 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1383 device_scale_factor_));
1384 if (compositor_)
1385 compositor_->setViewportSize(size_, physical_backing_size_);
1386}
1387
[email protected]91acd1c2012-03-14 08:32:391388void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221389 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1390
[email protected]c63b4d42012-04-26 01:01:071391#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211392 if (!is_accelerated_compositing_active_) {
1393 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1394 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1395 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1396 // going to switch to accelerated compositing, the GPU process may need
1397 // round-trips to the browser's UI thread before finishing the frame,
1398 // causing deadlocks if we delay the UpdateRect until we receive the
1399 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071400 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1401 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211402 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1403 }
[email protected]c63b4d42012-04-26 01:01:071404#endif
[email protected]aa4117f2011-12-09 22:19:211405
[email protected]ea162f92011-10-04 23:08:221406 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421407 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241408 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221409}
1410
1411void RenderWidget::didDeactivateCompositor() {
1412 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1413
1414 is_accelerated_compositing_active_ = false;
1415 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1416 routing_id_, is_accelerated_compositing_active_));
1417
[email protected]ea162f92011-10-04 23:08:221418 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241419 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091420
1421 // In single-threaded mode, we exit force compositing mode and re-enter in
1422 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1423 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1424 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361425 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091426 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561427}
1428
[email protected]e195e582013-03-08 01:32:591429void RenderWidget::initializeLayerTreeView() {
1430 compositor_ = RenderWidgetCompositor::Create(this);
1431 if (!compositor_)
1432 return;
1433
1434 compositor_->setViewportSize(size_, physical_backing_size_);
1435 if (init_complete_)
1436 compositor_->setSurfaceReady();
1437}
1438
[email protected]8926c602013-01-23 05:32:061439void RenderWidget::initializeLayerTreeView(
1440 WebKit::WebLayerTreeViewClient* client,
1441 const WebKit::WebLayer& root_layer,
1442 const WebKit::WebLayerTreeView::Settings& settings) {
[email protected]e195e582013-03-08 01:32:591443 initializeLayerTreeView();
1444 if (compositor_)
1445 compositor_->setRootLayer(root_layer);
[email protected]8926c602013-01-23 05:32:061446}
1447
1448WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281449 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061450}
1451
[email protected]9ed83fe2013-02-27 01:52:281452void RenderWidget::suppressCompositorScheduling(bool enable) {
1453 if (compositor_)
1454 compositor_->SetSuppressScheduleComposite(enable);
1455}
1456
[email protected]9cd43a62012-03-26 08:03:561457void RenderWidget::willBeginCompositorFrame() {
1458 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371459
[email protected]ea5f70a2013-03-07 12:30:361460 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy());
[email protected]abe8b3a2012-03-28 21:19:371461
1462 // The following two can result in further layout and possibly
1463 // enable GPU acceleration so they need to be called before any painting
1464 // is done.
[email protected]2d354272013-01-14 00:59:061465 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]abe8b3a2012-03-28 21:19:371466 UpdateSelectionBounds();
1467
[email protected]9cd43a62012-03-26 08:03:561468 WillInitiatePaint();
1469}
1470
[email protected]3391a0772012-03-28 00:32:071471void RenderWidget::didBecomeReadyForAdditionalInput() {
1472 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1473 if (pending_input_event_ack_.get())
1474 Send(pending_input_event_ack_.release());
1475}
1476
[email protected]6fceb912013-02-15 06:24:151477void RenderWidget::DidCommitCompositorFrame() {
1478}
1479
[email protected]58264a32011-11-17 23:36:151480void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501481 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351482 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1483 // NOTE: Tests may break if this event is renamed or moved.
1484 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161485 // Notify subclasses that we initiated the paint operation.
1486 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151487}
1488
1489void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181490 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1491
1492 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561493 DidFlushPaint();
1494
[email protected]aa4117f2011-12-09 22:19:211495 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151496 return;
1497
[email protected]ea3ee0a2012-05-15 03:43:091498 if (!next_paint_flags_ &&
1499 !need_update_rect_for_auto_resize_ &&
1500 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151501 return;
[email protected]ea3ee0a2012-05-15 03:43:091502 }
[email protected]58264a32011-11-17 23:36:151503
1504 ViewHostMsg_UpdateRect_Params params;
1505 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151506 params.plugin_window_moves.swap(plugin_window_moves_);
1507 params.flags = next_paint_flags_;
1508 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121509 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091510 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151511
1512 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1513 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091514 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151515}
1516
[email protected]f98d7e3c2010-09-13 22:30:461517void RenderWidget::scheduleComposite() {
[email protected]479b0172012-10-29 19:27:091518 TRACE_EVENT0("gpu", "RenderWidget::scheduleComposite");
[email protected]ea5f70a2013-03-07 12:30:361519 if (RenderThreadImpl::current()->compositor_message_loop_proxy() &&
[email protected]ba91a792013-02-06 09:48:281520 compositor_) {
1521 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041522 } else {
[email protected]c3d45532011-10-07 19:20:401523 // TODO(nduca): replace with something a little less hacky. The reason this
1524 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1525 // contains a lot of host-renderer synchronization logic that is still
1526 // important for the accelerated compositing case. The option of simply
1527 // duplicating all that code is less desirable than "faking out" the
1528 // invalidation path using a magical damage rect.
1529 didInvalidateRect(WebRect(0, 0, 1, 1));
1530 }
[email protected]f98d7e3c2010-09-13 22:30:461531}
1532
[email protected]5f8b1022011-01-21 23:34:501533void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201534 if (animation_update_pending_)
1535 return;
1536
[email protected]921244e42011-07-20 16:36:301537 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201538 animation_update_pending_ = true;
1539 if (!animation_timer_.IsRunning()) {
1540 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1541 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211542 }
[email protected]5f8b1022011-01-21 23:34:501543}
1544
[email protected]4873c7d2009-07-16 06:36:281545void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301546 // TODO(darin): Eliminate this temporary.
1547 WebCursor cursor(cursor_info);
1548
initial.commit09911bf2008-07-26 23:55:291549 // Only send a SetCursor message if we need to make a change.
1550 if (!current_cursor_.IsEqual(cursor)) {
1551 current_cursor_ = cursor;
1552 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1553 }
1554}
1555
1556// We are supposed to get a single call to Show for a newly created RenderWidget
1557// that was created via RenderWidget::CreateWebView. So, we wait until this
1558// point to dispatch the ShowWidget message.
1559//
1560// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281561// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291562//
[email protected]4873c7d2009-07-16 06:36:281563void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291564 DCHECK(!did_show_) << "received extraneous Show call";
1565 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1566 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1567
[email protected]8de12d942010-11-17 20:42:441568 if (did_show_)
1569 return;
1570
1571 did_show_ = true;
1572 // NOTE: initial_pos_ may still have its default values at this point, but
1573 // that's okay. It'll be ignored if as_popup is false, or the browser
1574 // process will impose a default position otherwise.
1575 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1576 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291577}
1578
[email protected]4873c7d2009-07-16 06:36:281579void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291580}
1581
[email protected]4873c7d2009-07-16 06:36:281582void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291583}
1584
[email protected]2533ce12009-05-09 00:02:241585void RenderWidget::DoDeferredClose() {
1586 Send(new ViewHostMsg_Close(routing_id_));
1587}
1588
[email protected]4873c7d2009-07-16 06:36:281589void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321590 if (is_swapped_out_) {
1591 // This widget is currently swapped out, and the active widget is in a
1592 // different process. Have the browser route the close request to the
1593 // active widget instead, so that the correct unload handlers are run.
1594 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1595 return;
1596 }
1597
initial.commit09911bf2008-07-26 23:55:291598 // If a page calls window.close() twice, we'll end up here twice, but that's
1599 // OK. It is safe to send multiple Close messages.
1600
[email protected]2533ce12009-05-09 00:02:241601 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1602 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1603 // could be closed before the JS finishes executing. So instead, post a
1604 // message back to the message loop, which won't run until the JS is
1605 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211606 MessageLoop::current()->PostTask(
1607 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291608}
1609
1610void RenderWidget::Close() {
1611 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061612 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281613 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281614 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291615 webwidget_ = NULL;
1616 }
1617}
1618
[email protected]4873c7d2009-07-16 06:36:281619WebRect RenderWidget::windowRect() {
1620 if (pending_window_rect_count_)
1621 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241622
[email protected]80ad8622012-11-07 16:33:031623 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291624}
1625
[email protected]8a9d6ca32011-06-06 20:11:301626void RenderWidget::setToolTipText(const WebKit::WebString& text,
1627 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541628 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301629}
1630
[email protected]4873c7d2009-07-16 06:36:281631void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291632 if (did_show_) {
[email protected]8be1c582013-03-06 00:55:031633 if (!RenderThreadImpl::current()->short_circuit_size_updates()) {
1634 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
1635 SetPendingWindowRect(pos);
1636 } else {
1637 WebSize new_size(pos.width, pos.height);
1638 Resize(new_size, new_size, WebRect(), is_fullscreen_, NO_RESIZE_ACK);
1639 view_screen_rect_ = pos;
1640 window_screen_rect_ = pos;
1641 }
initial.commit09911bf2008-07-26 23:55:291642 } else {
1643 initial_pos_ = pos;
1644 }
1645}
1646
[email protected]2533ce12009-05-09 00:02:241647void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1648 pending_window_rect_ = rect;
1649 pending_window_rect_count_++;
1650}
1651
[email protected]4873c7d2009-07-16 06:36:281652WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241653 if (pending_window_rect_count_) {
1654 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1655 // the RootWindowRect is probably going to return wrong results since the
1656 // browser may not have processed the Move yet. There isn't really anything
1657 // good to do in this case, and it shouldn't happen - since this size is
1658 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281659 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241660 }
1661
[email protected]80ad8622012-11-07 16:33:031662 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371663}
1664
[email protected]4873c7d2009-07-16 06:36:281665WebRect RenderWidget::windowResizerRect() {
1666 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191667}
1668
[email protected]fa7b1dc2010-06-23 17:53:041669void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031670 // To prevent this renderer process from sending unnecessary IPC messages to
1671 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041672 // only during the input method attached to the browser process is active.
1673 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291674}
1675
[email protected]58b48a0d2012-06-13 07:01:351676void RenderWidget::UpdateCompositionInfo(
1677 const ui::Range& range,
1678 const std::vector<gfx::Rect>& character_bounds) {
1679 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1680 return;
1681 composition_character_bounds_ = character_bounds;
1682 composition_range_ = range;
1683 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1684 routing_id(), composition_range_, composition_character_bounds_));
1685}
1686
[email protected]fa7b1dc2010-06-23 17:53:041687void RenderWidget::OnImeSetComposition(
1688 const string16& text,
1689 const std::vector<WebCompositionUnderline>& underlines,
1690 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281691 if (!webwidget_)
1692 return;
[email protected]e8f775f2013-02-14 21:00:501693 DCHECK(!handling_ime_event_);
1694 handling_ime_event_ = true;
[email protected]d4cff272011-05-02 15:46:011695 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041696 text, WebVector<WebCompositionUnderline>(underlines),
1697 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011698 // Setting the IME composition was successful. Send the new composition
1699 // range to the browser.
1700 ui::Range range(ui::Range::InvalidRange());
1701 size_t location, length;
1702 if (webwidget_->compositionRange(&location, &length)) {
1703 range.set_start(location);
1704 range.set_end(location + length);
1705 }
1706 // The IME was cancelled via the Esc key, so just send back the caret.
1707 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1708 range.set_start(location);
1709 range.set_end(location + length);
1710 }
[email protected]58b48a0d2012-06-13 07:01:351711 std::vector<gfx::Rect> character_bounds;
1712 GetCompositionCharacterBounds(&character_bounds);
1713 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011714 } else {
[email protected]fa7b1dc2010-06-23 17:53:041715 // If we failed to set the composition text, then we need to let the browser
1716 // process to cancel the input method's ongoing composition session, to make
1717 // sure we are in a consistent state.
1718 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011719
1720 // Send an updated IME range with just the caret range.
1721 ui::Range range(ui::Range::InvalidRange());
1722 size_t location, length;
1723 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1724 range.set_start(location);
1725 range.set_end(location + length);
1726 }
[email protected]58b48a0d2012-06-13 07:01:351727 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261728 }
[email protected]e8f775f2013-02-14 21:00:501729 handling_ime_event_ = false;
1730 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]fa7b1dc2010-06-23 17:53:041731}
1732
[email protected]4de6d1692011-10-12 08:45:441733void RenderWidget::OnImeConfirmComposition(
1734 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041735 if (!webwidget_)
1736 return;
[email protected]e8f775f2013-02-14 21:00:501737 DCHECK(!handling_ime_event_);
1738 handling_ime_event_ = true;
[email protected]d0be63772011-12-20 23:18:041739 handling_input_event_ = true;
1740 webwidget_->confirmComposition(text);
1741 handling_input_event_ = false;
1742
[email protected]d4cff272011-05-02 15:46:011743 // Send an updated IME range with just the caret range.
1744 ui::Range range(ui::Range::InvalidRange());
1745 size_t location, length;
1746 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1747 range.set_start(location);
1748 range.set_end(location + length);
1749 }
[email protected]58b48a0d2012-06-13 07:01:351750 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]e8f775f2013-02-14 21:00:501751 handling_ime_event_ = false;
1752 UpdateTextInputState(DO_NOT_SHOW_IME);
initial.commit09911bf2008-07-26 23:55:291753}
1754
[email protected]948f7ab72010-05-28 23:48:081755// This message causes the renderer to render an image of the
1756// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251757void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1758 int tag,
1759 const gfx::Size& page_size,
1760 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001761 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1762 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251763 // Close our unused handle.
1764#if defined(OS_WIN)
1765 ::CloseHandle(dib_handle);
1766#elif defined(OS_MACOSX)
1767 base::SharedMemory::CloseHandle(dib_handle);
1768#endif
1769 }
[email protected]d65adb12010-04-28 17:26:491770 return;
[email protected]45c6aad32010-11-11 04:46:251771 }
[email protected]d65adb12010-04-28 17:26:491772
[email protected]948f7ab72010-05-28 23:48:081773 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491774 // If one of these is empty, then we just return the dib we were
1775 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091776 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491777 return;
1778 }
1779
1780 // Map the given DIB ID into this process, and unmap it at the end
1781 // of this function.
[email protected]45c6aad32010-11-11 04:46:251782 scoped_ptr<TransportDIB> paint_at_size_buffer(
1783 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301784
[email protected]4b01b962012-10-09 23:17:351785 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281786 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351787 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281788 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511789 gfx::Size canvas_size = page_size_in_pixel;
1790 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491791 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511792 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491793 static_cast<float>(canvas_size.height());
1794
[email protected]ee8d6fd2010-05-26 17:05:481795 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491796 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1797 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481798 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491799
[email protected]36808ad2010-10-20 19:18:301800 scoped_ptr<skia::PlatformCanvas> canvas(
1801 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1802 canvas_size.height()));
1803 if (!canvas.get()) {
1804 NOTREACHED();
1805 return;
1806 }
1807
[email protected]d65adb12010-04-28 17:26:491808 // Reset bounds to what we actually received, but they should be the
1809 // same.
1810 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1811 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1812 bounds.set_width(canvas->getDevice()->width());
1813 bounds.set_height(canvas->getDevice()->height());
1814
1815 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081816 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491817 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1818
[email protected]948f7ab72010-05-28 23:48:081819 // Have to make sure we're laid out at the right size before
1820 // rendering.
1821 gfx::Size old_size = webwidget_->size();
1822 webwidget_->resize(page_size);
1823 webwidget_->layout();
1824
[email protected]d65adb12010-04-28 17:26:491825 // Paint the entire thing (using original bounds, not scaled bounds).
1826 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1827 canvas->restore();
1828
[email protected]948f7ab72010-05-28 23:48:081829 // Return the widget to its previous size.
1830 webwidget_->resize(old_size);
1831
[email protected]c88c9442010-07-19 18:55:091832 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491833}
1834
[email protected]3d9ec5052013-01-02 22:05:251835void RenderWidget::OnRepaint(const gfx::Size& size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:121836 // During shutdown we can just ignore this message.
1837 if (!webwidget_)
1838 return;
1839
1840 set_next_paint_is_repaint_ack();
[email protected]b89c90762013-01-06 20:29:301841 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:281842 if (compositor_)
1843 compositor_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461844 scheduleComposite();
1845 } else {
1846 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1847 didInvalidateRect(repaint_rect);
1848 }
[email protected]ec7dc112008-08-06 05:30:121849}
1850
[email protected]3d9ec5052013-01-02 22:05:251851void RenderWidget::OnSmoothScrollCompleted(int gesture_id) {
[email protected]0e241b4b2012-08-18 09:06:271852 PendingSmoothScrollGestureMap::iterator it =
1853 pending_smooth_scroll_gestures_.find(gesture_id);
1854 DCHECK(it != pending_smooth_scroll_gestures_.end());
1855 it->second.Run();
1856 pending_smooth_scroll_gestures_.erase(it);
1857}
1858
[email protected]4873c7d2009-07-16 06:36:281859void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111860 if (!webwidget_)
1861 return;
[email protected]4873c7d2009-07-16 06:36:281862 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111863}
1864
[email protected]6131a642012-06-15 23:26:531865void RenderWidget::OnScreenInfoChanged(
1866 const WebKit::WebScreenInfo& screen_info) {
1867 screen_info_ = screen_info;
[email protected]468ac582012-11-20 00:53:191868 SetDeviceScaleFactor(screen_info.deviceScaleFactor);
[email protected]6131a642012-06-15 23:26:531869}
1870
[email protected]80ad8622012-11-07 16:33:031871void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
1872 const gfx::Rect& window_screen_rect) {
1873 view_screen_rect_ = view_screen_rect;
1874 window_screen_rect_ = window_screen_rect;
1875 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
1876}
1877
[email protected]105dffb42013-02-20 03:46:211878#if defined(OS_ANDROID)
1879void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
1880 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
1881}
[email protected]2384b6c2013-02-28 23:58:511882
1883void RenderWidget::OnShowImeIfNeeded() {
1884 UpdateTextInputState(SHOW_IME_IF_NEEDED);
1885}
[email protected]105dffb42013-02-20 03:46:211886#endif
1887
[email protected]468ac582012-11-20 00:53:191888void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
1889 if (device_scale_factor_ == device_scale_factor)
1890 return;
1891
1892 device_scale_factor_ = device_scale_factor;
1893
1894 if (!is_accelerated_compositing_active_) {
1895 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1896 } else {
1897 scheduleComposite();
1898 }
1899}
1900
[email protected]719b36f2010-12-22 20:36:461901webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151902 const gfx::Rect& paint_bounds,
1903 TransportDIB** dib,
1904 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:201905 gfx::Rect* clip,
1906 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:461907 // Bare RenderWidgets don't support optimized plugin painting.
1908 return NULL;
[email protected]ca4847f2010-09-24 05:39:151909}
1910
[email protected]ceb36f7d2012-10-31 18:33:241911gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521912 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:241913 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:521914}
1915
[email protected]bee16aab2009-08-26 15:55:031916void RenderWidget::SetHidden(bool hidden) {
1917 if (is_hidden_ == hidden)
1918 return;
1919
1920 // The status has changed. Tell the RenderThread about it.
1921 is_hidden_ = hidden;
1922 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271923 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031924 else
[email protected]380244092011-10-07 17:26:271925 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031926}
1927
[email protected]2b624c562011-10-27 22:58:261928void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261929 if (!webwidget_)
1930 return;
1931
1932 if (is_fullscreen_) {
1933 webwidget_->willExitFullScreen();
1934 } else {
1935 webwidget_->willEnterFullScreen();
1936 }
[email protected]2b624c562011-10-27 22:58:261937}
1938
1939void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261940 if (!webwidget_)
1941 return;
1942
1943 if (is_fullscreen_) {
1944 webwidget_->didEnterFullScreen();
1945 } else {
1946 webwidget_->didExitFullScreen();
1947 }
[email protected]2b624c562011-10-27 22:58:261948}
1949
[email protected]699ab0d2009-04-23 23:19:141950void RenderWidget::SetBackground(const SkBitmap& background) {
1951 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461952
[email protected]699ab0d2009-04-23 23:19:141953 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281954 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141955}
1956
[email protected]674741932009-02-04 23:44:461957bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051958 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461959}
1960
1961bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051962 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461963}
1964
1965void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051966 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461967}
1968
1969void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051970 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461971}
1972
1973void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051974 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461975}
1976
[email protected]b18583c2012-12-18 06:55:271977static bool IsDateTimeInput(ui::TextInputType type) {
1978 return type == ui::TEXT_INPUT_TYPE_DATE ||
1979 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
1980 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
1981 type == ui::TEXT_INPUT_TYPE_MONTH ||
1982 type == ui::TEXT_INPUT_TYPE_TIME ||
1983 type == ui::TEXT_INPUT_TYPE_WEEK;
1984}
1985
[email protected]3306f262012-09-21 19:20:421986void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
[email protected]e8f775f2013-02-14 21:00:501987 if (handling_ime_event_)
1988 return;
[email protected]3306f262012-09-21 19:20:421989 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
1990 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291991 return;
[email protected]ad26ef42011-06-17 07:59:451992 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:271993 if (IsDateTimeInput(new_type))
1994 return; // Not considered as a text input field in WebKit/Chromium.
1995
[email protected]5b739cb2012-08-21 20:35:211996 WebKit::WebTextInputInfo new_info;
1997 if (webwidget_)
1998 new_info = webwidget_->textInputInfo();
1999
[email protected]ad26ef42011-06-17 07:59:452000 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212001
[email protected]3306f262012-09-21 19:20:422002 // Only sends text input params if they are changed or if the ime should be
2003 // shown.
2004 if (show_ime_if_needed || (text_input_type_ != new_type
2005 || text_input_info_ != new_info
2006 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212007 ViewHostMsg_TextInputState_Params p;
2008 p.type = new_type;
2009 p.value = new_info.value.utf8();
2010 p.selection_start = new_info.selectionStart;
2011 p.selection_end = new_info.selectionEnd;
2012 p.composition_start = new_info.compositionStart;
2013 p.composition_end = new_info.compositionEnd;
2014 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422015 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:212016 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2017
2018 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042019 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452020 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292021 }
initial.commit09911bf2008-07-26 23:55:292022}
2023
[email protected]7c8873e2013-02-05 08:03:012024void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2025 WebRect focus_webrect;
2026 WebRect anchor_webrect;
2027 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2028 *focus = focus_webrect;
2029 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322030}
2031
[email protected]e99ef6f2011-10-16 01:13:002032void RenderWidget::UpdateSelectionBounds() {
2033 if (!webwidget_)
2034 return;
2035
[email protected]7c8873e2013-02-05 08:03:012036 ViewHostMsg_SelectionBounds_Params params;
2037 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2038 if (selection_anchor_rect_ != params.anchor_rect ||
2039 selection_focus_rect_ != params.focus_rect) {
2040 selection_anchor_rect_ = params.anchor_rect;
2041 selection_focus_rect_ = params.focus_rect;
2042 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292043 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012044 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352045 }
[email protected]e99ef6f2011-10-16 01:13:002046
[email protected]58b48a0d2012-06-13 07:01:352047 std::vector<gfx::Rect> character_bounds;
2048 GetCompositionCharacterBounds(&character_bounds);
2049 UpdateCompositionInfo(composition_range_, character_bounds);
2050}
2051
2052bool RenderWidget::ShouldUpdateCompositionInfo(
2053 const ui::Range& range,
2054 const std::vector<gfx::Rect>& bounds) {
2055 if (composition_range_ != range)
2056 return true;
2057 if (bounds.size() != composition_character_bounds_.size())
2058 return true;
2059 for (size_t i = 0; i < bounds.size(); ++i) {
2060 if (bounds[i] != composition_character_bounds_[i])
2061 return true;
2062 }
2063 return false;
[email protected]e99ef6f2011-10-16 01:13:002064}
2065
[email protected]73bf95812011-10-12 11:38:322066// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452067COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2068 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2069COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2070 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2071COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2072 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182073COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2074 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2075COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2076 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2077COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2078 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2079COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2080 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2081COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2082 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002083COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2084 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2085COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2086 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2087COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2088 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2089COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2090 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2091COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2092 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2093COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2094 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012095COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2096 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2097COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2098 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152099COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2100 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452101
[email protected]5b739cb2012-08-21 20:35:212102ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2103 WebKit::WebTextInputType type) {
2104 // Check the type is in the range representable by ui::TextInputType.
2105 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2106 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2107 return static_cast<ui::TextInputType>(type);
2108}
2109
[email protected]ad26ef42011-06-17 07:59:452110ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272111 if (webwidget_)
2112 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452113 return ui::TEXT_INPUT_TYPE_NONE;
2114}
2115
[email protected]58b48a0d2012-06-13 07:01:352116void RenderWidget::GetCompositionCharacterBounds(
2117 std::vector<gfx::Rect>* bounds) {
2118 DCHECK(bounds);
2119 bounds->clear();
2120}
2121
[email protected]ad26ef42011-06-17 07:59:452122bool RenderWidget::CanComposeInline() {
2123 return true;
[email protected]56ea1a62011-05-30 07:05:572124}
2125
[email protected]4873c7d2009-07-16 06:36:282126WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042127 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282128}
2129
[email protected]f660d9c2012-06-06 18:31:212130float RenderWidget::deviceScaleFactor() {
2131 return device_scale_factor_;
2132}
2133
[email protected]fa7b1dc2010-06-23 17:53:042134void RenderWidget::resetInputMethod() {
2135 if (!input_method_is_active_)
2136 return;
2137
2138 // If the last text input type is not None, then we should finish any
2139 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452140 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042141 // If a composition text exists, then we need to let the browser process
2142 // to cancel the input method's ongoing composition session.
2143 if (webwidget_->confirmComposition())
2144 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2145 }
[email protected]d4cff272011-05-02 15:46:012146
2147 // Send an updated IME range with the current caret rect.
2148 ui::Range range(ui::Range::InvalidRange());
2149 size_t location, length;
2150 if (webwidget_->caretOrSelectionRange(&location, &length)) {
2151 range.set_start(location);
2152 range.set_end(location + length);
2153 }
[email protected]58b48a0d2012-06-13 07:01:352154
2155 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:042156}
2157
[email protected]c68c3e4e2013-01-24 00:36:562158void RenderWidget::didHandleGestureEvent(
2159 const WebGestureEvent& event,
2160 bool event_cancelled) {
2161#if defined(OS_ANDROID)
2162 if (event_cancelled)
2163 return;
2164 if (event.type == WebInputEvent::GestureTap ||
2165 event.type == WebInputEvent::GestureLongPress) {
2166 UpdateTextInputState(SHOW_IME_IF_NEEDED);
2167 }
2168#endif
2169}
2170
[email protected]f103ab72009-09-02 17:10:592171void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:502172 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292173 size_t i = 0;
2174 for (; i < plugin_window_moves_.size(); ++i) {
2175 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582176 if (move.rects_valid) {
2177 plugin_window_moves_[i] = move;
2178 } else {
2179 plugin_window_moves_[i].visible = move.visible;
2180 }
initial.commit09911bf2008-07-26 23:55:292181 break;
2182 }
2183 }
2184
2185 if (i == plugin_window_moves_.size())
2186 plugin_window_moves_.push_back(move);
2187}
[email protected]268654772009-08-06 23:02:042188
2189void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2190 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2191 i != plugin_window_moves_.end(); ++i) {
2192 if (i->window == window) {
2193 plugin_window_moves_.erase(i);
2194 break;
2195 }
2196 }
2197}
[email protected]67bfb83f2011-09-22 03:36:372198
[email protected]b63d58d2012-11-26 22:37:442199void RenderWidget::GetRenderingStats(
2200 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282201 if (compositor_)
[email protected]635353c2013-03-06 09:11:202202 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442203
2204 stats.rendering_stats.numAnimationFrames +=
2205 software_stats_.numAnimationFrames;
2206 stats.rendering_stats.numFramesSentToScreen +=
2207 software_stats_.numFramesSentToScreen;
[email protected]121cc752013-01-16 09:12:452208 stats.rendering_stats.totalPaintTime +=
2209 software_stats_.totalPaintTime;
[email protected]b63d58d2012-11-26 22:37:442210 stats.rendering_stats.totalPixelsPainted +=
2211 software_stats_.totalPixelsPainted;
[email protected]121cc752013-01-16 09:12:452212 stats.rendering_stats.totalRasterizeTime +=
2213 software_stats_.totalRasterizeTime;
[email protected]b63d58d2012-11-26 22:37:442214 stats.rendering_stats.totalPixelsRasterized +=
2215 software_stats_.totalPixelsRasterized;
[email protected]fef5e3972012-08-07 03:59:472216}
2217
[email protected]e9ff79c2012-10-19 21:31:262218bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522219 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2220 if (!gpu_channel)
2221 return false;
2222
2223 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2224}
2225
[email protected]0e241b4b2012-08-18 09:06:272226void RenderWidget::BeginSmoothScroll(
2227 bool down,
[email protected]ebd8b562012-10-09 14:44:292228 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192229 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292230 int mouse_event_x,
2231 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272232 DCHECK(!callback.is_null());
2233 int id = next_smooth_scroll_gesture_id_++;
[email protected]267909d2012-10-20 04:36:192234
2235 ViewHostMsg_BeginSmoothScroll_Params params;
2236 params.scroll_down = down;
2237 params.pixels_to_scroll = pixels_to_scroll;
2238 params.mouse_event_x = mouse_event_x;
2239 params.mouse_event_y = mouse_event_y;
2240
2241 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, id, params));
[email protected]0e241b4b2012-08-18 09:06:272242 pending_smooth_scroll_gestures_.insert(std::make_pair(id, callback));
[email protected]a39ca1652012-07-13 21:30:582243}
2244
[email protected]67bfb83f2011-09-22 03:36:372245bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2246 return false;
2247}
[email protected]c3d45532011-10-07 19:20:402248
[email protected]41d86852012-11-07 12:23:242249bool RenderWidget::WillHandleGestureEvent(
2250 const WebKit::WebGestureEvent& event) {
2251 return false;
2252}
2253
[email protected]3d5c243b2012-11-30 00:26:012254bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2255 return true;
2256}
2257
[email protected]ed7defa2013-03-12 21:29:592258WebGraphicsContext3D* RenderWidget::CreateGraphicsContext3D(
2259 const WebGraphicsContext3D::Attributes& attributes) {
2260 if (!webwidget_)
2261 return NULL;
2262 // The WebGraphicsContext3DInProcessImpl code path is used for
2263 // layout tests (though not through this code) as well as for
2264 // debugging and bringing up new ports.
2265 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessWebGL)) {
2266 return webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWebView(
2267 attributes, true);
2268 } else {
2269 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2270 new WebGraphicsContext3DCommandBufferImpl(
2271 surface_id(),
2272 GetURLForGraphicsContext3D(),
2273 RenderThreadImpl::current(),
2274 weak_ptr_factory_.GetWeakPtr()));
2275
2276 if (!context->Initialize(
2277 attributes,
2278 false /* bind generates resources */,
2279 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE))
2280 return NULL;
2281 return context.release();
2282 }
2283}
2284
[email protected]e9ff79c2012-10-19 21:31:262285} // namespace content