blob: a7cf43788dd38a1b069bf84c47a9837d4e3ba077 [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]8926c602013-01-23 05:32:0617#include "cc/layer_tree_host.h"
18#include "cc/thread.h"
19#include "cc/thread_impl.h"
[email protected]992db4c2011-05-12 15:37:1520#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5021#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2922#include "content/public/common/content_switches.h"
[email protected]8926c602013-01-23 05:32:0623#include "content/renderer/gpu/compositor_thread.h"
[email protected]ba91a792013-02-06 09:48:2824#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]8704f89b2011-04-15 00:30:0525#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4426#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1927#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1828#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4829#include "skia/ext/platform_canvas.h"
[email protected]debc8e52013-01-15 03:43:1230#include "third_party/WebKit/Source/Platform/chromium/public/WebPoint.h"
31#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
32#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
33#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
[email protected]8bd0fe62011-01-17 06:44:3734#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]4b1146bc2012-07-10 18:46:0335#include "third_party/WebKit/Source/WebKit/chromium/public/WebHelperPlugin.h"
[email protected]a7547fb2012-03-08 04:43:4436#include "third_party/WebKit/Source/WebKit/chromium/public/WebPagePopup.h"
[email protected]8bd0fe62011-01-17 06:44:3737#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
38#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0139#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3740#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4141#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1342#include "ui/base/ui_base_switches.h"
[email protected]a25e25b2012-09-28 14:32:3743#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3544#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4845#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2746#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4147#include "ui/surface/transport_dib.h"
[email protected]b63d58d2012-11-26 22:37:4448#include "webkit/compositor_bindings/web_rendering_stats_impl.h"
[email protected]8c89e7792009-08-19 21:18:3449#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5050#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4651#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4852
53#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4954#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5255#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4156#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4857#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4458
[email protected]8bd0fe62011-01-17 06:44:3759#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2960
[email protected]fa7b1dc2010-06-23 17:53:0461using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3062using WebKit::WebCursorInfo;
[email protected]41d86852012-11-07 12:23:2463using WebKit::WebGestureEvent;
[email protected]62cb33cae2009-03-27 23:30:2264using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0665using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2866using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4467using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0068using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2869using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5970using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1871using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0172using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5273using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1974using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5275using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2876using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2477using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0478using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1879using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2680
[email protected]6a4d7f62013-01-07 21:32:1381namespace {
82const char* GetEventName(WebInputEvent::Type type) {
83#define CASE_TYPE(t) case WebInputEvent::t: return #t
84 switch(type) {
85 CASE_TYPE(Undefined);
86 CASE_TYPE(MouseDown);
87 CASE_TYPE(MouseUp);
88 CASE_TYPE(MouseMove);
89 CASE_TYPE(MouseEnter);
90 CASE_TYPE(MouseLeave);
91 CASE_TYPE(ContextMenu);
92 CASE_TYPE(MouseWheel);
93 CASE_TYPE(RawKeyDown);
94 CASE_TYPE(KeyDown);
95 CASE_TYPE(KeyUp);
96 CASE_TYPE(Char);
97 CASE_TYPE(GestureScrollBegin);
98 CASE_TYPE(GestureScrollEnd);
99 CASE_TYPE(GestureScrollUpdate);
100 CASE_TYPE(GestureFlingStart);
101 CASE_TYPE(GestureFlingCancel);
102 CASE_TYPE(GestureTap);
103 CASE_TYPE(GestureTapDown);
104 CASE_TYPE(GestureTapCancel);
105 CASE_TYPE(GestureDoubleTap);
106 CASE_TYPE(GestureTwoFingerTap);
107 CASE_TYPE(GestureLongPress);
108 CASE_TYPE(GestureLongTap);
109 CASE_TYPE(GesturePinchBegin);
110 CASE_TYPE(GesturePinchEnd);
111 CASE_TYPE(GesturePinchUpdate);
112 CASE_TYPE(TouchStart);
113 CASE_TYPE(TouchMove);
114 CASE_TYPE(TouchEnd);
115 CASE_TYPE(TouchCancel);
[email protected]3ec08ed2013-01-11 15:59:57116 default:
117 // Must include default to let WebKit::WebInputEvent add new event types
118 // before they're added here.
119 DLOG(WARNING) << "Unhandled WebInputEvent type in GetEventName.\n";
120 break;
[email protected]6a4d7f62013-01-07 21:32:13121 }
122#undef CASE_TYPE
[email protected]3ec08ed2013-01-11 15:59:57123 return "";
[email protected]6a4d7f62013-01-07 21:32:13124}
125}
[email protected]e9ff79c2012-10-19 21:31:26126namespace content {
[email protected]62cb33cae2009-03-27 23:30:22127
[email protected]6fd35b72012-03-01 19:46:41128RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:04129 const WebKit::WebScreenInfo& screen_info,
[email protected]14392a52012-05-02 20:28:44130 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:29131 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56132 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11133 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29134 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30135 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35136 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:29137 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06138 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05139 update_reply_pending_(false),
[email protected]ea3ee0a2012-05-15 03:43:09140 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24141 using_asynchronous_swapbuffers_(false),
142 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29143 did_show_(false),
initial.commit09911bf2008-07-26 23:55:29144 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:09145 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29146 needs_repainting_on_restore_(false),
147 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49148 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50149 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48150 closing_(false),
[email protected]14392a52012-05-02 20:28:44151 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04152 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45153 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
154 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12155 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48156 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12157 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50158 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21159 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07160 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04161 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52162 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]0e241b4b2012-08-18 09:06:27163 throttle_input_events_(true),
[email protected]cb6430932012-10-31 00:53:36164 next_smooth_scroll_gesture_id_(0),
165 is_threaded_compositing_enabled_(false) {
[email protected]8b3f0eb2012-05-03 19:15:05166 if (!swapped_out)
167 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27168 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18169 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
170 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36171 is_threaded_compositing_enabled_ =
172 CommandLine::ForCurrentProcess()->HasSwitch(
173 switches::kEnableThreadedCompositing);
initial.commit09911bf2008-07-26 23:55:29174}
175
176RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11177 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21178 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35179 if (current_paint_buf_) {
180 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
181 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29182 }
[email protected]992db4c2011-05-12 15:37:15183 // If we are swapped out, we have released already.
184 if (!is_swapped_out_)
185 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29186}
187
[email protected]484955942010-08-19 16:13:18188// static
[email protected]8085dbc82008-09-26 22:53:44189RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04190 WebKit::WebPopupType popup_type,
191 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29192 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41193 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04194 new RenderWidget(popup_type, screen_info, false));
[email protected]a635f942012-12-07 10:34:29195 if (widget->Init(opener_id)) { // adds reference on success.
196 return widget;
197 }
198 return NULL;
initial.commit09911bf2008-07-26 23:55:29199}
200
[email protected]484955942010-08-19 16:13:18201// static
202WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
203 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03204 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18205 break;
206 case WebKit::WebPopupTypeSelect:
207 case WebKit::WebPopupTypeSuggestion:
208 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44209 case WebKit::WebPopupTypePage:
210 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03211 case WebKit::WebPopupTypeHelperPlugin:
212 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18213 default:
214 NOTREACHED();
215 }
216 return NULL;
217}
218
[email protected]a635f942012-12-07 10:34:29219bool RenderWidget::Init(int32 opener_id) {
220 return DoInit(opener_id,
221 RenderWidget::CreateWebWidget(this),
222 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
223 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18224}
225
[email protected]a635f942012-12-07 10:34:29226bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06227 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18228 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29229 DCHECK(!webwidget_);
230
231 if (opener_id != MSG_ROUTING_NONE)
232 opener_id_ = opener_id;
233
[email protected]484955942010-08-19 16:13:18234 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29235
[email protected]380244092011-10-07 17:26:27236 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29237 if (result) {
[email protected]380244092011-10-07 17:26:27238 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29239 // Take a reference on behalf of the RenderThread. This will be balanced
240 // when we receive ViewMsg_Close.
241 AddRef();
[email protected]a635f942012-12-07 10:34:29242 return true;
initial.commit09911bf2008-07-26 23:55:29243 } else {
[email protected]a635f942012-12-07 10:34:29244 // The above Send can fail when the tab is closing.
245 return false;
initial.commit09911bf2008-07-26 23:55:29246 }
247}
248
[email protected]fc4404d2012-11-07 19:53:30249// This is used to complete pending inits and non-pending inits.
250void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29251 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29252
[email protected]fc4404d2012-11-07 19:53:30253 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42254
[email protected]8926c602013-01-23 05:32:06255 if (webwidget_ && is_threaded_compositing_enabled_) {
256 webwidget_->enterForceCompositingMode(true);
257 }
[email protected]ba91a792013-02-06 09:48:28258 if (compositor_) {
259 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09260 }
[email protected]05a980d7a2012-02-07 22:16:42261 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29262
[email protected]6de74452009-02-25 18:04:59263 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29264}
265
[email protected]992db4c2011-05-12 15:37:15266void RenderWidget::SetSwappedOut(bool is_swapped_out) {
267 // We should only toggle between states.
268 DCHECK(is_swapped_out_ != is_swapped_out);
269 is_swapped_out_ = is_swapped_out;
270
271 // If we are swapping out, we will call ReleaseProcess, allowing the process
272 // to exit if all of its RenderViews are swapped out. We wait until the
273 // WasSwappedOut call to do this, to avoid showing the sad tab.
274 // If we are swapping in, we call AddRefProcess to prevent the process from
275 // exiting.
276 if (!is_swapped_out)
277 RenderProcess::current()->AddRefProcess();
278}
279
[email protected]a95986a82010-12-24 06:19:28280bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
281 bool handled = true;
282 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
283 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
284 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
285 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54286 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28287 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41288 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15289 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28290 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]a7266a92012-06-28 02:11:08291 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, OnSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28292 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
293 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
294 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
295 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
296 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
297 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25298 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
299 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
300 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted, OnSmoothScrollCompleted)
[email protected]a95986a82010-12-24 06:19:28301 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
302 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]6131a642012-06-15 23:26:53303 IPC_MESSAGE_HANDLER(ViewMsg_ScreenInfoChanged, OnScreenInfoChanged)
[email protected]80ad8622012-11-07 16:33:03304 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21305#if defined(OS_ANDROID)
306 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
307#endif
[email protected]a95986a82010-12-24 06:19:28308 IPC_MESSAGE_UNHANDLED(handled = false)
309 IPC_END_MESSAGE_MAP()
310 return handled;
311}
initial.commit09911bf2008-07-26 23:55:29312
313bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15314 // Don't send any messages after the browser has told us to close, and filter
315 // most outgoing messages while swapped out.
316 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26317 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11318 closing_) {
initial.commit09911bf2008-07-26 23:55:29319 delete message;
320 return false;
321 }
322
323 // If given a messsage without a routing ID, then assign our routing ID.
324 if (message->routing_id() == MSG_ROUTING_NONE)
325 message->set_routing_id(routing_id_);
326
[email protected]380244092011-10-07 17:26:27327 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44328}
329
[email protected]61e2b3cc2012-03-02 16:13:34330void RenderWidget::Resize(const gfx::Size& new_size,
331 const gfx::Rect& resizer_rect,
332 bool is_fullscreen,
333 ResizeAck resize_ack) {
334 // A resize ack shouldn't be requested if we have not ACK'd the previous one.
335 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
336 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29337
[email protected]61e2b3cc2012-03-02 16:13:34338 // Ignore this during shutdown.
339 if (!webwidget_)
340 return;
341
342 // Remember the rect where the resize corner will be drawn.
343 resizer_rect_ = resizer_rect;
344
345 // NOTE: We may have entered fullscreen mode without changing our size.
346 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
347 if (fullscreen_change)
348 WillToggleFullscreen();
349 is_fullscreen_ = is_fullscreen;
350
351 if (size_ != new_size) {
352 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34353 needs_repainting_on_restore_ = false;
354
355 size_ = new_size;
356
357 paint_aggregator_.ClearPendingUpdate();
358
359 // When resizing, we want to wait to paint before ACK'ing the resize. This
360 // ensures that we only resize as fast as we can paint. We only need to
361 // send an ACK if we are resized to a non-empty rect.
362 webwidget_->resize(new_size);
363 if (!new_size.IsEmpty()) {
364 if (!is_accelerated_compositing_active_) {
365 // Resize should have caused an invalidation of the entire view.
366 DCHECK(paint_aggregator_.HasPendingUpdate());
367 }
368
369 // Send the Resize_ACK flag once we paint again if requested.
370 if (resize_ack == SEND_RESIZE_ACK)
371 set_next_paint_is_resize_ack();
372 }
[email protected]ff475a322012-03-14 00:05:35373 } else {
374 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34375 }
376
377 if (fullscreen_change)
378 DidToggleFullscreen();
379
380 // If a resize ack is requested and it isn't set-up, then no more resizes will
381 // come in and in general things will go wrong.
382 DCHECK(resize_ack != SEND_RESIZE_ACK || new_size.IsEmpty() ||
383 next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29384}
385
386void RenderWidget::OnClose() {
387 if (closing_)
388 return;
389 closing_ = true;
390
391 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03392 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27393 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03394 SetHidden(false);
395 }
initial.commit09911bf2008-07-26 23:55:29396
initial.commit09911bf2008-07-26 23:55:29397 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25398 // now. Post a task that only gets invoked when there are no nested message
399 // loops.
[email protected]32876ae2011-11-15 22:25:21400 MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29401 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25402
403 // Balances the AddRef taken when we called AddRoute.
404 Release();
initial.commit09911bf2008-07-26 23:55:29405}
406
[email protected]61e2b3cc2012-03-02 16:13:34407// Got a response from the browser after the renderer decided to create a new
408// view.
[email protected]fc4404d2012-11-07 19:53:30409void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34410 DCHECK(routing_id_ != MSG_ROUTING_NONE);
411
[email protected]fc4404d2012-11-07 19:53:30412 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34413}
414
[email protected]f21c613a2009-02-12 14:46:17415void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09416 const gfx::Rect& resizer_rect,
417 bool is_fullscreen) {
[email protected]61e2b3cc2012-03-02 16:13:34418 Resize(new_size, resizer_rect, is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29419}
420
[email protected]b5913d72012-02-07 22:26:54421void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
422 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59423 gfx::Rect view_rect(size_);
424
[email protected]ce112fe2012-10-29 22:52:18425 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59426 if (!old_damage_rect.IsEmpty())
427 paint_aggregator_.InvalidateRect(old_damage_rect);
428
[email protected]ce112fe2012-10-29 22:52:18429 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59430 if (!new_damage_rect.IsEmpty())
431 paint_aggregator_.InvalidateRect(new_damage_rect);
432
[email protected]b5913d72012-02-07 22:26:54433 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59434
[email protected]b5913d72012-02-07 22:26:54435 if (webwidget_)
436 webwidget_->didChangeWindowResizerRect();
437 }
438}
439
initial.commit09911bf2008-07-26 23:55:29440void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31441 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29442 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03443 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29444}
445
[email protected]9e2e4632012-07-27 16:38:41446void RenderWidget::OnWasShown(bool needs_repainting) {
447 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29448 // During shutdown we can just ignore this message.
449 if (!webwidget_)
450 return;
451
452 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03453 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29454
455 if (!needs_repainting && !needs_repainting_on_restore_)
456 return;
457 needs_repainting_on_restore_ = false;
458
[email protected]d65adb12010-04-28 17:26:49459 // Tag the next paint as a restore ack, which is picked up by
460 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29461 set_next_paint_is_restore_ack();
462
463 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56464 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46465 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
466 } else {
467 scheduleComposite();
468 }
initial.commit09911bf2008-07-26 23:55:29469}
470
[email protected]992db4c2011-05-12 15:37:15471void RenderWidget::OnWasSwappedOut() {
472 // If we have been swapped out and no one else is using this process,
473 // it's safe to exit now. If we get swapped back in, we will call
474 // AddRefProcess in SetSwappedOut.
475 if (is_swapped_out_)
476 RenderProcess::current()->ReleaseProcess();
477}
478
[email protected]53d3f302009-12-21 04:42:05479void RenderWidget::OnRequestMoveAck() {
480 DCHECK(pending_window_rect_count_);
481 pending_window_rect_count_--;
482}
483
484void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58485 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21486 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05487 update_reply_pending_ = false;
488
[email protected]b4d08452010-10-05 17:34:35489 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
490 // have no current paint buffer.
491 if (current_paint_buf_) {
492 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
493 current_paint_buf_ = NULL;
494 }
495
[email protected]65225772011-05-12 21:10:24496 // If swapbuffers is still pending, then defer the update until the
497 // swapbuffers occurs.
498 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
499 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
500 return;
501 }
502
[email protected]29ed96a2012-02-04 18:12:16503 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18504 if (!is_accelerated_compositing_active_) {
505 DidFlushPaint();
506 }
[email protected]a2f6bc112009-06-27 16:27:25507
initial.commit09911bf2008-07-26 23:55:29508 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24509 DoDeferredUpdateAndSendInputAck();
510}
511
[email protected]d0be63772011-12-20 23:18:04512bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]65225772011-05-12 21:10:24513 return false;
514}
515
[email protected]479b0172012-10-29 19:27:09516bool RenderWidget::ForceCompositingModeEnabled() {
517 return false;
518}
519
[email protected]ba91a792013-02-06 09:48:28520scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface() {
521 return scoped_ptr<cc::OutputSurface>();
522}
523
[email protected]d0be63772011-12-20 23:18:04524void RenderWidget::OnSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24525 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21526 while (!updates_pending_swap_.empty()) {
527 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
528 updates_pending_swap_.pop_front();
529 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
530 // compositing pass, hence doesn't require an UpdateRect message.
531 if (msg)
532 Send(msg);
533 }
[email protected]65225772011-05-12 21:10:24534 num_swapbuffers_complete_pending_ = 0;
535 using_asynchronous_swapbuffers_ = false;
536 // Schedule another frame so the compositor learns about it.
537 scheduleComposite();
538}
539
[email protected]37a6f302011-07-11 23:43:08540void RenderWidget::OnSwapBuffersPosted() {
541 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21542
543 if (using_asynchronous_swapbuffers_) {
544 ViewHostMsg_UpdateRect* msg = NULL;
545 // pending_update_params_ can be NULL if the swap doesn't correspond to an
546 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
547 // message.
548 if (pending_update_params_.get()) {
549 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
550 pending_update_params_.reset();
551 }
552 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08553 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21554 }
[email protected]37a6f302011-07-11 23:43:08555}
556
557void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24558 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16559
[email protected]404939f2012-06-01 04:06:18560 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16561 DidFlushPaint();
562
[email protected]65225772011-05-12 21:10:24563 // When compositing deactivates, we reset the swapbuffers pending count. The
564 // swapbuffers acks may still arrive, however.
565 if (num_swapbuffers_complete_pending_ == 0) {
566 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
567 return;
568 }
[email protected]aa4117f2011-12-09 22:19:21569 DCHECK(!updates_pending_swap_.empty());
570 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
571 updates_pending_swap_.pop_front();
572 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
573 // compositing pass, hence doesn't require an UpdateRect message.
574 if (msg)
575 Send(msg);
[email protected]65225772011-05-12 21:10:24576 num_swapbuffers_complete_pending_--;
577
578 // If update reply is still pending, then defer the update until that reply
579 // occurs.
[email protected]d0be63772011-12-20 23:18:04580 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24581 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
582 return;
583 }
584
585 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06586 // when we were previously rendering. However, if an invalidation task is not
587 // posted, there may be software rendering work pending. In that case, don't
588 // early out.
589 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24590 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
591 return;
592 }
593
[email protected]cc66e682012-10-02 06:48:18594 // Do not call DoDeferredUpdate unless there's animation work to be done or
595 // a real invalidation. This prevents rendering in response to a swapbuffers
596 // callback coming back after we've navigated away from the page that
597 // generated it.
598 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
599 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
600 return;
601 }
602
[email protected]65225772011-05-12 21:10:24603 // Continue painting if necessary...
604 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29605}
606
[email protected]0dea1652012-12-14 00:09:09607void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
608 bool is_keyboard_shortcut) {
[email protected]65225772011-05-12 21:10:24609 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29610
[email protected]5dd768212009-08-13 23:34:49611 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09612 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49613 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29614 return;
[email protected]5dd768212009-08-13 23:34:49615 }
initial.commit09911bf2008-07-26 23:55:29616
[email protected]6a4d7f62013-01-07 21:32:13617 base::TimeDelta now = base::TimeDelta::FromInternalValue(
618 base::TimeTicks::Now().ToInternalValue());
619
620 int64 delta = static_cast<int64>(
621 (now.InSecondsF() - input_event->timeStampSeconds) *
622 base::Time::kMicrosecondsPerSecond);
623 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
624 std::string name_for_event =
625 base::StringPrintf("Event.Latency.Renderer.%s",
626 GetEventName(input_event->type));
[email protected]de415552013-01-23 04:12:17627 base::HistogramBase* counter_for_type =
[email protected]6a4d7f62013-01-07 21:32:13628 base::Histogram::FactoryTimeGet(
629 name_for_event,
630 base::TimeDelta::FromMilliseconds(0),
631 base::TimeDelta::FromMilliseconds(1000000),
632 100,
[email protected]de415552013-01-23 04:12:17633 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]6a4d7f62013-01-07 21:32:13634 counter_for_type->AddTime(base::TimeDelta::FromMicroseconds(delta));
635
[email protected]67bfb83f2011-09-22 03:36:37636 bool prevent_default = false;
637 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26638 const WebMouseEvent& mouse_event =
639 *static_cast<const WebMouseEvent*>(input_event);
640 TRACE_EVENT2("renderer", "HandleMouseMove",
641 "x", mouse_event.x, "y", mouse_event.y);
642 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37643 }
644
[email protected]41d86852012-11-07 12:23:24645 if (WebInputEvent::isGestureEventType(input_event->type)) {
646 const WebGestureEvent& gesture_event =
647 *static_cast<const WebGestureEvent*>(input_event);
648 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
649 }
650
[email protected]3ebcc7c2013-01-09 05:34:46651 if (input_event->type == WebInputEvent::GestureTap ||
652 input_event->type == WebInputEvent::GestureLongPress)
653 resetInputMethod();
654
[email protected]67bfb83f2011-09-22 03:36:37655 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12656 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
657 suppress_next_char_events_ = false;
658 if (!processed && webwidget_)
659 processed = webwidget_->handleInputEvent(*input_event);
660 }
661
662 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
663 // it's not processed by webkit, then we need to suppress the upcoming Char
664 // events.
665 if (!processed && is_keyboard_shortcut)
666 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29667
[email protected]3d5c243b2012-11-30 00:26:01668 InputEventAckState ack_result = processed ?
669 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
670 if (!processed && input_event->type == WebInputEvent::TouchStart) {
671 const WebTouchEvent& touch_event =
672 *static_cast<const WebTouchEvent*>(input_event);
673 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
674 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
675 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
676 }
677
[email protected]a9fb30aa2011-10-06 06:58:46678 IPC::Message* response =
679 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
[email protected]3d5c243b2012-11-30 00:26:01680 ack_result);
[email protected]3391a0772012-03-28 00:32:07681 bool event_type_gets_rate_limited =
682 input_event->type == WebInputEvent::MouseMove ||
683 input_event->type == WebInputEvent::MouseWheel ||
684 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06685
686 bool frame_pending = paint_aggregator_.HasPendingUpdate();
687 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28688 frame_pending = compositor_ &&
689 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06690 }
691
[email protected]3391a0772012-03-28 00:32:07692 bool is_input_throttled =
[email protected]ce2b28e2012-08-09 15:53:57693 throttle_input_events_ &&
[email protected]8926c602013-01-23 05:32:06694 frame_pending;
[email protected]e2824412009-02-27 01:57:05695
[email protected]f8868d72012-04-27 19:13:03696 if (event_type_gets_rate_limited && is_input_throttled && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24697 // We want to rate limit the input events in this case, so we'll wait for
698 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17699 if (pending_input_event_ack_.get()) {
700 // As two different kinds of events could cause us to postpone an ack
701 // we send it now, if we have one pending. The Browser should never
702 // send us the same kind of event we are delaying the ack for.
703 Send(pending_input_event_ack_.release());
704 }
[email protected]12fbad812009-09-01 18:21:24705 pending_input_event_ack_.reset(response);
706 } else {
707 Send(response);
708 }
709
[email protected]3306f262012-09-21 19:20:42710#if defined(OS_ANDROID)
711 // Allow the IME to be shown when the focus changes as a consequence
712 // of a processed touch end event.
713 if (input_event->type == WebInputEvent::TouchEnd && processed)
714 UpdateTextInputState(SHOW_IME_IF_NEEDED);
715#endif
716
[email protected]5dd768212009-08-13 23:34:49717 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48718
[email protected]67bfb83f2011-09-22 03:36:37719 if (!prevent_default) {
720 if (WebInputEvent::isKeyboardEventType(input_event->type))
721 DidHandleKeyEvent();
722 if (WebInputEvent::isMouseEventType(input_event->type))
723 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24724 if (WebInputEvent::isTouchEventType(input_event->type))
725 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37726 }
initial.commit09911bf2008-07-26 23:55:29727}
728
729void RenderWidget::OnMouseCaptureLost() {
730 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28731 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29732}
733
734void RenderWidget::OnSetFocus(bool enable) {
735 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33736 if (webwidget_)
737 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29738}
739
740void RenderWidget::ClearFocus() {
741 // We may have got the focus from the browser before this gets processed, in
742 // which case we do not want to unfocus ourself.
743 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28744 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29745}
746
[email protected]2d5d09d52009-06-15 14:29:21747void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00748 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21749 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06750 TRACE_EVENT2("renderer", "PaintRect",
751 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45752
753 const bool kEnableGpuBenchmarking =
754 CommandLine::ForCurrentProcess()->HasSwitch(
755 switches::kEnableGpuBenchmarking);
[email protected]4fb66842009-12-04 21:41:00756 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21757
758 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00759 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
760 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03761
[email protected]699ab0d2009-04-23 23:19:14762 // If there is a custom background, tile it.
763 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14764 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11765 skia::RefPtr<SkShader> shader = skia::AdoptRef(
766 SkShader::CreateBitmapShader(background_,
767 SkShader::kRepeat_TileMode,
768 SkShader::kRepeat_TileMode));
769 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20770
771 // Use kSrc_Mode to handle background_ transparency properly.
772 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
773
774 // Canvas could contain multiple update rects. Clip to given rect so that
775 // we don't accidentally clear other update rects.
776 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44777 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48778 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14779 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20780 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14781 }
782
[email protected]719b36f2010-12-22 20:36:46783 // First see if this rect is a plugin that can paint itself faster.
784 TransportDIB* optimized_dib = NULL;
785 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20786 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46787 webkit::ppapi::PluginInstance* optimized_instance =
788 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
789 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20790 &optimized_copy_rect,
791 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46792 if (optimized_instance) {
793 // This plugin can be optimize-painted and we can just ask it to paint
794 // itself. We don't actually need the TransportDIB in this case.
795 //
796 // This is an optimization for PPAPI plugins that know they're on top of
797 // the page content. If this rect is inside such a plugin, we can save some
798 // time and avoid re-rendering the page content which we know will be
799 // covered by the plugin later (this time can be significant, especially
800 // for a playing movie that is invalidating a lot).
801 //
802 // In the plugin movie case, hopefully the similar call to
803 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
804 // painting, because that avoids copying the plugin image to a different
805 // paint rect. Unfortunately, if anything on the page is animating other
806 // than the movie, it break this optimization since the union of the
807 // invalid regions will be larger than the plugin.
808 //
809 // This code optimizes that case, where we can still avoid painting in
810 // WebKit and filling the background (which can be slow) and just painting
811 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
812 // required.
[email protected]63ab54262012-11-09 15:58:45813 base::TimeTicks paint_begin_ticks;
814 if (kEnableGpuBenchmarking)
815 paint_begin_ticks = base::TimeTicks::HighResNow();
816
[email protected]df59dd42012-09-14 22:56:30817 SkAutoCanvasRestore auto_restore(canvas, true);
818 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:46819 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27820 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:21821 canvas->restore();
[email protected]63ab54262012-11-09 15:58:45822 if (kEnableGpuBenchmarking) {
823 base::TimeDelta paint_time =
824 base::TimeTicks::HighResNow() - paint_begin_ticks;
825 if (!is_accelerated_compositing_active_)
[email protected]121cc752013-01-16 09:12:45826 software_stats_.totalPaintTime += paint_time;
[email protected]63ab54262012-11-09 15:58:45827 }
[email protected]719b36f2010-12-22 20:36:46828 } else {
829 // Normal painting case.
[email protected]63ab54262012-11-09 15:58:45830 base::TimeTicks paint_begin_ticks;
831 if (kEnableGpuBenchmarking)
832 paint_begin_ticks = base::TimeTicks::HighResNow();
833
[email protected]719b36f2010-12-22 20:36:46834 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]63ab54262012-11-09 15:58:45835
836 if (kEnableGpuBenchmarking) {
837 base::TimeDelta paint_time =
838 base::TimeTicks::HighResNow() - paint_begin_ticks;
839 if (!is_accelerated_compositing_active_)
[email protected]121cc752013-01-16 09:12:45840 software_stats_.totalPaintTime += paint_time;
[email protected]63ab54262012-11-09 15:58:45841 }
[email protected]719b36f2010-12-22 20:36:46842
843 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35844 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46845 }
initial.commit09911bf2008-07-26 23:55:29846
[email protected]4fb66842009-12-04 21:41:00847 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00848 canvas->restore();
[email protected]63ab54262012-11-09 15:58:45849
850 if (kEnableGpuBenchmarking) {
[email protected]63ab54262012-11-09 15:58:45851 int64 num_pixels_processed = rect.width() * rect.height();
852 software_stats_.totalPixelsPainted += num_pixels_processed;
853 software_stats_.totalPixelsRasterized += num_pixels_processed;
854 }
[email protected]4fb66842009-12-04 21:41:00855}
856
857void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
858 skia::PlatformCanvas* canvas) {
859 static bool kPaintBorder =
860 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
861 if (!kPaintBorder)
862 return;
863
[email protected]53d3f302009-12-21 04:42:05864 // Cycle through these colors to help distinguish new paint rects.
865 const SkColor colors[] = {
866 SkColorSetARGB(0x3F, 0xFF, 0, 0),
867 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
868 SkColorSetARGB(0x3F, 0, 0, 0xFF),
869 };
870 static int color_selector = 0;
871
[email protected]4fb66842009-12-04 21:41:00872 SkPaint paint;
873 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05874 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00875 paint.setStrokeWidth(1);
876
877 SkIRect irect;
878 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
879 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29880}
881
[email protected]52ccd0ea2011-02-16 01:09:05882void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30883 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:30884 if (!animation_update_pending_) {
885 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59886 return;
[email protected]921244e42011-07-20 16:36:30887 }
[email protected]bd37ae252011-06-03 01:28:18888 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59889 // Record when we fired (according to base::Time::Now()) relative to when
890 // we posted the task to quantify how much the base::Time/base::TimeTicks
891 // skew is affecting animations.
892 base::TimeDelta animation_callback_delay = base::Time::Now() -
893 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
894 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
895 animation_callback_delay,
896 base::TimeDelta::FromMilliseconds(0),
897 base::TimeDelta::FromMilliseconds(30),
898 25);
899 }
[email protected]65225772011-05-12 21:10:24900 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24901}
902
[email protected]52ccd0ea2011-02-16 01:09:05903void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59904 if (!animation_update_pending_)
905 return;
[email protected]bd37ae252011-06-03 01:28:18906
907 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:33908 base::TimeDelta animationInterval = IsRenderingVSynced() ?
909 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:18910
[email protected]7c4329e2011-02-18 22:02:59911 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:45912
913 // animation_floor_time_ is the earliest time that we should animate when
914 // using the dead reckoning software scheduler. If we're using swapbuffers
915 // complete callbacks to rate limit, we can ignore this floor.
916 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:30917 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:33918 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:18919 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59920 // running animation callbacks so that if a callback requests another
921 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:38922 animation_timer_.Stop();
923 animation_timer_.Start(FROM_HERE, animationInterval, this,
924 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:59925 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:28926 if (is_accelerated_compositing_active_ && compositor_) {
927 compositor_->layer_tree_host()->updateAnimations(
[email protected]8926c602013-01-23 05:32:06928 base::TimeTicks::Now());
929 } else {
930 webwidget_->animate(0.0);
931 }
[email protected]7c4329e2011-02-18 22:02:59932 return;
[email protected]5f8b1022011-01-21 23:34:50933 }
[email protected]bd37ae252011-06-03 01:28:18934 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:38935 if (!animation_timer_.IsRunning()) {
936 // This code uses base::Time::Now() to calculate the floor and next fire
937 // time because javascript's Date object uses base::Time::Now(). The
938 // message loop uses base::TimeTicks, which on windows can have a
939 // different granularity than base::Time.
940 // The upshot of all this is that this function might be called before
941 // base::Time::Now() has advanced past the animation_floor_time_. To
942 // avoid exposing this delay to javascript, we keep posting delayed
943 // tasks until base::Time::Now() has advanced far enough.
944 base::TimeDelta delay = animation_floor_time_ - now;
945 animation_timer_.Start(FROM_HERE, delay, this,
946 &RenderWidget::AnimationCallback);
947 }
[email protected]5f8b1022011-01-21 23:34:50948}
949
[email protected]bd37ae252011-06-03 01:28:18950bool RenderWidget::IsRenderingVSynced() {
951 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
952 // not caught by this check. This will lead to artificially low frame rates
953 // for people who force vsync off at a driver level and expect Chrome to speed
954 // up.
955 return !has_disable_gpu_vsync_switch_;
956}
957
[email protected]65225772011-05-12 21:10:24958void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06959 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24960 invalidation_task_posted_ = false;
961 DoDeferredUpdateAndSendInputAck();
962}
963
964void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05965 DoDeferredUpdate();
966
967 if (pending_input_event_ack_.get())
968 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21969}
970
[email protected]552e6002009-11-19 05:24:57971void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58972 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08973
[email protected]65225772011-05-12 21:10:24974 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29975 return;
[email protected]05a980d7a2012-02-07 22:16:42976
[email protected]fc4404d2012-11-07 19:53:30977 if (!init_complete_) {
978 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:42979 return;
980 }
[email protected]aa4117f2011-12-09 22:19:21981 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24982 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
983 return;
984 }
[email protected]9ca84622011-06-02 23:46:39985 if (is_accelerated_compositing_active_ &&
986 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24987 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
988 return;
989 }
initial.commit09911bf2008-07-26 23:55:29990
[email protected]552e6002009-11-19 05:24:57991 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:05992 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:57993 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29994 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24995 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29996 return;
997 }
998
[email protected]05a980d7a2012-02-07 22:16:42999 if (is_accelerated_compositing_active_)
1000 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1001
[email protected]0fb93f52011-05-18 23:13:561002 // Tracking of frame rate jitter
1003 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371004 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051005 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501006
[email protected]f98d7e3c2010-09-13 22:30:461007 // Layout may generate more invalidation. It may also enable the
1008 // GPU acceleration, so make sure to run layout before we send the
1009 // GpuRenderingActivated message.
1010 webwidget_->layout();
1011
[email protected]dcca3aa92012-02-17 23:03:371012 // The following two can result in further layout and possibly
1013 // enable GPU acceleration so they need to be called before any painting
1014 // is done.
[email protected]3306f262012-09-21 19:20:421015 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]dcca3aa92012-02-17 23:03:371016 UpdateSelectionBounds();
1017
[email protected]5f8b1022011-01-21 23:34:501018 // Suppress painting if nothing is dirty. This has to be done after updating
1019 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241020 if (!paint_aggregator_.HasPendingUpdate()) {
1021 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371022 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501023 return;
[email protected]65225772011-05-12 21:10:241024 }
[email protected]5f8b1022011-01-21 23:34:501025
[email protected]479b0172012-10-29 19:27:091026 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361027 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091028 ForceCompositingModeEnabled()) {
1029 webwidget_->enterForceCompositingMode(true);
1030 }
1031
[email protected]872ae5b2011-05-26 20:20:501032 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561033 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041034 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561035 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1036 delay,
1037 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411038 base::TimeDelta::FromMilliseconds(120),
1039 60);
[email protected]d0be63772011-12-20 23:18:041040 } else {
[email protected]0fb93f52011-05-18 23:13:561041 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1042 delay,
1043 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411044 base::TimeDelta::FromMilliseconds(120),
1045 60);
[email protected]d0be63772011-12-20 23:18:041046 }
[email protected]872ae5b2011-05-26 20:20:501047
1048 // Calculate filtered time per frame:
1049 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1050 filtered_time_per_frame_ =
1051 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561052 }
1053 last_do_deferred_update_time_ = frame_begin_ticks;
1054
[email protected]fef5e3972012-08-07 03:59:471055 if (!is_accelerated_compositing_active_) {
1056 software_stats_.numAnimationFrames++;
1057 software_stats_.numFramesSentToScreen++;
1058 }
1059
[email protected]552e6002009-11-19 05:24:571060 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291061 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301062 PaintAggregator::PendingUpdate update;
1063 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291064
[email protected]53d3f302009-12-21 04:42:051065 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181066 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291067
[email protected]29ed96a2012-02-04 18:12:161068 // Notify derived classes that we're about to initiate a paint.
1069 WillInitiatePaint();
1070
[email protected]ca4847f2010-09-24 05:39:151071 // A plugin may be able to do an optimized paint. First check this, in which
1072 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461073 // This optimization allows PPAPI plugins that declare themselves on top of
1074 // the page (like a traditional windowed plugin) to be able to animate (think
1075 // movie playing) without repeatedly re-painting the page underneath, or
1076 // copying the plugin backing store (since we can send the plugin's backing
1077 // store directly to the browser).
1078 //
1079 // This optimization only works when the entire invalid region is contained
1080 // within the plugin. There is a related optimization in PaintRect for the
1081 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151082 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151083 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201084 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211085 DCHECK(!pending_update_params_.get());
1086 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551087 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211088 pending_update_params_->scroll_rect = update.scroll_rect;
1089 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211090 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1091 pending_update_params_->flags = next_paint_flags_;
1092 pending_update_params_->scroll_offset = GetScrollOffset();
1093 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091094 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211095 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091096 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211097
[email protected]ca4847f2010-09-24 05:39:151098 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561099 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151100 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201101 &optimized_copy_rect,
1102 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271103 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471104 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211105 pending_update_params_->bitmap = dib->id();
1106 pending_update_params_->bitmap_rect = optimized_copy_location;
1107 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201108 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561109 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461110 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101111
1112 bool fractional_scale = device_scale_factor_ -
1113 static_cast<int>(device_scale_factor_) != 0;
1114 if (fractional_scale) {
1115 // Damage might not be DIP aligned. Inflate damage to compensate.
1116 bounds.Inset(-1, -1);
1117 bounds.Intersect(gfx::Rect(size_));
1118 }
1119
1120 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181121 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101122
[email protected]ca4847f2010-09-24 05:39:151123 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351124 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591125 pixel_bounds));
[email protected]f98d7e3c2010-09-13 22:30:461126 if (!canvas.get()) {
1127 NOTREACHED();
1128 return;
1129 }
[email protected]cef3362f2009-12-21 17:48:451130
[email protected]f98d7e3c2010-09-13 22:30:461131 // We may get back a smaller canvas than we asked for.
1132 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591133 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1134 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1135 pixel_bounds.set_width(canvas->getDevice()->width());
1136 pixel_bounds.set_height(canvas->getDevice()->height());
1137 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1138 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051139
[email protected]f98d7e3c2010-09-13 22:30:461140 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1141
[email protected]aa4117f2011-12-09 22:19:211142 pending_update_params_->bitmap = current_paint_buf_->id();
1143 pending_update_params_->bitmap_rect = bounds;
1144
1145 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461146 // The scroll damage is just another rectangle to paint and copy.
1147 copy_rects.swap(update.paint_rects);
1148 if (!scroll_damage.IsEmpty())
1149 copy_rects.push_back(scroll_damage);
1150
[email protected]4889bd212013-02-11 22:23:101151 for (size_t i = 0; i < copy_rects.size(); ++i) {
1152 gfx::Rect rect = copy_rects[i];
1153 if (fractional_scale) {
1154 // Damage might not be DPI aligned. Inflate rect to compensate.
1155 rect.Inset(-1, -1);
1156 }
1157 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1158 }
[email protected]60a50072012-01-11 02:05:351159
1160 // Software FPS tick for performance tests. The accelerated path traces the
1161 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1162 // NOTE: Tests may break if this event is renamed or moved.
1163 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW");
[email protected]f98d7e3c2010-09-13 22:30:461164 } else { // Accelerated compositing path
1165 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211166 // If painting is done via the gpu process then we don't set any damage
1167 // rects to save the browser process from doing unecessary work.
1168 pending_update_params_->bitmap_rect = bounds;
1169 pending_update_params_->scroll_rect = gfx::Rect();
1170 // We don't need an ack, because we're not sharing a DIB with the browser.
1171 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1172 // with the browser for the GPU surface.
1173 pending_update_params_->needs_ack = false;
[email protected]ab543072013-01-25 04:38:151174 Composite();
[email protected]f98d7e3c2010-09-13 22:30:461175 }
1176
[email protected]936c6f52011-12-13 01:35:261177 // If we're holding a pending input event ACK, send the ACK before sending the
1178 // UpdateReply message so we can receive another input event before the
1179 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1180 // the UpdateRect IPC message handler.
1181 if (pending_input_event_ack_.get())
1182 Send(pending_input_event_ack_.release());
1183
[email protected]ab543072013-01-25 04:38:151184 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211185 // OnSwapBuffersPosted), meaning a message has been added to the
1186 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1187 // the message now.
1188 if (pending_update_params_.get()) {
1189 // sending an ack to browser process that the paint is complete...
1190 update_reply_pending_ = pending_update_params_->needs_ack;
1191 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1192 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341193 }
[email protected]53d3f302009-12-21 04:42:051194
[email protected]29ed96a2012-02-04 18:12:161195 // If we're software rendering then we're done initiating the paint.
1196 if (!is_accelerated_compositing_active_)
1197 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291198}
1199
[email protected]ab543072013-01-25 04:38:151200void RenderWidget::Composite() {
1201 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281202 if (compositor_) // TODO(jamesr): Figure out how this can be null.
1203 compositor_->composite();
[email protected]ab543072013-01-25 04:38:151204}
1205
initial.commit09911bf2008-07-26 23:55:291206///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461207// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291208
[email protected]4873c7d2009-07-16 06:36:281209void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]479b0172012-10-29 19:27:091210 TRACE_EVENT2("renderer", "RenderWidget::didInvalidateRect",
1211 "width", rect.width, "height", rect.height);
[email protected]552e6002009-11-19 05:24:571212 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481213 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181214 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571215 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291216 return;
1217
[email protected]552e6002009-11-19 05:24:571218 paint_aggregator_.InvalidateRect(damaged_rect);
1219
1220 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241221 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571222 return;
1223 if (!paint_aggregator_.HasPendingUpdate())
1224 return;
[email protected]aa4117f2011-12-09 22:19:211225 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241226 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1227 return;
1228
1229 // When GPU rendering, combine pending animations and invalidations into
1230 // a single update.
[email protected]816edc62012-03-17 01:27:221231 if (is_accelerated_compositing_active_ &&
1232 animation_update_pending_ &&
1233 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571234 return;
1235
1236 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291237 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1238 // on the call stack.
1239 // 2) Allows us to collect more damage rects before painting to help coalesce
1240 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241241 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211242 MessageLoop::current()->PostTask(
1243 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291244}
1245
[email protected]990278ff2012-11-13 02:12:551246void RenderWidget::didScrollRect(int dx, int dy,
1247 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461248 // Drop scrolls on the floor when we are in compositing mode.
1249 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561250 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461251 return;
1252
[email protected]552e6002009-11-19 05:24:571253 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481254 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181255 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571256 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291257 return;
1258
[email protected]990278ff2012-11-13 02:12:551259 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571260
1261 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241262 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571263 return;
1264 if (!paint_aggregator_.HasPendingUpdate())
1265 return;
[email protected]aa4117f2011-12-09 22:19:211266 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241267 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1268 return;
1269
1270 // When GPU rendering, combine pending animations and invalidations into
1271 // a single update.
[email protected]816edc62012-03-17 01:27:221272 if (is_accelerated_compositing_active_ &&
1273 animation_update_pending_ &&
1274 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571275 return;
1276
1277 // Perform updating asynchronously. This serves two purposes:
1278 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1279 // on the call stack.
1280 // 2) Allows us to collect more damage rects before painting to help coalesce
1281 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241282 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211283 MessageLoop::current()->PostTask(
1284 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291285}
1286
[email protected]244ac1892011-12-02 17:04:471287void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091288 if (size_.width() != new_size.width || size_.height() != new_size.height) {
1289 size_ = new_size;
1290 need_update_rect_for_auto_resize_ = true;
[email protected]240b5c32012-11-09 19:17:181291 // If we don't clear PaintAggregator after changing autoResize state, then
1292 // we might end up in a situation where bitmap_rect is larger than the
1293 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1294 // with invalid damage rects.
1295 paint_aggregator_.ClearPendingUpdate();
[email protected]ea3ee0a2012-05-15 03:43:091296 }
[email protected]244ac1892011-12-02 17:04:471297}
1298
[email protected]91acd1c2012-03-14 08:32:391299void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221300 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1301
[email protected]c63b4d42012-04-26 01:01:071302#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211303 if (!is_accelerated_compositing_active_) {
1304 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1305 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1306 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1307 // going to switch to accelerated compositing, the GPU process may need
1308 // round-trips to the browser's UI thread before finishing the frame,
1309 // causing deadlocks if we delay the UpdateRect until we receive the
1310 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071311 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1312 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211313 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1314 }
[email protected]c63b4d42012-04-26 01:01:071315#endif
[email protected]aa4117f2011-12-09 22:19:211316
[email protected]ea162f92011-10-04 23:08:221317 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421318 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241319 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221320}
1321
1322void RenderWidget::didDeactivateCompositor() {
1323 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1324
1325 is_accelerated_compositing_active_ = false;
1326 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1327 routing_id_, is_accelerated_compositing_active_));
1328
[email protected]ea162f92011-10-04 23:08:221329 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241330 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091331
1332 // In single-threaded mode, we exit force compositing mode and re-enter in
1333 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1334 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1335 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361336 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091337 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561338}
1339
[email protected]8926c602013-01-23 05:32:061340void RenderWidget::initializeLayerTreeView(
1341 WebKit::WebLayerTreeViewClient* client,
1342 const WebKit::WebLayer& root_layer,
1343 const WebKit::WebLayerTreeView::Settings& settings) {
[email protected]ba91a792013-02-06 09:48:281344 compositor_ = RenderWidgetCompositor::Create(this, client, settings);
1345 if (!compositor_)
[email protected]8926c602013-01-23 05:32:061346 return;
[email protected]ba91a792013-02-06 09:48:281347
1348 compositor_->setRootLayer(root_layer);
1349 if (init_complete_)
1350 compositor_->setSurfaceReady();
[email protected]8926c602013-01-23 05:32:061351}
1352
1353WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281354 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061355}
1356
[email protected]9ed83fe2013-02-27 01:52:281357void RenderWidget::suppressCompositorScheduling(bool enable) {
1358 if (compositor_)
1359 compositor_->SetSuppressScheduleComposite(enable);
1360}
1361
[email protected]9cd43a62012-03-26 08:03:561362void RenderWidget::willBeginCompositorFrame() {
1363 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371364
1365 DCHECK(RenderThreadImpl::current()->compositor_thread());
1366
1367 // The following two can result in further layout and possibly
1368 // enable GPU acceleration so they need to be called before any painting
1369 // is done.
[email protected]2d354272013-01-14 00:59:061370 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]abe8b3a2012-03-28 21:19:371371 UpdateSelectionBounds();
1372
[email protected]9cd43a62012-03-26 08:03:561373 WillInitiatePaint();
1374}
1375
[email protected]3391a0772012-03-28 00:32:071376void RenderWidget::didBecomeReadyForAdditionalInput() {
1377 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
1378 if (pending_input_event_ack_.get())
1379 Send(pending_input_event_ack_.release());
1380}
1381
[email protected]6fceb912013-02-15 06:24:151382void RenderWidget::DidCommitCompositorFrame() {
1383}
1384
[email protected]58264a32011-11-17 23:36:151385void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501386 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351387 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1388 // NOTE: Tests may break if this event is renamed or moved.
1389 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU");
[email protected]29ed96a2012-02-04 18:12:161390 // Notify subclasses that we initiated the paint operation.
1391 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151392}
1393
1394void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181395 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1396
1397 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561398 DidFlushPaint();
1399
[email protected]aa4117f2011-12-09 22:19:211400 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151401 return;
1402
[email protected]ea3ee0a2012-05-15 03:43:091403 if (!next_paint_flags_ &&
1404 !need_update_rect_for_auto_resize_ &&
1405 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151406 return;
[email protected]ea3ee0a2012-05-15 03:43:091407 }
[email protected]58264a32011-11-17 23:36:151408
1409 ViewHostMsg_UpdateRect_Params params;
1410 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151411 params.plugin_window_moves.swap(plugin_window_moves_);
1412 params.flags = next_paint_flags_;
1413 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121414 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091415 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151416
1417 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1418 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091419 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151420}
1421
[email protected]f98d7e3c2010-09-13 22:30:461422void RenderWidget::scheduleComposite() {
[email protected]479b0172012-10-29 19:27:091423 TRACE_EVENT0("gpu", "RenderWidget::scheduleComposite");
[email protected]74dc7642013-01-25 04:38:261424 if (RenderThreadImpl::current()->compositor_thread() &&
[email protected]ba91a792013-02-06 09:48:281425 compositor_) {
1426 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041427 } else {
[email protected]c3d45532011-10-07 19:20:401428 // TODO(nduca): replace with something a little less hacky. The reason this
1429 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1430 // contains a lot of host-renderer synchronization logic that is still
1431 // important for the accelerated compositing case. The option of simply
1432 // duplicating all that code is less desirable than "faking out" the
1433 // invalidation path using a magical damage rect.
1434 didInvalidateRect(WebRect(0, 0, 1, 1));
1435 }
[email protected]f98d7e3c2010-09-13 22:30:461436}
1437
[email protected]5f8b1022011-01-21 23:34:501438void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201439 if (animation_update_pending_)
1440 return;
1441
[email protected]921244e42011-07-20 16:36:301442 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201443 animation_update_pending_ = true;
1444 if (!animation_timer_.IsRunning()) {
1445 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1446 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211447 }
[email protected]5f8b1022011-01-21 23:34:501448}
1449
[email protected]4873c7d2009-07-16 06:36:281450void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301451 // TODO(darin): Eliminate this temporary.
1452 WebCursor cursor(cursor_info);
1453
initial.commit09911bf2008-07-26 23:55:291454 // Only send a SetCursor message if we need to make a change.
1455 if (!current_cursor_.IsEqual(cursor)) {
1456 current_cursor_ = cursor;
1457 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1458 }
1459}
1460
1461// We are supposed to get a single call to Show for a newly created RenderWidget
1462// that was created via RenderWidget::CreateWebView. So, we wait until this
1463// point to dispatch the ShowWidget message.
1464//
1465// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281466// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291467//
[email protected]4873c7d2009-07-16 06:36:281468void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291469 DCHECK(!did_show_) << "received extraneous Show call";
1470 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1471 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1472
[email protected]8de12d942010-11-17 20:42:441473 if (did_show_)
1474 return;
1475
1476 did_show_ = true;
1477 // NOTE: initial_pos_ may still have its default values at this point, but
1478 // that's okay. It'll be ignored if as_popup is false, or the browser
1479 // process will impose a default position otherwise.
1480 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1481 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291482}
1483
[email protected]4873c7d2009-07-16 06:36:281484void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291485}
1486
[email protected]4873c7d2009-07-16 06:36:281487void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291488}
1489
[email protected]2533ce12009-05-09 00:02:241490void RenderWidget::DoDeferredClose() {
1491 Send(new ViewHostMsg_Close(routing_id_));
1492}
1493
[email protected]4873c7d2009-07-16 06:36:281494void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321495 if (is_swapped_out_) {
1496 // This widget is currently swapped out, and the active widget is in a
1497 // different process. Have the browser route the close request to the
1498 // active widget instead, so that the correct unload handlers are run.
1499 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1500 return;
1501 }
1502
initial.commit09911bf2008-07-26 23:55:291503 // If a page calls window.close() twice, we'll end up here twice, but that's
1504 // OK. It is safe to send multiple Close messages.
1505
[email protected]2533ce12009-05-09 00:02:241506 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1507 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1508 // could be closed before the JS finishes executing. So instead, post a
1509 // message back to the message loop, which won't run until the JS is
1510 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211511 MessageLoop::current()->PostTask(
1512 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291513}
1514
1515void RenderWidget::Close() {
1516 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061517 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281518 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281519 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291520 webwidget_ = NULL;
1521 }
1522}
1523
[email protected]4873c7d2009-07-16 06:36:281524WebRect RenderWidget::windowRect() {
1525 if (pending_window_rect_count_)
1526 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241527
[email protected]80ad8622012-11-07 16:33:031528 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291529}
1530
[email protected]8a9d6ca32011-06-06 20:11:301531void RenderWidget::setToolTipText(const WebKit::WebString& text,
1532 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541533 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301534}
1535
[email protected]4873c7d2009-07-16 06:36:281536void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291537 if (did_show_) {
1538 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241539 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291540 } else {
1541 initial_pos_ = pos;
1542 }
1543}
1544
[email protected]2533ce12009-05-09 00:02:241545void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1546 pending_window_rect_ = rect;
1547 pending_window_rect_count_++;
1548}
1549
[email protected]4873c7d2009-07-16 06:36:281550WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241551 if (pending_window_rect_count_) {
1552 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1553 // the RootWindowRect is probably going to return wrong results since the
1554 // browser may not have processed the Move yet. There isn't really anything
1555 // good to do in this case, and it shouldn't happen - since this size is
1556 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281557 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241558 }
1559
[email protected]80ad8622012-11-07 16:33:031560 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371561}
1562
[email protected]4873c7d2009-07-16 06:36:281563WebRect RenderWidget::windowResizerRect() {
1564 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191565}
1566
[email protected]fa7b1dc2010-06-23 17:53:041567void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031568 // To prevent this renderer process from sending unnecessary IPC messages to
1569 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041570 // only during the input method attached to the browser process is active.
1571 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291572}
1573
[email protected]58b48a0d2012-06-13 07:01:351574void RenderWidget::UpdateCompositionInfo(
1575 const ui::Range& range,
1576 const std::vector<gfx::Rect>& character_bounds) {
1577 if (!ShouldUpdateCompositionInfo(range, character_bounds))
1578 return;
1579 composition_character_bounds_ = character_bounds;
1580 composition_range_ = range;
1581 Send(new ViewHostMsg_ImeCompositionRangeChanged(
1582 routing_id(), composition_range_, composition_character_bounds_));
1583}
1584
[email protected]fa7b1dc2010-06-23 17:53:041585void RenderWidget::OnImeSetComposition(
1586 const string16& text,
1587 const std::vector<WebCompositionUnderline>& underlines,
1588 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281589 if (!webwidget_)
1590 return;
[email protected]e8f775f2013-02-14 21:00:501591 DCHECK(!handling_ime_event_);
1592 handling_ime_event_ = true;
[email protected]d4cff272011-05-02 15:46:011593 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041594 text, WebVector<WebCompositionUnderline>(underlines),
1595 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011596 // Setting the IME composition was successful. Send the new composition
1597 // range to the browser.
1598 ui::Range range(ui::Range::InvalidRange());
1599 size_t location, length;
1600 if (webwidget_->compositionRange(&location, &length)) {
1601 range.set_start(location);
1602 range.set_end(location + length);
1603 }
1604 // The IME was cancelled via the Esc key, so just send back the caret.
1605 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1606 range.set_start(location);
1607 range.set_end(location + length);
1608 }
[email protected]58b48a0d2012-06-13 07:01:351609 std::vector<gfx::Rect> character_bounds;
1610 GetCompositionCharacterBounds(&character_bounds);
1611 UpdateCompositionInfo(range, character_bounds);
[email protected]d4cff272011-05-02 15:46:011612 } else {
[email protected]fa7b1dc2010-06-23 17:53:041613 // If we failed to set the composition text, then we need to let the browser
1614 // process to cancel the input method's ongoing composition session, to make
1615 // sure we are in a consistent state.
1616 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011617
1618 // Send an updated IME range with just the caret range.
1619 ui::Range range(ui::Range::InvalidRange());
1620 size_t location, length;
1621 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1622 range.set_start(location);
1623 range.set_end(location + length);
1624 }
[email protected]58b48a0d2012-06-13 07:01:351625 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]7f00efa2010-04-15 05:01:261626 }
[email protected]e8f775f2013-02-14 21:00:501627 handling_ime_event_ = false;
1628 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]fa7b1dc2010-06-23 17:53:041629}
1630
[email protected]4de6d1692011-10-12 08:45:441631void RenderWidget::OnImeConfirmComposition(
1632 const string16& text, const ui::Range& replacement_range) {
[email protected]d0be63772011-12-20 23:18:041633 if (!webwidget_)
1634 return;
[email protected]e8f775f2013-02-14 21:00:501635 DCHECK(!handling_ime_event_);
1636 handling_ime_event_ = true;
[email protected]d0be63772011-12-20 23:18:041637 handling_input_event_ = true;
1638 webwidget_->confirmComposition(text);
1639 handling_input_event_ = false;
1640
[email protected]d4cff272011-05-02 15:46:011641 // Send an updated IME range with just the caret range.
1642 ui::Range range(ui::Range::InvalidRange());
1643 size_t location, length;
1644 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1645 range.set_start(location);
1646 range.set_end(location + length);
1647 }
[email protected]58b48a0d2012-06-13 07:01:351648 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]e8f775f2013-02-14 21:00:501649 handling_ime_event_ = false;
1650 UpdateTextInputState(DO_NOT_SHOW_IME);
initial.commit09911bf2008-07-26 23:55:291651}
1652
[email protected]948f7ab72010-05-28 23:48:081653// This message causes the renderer to render an image of the
1654// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251655void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1656 int tag,
1657 const gfx::Size& page_size,
1658 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001659 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1660 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251661 // Close our unused handle.
1662#if defined(OS_WIN)
1663 ::CloseHandle(dib_handle);
1664#elif defined(OS_MACOSX)
1665 base::SharedMemory::CloseHandle(dib_handle);
1666#endif
1667 }
[email protected]d65adb12010-04-28 17:26:491668 return;
[email protected]45c6aad32010-11-11 04:46:251669 }
[email protected]d65adb12010-04-28 17:26:491670
[email protected]948f7ab72010-05-28 23:48:081671 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491672 // If one of these is empty, then we just return the dib we were
1673 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091674 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491675 return;
1676 }
1677
1678 // Map the given DIB ID into this process, and unmap it at the end
1679 // of this function.
[email protected]45c6aad32010-11-11 04:46:251680 scoped_ptr<TransportDIB> paint_at_size_buffer(
1681 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301682
[email protected]4b01b962012-10-09 23:17:351683 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281684 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351685 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281686 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511687 gfx::Size canvas_size = page_size_in_pixel;
1688 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491689 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511690 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491691 static_cast<float>(canvas_size.height());
1692
[email protected]ee8d6fd2010-05-26 17:05:481693 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491694 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1695 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481696 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491697
[email protected]36808ad2010-10-20 19:18:301698 scoped_ptr<skia::PlatformCanvas> canvas(
1699 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1700 canvas_size.height()));
1701 if (!canvas.get()) {
1702 NOTREACHED();
1703 return;
1704 }
1705
[email protected]d65adb12010-04-28 17:26:491706 // Reset bounds to what we actually received, but they should be the
1707 // same.
1708 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1709 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1710 bounds.set_width(canvas->getDevice()->width());
1711 bounds.set_height(canvas->getDevice()->height());
1712
1713 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081714 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491715 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1716
[email protected]948f7ab72010-05-28 23:48:081717 // Have to make sure we're laid out at the right size before
1718 // rendering.
1719 gfx::Size old_size = webwidget_->size();
1720 webwidget_->resize(page_size);
1721 webwidget_->layout();
1722
[email protected]d65adb12010-04-28 17:26:491723 // Paint the entire thing (using original bounds, not scaled bounds).
1724 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1725 canvas->restore();
1726
[email protected]948f7ab72010-05-28 23:48:081727 // Return the widget to its previous size.
1728 webwidget_->resize(old_size);
1729
[email protected]c88c9442010-07-19 18:55:091730 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491731}
1732
[email protected]3d9ec5052013-01-02 22:05:251733void RenderWidget::OnRepaint(const gfx::Size& size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:121734 // During shutdown we can just ignore this message.
1735 if (!webwidget_)
1736 return;
1737
1738 set_next_paint_is_repaint_ack();
[email protected]b89c90762013-01-06 20:29:301739 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:281740 if (compositor_)
1741 compositor_->setNeedsRedraw();
[email protected]f98d7e3c2010-09-13 22:30:461742 scheduleComposite();
1743 } else {
1744 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1745 didInvalidateRect(repaint_rect);
1746 }
[email protected]ec7dc112008-08-06 05:30:121747}
1748
[email protected]3d9ec5052013-01-02 22:05:251749void RenderWidget::OnSmoothScrollCompleted(int gesture_id) {
[email protected]0e241b4b2012-08-18 09:06:271750 PendingSmoothScrollGestureMap::iterator it =
1751 pending_smooth_scroll_gestures_.find(gesture_id);
1752 DCHECK(it != pending_smooth_scroll_gestures_.end());
1753 it->second.Run();
1754 pending_smooth_scroll_gestures_.erase(it);
1755}
1756
[email protected]4873c7d2009-07-16 06:36:281757void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111758 if (!webwidget_)
1759 return;
[email protected]4873c7d2009-07-16 06:36:281760 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111761}
1762
[email protected]6131a642012-06-15 23:26:531763void RenderWidget::OnScreenInfoChanged(
1764 const WebKit::WebScreenInfo& screen_info) {
1765 screen_info_ = screen_info;
[email protected]468ac582012-11-20 00:53:191766 SetDeviceScaleFactor(screen_info.deviceScaleFactor);
[email protected]6131a642012-06-15 23:26:531767}
1768
[email protected]80ad8622012-11-07 16:33:031769void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
1770 const gfx::Rect& window_screen_rect) {
1771 view_screen_rect_ = view_screen_rect;
1772 window_screen_rect_ = window_screen_rect;
1773 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
1774}
1775
[email protected]105dffb42013-02-20 03:46:211776#if defined(OS_ANDROID)
1777void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
1778 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
1779}
1780#endif
1781
[email protected]468ac582012-11-20 00:53:191782void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
1783 if (device_scale_factor_ == device_scale_factor)
1784 return;
1785
1786 device_scale_factor_ = device_scale_factor;
1787
1788 if (!is_accelerated_compositing_active_) {
1789 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
1790 } else {
1791 scheduleComposite();
1792 }
1793}
1794
[email protected]719b36f2010-12-22 20:36:461795webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151796 const gfx::Rect& paint_bounds,
1797 TransportDIB** dib,
1798 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:201799 gfx::Rect* clip,
1800 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:461801 // Bare RenderWidgets don't support optimized plugin painting.
1802 return NULL;
[email protected]ca4847f2010-09-24 05:39:151803}
1804
[email protected]ceb36f7d2012-10-31 18:33:241805gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521806 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:241807 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:521808}
1809
[email protected]bee16aab2009-08-26 15:55:031810void RenderWidget::SetHidden(bool hidden) {
1811 if (is_hidden_ == hidden)
1812 return;
1813
1814 // The status has changed. Tell the RenderThread about it.
1815 is_hidden_ = hidden;
1816 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271817 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031818 else
[email protected]380244092011-10-07 17:26:271819 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031820}
1821
[email protected]2b624c562011-10-27 22:58:261822void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261823 if (!webwidget_)
1824 return;
1825
1826 if (is_fullscreen_) {
1827 webwidget_->willExitFullScreen();
1828 } else {
1829 webwidget_->willEnterFullScreen();
1830 }
[email protected]2b624c562011-10-27 22:58:261831}
1832
1833void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261834 if (!webwidget_)
1835 return;
1836
1837 if (is_fullscreen_) {
1838 webwidget_->didEnterFullScreen();
1839 } else {
1840 webwidget_->didExitFullScreen();
1841 }
[email protected]2b624c562011-10-27 22:58:261842}
1843
[email protected]699ab0d2009-04-23 23:19:141844void RenderWidget::SetBackground(const SkBitmap& background) {
1845 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461846
[email protected]699ab0d2009-04-23 23:19:141847 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281848 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141849}
1850
[email protected]674741932009-02-04 23:44:461851bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051852 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461853}
1854
1855bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051856 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461857}
1858
1859void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051860 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461861}
1862
1863void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051864 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461865}
1866
1867void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051868 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461869}
1870
[email protected]b18583c2012-12-18 06:55:271871static bool IsDateTimeInput(ui::TextInputType type) {
1872 return type == ui::TEXT_INPUT_TYPE_DATE ||
1873 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
1874 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
1875 type == ui::TEXT_INPUT_TYPE_MONTH ||
1876 type == ui::TEXT_INPUT_TYPE_TIME ||
1877 type == ui::TEXT_INPUT_TYPE_WEEK;
1878}
1879
1880
[email protected]3306f262012-09-21 19:20:421881void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
[email protected]e8f775f2013-02-14 21:00:501882 if (handling_ime_event_)
1883 return;
[email protected]3306f262012-09-21 19:20:421884 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
1885 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291886 return;
[email protected]ad26ef42011-06-17 07:59:451887 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:271888 if (IsDateTimeInput(new_type))
1889 return; // Not considered as a text input field in WebKit/Chromium.
1890
[email protected]5b739cb2012-08-21 20:35:211891 WebKit::WebTextInputInfo new_info;
1892 if (webwidget_)
1893 new_info = webwidget_->textInputInfo();
1894
[email protected]ad26ef42011-06-17 07:59:451895 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:211896
[email protected]3306f262012-09-21 19:20:421897 // Only sends text input params if they are changed or if the ime should be
1898 // shown.
1899 if (show_ime_if_needed || (text_input_type_ != new_type
1900 || text_input_info_ != new_info
1901 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:211902 ViewHostMsg_TextInputState_Params p;
1903 p.type = new_type;
1904 p.value = new_info.value.utf8();
1905 p.selection_start = new_info.selectionStart;
1906 p.selection_end = new_info.selectionEnd;
1907 p.composition_start = new_info.compositionStart;
1908 p.composition_end = new_info.compositionEnd;
1909 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:421910 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:211911 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
1912
1913 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:041914 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451915 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:291916 }
initial.commit09911bf2008-07-26 23:55:291917}
1918
[email protected]7c8873e2013-02-05 08:03:011919void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
1920 WebRect focus_webrect;
1921 WebRect anchor_webrect;
1922 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
1923 *focus = focus_webrect;
1924 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:321925}
1926
[email protected]e99ef6f2011-10-16 01:13:001927void RenderWidget::UpdateSelectionBounds() {
1928 if (!webwidget_)
1929 return;
1930
[email protected]7c8873e2013-02-05 08:03:011931 ViewHostMsg_SelectionBounds_Params params;
1932 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
1933 if (selection_anchor_rect_ != params.anchor_rect ||
1934 selection_focus_rect_ != params.focus_rect) {
1935 selection_anchor_rect_ = params.anchor_rect;
1936 selection_focus_rect_ = params.focus_rect;
1937 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:291938 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:011939 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:351940 }
[email protected]e99ef6f2011-10-16 01:13:001941
[email protected]58b48a0d2012-06-13 07:01:351942 std::vector<gfx::Rect> character_bounds;
1943 GetCompositionCharacterBounds(&character_bounds);
1944 UpdateCompositionInfo(composition_range_, character_bounds);
1945}
1946
1947bool RenderWidget::ShouldUpdateCompositionInfo(
1948 const ui::Range& range,
1949 const std::vector<gfx::Rect>& bounds) {
1950 if (composition_range_ != range)
1951 return true;
1952 if (bounds.size() != composition_character_bounds_.size())
1953 return true;
1954 for (size_t i = 0; i < bounds.size(); ++i) {
1955 if (bounds[i] != composition_character_bounds_[i])
1956 return true;
1957 }
1958 return false;
[email protected]e99ef6f2011-10-16 01:13:001959}
1960
[email protected]73bf95812011-10-12 11:38:321961// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451962COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1963 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1964COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1965 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1966COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1967 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181968COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1969 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1970COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1971 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1972COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1973 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1974COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1975 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1976COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1977 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:001978COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
1979 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
1980COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
1981 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
1982COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
1983 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
1984COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
1985 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
1986COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
1987 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
1988COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
1989 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:011990COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
1991 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
1992COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
1993 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451994
[email protected]5b739cb2012-08-21 20:35:211995ui::TextInputType RenderWidget::WebKitToUiTextInputType(
1996 WebKit::WebTextInputType type) {
1997 // Check the type is in the range representable by ui::TextInputType.
1998 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
1999 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2000 return static_cast<ui::TextInputType>(type);
2001}
2002
[email protected]ad26ef42011-06-17 07:59:452003ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272004 if (webwidget_)
2005 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452006 return ui::TEXT_INPUT_TYPE_NONE;
2007}
2008
[email protected]58b48a0d2012-06-13 07:01:352009void RenderWidget::GetCompositionCharacterBounds(
2010 std::vector<gfx::Rect>* bounds) {
2011 DCHECK(bounds);
2012 bounds->clear();
2013}
2014
[email protected]ad26ef42011-06-17 07:59:452015bool RenderWidget::CanComposeInline() {
2016 return true;
[email protected]56ea1a62011-05-30 07:05:572017}
2018
[email protected]4873c7d2009-07-16 06:36:282019WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042020 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282021}
2022
[email protected]f660d9c2012-06-06 18:31:212023float RenderWidget::deviceScaleFactor() {
2024 return device_scale_factor_;
2025}
2026
[email protected]fa7b1dc2010-06-23 17:53:042027void RenderWidget::resetInputMethod() {
2028 if (!input_method_is_active_)
2029 return;
2030
2031 // If the last text input type is not None, then we should finish any
2032 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452033 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042034 // If a composition text exists, then we need to let the browser process
2035 // to cancel the input method's ongoing composition session.
2036 if (webwidget_->confirmComposition())
2037 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2038 }
[email protected]d4cff272011-05-02 15:46:012039
2040 // Send an updated IME range with the current caret rect.
2041 ui::Range range(ui::Range::InvalidRange());
2042 size_t location, length;
2043 if (webwidget_->caretOrSelectionRange(&location, &length)) {
2044 range.set_start(location);
2045 range.set_end(location + length);
2046 }
[email protected]58b48a0d2012-06-13 07:01:352047
2048 UpdateCompositionInfo(range, std::vector<gfx::Rect>());
[email protected]fa7b1dc2010-06-23 17:53:042049}
2050
[email protected]c68c3e4e2013-01-24 00:36:562051void RenderWidget::didHandleGestureEvent(
2052 const WebGestureEvent& event,
2053 bool event_cancelled) {
2054#if defined(OS_ANDROID)
2055 if (event_cancelled)
2056 return;
2057 if (event.type == WebInputEvent::GestureTap ||
2058 event.type == WebInputEvent::GestureLongPress) {
2059 UpdateTextInputState(SHOW_IME_IF_NEEDED);
2060 }
2061#endif
2062}
2063
[email protected]f103ab72009-09-02 17:10:592064void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:502065 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292066 size_t i = 0;
2067 for (; i < plugin_window_moves_.size(); ++i) {
2068 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582069 if (move.rects_valid) {
2070 plugin_window_moves_[i] = move;
2071 } else {
2072 plugin_window_moves_[i].visible = move.visible;
2073 }
initial.commit09911bf2008-07-26 23:55:292074 break;
2075 }
2076 }
2077
2078 if (i == plugin_window_moves_.size())
2079 plugin_window_moves_.push_back(move);
2080}
[email protected]268654772009-08-06 23:02:042081
2082void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2083 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2084 i != plugin_window_moves_.end(); ++i) {
2085 if (i->window == window) {
2086 plugin_window_moves_.erase(i);
2087 break;
2088 }
2089 }
2090}
[email protected]67bfb83f2011-09-22 03:36:372091
[email protected]b63d58d2012-11-26 22:37:442092void RenderWidget::GetRenderingStats(
2093 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282094 if (compositor_)
2095 compositor_->layer_tree_host()->renderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442096
2097 stats.rendering_stats.numAnimationFrames +=
2098 software_stats_.numAnimationFrames;
2099 stats.rendering_stats.numFramesSentToScreen +=
2100 software_stats_.numFramesSentToScreen;
[email protected]121cc752013-01-16 09:12:452101 stats.rendering_stats.totalPaintTime +=
2102 software_stats_.totalPaintTime;
[email protected]b63d58d2012-11-26 22:37:442103 stats.rendering_stats.totalPixelsPainted +=
2104 software_stats_.totalPixelsPainted;
[email protected]121cc752013-01-16 09:12:452105 stats.rendering_stats.totalRasterizeTime +=
2106 software_stats_.totalRasterizeTime;
[email protected]b63d58d2012-11-26 22:37:442107 stats.rendering_stats.totalPixelsRasterized +=
2108 software_stats_.totalPixelsRasterized;
[email protected]fef5e3972012-08-07 03:59:472109}
2110
[email protected]e9ff79c2012-10-19 21:31:262111bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522112 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2113 if (!gpu_channel)
2114 return false;
2115
2116 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2117}
2118
[email protected]0e241b4b2012-08-18 09:06:272119void RenderWidget::BeginSmoothScroll(
2120 bool down,
[email protected]ebd8b562012-10-09 14:44:292121 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192122 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292123 int mouse_event_x,
2124 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272125 DCHECK(!callback.is_null());
2126 int id = next_smooth_scroll_gesture_id_++;
[email protected]267909d2012-10-20 04:36:192127
2128 ViewHostMsg_BeginSmoothScroll_Params params;
2129 params.scroll_down = down;
2130 params.pixels_to_scroll = pixels_to_scroll;
2131 params.mouse_event_x = mouse_event_x;
2132 params.mouse_event_y = mouse_event_y;
2133
2134 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, id, params));
[email protected]0e241b4b2012-08-18 09:06:272135 pending_smooth_scroll_gestures_.insert(std::make_pair(id, callback));
[email protected]a39ca1652012-07-13 21:30:582136}
2137
[email protected]67bfb83f2011-09-22 03:36:372138bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2139 return false;
2140}
[email protected]c3d45532011-10-07 19:20:402141
[email protected]41d86852012-11-07 12:23:242142bool RenderWidget::WillHandleGestureEvent(
2143 const WebKit::WebGestureEvent& event) {
2144 return false;
2145}
2146
[email protected]3d5c243b2012-11-30 00:26:012147bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2148 return true;
2149}
2150
[email protected]e9ff79c2012-10-19 21:31:262151} // namespace content