blob: 829abe6d4a35b9f4a129daca0ca38908326befa2 [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]ed7defa2013-03-12 21:29:5922#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]c084330e02013-04-27 01:08:1523#include "content/common/input_messages.h"
[email protected]992db4c2011-05-12 15:37:1524#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5025#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2926#include "content/public/common/content_switches.h"
[email protected]953bd0062013-08-01 00:58:4027#include "content/renderer/cursor_utils.h"
[email protected]ed7defa2013-03-12 21:29:5928#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]2847b222013-04-06 00:59:2429#include "content/renderer/gpu/compositor_software_output_device.h"
[email protected]36e5ff12013-06-11 12:19:2930#include "content/renderer/gpu/delegated_compositor_output_surface.h"
[email protected]ea5f70a2013-03-07 12:30:3631#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5932#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2833#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]66fca5bc2013-05-23 06:58:2934#include "content/renderer/ime_event_guard.h"
[email protected]adab2332013-07-25 18:04:3235#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
[email protected]8704f89b2011-04-15 00:30:0536#include "content/renderer/render_process.h"
[email protected]fd33b59ff2013-06-21 22:57:0537#include "content/renderer/render_process_visibility_manager.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]14392a52012-05-02 20:28:44191 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:29192 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56193 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11194 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29195 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30196 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35197 current_paint_buf_(NULL),
[email protected]d9083762013-03-24 01:36:40198 overdraw_bottom_height_(0.f),
initial.commit09911bf2008-07-26 23:55:29199 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06200 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05201 update_reply_pending_(false),
[email protected]847a2582013-03-09 02:29:51202 auto_resize_mode_(false),
[email protected]ea3ee0a2012-05-15 03:43:09203 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24204 using_asynchronous_swapbuffers_(false),
205 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29206 did_show_(false),
initial.commit09911bf2008-07-26 23:55:29207 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:09208 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29209 needs_repainting_on_restore_(false),
210 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49211 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50212 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48213 closing_(false),
[email protected]14392a52012-05-02 20:28:44214 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04215 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45216 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
217 can_compose_inline_(true),
[email protected]b256eca2013-07-11 10:57:40218 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
[email protected]3e2b375b2010-04-07 17:03:12219 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48220 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12221 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50222 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21223 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07224 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04225 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52226 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]ed7defa2013-03-12 21:29:59227 is_threaded_compositing_enabled_(false),
[email protected]53b4cc12013-07-18 23:02:30228 next_output_surface_id_(0),
[email protected]0d1ebed12013-08-05 22:01:13229#if defined(OS_ANDROID)
230 outstanding_ime_acks_(0),
231#endif
[email protected]69e797f2013-04-30 01:10:22232 weak_ptr_factory_(this) {
[email protected]8b3f0eb2012-05-03 19:15:05233 if (!swapped_out)
234 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27235 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18236 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
237 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36238 is_threaded_compositing_enabled_ =
239 CommandLine::ForCurrentProcess()->HasSwitch(
240 switches::kEnableThreadedCompositing);
[email protected]fd33b59ff2013-06-21 22:57:05241
242 RenderProcessVisibilityManager::GetInstance()->WidgetVisibilityChanged(true);
initial.commit09911bf2008-07-26 23:55:29243}
244
245RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11246 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21247 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35248 if (current_paint_buf_) {
[email protected]467414eb2013-07-30 20:32:29249 if (RenderProcess::current()) {
250 // If the RenderProcess is already gone, it will have released all DIBs
251 // in its destructor anyway.
252 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
253 }
[email protected]b4d08452010-10-05 17:34:35254 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29255 }
[email protected]992db4c2011-05-12 15:37:15256 // If we are swapped out, we have released already.
257 if (!is_swapped_out_)
258 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29259}
260
[email protected]484955942010-08-19 16:13:18261// static
[email protected]8085dbc82008-09-26 22:53:44262RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04263 WebKit::WebPopupType popup_type,
264 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29265 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41266 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04267 new RenderWidget(popup_type, screen_info, false));
[email protected]a635f942012-12-07 10:34:29268 if (widget->Init(opener_id)) { // adds reference on success.
[email protected]fc72bb12013-06-02 21:13:46269 return widget.get();
[email protected]a635f942012-12-07 10:34:29270 }
271 return NULL;
initial.commit09911bf2008-07-26 23:55:29272}
273
[email protected]484955942010-08-19 16:13:18274// static
275WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
276 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03277 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18278 break;
279 case WebKit::WebPopupTypeSelect:
280 case WebKit::WebPopupTypeSuggestion:
281 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44282 case WebKit::WebPopupTypePage:
283 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03284 case WebKit::WebPopupTypeHelperPlugin:
285 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18286 default:
287 NOTREACHED();
288 }
289 return NULL;
290}
291
[email protected]a635f942012-12-07 10:34:29292bool RenderWidget::Init(int32 opener_id) {
293 return DoInit(opener_id,
294 RenderWidget::CreateWebWidget(this),
295 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
296 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18297}
298
[email protected]a635f942012-12-07 10:34:29299bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06300 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18301 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29302 DCHECK(!webwidget_);
303
304 if (opener_id != MSG_ROUTING_NONE)
305 opener_id_ = opener_id;
306
[email protected]484955942010-08-19 16:13:18307 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29308
[email protected]380244092011-10-07 17:26:27309 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29310 if (result) {
[email protected]380244092011-10-07 17:26:27311 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29312 // Take a reference on behalf of the RenderThread. This will be balanced
313 // when we receive ViewMsg_Close.
314 AddRef();
[email protected]a635f942012-12-07 10:34:29315 return true;
initial.commit09911bf2008-07-26 23:55:29316 } else {
[email protected]a635f942012-12-07 10:34:29317 // The above Send can fail when the tab is closing.
318 return false;
initial.commit09911bf2008-07-26 23:55:29319 }
320}
321
[email protected]fc4404d2012-11-07 19:53:30322// This is used to complete pending inits and non-pending inits.
323void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29324 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29325
[email protected]fc4404d2012-11-07 19:53:30326 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42327
[email protected]8926c602013-01-23 05:32:06328 if (webwidget_ && is_threaded_compositing_enabled_) {
329 webwidget_->enterForceCompositingMode(true);
330 }
[email protected]ba91a792013-02-06 09:48:28331 if (compositor_) {
332 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09333 }
[email protected]05a980d7a2012-02-07 22:16:42334 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29335
[email protected]6de74452009-02-25 18:04:59336 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29337}
338
[email protected]992db4c2011-05-12 15:37:15339void RenderWidget::SetSwappedOut(bool is_swapped_out) {
340 // We should only toggle between states.
341 DCHECK(is_swapped_out_ != is_swapped_out);
342 is_swapped_out_ = is_swapped_out;
343
344 // If we are swapping out, we will call ReleaseProcess, allowing the process
345 // to exit if all of its RenderViews are swapped out. We wait until the
346 // WasSwappedOut call to do this, to avoid showing the sad tab.
347 // If we are swapping in, we call AddRefProcess to prevent the process from
348 // exiting.
349 if (!is_swapped_out)
350 RenderProcess::current()->AddRefProcess();
351}
352
[email protected]34bb3ac2013-03-08 02:41:28353bool RenderWidget::AllowPartialSwap() const {
354 return true;
355}
356
[email protected]c8cbae72013-05-23 10:45:03357bool RenderWidget::UsingSynchronousRendererCompositor() const {
[email protected]ccc1722e2013-05-06 19:43:07358#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07359 return SynchronousCompositorFactory::GetInstance() != NULL;
360#else
[email protected]ccc1722e2013-05-06 19:43:07361 return false;
[email protected]913d99a2013-05-31 07:16:07362#endif
[email protected]ccc1722e2013-05-06 19:43:07363}
364
[email protected]a95986a82010-12-24 06:19:28365bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
366 bool handled = true;
367 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
[email protected]c084330e02013-04-27 01:08:15368 IPC_MESSAGE_HANDLER(InputMsg_HandleInputEvent, OnHandleInputEvent)
[email protected]34202de2013-05-06 23:36:22369 IPC_MESSAGE_HANDLER(InputMsg_CursorVisibilityChange,
370 OnCursorVisibilityChange)
[email protected]c084330e02013-04-27 01:08:15371 IPC_MESSAGE_HANDLER(InputMsg_MouseCaptureLost, OnMouseCaptureLost)
372 IPC_MESSAGE_HANDLER(InputMsg_SetFocus, OnSetFocus)
[email protected]a95986a82010-12-24 06:19:28373 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
374 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
375 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54376 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28377 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41378 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15379 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28380 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59381 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
382 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28383 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
384 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
385 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25386 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
387 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
388 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted, OnSmoothScrollCompleted)
[email protected]a95986a82010-12-24 06:19:28389 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
390 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]80ad8622012-11-07 16:33:03391 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21392#if defined(OS_ANDROID)
393 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
[email protected]2384b6c2013-02-28 23:58:51394 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]0d1ebed12013-08-05 22:01:13395 IPC_MESSAGE_HANDLER(ViewMsg_ImeEventAck, OnImeEventAck)
[email protected]105dffb42013-02-20 03:46:21396#endif
[email protected]51a49502013-03-23 01:50:19397 IPC_MESSAGE_HANDLER(ViewMsg_Snapshot, OnSnapshot)
[email protected]3639aa82013-06-04 11:00:04398 IPC_MESSAGE_HANDLER(ViewMsg_SetBrowserRenderingStats,
399 OnSetBrowserRenderingStats)
[email protected]a95986a82010-12-24 06:19:28400 IPC_MESSAGE_UNHANDLED(handled = false)
401 IPC_END_MESSAGE_MAP()
402 return handled;
403}
initial.commit09911bf2008-07-26 23:55:29404
405bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15406 // Don't send any messages after the browser has told us to close, and filter
407 // most outgoing messages while swapped out.
408 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26409 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11410 closing_) {
initial.commit09911bf2008-07-26 23:55:29411 delete message;
412 return false;
413 }
414
415 // If given a messsage without a routing ID, then assign our routing ID.
416 if (message->routing_id() == MSG_ROUTING_NONE)
417 message->set_routing_id(routing_id_);
418
[email protected]380244092011-10-07 17:26:27419 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44420}
421
[email protected]61e2b3cc2012-03-02 16:13:34422void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44423 const gfx::Size& physical_backing_size,
[email protected]d9083762013-03-24 01:36:40424 float overdraw_bottom_height,
[email protected]61e2b3cc2012-03-02 16:13:34425 const gfx::Rect& resizer_rect,
426 bool is_fullscreen,
427 ResizeAck resize_ack) {
[email protected]1c0008842013-06-06 08:35:48428 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
429 !RenderThreadImpl::current()->layout_test_mode()) {
430 // A resize ack shouldn't be requested if we have not ACK'd the previous
431 // one.
432 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
433 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
434 }
initial.commit09911bf2008-07-26 23:55:29435
[email protected]61e2b3cc2012-03-02 16:13:34436 // Ignore this during shutdown.
437 if (!webwidget_)
438 return;
439
[email protected]d9083762013-03-24 01:36:40440 if (compositor_) {
[email protected]60d47ac2013-03-01 23:42:44441 compositor_->setViewportSize(new_size, physical_backing_size);
[email protected]d9083762013-03-24 01:36:40442 compositor_->SetOverdrawBottomHeight(overdraw_bottom_height);
443 }
[email protected]60d47ac2013-03-01 23:42:44444
[email protected]dade8992013-03-04 07:34:34445 physical_backing_size_ = physical_backing_size;
[email protected]d9083762013-03-24 01:36:40446 overdraw_bottom_height_ = overdraw_bottom_height;
[email protected]61e2b3cc2012-03-02 16:13:34447 resizer_rect_ = resizer_rect;
448
449 // NOTE: We may have entered fullscreen mode without changing our size.
450 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
451 if (fullscreen_change)
452 WillToggleFullscreen();
453 is_fullscreen_ = is_fullscreen;
454
455 if (size_ != new_size) {
456 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34457 needs_repainting_on_restore_ = false;
458
459 size_ = new_size;
460
461 paint_aggregator_.ClearPendingUpdate();
462
463 // When resizing, we want to wait to paint before ACK'ing the resize. This
464 // ensures that we only resize as fast as we can paint. We only need to
465 // send an ACK if we are resized to a non-empty rect.
466 webwidget_->resize(new_size);
[email protected]0b70dbe2013-05-10 19:06:32467
[email protected]92201e32013-06-08 06:14:52468 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
469 !RenderThreadImpl::current()->layout_test_mode()) {
470 // Resize should have caused an invalidation of the entire view.
471 DCHECK(new_size.IsEmpty() || is_accelerated_compositing_active_ ||
472 paint_aggregator_.HasPendingUpdate());
473 }
[email protected]1c0008842013-06-06 08:35:48474 } else if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
475 !RenderThreadImpl::current()->layout_test_mode()) {
[email protected]632c4382013-05-15 08:58:45476 resize_ack = NO_RESIZE_ACK;
477 }
478
479 if (new_size.IsEmpty() || physical_backing_size.IsEmpty()) {
480 // For empty size or empty physical_backing_size, there is no next paint
481 // (along with which to send the ack) until they are set to non-empty.
[email protected]ff475a322012-03-14 00:05:35482 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34483 }
484
[email protected]20fbfc22013-05-08 20:50:58485 // Send the Resize_ACK flag once we paint again if requested.
[email protected]632c4382013-05-15 08:58:45486 if (resize_ack == SEND_RESIZE_ACK)
[email protected]20fbfc22013-05-08 20:50:58487 set_next_paint_is_resize_ack();
488
[email protected]61e2b3cc2012-03-02 16:13:34489 if (fullscreen_change)
490 DidToggleFullscreen();
491
492 // If a resize ack is requested and it isn't set-up, then no more resizes will
493 // come in and in general things will go wrong.
[email protected]632c4382013-05-15 08:58:45494 DCHECK(resize_ack != SEND_RESIZE_ACK || next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29495}
496
497void RenderWidget::OnClose() {
498 if (closing_)
499 return;
500 closing_ = true;
501
502 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03503 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27504 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03505 SetHidden(false);
506 }
initial.commit09911bf2008-07-26 23:55:29507
initial.commit09911bf2008-07-26 23:55:29508 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25509 // now. Post a task that only gets invoked when there are no nested message
510 // loops.
[email protected]dd32b1272013-05-04 14:17:11511 base::MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29512 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25513
514 // Balances the AddRef taken when we called AddRoute.
515 Release();
initial.commit09911bf2008-07-26 23:55:29516}
517
[email protected]61e2b3cc2012-03-02 16:13:34518// Got a response from the browser after the renderer decided to create a new
519// view.
[email protected]fc4404d2012-11-07 19:53:30520void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34521 DCHECK(routing_id_ != MSG_ROUTING_NONE);
522
[email protected]fc4404d2012-11-07 19:53:30523 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34524}
525
[email protected]0fdd5012013-05-29 08:05:56526void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) {
527 screen_info_ = params.screen_info;
528 SetDeviceScaleFactor(screen_info_.deviceScaleFactor);
529 Resize(params.new_size, params.physical_backing_size,
530 params.overdraw_bottom_height, params.resizer_rect,
531 params.is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29532}
533
[email protected]b5913d72012-02-07 22:26:54534void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
535 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59536 gfx::Rect view_rect(size_);
537
[email protected]ce112fe2012-10-29 22:52:18538 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59539 if (!old_damage_rect.IsEmpty())
540 paint_aggregator_.InvalidateRect(old_damage_rect);
541
[email protected]ce112fe2012-10-29 22:52:18542 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59543 if (!new_damage_rect.IsEmpty())
544 paint_aggregator_.InvalidateRect(new_damage_rect);
545
[email protected]b5913d72012-02-07 22:26:54546 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59547
[email protected]b5913d72012-02-07 22:26:54548 if (webwidget_)
549 webwidget_->didChangeWindowResizerRect();
550 }
551}
552
initial.commit09911bf2008-07-26 23:55:29553void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31554 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29555 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03556 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29557}
558
[email protected]9e2e4632012-07-27 16:38:41559void RenderWidget::OnWasShown(bool needs_repainting) {
560 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29561 // During shutdown we can just ignore this message.
562 if (!webwidget_)
563 return;
564
565 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03566 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29567
568 if (!needs_repainting && !needs_repainting_on_restore_)
569 return;
570 needs_repainting_on_restore_ = false;
571
[email protected]d65adb12010-04-28 17:26:49572 // Tag the next paint as a restore ack, which is picked up by
573 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29574 set_next_paint_is_restore_ack();
575
576 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56577 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46578 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
579 } else {
580 scheduleComposite();
581 }
initial.commit09911bf2008-07-26 23:55:29582}
583
[email protected]992db4c2011-05-12 15:37:15584void RenderWidget::OnWasSwappedOut() {
585 // If we have been swapped out and no one else is using this process,
586 // it's safe to exit now. If we get swapped back in, we will call
587 // AddRefProcess in SetSwappedOut.
588 if (is_swapped_out_)
589 RenderProcess::current()->ReleaseProcess();
590}
591
[email protected]53d3f302009-12-21 04:42:05592void RenderWidget::OnRequestMoveAck() {
593 DCHECK(pending_window_rect_count_);
594 pending_window_rect_count_--;
595}
596
597void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58598 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21599 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05600 update_reply_pending_ = false;
601
[email protected]b4d08452010-10-05 17:34:35602 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
603 // have no current paint buffer.
604 if (current_paint_buf_) {
605 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
606 current_paint_buf_ = NULL;
607 }
608
[email protected]65225772011-05-12 21:10:24609 // If swapbuffers is still pending, then defer the update until the
610 // swapbuffers occurs.
611 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
612 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
613 return;
614 }
615
[email protected]29ed96a2012-02-04 18:12:16616 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18617 if (!is_accelerated_compositing_active_) {
618 DidFlushPaint();
619 }
[email protected]a2f6bc112009-06-27 16:27:25620
initial.commit09911bf2008-07-26 23:55:29621 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24622 DoDeferredUpdateAndSendInputAck();
623}
624
[email protected]d0be63772011-12-20 23:18:04625bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59626 // Contexts using the command buffer support asynchronous swapbuffers.
627 // See RenderWidget::CreateOutputSurface().
[email protected]cadac622013-06-11 16:46:36628 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get())
[email protected]ed7defa2013-03-12 21:29:59629 return false;
630
631 return true;
632}
633
634GURL RenderWidget::GetURLForGraphicsContext3D() {
635 return GURL();
[email protected]65225772011-05-12 21:10:24636}
637
[email protected]479b0172012-10-29 19:27:09638bool RenderWidget::ForceCompositingModeEnabled() {
639 return false;
640}
641
[email protected]ebc0e1df2013-08-01 02:46:22642scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface(bool fallback) {
[email protected]c3e32ed42013-05-02 05:07:13643 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1811b8912013-05-09 15:35:19644
645#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07646 if (SynchronousCompositorFactory* factory =
647 SynchronousCompositorFactory::GetInstance()) {
648 return factory->CreateOutputSurface(routing_id());
[email protected]a1811b8912013-05-09 15:35:19649 }
650#endif
651
[email protected]53b4cc12013-07-18 23:02:30652 uint32 output_surface_id = next_output_surface_id_++;
653
[email protected]ed7defa2013-03-12 21:29:59654 // Explicitly disable antialiasing for the compositor. As of the time of
655 // this writing, the only platform that supported antialiasing for the
656 // compositor was Mac OS X, because the on-screen OpenGL context creation
657 // code paths on Windows and Linux didn't yet have multisampling support.
658 // Mac OS X essentially always behaves as though it's rendering offscreen.
659 // Multisampling has a heavy cost especially on devices with relatively low
660 // fill rate like most notebooks, and the Mac implementation would need to
661 // be optimized to resolve directly into the IOSurface shared between the
662 // GPU and browser processes. For these reasons and to avoid platform
663 // disparities we explicitly disable antialiasing.
664 WebKit::WebGraphicsContext3D::Attributes attributes;
665 attributes.antialias = false;
666 attributes.shareResources = true;
667 attributes.noAutomaticFlushes = true;
[email protected]a6886502013-05-16 20:59:18668 attributes.depth = false;
669 attributes.stencil = false;
[email protected]bec084292013-05-21 21:31:44670 if (command_line.HasSwitch(cc::switches::kForceDirectLayerDrawing))
671 attributes.stencil = true;
[email protected]ebc0e1df2013-08-01 02:46:22672 WebGraphicsContext3DCommandBufferImpl* context = NULL;
673 if (!fallback)
674 context = CreateGraphicsContext3D(attributes);
675
676 if (!context) {
677 if (!command_line.HasSwitch(switches::kEnableSoftwareCompositing))
678 return scoped_ptr<cc::OutputSurface>();
679 return scoped_ptr<cc::OutputSurface>(
680 new CompositorOutputSurface(routing_id(),
681 output_surface_id,
682 NULL,
683 new CompositorSoftwareOutputDevice(),
684 true));
685 }
[email protected]ed7defa2013-03-12 21:29:59686
[email protected]0c04d1c2013-07-10 00:02:32687 if (command_line.HasSwitch(switches::kEnableDelegatedRenderer) &&
688 !command_line.HasSwitch(switches::kDisableDelegatedRenderer)) {
[email protected]36e5ff12013-06-11 12:19:29689 DCHECK(is_threaded_compositing_enabled_);
690 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30691 new DelegatedCompositorOutputSurface(routing_id(), output_surface_id,
692 context, NULL));
[email protected]36e5ff12013-06-11 12:19:29693 }
694 if (command_line.HasSwitch(cc::switches::kCompositeToMailbox)) {
695 DCHECK(is_threaded_compositing_enabled_);
696 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30697 new MailboxOutputSurface(routing_id(), output_surface_id,
698 context, NULL));
[email protected]36e5ff12013-06-11 12:19:29699 }
700 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30701 new CompositorOutputSurface(routing_id(), output_surface_id,
702 context, NULL, false));
[email protected]ba91a792013-02-06 09:48:28703}
704
[email protected]ed7defa2013-03-12 21:29:59705void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24706 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21707 while (!updates_pending_swap_.empty()) {
708 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
709 updates_pending_swap_.pop_front();
710 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
711 // compositing pass, hence doesn't require an UpdateRect message.
712 if (msg)
713 Send(msg);
714 }
[email protected]65225772011-05-12 21:10:24715 num_swapbuffers_complete_pending_ = 0;
716 using_asynchronous_swapbuffers_ = false;
717 // Schedule another frame so the compositor learns about it.
718 scheduleComposite();
719}
720
[email protected]ed7defa2013-03-12 21:29:59721void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08722 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21723
724 if (using_asynchronous_swapbuffers_) {
725 ViewHostMsg_UpdateRect* msg = NULL;
726 // pending_update_params_ can be NULL if the swap doesn't correspond to an
727 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
728 // message.
[email protected]59383c782013-04-17 16:43:27729 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:21730 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
731 pending_update_params_.reset();
732 }
733 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08734 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21735 }
[email protected]37a6f302011-07-11 23:43:08736}
737
[email protected]ed7defa2013-03-12 21:29:59738void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24739 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16740
[email protected]404939f2012-06-01 04:06:18741 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16742 DidFlushPaint();
743
[email protected]65225772011-05-12 21:10:24744 // When compositing deactivates, we reset the swapbuffers pending count. The
745 // swapbuffers acks may still arrive, however.
746 if (num_swapbuffers_complete_pending_ == 0) {
747 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
748 return;
749 }
[email protected]aa4117f2011-12-09 22:19:21750 DCHECK(!updates_pending_swap_.empty());
751 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
752 updates_pending_swap_.pop_front();
753 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
754 // compositing pass, hence doesn't require an UpdateRect message.
755 if (msg)
756 Send(msg);
[email protected]65225772011-05-12 21:10:24757 num_swapbuffers_complete_pending_--;
758
759 // If update reply is still pending, then defer the update until that reply
760 // occurs.
[email protected]d0be63772011-12-20 23:18:04761 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24762 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
763 return;
764 }
765
766 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06767 // when we were previously rendering. However, if an invalidation task is not
768 // posted, there may be software rendering work pending. In that case, don't
769 // early out.
770 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24771 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
772 return;
773 }
774
[email protected]cc66e682012-10-02 06:48:18775 // Do not call DoDeferredUpdate unless there's animation work to be done or
776 // a real invalidation. This prevents rendering in response to a swapbuffers
777 // callback coming back after we've navigated away from the page that
778 // generated it.
779 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
780 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
781 return;
782 }
783
[email protected]65225772011-05-12 21:10:24784 // Continue painting if necessary...
785 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29786}
787
[email protected]0dea1652012-12-14 00:09:09788void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
[email protected]4b157662013-05-29 04:05:05789 const ui::LatencyInfo& latency_info,
[email protected]0dea1652012-12-14 00:09:09790 bool is_keyboard_shortcut) {
[email protected]5dd768212009-08-13 23:34:49791 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09792 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49793 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29794 return;
[email protected]5dd768212009-08-13 23:34:49795 }
initial.commit09911bf2008-07-26 23:55:29796
[email protected]b4841e1c2013-05-16 22:30:10797 const char* const event_name = GetEventName(input_event->type);
798 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent",
799 "event", event_name);
800
[email protected]c2eaa8f2013-05-10 02:41:55801 if (compositor_)
802 compositor_->SetLatencyInfo(latency_info);
[email protected]256737c2013-06-08 04:39:10803 else
804 latency_info_.MergeWith(latency_info);
[email protected]c2eaa8f2013-05-10 02:41:55805
[email protected]6a4d7f62013-01-07 21:32:13806 base::TimeDelta now = base::TimeDelta::FromInternalValue(
807 base::TimeTicks::Now().ToInternalValue());
808
809 int64 delta = static_cast<int64>(
810 (now.InSecondsF() - input_event->timeStampSeconds) *
811 base::Time::kMicrosecondsPerSecond);
812 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
[email protected]de415552013-01-23 04:12:17813 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:48814 base::Histogram::FactoryGet(
[email protected]b4841e1c2013-05-16 22:30:10815 base::StringPrintf("Event.Latency.Renderer.%s", event_name),
[email protected]bafdc5d52013-02-27 18:18:48816 0,
817 1000000,
[email protected]6a4d7f62013-01-07 21:32:13818 100,
[email protected]de415552013-01-23 04:12:17819 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:48820 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:13821
[email protected]67bfb83f2011-09-22 03:36:37822 bool prevent_default = false;
823 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26824 const WebMouseEvent& mouse_event =
825 *static_cast<const WebMouseEvent*>(input_event);
826 TRACE_EVENT2("renderer", "HandleMouseMove",
827 "x", mouse_event.x, "y", mouse_event.y);
828 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37829 }
830
[email protected]f56c7872013-06-18 12:31:57831 if (WebInputEvent::isKeyboardEventType(input_event->type)) {
832 const WebKeyboardEvent& key_event =
833 *static_cast<const WebKeyboardEvent*>(input_event);
834 prevent_default = WillHandleKeyEvent(key_event);
835 }
836
[email protected]41d86852012-11-07 12:23:24837 if (WebInputEvent::isGestureEventType(input_event->type)) {
838 const WebGestureEvent& gesture_event =
839 *static_cast<const WebGestureEvent*>(input_event);
840 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
841 }
842
[email protected]3ebcc7c2013-01-09 05:34:46843 if (input_event->type == WebInputEvent::GestureTap ||
844 input_event->type == WebInputEvent::GestureLongPress)
845 resetInputMethod();
846
[email protected]67bfb83f2011-09-22 03:36:37847 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12848 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
849 suppress_next_char_events_ = false;
850 if (!processed && webwidget_)
851 processed = webwidget_->handleInputEvent(*input_event);
852 }
853
854 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
855 // it's not processed by webkit, then we need to suppress the upcoming Char
856 // events.
857 if (!processed && is_keyboard_shortcut)
858 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29859
[email protected]3d5c243b2012-11-30 00:26:01860 InputEventAckState ack_result = processed ?
861 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
862 if (!processed && input_event->type == WebInputEvent::TouchStart) {
863 const WebTouchEvent& touch_event =
864 *static_cast<const WebTouchEvent*>(input_event);
865 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
866 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
867 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
868 }
869
[email protected]a9fb30aa2011-10-06 06:58:46870 IPC::Message* response =
[email protected]c084330e02013-04-27 01:08:15871 new InputHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
872 ack_result);
[email protected]3391a0772012-03-28 00:32:07873 bool event_type_gets_rate_limited =
874 input_event->type == WebInputEvent::MouseMove ||
875 input_event->type == WebInputEvent::MouseWheel ||
876 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06877
878 bool frame_pending = paint_aggregator_.HasPendingUpdate();
879 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28880 frame_pending = compositor_ &&
881 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06882 }
883
[email protected]9a8ce7f92013-06-11 12:39:49884 if (event_type_gets_rate_limited && frame_pending && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24885 // We want to rate limit the input events in this case, so we'll wait for
886 // painting to finish before ACKing this message.
[email protected]59383c782013-04-17 16:43:27887 if (pending_input_event_ack_) {
[email protected]353a34c2010-05-28 23:35:17888 // As two different kinds of events could cause us to postpone an ack
889 // we send it now, if we have one pending. The Browser should never
890 // send us the same kind of event we are delaying the ack for.
891 Send(pending_input_event_ack_.release());
892 }
[email protected]12fbad812009-09-01 18:21:24893 pending_input_event_ack_.reset(response);
[email protected]df09e052013-07-31 18:59:50894 if (compositor_)
895 compositor_->NotifyInputThrottledUntilCommit();
[email protected]12fbad812009-09-01 18:21:24896 } else {
897 Send(response);
898 }
899
[email protected]3306f262012-09-21 19:20:42900#if defined(OS_ANDROID)
901 // Allow the IME to be shown when the focus changes as a consequence
902 // of a processed touch end event.
903 if (input_event->type == WebInputEvent::TouchEnd && processed)
[email protected]0d1ebed12013-08-05 22:01:13904 UpdateTextInputState(true, true);
[email protected]3306f262012-09-21 19:20:42905#endif
906
[email protected]5dd768212009-08-13 23:34:49907 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48908
[email protected]67bfb83f2011-09-22 03:36:37909 if (!prevent_default) {
910 if (WebInputEvent::isKeyboardEventType(input_event->type))
911 DidHandleKeyEvent();
912 if (WebInputEvent::isMouseEventType(input_event->type))
913 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24914 if (WebInputEvent::isTouchEventType(input_event->type))
915 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37916 }
initial.commit09911bf2008-07-26 23:55:29917}
918
[email protected]34202de2013-05-06 23:36:22919void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
920 if (webwidget_)
921 webwidget_->setCursorVisibilityState(is_visible);
922}
923
initial.commit09911bf2008-07-26 23:55:29924void RenderWidget::OnMouseCaptureLost() {
925 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28926 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29927}
928
929void RenderWidget::OnSetFocus(bool enable) {
930 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33931 if (webwidget_)
932 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29933}
934
935void RenderWidget::ClearFocus() {
936 // We may have got the focus from the browser before this gets processed, in
937 // which case we do not want to unfocus ourself.
938 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28939 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29940}
941
[email protected]2d5d09d52009-06-15 14:29:21942void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00943 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21944 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06945 TRACE_EVENT2("renderer", "PaintRect",
946 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45947
948 const bool kEnableGpuBenchmarking =
949 CommandLine::ForCurrentProcess()->HasSwitch(
950 switches::kEnableGpuBenchmarking);
[email protected]4fb66842009-12-04 21:41:00951 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21952
953 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00954 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
955 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03956
[email protected]699ab0d2009-04-23 23:19:14957 // If there is a custom background, tile it.
958 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14959 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11960 skia::RefPtr<SkShader> shader = skia::AdoptRef(
961 SkShader::CreateBitmapShader(background_,
962 SkShader::kRepeat_TileMode,
963 SkShader::kRepeat_TileMode));
964 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20965
966 // Use kSrc_Mode to handle background_ transparency properly.
967 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
968
969 // Canvas could contain multiple update rects. Clip to given rect so that
970 // we don't accidentally clear other update rects.
971 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44972 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48973 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14974 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20975 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14976 }
977
[email protected]719b36f2010-12-22 20:36:46978 // First see if this rect is a plugin that can paint itself faster.
979 TransportDIB* optimized_dib = NULL;
980 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20981 float dib_scale_factor;
[email protected]adab2332013-07-25 18:04:32982 PepperPluginInstanceImpl* optimized_instance =
[email protected]719b36f2010-12-22 20:36:46983 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
984 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20985 &optimized_copy_rect,
986 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46987 if (optimized_instance) {
[email protected]20790a222013-07-25 02:23:05988#if defined(ENABLE_PLUGINS)
[email protected]719b36f2010-12-22 20:36:46989 // This plugin can be optimize-painted and we can just ask it to paint
990 // itself. We don't actually need the TransportDIB in this case.
991 //
992 // This is an optimization for PPAPI plugins that know they're on top of
993 // the page content. If this rect is inside such a plugin, we can save some
994 // time and avoid re-rendering the page content which we know will be
995 // covered by the plugin later (this time can be significant, especially
996 // for a playing movie that is invalidating a lot).
997 //
998 // In the plugin movie case, hopefully the similar call to
999 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
1000 // painting, because that avoids copying the plugin image to a different
1001 // paint rect. Unfortunately, if anything on the page is animating other
1002 // than the movie, it break this optimization since the union of the
1003 // invalid regions will be larger than the plugin.
1004 //
1005 // This code optimizes that case, where we can still avoid painting in
1006 // WebKit and filling the background (which can be slow) and just painting
1007 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
1008 // required.
[email protected]63ab54262012-11-09 15:58:451009 base::TimeTicks paint_begin_ticks;
1010 if (kEnableGpuBenchmarking)
1011 paint_begin_ticks = base::TimeTicks::HighResNow();
1012
[email protected]df59dd42012-09-14 22:56:301013 SkAutoCanvasRestore auto_restore(canvas, true);
1014 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]6bd867b2013-07-24 22:10:201015 optimized_instance->Paint(canvas, optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:211016 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451017 if (kEnableGpuBenchmarking) {
1018 base::TimeDelta paint_time =
1019 base::TimeTicks::HighResNow() - paint_begin_ticks;
1020 if (!is_accelerated_compositing_active_)
[email protected]62049562013-03-24 00:39:011021 software_stats_.total_paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451022 }
[email protected]20790a222013-07-25 02:23:051023#endif
[email protected]719b36f2010-12-22 20:36:461024 } else {
1025 // Normal painting case.
[email protected]63ab54262012-11-09 15:58:451026 base::TimeTicks paint_begin_ticks;
1027 if (kEnableGpuBenchmarking)
1028 paint_begin_ticks = base::TimeTicks::HighResNow();
1029
[email protected]6bd867b2013-07-24 22:10:201030 webwidget_->paint(canvas, rect);
[email protected]63ab54262012-11-09 15:58:451031
1032 if (kEnableGpuBenchmarking) {
1033 base::TimeDelta paint_time =
1034 base::TimeTicks::HighResNow() - paint_begin_ticks;
1035 if (!is_accelerated_compositing_active_)
[email protected]62049562013-03-24 00:39:011036 software_stats_.total_paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451037 }
[email protected]719b36f2010-12-22 20:36:461038
1039 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:351040 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:461041 }
initial.commit09911bf2008-07-26 23:55:291042
[email protected]4fb66842009-12-04 21:41:001043 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:001044 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451045
1046 if (kEnableGpuBenchmarking) {
[email protected]63ab54262012-11-09 15:58:451047 int64 num_pixels_processed = rect.width() * rect.height();
[email protected]62049562013-03-24 00:39:011048 software_stats_.total_pixels_painted += num_pixels_processed;
[email protected]63ab54262012-11-09 15:58:451049 }
[email protected]4fb66842009-12-04 21:41:001050}
1051
1052void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
1053 skia::PlatformCanvas* canvas) {
1054 static bool kPaintBorder =
1055 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
1056 if (!kPaintBorder)
1057 return;
1058
[email protected]53d3f302009-12-21 04:42:051059 // Cycle through these colors to help distinguish new paint rects.
1060 const SkColor colors[] = {
1061 SkColorSetARGB(0x3F, 0xFF, 0, 0),
1062 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
1063 SkColorSetARGB(0x3F, 0, 0, 0xFF),
1064 };
1065 static int color_selector = 0;
1066
[email protected]4fb66842009-12-04 21:41:001067 SkPaint paint;
1068 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:051069 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:001070 paint.setStrokeWidth(1);
1071
1072 SkIRect irect;
1073 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
1074 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:291075}
1076
[email protected]52ccd0ea2011-02-16 01:09:051077void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:301078 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:301079 if (!animation_update_pending_) {
1080 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:591081 return;
[email protected]921244e42011-07-20 16:36:301082 }
[email protected]bd37ae252011-06-03 01:28:181083 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:591084 // Record when we fired (according to base::Time::Now()) relative to when
1085 // we posted the task to quantify how much the base::Time/base::TimeTicks
1086 // skew is affecting animations.
1087 base::TimeDelta animation_callback_delay = base::Time::Now() -
1088 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
1089 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
1090 animation_callback_delay,
1091 base::TimeDelta::FromMilliseconds(0),
1092 base::TimeDelta::FromMilliseconds(30),
1093 25);
1094 }
[email protected]65225772011-05-12 21:10:241095 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:241096}
1097
[email protected]52ccd0ea2011-02-16 01:09:051098void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:591099 if (!animation_update_pending_)
1100 return;
[email protected]bd37ae252011-06-03 01:28:181101
1102 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:331103 base::TimeDelta animationInterval = IsRenderingVSynced() ?
1104 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:181105
[email protected]7c4329e2011-02-18 22:02:591106 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:451107
1108 // animation_floor_time_ is the earliest time that we should animate when
1109 // using the dead reckoning software scheduler. If we're using swapbuffers
1110 // complete callbacks to rate limit, we can ignore this floor.
1111 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:301112 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:331113 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:181114 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:591115 // running animation callbacks so that if a callback requests another
1116 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:381117 animation_timer_.Stop();
1118 animation_timer_.Start(FROM_HERE, animationInterval, this,
1119 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:591120 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:281121 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:201122 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:061123 } else {
[email protected]635353c2013-03-06 09:11:201124 double frame_begin_time =
1125 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
1126 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061127 }
[email protected]7c4329e2011-02-18 22:02:591128 return;
[email protected]5f8b1022011-01-21 23:34:501129 }
[email protected]bd37ae252011-06-03 01:28:181130 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381131 if (!animation_timer_.IsRunning()) {
1132 // This code uses base::Time::Now() to calculate the floor and next fire
1133 // time because javascript's Date object uses base::Time::Now(). The
1134 // message loop uses base::TimeTicks, which on windows can have a
1135 // different granularity than base::Time.
1136 // The upshot of all this is that this function might be called before
1137 // base::Time::Now() has advanced past the animation_floor_time_. To
1138 // avoid exposing this delay to javascript, we keep posting delayed
1139 // tasks until base::Time::Now() has advanced far enough.
1140 base::TimeDelta delay = animation_floor_time_ - now;
1141 animation_timer_.Start(FROM_HERE, delay, this,
1142 &RenderWidget::AnimationCallback);
1143 }
[email protected]5f8b1022011-01-21 23:34:501144}
1145
[email protected]bd37ae252011-06-03 01:28:181146bool RenderWidget::IsRenderingVSynced() {
1147 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1148 // not caught by this check. This will lead to artificially low frame rates
1149 // for people who force vsync off at a driver level and expect Chrome to speed
1150 // up.
1151 return !has_disable_gpu_vsync_switch_;
1152}
1153
[email protected]65225772011-05-12 21:10:241154void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061155 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241156 invalidation_task_posted_ = false;
1157 DoDeferredUpdateAndSendInputAck();
1158}
1159
1160void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051161 DoDeferredUpdate();
1162
[email protected]59383c782013-04-17 16:43:271163 if (pending_input_event_ack_)
[email protected]52ccd0ea2011-02-16 01:09:051164 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211165}
1166
[email protected]552e6002009-11-19 05:24:571167void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581168 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]fc8bde1a2013-07-04 08:54:071169 TRACE_EVENT_SCOPED_SAMPLING_STATE("Chrome", "Paint");
[email protected]71e2f0a2011-03-15 22:25:081170
[email protected]65225772011-05-12 21:10:241171 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291172 return;
[email protected]05a980d7a2012-02-07 22:16:421173
[email protected]fc4404d2012-11-07 19:53:301174 if (!init_complete_) {
1175 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421176 return;
1177 }
[email protected]aa4117f2011-12-09 22:19:211178 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241179 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1180 return;
1181 }
[email protected]9ca84622011-06-02 23:46:391182 if (is_accelerated_compositing_active_ &&
1183 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241184 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1185 return;
1186 }
initial.commit09911bf2008-07-26 23:55:291187
[email protected]552e6002009-11-19 05:24:571188 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051189 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571190 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291191 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241192 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291193 return;
1194 }
1195
[email protected]0fb93f52011-05-18 23:13:561196 // Tracking of frame rate jitter
1197 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371198 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051199 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501200
[email protected]f98d7e3c2010-09-13 22:30:461201 // Layout may generate more invalidation. It may also enable the
1202 // GPU acceleration, so make sure to run layout before we send the
1203 // GpuRenderingActivated message.
1204 webwidget_->layout();
1205
[email protected]793b2d62013-06-11 00:43:251206 // Check for whether we need to track swap buffers. We need to do that after
1207 // layout() because it may have switched us to accelerated compositing.
1208 if (is_accelerated_compositing_active_)
1209 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1210
[email protected]dcca3aa92012-02-17 23:03:371211 // The following two can result in further layout and possibly
1212 // enable GPU acceleration so they need to be called before any painting
1213 // is done.
[email protected]cb9e2632013-06-18 11:26:471214 UpdateTextInputType();
[email protected]dcca3aa92012-02-17 23:03:371215 UpdateSelectionBounds();
1216
[email protected]5f8b1022011-01-21 23:34:501217 // Suppress painting if nothing is dirty. This has to be done after updating
1218 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241219 if (!paint_aggregator_.HasPendingUpdate()) {
1220 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371221 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501222 return;
[email protected]65225772011-05-12 21:10:241223 }
[email protected]5f8b1022011-01-21 23:34:501224
[email protected]479b0172012-10-29 19:27:091225 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361226 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091227 ForceCompositingModeEnabled()) {
1228 webwidget_->enterForceCompositingMode(true);
1229 }
1230
[email protected]872ae5b2011-05-26 20:20:501231 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561232 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041233 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561234 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1235 delay,
1236 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411237 base::TimeDelta::FromMilliseconds(120),
1238 60);
[email protected]d0be63772011-12-20 23:18:041239 } else {
[email protected]0fb93f52011-05-18 23:13:561240 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1241 delay,
1242 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411243 base::TimeDelta::FromMilliseconds(120),
1244 60);
[email protected]d0be63772011-12-20 23:18:041245 }
[email protected]872ae5b2011-05-26 20:20:501246
1247 // Calculate filtered time per frame:
1248 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1249 filtered_time_per_frame_ =
1250 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561251 }
1252 last_do_deferred_update_time_ = frame_begin_ticks;
1253
[email protected]fef5e3972012-08-07 03:59:471254 if (!is_accelerated_compositing_active_) {
[email protected]62049562013-03-24 00:39:011255 software_stats_.animation_frame_count++;
1256 software_stats_.screen_frame_count++;
[email protected]fef5e3972012-08-07 03:59:471257 }
1258
[email protected]552e6002009-11-19 05:24:571259 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291260 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301261 PaintAggregator::PendingUpdate update;
1262 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291263
[email protected]53d3f302009-12-21 04:42:051264 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181265 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291266
[email protected]29ed96a2012-02-04 18:12:161267 // Notify derived classes that we're about to initiate a paint.
1268 WillInitiatePaint();
1269
[email protected]ca4847f2010-09-24 05:39:151270 // A plugin may be able to do an optimized paint. First check this, in which
1271 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461272 // This optimization allows PPAPI plugins that declare themselves on top of
1273 // the page (like a traditional windowed plugin) to be able to animate (think
1274 // movie playing) without repeatedly re-painting the page underneath, or
1275 // copying the plugin backing store (since we can send the plugin's backing
1276 // store directly to the browser).
1277 //
1278 // This optimization only works when the entire invalid region is contained
1279 // within the plugin. There is a related optimization in PaintRect for the
1280 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151281 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151282 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201283 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211284 DCHECK(!pending_update_params_.get());
1285 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551286 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211287 pending_update_params_->scroll_rect = update.scroll_rect;
1288 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211289 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1290 pending_update_params_->flags = next_paint_flags_;
1291 pending_update_params_->scroll_offset = GetScrollOffset();
1292 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091293 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211294 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091295 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211296
[email protected]256737c2013-06-08 04:39:101297 if (!is_accelerated_compositing_active_)
1298 pending_update_params_->latency_info = latency_info_;
1299
1300 latency_info_.Clear();
1301
[email protected]ca4847f2010-09-24 05:39:151302 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561303 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151304 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201305 &optimized_copy_rect,
1306 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271307 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471308 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211309 pending_update_params_->bitmap = dib->id();
1310 pending_update_params_->bitmap_rect = optimized_copy_location;
1311 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201312 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561313 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461314 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101315
1316 bool fractional_scale = device_scale_factor_ -
1317 static_cast<int>(device_scale_factor_) != 0;
1318 if (fractional_scale) {
1319 // Damage might not be DIP aligned. Inflate damage to compensate.
1320 bounds.Inset(-1, -1);
1321 bounds.Intersect(gfx::Rect(size_));
1322 }
1323
1324 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181325 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101326
[email protected]ca4847f2010-09-24 05:39:151327 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351328 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591329 pixel_bounds));
[email protected]59383c782013-04-17 16:43:271330 if (!canvas) {
[email protected]f98d7e3c2010-09-13 22:30:461331 NOTREACHED();
1332 return;
1333 }
[email protected]cef3362f2009-12-21 17:48:451334
[email protected]f98d7e3c2010-09-13 22:30:461335 // We may get back a smaller canvas than we asked for.
1336 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591337 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1338 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1339 pixel_bounds.set_width(canvas->getDevice()->width());
1340 pixel_bounds.set_height(canvas->getDevice()->height());
1341 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1342 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051343
[email protected]f98d7e3c2010-09-13 22:30:461344 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1345
[email protected]aa4117f2011-12-09 22:19:211346 pending_update_params_->bitmap = current_paint_buf_->id();
1347 pending_update_params_->bitmap_rect = bounds;
1348
1349 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461350 // The scroll damage is just another rectangle to paint and copy.
1351 copy_rects.swap(update.paint_rects);
1352 if (!scroll_damage.IsEmpty())
1353 copy_rects.push_back(scroll_damage);
1354
[email protected]4889bd212013-02-11 22:23:101355 for (size_t i = 0; i < copy_rects.size(); ++i) {
1356 gfx::Rect rect = copy_rects[i];
1357 if (fractional_scale) {
1358 // Damage might not be DPI aligned. Inflate rect to compensate.
1359 rect.Inset(-1, -1);
1360 }
1361 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1362 }
[email protected]60a50072012-01-11 02:05:351363
1364 // Software FPS tick for performance tests. The accelerated path traces the
1365 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1366 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421367 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW",
1368 TRACE_EVENT_SCOPE_THREAD);
[email protected]f98d7e3c2010-09-13 22:30:461369 } else { // Accelerated compositing path
1370 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211371 // If painting is done via the gpu process then we don't set any damage
1372 // rects to save the browser process from doing unecessary work.
1373 pending_update_params_->bitmap_rect = bounds;
1374 pending_update_params_->scroll_rect = gfx::Rect();
1375 // We don't need an ack, because we're not sharing a DIB with the browser.
1376 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1377 // with the browser for the GPU surface.
1378 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521379 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461380 }
1381
[email protected]936c6f52011-12-13 01:35:261382 // If we're holding a pending input event ACK, send the ACK before sending the
1383 // UpdateReply message so we can receive another input event before the
1384 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1385 // the UpdateRect IPC message handler.
[email protected]59383c782013-04-17 16:43:271386 if (pending_input_event_ack_)
[email protected]936c6f52011-12-13 01:35:261387 Send(pending_input_event_ack_.release());
1388
[email protected]ab543072013-01-25 04:38:151389 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211390 // OnSwapBuffersPosted), meaning a message has been added to the
1391 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1392 // the message now.
[email protected]59383c782013-04-17 16:43:271393 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:211394 // sending an ack to browser process that the paint is complete...
1395 update_reply_pending_ = pending_update_params_->needs_ack;
1396 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1397 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341398 }
[email protected]53d3f302009-12-21 04:42:051399
[email protected]29ed96a2012-02-04 18:12:161400 // If we're software rendering then we're done initiating the paint.
1401 if (!is_accelerated_compositing_active_)
1402 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291403}
1404
[email protected]f0c2a242013-03-15 19:34:521405void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151406 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281407 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521408 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151409}
1410
initial.commit09911bf2008-07-26 23:55:291411///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461412// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291413
[email protected]4873c7d2009-07-16 06:36:281414void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571415 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481416 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181417 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571418 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291419 return;
1420
[email protected]552e6002009-11-19 05:24:571421 paint_aggregator_.InvalidateRect(damaged_rect);
1422
1423 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241424 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571425 return;
1426 if (!paint_aggregator_.HasPendingUpdate())
1427 return;
[email protected]aa4117f2011-12-09 22:19:211428 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241429 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1430 return;
1431
1432 // When GPU rendering, combine pending animations and invalidations into
1433 // a single update.
[email protected]816edc62012-03-17 01:27:221434 if (is_accelerated_compositing_active_ &&
1435 animation_update_pending_ &&
1436 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571437 return;
1438
1439 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291440 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1441 // on the call stack.
1442 // 2) Allows us to collect more damage rects before painting to help coalesce
1443 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241444 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111445 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211446 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291447}
1448
[email protected]990278ff2012-11-13 02:12:551449void RenderWidget::didScrollRect(int dx, int dy,
1450 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461451 // Drop scrolls on the floor when we are in compositing mode.
1452 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561453 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461454 return;
1455
[email protected]552e6002009-11-19 05:24:571456 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481457 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181458 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571459 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291460 return;
1461
[email protected]990278ff2012-11-13 02:12:551462 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571463
1464 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241465 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571466 return;
1467 if (!paint_aggregator_.HasPendingUpdate())
1468 return;
[email protected]aa4117f2011-12-09 22:19:211469 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241470 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1471 return;
1472
1473 // When GPU rendering, combine pending animations and invalidations into
1474 // a single update.
[email protected]816edc62012-03-17 01:27:221475 if (is_accelerated_compositing_active_ &&
1476 animation_update_pending_ &&
1477 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571478 return;
1479
1480 // Perform updating asynchronously. This serves two purposes:
1481 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1482 // on the call stack.
1483 // 2) Allows us to collect more damage rects before painting to help coalesce
1484 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241485 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111486 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211487 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291488}
1489
[email protected]244ac1892011-12-02 17:04:471490void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091491 if (size_.width() != new_size.width || size_.height() != new_size.height) {
[email protected]eac2b362013-05-22 07:01:451492 size_ = new_size;
[email protected]20fbfc22013-05-08 20:50:581493
[email protected]eac2b362013-05-22 07:01:451494 // If we don't clear PaintAggregator after changing autoResize state, then
1495 // we might end up in a situation where bitmap_rect is larger than the
1496 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1497 // with invalid damage rects.
1498 paint_aggregator_.ClearPendingUpdate();
1499
[email protected]70dee7e2013-05-29 18:28:301500 if (RenderThreadImpl::current()->layout_test_mode()) {
[email protected]eac2b362013-05-22 07:01:451501 WebRect new_pos(rootWindowRect().x,
1502 rootWindowRect().y,
1503 new_size.width,
1504 new_size.height);
1505 view_screen_rect_ = new_pos;
1506 window_screen_rect_ = new_pos;
[email protected]8be1c582013-03-06 00:55:031507 }
[email protected]20fbfc22013-05-08 20:50:581508
[email protected]eac2b362013-05-22 07:01:451509 AutoResizeCompositor();
[email protected]20fbfc22013-05-08 20:50:581510
[email protected]70dee7e2013-05-29 18:28:301511 if (!RenderThreadImpl::current()->layout_test_mode())
[email protected]20fbfc22013-05-08 20:50:581512 need_update_rect_for_auto_resize_ = true;
[email protected]ea3ee0a2012-05-15 03:43:091513 }
[email protected]244ac1892011-12-02 17:04:471514}
1515
[email protected]3a1c8a8032013-03-18 22:35:321516void RenderWidget::AutoResizeCompositor() {
[email protected]97e1bf72013-03-06 14:06:051517 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1518 device_scale_factor_));
1519 if (compositor_)
1520 compositor_->setViewportSize(size_, physical_backing_size_);
1521}
1522
[email protected]91acd1c2012-03-14 08:32:391523void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221524 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1525
[email protected]c63b4d42012-04-26 01:01:071526#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211527 if (!is_accelerated_compositing_active_) {
1528 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1529 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1530 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1531 // going to switch to accelerated compositing, the GPU process may need
1532 // round-trips to the browser's UI thread before finishing the frame,
1533 // causing deadlocks if we delay the UpdateRect until we receive the
1534 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071535 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1536 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211537 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1538 }
[email protected]c63b4d42012-04-26 01:01:071539#endif
[email protected]aa4117f2011-12-09 22:19:211540
[email protected]ea162f92011-10-04 23:08:221541 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421542 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241543 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221544}
1545
1546void RenderWidget::didDeactivateCompositor() {
1547 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1548
1549 is_accelerated_compositing_active_ = false;
1550 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1551 routing_id_, is_accelerated_compositing_active_));
1552
[email protected]ea162f92011-10-04 23:08:221553 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241554 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091555
1556 // In single-threaded mode, we exit force compositing mode and re-enter in
1557 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1558 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1559 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361560 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091561 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561562}
1563
[email protected]e195e582013-03-08 01:32:591564void RenderWidget::initializeLayerTreeView() {
[email protected]c5fa4c42013-07-20 05:47:371565 compositor_ = RenderWidgetCompositor::Create(
1566 this, is_threaded_compositing_enabled_);
[email protected]e195e582013-03-08 01:32:591567 if (!compositor_)
1568 return;
1569
1570 compositor_->setViewportSize(size_, physical_backing_size_);
1571 if (init_complete_)
1572 compositor_->setSurfaceReady();
1573}
1574
[email protected]8926c602013-01-23 05:32:061575WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281576 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061577}
1578
[email protected]9ed83fe2013-02-27 01:52:281579void RenderWidget::suppressCompositorScheduling(bool enable) {
1580 if (compositor_)
1581 compositor_->SetSuppressScheduleComposite(enable);
1582}
1583
[email protected]9cd43a62012-03-26 08:03:561584void RenderWidget::willBeginCompositorFrame() {
1585 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371586
[email protected]cadac622013-06-11 16:46:361587 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy().get());
[email protected]abe8b3a2012-03-28 21:19:371588
1589 // The following two can result in further layout and possibly
1590 // enable GPU acceleration so they need to be called before any painting
1591 // is done.
[email protected]cb9e2632013-06-18 11:26:471592 UpdateTextInputType();
1593#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:131594 UpdateTextInputState(false, true);
[email protected]cb9e2632013-06-18 11:26:471595#endif
[email protected]abe8b3a2012-03-28 21:19:371596 UpdateSelectionBounds();
1597
[email protected]9cd43a62012-03-26 08:03:561598 WillInitiatePaint();
1599}
1600
[email protected]3391a0772012-03-28 00:32:071601void RenderWidget::didBecomeReadyForAdditionalInput() {
1602 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
[email protected]59383c782013-04-17 16:43:271603 if (pending_input_event_ack_)
[email protected]3391a0772012-03-28 00:32:071604 Send(pending_input_event_ack_.release());
1605}
1606
[email protected]6fceb912013-02-15 06:24:151607void RenderWidget::DidCommitCompositorFrame() {
1608}
1609
[email protected]58264a32011-11-17 23:36:151610void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501611 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351612 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1613 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421614 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU",
1615 TRACE_EVENT_SCOPE_THREAD);
[email protected]29ed96a2012-02-04 18:12:161616 // Notify subclasses that we initiated the paint operation.
1617 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151618}
1619
1620void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181621 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1622
1623 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561624 DidFlushPaint();
1625
[email protected]aa4117f2011-12-09 22:19:211626 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151627 return;
1628
[email protected]ea3ee0a2012-05-15 03:43:091629 if (!next_paint_flags_ &&
1630 !need_update_rect_for_auto_resize_ &&
1631 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151632 return;
[email protected]ea3ee0a2012-05-15 03:43:091633 }
[email protected]58264a32011-11-17 23:36:151634
1635 ViewHostMsg_UpdateRect_Params params;
1636 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151637 params.plugin_window_moves.swap(plugin_window_moves_);
1638 params.flags = next_paint_flags_;
1639 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121640 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091641 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151642
1643 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1644 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091645 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151646}
1647
[email protected]f98d7e3c2010-09-13 22:30:461648void RenderWidget::scheduleComposite() {
[email protected]cadac622013-06-11 16:46:361649 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get() &&
[email protected]ba91a792013-02-06 09:48:281650 compositor_) {
1651 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041652 } else {
[email protected]c3d45532011-10-07 19:20:401653 // TODO(nduca): replace with something a little less hacky. The reason this
1654 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1655 // contains a lot of host-renderer synchronization logic that is still
1656 // important for the accelerated compositing case. The option of simply
1657 // duplicating all that code is less desirable than "faking out" the
1658 // invalidation path using a magical damage rect.
1659 didInvalidateRect(WebRect(0, 0, 1, 1));
1660 }
[email protected]f98d7e3c2010-09-13 22:30:461661}
1662
[email protected]5f8b1022011-01-21 23:34:501663void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201664 if (animation_update_pending_)
1665 return;
1666
[email protected]921244e42011-07-20 16:36:301667 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201668 animation_update_pending_ = true;
1669 if (!animation_timer_.IsRunning()) {
1670 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1671 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211672 }
[email protected]5f8b1022011-01-21 23:34:501673}
1674
[email protected]4873c7d2009-07-16 06:36:281675void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301676 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521677 WebCursor cursor;
[email protected]953bd0062013-08-01 00:58:401678 InitializeCursorFromWebKitCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291679 // Only send a SetCursor message if we need to make a change.
1680 if (!current_cursor_.IsEqual(cursor)) {
1681 current_cursor_ = cursor;
1682 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1683 }
1684}
1685
1686// We are supposed to get a single call to Show for a newly created RenderWidget
1687// that was created via RenderWidget::CreateWebView. So, we wait until this
1688// point to dispatch the ShowWidget message.
1689//
1690// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281691// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291692//
[email protected]4873c7d2009-07-16 06:36:281693void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291694 DCHECK(!did_show_) << "received extraneous Show call";
1695 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1696 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1697
[email protected]8de12d942010-11-17 20:42:441698 if (did_show_)
1699 return;
1700
1701 did_show_ = true;
1702 // NOTE: initial_pos_ may still have its default values at this point, but
1703 // that's okay. It'll be ignored if as_popup is false, or the browser
1704 // process will impose a default position otherwise.
1705 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1706 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291707}
1708
[email protected]9b003482013-05-21 14:00:171709void RenderWidget::didProgrammaticallyScroll(
1710 const WebKit::WebPoint& scroll_point) {
1711 if (!compositor_)
1712 return;
1713 Send(new ViewHostMsg_DidProgrammaticallyScroll(
1714 routing_id_, gfx::Vector2d(scroll_point.x, scroll_point.y)));
1715}
1716
[email protected]4873c7d2009-07-16 06:36:281717void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291718}
1719
[email protected]4873c7d2009-07-16 06:36:281720void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291721}
1722
[email protected]2533ce12009-05-09 00:02:241723void RenderWidget::DoDeferredClose() {
1724 Send(new ViewHostMsg_Close(routing_id_));
1725}
1726
[email protected]4873c7d2009-07-16 06:36:281727void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321728 if (is_swapped_out_) {
1729 // This widget is currently swapped out, and the active widget is in a
1730 // different process. Have the browser route the close request to the
1731 // active widget instead, so that the correct unload handlers are run.
1732 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1733 return;
1734 }
1735
initial.commit09911bf2008-07-26 23:55:291736 // If a page calls window.close() twice, we'll end up here twice, but that's
1737 // OK. It is safe to send multiple Close messages.
1738
[email protected]2533ce12009-05-09 00:02:241739 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1740 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1741 // could be closed before the JS finishes executing. So instead, post a
1742 // message back to the message loop, which won't run until the JS is
1743 // complete, and then the Close message can be sent.
[email protected]dd32b1272013-05-04 14:17:111744 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211745 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291746}
1747
1748void RenderWidget::Close() {
1749 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061750 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281751 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281752 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291753 webwidget_ = NULL;
1754 }
1755}
1756
[email protected]4873c7d2009-07-16 06:36:281757WebRect RenderWidget::windowRect() {
1758 if (pending_window_rect_count_)
1759 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241760
[email protected]80ad8622012-11-07 16:33:031761 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291762}
1763
[email protected]8a9d6ca32011-06-06 20:11:301764void RenderWidget::setToolTipText(const WebKit::WebString& text,
1765 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541766 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301767}
1768
[email protected]4873c7d2009-07-16 06:36:281769void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291770 if (did_show_) {
[email protected]70dee7e2013-05-29 18:28:301771 if (!RenderThreadImpl::current()->layout_test_mode()) {
[email protected]8be1c582013-03-06 00:55:031772 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
1773 SetPendingWindowRect(pos);
1774 } else {
1775 WebSize new_size(pos.width, pos.height);
[email protected]d9083762013-03-24 01:36:401776 Resize(new_size, new_size, overdraw_bottom_height_,
1777 WebRect(), is_fullscreen_, NO_RESIZE_ACK);
[email protected]8be1c582013-03-06 00:55:031778 view_screen_rect_ = pos;
1779 window_screen_rect_ = pos;
1780 }
initial.commit09911bf2008-07-26 23:55:291781 } else {
1782 initial_pos_ = pos;
1783 }
1784}
1785
[email protected]2533ce12009-05-09 00:02:241786void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1787 pending_window_rect_ = rect;
1788 pending_window_rect_count_++;
1789}
1790
[email protected]4873c7d2009-07-16 06:36:281791WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241792 if (pending_window_rect_count_) {
1793 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1794 // the RootWindowRect is probably going to return wrong results since the
1795 // browser may not have processed the Move yet. There isn't really anything
1796 // good to do in this case, and it shouldn't happen - since this size is
1797 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281798 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241799 }
1800
[email protected]80ad8622012-11-07 16:33:031801 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371802}
1803
[email protected]4873c7d2009-07-16 06:36:281804WebRect RenderWidget::windowResizerRect() {
1805 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191806}
1807
[email protected]fa7b1dc2010-06-23 17:53:041808void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031809 // To prevent this renderer process from sending unnecessary IPC messages to
1810 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041811 // only during the input method attached to the browser process is active.
1812 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291813}
1814
[email protected]fa7b1dc2010-06-23 17:53:041815void RenderWidget::OnImeSetComposition(
1816 const string16& text,
1817 const std::vector<WebCompositionUnderline>& underlines,
1818 int selection_start, int selection_end) {
[email protected]0d1ebed12013-08-05 22:01:131819 if (!ShouldHandleImeEvent())
[email protected]4873c7d2009-07-16 06:36:281820 return;
[email protected]66fca5bc2013-05-23 06:58:291821 ImeEventGuard guard(this);
[email protected]88dbe32f2013-06-20 23:31:361822 if (!webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041823 text, WebVector<WebCompositionUnderline>(underlines),
1824 selection_start, selection_end)) {
1825 // If we failed to set the composition text, then we need to let the browser
1826 // process to cancel the input method's ongoing composition session, to make
1827 // sure we are in a consistent state.
1828 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:261829 }
[email protected]501ea13d2013-07-09 17:03:291830#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361831 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291832#endif
[email protected]fa7b1dc2010-06-23 17:53:041833}
1834
[email protected]0e45bd02013-07-12 20:20:021835void RenderWidget::OnImeConfirmComposition(const string16& text,
1836 const ui::Range& replacement_range,
1837 bool keep_selection) {
[email protected]0d1ebed12013-08-05 22:01:131838 if (!ShouldHandleImeEvent())
[email protected]d0be63772011-12-20 23:18:041839 return;
[email protected]66fca5bc2013-05-23 06:58:291840 ImeEventGuard guard(this);
[email protected]d0be63772011-12-20 23:18:041841 handling_input_event_ = true;
[email protected]0e45bd02013-07-12 20:20:021842 if (text.length())
1843 webwidget_->confirmComposition(text);
1844 else if (keep_selection)
1845 webwidget_->confirmComposition(WebWidget::KeepSelection);
1846 else
1847 webwidget_->confirmComposition(WebWidget::DoNotKeepSelection);
[email protected]d0be63772011-12-20 23:18:041848 handling_input_event_ = false;
[email protected]501ea13d2013-07-09 17:03:291849#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361850 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291851#endif
initial.commit09911bf2008-07-26 23:55:291852}
1853
[email protected]948f7ab72010-05-28 23:48:081854// This message causes the renderer to render an image of the
1855// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251856void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1857 int tag,
1858 const gfx::Size& page_size,
1859 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001860 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1861 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251862 // Close our unused handle.
1863#if defined(OS_WIN)
1864 ::CloseHandle(dib_handle);
1865#elif defined(OS_MACOSX)
1866 base::SharedMemory::CloseHandle(dib_handle);
1867#endif
1868 }
[email protected]d65adb12010-04-28 17:26:491869 return;
[email protected]45c6aad32010-11-11 04:46:251870 }
[email protected]d65adb12010-04-28 17:26:491871
[email protected]948f7ab72010-05-28 23:48:081872 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491873 // If one of these is empty, then we just return the dib we were
1874 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091875 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491876 return;
1877 }
1878
1879 // Map the given DIB ID into this process, and unmap it at the end
1880 // of this function.
[email protected]45c6aad32010-11-11 04:46:251881 scoped_ptr<TransportDIB> paint_at_size_buffer(
1882 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301883
[email protected]4b01b962012-10-09 23:17:351884 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281885 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351886 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281887 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511888 gfx::Size canvas_size = page_size_in_pixel;
1889 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491890 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511891 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491892 static_cast<float>(canvas_size.height());
1893
[email protected]ee8d6fd2010-05-26 17:05:481894 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491895 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1896 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481897 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491898
[email protected]36808ad2010-10-20 19:18:301899 scoped_ptr<skia::PlatformCanvas> canvas(
1900 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1901 canvas_size.height()));
[email protected]59383c782013-04-17 16:43:271902 if (!canvas) {
[email protected]36808ad2010-10-20 19:18:301903 NOTREACHED();
1904 return;
1905 }
1906
[email protected]d65adb12010-04-28 17:26:491907 // Reset bounds to what we actually received, but they should be the
1908 // same.
1909 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1910 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1911 bounds.set_width(canvas->getDevice()->width());
1912 bounds.set_height(canvas->getDevice()->height());
1913
1914 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081915 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491916 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1917
[email protected]948f7ab72010-05-28 23:48:081918 // Have to make sure we're laid out at the right size before
1919 // rendering.
1920 gfx::Size old_size = webwidget_->size();
1921 webwidget_->resize(page_size);
1922 webwidget_->layout();
1923
[email protected]d65adb12010-04-28 17:26:491924 // Paint the entire thing (using original bounds, not scaled bounds).
1925 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1926 canvas->restore();
1927
[email protected]948f7ab72010-05-28 23:48:081928 // Return the widget to its previous size.
1929 webwidget_->resize(old_size);
1930
[email protected]c88c9442010-07-19 18:55:091931 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491932}
1933
[email protected]51a49502013-03-23 01:50:191934void RenderWidget::OnSnapshot(const gfx::Rect& src_subrect) {
1935 SkBitmap snapshot;
1936
1937 if (OnSnapshotHelper(src_subrect, &snapshot)) {
1938 Send(new ViewHostMsg_Snapshot(routing_id(), true, snapshot));
1939 } else {
1940 Send(new ViewHostMsg_Snapshot(routing_id(), false, SkBitmap()));
1941 }
1942}
1943
1944bool RenderWidget::OnSnapshotHelper(const gfx::Rect& src_subrect,
1945 SkBitmap* snapshot) {
1946 base::TimeTicks beginning_time = base::TimeTicks::Now();
1947
1948 if (!webwidget_ || src_subrect.IsEmpty())
1949 return false;
1950
1951 gfx::Rect viewport_size = gfx::IntersectRects(
1952 src_subrect, gfx::Rect(physical_backing_size_));
1953
1954 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
1955 skia::CreatePlatformCanvas(viewport_size.width(),
1956 viewport_size.height(),
1957 true,
1958 NULL,
1959 skia::RETURN_NULL_ON_FAILURE));
[email protected]59383c782013-04-17 16:43:271960 if (!canvas)
[email protected]51a49502013-03-23 01:50:191961 return false;
1962
1963 canvas->save();
1964 webwidget_->layout();
1965
1966 PaintRect(viewport_size, viewport_size.origin(), canvas.get());
1967 canvas->restore();
1968
1969 const SkBitmap& bitmap = skia::GetTopDevice(*canvas)->accessBitmap(false);
1970 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
1971 return false;
1972
1973 UMA_HISTOGRAM_TIMES("Renderer4.Snapshot",
1974 base::TimeTicks::Now() - beginning_time);
1975 return true;
1976}
1977
[email protected]0bc1f572013-04-17 01:46:311978void RenderWidget::OnRepaint(gfx::Size size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:121979 // During shutdown we can just ignore this message.
1980 if (!webwidget_)
1981 return;
1982
[email protected]0bc1f572013-04-17 01:46:311983 // Even if the browser provides an empty damage rect, it's still expecting to
1984 // receive a repaint ack so just damage the entire widget bounds.
1985 if (size_to_paint.IsEmpty()) {
1986 size_to_paint = size_;
1987 }
1988
[email protected]ec7dc112008-08-06 05:30:121989 set_next_paint_is_repaint_ack();
[email protected]0bc1f572013-04-17 01:46:311990 if (is_accelerated_compositing_active_ && compositor_) {
1991 compositor_->SetNeedsRedrawRect(gfx::Rect(size_to_paint));
[email protected]f98d7e3c2010-09-13 22:30:461992 } else {
1993 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1994 didInvalidateRect(repaint_rect);
1995 }
[email protected]ec7dc112008-08-06 05:30:121996}
1997
[email protected]4a9dba42013-04-29 18:24:221998void RenderWidget::OnSmoothScrollCompleted() {
1999 pending_smooth_scroll_gesture_.Run();
[email protected]0e241b4b2012-08-18 09:06:272000}
2001
[email protected]4873c7d2009-07-16 06:36:282002void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:112003 if (!webwidget_)
2004 return;
[email protected]4873c7d2009-07-16 06:36:282005 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112006}
2007
[email protected]80ad8622012-11-07 16:33:032008void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
2009 const gfx::Rect& window_screen_rect) {
2010 view_screen_rect_ = view_screen_rect;
2011 window_screen_rect_ = window_screen_rect;
2012 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
2013}
2014
[email protected]105dffb42013-02-20 03:46:212015#if defined(OS_ANDROID)
2016void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
2017 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
2018}
[email protected]2384b6c2013-02-28 23:58:512019
2020void RenderWidget::OnShowImeIfNeeded() {
[email protected]0d1ebed12013-08-05 22:01:132021 UpdateTextInputState(true, true);
2022}
2023
2024void RenderWidget::IncrementOutstandingImeEventAcks() {
2025 ++outstanding_ime_acks_;
2026}
2027
2028void RenderWidget::OnImeEventAck() {
2029 --outstanding_ime_acks_;
2030 DCHECK(outstanding_ime_acks_ >= 0);
[email protected]2384b6c2013-02-28 23:58:512031}
[email protected]105dffb42013-02-20 03:46:212032#endif
2033
[email protected]0d1ebed12013-08-05 22:01:132034bool RenderWidget::ShouldHandleImeEvent() {
2035#if defined(OS_ANDROID)
2036 return !!webwidget_ && outstanding_ime_acks_ == 0;
2037#else
2038 return !!webwidget_;
2039#endif
2040}
2041
[email protected]468ac582012-11-20 00:53:192042void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
2043 if (device_scale_factor_ == device_scale_factor)
2044 return;
2045
2046 device_scale_factor_ = device_scale_factor;
2047
2048 if (!is_accelerated_compositing_active_) {
2049 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
2050 } else {
2051 scheduleComposite();
2052 }
2053}
2054
[email protected]adab2332013-07-25 18:04:322055PepperPluginInstanceImpl* RenderWidget::GetBitmapForOptimizedPluginPaint(
2056 const gfx::Rect& paint_bounds,
2057 TransportDIB** dib,
2058 gfx::Rect* location,
2059 gfx::Rect* clip,
2060 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:462061 // Bare RenderWidgets don't support optimized plugin painting.
2062 return NULL;
[email protected]ca4847f2010-09-24 05:39:152063}
2064
[email protected]ceb36f7d2012-10-31 18:33:242065gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:522066 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:242067 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:522068}
2069
[email protected]bee16aab2009-08-26 15:55:032070void RenderWidget::SetHidden(bool hidden) {
2071 if (is_hidden_ == hidden)
2072 return;
2073
2074 // The status has changed. Tell the RenderThread about it.
2075 is_hidden_ = hidden;
2076 if (is_hidden_)
[email protected]380244092011-10-07 17:26:272077 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:032078 else
[email protected]380244092011-10-07 17:26:272079 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:032080}
2081
[email protected]2b624c562011-10-27 22:58:262082void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262083 if (!webwidget_)
2084 return;
2085
2086 if (is_fullscreen_) {
2087 webwidget_->willExitFullScreen();
2088 } else {
2089 webwidget_->willEnterFullScreen();
2090 }
[email protected]2b624c562011-10-27 22:58:262091}
2092
2093void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262094 if (!webwidget_)
2095 return;
2096
2097 if (is_fullscreen_) {
2098 webwidget_->didEnterFullScreen();
2099 } else {
2100 webwidget_->didExitFullScreen();
2101 }
[email protected]2b624c562011-10-27 22:58:262102}
2103
[email protected]699ab0d2009-04-23 23:19:142104void RenderWidget::SetBackground(const SkBitmap& background) {
2105 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:462106
[email protected]699ab0d2009-04-23 23:19:142107 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:282108 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:142109}
2110
[email protected]674741932009-02-04 23:44:462111bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:052112 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462113}
2114
2115bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:052116 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462117}
2118
2119void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:052120 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:462121}
2122
2123void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:052124 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:462125}
2126
2127void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:052128 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:462129}
2130
[email protected]b18583c2012-12-18 06:55:272131static bool IsDateTimeInput(ui::TextInputType type) {
2132 return type == ui::TEXT_INPUT_TYPE_DATE ||
2133 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
2134 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
2135 type == ui::TEXT_INPUT_TYPE_MONTH ||
2136 type == ui::TEXT_INPUT_TYPE_TIME ||
2137 type == ui::TEXT_INPUT_TYPE_WEEK;
2138}
2139
[email protected]66fca5bc2013-05-23 06:58:292140
2141void RenderWidget::StartHandlingImeEvent() {
2142 DCHECK(!handling_ime_event_);
2143 handling_ime_event_ = true;
2144}
2145
2146void RenderWidget::FinishHandlingImeEvent() {
2147 DCHECK(handling_ime_event_);
2148 handling_ime_event_ = false;
2149 // While handling an ime event, text input state and selection bounds updates
2150 // are ignored. These must explicitly be updated once finished handling the
2151 // ime event.
2152 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472153#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132154 UpdateTextInputState(false, false);
[email protected]cb9e2632013-06-18 11:26:472155#endif
[email protected]66fca5bc2013-05-23 06:58:292156}
2157
[email protected]cb9e2632013-06-18 11:26:472158void RenderWidget::UpdateTextInputType() {
2159 if (!input_method_is_active_)
2160 return;
2161
[email protected]e7c569d2013-07-17 16:00:362162 ui::TextInputType new_type = GetTextInputType();
[email protected]cb9e2632013-06-18 11:26:472163 if (IsDateTimeInput(new_type))
2164 return; // Not considered as a text input field in WebKit/Chromium.
2165
2166 bool new_can_compose_inline = CanComposeInline();
2167
[email protected]e7c569d2013-07-17 16:00:362168 WebKit::WebTextInputInfo new_info;
2169 if (webwidget_)
2170 new_info = webwidget_->textInputInfo();
[email protected]b256eca2013-07-11 10:57:402171 const ui::TextInputMode new_mode = ConvertInputMode(new_info.inputMode);
2172
[email protected]cb9e2632013-06-18 11:26:472173 if (text_input_type_ != new_type
[email protected]b256eca2013-07-11 10:57:402174 || can_compose_inline_ != new_can_compose_inline
2175 || text_input_mode_ != new_mode) {
[email protected]cb9e2632013-06-18 11:26:472176 Send(new ViewHostMsg_TextInputTypeChanged(routing_id(),
2177 new_type,
[email protected]b256eca2013-07-11 10:57:402178 new_can_compose_inline,
2179 new_mode));
[email protected]cb9e2632013-06-18 11:26:472180 text_input_type_ = new_type;
2181 can_compose_inline_ = new_can_compose_inline;
[email protected]b256eca2013-07-11 10:57:402182 text_input_mode_ = new_mode;
[email protected]cb9e2632013-06-18 11:26:472183 }
2184}
2185
2186#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132187void RenderWidget::UpdateTextInputState(bool show_ime_if_needed,
2188 bool send_ime_ack) {
[email protected]e8f775f2013-02-14 21:00:502189 if (handling_ime_event_)
2190 return;
[email protected]3306f262012-09-21 19:20:422191 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:292192 return;
[email protected]ad26ef42011-06-17 07:59:452193 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:272194 if (IsDateTimeInput(new_type))
2195 return; // Not considered as a text input field in WebKit/Chromium.
2196
[email protected]5b739cb2012-08-21 20:35:212197 WebKit::WebTextInputInfo new_info;
2198 if (webwidget_)
2199 new_info = webwidget_->textInputInfo();
2200
[email protected]ad26ef42011-06-17 07:59:452201 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212202
[email protected]3306f262012-09-21 19:20:422203 // Only sends text input params if they are changed or if the ime should be
2204 // shown.
2205 if (show_ime_if_needed || (text_input_type_ != new_type
2206 || text_input_info_ != new_info
2207 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212208 ViewHostMsg_TextInputState_Params p;
2209 p.type = new_type;
2210 p.value = new_info.value.utf8();
2211 p.selection_start = new_info.selectionStart;
2212 p.selection_end = new_info.selectionEnd;
2213 p.composition_start = new_info.compositionStart;
2214 p.composition_end = new_info.compositionEnd;
2215 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422216 p.show_ime_if_needed = show_ime_if_needed;
[email protected]0d1ebed12013-08-05 22:01:132217 p.require_ack = send_ime_ack;
2218 if (p.require_ack)
2219 IncrementOutstandingImeEventAcks();
[email protected]5b739cb2012-08-21 20:35:212220 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2221
2222 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042223 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452224 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292225 }
initial.commit09911bf2008-07-26 23:55:292226}
[email protected]cb9e2632013-06-18 11:26:472227#endif
initial.commit09911bf2008-07-26 23:55:292228
[email protected]7c8873e2013-02-05 08:03:012229void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2230 WebRect focus_webrect;
2231 WebRect anchor_webrect;
2232 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2233 *focus = focus_webrect;
2234 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322235}
2236
[email protected]e99ef6f2011-10-16 01:13:002237void RenderWidget::UpdateSelectionBounds() {
2238 if (!webwidget_)
2239 return;
[email protected]66fca5bc2013-05-23 06:58:292240 if (handling_ime_event_)
2241 return;
[email protected]e99ef6f2011-10-16 01:13:002242
[email protected]7c8873e2013-02-05 08:03:012243 ViewHostMsg_SelectionBounds_Params params;
2244 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2245 if (selection_anchor_rect_ != params.anchor_rect ||
2246 selection_focus_rect_ != params.focus_rect) {
2247 selection_anchor_rect_ = params.anchor_rect;
2248 selection_focus_rect_ = params.focus_rect;
2249 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292250 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012251 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352252 }
[email protected]501ea13d2013-07-09 17:03:292253#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362254 UpdateCompositionInfo(false);
[email protected]501ea13d2013-07-09 17:03:292255#endif
[email protected]e99ef6f2011-10-16 01:13:002256}
2257
[email protected]73bf95812011-10-12 11:38:322258// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452259COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2260 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2261COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2262 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2263COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2264 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182265COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2266 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2267COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2268 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2269COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2270 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2271COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2272 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2273COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2274 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002275COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2276 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2277COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2278 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2279COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2280 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2281COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2282 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2283COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2284 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2285COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2286 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012287COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2288 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2289COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2290 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152291COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2292 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452293
[email protected]5b739cb2012-08-21 20:35:212294ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2295 WebKit::WebTextInputType type) {
2296 // Check the type is in the range representable by ui::TextInputType.
2297 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2298 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2299 return static_cast<ui::TextInputType>(type);
2300}
2301
[email protected]ad26ef42011-06-17 07:59:452302ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272303 if (webwidget_)
2304 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452305 return ui::TEXT_INPUT_TYPE_NONE;
2306}
2307
[email protected]501ea13d2013-07-09 17:03:292308#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
2309void RenderWidget::UpdateCompositionInfo(bool should_update_range) {
2310 ui::Range range = ui::Range();
2311 if (should_update_range) {
2312 GetCompositionRange(&range);
2313 } else {
2314 range = composition_range_;
2315 }
2316 std::vector<gfx::Rect> character_bounds;
2317 GetCompositionCharacterBounds(&character_bounds);
2318
2319 if (!ShouldUpdateCompositionInfo(range, character_bounds))
2320 return;
2321 composition_character_bounds_ = character_bounds;
2322 composition_range_ = range;
2323 Send(new ViewHostMsg_ImeCompositionRangeChanged(
2324 routing_id(), composition_range_, composition_character_bounds_));
2325}
2326
[email protected]58b48a0d2012-06-13 07:01:352327void RenderWidget::GetCompositionCharacterBounds(
2328 std::vector<gfx::Rect>* bounds) {
2329 DCHECK(bounds);
2330 bounds->clear();
2331}
2332
[email protected]88dbe32f2013-06-20 23:31:362333void RenderWidget::GetCompositionRange(ui::Range* range) {
2334 size_t location, length;
2335 if (webwidget_->compositionRange(&location, &length)) {
2336 range->set_start(location);
2337 range->set_end(location + length);
2338 } else if (webwidget_->caretOrSelectionRange(&location, &length)) {
2339 range->set_start(location);
2340 range->set_end(location + length);
2341 } else {
2342 *range = ui::Range::InvalidRange();
2343 }
2344}
2345
[email protected]501ea13d2013-07-09 17:03:292346bool RenderWidget::ShouldUpdateCompositionInfo(
2347 const ui::Range& range,
2348 const std::vector<gfx::Rect>& bounds) {
2349 if (composition_range_ != range)
2350 return true;
2351 if (bounds.size() != composition_character_bounds_.size())
2352 return true;
2353 for (size_t i = 0; i < bounds.size(); ++i) {
2354 if (bounds[i] != composition_character_bounds_[i])
2355 return true;
2356 }
2357 return false;
2358}
2359#endif
2360
[email protected]ad26ef42011-06-17 07:59:452361bool RenderWidget::CanComposeInline() {
2362 return true;
[email protected]56ea1a62011-05-30 07:05:572363}
2364
[email protected]4873c7d2009-07-16 06:36:282365WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042366 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282367}
2368
[email protected]f660d9c2012-06-06 18:31:212369float RenderWidget::deviceScaleFactor() {
2370 return device_scale_factor_;
2371}
2372
[email protected]fa7b1dc2010-06-23 17:53:042373void RenderWidget::resetInputMethod() {
2374 if (!input_method_is_active_)
2375 return;
2376
[email protected]0e45bd02013-07-12 20:20:022377 ImeEventGuard guard(this);
[email protected]fa7b1dc2010-06-23 17:53:042378 // If the last text input type is not None, then we should finish any
2379 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452380 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042381 // If a composition text exists, then we need to let the browser process
2382 // to cancel the input method's ongoing composition session.
2383 if (webwidget_->confirmComposition())
2384 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2385 }
[email protected]d4cff272011-05-02 15:46:012386
[email protected]501ea13d2013-07-09 17:03:292387#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362388 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292389#endif
[email protected]fa7b1dc2010-06-23 17:53:042390}
2391
[email protected]c68c3e4e2013-01-24 00:36:562392void RenderWidget::didHandleGestureEvent(
2393 const WebGestureEvent& event,
2394 bool event_cancelled) {
2395#if defined(OS_ANDROID)
2396 if (event_cancelled)
2397 return;
2398 if (event.type == WebInputEvent::GestureTap ||
2399 event.type == WebInputEvent::GestureLongPress) {
[email protected]0d1ebed12013-08-05 22:01:132400 UpdateTextInputState(true, true);
[email protected]c68c3e4e2013-01-24 00:36:562401 }
2402#endif
2403}
2404
[email protected]29e2fb42013-07-19 01:13:472405void RenderWidget::SchedulePluginMove(const WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292406 size_t i = 0;
2407 for (; i < plugin_window_moves_.size(); ++i) {
2408 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582409 if (move.rects_valid) {
2410 plugin_window_moves_[i] = move;
2411 } else {
2412 plugin_window_moves_[i].visible = move.visible;
2413 }
initial.commit09911bf2008-07-26 23:55:292414 break;
2415 }
2416 }
2417
2418 if (i == plugin_window_moves_.size())
2419 plugin_window_moves_.push_back(move);
2420}
[email protected]268654772009-08-06 23:02:042421
2422void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2423 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2424 i != plugin_window_moves_.end(); ++i) {
2425 if (i->window == window) {
2426 plugin_window_moves_.erase(i);
2427 break;
2428 }
2429 }
2430}
[email protected]67bfb83f2011-09-22 03:36:372431
[email protected]b63d58d2012-11-26 22:37:442432void RenderWidget::GetRenderingStats(
2433 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282434 if (compositor_)
[email protected]635353c2013-03-06 09:11:202435 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442436
[email protected]62049562013-03-24 00:39:012437 stats.rendering_stats.animation_frame_count +=
2438 software_stats_.animation_frame_count;
2439 stats.rendering_stats.screen_frame_count +=
2440 software_stats_.screen_frame_count;
2441 stats.rendering_stats.total_paint_time +=
2442 software_stats_.total_paint_time;
2443 stats.rendering_stats.total_pixels_painted +=
2444 software_stats_.total_pixels_painted;
[email protected]fef5e3972012-08-07 03:59:472445}
2446
[email protected]e9ff79c2012-10-19 21:31:262447bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522448 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2449 if (!gpu_channel)
2450 return false;
2451
2452 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2453}
2454
[email protected]24ed0432013-04-24 07:50:312455RenderWidgetCompositor* RenderWidget::compositor() const {
2456 return compositor_.get();
2457}
2458
[email protected]3639aa82013-06-04 11:00:042459void RenderWidget::OnSetBrowserRenderingStats(
2460 const BrowserRenderingStats& stats) {
2461 browser_rendering_stats_ = stats;
2462}
2463
2464void RenderWidget::GetBrowserRenderingStats(BrowserRenderingStats* stats) {
2465 *stats = browser_rendering_stats_;
2466}
2467
[email protected]0c2ebef2013-04-03 12:14:102468void RenderWidget::BeginSmoothScroll(
[email protected]0e241b4b2012-08-18 09:06:272469 bool down,
[email protected]ebd8b562012-10-09 14:44:292470 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192471 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292472 int mouse_event_x,
2473 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272474 DCHECK(!callback.is_null());
[email protected]267909d2012-10-20 04:36:192475
2476 ViewHostMsg_BeginSmoothScroll_Params params;
2477 params.scroll_down = down;
2478 params.pixels_to_scroll = pixels_to_scroll;
2479 params.mouse_event_x = mouse_event_x;
2480 params.mouse_event_y = mouse_event_y;
2481
[email protected]4a9dba42013-04-29 18:24:222482 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, params));
2483 pending_smooth_scroll_gesture_ = callback;
[email protected]a39ca1652012-07-13 21:30:582484}
2485
[email protected]67bfb83f2011-09-22 03:36:372486bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2487 return false;
2488}
[email protected]c3d45532011-10-07 19:20:402489
[email protected]f56c7872013-06-18 12:31:572490bool RenderWidget::WillHandleKeyEvent(const WebKit::WebKeyboardEvent& event) {
2491 return false;
2492}
2493
[email protected]41d86852012-11-07 12:23:242494bool RenderWidget::WillHandleGestureEvent(
2495 const WebKit::WebGestureEvent& event) {
2496 return false;
2497}
2498
[email protected]ce6689f2013-03-29 12:52:552499void RenderWidget::hasTouchEventHandlers(bool has_handlers) {
2500 Send(new ViewHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
2501}
2502
[email protected]3d5c243b2012-11-30 00:26:012503bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2504 return true;
2505}
2506
[email protected]3ae68c52013-04-12 06:10:052507WebGraphicsContext3DCommandBufferImpl* RenderWidget::CreateGraphicsContext3D(
[email protected]92fd8c02013-03-29 08:54:152508 const WebKit::WebGraphicsContext3D::Attributes& attributes) {
[email protected]ed7defa2013-03-12 21:29:592509 if (!webwidget_)
2510 return NULL;
[email protected]ebc0e1df2013-08-01 02:46:222511 if (CommandLine::ForCurrentProcess()->HasSwitch(
2512 switches::kDisableGpuCompositing))
2513 return NULL;
[email protected]8f746982013-03-21 06:28:032514 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2515 new WebGraphicsContext3DCommandBufferImpl(
2516 surface_id(),
2517 GetURLForGraphicsContext3D(),
2518 RenderThreadImpl::current(),
2519 weak_ptr_factory_.GetWeakPtr()));
[email protected]ed7defa2013-03-12 21:29:592520
[email protected]990e4c212013-06-08 05:03:332521 if (!context->InitializeWithDefaultBufferSizes(
[email protected]8f746982013-03-21 06:28:032522 attributes,
2523 false /* bind generates resources */,
2524 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE))
2525 return NULL;
2526 return context.release();
[email protected]ed7defa2013-03-12 21:29:592527}
2528
[email protected]e9ff79c2012-10-19 21:31:262529} // namespace content