blob: 9dea50d38030e27658ae664eda63d2684b26b4d1 [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]32876ae2011-11-15 22:25:217#include "base/bind.h"
[email protected]4fb66842009-12-04 21:41:008#include "base/command_line.h"
[email protected]366ae242011-05-10 02:23:589#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]b256eca2013-07-11 10:57:4012#include "base/memory/singleton.h"
[email protected]aaf68892013-07-18 00:11:3013#include "base/message_loop/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3814#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2115#include "base/stl_util.h"
[email protected]74ebfb12013-06-07 20:48:0016#include "base/strings/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4817#include "build/build_config.h"
[email protected]681ccff2013-03-18 06:13:5218#include "cc/base/switches.h"
[email protected]7f0d825f2013-03-18 07:24:3019#include "cc/output/output_surface.h"
[email protected]556fd292013-03-18 08:03:0420#include "cc/trees/layer_tree_host.h"
[email protected]29e2fb42013-07-19 01:13:4721#include "content/child/npapi/webplugin.h"
[email protected]ed7defa2013-03-12 21:29:5922#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]c084330e02013-04-27 01:08:1523#include "content/common/input_messages.h"
[email protected]992db4c2011-05-12 15:37:1524#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5025#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2926#include "content/public/common/content_switches.h"
[email protected]ed7defa2013-03-12 21:29:5927#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]2847b222013-04-06 00:59:2428#include "content/renderer/gpu/compositor_software_output_device.h"
[email protected]36e5ff12013-06-11 12:19:2929#include "content/renderer/gpu/delegated_compositor_output_surface.h"
[email protected]ea5f70a2013-03-07 12:30:3630#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5931#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2832#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]66fca5bc2013-05-23 06:58:2933#include "content/renderer/ime_event_guard.h"
[email protected]8704f89b2011-04-15 00:30:0534#include "content/renderer/render_process.h"
[email protected]fd33b59ff2013-06-21 22:57:0535#include "content/renderer/render_process_visibility_manager.h"
[email protected]f1a29a02011-10-06 23:08:4436#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1937#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1838#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4839#include "skia/ext/platform_canvas.h"
[email protected]aaf68892013-07-18 00:11:3040#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
41#include "third_party/WebKit/public/platform/WebPoint.h"
42#include "third_party/WebKit/public/platform/WebRect.h"
43#include "third_party/WebKit/public/platform/WebSize.h"
44#include "third_party/WebKit/public/platform/WebString.h"
[email protected]2255a9332013-06-17 05:12:3145#include "third_party/WebKit/public/web/WebCursorInfo.h"
46#include "third_party/WebKit/public/web/WebHelperPlugin.h"
47#include "third_party/WebKit/public/web/WebPagePopup.h"
48#include "third_party/WebKit/public/web/WebPopupMenu.h"
49#include "third_party/WebKit/public/web/WebPopupMenuInfo.h"
50#include "third_party/WebKit/public/web/WebRange.h"
51#include "third_party/WebKit/public/web/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4152#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1353#include "ui/base/ui_base_switches.h"
[email protected]9b003482013-05-21 14:00:1754#include "ui/gfx/point.h"
[email protected]a25e25b2012-09-28 14:32:3755#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3556#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4857#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2758#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4159#include "ui/surface/transport_dib.h"
[email protected]8c89e7792009-08-19 21:18:3460#include "webkit/glue/webkit_glue.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]53b4cc12013-07-18 23:02:30229 next_output_surface_id_(0),
[email protected]69e797f2013-04-30 01:10:22230 weak_ptr_factory_(this) {
[email protected]8b3f0eb2012-05-03 19:15:05231 if (!swapped_out)
232 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27233 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18234 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
235 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36236 is_threaded_compositing_enabled_ =
237 CommandLine::ForCurrentProcess()->HasSwitch(
238 switches::kEnableThreadedCompositing);
[email protected]fd33b59ff2013-06-21 22:57:05239
240 RenderProcessVisibilityManager::GetInstance()->WidgetVisibilityChanged(true);
initial.commit09911bf2008-07-26 23:55:29241}
242
243RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11244 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21245 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35246 if (current_paint_buf_) {
247 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
248 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29249 }
[email protected]992db4c2011-05-12 15:37:15250 // If we are swapped out, we have released already.
251 if (!is_swapped_out_)
252 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29253}
254
[email protected]484955942010-08-19 16:13:18255// static
[email protected]8085dbc82008-09-26 22:53:44256RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04257 WebKit::WebPopupType popup_type,
258 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29259 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41260 scoped_refptr<RenderWidget> widget(
[email protected]842f10652012-06-06 01:54:04261 new RenderWidget(popup_type, screen_info, false));
[email protected]a635f942012-12-07 10:34:29262 if (widget->Init(opener_id)) { // adds reference on success.
[email protected]fc72bb12013-06-02 21:13:46263 return widget.get();
[email protected]a635f942012-12-07 10:34:29264 }
265 return NULL;
initial.commit09911bf2008-07-26 23:55:29266}
267
[email protected]484955942010-08-19 16:13:18268// static
269WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
270 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03271 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18272 break;
273 case WebKit::WebPopupTypeSelect:
274 case WebKit::WebPopupTypeSuggestion:
275 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44276 case WebKit::WebPopupTypePage:
277 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03278 case WebKit::WebPopupTypeHelperPlugin:
279 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18280 default:
281 NOTREACHED();
282 }
283 return NULL;
284}
285
[email protected]a635f942012-12-07 10:34:29286bool RenderWidget::Init(int32 opener_id) {
287 return DoInit(opener_id,
288 RenderWidget::CreateWebWidget(this),
289 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
290 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18291}
292
[email protected]a635f942012-12-07 10:34:29293bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06294 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18295 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29296 DCHECK(!webwidget_);
297
298 if (opener_id != MSG_ROUTING_NONE)
299 opener_id_ = opener_id;
300
[email protected]484955942010-08-19 16:13:18301 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29302
[email protected]380244092011-10-07 17:26:27303 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29304 if (result) {
[email protected]380244092011-10-07 17:26:27305 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29306 // Take a reference on behalf of the RenderThread. This will be balanced
307 // when we receive ViewMsg_Close.
308 AddRef();
[email protected]a635f942012-12-07 10:34:29309 return true;
initial.commit09911bf2008-07-26 23:55:29310 } else {
[email protected]a635f942012-12-07 10:34:29311 // The above Send can fail when the tab is closing.
312 return false;
initial.commit09911bf2008-07-26 23:55:29313 }
314}
315
[email protected]fc4404d2012-11-07 19:53:30316// This is used to complete pending inits and non-pending inits.
317void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29318 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29319
[email protected]fc4404d2012-11-07 19:53:30320 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42321
[email protected]8926c602013-01-23 05:32:06322 if (webwidget_ && is_threaded_compositing_enabled_) {
323 webwidget_->enterForceCompositingMode(true);
324 }
[email protected]ba91a792013-02-06 09:48:28325 if (compositor_) {
326 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09327 }
[email protected]05a980d7a2012-02-07 22:16:42328 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29329
[email protected]6de74452009-02-25 18:04:59330 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29331}
332
[email protected]992db4c2011-05-12 15:37:15333void RenderWidget::SetSwappedOut(bool is_swapped_out) {
334 // We should only toggle between states.
335 DCHECK(is_swapped_out_ != is_swapped_out);
336 is_swapped_out_ = is_swapped_out;
337
338 // If we are swapping out, we will call ReleaseProcess, allowing the process
339 // to exit if all of its RenderViews are swapped out. We wait until the
340 // WasSwappedOut call to do this, to avoid showing the sad tab.
341 // If we are swapping in, we call AddRefProcess to prevent the process from
342 // exiting.
343 if (!is_swapped_out)
344 RenderProcess::current()->AddRefProcess();
345}
346
[email protected]34bb3ac2013-03-08 02:41:28347bool RenderWidget::AllowPartialSwap() const {
348 return true;
349}
350
[email protected]c8cbae72013-05-23 10:45:03351bool RenderWidget::UsingSynchronousRendererCompositor() const {
[email protected]ccc1722e2013-05-06 19:43:07352#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07353 return SynchronousCompositorFactory::GetInstance() != NULL;
354#else
[email protected]ccc1722e2013-05-06 19:43:07355 return false;
[email protected]913d99a2013-05-31 07:16:07356#endif
[email protected]ccc1722e2013-05-06 19:43:07357}
358
[email protected]a95986a82010-12-24 06:19:28359bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
360 bool handled = true;
361 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
[email protected]c084330e02013-04-27 01:08:15362 IPC_MESSAGE_HANDLER(InputMsg_HandleInputEvent, OnHandleInputEvent)
[email protected]34202de2013-05-06 23:36:22363 IPC_MESSAGE_HANDLER(InputMsg_CursorVisibilityChange,
364 OnCursorVisibilityChange)
[email protected]c084330e02013-04-27 01:08:15365 IPC_MESSAGE_HANDLER(InputMsg_MouseCaptureLost, OnMouseCaptureLost)
366 IPC_MESSAGE_HANDLER(InputMsg_SetFocus, OnSetFocus)
[email protected]a95986a82010-12-24 06:19:28367 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
368 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
369 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54370 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28371 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41372 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15373 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28374 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59375 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
376 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28377 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
378 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
379 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25380 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
381 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
382 IPC_MESSAGE_HANDLER(ViewMsg_SmoothScrollCompleted, OnSmoothScrollCompleted)
[email protected]a95986a82010-12-24 06:19:28383 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
384 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]80ad8622012-11-07 16:33:03385 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21386#if defined(OS_ANDROID)
387 IPC_MESSAGE_HANDLER(ViewMsg_ImeBatchStateChanged, OnImeBatchStateChanged)
[email protected]2384b6c2013-02-28 23:58:51388 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]105dffb42013-02-20 03:46:21389#endif
[email protected]51a49502013-03-23 01:50:19390 IPC_MESSAGE_HANDLER(ViewMsg_Snapshot, OnSnapshot)
[email protected]3639aa82013-06-04 11:00:04391 IPC_MESSAGE_HANDLER(ViewMsg_SetBrowserRenderingStats,
392 OnSetBrowserRenderingStats)
[email protected]a95986a82010-12-24 06:19:28393 IPC_MESSAGE_UNHANDLED(handled = false)
394 IPC_END_MESSAGE_MAP()
395 return handled;
396}
initial.commit09911bf2008-07-26 23:55:29397
398bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15399 // Don't send any messages after the browser has told us to close, and filter
400 // most outgoing messages while swapped out.
401 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26402 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11403 closing_) {
initial.commit09911bf2008-07-26 23:55:29404 delete message;
405 return false;
406 }
407
408 // If given a messsage without a routing ID, then assign our routing ID.
409 if (message->routing_id() == MSG_ROUTING_NONE)
410 message->set_routing_id(routing_id_);
411
[email protected]380244092011-10-07 17:26:27412 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44413}
414
[email protected]61e2b3cc2012-03-02 16:13:34415void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44416 const gfx::Size& physical_backing_size,
[email protected]d9083762013-03-24 01:36:40417 float overdraw_bottom_height,
[email protected]61e2b3cc2012-03-02 16:13:34418 const gfx::Rect& resizer_rect,
419 bool is_fullscreen,
420 ResizeAck resize_ack) {
[email protected]1c0008842013-06-06 08:35:48421 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
422 !RenderThreadImpl::current()->layout_test_mode()) {
423 // A resize ack shouldn't be requested if we have not ACK'd the previous
424 // one.
425 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
426 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
427 }
initial.commit09911bf2008-07-26 23:55:29428
[email protected]61e2b3cc2012-03-02 16:13:34429 // Ignore this during shutdown.
430 if (!webwidget_)
431 return;
432
[email protected]d9083762013-03-24 01:36:40433 if (compositor_) {
[email protected]60d47ac2013-03-01 23:42:44434 compositor_->setViewportSize(new_size, physical_backing_size);
[email protected]d9083762013-03-24 01:36:40435 compositor_->SetOverdrawBottomHeight(overdraw_bottom_height);
436 }
[email protected]60d47ac2013-03-01 23:42:44437
[email protected]dade8992013-03-04 07:34:34438 physical_backing_size_ = physical_backing_size;
[email protected]d9083762013-03-24 01:36:40439 overdraw_bottom_height_ = overdraw_bottom_height;
[email protected]61e2b3cc2012-03-02 16:13:34440 resizer_rect_ = resizer_rect;
441
442 // NOTE: We may have entered fullscreen mode without changing our size.
443 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
444 if (fullscreen_change)
445 WillToggleFullscreen();
446 is_fullscreen_ = is_fullscreen;
447
448 if (size_ != new_size) {
449 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34450 needs_repainting_on_restore_ = false;
451
452 size_ = new_size;
453
454 paint_aggregator_.ClearPendingUpdate();
455
456 // When resizing, we want to wait to paint before ACK'ing the resize. This
457 // ensures that we only resize as fast as we can paint. We only need to
458 // send an ACK if we are resized to a non-empty rect.
459 webwidget_->resize(new_size);
[email protected]0b70dbe2013-05-10 19:06:32460
[email protected]92201e32013-06-08 06:14:52461 if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
462 !RenderThreadImpl::current()->layout_test_mode()) {
463 // Resize should have caused an invalidation of the entire view.
464 DCHECK(new_size.IsEmpty() || is_accelerated_compositing_active_ ||
465 paint_aggregator_.HasPendingUpdate());
466 }
[email protected]1c0008842013-06-06 08:35:48467 } else if (!RenderThreadImpl::current() || // Will be NULL during unit tests.
468 !RenderThreadImpl::current()->layout_test_mode()) {
[email protected]632c4382013-05-15 08:58:45469 resize_ack = NO_RESIZE_ACK;
470 }
471
472 if (new_size.IsEmpty() || physical_backing_size.IsEmpty()) {
473 // For empty size or empty physical_backing_size, there is no next paint
474 // (along with which to send the ack) until they are set to non-empty.
[email protected]ff475a322012-03-14 00:05:35475 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34476 }
477
[email protected]20fbfc22013-05-08 20:50:58478 // Send the Resize_ACK flag once we paint again if requested.
[email protected]632c4382013-05-15 08:58:45479 if (resize_ack == SEND_RESIZE_ACK)
[email protected]20fbfc22013-05-08 20:50:58480 set_next_paint_is_resize_ack();
481
[email protected]61e2b3cc2012-03-02 16:13:34482 if (fullscreen_change)
483 DidToggleFullscreen();
484
485 // If a resize ack is requested and it isn't set-up, then no more resizes will
486 // come in and in general things will go wrong.
[email protected]632c4382013-05-15 08:58:45487 DCHECK(resize_ack != SEND_RESIZE_ACK || next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29488}
489
490void RenderWidget::OnClose() {
491 if (closing_)
492 return;
493 closing_ = true;
494
495 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03496 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27497 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03498 SetHidden(false);
499 }
initial.commit09911bf2008-07-26 23:55:29500
initial.commit09911bf2008-07-26 23:55:29501 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25502 // now. Post a task that only gets invoked when there are no nested message
503 // loops.
[email protected]dd32b1272013-05-04 14:17:11504 base::MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29505 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25506
507 // Balances the AddRef taken when we called AddRoute.
508 Release();
initial.commit09911bf2008-07-26 23:55:29509}
510
[email protected]61e2b3cc2012-03-02 16:13:34511// Got a response from the browser after the renderer decided to create a new
512// view.
[email protected]fc4404d2012-11-07 19:53:30513void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34514 DCHECK(routing_id_ != MSG_ROUTING_NONE);
515
[email protected]fc4404d2012-11-07 19:53:30516 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34517}
518
[email protected]0fdd5012013-05-29 08:05:56519void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) {
520 screen_info_ = params.screen_info;
521 SetDeviceScaleFactor(screen_info_.deviceScaleFactor);
522 Resize(params.new_size, params.physical_backing_size,
523 params.overdraw_bottom_height, params.resizer_rect,
524 params.is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29525}
526
[email protected]b5913d72012-02-07 22:26:54527void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
528 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59529 gfx::Rect view_rect(size_);
530
[email protected]ce112fe2012-10-29 22:52:18531 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59532 if (!old_damage_rect.IsEmpty())
533 paint_aggregator_.InvalidateRect(old_damage_rect);
534
[email protected]ce112fe2012-10-29 22:52:18535 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59536 if (!new_damage_rect.IsEmpty())
537 paint_aggregator_.InvalidateRect(new_damage_rect);
538
[email protected]b5913d72012-02-07 22:26:54539 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59540
[email protected]b5913d72012-02-07 22:26:54541 if (webwidget_)
542 webwidget_->didChangeWindowResizerRect();
543 }
544}
545
initial.commit09911bf2008-07-26 23:55:29546void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31547 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29548 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03549 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29550}
551
[email protected]9e2e4632012-07-27 16:38:41552void RenderWidget::OnWasShown(bool needs_repainting) {
553 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29554 // During shutdown we can just ignore this message.
555 if (!webwidget_)
556 return;
557
558 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03559 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29560
561 if (!needs_repainting && !needs_repainting_on_restore_)
562 return;
563 needs_repainting_on_restore_ = false;
564
[email protected]d65adb12010-04-28 17:26:49565 // Tag the next paint as a restore ack, which is picked up by
566 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29567 set_next_paint_is_restore_ack();
568
569 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56570 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46571 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
572 } else {
573 scheduleComposite();
574 }
initial.commit09911bf2008-07-26 23:55:29575}
576
[email protected]992db4c2011-05-12 15:37:15577void RenderWidget::OnWasSwappedOut() {
578 // If we have been swapped out and no one else is using this process,
579 // it's safe to exit now. If we get swapped back in, we will call
580 // AddRefProcess in SetSwappedOut.
581 if (is_swapped_out_)
582 RenderProcess::current()->ReleaseProcess();
583}
584
[email protected]53d3f302009-12-21 04:42:05585void RenderWidget::OnRequestMoveAck() {
586 DCHECK(pending_window_rect_count_);
587 pending_window_rect_count_--;
588}
589
590void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58591 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21592 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05593 update_reply_pending_ = false;
594
[email protected]b4d08452010-10-05 17:34:35595 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
596 // have no current paint buffer.
597 if (current_paint_buf_) {
598 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
599 current_paint_buf_ = NULL;
600 }
601
[email protected]65225772011-05-12 21:10:24602 // If swapbuffers is still pending, then defer the update until the
603 // swapbuffers occurs.
604 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
605 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
606 return;
607 }
608
[email protected]29ed96a2012-02-04 18:12:16609 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18610 if (!is_accelerated_compositing_active_) {
611 DidFlushPaint();
612 }
[email protected]a2f6bc112009-06-27 16:27:25613
initial.commit09911bf2008-07-26 23:55:29614 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24615 DoDeferredUpdateAndSendInputAck();
616}
617
[email protected]d0be63772011-12-20 23:18:04618bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59619 // Contexts using the command buffer support asynchronous swapbuffers.
620 // See RenderWidget::CreateOutputSurface().
[email protected]cadac622013-06-11 16:46:36621 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get())
[email protected]ed7defa2013-03-12 21:29:59622 return false;
623
624 return true;
625}
626
627GURL RenderWidget::GetURLForGraphicsContext3D() {
628 return GURL();
[email protected]65225772011-05-12 21:10:24629}
630
[email protected]479b0172012-10-29 19:27:09631bool RenderWidget::ForceCompositingModeEnabled() {
632 return false;
633}
634
[email protected]ba91a792013-02-06 09:48:28635scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface() {
[email protected]c3e32ed42013-05-02 05:07:13636 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1811b8912013-05-09 15:35:19637
638#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07639 if (SynchronousCompositorFactory* factory =
640 SynchronousCompositorFactory::GetInstance()) {
641 return factory->CreateOutputSurface(routing_id());
[email protected]a1811b8912013-05-09 15:35:19642 }
643#endif
644
[email protected]53b4cc12013-07-18 23:02:30645 uint32 output_surface_id = next_output_surface_id_++;
646
[email protected]8bbe3a92013-05-12 00:58:35647 if (command_line.HasSwitch(switches::kEnableSoftwareCompositingGLAdapter)) {
648 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30649 new CompositorOutputSurface(routing_id(), output_surface_id, NULL,
[email protected]36e5ff12013-06-11 12:19:29650 new CompositorSoftwareOutputDevice(), true));
[email protected]8bbe3a92013-05-12 00:58:35651 }
652
[email protected]ed7defa2013-03-12 21:29:59653 // Explicitly disable antialiasing for the compositor. As of the time of
654 // this writing, the only platform that supported antialiasing for the
655 // compositor was Mac OS X, because the on-screen OpenGL context creation
656 // code paths on Windows and Linux didn't yet have multisampling support.
657 // Mac OS X essentially always behaves as though it's rendering offscreen.
658 // Multisampling has a heavy cost especially on devices with relatively low
659 // fill rate like most notebooks, and the Mac implementation would need to
660 // be optimized to resolve directly into the IOSurface shared between the
661 // GPU and browser processes. For these reasons and to avoid platform
662 // disparities we explicitly disable antialiasing.
663 WebKit::WebGraphicsContext3D::Attributes attributes;
664 attributes.antialias = false;
665 attributes.shareResources = true;
666 attributes.noAutomaticFlushes = true;
[email protected]a6886502013-05-16 20:59:18667 attributes.depth = false;
668 attributes.stencil = false;
[email protected]bec084292013-05-21 21:31:44669 if (command_line.HasSwitch(cc::switches::kForceDirectLayerDrawing))
670 attributes.stencil = true;
[email protected]3ae68c52013-04-12 06:10:05671 WebGraphicsContext3DCommandBufferImpl* context =
672 CreateGraphicsContext3D(attributes);
[email protected]8bbe3a92013-05-12 00:58:35673 if (!context)
674 return scoped_ptr<cc::OutputSurface>();
[email protected]ed7defa2013-03-12 21:29:59675
[email protected]0c04d1c2013-07-10 00:02:32676 if (command_line.HasSwitch(switches::kEnableDelegatedRenderer) &&
677 !command_line.HasSwitch(switches::kDisableDelegatedRenderer)) {
[email protected]36e5ff12013-06-11 12:19:29678 DCHECK(is_threaded_compositing_enabled_);
679 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30680 new DelegatedCompositorOutputSurface(routing_id(), output_surface_id,
681 context, NULL));
[email protected]36e5ff12013-06-11 12:19:29682 }
683 if (command_line.HasSwitch(cc::switches::kCompositeToMailbox)) {
684 DCHECK(is_threaded_compositing_enabled_);
685 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30686 new MailboxOutputSurface(routing_id(), output_surface_id,
687 context, NULL));
[email protected]36e5ff12013-06-11 12:19:29688 }
689 return scoped_ptr<cc::OutputSurface>(
[email protected]53b4cc12013-07-18 23:02:30690 new CompositorOutputSurface(routing_id(), output_surface_id,
691 context, NULL, false));
[email protected]ba91a792013-02-06 09:48:28692}
693
[email protected]ed7defa2013-03-12 21:29:59694void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24695 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21696 while (!updates_pending_swap_.empty()) {
697 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
698 updates_pending_swap_.pop_front();
699 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
700 // compositing pass, hence doesn't require an UpdateRect message.
701 if (msg)
702 Send(msg);
703 }
[email protected]65225772011-05-12 21:10:24704 num_swapbuffers_complete_pending_ = 0;
705 using_asynchronous_swapbuffers_ = false;
706 // Schedule another frame so the compositor learns about it.
707 scheduleComposite();
708}
709
[email protected]ed7defa2013-03-12 21:29:59710void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08711 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21712
713 if (using_asynchronous_swapbuffers_) {
714 ViewHostMsg_UpdateRect* msg = NULL;
715 // pending_update_params_ can be NULL if the swap doesn't correspond to an
716 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
717 // message.
[email protected]59383c782013-04-17 16:43:27718 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:21719 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
720 pending_update_params_.reset();
721 }
722 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08723 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21724 }
[email protected]37a6f302011-07-11 23:43:08725}
726
[email protected]ed7defa2013-03-12 21:29:59727void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24728 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16729
[email protected]404939f2012-06-01 04:06:18730 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16731 DidFlushPaint();
732
[email protected]65225772011-05-12 21:10:24733 // When compositing deactivates, we reset the swapbuffers pending count. The
734 // swapbuffers acks may still arrive, however.
735 if (num_swapbuffers_complete_pending_ == 0) {
736 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
737 return;
738 }
[email protected]aa4117f2011-12-09 22:19:21739 DCHECK(!updates_pending_swap_.empty());
740 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
741 updates_pending_swap_.pop_front();
742 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
743 // compositing pass, hence doesn't require an UpdateRect message.
744 if (msg)
745 Send(msg);
[email protected]65225772011-05-12 21:10:24746 num_swapbuffers_complete_pending_--;
747
748 // If update reply is still pending, then defer the update until that reply
749 // occurs.
[email protected]d0be63772011-12-20 23:18:04750 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24751 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
752 return;
753 }
754
755 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06756 // when we were previously rendering. However, if an invalidation task is not
757 // posted, there may be software rendering work pending. In that case, don't
758 // early out.
759 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24760 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
761 return;
762 }
763
[email protected]cc66e682012-10-02 06:48:18764 // Do not call DoDeferredUpdate unless there's animation work to be done or
765 // a real invalidation. This prevents rendering in response to a swapbuffers
766 // callback coming back after we've navigated away from the page that
767 // generated it.
768 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
769 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
770 return;
771 }
772
[email protected]65225772011-05-12 21:10:24773 // Continue painting if necessary...
774 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29775}
776
[email protected]0dea1652012-12-14 00:09:09777void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
[email protected]4b157662013-05-29 04:05:05778 const ui::LatencyInfo& latency_info,
[email protected]0dea1652012-12-14 00:09:09779 bool is_keyboard_shortcut) {
[email protected]5dd768212009-08-13 23:34:49780 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:09781 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:49782 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29783 return;
[email protected]5dd768212009-08-13 23:34:49784 }
initial.commit09911bf2008-07-26 23:55:29785
[email protected]b4841e1c2013-05-16 22:30:10786 const char* const event_name = GetEventName(input_event->type);
787 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent",
788 "event", event_name);
789
[email protected]c2eaa8f2013-05-10 02:41:55790 if (compositor_)
791 compositor_->SetLatencyInfo(latency_info);
[email protected]256737c2013-06-08 04:39:10792 else
793 latency_info_.MergeWith(latency_info);
[email protected]c2eaa8f2013-05-10 02:41:55794
[email protected]6a4d7f62013-01-07 21:32:13795 base::TimeDelta now = base::TimeDelta::FromInternalValue(
796 base::TimeTicks::Now().ToInternalValue());
797
798 int64 delta = static_cast<int64>(
799 (now.InSecondsF() - input_event->timeStampSeconds) *
800 base::Time::kMicrosecondsPerSecond);
801 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
[email protected]de415552013-01-23 04:12:17802 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:48803 base::Histogram::FactoryGet(
[email protected]b4841e1c2013-05-16 22:30:10804 base::StringPrintf("Event.Latency.Renderer.%s", event_name),
[email protected]bafdc5d52013-02-27 18:18:48805 0,
806 1000000,
[email protected]6a4d7f62013-01-07 21:32:13807 100,
[email protected]de415552013-01-23 04:12:17808 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:48809 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:13810
[email protected]67bfb83f2011-09-22 03:36:37811 bool prevent_default = false;
812 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26813 const WebMouseEvent& mouse_event =
814 *static_cast<const WebMouseEvent*>(input_event);
815 TRACE_EVENT2("renderer", "HandleMouseMove",
816 "x", mouse_event.x, "y", mouse_event.y);
817 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37818 }
819
[email protected]f56c7872013-06-18 12:31:57820 if (WebInputEvent::isKeyboardEventType(input_event->type)) {
821 const WebKeyboardEvent& key_event =
822 *static_cast<const WebKeyboardEvent*>(input_event);
823 prevent_default = WillHandleKeyEvent(key_event);
824 }
825
[email protected]41d86852012-11-07 12:23:24826 if (WebInputEvent::isGestureEventType(input_event->type)) {
827 const WebGestureEvent& gesture_event =
828 *static_cast<const WebGestureEvent*>(input_event);
829 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
830 }
831
[email protected]3ebcc7c2013-01-09 05:34:46832 if (input_event->type == WebInputEvent::GestureTap ||
833 input_event->type == WebInputEvent::GestureLongPress)
834 resetInputMethod();
835
[email protected]67bfb83f2011-09-22 03:36:37836 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12837 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
838 suppress_next_char_events_ = false;
839 if (!processed && webwidget_)
840 processed = webwidget_->handleInputEvent(*input_event);
841 }
842
843 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
844 // it's not processed by webkit, then we need to suppress the upcoming Char
845 // events.
846 if (!processed && is_keyboard_shortcut)
847 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29848
[email protected]3d5c243b2012-11-30 00:26:01849 InputEventAckState ack_result = processed ?
850 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
851 if (!processed && input_event->type == WebInputEvent::TouchStart) {
852 const WebTouchEvent& touch_event =
853 *static_cast<const WebTouchEvent*>(input_event);
854 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
855 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
856 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
857 }
858
[email protected]a9fb30aa2011-10-06 06:58:46859 IPC::Message* response =
[email protected]c084330e02013-04-27 01:08:15860 new InputHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
861 ack_result);
[email protected]3391a0772012-03-28 00:32:07862 bool event_type_gets_rate_limited =
863 input_event->type == WebInputEvent::MouseMove ||
864 input_event->type == WebInputEvent::MouseWheel ||
865 WebInputEvent::isTouchEventType(input_event->type);
[email protected]8926c602013-01-23 05:32:06866
867 bool frame_pending = paint_aggregator_.HasPendingUpdate();
868 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:28869 frame_pending = compositor_ &&
870 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:06871 }
872
[email protected]9a8ce7f92013-06-11 12:39:49873 if (event_type_gets_rate_limited && frame_pending && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:24874 // We want to rate limit the input events in this case, so we'll wait for
875 // painting to finish before ACKing this message.
[email protected]59383c782013-04-17 16:43:27876 if (pending_input_event_ack_) {
[email protected]353a34c2010-05-28 23:35:17877 // As two different kinds of events could cause us to postpone an ack
878 // we send it now, if we have one pending. The Browser should never
879 // send us the same kind of event we are delaying the ack for.
880 Send(pending_input_event_ack_.release());
881 }
[email protected]12fbad812009-09-01 18:21:24882 pending_input_event_ack_.reset(response);
883 } else {
884 Send(response);
885 }
886
[email protected]3306f262012-09-21 19:20:42887#if defined(OS_ANDROID)
888 // Allow the IME to be shown when the focus changes as a consequence
889 // of a processed touch end event.
890 if (input_event->type == WebInputEvent::TouchEnd && processed)
891 UpdateTextInputState(SHOW_IME_IF_NEEDED);
892#endif
893
[email protected]5dd768212009-08-13 23:34:49894 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48895
[email protected]67bfb83f2011-09-22 03:36:37896 if (!prevent_default) {
897 if (WebInputEvent::isKeyboardEventType(input_event->type))
898 DidHandleKeyEvent();
899 if (WebInputEvent::isMouseEventType(input_event->type))
900 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24901 if (WebInputEvent::isTouchEventType(input_event->type))
902 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37903 }
initial.commit09911bf2008-07-26 23:55:29904}
905
[email protected]34202de2013-05-06 23:36:22906void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
907 if (webwidget_)
908 webwidget_->setCursorVisibilityState(is_visible);
909}
910
initial.commit09911bf2008-07-26 23:55:29911void RenderWidget::OnMouseCaptureLost() {
912 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28913 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29914}
915
916void RenderWidget::OnSetFocus(bool enable) {
917 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33918 if (webwidget_)
919 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29920}
921
922void RenderWidget::ClearFocus() {
923 // We may have got the focus from the browser before this gets processed, in
924 // which case we do not want to unfocus ourself.
925 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28926 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29927}
928
[email protected]2d5d09d52009-06-15 14:29:21929void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00930 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21931 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06932 TRACE_EVENT2("renderer", "PaintRect",
933 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:45934
935 const bool kEnableGpuBenchmarking =
936 CommandLine::ForCurrentProcess()->HasSwitch(
937 switches::kEnableGpuBenchmarking);
[email protected]4fb66842009-12-04 21:41:00938 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21939
940 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00941 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
942 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03943
[email protected]699ab0d2009-04-23 23:19:14944 // If there is a custom background, tile it.
945 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14946 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:11947 skia::RefPtr<SkShader> shader = skia::AdoptRef(
948 SkShader::CreateBitmapShader(background_,
949 SkShader::kRepeat_TileMode,
950 SkShader::kRepeat_TileMode));
951 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:20952
953 // Use kSrc_Mode to handle background_ transparency properly.
954 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
955
956 // Canvas could contain multiple update rects. Clip to given rect so that
957 // we don't accidentally clear other update rects.
958 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:44959 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:48960 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:14961 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20962 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14963 }
964
[email protected]719b36f2010-12-22 20:36:46965 // First see if this rect is a plugin that can paint itself faster.
966 TransportDIB* optimized_dib = NULL;
967 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:20968 float dib_scale_factor;
[email protected]719b36f2010-12-22 20:36:46969 webkit::ppapi::PluginInstance* optimized_instance =
970 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
971 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:20972 &optimized_copy_rect,
973 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:46974 if (optimized_instance) {
975 // This plugin can be optimize-painted and we can just ask it to paint
976 // itself. We don't actually need the TransportDIB in this case.
977 //
978 // This is an optimization for PPAPI plugins that know they're on top of
979 // the page content. If this rect is inside such a plugin, we can save some
980 // time and avoid re-rendering the page content which we know will be
981 // covered by the plugin later (this time can be significant, especially
982 // for a playing movie that is invalidating a lot).
983 //
984 // In the plugin movie case, hopefully the similar call to
985 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
986 // painting, because that avoids copying the plugin image to a different
987 // paint rect. Unfortunately, if anything on the page is animating other
988 // than the movie, it break this optimization since the union of the
989 // invalid regions will be larger than the plugin.
990 //
991 // This code optimizes that case, where we can still avoid painting in
992 // WebKit and filling the background (which can be slow) and just painting
993 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
994 // required.
[email protected]63ab54262012-11-09 15:58:45995 base::TimeTicks paint_begin_ticks;
996 if (kEnableGpuBenchmarking)
997 paint_begin_ticks = base::TimeTicks::HighResNow();
998
[email protected]df59dd42012-09-14 22:56:30999 SkAutoCanvasRestore auto_restore(canvas, true);
1000 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]719b36f2010-12-22 20:36:461001 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:271002 optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:211003 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451004 if (kEnableGpuBenchmarking) {
1005 base::TimeDelta paint_time =
1006 base::TimeTicks::HighResNow() - paint_begin_ticks;
1007 if (!is_accelerated_compositing_active_)
[email protected]62049562013-03-24 00:39:011008 software_stats_.total_paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451009 }
[email protected]719b36f2010-12-22 20:36:461010 } else {
1011 // Normal painting case.
[email protected]63ab54262012-11-09 15:58:451012 base::TimeTicks paint_begin_ticks;
1013 if (kEnableGpuBenchmarking)
1014 paint_begin_ticks = base::TimeTicks::HighResNow();
1015
[email protected]719b36f2010-12-22 20:36:461016 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
[email protected]63ab54262012-11-09 15:58:451017
1018 if (kEnableGpuBenchmarking) {
1019 base::TimeDelta paint_time =
1020 base::TimeTicks::HighResNow() - paint_begin_ticks;
1021 if (!is_accelerated_compositing_active_)
[email protected]62049562013-03-24 00:39:011022 software_stats_.total_paint_time += paint_time;
[email protected]63ab54262012-11-09 15:58:451023 }
[email protected]719b36f2010-12-22 20:36:461024
1025 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:351026 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:461027 }
initial.commit09911bf2008-07-26 23:55:291028
[email protected]4fb66842009-12-04 21:41:001029 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:001030 canvas->restore();
[email protected]63ab54262012-11-09 15:58:451031
1032 if (kEnableGpuBenchmarking) {
[email protected]63ab54262012-11-09 15:58:451033 int64 num_pixels_processed = rect.width() * rect.height();
[email protected]62049562013-03-24 00:39:011034 software_stats_.total_pixels_painted += num_pixels_processed;
[email protected]63ab54262012-11-09 15:58:451035 }
[email protected]4fb66842009-12-04 21:41:001036}
1037
1038void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
1039 skia::PlatformCanvas* canvas) {
1040 static bool kPaintBorder =
1041 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
1042 if (!kPaintBorder)
1043 return;
1044
[email protected]53d3f302009-12-21 04:42:051045 // Cycle through these colors to help distinguish new paint rects.
1046 const SkColor colors[] = {
1047 SkColorSetARGB(0x3F, 0xFF, 0, 0),
1048 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
1049 SkColorSetARGB(0x3F, 0, 0, 0xFF),
1050 };
1051 static int color_selector = 0;
1052
[email protected]4fb66842009-12-04 21:41:001053 SkPaint paint;
1054 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:051055 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:001056 paint.setStrokeWidth(1);
1057
1058 SkIRect irect;
1059 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
1060 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:291061}
1062
[email protected]52ccd0ea2011-02-16 01:09:051063void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:301064 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:301065 if (!animation_update_pending_) {
1066 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:591067 return;
[email protected]921244e42011-07-20 16:36:301068 }
[email protected]bd37ae252011-06-03 01:28:181069 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:591070 // Record when we fired (according to base::Time::Now()) relative to when
1071 // we posted the task to quantify how much the base::Time/base::TimeTicks
1072 // skew is affecting animations.
1073 base::TimeDelta animation_callback_delay = base::Time::Now() -
1074 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
1075 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
1076 animation_callback_delay,
1077 base::TimeDelta::FromMilliseconds(0),
1078 base::TimeDelta::FromMilliseconds(30),
1079 25);
1080 }
[email protected]65225772011-05-12 21:10:241081 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:241082}
1083
[email protected]52ccd0ea2011-02-16 01:09:051084void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:591085 if (!animation_update_pending_)
1086 return;
[email protected]bd37ae252011-06-03 01:28:181087
1088 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:331089 base::TimeDelta animationInterval = IsRenderingVSynced() ?
1090 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:181091
[email protected]7c4329e2011-02-18 22:02:591092 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:451093
1094 // animation_floor_time_ is the earliest time that we should animate when
1095 // using the dead reckoning software scheduler. If we're using swapbuffers
1096 // complete callbacks to rate limit, we can ignore this floor.
1097 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:301098 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:331099 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:181100 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:591101 // running animation callbacks so that if a callback requests another
1102 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:381103 animation_timer_.Stop();
1104 animation_timer_.Start(FROM_HERE, animationInterval, this,
1105 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:591106 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:281107 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:201108 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:061109 } else {
[email protected]635353c2013-03-06 09:11:201110 double frame_begin_time =
1111 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
1112 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061113 }
[email protected]7c4329e2011-02-18 22:02:591114 return;
[email protected]5f8b1022011-01-21 23:34:501115 }
[email protected]bd37ae252011-06-03 01:28:181116 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381117 if (!animation_timer_.IsRunning()) {
1118 // This code uses base::Time::Now() to calculate the floor and next fire
1119 // time because javascript's Date object uses base::Time::Now(). The
1120 // message loop uses base::TimeTicks, which on windows can have a
1121 // different granularity than base::Time.
1122 // The upshot of all this is that this function might be called before
1123 // base::Time::Now() has advanced past the animation_floor_time_. To
1124 // avoid exposing this delay to javascript, we keep posting delayed
1125 // tasks until base::Time::Now() has advanced far enough.
1126 base::TimeDelta delay = animation_floor_time_ - now;
1127 animation_timer_.Start(FROM_HERE, delay, this,
1128 &RenderWidget::AnimationCallback);
1129 }
[email protected]5f8b1022011-01-21 23:34:501130}
1131
[email protected]bd37ae252011-06-03 01:28:181132bool RenderWidget::IsRenderingVSynced() {
1133 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1134 // not caught by this check. This will lead to artificially low frame rates
1135 // for people who force vsync off at a driver level and expect Chrome to speed
1136 // up.
1137 return !has_disable_gpu_vsync_switch_;
1138}
1139
[email protected]65225772011-05-12 21:10:241140void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061141 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241142 invalidation_task_posted_ = false;
1143 DoDeferredUpdateAndSendInputAck();
1144}
1145
1146void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051147 DoDeferredUpdate();
1148
[email protected]59383c782013-04-17 16:43:271149 if (pending_input_event_ack_)
[email protected]52ccd0ea2011-02-16 01:09:051150 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211151}
1152
[email protected]552e6002009-11-19 05:24:571153void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581154 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]fc8bde1a2013-07-04 08:54:071155 TRACE_EVENT_SCOPED_SAMPLING_STATE("Chrome", "Paint");
[email protected]71e2f0a2011-03-15 22:25:081156
[email protected]65225772011-05-12 21:10:241157 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291158 return;
[email protected]05a980d7a2012-02-07 22:16:421159
[email protected]fc4404d2012-11-07 19:53:301160 if (!init_complete_) {
1161 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421162 return;
1163 }
[email protected]aa4117f2011-12-09 22:19:211164 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241165 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1166 return;
1167 }
[email protected]9ca84622011-06-02 23:46:391168 if (is_accelerated_compositing_active_ &&
1169 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241170 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1171 return;
1172 }
initial.commit09911bf2008-07-26 23:55:291173
[email protected]552e6002009-11-19 05:24:571174 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051175 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571176 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291177 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241178 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291179 return;
1180 }
1181
[email protected]0fb93f52011-05-18 23:13:561182 // Tracking of frame rate jitter
1183 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371184 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051185 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501186
[email protected]f98d7e3c2010-09-13 22:30:461187 // Layout may generate more invalidation. It may also enable the
1188 // GPU acceleration, so make sure to run layout before we send the
1189 // GpuRenderingActivated message.
1190 webwidget_->layout();
1191
[email protected]793b2d62013-06-11 00:43:251192 // Check for whether we need to track swap buffers. We need to do that after
1193 // layout() because it may have switched us to accelerated compositing.
1194 if (is_accelerated_compositing_active_)
1195 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1196
[email protected]dcca3aa92012-02-17 23:03:371197 // The following two can result in further layout and possibly
1198 // enable GPU acceleration so they need to be called before any painting
1199 // is done.
[email protected]cb9e2632013-06-18 11:26:471200 UpdateTextInputType();
[email protected]dcca3aa92012-02-17 23:03:371201 UpdateSelectionBounds();
1202
[email protected]5f8b1022011-01-21 23:34:501203 // Suppress painting if nothing is dirty. This has to be done after updating
1204 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241205 if (!paint_aggregator_.HasPendingUpdate()) {
1206 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371207 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501208 return;
[email protected]65225772011-05-12 21:10:241209 }
[email protected]5f8b1022011-01-21 23:34:501210
[email protected]479b0172012-10-29 19:27:091211 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361212 !is_threaded_compositing_enabled_ &&
[email protected]479b0172012-10-29 19:27:091213 ForceCompositingModeEnabled()) {
1214 webwidget_->enterForceCompositingMode(true);
1215 }
1216
[email protected]872ae5b2011-05-26 20:20:501217 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561218 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041219 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561220 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
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 } else {
[email protected]0fb93f52011-05-18 23:13:561226 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1227 delay,
1228 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411229 base::TimeDelta::FromMilliseconds(120),
1230 60);
[email protected]d0be63772011-12-20 23:18:041231 }
[email protected]872ae5b2011-05-26 20:20:501232
1233 // Calculate filtered time per frame:
1234 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1235 filtered_time_per_frame_ =
1236 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561237 }
1238 last_do_deferred_update_time_ = frame_begin_ticks;
1239
[email protected]fef5e3972012-08-07 03:59:471240 if (!is_accelerated_compositing_active_) {
[email protected]62049562013-03-24 00:39:011241 software_stats_.animation_frame_count++;
1242 software_stats_.screen_frame_count++;
[email protected]fef5e3972012-08-07 03:59:471243 }
1244
[email protected]552e6002009-11-19 05:24:571245 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291246 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301247 PaintAggregator::PendingUpdate update;
1248 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291249
[email protected]53d3f302009-12-21 04:42:051250 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181251 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291252
[email protected]29ed96a2012-02-04 18:12:161253 // Notify derived classes that we're about to initiate a paint.
1254 WillInitiatePaint();
1255
[email protected]ca4847f2010-09-24 05:39:151256 // A plugin may be able to do an optimized paint. First check this, in which
1257 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461258 // This optimization allows PPAPI plugins that declare themselves on top of
1259 // the page (like a traditional windowed plugin) to be able to animate (think
1260 // movie playing) without repeatedly re-painting the page underneath, or
1261 // copying the plugin backing store (since we can send the plugin's backing
1262 // store directly to the browser).
1263 //
1264 // This optimization only works when the entire invalid region is contained
1265 // within the plugin. There is a related optimization in PaintRect for the
1266 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151267 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151268 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201269 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211270 DCHECK(!pending_update_params_.get());
1271 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551272 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211273 pending_update_params_->scroll_rect = update.scroll_rect;
1274 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211275 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1276 pending_update_params_->flags = next_paint_flags_;
1277 pending_update_params_->scroll_offset = GetScrollOffset();
1278 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091279 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211280 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091281 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211282
[email protected]256737c2013-06-08 04:39:101283 if (!is_accelerated_compositing_active_)
1284 pending_update_params_->latency_info = latency_info_;
1285
1286 latency_info_.Clear();
1287
[email protected]ca4847f2010-09-24 05:39:151288 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561289 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151290 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201291 &optimized_copy_rect,
1292 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271293 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471294 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211295 pending_update_params_->bitmap = dib->id();
1296 pending_update_params_->bitmap_rect = optimized_copy_location;
1297 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201298 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561299 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461300 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101301
1302 bool fractional_scale = device_scale_factor_ -
1303 static_cast<int>(device_scale_factor_) != 0;
1304 if (fractional_scale) {
1305 // Damage might not be DIP aligned. Inflate damage to compensate.
1306 bounds.Inset(-1, -1);
1307 bounds.Intersect(gfx::Rect(size_));
1308 }
1309
1310 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181311 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101312
[email protected]ca4847f2010-09-24 05:39:151313 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351314 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591315 pixel_bounds));
[email protected]59383c782013-04-17 16:43:271316 if (!canvas) {
[email protected]f98d7e3c2010-09-13 22:30:461317 NOTREACHED();
1318 return;
1319 }
[email protected]cef3362f2009-12-21 17:48:451320
[email protected]f98d7e3c2010-09-13 22:30:461321 // We may get back a smaller canvas than we asked for.
1322 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591323 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1324 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1325 pixel_bounds.set_width(canvas->getDevice()->width());
1326 pixel_bounds.set_height(canvas->getDevice()->height());
1327 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1328 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051329
[email protected]f98d7e3c2010-09-13 22:30:461330 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1331
[email protected]aa4117f2011-12-09 22:19:211332 pending_update_params_->bitmap = current_paint_buf_->id();
1333 pending_update_params_->bitmap_rect = bounds;
1334
1335 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461336 // The scroll damage is just another rectangle to paint and copy.
1337 copy_rects.swap(update.paint_rects);
1338 if (!scroll_damage.IsEmpty())
1339 copy_rects.push_back(scroll_damage);
1340
[email protected]4889bd212013-02-11 22:23:101341 for (size_t i = 0; i < copy_rects.size(); ++i) {
1342 gfx::Rect rect = copy_rects[i];
1343 if (fractional_scale) {
1344 // Damage might not be DPI aligned. Inflate rect to compensate.
1345 rect.Inset(-1, -1);
1346 }
1347 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1348 }
[email protected]60a50072012-01-11 02:05:351349
1350 // Software FPS tick for performance tests. The accelerated path traces the
1351 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1352 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421353 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW",
1354 TRACE_EVENT_SCOPE_THREAD);
[email protected]f98d7e3c2010-09-13 22:30:461355 } else { // Accelerated compositing path
1356 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211357 // If painting is done via the gpu process then we don't set any damage
1358 // rects to save the browser process from doing unecessary work.
1359 pending_update_params_->bitmap_rect = bounds;
1360 pending_update_params_->scroll_rect = gfx::Rect();
1361 // We don't need an ack, because we're not sharing a DIB with the browser.
1362 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1363 // with the browser for the GPU surface.
1364 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521365 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461366 }
1367
[email protected]936c6f52011-12-13 01:35:261368 // If we're holding a pending input event ACK, send the ACK before sending the
1369 // UpdateReply message so we can receive another input event before the
1370 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1371 // the UpdateRect IPC message handler.
[email protected]59383c782013-04-17 16:43:271372 if (pending_input_event_ack_)
[email protected]936c6f52011-12-13 01:35:261373 Send(pending_input_event_ack_.release());
1374
[email protected]ab543072013-01-25 04:38:151375 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211376 // OnSwapBuffersPosted), meaning a message has been added to the
1377 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1378 // the message now.
[email protected]59383c782013-04-17 16:43:271379 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:211380 // sending an ack to browser process that the paint is complete...
1381 update_reply_pending_ = pending_update_params_->needs_ack;
1382 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1383 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341384 }
[email protected]53d3f302009-12-21 04:42:051385
[email protected]29ed96a2012-02-04 18:12:161386 // If we're software rendering then we're done initiating the paint.
1387 if (!is_accelerated_compositing_active_)
1388 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291389}
1390
[email protected]f0c2a242013-03-15 19:34:521391void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151392 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281393 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521394 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151395}
1396
initial.commit09911bf2008-07-26 23:55:291397///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461398// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291399
[email protected]4873c7d2009-07-16 06:36:281400void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]479b0172012-10-29 19:27:091401 TRACE_EVENT2("renderer", "RenderWidget::didInvalidateRect",
1402 "width", rect.width, "height", rect.height);
[email protected]552e6002009-11-19 05:24:571403 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481404 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181405 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571406 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291407 return;
1408
[email protected]552e6002009-11-19 05:24:571409 paint_aggregator_.InvalidateRect(damaged_rect);
1410
1411 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241412 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571413 return;
1414 if (!paint_aggregator_.HasPendingUpdate())
1415 return;
[email protected]aa4117f2011-12-09 22:19:211416 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241417 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1418 return;
1419
1420 // When GPU rendering, combine pending animations and invalidations into
1421 // a single update.
[email protected]816edc62012-03-17 01:27:221422 if (is_accelerated_compositing_active_ &&
1423 animation_update_pending_ &&
1424 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571425 return;
1426
1427 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291428 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1429 // on the call stack.
1430 // 2) Allows us to collect more damage rects before painting to help coalesce
1431 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241432 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111433 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211434 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291435}
1436
[email protected]990278ff2012-11-13 02:12:551437void RenderWidget::didScrollRect(int dx, int dy,
1438 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461439 // Drop scrolls on the floor when we are in compositing mode.
1440 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561441 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461442 return;
1443
[email protected]552e6002009-11-19 05:24:571444 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481445 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181446 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571447 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291448 return;
1449
[email protected]990278ff2012-11-13 02:12:551450 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571451
1452 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241453 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571454 return;
1455 if (!paint_aggregator_.HasPendingUpdate())
1456 return;
[email protected]aa4117f2011-12-09 22:19:211457 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241458 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1459 return;
1460
1461 // When GPU rendering, combine pending animations and invalidations into
1462 // a single update.
[email protected]816edc62012-03-17 01:27:221463 if (is_accelerated_compositing_active_ &&
1464 animation_update_pending_ &&
1465 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571466 return;
1467
1468 // Perform updating asynchronously. This serves two purposes:
1469 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1470 // on the call stack.
1471 // 2) Allows us to collect more damage rects before painting to help coalesce
1472 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241473 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111474 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211475 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291476}
1477
[email protected]244ac1892011-12-02 17:04:471478void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091479 if (size_.width() != new_size.width || size_.height() != new_size.height) {
[email protected]eac2b362013-05-22 07:01:451480 size_ = new_size;
[email protected]20fbfc22013-05-08 20:50:581481
[email protected]eac2b362013-05-22 07:01:451482 // If we don't clear PaintAggregator after changing autoResize state, then
1483 // we might end up in a situation where bitmap_rect is larger than the
1484 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1485 // with invalid damage rects.
1486 paint_aggregator_.ClearPendingUpdate();
1487
[email protected]70dee7e2013-05-29 18:28:301488 if (RenderThreadImpl::current()->layout_test_mode()) {
[email protected]eac2b362013-05-22 07:01:451489 WebRect new_pos(rootWindowRect().x,
1490 rootWindowRect().y,
1491 new_size.width,
1492 new_size.height);
1493 view_screen_rect_ = new_pos;
1494 window_screen_rect_ = new_pos;
[email protected]8be1c582013-03-06 00:55:031495 }
[email protected]20fbfc22013-05-08 20:50:581496
[email protected]eac2b362013-05-22 07:01:451497 AutoResizeCompositor();
[email protected]20fbfc22013-05-08 20:50:581498
[email protected]70dee7e2013-05-29 18:28:301499 if (!RenderThreadImpl::current()->layout_test_mode())
[email protected]20fbfc22013-05-08 20:50:581500 need_update_rect_for_auto_resize_ = true;
[email protected]ea3ee0a2012-05-15 03:43:091501 }
[email protected]244ac1892011-12-02 17:04:471502}
1503
[email protected]3a1c8a8032013-03-18 22:35:321504void RenderWidget::AutoResizeCompositor() {
[email protected]97e1bf72013-03-06 14:06:051505 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1506 device_scale_factor_));
1507 if (compositor_)
1508 compositor_->setViewportSize(size_, physical_backing_size_);
1509}
1510
[email protected]91acd1c2012-03-14 08:32:391511void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221512 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1513
[email protected]c63b4d42012-04-26 01:01:071514#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211515 if (!is_accelerated_compositing_active_) {
1516 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1517 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1518 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1519 // going to switch to accelerated compositing, the GPU process may need
1520 // round-trips to the browser's UI thread before finishing the frame,
1521 // causing deadlocks if we delay the UpdateRect until we receive the
1522 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071523 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1524 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211525 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1526 }
[email protected]c63b4d42012-04-26 01:01:071527#endif
[email protected]aa4117f2011-12-09 22:19:211528
[email protected]ea162f92011-10-04 23:08:221529 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421530 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241531 routing_id_, is_accelerated_compositing_active_));
[email protected]ea162f92011-10-04 23:08:221532}
1533
1534void RenderWidget::didDeactivateCompositor() {
1535 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1536
1537 is_accelerated_compositing_active_ = false;
1538 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1539 routing_id_, is_accelerated_compositing_active_));
1540
[email protected]ea162f92011-10-04 23:08:221541 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241542 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091543
1544 // In single-threaded mode, we exit force compositing mode and re-enter in
1545 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1546 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1547 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361548 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091549 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561550}
1551
[email protected]e195e582013-03-08 01:32:591552void RenderWidget::initializeLayerTreeView() {
1553 compositor_ = RenderWidgetCompositor::Create(this);
1554 if (!compositor_)
1555 return;
1556
1557 compositor_->setViewportSize(size_, physical_backing_size_);
1558 if (init_complete_)
1559 compositor_->setSurfaceReady();
1560}
1561
[email protected]8926c602013-01-23 05:32:061562WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281563 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061564}
1565
[email protected]9ed83fe2013-02-27 01:52:281566void RenderWidget::suppressCompositorScheduling(bool enable) {
1567 if (compositor_)
1568 compositor_->SetSuppressScheduleComposite(enable);
1569}
1570
[email protected]9cd43a62012-03-26 08:03:561571void RenderWidget::willBeginCompositorFrame() {
1572 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371573
[email protected]cadac622013-06-11 16:46:361574 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy().get());
[email protected]abe8b3a2012-03-28 21:19:371575
1576 // The following two can result in further layout and possibly
1577 // enable GPU acceleration so they need to be called before any painting
1578 // is done.
[email protected]cb9e2632013-06-18 11:26:471579 UpdateTextInputType();
1580#if defined(OS_ANDROID)
[email protected]2d354272013-01-14 00:59:061581 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]cb9e2632013-06-18 11:26:471582#endif
[email protected]abe8b3a2012-03-28 21:19:371583 UpdateSelectionBounds();
1584
[email protected]9cd43a62012-03-26 08:03:561585 WillInitiatePaint();
1586}
1587
[email protected]3391a0772012-03-28 00:32:071588void RenderWidget::didBecomeReadyForAdditionalInput() {
1589 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
[email protected]59383c782013-04-17 16:43:271590 if (pending_input_event_ack_)
[email protected]3391a0772012-03-28 00:32:071591 Send(pending_input_event_ack_.release());
1592}
1593
[email protected]6fceb912013-02-15 06:24:151594void RenderWidget::DidCommitCompositorFrame() {
1595}
1596
[email protected]58264a32011-11-17 23:36:151597void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501598 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351599 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1600 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421601 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU",
1602 TRACE_EVENT_SCOPE_THREAD);
[email protected]29ed96a2012-02-04 18:12:161603 // Notify subclasses that we initiated the paint operation.
1604 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151605}
1606
1607void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181608 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1609
1610 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561611 DidFlushPaint();
1612
[email protected]aa4117f2011-12-09 22:19:211613 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151614 return;
1615
[email protected]ea3ee0a2012-05-15 03:43:091616 if (!next_paint_flags_ &&
1617 !need_update_rect_for_auto_resize_ &&
1618 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151619 return;
[email protected]ea3ee0a2012-05-15 03:43:091620 }
[email protected]58264a32011-11-17 23:36:151621
1622 ViewHostMsg_UpdateRect_Params params;
1623 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151624 params.plugin_window_moves.swap(plugin_window_moves_);
1625 params.flags = next_paint_flags_;
1626 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121627 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091628 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151629
1630 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1631 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091632 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151633}
1634
[email protected]f98d7e3c2010-09-13 22:30:461635void RenderWidget::scheduleComposite() {
[email protected]479b0172012-10-29 19:27:091636 TRACE_EVENT0("gpu", "RenderWidget::scheduleComposite");
[email protected]cadac622013-06-11 16:46:361637 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get() &&
[email protected]ba91a792013-02-06 09:48:281638 compositor_) {
1639 compositor_->setNeedsRedraw();
[email protected]d0be63772011-12-20 23:18:041640 } else {
[email protected]c3d45532011-10-07 19:20:401641 // TODO(nduca): replace with something a little less hacky. The reason this
1642 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1643 // contains a lot of host-renderer synchronization logic that is still
1644 // important for the accelerated compositing case. The option of simply
1645 // duplicating all that code is less desirable than "faking out" the
1646 // invalidation path using a magical damage rect.
1647 didInvalidateRect(WebRect(0, 0, 1, 1));
1648 }
[email protected]f98d7e3c2010-09-13 22:30:461649}
1650
[email protected]5f8b1022011-01-21 23:34:501651void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201652 if (animation_update_pending_)
1653 return;
1654
[email protected]921244e42011-07-20 16:36:301655 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201656 animation_update_pending_ = true;
1657 if (!animation_timer_.IsRunning()) {
1658 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1659 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211660 }
[email protected]5f8b1022011-01-21 23:34:501661}
1662
[email protected]4873c7d2009-07-16 06:36:281663void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301664 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521665 WebCursor cursor;
1666 webkit_glue::InitializeCursorFromWebKitCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291667 // Only send a SetCursor message if we need to make a change.
1668 if (!current_cursor_.IsEqual(cursor)) {
1669 current_cursor_ = cursor;
1670 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1671 }
1672}
1673
1674// We are supposed to get a single call to Show for a newly created RenderWidget
1675// that was created via RenderWidget::CreateWebView. So, we wait until this
1676// point to dispatch the ShowWidget message.
1677//
1678// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281679// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291680//
[email protected]4873c7d2009-07-16 06:36:281681void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291682 DCHECK(!did_show_) << "received extraneous Show call";
1683 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1684 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1685
[email protected]8de12d942010-11-17 20:42:441686 if (did_show_)
1687 return;
1688
1689 did_show_ = true;
1690 // NOTE: initial_pos_ may still have its default values at this point, but
1691 // that's okay. It'll be ignored if as_popup is false, or the browser
1692 // process will impose a default position otherwise.
1693 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1694 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291695}
1696
[email protected]9b003482013-05-21 14:00:171697void RenderWidget::didProgrammaticallyScroll(
1698 const WebKit::WebPoint& scroll_point) {
1699 if (!compositor_)
1700 return;
1701 Send(new ViewHostMsg_DidProgrammaticallyScroll(
1702 routing_id_, gfx::Vector2d(scroll_point.x, scroll_point.y)));
1703}
1704
[email protected]4873c7d2009-07-16 06:36:281705void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291706}
1707
[email protected]4873c7d2009-07-16 06:36:281708void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291709}
1710
[email protected]2533ce12009-05-09 00:02:241711void RenderWidget::DoDeferredClose() {
1712 Send(new ViewHostMsg_Close(routing_id_));
1713}
1714
[email protected]4873c7d2009-07-16 06:36:281715void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321716 if (is_swapped_out_) {
1717 // This widget is currently swapped out, and the active widget is in a
1718 // different process. Have the browser route the close request to the
1719 // active widget instead, so that the correct unload handlers are run.
1720 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1721 return;
1722 }
1723
initial.commit09911bf2008-07-26 23:55:291724 // If a page calls window.close() twice, we'll end up here twice, but that's
1725 // OK. It is safe to send multiple Close messages.
1726
[email protected]2533ce12009-05-09 00:02:241727 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1728 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1729 // could be closed before the JS finishes executing. So instead, post a
1730 // message back to the message loop, which won't run until the JS is
1731 // complete, and then the Close message can be sent.
[email protected]dd32b1272013-05-04 14:17:111732 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211733 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291734}
1735
1736void RenderWidget::Close() {
1737 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061738 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281739 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281740 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291741 webwidget_ = NULL;
1742 }
1743}
1744
[email protected]4873c7d2009-07-16 06:36:281745WebRect RenderWidget::windowRect() {
1746 if (pending_window_rect_count_)
1747 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241748
[email protected]80ad8622012-11-07 16:33:031749 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291750}
1751
[email protected]8a9d6ca32011-06-06 20:11:301752void RenderWidget::setToolTipText(const WebKit::WebString& text,
1753 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541754 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301755}
1756
[email protected]4873c7d2009-07-16 06:36:281757void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291758 if (did_show_) {
[email protected]70dee7e2013-05-29 18:28:301759 if (!RenderThreadImpl::current()->layout_test_mode()) {
[email protected]8be1c582013-03-06 00:55:031760 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
1761 SetPendingWindowRect(pos);
1762 } else {
1763 WebSize new_size(pos.width, pos.height);
[email protected]d9083762013-03-24 01:36:401764 Resize(new_size, new_size, overdraw_bottom_height_,
1765 WebRect(), is_fullscreen_, NO_RESIZE_ACK);
[email protected]8be1c582013-03-06 00:55:031766 view_screen_rect_ = pos;
1767 window_screen_rect_ = pos;
1768 }
initial.commit09911bf2008-07-26 23:55:291769 } else {
1770 initial_pos_ = pos;
1771 }
1772}
1773
[email protected]2533ce12009-05-09 00:02:241774void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1775 pending_window_rect_ = rect;
1776 pending_window_rect_count_++;
1777}
1778
[email protected]4873c7d2009-07-16 06:36:281779WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241780 if (pending_window_rect_count_) {
1781 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1782 // the RootWindowRect is probably going to return wrong results since the
1783 // browser may not have processed the Move yet. There isn't really anything
1784 // good to do in this case, and it shouldn't happen - since this size is
1785 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281786 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241787 }
1788
[email protected]80ad8622012-11-07 16:33:031789 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:371790}
1791
[email protected]4873c7d2009-07-16 06:36:281792WebRect RenderWidget::windowResizerRect() {
1793 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191794}
1795
[email protected]fa7b1dc2010-06-23 17:53:041796void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031797 // To prevent this renderer process from sending unnecessary IPC messages to
1798 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041799 // only during the input method attached to the browser process is active.
1800 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291801}
1802
[email protected]fa7b1dc2010-06-23 17:53:041803void RenderWidget::OnImeSetComposition(
1804 const string16& text,
1805 const std::vector<WebCompositionUnderline>& underlines,
1806 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281807 if (!webwidget_)
1808 return;
[email protected]66fca5bc2013-05-23 06:58:291809 ImeEventGuard guard(this);
[email protected]88dbe32f2013-06-20 23:31:361810 if (!webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041811 text, WebVector<WebCompositionUnderline>(underlines),
1812 selection_start, selection_end)) {
1813 // If we failed to set the composition text, then we need to let the browser
1814 // process to cancel the input method's ongoing composition session, to make
1815 // sure we are in a consistent state.
1816 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:261817 }
[email protected]501ea13d2013-07-09 17:03:291818#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361819 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291820#endif
[email protected]fa7b1dc2010-06-23 17:53:041821}
1822
[email protected]0e45bd02013-07-12 20:20:021823void RenderWidget::OnImeConfirmComposition(const string16& text,
1824 const ui::Range& replacement_range,
1825 bool keep_selection) {
[email protected]d0be63772011-12-20 23:18:041826 if (!webwidget_)
1827 return;
[email protected]66fca5bc2013-05-23 06:58:291828 ImeEventGuard guard(this);
[email protected]d0be63772011-12-20 23:18:041829 handling_input_event_ = true;
[email protected]0e45bd02013-07-12 20:20:021830 if (text.length())
1831 webwidget_->confirmComposition(text);
1832 else if (keep_selection)
1833 webwidget_->confirmComposition(WebWidget::KeepSelection);
1834 else
1835 webwidget_->confirmComposition(WebWidget::DoNotKeepSelection);
[email protected]d0be63772011-12-20 23:18:041836 handling_input_event_ = false;
[email protected]501ea13d2013-07-09 17:03:291837#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:361838 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:291839#endif
initial.commit09911bf2008-07-26 23:55:291840}
1841
[email protected]948f7ab72010-05-28 23:48:081842// This message causes the renderer to render an image of the
1843// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:251844void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
1845 int tag,
1846 const gfx::Size& page_size,
1847 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001848 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1849 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251850 // Close our unused handle.
1851#if defined(OS_WIN)
1852 ::CloseHandle(dib_handle);
1853#elif defined(OS_MACOSX)
1854 base::SharedMemory::CloseHandle(dib_handle);
1855#endif
1856 }
[email protected]d65adb12010-04-28 17:26:491857 return;
[email protected]45c6aad32010-11-11 04:46:251858 }
[email protected]d65adb12010-04-28 17:26:491859
[email protected]948f7ab72010-05-28 23:48:081860 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491861 // If one of these is empty, then we just return the dib we were
1862 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091863 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491864 return;
1865 }
1866
1867 // Map the given DIB ID into this process, and unmap it at the end
1868 // of this function.
[email protected]45c6aad32010-11-11 04:46:251869 scoped_ptr<TransportDIB> paint_at_size_buffer(
1870 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301871
[email protected]4b01b962012-10-09 23:17:351872 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281873 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:351874 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:281875 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:511876 gfx::Size canvas_size = page_size_in_pixel;
1877 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:491878 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:511879 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:491880 static_cast<float>(canvas_size.height());
1881
[email protected]ee8d6fd2010-05-26 17:05:481882 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491883 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1884 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481885 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491886
[email protected]36808ad2010-10-20 19:18:301887 scoped_ptr<skia::PlatformCanvas> canvas(
1888 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1889 canvas_size.height()));
[email protected]59383c782013-04-17 16:43:271890 if (!canvas) {
[email protected]36808ad2010-10-20 19:18:301891 NOTREACHED();
1892 return;
1893 }
1894
[email protected]d65adb12010-04-28 17:26:491895 // Reset bounds to what we actually received, but they should be the
1896 // same.
1897 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1898 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1899 bounds.set_width(canvas->getDevice()->width());
1900 bounds.set_height(canvas->getDevice()->height());
1901
1902 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081903 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491904 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1905
[email protected]948f7ab72010-05-28 23:48:081906 // Have to make sure we're laid out at the right size before
1907 // rendering.
1908 gfx::Size old_size = webwidget_->size();
1909 webwidget_->resize(page_size);
1910 webwidget_->layout();
1911
[email protected]d65adb12010-04-28 17:26:491912 // Paint the entire thing (using original bounds, not scaled bounds).
1913 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1914 canvas->restore();
1915
[email protected]948f7ab72010-05-28 23:48:081916 // Return the widget to its previous size.
1917 webwidget_->resize(old_size);
1918
[email protected]c88c9442010-07-19 18:55:091919 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491920}
1921
[email protected]51a49502013-03-23 01:50:191922void RenderWidget::OnSnapshot(const gfx::Rect& src_subrect) {
1923 SkBitmap snapshot;
1924
1925 if (OnSnapshotHelper(src_subrect, &snapshot)) {
1926 Send(new ViewHostMsg_Snapshot(routing_id(), true, snapshot));
1927 } else {
1928 Send(new ViewHostMsg_Snapshot(routing_id(), false, SkBitmap()));
1929 }
1930}
1931
1932bool RenderWidget::OnSnapshotHelper(const gfx::Rect& src_subrect,
1933 SkBitmap* snapshot) {
1934 base::TimeTicks beginning_time = base::TimeTicks::Now();
1935
1936 if (!webwidget_ || src_subrect.IsEmpty())
1937 return false;
1938
1939 gfx::Rect viewport_size = gfx::IntersectRects(
1940 src_subrect, gfx::Rect(physical_backing_size_));
1941
1942 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
1943 skia::CreatePlatformCanvas(viewport_size.width(),
1944 viewport_size.height(),
1945 true,
1946 NULL,
1947 skia::RETURN_NULL_ON_FAILURE));
[email protected]59383c782013-04-17 16:43:271948 if (!canvas)
[email protected]51a49502013-03-23 01:50:191949 return false;
1950
1951 canvas->save();
1952 webwidget_->layout();
1953
1954 PaintRect(viewport_size, viewport_size.origin(), canvas.get());
1955 canvas->restore();
1956
1957 const SkBitmap& bitmap = skia::GetTopDevice(*canvas)->accessBitmap(false);
1958 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
1959 return false;
1960
1961 UMA_HISTOGRAM_TIMES("Renderer4.Snapshot",
1962 base::TimeTicks::Now() - beginning_time);
1963 return true;
1964}
1965
[email protected]0bc1f572013-04-17 01:46:311966void RenderWidget::OnRepaint(gfx::Size size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:121967 // During shutdown we can just ignore this message.
1968 if (!webwidget_)
1969 return;
1970
[email protected]0bc1f572013-04-17 01:46:311971 // Even if the browser provides an empty damage rect, it's still expecting to
1972 // receive a repaint ack so just damage the entire widget bounds.
1973 if (size_to_paint.IsEmpty()) {
1974 size_to_paint = size_;
1975 }
1976
[email protected]ec7dc112008-08-06 05:30:121977 set_next_paint_is_repaint_ack();
[email protected]0bc1f572013-04-17 01:46:311978 if (is_accelerated_compositing_active_ && compositor_) {
1979 compositor_->SetNeedsRedrawRect(gfx::Rect(size_to_paint));
[email protected]f98d7e3c2010-09-13 22:30:461980 } else {
1981 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1982 didInvalidateRect(repaint_rect);
1983 }
[email protected]ec7dc112008-08-06 05:30:121984}
1985
[email protected]4a9dba42013-04-29 18:24:221986void RenderWidget::OnSmoothScrollCompleted() {
1987 pending_smooth_scroll_gesture_.Run();
[email protected]0e241b4b2012-08-18 09:06:271988}
1989
[email protected]4873c7d2009-07-16 06:36:281990void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111991 if (!webwidget_)
1992 return;
[email protected]4873c7d2009-07-16 06:36:281993 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111994}
1995
[email protected]80ad8622012-11-07 16:33:031996void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
1997 const gfx::Rect& window_screen_rect) {
1998 view_screen_rect_ = view_screen_rect;
1999 window_screen_rect_ = window_screen_rect;
2000 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
2001}
2002
[email protected]105dffb42013-02-20 03:46:212003#if defined(OS_ANDROID)
2004void RenderWidget::OnImeBatchStateChanged(bool is_begin) {
2005 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin));
2006}
[email protected]2384b6c2013-02-28 23:58:512007
2008void RenderWidget::OnShowImeIfNeeded() {
2009 UpdateTextInputState(SHOW_IME_IF_NEEDED);
2010}
[email protected]105dffb42013-02-20 03:46:212011#endif
2012
[email protected]468ac582012-11-20 00:53:192013void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
2014 if (device_scale_factor_ == device_scale_factor)
2015 return;
2016
2017 device_scale_factor_ = device_scale_factor;
2018
2019 if (!is_accelerated_compositing_active_) {
2020 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
2021 } else {
2022 scheduleComposite();
2023 }
2024}
2025
[email protected]719b36f2010-12-22 20:36:462026webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:152027 const gfx::Rect& paint_bounds,
2028 TransportDIB** dib,
2029 gfx::Rect* location,
[email protected]0f3a2d12012-09-01 03:37:202030 gfx::Rect* clip,
2031 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:462032 // Bare RenderWidgets don't support optimized plugin painting.
2033 return NULL;
[email protected]ca4847f2010-09-24 05:39:152034}
2035
[email protected]ceb36f7d2012-10-31 18:33:242036gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:522037 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:242038 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:522039}
2040
[email protected]bee16aab2009-08-26 15:55:032041void RenderWidget::SetHidden(bool hidden) {
2042 if (is_hidden_ == hidden)
2043 return;
2044
2045 // The status has changed. Tell the RenderThread about it.
2046 is_hidden_ = hidden;
2047 if (is_hidden_)
[email protected]380244092011-10-07 17:26:272048 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:032049 else
[email protected]380244092011-10-07 17:26:272050 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:032051}
2052
[email protected]2b624c562011-10-27 22:58:262053void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262054 if (!webwidget_)
2055 return;
2056
2057 if (is_fullscreen_) {
2058 webwidget_->willExitFullScreen();
2059 } else {
2060 webwidget_->willEnterFullScreen();
2061 }
[email protected]2b624c562011-10-27 22:58:262062}
2063
2064void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262065 if (!webwidget_)
2066 return;
2067
2068 if (is_fullscreen_) {
2069 webwidget_->didEnterFullScreen();
2070 } else {
2071 webwidget_->didExitFullScreen();
2072 }
[email protected]2b624c562011-10-27 22:58:262073}
2074
[email protected]699ab0d2009-04-23 23:19:142075void RenderWidget::SetBackground(const SkBitmap& background) {
2076 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:462077
[email protected]699ab0d2009-04-23 23:19:142078 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:282079 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:142080}
2081
[email protected]674741932009-02-04 23:44:462082bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:052083 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462084}
2085
2086bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:052087 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462088}
2089
2090void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:052091 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:462092}
2093
2094void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:052095 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:462096}
2097
2098void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:052099 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:462100}
2101
[email protected]b18583c2012-12-18 06:55:272102static bool IsDateTimeInput(ui::TextInputType type) {
2103 return type == ui::TEXT_INPUT_TYPE_DATE ||
2104 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
2105 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
2106 type == ui::TEXT_INPUT_TYPE_MONTH ||
2107 type == ui::TEXT_INPUT_TYPE_TIME ||
2108 type == ui::TEXT_INPUT_TYPE_WEEK;
2109}
2110
[email protected]66fca5bc2013-05-23 06:58:292111
2112void RenderWidget::StartHandlingImeEvent() {
2113 DCHECK(!handling_ime_event_);
2114 handling_ime_event_ = true;
2115}
2116
2117void RenderWidget::FinishHandlingImeEvent() {
2118 DCHECK(handling_ime_event_);
2119 handling_ime_event_ = false;
2120 // While handling an ime event, text input state and selection bounds updates
2121 // are ignored. These must explicitly be updated once finished handling the
2122 // ime event.
2123 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472124#if defined(OS_ANDROID)
[email protected]66fca5bc2013-05-23 06:58:292125 UpdateTextInputState(DO_NOT_SHOW_IME);
[email protected]cb9e2632013-06-18 11:26:472126#endif
[email protected]66fca5bc2013-05-23 06:58:292127}
2128
[email protected]cb9e2632013-06-18 11:26:472129void RenderWidget::UpdateTextInputType() {
2130 if (!input_method_is_active_)
2131 return;
2132
[email protected]e7c569d2013-07-17 16:00:362133 ui::TextInputType new_type = GetTextInputType();
[email protected]cb9e2632013-06-18 11:26:472134 if (IsDateTimeInput(new_type))
2135 return; // Not considered as a text input field in WebKit/Chromium.
2136
2137 bool new_can_compose_inline = CanComposeInline();
2138
[email protected]e7c569d2013-07-17 16:00:362139 WebKit::WebTextInputInfo new_info;
2140 if (webwidget_)
2141 new_info = webwidget_->textInputInfo();
[email protected]b256eca2013-07-11 10:57:402142 const ui::TextInputMode new_mode = ConvertInputMode(new_info.inputMode);
2143
[email protected]cb9e2632013-06-18 11:26:472144 if (text_input_type_ != new_type
[email protected]b256eca2013-07-11 10:57:402145 || can_compose_inline_ != new_can_compose_inline
2146 || text_input_mode_ != new_mode) {
[email protected]cb9e2632013-06-18 11:26:472147 Send(new ViewHostMsg_TextInputTypeChanged(routing_id(),
2148 new_type,
[email protected]b256eca2013-07-11 10:57:402149 new_can_compose_inline,
2150 new_mode));
[email protected]cb9e2632013-06-18 11:26:472151 text_input_type_ = new_type;
2152 can_compose_inline_ = new_can_compose_inline;
[email protected]b256eca2013-07-11 10:57:402153 text_input_mode_ = new_mode;
[email protected]cb9e2632013-06-18 11:26:472154 }
2155}
2156
2157#if defined(OS_ANDROID)
[email protected]3306f262012-09-21 19:20:422158void RenderWidget::UpdateTextInputState(ShowIme show_ime) {
[email protected]e8f775f2013-02-14 21:00:502159 if (handling_ime_event_)
2160 return;
[email protected]3306f262012-09-21 19:20:422161 bool show_ime_if_needed = (show_ime == SHOW_IME_IF_NEEDED);
2162 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:292163 return;
[email protected]ad26ef42011-06-17 07:59:452164 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:272165 if (IsDateTimeInput(new_type))
2166 return; // Not considered as a text input field in WebKit/Chromium.
2167
[email protected]5b739cb2012-08-21 20:35:212168 WebKit::WebTextInputInfo new_info;
2169 if (webwidget_)
2170 new_info = webwidget_->textInputInfo();
2171
[email protected]ad26ef42011-06-17 07:59:452172 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212173
[email protected]3306f262012-09-21 19:20:422174 // Only sends text input params if they are changed or if the ime should be
2175 // shown.
2176 if (show_ime_if_needed || (text_input_type_ != new_type
2177 || text_input_info_ != new_info
2178 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212179 ViewHostMsg_TextInputState_Params p;
2180 p.type = new_type;
2181 p.value = new_info.value.utf8();
2182 p.selection_start = new_info.selectionStart;
2183 p.selection_end = new_info.selectionEnd;
2184 p.composition_start = new_info.compositionStart;
2185 p.composition_end = new_info.compositionEnd;
2186 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422187 p.show_ime_if_needed = show_ime_if_needed;
[email protected]5b739cb2012-08-21 20:35:212188 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2189
2190 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042191 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452192 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292193 }
initial.commit09911bf2008-07-26 23:55:292194}
[email protected]cb9e2632013-06-18 11:26:472195#endif
initial.commit09911bf2008-07-26 23:55:292196
[email protected]7c8873e2013-02-05 08:03:012197void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2198 WebRect focus_webrect;
2199 WebRect anchor_webrect;
2200 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2201 *focus = focus_webrect;
2202 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322203}
2204
[email protected]e99ef6f2011-10-16 01:13:002205void RenderWidget::UpdateSelectionBounds() {
2206 if (!webwidget_)
2207 return;
[email protected]66fca5bc2013-05-23 06:58:292208 if (handling_ime_event_)
2209 return;
[email protected]e99ef6f2011-10-16 01:13:002210
[email protected]7c8873e2013-02-05 08:03:012211 ViewHostMsg_SelectionBounds_Params params;
2212 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2213 if (selection_anchor_rect_ != params.anchor_rect ||
2214 selection_focus_rect_ != params.focus_rect) {
2215 selection_anchor_rect_ = params.anchor_rect;
2216 selection_focus_rect_ = params.focus_rect;
2217 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292218 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012219 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352220 }
[email protected]501ea13d2013-07-09 17:03:292221#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362222 UpdateCompositionInfo(false);
[email protected]501ea13d2013-07-09 17:03:292223#endif
[email protected]e99ef6f2011-10-16 01:13:002224}
2225
[email protected]73bf95812011-10-12 11:38:322226// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452227COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2228 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2229COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2230 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2231COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2232 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182233COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2234 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2235COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2236 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2237COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2238 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2239COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2240 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2241COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2242 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002243COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2244 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2245COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2246 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2247COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2248 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2249COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2250 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2251COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2252 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2253COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2254 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012255COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2256 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2257COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2258 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152259COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2260 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452261
[email protected]5b739cb2012-08-21 20:35:212262ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2263 WebKit::WebTextInputType type) {
2264 // Check the type is in the range representable by ui::TextInputType.
2265 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2266 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2267 return static_cast<ui::TextInputType>(type);
2268}
2269
[email protected]ad26ef42011-06-17 07:59:452270ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272271 if (webwidget_)
2272 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452273 return ui::TEXT_INPUT_TYPE_NONE;
2274}
2275
[email protected]501ea13d2013-07-09 17:03:292276#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
2277void RenderWidget::UpdateCompositionInfo(bool should_update_range) {
2278 ui::Range range = ui::Range();
2279 if (should_update_range) {
2280 GetCompositionRange(&range);
2281 } else {
2282 range = composition_range_;
2283 }
2284 std::vector<gfx::Rect> character_bounds;
2285 GetCompositionCharacterBounds(&character_bounds);
2286
2287 if (!ShouldUpdateCompositionInfo(range, character_bounds))
2288 return;
2289 composition_character_bounds_ = character_bounds;
2290 composition_range_ = range;
2291 Send(new ViewHostMsg_ImeCompositionRangeChanged(
2292 routing_id(), composition_range_, composition_character_bounds_));
2293}
2294
[email protected]58b48a0d2012-06-13 07:01:352295void RenderWidget::GetCompositionCharacterBounds(
2296 std::vector<gfx::Rect>* bounds) {
2297 DCHECK(bounds);
2298 bounds->clear();
2299}
2300
[email protected]88dbe32f2013-06-20 23:31:362301void RenderWidget::GetCompositionRange(ui::Range* range) {
2302 size_t location, length;
2303 if (webwidget_->compositionRange(&location, &length)) {
2304 range->set_start(location);
2305 range->set_end(location + length);
2306 } else if (webwidget_->caretOrSelectionRange(&location, &length)) {
2307 range->set_start(location);
2308 range->set_end(location + length);
2309 } else {
2310 *range = ui::Range::InvalidRange();
2311 }
2312}
2313
[email protected]501ea13d2013-07-09 17:03:292314bool RenderWidget::ShouldUpdateCompositionInfo(
2315 const ui::Range& range,
2316 const std::vector<gfx::Rect>& bounds) {
2317 if (composition_range_ != range)
2318 return true;
2319 if (bounds.size() != composition_character_bounds_.size())
2320 return true;
2321 for (size_t i = 0; i < bounds.size(); ++i) {
2322 if (bounds[i] != composition_character_bounds_[i])
2323 return true;
2324 }
2325 return false;
2326}
2327#endif
2328
[email protected]ad26ef42011-06-17 07:59:452329bool RenderWidget::CanComposeInline() {
2330 return true;
[email protected]56ea1a62011-05-30 07:05:572331}
2332
[email protected]4873c7d2009-07-16 06:36:282333WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042334 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282335}
2336
[email protected]f660d9c2012-06-06 18:31:212337float RenderWidget::deviceScaleFactor() {
2338 return device_scale_factor_;
2339}
2340
[email protected]fa7b1dc2010-06-23 17:53:042341void RenderWidget::resetInputMethod() {
2342 if (!input_method_is_active_)
2343 return;
2344
[email protected]0e45bd02013-07-12 20:20:022345 ImeEventGuard guard(this);
[email protected]fa7b1dc2010-06-23 17:53:042346 // If the last text input type is not None, then we should finish any
2347 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452348 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042349 // If a composition text exists, then we need to let the browser process
2350 // to cancel the input method's ongoing composition session.
2351 if (webwidget_->confirmComposition())
2352 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2353 }
[email protected]d4cff272011-05-02 15:46:012354
[email protected]501ea13d2013-07-09 17:03:292355#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362356 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292357#endif
[email protected]fa7b1dc2010-06-23 17:53:042358}
2359
[email protected]c68c3e4e2013-01-24 00:36:562360void RenderWidget::didHandleGestureEvent(
2361 const WebGestureEvent& event,
2362 bool event_cancelled) {
2363#if defined(OS_ANDROID)
2364 if (event_cancelled)
2365 return;
2366 if (event.type == WebInputEvent::GestureTap ||
2367 event.type == WebInputEvent::GestureLongPress) {
2368 UpdateTextInputState(SHOW_IME_IF_NEEDED);
2369 }
2370#endif
2371}
2372
[email protected]29e2fb42013-07-19 01:13:472373void RenderWidget::SchedulePluginMove(const WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292374 size_t i = 0;
2375 for (; i < plugin_window_moves_.size(); ++i) {
2376 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582377 if (move.rects_valid) {
2378 plugin_window_moves_[i] = move;
2379 } else {
2380 plugin_window_moves_[i].visible = move.visible;
2381 }
initial.commit09911bf2008-07-26 23:55:292382 break;
2383 }
2384 }
2385
2386 if (i == plugin_window_moves_.size())
2387 plugin_window_moves_.push_back(move);
2388}
[email protected]268654772009-08-06 23:02:042389
2390void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2391 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2392 i != plugin_window_moves_.end(); ++i) {
2393 if (i->window == window) {
2394 plugin_window_moves_.erase(i);
2395 break;
2396 }
2397 }
2398}
[email protected]67bfb83f2011-09-22 03:36:372399
[email protected]b63d58d2012-11-26 22:37:442400void RenderWidget::GetRenderingStats(
2401 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282402 if (compositor_)
[email protected]635353c2013-03-06 09:11:202403 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442404
[email protected]62049562013-03-24 00:39:012405 stats.rendering_stats.animation_frame_count +=
2406 software_stats_.animation_frame_count;
2407 stats.rendering_stats.screen_frame_count +=
2408 software_stats_.screen_frame_count;
2409 stats.rendering_stats.total_paint_time +=
2410 software_stats_.total_paint_time;
2411 stats.rendering_stats.total_pixels_painted +=
2412 software_stats_.total_pixels_painted;
[email protected]fef5e3972012-08-07 03:59:472413}
2414
[email protected]e9ff79c2012-10-19 21:31:262415bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522416 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2417 if (!gpu_channel)
2418 return false;
2419
2420 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2421}
2422
[email protected]24ed0432013-04-24 07:50:312423RenderWidgetCompositor* RenderWidget::compositor() const {
2424 return compositor_.get();
2425}
2426
[email protected]3639aa82013-06-04 11:00:042427void RenderWidget::OnSetBrowserRenderingStats(
2428 const BrowserRenderingStats& stats) {
2429 browser_rendering_stats_ = stats;
2430}
2431
2432void RenderWidget::GetBrowserRenderingStats(BrowserRenderingStats* stats) {
2433 *stats = browser_rendering_stats_;
2434}
2435
[email protected]0c2ebef2013-04-03 12:14:102436void RenderWidget::BeginSmoothScroll(
[email protected]0e241b4b2012-08-18 09:06:272437 bool down,
[email protected]ebd8b562012-10-09 14:44:292438 const SmoothScrollCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192439 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292440 int mouse_event_x,
2441 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272442 DCHECK(!callback.is_null());
[email protected]267909d2012-10-20 04:36:192443
2444 ViewHostMsg_BeginSmoothScroll_Params params;
2445 params.scroll_down = down;
2446 params.pixels_to_scroll = pixels_to_scroll;
2447 params.mouse_event_x = mouse_event_x;
2448 params.mouse_event_y = mouse_event_y;
2449
[email protected]4a9dba42013-04-29 18:24:222450 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, params));
2451 pending_smooth_scroll_gesture_ = callback;
[email protected]a39ca1652012-07-13 21:30:582452}
2453
[email protected]67bfb83f2011-09-22 03:36:372454bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2455 return false;
2456}
[email protected]c3d45532011-10-07 19:20:402457
[email protected]f56c7872013-06-18 12:31:572458bool RenderWidget::WillHandleKeyEvent(const WebKit::WebKeyboardEvent& event) {
2459 return false;
2460}
2461
[email protected]41d86852012-11-07 12:23:242462bool RenderWidget::WillHandleGestureEvent(
2463 const WebKit::WebGestureEvent& event) {
2464 return false;
2465}
2466
[email protected]ce6689f2013-03-29 12:52:552467void RenderWidget::hasTouchEventHandlers(bool has_handlers) {
2468 Send(new ViewHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
2469}
2470
[email protected]3d5c243b2012-11-30 00:26:012471bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2472 return true;
2473}
2474
[email protected]3ae68c52013-04-12 06:10:052475WebGraphicsContext3DCommandBufferImpl* RenderWidget::CreateGraphicsContext3D(
[email protected]92fd8c02013-03-29 08:54:152476 const WebKit::WebGraphicsContext3D::Attributes& attributes) {
[email protected]ed7defa2013-03-12 21:29:592477 if (!webwidget_)
2478 return NULL;
[email protected]8f746982013-03-21 06:28:032479 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2480 new WebGraphicsContext3DCommandBufferImpl(
2481 surface_id(),
2482 GetURLForGraphicsContext3D(),
2483 RenderThreadImpl::current(),
2484 weak_ptr_factory_.GetWeakPtr()));
[email protected]ed7defa2013-03-12 21:29:592485
[email protected]990e4c212013-06-08 05:03:332486 if (!context->InitializeWithDefaultBufferSizes(
[email protected]8f746982013-03-21 06:28:032487 attributes,
2488 false /* bind generates resources */,
2489 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE))
2490 return NULL;
2491 return context.release();
[email protected]ed7defa2013-03-12 21:29:592492}
2493
[email protected]e9ff79c2012-10-19 21:31:262494} // namespace content