blob: 61bfe829fc26c43231d390dd16bacc3aacd2482f [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"
initial.commit09911bf2008-07-26 23:55:2913#include "base/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]ed7defa2013-03-12 21:29:5921#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]c084330e02013-04-27 01:08:1522#include "content/common/input_messages.h"
[email protected]992db4c2011-05-12 15:37:1523#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5024#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2925#include "content/public/common/content_switches.h"
[email protected]ed7defa2013-03-12 21:29:5926#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]2847b222013-04-06 00:59:2427#include "content/renderer/gpu/compositor_software_output_device.h"
[email protected]36e5ff12013-06-11 12:19:2928#include "content/renderer/gpu/delegated_compositor_output_surface.h"
[email protected]ea5f70a2013-03-07 12:30:3629#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5930#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2831#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]66fca5bc2013-05-23 06:58:2932#include "content/renderer/ime_event_guard.h"
[email protected]8704f89b2011-04-15 00:30:0533#include "content/renderer/render_process.h"
[email protected]fd33b59ff2013-06-21 22:57:0534#include "content/renderer/render_process_visibility_manager.h"
[email protected]f1a29a02011-10-06 23:08:4435#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1936#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1837#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4838#include "skia/ext/platform_canvas.h"
[email protected]2255a9332013-06-17 05:12:3139#include "third_party/WebKit/public/web/WebCursorInfo.h"
40#include "third_party/WebKit/public/web/WebHelperPlugin.h"
41#include "third_party/WebKit/public/web/WebPagePopup.h"
42#include "third_party/WebKit/public/web/WebPopupMenu.h"
43#include "third_party/WebKit/public/web/WebPopupMenuInfo.h"
44#include "third_party/WebKit/public/web/WebRange.h"
45#include "third_party/WebKit/public/web/WebScreenInfo.h"
[email protected]152fd7f72013-06-15 17:18:4146#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
47#include "third_party/WebKit/public/platform/WebPoint.h"
48#include "third_party/WebKit/public/platform/WebRect.h"
49#include "third_party/WebKit/public/platform/WebSize.h"
50#include "third_party/WebKit/public/platform/WebString.h"
[email protected]d353541f2012-05-03 22:45:4151#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1352#include "ui/base/ui_base_switches.h"
[email protected]9b003482013-05-21 14:00:1753#include "ui/gfx/point.h"
[email protected]a25e25b2012-09-28 14:32:3754#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3555#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4856#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2757#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4158#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3459#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5060#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4661#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]799fd732013-05-15 21:18:5262#include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h"
[email protected]152fd7f72013-06-15 17:18:4163#include "webkit/renderer/cursor_utils.h"
[email protected]661eb9d2009-02-03 02:11:4864
[email protected]eeb93112013-05-01 19:41:1065#if defined(OS_ANDROID)
[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]14392a52012-05-02 20:28:44192 bool swapped_out)
initial.commit09911bf2008-07-26 23:55:29193 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56194 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11195 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29196 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30197 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35198 current_paint_buf_(NULL),
[email protected]d9083762013-03-24 01:36:40199 overdraw_bottom_height_(0.f),
initial.commit09911bf2008-07-26 23:55:29200 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06201 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05202 update_reply_pending_(false),
[email protected]847a2582013-03-09 02:29:51203 auto_resize_mode_(false),
[email protected]ea3ee0a2012-05-15 03:43:09204 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24205 using_asynchronous_swapbuffers_(false),
206 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29207 did_show_(false),
initial.commit09911bf2008-07-26 23:55:29208 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:09209 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29210 needs_repainting_on_restore_(false),
211 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49212 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50213 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48214 closing_(false),
[email protected]14392a52012-05-02 20:28:44215 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04216 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45217 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
218 can_compose_inline_(true),
[email protected]b256eca2013-07-11 10:57:40219 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
[email protected]3e2b375b2010-04-07 17:03:12220 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48221 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12222 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50223 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21224 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07225 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04226 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52227 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]ed7defa2013-03-12 21:29:59228 is_threaded_compositing_enabled_(false),
[email protected]69e797f2013-04-30 01:10:22229 weak_ptr_factory_(this) {
[email protected]8b3f0eb2012-05-03 19:15:05230 if (!swapped_out)
231 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27232 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18233 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
234 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36235 is_threaded_compositing_enabled_ =
236 CommandLine::ForCurrentProcess()->HasSwitch(
237 switches::kEnableThreadedCompositing);
[email protected]fd33b59ff2013-06-21 22:57:05238
239 RenderProcessVisibilityManager::GetInstance()->WidgetVisibilityChanged(true);
initial.commit09911bf2008-07-26 23:55:29240}
241
242RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11243 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21244 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35245 if (current_paint_buf_) {
246 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
247 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29248 }
[email protected]992db4c2011-05-12 15:37:15249 // If we are swapped out, we have released already.
250 if (!is_swapped_out_)
251 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29252}
253
[email protected]484955942010-08-19 16:13:18254// static
[email protected]8085dbc82008-09-26 22:53:44255RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04256 WebKit::WebPopupType popup_type,
257 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29258 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41259 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04260 new RenderWidget(popup_type, screen_info, false));
[email protected]a635f942012-12-07 10:34:29261 if (widget->Init(opener_id)) { // adds reference on success.
[email protected]fc72bb12013-06-02 21:13:46262 return widget.get();
[email protected]a635f942012-12-07 10:34:29263 }
264 return NULL;
initial.commit09911bf2008-07-26 23:55:29265}
266
[email protected]484955942010-08-19 16:13:18267// static
268WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
269 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03270 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18271 break;
272 case WebKit::WebPopupTypeSelect:
273 case WebKit::WebPopupTypeSuggestion:
274 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44275 case WebKit::WebPopupTypePage:
276 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03277 case WebKit::WebPopupTypeHelperPlugin:
278 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18279 default:
280 NOTREACHED();
281 }
282 return NULL;
283}
284
[email protected]a635f942012-12-07 10:34:29285bool RenderWidget::Init(int32 opener_id) {
286 return DoInit(opener_id,
287 RenderWidget::CreateWebWidget(this),
288 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
289 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18290}
291
[email protected]a635f942012-12-07 10:34:29292bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06293 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18294 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29295 DCHECK(!webwidget_);
296
297 if (opener_id != MSG_ROUTING_NONE)
298 opener_id_ = opener_id;
299
[email protected]484955942010-08-19 16:13:18300 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29301
[email protected]380244092011-10-07 17:26:27302 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29303 if (result) {
[email protected]380244092011-10-07 17:26:27304 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29305 // Take a reference on behalf of the RenderThread. This will be balanced
306 // when we receive ViewMsg_Close.
307 AddRef();
[email protected]a635f942012-12-07 10:34:29308 return true;
initial.commit09911bf2008-07-26 23:55:29309 } else {
[email protected]a635f942012-12-07 10:34:29310 // The above Send can fail when the tab is closing.
311 return false;
initial.commit09911bf2008-07-26 23:55:29312 }
313}
314
[email protected]fc4404d2012-11-07 19:53:30315// This is used to complete pending inits and non-pending inits.
316void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29317 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29318
[email protected]fc4404d2012-11-07 19:53:30319 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42320
[email protected]8926c602013-01-23 05:32:06321 if (webwidget_ && is_threaded_compositing_enabled_) {
322 webwidget_->enterForceCompositingMode(true);
323 }
[email protected]ba91a792013-02-06 09:48:28324 if (compositor_) {
325 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09326 }
[email protected]05a980d7a2012-02-07 22:16:42327 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29328
[email protected]6de74452009-02-25 18:04:59329 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29330}
331
[email protected]992db4c2011-05-12 15:37:15332void RenderWidget::SetSwappedOut(bool is_swapped_out) {
333 // We should only toggle between states.
334 DCHECK(is_swapped_out_ != is_swapped_out);
335 is_swapped_out_ = is_swapped_out;
336
337 // If we are swapping out, we will call ReleaseProcess, allowing the process
338 // to exit if all of its RenderViews are swapped out. We wait until the
339 // WasSwappedOut call to do this, to avoid showing the sad tab.
340 // If we are swapping in, we call AddRefProcess to prevent the process from
341 // exiting.
342 if (!is_swapped_out)
343 RenderProcess::current()->AddRefProcess();
344}
345
[email protected]34bb3ac2013-03-08 02:41:28346bool RenderWidget::AllowPartialSwap() const {
347 return true;
348}
349
[email protected]c8cbae72013-05-23 10:45:03350bool RenderWidget::UsingSynchronousRendererCompositor() const {
[email protected]ccc1722e2013-05-06 19:43:07351#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07352 return SynchronousCompositorFactory::GetInstance() != NULL;
353#else
[email protected]ccc1722e2013-05-06 19:43:07354 return false;
[email protected]913d99a2013-05-31 07:16:07355#endif
[email protected]ccc1722e2013-05-06 19:43:07356}
357
[email protected]a95986a82010-12-24 06:19:28358bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
359 bool handled = true;
360 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
[email protected]c084330e02013-04-27 01:08:15361 IPC_MESSAGE_HANDLER(InputMsg_HandleInputEvent, OnHandleInputEvent)
[email protected]34202de2013-05-06 23:36:22362 IPC_MESSAGE_HANDLER(InputMsg_CursorVisibilityChange,
363 OnCursorVisibilityChange)
[email protected]c084330e02013-04-27 01:08:15364 IPC_MESSAGE_HANDLER(InputMsg_MouseCaptureLost, OnMouseCaptureLost)
365 IPC_MESSAGE_HANDLER(InputMsg_SetFocus, OnSetFocus)
[email protected]a95986a82010-12-24 06:19:28366 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
367 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
368 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54369 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28370 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41371 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15372 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28373 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59374 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
375 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28376 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
377 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
378 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25379 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
380 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
381 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted, OnSmoothScrollCompleted)
[email protected]a95986a82010-12-24 06:19:28382 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
383 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]80ad8622012-11-07 16:33:03384 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21385#if defined(OS_ANDROID)
386 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
[email protected]2384b6c2013-02-28 23:58:51387 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]105dffb42013-02-20 03:46:21388#endif
[email protected]51a49502013-03-23 01:50:19389 IPC_MESSAGE_HANDLER(ViewMsg_Snapshot, OnSnapshot)
[email protected]3639aa82013-06-04 11:00:04390 IPC_MESSAGE_HANDLER(ViewMsg_SetBrowserRenderingStats,
391 OnSetBrowserRenderingStats)
[email protected]a95986a82010-12-24 06:19:28392 IPC_MESSAGE_UNHANDLED(handled = false)
393 IPC_END_MESSAGE_MAP()
394 return handled;
395}
initial.commit09911bf2008-07-26 23:55:29396
397bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15398 // Don't send any messages after the browser has told us to close, and filter
399 // most outgoing messages while swapped out.
400 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26401 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11402 closing_) {
initial.commit09911bf2008-07-26 23:55:29403 delete message;
404 return false;
405 }
406
407 // If given a messsage without a routing ID, then assign our routing ID.
408 if (message->routing_id() == MSG_ROUTING_NONE)
409 message->set_routing_id(routing_id_);
410
[email protected]380244092011-10-07 17:26:27411 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44412}
413
[email protected]61e2b3cc2012-03-02 16:13:34414void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44415 const gfx::Size& physical_backing_size,
[email protected]d9083762013-03-24 01:36:40416 float overdraw_bottom_height,
[email protected]61e2b3cc2012-03-02 16:13:34417 const gfx::Rect& resizer_rect,
418 bool is_fullscreen,
419 ResizeAck resize_ack) {
[email protected]1c0008842013-06-06 08:35:48420 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
421 !RenderThreadImpl::current()->layout_test_mode()) {
422 // A resize ack shouldn't be requested if we have not ACK'd the previous
423 // one.
424 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
425 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
426 }
initial.commit09911bf2008-07-26 23:55:29427
[email protected]61e2b3cc2012-03-02 16:13:34428 // Ignore this during shutdown.
429 if (!webwidget_)
430 return;
431
[email protected]d9083762013-03-24 01:36:40432 if (compositor_) {
[email protected]60d47ac2013-03-01 23:42:44433 compositor_->setViewportSize(new_size, physical_backing_size);
[email protected]d9083762013-03-24 01:36:40434 compositor_->SetOverdrawBottomHeight(overdraw_bottom_height);
435 }
[email protected]60d47ac2013-03-01 23:42:44436
[email protected]dade8992013-03-04 07:34:34437 physical_backing_size_ = physical_backing_size;
[email protected]d9083762013-03-24 01:36:40438 overdraw_bottom_height_ = overdraw_bottom_height;
[email protected]61e2b3cc2012-03-02 16:13:34439 resizer_rect_ = resizer_rect;
440
441 // NOTE: We may have entered fullscreen mode without changing our size.
442 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
443 if (fullscreen_change)
444 WillToggleFullscreen();
445 is_fullscreen_ = is_fullscreen;
446
447 if (size_ != new_size) {
448 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34449 needs_repainting_on_restore_ = false;
450
451 size_ = new_size;
452
453 paint_aggregator_.ClearPendingUpdate();
454
455 // When resizing, we want to wait to paint before ACK'ing the resize. This
456 // ensures that we only resize as fast as we can paint. We only need to
457 // send an ACK if we are resized to a non-empty rect.
458 webwidget_->resize(new_size);
[email protected]0b70dbe2013-05-10 19:06:32459
[email protected]92201e32013-06-08 06:14:52460 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
461 !RenderThreadImpl::current()->layout_test_mode()) {
462 // Resize should have caused an invalidation of the entire view.
463 DCHECK(new_size.IsEmpty() || is_accelerated_compositing_active_ ||
464 paint_aggregator_.HasPendingUpdate());
465 }
[email protected]1c0008842013-06-06 08:35:48466 } else if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
467 !RenderThreadImpl::current()->layout_test_mode()) {
[email protected]632c4382013-05-15 08:58:45468 resize_ack = NO_RESIZE_ACK;
469 }
470
471 if (new_size.IsEmpty() || physical_backing_size.IsEmpty()) {
472 // For empty size or empty physical_backing_size, there is no next paint
473 // (along with which to send the ack) until they are set to non-empty.
[email protected]ff475a322012-03-14 00:05:35474 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34475 }
476
[email protected]20fbfc22013-05-08 20:50:58477 // Send the Resize_ACK flag once we paint again if requested.
[email protected]632c4382013-05-15 08:58:45478 if (resize_ack == SEND_RESIZE_ACK)
[email protected]20fbfc22013-05-08 20:50:58479 set_next_paint_is_resize_ack();
480
[email protected]61e2b3cc2012-03-02 16:13:34481 if (fullscreen_change)
482 DidToggleFullscreen();
483
484 // If a resize ack is requested and it isn't set-up, then no more resizes will
485 // come in and in general things will go wrong.
[email protected]632c4382013-05-15 08:58:45486 DCHECK(resize_ack != SEND_RESIZE_ACK || next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29487}
488
489void RenderWidget::OnClose() {
490 if (closing_)
491 return;
492 closing_ = true;
493
494 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03495 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27496 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03497 SetHidden(false);
498 }
initial.commit09911bf2008-07-26 23:55:29499
initial.commit09911bf2008-07-26 23:55:29500 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25501 // now. Post a task that only gets invoked when there are no nested message
502 // loops.
[email protected]dd32b1272013-05-04 14:17:11503 base::MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29504 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25505
506 // Balances the AddRef taken when we called AddRoute.
507 Release();
initial.commit09911bf2008-07-26 23:55:29508}
509
[email protected]61e2b3cc2012-03-02 16:13:34510// Got a response from the browser after the renderer decided to create a new
511// view.
[email protected]fc4404d2012-11-07 19:53:30512void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34513 DCHECK(routing_id_ != MSG_ROUTING_NONE);
514
[email protected]fc4404d2012-11-07 19:53:30515 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34516}
517
[email protected]0fdd5012013-05-29 08:05:56518void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) {
519 screen_info_ = params.screen_info;
520 SetDeviceScaleFactor(screen_info_.deviceScaleFactor);
521 Resize(params.new_size, params.physical_backing_size,
522 params.overdraw_bottom_height, params.resizer_rect,
523 params.is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29524}
525
[email protected]b5913d72012-02-07 22:26:54526void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
527 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59528 gfx::Rect view_rect(size_);
529
[email protected]ce112fe2012-10-29 22:52:18530 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59531 if (!old_damage_rect.IsEmpty())
532 paint_aggregator_.InvalidateRect(old_damage_rect);
533
[email protected]ce112fe2012-10-29 22:52:18534 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59535 if (!new_damage_rect.IsEmpty())
536 paint_aggregator_.InvalidateRect(new_damage_rect);
537
[email protected]b5913d72012-02-07 22:26:54538 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59539
[email protected]b5913d72012-02-07 22:26:54540 if (webwidget_)
541 webwidget_->didChangeWindowResizerRect();
542 }
543}
544
initial.commit09911bf2008-07-26 23:55:29545void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31546 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29547 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03548 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29549}
550
[email protected]9e2e4632012-07-27 16:38:41551void RenderWidget::OnWasShown(bool needs_repainting) {
552 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29553 // During shutdown we can just ignore this message.
554 if (!webwidget_)
555 return;
556
557 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03558 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29559
560 if (!needs_repainting && !needs_repainting_on_restore_)
561 return;
562 needs_repainting_on_restore_ = false;
563
[email protected]d65adb12010-04-28 17:26:49564 // Tag the next paint as a restore ack, which is picked up by
565 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29566 set_next_paint_is_restore_ack();
567
568 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56569 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46570 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
571 } else {
572 scheduleComposite();
573 }
initial.commit09911bf2008-07-26 23:55:29574}
575
[email protected]992db4c2011-05-12 15:37:15576void RenderWidget::OnWasSwappedOut() {
577 // If we have been swapped out and no one else is using this process,
578 // it's safe to exit now. If we get swapped back in, we will call
579 // AddRefProcess in SetSwappedOut.
580 if (is_swapped_out_)
581 RenderProcess::current()->ReleaseProcess();
582}
583
[email protected]53d3f302009-12-21 04:42:05584void RenderWidget::OnRequestMoveAck() {
585 DCHECK(pending_window_rect_count_);
586 pending_window_rect_count_--;
587}
588
589void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58590 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21591 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05592 update_reply_pending_ = false;
593
[email protected]b4d08452010-10-05 17:34:35594 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
595 // have no current paint buffer.
596 if (current_paint_buf_) {
597 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
598 current_paint_buf_ = NULL;
599 }
600
[email protected]65225772011-05-12 21:10:24601 // If swapbuffers is still pending, then defer the update until the
602 // swapbuffers occurs.
603 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
604 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
605 return;
606 }
607
[email protected]29ed96a2012-02-04 18:12:16608 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18609 if (!is_accelerated_compositing_active_) {
610 DidFlushPaint();
611 }
[email protected]a2f6bc112009-06-27 16:27:25612
initial.commit09911bf2008-07-26 23:55:29613 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24614 DoDeferredUpdateAndSendInputAck();
615}
616
[email protected]d0be63772011-12-20 23:18:04617bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59618 // Contexts using the command buffer support asynchronous swapbuffers.
619 // See RenderWidget::CreateOutputSurface().
[email protected]cadac622013-06-11 16:46:36620 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get())
[email protected]ed7defa2013-03-12 21:29:59621 return false;
622
623 return true;
624}
625
626GURL RenderWidget::GetURLForGraphicsContext3D() {
627 return GURL();
[email protected]65225772011-05-12 21:10:24628}
629
[email protected]479b0172012-10-29 19:27:09630bool RenderWidget::ForceCompositingModeEnabled() {
631 return false;
632}
633
[email protected]ba91a792013-02-06 09:48:28634scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface() {
[email protected]c3e32ed42013-05-02 05:07:13635 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1811b8912013-05-09 15:35:19636
637#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07638 if (SynchronousCompositorFactory* factory =
639 SynchronousCompositorFactory::GetInstance()) {
640 return factory->CreateOutputSurface(routing_id());
[email protected]a1811b8912013-05-09 15:35:19641 }
642#endif
643
[email protected]8bbe3a92013-05-12 00:58:35644 if (command_line.HasSwitch(switches::kEnableSoftwareCompositingGLAdapter)) {
645 return scoped_ptr<cc::OutputSurface>(
646 new CompositorOutputSurface(routing_id(), NULL,
[email protected]36e5ff12013-06-11 12:19:29647 new CompositorSoftwareOutputDevice(), true));
[email protected]8bbe3a92013-05-12 00:58:35648 }
649
[email protected]ed7defa2013-03-12 21:29:59650 // Explicitly disable antialiasing for the compositor. As of the time of
651 // this writing, the only platform that supported antialiasing for the
652 // compositor was Mac OS X, because the on-screen OpenGL context creation
653 // code paths on Windows and Linux didn't yet have multisampling support.
654 // Mac OS X essentially always behaves as though it's rendering offscreen.
655 // Multisampling has a heavy cost especially on devices with relatively low
656 // fill rate like most notebooks, and the Mac implementation would need to
657 // be optimized to resolve directly into the IOSurface shared between the
658 // GPU and browser processes. For these reasons and to avoid platform
659 // disparities we explicitly disable antialiasing.
660 WebKit::WebGraphicsContext3D::Attributes attributes;
661 attributes.antialias = false;
662 attributes.shareResources = true;
663 attributes.noAutomaticFlushes = true;
[email protected]a6886502013-05-16 20:59:18664 attributes.depth = false;
665 attributes.stencil = false;
[email protected]bec084292013-05-21 21:31:44666 if (command_line.HasSwitch(cc::switches::kForceDirectLayerDrawing))
667 attributes.stencil = true;
[email protected]3ae68c52013-04-12 06:10:05668 WebGraphicsContext3DCommandBufferImpl* context =
669 CreateGraphicsContext3D(attributes);
[email protected]8bbe3a92013-05-12 00:58:35670 if (!context)
671 return scoped_ptr<cc::OutputSurface>();
[email protected]ed7defa2013-03-12 21:29:59672
[email protected]0c04d1c2013-07-10 00:02:32673 if (command_line.HasSwitch(switches::kEnableDelegatedRenderer) &&
674 !command_line.HasSwitch(switches::kDisableDelegatedRenderer)) {
[email protected]36e5ff12013-06-11 12:19:29675 DCHECK(is_threaded_compositing_enabled_);
676 return scoped_ptr<cc::OutputSurface>(
677 new DelegatedCompositorOutputSurface(routing_id(), context, NULL));
678 }
679 if (command_line.HasSwitch(cc::switches::kCompositeToMailbox)) {
680 DCHECK(is_threaded_compositing_enabled_);
681 return scoped_ptr<cc::OutputSurface>(
682 new MailboxOutputSurface(routing_id(), context, NULL));
683 }
684 return scoped_ptr<cc::OutputSurface>(
685 new CompositorOutputSurface(routing_id(), context, NULL, false));
[email protected]ba91a792013-02-06 09:48:28686}
687
[email protected]ed7defa2013-03-12 21:29:59688void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24689 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21690 while (!updates_pending_swap_.empty()) {
691 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
692 updates_pending_swap_.pop_front();
693 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
694 // compositing pass, hence doesn't require an UpdateRect message.
695 if (msg)
696 Send(msg);
697 }
[email protected]65225772011-05-12 21:10:24698 num_swapbuffers_complete_pending_ = 0;
699 using_asynchronous_swapbuffers_ = false;
700 // Schedule another frame so the compositor learns about it.
701 scheduleComposite();
702}
703
[email protected]ed7defa2013-03-12 21:29:59704void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08705 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21706
707 if (using_asynchronous_swapbuffers_) {
708 ViewHostMsg_UpdateRect* msg = NULL;
709 // pending_update_params_ can be NULL if the swap doesn't correspond to an
710 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
711 // message.
[email protected]59383c782013-04-17 16:43:27712 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:21713 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
714 pending_update_params_.reset();
715 }
716 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08717 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21718 }
[email protected]37a6f302011-07-11 23:43:08719}
720
[email protected]ed7defa2013-03-12 21:29:59721void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24722 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16723
[email protected]404939f2012-06-01 04:06:18724 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16725 DidFlushPaint();
726
[email protected]65225772011-05-12 21:10:24727 // When compositing deactivates, we reset the swapbuffers pending count. The
728 // swapbuffers acks may still arrive, however.
729 if (num_swapbuffers_complete_pending_ == 0) {
730 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
731 return;
732 }
[email protected]aa4117f2011-12-09 22:19:21733 DCHECK(!updates_pending_swap_.empty());
734 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
735 updates_pending_swap_.pop_front();
736 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
737 // compositing pass, hence doesn't require an UpdateRect message.
738 if (msg)
739 Send(msg);
[email protected]65225772011-05-12 21:10:24740 num_swapbuffers_complete_pending_--;
741
742 // If update reply is still pending, then defer the update until that reply
743 // occurs.
[email protected]d0be63772011-12-20 23:18:04744 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24745 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
746 return;
747 }
748
749 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06750 // when we were previously rendering. However, if an invalidation task is not
751 // posted, there may be software rendering work pending. In that case, don't
752 // early out.
753 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24754 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
755 return;
756 }
757
[email protected]cc66e682012-10-02 06:48:18758 // Do not call DoDeferredUpdate unless there's animation work to be done or
759 // a real invalidation. This prevents rendering in response to a swapbuffers
760 // callback coming back after we've navigated away from the page that
761 // generated it.
762 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
763 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
764 return;
765 }
766
[email protected]65225772011-05-12 21:10:24767 // Continue painting if necessary...
768 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29769}
770
[email protected]0dea1652012-12-14 00:09:09771void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
[email protected]4b157662013-05-29 04:05:05772 const ui::LatencyInfo& latency_info,
[email protected]0dea1652012-12-14 00:09:09773 bool is_keyboard_shortcut) {
[email protected]5dd768212009-08-13 23:34:49774 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09775 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49776 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29777 return;
[email protected]5dd768212009-08-13 23:34:49778 }
initial.commit09911bf2008-07-26 23:55:29779
[email protected]b4841e1c2013-05-16 22:30:10780 const char* const event_name = GetEventName(input_event->type);
781 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent",
782 "event", event_name);
783
[email protected]c2eaa8f2013-05-10 02:41:55784 if (compositor_)
785 compositor_->SetLatencyInfo(latency_info);
[email protected]256737c2013-06-08 04:39:10786 else
787 latency_info_.MergeWith(latency_info);
[email protected]c2eaa8f2013-05-10 02:41:55788
[email protected]6a4d7f62013-01-07 21:32:13789 base::TimeDelta now = base::TimeDelta::FromInternalValue(
790 base::TimeTicks::Now().ToInternalValue());
791
792 int64 delta = static_cast<int64>(
793 (now.InSecondsF() - input_event->timeStampSeconds) *
794 base::Time::kMicrosecondsPerSecond);
795 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
[email protected]de415552013-01-23 04:12:17796 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:48797 base::Histogram::FactoryGet(
[email protected]b4841e1c2013-05-16 22:30:10798 base::StringPrintf("Event.Latency.Renderer.%s", event_name),
[email protected]bafdc5d52013-02-27 18:18:48799 0,
800 1000000,
[email protected]6a4d7f62013-01-07 21:32:13801 100,
[email protected]de415552013-01-23 04:12:17802 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:48803 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:13804
[email protected]67bfb83f2011-09-22 03:36:37805 bool prevent_default = false;
806 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26807 const WebMouseEvent& mouse_event =
808 *static_cast<const WebMouseEvent*>(input_event);
809 TRACE_EVENT2("renderer", "HandleMouseMove",
810 "x", mouse_event.x, "y", mouse_event.y);
811 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37812 }
813
[email protected]f56c7872013-06-18 12:31:57814 if (WebInputEvent::isKeyboardEventType(input_event->type)) {
815 const WebKeyboardEvent& key_event =
816 *static_cast<const WebKeyboardEvent*>(input_event);
817 prevent_default = WillHandleKeyEvent(key_event);
818 }
819
[email protected]41d86852012-11-07 12:23:24820 if (WebInputEvent::isGestureEventType(input_event->type)) {
821 const WebGestureEvent& gesture_event =
822 *static_cast<const WebGestureEvent*>(input_event);
823 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
824 }
825
[email protected]3ebcc7c2013-01-09 05:34:46826 if (input_event->type == WebInputEvent::GestureTap ||
827 input_event->type == WebInputEvent::GestureLongPress)
828 resetInputMethod();
829
[email protected]67bfb83f2011-09-22 03:36:37830 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12831 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
832 suppress_next_char_events_ = false;
833 if (!processed && webwidget_)
834 processed = webwidget_->handleInputEvent(*input_event);
835 }
836
837 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
838 // it's not processed by webkit, then we need to suppress the upcoming Char
839 // events.
840 if (!processed && is_keyboard_shortcut)
841 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29842
[email protected]3d5c243b2012-11-30 00:26:01843 InputEventAckState ack_result = processed ?
844 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
845 if (!processed && input_event->type == WebInputEvent::TouchStart) {
846 const WebTouchEvent& touch_event =
847 *static_cast<const WebTouchEvent*>(input_event);
848 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
849 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
850 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
851 }
852
[email protected]a9fb30aa2011-10-06 06:58:46853 IPC::Message* response =
[email protected]c084330e02013-04-27 01:08:15854 new InputHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
855 ack_result);
[email protected]3391a0772012-03-28 00:32:07856 bool event_type_gets_rate_limited =
857 input_event->type == WebInputEvent::MouseMove ||
858 input_event->type == WebInputEvent::MouseWheel ||
859 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06860
861 bool frame_pending = paint_aggregator_.HasPendingUpdate();
862 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28863 frame_pending = compositor_ &&
864 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06865 }
866
[email protected]9a8ce7f92013-06-11 12:39:49867 if (event_type_gets_rate_limited && frame_pending && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24868 // We want to rate limit the input events in this case, so we'll wait for
869 // painting to finish before ACKing this message.
[email protected]59383c782013-04-17 16:43:27870 if (pending_input_event_ack_) {
[email protected]353a34c2010-05-28 23:35:17871 // As two different kinds of events could cause us to postpone an ack
872 // we send it now, if we have one pending. The Browser should never
873 // send us the same kind of event we are delaying the ack for.
874 Send(pending_input_event_ack_.release());
875 }
[email protected]12fbad812009-09-01 18:21:24876 pending_input_event_ack_.reset(response);
877 } else {
878 Send(response);
879 }
880
[email protected]3306f262012-09-21 19:20:42881#if defined(OS_ANDROID)
882 // Allow the IME to be shown when the focus changes as a consequence
883 // of a processed touch end event.
884 if (input_event->type == WebInputEvent::TouchEnd && processed)
885 UpdateTextInputState(SHOW_IME_IF_NEEDED);
886#endif
887
[email protected]5dd768212009-08-13 23:34:49888 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48889
[email protected]67bfb83f2011-09-22 03:36:37890 if (!prevent_default) {
891 if (WebInputEvent::isKeyboardEventType(input_event->type))
892 DidHandleKeyEvent();
893 if (WebInputEvent::isMouseEventType(input_event->type))
894 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24895 if (WebInputEvent::isTouchEventType(input_event->type))
896 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37897 }
initial.commit09911bf2008-07-26 23:55:29898}
899
[email protected]34202de2013-05-06 23:36:22900void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
901 if (webwidget_)
902 webwidget_->setCursorVisibilityState(is_visible);
903}
904
initial.commit09911bf2008-07-26 23:55:29905void RenderWidget::OnMouseCaptureLost() {
906 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28907 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29908}
909
910void RenderWidget::OnSetFocus(bool enable) {
911 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33912 if (webwidget_)
913 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29914}
915
916void RenderWidget::ClearFocus() {
917 // We may have got the focus from the browser before this gets processed, in
918 // which case we do not want to unfocus ourself.
919 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28920 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29921}
922
[email protected]2d5d09d52009-06-15 14:29:21923void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00924 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21925 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06926 TRACE_EVENT2("renderer", "PaintRect",
927 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45928
929 const bool kEnableGpuBenchmarking =
930 CommandLine::ForCurrentProcess()->HasSwitch(
931 switches::kEnableGpuBenchmarking);
[email protected]4fb66842009-12-04 21:41:00932 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21933
934 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00935 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
936 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03937
[email protected]699ab0d2009-04-23 23:19:14938 // If there is a custom background, tile it.
939 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14940 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11941 skia::RefPtr<SkShader> shader = skia::AdoptRef(
942 SkShader::CreateBitmapShader(background_,
943 SkShader::kRepeat_TileMode,
944 SkShader::kRepeat_TileMode));
945 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20946
947 // Use kSrc_Mode to handle background_ transparency properly.
948 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
949
950 // Canvas could contain multiple update rects. Clip to given rect so that
951 // we don't accidentally clear other update rects.
952 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44953 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48954 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14955 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20956 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14957 }
958
[email protected]719b36f2010-12-22 20:36:46959 // First see if this rect is a plugin that can paint itself faster.
960 TransportDIB* optimized_dib = NULL;
961 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20962 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46963 webkit::ppapi::PluginInstance* optimized_instance =
964 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
965 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20966 &optimized_copy_rect,
967 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46968 if (optimized_instance) {
969 // This plugin can be optimize-painted and we can just ask it to paint
970 // itself. We don't actually need the TransportDIB in this case.
971 //
972 // This is an optimization for PPAPI plugins that know they're on top of
973 // the page content. If this rect is inside such a plugin, we can save some
974 // time and avoid re-rendering the page content which we know will be
975 // covered by the plugin later (this time can be significant, especially
976 // for a playing movie that is invalidating a lot).
977 //
978 // In the plugin movie case, hopefully the similar call to
979 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
980 // painting, because that avoids copying the plugin image to a different
981 // paint rect. Unfortunately, if anything on the page is animating other
982 // than the movie, it break this optimization since the union of the
983 // invalid regions will be larger than the plugin.
984 //
985 // This code optimizes that case, where we can still avoid painting in
986 // WebKit and filling the background (which can be slow) and just painting
987 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
988 // required.
[email protected]63ab54262012-11-09 15:58:45989 base::TimeTicks paint_begin_ticks;
990 if (kEnableGpuBenchmarking)
991 paint_begin_ticks = base::TimeTicks::HighResNow();
992
[email protected]df59dd42012-09-14 22:56:30993 SkAutoCanvasRestore auto_restore(canvas, true);
994 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:46995 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27996 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:21997 canvas->restore();
[email protected]63ab54262012-11-09 15:58:45998 if (kEnableGpuBenchmarking) {
999 base::TimeDelta paint_time =
1000 base::TimeTicks::HighResNow() - paint_begin_ticks;
1001 if (!is_accelerated_compositing_active_)
[email protected]62049562013-03-24 00:39:011002 software_stats_.total_paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451003 }
[email protected]719b36f2010-12-22 20:36:461004 } else {
1005 // Normal painting case.
[email protected]63ab54262012-11-09 15:58:451006 base::TimeTicks paint_begin_ticks;
1007 if (kEnableGpuBenchmarking)
1008 paint_begin_ticks = base::TimeTicks::HighResNow();
1009
[email protected]719b36f2010-12-22 20:36:461010 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]63ab54262012-11-09 15:58:451011
1012 if (kEnableGpuBenchmarking) {
1013 base::TimeDelta paint_time =
1014 base::TimeTicks::HighResNow() - paint_begin_ticks;
1015 if (!is_accelerated_compositing_active_)
[email protected]62049562013-03-24 00:39:011016 software_stats_.total_paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451017 }
[email protected]719b36f2010-12-22 20:36:461018
1019 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:351020 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:461021 }
initial.commit09911bf2008-07-26 23:55:291022
[email protected]4fb66842009-12-04 21:41:001023 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:001024 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451025
1026 if (kEnableGpuBenchmarking) {
[email protected]63ab54262012-11-09 15:58:451027 int64 num_pixels_processed = rect.width() * rect.height();
[email protected]62049562013-03-24 00:39:011028 software_stats_.total_pixels_painted += num_pixels_processed;
[email protected]63ab54262012-11-09 15:58:451029 }
[email protected]4fb66842009-12-04 21:41:001030}
1031
1032void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
1033 skia::PlatformCanvas* canvas) {
1034 static bool kPaintBorder =
1035 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
1036 if (!kPaintBorder)
1037 return;
1038
[email protected]53d3f302009-12-21 04:42:051039 // Cycle through these colors to help distinguish new paint rects.
1040 const SkColor colors[] = {
1041 SkColorSetARGB(0x3F, 0xFF, 0, 0),
1042 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
1043 SkColorSetARGB(0x3F, 0, 0, 0xFF),
1044 };
1045 static int color_selector = 0;
1046
[email protected]4fb66842009-12-04 21:41:001047 SkPaint paint;
1048 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:051049 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:001050 paint.setStrokeWidth(1);
1051
1052 SkIRect irect;
1053 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
1054 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:291055}
1056
[email protected]52ccd0ea2011-02-16 01:09:051057void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:301058 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:301059 if (!animation_update_pending_) {
1060 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:591061 return;
[email protected]921244e42011-07-20 16:36:301062 }
[email protected]bd37ae252011-06-03 01:28:181063 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:591064 // Record when we fired (according to base::Time::Now()) relative to when
1065 // we posted the task to quantify how much the base::Time/base::TimeTicks
1066 // skew is affecting animations.
1067 base::TimeDelta animation_callback_delay = base::Time::Now() -
1068 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
1069 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
1070 animation_callback_delay,
1071 base::TimeDelta::FromMilliseconds(0),
1072 base::TimeDelta::FromMilliseconds(30),
1073 25);
1074 }
[email protected]65225772011-05-12 21:10:241075 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:241076}
1077
[email protected]52ccd0ea2011-02-16 01:09:051078void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:591079 if (!animation_update_pending_)
1080 return;
[email protected]bd37ae252011-06-03 01:28:181081
1082 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:331083 base::TimeDelta animationInterval = IsRenderingVSynced() ?
1084 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:181085
[email protected]7c4329e2011-02-18 22:02:591086 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:451087
1088 // animation_floor_time_ is the earliest time that we should animate when
1089 // using the dead reckoning software scheduler. If we're using swapbuffers
1090 // complete callbacks to rate limit, we can ignore this floor.
1091 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:301092 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:331093 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:181094 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:591095 // running animation callbacks so that if a callback requests another
1096 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:381097 animation_timer_.Stop();
1098 animation_timer_.Start(FROM_HERE, animationInterval, this,
1099 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:591100 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:281101 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:201102 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:061103 } else {
[email protected]635353c2013-03-06 09:11:201104 double frame_begin_time =
1105 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
1106 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061107 }
[email protected]7c4329e2011-02-18 22:02:591108 return;
[email protected]5f8b1022011-01-21 23:34:501109 }
[email protected]bd37ae252011-06-03 01:28:181110 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381111 if (!animation_timer_.IsRunning()) {
1112 // This code uses base::Time::Now() to calculate the floor and next fire
1113 // time because javascript's Date object uses base::Time::Now(). The
1114 // message loop uses base::TimeTicks, which on windows can have a
1115 // different granularity than base::Time.
1116 // The upshot of all this is that this function might be called before
1117 // base::Time::Now() has advanced past the animation_floor_time_. To
1118 // avoid exposing this delay to javascript, we keep posting delayed
1119 // tasks until base::Time::Now() has advanced far enough.
1120 base::TimeDelta delay = animation_floor_time_ - now;
1121 animation_timer_.Start(FROM_HERE, delay, this,
1122 &RenderWidget::AnimationCallback);
1123 }
[email protected]5f8b1022011-01-21 23:34:501124}
1125
[email protected]bd37ae252011-06-03 01:28:181126bool RenderWidget::IsRenderingVSynced() {
1127 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1128 // not caught by this check. This will lead to artificially low frame rates
1129 // for people who force vsync off at a driver level and expect Chrome to speed
1130 // up.
1131 return !has_disable_gpu_vsync_switch_;
1132}
1133
[email protected]65225772011-05-12 21:10:241134void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061135 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241136 invalidation_task_posted_ = false;
1137 DoDeferredUpdateAndSendInputAck();
1138}
1139
1140void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051141 DoDeferredUpdate();
1142
[email protected]59383c782013-04-17 16:43:271143 if (pending_input_event_ack_)
[email protected]52ccd0ea2011-02-16 01:09:051144 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211145}
1146
[email protected]552e6002009-11-19 05:24:571147void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581148 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]fc8bde1a2013-07-04 08:54:071149 TRACE_EVENT_SCOPED_SAMPLING_STATE("Chrome", "Paint");
[email protected]71e2f0a2011-03-15 22:25:081150
[email protected]65225772011-05-12 21:10:241151 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291152 return;
[email protected]05a980d7a2012-02-07 22:16:421153
[email protected]fc4404d2012-11-07 19:53:301154 if (!init_complete_) {
1155 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421156 return;
1157 }
[email protected]aa4117f2011-12-09 22:19:211158 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241159 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1160 return;
1161 }
[email protected]9ca84622011-06-02 23:46:391162 if (is_accelerated_compositing_active_ &&
1163 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241164 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1165 return;
1166 }
initial.commit09911bf2008-07-26 23:55:291167
[email protected]552e6002009-11-19 05:24:571168 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051169 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571170 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291171 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241172 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291173 return;
1174 }
1175
[email protected]0fb93f52011-05-18 23:13:561176 // Tracking of frame rate jitter
1177 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371178 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051179 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501180
[email protected]f98d7e3c2010-09-13 22:30:461181 // Layout may generate more invalidation. It may also enable the
1182 // GPU acceleration, so make sure to run layout before we send the
1183 // GpuRenderingActivated message.
1184 webwidget_->layout();
1185
[email protected]793b2d62013-06-11 00:43:251186 // Check for whether we need to track swap buffers. We need to do that after
1187 // layout() because it may have switched us to accelerated compositing.
1188 if (is_accelerated_compositing_active_)
1189 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1190
[email protected]dcca3aa92012-02-17 23:03:371191 // The following two can result in further layout and possibly
1192 // enable GPU acceleration so they need to be called before any painting
1193 // is done.
[email protected]cb9e2632013-06-18 11:26:471194 UpdateTextInputType();
[email protected]dcca3aa92012-02-17 23:03:371195 UpdateSelectionBounds();
1196
[email protected]5f8b1022011-01-21 23:34:501197 // Suppress painting if nothing is dirty. This has to be done after updating
1198 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241199 if (!paint_aggregator_.HasPendingUpdate()) {
1200 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371201 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501202 return;
[email protected]65225772011-05-12 21:10:241203 }
[email protected]5f8b1022011-01-21 23:34:501204
[email protected]479b0172012-10-29 19:27:091205 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361206 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091207 ForceCompositingModeEnabled()) {
1208 webwidget_->enterForceCompositingMode(true);
1209 }
1210
[email protected]872ae5b2011-05-26 20:20:501211 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561212 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041213 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561214 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1215 delay,
1216 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411217 base::TimeDelta::FromMilliseconds(120),
1218 60);
[email protected]d0be63772011-12-20 23:18:041219 } else {
[email protected]0fb93f52011-05-18 23:13:561220 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1221 delay,
1222 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411223 base::TimeDelta::FromMilliseconds(120),
1224 60);
[email protected]d0be63772011-12-20 23:18:041225 }
[email protected]872ae5b2011-05-26 20:20:501226
1227 // Calculate filtered time per frame:
1228 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1229 filtered_time_per_frame_ =
1230 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561231 }
1232 last_do_deferred_update_time_ = frame_begin_ticks;
1233
[email protected]fef5e3972012-08-07 03:59:471234 if (!is_accelerated_compositing_active_) {
[email protected]62049562013-03-24 00:39:011235 software_stats_.animation_frame_count++;
1236 software_stats_.screen_frame_count++;
[email protected]fef5e3972012-08-07 03:59:471237 }
1238
[email protected]552e6002009-11-19 05:24:571239 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291240 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301241 PaintAggregator::PendingUpdate update;
1242 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291243
[email protected]53d3f302009-12-21 04:42:051244 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181245 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291246
[email protected]29ed96a2012-02-04 18:12:161247 // Notify derived classes that we're about to initiate a paint.
1248 WillInitiatePaint();
1249
[email protected]ca4847f2010-09-24 05:39:151250 // A plugin may be able to do an optimized paint. First check this, in which
1251 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461252 // This optimization allows PPAPI plugins that declare themselves on top of
1253 // the page (like a traditional windowed plugin) to be able to animate (think
1254 // movie playing) without repeatedly re-painting the page underneath, or
1255 // copying the plugin backing store (since we can send the plugin's backing
1256 // store directly to the browser).
1257 //
1258 // This optimization only works when the entire invalid region is contained
1259 // within the plugin. There is a related optimization in PaintRect for the
1260 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151261 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151262 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201263 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211264 DCHECK(!pending_update_params_.get());
1265 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551266 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211267 pending_update_params_->scroll_rect = update.scroll_rect;
1268 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211269 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1270 pending_update_params_->flags = next_paint_flags_;
1271 pending_update_params_->scroll_offset = GetScrollOffset();
1272 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091273 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211274 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091275 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211276
[email protected]256737c2013-06-08 04:39:101277 if (!is_accelerated_compositing_active_)
1278 pending_update_params_->latency_info = latency_info_;
1279
1280 latency_info_.Clear();
1281
[email protected]ca4847f2010-09-24 05:39:151282 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561283 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151284 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201285 &optimized_copy_rect,
1286 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271287 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471288 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211289 pending_update_params_->bitmap = dib->id();
1290 pending_update_params_->bitmap_rect = optimized_copy_location;
1291 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201292 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561293 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461294 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101295
1296 bool fractional_scale = device_scale_factor_ -
1297 static_cast<int>(device_scale_factor_) != 0;
1298 if (fractional_scale) {
1299 // Damage might not be DIP aligned. Inflate damage to compensate.
1300 bounds.Inset(-1, -1);
1301 bounds.Intersect(gfx::Rect(size_));
1302 }
1303
1304 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181305 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101306
[email protected]ca4847f2010-09-24 05:39:151307 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351308 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591309 pixel_bounds));
[email protected]59383c782013-04-17 16:43:271310 if (!canvas) {
[email protected]f98d7e3c2010-09-13 22:30:461311 NOTREACHED();
1312 return;
1313 }
[email protected]cef3362f2009-12-21 17:48:451314
[email protected]f98d7e3c2010-09-13 22:30:461315 // We may get back a smaller canvas than we asked for.
1316 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591317 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1318 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1319 pixel_bounds.set_width(canvas->getDevice()->width());
1320 pixel_bounds.set_height(canvas->getDevice()->height());
1321 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1322 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051323
[email protected]f98d7e3c2010-09-13 22:30:461324 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1325
[email protected]aa4117f2011-12-09 22:19:211326 pending_update_params_->bitmap = current_paint_buf_->id();
1327 pending_update_params_->bitmap_rect = bounds;
1328
1329 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461330 // The scroll damage is just another rectangle to paint and copy.
1331 copy_rects.swap(update.paint_rects);
1332 if (!scroll_damage.IsEmpty())
1333 copy_rects.push_back(scroll_damage);
1334
[email protected]4889bd212013-02-11 22:23:101335 for (size_t i = 0; i < copy_rects.size(); ++i) {
1336 gfx::Rect rect = copy_rects[i];
1337 if (fractional_scale) {
1338 // Damage might not be DPI aligned. Inflate rect to compensate.
1339 rect.Inset(-1, -1);
1340 }
1341 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1342 }
[email protected]60a50072012-01-11 02:05:351343
1344 // Software FPS tick for performance tests. The accelerated path traces the
1345 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1346 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421347 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW",
1348 TRACE_EVENT_SCOPE_THREAD);
[email protected]f98d7e3c2010-09-13 22:30:461349 } else { // Accelerated compositing path
1350 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211351 // If painting is done via the gpu process then we don't set any damage
1352 // rects to save the browser process from doing unecessary work.
1353 pending_update_params_->bitmap_rect = bounds;
1354 pending_update_params_->scroll_rect = gfx::Rect();
1355 // We don't need an ack, because we're not sharing a DIB with the browser.
1356 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1357 // with the browser for the GPU surface.
1358 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521359 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461360 }
1361
[email protected]936c6f52011-12-13 01:35:261362 // If we're holding a pending input event ACK, send the ACK before sending the
1363 // UpdateReply message so we can receive another input event before the
1364 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1365 // the UpdateRect IPC message handler.
[email protected]59383c782013-04-17 16:43:271366 if (pending_input_event_ack_)
[email protected]936c6f52011-12-13 01:35:261367 Send(pending_input_event_ack_.release());
1368
[email protected]ab543072013-01-25 04:38:151369 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211370 // OnSwapBuffersPosted), meaning a message has been added to the
1371 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1372 // the message now.
[email protected]59383c782013-04-17 16:43:271373 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:211374 // sending an ack to browser process that the paint is complete...
1375 update_reply_pending_ = pending_update_params_->needs_ack;
1376 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1377 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341378 }
[email protected]53d3f302009-12-21 04:42:051379
[email protected]29ed96a2012-02-04 18:12:161380 // If we're software rendering then we're done initiating the paint.
1381 if (!is_accelerated_compositing_active_)
1382 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291383}
1384
[email protected]f0c2a242013-03-15 19:34:521385void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151386 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281387 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521388 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151389}
1390
initial.commit09911bf2008-07-26 23:55:291391///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461392// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291393
[email protected]4873c7d2009-07-16 06:36:281394void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]479b0172012-10-29 19:27:091395 TRACE_EVENT2("renderer", "RenderWidget::didInvalidateRect",
1396 "width", rect.width, "height", rect.height);
[email protected]552e6002009-11-19 05:24:571397 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481398 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181399 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571400 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291401 return;
1402
[email protected]552e6002009-11-19 05:24:571403 paint_aggregator_.InvalidateRect(damaged_rect);
1404
1405 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241406 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571407 return;
1408 if (!paint_aggregator_.HasPendingUpdate())
1409 return;
[email protected]aa4117f2011-12-09 22:19:211410 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241411 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1412 return;
1413
1414 // When GPU rendering, combine pending animations and invalidations into
1415 // a single update.
[email protected]816edc62012-03-17 01:27:221416 if (is_accelerated_compositing_active_ &&
1417 animation_update_pending_ &&
1418 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571419 return;
1420
1421 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291422 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1423 // on the call stack.
1424 // 2) Allows us to collect more damage rects before painting to help coalesce
1425 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241426 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111427 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211428 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291429}
1430
[email protected]990278ff2012-11-13 02:12:551431void RenderWidget::didScrollRect(int dx, int dy,
1432 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461433 // Drop scrolls on the floor when we are in compositing mode.
1434 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561435 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461436 return;
1437
[email protected]552e6002009-11-19 05:24:571438 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481439 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181440 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571441 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291442 return;
1443
[email protected]990278ff2012-11-13 02:12:551444 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571445
1446 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241447 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571448 return;
1449 if (!paint_aggregator_.HasPendingUpdate())
1450 return;
[email protected]aa4117f2011-12-09 22:19:211451 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241452 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1453 return;
1454
1455 // When GPU rendering, combine pending animations and invalidations into
1456 // a single update.
[email protected]816edc62012-03-17 01:27:221457 if (is_accelerated_compositing_active_ &&
1458 animation_update_pending_ &&
1459 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571460 return;
1461
1462 // Perform updating asynchronously. This serves two purposes:
1463 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1464 // on the call stack.
1465 // 2) Allows us to collect more damage rects before painting to help coalesce
1466 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241467 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111468 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211469 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291470}
1471
[email protected]244ac1892011-12-02 17:04:471472void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091473 if (size_.width() != new_size.width || size_.height() != new_size.height) {
[email protected]eac2b362013-05-22 07:01:451474 size_ = new_size;
[email protected]20fbfc22013-05-08 20:50:581475
[email protected]eac2b362013-05-22 07:01:451476 // If we don't clear PaintAggregator after changing autoResize state, then
1477 // we might end up in a situation where bitmap_rect is larger than the
1478 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1479 // with invalid damage rects.
1480 paint_aggregator_.ClearPendingUpdate();
1481
[email protected]70dee7e2013-05-29 18:28:301482 if (RenderThreadImpl::current()->layout_test_mode()) {
[email protected]eac2b362013-05-22 07:01:451483 WebRect new_pos(rootWindowRect().x,
1484 rootWindowRect().y,
1485 new_size.width,
1486 new_size.height);
1487 view_screen_rect_ = new_pos;
1488 window_screen_rect_ = new_pos;
[email protected]8be1c582013-03-06 00:55:031489 }
[email protected]20fbfc22013-05-08 20:50:581490
[email protected]eac2b362013-05-22 07:01:451491 AutoResizeCompositor();
[email protected]20fbfc22013-05-08 20:50:581492
[email protected]70dee7e2013-05-29 18:28:301493 if (!RenderThreadImpl::current()->layout_test_mode())
[email protected]20fbfc22013-05-08 20:50:581494 need_update_rect_for_auto_resize_ = true;
[email protected]ea3ee0a2012-05-15 03:43:091495 }
[email protected]244ac1892011-12-02 17:04:471496}
1497
[email protected]3a1c8a8032013-03-18 22:35:321498void RenderWidget::AutoResizeCompositor() {
[email protected]97e1bf72013-03-06 14:06:051499 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1500 device_scale_factor_));
1501 if (compositor_)
1502 compositor_->setViewportSize(size_, physical_backing_size_);
1503}
1504
[email protected]91acd1c2012-03-14 08:32:391505void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221506 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1507
[email protected]c63b4d42012-04-26 01:01:071508#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211509 if (!is_accelerated_compositing_active_) {
1510 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1511 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1512 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1513 // going to switch to accelerated compositing, the GPU process may need
1514 // round-trips to the browser's UI thread before finishing the frame,
1515 // causing deadlocks if we delay the UpdateRect until we receive the
1516 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071517 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1518 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211519 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1520 }
[email protected]c63b4d42012-04-26 01:01:071521#endif
[email protected]aa4117f2011-12-09 22:19:211522
[email protected]ea162f92011-10-04 23:08:221523 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421524 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241525 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221526}
1527
1528void RenderWidget::didDeactivateCompositor() {
1529 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1530
1531 is_accelerated_compositing_active_ = false;
1532 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1533 routing_id_, is_accelerated_compositing_active_));
1534
[email protected]ea162f92011-10-04 23:08:221535 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241536 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091537
1538 // In single-threaded mode, we exit force compositing mode and re-enter in
1539 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1540 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1541 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361542 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091543 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561544}
1545
[email protected]e195e582013-03-08 01:32:591546void RenderWidget::initializeLayerTreeView() {
1547 compositor_ = RenderWidgetCompositor::Create(this);
1548 if (!compositor_)
1549 return;
1550
1551 compositor_->setViewportSize(size_, physical_backing_size_);
1552 if (init_complete_)
1553 compositor_->setSurfaceReady();
1554}
1555
[email protected]8926c602013-01-23 05:32:061556WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281557 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061558}
1559
[email protected]9ed83fe2013-02-27 01:52:281560void RenderWidget::suppressCompositorScheduling(bool enable) {
1561 if (compositor_)
1562 compositor_->SetSuppressScheduleComposite(enable);
1563}
1564
[email protected]9cd43a62012-03-26 08:03:561565void RenderWidget::willBeginCompositorFrame() {
1566 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371567
[email protected]cadac622013-06-11 16:46:361568 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy().get());
[email protected]abe8b3a2012-03-28 21:19:371569
1570 // The following two can result in further layout and possibly
1571 // enable GPU acceleration so they need to be called before any painting
1572 // is done.
[email protected]cb9e2632013-06-18 11:26:471573 UpdateTextInputType();
1574#if defined(OS_ANDROID)
[email protected]2d354272013-01-14 00:59:061575 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]cb9e2632013-06-18 11:26:471576#endif
[email protected]abe8b3a2012-03-28 21:19:371577 UpdateSelectionBounds();
1578
[email protected]9cd43a62012-03-26 08:03:561579 WillInitiatePaint();
1580}
1581
[email protected]3391a0772012-03-28 00:32:071582void RenderWidget::didBecomeReadyForAdditionalInput() {
1583 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
[email protected]59383c782013-04-17 16:43:271584 if (pending_input_event_ack_)
[email protected]3391a0772012-03-28 00:32:071585 Send(pending_input_event_ack_.release());
1586}
1587
[email protected]6fceb912013-02-15 06:24:151588void RenderWidget::DidCommitCompositorFrame() {
1589}
1590
[email protected]58264a32011-11-17 23:36:151591void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501592 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351593 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1594 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421595 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU",
1596 TRACE_EVENT_SCOPE_THREAD);
[email protected]29ed96a2012-02-04 18:12:161597 // Notify subclasses that we initiated the paint operation.
1598 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151599}
1600
1601void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181602 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1603
1604 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561605 DidFlushPaint();
1606
[email protected]aa4117f2011-12-09 22:19:211607 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151608 return;
1609
[email protected]ea3ee0a2012-05-15 03:43:091610 if (!next_paint_flags_ &&
1611 !need_update_rect_for_auto_resize_ &&
1612 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151613 return;
[email protected]ea3ee0a2012-05-15 03:43:091614 }
[email protected]58264a32011-11-17 23:36:151615
1616 ViewHostMsg_UpdateRect_Params params;
1617 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151618 params.plugin_window_moves.swap(plugin_window_moves_);
1619 params.flags = next_paint_flags_;
1620 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121621 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091622 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151623
1624 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1625 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091626 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151627}
1628
[email protected]f98d7e3c2010-09-13 22:30:461629void RenderWidget::scheduleComposite() {
[email protected]479b0172012-10-29 19:27:091630 TRACE_EVENT0("gpu", "RenderWidget::scheduleComposite");
[email protected]cadac622013-06-11 16:46:361631 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get() &&
[email protected]ba91a792013-02-06 09:48:281632 compositor_) {
1633 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041634 } else {
[email protected]c3d45532011-10-07 19:20:401635 // TODO(nduca): replace with something a little less hacky. The reason this
1636 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1637 // contains a lot of host-renderer synchronization logic that is still
1638 // important for the accelerated compositing case. The option of simply
1639 // duplicating all that code is less desirable than "faking out" the
1640 // invalidation path using a magical damage rect.
1641 didInvalidateRect(WebRect(0, 0, 1, 1));
1642 }
[email protected]f98d7e3c2010-09-13 22:30:461643}
1644
[email protected]5f8b1022011-01-21 23:34:501645void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201646 if (animation_update_pending_)
1647 return;
1648
[email protected]921244e42011-07-20 16:36:301649 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201650 animation_update_pending_ = true;
1651 if (!animation_timer_.IsRunning()) {
1652 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1653 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211654 }
[email protected]5f8b1022011-01-21 23:34:501655}
1656
[email protected]4873c7d2009-07-16 06:36:281657void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301658 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521659 WebCursor cursor;
1660 webkit_glue::InitializeCursorFromWebKitCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291661 // Only send a SetCursor message if we need to make a change.
1662 if (!current_cursor_.IsEqual(cursor)) {
1663 current_cursor_ = cursor;
1664 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1665 }
1666}
1667
1668// We are supposed to get a single call to Show for a newly created RenderWidget
1669// that was created via RenderWidget::CreateWebView. So, we wait until this
1670// point to dispatch the ShowWidget message.
1671//
1672// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281673// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291674//
[email protected]4873c7d2009-07-16 06:36:281675void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291676 DCHECK(!did_show_) << "received extraneous Show call";
1677 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1678 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1679
[email protected]8de12d942010-11-17 20:42:441680 if (did_show_)
1681 return;
1682
1683 did_show_ = true;
1684 // NOTE: initial_pos_ may still have its default values at this point, but
1685 // that's okay. It'll be ignored if as_popup is false, or the browser
1686 // process will impose a default position otherwise.
1687 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1688 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291689}
1690
[email protected]9b003482013-05-21 14:00:171691void RenderWidget::didProgrammaticallyScroll(
1692 const WebKit::WebPoint& scroll_point) {
1693 if (!compositor_)
1694 return;
1695 Send(new ViewHostMsg_DidProgrammaticallyScroll(
1696 routing_id_, gfx::Vector2d(scroll_point.x, scroll_point.y)));
1697}
1698
[email protected]4873c7d2009-07-16 06:36:281699void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291700}
1701
[email protected]4873c7d2009-07-16 06:36:281702void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291703}
1704
[email protected]2533ce12009-05-09 00:02:241705void RenderWidget::DoDeferredClose() {
1706 Send(new ViewHostMsg_Close(routing_id_));
1707}
1708
[email protected]4873c7d2009-07-16 06:36:281709void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321710 if (is_swapped_out_) {
1711 // This widget is currently swapped out, and the active widget is in a
1712 // different process. Have the browser route the close request to the
1713 // active widget instead, so that the correct unload handlers are run.
1714 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1715 return;
1716 }
1717
initial.commit09911bf2008-07-26 23:55:291718 // If a page calls window.close() twice, we'll end up here twice, but that's
1719 // OK. It is safe to send multiple Close messages.
1720
[email protected]2533ce12009-05-09 00:02:241721 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1722 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1723 // could be closed before the JS finishes executing. So instead, post a
1724 // message back to the message loop, which won't run until the JS is
1725 // complete, and then the Close message can be sent.
[email protected]dd32b1272013-05-04 14:17:111726 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211727 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291728}
1729
1730void RenderWidget::Close() {
1731 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061732 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281733 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281734 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291735 webwidget_ = NULL;
1736 }
1737}
1738
[email protected]4873c7d2009-07-16 06:36:281739WebRect RenderWidget::windowRect() {
1740 if (pending_window_rect_count_)
1741 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241742
[email protected]80ad8622012-11-07 16:33:031743 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291744}
1745
[email protected]8a9d6ca32011-06-06 20:11:301746void RenderWidget::setToolTipText(const WebKit::WebString& text,
1747 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541748 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301749}
1750
[email protected]4873c7d2009-07-16 06:36:281751void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291752 if (did_show_) {
[email protected]70dee7e2013-05-29 18:28:301753 if (!RenderThreadImpl::current()->layout_test_mode()) {
[email protected]8be1c582013-03-06 00:55:031754 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
1755 SetPendingWindowRect(pos);
1756 } else {
1757 WebSize new_size(pos.width, pos.height);
[email protected]d9083762013-03-24 01:36:401758 Resize(new_size, new_size, overdraw_bottom_height_,
1759 WebRect(), is_fullscreen_, NO_RESIZE_ACK);
[email protected]8be1c582013-03-06 00:55:031760 view_screen_rect_ = pos;
1761 window_screen_rect_ = pos;
1762 }
initial.commit09911bf2008-07-26 23:55:291763 } else {
1764 initial_pos_ = pos;
1765 }
1766}
1767
[email protected]2533ce12009-05-09 00:02:241768void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1769 pending_window_rect_ = rect;
1770 pending_window_rect_count_++;
1771}
1772
[email protected]4873c7d2009-07-16 06:36:281773WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241774 if (pending_window_rect_count_) {
1775 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1776 // the RootWindowRect is probably going to return wrong results since the
1777 // browser may not have processed the Move yet. There isn't really anything
1778 // good to do in this case, and it shouldn't happen - since this size is
1779 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281780 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241781 }
1782
[email protected]80ad8622012-11-07 16:33:031783 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371784}
1785
[email protected]4873c7d2009-07-16 06:36:281786WebRect RenderWidget::windowResizerRect() {
1787 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191788}
1789
[email protected]fa7b1dc2010-06-23 17:53:041790void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031791 // To prevent this renderer process from sending unnecessary IPC messages to
1792 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041793 // only during the input method attached to the browser process is active.
1794 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291795}
1796
[email protected]fa7b1dc2010-06-23 17:53:041797void RenderWidget::OnImeSetComposition(
1798 const string16& text,
1799 const std::vector<WebCompositionUnderline>& underlines,
1800 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281801 if (!webwidget_)
1802 return;
[email protected]66fca5bc2013-05-23 06:58:291803 ImeEventGuard guard(this);
[email protected]88dbe32f2013-06-20 23:31:361804 if (!webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041805 text, WebVector<WebCompositionUnderline>(underlines),
1806 selection_start, selection_end)) {
1807 // If we failed to set the composition text, then we need to let the browser
1808 // process to cancel the input method's ongoing composition session, to make
1809 // sure we are in a consistent state.
1810 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:261811 }
[email protected]501ea13d2013-07-09 17:03:291812#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361813 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291814#endif
[email protected]fa7b1dc2010-06-23 17:53:041815}
1816
[email protected]0e45bd02013-07-12 20:20:021817void RenderWidget::OnImeConfirmComposition(const string16& text,
1818 const ui::Range& replacement_range,
1819 bool keep_selection) {
[email protected]d0be63772011-12-20 23:18:041820 if (!webwidget_)
1821 return;
[email protected]66fca5bc2013-05-23 06:58:291822 ImeEventGuard guard(this);
[email protected]d0be63772011-12-20 23:18:041823 handling_input_event_ = true;
[email protected]0e45bd02013-07-12 20:20:021824 if (text.length())
1825 webwidget_->confirmComposition(text);
1826 else if (keep_selection)
1827 webwidget_->confirmComposition(WebWidget::KeepSelection);
1828 else
1829 webwidget_->confirmComposition(WebWidget::DoNotKeepSelection);
[email protected]d0be63772011-12-20 23:18:041830 handling_input_event_ = false;
[email protected]501ea13d2013-07-09 17:03:291831#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361832 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291833#endif
initial.commit09911bf2008-07-26 23:55:291834}
1835
[email protected]948f7ab72010-05-28 23:48:081836// This message causes the renderer to render an image of the
1837// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251838void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1839 int tag,
1840 const gfx::Size& page_size,
1841 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001842 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1843 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251844 // Close our unused handle.
1845#if defined(OS_WIN)
1846 ::CloseHandle(dib_handle);
1847#elif defined(OS_MACOSX)
1848 base::SharedMemory::CloseHandle(dib_handle);
1849#endif
1850 }
[email protected]d65adb12010-04-28 17:26:491851 return;
[email protected]45c6aad32010-11-11 04:46:251852 }
[email protected]d65adb12010-04-28 17:26:491853
[email protected]948f7ab72010-05-28 23:48:081854 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491855 // If one of these is empty, then we just return the dib we were
1856 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091857 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491858 return;
1859 }
1860
1861 // Map the given DIB ID into this process, and unmap it at the end
1862 // of this function.
[email protected]45c6aad32010-11-11 04:46:251863 scoped_ptr<TransportDIB> paint_at_size_buffer(
1864 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301865
[email protected]4b01b962012-10-09 23:17:351866 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281867 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351868 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281869 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511870 gfx::Size canvas_size = page_size_in_pixel;
1871 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491872 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511873 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491874 static_cast<float>(canvas_size.height());
1875
[email protected]ee8d6fd2010-05-26 17:05:481876 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491877 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1878 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481879 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491880
[email protected]36808ad2010-10-20 19:18:301881 scoped_ptr<skia::PlatformCanvas> canvas(
1882 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1883 canvas_size.height()));
[email protected]59383c782013-04-17 16:43:271884 if (!canvas) {
[email protected]36808ad2010-10-20 19:18:301885 NOTREACHED();
1886 return;
1887 }
1888
[email protected]d65adb12010-04-28 17:26:491889 // Reset bounds to what we actually received, but they should be the
1890 // same.
1891 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1892 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1893 bounds.set_width(canvas->getDevice()->width());
1894 bounds.set_height(canvas->getDevice()->height());
1895
1896 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081897 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491898 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1899
[email protected]948f7ab72010-05-28 23:48:081900 // Have to make sure we're laid out at the right size before
1901 // rendering.
1902 gfx::Size old_size = webwidget_->size();
1903 webwidget_->resize(page_size);
1904 webwidget_->layout();
1905
[email protected]d65adb12010-04-28 17:26:491906 // Paint the entire thing (using original bounds, not scaled bounds).
1907 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1908 canvas->restore();
1909
[email protected]948f7ab72010-05-28 23:48:081910 // Return the widget to its previous size.
1911 webwidget_->resize(old_size);
1912
[email protected]c88c9442010-07-19 18:55:091913 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491914}
1915
[email protected]51a49502013-03-23 01:50:191916void RenderWidget::OnSnapshot(const gfx::Rect& src_subrect) {
1917 SkBitmap snapshot;
1918
1919 if (OnSnapshotHelper(src_subrect, &snapshot)) {
1920 Send(new ViewHostMsg_Snapshot(routing_id(), true, snapshot));
1921 } else {
1922 Send(new ViewHostMsg_Snapshot(routing_id(), false, SkBitmap()));
1923 }
1924}
1925
1926bool RenderWidget::OnSnapshotHelper(const gfx::Rect& src_subrect,
1927 SkBitmap* snapshot) {
1928 base::TimeTicks beginning_time = base::TimeTicks::Now();
1929
1930 if (!webwidget_ || src_subrect.IsEmpty())
1931 return false;
1932
1933 gfx::Rect viewport_size = gfx::IntersectRects(
1934 src_subrect, gfx::Rect(physical_backing_size_));
1935
1936 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
1937 skia::CreatePlatformCanvas(viewport_size.width(),
1938 viewport_size.height(),
1939 true,
1940 NULL,
1941 skia::RETURN_NULL_ON_FAILURE));
[email protected]59383c782013-04-17 16:43:271942 if (!canvas)
[email protected]51a49502013-03-23 01:50:191943 return false;
1944
1945 canvas->save();
1946 webwidget_->layout();
1947
1948 PaintRect(viewport_size, viewport_size.origin(), canvas.get());
1949 canvas->restore();
1950
1951 const SkBitmap& bitmap = skia::GetTopDevice(*canvas)->accessBitmap(false);
1952 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
1953 return false;
1954
1955 UMA_HISTOGRAM_TIMES("Renderer4.Snapshot",
1956 base::TimeTicks::Now() - beginning_time);
1957 return true;
1958}
1959
[email protected]0bc1f572013-04-17 01:46:311960void RenderWidget::OnRepaint(gfx::Size size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:121961 // During shutdown we can just ignore this message.
1962 if (!webwidget_)
1963 return;
1964
[email protected]0bc1f572013-04-17 01:46:311965 // Even if the browser provides an empty damage rect, it's still expecting to
1966 // receive a repaint ack so just damage the entire widget bounds.
1967 if (size_to_paint.IsEmpty()) {
1968 size_to_paint = size_;
1969 }
1970
[email protected]ec7dc112008-08-06 05:30:121971 set_next_paint_is_repaint_ack();
[email protected]0bc1f572013-04-17 01:46:311972 if (is_accelerated_compositing_active_ && compositor_) {
1973 compositor_->SetNeedsRedrawRect(gfx::Rect(size_to_paint));
[email protected]f98d7e3c2010-09-13 22:30:461974 } else {
1975 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1976 didInvalidateRect(repaint_rect);
1977 }
[email protected]ec7dc112008-08-06 05:30:121978}
1979
[email protected]4a9dba42013-04-29 18:24:221980void RenderWidget::OnSmoothScrollCompleted() {
1981 pending_smooth_scroll_gesture_.Run();
[email protected]0e241b4b2012-08-18 09:06:271982}
1983
[email protected]4873c7d2009-07-16 06:36:281984void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111985 if (!webwidget_)
1986 return;
[email protected]4873c7d2009-07-16 06:36:281987 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111988}
1989
[email protected]80ad8622012-11-07 16:33:031990void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
1991 const gfx::Rect& window_screen_rect) {
1992 view_screen_rect_ = view_screen_rect;
1993 window_screen_rect_ = window_screen_rect;
1994 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
1995}
1996
[email protected]105dffb42013-02-20 03:46:211997#if defined(OS_ANDROID)
1998void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
1999 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
2000}
[email protected]2384b6c2013-02-28 23:58:512001
2002void RenderWidget::OnShowImeIfNeeded() {
2003 UpdateTextInputState(SHOW_IME_IF_NEEDED);
2004}
[email protected]105dffb42013-02-20 03:46:212005#endif
2006
[email protected]468ac582012-11-20 00:53:192007void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
2008 if (device_scale_factor_ == device_scale_factor)
2009 return;
2010
2011 device_scale_factor_ = device_scale_factor;
2012
2013 if (!is_accelerated_compositing_active_) {
2014 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
2015 } else {
2016 scheduleComposite();
2017 }
2018}
2019
[email protected]719b36f2010-12-22 20:36:462020webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:152021 const gfx::Rect& paint_bounds,
2022 TransportDIB** dib,
2023 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:202024 gfx::Rect* clip,
2025 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:462026 // Bare RenderWidgets don't support optimized plugin painting.
2027 return NULL;
[email protected]ca4847f2010-09-24 05:39:152028}
2029
[email protected]ceb36f7d2012-10-31 18:33:242030gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:522031 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:242032 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:522033}
2034
[email protected]bee16aab2009-08-26 15:55:032035void RenderWidget::SetHidden(bool hidden) {
2036 if (is_hidden_ == hidden)
2037 return;
2038
2039 // The status has changed. Tell the RenderThread about it.
2040 is_hidden_ = hidden;
2041 if (is_hidden_)
[email protected]380244092011-10-07 17:26:272042 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:032043 else
[email protected]380244092011-10-07 17:26:272044 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:032045}
2046
[email protected]2b624c562011-10-27 22:58:262047void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262048 if (!webwidget_)
2049 return;
2050
2051 if (is_fullscreen_) {
2052 webwidget_->willExitFullScreen();
2053 } else {
2054 webwidget_->willEnterFullScreen();
2055 }
[email protected]2b624c562011-10-27 22:58:262056}
2057
2058void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262059 if (!webwidget_)
2060 return;
2061
2062 if (is_fullscreen_) {
2063 webwidget_->didEnterFullScreen();
2064 } else {
2065 webwidget_->didExitFullScreen();
2066 }
[email protected]2b624c562011-10-27 22:58:262067}
2068
[email protected]699ab0d2009-04-23 23:19:142069void RenderWidget::SetBackground(const SkBitmap& background) {
2070 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:462071
[email protected]699ab0d2009-04-23 23:19:142072 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:282073 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:142074}
2075
[email protected]674741932009-02-04 23:44:462076bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:052077 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462078}
2079
2080bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:052081 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462082}
2083
2084void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:052085 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:462086}
2087
2088void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:052089 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:462090}
2091
2092void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:052093 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:462094}
2095
[email protected]b18583c2012-12-18 06:55:272096static bool IsDateTimeInput(ui::TextInputType type) {
2097 return type == ui::TEXT_INPUT_TYPE_DATE ||
2098 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
2099 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
2100 type == ui::TEXT_INPUT_TYPE_MONTH ||
2101 type == ui::TEXT_INPUT_TYPE_TIME ||
2102 type == ui::TEXT_INPUT_TYPE_WEEK;
2103}
2104
[email protected]66fca5bc2013-05-23 06:58:292105
2106void RenderWidget::StartHandlingImeEvent() {
2107 DCHECK(!handling_ime_event_);
2108 handling_ime_event_ = true;
2109}
2110
2111void RenderWidget::FinishHandlingImeEvent() {
2112 DCHECK(handling_ime_event_);
2113 handling_ime_event_ = false;
2114 // While handling an ime event, text input state and selection bounds updates
2115 // are ignored. These must explicitly be updated once finished handling the
2116 // ime event.
2117 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472118#if defined(OS_ANDROID)
[email protected]66fca5bc2013-05-23 06:58:292119 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]cb9e2632013-06-18 11:26:472120#endif
[email protected]66fca5bc2013-05-23 06:58:292121}
2122
[email protected]cb9e2632013-06-18 11:26:472123void RenderWidget::UpdateTextInputType() {
2124 if (!input_method_is_active_)
2125 return;
2126
[email protected]b256eca2013-07-11 10:57:402127 WebKit::WebTextInputInfo new_info;
2128 if (webwidget_)
2129 new_info = webwidget_->textInputInfo();
2130
2131 ui::TextInputType new_type = WebKitToUiTextInputType(new_info.type);
[email protected]cb9e2632013-06-18 11:26:472132 if (IsDateTimeInput(new_type))
2133 return; // Not considered as a text input field in WebKit/Chromium.
2134
2135 bool new_can_compose_inline = CanComposeInline();
2136
[email protected]b256eca2013-07-11 10:57:402137 const ui::TextInputMode new_mode = ConvertInputMode(new_info.inputMode);
2138
[email protected]cb9e2632013-06-18 11:26:472139 if (text_input_type_ != new_type
[email protected]b256eca2013-07-11 10:57:402140 || can_compose_inline_ != new_can_compose_inline
2141 || text_input_mode_ != new_mode) {
[email protected]cb9e2632013-06-18 11:26:472142 Send(new ViewHostMsg_TextInputTypeChanged(routing_id(),
2143 new_type,
[email protected]b256eca2013-07-11 10:57:402144 new_can_compose_inline,
2145 new_mode));
[email protected]cb9e2632013-06-18 11:26:472146 text_input_type_ = new_type;
2147 can_compose_inline_ = new_can_compose_inline;
[email protected]b256eca2013-07-11 10:57:402148 text_input_mode_ = new_mode;
[email protected]cb9e2632013-06-18 11:26:472149 }
2150}
2151
2152#if defined(OS_ANDROID)
[email protected]3306f262012-09-21 19:20:422153void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
[email protected]e8f775f2013-02-14 21:00:502154 if (handling_ime_event_)
2155 return;
[email protected]3306f262012-09-21 19:20:422156 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
2157 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:292158 return;
[email protected]ad26ef42011-06-17 07:59:452159 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:272160 if (IsDateTimeInput(new_type))
2161 return; // Not considered as a text input field in WebKit/Chromium.
2162
[email protected]5b739cb2012-08-21 20:35:212163 WebKit::WebTextInputInfo new_info;
2164 if (webwidget_)
2165 new_info = webwidget_->textInputInfo();
2166
[email protected]ad26ef42011-06-17 07:59:452167 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212168
[email protected]3306f262012-09-21 19:20:422169 // Only sends text input params if they are changed or if the ime should be
2170 // shown.
2171 if (show_ime_if_needed || (text_input_type_ != new_type
2172 || text_input_info_ != new_info
2173 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212174 ViewHostMsg_TextInputState_Params p;
2175 p.type = new_type;
2176 p.value = new_info.value.utf8();
2177 p.selection_start = new_info.selectionStart;
2178 p.selection_end = new_info.selectionEnd;
2179 p.composition_start = new_info.compositionStart;
2180 p.composition_end = new_info.compositionEnd;
2181 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422182 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:212183 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2184
2185 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042186 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452187 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292188 }
initial.commit09911bf2008-07-26 23:55:292189}
[email protected]cb9e2632013-06-18 11:26:472190#endif
initial.commit09911bf2008-07-26 23:55:292191
[email protected]7c8873e2013-02-05 08:03:012192void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2193 WebRect focus_webrect;
2194 WebRect anchor_webrect;
2195 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2196 *focus = focus_webrect;
2197 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322198}
2199
[email protected]e99ef6f2011-10-16 01:13:002200void RenderWidget::UpdateSelectionBounds() {
2201 if (!webwidget_)
2202 return;
[email protected]66fca5bc2013-05-23 06:58:292203 if (handling_ime_event_)
2204 return;
[email protected]e99ef6f2011-10-16 01:13:002205
[email protected]7c8873e2013-02-05 08:03:012206 ViewHostMsg_SelectionBounds_Params params;
2207 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2208 if (selection_anchor_rect_ != params.anchor_rect ||
2209 selection_focus_rect_ != params.focus_rect) {
2210 selection_anchor_rect_ = params.anchor_rect;
2211 selection_focus_rect_ = params.focus_rect;
2212 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292213 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012214 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352215 }
[email protected]501ea13d2013-07-09 17:03:292216#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362217 UpdateCompositionInfo(false);
[email protected]501ea13d2013-07-09 17:03:292218#endif
[email protected]e99ef6f2011-10-16 01:13:002219}
2220
[email protected]73bf95812011-10-12 11:38:322221// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452222COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2223 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2224COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2225 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2226COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2227 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182228COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2229 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2230COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2231 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2232COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2233 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2234COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2235 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2236COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2237 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002238COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2239 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2240COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2241 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2242COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2243 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2244COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2245 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2246COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2247 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2248COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2249 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012250COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2251 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2252COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2253 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152254COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2255 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452256
[email protected]5b739cb2012-08-21 20:35:212257ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2258 WebKit::WebTextInputType type) {
2259 // Check the type is in the range representable by ui::TextInputType.
2260 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2261 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2262 return static_cast<ui::TextInputType>(type);
2263}
2264
[email protected]ad26ef42011-06-17 07:59:452265ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272266 if (webwidget_)
2267 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452268 return ui::TEXT_INPUT_TYPE_NONE;
2269}
2270
[email protected]501ea13d2013-07-09 17:03:292271#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
2272void RenderWidget::UpdateCompositionInfo(bool should_update_range) {
2273 ui::Range range = ui::Range();
2274 if (should_update_range) {
2275 GetCompositionRange(&range);
2276 } else {
2277 range = composition_range_;
2278 }
2279 std::vector<gfx::Rect> character_bounds;
2280 GetCompositionCharacterBounds(&character_bounds);
2281
2282 if (!ShouldUpdateCompositionInfo(range, character_bounds))
2283 return;
2284 composition_character_bounds_ = character_bounds;
2285 composition_range_ = range;
2286 Send(new ViewHostMsg_ImeCompositionRangeChanged(
2287 routing_id(), composition_range_, composition_character_bounds_));
2288}
2289
[email protected]58b48a0d2012-06-13 07:01:352290void RenderWidget::GetCompositionCharacterBounds(
2291 std::vector<gfx::Rect>* bounds) {
2292 DCHECK(bounds);
2293 bounds->clear();
2294}
2295
[email protected]88dbe32f2013-06-20 23:31:362296void RenderWidget::GetCompositionRange(ui::Range* range) {
2297 size_t location, length;
2298 if (webwidget_->compositionRange(&location, &length)) {
2299 range->set_start(location);
2300 range->set_end(location + length);
2301 } else if (webwidget_->caretOrSelectionRange(&location, &length)) {
2302 range->set_start(location);
2303 range->set_end(location + length);
2304 } else {
2305 *range = ui::Range::InvalidRange();
2306 }
2307}
2308
[email protected]501ea13d2013-07-09 17:03:292309bool RenderWidget::ShouldUpdateCompositionInfo(
2310 const ui::Range& range,
2311 const std::vector<gfx::Rect>& bounds) {
2312 if (composition_range_ != range)
2313 return true;
2314 if (bounds.size() != composition_character_bounds_.size())
2315 return true;
2316 for (size_t i = 0; i < bounds.size(); ++i) {
2317 if (bounds[i] != composition_character_bounds_[i])
2318 return true;
2319 }
2320 return false;
2321}
2322#endif
2323
[email protected]ad26ef42011-06-17 07:59:452324bool RenderWidget::CanComposeInline() {
2325 return true;
[email protected]56ea1a62011-05-30 07:05:572326}
2327
[email protected]4873c7d2009-07-16 06:36:282328WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042329 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282330}
2331
[email protected]f660d9c2012-06-06 18:31:212332float RenderWidget::deviceScaleFactor() {
2333 return device_scale_factor_;
2334}
2335
[email protected]fa7b1dc2010-06-23 17:53:042336void RenderWidget::resetInputMethod() {
2337 if (!input_method_is_active_)
2338 return;
2339
[email protected]0e45bd02013-07-12 20:20:022340 ImeEventGuard guard(this);
[email protected]fa7b1dc2010-06-23 17:53:042341 // If the last text input type is not None, then we should finish any
2342 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452343 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042344 // If a composition text exists, then we need to let the browser process
2345 // to cancel the input method's ongoing composition session.
2346 if (webwidget_->confirmComposition())
2347 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2348 }
[email protected]d4cff272011-05-02 15:46:012349
[email protected]501ea13d2013-07-09 17:03:292350#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362351 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292352#endif
[email protected]fa7b1dc2010-06-23 17:53:042353}
2354
[email protected]c68c3e4e2013-01-24 00:36:562355void RenderWidget::didHandleGestureEvent(
2356 const WebGestureEvent& event,
2357 bool event_cancelled) {
2358#if defined(OS_ANDROID)
2359 if (event_cancelled)
2360 return;
2361 if (event.type == WebInputEvent::GestureTap ||
2362 event.type == WebInputEvent::GestureLongPress) {
2363 UpdateTextInputState(SHOW_IME_IF_NEEDED);
2364 }
2365#endif
2366}
2367
[email protected]f103ab72009-09-02 17:10:592368void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:502369 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292370 size_t i = 0;
2371 for (; i < plugin_window_moves_.size(); ++i) {
2372 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582373 if (move.rects_valid) {
2374 plugin_window_moves_[i] = move;
2375 } else {
2376 plugin_window_moves_[i].visible = move.visible;
2377 }
initial.commit09911bf2008-07-26 23:55:292378 break;
2379 }
2380 }
2381
2382 if (i == plugin_window_moves_.size())
2383 plugin_window_moves_.push_back(move);
2384}
[email protected]268654772009-08-06 23:02:042385
2386void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2387 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2388 i != plugin_window_moves_.end(); ++i) {
2389 if (i->window == window) {
2390 plugin_window_moves_.erase(i);
2391 break;
2392 }
2393 }
2394}
[email protected]67bfb83f2011-09-22 03:36:372395
[email protected]b63d58d2012-11-26 22:37:442396void RenderWidget::GetRenderingStats(
2397 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282398 if (compositor_)
[email protected]635353c2013-03-06 09:11:202399 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442400
[email protected]62049562013-03-24 00:39:012401 stats.rendering_stats.animation_frame_count +=
2402 software_stats_.animation_frame_count;
2403 stats.rendering_stats.screen_frame_count +=
2404 software_stats_.screen_frame_count;
2405 stats.rendering_stats.total_paint_time +=
2406 software_stats_.total_paint_time;
2407 stats.rendering_stats.total_pixels_painted +=
2408 software_stats_.total_pixels_painted;
[email protected]fef5e3972012-08-07 03:59:472409}
2410
[email protected]e9ff79c2012-10-19 21:31:262411bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522412 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2413 if (!gpu_channel)
2414 return false;
2415
2416 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2417}
2418
[email protected]24ed0432013-04-24 07:50:312419RenderWidgetCompositor* RenderWidget::compositor() const {
2420 return compositor_.get();
2421}
2422
[email protected]3639aa82013-06-04 11:00:042423void RenderWidget::OnSetBrowserRenderingStats(
2424 const BrowserRenderingStats& stats) {
2425 browser_rendering_stats_ = stats;
2426}
2427
2428void RenderWidget::GetBrowserRenderingStats(BrowserRenderingStats* stats) {
2429 *stats = browser_rendering_stats_;
2430}
2431
[email protected]0c2ebef2013-04-03 12:14:102432void RenderWidget::BeginSmoothScroll(
[email protected]0e241b4b2012-08-18 09:06:272433 bool down,
[email protected]ebd8b562012-10-09 14:44:292434 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192435 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292436 int mouse_event_x,
2437 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272438 DCHECK(!callback.is_null());
[email protected]267909d2012-10-20 04:36:192439
2440 ViewHostMsg_BeginSmoothScroll_Params params;
2441 params.scroll_down = down;
2442 params.pixels_to_scroll = pixels_to_scroll;
2443 params.mouse_event_x = mouse_event_x;
2444 params.mouse_event_y = mouse_event_y;
2445
[email protected]4a9dba42013-04-29 18:24:222446 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, params));
2447 pending_smooth_scroll_gesture_ = callback;
[email protected]a39ca1652012-07-13 21:30:582448}
2449
[email protected]67bfb83f2011-09-22 03:36:372450bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2451 return false;
2452}
[email protected]c3d45532011-10-07 19:20:402453
[email protected]f56c7872013-06-18 12:31:572454bool RenderWidget::WillHandleKeyEvent(const WebKit::WebKeyboardEvent& event) {
2455 return false;
2456}
2457
[email protected]41d86852012-11-07 12:23:242458bool RenderWidget::WillHandleGestureEvent(
2459 const WebKit::WebGestureEvent& event) {
2460 return false;
2461}
2462
[email protected]ce6689f2013-03-29 12:52:552463void RenderWidget::hasTouchEventHandlers(bool has_handlers) {
2464 Send(new ViewHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
2465}
2466
[email protected]3d5c243b2012-11-30 00:26:012467bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2468 return true;
2469}
2470
[email protected]3ae68c52013-04-12 06:10:052471WebGraphicsContext3DCommandBufferImpl* RenderWidget::CreateGraphicsContext3D(
[email protected]92fd8c02013-03-29 08:54:152472 const WebKit::WebGraphicsContext3D::Attributes& attributes) {
[email protected]ed7defa2013-03-12 21:29:592473 if (!webwidget_)
2474 return NULL;
[email protected]8f746982013-03-21 06:28:032475 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2476 new WebGraphicsContext3DCommandBufferImpl(
2477 surface_id(),
2478 GetURLForGraphicsContext3D(),
2479 RenderThreadImpl::current(),
2480 weak_ptr_factory_.GetWeakPtr()));
[email protected]ed7defa2013-03-12 21:29:592481
[email protected]990e4c212013-06-08 05:03:332482 if (!context->InitializeWithDefaultBufferSizes(
[email protected]8f746982013-03-21 06:28:032483 attributes,
2484 false /* bind generates resources */,
2485 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE))
2486 return NULL;
2487 return context.release();
[email protected]ed7defa2013-03-12 21:29:592488}
2489
[email protected]e9ff79c2012-10-19 21:31:262490} // namespace content