blob: c56f64eacf6584595fe45da932a4a87f490f4ddd [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"
[email protected]b256eca2013-07-11 10:57:4012#include "base/memory/singleton.h"
[email protected]aaf68892013-07-18 00:11:3013#include "base/message_loop/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3814#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2115#include "base/stl_util.h"
[email protected]74ebfb12013-06-07 20:48:0016#include "base/strings/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4817#include "build/build_config.h"
[email protected]681ccff2013-03-18 06:13:5218#include "cc/base/switches.h"
[email protected]7f0d825f2013-03-18 07:24:3019#include "cc/output/output_surface.h"
[email protected]556fd292013-03-18 08:03:0420#include "cc/trees/layer_tree_host.h"
[email protected]29e2fb42013-07-19 01:13:4721#include "content/child/npapi/webplugin.h"
[email protected]0634cdd42013-08-16 00:46:0922#include "content/common/gpu/client/context_provider_command_buffer.h"
[email protected]ed7defa2013-03-12 21:29:5923#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]c084330e02013-04-27 01:08:1524#include "content/common/input_messages.h"
[email protected]992db4c2011-05-12 15:37:1525#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5026#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2927#include "content/public/common/content_switches.h"
[email protected]953bd0062013-08-01 00:58:4028#include "content/renderer/cursor_utils.h"
[email protected]ed7defa2013-03-12 21:29:5929#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]2847b222013-04-06 00:59:2430#include "content/renderer/gpu/compositor_software_output_device.h"
[email protected]36e5ff12013-06-11 12:19:2931#include "content/renderer/gpu/delegated_compositor_output_surface.h"
[email protected]ea5f70a2013-03-07 12:30:3632#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5933#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2834#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]66fca5bc2013-05-23 06:58:2935#include "content/renderer/ime_event_guard.h"
[email protected]adab2332013-07-25 18:04:3236#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
[email protected]8704f89b2011-04-15 00:30:0537#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4438#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1939#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1840#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4841#include "skia/ext/platform_canvas.h"
[email protected]aaf68892013-07-18 00:11:3042#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
43#include "third_party/WebKit/public/platform/WebPoint.h"
44#include "third_party/WebKit/public/platform/WebRect.h"
45#include "third_party/WebKit/public/platform/WebSize.h"
46#include "third_party/WebKit/public/platform/WebString.h"
[email protected]2255a9332013-06-17 05:12:3147#include "third_party/WebKit/public/web/WebCursorInfo.h"
48#include "third_party/WebKit/public/web/WebHelperPlugin.h"
49#include "third_party/WebKit/public/web/WebPagePopup.h"
50#include "third_party/WebKit/public/web/WebPopupMenu.h"
51#include "third_party/WebKit/public/web/WebPopupMenuInfo.h"
52#include "third_party/WebKit/public/web/WebRange.h"
53#include "third_party/WebKit/public/web/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4154#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1355#include "ui/base/ui_base_switches.h"
[email protected]9b003482013-05-21 14:00:1756#include "ui/gfx/point.h"
[email protected]a25e25b2012-09-28 14:32:3757#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3558#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4859#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2760#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4161#include "ui/surface/transport_dib.h"
[email protected]799fd732013-05-15 21:18:5262#include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h"
[email protected]661eb9d2009-02-03 02:11:4863
[email protected]eeb93112013-05-01 19:41:1064#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:0765#include "content/renderer/android/synchronous_compositor_factory.h"
[email protected]eeb93112013-05-01 19:41:1066#endif
67
[email protected]661eb9d2009-02-03 02:11:4868#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4969#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5270#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4171#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4872#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4473
[email protected]2255a9332013-06-17 05:12:3174#include "third_party/WebKit/public/web/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2975
[email protected]fa7b1dc2010-06-23 17:53:0476using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3077using WebKit::WebCursorInfo;
[email protected]41d86852012-11-07 12:23:2478using WebKit::WebGestureEvent;
[email protected]62cb33cae2009-03-27 23:30:2279using WebKit::WebInputEvent;
[email protected]f56c7872013-06-18 12:31:5780using WebKit::WebKeyboardEvent;
[email protected]6a8ddba52010-09-05 04:38:0681using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2882using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4483using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0084using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2885using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5986using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1887using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0188using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5289using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1990using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5291using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2892using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2493using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0494using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1895using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2696
[email protected]6a4d7f62013-01-07 21:32:1397namespace {
98const char* GetEventName(WebInputEvent::Type type) {
99#define CASE_TYPE(t) case WebInputEvent::t: return #t
100 switch(type) {
101 CASE_TYPE(Undefined);
102 CASE_TYPE(MouseDown);
103 CASE_TYPE(MouseUp);
104 CASE_TYPE(MouseMove);
105 CASE_TYPE(MouseEnter);
106 CASE_TYPE(MouseLeave);
107 CASE_TYPE(ContextMenu);
108 CASE_TYPE(MouseWheel);
109 CASE_TYPE(RawKeyDown);
110 CASE_TYPE(KeyDown);
111 CASE_TYPE(KeyUp);
112 CASE_TYPE(Char);
113 CASE_TYPE(GestureScrollBegin);
114 CASE_TYPE(GestureScrollEnd);
115 CASE_TYPE(GestureScrollUpdate);
116 CASE_TYPE(GestureFlingStart);
117 CASE_TYPE(GestureFlingCancel);
118 CASE_TYPE(GestureTap);
[email protected]1c43b0a2013-05-10 07:43:23119 CASE_TYPE(GestureTapUnconfirmed);
[email protected]6a4d7f62013-01-07 21:32:13120 CASE_TYPE(GestureTapDown);
121 CASE_TYPE(GestureTapCancel);
122 CASE_TYPE(GestureDoubleTap);
123 CASE_TYPE(GestureTwoFingerTap);
124 CASE_TYPE(GestureLongPress);
125 CASE_TYPE(GestureLongTap);
126 CASE_TYPE(GesturePinchBegin);
127 CASE_TYPE(GesturePinchEnd);
128 CASE_TYPE(GesturePinchUpdate);
129 CASE_TYPE(TouchStart);
130 CASE_TYPE(TouchMove);
131 CASE_TYPE(TouchEnd);
132 CASE_TYPE(TouchCancel);
[email protected]3ec08ed2013-01-11 15:59:57133 default:
134 // Must include default to let WebKit::WebInputEvent add new event types
135 // before they're added here.
136 DLOG(WARNING) << "Unhandled WebInputEvent type in GetEventName.\n";
137 break;
[email protected]6a4d7f62013-01-07 21:32:13138 }
139#undef CASE_TYPE
[email protected]3ec08ed2013-01-11 15:59:57140 return "";
[email protected]6a4d7f62013-01-07 21:32:13141}
[email protected]b256eca2013-07-11 10:57:40142
143typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
144
145class TextInputModeMapSingleton {
146 public:
147 static TextInputModeMapSingleton* GetInstance() {
148 return Singleton<TextInputModeMapSingleton>::get();
149 }
150 TextInputModeMapSingleton()
151 : map() {
152 map["verbatim"] = ui::TEXT_INPUT_MODE_VERBATIM;
153 map["latin"] = ui::TEXT_INPUT_MODE_LATIN;
154 map["latin-name"] = ui::TEXT_INPUT_MODE_LATIN_NAME;
155 map["latin-prose"] = ui::TEXT_INPUT_MODE_LATIN_PROSE;
156 map["full-width-latin"] = ui::TEXT_INPUT_MODE_FULL_WIDTH_LATIN;
157 map["kana"] = ui::TEXT_INPUT_MODE_KANA;
158 map["katakana"] = ui::TEXT_INPUT_MODE_KATAKANA;
159 map["numeric"] = ui::TEXT_INPUT_MODE_NUMERIC;
160 map["tel"] = ui::TEXT_INPUT_MODE_TEL;
161 map["email"] = ui::TEXT_INPUT_MODE_EMAIL;
162 map["url"] = ui::TEXT_INPUT_MODE_URL;
163 }
164 TextInputModeMap& Map() {
165 return map;
166 }
167 private:
168 TextInputModeMap map;
169
170 friend struct DefaultSingletonTraits<TextInputModeMapSingleton>;
171
172 DISALLOW_COPY_AND_ASSIGN(TextInputModeMapSingleton);
173};
174
175ui::TextInputMode ConvertInputMode(
176 const WebKit::WebString& input_mode) {
177 static TextInputModeMapSingleton* singleton =
178 TextInputModeMapSingleton::GetInstance();
179 TextInputModeMap::iterator it = singleton->Map().find(input_mode.utf8());
180 if (it == singleton->Map().end())
181 return ui::TEXT_INPUT_MODE_DEFAULT;
182 return it->second;
[email protected]6a4d7f62013-01-07 21:32:13183}
[email protected]b256eca2013-07-11 10:57:40184
185} // namespace
186
[email protected]e9ff79c2012-10-19 21:31:26187namespace content {
[email protected]62cb33cae2009-03-27 23:30:22188
[email protected]6fd35b72012-03-01 19:46:41189RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:04190 const WebKit::WebScreenInfo& screen_info,
[email protected]1ac10dca2013-08-20 20:47:04191 bool swapped_out,
192 bool hidden)
initial.commit09911bf2008-07-26 23:55:29193 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56194 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11195 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29196 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30197 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35198 current_paint_buf_(NULL),
[email protected]d9083762013-03-24 01:36:40199 overdraw_bottom_height_(0.f),
initial.commit09911bf2008-07-26 23:55:29200 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06201 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05202 update_reply_pending_(false),
[email protected]847a2582013-03-09 02:29:51203 auto_resize_mode_(false),
[email protected]ea3ee0a2012-05-15 03:43:09204 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24205 using_asynchronous_swapbuffers_(false),
206 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29207 did_show_(false),
[email protected]1ac10dca2013-08-20 20:47:04208 is_hidden_(hidden),
[email protected]ee41e7d22011-10-14 19:34:09209 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29210 needs_repainting_on_restore_(false),
211 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49212 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50213 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48214 closing_(false),
[email protected]14392a52012-05-02 20:28:44215 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04216 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45217 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
218 can_compose_inline_(true),
[email protected]b256eca2013-07-11 10:57:40219 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
[email protected]3e2b375b2010-04-07 17:03:12220 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48221 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12222 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50223 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21224 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07225 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04226 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52227 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]ed7defa2013-03-12 21:29:59228 is_threaded_compositing_enabled_(false),
[email protected]53b4cc12013-07-18 23:02:30229 next_output_surface_id_(0),
[email protected]0d1ebed12013-08-05 22:01:13230#if defined(OS_ANDROID)
231 outstanding_ime_acks_(0),
232#endif
[email protected]69e797f2013-04-30 01:10:22233 weak_ptr_factory_(this) {
[email protected]8b3f0eb2012-05-03 19:15:05234 if (!swapped_out)
235 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27236 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18237 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
238 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36239 is_threaded_compositing_enabled_ =
240 CommandLine::ForCurrentProcess()->HasSwitch(
241 switches::kEnableThreadedCompositing);
initial.commit09911bf2008-07-26 23:55:29242}
243
244RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11245 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21246 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35247 if (current_paint_buf_) {
[email protected]467414eb2013-07-30 20:32:29248 if (RenderProcess::current()) {
249 // If the RenderProcess is already gone, it will have released all DIBs
250 // in its destructor anyway.
251 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
252 }
[email protected]b4d08452010-10-05 17:34:35253 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29254 }
[email protected]992db4c2011-05-12 15:37:15255 // If we are swapped out, we have released already.
[email protected]d2e2f9ee2013-08-21 11:02:02256 if (!is_swapped_out_ && RenderProcess::current())
[email protected]992db4c2011-05-12 15:37:15257 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29258}
259
[email protected]484955942010-08-19 16:13:18260// static
[email protected]8085dbc82008-09-26 22:53:44261RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04262 WebKit::WebPopupType popup_type,
263 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29264 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41265 scoped_refptr<RenderWidget> widget(
[email protected]1ac10dca2013-08-20 20:47:04266 new RenderWidget(popup_type, screen_info, false, false));
[email protected]a635f942012-12-07 10:34:29267 if (widget->Init(opener_id)) { // adds reference on success.
[email protected]fc72bb12013-06-02 21:13:46268 return widget.get();
[email protected]a635f942012-12-07 10:34:29269 }
270 return NULL;
initial.commit09911bf2008-07-26 23:55:29271}
272
[email protected]484955942010-08-19 16:13:18273// static
274WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
275 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03276 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18277 break;
278 case WebKit::WebPopupTypeSelect:
279 case WebKit::WebPopupTypeSuggestion:
280 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44281 case WebKit::WebPopupTypePage:
282 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03283 case WebKit::WebPopupTypeHelperPlugin:
284 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18285 default:
286 NOTREACHED();
287 }
288 return NULL;
289}
290
[email protected]a635f942012-12-07 10:34:29291bool RenderWidget::Init(int32 opener_id) {
292 return DoInit(opener_id,
293 RenderWidget::CreateWebWidget(this),
294 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
295 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18296}
297
[email protected]a635f942012-12-07 10:34:29298bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06299 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18300 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29301 DCHECK(!webwidget_);
302
303 if (opener_id != MSG_ROUTING_NONE)
304 opener_id_ = opener_id;
305
[email protected]484955942010-08-19 16:13:18306 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29307
[email protected]380244092011-10-07 17:26:27308 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29309 if (result) {
[email protected]380244092011-10-07 17:26:27310 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29311 // Take a reference on behalf of the RenderThread. This will be balanced
312 // when we receive ViewMsg_Close.
313 AddRef();
[email protected]1ac10dca2013-08-20 20:47:04314 if (is_hidden_)
315 RenderThread::Get()->WidgetHidden();
[email protected]a635f942012-12-07 10:34:29316 return true;
initial.commit09911bf2008-07-26 23:55:29317 } else {
[email protected]a635f942012-12-07 10:34:29318 // The above Send can fail when the tab is closing.
319 return false;
initial.commit09911bf2008-07-26 23:55:29320 }
321}
322
[email protected]fc4404d2012-11-07 19:53:30323// This is used to complete pending inits and non-pending inits.
324void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29325 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29326
[email protected]fc4404d2012-11-07 19:53:30327 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42328
[email protected]8926c602013-01-23 05:32:06329 if (webwidget_ && is_threaded_compositing_enabled_) {
330 webwidget_->enterForceCompositingMode(true);
331 }
[email protected]ba91a792013-02-06 09:48:28332 if (compositor_) {
333 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09334 }
[email protected]05a980d7a2012-02-07 22:16:42335 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29336
[email protected]6de74452009-02-25 18:04:59337 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29338}
339
[email protected]992db4c2011-05-12 15:37:15340void RenderWidget::SetSwappedOut(bool is_swapped_out) {
341 // We should only toggle between states.
342 DCHECK(is_swapped_out_ != is_swapped_out);
343 is_swapped_out_ = is_swapped_out;
344
345 // If we are swapping out, we will call ReleaseProcess, allowing the process
346 // to exit if all of its RenderViews are swapped out. We wait until the
347 // WasSwappedOut call to do this, to avoid showing the sad tab.
348 // If we are swapping in, we call AddRefProcess to prevent the process from
349 // exiting.
350 if (!is_swapped_out)
351 RenderProcess::current()->AddRefProcess();
352}
353
[email protected]34bb3ac2013-03-08 02:41:28354bool RenderWidget::AllowPartialSwap() const {
355 return true;
356}
357
[email protected]c8cbae72013-05-23 10:45:03358bool RenderWidget::UsingSynchronousRendererCompositor() const {
[email protected]ccc1722e2013-05-06 19:43:07359#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07360 return SynchronousCompositorFactory::GetInstance() != NULL;
361#else
[email protected]ccc1722e2013-05-06 19:43:07362 return false;
[email protected]913d99a2013-05-31 07:16:07363#endif
[email protected]ccc1722e2013-05-06 19:43:07364}
365
[email protected]a95986a82010-12-24 06:19:28366bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
367 bool handled = true;
368 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
[email protected]c084330e02013-04-27 01:08:15369 IPC_MESSAGE_HANDLER(InputMsg_HandleInputEvent, OnHandleInputEvent)
[email protected]34202de2013-05-06 23:36:22370 IPC_MESSAGE_HANDLER(InputMsg_CursorVisibilityChange,
371 OnCursorVisibilityChange)
[email protected]c084330e02013-04-27 01:08:15372 IPC_MESSAGE_HANDLER(InputMsg_MouseCaptureLost, OnMouseCaptureLost)
373 IPC_MESSAGE_HANDLER(InputMsg_SetFocus, OnSetFocus)
[email protected]a95986a82010-12-24 06:19:28374 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
375 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
376 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54377 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28378 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41379 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15380 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28381 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59382 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
383 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28384 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
385 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
386 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25387 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
388 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
[email protected]79fa22e2013-08-23 15:18:12389 IPC_MESSAGE_HANDLER(ViewMsg_SyntheticGestureCompleted,
390 OnSyntheticGestureCompleted)
[email protected]a95986a82010-12-24 06:19:28391 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
392 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]80ad8622012-11-07 16:33:03393 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21394#if defined(OS_ANDROID)
395 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
[email protected]2384b6c2013-02-28 23:58:51396 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]0d1ebed12013-08-05 22:01:13397 IPC_MESSAGE_HANDLER(ViewMsg_ImeEventAck, OnImeEventAck)
[email protected]105dffb42013-02-20 03:46:21398#endif
[email protected]51a49502013-03-23 01:50:19399 IPC_MESSAGE_HANDLER(ViewMsg_Snapshot, OnSnapshot)
[email protected]3639aa82013-06-04 11:00:04400 IPC_MESSAGE_HANDLER(ViewMsg_SetBrowserRenderingStats,
401 OnSetBrowserRenderingStats)
[email protected]a95986a82010-12-24 06:19:28402 IPC_MESSAGE_UNHANDLED(handled = false)
403 IPC_END_MESSAGE_MAP()
404 return handled;
405}
initial.commit09911bf2008-07-26 23:55:29406
407bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15408 // Don't send any messages after the browser has told us to close, and filter
409 // most outgoing messages while swapped out.
410 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26411 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11412 closing_) {
initial.commit09911bf2008-07-26 23:55:29413 delete message;
414 return false;
415 }
416
417 // If given a messsage without a routing ID, then assign our routing ID.
418 if (message->routing_id() == MSG_ROUTING_NONE)
419 message->set_routing_id(routing_id_);
420
[email protected]380244092011-10-07 17:26:27421 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44422}
423
[email protected]61e2b3cc2012-03-02 16:13:34424void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44425 const gfx::Size& physical_backing_size,
[email protected]d9083762013-03-24 01:36:40426 float overdraw_bottom_height,
[email protected]61e2b3cc2012-03-02 16:13:34427 const gfx::Rect& resizer_rect,
428 bool is_fullscreen,
429 ResizeAck resize_ack) {
[email protected]1c0008842013-06-06 08:35:48430 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
431 !RenderThreadImpl::current()->layout_test_mode()) {
432 // A resize ack shouldn't be requested if we have not ACK'd the previous
433 // one.
434 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
435 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
436 }
initial.commit09911bf2008-07-26 23:55:29437
[email protected]61e2b3cc2012-03-02 16:13:34438 // Ignore this during shutdown.
439 if (!webwidget_)
440 return;
441
[email protected]d9083762013-03-24 01:36:40442 if (compositor_) {
[email protected]60d47ac2013-03-01 23:42:44443 compositor_->setViewportSize(new_size, physical_backing_size);
[email protected]d9083762013-03-24 01:36:40444 compositor_->SetOverdrawBottomHeight(overdraw_bottom_height);
445 }
[email protected]60d47ac2013-03-01 23:42:44446
[email protected]dade8992013-03-04 07:34:34447 physical_backing_size_ = physical_backing_size;
[email protected]d9083762013-03-24 01:36:40448 overdraw_bottom_height_ = overdraw_bottom_height;
[email protected]61e2b3cc2012-03-02 16:13:34449 resizer_rect_ = resizer_rect;
450
451 // NOTE: We may have entered fullscreen mode without changing our size.
452 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
453 if (fullscreen_change)
454 WillToggleFullscreen();
455 is_fullscreen_ = is_fullscreen;
456
457 if (size_ != new_size) {
458 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34459 needs_repainting_on_restore_ = false;
460
461 size_ = new_size;
462
463 paint_aggregator_.ClearPendingUpdate();
464
465 // When resizing, we want to wait to paint before ACK'ing the resize. This
466 // ensures that we only resize as fast as we can paint. We only need to
467 // send an ACK if we are resized to a non-empty rect.
468 webwidget_->resize(new_size);
[email protected]0b70dbe2013-05-10 19:06:32469
[email protected]92201e32013-06-08 06:14:52470 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
471 !RenderThreadImpl::current()->layout_test_mode()) {
472 // Resize should have caused an invalidation of the entire view.
473 DCHECK(new_size.IsEmpty() || is_accelerated_compositing_active_ ||
474 paint_aggregator_.HasPendingUpdate());
475 }
[email protected]1c0008842013-06-06 08:35:48476 } else if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
477 !RenderThreadImpl::current()->layout_test_mode()) {
[email protected]632c4382013-05-15 08:58:45478 resize_ack = NO_RESIZE_ACK;
479 }
480
481 if (new_size.IsEmpty() || physical_backing_size.IsEmpty()) {
482 // For empty size or empty physical_backing_size, there is no next paint
483 // (along with which to send the ack) until they are set to non-empty.
[email protected]ff475a322012-03-14 00:05:35484 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34485 }
486
[email protected]20fbfc22013-05-08 20:50:58487 // Send the Resize_ACK flag once we paint again if requested.
[email protected]632c4382013-05-15 08:58:45488 if (resize_ack == SEND_RESIZE_ACK)
[email protected]20fbfc22013-05-08 20:50:58489 set_next_paint_is_resize_ack();
490
[email protected]61e2b3cc2012-03-02 16:13:34491 if (fullscreen_change)
492 DidToggleFullscreen();
493
494 // If a resize ack is requested and it isn't set-up, then no more resizes will
495 // come in and in general things will go wrong.
[email protected]632c4382013-05-15 08:58:45496 DCHECK(resize_ack != SEND_RESIZE_ACK || next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29497}
498
499void RenderWidget::OnClose() {
500 if (closing_)
501 return;
502 closing_ = true;
503
504 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03505 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27506 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03507 SetHidden(false);
508 }
initial.commit09911bf2008-07-26 23:55:29509
initial.commit09911bf2008-07-26 23:55:29510 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25511 // now. Post a task that only gets invoked when there are no nested message
512 // loops.
[email protected]dd32b1272013-05-04 14:17:11513 base::MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29514 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25515
516 // Balances the AddRef taken when we called AddRoute.
517 Release();
initial.commit09911bf2008-07-26 23:55:29518}
519
[email protected]61e2b3cc2012-03-02 16:13:34520// Got a response from the browser after the renderer decided to create a new
521// view.
[email protected]fc4404d2012-11-07 19:53:30522void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34523 DCHECK(routing_id_ != MSG_ROUTING_NONE);
524
[email protected]fc4404d2012-11-07 19:53:30525 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34526}
527
[email protected]0fdd5012013-05-29 08:05:56528void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) {
529 screen_info_ = params.screen_info;
530 SetDeviceScaleFactor(screen_info_.deviceScaleFactor);
531 Resize(params.new_size, params.physical_backing_size,
532 params.overdraw_bottom_height, params.resizer_rect,
533 params.is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29534}
535
[email protected]b5913d72012-02-07 22:26:54536void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
537 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59538 gfx::Rect view_rect(size_);
539
[email protected]ce112fe2012-10-29 22:52:18540 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59541 if (!old_damage_rect.IsEmpty())
542 paint_aggregator_.InvalidateRect(old_damage_rect);
543
[email protected]ce112fe2012-10-29 22:52:18544 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59545 if (!new_damage_rect.IsEmpty())
546 paint_aggregator_.InvalidateRect(new_damage_rect);
547
[email protected]b5913d72012-02-07 22:26:54548 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59549
[email protected]b5913d72012-02-07 22:26:54550 if (webwidget_)
551 webwidget_->didChangeWindowResizerRect();
552 }
553}
554
initial.commit09911bf2008-07-26 23:55:29555void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31556 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29557 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03558 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29559}
560
[email protected]9e2e4632012-07-27 16:38:41561void RenderWidget::OnWasShown(bool needs_repainting) {
562 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29563 // During shutdown we can just ignore this message.
564 if (!webwidget_)
565 return;
566
567 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03568 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29569
570 if (!needs_repainting && !needs_repainting_on_restore_)
571 return;
572 needs_repainting_on_restore_ = false;
573
[email protected]d65adb12010-04-28 17:26:49574 // Tag the next paint as a restore ack, which is picked up by
575 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29576 set_next_paint_is_restore_ack();
577
578 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56579 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46580 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
581 } else {
582 scheduleComposite();
583 }
initial.commit09911bf2008-07-26 23:55:29584}
585
[email protected]992db4c2011-05-12 15:37:15586void RenderWidget::OnWasSwappedOut() {
587 // If we have been swapped out and no one else is using this process,
588 // it's safe to exit now. If we get swapped back in, we will call
589 // AddRefProcess in SetSwappedOut.
590 if (is_swapped_out_)
591 RenderProcess::current()->ReleaseProcess();
592}
593
[email protected]53d3f302009-12-21 04:42:05594void RenderWidget::OnRequestMoveAck() {
595 DCHECK(pending_window_rect_count_);
596 pending_window_rect_count_--;
597}
598
599void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58600 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21601 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05602 update_reply_pending_ = false;
603
[email protected]b4d08452010-10-05 17:34:35604 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
605 // have no current paint buffer.
606 if (current_paint_buf_) {
607 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
608 current_paint_buf_ = NULL;
609 }
610
[email protected]65225772011-05-12 21:10:24611 // If swapbuffers is still pending, then defer the update until the
612 // swapbuffers occurs.
613 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
614 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
615 return;
616 }
617
[email protected]29ed96a2012-02-04 18:12:16618 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18619 if (!is_accelerated_compositing_active_) {
620 DidFlushPaint();
621 }
[email protected]a2f6bc112009-06-27 16:27:25622
initial.commit09911bf2008-07-26 23:55:29623 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24624 DoDeferredUpdateAndSendInputAck();
625}
626
[email protected]d0be63772011-12-20 23:18:04627bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59628 // Contexts using the command buffer support asynchronous swapbuffers.
629 // See RenderWidget::CreateOutputSurface().
[email protected]cadac622013-06-11 16:46:36630 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get())
[email protected]ed7defa2013-03-12 21:29:59631 return false;
632
633 return true;
634}
635
636GURL RenderWidget::GetURLForGraphicsContext3D() {
637 return GURL();
[email protected]65225772011-05-12 21:10:24638}
639
[email protected]479b0172012-10-29 19:27:09640bool RenderWidget::ForceCompositingModeEnabled() {
641 return false;
642}
643
[email protected]ebc0e1df2013-08-01 02:46:22644scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface(bool fallback) {
[email protected]c3e32ed42013-05-02 05:07:13645 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1811b8912013-05-09 15:35:19646
647#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07648 if (SynchronousCompositorFactory* factory =
649 SynchronousCompositorFactory::GetInstance()) {
650 return factory->CreateOutputSurface(routing_id());
[email protected]a1811b8912013-05-09 15:35:19651 }
652#endif
653
[email protected]53b4cc12013-07-18 23:02:30654 uint32 output_surface_id = next_output_surface_id_++;
655
[email protected]ed7defa2013-03-12 21:29:59656 // Explicitly disable antialiasing for the compositor. As of the time of
657 // this writing, the only platform that supported antialiasing for the
658 // compositor was Mac OS X, because the on-screen OpenGL context creation
659 // code paths on Windows and Linux didn't yet have multisampling support.
660 // Mac OS X essentially always behaves as though it's rendering offscreen.
661 // Multisampling has a heavy cost especially on devices with relatively low
662 // fill rate like most notebooks, and the Mac implementation would need to
663 // be optimized to resolve directly into the IOSurface shared between the
664 // GPU and browser processes. For these reasons and to avoid platform
665 // disparities we explicitly disable antialiasing.
666 WebKit::WebGraphicsContext3D::Attributes attributes;
667 attributes.antialias = false;
668 attributes.shareResources = true;
669 attributes.noAutomaticFlushes = true;
[email protected]a6886502013-05-16 20:59:18670 attributes.depth = false;
671 attributes.stencil = false;
[email protected]bec084292013-05-21 21:31:44672 if (command_line.HasSwitch(cc::switches::kForceDirectLayerDrawing))
673 attributes.stencil = true;
[email protected]0634cdd42013-08-16 00:46:09674 scoped_refptr<ContextProviderCommandBuffer> context_provider;
675 if (!fallback) {
676 context_provider = ContextProviderCommandBuffer::Create(
[email protected]3a98aed92013-08-16 04:58:13677 CreateGraphicsContext3D(attributes));
[email protected]0634cdd42013-08-16 00:46:09678 }
[email protected]ebc0e1df2013-08-01 02:46:22679
[email protected]0634cdd42013-08-16 00:46:09680 if (!context_provider.get()) {
[email protected]ebc0e1df2013-08-01 02:46:22681 if (!command_line.HasSwitch(switches::kEnableSoftwareCompositing))
682 return scoped_ptr<cc::OutputSurface>();
[email protected]0634cdd42013-08-16 00:46:09683
684 scoped_ptr<cc::SoftwareOutputDevice> software_device(
685 new CompositorSoftwareOutputDevice());
686
687 return scoped_ptr<cc::OutputSurface>(new CompositorOutputSurface(
688 routing_id(),
689 output_surface_id,
690 NULL,
691 software_device.Pass(),
692 true));
[email protected]ebc0e1df2013-08-01 02:46:22693 }
[email protected]ed7defa2013-03-12 21:29:59694
[email protected]0c04d1c2013-07-10 00:02:32695 if (command_line.HasSwitch(switches::kEnableDelegatedRenderer) &&
696 !command_line.HasSwitch(switches::kDisableDelegatedRenderer)) {
[email protected]36e5ff12013-06-11 12:19:29697 DCHECK(is_threaded_compositing_enabled_);
698 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09699 new DelegatedCompositorOutputSurface(
700 routing_id(),
701 output_surface_id,
702 context_provider,
703 scoped_ptr<cc::SoftwareOutputDevice>()));
[email protected]36e5ff12013-06-11 12:19:29704 }
705 if (command_line.HasSwitch(cc::switches::kCompositeToMailbox)) {
706 DCHECK(is_threaded_compositing_enabled_);
707 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09708 new MailboxOutputSurface(
709 routing_id(),
710 output_surface_id,
711 context_provider,
712 scoped_ptr<cc::SoftwareOutputDevice>()));
[email protected]36e5ff12013-06-11 12:19:29713 }
[email protected]0634cdd42013-08-16 00:46:09714 bool use_swap_compositor_frame_message = false;
[email protected]36e5ff12013-06-11 12:19:29715 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09716 new CompositorOutputSurface(
717 routing_id(),
718 output_surface_id,
719 context_provider,
720 scoped_ptr<cc::SoftwareOutputDevice>(),
721 use_swap_compositor_frame_message));
[email protected]ba91a792013-02-06 09:48:28722}
723
[email protected]ed7defa2013-03-12 21:29:59724void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24725 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21726 while (!updates_pending_swap_.empty()) {
727 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
728 updates_pending_swap_.pop_front();
729 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
730 // compositing pass, hence doesn't require an UpdateRect message.
731 if (msg)
732 Send(msg);
733 }
[email protected]65225772011-05-12 21:10:24734 num_swapbuffers_complete_pending_ = 0;
735 using_asynchronous_swapbuffers_ = false;
736 // Schedule another frame so the compositor learns about it.
737 scheduleComposite();
738}
739
[email protected]ed7defa2013-03-12 21:29:59740void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08741 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21742
743 if (using_asynchronous_swapbuffers_) {
744 ViewHostMsg_UpdateRect* msg = NULL;
745 // pending_update_params_ can be NULL if the swap doesn't correspond to an
746 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
747 // message.
[email protected]59383c782013-04-17 16:43:27748 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:21749 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
750 pending_update_params_.reset();
751 }
752 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08753 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21754 }
[email protected]37a6f302011-07-11 23:43:08755}
756
[email protected]ed7defa2013-03-12 21:29:59757void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24758 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16759
[email protected]404939f2012-06-01 04:06:18760 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16761 DidFlushPaint();
762
[email protected]65225772011-05-12 21:10:24763 // When compositing deactivates, we reset the swapbuffers pending count. The
764 // swapbuffers acks may still arrive, however.
765 if (num_swapbuffers_complete_pending_ == 0) {
766 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
767 return;
768 }
[email protected]aa4117f2011-12-09 22:19:21769 DCHECK(!updates_pending_swap_.empty());
770 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
771 updates_pending_swap_.pop_front();
772 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
773 // compositing pass, hence doesn't require an UpdateRect message.
774 if (msg)
775 Send(msg);
[email protected]65225772011-05-12 21:10:24776 num_swapbuffers_complete_pending_--;
777
778 // If update reply is still pending, then defer the update until that reply
779 // occurs.
[email protected]d0be63772011-12-20 23:18:04780 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24781 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
782 return;
783 }
784
785 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06786 // when we were previously rendering. However, if an invalidation task is not
787 // posted, there may be software rendering work pending. In that case, don't
788 // early out.
789 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24790 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
791 return;
792 }
793
[email protected]cc66e682012-10-02 06:48:18794 // Do not call DoDeferredUpdate unless there's animation work to be done or
795 // a real invalidation. This prevents rendering in response to a swapbuffers
796 // callback coming back after we've navigated away from the page that
797 // generated it.
798 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
799 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
800 return;
801 }
802
[email protected]65225772011-05-12 21:10:24803 // Continue painting if necessary...
804 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29805}
806
[email protected]0dea1652012-12-14 00:09:09807void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
[email protected]4b157662013-05-29 04:05:05808 const ui::LatencyInfo& latency_info,
[email protected]0dea1652012-12-14 00:09:09809 bool is_keyboard_shortcut) {
[email protected]5dd768212009-08-13 23:34:49810 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09811 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49812 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29813 return;
[email protected]5dd768212009-08-13 23:34:49814 }
initial.commit09911bf2008-07-26 23:55:29815
[email protected]b4841e1c2013-05-16 22:30:10816 const char* const event_name = GetEventName(input_event->type);
817 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent",
818 "event", event_name);
819
[email protected]c2eaa8f2013-05-10 02:41:55820 if (compositor_)
821 compositor_->SetLatencyInfo(latency_info);
[email protected]256737c2013-06-08 04:39:10822 else
823 latency_info_.MergeWith(latency_info);
[email protected]c2eaa8f2013-05-10 02:41:55824
[email protected]6a4d7f62013-01-07 21:32:13825 base::TimeDelta now = base::TimeDelta::FromInternalValue(
826 base::TimeTicks::Now().ToInternalValue());
827
828 int64 delta = static_cast<int64>(
829 (now.InSecondsF() - input_event->timeStampSeconds) *
830 base::Time::kMicrosecondsPerSecond);
831 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
[email protected]de415552013-01-23 04:12:17832 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:48833 base::Histogram::FactoryGet(
[email protected]b4841e1c2013-05-16 22:30:10834 base::StringPrintf("Event.Latency.Renderer.%s", event_name),
[email protected]bafdc5d52013-02-27 18:18:48835 0,
836 1000000,
[email protected]6a4d7f62013-01-07 21:32:13837 100,
[email protected]de415552013-01-23 04:12:17838 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:48839 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:13840
[email protected]67bfb83f2011-09-22 03:36:37841 bool prevent_default = false;
842 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26843 const WebMouseEvent& mouse_event =
844 *static_cast<const WebMouseEvent*>(input_event);
845 TRACE_EVENT2("renderer", "HandleMouseMove",
846 "x", mouse_event.x, "y", mouse_event.y);
847 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37848 }
849
[email protected]f56c7872013-06-18 12:31:57850 if (WebInputEvent::isKeyboardEventType(input_event->type)) {
851 const WebKeyboardEvent& key_event =
852 *static_cast<const WebKeyboardEvent*>(input_event);
853 prevent_default = WillHandleKeyEvent(key_event);
854 }
855
[email protected]41d86852012-11-07 12:23:24856 if (WebInputEvent::isGestureEventType(input_event->type)) {
857 const WebGestureEvent& gesture_event =
858 *static_cast<const WebGestureEvent*>(input_event);
859 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
860 }
861
[email protected]3ebcc7c2013-01-09 05:34:46862 if (input_event->type == WebInputEvent::GestureTap ||
863 input_event->type == WebInputEvent::GestureLongPress)
864 resetInputMethod();
865
[email protected]67bfb83f2011-09-22 03:36:37866 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12867 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
868 suppress_next_char_events_ = false;
869 if (!processed && webwidget_)
870 processed = webwidget_->handleInputEvent(*input_event);
871 }
872
873 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
874 // it's not processed by webkit, then we need to suppress the upcoming Char
875 // events.
876 if (!processed && is_keyboard_shortcut)
877 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29878
[email protected]3d5c243b2012-11-30 00:26:01879 InputEventAckState ack_result = processed ?
880 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
881 if (!processed && input_event->type == WebInputEvent::TouchStart) {
882 const WebTouchEvent& touch_event =
883 *static_cast<const WebTouchEvent*>(input_event);
884 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
885 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
886 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
887 }
888
[email protected]a9fb30aa2011-10-06 06:58:46889 IPC::Message* response =
[email protected]f6df0edf2013-08-07 00:27:02890 new InputHostMsg_HandleInputEvent_ACK(routing_id_,
891 input_event->type,
892 ack_result,
893 latency_info);
[email protected]3391a0772012-03-28 00:32:07894 bool event_type_gets_rate_limited =
895 input_event->type == WebInputEvent::MouseMove ||
896 input_event->type == WebInputEvent::MouseWheel ||
897 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06898
899 bool frame_pending = paint_aggregator_.HasPendingUpdate();
900 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28901 frame_pending = compositor_ &&
902 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06903 }
904
[email protected]9a8ce7f92013-06-11 12:39:49905 if (event_type_gets_rate_limited && frame_pending && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24906 // We want to rate limit the input events in this case, so we'll wait for
907 // painting to finish before ACKing this message.
[email protected]59383c782013-04-17 16:43:27908 if (pending_input_event_ack_) {
[email protected]353a34c2010-05-28 23:35:17909 // As two different kinds of events could cause us to postpone an ack
910 // we send it now, if we have one pending. The Browser should never
911 // send us the same kind of event we are delaying the ack for.
912 Send(pending_input_event_ack_.release());
913 }
[email protected]12fbad812009-09-01 18:21:24914 pending_input_event_ack_.reset(response);
[email protected]df09e052013-07-31 18:59:50915 if (compositor_)
916 compositor_->NotifyInputThrottledUntilCommit();
[email protected]12fbad812009-09-01 18:21:24917 } else {
918 Send(response);
919 }
920
[email protected]3306f262012-09-21 19:20:42921#if defined(OS_ANDROID)
922 // Allow the IME to be shown when the focus changes as a consequence
923 // of a processed touch end event.
924 if (input_event->type == WebInputEvent::TouchEnd && processed)
[email protected]0d1ebed12013-08-05 22:01:13925 UpdateTextInputState(true, true);
[email protected]3306f262012-09-21 19:20:42926#endif
927
[email protected]5dd768212009-08-13 23:34:49928 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48929
[email protected]67bfb83f2011-09-22 03:36:37930 if (!prevent_default) {
931 if (WebInputEvent::isKeyboardEventType(input_event->type))
932 DidHandleKeyEvent();
933 if (WebInputEvent::isMouseEventType(input_event->type))
934 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24935 if (WebInputEvent::isTouchEventType(input_event->type))
936 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37937 }
initial.commit09911bf2008-07-26 23:55:29938}
939
[email protected]34202de2013-05-06 23:36:22940void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
941 if (webwidget_)
942 webwidget_->setCursorVisibilityState(is_visible);
943}
944
initial.commit09911bf2008-07-26 23:55:29945void RenderWidget::OnMouseCaptureLost() {
946 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28947 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29948}
949
950void RenderWidget::OnSetFocus(bool enable) {
951 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33952 if (webwidget_)
953 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29954}
955
956void RenderWidget::ClearFocus() {
957 // We may have got the focus from the browser before this gets processed, in
958 // which case we do not want to unfocus ourself.
959 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28960 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29961}
962
[email protected]2d5d09d52009-06-15 14:29:21963void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00964 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21965 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06966 TRACE_EVENT2("renderer", "PaintRect",
967 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45968
969 const bool kEnableGpuBenchmarking =
970 CommandLine::ForCurrentProcess()->HasSwitch(
971 switches::kEnableGpuBenchmarking);
[email protected]4fb66842009-12-04 21:41:00972 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21973
974 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00975 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
976 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03977
[email protected]699ab0d2009-04-23 23:19:14978 // If there is a custom background, tile it.
979 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14980 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11981 skia::RefPtr<SkShader> shader = skia::AdoptRef(
982 SkShader::CreateBitmapShader(background_,
983 SkShader::kRepeat_TileMode,
984 SkShader::kRepeat_TileMode));
985 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20986
987 // Use kSrc_Mode to handle background_ transparency properly.
988 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
989
990 // Canvas could contain multiple update rects. Clip to given rect so that
991 // we don't accidentally clear other update rects.
992 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44993 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48994 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14995 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20996 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14997 }
998
[email protected]719b36f2010-12-22 20:36:46999 // First see if this rect is a plugin that can paint itself faster.
1000 TransportDIB* optimized_dib = NULL;
1001 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201002 float dib_scale_factor;
[email protected]adab2332013-07-25 18:04:321003 PepperPluginInstanceImpl* optimized_instance =
[email protected]719b36f2010-12-22 20:36:461004 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
1005 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201006 &optimized_copy_rect,
1007 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:461008 if (optimized_instance) {
[email protected]20790a222013-07-25 02:23:051009#if defined(ENABLE_PLUGINS)
[email protected]719b36f2010-12-22 20:36:461010 // This plugin can be optimize-painted and we can just ask it to paint
1011 // itself. We don't actually need the TransportDIB in this case.
1012 //
1013 // This is an optimization for PPAPI plugins that know they're on top of
1014 // the page content. If this rect is inside such a plugin, we can save some
1015 // time and avoid re-rendering the page content which we know will be
1016 // covered by the plugin later (this time can be significant, especially
1017 // for a playing movie that is invalidating a lot).
1018 //
1019 // In the plugin movie case, hopefully the similar call to
1020 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
1021 // painting, because that avoids copying the plugin image to a different
1022 // paint rect. Unfortunately, if anything on the page is animating other
1023 // than the movie, it break this optimization since the union of the
1024 // invalid regions will be larger than the plugin.
1025 //
1026 // This code optimizes that case, where we can still avoid painting in
1027 // WebKit and filling the background (which can be slow) and just painting
1028 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
1029 // required.
[email protected]63ab54262012-11-09 15:58:451030 base::TimeTicks paint_begin_ticks;
1031 if (kEnableGpuBenchmarking)
1032 paint_begin_ticks = base::TimeTicks::HighResNow();
1033
[email protected]df59dd42012-09-14 22:56:301034 SkAutoCanvasRestore auto_restore(canvas, true);
1035 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]6bd867b2013-07-24 22:10:201036 optimized_instance->Paint(canvas, optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:211037 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451038 if (kEnableGpuBenchmarking) {
1039 base::TimeDelta paint_time =
1040 base::TimeTicks::HighResNow() - paint_begin_ticks;
1041 if (!is_accelerated_compositing_active_)
[email protected]a9dc0d0f2013-08-17 02:43:181042 software_stats_.main_stats.paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451043 }
[email protected]20790a222013-07-25 02:23:051044#endif
[email protected]719b36f2010-12-22 20:36:461045 } else {
1046 // Normal painting case.
[email protected]63ab54262012-11-09 15:58:451047 base::TimeTicks paint_begin_ticks;
1048 if (kEnableGpuBenchmarking)
1049 paint_begin_ticks = base::TimeTicks::HighResNow();
1050
[email protected]6bd867b2013-07-24 22:10:201051 webwidget_->paint(canvas, rect);
[email protected]63ab54262012-11-09 15:58:451052
1053 if (kEnableGpuBenchmarking) {
1054 base::TimeDelta paint_time =
1055 base::TimeTicks::HighResNow() - paint_begin_ticks;
1056 if (!is_accelerated_compositing_active_)
[email protected]a9dc0d0f2013-08-17 02:43:181057 software_stats_.main_stats.paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451058 }
[email protected]719b36f2010-12-22 20:36:461059
1060 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:351061 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:461062 }
initial.commit09911bf2008-07-26 23:55:291063
[email protected]4fb66842009-12-04 21:41:001064 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:001065 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451066
1067 if (kEnableGpuBenchmarking) {
[email protected]63ab54262012-11-09 15:58:451068 int64 num_pixels_processed = rect.width() * rect.height();
[email protected]a9dc0d0f2013-08-17 02:43:181069 software_stats_.main_stats.painted_pixel_count += num_pixels_processed;
[email protected]63ab54262012-11-09 15:58:451070 }
[email protected]4fb66842009-12-04 21:41:001071}
1072
1073void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
1074 skia::PlatformCanvas* canvas) {
1075 static bool kPaintBorder =
1076 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
1077 if (!kPaintBorder)
1078 return;
1079
[email protected]53d3f302009-12-21 04:42:051080 // Cycle through these colors to help distinguish new paint rects.
1081 const SkColor colors[] = {
1082 SkColorSetARGB(0x3F, 0xFF, 0, 0),
1083 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
1084 SkColorSetARGB(0x3F, 0, 0, 0xFF),
1085 };
1086 static int color_selector = 0;
1087
[email protected]4fb66842009-12-04 21:41:001088 SkPaint paint;
1089 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:051090 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:001091 paint.setStrokeWidth(1);
1092
1093 SkIRect irect;
1094 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
1095 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:291096}
1097
[email protected]52ccd0ea2011-02-16 01:09:051098void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:301099 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:301100 if (!animation_update_pending_) {
1101 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:591102 return;
[email protected]921244e42011-07-20 16:36:301103 }
[email protected]bd37ae252011-06-03 01:28:181104 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:591105 // Record when we fired (according to base::Time::Now()) relative to when
1106 // we posted the task to quantify how much the base::Time/base::TimeTicks
1107 // skew is affecting animations.
1108 base::TimeDelta animation_callback_delay = base::Time::Now() -
1109 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
1110 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
1111 animation_callback_delay,
1112 base::TimeDelta::FromMilliseconds(0),
1113 base::TimeDelta::FromMilliseconds(30),
1114 25);
1115 }
[email protected]65225772011-05-12 21:10:241116 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:241117}
1118
[email protected]52ccd0ea2011-02-16 01:09:051119void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:591120 if (!animation_update_pending_)
1121 return;
[email protected]bd37ae252011-06-03 01:28:181122
1123 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:331124 base::TimeDelta animationInterval = IsRenderingVSynced() ?
1125 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:181126
[email protected]7c4329e2011-02-18 22:02:591127 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:451128
1129 // animation_floor_time_ is the earliest time that we should animate when
1130 // using the dead reckoning software scheduler. If we're using swapbuffers
1131 // complete callbacks to rate limit, we can ignore this floor.
1132 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:301133 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:331134 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:181135 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:591136 // running animation callbacks so that if a callback requests another
1137 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:381138 animation_timer_.Stop();
1139 animation_timer_.Start(FROM_HERE, animationInterval, this,
1140 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:591141 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:281142 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:201143 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:061144 } else {
[email protected]635353c2013-03-06 09:11:201145 double frame_begin_time =
1146 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
1147 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061148 }
[email protected]7c4329e2011-02-18 22:02:591149 return;
[email protected]5f8b1022011-01-21 23:34:501150 }
[email protected]bd37ae252011-06-03 01:28:181151 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381152 if (!animation_timer_.IsRunning()) {
1153 // This code uses base::Time::Now() to calculate the floor and next fire
1154 // time because javascript's Date object uses base::Time::Now(). The
1155 // message loop uses base::TimeTicks, which on windows can have a
1156 // different granularity than base::Time.
1157 // The upshot of all this is that this function might be called before
1158 // base::Time::Now() has advanced past the animation_floor_time_. To
1159 // avoid exposing this delay to javascript, we keep posting delayed
1160 // tasks until base::Time::Now() has advanced far enough.
1161 base::TimeDelta delay = animation_floor_time_ - now;
1162 animation_timer_.Start(FROM_HERE, delay, this,
1163 &RenderWidget::AnimationCallback);
1164 }
[email protected]5f8b1022011-01-21 23:34:501165}
1166
[email protected]bd37ae252011-06-03 01:28:181167bool RenderWidget::IsRenderingVSynced() {
1168 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1169 // not caught by this check. This will lead to artificially low frame rates
1170 // for people who force vsync off at a driver level and expect Chrome to speed
1171 // up.
1172 return !has_disable_gpu_vsync_switch_;
1173}
1174
[email protected]65225772011-05-12 21:10:241175void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061176 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241177 invalidation_task_posted_ = false;
1178 DoDeferredUpdateAndSendInputAck();
1179}
1180
1181void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051182 DoDeferredUpdate();
1183
[email protected]59383c782013-04-17 16:43:271184 if (pending_input_event_ack_)
[email protected]52ccd0ea2011-02-16 01:09:051185 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211186}
1187
[email protected]552e6002009-11-19 05:24:571188void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581189 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]fc8bde1a2013-07-04 08:54:071190 TRACE_EVENT_SCOPED_SAMPLING_STATE("Chrome", "Paint");
[email protected]71e2f0a2011-03-15 22:25:081191
[email protected]65225772011-05-12 21:10:241192 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291193 return;
[email protected]05a980d7a2012-02-07 22:16:421194
[email protected]fc4404d2012-11-07 19:53:301195 if (!init_complete_) {
1196 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421197 return;
1198 }
[email protected]aa4117f2011-12-09 22:19:211199 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241200 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1201 return;
1202 }
[email protected]9ca84622011-06-02 23:46:391203 if (is_accelerated_compositing_active_ &&
1204 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241205 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1206 return;
1207 }
initial.commit09911bf2008-07-26 23:55:291208
[email protected]552e6002009-11-19 05:24:571209 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051210 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571211 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291212 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241213 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291214 return;
1215 }
1216
[email protected]0fb93f52011-05-18 23:13:561217 // Tracking of frame rate jitter
1218 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371219 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051220 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501221
[email protected]f98d7e3c2010-09-13 22:30:461222 // Layout may generate more invalidation. It may also enable the
1223 // GPU acceleration, so make sure to run layout before we send the
1224 // GpuRenderingActivated message.
1225 webwidget_->layout();
1226
[email protected]793b2d62013-06-11 00:43:251227 // Check for whether we need to track swap buffers. We need to do that after
1228 // layout() because it may have switched us to accelerated compositing.
1229 if (is_accelerated_compositing_active_)
1230 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1231
[email protected]dcca3aa92012-02-17 23:03:371232 // The following two can result in further layout and possibly
1233 // enable GPU acceleration so they need to be called before any painting
1234 // is done.
[email protected]cb9e2632013-06-18 11:26:471235 UpdateTextInputType();
[email protected]dcca3aa92012-02-17 23:03:371236 UpdateSelectionBounds();
1237
[email protected]5f8b1022011-01-21 23:34:501238 // Suppress painting if nothing is dirty. This has to be done after updating
1239 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241240 if (!paint_aggregator_.HasPendingUpdate()) {
1241 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371242 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501243 return;
[email protected]65225772011-05-12 21:10:241244 }
[email protected]5f8b1022011-01-21 23:34:501245
[email protected]479b0172012-10-29 19:27:091246 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361247 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091248 ForceCompositingModeEnabled()) {
1249 webwidget_->enterForceCompositingMode(true);
1250 }
1251
[email protected]872ae5b2011-05-26 20:20:501252 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561253 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041254 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561255 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1256 delay,
1257 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411258 base::TimeDelta::FromMilliseconds(120),
1259 60);
[email protected]d0be63772011-12-20 23:18:041260 } else {
[email protected]0fb93f52011-05-18 23:13:561261 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1262 delay,
1263 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411264 base::TimeDelta::FromMilliseconds(120),
1265 60);
[email protected]d0be63772011-12-20 23:18:041266 }
[email protected]872ae5b2011-05-26 20:20:501267
1268 // Calculate filtered time per frame:
1269 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1270 filtered_time_per_frame_ =
1271 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561272 }
1273 last_do_deferred_update_time_ = frame_begin_ticks;
1274
[email protected]fef5e3972012-08-07 03:59:471275 if (!is_accelerated_compositing_active_) {
[email protected]a9dc0d0f2013-08-17 02:43:181276 software_stats_.main_stats.animation_frame_count++;
1277 software_stats_.main_stats.screen_frame_count++;
[email protected]fef5e3972012-08-07 03:59:471278 }
1279
[email protected]552e6002009-11-19 05:24:571280 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291281 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301282 PaintAggregator::PendingUpdate update;
1283 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291284
[email protected]53d3f302009-12-21 04:42:051285 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181286 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291287
[email protected]29ed96a2012-02-04 18:12:161288 // Notify derived classes that we're about to initiate a paint.
1289 WillInitiatePaint();
1290
[email protected]ca4847f2010-09-24 05:39:151291 // A plugin may be able to do an optimized paint. First check this, in which
1292 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461293 // This optimization allows PPAPI plugins that declare themselves on top of
1294 // the page (like a traditional windowed plugin) to be able to animate (think
1295 // movie playing) without repeatedly re-painting the page underneath, or
1296 // copying the plugin backing store (since we can send the plugin's backing
1297 // store directly to the browser).
1298 //
1299 // This optimization only works when the entire invalid region is contained
1300 // within the plugin. There is a related optimization in PaintRect for the
1301 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151302 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151303 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201304 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211305 DCHECK(!pending_update_params_.get());
1306 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551307 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211308 pending_update_params_->scroll_rect = update.scroll_rect;
1309 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211310 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1311 pending_update_params_->flags = next_paint_flags_;
1312 pending_update_params_->scroll_offset = GetScrollOffset();
1313 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091314 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211315 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091316 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211317
[email protected]256737c2013-06-08 04:39:101318 if (!is_accelerated_compositing_active_)
1319 pending_update_params_->latency_info = latency_info_;
1320
1321 latency_info_.Clear();
1322
[email protected]ca4847f2010-09-24 05:39:151323 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561324 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151325 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201326 &optimized_copy_rect,
1327 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271328 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471329 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211330 pending_update_params_->bitmap = dib->id();
1331 pending_update_params_->bitmap_rect = optimized_copy_location;
1332 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201333 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561334 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461335 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101336
1337 bool fractional_scale = device_scale_factor_ -
1338 static_cast<int>(device_scale_factor_) != 0;
1339 if (fractional_scale) {
1340 // Damage might not be DIP aligned. Inflate damage to compensate.
1341 bounds.Inset(-1, -1);
1342 bounds.Intersect(gfx::Rect(size_));
1343 }
1344
1345 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181346 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101347
[email protected]ca4847f2010-09-24 05:39:151348 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351349 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591350 pixel_bounds));
[email protected]59383c782013-04-17 16:43:271351 if (!canvas) {
[email protected]f98d7e3c2010-09-13 22:30:461352 NOTREACHED();
1353 return;
1354 }
[email protected]cef3362f2009-12-21 17:48:451355
[email protected]f98d7e3c2010-09-13 22:30:461356 // We may get back a smaller canvas than we asked for.
1357 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591358 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1359 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1360 pixel_bounds.set_width(canvas->getDevice()->width());
1361 pixel_bounds.set_height(canvas->getDevice()->height());
1362 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1363 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051364
[email protected]f98d7e3c2010-09-13 22:30:461365 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1366
[email protected]aa4117f2011-12-09 22:19:211367 pending_update_params_->bitmap = current_paint_buf_->id();
1368 pending_update_params_->bitmap_rect = bounds;
1369
1370 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461371 // The scroll damage is just another rectangle to paint and copy.
1372 copy_rects.swap(update.paint_rects);
1373 if (!scroll_damage.IsEmpty())
1374 copy_rects.push_back(scroll_damage);
1375
[email protected]4889bd212013-02-11 22:23:101376 for (size_t i = 0; i < copy_rects.size(); ++i) {
1377 gfx::Rect rect = copy_rects[i];
1378 if (fractional_scale) {
1379 // Damage might not be DPI aligned. Inflate rect to compensate.
1380 rect.Inset(-1, -1);
1381 }
1382 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1383 }
[email protected]60a50072012-01-11 02:05:351384
1385 // Software FPS tick for performance tests. The accelerated path traces the
1386 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1387 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421388 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW",
1389 TRACE_EVENT_SCOPE_THREAD);
[email protected]f98d7e3c2010-09-13 22:30:461390 } else { // Accelerated compositing path
1391 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211392 // If painting is done via the gpu process then we don't set any damage
1393 // rects to save the browser process from doing unecessary work.
1394 pending_update_params_->bitmap_rect = bounds;
1395 pending_update_params_->scroll_rect = gfx::Rect();
1396 // We don't need an ack, because we're not sharing a DIB with the browser.
1397 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1398 // with the browser for the GPU surface.
1399 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521400 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461401 }
1402
[email protected]936c6f52011-12-13 01:35:261403 // If we're holding a pending input event ACK, send the ACK before sending the
1404 // UpdateReply message so we can receive another input event before the
1405 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1406 // the UpdateRect IPC message handler.
[email protected]59383c782013-04-17 16:43:271407 if (pending_input_event_ack_)
[email protected]936c6f52011-12-13 01:35:261408 Send(pending_input_event_ack_.release());
1409
[email protected]ab543072013-01-25 04:38:151410 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211411 // OnSwapBuffersPosted), meaning a message has been added to the
1412 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1413 // the message now.
[email protected]59383c782013-04-17 16:43:271414 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:211415 // sending an ack to browser process that the paint is complete...
1416 update_reply_pending_ = pending_update_params_->needs_ack;
1417 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1418 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341419 }
[email protected]53d3f302009-12-21 04:42:051420
[email protected]29ed96a2012-02-04 18:12:161421 // If we're software rendering then we're done initiating the paint.
1422 if (!is_accelerated_compositing_active_)
1423 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291424}
1425
[email protected]f0c2a242013-03-15 19:34:521426void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151427 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281428 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521429 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151430}
1431
initial.commit09911bf2008-07-26 23:55:291432///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461433// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291434
[email protected]4873c7d2009-07-16 06:36:281435void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571436 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481437 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181438 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571439 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291440 return;
1441
[email protected]552e6002009-11-19 05:24:571442 paint_aggregator_.InvalidateRect(damaged_rect);
1443
1444 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241445 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571446 return;
1447 if (!paint_aggregator_.HasPendingUpdate())
1448 return;
[email protected]aa4117f2011-12-09 22:19:211449 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241450 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1451 return;
1452
1453 // When GPU rendering, combine pending animations and invalidations into
1454 // a single update.
[email protected]816edc62012-03-17 01:27:221455 if (is_accelerated_compositing_active_ &&
1456 animation_update_pending_ &&
1457 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571458 return;
1459
1460 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291461 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1462 // on the call stack.
1463 // 2) Allows us to collect more damage rects before painting to help coalesce
1464 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241465 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111466 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211467 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291468}
1469
[email protected]990278ff2012-11-13 02:12:551470void RenderWidget::didScrollRect(int dx, int dy,
1471 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461472 // Drop scrolls on the floor when we are in compositing mode.
1473 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561474 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461475 return;
1476
[email protected]552e6002009-11-19 05:24:571477 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481478 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181479 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571480 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291481 return;
1482
[email protected]990278ff2012-11-13 02:12:551483 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571484
1485 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241486 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571487 return;
1488 if (!paint_aggregator_.HasPendingUpdate())
1489 return;
[email protected]aa4117f2011-12-09 22:19:211490 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241491 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1492 return;
1493
1494 // When GPU rendering, combine pending animations and invalidations into
1495 // a single update.
[email protected]816edc62012-03-17 01:27:221496 if (is_accelerated_compositing_active_ &&
1497 animation_update_pending_ &&
1498 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571499 return;
1500
1501 // Perform updating asynchronously. This serves two purposes:
1502 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1503 // on the call stack.
1504 // 2) Allows us to collect more damage rects before painting to help coalesce
1505 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241506 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111507 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211508 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291509}
1510
[email protected]244ac1892011-12-02 17:04:471511void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091512 if (size_.width() != new_size.width || size_.height() != new_size.height) {
[email protected]eac2b362013-05-22 07:01:451513 size_ = new_size;
[email protected]20fbfc22013-05-08 20:50:581514
[email protected]eac2b362013-05-22 07:01:451515 // If we don't clear PaintAggregator after changing autoResize state, then
1516 // we might end up in a situation where bitmap_rect is larger than the
1517 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1518 // with invalid damage rects.
1519 paint_aggregator_.ClearPendingUpdate();
1520
[email protected]70dee7e2013-05-29 18:28:301521 if (RenderThreadImpl::current()->layout_test_mode()) {
[email protected]eac2b362013-05-22 07:01:451522 WebRect new_pos(rootWindowRect().x,
1523 rootWindowRect().y,
1524 new_size.width,
1525 new_size.height);
1526 view_screen_rect_ = new_pos;
1527 window_screen_rect_ = new_pos;
[email protected]8be1c582013-03-06 00:55:031528 }
[email protected]20fbfc22013-05-08 20:50:581529
[email protected]eac2b362013-05-22 07:01:451530 AutoResizeCompositor();
[email protected]20fbfc22013-05-08 20:50:581531
[email protected]70dee7e2013-05-29 18:28:301532 if (!RenderThreadImpl::current()->layout_test_mode())
[email protected]20fbfc22013-05-08 20:50:581533 need_update_rect_for_auto_resize_ = true;
[email protected]ea3ee0a2012-05-15 03:43:091534 }
[email protected]244ac1892011-12-02 17:04:471535}
1536
[email protected]3a1c8a8032013-03-18 22:35:321537void RenderWidget::AutoResizeCompositor() {
[email protected]97e1bf72013-03-06 14:06:051538 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1539 device_scale_factor_));
1540 if (compositor_)
1541 compositor_->setViewportSize(size_, physical_backing_size_);
1542}
1543
[email protected]91acd1c2012-03-14 08:32:391544void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221545 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1546
[email protected]c63b4d42012-04-26 01:01:071547#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211548 if (!is_accelerated_compositing_active_) {
1549 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1550 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1551 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1552 // going to switch to accelerated compositing, the GPU process may need
1553 // round-trips to the browser's UI thread before finishing the frame,
1554 // causing deadlocks if we delay the UpdateRect until we receive the
1555 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071556 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1557 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211558 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1559 }
[email protected]c63b4d42012-04-26 01:01:071560#endif
[email protected]aa4117f2011-12-09 22:19:211561
[email protected]ea162f92011-10-04 23:08:221562 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421563 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241564 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221565}
1566
1567void RenderWidget::didDeactivateCompositor() {
1568 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1569
1570 is_accelerated_compositing_active_ = false;
1571 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1572 routing_id_, is_accelerated_compositing_active_));
1573
[email protected]ea162f92011-10-04 23:08:221574 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241575 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091576
1577 // In single-threaded mode, we exit force compositing mode and re-enter in
1578 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1579 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1580 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361581 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091582 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561583}
1584
[email protected]e195e582013-03-08 01:32:591585void RenderWidget::initializeLayerTreeView() {
[email protected]c5fa4c42013-07-20 05:47:371586 compositor_ = RenderWidgetCompositor::Create(
1587 this, is_threaded_compositing_enabled_);
[email protected]e195e582013-03-08 01:32:591588 if (!compositor_)
1589 return;
1590
1591 compositor_->setViewportSize(size_, physical_backing_size_);
1592 if (init_complete_)
1593 compositor_->setSurfaceReady();
1594}
1595
[email protected]8926c602013-01-23 05:32:061596WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281597 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061598}
1599
[email protected]9ed83fe2013-02-27 01:52:281600void RenderWidget::suppressCompositorScheduling(bool enable) {
1601 if (compositor_)
1602 compositor_->SetSuppressScheduleComposite(enable);
1603}
1604
[email protected]9cd43a62012-03-26 08:03:561605void RenderWidget::willBeginCompositorFrame() {
1606 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371607
[email protected]cadac622013-06-11 16:46:361608 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy().get());
[email protected]abe8b3a2012-03-28 21:19:371609
1610 // The following two can result in further layout and possibly
1611 // enable GPU acceleration so they need to be called before any painting
1612 // is done.
[email protected]cb9e2632013-06-18 11:26:471613 UpdateTextInputType();
1614#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:131615 UpdateTextInputState(false, true);
[email protected]cb9e2632013-06-18 11:26:471616#endif
[email protected]abe8b3a2012-03-28 21:19:371617 UpdateSelectionBounds();
1618
[email protected]9cd43a62012-03-26 08:03:561619 WillInitiatePaint();
1620}
1621
[email protected]3391a0772012-03-28 00:32:071622void RenderWidget::didBecomeReadyForAdditionalInput() {
1623 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
[email protected]59383c782013-04-17 16:43:271624 if (pending_input_event_ack_)
[email protected]3391a0772012-03-28 00:32:071625 Send(pending_input_event_ack_.release());
1626}
1627
[email protected]6fceb912013-02-15 06:24:151628void RenderWidget::DidCommitCompositorFrame() {
1629}
1630
[email protected]58264a32011-11-17 23:36:151631void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501632 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351633 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1634 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421635 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU",
1636 TRACE_EVENT_SCOPE_THREAD);
[email protected]29ed96a2012-02-04 18:12:161637 // Notify subclasses that we initiated the paint operation.
1638 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151639}
1640
1641void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181642 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1643
1644 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561645 DidFlushPaint();
1646
[email protected]aa4117f2011-12-09 22:19:211647 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151648 return;
1649
[email protected]ea3ee0a2012-05-15 03:43:091650 if (!next_paint_flags_ &&
1651 !need_update_rect_for_auto_resize_ &&
1652 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151653 return;
[email protected]ea3ee0a2012-05-15 03:43:091654 }
[email protected]58264a32011-11-17 23:36:151655
1656 ViewHostMsg_UpdateRect_Params params;
1657 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151658 params.plugin_window_moves.swap(plugin_window_moves_);
1659 params.flags = next_paint_flags_;
1660 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121661 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091662 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151663
1664 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1665 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091666 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151667}
1668
[email protected]f98d7e3c2010-09-13 22:30:461669void RenderWidget::scheduleComposite() {
[email protected]cadac622013-06-11 16:46:361670 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get() &&
[email protected]ba91a792013-02-06 09:48:281671 compositor_) {
1672 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041673 } else {
[email protected]c3d45532011-10-07 19:20:401674 // TODO(nduca): replace with something a little less hacky. The reason this
1675 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1676 // contains a lot of host-renderer synchronization logic that is still
1677 // important for the accelerated compositing case. The option of simply
1678 // duplicating all that code is less desirable than "faking out" the
1679 // invalidation path using a magical damage rect.
1680 didInvalidateRect(WebRect(0, 0, 1, 1));
1681 }
[email protected]f98d7e3c2010-09-13 22:30:461682}
1683
[email protected]5f8b1022011-01-21 23:34:501684void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201685 if (animation_update_pending_)
1686 return;
1687
[email protected]921244e42011-07-20 16:36:301688 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201689 animation_update_pending_ = true;
1690 if (!animation_timer_.IsRunning()) {
1691 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1692 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211693 }
[email protected]5f8b1022011-01-21 23:34:501694}
1695
[email protected]4873c7d2009-07-16 06:36:281696void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301697 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521698 WebCursor cursor;
[email protected]953bd0062013-08-01 00:58:401699 InitializeCursorFromWebKitCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291700 // Only send a SetCursor message if we need to make a change.
1701 if (!current_cursor_.IsEqual(cursor)) {
1702 current_cursor_ = cursor;
1703 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1704 }
1705}
1706
1707// We are supposed to get a single call to Show for a newly created RenderWidget
1708// that was created via RenderWidget::CreateWebView. So, we wait until this
1709// point to dispatch the ShowWidget message.
1710//
1711// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281712// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291713//
[email protected]4873c7d2009-07-16 06:36:281714void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291715 DCHECK(!did_show_) << "received extraneous Show call";
1716 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1717 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1718
[email protected]8de12d942010-11-17 20:42:441719 if (did_show_)
1720 return;
1721
1722 did_show_ = true;
1723 // NOTE: initial_pos_ may still have its default values at this point, but
1724 // that's okay. It'll be ignored if as_popup is false, or the browser
1725 // process will impose a default position otherwise.
1726 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1727 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291728}
1729
[email protected]9b003482013-05-21 14:00:171730void RenderWidget::didProgrammaticallyScroll(
1731 const WebKit::WebPoint& scroll_point) {
1732 if (!compositor_)
1733 return;
1734 Send(new ViewHostMsg_DidProgrammaticallyScroll(
1735 routing_id_, gfx::Vector2d(scroll_point.x, scroll_point.y)));
1736}
1737
[email protected]4873c7d2009-07-16 06:36:281738void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291739}
1740
[email protected]4873c7d2009-07-16 06:36:281741void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291742}
1743
[email protected]2533ce12009-05-09 00:02:241744void RenderWidget::DoDeferredClose() {
1745 Send(new ViewHostMsg_Close(routing_id_));
1746}
1747
[email protected]4873c7d2009-07-16 06:36:281748void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321749 if (is_swapped_out_) {
1750 // This widget is currently swapped out, and the active widget is in a
1751 // different process. Have the browser route the close request to the
1752 // active widget instead, so that the correct unload handlers are run.
1753 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1754 return;
1755 }
1756
initial.commit09911bf2008-07-26 23:55:291757 // If a page calls window.close() twice, we'll end up here twice, but that's
1758 // OK. It is safe to send multiple Close messages.
1759
[email protected]2533ce12009-05-09 00:02:241760 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1761 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1762 // could be closed before the JS finishes executing. So instead, post a
1763 // message back to the message loop, which won't run until the JS is
1764 // complete, and then the Close message can be sent.
[email protected]dd32b1272013-05-04 14:17:111765 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211766 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291767}
1768
1769void RenderWidget::Close() {
1770 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061771 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281772 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281773 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291774 webwidget_ = NULL;
1775 }
1776}
1777
[email protected]4873c7d2009-07-16 06:36:281778WebRect RenderWidget::windowRect() {
1779 if (pending_window_rect_count_)
1780 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241781
[email protected]80ad8622012-11-07 16:33:031782 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291783}
1784
[email protected]8a9d6ca32011-06-06 20:11:301785void RenderWidget::setToolTipText(const WebKit::WebString& text,
1786 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541787 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301788}
1789
[email protected]4873c7d2009-07-16 06:36:281790void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291791 if (did_show_) {
[email protected]70dee7e2013-05-29 18:28:301792 if (!RenderThreadImpl::current()->layout_test_mode()) {
[email protected]8be1c582013-03-06 00:55:031793 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
1794 SetPendingWindowRect(pos);
1795 } else {
1796 WebSize new_size(pos.width, pos.height);
[email protected]d9083762013-03-24 01:36:401797 Resize(new_size, new_size, overdraw_bottom_height_,
1798 WebRect(), is_fullscreen_, NO_RESIZE_ACK);
[email protected]8be1c582013-03-06 00:55:031799 view_screen_rect_ = pos;
1800 window_screen_rect_ = pos;
1801 }
initial.commit09911bf2008-07-26 23:55:291802 } else {
1803 initial_pos_ = pos;
1804 }
1805}
1806
[email protected]2533ce12009-05-09 00:02:241807void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1808 pending_window_rect_ = rect;
1809 pending_window_rect_count_++;
1810}
1811
[email protected]4873c7d2009-07-16 06:36:281812WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241813 if (pending_window_rect_count_) {
1814 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1815 // the RootWindowRect is probably going to return wrong results since the
1816 // browser may not have processed the Move yet. There isn't really anything
1817 // good to do in this case, and it shouldn't happen - since this size is
1818 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281819 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241820 }
1821
[email protected]80ad8622012-11-07 16:33:031822 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371823}
1824
[email protected]4873c7d2009-07-16 06:36:281825WebRect RenderWidget::windowResizerRect() {
1826 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191827}
1828
[email protected]fa7b1dc2010-06-23 17:53:041829void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031830 // To prevent this renderer process from sending unnecessary IPC messages to
1831 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041832 // only during the input method attached to the browser process is active.
1833 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291834}
1835
[email protected]fa7b1dc2010-06-23 17:53:041836void RenderWidget::OnImeSetComposition(
1837 const string16& text,
1838 const std::vector<WebCompositionUnderline>& underlines,
1839 int selection_start, int selection_end) {
[email protected]0d1ebed12013-08-05 22:01:131840 if (!ShouldHandleImeEvent())
[email protected]4873c7d2009-07-16 06:36:281841 return;
[email protected]66fca5bc2013-05-23 06:58:291842 ImeEventGuard guard(this);
[email protected]88dbe32f2013-06-20 23:31:361843 if (!webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041844 text, WebVector<WebCompositionUnderline>(underlines),
1845 selection_start, selection_end)) {
1846 // If we failed to set the composition text, then we need to let the browser
1847 // process to cancel the input method's ongoing composition session, to make
1848 // sure we are in a consistent state.
1849 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:261850 }
[email protected]501ea13d2013-07-09 17:03:291851#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361852 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291853#endif
[email protected]fa7b1dc2010-06-23 17:53:041854}
1855
[email protected]0e45bd02013-07-12 20:20:021856void RenderWidget::OnImeConfirmComposition(const string16& text,
1857 const ui::Range& replacement_range,
1858 bool keep_selection) {
[email protected]0d1ebed12013-08-05 22:01:131859 if (!ShouldHandleImeEvent())
[email protected]d0be63772011-12-20 23:18:041860 return;
[email protected]66fca5bc2013-05-23 06:58:291861 ImeEventGuard guard(this);
[email protected]d0be63772011-12-20 23:18:041862 handling_input_event_ = true;
[email protected]0e45bd02013-07-12 20:20:021863 if (text.length())
1864 webwidget_->confirmComposition(text);
1865 else if (keep_selection)
1866 webwidget_->confirmComposition(WebWidget::KeepSelection);
1867 else
1868 webwidget_->confirmComposition(WebWidget::DoNotKeepSelection);
[email protected]d0be63772011-12-20 23:18:041869 handling_input_event_ = false;
[email protected]501ea13d2013-07-09 17:03:291870#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361871 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291872#endif
initial.commit09911bf2008-07-26 23:55:291873}
1874
[email protected]948f7ab72010-05-28 23:48:081875// This message causes the renderer to render an image of the
1876// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251877void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1878 int tag,
1879 const gfx::Size& page_size,
1880 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001881 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1882 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251883 // Close our unused handle.
1884#if defined(OS_WIN)
1885 ::CloseHandle(dib_handle);
1886#elif defined(OS_MACOSX)
1887 base::SharedMemory::CloseHandle(dib_handle);
1888#endif
1889 }
[email protected]d65adb12010-04-28 17:26:491890 return;
[email protected]45c6aad32010-11-11 04:46:251891 }
[email protected]d65adb12010-04-28 17:26:491892
[email protected]948f7ab72010-05-28 23:48:081893 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491894 // If one of these is empty, then we just return the dib we were
1895 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091896 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491897 return;
1898 }
1899
1900 // Map the given DIB ID into this process, and unmap it at the end
1901 // of this function.
[email protected]45c6aad32010-11-11 04:46:251902 scoped_ptr<TransportDIB> paint_at_size_buffer(
1903 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301904
[email protected]4b01b962012-10-09 23:17:351905 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281906 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351907 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281908 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511909 gfx::Size canvas_size = page_size_in_pixel;
1910 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491911 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511912 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491913 static_cast<float>(canvas_size.height());
1914
[email protected]ee8d6fd2010-05-26 17:05:481915 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491916 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1917 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481918 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491919
[email protected]36808ad2010-10-20 19:18:301920 scoped_ptr<skia::PlatformCanvas> canvas(
1921 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1922 canvas_size.height()));
[email protected]59383c782013-04-17 16:43:271923 if (!canvas) {
[email protected]36808ad2010-10-20 19:18:301924 NOTREACHED();
1925 return;
1926 }
1927
[email protected]d65adb12010-04-28 17:26:491928 // Reset bounds to what we actually received, but they should be the
1929 // same.
1930 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1931 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1932 bounds.set_width(canvas->getDevice()->width());
1933 bounds.set_height(canvas->getDevice()->height());
1934
1935 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081936 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491937 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1938
[email protected]948f7ab72010-05-28 23:48:081939 // Have to make sure we're laid out at the right size before
1940 // rendering.
1941 gfx::Size old_size = webwidget_->size();
1942 webwidget_->resize(page_size);
1943 webwidget_->layout();
1944
[email protected]d65adb12010-04-28 17:26:491945 // Paint the entire thing (using original bounds, not scaled bounds).
1946 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1947 canvas->restore();
1948
[email protected]948f7ab72010-05-28 23:48:081949 // Return the widget to its previous size.
1950 webwidget_->resize(old_size);
1951
[email protected]c88c9442010-07-19 18:55:091952 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491953}
1954
[email protected]51a49502013-03-23 01:50:191955void RenderWidget::OnSnapshot(const gfx::Rect& src_subrect) {
1956 SkBitmap snapshot;
1957
1958 if (OnSnapshotHelper(src_subrect, &snapshot)) {
1959 Send(new ViewHostMsg_Snapshot(routing_id(), true, snapshot));
1960 } else {
1961 Send(new ViewHostMsg_Snapshot(routing_id(), false, SkBitmap()));
1962 }
1963}
1964
1965bool RenderWidget::OnSnapshotHelper(const gfx::Rect& src_subrect,
1966 SkBitmap* snapshot) {
1967 base::TimeTicks beginning_time = base::TimeTicks::Now();
1968
1969 if (!webwidget_ || src_subrect.IsEmpty())
1970 return false;
1971
1972 gfx::Rect viewport_size = gfx::IntersectRects(
1973 src_subrect, gfx::Rect(physical_backing_size_));
1974
1975 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
1976 skia::CreatePlatformCanvas(viewport_size.width(),
1977 viewport_size.height(),
1978 true,
1979 NULL,
1980 skia::RETURN_NULL_ON_FAILURE));
[email protected]59383c782013-04-17 16:43:271981 if (!canvas)
[email protected]51a49502013-03-23 01:50:191982 return false;
1983
1984 canvas->save();
1985 webwidget_->layout();
1986
1987 PaintRect(viewport_size, viewport_size.origin(), canvas.get());
1988 canvas->restore();
1989
1990 const SkBitmap& bitmap = skia::GetTopDevice(*canvas)->accessBitmap(false);
1991 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
1992 return false;
1993
1994 UMA_HISTOGRAM_TIMES("Renderer4.Snapshot",
1995 base::TimeTicks::Now() - beginning_time);
1996 return true;
1997}
1998
[email protected]0bc1f572013-04-17 01:46:311999void RenderWidget::OnRepaint(gfx::Size size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:122000 // During shutdown we can just ignore this message.
2001 if (!webwidget_)
2002 return;
2003
[email protected]0bc1f572013-04-17 01:46:312004 // Even if the browser provides an empty damage rect, it's still expecting to
2005 // receive a repaint ack so just damage the entire widget bounds.
2006 if (size_to_paint.IsEmpty()) {
2007 size_to_paint = size_;
2008 }
2009
[email protected]ec7dc112008-08-06 05:30:122010 set_next_paint_is_repaint_ack();
[email protected]0bc1f572013-04-17 01:46:312011 if (is_accelerated_compositing_active_ && compositor_) {
2012 compositor_->SetNeedsRedrawRect(gfx::Rect(size_to_paint));
[email protected]f98d7e3c2010-09-13 22:30:462013 } else {
2014 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
2015 didInvalidateRect(repaint_rect);
2016 }
[email protected]ec7dc112008-08-06 05:30:122017}
2018
[email protected]79fa22e2013-08-23 15:18:122019void RenderWidget::OnSyntheticGestureCompleted() {
2020 pending_synthetic_gesture_.Run();
[email protected]0e241b4b2012-08-18 09:06:272021}
2022
[email protected]4873c7d2009-07-16 06:36:282023void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:112024 if (!webwidget_)
2025 return;
[email protected]4873c7d2009-07-16 06:36:282026 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112027}
2028
[email protected]80ad8622012-11-07 16:33:032029void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
2030 const gfx::Rect& window_screen_rect) {
2031 view_screen_rect_ = view_screen_rect;
2032 window_screen_rect_ = window_screen_rect;
2033 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
2034}
2035
[email protected]105dffb42013-02-20 03:46:212036#if defined(OS_ANDROID)
2037void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
2038 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
2039}
[email protected]2384b6c2013-02-28 23:58:512040
2041void RenderWidget::OnShowImeIfNeeded() {
[email protected]0d1ebed12013-08-05 22:01:132042 UpdateTextInputState(true, true);
2043}
2044
2045void RenderWidget::IncrementOutstandingImeEventAcks() {
2046 ++outstanding_ime_acks_;
2047}
2048
2049void RenderWidget::OnImeEventAck() {
2050 --outstanding_ime_acks_;
2051 DCHECK(outstanding_ime_acks_ >= 0);
[email protected]2384b6c2013-02-28 23:58:512052}
[email protected]105dffb42013-02-20 03:46:212053#endif
2054
[email protected]0d1ebed12013-08-05 22:01:132055bool RenderWidget::ShouldHandleImeEvent() {
2056#if defined(OS_ANDROID)
2057 return !!webwidget_ && outstanding_ime_acks_ == 0;
2058#else
2059 return !!webwidget_;
2060#endif
2061}
2062
[email protected]468ac582012-11-20 00:53:192063void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
2064 if (device_scale_factor_ == device_scale_factor)
2065 return;
2066
2067 device_scale_factor_ = device_scale_factor;
2068
2069 if (!is_accelerated_compositing_active_) {
2070 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
2071 } else {
2072 scheduleComposite();
2073 }
2074}
2075
[email protected]adab2332013-07-25 18:04:322076PepperPluginInstanceImpl* RenderWidget::GetBitmapForOptimizedPluginPaint(
2077 const gfx::Rect& paint_bounds,
2078 TransportDIB** dib,
2079 gfx::Rect* location,
2080 gfx::Rect* clip,
2081 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:462082 // Bare RenderWidgets don't support optimized plugin painting.
2083 return NULL;
[email protected]ca4847f2010-09-24 05:39:152084}
2085
[email protected]ceb36f7d2012-10-31 18:33:242086gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:522087 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:242088 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:522089}
2090
[email protected]bee16aab2009-08-26 15:55:032091void RenderWidget::SetHidden(bool hidden) {
2092 if (is_hidden_ == hidden)
2093 return;
2094
2095 // The status has changed. Tell the RenderThread about it.
2096 is_hidden_ = hidden;
2097 if (is_hidden_)
[email protected]380244092011-10-07 17:26:272098 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:032099 else
[email protected]380244092011-10-07 17:26:272100 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:032101}
2102
[email protected]2b624c562011-10-27 22:58:262103void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262104 if (!webwidget_)
2105 return;
2106
2107 if (is_fullscreen_) {
2108 webwidget_->willExitFullScreen();
2109 } else {
2110 webwidget_->willEnterFullScreen();
2111 }
[email protected]2b624c562011-10-27 22:58:262112}
2113
2114void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262115 if (!webwidget_)
2116 return;
2117
2118 if (is_fullscreen_) {
2119 webwidget_->didEnterFullScreen();
2120 } else {
2121 webwidget_->didExitFullScreen();
2122 }
[email protected]2b624c562011-10-27 22:58:262123}
2124
[email protected]699ab0d2009-04-23 23:19:142125void RenderWidget::SetBackground(const SkBitmap& background) {
2126 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:462127
[email protected]699ab0d2009-04-23 23:19:142128 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:282129 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:142130}
2131
[email protected]674741932009-02-04 23:44:462132bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:052133 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462134}
2135
2136bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:052137 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462138}
2139
2140void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:052141 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:462142}
2143
2144void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:052145 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:462146}
2147
2148void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:052149 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:462150}
2151
[email protected]b18583c2012-12-18 06:55:272152static bool IsDateTimeInput(ui::TextInputType type) {
2153 return type == ui::TEXT_INPUT_TYPE_DATE ||
2154 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
2155 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
2156 type == ui::TEXT_INPUT_TYPE_MONTH ||
2157 type == ui::TEXT_INPUT_TYPE_TIME ||
2158 type == ui::TEXT_INPUT_TYPE_WEEK;
2159}
2160
[email protected]66fca5bc2013-05-23 06:58:292161
2162void RenderWidget::StartHandlingImeEvent() {
2163 DCHECK(!handling_ime_event_);
2164 handling_ime_event_ = true;
2165}
2166
2167void RenderWidget::FinishHandlingImeEvent() {
2168 DCHECK(handling_ime_event_);
2169 handling_ime_event_ = false;
2170 // While handling an ime event, text input state and selection bounds updates
2171 // are ignored. These must explicitly be updated once finished handling the
2172 // ime event.
2173 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472174#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132175 UpdateTextInputState(false, false);
[email protected]cb9e2632013-06-18 11:26:472176#endif
[email protected]66fca5bc2013-05-23 06:58:292177}
2178
[email protected]cb9e2632013-06-18 11:26:472179void RenderWidget::UpdateTextInputType() {
2180 if (!input_method_is_active_)
2181 return;
2182
[email protected]e7c569d2013-07-17 16:00:362183 ui::TextInputType new_type = GetTextInputType();
[email protected]cb9e2632013-06-18 11:26:472184 if (IsDateTimeInput(new_type))
2185 return; // Not considered as a text input field in WebKit/Chromium.
2186
2187 bool new_can_compose_inline = CanComposeInline();
2188
[email protected]e7c569d2013-07-17 16:00:362189 WebKit::WebTextInputInfo new_info;
2190 if (webwidget_)
2191 new_info = webwidget_->textInputInfo();
[email protected]b256eca2013-07-11 10:57:402192 const ui::TextInputMode new_mode = ConvertInputMode(new_info.inputMode);
2193
[email protected]cb9e2632013-06-18 11:26:472194 if (text_input_type_ != new_type
[email protected]b256eca2013-07-11 10:57:402195 || can_compose_inline_ != new_can_compose_inline
2196 || text_input_mode_ != new_mode) {
[email protected]cb9e2632013-06-18 11:26:472197 Send(new ViewHostMsg_TextInputTypeChanged(routing_id(),
2198 new_type,
[email protected]b256eca2013-07-11 10:57:402199 new_can_compose_inline,
2200 new_mode));
[email protected]cb9e2632013-06-18 11:26:472201 text_input_type_ = new_type;
2202 can_compose_inline_ = new_can_compose_inline;
[email protected]b256eca2013-07-11 10:57:402203 text_input_mode_ = new_mode;
[email protected]cb9e2632013-06-18 11:26:472204 }
2205}
2206
2207#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132208void RenderWidget::UpdateTextInputState(bool show_ime_if_needed,
2209 bool send_ime_ack) {
[email protected]e8f775f2013-02-14 21:00:502210 if (handling_ime_event_)
2211 return;
[email protected]3306f262012-09-21 19:20:422212 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:292213 return;
[email protected]ad26ef42011-06-17 07:59:452214 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:272215 if (IsDateTimeInput(new_type))
2216 return; // Not considered as a text input field in WebKit/Chromium.
2217
[email protected]5b739cb2012-08-21 20:35:212218 WebKit::WebTextInputInfo new_info;
2219 if (webwidget_)
2220 new_info = webwidget_->textInputInfo();
2221
[email protected]ad26ef42011-06-17 07:59:452222 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212223
[email protected]3306f262012-09-21 19:20:422224 // Only sends text input params if they are changed or if the ime should be
2225 // shown.
2226 if (show_ime_if_needed || (text_input_type_ != new_type
2227 || text_input_info_ != new_info
2228 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212229 ViewHostMsg_TextInputState_Params p;
2230 p.type = new_type;
2231 p.value = new_info.value.utf8();
2232 p.selection_start = new_info.selectionStart;
2233 p.selection_end = new_info.selectionEnd;
2234 p.composition_start = new_info.compositionStart;
2235 p.composition_end = new_info.compositionEnd;
2236 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422237 p.show_ime_if_needed = show_ime_if_needed;
[email protected]0d1ebed12013-08-05 22:01:132238 p.require_ack = send_ime_ack;
2239 if (p.require_ack)
2240 IncrementOutstandingImeEventAcks();
[email protected]5b739cb2012-08-21 20:35:212241 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2242
2243 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042244 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452245 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292246 }
initial.commit09911bf2008-07-26 23:55:292247}
[email protected]cb9e2632013-06-18 11:26:472248#endif
initial.commit09911bf2008-07-26 23:55:292249
[email protected]7c8873e2013-02-05 08:03:012250void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2251 WebRect focus_webrect;
2252 WebRect anchor_webrect;
2253 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2254 *focus = focus_webrect;
2255 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322256}
2257
[email protected]e99ef6f2011-10-16 01:13:002258void RenderWidget::UpdateSelectionBounds() {
2259 if (!webwidget_)
2260 return;
[email protected]66fca5bc2013-05-23 06:58:292261 if (handling_ime_event_)
2262 return;
[email protected]e99ef6f2011-10-16 01:13:002263
[email protected]7c8873e2013-02-05 08:03:012264 ViewHostMsg_SelectionBounds_Params params;
2265 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2266 if (selection_anchor_rect_ != params.anchor_rect ||
2267 selection_focus_rect_ != params.focus_rect) {
2268 selection_anchor_rect_ = params.anchor_rect;
2269 selection_focus_rect_ = params.focus_rect;
2270 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292271 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012272 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352273 }
[email protected]501ea13d2013-07-09 17:03:292274#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362275 UpdateCompositionInfo(false);
[email protected]501ea13d2013-07-09 17:03:292276#endif
[email protected]e99ef6f2011-10-16 01:13:002277}
2278
[email protected]73bf95812011-10-12 11:38:322279// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452280COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2281 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2282COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2283 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2284COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2285 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182286COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2287 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2288COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2289 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2290COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2291 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2292COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2293 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2294COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2295 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002296COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2297 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2298COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2299 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2300COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2301 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2302COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2303 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2304COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2305 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2306COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2307 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012308COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2309 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2310COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2311 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152312COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2313 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452314
[email protected]5b739cb2012-08-21 20:35:212315ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2316 WebKit::WebTextInputType type) {
2317 // Check the type is in the range representable by ui::TextInputType.
2318 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2319 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2320 return static_cast<ui::TextInputType>(type);
2321}
2322
[email protected]ad26ef42011-06-17 07:59:452323ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272324 if (webwidget_)
2325 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452326 return ui::TEXT_INPUT_TYPE_NONE;
2327}
2328
[email protected]501ea13d2013-07-09 17:03:292329#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
2330void RenderWidget::UpdateCompositionInfo(bool should_update_range) {
2331 ui::Range range = ui::Range();
2332 if (should_update_range) {
2333 GetCompositionRange(&range);
2334 } else {
2335 range = composition_range_;
2336 }
2337 std::vector<gfx::Rect> character_bounds;
2338 GetCompositionCharacterBounds(&character_bounds);
2339
2340 if (!ShouldUpdateCompositionInfo(range, character_bounds))
2341 return;
2342 composition_character_bounds_ = character_bounds;
2343 composition_range_ = range;
2344 Send(new ViewHostMsg_ImeCompositionRangeChanged(
2345 routing_id(), composition_range_, composition_character_bounds_));
2346}
2347
[email protected]58b48a0d2012-06-13 07:01:352348void RenderWidget::GetCompositionCharacterBounds(
2349 std::vector<gfx::Rect>* bounds) {
2350 DCHECK(bounds);
2351 bounds->clear();
2352}
2353
[email protected]88dbe32f2013-06-20 23:31:362354void RenderWidget::GetCompositionRange(ui::Range* range) {
2355 size_t location, length;
2356 if (webwidget_->compositionRange(&location, &length)) {
2357 range->set_start(location);
2358 range->set_end(location + length);
2359 } else if (webwidget_->caretOrSelectionRange(&location, &length)) {
2360 range->set_start(location);
2361 range->set_end(location + length);
2362 } else {
2363 *range = ui::Range::InvalidRange();
2364 }
2365}
2366
[email protected]501ea13d2013-07-09 17:03:292367bool RenderWidget::ShouldUpdateCompositionInfo(
2368 const ui::Range& range,
2369 const std::vector<gfx::Rect>& bounds) {
2370 if (composition_range_ != range)
2371 return true;
2372 if (bounds.size() != composition_character_bounds_.size())
2373 return true;
2374 for (size_t i = 0; i < bounds.size(); ++i) {
2375 if (bounds[i] != composition_character_bounds_[i])
2376 return true;
2377 }
2378 return false;
2379}
2380#endif
2381
[email protected]ad26ef42011-06-17 07:59:452382bool RenderWidget::CanComposeInline() {
2383 return true;
[email protected]56ea1a62011-05-30 07:05:572384}
2385
[email protected]4873c7d2009-07-16 06:36:282386WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042387 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282388}
2389
[email protected]f660d9c2012-06-06 18:31:212390float RenderWidget::deviceScaleFactor() {
2391 return device_scale_factor_;
2392}
2393
[email protected]fa7b1dc2010-06-23 17:53:042394void RenderWidget::resetInputMethod() {
2395 if (!input_method_is_active_)
2396 return;
2397
[email protected]0e45bd02013-07-12 20:20:022398 ImeEventGuard guard(this);
[email protected]fa7b1dc2010-06-23 17:53:042399 // If the last text input type is not None, then we should finish any
2400 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452401 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042402 // If a composition text exists, then we need to let the browser process
2403 // to cancel the input method's ongoing composition session.
2404 if (webwidget_->confirmComposition())
2405 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2406 }
[email protected]d4cff272011-05-02 15:46:012407
[email protected]501ea13d2013-07-09 17:03:292408#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362409 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292410#endif
[email protected]fa7b1dc2010-06-23 17:53:042411}
2412
[email protected]c68c3e4e2013-01-24 00:36:562413void RenderWidget::didHandleGestureEvent(
2414 const WebGestureEvent& event,
2415 bool event_cancelled) {
2416#if defined(OS_ANDROID)
2417 if (event_cancelled)
2418 return;
2419 if (event.type == WebInputEvent::GestureTap ||
2420 event.type == WebInputEvent::GestureLongPress) {
[email protected]0d1ebed12013-08-05 22:01:132421 UpdateTextInputState(true, true);
[email protected]c68c3e4e2013-01-24 00:36:562422 }
2423#endif
2424}
2425
[email protected]29e2fb42013-07-19 01:13:472426void RenderWidget::SchedulePluginMove(const WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292427 size_t i = 0;
2428 for (; i < plugin_window_moves_.size(); ++i) {
2429 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582430 if (move.rects_valid) {
2431 plugin_window_moves_[i] = move;
2432 } else {
2433 plugin_window_moves_[i].visible = move.visible;
2434 }
initial.commit09911bf2008-07-26 23:55:292435 break;
2436 }
2437 }
2438
2439 if (i == plugin_window_moves_.size())
2440 plugin_window_moves_.push_back(move);
2441}
[email protected]268654772009-08-06 23:02:042442
2443void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2444 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2445 i != plugin_window_moves_.end(); ++i) {
2446 if (i->window == window) {
2447 plugin_window_moves_.erase(i);
2448 break;
2449 }
2450 }
2451}
[email protected]67bfb83f2011-09-22 03:36:372452
[email protected]b63d58d2012-11-26 22:37:442453void RenderWidget::GetRenderingStats(
2454 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282455 if (compositor_)
[email protected]635353c2013-03-06 09:11:202456 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442457
[email protected]a9dc0d0f2013-08-17 02:43:182458 stats.rendering_stats.main_stats.animation_frame_count +=
2459 software_stats_.main_stats.animation_frame_count;
2460 stats.rendering_stats.main_stats.screen_frame_count +=
2461 software_stats_.main_stats.screen_frame_count;
2462 stats.rendering_stats.main_stats.paint_time +=
2463 software_stats_.main_stats.paint_time;
2464 stats.rendering_stats.main_stats.painted_pixel_count +=
2465 software_stats_.main_stats.painted_pixel_count;
[email protected]fef5e3972012-08-07 03:59:472466}
2467
[email protected]e9ff79c2012-10-19 21:31:262468bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522469 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2470 if (!gpu_channel)
2471 return false;
2472
2473 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2474}
2475
[email protected]24ed0432013-04-24 07:50:312476RenderWidgetCompositor* RenderWidget::compositor() const {
2477 return compositor_.get();
2478}
2479
[email protected]3639aa82013-06-04 11:00:042480void RenderWidget::OnSetBrowserRenderingStats(
2481 const BrowserRenderingStats& stats) {
2482 browser_rendering_stats_ = stats;
2483}
2484
2485void RenderWidget::GetBrowserRenderingStats(BrowserRenderingStats* stats) {
2486 *stats = browser_rendering_stats_;
2487}
2488
[email protected]0c2ebef2013-04-03 12:14:102489void RenderWidget::BeginSmoothScroll(
[email protected]0e241b4b2012-08-18 09:06:272490 bool down,
[email protected]79fa22e2013-08-23 15:18:122491 const SyntheticGestureCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192492 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292493 int mouse_event_x,
2494 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272495 DCHECK(!callback.is_null());
[email protected]267909d2012-10-20 04:36:192496
2497 ViewHostMsg_BeginSmoothScroll_Params params;
2498 params.scroll_down = down;
2499 params.pixels_to_scroll = pixels_to_scroll;
2500 params.mouse_event_x = mouse_event_x;
2501 params.mouse_event_y = mouse_event_y;
2502
[email protected]4a9dba42013-04-29 18:24:222503 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, params));
[email protected]79fa22e2013-08-23 15:18:122504 pending_synthetic_gesture_ = callback;
[email protected]a39ca1652012-07-13 21:30:582505}
2506
[email protected]67bfb83f2011-09-22 03:36:372507bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2508 return false;
2509}
[email protected]c3d45532011-10-07 19:20:402510
[email protected]f56c7872013-06-18 12:31:572511bool RenderWidget::WillHandleKeyEvent(const WebKit::WebKeyboardEvent& event) {
2512 return false;
2513}
2514
[email protected]41d86852012-11-07 12:23:242515bool RenderWidget::WillHandleGestureEvent(
2516 const WebKit::WebGestureEvent& event) {
2517 return false;
2518}
2519
[email protected]ce6689f2013-03-29 12:52:552520void RenderWidget::hasTouchEventHandlers(bool has_handlers) {
2521 Send(new ViewHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
2522}
2523
[email protected]3d5c243b2012-11-30 00:26:012524bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2525 return true;
2526}
2527
[email protected]0634cdd42013-08-16 00:46:092528scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
2529RenderWidget::CreateGraphicsContext3D(
[email protected]92fd8c02013-03-29 08:54:152530 const WebKit::WebGraphicsContext3D::Attributes& attributes) {
[email protected]ed7defa2013-03-12 21:29:592531 if (!webwidget_)
[email protected]0634cdd42013-08-16 00:46:092532 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]ebc0e1df2013-08-01 02:46:222533 if (CommandLine::ForCurrentProcess()->HasSwitch(
2534 switches::kDisableGpuCompositing))
[email protected]0634cdd42013-08-16 00:46:092535 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]8f746982013-03-21 06:28:032536 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2537 new WebGraphicsContext3DCommandBufferImpl(
2538 surface_id(),
2539 GetURLForGraphicsContext3D(),
2540 RenderThreadImpl::current(),
2541 weak_ptr_factory_.GetWeakPtr()));
[email protected]ed7defa2013-03-12 21:29:592542
[email protected]990e4c212013-06-08 05:03:332543 if (!context->InitializeWithDefaultBufferSizes(
[email protected]8f746982013-03-21 06:28:032544 attributes,
2545 false /* bind generates resources */,
2546 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE))
[email protected]0634cdd42013-08-16 00:46:092547 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
2548 return context.Pass();
[email protected]ed7defa2013-03-12 21:29:592549}
2550
[email protected]e9ff79c2012-10-19 21:31:262551} // namespace content