blob: fd9721d4680d2bc3889674588d733a789dbf1f18 [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]32876ae2011-11-15 22:25:217#include "base/bind.h"
[email protected]4fb66842009-12-04 21:41:008#include "base/command_line.h"
[email protected]366ae242011-05-10 02:23:589#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]b256eca2013-07-11 10:57:4012#include "base/memory/singleton.h"
[email protected]aaf68892013-07-18 00:11:3013#include "base/message_loop/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3814#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2115#include "base/stl_util.h"
[email protected]74ebfb12013-06-07 20:48:0016#include "base/strings/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4817#include "build/build_config.h"
[email protected]681ccff2013-03-18 06:13:5218#include "cc/base/switches.h"
[email protected]7f0d825f2013-03-18 07:24:3019#include "cc/output/output_surface.h"
[email protected]556fd292013-03-18 08:03:0420#include "cc/trees/layer_tree_host.h"
[email protected]29e2fb42013-07-19 01:13:4721#include "content/child/npapi/webplugin.h"
[email protected]0634cdd42013-08-16 00:46:0922#include "content/common/gpu/client/context_provider_command_buffer.h"
[email protected]ed7defa2013-03-12 21:29:5923#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]c084330e02013-04-27 01:08:1524#include "content/common/input_messages.h"
[email protected]992db4c2011-05-12 15:37:1525#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5026#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2927#include "content/public/common/content_switches.h"
[email protected]953bd0062013-08-01 00:58:4028#include "content/renderer/cursor_utils.h"
[email protected]ed7defa2013-03-12 21:29:5929#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]2847b222013-04-06 00:59:2430#include "content/renderer/gpu/compositor_software_output_device.h"
[email protected]36e5ff12013-06-11 12:19:2931#include "content/renderer/gpu/delegated_compositor_output_surface.h"
[email protected]ea5f70a2013-03-07 12:30:3632#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5933#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2834#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]66fca5bc2013-05-23 06:58:2935#include "content/renderer/ime_event_guard.h"
[email protected]adab2332013-07-25 18:04:3236#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
[email protected]8704f89b2011-04-15 00:30:0537#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4438#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1939#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1840#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4841#include "skia/ext/platform_canvas.h"
[email protected]aaf68892013-07-18 00:11:3042#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
43#include "third_party/WebKit/public/platform/WebPoint.h"
44#include "third_party/WebKit/public/platform/WebRect.h"
45#include "third_party/WebKit/public/platform/WebSize.h"
46#include "third_party/WebKit/public/platform/WebString.h"
[email protected]2255a9332013-06-17 05:12:3147#include "third_party/WebKit/public/web/WebCursorInfo.h"
48#include "third_party/WebKit/public/web/WebHelperPlugin.h"
49#include "third_party/WebKit/public/web/WebPagePopup.h"
50#include "third_party/WebKit/public/web/WebPopupMenu.h"
51#include "third_party/WebKit/public/web/WebPopupMenuInfo.h"
52#include "third_party/WebKit/public/web/WebRange.h"
53#include "third_party/WebKit/public/web/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4154#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1355#include "ui/base/ui_base_switches.h"
[email protected]9b003482013-05-21 14:00:1756#include "ui/gfx/point.h"
[email protected]a25e25b2012-09-28 14:32:3757#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3558#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4859#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2760#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4161#include "ui/surface/transport_dib.h"
[email protected]799fd732013-05-15 21:18:5262#include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h"
[email protected]661eb9d2009-02-03 02:11:4863
[email protected]eeb93112013-05-01 19:41:1064#if defined(OS_ANDROID)
[email protected]b6eb8e332013-09-10 00:51:0165#include "base/android/sys_utils.h"
[email protected]913d99a2013-05-31 07:16:0766#include "content/renderer/android/synchronous_compositor_factory.h"
[email protected]eeb93112013-05-01 19:41:1067#endif
68
[email protected]661eb9d2009-02-03 02:11:4869#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4970#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5271#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4172#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4873#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4474
[email protected]2255a9332013-06-17 05:12:3175#include "third_party/WebKit/public/web/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2976
[email protected]fa7b1dc2010-06-23 17:53:0477using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3078using WebKit::WebCursorInfo;
[email protected]41d86852012-11-07 12:23:2479using WebKit::WebGestureEvent;
[email protected]62cb33cae2009-03-27 23:30:2280using WebKit::WebInputEvent;
[email protected]f56c7872013-06-18 12:31:5781using WebKit::WebKeyboardEvent;
[email protected]6a8ddba52010-09-05 04:38:0682using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2883using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4484using WebKit::WebPagePopup;
[email protected]e99ef6f2011-10-16 01:13:0085using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2886using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5987using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1888using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0189using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5290using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1991using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5292using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2893using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2494using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0495using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1896using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2697
[email protected]6a4d7f62013-01-07 21:32:1398namespace {
99const char* GetEventName(WebInputEvent::Type type) {
100#define CASE_TYPE(t) case WebInputEvent::t: return #t
101 switch(type) {
102 CASE_TYPE(Undefined);
103 CASE_TYPE(MouseDown);
104 CASE_TYPE(MouseUp);
105 CASE_TYPE(MouseMove);
106 CASE_TYPE(MouseEnter);
107 CASE_TYPE(MouseLeave);
108 CASE_TYPE(ContextMenu);
109 CASE_TYPE(MouseWheel);
110 CASE_TYPE(RawKeyDown);
111 CASE_TYPE(KeyDown);
112 CASE_TYPE(KeyUp);
113 CASE_TYPE(Char);
114 CASE_TYPE(GestureScrollBegin);
115 CASE_TYPE(GestureScrollEnd);
116 CASE_TYPE(GestureScrollUpdate);
117 CASE_TYPE(GestureFlingStart);
118 CASE_TYPE(GestureFlingCancel);
119 CASE_TYPE(GestureTap);
[email protected]1c43b0a2013-05-10 07:43:23120 CASE_TYPE(GestureTapUnconfirmed);
[email protected]6a4d7f62013-01-07 21:32:13121 CASE_TYPE(GestureTapDown);
122 CASE_TYPE(GestureTapCancel);
123 CASE_TYPE(GestureDoubleTap);
124 CASE_TYPE(GestureTwoFingerTap);
125 CASE_TYPE(GestureLongPress);
126 CASE_TYPE(GestureLongTap);
127 CASE_TYPE(GesturePinchBegin);
128 CASE_TYPE(GesturePinchEnd);
129 CASE_TYPE(GesturePinchUpdate);
130 CASE_TYPE(TouchStart);
131 CASE_TYPE(TouchMove);
132 CASE_TYPE(TouchEnd);
133 CASE_TYPE(TouchCancel);
[email protected]3ec08ed2013-01-11 15:59:57134 default:
135 // Must include default to let WebKit::WebInputEvent add new event types
136 // before they're added here.
137 DLOG(WARNING) << "Unhandled WebInputEvent type in GetEventName.\n";
138 break;
[email protected]6a4d7f62013-01-07 21:32:13139 }
140#undef CASE_TYPE
[email protected]3ec08ed2013-01-11 15:59:57141 return "";
[email protected]6a4d7f62013-01-07 21:32:13142}
[email protected]b256eca2013-07-11 10:57:40143
144typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
145
146class TextInputModeMapSingleton {
147 public:
148 static TextInputModeMapSingleton* GetInstance() {
149 return Singleton<TextInputModeMapSingleton>::get();
150 }
151 TextInputModeMapSingleton()
152 : map() {
153 map["verbatim"] = ui::TEXT_INPUT_MODE_VERBATIM;
154 map["latin"] = ui::TEXT_INPUT_MODE_LATIN;
155 map["latin-name"] = ui::TEXT_INPUT_MODE_LATIN_NAME;
156 map["latin-prose"] = ui::TEXT_INPUT_MODE_LATIN_PROSE;
157 map["full-width-latin"] = ui::TEXT_INPUT_MODE_FULL_WIDTH_LATIN;
158 map["kana"] = ui::TEXT_INPUT_MODE_KANA;
159 map["katakana"] = ui::TEXT_INPUT_MODE_KATAKANA;
160 map["numeric"] = ui::TEXT_INPUT_MODE_NUMERIC;
161 map["tel"] = ui::TEXT_INPUT_MODE_TEL;
162 map["email"] = ui::TEXT_INPUT_MODE_EMAIL;
163 map["url"] = ui::TEXT_INPUT_MODE_URL;
164 }
165 TextInputModeMap& Map() {
166 return map;
167 }
168 private:
169 TextInputModeMap map;
170
171 friend struct DefaultSingletonTraits<TextInputModeMapSingleton>;
172
173 DISALLOW_COPY_AND_ASSIGN(TextInputModeMapSingleton);
174};
175
176ui::TextInputMode ConvertInputMode(
177 const WebKit::WebString& input_mode) {
178 static TextInputModeMapSingleton* singleton =
179 TextInputModeMapSingleton::GetInstance();
180 TextInputModeMap::iterator it = singleton->Map().find(input_mode.utf8());
181 if (it == singleton->Map().end())
182 return ui::TEXT_INPUT_MODE_DEFAULT;
183 return it->second;
[email protected]6a4d7f62013-01-07 21:32:13184}
[email protected]b256eca2013-07-11 10:57:40185
186} // namespace
187
[email protected]e9ff79c2012-10-19 21:31:26188namespace content {
[email protected]62cb33cae2009-03-27 23:30:22189
[email protected]6fd35b72012-03-01 19:46:41190RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:04191 const WebKit::WebScreenInfo& screen_info,
[email protected]1ac10dca2013-08-20 20:47:04192 bool swapped_out,
193 bool hidden)
initial.commit09911bf2008-07-26 23:55:29194 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56195 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11196 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29197 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30198 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35199 current_paint_buf_(NULL),
[email protected]d9083762013-03-24 01:36:40200 overdraw_bottom_height_(0.f),
initial.commit09911bf2008-07-26 23:55:29201 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06202 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05203 update_reply_pending_(false),
[email protected]847a2582013-03-09 02:29:51204 auto_resize_mode_(false),
[email protected]ea3ee0a2012-05-15 03:43:09205 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24206 using_asynchronous_swapbuffers_(false),
207 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29208 did_show_(false),
[email protected]1ac10dca2013-08-20 20:47:04209 is_hidden_(hidden),
[email protected]ee41e7d22011-10-14 19:34:09210 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29211 needs_repainting_on_restore_(false),
212 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49213 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50214 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48215 closing_(false),
[email protected]14392a52012-05-02 20:28:44216 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04217 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45218 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40219 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
[email protected]86ba5fcb2013-09-04 00:36:53220 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12221 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48222 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12223 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50224 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21225 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07226 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04227 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52228 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]ed7defa2013-03-12 21:29:59229 is_threaded_compositing_enabled_(false),
[email protected]53b4cc12013-07-18 23:02:30230 next_output_surface_id_(0),
[email protected]0d1ebed12013-08-05 22:01:13231#if defined(OS_ANDROID)
232 outstanding_ime_acks_(0),
233#endif
[email protected]69e797f2013-04-30 01:10:22234 weak_ptr_factory_(this) {
[email protected]8b3f0eb2012-05-03 19:15:05235 if (!swapped_out)
236 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27237 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18238 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
239 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36240 is_threaded_compositing_enabled_ =
241 CommandLine::ForCurrentProcess()->HasSwitch(
242 switches::kEnableThreadedCompositing);
[email protected]c1e6cc062013-08-24 03:35:35243
244 legacy_software_mode_stats_ = cc::RenderingStatsInstrumentation::Create();
245 if (CommandLine::ForCurrentProcess()->HasSwitch(
246 switches::kEnableGpuBenchmarking))
247 legacy_software_mode_stats_->set_record_rendering_stats(true);
initial.commit09911bf2008-07-26 23:55:29248}
249
250RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11251 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21252 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35253 if (current_paint_buf_) {
[email protected]467414eb2013-07-30 20:32:29254 if (RenderProcess::current()) {
255 // If the RenderProcess is already gone, it will have released all DIBs
256 // in its destructor anyway.
257 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
258 }
[email protected]b4d08452010-10-05 17:34:35259 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29260 }
[email protected]992db4c2011-05-12 15:37:15261 // If we are swapped out, we have released already.
[email protected]d2e2f9ee2013-08-21 11:02:02262 if (!is_swapped_out_ && RenderProcess::current())
[email protected]992db4c2011-05-12 15:37:15263 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29264}
265
[email protected]484955942010-08-19 16:13:18266// static
[email protected]8085dbc82008-09-26 22:53:44267RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04268 WebKit::WebPopupType popup_type,
269 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29270 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41271 scoped_refptr<RenderWidget> widget(
[email protected]1ac10dca2013-08-20 20:47:04272 new RenderWidget(popup_type, screen_info, false, false));
[email protected]a635f942012-12-07 10:34:29273 if (widget->Init(opener_id)) { // adds reference on success.
[email protected]fc72bb12013-06-02 21:13:46274 return widget.get();
[email protected]a635f942012-12-07 10:34:29275 }
276 return NULL;
initial.commit09911bf2008-07-26 23:55:29277}
278
[email protected]484955942010-08-19 16:13:18279// static
280WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
281 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03282 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18283 break;
284 case WebKit::WebPopupTypeSelect:
285 case WebKit::WebPopupTypeSuggestion:
286 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44287 case WebKit::WebPopupTypePage:
288 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03289 case WebKit::WebPopupTypeHelperPlugin:
290 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18291 default:
292 NOTREACHED();
293 }
294 return NULL;
295}
296
[email protected]a635f942012-12-07 10:34:29297bool RenderWidget::Init(int32 opener_id) {
298 return DoInit(opener_id,
299 RenderWidget::CreateWebWidget(this),
300 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
301 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18302}
303
[email protected]a635f942012-12-07 10:34:29304bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06305 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18306 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29307 DCHECK(!webwidget_);
308
309 if (opener_id != MSG_ROUTING_NONE)
310 opener_id_ = opener_id;
311
[email protected]484955942010-08-19 16:13:18312 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29313
[email protected]380244092011-10-07 17:26:27314 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29315 if (result) {
[email protected]380244092011-10-07 17:26:27316 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29317 // Take a reference on behalf of the RenderThread. This will be balanced
318 // when we receive ViewMsg_Close.
319 AddRef();
[email protected]1ac10dca2013-08-20 20:47:04320 if (is_hidden_)
321 RenderThread::Get()->WidgetHidden();
[email protected]a635f942012-12-07 10:34:29322 return true;
initial.commit09911bf2008-07-26 23:55:29323 } else {
[email protected]a635f942012-12-07 10:34:29324 // The above Send can fail when the tab is closing.
325 return false;
initial.commit09911bf2008-07-26 23:55:29326 }
327}
328
[email protected]fc4404d2012-11-07 19:53:30329// This is used to complete pending inits and non-pending inits.
330void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29331 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29332
[email protected]fc4404d2012-11-07 19:53:30333 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42334
[email protected]8926c602013-01-23 05:32:06335 if (webwidget_ && is_threaded_compositing_enabled_) {
336 webwidget_->enterForceCompositingMode(true);
337 }
[email protected]ba91a792013-02-06 09:48:28338 if (compositor_) {
339 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09340 }
[email protected]05a980d7a2012-02-07 22:16:42341 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29342
[email protected]6de74452009-02-25 18:04:59343 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29344}
345
[email protected]992db4c2011-05-12 15:37:15346void RenderWidget::SetSwappedOut(bool is_swapped_out) {
347 // We should only toggle between states.
348 DCHECK(is_swapped_out_ != is_swapped_out);
349 is_swapped_out_ = is_swapped_out;
350
351 // If we are swapping out, we will call ReleaseProcess, allowing the process
352 // to exit if all of its RenderViews are swapped out. We wait until the
353 // WasSwappedOut call to do this, to avoid showing the sad tab.
354 // If we are swapping in, we call AddRefProcess to prevent the process from
355 // exiting.
356 if (!is_swapped_out)
357 RenderProcess::current()->AddRefProcess();
358}
359
[email protected]34bb3ac2013-03-08 02:41:28360bool RenderWidget::AllowPartialSwap() const {
361 return true;
362}
363
[email protected]c8cbae72013-05-23 10:45:03364bool RenderWidget::UsingSynchronousRendererCompositor() const {
[email protected]ccc1722e2013-05-06 19:43:07365#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07366 return SynchronousCompositorFactory::GetInstance() != NULL;
367#else
[email protected]ccc1722e2013-05-06 19:43:07368 return false;
[email protected]913d99a2013-05-31 07:16:07369#endif
[email protected]ccc1722e2013-05-06 19:43:07370}
371
[email protected]a95986a82010-12-24 06:19:28372bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
373 bool handled = true;
374 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
[email protected]c084330e02013-04-27 01:08:15375 IPC_MESSAGE_HANDLER(InputMsg_HandleInputEvent, OnHandleInputEvent)
[email protected]34202de2013-05-06 23:36:22376 IPC_MESSAGE_HANDLER(InputMsg_CursorVisibilityChange,
377 OnCursorVisibilityChange)
[email protected]c084330e02013-04-27 01:08:15378 IPC_MESSAGE_HANDLER(InputMsg_MouseCaptureLost, OnMouseCaptureLost)
379 IPC_MESSAGE_HANDLER(InputMsg_SetFocus, OnSetFocus)
[email protected]a95986a82010-12-24 06:19:28380 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
381 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
382 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54383 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28384 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41385 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15386 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28387 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59388 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
389 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28390 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
391 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
392 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25393 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
394 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
[email protected]79fa22e2013-08-23 15:18:12395 IPC_MESSAGE_HANDLER(ViewMsg_SyntheticGestureCompleted,
396 OnSyntheticGestureCompleted)
[email protected]a95986a82010-12-24 06:19:28397 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
398 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]80ad8622012-11-07 16:33:03399 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21400#if defined(OS_ANDROID)
401 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
[email protected]2384b6c2013-02-28 23:58:51402 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]0d1ebed12013-08-05 22:01:13403 IPC_MESSAGE_HANDLER(ViewMsg_ImeEventAck, OnImeEventAck)
[email protected]105dffb42013-02-20 03:46:21404#endif
[email protected]51a49502013-03-23 01:50:19405 IPC_MESSAGE_HANDLER(ViewMsg_Snapshot, OnSnapshot)
[email protected]3639aa82013-06-04 11:00:04406 IPC_MESSAGE_HANDLER(ViewMsg_SetBrowserRenderingStats,
407 OnSetBrowserRenderingStats)
[email protected]a95986a82010-12-24 06:19:28408 IPC_MESSAGE_UNHANDLED(handled = false)
409 IPC_END_MESSAGE_MAP()
410 return handled;
411}
initial.commit09911bf2008-07-26 23:55:29412
413bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15414 // Don't send any messages after the browser has told us to close, and filter
415 // most outgoing messages while swapped out.
416 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26417 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11418 closing_) {
initial.commit09911bf2008-07-26 23:55:29419 delete message;
420 return false;
421 }
422
423 // If given a messsage without a routing ID, then assign our routing ID.
424 if (message->routing_id() == MSG_ROUTING_NONE)
425 message->set_routing_id(routing_id_);
426
[email protected]380244092011-10-07 17:26:27427 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44428}
429
[email protected]61e2b3cc2012-03-02 16:13:34430void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44431 const gfx::Size& physical_backing_size,
[email protected]d9083762013-03-24 01:36:40432 float overdraw_bottom_height,
[email protected]61e2b3cc2012-03-02 16:13:34433 const gfx::Rect& resizer_rect,
434 bool is_fullscreen,
435 ResizeAck resize_ack) {
[email protected]1c0008842013-06-06 08:35:48436 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
437 !RenderThreadImpl::current()->layout_test_mode()) {
438 // A resize ack shouldn't be requested if we have not ACK'd the previous
439 // one.
440 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
441 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
442 }
initial.commit09911bf2008-07-26 23:55:29443
[email protected]61e2b3cc2012-03-02 16:13:34444 // Ignore this during shutdown.
445 if (!webwidget_)
446 return;
447
[email protected]d9083762013-03-24 01:36:40448 if (compositor_) {
[email protected]60d47ac2013-03-01 23:42:44449 compositor_->setViewportSize(new_size, physical_backing_size);
[email protected]d9083762013-03-24 01:36:40450 compositor_->SetOverdrawBottomHeight(overdraw_bottom_height);
451 }
[email protected]60d47ac2013-03-01 23:42:44452
[email protected]dade8992013-03-04 07:34:34453 physical_backing_size_ = physical_backing_size;
[email protected]d9083762013-03-24 01:36:40454 overdraw_bottom_height_ = overdraw_bottom_height;
[email protected]61e2b3cc2012-03-02 16:13:34455 resizer_rect_ = resizer_rect;
456
457 // NOTE: We may have entered fullscreen mode without changing our size.
458 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
459 if (fullscreen_change)
460 WillToggleFullscreen();
461 is_fullscreen_ = is_fullscreen;
462
463 if (size_ != new_size) {
464 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34465 needs_repainting_on_restore_ = false;
466
467 size_ = new_size;
468
469 paint_aggregator_.ClearPendingUpdate();
470
471 // When resizing, we want to wait to paint before ACK'ing the resize. This
472 // ensures that we only resize as fast as we can paint. We only need to
473 // send an ACK if we are resized to a non-empty rect.
474 webwidget_->resize(new_size);
[email protected]0b70dbe2013-05-10 19:06:32475
[email protected]92201e32013-06-08 06:14:52476 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
477 !RenderThreadImpl::current()->layout_test_mode()) {
478 // Resize should have caused an invalidation of the entire view.
479 DCHECK(new_size.IsEmpty() || is_accelerated_compositing_active_ ||
480 paint_aggregator_.HasPendingUpdate());
481 }
[email protected]1c0008842013-06-06 08:35:48482 } else if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
483 !RenderThreadImpl::current()->layout_test_mode()) {
[email protected]632c4382013-05-15 08:58:45484 resize_ack = NO_RESIZE_ACK;
485 }
486
487 if (new_size.IsEmpty() || physical_backing_size.IsEmpty()) {
488 // For empty size or empty physical_backing_size, there is no next paint
489 // (along with which to send the ack) until they are set to non-empty.
[email protected]ff475a322012-03-14 00:05:35490 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34491 }
492
[email protected]20fbfc22013-05-08 20:50:58493 // Send the Resize_ACK flag once we paint again if requested.
[email protected]632c4382013-05-15 08:58:45494 if (resize_ack == SEND_RESIZE_ACK)
[email protected]20fbfc22013-05-08 20:50:58495 set_next_paint_is_resize_ack();
496
[email protected]61e2b3cc2012-03-02 16:13:34497 if (fullscreen_change)
498 DidToggleFullscreen();
499
500 // If a resize ack is requested and it isn't set-up, then no more resizes will
501 // come in and in general things will go wrong.
[email protected]632c4382013-05-15 08:58:45502 DCHECK(resize_ack != SEND_RESIZE_ACK || next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29503}
504
505void RenderWidget::OnClose() {
506 if (closing_)
507 return;
508 closing_ = true;
509
510 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03511 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27512 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03513 SetHidden(false);
514 }
initial.commit09911bf2008-07-26 23:55:29515
initial.commit09911bf2008-07-26 23:55:29516 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25517 // now. Post a task that only gets invoked when there are no nested message
518 // loops.
[email protected]dd32b1272013-05-04 14:17:11519 base::MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29520 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25521
522 // Balances the AddRef taken when we called AddRoute.
523 Release();
initial.commit09911bf2008-07-26 23:55:29524}
525
[email protected]61e2b3cc2012-03-02 16:13:34526// Got a response from the browser after the renderer decided to create a new
527// view.
[email protected]fc4404d2012-11-07 19:53:30528void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34529 DCHECK(routing_id_ != MSG_ROUTING_NONE);
530
[email protected]fc4404d2012-11-07 19:53:30531 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34532}
533
[email protected]0fdd5012013-05-29 08:05:56534void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) {
535 screen_info_ = params.screen_info;
536 SetDeviceScaleFactor(screen_info_.deviceScaleFactor);
537 Resize(params.new_size, params.physical_backing_size,
538 params.overdraw_bottom_height, params.resizer_rect,
539 params.is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29540}
541
[email protected]b5913d72012-02-07 22:26:54542void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
543 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59544 gfx::Rect view_rect(size_);
545
[email protected]ce112fe2012-10-29 22:52:18546 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59547 if (!old_damage_rect.IsEmpty())
548 paint_aggregator_.InvalidateRect(old_damage_rect);
549
[email protected]ce112fe2012-10-29 22:52:18550 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59551 if (!new_damage_rect.IsEmpty())
552 paint_aggregator_.InvalidateRect(new_damage_rect);
553
[email protected]b5913d72012-02-07 22:26:54554 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59555
[email protected]b5913d72012-02-07 22:26:54556 if (webwidget_)
557 webwidget_->didChangeWindowResizerRect();
558 }
559}
560
initial.commit09911bf2008-07-26 23:55:29561void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31562 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29563 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03564 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29565}
566
[email protected]9e2e4632012-07-27 16:38:41567void RenderWidget::OnWasShown(bool needs_repainting) {
568 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29569 // During shutdown we can just ignore this message.
570 if (!webwidget_)
571 return;
572
573 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03574 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29575
576 if (!needs_repainting && !needs_repainting_on_restore_)
577 return;
578 needs_repainting_on_restore_ = false;
579
[email protected]d65adb12010-04-28 17:26:49580 // Tag the next paint as a restore ack, which is picked up by
581 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29582 set_next_paint_is_restore_ack();
583
584 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56585 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46586 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
587 } else {
588 scheduleComposite();
589 }
initial.commit09911bf2008-07-26 23:55:29590}
591
[email protected]992db4c2011-05-12 15:37:15592void RenderWidget::OnWasSwappedOut() {
593 // If we have been swapped out and no one else is using this process,
594 // it's safe to exit now. If we get swapped back in, we will call
595 // AddRefProcess in SetSwappedOut.
596 if (is_swapped_out_)
597 RenderProcess::current()->ReleaseProcess();
598}
599
[email protected]53d3f302009-12-21 04:42:05600void RenderWidget::OnRequestMoveAck() {
601 DCHECK(pending_window_rect_count_);
602 pending_window_rect_count_--;
603}
604
605void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58606 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21607 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05608 update_reply_pending_ = false;
609
[email protected]b4d08452010-10-05 17:34:35610 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
611 // have no current paint buffer.
612 if (current_paint_buf_) {
613 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
614 current_paint_buf_ = NULL;
615 }
616
[email protected]65225772011-05-12 21:10:24617 // If swapbuffers is still pending, then defer the update until the
618 // swapbuffers occurs.
619 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
620 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
621 return;
622 }
623
[email protected]29ed96a2012-02-04 18:12:16624 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18625 if (!is_accelerated_compositing_active_) {
626 DidFlushPaint();
627 }
[email protected]a2f6bc112009-06-27 16:27:25628
initial.commit09911bf2008-07-26 23:55:29629 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24630 DoDeferredUpdateAndSendInputAck();
631}
632
[email protected]d0be63772011-12-20 23:18:04633bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59634 // Contexts using the command buffer support asynchronous swapbuffers.
635 // See RenderWidget::CreateOutputSurface().
[email protected]cadac622013-06-11 16:46:36636 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get())
[email protected]ed7defa2013-03-12 21:29:59637 return false;
638
639 return true;
640}
641
642GURL RenderWidget::GetURLForGraphicsContext3D() {
643 return GURL();
[email protected]65225772011-05-12 21:10:24644}
645
[email protected]479b0172012-10-29 19:27:09646bool RenderWidget::ForceCompositingModeEnabled() {
647 return false;
648}
649
[email protected]ebc0e1df2013-08-01 02:46:22650scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface(bool fallback) {
[email protected]a1811b8912013-05-09 15:35:19651
652#if defined(OS_ANDROID)
[email protected]b6eb8e332013-09-10 00:51:01653 if (SynchronousCompositorFactory* factory =
654 SynchronousCompositorFactory::GetInstance()) {
[email protected]913d99a2013-05-31 07:16:07655 return factory->CreateOutputSurface(routing_id());
[email protected]a1811b8912013-05-09 15:35:19656 }
657#endif
658
[email protected]ed7defa2013-03-12 21:29:59659 // Explicitly disable antialiasing for the compositor. As of the time of
660 // this writing, the only platform that supported antialiasing for the
661 // compositor was Mac OS X, because the on-screen OpenGL context creation
662 // code paths on Windows and Linux didn't yet have multisampling support.
663 // Mac OS X essentially always behaves as though it's rendering offscreen.
664 // Multisampling has a heavy cost especially on devices with relatively low
665 // fill rate like most notebooks, and the Mac implementation would need to
666 // be optimized to resolve directly into the IOSurface shared between the
667 // GPU and browser processes. For these reasons and to avoid platform
668 // disparities we explicitly disable antialiasing.
669 WebKit::WebGraphicsContext3D::Attributes attributes;
670 attributes.antialias = false;
671 attributes.shareResources = true;
672 attributes.noAutomaticFlushes = true;
[email protected]a6886502013-05-16 20:59:18673 attributes.depth = false;
674 attributes.stencil = false;
[email protected]b6eb8e332013-09-10 00:51:01675
676 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]bec084292013-05-21 21:31:44677 if (command_line.HasSwitch(cc::switches::kForceDirectLayerDrawing))
678 attributes.stencil = true;
[email protected]b6eb8e332013-09-10 00:51:01679
[email protected]0634cdd42013-08-16 00:46:09680 scoped_refptr<ContextProviderCommandBuffer> context_provider;
681 if (!fallback) {
682 context_provider = ContextProviderCommandBuffer::Create(
[email protected]af13d832013-09-13 06:56:27683 CreateGraphicsContext3D(attributes),
684 "RenderCompositor");
[email protected]0634cdd42013-08-16 00:46:09685 }
[email protected]ebc0e1df2013-08-01 02:46:22686
[email protected]b6eb8e332013-09-10 00:51:01687 uint32 output_surface_id = next_output_surface_id_++;
[email protected]0634cdd42013-08-16 00:46:09688 if (!context_provider.get()) {
[email protected]ebc0e1df2013-08-01 02:46:22689 if (!command_line.HasSwitch(switches::kEnableSoftwareCompositing))
690 return scoped_ptr<cc::OutputSurface>();
[email protected]0634cdd42013-08-16 00:46:09691
692 scoped_ptr<cc::SoftwareOutputDevice> software_device(
693 new CompositorSoftwareOutputDevice());
694
695 return scoped_ptr<cc::OutputSurface>(new CompositorOutputSurface(
696 routing_id(),
697 output_surface_id,
698 NULL,
699 software_device.Pass(),
700 true));
[email protected]ebc0e1df2013-08-01 02:46:22701 }
[email protected]ed7defa2013-03-12 21:29:59702
[email protected]0c04d1c2013-07-10 00:02:32703 if (command_line.HasSwitch(switches::kEnableDelegatedRenderer) &&
704 !command_line.HasSwitch(switches::kDisableDelegatedRenderer)) {
[email protected]36e5ff12013-06-11 12:19:29705 DCHECK(is_threaded_compositing_enabled_);
706 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09707 new DelegatedCompositorOutputSurface(
708 routing_id(),
709 output_surface_id,
710 context_provider,
711 scoped_ptr<cc::SoftwareOutputDevice>()));
[email protected]36e5ff12013-06-11 12:19:29712 }
713 if (command_line.HasSwitch(cc::switches::kCompositeToMailbox)) {
714 DCHECK(is_threaded_compositing_enabled_);
715 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09716 new MailboxOutputSurface(
717 routing_id(),
718 output_surface_id,
719 context_provider,
720 scoped_ptr<cc::SoftwareOutputDevice>()));
[email protected]36e5ff12013-06-11 12:19:29721 }
[email protected]0634cdd42013-08-16 00:46:09722 bool use_swap_compositor_frame_message = false;
[email protected]36e5ff12013-06-11 12:19:29723 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09724 new CompositorOutputSurface(
725 routing_id(),
726 output_surface_id,
727 context_provider,
728 scoped_ptr<cc::SoftwareOutputDevice>(),
729 use_swap_compositor_frame_message));
[email protected]ba91a792013-02-06 09:48:28730}
731
[email protected]ed7defa2013-03-12 21:29:59732void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24733 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21734 while (!updates_pending_swap_.empty()) {
735 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
736 updates_pending_swap_.pop_front();
737 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
738 // compositing pass, hence doesn't require an UpdateRect message.
739 if (msg)
740 Send(msg);
741 }
[email protected]65225772011-05-12 21:10:24742 num_swapbuffers_complete_pending_ = 0;
743 using_asynchronous_swapbuffers_ = false;
744 // Schedule another frame so the compositor learns about it.
745 scheduleComposite();
746}
747
[email protected]ed7defa2013-03-12 21:29:59748void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08749 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21750
751 if (using_asynchronous_swapbuffers_) {
752 ViewHostMsg_UpdateRect* msg = NULL;
753 // pending_update_params_ can be NULL if the swap doesn't correspond to an
754 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
755 // message.
[email protected]59383c782013-04-17 16:43:27756 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:21757 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
758 pending_update_params_.reset();
759 }
760 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08761 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21762 }
[email protected]37a6f302011-07-11 23:43:08763}
764
[email protected]ed7defa2013-03-12 21:29:59765void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24766 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16767
[email protected]404939f2012-06-01 04:06:18768 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16769 DidFlushPaint();
770
[email protected]65225772011-05-12 21:10:24771 // When compositing deactivates, we reset the swapbuffers pending count. The
772 // swapbuffers acks may still arrive, however.
773 if (num_swapbuffers_complete_pending_ == 0) {
774 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
775 return;
776 }
[email protected]aa4117f2011-12-09 22:19:21777 DCHECK(!updates_pending_swap_.empty());
778 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
779 updates_pending_swap_.pop_front();
780 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
781 // compositing pass, hence doesn't require an UpdateRect message.
782 if (msg)
783 Send(msg);
[email protected]65225772011-05-12 21:10:24784 num_swapbuffers_complete_pending_--;
785
786 // If update reply is still pending, then defer the update until that reply
787 // occurs.
[email protected]d0be63772011-12-20 23:18:04788 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24789 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
790 return;
791 }
792
793 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06794 // when we were previously rendering. However, if an invalidation task is not
795 // posted, there may be software rendering work pending. In that case, don't
796 // early out.
797 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24798 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
799 return;
800 }
801
[email protected]cc66e682012-10-02 06:48:18802 // Do not call DoDeferredUpdate unless there's animation work to be done or
803 // a real invalidation. This prevents rendering in response to a swapbuffers
804 // callback coming back after we've navigated away from the page that
805 // generated it.
806 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
807 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
808 return;
809 }
810
[email protected]65225772011-05-12 21:10:24811 // Continue painting if necessary...
812 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]0dea1652012-12-14 00:09:09815void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
[email protected]4b157662013-05-29 04:05:05816 const ui::LatencyInfo& latency_info,
[email protected]0dea1652012-12-14 00:09:09817 bool is_keyboard_shortcut) {
[email protected]5dd768212009-08-13 23:34:49818 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09819 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49820 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29821 return;
[email protected]5dd768212009-08-13 23:34:49822 }
initial.commit09911bf2008-07-26 23:55:29823
[email protected]b4841e1c2013-05-16 22:30:10824 const char* const event_name = GetEventName(input_event->type);
825 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent",
826 "event", event_name);
827
[email protected]c2eaa8f2013-05-10 02:41:55828 if (compositor_)
829 compositor_->SetLatencyInfo(latency_info);
[email protected]256737c2013-06-08 04:39:10830 else
831 latency_info_.MergeWith(latency_info);
[email protected]c2eaa8f2013-05-10 02:41:55832
[email protected]6a4d7f62013-01-07 21:32:13833 base::TimeDelta now = base::TimeDelta::FromInternalValue(
834 base::TimeTicks::Now().ToInternalValue());
835
836 int64 delta = static_cast<int64>(
837 (now.InSecondsF() - input_event->timeStampSeconds) *
838 base::Time::kMicrosecondsPerSecond);
839 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
[email protected]de415552013-01-23 04:12:17840 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:48841 base::Histogram::FactoryGet(
[email protected]b4841e1c2013-05-16 22:30:10842 base::StringPrintf("Event.Latency.Renderer.%s", event_name),
[email protected]bafdc5d52013-02-27 18:18:48843 0,
844 1000000,
[email protected]6a4d7f62013-01-07 21:32:13845 100,
[email protected]de415552013-01-23 04:12:17846 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:48847 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:13848
[email protected]67bfb83f2011-09-22 03:36:37849 bool prevent_default = false;
850 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26851 const WebMouseEvent& mouse_event =
852 *static_cast<const WebMouseEvent*>(input_event);
853 TRACE_EVENT2("renderer", "HandleMouseMove",
854 "x", mouse_event.x, "y", mouse_event.y);
855 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37856 }
857
[email protected]f56c7872013-06-18 12:31:57858 if (WebInputEvent::isKeyboardEventType(input_event->type)) {
859 const WebKeyboardEvent& key_event =
860 *static_cast<const WebKeyboardEvent*>(input_event);
861 prevent_default = WillHandleKeyEvent(key_event);
862 }
863
[email protected]41d86852012-11-07 12:23:24864 if (WebInputEvent::isGestureEventType(input_event->type)) {
865 const WebGestureEvent& gesture_event =
866 *static_cast<const WebGestureEvent*>(input_event);
867 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
868 }
869
[email protected]3ebcc7c2013-01-09 05:34:46870 if (input_event->type == WebInputEvent::GestureTap ||
871 input_event->type == WebInputEvent::GestureLongPress)
872 resetInputMethod();
873
[email protected]67bfb83f2011-09-22 03:36:37874 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12875 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
876 suppress_next_char_events_ = false;
877 if (!processed && webwidget_)
878 processed = webwidget_->handleInputEvent(*input_event);
879 }
880
881 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
882 // it's not processed by webkit, then we need to suppress the upcoming Char
883 // events.
884 if (!processed && is_keyboard_shortcut)
885 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29886
[email protected]3d5c243b2012-11-30 00:26:01887 InputEventAckState ack_result = processed ?
888 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
889 if (!processed && input_event->type == WebInputEvent::TouchStart) {
890 const WebTouchEvent& touch_event =
891 *static_cast<const WebTouchEvent*>(input_event);
892 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
893 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
894 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
895 }
896
[email protected]a9fb30aa2011-10-06 06:58:46897 IPC::Message* response =
[email protected]f6df0edf2013-08-07 00:27:02898 new InputHostMsg_HandleInputEvent_ACK(routing_id_,
899 input_event->type,
900 ack_result,
901 latency_info);
[email protected]3391a0772012-03-28 00:32:07902 bool event_type_gets_rate_limited =
903 input_event->type == WebInputEvent::MouseMove ||
904 input_event->type == WebInputEvent::MouseWheel ||
905 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06906
907 bool frame_pending = paint_aggregator_.HasPendingUpdate();
908 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28909 frame_pending = compositor_ &&
910 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06911 }
912
[email protected]9a8ce7f92013-06-11 12:39:49913 if (event_type_gets_rate_limited && frame_pending && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24914 // We want to rate limit the input events in this case, so we'll wait for
915 // painting to finish before ACKing this message.
[email protected]59383c782013-04-17 16:43:27916 if (pending_input_event_ack_) {
[email protected]353a34c2010-05-28 23:35:17917 // As two different kinds of events could cause us to postpone an ack
918 // we send it now, if we have one pending. The Browser should never
919 // send us the same kind of event we are delaying the ack for.
920 Send(pending_input_event_ack_.release());
921 }
[email protected]12fbad812009-09-01 18:21:24922 pending_input_event_ack_.reset(response);
[email protected]df09e052013-07-31 18:59:50923 if (compositor_)
924 compositor_->NotifyInputThrottledUntilCommit();
[email protected]12fbad812009-09-01 18:21:24925 } else {
926 Send(response);
927 }
928
[email protected]3306f262012-09-21 19:20:42929#if defined(OS_ANDROID)
930 // Allow the IME to be shown when the focus changes as a consequence
931 // of a processed touch end event.
932 if (input_event->type == WebInputEvent::TouchEnd && processed)
[email protected]0d1ebed12013-08-05 22:01:13933 UpdateTextInputState(true, true);
[email protected]3306f262012-09-21 19:20:42934#endif
935
[email protected]5dd768212009-08-13 23:34:49936 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48937
[email protected]67bfb83f2011-09-22 03:36:37938 if (!prevent_default) {
939 if (WebInputEvent::isKeyboardEventType(input_event->type))
940 DidHandleKeyEvent();
941 if (WebInputEvent::isMouseEventType(input_event->type))
942 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24943 if (WebInputEvent::isTouchEventType(input_event->type))
944 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37945 }
initial.commit09911bf2008-07-26 23:55:29946}
947
[email protected]34202de2013-05-06 23:36:22948void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
949 if (webwidget_)
950 webwidget_->setCursorVisibilityState(is_visible);
951}
952
initial.commit09911bf2008-07-26 23:55:29953void RenderWidget::OnMouseCaptureLost() {
954 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28955 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29956}
957
958void RenderWidget::OnSetFocus(bool enable) {
959 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33960 if (webwidget_)
961 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29962}
963
964void RenderWidget::ClearFocus() {
965 // We may have got the focus from the browser before this gets processed, in
966 // which case we do not want to unfocus ourself.
967 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28968 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29969}
970
[email protected]2d5d09d52009-06-15 14:29:21971void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00972 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21973 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06974 TRACE_EVENT2("renderer", "PaintRect",
975 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45976
[email protected]4fb66842009-12-04 21:41:00977 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21978
979 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00980 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
981 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03982
[email protected]699ab0d2009-04-23 23:19:14983 // If there is a custom background, tile it.
984 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14985 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11986 skia::RefPtr<SkShader> shader = skia::AdoptRef(
987 SkShader::CreateBitmapShader(background_,
988 SkShader::kRepeat_TileMode,
989 SkShader::kRepeat_TileMode));
990 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20991
992 // Use kSrc_Mode to handle background_ transparency properly.
993 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
994
995 // Canvas could contain multiple update rects. Clip to given rect so that
996 // we don't accidentally clear other update rects.
997 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44998 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48999 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:141000 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:201001 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:141002 }
1003
[email protected]719b36f2010-12-22 20:36:461004 // First see if this rect is a plugin that can paint itself faster.
1005 TransportDIB* optimized_dib = NULL;
1006 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201007 float dib_scale_factor;
[email protected]adab2332013-07-25 18:04:321008 PepperPluginInstanceImpl* optimized_instance =
[email protected]719b36f2010-12-22 20:36:461009 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
1010 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201011 &optimized_copy_rect,
1012 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:461013 if (optimized_instance) {
[email protected]20790a222013-07-25 02:23:051014#if defined(ENABLE_PLUGINS)
[email protected]719b36f2010-12-22 20:36:461015 // This plugin can be optimize-painted and we can just ask it to paint
1016 // itself. We don't actually need the TransportDIB in this case.
1017 //
1018 // This is an optimization for PPAPI plugins that know they're on top of
1019 // the page content. If this rect is inside such a plugin, we can save some
1020 // time and avoid re-rendering the page content which we know will be
1021 // covered by the plugin later (this time can be significant, especially
1022 // for a playing movie that is invalidating a lot).
1023 //
1024 // In the plugin movie case, hopefully the similar call to
1025 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
1026 // painting, because that avoids copying the plugin image to a different
1027 // paint rect. Unfortunately, if anything on the page is animating other
1028 // than the movie, it break this optimization since the union of the
1029 // invalid regions will be larger than the plugin.
1030 //
1031 // This code optimizes that case, where we can still avoid painting in
1032 // WebKit and filling the background (which can be slow) and just painting
1033 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
1034 // required.
[email protected]df59dd42012-09-14 22:56:301035 SkAutoCanvasRestore auto_restore(canvas, true);
1036 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]6bd867b2013-07-24 22:10:201037 optimized_instance->Paint(canvas, optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:211038 canvas->restore();
[email protected]20790a222013-07-25 02:23:051039#endif
[email protected]719b36f2010-12-22 20:36:461040 } else {
1041 // Normal painting case.
[email protected]c1e6cc062013-08-24 03:35:351042 base::TimeTicks start_time;
1043 if (!is_accelerated_compositing_active_)
1044 start_time = legacy_software_mode_stats_->StartRecording();
[email protected]63ab54262012-11-09 15:58:451045
[email protected]6bd867b2013-07-24 22:10:201046 webwidget_->paint(canvas, rect);
[email protected]63ab54262012-11-09 15:58:451047
[email protected]c1e6cc062013-08-24 03:35:351048 if (!is_accelerated_compositing_active_) {
[email protected]63ab54262012-11-09 15:58:451049 base::TimeDelta paint_time =
[email protected]c1e6cc062013-08-24 03:35:351050 legacy_software_mode_stats_->EndRecording(start_time);
1051 int64 painted_pixel_count = rect.width() * rect.height();
1052 legacy_software_mode_stats_->AddPaint(paint_time, painted_pixel_count);
[email protected]63ab54262012-11-09 15:58:451053 }
[email protected]719b36f2010-12-22 20:36:461054
1055 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:351056 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:461057 }
initial.commit09911bf2008-07-26 23:55:291058
[email protected]4fb66842009-12-04 21:41:001059 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:001060 canvas->restore();
1061}
1062
1063void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
1064 skia::PlatformCanvas* canvas) {
1065 static bool kPaintBorder =
1066 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
1067 if (!kPaintBorder)
1068 return;
1069
[email protected]53d3f302009-12-21 04:42:051070 // Cycle through these colors to help distinguish new paint rects.
1071 const SkColor colors[] = {
1072 SkColorSetARGB(0x3F, 0xFF, 0, 0),
1073 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
1074 SkColorSetARGB(0x3F, 0, 0, 0xFF),
1075 };
1076 static int color_selector = 0;
1077
[email protected]4fb66842009-12-04 21:41:001078 SkPaint paint;
1079 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:051080 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:001081 paint.setStrokeWidth(1);
1082
1083 SkIRect irect;
1084 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
1085 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:291086}
1087
[email protected]52ccd0ea2011-02-16 01:09:051088void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:301089 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:301090 if (!animation_update_pending_) {
1091 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:591092 return;
[email protected]921244e42011-07-20 16:36:301093 }
[email protected]bd37ae252011-06-03 01:28:181094 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:591095 // Record when we fired (according to base::Time::Now()) relative to when
1096 // we posted the task to quantify how much the base::Time/base::TimeTicks
1097 // skew is affecting animations.
1098 base::TimeDelta animation_callback_delay = base::Time::Now() -
1099 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
1100 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
1101 animation_callback_delay,
1102 base::TimeDelta::FromMilliseconds(0),
1103 base::TimeDelta::FromMilliseconds(30),
1104 25);
1105 }
[email protected]65225772011-05-12 21:10:241106 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:241107}
1108
[email protected]52ccd0ea2011-02-16 01:09:051109void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:591110 if (!animation_update_pending_)
1111 return;
[email protected]bd37ae252011-06-03 01:28:181112
1113 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:331114 base::TimeDelta animationInterval = IsRenderingVSynced() ?
1115 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:181116
[email protected]7c4329e2011-02-18 22:02:591117 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:451118
1119 // animation_floor_time_ is the earliest time that we should animate when
1120 // using the dead reckoning software scheduler. If we're using swapbuffers
1121 // complete callbacks to rate limit, we can ignore this floor.
1122 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:301123 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:331124 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:181125 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:591126 // running animation callbacks so that if a callback requests another
1127 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:381128 animation_timer_.Stop();
1129 animation_timer_.Start(FROM_HERE, animationInterval, this,
1130 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:591131 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:281132 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:201133 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:061134 } else {
[email protected]635353c2013-03-06 09:11:201135 double frame_begin_time =
1136 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
1137 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061138 }
[email protected]7c4329e2011-02-18 22:02:591139 return;
[email protected]5f8b1022011-01-21 23:34:501140 }
[email protected]bd37ae252011-06-03 01:28:181141 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381142 if (!animation_timer_.IsRunning()) {
1143 // This code uses base::Time::Now() to calculate the floor and next fire
1144 // time because javascript's Date object uses base::Time::Now(). The
1145 // message loop uses base::TimeTicks, which on windows can have a
1146 // different granularity than base::Time.
1147 // The upshot of all this is that this function might be called before
1148 // base::Time::Now() has advanced past the animation_floor_time_. To
1149 // avoid exposing this delay to javascript, we keep posting delayed
1150 // tasks until base::Time::Now() has advanced far enough.
1151 base::TimeDelta delay = animation_floor_time_ - now;
1152 animation_timer_.Start(FROM_HERE, delay, this,
1153 &RenderWidget::AnimationCallback);
1154 }
[email protected]5f8b1022011-01-21 23:34:501155}
1156
[email protected]bd37ae252011-06-03 01:28:181157bool RenderWidget::IsRenderingVSynced() {
1158 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1159 // not caught by this check. This will lead to artificially low frame rates
1160 // for people who force vsync off at a driver level and expect Chrome to speed
1161 // up.
1162 return !has_disable_gpu_vsync_switch_;
1163}
1164
[email protected]65225772011-05-12 21:10:241165void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061166 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241167 invalidation_task_posted_ = false;
1168 DoDeferredUpdateAndSendInputAck();
1169}
1170
1171void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051172 DoDeferredUpdate();
1173
[email protected]59383c782013-04-17 16:43:271174 if (pending_input_event_ack_)
[email protected]52ccd0ea2011-02-16 01:09:051175 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211176}
1177
[email protected]552e6002009-11-19 05:24:571178void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581179 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]fc8bde1a2013-07-04 08:54:071180 TRACE_EVENT_SCOPED_SAMPLING_STATE("Chrome", "Paint");
[email protected]71e2f0a2011-03-15 22:25:081181
[email protected]65225772011-05-12 21:10:241182 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291183 return;
[email protected]05a980d7a2012-02-07 22:16:421184
[email protected]fc4404d2012-11-07 19:53:301185 if (!init_complete_) {
1186 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421187 return;
1188 }
[email protected]aa4117f2011-12-09 22:19:211189 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241190 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1191 return;
1192 }
[email protected]9ca84622011-06-02 23:46:391193 if (is_accelerated_compositing_active_ &&
1194 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241195 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1196 return;
1197 }
initial.commit09911bf2008-07-26 23:55:291198
[email protected]552e6002009-11-19 05:24:571199 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051200 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571201 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291202 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241203 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291204 return;
1205 }
1206
[email protected]0fb93f52011-05-18 23:13:561207 // Tracking of frame rate jitter
1208 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371209 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051210 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501211
[email protected]f98d7e3c2010-09-13 22:30:461212 // Layout may generate more invalidation. It may also enable the
1213 // GPU acceleration, so make sure to run layout before we send the
1214 // GpuRenderingActivated message.
1215 webwidget_->layout();
1216
[email protected]793b2d62013-06-11 00:43:251217 // Check for whether we need to track swap buffers. We need to do that after
1218 // layout() because it may have switched us to accelerated compositing.
1219 if (is_accelerated_compositing_active_)
1220 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1221
[email protected]dcca3aa92012-02-17 23:03:371222 // The following two can result in further layout and possibly
1223 // enable GPU acceleration so they need to be called before any painting
1224 // is done.
[email protected]cb9e2632013-06-18 11:26:471225 UpdateTextInputType();
[email protected]dcca3aa92012-02-17 23:03:371226 UpdateSelectionBounds();
1227
[email protected]5f8b1022011-01-21 23:34:501228 // Suppress painting if nothing is dirty. This has to be done after updating
1229 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241230 if (!paint_aggregator_.HasPendingUpdate()) {
1231 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371232 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501233 return;
[email protected]65225772011-05-12 21:10:241234 }
[email protected]5f8b1022011-01-21 23:34:501235
[email protected]479b0172012-10-29 19:27:091236 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361237 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091238 ForceCompositingModeEnabled()) {
1239 webwidget_->enterForceCompositingMode(true);
1240 }
1241
[email protected]872ae5b2011-05-26 20:20:501242 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561243 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041244 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561245 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1246 delay,
1247 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411248 base::TimeDelta::FromMilliseconds(120),
1249 60);
[email protected]d0be63772011-12-20 23:18:041250 } else {
[email protected]0fb93f52011-05-18 23:13:561251 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1252 delay,
1253 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411254 base::TimeDelta::FromMilliseconds(120),
1255 60);
[email protected]d0be63772011-12-20 23:18:041256 }
[email protected]872ae5b2011-05-26 20:20:501257
1258 // Calculate filtered time per frame:
1259 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1260 filtered_time_per_frame_ =
1261 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561262 }
1263 last_do_deferred_update_time_ = frame_begin_ticks;
1264
[email protected]fef5e3972012-08-07 03:59:471265 if (!is_accelerated_compositing_active_) {
[email protected]c1e6cc062013-08-24 03:35:351266 legacy_software_mode_stats_->IncrementAnimationFrameCount();
1267 legacy_software_mode_stats_->IncrementScreenFrameCount(1, true);
1268 legacy_software_mode_stats_->IssueTraceEventForMainThreadStats();
1269 legacy_software_mode_stats_->AccumulateAndClearMainThreadStats();
[email protected]fef5e3972012-08-07 03:59:471270 }
1271
[email protected]552e6002009-11-19 05:24:571272 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291273 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301274 PaintAggregator::PendingUpdate update;
1275 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291276
[email protected]53d3f302009-12-21 04:42:051277 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181278 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291279
[email protected]29ed96a2012-02-04 18:12:161280 // Notify derived classes that we're about to initiate a paint.
1281 WillInitiatePaint();
1282
[email protected]ca4847f2010-09-24 05:39:151283 // A plugin may be able to do an optimized paint. First check this, in which
1284 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461285 // This optimization allows PPAPI plugins that declare themselves on top of
1286 // the page (like a traditional windowed plugin) to be able to animate (think
1287 // movie playing) without repeatedly re-painting the page underneath, or
1288 // copying the plugin backing store (since we can send the plugin's backing
1289 // store directly to the browser).
1290 //
1291 // This optimization only works when the entire invalid region is contained
1292 // within the plugin. There is a related optimization in PaintRect for the
1293 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151294 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151295 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201296 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211297 DCHECK(!pending_update_params_.get());
1298 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551299 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211300 pending_update_params_->scroll_rect = update.scroll_rect;
1301 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211302 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1303 pending_update_params_->flags = next_paint_flags_;
1304 pending_update_params_->scroll_offset = GetScrollOffset();
1305 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091306 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211307 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091308 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211309
[email protected]256737c2013-06-08 04:39:101310 if (!is_accelerated_compositing_active_)
1311 pending_update_params_->latency_info = latency_info_;
1312
1313 latency_info_.Clear();
1314
[email protected]ca4847f2010-09-24 05:39:151315 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561316 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151317 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201318 &optimized_copy_rect,
1319 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271320 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471321 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211322 pending_update_params_->bitmap = dib->id();
1323 pending_update_params_->bitmap_rect = optimized_copy_location;
1324 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201325 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561326 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461327 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101328
1329 bool fractional_scale = device_scale_factor_ -
1330 static_cast<int>(device_scale_factor_) != 0;
1331 if (fractional_scale) {
1332 // Damage might not be DIP aligned. Inflate damage to compensate.
1333 bounds.Inset(-1, -1);
1334 bounds.Intersect(gfx::Rect(size_));
1335 }
1336
1337 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181338 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101339
[email protected]ca4847f2010-09-24 05:39:151340 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351341 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591342 pixel_bounds));
[email protected]59383c782013-04-17 16:43:271343 if (!canvas) {
[email protected]f98d7e3c2010-09-13 22:30:461344 NOTREACHED();
1345 return;
1346 }
[email protected]cef3362f2009-12-21 17:48:451347
[email protected]f98d7e3c2010-09-13 22:30:461348 // We may get back a smaller canvas than we asked for.
1349 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591350 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1351 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1352 pixel_bounds.set_width(canvas->getDevice()->width());
1353 pixel_bounds.set_height(canvas->getDevice()->height());
1354 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1355 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051356
[email protected]f98d7e3c2010-09-13 22:30:461357 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1358
[email protected]aa4117f2011-12-09 22:19:211359 pending_update_params_->bitmap = current_paint_buf_->id();
1360 pending_update_params_->bitmap_rect = bounds;
1361
1362 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461363 // The scroll damage is just another rectangle to paint and copy.
1364 copy_rects.swap(update.paint_rects);
1365 if (!scroll_damage.IsEmpty())
1366 copy_rects.push_back(scroll_damage);
1367
[email protected]4889bd212013-02-11 22:23:101368 for (size_t i = 0; i < copy_rects.size(); ++i) {
1369 gfx::Rect rect = copy_rects[i];
1370 if (fractional_scale) {
1371 // Damage might not be DPI aligned. Inflate rect to compensate.
1372 rect.Inset(-1, -1);
1373 }
1374 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1375 }
[email protected]60a50072012-01-11 02:05:351376
1377 // Software FPS tick for performance tests. The accelerated path traces the
1378 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1379 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421380 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW",
1381 TRACE_EVENT_SCOPE_THREAD);
[email protected]f98d7e3c2010-09-13 22:30:461382 } else { // Accelerated compositing path
1383 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211384 // If painting is done via the gpu process then we don't set any damage
1385 // rects to save the browser process from doing unecessary work.
1386 pending_update_params_->bitmap_rect = bounds;
1387 pending_update_params_->scroll_rect = gfx::Rect();
1388 // We don't need an ack, because we're not sharing a DIB with the browser.
1389 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1390 // with the browser for the GPU surface.
1391 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521392 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461393 }
1394
[email protected]936c6f52011-12-13 01:35:261395 // If we're holding a pending input event ACK, send the ACK before sending the
1396 // UpdateReply message so we can receive another input event before the
1397 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1398 // the UpdateRect IPC message handler.
[email protected]59383c782013-04-17 16:43:271399 if (pending_input_event_ack_)
[email protected]936c6f52011-12-13 01:35:261400 Send(pending_input_event_ack_.release());
1401
[email protected]ab543072013-01-25 04:38:151402 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211403 // OnSwapBuffersPosted), meaning a message has been added to the
1404 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1405 // the message now.
[email protected]59383c782013-04-17 16:43:271406 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:211407 // sending an ack to browser process that the paint is complete...
1408 update_reply_pending_ = pending_update_params_->needs_ack;
1409 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1410 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341411 }
[email protected]53d3f302009-12-21 04:42:051412
[email protected]29ed96a2012-02-04 18:12:161413 // If we're software rendering then we're done initiating the paint.
1414 if (!is_accelerated_compositing_active_)
1415 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291416}
1417
[email protected]f0c2a242013-03-15 19:34:521418void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151419 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281420 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521421 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151422}
1423
initial.commit09911bf2008-07-26 23:55:291424///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461425// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291426
[email protected]4873c7d2009-07-16 06:36:281427void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571428 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481429 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181430 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571431 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291432 return;
1433
[email protected]552e6002009-11-19 05:24:571434 paint_aggregator_.InvalidateRect(damaged_rect);
1435
1436 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241437 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571438 return;
1439 if (!paint_aggregator_.HasPendingUpdate())
1440 return;
[email protected]aa4117f2011-12-09 22:19:211441 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241442 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1443 return;
1444
1445 // When GPU rendering, combine pending animations and invalidations into
1446 // a single update.
[email protected]816edc62012-03-17 01:27:221447 if (is_accelerated_compositing_active_ &&
1448 animation_update_pending_ &&
1449 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571450 return;
1451
1452 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291453 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1454 // on the call stack.
1455 // 2) Allows us to collect more damage rects before painting to help coalesce
1456 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241457 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111458 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211459 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291460}
1461
[email protected]990278ff2012-11-13 02:12:551462void RenderWidget::didScrollRect(int dx, int dy,
1463 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461464 // Drop scrolls on the floor when we are in compositing mode.
1465 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561466 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461467 return;
1468
[email protected]552e6002009-11-19 05:24:571469 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481470 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181471 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571472 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291473 return;
1474
[email protected]990278ff2012-11-13 02:12:551475 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571476
1477 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241478 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571479 return;
1480 if (!paint_aggregator_.HasPendingUpdate())
1481 return;
[email protected]aa4117f2011-12-09 22:19:211482 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241483 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1484 return;
1485
1486 // When GPU rendering, combine pending animations and invalidations into
1487 // a single update.
[email protected]816edc62012-03-17 01:27:221488 if (is_accelerated_compositing_active_ &&
1489 animation_update_pending_ &&
1490 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571491 return;
1492
1493 // Perform updating asynchronously. This serves two purposes:
1494 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1495 // on the call stack.
1496 // 2) Allows us to collect more damage rects before painting to help coalesce
1497 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241498 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111499 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211500 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291501}
1502
[email protected]244ac1892011-12-02 17:04:471503void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091504 if (size_.width() != new_size.width || size_.height() != new_size.height) {
[email protected]eac2b362013-05-22 07:01:451505 size_ = new_size;
[email protected]20fbfc22013-05-08 20:50:581506
[email protected]eac2b362013-05-22 07:01:451507 // If we don't clear PaintAggregator after changing autoResize state, then
1508 // we might end up in a situation where bitmap_rect is larger than the
1509 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1510 // with invalid damage rects.
1511 paint_aggregator_.ClearPendingUpdate();
1512
[email protected]70dee7e2013-05-29 18:28:301513 if (RenderThreadImpl::current()->layout_test_mode()) {
[email protected]eac2b362013-05-22 07:01:451514 WebRect new_pos(rootWindowRect().x,
1515 rootWindowRect().y,
1516 new_size.width,
1517 new_size.height);
1518 view_screen_rect_ = new_pos;
1519 window_screen_rect_ = new_pos;
[email protected]8be1c582013-03-06 00:55:031520 }
[email protected]20fbfc22013-05-08 20:50:581521
[email protected]eac2b362013-05-22 07:01:451522 AutoResizeCompositor();
[email protected]20fbfc22013-05-08 20:50:581523
[email protected]70dee7e2013-05-29 18:28:301524 if (!RenderThreadImpl::current()->layout_test_mode())
[email protected]20fbfc22013-05-08 20:50:581525 need_update_rect_for_auto_resize_ = true;
[email protected]ea3ee0a2012-05-15 03:43:091526 }
[email protected]244ac1892011-12-02 17:04:471527}
1528
[email protected]3a1c8a8032013-03-18 22:35:321529void RenderWidget::AutoResizeCompositor() {
[email protected]97e1bf72013-03-06 14:06:051530 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1531 device_scale_factor_));
1532 if (compositor_)
1533 compositor_->setViewportSize(size_, physical_backing_size_);
1534}
1535
[email protected]91acd1c2012-03-14 08:32:391536void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221537 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1538
[email protected]c63b4d42012-04-26 01:01:071539#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211540 if (!is_accelerated_compositing_active_) {
1541 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1542 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1543 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1544 // going to switch to accelerated compositing, the GPU process may need
1545 // round-trips to the browser's UI thread before finishing the frame,
1546 // causing deadlocks if we delay the UpdateRect until we receive the
1547 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071548 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1549 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211550 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1551 }
[email protected]c63b4d42012-04-26 01:01:071552#endif
[email protected]aa4117f2011-12-09 22:19:211553
[email protected]ea162f92011-10-04 23:08:221554 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421555 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241556 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221557}
1558
1559void RenderWidget::didDeactivateCompositor() {
1560 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1561
1562 is_accelerated_compositing_active_ = false;
1563 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1564 routing_id_, is_accelerated_compositing_active_));
1565
[email protected]ea162f92011-10-04 23:08:221566 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241567 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091568
1569 // In single-threaded mode, we exit force compositing mode and re-enter in
1570 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1571 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1572 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361573 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091574 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561575}
1576
[email protected]e195e582013-03-08 01:32:591577void RenderWidget::initializeLayerTreeView() {
[email protected]c5fa4c42013-07-20 05:47:371578 compositor_ = RenderWidgetCompositor::Create(
1579 this, is_threaded_compositing_enabled_);
[email protected]e195e582013-03-08 01:32:591580 if (!compositor_)
1581 return;
1582
1583 compositor_->setViewportSize(size_, physical_backing_size_);
1584 if (init_complete_)
1585 compositor_->setSurfaceReady();
1586}
1587
[email protected]8926c602013-01-23 05:32:061588WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281589 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061590}
1591
[email protected]9ed83fe2013-02-27 01:52:281592void RenderWidget::suppressCompositorScheduling(bool enable) {
1593 if (compositor_)
1594 compositor_->SetSuppressScheduleComposite(enable);
1595}
1596
[email protected]9cd43a62012-03-26 08:03:561597void RenderWidget::willBeginCompositorFrame() {
1598 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371599
[email protected]cadac622013-06-11 16:46:361600 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy().get());
[email protected]abe8b3a2012-03-28 21:19:371601
1602 // The following two can result in further layout and possibly
1603 // enable GPU acceleration so they need to be called before any painting
1604 // is done.
[email protected]cb9e2632013-06-18 11:26:471605 UpdateTextInputType();
1606#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:131607 UpdateTextInputState(false, true);
[email protected]cb9e2632013-06-18 11:26:471608#endif
[email protected]abe8b3a2012-03-28 21:19:371609 UpdateSelectionBounds();
1610
[email protected]9cd43a62012-03-26 08:03:561611 WillInitiatePaint();
1612}
1613
[email protected]3391a0772012-03-28 00:32:071614void RenderWidget::didBecomeReadyForAdditionalInput() {
1615 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
[email protected]59383c782013-04-17 16:43:271616 if (pending_input_event_ack_)
[email protected]3391a0772012-03-28 00:32:071617 Send(pending_input_event_ack_.release());
1618}
1619
[email protected]6fceb912013-02-15 06:24:151620void RenderWidget::DidCommitCompositorFrame() {
1621}
1622
[email protected]58264a32011-11-17 23:36:151623void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501624 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351625 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1626 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421627 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU",
1628 TRACE_EVENT_SCOPE_THREAD);
[email protected]29ed96a2012-02-04 18:12:161629 // Notify subclasses that we initiated the paint operation.
1630 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151631}
1632
1633void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181634 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1635
1636 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561637 DidFlushPaint();
1638
[email protected]aa4117f2011-12-09 22:19:211639 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151640 return;
1641
[email protected]ea3ee0a2012-05-15 03:43:091642 if (!next_paint_flags_ &&
1643 !need_update_rect_for_auto_resize_ &&
1644 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151645 return;
[email protected]ea3ee0a2012-05-15 03:43:091646 }
[email protected]58264a32011-11-17 23:36:151647
1648 ViewHostMsg_UpdateRect_Params params;
1649 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151650 params.plugin_window_moves.swap(plugin_window_moves_);
1651 params.flags = next_paint_flags_;
1652 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121653 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091654 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151655
1656 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1657 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091658 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151659}
1660
[email protected]f98d7e3c2010-09-13 22:30:461661void RenderWidget::scheduleComposite() {
[email protected]cadac622013-06-11 16:46:361662 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get() &&
[email protected]ba91a792013-02-06 09:48:281663 compositor_) {
1664 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041665 } else {
[email protected]c3d45532011-10-07 19:20:401666 // TODO(nduca): replace with something a little less hacky. The reason this
1667 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1668 // contains a lot of host-renderer synchronization logic that is still
1669 // important for the accelerated compositing case. The option of simply
1670 // duplicating all that code is less desirable than "faking out" the
1671 // invalidation path using a magical damage rect.
1672 didInvalidateRect(WebRect(0, 0, 1, 1));
1673 }
[email protected]f98d7e3c2010-09-13 22:30:461674}
1675
[email protected]5f8b1022011-01-21 23:34:501676void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201677 if (animation_update_pending_)
1678 return;
1679
[email protected]921244e42011-07-20 16:36:301680 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201681 animation_update_pending_ = true;
1682 if (!animation_timer_.IsRunning()) {
1683 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1684 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211685 }
[email protected]5f8b1022011-01-21 23:34:501686}
1687
[email protected]4873c7d2009-07-16 06:36:281688void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301689 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521690 WebCursor cursor;
[email protected]953bd0062013-08-01 00:58:401691 InitializeCursorFromWebKitCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291692 // Only send a SetCursor message if we need to make a change.
1693 if (!current_cursor_.IsEqual(cursor)) {
1694 current_cursor_ = cursor;
1695 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1696 }
1697}
1698
1699// We are supposed to get a single call to Show for a newly created RenderWidget
1700// that was created via RenderWidget::CreateWebView. So, we wait until this
1701// point to dispatch the ShowWidget message.
1702//
1703// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281704// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291705//
[email protected]4873c7d2009-07-16 06:36:281706void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291707 DCHECK(!did_show_) << "received extraneous Show call";
1708 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1709 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1710
[email protected]8de12d942010-11-17 20:42:441711 if (did_show_)
1712 return;
1713
1714 did_show_ = true;
1715 // NOTE: initial_pos_ may still have its default values at this point, but
1716 // that's okay. It'll be ignored if as_popup is false, or the browser
1717 // process will impose a default position otherwise.
1718 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1719 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291720}
1721
[email protected]9b003482013-05-21 14:00:171722void RenderWidget::didProgrammaticallyScroll(
1723 const WebKit::WebPoint& scroll_point) {
1724 if (!compositor_)
1725 return;
1726 Send(new ViewHostMsg_DidProgrammaticallyScroll(
1727 routing_id_, gfx::Vector2d(scroll_point.x, scroll_point.y)));
1728}
1729
[email protected]4873c7d2009-07-16 06:36:281730void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291731}
1732
[email protected]4873c7d2009-07-16 06:36:281733void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291734}
1735
[email protected]2533ce12009-05-09 00:02:241736void RenderWidget::DoDeferredClose() {
1737 Send(new ViewHostMsg_Close(routing_id_));
1738}
1739
[email protected]4873c7d2009-07-16 06:36:281740void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321741 if (is_swapped_out_) {
1742 // This widget is currently swapped out, and the active widget is in a
1743 // different process. Have the browser route the close request to the
1744 // active widget instead, so that the correct unload handlers are run.
1745 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1746 return;
1747 }
1748
initial.commit09911bf2008-07-26 23:55:291749 // If a page calls window.close() twice, we'll end up here twice, but that's
1750 // OK. It is safe to send multiple Close messages.
1751
[email protected]2533ce12009-05-09 00:02:241752 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1753 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1754 // could be closed before the JS finishes executing. So instead, post a
1755 // message back to the message loop, which won't run until the JS is
1756 // complete, and then the Close message can be sent.
[email protected]dd32b1272013-05-04 14:17:111757 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211758 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291759}
1760
1761void RenderWidget::Close() {
1762 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061763 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281764 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281765 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291766 webwidget_ = NULL;
1767 }
1768}
1769
[email protected]4873c7d2009-07-16 06:36:281770WebRect RenderWidget::windowRect() {
1771 if (pending_window_rect_count_)
1772 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241773
[email protected]80ad8622012-11-07 16:33:031774 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291775}
1776
[email protected]8a9d6ca32011-06-06 20:11:301777void RenderWidget::setToolTipText(const WebKit::WebString& text,
1778 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541779 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301780}
1781
[email protected]4873c7d2009-07-16 06:36:281782void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291783 if (did_show_) {
[email protected]70dee7e2013-05-29 18:28:301784 if (!RenderThreadImpl::current()->layout_test_mode()) {
[email protected]8be1c582013-03-06 00:55:031785 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
1786 SetPendingWindowRect(pos);
1787 } else {
1788 WebSize new_size(pos.width, pos.height);
[email protected]d9083762013-03-24 01:36:401789 Resize(new_size, new_size, overdraw_bottom_height_,
1790 WebRect(), is_fullscreen_, NO_RESIZE_ACK);
[email protected]8be1c582013-03-06 00:55:031791 view_screen_rect_ = pos;
1792 window_screen_rect_ = pos;
1793 }
initial.commit09911bf2008-07-26 23:55:291794 } else {
1795 initial_pos_ = pos;
1796 }
1797}
1798
[email protected]2533ce12009-05-09 00:02:241799void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1800 pending_window_rect_ = rect;
1801 pending_window_rect_count_++;
1802}
1803
[email protected]4873c7d2009-07-16 06:36:281804WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241805 if (pending_window_rect_count_) {
1806 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1807 // the RootWindowRect is probably going to return wrong results since the
1808 // browser may not have processed the Move yet. There isn't really anything
1809 // good to do in this case, and it shouldn't happen - since this size is
1810 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281811 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241812 }
1813
[email protected]80ad8622012-11-07 16:33:031814 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371815}
1816
[email protected]4873c7d2009-07-16 06:36:281817WebRect RenderWidget::windowResizerRect() {
1818 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191819}
1820
[email protected]fa7b1dc2010-06-23 17:53:041821void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031822 // To prevent this renderer process from sending unnecessary IPC messages to
1823 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041824 // only during the input method attached to the browser process is active.
1825 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291826}
1827
[email protected]fa7b1dc2010-06-23 17:53:041828void RenderWidget::OnImeSetComposition(
1829 const string16& text,
1830 const std::vector<WebCompositionUnderline>& underlines,
1831 int selection_start, int selection_end) {
[email protected]0d1ebed12013-08-05 22:01:131832 if (!ShouldHandleImeEvent())
[email protected]4873c7d2009-07-16 06:36:281833 return;
[email protected]66fca5bc2013-05-23 06:58:291834 ImeEventGuard guard(this);
[email protected]88dbe32f2013-06-20 23:31:361835 if (!webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041836 text, WebVector<WebCompositionUnderline>(underlines),
1837 selection_start, selection_end)) {
1838 // If we failed to set the composition text, then we need to let the browser
1839 // process to cancel the input method's ongoing composition session, to make
1840 // sure we are in a consistent state.
1841 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:261842 }
[email protected]501ea13d2013-07-09 17:03:291843#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361844 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291845#endif
[email protected]fa7b1dc2010-06-23 17:53:041846}
1847
[email protected]0e45bd02013-07-12 20:20:021848void RenderWidget::OnImeConfirmComposition(const string16& text,
[email protected]db4fc1e2013-09-06 20:01:511849 const gfx::Range& replacement_range,
[email protected]0e45bd02013-07-12 20:20:021850 bool keep_selection) {
[email protected]0d1ebed12013-08-05 22:01:131851 if (!ShouldHandleImeEvent())
[email protected]d0be63772011-12-20 23:18:041852 return;
[email protected]66fca5bc2013-05-23 06:58:291853 ImeEventGuard guard(this);
[email protected]d0be63772011-12-20 23:18:041854 handling_input_event_ = true;
[email protected]0e45bd02013-07-12 20:20:021855 if (text.length())
1856 webwidget_->confirmComposition(text);
1857 else if (keep_selection)
1858 webwidget_->confirmComposition(WebWidget::KeepSelection);
1859 else
1860 webwidget_->confirmComposition(WebWidget::DoNotKeepSelection);
[email protected]d0be63772011-12-20 23:18:041861 handling_input_event_ = false;
[email protected]501ea13d2013-07-09 17:03:291862#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361863 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291864#endif
initial.commit09911bf2008-07-26 23:55:291865}
1866
[email protected]948f7ab72010-05-28 23:48:081867// This message causes the renderer to render an image of the
1868// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251869void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1870 int tag,
1871 const gfx::Size& page_size,
1872 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001873 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1874 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251875 // Close our unused handle.
1876#if defined(OS_WIN)
1877 ::CloseHandle(dib_handle);
1878#elif defined(OS_MACOSX)
1879 base::SharedMemory::CloseHandle(dib_handle);
1880#endif
1881 }
[email protected]d65adb12010-04-28 17:26:491882 return;
[email protected]45c6aad32010-11-11 04:46:251883 }
[email protected]d65adb12010-04-28 17:26:491884
[email protected]948f7ab72010-05-28 23:48:081885 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491886 // If one of these is empty, then we just return the dib we were
1887 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091888 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491889 return;
1890 }
1891
1892 // Map the given DIB ID into this process, and unmap it at the end
1893 // of this function.
[email protected]45c6aad32010-11-11 04:46:251894 scoped_ptr<TransportDIB> paint_at_size_buffer(
1895 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301896
[email protected]4b01b962012-10-09 23:17:351897 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281898 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351899 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281900 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511901 gfx::Size canvas_size = page_size_in_pixel;
1902 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491903 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511904 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491905 static_cast<float>(canvas_size.height());
1906
[email protected]ee8d6fd2010-05-26 17:05:481907 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491908 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1909 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481910 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491911
[email protected]36808ad2010-10-20 19:18:301912 scoped_ptr<skia::PlatformCanvas> canvas(
1913 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1914 canvas_size.height()));
[email protected]59383c782013-04-17 16:43:271915 if (!canvas) {
[email protected]36808ad2010-10-20 19:18:301916 NOTREACHED();
1917 return;
1918 }
1919
[email protected]d65adb12010-04-28 17:26:491920 // Reset bounds to what we actually received, but they should be the
1921 // same.
1922 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1923 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1924 bounds.set_width(canvas->getDevice()->width());
1925 bounds.set_height(canvas->getDevice()->height());
1926
1927 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081928 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491929 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1930
[email protected]948f7ab72010-05-28 23:48:081931 // Have to make sure we're laid out at the right size before
1932 // rendering.
1933 gfx::Size old_size = webwidget_->size();
1934 webwidget_->resize(page_size);
1935 webwidget_->layout();
1936
[email protected]d65adb12010-04-28 17:26:491937 // Paint the entire thing (using original bounds, not scaled bounds).
1938 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1939 canvas->restore();
1940
[email protected]948f7ab72010-05-28 23:48:081941 // Return the widget to its previous size.
1942 webwidget_->resize(old_size);
1943
[email protected]c88c9442010-07-19 18:55:091944 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491945}
1946
[email protected]51a49502013-03-23 01:50:191947void RenderWidget::OnSnapshot(const gfx::Rect& src_subrect) {
1948 SkBitmap snapshot;
1949
1950 if (OnSnapshotHelper(src_subrect, &snapshot)) {
1951 Send(new ViewHostMsg_Snapshot(routing_id(), true, snapshot));
1952 } else {
1953 Send(new ViewHostMsg_Snapshot(routing_id(), false, SkBitmap()));
1954 }
1955}
1956
1957bool RenderWidget::OnSnapshotHelper(const gfx::Rect& src_subrect,
1958 SkBitmap* snapshot) {
1959 base::TimeTicks beginning_time = base::TimeTicks::Now();
1960
1961 if (!webwidget_ || src_subrect.IsEmpty())
1962 return false;
1963
1964 gfx::Rect viewport_size = gfx::IntersectRects(
1965 src_subrect, gfx::Rect(physical_backing_size_));
1966
1967 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
1968 skia::CreatePlatformCanvas(viewport_size.width(),
1969 viewport_size.height(),
1970 true,
1971 NULL,
1972 skia::RETURN_NULL_ON_FAILURE));
[email protected]59383c782013-04-17 16:43:271973 if (!canvas)
[email protected]51a49502013-03-23 01:50:191974 return false;
1975
1976 canvas->save();
1977 webwidget_->layout();
1978
1979 PaintRect(viewport_size, viewport_size.origin(), canvas.get());
1980 canvas->restore();
1981
1982 const SkBitmap& bitmap = skia::GetTopDevice(*canvas)->accessBitmap(false);
1983 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
1984 return false;
1985
1986 UMA_HISTOGRAM_TIMES("Renderer4.Snapshot",
1987 base::TimeTicks::Now() - beginning_time);
1988 return true;
1989}
1990
[email protected]0bc1f572013-04-17 01:46:311991void RenderWidget::OnRepaint(gfx::Size size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:121992 // During shutdown we can just ignore this message.
1993 if (!webwidget_)
1994 return;
1995
[email protected]0bc1f572013-04-17 01:46:311996 // Even if the browser provides an empty damage rect, it's still expecting to
1997 // receive a repaint ack so just damage the entire widget bounds.
1998 if (size_to_paint.IsEmpty()) {
1999 size_to_paint = size_;
2000 }
2001
[email protected]ec7dc112008-08-06 05:30:122002 set_next_paint_is_repaint_ack();
[email protected]0bc1f572013-04-17 01:46:312003 if (is_accelerated_compositing_active_ && compositor_) {
2004 compositor_->SetNeedsRedrawRect(gfx::Rect(size_to_paint));
[email protected]f98d7e3c2010-09-13 22:30:462005 } else {
2006 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
2007 didInvalidateRect(repaint_rect);
2008 }
[email protected]ec7dc112008-08-06 05:30:122009}
2010
[email protected]79fa22e2013-08-23 15:18:122011void RenderWidget::OnSyntheticGestureCompleted() {
2012 pending_synthetic_gesture_.Run();
[email protected]0e241b4b2012-08-18 09:06:272013}
2014
[email protected]4873c7d2009-07-16 06:36:282015void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:112016 if (!webwidget_)
2017 return;
[email protected]4873c7d2009-07-16 06:36:282018 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112019}
2020
[email protected]80ad8622012-11-07 16:33:032021void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
2022 const gfx::Rect& window_screen_rect) {
2023 view_screen_rect_ = view_screen_rect;
2024 window_screen_rect_ = window_screen_rect;
2025 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
2026}
2027
[email protected]105dffb42013-02-20 03:46:212028#if defined(OS_ANDROID)
2029void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
2030 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
2031}
[email protected]2384b6c2013-02-28 23:58:512032
2033void RenderWidget::OnShowImeIfNeeded() {
[email protected]0d1ebed12013-08-05 22:01:132034 UpdateTextInputState(true, true);
2035}
2036
2037void RenderWidget::IncrementOutstandingImeEventAcks() {
2038 ++outstanding_ime_acks_;
2039}
2040
2041void RenderWidget::OnImeEventAck() {
2042 --outstanding_ime_acks_;
2043 DCHECK(outstanding_ime_acks_ >= 0);
[email protected]2384b6c2013-02-28 23:58:512044}
[email protected]105dffb42013-02-20 03:46:212045#endif
2046
[email protected]0d1ebed12013-08-05 22:01:132047bool RenderWidget::ShouldHandleImeEvent() {
2048#if defined(OS_ANDROID)
2049 return !!webwidget_ && outstanding_ime_acks_ == 0;
2050#else
2051 return !!webwidget_;
2052#endif
2053}
2054
[email protected]468ac582012-11-20 00:53:192055void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
2056 if (device_scale_factor_ == device_scale_factor)
2057 return;
2058
2059 device_scale_factor_ = device_scale_factor;
2060
2061 if (!is_accelerated_compositing_active_) {
2062 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
2063 } else {
2064 scheduleComposite();
2065 }
2066}
2067
[email protected]adab2332013-07-25 18:04:322068PepperPluginInstanceImpl* RenderWidget::GetBitmapForOptimizedPluginPaint(
2069 const gfx::Rect& paint_bounds,
2070 TransportDIB** dib,
2071 gfx::Rect* location,
2072 gfx::Rect* clip,
2073 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:462074 // Bare RenderWidgets don't support optimized plugin painting.
2075 return NULL;
[email protected]ca4847f2010-09-24 05:39:152076}
2077
[email protected]ceb36f7d2012-10-31 18:33:242078gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:522079 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:242080 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:522081}
2082
[email protected]bee16aab2009-08-26 15:55:032083void RenderWidget::SetHidden(bool hidden) {
2084 if (is_hidden_ == hidden)
2085 return;
2086
2087 // The status has changed. Tell the RenderThread about it.
2088 is_hidden_ = hidden;
2089 if (is_hidden_)
[email protected]380244092011-10-07 17:26:272090 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:032091 else
[email protected]380244092011-10-07 17:26:272092 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:032093}
2094
[email protected]2b624c562011-10-27 22:58:262095void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262096 if (!webwidget_)
2097 return;
2098
2099 if (is_fullscreen_) {
2100 webwidget_->willExitFullScreen();
2101 } else {
2102 webwidget_->willEnterFullScreen();
2103 }
[email protected]2b624c562011-10-27 22:58:262104}
2105
2106void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262107 if (!webwidget_)
2108 return;
2109
2110 if (is_fullscreen_) {
2111 webwidget_->didEnterFullScreen();
2112 } else {
2113 webwidget_->didExitFullScreen();
2114 }
[email protected]2b624c562011-10-27 22:58:262115}
2116
[email protected]699ab0d2009-04-23 23:19:142117void RenderWidget::SetBackground(const SkBitmap& background) {
2118 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:462119
[email protected]699ab0d2009-04-23 23:19:142120 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:282121 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:142122}
2123
[email protected]674741932009-02-04 23:44:462124bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:052125 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462126}
2127
2128bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:052129 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462130}
2131
2132void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:052133 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:462134}
2135
2136void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:052137 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:462138}
2139
2140void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:052141 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:462142}
2143
[email protected]b18583c2012-12-18 06:55:272144static bool IsDateTimeInput(ui::TextInputType type) {
2145 return type == ui::TEXT_INPUT_TYPE_DATE ||
2146 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
2147 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
2148 type == ui::TEXT_INPUT_TYPE_MONTH ||
2149 type == ui::TEXT_INPUT_TYPE_TIME ||
2150 type == ui::TEXT_INPUT_TYPE_WEEK;
2151}
2152
[email protected]66fca5bc2013-05-23 06:58:292153
2154void RenderWidget::StartHandlingImeEvent() {
2155 DCHECK(!handling_ime_event_);
2156 handling_ime_event_ = true;
2157}
2158
2159void RenderWidget::FinishHandlingImeEvent() {
2160 DCHECK(handling_ime_event_);
2161 handling_ime_event_ = false;
2162 // While handling an ime event, text input state and selection bounds updates
2163 // are ignored. These must explicitly be updated once finished handling the
2164 // ime event.
2165 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472166#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132167 UpdateTextInputState(false, false);
[email protected]cb9e2632013-06-18 11:26:472168#endif
[email protected]66fca5bc2013-05-23 06:58:292169}
2170
[email protected]cb9e2632013-06-18 11:26:472171void RenderWidget::UpdateTextInputType() {
2172 if (!input_method_is_active_)
2173 return;
2174
[email protected]e7c569d2013-07-17 16:00:362175 ui::TextInputType new_type = GetTextInputType();
[email protected]cb9e2632013-06-18 11:26:472176 if (IsDateTimeInput(new_type))
2177 return; // Not considered as a text input field in WebKit/Chromium.
2178
2179 bool new_can_compose_inline = CanComposeInline();
2180
[email protected]e7c569d2013-07-17 16:00:362181 WebKit::WebTextInputInfo new_info;
2182 if (webwidget_)
2183 new_info = webwidget_->textInputInfo();
[email protected]b256eca2013-07-11 10:57:402184 const ui::TextInputMode new_mode = ConvertInputMode(new_info.inputMode);
2185
[email protected]cb9e2632013-06-18 11:26:472186 if (text_input_type_ != new_type
[email protected]b256eca2013-07-11 10:57:402187 || can_compose_inline_ != new_can_compose_inline
2188 || text_input_mode_ != new_mode) {
[email protected]cb9e2632013-06-18 11:26:472189 Send(new ViewHostMsg_TextInputTypeChanged(routing_id(),
2190 new_type,
[email protected]86ba5fcb2013-09-04 00:36:532191 new_mode,
2192 new_can_compose_inline));
[email protected]cb9e2632013-06-18 11:26:472193 text_input_type_ = new_type;
2194 can_compose_inline_ = new_can_compose_inline;
[email protected]b256eca2013-07-11 10:57:402195 text_input_mode_ = new_mode;
[email protected]cb9e2632013-06-18 11:26:472196 }
2197}
2198
2199#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132200void RenderWidget::UpdateTextInputState(bool show_ime_if_needed,
2201 bool send_ime_ack) {
[email protected]e8f775f2013-02-14 21:00:502202 if (handling_ime_event_)
2203 return;
[email protected]3306f262012-09-21 19:20:422204 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:292205 return;
[email protected]ad26ef42011-06-17 07:59:452206 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:272207 if (IsDateTimeInput(new_type))
2208 return; // Not considered as a text input field in WebKit/Chromium.
2209
[email protected]5b739cb2012-08-21 20:35:212210 WebKit::WebTextInputInfo new_info;
2211 if (webwidget_)
2212 new_info = webwidget_->textInputInfo();
2213
[email protected]ad26ef42011-06-17 07:59:452214 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212215
[email protected]3306f262012-09-21 19:20:422216 // Only sends text input params if they are changed or if the ime should be
2217 // shown.
2218 if (show_ime_if_needed || (text_input_type_ != new_type
2219 || text_input_info_ != new_info
2220 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212221 ViewHostMsg_TextInputState_Params p;
2222 p.type = new_type;
2223 p.value = new_info.value.utf8();
2224 p.selection_start = new_info.selectionStart;
2225 p.selection_end = new_info.selectionEnd;
2226 p.composition_start = new_info.compositionStart;
2227 p.composition_end = new_info.compositionEnd;
2228 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422229 p.show_ime_if_needed = show_ime_if_needed;
[email protected]0d1ebed12013-08-05 22:01:132230 p.require_ack = send_ime_ack;
2231 if (p.require_ack)
2232 IncrementOutstandingImeEventAcks();
[email protected]5b739cb2012-08-21 20:35:212233 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2234
2235 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042236 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452237 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292238 }
initial.commit09911bf2008-07-26 23:55:292239}
[email protected]cb9e2632013-06-18 11:26:472240#endif
initial.commit09911bf2008-07-26 23:55:292241
[email protected]7c8873e2013-02-05 08:03:012242void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2243 WebRect focus_webrect;
2244 WebRect anchor_webrect;
2245 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2246 *focus = focus_webrect;
2247 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322248}
2249
[email protected]e99ef6f2011-10-16 01:13:002250void RenderWidget::UpdateSelectionBounds() {
2251 if (!webwidget_)
2252 return;
[email protected]66fca5bc2013-05-23 06:58:292253 if (handling_ime_event_)
2254 return;
[email protected]e99ef6f2011-10-16 01:13:002255
[email protected]7c8873e2013-02-05 08:03:012256 ViewHostMsg_SelectionBounds_Params params;
2257 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2258 if (selection_anchor_rect_ != params.anchor_rect ||
2259 selection_focus_rect_ != params.focus_rect) {
2260 selection_anchor_rect_ = params.anchor_rect;
2261 selection_focus_rect_ = params.focus_rect;
2262 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292263 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012264 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352265 }
[email protected]501ea13d2013-07-09 17:03:292266#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362267 UpdateCompositionInfo(false);
[email protected]501ea13d2013-07-09 17:03:292268#endif
[email protected]e99ef6f2011-10-16 01:13:002269}
2270
[email protected]73bf95812011-10-12 11:38:322271// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452272COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2273 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2274COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2275 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2276COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2277 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182278COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2279 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2280COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2281 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2282COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2283 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2284COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2285 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2286COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2287 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002288COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2289 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2290COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2291 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2292COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2293 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2294COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2295 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2296COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2297 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2298COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2299 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012300COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2301 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2302COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2303 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152304COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2305 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452306
[email protected]5b739cb2012-08-21 20:35:212307ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2308 WebKit::WebTextInputType type) {
2309 // Check the type is in the range representable by ui::TextInputType.
2310 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2311 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2312 return static_cast<ui::TextInputType>(type);
2313}
2314
[email protected]ad26ef42011-06-17 07:59:452315ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272316 if (webwidget_)
2317 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452318 return ui::TEXT_INPUT_TYPE_NONE;
2319}
2320
[email protected]501ea13d2013-07-09 17:03:292321#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
2322void RenderWidget::UpdateCompositionInfo(bool should_update_range) {
[email protected]db4fc1e2013-09-06 20:01:512323 gfx::Range range = gfx::Range();
[email protected]501ea13d2013-07-09 17:03:292324 if (should_update_range) {
2325 GetCompositionRange(&range);
2326 } else {
2327 range = composition_range_;
2328 }
2329 std::vector<gfx::Rect> character_bounds;
2330 GetCompositionCharacterBounds(&character_bounds);
2331
2332 if (!ShouldUpdateCompositionInfo(range, character_bounds))
2333 return;
2334 composition_character_bounds_ = character_bounds;
2335 composition_range_ = range;
2336 Send(new ViewHostMsg_ImeCompositionRangeChanged(
2337 routing_id(), composition_range_, composition_character_bounds_));
2338}
2339
[email protected]58b48a0d2012-06-13 07:01:352340void RenderWidget::GetCompositionCharacterBounds(
2341 std::vector<gfx::Rect>* bounds) {
2342 DCHECK(bounds);
2343 bounds->clear();
2344}
2345
[email protected]db4fc1e2013-09-06 20:01:512346void RenderWidget::GetCompositionRange(gfx::Range* range) {
[email protected]88dbe32f2013-06-20 23:31:362347 size_t location, length;
2348 if (webwidget_->compositionRange(&location, &length)) {
2349 range->set_start(location);
2350 range->set_end(location + length);
2351 } else if (webwidget_->caretOrSelectionRange(&location, &length)) {
2352 range->set_start(location);
2353 range->set_end(location + length);
2354 } else {
[email protected]db4fc1e2013-09-06 20:01:512355 *range = gfx::Range::InvalidRange();
[email protected]88dbe32f2013-06-20 23:31:362356 }
2357}
2358
[email protected]501ea13d2013-07-09 17:03:292359bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512360 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292361 const std::vector<gfx::Rect>& bounds) {
2362 if (composition_range_ != range)
2363 return true;
2364 if (bounds.size() != composition_character_bounds_.size())
2365 return true;
2366 for (size_t i = 0; i < bounds.size(); ++i) {
2367 if (bounds[i] != composition_character_bounds_[i])
2368 return true;
2369 }
2370 return false;
2371}
2372#endif
2373
[email protected]ad26ef42011-06-17 07:59:452374bool RenderWidget::CanComposeInline() {
2375 return true;
[email protected]56ea1a62011-05-30 07:05:572376}
2377
[email protected]4873c7d2009-07-16 06:36:282378WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042379 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282380}
2381
[email protected]f660d9c2012-06-06 18:31:212382float RenderWidget::deviceScaleFactor() {
2383 return device_scale_factor_;
2384}
2385
[email protected]fa7b1dc2010-06-23 17:53:042386void RenderWidget::resetInputMethod() {
2387 if (!input_method_is_active_)
2388 return;
2389
[email protected]0e45bd02013-07-12 20:20:022390 ImeEventGuard guard(this);
[email protected]fa7b1dc2010-06-23 17:53:042391 // If the last text input type is not None, then we should finish any
2392 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452393 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042394 // If a composition text exists, then we need to let the browser process
2395 // to cancel the input method's ongoing composition session.
2396 if (webwidget_->confirmComposition())
2397 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2398 }
[email protected]d4cff272011-05-02 15:46:012399
[email protected]501ea13d2013-07-09 17:03:292400#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362401 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292402#endif
[email protected]fa7b1dc2010-06-23 17:53:042403}
2404
[email protected]c68c3e4e2013-01-24 00:36:562405void RenderWidget::didHandleGestureEvent(
2406 const WebGestureEvent& event,
2407 bool event_cancelled) {
2408#if defined(OS_ANDROID)
2409 if (event_cancelled)
2410 return;
2411 if (event.type == WebInputEvent::GestureTap ||
2412 event.type == WebInputEvent::GestureLongPress) {
[email protected]0d1ebed12013-08-05 22:01:132413 UpdateTextInputState(true, true);
[email protected]c68c3e4e2013-01-24 00:36:562414 }
2415#endif
2416}
2417
[email protected]29e2fb42013-07-19 01:13:472418void RenderWidget::SchedulePluginMove(const WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292419 size_t i = 0;
2420 for (; i < plugin_window_moves_.size(); ++i) {
2421 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582422 if (move.rects_valid) {
2423 plugin_window_moves_[i] = move;
2424 } else {
2425 plugin_window_moves_[i].visible = move.visible;
2426 }
initial.commit09911bf2008-07-26 23:55:292427 break;
2428 }
2429 }
2430
2431 if (i == plugin_window_moves_.size())
2432 plugin_window_moves_.push_back(move);
2433}
[email protected]268654772009-08-06 23:02:042434
2435void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2436 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2437 i != plugin_window_moves_.end(); ++i) {
2438 if (i->window == window) {
2439 plugin_window_moves_.erase(i);
2440 break;
2441 }
2442 }
2443}
[email protected]67bfb83f2011-09-22 03:36:372444
[email protected]b63d58d2012-11-26 22:37:442445void RenderWidget::GetRenderingStats(
2446 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282447 if (compositor_)
[email protected]635353c2013-03-06 09:11:202448 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442449
[email protected]c1e6cc062013-08-24 03:35:352450 stats.rendering_stats.Add(
2451 legacy_software_mode_stats_->GetRenderingStats());
[email protected]fef5e3972012-08-07 03:59:472452}
2453
[email protected]e9ff79c2012-10-19 21:31:262454bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522455 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2456 if (!gpu_channel)
2457 return false;
2458
2459 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2460}
2461
[email protected]24ed0432013-04-24 07:50:312462RenderWidgetCompositor* RenderWidget::compositor() const {
2463 return compositor_.get();
2464}
2465
[email protected]3639aa82013-06-04 11:00:042466void RenderWidget::OnSetBrowserRenderingStats(
2467 const BrowserRenderingStats& stats) {
2468 browser_rendering_stats_ = stats;
2469}
2470
2471void RenderWidget::GetBrowserRenderingStats(BrowserRenderingStats* stats) {
2472 *stats = browser_rendering_stats_;
2473}
2474
[email protected]0c2ebef2013-04-03 12:14:102475void RenderWidget::BeginSmoothScroll(
[email protected]0e241b4b2012-08-18 09:06:272476 bool down,
[email protected]79fa22e2013-08-23 15:18:122477 const SyntheticGestureCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192478 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292479 int mouse_event_x,
2480 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272481 DCHECK(!callback.is_null());
[email protected]267909d2012-10-20 04:36:192482
2483 ViewHostMsg_BeginSmoothScroll_Params params;
2484 params.scroll_down = down;
2485 params.pixels_to_scroll = pixels_to_scroll;
2486 params.mouse_event_x = mouse_event_x;
2487 params.mouse_event_y = mouse_event_y;
2488
[email protected]4a9dba42013-04-29 18:24:222489 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, params));
[email protected]79fa22e2013-08-23 15:18:122490 pending_synthetic_gesture_ = callback;
[email protected]a39ca1652012-07-13 21:30:582491}
2492
[email protected]1e1dd182013-09-12 01:51:152493void RenderWidget::BeginPinch(
2494 bool zoom_in,
2495 int pixels_to_move,
2496 int anchor_x,
2497 int anchor_y,
2498 const SyntheticGestureCompletionCallback& callback) {
2499 DCHECK(!callback.is_null());
2500
2501 ViewHostMsg_BeginPinch_Params params;
2502 params.zoom_in = zoom_in;
2503 params.pixels_to_move = pixels_to_move;
2504 params.anchor_x = anchor_x;
2505 params.anchor_y = anchor_y;
2506
2507 Send(new ViewHostMsg_BeginPinch(routing_id_, params));
2508 pending_synthetic_gesture_ = callback;
2509}
2510
[email protected]67bfb83f2011-09-22 03:36:372511bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2512 return false;
2513}
[email protected]c3d45532011-10-07 19:20:402514
[email protected]f56c7872013-06-18 12:31:572515bool RenderWidget::WillHandleKeyEvent(const WebKit::WebKeyboardEvent& event) {
2516 return false;
2517}
2518
[email protected]41d86852012-11-07 12:23:242519bool RenderWidget::WillHandleGestureEvent(
2520 const WebKit::WebGestureEvent& event) {
2521 return false;
2522}
2523
[email protected]ce6689f2013-03-29 12:52:552524void RenderWidget::hasTouchEventHandlers(bool has_handlers) {
2525 Send(new ViewHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
2526}
2527
[email protected]3d5c243b2012-11-30 00:26:012528bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2529 return true;
2530}
2531
[email protected]0634cdd42013-08-16 00:46:092532scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
2533RenderWidget::CreateGraphicsContext3D(
[email protected]92fd8c02013-03-29 08:54:152534 const WebKit::WebGraphicsContext3D::Attributes& attributes) {
[email protected]ed7defa2013-03-12 21:29:592535 if (!webwidget_)
[email protected]0634cdd42013-08-16 00:46:092536 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]ebc0e1df2013-08-01 02:46:222537 if (CommandLine::ForCurrentProcess()->HasSwitch(
2538 switches::kDisableGpuCompositing))
[email protected]0634cdd42013-08-16 00:46:092539 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]8f746982013-03-21 06:28:032540 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2541 new WebGraphicsContext3DCommandBufferImpl(
2542 surface_id(),
2543 GetURLForGraphicsContext3D(),
2544 RenderThreadImpl::current(),
2545 weak_ptr_factory_.GetWeakPtr()));
[email protected]ed7defa2013-03-12 21:29:592546
[email protected]b6eb8e332013-09-10 00:51:012547#if defined(OS_ANDROID)
2548 // If we raster too fast we become upload bound, and pending
2549 // uploads consume memory. For maximum upload throughput, we would
2550 // want to allow for upload_throughput * pipeline_time of pending
2551 // uploads, after which we are just wasting memory. Since we don't
2552 // know our upload throughput yet, this just caps our memory usage.
2553 size_t divider = 1;
2554 if (base::android::SysUtils::IsLowEndDevice())
[email protected]657be322013-09-20 08:50:032555 divider = 6;
[email protected]b6eb8e332013-09-10 00:51:012556 // For reference Nexus10 can upload 1MB in about 2.5ms.
[email protected]657be322013-09-20 08:50:032557 const double max_mb_uploaded_per_ms = 2.0 / (5 * divider);
[email protected]b6eb8e332013-09-10 00:51:012558 // Deadline to draw a frame to achieve 60 frames per second.
2559 const size_t kMillisecondsPerFrame = 16;
2560 // Assuming a two frame deep pipeline between the CPU and the GPU.
[email protected]657be322013-09-20 08:50:032561 size_t max_transfer_buffer_usage_mb =
2562 static_cast<size_t>(2 * kMillisecondsPerFrame * max_mb_uploaded_per_ms);
2563 static const size_t kBytesPerMegabyte = 1024 * 1024;
[email protected]b6eb8e332013-09-10 00:51:012564 // We keep the MappedMemoryReclaimLimit the same as the upload limit
2565 // to avoid unnecessarily stalling the compositor thread.
[email protected]657be322013-09-20 08:50:032566 const size_t mapped_memory_reclaim_limit =
2567 max_transfer_buffer_usage_mb * kBytesPerMegabyte;
[email protected]b6eb8e332013-09-10 00:51:012568#else
2569 const size_t mapped_memory_reclaim_limit =
2570 WebGraphicsContext3DCommandBufferImpl::kNoLimit;
2571#endif
2572 if (!context->Initialize(
[email protected]8f746982013-03-21 06:28:032573 attributes,
2574 false /* bind generates resources */,
[email protected]b6eb8e332013-09-10 00:51:012575 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
2576 kDefaultCommandBufferSize,
2577 kDefaultStartTransferBufferSize,
2578 kDefaultMinTransferBufferSize,
2579 kDefaultMaxTransferBufferSize,
2580 mapped_memory_reclaim_limit))
[email protected]0634cdd42013-08-16 00:46:092581 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
2582 return context.Pass();
[email protected]ed7defa2013-03-12 21:29:592583}
2584
[email protected]e9ff79c2012-10-19 21:31:262585} // namespace content