blob: 218556abd89a2685f1f383ef17a2854662159299 [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]adbe30f2013-10-11 21:12:3319#include "cc/debug/benchmark_instrumentation.h"
[email protected]7f0d825f2013-03-18 07:24:3020#include "cc/output/output_surface.h"
[email protected]556fd292013-03-18 08:03:0421#include "cc/trees/layer_tree_host.h"
[email protected]29e2fb42013-07-19 01:13:4722#include "content/child/npapi/webplugin.h"
[email protected]0634cdd42013-08-16 00:46:0923#include "content/common/gpu/client/context_provider_command_buffer.h"
[email protected]ed7defa2013-03-12 21:29:5924#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]8e299aa2013-10-16 18:17:4425#include "content/common/input/web_input_event_traits.h"
[email protected]c084330e02013-04-27 01:08:1526#include "content/common/input_messages.h"
[email protected]992db4c2011-05-12 15:37:1527#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5028#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2929#include "content/public/common/content_switches.h"
[email protected]953bd0062013-08-01 00:58:4030#include "content/renderer/cursor_utils.h"
[email protected]b2e4c70132013-10-03 02:07:5131#include "content/renderer/external_popup_menu.h"
[email protected]ed7defa2013-03-12 21:29:5932#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]2847b222013-04-06 00:59:2433#include "content/renderer/gpu/compositor_software_output_device.h"
[email protected]36e5ff12013-06-11 12:19:2934#include "content/renderer/gpu/delegated_compositor_output_surface.h"
[email protected]ea5f70a2013-03-07 12:30:3635#include "content/renderer/gpu/input_handler_manager.h"
[email protected]ed7defa2013-03-12 21:29:5936#include "content/renderer/gpu/mailbox_output_surface.h"
[email protected]ba91a792013-02-06 09:48:2837#include "content/renderer/gpu/render_widget_compositor.h"
[email protected]66fca5bc2013-05-23 06:58:2938#include "content/renderer/ime_event_guard.h"
[email protected]adab2332013-07-25 18:04:3239#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
[email protected]8704f89b2011-04-15 00:30:0540#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4441#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1942#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1843#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4844#include "skia/ext/platform_canvas.h"
[email protected]aaf68892013-07-18 00:11:3045#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
[email protected]aaf68892013-07-18 00:11:3046#include "third_party/WebKit/public/platform/WebRect.h"
47#include "third_party/WebKit/public/platform/WebSize.h"
48#include "third_party/WebKit/public/platform/WebString.h"
[email protected]2255a9332013-06-17 05:12:3149#include "third_party/WebKit/public/web/WebCursorInfo.h"
50#include "third_party/WebKit/public/web/WebHelperPlugin.h"
51#include "third_party/WebKit/public/web/WebPagePopup.h"
52#include "third_party/WebKit/public/web/WebPopupMenu.h"
53#include "third_party/WebKit/public/web/WebPopupMenuInfo.h"
54#include "third_party/WebKit/public/web/WebRange.h"
55#include "third_party/WebKit/public/web/WebScreenInfo.h"
[email protected]d353541f2012-05-03 22:45:4156#include "third_party/skia/include/core/SkShader.h"
[email protected]faec7b12012-06-19 14:42:1357#include "ui/base/ui_base_switches.h"
[email protected]a25e25b2012-09-28 14:32:3758#include "ui/gfx/rect_conversions.h"
[email protected]4b01b962012-10-09 23:17:3559#include "ui/gfx/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:4860#include "ui/gfx/skia_util.h"
[email protected]c9e2cbbb2012-05-12 21:17:2761#include "ui/gl/gl_switches.h"
[email protected]d353541f2012-05-03 22:45:4162#include "ui/surface/transport_dib.h"
[email protected]799fd732013-05-15 21:18:5263#include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h"
[email protected]661eb9d2009-02-03 02:11:4864
[email protected]eeb93112013-05-01 19:41:1065#if defined(OS_ANDROID)
[email protected]b6eb8e332013-09-10 00:51:0166#include "base/android/sys_utils.h"
[email protected]913d99a2013-05-31 07:16:0767#include "content/renderer/android/synchronous_compositor_factory.h"
[email protected]eeb93112013-05-01 19:41:1068#endif
69
[email protected]661eb9d2009-02-03 02:11:4870#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4971#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5272#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:4173#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4874#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4475
[email protected]2255a9332013-06-17 05:12:3176#include "third_party/WebKit/public/web/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2977
[email protected]fa7b1dc2010-06-23 17:53:0478using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3079using WebKit::WebCursorInfo;
[email protected]41d86852012-11-07 12:23:2480using WebKit::WebGestureEvent;
[email protected]62cb33cae2009-03-27 23:30:2281using WebKit::WebInputEvent;
[email protected]f56c7872013-06-18 12:31:5782using WebKit::WebKeyboardEvent;
[email protected]6a8ddba52010-09-05 04:38:0683using WebKit::WebMouseEvent;
[email protected]b2e4c70132013-10-03 02:07:5184using WebKit::WebMouseWheelEvent;
[email protected]4873c7d2009-07-16 06:36:2885using WebKit::WebNavigationPolicy;
[email protected]a7547fb2012-03-08 04:43:4486using WebKit::WebPagePopup;
[email protected]4873c7d2009-07-16 06:36:2887using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5988using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1889using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0190using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5291using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1992using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5293using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2894using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2495using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0496using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1897using WebKit::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:2698
[email protected]6a4d7f62013-01-07 21:32:1399namespace {
[email protected]b256eca2013-07-11 10:57:40100
101typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
102
103class TextInputModeMapSingleton {
104 public:
105 static TextInputModeMapSingleton* GetInstance() {
106 return Singleton<TextInputModeMapSingleton>::get();
107 }
108 TextInputModeMapSingleton()
109 : map() {
110 map["verbatim"] = ui::TEXT_INPUT_MODE_VERBATIM;
111 map["latin"] = ui::TEXT_INPUT_MODE_LATIN;
112 map["latin-name"] = ui::TEXT_INPUT_MODE_LATIN_NAME;
113 map["latin-prose"] = ui::TEXT_INPUT_MODE_LATIN_PROSE;
114 map["full-width-latin"] = ui::TEXT_INPUT_MODE_FULL_WIDTH_LATIN;
115 map["kana"] = ui::TEXT_INPUT_MODE_KANA;
116 map["katakana"] = ui::TEXT_INPUT_MODE_KATAKANA;
117 map["numeric"] = ui::TEXT_INPUT_MODE_NUMERIC;
118 map["tel"] = ui::TEXT_INPUT_MODE_TEL;
119 map["email"] = ui::TEXT_INPUT_MODE_EMAIL;
120 map["url"] = ui::TEXT_INPUT_MODE_URL;
121 }
122 TextInputModeMap& Map() {
123 return map;
124 }
125 private:
126 TextInputModeMap map;
127
128 friend struct DefaultSingletonTraits<TextInputModeMapSingleton>;
129
130 DISALLOW_COPY_AND_ASSIGN(TextInputModeMapSingleton);
131};
132
133ui::TextInputMode ConvertInputMode(
134 const WebKit::WebString& input_mode) {
135 static TextInputModeMapSingleton* singleton =
136 TextInputModeMapSingleton::GetInstance();
137 TextInputModeMap::iterator it = singleton->Map().find(input_mode.utf8());
138 if (it == singleton->Map().end())
139 return ui::TEXT_INPUT_MODE_DEFAULT;
140 return it->second;
[email protected]6a4d7f62013-01-07 21:32:13141}
[email protected]b256eca2013-07-11 10:57:40142
143} // namespace
144
[email protected]e9ff79c2012-10-19 21:31:26145namespace content {
[email protected]62cb33cae2009-03-27 23:30:22146
[email protected]b2e4c70132013-10-03 02:07:51147// RenderWidget::ScreenMetricsEmulator ----------------------------------------
148
149class RenderWidget::ScreenMetricsEmulator {
150 public:
151 ScreenMetricsEmulator(
152 RenderWidget* widget,
153 const gfx::Size& device_size,
154 const gfx::Rect& widget_rect,
155 float device_scale_factor,
156 bool fit_to_view);
157 virtual ~ScreenMetricsEmulator();
158
159 float scale() { return scale_; }
160 gfx::Rect widget_rect() const { return widget_rect_; }
161 gfx::Rect original_screen_rect() const { return original_view_screen_rect_; }
162
163 void ChangeEmulationParams(
164 const gfx::Size& device_size,
165 const gfx::Rect& widget_rect,
166 float device_scale_factor,
167 bool fit_to_view);
168
169 // The following methods alter handlers' behavior for messages related to
170 // widget size and position.
171 void OnResizeMessage(const ViewMsg_Resize_Params& params);
172 void OnUpdateScreenRectsMessage(const gfx::Rect& view_screen_rect,
173 const gfx::Rect& window_screen_rect);
174 void OnShowContextMenu(ContextMenuParams* params);
175
176 private:
177 void Apply(float overdraw_bottom_height,
178 gfx::Rect resizer_rect, bool is_fullscreen);
179
180 RenderWidget* widget_;
181
182 // Parameters as passed by RenderWidget::EmulateScreenMetrics.
183 gfx::Size device_size_;
184 gfx::Rect widget_rect_;
185 float device_scale_factor_;
186 bool fit_to_view_;
187
188 // The computed scaled used to fit widget into browser window.
189 float scale_;
190
191 // Original values to restore back after emulation ends.
192 gfx::Size original_size_;
193 gfx::Size original_physical_backing_size_;
194 WebKit::WebScreenInfo original_screen_info_;
195 gfx::Rect original_view_screen_rect_;
196 gfx::Rect original_window_screen_rect_;
197};
198
199RenderWidget::ScreenMetricsEmulator::ScreenMetricsEmulator(
200 RenderWidget* widget,
201 const gfx::Size& device_size,
202 const gfx::Rect& widget_rect,
203 float device_scale_factor,
204 bool fit_to_view)
205 : widget_(widget),
206 device_size_(device_size),
207 widget_rect_(widget_rect),
208 device_scale_factor_(device_scale_factor),
209 fit_to_view_(fit_to_view),
210 scale_(1.f) {
211 original_size_ = widget_->size_;
212 original_physical_backing_size_ = widget_->physical_backing_size_;
213 original_screen_info_ = widget_->screen_info_;
214 original_view_screen_rect_ = widget_->view_screen_rect_;
215 original_window_screen_rect_ = widget_->window_screen_rect_;
216 Apply(widget_->overdraw_bottom_height_,
217 widget_->resizer_rect_, widget_->is_fullscreen_);
218}
219
220RenderWidget::ScreenMetricsEmulator::~ScreenMetricsEmulator() {
221 widget_->screen_info_ = original_screen_info_;
222
223 widget_->SetDeviceScaleFactor(original_screen_info_.deviceScaleFactor);
224 widget_->SetScreenMetricsEmulationParameters(0.f, 1.f);
225 widget_->view_screen_rect_ = original_view_screen_rect_;
226 widget_->window_screen_rect_ = original_window_screen_rect_;
227 widget_->Resize(original_size_, original_physical_backing_size_,
228 widget_->overdraw_bottom_height_, widget_->resizer_rect_,
229 widget_->is_fullscreen_, NO_RESIZE_ACK);
230}
231
232void RenderWidget::ScreenMetricsEmulator::ChangeEmulationParams(
233 const gfx::Size& device_size,
234 const gfx::Rect& widget_rect,
235 float device_scale_factor,
236 bool fit_to_view) {
237 device_size_ = device_size;
238 widget_rect_ = widget_rect;
239 device_scale_factor_ = device_scale_factor;
240 fit_to_view_ = fit_to_view;
241 Apply(widget_->overdraw_bottom_height_,
242 widget_->resizer_rect_, widget_->is_fullscreen_);
243}
244
245void RenderWidget::ScreenMetricsEmulator::Apply(
246 float overdraw_bottom_height, gfx::Rect resizer_rect, bool is_fullscreen) {
247 if (fit_to_view_) {
248 DCHECK(!original_size_.IsEmpty());
249 float width_ratio =
250 static_cast<float>(widget_rect_.width()) / original_size_.width();
251 float height_ratio =
252 static_cast<float>(widget_rect_.height()) / original_size_.height();
253 float ratio = std::max(1.0f, std::max(width_ratio, height_ratio));
254 scale_ = 1.f / ratio;
255 } else {
256 scale_ = 1.f;
257 }
258
259 widget_->screen_info_.rect = gfx::Rect(device_size_);
260 widget_->screen_info_.availableRect = gfx::Rect(device_size_);
261 widget_->screen_info_.deviceScaleFactor = device_scale_factor_;
262
263 // Pass two emulation parameters to the blink side:
264 // - we keep the real device scale factor in compositor to produce sharp image
265 // even when emulating different scale factor;
266 // - in order to fit into view, WebView applies scaling transform to the
267 // root layer.
268 widget_->SetScreenMetricsEmulationParameters(
269 original_screen_info_.deviceScaleFactor, scale_);
270
271 widget_->SetDeviceScaleFactor(device_scale_factor_);
272 widget_->view_screen_rect_ = widget_rect_;
273 widget_->window_screen_rect_ = widget_->screen_info_.availableRect;
274
275 gfx::Size physical_backing_size = gfx::ToCeiledSize(gfx::ScaleSize(
276 original_size_, original_screen_info_.deviceScaleFactor));
277 widget_->Resize(widget_rect_.size(), physical_backing_size,
278 overdraw_bottom_height, resizer_rect, is_fullscreen, NO_RESIZE_ACK);
279}
280
281void RenderWidget::ScreenMetricsEmulator::OnResizeMessage(
282 const ViewMsg_Resize_Params& params) {
283 bool need_ack = params.new_size != original_size_ &&
284 !params.new_size.IsEmpty() && !params.physical_backing_size.IsEmpty();
285 original_size_ = params.new_size;
286 original_physical_backing_size_ = params.physical_backing_size;
287 original_screen_info_ = params.screen_info;
288 Apply(params.overdraw_bottom_height, params.resizer_rect,
289 params.is_fullscreen);
290
291 if (need_ack) {
292 widget_->set_next_paint_is_resize_ack();
293 if (widget_->compositor_)
294 widget_->compositor_->SetNeedsRedrawRect(gfx::Rect(widget_->size_));
295 }
296}
297
298void RenderWidget::ScreenMetricsEmulator::OnUpdateScreenRectsMessage(
299 const gfx::Rect& view_screen_rect,
300 const gfx::Rect& window_screen_rect) {
301 original_view_screen_rect_ = view_screen_rect;
302 original_window_screen_rect_ = window_screen_rect;
303}
304
305void RenderWidget::ScreenMetricsEmulator::OnShowContextMenu(
306 ContextMenuParams* params) {
307 params->x *= scale_;
308 params->y *= scale_;
309}
310
[email protected]03e88672013-10-22 21:31:32311// RenderWidget::ResizingModeSelector ------------------------------------------
312
313class RenderWidget::ResizingModeSelector {
314 public:
315 static bool ShouldAbortOnResize(RenderWidget* widget,
316 const ViewMsg_Resize_Params& params);
317 static bool IsLegacyMode();
318};
319
320bool RenderWidget::ResizingModeSelector::ShouldAbortOnResize(
321 RenderWidget* widget,
322 const ViewMsg_Resize_Params& params) {
323 return RenderThreadImpl::current() &&
324 RenderThreadImpl::current()->layout_test_mode() &&
325 params.is_fullscreen == widget->is_fullscreen_ &&
326 params.screen_info.deviceScaleFactor ==
327 widget->screen_info_.deviceScaleFactor;
328}
329
330bool RenderWidget::ResizingModeSelector::IsLegacyMode() {
331 return RenderThreadImpl::current() && // Will be NULL during unit tests.
332 RenderThreadImpl::current()->layout_test_mode();
333}
334
[email protected]b2e4c70132013-10-03 02:07:51335// RenderWidget ---------------------------------------------------------------
336
[email protected]6fd35b72012-03-01 19:46:41337RenderWidget::RenderWidget(WebKit::WebPopupType popup_type,
[email protected]842f10652012-06-06 01:54:04338 const WebKit::WebScreenInfo& screen_info,
[email protected]1ac10dca2013-08-20 20:47:04339 bool swapped_out,
340 bool hidden)
initial.commit09911bf2008-07-26 23:55:29341 : routing_id_(MSG_ROUTING_NONE),
[email protected]9f4f3322012-01-18 22:29:56342 surface_id_(0),
[email protected]c5b3b5e2009-02-13 06:41:11343 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:29344 opener_id_(MSG_ROUTING_NONE),
[email protected]fc4404d2012-11-07 19:53:30345 init_complete_(false),
[email protected]b4d08452010-10-05 17:34:35346 current_paint_buf_(NULL),
[email protected]d9083762013-03-24 01:36:40347 overdraw_bottom_height_(0.f),
initial.commit09911bf2008-07-26 23:55:29348 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:06349 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:05350 update_reply_pending_(false),
[email protected]847a2582013-03-09 02:29:51351 auto_resize_mode_(false),
[email protected]ea3ee0a2012-05-15 03:43:09352 need_update_rect_for_auto_resize_(false),
[email protected]65225772011-05-12 21:10:24353 using_asynchronous_swapbuffers_(false),
354 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:29355 did_show_(false),
[email protected]1ac10dca2013-08-20 20:47:04356 is_hidden_(hidden),
[email protected]ee41e7d22011-10-14 19:34:09357 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:29358 needs_repainting_on_restore_(false),
359 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:49360 handling_input_event_(false),
[email protected]e8f775f2013-02-14 21:00:50361 handling_ime_event_(false),
[email protected]661eb9d2009-02-03 02:11:48362 closing_(false),
[email protected]14392a52012-05-02 20:28:44363 is_swapped_out_(swapped_out),
[email protected]fa7b1dc2010-06-23 17:53:04364 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:45365 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40366 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
[email protected]86ba5fcb2013-09-04 00:36:53367 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:12368 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:48369 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:12370 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:50371 is_accelerated_compositing_active_(false),
[email protected]d8a6c5d12013-09-23 04:37:10372 was_accelerated_compositing_ever_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:21373 animation_update_pending_(false),
[email protected]4b03e292012-02-13 18:40:07374 invalidation_task_posted_(false),
[email protected]842f10652012-06-06 01:54:04375 screen_info_(screen_info),
[email protected]3d779472012-11-15 20:49:52376 device_scale_factor_(screen_info_.deviceScaleFactor),
[email protected]ed7defa2013-03-12 21:29:59377 is_threaded_compositing_enabled_(false),
[email protected]53b4cc12013-07-18 23:02:30378 next_output_surface_id_(0),
[email protected]0d1ebed12013-08-05 22:01:13379#if defined(OS_ANDROID)
380 outstanding_ime_acks_(0),
381#endif
[email protected]b2e4c70132013-10-03 02:07:51382 popup_origin_scale_for_emulation_(0.f),
[email protected]69e797f2013-04-30 01:10:22383 weak_ptr_factory_(this) {
[email protected]8b3f0eb2012-05-03 19:15:05384 if (!swapped_out)
385 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27386 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18387 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
388 switches::kDisableGpuVsync);
[email protected]cb6430932012-10-31 00:53:36389 is_threaded_compositing_enabled_ =
390 CommandLine::ForCurrentProcess()->HasSwitch(
391 switches::kEnableThreadedCompositing);
[email protected]c1e6cc062013-08-24 03:35:35392
393 legacy_software_mode_stats_ = cc::RenderingStatsInstrumentation::Create();
394 if (CommandLine::ForCurrentProcess()->HasSwitch(
395 switches::kEnableGpuBenchmarking))
396 legacy_software_mode_stats_->set_record_rendering_stats(true);
initial.commit09911bf2008-07-26 23:55:29397}
398
399RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11400 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21401 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35402 if (current_paint_buf_) {
[email protected]467414eb2013-07-30 20:32:29403 if (RenderProcess::current()) {
404 // If the RenderProcess is already gone, it will have released all DIBs
405 // in its destructor anyway.
406 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
407 }
[email protected]b4d08452010-10-05 17:34:35408 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29409 }
[email protected]992db4c2011-05-12 15:37:15410 // If we are swapped out, we have released already.
[email protected]d2e2f9ee2013-08-21 11:02:02411 if (!is_swapped_out_ && RenderProcess::current())
[email protected]992db4c2011-05-12 15:37:15412 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29413}
414
[email protected]484955942010-08-19 16:13:18415// static
[email protected]8085dbc82008-09-26 22:53:44416RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]842f10652012-06-06 01:54:04417 WebKit::WebPopupType popup_type,
418 const WebKit::WebScreenInfo& screen_info) {
initial.commit09911bf2008-07-26 23:55:29419 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]6fd35b72012-03-01 19:46:41420 scoped_refptr<RenderWidget> widget(
[email protected]1ac10dca2013-08-20 20:47:04421 new RenderWidget(popup_type, screen_info, false, false));
[email protected]a635f942012-12-07 10:34:29422 if (widget->Init(opener_id)) { // adds reference on success.
[email protected]fc72bb12013-06-02 21:13:46423 return widget.get();
[email protected]a635f942012-12-07 10:34:29424 }
425 return NULL;
initial.commit09911bf2008-07-26 23:55:29426}
427
[email protected]484955942010-08-19 16:13:18428// static
429WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
430 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03431 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18432 break;
433 case WebKit::WebPopupTypeSelect:
434 case WebKit::WebPopupTypeSuggestion:
435 return WebPopupMenu::create(render_widget);
[email protected]a7547fb2012-03-08 04:43:44436 case WebKit::WebPopupTypePage:
437 return WebPagePopup::create(render_widget);
[email protected]4b1146bc2012-07-10 18:46:03438 case WebKit::WebPopupTypeHelperPlugin:
439 return WebKit::WebHelperPlugin::create(render_widget);
[email protected]484955942010-08-19 16:13:18440 default:
441 NOTREACHED();
442 }
443 return NULL;
444}
445
[email protected]a635f942012-12-07 10:34:29446bool RenderWidget::Init(int32 opener_id) {
447 return DoInit(opener_id,
448 RenderWidget::CreateWebWidget(this),
449 new ViewHostMsg_CreateWidget(opener_id, popup_type_,
450 &routing_id_, &surface_id_));
[email protected]484955942010-08-19 16:13:18451}
452
[email protected]a635f942012-12-07 10:34:29453bool RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06454 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18455 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29456 DCHECK(!webwidget_);
457
458 if (opener_id != MSG_ROUTING_NONE)
459 opener_id_ = opener_id;
460
[email protected]484955942010-08-19 16:13:18461 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29462
[email protected]380244092011-10-07 17:26:27463 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29464 if (result) {
[email protected]380244092011-10-07 17:26:27465 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29466 // Take a reference on behalf of the RenderThread. This will be balanced
467 // when we receive ViewMsg_Close.
468 AddRef();
[email protected]1ac10dca2013-08-20 20:47:04469 if (is_hidden_)
470 RenderThread::Get()->WidgetHidden();
[email protected]a635f942012-12-07 10:34:29471 return true;
initial.commit09911bf2008-07-26 23:55:29472 } else {
[email protected]a635f942012-12-07 10:34:29473 // The above Send can fail when the tab is closing.
474 return false;
initial.commit09911bf2008-07-26 23:55:29475 }
476}
477
[email protected]fc4404d2012-11-07 19:53:30478// This is used to complete pending inits and non-pending inits.
479void RenderWidget::CompleteInit() {
initial.commit09911bf2008-07-26 23:55:29480 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29481
[email protected]fc4404d2012-11-07 19:53:30482 init_complete_ = true;
[email protected]05a980d7a2012-02-07 22:16:42483
[email protected]8926c602013-01-23 05:32:06484 if (webwidget_ && is_threaded_compositing_enabled_) {
485 webwidget_->enterForceCompositingMode(true);
486 }
[email protected]ba91a792013-02-06 09:48:28487 if (compositor_) {
488 compositor_->setSurfaceReady();
[email protected]479b0172012-10-29 19:27:09489 }
[email protected]05a980d7a2012-02-07 22:16:42490 DoDeferredUpdate();
initial.commit09911bf2008-07-26 23:55:29491
[email protected]6de74452009-02-25 18:04:59492 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29493}
494
[email protected]992db4c2011-05-12 15:37:15495void RenderWidget::SetSwappedOut(bool is_swapped_out) {
496 // We should only toggle between states.
497 DCHECK(is_swapped_out_ != is_swapped_out);
498 is_swapped_out_ = is_swapped_out;
499
500 // If we are swapping out, we will call ReleaseProcess, allowing the process
501 // to exit if all of its RenderViews are swapped out. We wait until the
502 // WasSwappedOut call to do this, to avoid showing the sad tab.
503 // If we are swapping in, we call AddRefProcess to prevent the process from
504 // exiting.
505 if (!is_swapped_out)
506 RenderProcess::current()->AddRefProcess();
507}
508
[email protected]34bb3ac2013-03-08 02:41:28509bool RenderWidget::AllowPartialSwap() const {
510 return true;
511}
512
[email protected]c8cbae72013-05-23 10:45:03513bool RenderWidget::UsingSynchronousRendererCompositor() const {
[email protected]ccc1722e2013-05-06 19:43:07514#if defined(OS_ANDROID)
[email protected]913d99a2013-05-31 07:16:07515 return SynchronousCompositorFactory::GetInstance() != NULL;
516#else
[email protected]ccc1722e2013-05-06 19:43:07517 return false;
[email protected]913d99a2013-05-31 07:16:07518#endif
[email protected]ccc1722e2013-05-06 19:43:07519}
520
[email protected]b2e4c70132013-10-03 02:07:51521void RenderWidget::EnableScreenMetricsEmulation(
522 const gfx::Size& device_size,
523 const gfx::Rect& widget_rect,
524 float device_scale_factor,
525 bool fit_to_view) {
526 if (!screen_metrics_emulator_) {
527 screen_metrics_emulator_.reset(new ScreenMetricsEmulator(this,
528 device_size, widget_rect, device_scale_factor, fit_to_view));
529 } else {
530 screen_metrics_emulator_->ChangeEmulationParams(device_size,
531 widget_rect, device_scale_factor, fit_to_view);
532 }
533}
534
535void RenderWidget::DisableScreenMetricsEmulation() {
536 screen_metrics_emulator_.reset();
537}
538
539void RenderWidget::SetPopupOriginAdjustmentsForEmulation(
540 ScreenMetricsEmulator* emulator) {
541 popup_origin_scale_for_emulation_ = emulator->scale();
542 popup_view_origin_for_emulation_ = emulator->widget_rect().origin();
543 popup_screen_origin_for_emulation_ =
544 emulator->original_screen_rect().origin();
545}
546
547void RenderWidget::SetScreenMetricsEmulationParameters(
548 float device_scale_factor, float root_layer_scale) {
549 // This is only supported in RenderView.
550 NOTREACHED();
551}
552
553void RenderWidget::SetExternalPopupOriginAdjustmentsForEmulation(
554 ExternalPopupMenu* popup, ScreenMetricsEmulator* emulator) {
555 popup->SetOriginScaleForEmulation(emulator->scale());
556}
557
558void RenderWidget::OnShowHostContextMenu(ContextMenuParams* params) {
559 if (screen_metrics_emulator_)
560 screen_metrics_emulator_->OnShowContextMenu(params);
561}
562
[email protected]7d08a9352013-10-15 08:24:56563void RenderWidget::ScheduleCompositeWithForcedRedraw() {
564 if (compositor_) {
565 // Regardless of whether threaded compositing is enabled, always
566 // use this mechanism to force the compositor to redraw. However,
567 // the invalidation code path below is still needed for the
568 // non-threaded case.
569 compositor_->SetNeedsForcedRedraw();
570 }
571 ScheduleCompositeImpl(true);
572}
573
574void RenderWidget::ScheduleCompositeImpl(bool force_redraw) {
575 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get() &&
576 compositor_) {
577 if (!force_redraw) {
578 compositor_->setNeedsRedraw();
579 }
580 } else {
581 // TODO(nduca): replace with something a little less hacky. The reason this
582 // hack is still used is because the Invalidate-DoDeferredUpdate loop
583 // contains a lot of host-renderer synchronization logic that is still
584 // important for the accelerated compositing case. The option of simply
585 // duplicating all that code is less desirable than "faking out" the
586 // invalidation path using a magical damage rect.
587 didInvalidateRect(WebRect(0, 0, 1, 1));
588 }
589}
590
[email protected]a95986a82010-12-24 06:19:28591bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
592 bool handled = true;
593 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
[email protected]c084330e02013-04-27 01:08:15594 IPC_MESSAGE_HANDLER(InputMsg_HandleInputEvent, OnHandleInputEvent)
[email protected]34202de2013-05-06 23:36:22595 IPC_MESSAGE_HANDLER(InputMsg_CursorVisibilityChange,
596 OnCursorVisibilityChange)
[email protected]c084330e02013-04-27 01:08:15597 IPC_MESSAGE_HANDLER(InputMsg_MouseCaptureLost, OnMouseCaptureLost)
598 IPC_MESSAGE_HANDLER(InputMsg_SetFocus, OnSetFocus)
[email protected]a95986a82010-12-24 06:19:28599 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
600 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
601 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
[email protected]b5913d72012-02-07 22:26:54602 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect)
[email protected]a95986a82010-12-24 06:19:28603 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
[email protected]9e2e4632012-07-27 16:38:41604 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown)
[email protected]992db4c2011-05-12 15:37:15605 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28606 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
[email protected]ed7defa2013-03-12 21:29:59607 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK,
608 OnViewContextSwapBuffersComplete)
[email protected]a95986a82010-12-24 06:19:28609 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
610 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
611 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
[email protected]3d9ec5052013-01-02 22:05:25612 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize)
613 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint)
[email protected]79fa22e2013-08-23 15:18:12614 IPC_MESSAGE_HANDLER(ViewMsg_SyntheticGestureCompleted,
615 OnSyntheticGestureCompleted)
[email protected]a95986a82010-12-24 06:19:28616 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
617 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]80ad8622012-11-07 16:33:03618 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects)
[email protected]105dffb42013-02-20 03:46:21619#if defined(OS_ANDROID)
[email protected]2384b6c2013-02-28 23:58:51620 IPC_MESSAGE_HANDLER(ViewMsg_ShowImeIfNeeded, OnShowImeIfNeeded)
[email protected]0d1ebed12013-08-05 22:01:13621 IPC_MESSAGE_HANDLER(ViewMsg_ImeEventAck, OnImeEventAck)
[email protected]105dffb42013-02-20 03:46:21622#endif
[email protected]51a49502013-03-23 01:50:19623 IPC_MESSAGE_HANDLER(ViewMsg_Snapshot, OnSnapshot)
[email protected]3639aa82013-06-04 11:00:04624 IPC_MESSAGE_HANDLER(ViewMsg_SetBrowserRenderingStats,
625 OnSetBrowserRenderingStats)
[email protected]a95986a82010-12-24 06:19:28626 IPC_MESSAGE_UNHANDLED(handled = false)
627 IPC_END_MESSAGE_MAP()
628 return handled;
629}
initial.commit09911bf2008-07-26 23:55:29630
631bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15632 // Don't send any messages after the browser has told us to close, and filter
633 // most outgoing messages while swapped out.
634 if ((is_swapped_out_ &&
[email protected]e9ff79c2012-10-19 21:31:26635 !SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
[email protected]c6c921e92012-05-10 23:31:11636 closing_) {
initial.commit09911bf2008-07-26 23:55:29637 delete message;
638 return false;
639 }
640
641 // If given a messsage without a routing ID, then assign our routing ID.
642 if (message->routing_id() == MSG_ROUTING_NONE)
643 message->set_routing_id(routing_id_);
644
[email protected]380244092011-10-07 17:26:27645 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44646}
647
[email protected]61e2b3cc2012-03-02 16:13:34648void RenderWidget::Resize(const gfx::Size& new_size,
[email protected]60d47ac2013-03-01 23:42:44649 const gfx::Size& physical_backing_size,
[email protected]d9083762013-03-24 01:36:40650 float overdraw_bottom_height,
[email protected]61e2b3cc2012-03-02 16:13:34651 const gfx::Rect& resizer_rect,
652 bool is_fullscreen,
653 ResizeAck resize_ack) {
[email protected]03e88672013-10-22 21:31:32654 if (!ResizingModeSelector::IsLegacyMode()) {
[email protected]1c0008842013-06-06 08:35:48655 // A resize ack shouldn't be requested if we have not ACK'd the previous
656 // one.
657 DCHECK(resize_ack != SEND_RESIZE_ACK || !next_paint_is_resize_ack());
658 DCHECK(resize_ack == SEND_RESIZE_ACK || resize_ack == NO_RESIZE_ACK);
659 }
initial.commit09911bf2008-07-26 23:55:29660
[email protected]61e2b3cc2012-03-02 16:13:34661 // Ignore this during shutdown.
662 if (!webwidget_)
663 return;
664
[email protected]d9083762013-03-24 01:36:40665 if (compositor_) {
[email protected]60d47ac2013-03-01 23:42:44666 compositor_->setViewportSize(new_size, physical_backing_size);
[email protected]d9083762013-03-24 01:36:40667 compositor_->SetOverdrawBottomHeight(overdraw_bottom_height);
668 }
[email protected]60d47ac2013-03-01 23:42:44669
[email protected]dade8992013-03-04 07:34:34670 physical_backing_size_ = physical_backing_size;
[email protected]d9083762013-03-24 01:36:40671 overdraw_bottom_height_ = overdraw_bottom_height;
[email protected]61e2b3cc2012-03-02 16:13:34672 resizer_rect_ = resizer_rect;
673
674 // NOTE: We may have entered fullscreen mode without changing our size.
675 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
676 if (fullscreen_change)
677 WillToggleFullscreen();
678 is_fullscreen_ = is_fullscreen;
679
680 if (size_ != new_size) {
681 // TODO(darin): We should not need to reset this here.
[email protected]61e2b3cc2012-03-02 16:13:34682 needs_repainting_on_restore_ = false;
683
684 size_ = new_size;
685
686 paint_aggregator_.ClearPendingUpdate();
687
688 // When resizing, we want to wait to paint before ACK'ing the resize. This
689 // ensures that we only resize as fast as we can paint. We only need to
690 // send an ACK if we are resized to a non-empty rect.
691 webwidget_->resize(new_size);
[email protected]0b70dbe2013-05-10 19:06:32692
[email protected]03e88672013-10-22 21:31:32693 if (!ResizingModeSelector::IsLegacyMode()) {
[email protected]92201e32013-06-08 06:14:52694 // Resize should have caused an invalidation of the entire view.
695 DCHECK(new_size.IsEmpty() || is_accelerated_compositing_active_ ||
696 paint_aggregator_.HasPendingUpdate());
697 }
[email protected]03e88672013-10-22 21:31:32698 } else if (!ResizingModeSelector::IsLegacyMode()) {
[email protected]632c4382013-05-15 08:58:45699 resize_ack = NO_RESIZE_ACK;
700 }
701
702 if (new_size.IsEmpty() || physical_backing_size.IsEmpty()) {
703 // For empty size or empty physical_backing_size, there is no next paint
704 // (along with which to send the ack) until they are set to non-empty.
[email protected]ff475a322012-03-14 00:05:35705 resize_ack = NO_RESIZE_ACK;
[email protected]61e2b3cc2012-03-02 16:13:34706 }
707
[email protected]20fbfc22013-05-08 20:50:58708 // Send the Resize_ACK flag once we paint again if requested.
[email protected]632c4382013-05-15 08:58:45709 if (resize_ack == SEND_RESIZE_ACK)
[email protected]20fbfc22013-05-08 20:50:58710 set_next_paint_is_resize_ack();
711
[email protected]61e2b3cc2012-03-02 16:13:34712 if (fullscreen_change)
713 DidToggleFullscreen();
714
715 // If a resize ack is requested and it isn't set-up, then no more resizes will
716 // come in and in general things will go wrong.
[email protected]632c4382013-05-15 08:58:45717 DCHECK(resize_ack != SEND_RESIZE_ACK || next_paint_is_resize_ack());
initial.commit09911bf2008-07-26 23:55:29718}
719
720void RenderWidget::OnClose() {
721 if (closing_)
722 return;
723 closing_ = true;
724
725 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03726 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27727 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03728 SetHidden(false);
729 }
initial.commit09911bf2008-07-26 23:55:29730
initial.commit09911bf2008-07-26 23:55:29731 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25732 // now. Post a task that only gets invoked when there are no nested message
733 // loops.
[email protected]dd32b1272013-05-04 14:17:11734 base::MessageLoop::current()->PostNonNestableTask(
[email protected]3a5a7822011-12-23 18:27:29735 FROM_HERE, base::Bind(&RenderWidget::Close, this));
[email protected]d3fc25652009-02-24 22:31:25736
737 // Balances the AddRef taken when we called AddRoute.
738 Release();
initial.commit09911bf2008-07-26 23:55:29739}
740
[email protected]61e2b3cc2012-03-02 16:13:34741// Got a response from the browser after the renderer decided to create a new
742// view.
[email protected]fc4404d2012-11-07 19:53:30743void RenderWidget::OnCreatingNewAck() {
[email protected]61e2b3cc2012-03-02 16:13:34744 DCHECK(routing_id_ != MSG_ROUTING_NONE);
745
[email protected]fc4404d2012-11-07 19:53:30746 CompleteInit();
[email protected]61e2b3cc2012-03-02 16:13:34747}
748
[email protected]0fdd5012013-05-29 08:05:56749void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) {
[email protected]03e88672013-10-22 21:31:32750 if (ResizingModeSelector::ShouldAbortOnResize(this, params))
751 return;
752
[email protected]b2e4c70132013-10-03 02:07:51753 if (screen_metrics_emulator_) {
754 screen_metrics_emulator_->OnResizeMessage(params);
755 return;
756 }
757
[email protected]0fdd5012013-05-29 08:05:56758 screen_info_ = params.screen_info;
759 SetDeviceScaleFactor(screen_info_.deviceScaleFactor);
760 Resize(params.new_size, params.physical_backing_size,
761 params.overdraw_bottom_height, params.resizer_rect,
762 params.is_fullscreen, SEND_RESIZE_ACK);
initial.commit09911bf2008-07-26 23:55:29763}
764
[email protected]b5913d72012-02-07 22:26:54765void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) {
766 if (resizer_rect_ != resizer_rect) {
[email protected]b9769d82012-02-10 00:23:59767 gfx::Rect view_rect(size_);
768
[email protected]ce112fe2012-10-29 22:52:18769 gfx::Rect old_damage_rect = gfx::IntersectRects(view_rect, resizer_rect_);
[email protected]b9769d82012-02-10 00:23:59770 if (!old_damage_rect.IsEmpty())
771 paint_aggregator_.InvalidateRect(old_damage_rect);
772
[email protected]ce112fe2012-10-29 22:52:18773 gfx::Rect new_damage_rect = gfx::IntersectRects(view_rect, resizer_rect);
[email protected]b9769d82012-02-10 00:23:59774 if (!new_damage_rect.IsEmpty())
775 paint_aggregator_.InvalidateRect(new_damage_rect);
776
[email protected]b5913d72012-02-07 22:26:54777 resizer_rect_ = resizer_rect;
[email protected]b9769d82012-02-10 00:23:59778
[email protected]b5913d72012-02-07 22:26:54779 if (webwidget_)
780 webwidget_->didChangeWindowResizerRect();
781 }
782}
783
initial.commit09911bf2008-07-26 23:55:29784void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31785 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29786 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03787 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29788}
789
[email protected]9e2e4632012-07-27 16:38:41790void RenderWidget::OnWasShown(bool needs_repainting) {
791 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
initial.commit09911bf2008-07-26 23:55:29792 // During shutdown we can just ignore this message.
793 if (!webwidget_)
794 return;
795
796 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03797 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29798
799 if (!needs_repainting && !needs_repainting_on_restore_)
800 return;
801 needs_repainting_on_restore_ = false;
802
[email protected]d65adb12010-04-28 17:26:49803 // Tag the next paint as a restore ack, which is picked up by
804 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29805 set_next_paint_is_restore_ack();
806
807 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56808 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46809 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
810 } else {
811 scheduleComposite();
812 }
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]992db4c2011-05-12 15:37:15815void RenderWidget::OnWasSwappedOut() {
816 // If we have been swapped out and no one else is using this process,
817 // it's safe to exit now. If we get swapped back in, we will call
818 // AddRefProcess in SetSwappedOut.
819 if (is_swapped_out_)
820 RenderProcess::current()->ReleaseProcess();
821}
822
[email protected]53d3f302009-12-21 04:42:05823void RenderWidget::OnRequestMoveAck() {
824 DCHECK(pending_window_rect_count_);
825 pending_window_rect_count_--;
826}
827
828void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58829 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21830 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05831 update_reply_pending_ = false;
832
[email protected]b4d08452010-10-05 17:34:35833 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
834 // have no current paint buffer.
835 if (current_paint_buf_) {
836 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
837 current_paint_buf_ = NULL;
838 }
839
[email protected]65225772011-05-12 21:10:24840 // If swapbuffers is still pending, then defer the update until the
841 // swapbuffers occurs.
842 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
843 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
844 return;
845 }
846
[email protected]29ed96a2012-02-04 18:12:16847 // Notify subclasses that software rendering was flushed to the screen.
[email protected]404939f2012-06-01 04:06:18848 if (!is_accelerated_compositing_active_) {
849 DidFlushPaint();
850 }
[email protected]a2f6bc112009-06-27 16:27:25851
initial.commit09911bf2008-07-26 23:55:29852 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24853 DoDeferredUpdateAndSendInputAck();
854}
855
[email protected]d0be63772011-12-20 23:18:04856bool RenderWidget::SupportsAsynchronousSwapBuffers() {
[email protected]ed7defa2013-03-12 21:29:59857 // Contexts using the command buffer support asynchronous swapbuffers.
858 // See RenderWidget::CreateOutputSurface().
[email protected]cadac622013-06-11 16:46:36859 if (RenderThreadImpl::current()->compositor_message_loop_proxy().get())
[email protected]ed7defa2013-03-12 21:29:59860 return false;
861
862 return true;
863}
864
865GURL RenderWidget::GetURLForGraphicsContext3D() {
866 return GURL();
[email protected]65225772011-05-12 21:10:24867}
868
[email protected]479b0172012-10-29 19:27:09869bool RenderWidget::ForceCompositingModeEnabled() {
870 return false;
871}
872
[email protected]ebc0e1df2013-08-01 02:46:22873scoped_ptr<cc::OutputSurface> RenderWidget::CreateOutputSurface(bool fallback) {
[email protected]a1811b8912013-05-09 15:35:19874
875#if defined(OS_ANDROID)
[email protected]b6eb8e332013-09-10 00:51:01876 if (SynchronousCompositorFactory* factory =
877 SynchronousCompositorFactory::GetInstance()) {
[email protected]913d99a2013-05-31 07:16:07878 return factory->CreateOutputSurface(routing_id());
[email protected]a1811b8912013-05-09 15:35:19879 }
880#endif
881
[email protected]ed7defa2013-03-12 21:29:59882 // Explicitly disable antialiasing for the compositor. As of the time of
883 // this writing, the only platform that supported antialiasing for the
884 // compositor was Mac OS X, because the on-screen OpenGL context creation
885 // code paths on Windows and Linux didn't yet have multisampling support.
886 // Mac OS X essentially always behaves as though it's rendering offscreen.
887 // Multisampling has a heavy cost especially on devices with relatively low
888 // fill rate like most notebooks, and the Mac implementation would need to
889 // be optimized to resolve directly into the IOSurface shared between the
890 // GPU and browser processes. For these reasons and to avoid platform
891 // disparities we explicitly disable antialiasing.
892 WebKit::WebGraphicsContext3D::Attributes attributes;
893 attributes.antialias = false;
894 attributes.shareResources = true;
895 attributes.noAutomaticFlushes = true;
[email protected]a6886502013-05-16 20:59:18896 attributes.depth = false;
897 attributes.stencil = false;
[email protected]b6eb8e332013-09-10 00:51:01898
899 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]bec084292013-05-21 21:31:44900 if (command_line.HasSwitch(cc::switches::kForceDirectLayerDrawing))
901 attributes.stencil = true;
[email protected]b6eb8e332013-09-10 00:51:01902
[email protected]0634cdd42013-08-16 00:46:09903 scoped_refptr<ContextProviderCommandBuffer> context_provider;
904 if (!fallback) {
905 context_provider = ContextProviderCommandBuffer::Create(
[email protected]af13d832013-09-13 06:56:27906 CreateGraphicsContext3D(attributes),
907 "RenderCompositor");
[email protected]0634cdd42013-08-16 00:46:09908 }
[email protected]ebc0e1df2013-08-01 02:46:22909
[email protected]b6eb8e332013-09-10 00:51:01910 uint32 output_surface_id = next_output_surface_id_++;
[email protected]0634cdd42013-08-16 00:46:09911 if (!context_provider.get()) {
[email protected]ebc0e1df2013-08-01 02:46:22912 if (!command_line.HasSwitch(switches::kEnableSoftwareCompositing))
913 return scoped_ptr<cc::OutputSurface>();
[email protected]0634cdd42013-08-16 00:46:09914
915 scoped_ptr<cc::SoftwareOutputDevice> software_device(
916 new CompositorSoftwareOutputDevice());
917
918 return scoped_ptr<cc::OutputSurface>(new CompositorOutputSurface(
919 routing_id(),
920 output_surface_id,
921 NULL,
922 software_device.Pass(),
923 true));
[email protected]ebc0e1df2013-08-01 02:46:22924 }
[email protected]ed7defa2013-03-12 21:29:59925
[email protected]0c04d1c2013-07-10 00:02:32926 if (command_line.HasSwitch(switches::kEnableDelegatedRenderer) &&
927 !command_line.HasSwitch(switches::kDisableDelegatedRenderer)) {
[email protected]36e5ff12013-06-11 12:19:29928 DCHECK(is_threaded_compositing_enabled_);
929 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09930 new DelegatedCompositorOutputSurface(
931 routing_id(),
932 output_surface_id,
933 context_provider,
934 scoped_ptr<cc::SoftwareOutputDevice>()));
[email protected]36e5ff12013-06-11 12:19:29935 }
936 if (command_line.HasSwitch(cc::switches::kCompositeToMailbox)) {
937 DCHECK(is_threaded_compositing_enabled_);
[email protected]186f09602013-09-24 07:13:16938 cc::ResourceFormat format = cc::RGBA_8888;
939#if defined(OS_ANDROID)
940 if (base::android::SysUtils::IsLowEndDevice())
941 format = cc::RGB_565;
942#endif
[email protected]36e5ff12013-06-11 12:19:29943 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09944 new MailboxOutputSurface(
945 routing_id(),
946 output_surface_id,
947 context_provider,
[email protected]186f09602013-09-24 07:13:16948 scoped_ptr<cc::SoftwareOutputDevice>(),
949 format));
[email protected]36e5ff12013-06-11 12:19:29950 }
[email protected]0634cdd42013-08-16 00:46:09951 bool use_swap_compositor_frame_message = false;
[email protected]36e5ff12013-06-11 12:19:29952 return scoped_ptr<cc::OutputSurface>(
[email protected]0634cdd42013-08-16 00:46:09953 new CompositorOutputSurface(
954 routing_id(),
955 output_surface_id,
956 context_provider,
957 scoped_ptr<cc::SoftwareOutputDevice>(),
958 use_swap_compositor_frame_message));
[email protected]ba91a792013-02-06 09:48:28959}
960
[email protected]ed7defa2013-03-12 21:29:59961void RenderWidget::OnViewContextSwapBuffersAborted() {
[email protected]65225772011-05-12 21:10:24962 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21963 while (!updates_pending_swap_.empty()) {
964 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
965 updates_pending_swap_.pop_front();
966 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
967 // compositing pass, hence doesn't require an UpdateRect message.
968 if (msg)
969 Send(msg);
970 }
[email protected]65225772011-05-12 21:10:24971 num_swapbuffers_complete_pending_ = 0;
972 using_asynchronous_swapbuffers_ = false;
973 // Schedule another frame so the compositor learns about it.
974 scheduleComposite();
975}
976
[email protected]ed7defa2013-03-12 21:29:59977void RenderWidget::OnViewContextSwapBuffersPosted() {
[email protected]37a6f302011-07-11 23:43:08978 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21979
980 if (using_asynchronous_swapbuffers_) {
981 ViewHostMsg_UpdateRect* msg = NULL;
982 // pending_update_params_ can be NULL if the swap doesn't correspond to an
983 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
984 // message.
[email protected]59383c782013-04-17 16:43:27985 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:21986 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
987 pending_update_params_.reset();
988 }
989 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08990 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21991 }
[email protected]37a6f302011-07-11 23:43:08992}
993
[email protected]ed7defa2013-03-12 21:29:59994void RenderWidget::OnViewContextSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24995 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
[email protected]29ed96a2012-02-04 18:12:16996
[email protected]404939f2012-06-01 04:06:18997 // Notify subclasses that composited rendering was flushed to the screen.
[email protected]29ed96a2012-02-04 18:12:16998 DidFlushPaint();
999
[email protected]65225772011-05-12 21:10:241000 // When compositing deactivates, we reset the swapbuffers pending count. The
1001 // swapbuffers acks may still arrive, however.
1002 if (num_swapbuffers_complete_pending_ == 0) {
1003 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
1004 return;
1005 }
[email protected]aa4117f2011-12-09 22:19:211006 DCHECK(!updates_pending_swap_.empty());
1007 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
1008 updates_pending_swap_.pop_front();
1009 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
1010 // compositing pass, hence doesn't require an UpdateRect message.
1011 if (msg)
1012 Send(msg);
[email protected]65225772011-05-12 21:10:241013 num_swapbuffers_complete_pending_--;
1014
1015 // If update reply is still pending, then defer the update until that reply
1016 // occurs.
[email protected]d0be63772011-12-20 23:18:041017 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241018 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1019 return;
1020 }
1021
1022 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:061023 // when we were previously rendering. However, if an invalidation task is not
1024 // posted, there may be software rendering work pending. In that case, don't
1025 // early out.
1026 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:241027 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
1028 return;
1029 }
1030
[email protected]cc66e682012-10-02 06:48:181031 // Do not call DoDeferredUpdate unless there's animation work to be done or
1032 // a real invalidation. This prevents rendering in response to a swapbuffers
1033 // callback coming back after we've navigated away from the page that
1034 // generated it.
1035 if (!animation_update_pending_ && !paint_aggregator_.HasPendingUpdate()) {
1036 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
1037 return;
1038 }
1039
[email protected]65225772011-05-12 21:10:241040 // Continue painting if necessary...
1041 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:291042}
1043
[email protected]0dea1652012-12-14 00:09:091044void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event,
[email protected]4b157662013-05-29 04:05:051045 const ui::LatencyInfo& latency_info,
[email protected]0dea1652012-12-14 00:09:091046 bool is_keyboard_shortcut) {
[email protected]5dd768212009-08-13 23:34:491047 handling_input_event_ = true;
[email protected]0dea1652012-12-14 00:09:091048 if (!input_event) {
[email protected]5dd768212009-08-13 23:34:491049 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:291050 return;
[email protected]5dd768212009-08-13 23:34:491051 }
initial.commit09911bf2008-07-26 23:55:291052
[email protected]8e299aa2013-10-16 18:17:441053 const char* const event_name =
1054 WebInputEventTraits::GetName(input_event->type);
[email protected]b4841e1c2013-05-16 22:30:101055 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent",
1056 "event", event_name);
1057
[email protected]c2eaa8f2013-05-10 02:41:551058 if (compositor_)
1059 compositor_->SetLatencyInfo(latency_info);
[email protected]256737c2013-06-08 04:39:101060 else
1061 latency_info_.MergeWith(latency_info);
[email protected]c2eaa8f2013-05-10 02:41:551062
[email protected]6a4d7f62013-01-07 21:32:131063 base::TimeDelta now = base::TimeDelta::FromInternalValue(
1064 base::TimeTicks::Now().ToInternalValue());
1065
1066 int64 delta = static_cast<int64>(
1067 (now.InSecondsF() - input_event->timeStampSeconds) *
1068 base::Time::kMicrosecondsPerSecond);
1069 UMA_HISTOGRAM_CUSTOM_COUNTS("Event.Latency.Renderer", delta, 0, 1000000, 100);
[email protected]de415552013-01-23 04:12:171070 base::HistogramBase* counter_for_type =
[email protected]bafdc5d52013-02-27 18:18:481071 base::Histogram::FactoryGet(
[email protected]b4841e1c2013-05-16 22:30:101072 base::StringPrintf("Event.Latency.Renderer.%s", event_name),
[email protected]bafdc5d52013-02-27 18:18:481073 0,
1074 1000000,
[email protected]6a4d7f62013-01-07 21:32:131075 100,
[email protected]de415552013-01-23 04:12:171076 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]bafdc5d52013-02-27 18:18:481077 counter_for_type->Add(delta);
[email protected]6a4d7f62013-01-07 21:32:131078
[email protected]67bfb83f2011-09-22 03:36:371079 bool prevent_default = false;
1080 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:261081 const WebMouseEvent& mouse_event =
1082 *static_cast<const WebMouseEvent*>(input_event);
1083 TRACE_EVENT2("renderer", "HandleMouseMove",
1084 "x", mouse_event.x, "y", mouse_event.y);
1085 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:371086 }
1087
[email protected]f56c7872013-06-18 12:31:571088 if (WebInputEvent::isKeyboardEventType(input_event->type)) {
1089 const WebKeyboardEvent& key_event =
1090 *static_cast<const WebKeyboardEvent*>(input_event);
1091 prevent_default = WillHandleKeyEvent(key_event);
1092 }
1093
[email protected]41d86852012-11-07 12:23:241094 if (WebInputEvent::isGestureEventType(input_event->type)) {
1095 const WebGestureEvent& gesture_event =
1096 *static_cast<const WebGestureEvent*>(input_event);
1097 prevent_default = prevent_default || WillHandleGestureEvent(gesture_event);
1098 }
1099
[email protected]3ebcc7c2013-01-09 05:34:461100 if (input_event->type == WebInputEvent::GestureTap ||
1101 input_event->type == WebInputEvent::GestureLongPress)
1102 resetInputMethod();
1103
[email protected]67bfb83f2011-09-22 03:36:371104 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:121105 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
1106 suppress_next_char_events_ = false;
1107 if (!processed && webwidget_)
1108 processed = webwidget_->handleInputEvent(*input_event);
1109 }
1110
1111 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
1112 // it's not processed by webkit, then we need to suppress the upcoming Char
1113 // events.
1114 if (!processed && is_keyboard_shortcut)
1115 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:291116
[email protected]3d5c243b2012-11-30 00:26:011117 InputEventAckState ack_result = processed ?
1118 INPUT_EVENT_ACK_STATE_CONSUMED : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
1119 if (!processed && input_event->type == WebInputEvent::TouchStart) {
1120 const WebTouchEvent& touch_event =
1121 *static_cast<const WebTouchEvent*>(input_event);
1122 ack_result = HasTouchEventHandlersAt(touch_event.touches[0].position) ?
1123 INPUT_EVENT_ACK_STATE_NOT_CONSUMED :
1124 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
1125 }
1126
[email protected]a9fb30aa2011-10-06 06:58:461127 IPC::Message* response =
[email protected]f6df0edf2013-08-07 00:27:021128 new InputHostMsg_HandleInputEvent_ACK(routing_id_,
1129 input_event->type,
1130 ack_result,
1131 latency_info);
[email protected]3391a0772012-03-28 00:32:071132 bool event_type_gets_rate_limited =
1133 input_event->type == WebInputEvent::MouseMove ||
1134 input_event->type == WebInputEvent::MouseWheel ||
[email protected]3475bb52013-10-03 08:25:161135 input_event->type == WebInputEvent::TouchMove;
[email protected]8926c602013-01-23 05:32:061136
1137 bool frame_pending = paint_aggregator_.HasPendingUpdate();
1138 if (is_accelerated_compositing_active_) {
[email protected]ba91a792013-02-06 09:48:281139 frame_pending = compositor_ &&
1140 compositor_->commitRequested();
[email protected]8926c602013-01-23 05:32:061141 }
1142
[email protected]9a8ce7f92013-06-11 12:39:491143 if (event_type_gets_rate_limited && frame_pending && !is_hidden_) {
[email protected]12fbad812009-09-01 18:21:241144 // We want to rate limit the input events in this case, so we'll wait for
1145 // painting to finish before ACKing this message.
[email protected]59383c782013-04-17 16:43:271146 if (pending_input_event_ack_) {
[email protected]353a34c2010-05-28 23:35:171147 // As two different kinds of events could cause us to postpone an ack
1148 // we send it now, if we have one pending. The Browser should never
1149 // send us the same kind of event we are delaying the ack for.
1150 Send(pending_input_event_ack_.release());
1151 }
[email protected]12fbad812009-09-01 18:21:241152 pending_input_event_ack_.reset(response);
[email protected]df09e052013-07-31 18:59:501153 if (compositor_)
1154 compositor_->NotifyInputThrottledUntilCommit();
[email protected]12fbad812009-09-01 18:21:241155 } else {
1156 Send(response);
1157 }
1158
[email protected]3306f262012-09-21 19:20:421159#if defined(OS_ANDROID)
1160 // Allow the IME to be shown when the focus changes as a consequence
1161 // of a processed touch end event.
1162 if (input_event->type == WebInputEvent::TouchEnd && processed)
[email protected]0d1ebed12013-08-05 22:01:131163 UpdateTextInputState(true, true);
[email protected]3306f262012-09-21 19:20:421164#endif
1165
[email protected]5dd768212009-08-13 23:34:491166 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:481167
[email protected]67bfb83f2011-09-22 03:36:371168 if (!prevent_default) {
1169 if (WebInputEvent::isKeyboardEventType(input_event->type))
1170 DidHandleKeyEvent();
1171 if (WebInputEvent::isMouseEventType(input_event->type))
1172 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:241173 if (WebInputEvent::isTouchEventType(input_event->type))
1174 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:371175 }
initial.commit09911bf2008-07-26 23:55:291176}
1177
[email protected]34202de2013-05-06 23:36:221178void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
1179 if (webwidget_)
1180 webwidget_->setCursorVisibilityState(is_visible);
1181}
1182
initial.commit09911bf2008-07-26 23:55:291183void RenderWidget::OnMouseCaptureLost() {
1184 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:281185 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:291186}
1187
1188void RenderWidget::OnSetFocus(bool enable) {
1189 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:331190 if (webwidget_)
1191 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:291192}
1193
1194void RenderWidget::ClearFocus() {
1195 // We may have got the focus from the browser before this gets processed, in
1196 // which case we do not want to unfocus ourself.
1197 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:281198 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:291199}
1200
[email protected]2d5d09d52009-06-15 14:29:211201void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:001202 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:211203 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:061204 TRACE_EVENT2("renderer", "PaintRect",
1205 "width", rect.width(), "height", rect.height());
[email protected]63ab54262012-11-09 15:58:451206
[email protected]4fb66842009-12-04 21:41:001207 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:211208
1209 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:001210 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
1211 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:031212
[email protected]699ab0d2009-04-23 23:19:141213 // If there is a custom background, tile it.
1214 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:141215 SkPaint paint;
[email protected]4e29afd2012-12-04 04:07:111216 skia::RefPtr<SkShader> shader = skia::AdoptRef(
1217 SkShader::CreateBitmapShader(background_,
1218 SkShader::kRepeat_TileMode,
1219 SkShader::kRepeat_TileMode));
1220 paint.setShader(shader.get());
[email protected]fb10ec5b2011-10-24 17:54:201221
1222 // Use kSrc_Mode to handle background_ transparency properly.
1223 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1224
1225 // Canvas could contain multiple update rects. Clip to given rect so that
1226 // we don't accidentally clear other update rects.
1227 canvas->save();
[email protected]aa7e7a12013-02-22 13:37:441228 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]1835b9e2012-02-28 13:12:481229 canvas->clipRect(gfx::RectToSkRect(rect));
[email protected]699ab0d2009-04-23 23:19:141230 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:201231 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:141232 }
1233
[email protected]719b36f2010-12-22 20:36:461234 // First see if this rect is a plugin that can paint itself faster.
1235 TransportDIB* optimized_dib = NULL;
1236 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201237 float dib_scale_factor;
[email protected]adab2332013-07-25 18:04:321238 PepperPluginInstanceImpl* optimized_instance =
[email protected]719b36f2010-12-22 20:36:461239 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
1240 &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201241 &optimized_copy_rect,
1242 &dib_scale_factor);
[email protected]719b36f2010-12-22 20:36:461243 if (optimized_instance) {
[email protected]20790a222013-07-25 02:23:051244#if defined(ENABLE_PLUGINS)
[email protected]719b36f2010-12-22 20:36:461245 // This plugin can be optimize-painted and we can just ask it to paint
1246 // itself. We don't actually need the TransportDIB in this case.
1247 //
1248 // This is an optimization for PPAPI plugins that know they're on top of
1249 // the page content. If this rect is inside such a plugin, we can save some
1250 // time and avoid re-rendering the page content which we know will be
1251 // covered by the plugin later (this time can be significant, especially
1252 // for a playing movie that is invalidating a lot).
1253 //
1254 // In the plugin movie case, hopefully the similar call to
1255 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
1256 // painting, because that avoids copying the plugin image to a different
1257 // paint rect. Unfortunately, if anything on the page is animating other
1258 // than the movie, it break this optimization since the union of the
1259 // invalid regions will be larger than the plugin.
1260 //
1261 // This code optimizes that case, where we can still avoid painting in
1262 // WebKit and filling the background (which can be slow) and just painting
1263 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
1264 // required.
[email protected]df59dd42012-09-14 22:56:301265 SkAutoCanvasRestore auto_restore(canvas, true);
1266 canvas->scale(device_scale_factor_, device_scale_factor_);
[email protected]6bd867b2013-07-24 22:10:201267 optimized_instance->Paint(canvas, optimized_copy_location, rect);
[email protected]ea43e752012-09-06 22:39:211268 canvas->restore();
[email protected]20790a222013-07-25 02:23:051269#endif
[email protected]719b36f2010-12-22 20:36:461270 } else {
1271 // Normal painting case.
[email protected]c1e6cc062013-08-24 03:35:351272 base::TimeTicks start_time;
1273 if (!is_accelerated_compositing_active_)
1274 start_time = legacy_software_mode_stats_->StartRecording();
[email protected]63ab54262012-11-09 15:58:451275
[email protected]6bd867b2013-07-24 22:10:201276 webwidget_->paint(canvas, rect);
[email protected]63ab54262012-11-09 15:58:451277
[email protected]c1e6cc062013-08-24 03:35:351278 if (!is_accelerated_compositing_active_) {
[email protected]63ab54262012-11-09 15:58:451279 base::TimeDelta paint_time =
[email protected]c1e6cc062013-08-24 03:35:351280 legacy_software_mode_stats_->EndRecording(start_time);
1281 int64 painted_pixel_count = rect.width() * rect.height();
1282 legacy_software_mode_stats_->AddPaint(paint_time, painted_pixel_count);
[email protected]63ab54262012-11-09 15:58:451283 }
[email protected]719b36f2010-12-22 20:36:461284
1285 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:351286 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:461287 }
initial.commit09911bf2008-07-26 23:55:291288
[email protected]4fb66842009-12-04 21:41:001289 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:001290 canvas->restore();
1291}
1292
1293void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
1294 skia::PlatformCanvas* canvas) {
1295 static bool kPaintBorder =
1296 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
1297 if (!kPaintBorder)
1298 return;
1299
[email protected]53d3f302009-12-21 04:42:051300 // Cycle through these colors to help distinguish new paint rects.
1301 const SkColor colors[] = {
1302 SkColorSetARGB(0x3F, 0xFF, 0, 0),
1303 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
1304 SkColorSetARGB(0x3F, 0, 0, 0xFF),
1305 };
1306 static int color_selector = 0;
1307
[email protected]4fb66842009-12-04 21:41:001308 SkPaint paint;
1309 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:051310 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:001311 paint.setStrokeWidth(1);
1312
1313 SkIRect irect;
1314 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
1315 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:291316}
1317
[email protected]52ccd0ea2011-02-16 01:09:051318void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:301319 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]921244e42011-07-20 16:36:301320 if (!animation_update_pending_) {
1321 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:591322 return;
[email protected]921244e42011-07-20 16:36:301323 }
[email protected]bd37ae252011-06-03 01:28:181324 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:591325 // Record when we fired (according to base::Time::Now()) relative to when
1326 // we posted the task to quantify how much the base::Time/base::TimeTicks
1327 // skew is affecting animations.
1328 base::TimeDelta animation_callback_delay = base::Time::Now() -
1329 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
1330 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
1331 animation_callback_delay,
1332 base::TimeDelta::FromMilliseconds(0),
1333 base::TimeDelta::FromMilliseconds(30),
1334 25);
1335 }
[email protected]65225772011-05-12 21:10:241336 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:241337}
1338
[email protected]52ccd0ea2011-02-16 01:09:051339void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:591340 if (!animation_update_pending_)
1341 return;
[email protected]bd37ae252011-06-03 01:28:181342
1343 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
[email protected]02798a982012-01-27 00:45:331344 base::TimeDelta animationInterval = IsRenderingVSynced() ?
1345 base::TimeDelta::FromMilliseconds(16) : base::TimeDelta();
[email protected]bd37ae252011-06-03 01:28:181346
[email protected]7c4329e2011-02-18 22:02:591347 base::Time now = base::Time::Now();
[email protected]51e403bb2012-03-02 21:09:451348
1349 // animation_floor_time_ is the earliest time that we should animate when
1350 // using the dead reckoning software scheduler. If we're using swapbuffers
1351 // complete callbacks to rate limit, we can ignore this floor.
1352 if (now >= animation_floor_time_ || num_swapbuffers_complete_pending_ > 0) {
[email protected]921244e42011-07-20 16:36:301353 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]02798a982012-01-27 00:45:331354 animation_floor_time_ = now + animationInterval;
[email protected]bd37ae252011-06-03 01:28:181355 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:591356 // running animation callbacks so that if a callback requests another
1357 // we'll be sure to run it at the proper time.
[email protected]350ce8702012-03-09 04:23:381358 animation_timer_.Stop();
1359 animation_timer_.Start(FROM_HERE, animationInterval, this,
1360 &RenderWidget::AnimationCallback);
[email protected]7c4329e2011-02-18 22:02:591361 animation_update_pending_ = false;
[email protected]ba91a792013-02-06 09:48:281362 if (is_accelerated_compositing_active_ && compositor_) {
[email protected]635353c2013-03-06 09:11:201363 compositor_->Animate(base::TimeTicks::Now());
[email protected]8926c602013-01-23 05:32:061364 } else {
[email protected]635353c2013-03-06 09:11:201365 double frame_begin_time =
1366 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
1367 webwidget_->animate(frame_begin_time);
[email protected]8926c602013-01-23 05:32:061368 }
[email protected]7c4329e2011-02-18 22:02:591369 return;
[email protected]5f8b1022011-01-21 23:34:501370 }
[email protected]bd37ae252011-06-03 01:28:181371 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]350ce8702012-03-09 04:23:381372 if (!animation_timer_.IsRunning()) {
1373 // This code uses base::Time::Now() to calculate the floor and next fire
1374 // time because javascript's Date object uses base::Time::Now(). The
1375 // message loop uses base::TimeTicks, which on windows can have a
1376 // different granularity than base::Time.
1377 // The upshot of all this is that this function might be called before
1378 // base::Time::Now() has advanced past the animation_floor_time_. To
1379 // avoid exposing this delay to javascript, we keep posting delayed
1380 // tasks until base::Time::Now() has advanced far enough.
1381 base::TimeDelta delay = animation_floor_time_ - now;
1382 animation_timer_.Start(FROM_HERE, delay, this,
1383 &RenderWidget::AnimationCallback);
1384 }
[email protected]5f8b1022011-01-21 23:34:501385}
1386
[email protected]bd37ae252011-06-03 01:28:181387bool RenderWidget::IsRenderingVSynced() {
1388 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
1389 // not caught by this check. This will lead to artificially low frame rates
1390 // for people who force vsync off at a driver level and expect Chrome to speed
1391 // up.
1392 return !has_disable_gpu_vsync_switch_;
1393}
1394
[email protected]65225772011-05-12 21:10:241395void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:061396 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:241397 invalidation_task_posted_ = false;
1398 DoDeferredUpdateAndSendInputAck();
1399}
1400
1401void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:051402 DoDeferredUpdate();
1403
[email protected]59383c782013-04-17 16:43:271404 if (pending_input_event_ack_)
[email protected]52ccd0ea2011-02-16 01:09:051405 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:211406}
1407
[email protected]552e6002009-11-19 05:24:571408void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:581409 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]fc8bde1a2013-07-04 08:54:071410 TRACE_EVENT_SCOPED_SAMPLING_STATE("Chrome", "Paint");
[email protected]71e2f0a2011-03-15 22:25:081411
[email protected]65225772011-05-12 21:10:241412 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:291413 return;
[email protected]05a980d7a2012-02-07 22:16:421414
[email protected]fc4404d2012-11-07 19:53:301415 if (!init_complete_) {
1416 TRACE_EVENT0("renderer", "EarlyOut_InitNotComplete");
[email protected]05a980d7a2012-02-07 22:16:421417 return;
1418 }
[email protected]aa4117f2011-12-09 22:19:211419 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:241420 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
1421 return;
1422 }
[email protected]9ca84622011-06-02 23:46:391423 if (is_accelerated_compositing_active_ &&
1424 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:241425 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
1426 return;
1427 }
initial.commit09911bf2008-07-26 23:55:291428
[email protected]552e6002009-11-19 05:24:571429 // Suppress updating when we are hidden.
[email protected]e3d92a7f2013-01-10 02:35:051430 if (is_hidden_ || size_.IsEmpty() || is_swapped_out_) {
[email protected]552e6002009-11-19 05:24:571431 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:291432 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:241433 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:291434 return;
1435 }
1436
[email protected]0fb93f52011-05-18 23:13:561437 // Tracking of frame rate jitter
1438 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]38ce4e7b2013-02-23 06:17:371439 InstrumentWillBeginFrame();
[email protected]52ccd0ea2011-02-16 01:09:051440 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:501441
[email protected]f98d7e3c2010-09-13 22:30:461442 // Layout may generate more invalidation. It may also enable the
1443 // GPU acceleration, so make sure to run layout before we send the
1444 // GpuRenderingActivated message.
1445 webwidget_->layout();
1446
[email protected]793b2d62013-06-11 00:43:251447 // Check for whether we need to track swap buffers. We need to do that after
1448 // layout() because it may have switched us to accelerated compositing.
1449 if (is_accelerated_compositing_active_)
1450 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
1451
[email protected]dcca3aa92012-02-17 23:03:371452 // The following two can result in further layout and possibly
1453 // enable GPU acceleration so they need to be called before any painting
1454 // is done.
[email protected]cb9e2632013-06-18 11:26:471455 UpdateTextInputType();
[email protected]dcca3aa92012-02-17 23:03:371456 UpdateSelectionBounds();
1457
[email protected]5f8b1022011-01-21 23:34:501458 // Suppress painting if nothing is dirty. This has to be done after updating
1459 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:241460 if (!paint_aggregator_.HasPendingUpdate()) {
1461 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]38ce4e7b2013-02-23 06:17:371462 InstrumentDidCancelFrame();
[email protected]5f8b1022011-01-21 23:34:501463 return;
[email protected]65225772011-05-12 21:10:241464 }
[email protected]5f8b1022011-01-21 23:34:501465
[email protected]479b0172012-10-29 19:27:091466 if (!is_accelerated_compositing_active_ &&
[email protected]cb6430932012-10-31 00:53:361467 !is_threaded_compositing_enabled_ &&
[email protected]d8a6c5d12013-09-23 04:37:101468 (ForceCompositingModeEnabled() ||
1469 was_accelerated_compositing_ever_active_)) {
[email protected]479b0172012-10-29 19:27:091470 webwidget_->enterForceCompositingMode(true);
1471 }
1472
[email protected]872ae5b2011-05-26 20:20:501473 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:561474 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
[email protected]d0be63772011-12-20 23:18:041475 if (is_accelerated_compositing_active_) {
[email protected]0fb93f52011-05-18 23:13:561476 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
1477 delay,
1478 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411479 base::TimeDelta::FromMilliseconds(120),
1480 60);
[email protected]d0be63772011-12-20 23:18:041481 } else {
[email protected]0fb93f52011-05-18 23:13:561482 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
1483 delay,
1484 base::TimeDelta::FromMilliseconds(1),
[email protected]b604cf82012-07-19 05:31:411485 base::TimeDelta::FromMilliseconds(120),
1486 60);
[email protected]d0be63772011-12-20 23:18:041487 }
[email protected]872ae5b2011-05-26 20:20:501488
1489 // Calculate filtered time per frame:
1490 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
1491 filtered_time_per_frame_ =
1492 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:561493 }
1494 last_do_deferred_update_time_ = frame_begin_ticks;
1495
[email protected]fef5e3972012-08-07 03:59:471496 if (!is_accelerated_compositing_active_) {
[email protected]922c6e1f2013-10-09 04:04:091497 legacy_software_mode_stats_->IncrementFrameCount(1, true);
[email protected]adbe30f2013-10-11 21:12:331498 cc::BenchmarkInstrumentation::IssueMainThreadRenderingStatsEvent(
1499 legacy_software_mode_stats_->main_thread_rendering_stats());
[email protected]c1e6cc062013-08-24 03:35:351500 legacy_software_mode_stats_->AccumulateAndClearMainThreadStats();
[email protected]fef5e3972012-08-07 03:59:471501 }
1502
[email protected]552e6002009-11-19 05:24:571503 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:291504 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:301505 PaintAggregator::PendingUpdate update;
1506 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:291507
[email protected]53d3f302009-12-21 04:42:051508 gfx::Rect scroll_damage = update.GetScrollDamage();
[email protected]ce112fe2012-10-29 22:52:181509 gfx::Rect bounds = gfx::UnionRects(update.GetPaintBounds(), scroll_damage);
initial.commit09911bf2008-07-26 23:55:291510
[email protected]ca4847f2010-09-24 05:39:151511 // A plugin may be able to do an optimized paint. First check this, in which
1512 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:461513 // This optimization allows PPAPI plugins that declare themselves on top of
1514 // the page (like a traditional windowed plugin) to be able to animate (think
1515 // movie playing) without repeatedly re-painting the page underneath, or
1516 // copying the plugin backing store (since we can send the plugin's backing
1517 // store directly to the browser).
1518 //
1519 // This optimization only works when the entire invalid region is contained
1520 // within the plugin. There is a related optimization in PaintRect for the
1521 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:151522 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:151523 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]0f3a2d12012-09-01 03:37:201524 float dib_scale_factor = 1;
[email protected]aa4117f2011-12-09 22:19:211525 DCHECK(!pending_update_params_.get());
1526 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
[email protected]990278ff2012-11-13 02:12:551527 pending_update_params_->scroll_delta = update.scroll_delta;
[email protected]aa4117f2011-12-09 22:19:211528 pending_update_params_->scroll_rect = update.scroll_rect;
1529 pending_update_params_->view_size = size_;
[email protected]aa4117f2011-12-09 22:19:211530 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
1531 pending_update_params_->flags = next_paint_flags_;
1532 pending_update_params_->scroll_offset = GetScrollOffset();
1533 pending_update_params_->needs_ack = true;
[email protected]7ded9f12012-06-13 20:47:091534 pending_update_params_->scale_factor = device_scale_factor_;
[email protected]aa4117f2011-12-09 22:19:211535 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091536 need_update_rect_for_auto_resize_ = false;
[email protected]aa4117f2011-12-09 22:19:211537
[email protected]256737c2013-06-08 04:39:101538 if (!is_accelerated_compositing_active_)
1539 pending_update_params_->latency_info = latency_info_;
1540
1541 latency_info_.Clear();
1542
[email protected]ca4847f2010-09-24 05:39:151543 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:561544 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:151545 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
[email protected]0f3a2d12012-09-01 03:37:201546 &optimized_copy_rect,
1547 &dib_scale_factor)) {
[email protected]2df1b362011-01-21 21:22:271548 // Only update the part of the plugin that actually changed.
[email protected]d4030502012-10-23 16:51:471549 optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:211550 pending_update_params_->bitmap = dib->id();
1551 pending_update_params_->bitmap_rect = optimized_copy_location;
1552 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]0f3a2d12012-09-01 03:37:201553 pending_update_params_->scale_factor = dib_scale_factor;
[email protected]a79d8a632010-11-18 22:35:561554 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461555 // Compute a buffer for painting and cache it.
[email protected]4889bd212013-02-11 22:23:101556
1557 bool fractional_scale = device_scale_factor_ -
1558 static_cast<int>(device_scale_factor_) != 0;
1559 if (fractional_scale) {
1560 // Damage might not be DIP aligned. Inflate damage to compensate.
1561 bounds.Inset(-1, -1);
1562 bounds.Intersect(gfx::Rect(size_));
1563 }
1564
1565 gfx::Rect pixel_bounds = gfx::ToEnclosingRect(
[email protected]ce112fe2012-10-29 22:52:181566 gfx::ScaleRect(bounds, device_scale_factor_));
[email protected]4889bd212013-02-11 22:23:101567
[email protected]ca4847f2010-09-24 05:39:151568 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:351569 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
[email protected]f1cccb32012-06-06 18:29:591570 pixel_bounds));
[email protected]59383c782013-04-17 16:43:271571 if (!canvas) {
[email protected]f98d7e3c2010-09-13 22:30:461572 NOTREACHED();
1573 return;
1574 }
[email protected]cef3362f2009-12-21 17:48:451575
[email protected]f98d7e3c2010-09-13 22:30:461576 // We may get back a smaller canvas than we asked for.
1577 // TODO(darin): This seems like it could cause painting problems!
[email protected]f1cccb32012-06-06 18:29:591578 DCHECK_EQ(pixel_bounds.width(), canvas->getDevice()->width());
1579 DCHECK_EQ(pixel_bounds.height(), canvas->getDevice()->height());
1580 pixel_bounds.set_width(canvas->getDevice()->width());
1581 pixel_bounds.set_height(canvas->getDevice()->height());
1582 bounds.set_width(pixel_bounds.width() / device_scale_factor_);
1583 bounds.set_height(pixel_bounds.height() / device_scale_factor_);
[email protected]53d3f302009-12-21 04:42:051584
[email protected]f98d7e3c2010-09-13 22:30:461585 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
1586
[email protected]aa4117f2011-12-09 22:19:211587 pending_update_params_->bitmap = current_paint_buf_->id();
1588 pending_update_params_->bitmap_rect = bounds;
1589
1590 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:461591 // The scroll damage is just another rectangle to paint and copy.
1592 copy_rects.swap(update.paint_rects);
1593 if (!scroll_damage.IsEmpty())
1594 copy_rects.push_back(scroll_damage);
1595
[email protected]4889bd212013-02-11 22:23:101596 for (size_t i = 0; i < copy_rects.size(); ++i) {
1597 gfx::Rect rect = copy_rects[i];
1598 if (fractional_scale) {
1599 // Damage might not be DPI aligned. Inflate rect to compensate.
1600 rect.Inset(-1, -1);
1601 }
1602 PaintRect(rect, pixel_bounds.origin(), canvas.get());
1603 }
[email protected]60a50072012-01-11 02:05:351604
1605 // Software FPS tick for performance tests. The accelerated path traces the
1606 // frame events in didCommitAndDrawCompositorFrame. See throughput_tests.cc.
1607 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421608 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickSW",
1609 TRACE_EVENT_SCOPE_THREAD);
[email protected]f98d7e3c2010-09-13 22:30:461610 } else { // Accelerated compositing path
1611 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:211612 // If painting is done via the gpu process then we don't set any damage
1613 // rects to save the browser process from doing unecessary work.
1614 pending_update_params_->bitmap_rect = bounds;
1615 pending_update_params_->scroll_rect = gfx::Rect();
1616 // We don't need an ack, because we're not sharing a DIB with the browser.
1617 // If it needs to (e.g. composited UI), the GPU process does its own ACK
1618 // with the browser for the GPU surface.
1619 pending_update_params_->needs_ack = false;
[email protected]f0c2a242013-03-15 19:34:521620 Composite(frame_begin_ticks);
[email protected]f98d7e3c2010-09-13 22:30:461621 }
1622
[email protected]936c6f52011-12-13 01:35:261623 // If we're holding a pending input event ACK, send the ACK before sending the
1624 // UpdateReply message so we can receive another input event before the
1625 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
1626 // the UpdateRect IPC message handler.
[email protected]59383c782013-04-17 16:43:271627 if (pending_input_event_ack_)
[email protected]936c6f52011-12-13 01:35:261628 Send(pending_input_event_ack_.release());
1629
[email protected]ab543072013-01-25 04:38:151630 // If Composite() called SwapBuffers, pending_update_params_ will be reset (in
[email protected]aa4117f2011-12-09 22:19:211631 // OnSwapBuffersPosted), meaning a message has been added to the
1632 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
1633 // the message now.
[email protected]59383c782013-04-17 16:43:271634 if (pending_update_params_) {
[email protected]aa4117f2011-12-09 22:19:211635 // sending an ack to browser process that the paint is complete...
1636 update_reply_pending_ = pending_update_params_->needs_ack;
1637 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
1638 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:341639 }
[email protected]53d3f302009-12-21 04:42:051640
[email protected]29ed96a2012-02-04 18:12:161641 // If we're software rendering then we're done initiating the paint.
1642 if (!is_accelerated_compositing_active_)
1643 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:291644}
1645
[email protected]f0c2a242013-03-15 19:34:521646void RenderWidget::Composite(base::TimeTicks frame_begin_time) {
[email protected]ab543072013-01-25 04:38:151647 DCHECK(is_accelerated_compositing_active_);
[email protected]ba91a792013-02-06 09:48:281648 if (compositor_) // TODO(jamesr): Figure out how this can be null.
[email protected]f0c2a242013-03-15 19:34:521649 compositor_->Composite(frame_begin_time);
[email protected]ab543072013-01-25 04:38:151650}
1651
initial.commit09911bf2008-07-26 23:55:291652///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461653// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291654
[email protected]4873c7d2009-07-16 06:36:281655void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:571656 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481657 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181658 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect);
[email protected]552e6002009-11-19 05:24:571659 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291660 return;
1661
[email protected]552e6002009-11-19 05:24:571662 paint_aggregator_.InvalidateRect(damaged_rect);
1663
1664 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241665 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571666 return;
1667 if (!paint_aggregator_.HasPendingUpdate())
1668 return;
[email protected]aa4117f2011-12-09 22:19:211669 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241670 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1671 return;
1672
1673 // When GPU rendering, combine pending animations and invalidations into
1674 // a single update.
[email protected]816edc62012-03-17 01:27:221675 if (is_accelerated_compositing_active_ &&
1676 animation_update_pending_ &&
1677 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571678 return;
1679
1680 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:291681 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1682 // on the call stack.
1683 // 2) Allows us to collect more damage rects before painting to help coalesce
1684 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241685 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111686 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211687 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291688}
1689
[email protected]990278ff2012-11-13 02:12:551690void RenderWidget::didScrollRect(int dx, int dy,
1691 const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:461692 // Drop scrolls on the floor when we are in compositing mode.
1693 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:561694 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:461695 return;
1696
[email protected]552e6002009-11-19 05:24:571697 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:481698 gfx::Rect view_rect(size_);
[email protected]ce112fe2012-10-29 22:52:181699 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, clip_rect);
[email protected]552e6002009-11-19 05:24:571700 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:291701 return;
1702
[email protected]990278ff2012-11-13 02:12:551703 paint_aggregator_.ScrollRect(gfx::Vector2d(dx, dy), damaged_rect);
[email protected]552e6002009-11-19 05:24:571704
1705 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:241706 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:571707 return;
1708 if (!paint_aggregator_.HasPendingUpdate())
1709 return;
[email protected]aa4117f2011-12-09 22:19:211710 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:241711 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
1712 return;
1713
1714 // When GPU rendering, combine pending animations and invalidations into
1715 // a single update.
[email protected]816edc62012-03-17 01:27:221716 if (is_accelerated_compositing_active_ &&
1717 animation_update_pending_ &&
1718 animation_timer_.IsRunning())
[email protected]552e6002009-11-19 05:24:571719 return;
1720
1721 // Perform updating asynchronously. This serves two purposes:
1722 // 1) Ensures that we call WebView::Paint without a bunch of other junk
1723 // on the call stack.
1724 // 2) Allows us to collect more damage rects before painting to help coalesce
1725 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:241726 invalidation_task_posted_ = true;
[email protected]dd32b1272013-05-04 14:17:111727 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211728 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:291729}
1730
[email protected]244ac1892011-12-02 17:04:471731void RenderWidget::didAutoResize(const WebSize& new_size) {
[email protected]ea3ee0a2012-05-15 03:43:091732 if (size_.width() != new_size.width || size_.height() != new_size.height) {
[email protected]eac2b362013-05-22 07:01:451733 size_ = new_size;
[email protected]20fbfc22013-05-08 20:50:581734
[email protected]eac2b362013-05-22 07:01:451735 // If we don't clear PaintAggregator after changing autoResize state, then
1736 // we might end up in a situation where bitmap_rect is larger than the
1737 // view_size. By clearing PaintAggregator, we ensure that we don't end up
1738 // with invalid damage rects.
1739 paint_aggregator_.ClearPendingUpdate();
1740
[email protected]03e88672013-10-22 21:31:321741 if (ResizingModeSelector::IsLegacyMode()) {
[email protected]eac2b362013-05-22 07:01:451742 WebRect new_pos(rootWindowRect().x,
1743 rootWindowRect().y,
1744 new_size.width,
1745 new_size.height);
1746 view_screen_rect_ = new_pos;
1747 window_screen_rect_ = new_pos;
[email protected]8be1c582013-03-06 00:55:031748 }
[email protected]20fbfc22013-05-08 20:50:581749
[email protected]eac2b362013-05-22 07:01:451750 AutoResizeCompositor();
[email protected]20fbfc22013-05-08 20:50:581751
[email protected]03e88672013-10-22 21:31:321752 if (!ResizingModeSelector::IsLegacyMode())
[email protected]20fbfc22013-05-08 20:50:581753 need_update_rect_for_auto_resize_ = true;
[email protected]ea3ee0a2012-05-15 03:43:091754 }
[email protected]244ac1892011-12-02 17:04:471755}
1756
[email protected]3a1c8a8032013-03-18 22:35:321757void RenderWidget::AutoResizeCompositor() {
[email protected]97e1bf72013-03-06 14:06:051758 physical_backing_size_ = gfx::ToCeiledSize(gfx::ScaleSize(size_,
1759 device_scale_factor_));
1760 if (compositor_)
1761 compositor_->setViewportSize(size_, physical_backing_size_);
1762}
1763
[email protected]91acd1c2012-03-14 08:32:391764void RenderWidget::didActivateCompositor(int input_handler_identifier) {
[email protected]ea162f92011-10-04 23:08:221765 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
1766
[email protected]c63b4d42012-04-26 01:01:071767#if !defined(OS_MACOSX)
[email protected]aa4117f2011-12-09 22:19:211768 if (!is_accelerated_compositing_active_) {
1769 // When not in accelerated compositing mode, in certain cases (e.g. waiting
1770 // for a resize or if no backing store) the RenderWidgetHost is blocking the
1771 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1772 // going to switch to accelerated compositing, the GPU process may need
1773 // round-trips to the browser's UI thread before finishing the frame,
1774 // causing deadlocks if we delay the UpdateRect until we receive the
1775 // OnSwapBuffersComplete. So send a dummy message that will unblock the
[email protected]c63b4d42012-04-26 01:01:071776 // browser's UI thread. This is not necessary on Mac, because SwapBuffers
1777 // now unblocks GetBackingStore on Mac.
[email protected]aa4117f2011-12-09 22:19:211778 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1779 }
[email protected]c63b4d42012-04-26 01:01:071780#endif
[email protected]aa4117f2011-12-09 22:19:211781
[email protected]ea162f92011-10-04 23:08:221782 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421783 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241784 routing_id_, is_accelerated_compositing_active_));
[email protected]d8a6c5d12013-09-23 04:37:101785
1786 if (!was_accelerated_compositing_ever_active_) {
1787 was_accelerated_compositing_ever_active_ = true;
1788 webwidget_->enterForceCompositingMode(true);
1789 }
[email protected]ea162f92011-10-04 23:08:221790}
1791
1792void RenderWidget::didDeactivateCompositor() {
1793 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1794
1795 is_accelerated_compositing_active_ = false;
1796 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1797 routing_id_, is_accelerated_compositing_active_));
1798
[email protected]ea162f92011-10-04 23:08:221799 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241800 using_asynchronous_swapbuffers_ = false;
[email protected]479b0172012-10-29 19:27:091801
1802 // In single-threaded mode, we exit force compositing mode and re-enter in
1803 // DoDeferredUpdate() if appropriate. In threaded compositing mode,
1804 // DoDeferredUpdate() is bypassed and WebKit is responsible for exiting and
1805 // entering force compositing mode at the appropriate times.
[email protected]cb6430932012-10-31 00:53:361806 if (!is_threaded_compositing_enabled_)
[email protected]479b0172012-10-29 19:27:091807 webwidget_->enterForceCompositingMode(false);
[email protected]a79d8a632010-11-18 22:35:561808}
1809
[email protected]e195e582013-03-08 01:32:591810void RenderWidget::initializeLayerTreeView() {
[email protected]c5fa4c42013-07-20 05:47:371811 compositor_ = RenderWidgetCompositor::Create(
1812 this, is_threaded_compositing_enabled_);
[email protected]e195e582013-03-08 01:32:591813 if (!compositor_)
1814 return;
1815
1816 compositor_->setViewportSize(size_, physical_backing_size_);
1817 if (init_complete_)
1818 compositor_->setSurfaceReady();
1819}
1820
[email protected]8926c602013-01-23 05:32:061821WebKit::WebLayerTreeView* RenderWidget::layerTreeView() {
[email protected]ba91a792013-02-06 09:48:281822 return compositor_.get();
[email protected]8926c602013-01-23 05:32:061823}
1824
[email protected]9ed83fe2013-02-27 01:52:281825void RenderWidget::suppressCompositorScheduling(bool enable) {
1826 if (compositor_)
1827 compositor_->SetSuppressScheduleComposite(enable);
1828}
1829
[email protected]9cd43a62012-03-26 08:03:561830void RenderWidget::willBeginCompositorFrame() {
1831 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
[email protected]abe8b3a2012-03-28 21:19:371832
[email protected]cadac622013-06-11 16:46:361833 DCHECK(RenderThreadImpl::current()->compositor_message_loop_proxy().get());
[email protected]abe8b3a2012-03-28 21:19:371834
1835 // The following two can result in further layout and possibly
1836 // enable GPU acceleration so they need to be called before any painting
1837 // is done.
[email protected]cb9e2632013-06-18 11:26:471838 UpdateTextInputType();
1839#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:131840 UpdateTextInputState(false, true);
[email protected]cb9e2632013-06-18 11:26:471841#endif
[email protected]abe8b3a2012-03-28 21:19:371842 UpdateSelectionBounds();
[email protected]9cd43a62012-03-26 08:03:561843}
1844
[email protected]3391a0772012-03-28 00:32:071845void RenderWidget::didBecomeReadyForAdditionalInput() {
1846 TRACE_EVENT0("renderer", "RenderWidget::didBecomeReadyForAdditionalInput");
[email protected]59383c782013-04-17 16:43:271847 if (pending_input_event_ack_)
[email protected]3391a0772012-03-28 00:32:071848 Send(pending_input_event_ack_.release());
1849}
1850
[email protected]6fceb912013-02-15 06:24:151851void RenderWidget::DidCommitCompositorFrame() {
1852}
1853
[email protected]58264a32011-11-17 23:36:151854void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501855 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
[email protected]60a50072012-01-11 02:05:351856 // Accelerated FPS tick for performance tests. See throughput_tests.cc.
1857 // NOTE: Tests may break if this event is renamed or moved.
[email protected]c76faea2013-03-26 07:42:421858 UNSHIPPED_TRACE_EVENT_INSTANT0("test_fps", "TestFrameTickGPU",
1859 TRACE_EVENT_SCOPE_THREAD);
[email protected]29ed96a2012-02-04 18:12:161860 // Notify subclasses that we initiated the paint operation.
1861 DidInitiatePaint();
[email protected]58264a32011-11-17 23:36:151862}
1863
1864void RenderWidget::didCompleteSwapBuffers() {
[email protected]404939f2012-06-01 04:06:181865 TRACE_EVENT0("renderer", "RenderWidget::didCompleteSwapBuffers");
1866
1867 // Notify subclasses threaded composited rendering was flushed to the screen.
[email protected]9cd43a62012-03-26 08:03:561868 DidFlushPaint();
1869
[email protected]aa4117f2011-12-09 22:19:211870 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151871 return;
1872
[email protected]ea3ee0a2012-05-15 03:43:091873 if (!next_paint_flags_ &&
1874 !need_update_rect_for_auto_resize_ &&
1875 !plugin_window_moves_.size()) {
[email protected]58264a32011-11-17 23:36:151876 return;
[email protected]ea3ee0a2012-05-15 03:43:091877 }
[email protected]58264a32011-11-17 23:36:151878
1879 ViewHostMsg_UpdateRect_Params params;
1880 params.view_size = size_;
[email protected]58264a32011-11-17 23:36:151881 params.plugin_window_moves.swap(plugin_window_moves_);
1882 params.flags = next_paint_flags_;
1883 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121884 params.needs_ack = false;
[email protected]7ded9f12012-06-13 20:47:091885 params.scale_factor = device_scale_factor_;
[email protected]58264a32011-11-17 23:36:151886
1887 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1888 next_paint_flags_ = 0;
[email protected]ea3ee0a2012-05-15 03:43:091889 need_update_rect_for_auto_resize_ = false;
[email protected]58264a32011-11-17 23:36:151890}
1891
[email protected]f98d7e3c2010-09-13 22:30:461892void RenderWidget::scheduleComposite() {
[email protected]7d08a9352013-10-15 08:24:561893 ScheduleCompositeImpl(false);
[email protected]f98d7e3c2010-09-13 22:30:461894}
1895
[email protected]5f8b1022011-01-21 23:34:501896void RenderWidget::scheduleAnimation() {
[email protected]ce65fb782012-04-19 05:01:201897 if (animation_update_pending_)
1898 return;
1899
[email protected]921244e42011-07-20 16:36:301900 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ce65fb782012-04-19 05:01:201901 animation_update_pending_ = true;
1902 if (!animation_timer_.IsRunning()) {
1903 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(0), this,
1904 &RenderWidget::AnimationCallback);
[email protected]ee3d3ad2011-02-04 00:42:211905 }
[email protected]5f8b1022011-01-21 23:34:501906}
1907
[email protected]4873c7d2009-07-16 06:36:281908void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301909 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521910 WebCursor cursor;
[email protected]953bd0062013-08-01 00:58:401911 InitializeCursorFromWebKitCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291912 // Only send a SetCursor message if we need to make a change.
1913 if (!current_cursor_.IsEqual(cursor)) {
1914 current_cursor_ = cursor;
1915 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1916 }
1917}
1918
1919// We are supposed to get a single call to Show for a newly created RenderWidget
1920// that was created via RenderWidget::CreateWebView. So, we wait until this
1921// point to dispatch the ShowWidget message.
1922//
1923// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281924// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291925//
[email protected]4873c7d2009-07-16 06:36:281926void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291927 DCHECK(!did_show_) << "received extraneous Show call";
1928 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1929 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1930
[email protected]8de12d942010-11-17 20:42:441931 if (did_show_)
1932 return;
1933
1934 did_show_ = true;
1935 // NOTE: initial_pos_ may still have its default values at this point, but
1936 // that's okay. It'll be ignored if as_popup is false, or the browser
1937 // process will impose a default position otherwise.
1938 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1939 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291940}
1941
[email protected]4873c7d2009-07-16 06:36:281942void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291943}
1944
[email protected]4873c7d2009-07-16 06:36:281945void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291946}
1947
[email protected]2533ce12009-05-09 00:02:241948void RenderWidget::DoDeferredClose() {
1949 Send(new ViewHostMsg_Close(routing_id_));
1950}
1951
[email protected]4873c7d2009-07-16 06:36:281952void RenderWidget::closeWidgetSoon() {
[email protected]e1c3a552012-05-04 20:51:321953 if (is_swapped_out_) {
1954 // This widget is currently swapped out, and the active widget is in a
1955 // different process. Have the browser route the close request to the
1956 // active widget instead, so that the correct unload handlers are run.
1957 Send(new ViewHostMsg_RouteCloseEvent(routing_id_));
1958 return;
1959 }
1960
initial.commit09911bf2008-07-26 23:55:291961 // If a page calls window.close() twice, we'll end up here twice, but that's
1962 // OK. It is safe to send multiple Close messages.
1963
[email protected]2533ce12009-05-09 00:02:241964 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1965 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1966 // could be closed before the JS finishes executing. So instead, post a
1967 // message back to the message loop, which won't run until the JS is
1968 // complete, and then the Close message can be sent.
[email protected]dd32b1272013-05-04 14:17:111969 base::MessageLoop::current()->PostTask(
[email protected]32876ae2011-11-15 22:25:211970 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291971}
1972
1973void RenderWidget::Close() {
1974 if (webwidget_) {
[email protected]8926c602013-01-23 05:32:061975 webwidget_->willCloseLayerTreeView();
[email protected]ba91a792013-02-06 09:48:281976 compositor_.reset();
[email protected]4873c7d2009-07-16 06:36:281977 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291978 webwidget_ = NULL;
1979 }
1980}
1981
[email protected]4873c7d2009-07-16 06:36:281982WebRect RenderWidget::windowRect() {
1983 if (pending_window_rect_count_)
1984 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241985
[email protected]80ad8622012-11-07 16:33:031986 return view_screen_rect_;
initial.commit09911bf2008-07-26 23:55:291987}
1988
[email protected]8a9d6ca32011-06-06 20:11:301989void RenderWidget::setToolTipText(const WebKit::WebString& text,
1990 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541991 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301992}
1993
[email protected]b2e4c70132013-10-03 02:07:511994void RenderWidget::setWindowRect(const WebRect& rect) {
1995 WebRect pos = rect;
1996 if (popup_origin_scale_for_emulation_) {
1997 float scale = popup_origin_scale_for_emulation_;
1998 pos.x = popup_screen_origin_for_emulation_.x() +
1999 (pos.x - popup_view_origin_for_emulation_.x()) * scale;
2000 pos.y = popup_screen_origin_for_emulation_.y() +
2001 (pos.y - popup_view_origin_for_emulation_.y()) * scale;
2002 }
2003
[email protected]03e88672013-10-22 21:31:322004 if (!ResizingModeSelector::IsLegacyMode()) {
[email protected]ec951b9d2013-10-20 06:21:202005 if (did_show_) {
[email protected]8be1c582013-03-06 00:55:032006 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
2007 SetPendingWindowRect(pos);
2008 } else {
[email protected]ec951b9d2013-10-20 06:21:202009 initial_pos_ = pos;
[email protected]8be1c582013-03-06 00:55:032010 }
initial.commit09911bf2008-07-26 23:55:292011 } else {
[email protected]ec951b9d2013-10-20 06:21:202012 WebSize new_size(pos.width, pos.height);
2013 Resize(new_size, new_size, overdraw_bottom_height_,
2014 WebRect(), is_fullscreen_, NO_RESIZE_ACK);
2015 view_screen_rect_ = pos;
2016 window_screen_rect_ = pos;
2017 if (!did_show_)
2018 initial_pos_ = pos;
initial.commit09911bf2008-07-26 23:55:292019 }
2020}
2021
[email protected]2533ce12009-05-09 00:02:242022void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
2023 pending_window_rect_ = rect;
2024 pending_window_rect_count_++;
2025}
2026
[email protected]4873c7d2009-07-16 06:36:282027WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:242028 if (pending_window_rect_count_) {
2029 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
2030 // the RootWindowRect is probably going to return wrong results since the
2031 // browser may not have processed the Move yet. There isn't really anything
2032 // good to do in this case, and it shouldn't happen - since this size is
2033 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:282034 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:242035 }
2036
[email protected]80ad8622012-11-07 16:33:032037 return window_screen_rect_;
[email protected]d4547452008-08-28 18:36:372038}
2039
[email protected]4873c7d2009-07-16 06:36:282040WebRect RenderWidget::windowResizerRect() {
2041 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:192042}
2043
[email protected]fa7b1dc2010-06-23 17:53:042044void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:032045 // To prevent this renderer process from sending unnecessary IPC messages to
2046 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:042047 // only during the input method attached to the browser process is active.
2048 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:292049}
2050
[email protected]fa7b1dc2010-06-23 17:53:042051void RenderWidget::OnImeSetComposition(
2052 const string16& text,
2053 const std::vector<WebCompositionUnderline>& underlines,
2054 int selection_start, int selection_end) {
[email protected]0d1ebed12013-08-05 22:01:132055 if (!ShouldHandleImeEvent())
[email protected]4873c7d2009-07-16 06:36:282056 return;
[email protected]66fca5bc2013-05-23 06:58:292057 ImeEventGuard guard(this);
[email protected]88dbe32f2013-06-20 23:31:362058 if (!webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:042059 text, WebVector<WebCompositionUnderline>(underlines),
2060 selection_start, selection_end)) {
2061 // If we failed to set the composition text, then we need to let the browser
2062 // process to cancel the input method's ongoing composition session, to make
2063 // sure we are in a consistent state.
2064 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]7f00efa2010-04-15 05:01:262065 }
[email protected]501ea13d2013-07-09 17:03:292066#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362067 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292068#endif
[email protected]fa7b1dc2010-06-23 17:53:042069}
2070
[email protected]0e45bd02013-07-12 20:20:022071void RenderWidget::OnImeConfirmComposition(const string16& text,
[email protected]db4fc1e2013-09-06 20:01:512072 const gfx::Range& replacement_range,
[email protected]0e45bd02013-07-12 20:20:022073 bool keep_selection) {
[email protected]0d1ebed12013-08-05 22:01:132074 if (!ShouldHandleImeEvent())
[email protected]d0be63772011-12-20 23:18:042075 return;
[email protected]66fca5bc2013-05-23 06:58:292076 ImeEventGuard guard(this);
[email protected]d0be63772011-12-20 23:18:042077 handling_input_event_ = true;
[email protected]0e45bd02013-07-12 20:20:022078 if (text.length())
2079 webwidget_->confirmComposition(text);
2080 else if (keep_selection)
2081 webwidget_->confirmComposition(WebWidget::KeepSelection);
2082 else
2083 webwidget_->confirmComposition(WebWidget::DoNotKeepSelection);
[email protected]d0be63772011-12-20 23:18:042084 handling_input_event_ = false;
[email protected]501ea13d2013-07-09 17:03:292085#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362086 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292087#endif
initial.commit09911bf2008-07-26 23:55:292088}
2089
[email protected]948f7ab72010-05-28 23:48:082090// This message causes the renderer to render an image of the
2091// desired_size, regardless of whether the tab is hidden or not.
[email protected]3d9ec5052013-01-02 22:05:252092void RenderWidget::OnPaintAtSize(const TransportDIB::Handle& dib_handle,
2093 int tag,
2094 const gfx::Size& page_size,
2095 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:002096 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
2097 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:252098 // Close our unused handle.
2099#if defined(OS_WIN)
2100 ::CloseHandle(dib_handle);
2101#elif defined(OS_MACOSX)
2102 base::SharedMemory::CloseHandle(dib_handle);
2103#endif
2104 }
[email protected]d65adb12010-04-28 17:26:492105 return;
[email protected]45c6aad32010-11-11 04:46:252106 }
[email protected]d65adb12010-04-28 17:26:492107
[email protected]948f7ab72010-05-28 23:48:082108 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:492109 // If one of these is empty, then we just return the dib we were
2110 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:092111 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:492112 return;
2113 }
2114
2115 // Map the given DIB ID into this process, and unmap it at the end
2116 // of this function.
[email protected]45c6aad32010-11-11 04:46:252117 scoped_ptr<TransportDIB> paint_at_size_buffer(
2118 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:302119
[email protected]4b01b962012-10-09 23:17:352120 gfx::Size page_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:282121 gfx::ScaleSize(page_size, device_scale_factor_));
[email protected]4b01b962012-10-09 23:17:352122 gfx::Size desired_size_in_pixel = gfx::ToFlooredSize(
[email protected]01a15a72012-11-10 09:34:282123 gfx::ScaleSize(desired_size, device_scale_factor_));
[email protected]8f640512012-08-07 23:52:512124 gfx::Size canvas_size = page_size_in_pixel;
2125 float x_scale = static_cast<float>(desired_size_in_pixel.width()) /
[email protected]d65adb12010-04-28 17:26:492126 static_cast<float>(canvas_size.width());
[email protected]8f640512012-08-07 23:52:512127 float y_scale = static_cast<float>(desired_size_in_pixel.height()) /
[email protected]d65adb12010-04-28 17:26:492128 static_cast<float>(canvas_size.height());
2129
[email protected]ee8d6fd2010-05-26 17:05:482130 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:492131 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
2132 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:482133 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:492134
[email protected]36808ad2010-10-20 19:18:302135 scoped_ptr<skia::PlatformCanvas> canvas(
2136 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
2137 canvas_size.height()));
[email protected]59383c782013-04-17 16:43:272138 if (!canvas) {
[email protected]36808ad2010-10-20 19:18:302139 NOTREACHED();
2140 return;
2141 }
2142
[email protected]d65adb12010-04-28 17:26:492143 // Reset bounds to what we actually received, but they should be the
2144 // same.
2145 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
2146 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
2147 bounds.set_width(canvas->getDevice()->width());
2148 bounds.set_height(canvas->getDevice()->height());
2149
2150 canvas->save();
[email protected]948f7ab72010-05-28 23:48:082151 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:492152 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
2153
[email protected]948f7ab72010-05-28 23:48:082154 // Have to make sure we're laid out at the right size before
2155 // rendering.
2156 gfx::Size old_size = webwidget_->size();
2157 webwidget_->resize(page_size);
2158 webwidget_->layout();
2159
[email protected]d65adb12010-04-28 17:26:492160 // Paint the entire thing (using original bounds, not scaled bounds).
2161 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
2162 canvas->restore();
2163
[email protected]948f7ab72010-05-28 23:48:082164 // Return the widget to its previous size.
2165 webwidget_->resize(old_size);
2166
[email protected]c88c9442010-07-19 18:55:092167 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:492168}
2169
[email protected]51a49502013-03-23 01:50:192170void RenderWidget::OnSnapshot(const gfx::Rect& src_subrect) {
2171 SkBitmap snapshot;
2172
2173 if (OnSnapshotHelper(src_subrect, &snapshot)) {
2174 Send(new ViewHostMsg_Snapshot(routing_id(), true, snapshot));
2175 } else {
2176 Send(new ViewHostMsg_Snapshot(routing_id(), false, SkBitmap()));
2177 }
2178}
2179
2180bool RenderWidget::OnSnapshotHelper(const gfx::Rect& src_subrect,
2181 SkBitmap* snapshot) {
2182 base::TimeTicks beginning_time = base::TimeTicks::Now();
2183
2184 if (!webwidget_ || src_subrect.IsEmpty())
2185 return false;
2186
2187 gfx::Rect viewport_size = gfx::IntersectRects(
2188 src_subrect, gfx::Rect(physical_backing_size_));
2189
2190 skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
2191 skia::CreatePlatformCanvas(viewport_size.width(),
2192 viewport_size.height(),
2193 true,
2194 NULL,
2195 skia::RETURN_NULL_ON_FAILURE));
[email protected]59383c782013-04-17 16:43:272196 if (!canvas)
[email protected]51a49502013-03-23 01:50:192197 return false;
2198
2199 canvas->save();
2200 webwidget_->layout();
2201
2202 PaintRect(viewport_size, viewport_size.origin(), canvas.get());
2203 canvas->restore();
2204
2205 const SkBitmap& bitmap = skia::GetTopDevice(*canvas)->accessBitmap(false);
2206 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config))
2207 return false;
2208
2209 UMA_HISTOGRAM_TIMES("Renderer4.Snapshot",
2210 base::TimeTicks::Now() - beginning_time);
2211 return true;
2212}
2213
[email protected]0bc1f572013-04-17 01:46:312214void RenderWidget::OnRepaint(gfx::Size size_to_paint) {
[email protected]ec7dc112008-08-06 05:30:122215 // During shutdown we can just ignore this message.
2216 if (!webwidget_)
2217 return;
2218
[email protected]0bc1f572013-04-17 01:46:312219 // Even if the browser provides an empty damage rect, it's still expecting to
2220 // receive a repaint ack so just damage the entire widget bounds.
2221 if (size_to_paint.IsEmpty()) {
2222 size_to_paint = size_;
2223 }
2224
[email protected]ec7dc112008-08-06 05:30:122225 set_next_paint_is_repaint_ack();
[email protected]0bc1f572013-04-17 01:46:312226 if (is_accelerated_compositing_active_ && compositor_) {
2227 compositor_->SetNeedsRedrawRect(gfx::Rect(size_to_paint));
[email protected]f98d7e3c2010-09-13 22:30:462228 } else {
2229 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
2230 didInvalidateRect(repaint_rect);
2231 }
[email protected]ec7dc112008-08-06 05:30:122232}
2233
[email protected]79fa22e2013-08-23 15:18:122234void RenderWidget::OnSyntheticGestureCompleted() {
2235 pending_synthetic_gesture_.Run();
[email protected]0e241b4b2012-08-18 09:06:272236}
2237
[email protected]4873c7d2009-07-16 06:36:282238void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:112239 if (!webwidget_)
2240 return;
[email protected]4873c7d2009-07-16 06:36:282241 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112242}
2243
[email protected]80ad8622012-11-07 16:33:032244void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect,
2245 const gfx::Rect& window_screen_rect) {
[email protected]b2e4c70132013-10-03 02:07:512246 if (screen_metrics_emulator_) {
2247 screen_metrics_emulator_->OnUpdateScreenRectsMessage(
2248 view_screen_rect, window_screen_rect);
2249 } else {
2250 view_screen_rect_ = view_screen_rect;
2251 window_screen_rect_ = window_screen_rect;
2252 }
[email protected]80ad8622012-11-07 16:33:032253 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id()));
2254}
2255
[email protected]105dffb42013-02-20 03:46:212256#if defined(OS_ANDROID)
[email protected]2384b6c2013-02-28 23:58:512257void RenderWidget::OnShowImeIfNeeded() {
[email protected]0d1ebed12013-08-05 22:01:132258 UpdateTextInputState(true, true);
2259}
2260
2261void RenderWidget::IncrementOutstandingImeEventAcks() {
2262 ++outstanding_ime_acks_;
2263}
2264
2265void RenderWidget::OnImeEventAck() {
2266 --outstanding_ime_acks_;
2267 DCHECK(outstanding_ime_acks_ >= 0);
[email protected]2384b6c2013-02-28 23:58:512268}
[email protected]105dffb42013-02-20 03:46:212269#endif
2270
[email protected]0d1ebed12013-08-05 22:01:132271bool RenderWidget::ShouldHandleImeEvent() {
2272#if defined(OS_ANDROID)
2273 return !!webwidget_ && outstanding_ime_acks_ == 0;
2274#else
2275 return !!webwidget_;
2276#endif
2277}
2278
[email protected]468ac582012-11-20 00:53:192279void RenderWidget::SetDeviceScaleFactor(float device_scale_factor) {
2280 if (device_scale_factor_ == device_scale_factor)
2281 return;
2282
2283 device_scale_factor_ = device_scale_factor;
2284
2285 if (!is_accelerated_compositing_active_) {
2286 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
2287 } else {
2288 scheduleComposite();
2289 }
2290}
2291
[email protected]adab2332013-07-25 18:04:322292PepperPluginInstanceImpl* RenderWidget::GetBitmapForOptimizedPluginPaint(
2293 const gfx::Rect& paint_bounds,
2294 TransportDIB** dib,
2295 gfx::Rect* location,
2296 gfx::Rect* clip,
2297 float* scale_factor) {
[email protected]719b36f2010-12-22 20:36:462298 // Bare RenderWidgets don't support optimized plugin painting.
2299 return NULL;
[email protected]ca4847f2010-09-24 05:39:152300}
2301
[email protected]ceb36f7d2012-10-31 18:33:242302gfx::Vector2d RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:522303 // Bare RenderWidgets don't support scroll offset.
[email protected]ceb36f7d2012-10-31 18:33:242304 return gfx::Vector2d();
[email protected]d54169e92011-01-21 09:19:522305}
2306
[email protected]bee16aab2009-08-26 15:55:032307void RenderWidget::SetHidden(bool hidden) {
2308 if (is_hidden_ == hidden)
2309 return;
2310
2311 // The status has changed. Tell the RenderThread about it.
2312 is_hidden_ = hidden;
2313 if (is_hidden_)
[email protected]380244092011-10-07 17:26:272314 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:032315 else
[email protected]380244092011-10-07 17:26:272316 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:032317}
2318
[email protected]2b624c562011-10-27 22:58:262319void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262320 if (!webwidget_)
2321 return;
2322
2323 if (is_fullscreen_) {
2324 webwidget_->willExitFullScreen();
2325 } else {
2326 webwidget_->willEnterFullScreen();
2327 }
[email protected]2b624c562011-10-27 22:58:262328}
2329
2330void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:262331 if (!webwidget_)
2332 return;
2333
2334 if (is_fullscreen_) {
2335 webwidget_->didEnterFullScreen();
2336 } else {
2337 webwidget_->didExitFullScreen();
2338 }
[email protected]2b624c562011-10-27 22:58:262339}
2340
[email protected]699ab0d2009-04-23 23:19:142341void RenderWidget::SetBackground(const SkBitmap& background) {
2342 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:462343
[email protected]699ab0d2009-04-23 23:19:142344 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:282345 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:142346}
2347
[email protected]674741932009-02-04 23:44:462348bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:052349 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462350}
2351
2352bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:052353 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:462354}
2355
2356void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:052357 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:462358}
2359
2360void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:052361 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:462362}
2363
2364void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:052365 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:462366}
2367
[email protected]b18583c2012-12-18 06:55:272368static bool IsDateTimeInput(ui::TextInputType type) {
2369 return type == ui::TEXT_INPUT_TYPE_DATE ||
2370 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
2371 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
2372 type == ui::TEXT_INPUT_TYPE_MONTH ||
2373 type == ui::TEXT_INPUT_TYPE_TIME ||
2374 type == ui::TEXT_INPUT_TYPE_WEEK;
2375}
2376
[email protected]66fca5bc2013-05-23 06:58:292377
2378void RenderWidget::StartHandlingImeEvent() {
2379 DCHECK(!handling_ime_event_);
2380 handling_ime_event_ = true;
2381}
2382
2383void RenderWidget::FinishHandlingImeEvent() {
2384 DCHECK(handling_ime_event_);
2385 handling_ime_event_ = false;
2386 // While handling an ime event, text input state and selection bounds updates
2387 // are ignored. These must explicitly be updated once finished handling the
2388 // ime event.
2389 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472390#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132391 UpdateTextInputState(false, false);
[email protected]cb9e2632013-06-18 11:26:472392#endif
[email protected]66fca5bc2013-05-23 06:58:292393}
2394
[email protected]cb9e2632013-06-18 11:26:472395void RenderWidget::UpdateTextInputType() {
[email protected]11f22bb62013-09-24 04:59:062396 // On Windows, not only an IME but also an on-screen keyboard relies on the
2397 // latest TextInputType to optimize its layout and functionality. Thus
2398 // |input_method_is_active_| is no longer an appropriate condition to suppress
2399 // TextInputTypeChanged IPC on Windows.
2400 // TODO(yukawa, yoichio): Consider to stop checking |input_method_is_active_|
2401 // on other platforms as well as Windows if the overhead is acceptable.
2402#if !defined(OS_WIN)
[email protected]cb9e2632013-06-18 11:26:472403 if (!input_method_is_active_)
2404 return;
[email protected]11f22bb62013-09-24 04:59:062405#endif
[email protected]cb9e2632013-06-18 11:26:472406
[email protected]e7c569d2013-07-17 16:00:362407 ui::TextInputType new_type = GetTextInputType();
[email protected]cb9e2632013-06-18 11:26:472408 if (IsDateTimeInput(new_type))
2409 return; // Not considered as a text input field in WebKit/Chromium.
2410
2411 bool new_can_compose_inline = CanComposeInline();
2412
[email protected]e7c569d2013-07-17 16:00:362413 WebKit::WebTextInputInfo new_info;
2414 if (webwidget_)
2415 new_info = webwidget_->textInputInfo();
[email protected]b256eca2013-07-11 10:57:402416 const ui::TextInputMode new_mode = ConvertInputMode(new_info.inputMode);
2417
[email protected]cb9e2632013-06-18 11:26:472418 if (text_input_type_ != new_type
[email protected]b256eca2013-07-11 10:57:402419 || can_compose_inline_ != new_can_compose_inline
2420 || text_input_mode_ != new_mode) {
[email protected]cb9e2632013-06-18 11:26:472421 Send(new ViewHostMsg_TextInputTypeChanged(routing_id(),
2422 new_type,
[email protected]86ba5fcb2013-09-04 00:36:532423 new_mode,
2424 new_can_compose_inline));
[email protected]cb9e2632013-06-18 11:26:472425 text_input_type_ = new_type;
2426 can_compose_inline_ = new_can_compose_inline;
[email protected]b256eca2013-07-11 10:57:402427 text_input_mode_ = new_mode;
[email protected]cb9e2632013-06-18 11:26:472428 }
2429}
2430
2431#if defined(OS_ANDROID)
[email protected]0d1ebed12013-08-05 22:01:132432void RenderWidget::UpdateTextInputState(bool show_ime_if_needed,
2433 bool send_ime_ack) {
[email protected]e8f775f2013-02-14 21:00:502434 if (handling_ime_event_)
2435 return;
[email protected]3306f262012-09-21 19:20:422436 if (!show_ime_if_needed && !input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:292437 return;
[email protected]ad26ef42011-06-17 07:59:452438 ui::TextInputType new_type = GetTextInputType();
[email protected]b18583c2012-12-18 06:55:272439 if (IsDateTimeInput(new_type))
2440 return; // Not considered as a text input field in WebKit/Chromium.
2441
[email protected]5b739cb2012-08-21 20:35:212442 WebKit::WebTextInputInfo new_info;
2443 if (webwidget_)
2444 new_info = webwidget_->textInputInfo();
2445
[email protected]ad26ef42011-06-17 07:59:452446 bool new_can_compose_inline = CanComposeInline();
[email protected]5b739cb2012-08-21 20:35:212447
[email protected]3306f262012-09-21 19:20:422448 // Only sends text input params if they are changed or if the ime should be
2449 // shown.
2450 if (show_ime_if_needed || (text_input_type_ != new_type
2451 || text_input_info_ != new_info
2452 || can_compose_inline_ != new_can_compose_inline)) {
[email protected]5b739cb2012-08-21 20:35:212453 ViewHostMsg_TextInputState_Params p;
2454 p.type = new_type;
2455 p.value = new_info.value.utf8();
2456 p.selection_start = new_info.selectionStart;
2457 p.selection_end = new_info.selectionEnd;
2458 p.composition_start = new_info.compositionStart;
2459 p.composition_end = new_info.compositionEnd;
2460 p.can_compose_inline = new_can_compose_inline;
[email protected]3306f262012-09-21 19:20:422461 p.show_ime_if_needed = show_ime_if_needed;
[email protected]0d1ebed12013-08-05 22:01:132462 p.require_ack = send_ime_ack;
2463 if (p.require_ack)
2464 IncrementOutstandingImeEventAcks();
[email protected]5b739cb2012-08-21 20:35:212465 Send(new ViewHostMsg_TextInputStateChanged(routing_id(), p));
2466
2467 text_input_info_ = new_info;
[email protected]fa7b1dc2010-06-23 17:53:042468 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:452469 can_compose_inline_ = new_can_compose_inline;
initial.commit09911bf2008-07-26 23:55:292470 }
initial.commit09911bf2008-07-26 23:55:292471}
[email protected]cb9e2632013-06-18 11:26:472472#endif
initial.commit09911bf2008-07-26 23:55:292473
[email protected]7c8873e2013-02-05 08:03:012474void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
2475 WebRect focus_webrect;
2476 WebRect anchor_webrect;
2477 webwidget_->selectionBounds(focus_webrect, anchor_webrect);
2478 *focus = focus_webrect;
2479 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322480}
2481
[email protected]e99ef6f2011-10-16 01:13:002482void RenderWidget::UpdateSelectionBounds() {
2483 if (!webwidget_)
2484 return;
[email protected]66fca5bc2013-05-23 06:58:292485 if (handling_ime_event_)
2486 return;
[email protected]e99ef6f2011-10-16 01:13:002487
[email protected]7c8873e2013-02-05 08:03:012488 ViewHostMsg_SelectionBounds_Params params;
2489 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2490 if (selection_anchor_rect_ != params.anchor_rect ||
2491 selection_focus_rect_ != params.focus_rect) {
2492 selection_anchor_rect_ = params.anchor_rect;
2493 selection_focus_rect_ = params.focus_rect;
2494 webwidget_->selectionTextDirection(params.focus_dir, params.anchor_dir);
[email protected]129b7382013-02-12 02:14:292495 params.is_anchor_first = webwidget_->isSelectionAnchorFirst();
[email protected]7c8873e2013-02-05 08:03:012496 Send(new ViewHostMsg_SelectionBoundsChanged(routing_id_, params));
[email protected]58b48a0d2012-06-13 07:01:352497 }
[email protected]501ea13d2013-07-09 17:03:292498#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362499 UpdateCompositionInfo(false);
[email protected]501ea13d2013-07-09 17:03:292500#endif
[email protected]e99ef6f2011-10-16 01:13:002501}
2502
[email protected]73bf95812011-10-12 11:38:322503// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:452504COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
2505 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
2506COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
2507 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
2508COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
2509 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:182510COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
2511 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
2512COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
2513 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
2514COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
2515 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
2516COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
2517 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
2518COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
2519 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]feb8cf752012-06-08 04:48:002520COMPILE_ASSERT(int(WebKit::WebTextInputTypeDate) == \
2521 int(ui::TEXT_INPUT_TYPE_DATE), mismatching_enum);
2522COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTime) == \
2523 int(ui::TEXT_INPUT_TYPE_DATE_TIME), mismatching_enum);
2524COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeLocal) == \
2525 int(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL), mismatching_enum);
2526COMPILE_ASSERT(int(WebKit::WebTextInputTypeMonth) == \
2527 int(ui::TEXT_INPUT_TYPE_MONTH), mismatching_enum);
2528COMPILE_ASSERT(int(WebKit::WebTextInputTypeTime) == \
2529 int(ui::TEXT_INPUT_TYPE_TIME), mismatching_enum);
2530COMPILE_ASSERT(int(WebKit::WebTextInputTypeWeek) == \
2531 int(ui::TEXT_INPUT_TYPE_WEEK), mismatching_enum);
[email protected]2a9893672012-11-09 20:33:012532COMPILE_ASSERT(int(WebKit::WebTextInputTypeTextArea) == \
2533 int(ui::TEXT_INPUT_TYPE_TEXT_AREA), mismatching_enums);
2534COMPILE_ASSERT(int(WebKit::WebTextInputTypeContentEditable) == \
2535 int(ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE), mismatching_enums);
[email protected]8b4992e2013-03-01 15:42:152536COMPILE_ASSERT(int(WebKit::WebTextInputTypeDateTimeField) == \
2537 int(ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:452538
[email protected]5b739cb2012-08-21 20:35:212539ui::TextInputType RenderWidget::WebKitToUiTextInputType(
2540 WebKit::WebTextInputType type) {
2541 // Check the type is in the range representable by ui::TextInputType.
2542 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX)) <<
2543 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
2544 return static_cast<ui::TextInputType>(type);
2545}
2546
[email protected]ad26ef42011-06-17 07:59:452547ui::TextInputType RenderWidget::GetTextInputType() {
[email protected]8969bb3f2012-11-30 21:49:272548 if (webwidget_)
2549 return WebKitToUiTextInputType(webwidget_->textInputInfo().type);
[email protected]ad26ef42011-06-17 07:59:452550 return ui::TEXT_INPUT_TYPE_NONE;
2551}
2552
[email protected]501ea13d2013-07-09 17:03:292553#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
2554void RenderWidget::UpdateCompositionInfo(bool should_update_range) {
[email protected]db4fc1e2013-09-06 20:01:512555 gfx::Range range = gfx::Range();
[email protected]501ea13d2013-07-09 17:03:292556 if (should_update_range) {
2557 GetCompositionRange(&range);
2558 } else {
2559 range = composition_range_;
2560 }
2561 std::vector<gfx::Rect> character_bounds;
2562 GetCompositionCharacterBounds(&character_bounds);
2563
2564 if (!ShouldUpdateCompositionInfo(range, character_bounds))
2565 return;
2566 composition_character_bounds_ = character_bounds;
2567 composition_range_ = range;
2568 Send(new ViewHostMsg_ImeCompositionRangeChanged(
2569 routing_id(), composition_range_, composition_character_bounds_));
2570}
2571
[email protected]58b48a0d2012-06-13 07:01:352572void RenderWidget::GetCompositionCharacterBounds(
2573 std::vector<gfx::Rect>* bounds) {
2574 DCHECK(bounds);
2575 bounds->clear();
2576}
2577
[email protected]db4fc1e2013-09-06 20:01:512578void RenderWidget::GetCompositionRange(gfx::Range* range) {
[email protected]88dbe32f2013-06-20 23:31:362579 size_t location, length;
2580 if (webwidget_->compositionRange(&location, &length)) {
2581 range->set_start(location);
2582 range->set_end(location + length);
2583 } else if (webwidget_->caretOrSelectionRange(&location, &length)) {
2584 range->set_start(location);
2585 range->set_end(location + length);
2586 } else {
[email protected]db4fc1e2013-09-06 20:01:512587 *range = gfx::Range::InvalidRange();
[email protected]88dbe32f2013-06-20 23:31:362588 }
2589}
2590
[email protected]501ea13d2013-07-09 17:03:292591bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512592 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292593 const std::vector<gfx::Rect>& bounds) {
2594 if (composition_range_ != range)
2595 return true;
2596 if (bounds.size() != composition_character_bounds_.size())
2597 return true;
2598 for (size_t i = 0; i < bounds.size(); ++i) {
2599 if (bounds[i] != composition_character_bounds_[i])
2600 return true;
2601 }
2602 return false;
2603}
2604#endif
2605
[email protected]ad26ef42011-06-17 07:59:452606bool RenderWidget::CanComposeInline() {
2607 return true;
[email protected]56ea1a62011-05-30 07:05:572608}
2609
[email protected]4873c7d2009-07-16 06:36:282610WebScreenInfo RenderWidget::screenInfo() {
[email protected]842f10652012-06-06 01:54:042611 return screen_info_;
[email protected]4873c7d2009-07-16 06:36:282612}
2613
[email protected]f660d9c2012-06-06 18:31:212614float RenderWidget::deviceScaleFactor() {
2615 return device_scale_factor_;
2616}
2617
[email protected]fa7b1dc2010-06-23 17:53:042618void RenderWidget::resetInputMethod() {
2619 if (!input_method_is_active_)
2620 return;
2621
[email protected]0e45bd02013-07-12 20:20:022622 ImeEventGuard guard(this);
[email protected]fa7b1dc2010-06-23 17:53:042623 // If the last text input type is not None, then we should finish any
2624 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:452625 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:042626 // If a composition text exists, then we need to let the browser process
2627 // to cancel the input method's ongoing composition session.
2628 if (webwidget_->confirmComposition())
2629 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
2630 }
[email protected]d4cff272011-05-02 15:46:012631
[email protected]501ea13d2013-07-09 17:03:292632#if defined(OS_MACOSX) || defined(OS_WIN) || defined(USE_AURA)
[email protected]88dbe32f2013-06-20 23:31:362633 UpdateCompositionInfo(true);
[email protected]501ea13d2013-07-09 17:03:292634#endif
[email protected]fa7b1dc2010-06-23 17:53:042635}
2636
[email protected]c68c3e4e2013-01-24 00:36:562637void RenderWidget::didHandleGestureEvent(
2638 const WebGestureEvent& event,
2639 bool event_cancelled) {
2640#if defined(OS_ANDROID)
2641 if (event_cancelled)
2642 return;
2643 if (event.type == WebInputEvent::GestureTap ||
2644 event.type == WebInputEvent::GestureLongPress) {
[email protected]0d1ebed12013-08-05 22:01:132645 UpdateTextInputState(true, true);
[email protected]c68c3e4e2013-01-24 00:36:562646 }
2647#endif
2648}
2649
[email protected]29e2fb42013-07-19 01:13:472650void RenderWidget::SchedulePluginMove(const WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:292651 size_t i = 0;
2652 for (; i < plugin_window_moves_.size(); ++i) {
2653 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:582654 if (move.rects_valid) {
2655 plugin_window_moves_[i] = move;
2656 } else {
2657 plugin_window_moves_[i].visible = move.visible;
2658 }
initial.commit09911bf2008-07-26 23:55:292659 break;
2660 }
2661 }
2662
2663 if (i == plugin_window_moves_.size())
2664 plugin_window_moves_.push_back(move);
2665}
[email protected]268654772009-08-06 23:02:042666
2667void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
2668 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
2669 i != plugin_window_moves_.end(); ++i) {
2670 if (i->window == window) {
2671 plugin_window_moves_.erase(i);
2672 break;
2673 }
2674 }
2675}
[email protected]67bfb83f2011-09-22 03:36:372676
[email protected]b63d58d2012-11-26 22:37:442677void RenderWidget::GetRenderingStats(
2678 WebKit::WebRenderingStatsImpl& stats) const {
[email protected]ba91a792013-02-06 09:48:282679 if (compositor_)
[email protected]635353c2013-03-06 09:11:202680 compositor_->GetRenderingStats(&stats.rendering_stats);
[email protected]b63d58d2012-11-26 22:37:442681
[email protected]c1e6cc062013-08-24 03:35:352682 stats.rendering_stats.Add(
2683 legacy_software_mode_stats_->GetRenderingStats());
[email protected]fef5e3972012-08-07 03:59:472684}
2685
[email protected]e9ff79c2012-10-19 21:31:262686bool RenderWidget::GetGpuRenderingStats(GpuRenderingStats* stats) const {
[email protected]63b465922012-09-06 02:04:522687 GpuChannelHost* gpu_channel = RenderThreadImpl::current()->GetGpuChannel();
2688 if (!gpu_channel)
2689 return false;
2690
2691 return gpu_channel->CollectRenderingStatsForSurface(surface_id(), stats);
2692}
2693
[email protected]24ed0432013-04-24 07:50:312694RenderWidgetCompositor* RenderWidget::compositor() const {
2695 return compositor_.get();
2696}
2697
[email protected]3639aa82013-06-04 11:00:042698void RenderWidget::OnSetBrowserRenderingStats(
2699 const BrowserRenderingStats& stats) {
2700 browser_rendering_stats_ = stats;
2701}
2702
2703void RenderWidget::GetBrowserRenderingStats(BrowserRenderingStats* stats) {
2704 *stats = browser_rendering_stats_;
2705}
2706
[email protected]0c2ebef2013-04-03 12:14:102707void RenderWidget::BeginSmoothScroll(
[email protected]0e241b4b2012-08-18 09:06:272708 bool down,
[email protected]79fa22e2013-08-23 15:18:122709 const SyntheticGestureCompletionCallback& callback,
[email protected]267909d2012-10-20 04:36:192710 int pixels_to_scroll,
[email protected]ebd8b562012-10-09 14:44:292711 int mouse_event_x,
2712 int mouse_event_y) {
[email protected]0e241b4b2012-08-18 09:06:272713 DCHECK(!callback.is_null());
[email protected]267909d2012-10-20 04:36:192714
2715 ViewHostMsg_BeginSmoothScroll_Params params;
2716 params.scroll_down = down;
2717 params.pixels_to_scroll = pixels_to_scroll;
2718 params.mouse_event_x = mouse_event_x;
2719 params.mouse_event_y = mouse_event_y;
2720
[email protected]4a9dba42013-04-29 18:24:222721 Send(new ViewHostMsg_BeginSmoothScroll(routing_id_, params));
[email protected]79fa22e2013-08-23 15:18:122722 pending_synthetic_gesture_ = callback;
[email protected]a39ca1652012-07-13 21:30:582723}
2724
[email protected]1e1dd182013-09-12 01:51:152725void RenderWidget::BeginPinch(
2726 bool zoom_in,
2727 int pixels_to_move,
2728 int anchor_x,
2729 int anchor_y,
2730 const SyntheticGestureCompletionCallback& callback) {
2731 DCHECK(!callback.is_null());
2732
2733 ViewHostMsg_BeginPinch_Params params;
2734 params.zoom_in = zoom_in;
2735 params.pixels_to_move = pixels_to_move;
2736 params.anchor_x = anchor_x;
2737 params.anchor_y = anchor_y;
2738
2739 Send(new ViewHostMsg_BeginPinch(routing_id_, params));
2740 pending_synthetic_gesture_ = callback;
2741}
2742
[email protected]67bfb83f2011-09-22 03:36:372743bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
2744 return false;
2745}
[email protected]c3d45532011-10-07 19:20:402746
[email protected]f56c7872013-06-18 12:31:572747bool RenderWidget::WillHandleKeyEvent(const WebKit::WebKeyboardEvent& event) {
2748 return false;
2749}
2750
[email protected]41d86852012-11-07 12:23:242751bool RenderWidget::WillHandleGestureEvent(
2752 const WebKit::WebGestureEvent& event) {
2753 return false;
2754}
2755
[email protected]ce6689f2013-03-29 12:52:552756void RenderWidget::hasTouchEventHandlers(bool has_handlers) {
2757 Send(new ViewHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
2758}
2759
[email protected]3d5c243b2012-11-30 00:26:012760bool RenderWidget::HasTouchEventHandlersAt(const gfx::Point& point) const {
2761 return true;
2762}
2763
[email protected]0634cdd42013-08-16 00:46:092764scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
2765RenderWidget::CreateGraphicsContext3D(
[email protected]92fd8c02013-03-29 08:54:152766 const WebKit::WebGraphicsContext3D::Attributes& attributes) {
[email protected]ed7defa2013-03-12 21:29:592767 if (!webwidget_)
[email protected]0634cdd42013-08-16 00:46:092768 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]ebc0e1df2013-08-01 02:46:222769 if (CommandLine::ForCurrentProcess()->HasSwitch(
2770 switches::kDisableGpuCompositing))
[email protected]0634cdd42013-08-16 00:46:092771 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]bf676c192013-10-15 23:59:252772 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context(
2773 new WebGraphicsContext3DCommandBufferImpl(
2774 surface_id(),
2775 GetURLForGraphicsContext3D(),
2776 RenderThreadImpl::current(),
2777 weak_ptr_factory_.GetWeakPtr()));
[email protected]ed7defa2013-03-12 21:29:592778
[email protected]b6eb8e332013-09-10 00:51:012779#if defined(OS_ANDROID)
2780 // If we raster too fast we become upload bound, and pending
2781 // uploads consume memory. For maximum upload throughput, we would
2782 // want to allow for upload_throughput * pipeline_time of pending
2783 // uploads, after which we are just wasting memory. Since we don't
2784 // know our upload throughput yet, this just caps our memory usage.
2785 size_t divider = 1;
2786 if (base::android::SysUtils::IsLowEndDevice())
[email protected]657be322013-09-20 08:50:032787 divider = 6;
[email protected]b6eb8e332013-09-10 00:51:012788 // For reference Nexus10 can upload 1MB in about 2.5ms.
[email protected]657be322013-09-20 08:50:032789 const double max_mb_uploaded_per_ms = 2.0 / (5 * divider);
[email protected]b6eb8e332013-09-10 00:51:012790 // Deadline to draw a frame to achieve 60 frames per second.
2791 const size_t kMillisecondsPerFrame = 16;
2792 // Assuming a two frame deep pipeline between the CPU and the GPU.
[email protected]657be322013-09-20 08:50:032793 size_t max_transfer_buffer_usage_mb =
2794 static_cast<size_t>(2 * kMillisecondsPerFrame * max_mb_uploaded_per_ms);
2795 static const size_t kBytesPerMegabyte = 1024 * 1024;
[email protected]b6eb8e332013-09-10 00:51:012796 // We keep the MappedMemoryReclaimLimit the same as the upload limit
2797 // to avoid unnecessarily stalling the compositor thread.
[email protected]bf676c192013-10-15 23:59:252798 const size_t mapped_memory_reclaim_limit =
[email protected]657be322013-09-20 08:50:032799 max_transfer_buffer_usage_mb * kBytesPerMegabyte;
[email protected]bf676c192013-10-15 23:59:252800#else
2801 const size_t mapped_memory_reclaim_limit =
2802 WebGraphicsContext3DCommandBufferImpl::kNoLimit;
[email protected]b6eb8e332013-09-10 00:51:012803#endif
[email protected]bf676c192013-10-15 23:59:252804 if (!context->Initialize(
[email protected]8f746982013-03-21 06:28:032805 attributes,
2806 false /* bind generates resources */,
[email protected]bf676c192013-10-15 23:59:252807 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
2808 kDefaultCommandBufferSize,
2809 kDefaultStartTransferBufferSize,
2810 kDefaultMinTransferBufferSize,
2811 kDefaultMaxTransferBufferSize,
2812 mapped_memory_reclaim_limit))
2813 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]0634cdd42013-08-16 00:46:092814 return context.Pass();
[email protected]ed7defa2013-03-12 21:29:592815}
2816
[email protected]e9ff79c2012-10-19 21:31:262817} // namespace content