blob: 03d077641ace14e1f9541905545e84ce9e8184f3 [file] [log] [blame]
[email protected]c5218762011-03-24 17:48:511// Copyright (c) 2011 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"
initial.commit09911bf2008-07-26 23:55:2912#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]aa4117f2011-12-09 22:19:2114#include "base/stl_util.h"
[email protected]8a9d6ca32011-06-06 20:11:3015#include "base/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4816#include "build/build_config.h"
[email protected]992db4c2011-05-12 15:37:1517#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5018#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2919#include "content/public/common/content_switches.h"
[email protected]3da12cb2011-10-08 02:25:0420#include "content/renderer/gpu/compositor_thread.h"
[email protected]8704f89b2011-04-15 00:30:0521#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4422#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1923#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1824#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4825#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5226#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3727#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]e6e90dc2011-12-03 00:01:3728#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPoint.h"
[email protected]8bd0fe62011-01-17 06:44:3729#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
30#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0131#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]e6e90dc2011-12-03 00:01:3732#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
[email protected]8bd0fe62011-01-17 06:44:3733#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
[email protected]e6e90dc2011-12-03 00:01:3734#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
35#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]08397d52011-02-05 01:53:3836#include "ui/gfx/point.h"
37#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1238#include "ui/gfx/surface/transport_dib.h"
[email protected]bd37ae252011-06-03 01:28:1839#include "ui/gfx/gl/gl_switches.h"
[email protected]8c89e7792009-08-19 21:18:3440#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5041#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4642#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4843
44#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4945#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5246#include "third_party/skia/include/core/SkPixelRef.h"
47#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4848#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4449
[email protected]8bd0fe62011-01-17 06:44:3750#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2951
[email protected]fa7b1dc2010-06-23 17:53:0452using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3053using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2254using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0655using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2856using WebKit::WebNavigationPolicy;
[email protected]e99ef6f2011-10-16 01:13:0057using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2858using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5959using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1860using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0161using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5262using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1963using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5264using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2865using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2466using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0467using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1868using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2769using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2270
[email protected]380244092011-10-07 17:26:2771RenderWidget::RenderWidget(WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2972 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1173 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2974 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4275 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3576 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2977 next_paint_flags_(0),
[email protected]0cff69e2011-11-22 22:26:0678 filtered_time_per_frame_(0.0f),
[email protected]53d3f302009-12-21 04:42:0579 update_reply_pending_(false),
[email protected]65225772011-05-12 21:10:2480 using_asynchronous_swapbuffers_(false),
81 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2982 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2983 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0984 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2985 needs_repainting_on_restore_(false),
86 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4987 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4888 closing_(false),
[email protected]992db4c2011-05-12 15:37:1589 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0490 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:4591 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
92 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:1293 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4894 pending_window_rect_count_(0),
[email protected]b68a0e52011-12-08 15:11:1295 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5096 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2197 animation_update_pending_(false),
[email protected]65225772011-05-12 21:10:2498 animation_task_posted_(false),
99 invalidation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:28100 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:27101 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18102 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
103 switches::kDisableGpuVsync);
initial.commit09911bf2008-07-26 23:55:29104}
105
106RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11107 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]aa4117f2011-12-09 22:19:21108 STLDeleteElements(&updates_pending_swap_);
[email protected]b4d08452010-10-05 17:34:35109 if (current_paint_buf_) {
110 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
111 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29112 }
[email protected]992db4c2011-05-12 15:37:15113 // If we are swapped out, we have released already.
114 if (!is_swapped_out_)
115 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29116}
117
[email protected]484955942010-08-19 16:13:18118// static
[email protected]8085dbc82008-09-26 22:53:44119RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]3e2b375b2010-04-07 17:03:12120 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29121 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27122 scoped_refptr<RenderWidget> widget(new RenderWidget(popup_type));
initial.commit09911bf2008-07-26 23:55:29123 widget->Init(opener_id); // adds reference
124 return widget;
125}
126
[email protected]484955942010-08-19 16:13:18127// static
128WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
129 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03130 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18131 break;
132 case WebKit::WebPopupTypeSelect:
133 case WebKit::WebPopupTypeSuggestion:
134 return WebPopupMenu::create(render_widget);
135 default:
136 NOTREACHED();
137 }
138 return NULL;
139}
140
initial.commit09911bf2008-07-26 23:55:29141void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18142 DoInit(opener_id,
143 RenderWidget::CreateWebWidget(this),
144 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
145}
146
[email protected]484955942010-08-19 16:13:18147void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06148 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18149 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29150 DCHECK(!webwidget_);
151
152 if (opener_id != MSG_ROUTING_NONE)
153 opener_id_ = opener_id;
154
[email protected]484955942010-08-19 16:13:18155 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29156
[email protected]380244092011-10-07 17:26:27157 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29158 if (result) {
[email protected]380244092011-10-07 17:26:27159 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29160 // Take a reference on behalf of the RenderThread. This will be balanced
161 // when we receive ViewMsg_Close.
162 AddRef();
163 } else {
164 DCHECK(false);
165 }
166}
167
168// This is used to complete pending inits and non-pending inits. For non-
169// pending cases, the parent will be the same as the current parent. This
170// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55171void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29172 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29173
174 host_window_ = parent_hwnd;
175
[email protected]6de74452009-02-25 18:04:59176 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29177}
178
[email protected]992db4c2011-05-12 15:37:15179void RenderWidget::SetSwappedOut(bool is_swapped_out) {
180 // We should only toggle between states.
181 DCHECK(is_swapped_out_ != is_swapped_out);
182 is_swapped_out_ = is_swapped_out;
183
184 // If we are swapping out, we will call ReleaseProcess, allowing the process
185 // to exit if all of its RenderViews are swapped out. We wait until the
186 // WasSwappedOut call to do this, to avoid showing the sad tab.
187 // If we are swapping in, we call AddRefProcess to prevent the process from
188 // exiting.
189 if (!is_swapped_out)
190 RenderProcess::current()->AddRefProcess();
191}
192
[email protected]a95986a82010-12-24 06:19:28193bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
194 bool handled = true;
195 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
196 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
197 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
198 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
199 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
200 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15201 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28202 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
203 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
204 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
205 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
206 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
207 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
208 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
209 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
210 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
211 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
212 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
213 IPC_MESSAGE_UNHANDLED(handled = false)
214 IPC_END_MESSAGE_MAP()
215 return handled;
216}
initial.commit09911bf2008-07-26 23:55:29217
218bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15219 // Don't send any messages after the browser has told us to close, and filter
220 // most outgoing messages while swapped out.
221 if ((is_swapped_out_ &&
222 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
223 closing_) {
initial.commit09911bf2008-07-26 23:55:29224 delete message;
225 return false;
226 }
227
228 // If given a messsage without a routing ID, then assign our routing ID.
229 if (message->routing_id() == MSG_ROUTING_NONE)
230 message->set_routing_id(routing_id_);
231
[email protected]380244092011-10-07 17:26:27232 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44233}
234
initial.commit09911bf2008-07-26 23:55:29235// Got a response from the browser after the renderer decided to create a new
236// view.
[email protected]c03a6e72011-04-19 21:42:06237void RenderWidget::OnCreatingNewAck(
[email protected]2d7c8552011-06-27 19:21:55238 gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29239 DCHECK(routing_id_ != MSG_ROUTING_NONE);
240
[email protected]2d7c8552011-06-27 19:21:55241 CompleteInit(parent);
initial.commit09911bf2008-07-26 23:55:29242}
243
244void RenderWidget::OnClose() {
245 if (closing_)
246 return;
247 closing_ = true;
248
249 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03250 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27251 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03252 SetHidden(false);
253 }
initial.commit09911bf2008-07-26 23:55:29254
initial.commit09911bf2008-07-26 23:55:29255 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25256 // now. Post a task that only gets invoked when there are no nested message
257 // loops.
[email protected]32876ae2011-11-15 22:25:21258 MessageLoop::current()->PostNonNestableTask(
259 FROM_HERE, NewRunnableMethod(this, &RenderWidget::Close));
[email protected]d3fc25652009-02-24 22:31:25260
261 // Balances the AddRef taken when we called AddRoute.
262 Release();
initial.commit09911bf2008-07-26 23:55:29263}
264
[email protected]f21c613a2009-02-12 14:46:17265void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09266 const gfx::Rect& resizer_rect,
267 bool is_fullscreen) {
initial.commit09911bf2008-07-26 23:55:29268 // During shutdown we can just ignore this message.
269 if (!webwidget_)
270 return;
271
[email protected]e2356242010-11-16 22:33:03272 // We shouldn't be asked to resize to our current size.
273 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
274
[email protected]f21c613a2009-02-12 14:46:17275 // Remember the rect where the resize corner will be drawn.
276 resizer_rect_ = resizer_rect;
277
[email protected]2b624c562011-10-27 22:58:26278 // NOTE: We may have entered fullscreen mode without changing our size.
279 bool fullscreen_change = is_fullscreen_ != is_fullscreen;
280 if (fullscreen_change)
281 WillToggleFullscreen();
[email protected]ee41e7d22011-10-14 19:34:09282 is_fullscreen_ = is_fullscreen;
initial.commit09911bf2008-07-26 23:55:29283
[email protected]2b624c562011-10-27 22:58:26284 if (size_ != new_size) {
285 // TODO(darin): We should not need to reset this here.
286 SetHidden(false);
287 needs_repainting_on_restore_ = false;
initial.commit09911bf2008-07-26 23:55:29288
[email protected]2b624c562011-10-27 22:58:26289 size_ = new_size;
[email protected]552e6002009-11-19 05:24:57290
[email protected]2b624c562011-10-27 22:58:26291 // We should not be sent a Resize message if we have not ACK'd the previous
292 DCHECK(!next_paint_is_resize_ack());
293
294 paint_aggregator_.ClearPendingUpdate();
295
296 // When resizing, we want to wait to paint before ACK'ing the resize. This
297 // ensures that we only resize as fast as we can paint. We only need to
298 // send an ACK if we are resized to a non-empty rect.
299 webwidget_->resize(new_size);
300 if (!new_size.IsEmpty()) {
301 if (!is_accelerated_compositing_active_) {
302 // Resize should have caused an invalidation of the entire view.
303 DCHECK(paint_aggregator_.HasPendingUpdate());
304 }
305
306 // We will send the Resize_ACK flag once we paint again.
307 set_next_paint_is_resize_ack();
[email protected]f98d7e3c2010-09-13 22:30:46308 }
initial.commit09911bf2008-07-26 23:55:29309 }
[email protected]2b624c562011-10-27 22:58:26310
311 if (fullscreen_change)
312 DidToggleFullscreen();
initial.commit09911bf2008-07-26 23:55:29313}
314
315void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31316 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29317 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03318 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29319}
320
321void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31322 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29323 // During shutdown we can just ignore this message.
324 if (!webwidget_)
325 return;
326
327 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03328 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29329
330 if (!needs_repainting && !needs_repainting_on_restore_)
331 return;
332 needs_repainting_on_restore_ = false;
333
[email protected]d65adb12010-04-28 17:26:49334 // Tag the next paint as a restore ack, which is picked up by
335 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29336 set_next_paint_is_restore_ack();
337
338 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56339 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46340 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
341 } else {
342 scheduleComposite();
343 }
initial.commit09911bf2008-07-26 23:55:29344}
345
[email protected]992db4c2011-05-12 15:37:15346void RenderWidget::OnWasSwappedOut() {
347 // If we have been swapped out and no one else is using this process,
348 // it's safe to exit now. If we get swapped back in, we will call
349 // AddRefProcess in SetSwappedOut.
350 if (is_swapped_out_)
351 RenderProcess::current()->ReleaseProcess();
352}
353
[email protected]53d3f302009-12-21 04:42:05354void RenderWidget::OnRequestMoveAck() {
355 DCHECK(pending_window_rect_count_);
356 pending_window_rect_count_--;
357}
358
359void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58360 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]aa4117f2011-12-09 22:19:21361 DCHECK(update_reply_pending_);
[email protected]53d3f302009-12-21 04:42:05362 update_reply_pending_ = false;
363
[email protected]b4d08452010-10-05 17:34:35364 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
365 // have no current paint buffer.
366 if (current_paint_buf_) {
367 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
368 current_paint_buf_ = NULL;
369 }
370
[email protected]65225772011-05-12 21:10:24371 // If swapbuffers is still pending, then defer the update until the
372 // swapbuffers occurs.
373 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
374 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
375 return;
376 }
377
[email protected]00c39612010-03-06 02:53:28378 // Notify subclasses.
[email protected]b5db7eb2011-11-29 09:11:50379 if (!is_accelerated_compositing_active_)
380 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25381
initial.commit09911bf2008-07-26 23:55:29382 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24383 DoDeferredUpdateAndSendInputAck();
384}
385
386bool RenderWidget::SupportsAsynchronousSwapBuffers()
387{
388 return false;
389}
390
391void RenderWidget::OnSwapBuffersAborted()
392{
393 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
[email protected]aa4117f2011-12-09 22:19:21394 while (!updates_pending_swap_.empty()) {
395 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
396 updates_pending_swap_.pop_front();
397 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
398 // compositing pass, hence doesn't require an UpdateRect message.
399 if (msg)
400 Send(msg);
401 }
[email protected]65225772011-05-12 21:10:24402 num_swapbuffers_complete_pending_ = 0;
403 using_asynchronous_swapbuffers_ = false;
404 // Schedule another frame so the compositor learns about it.
405 scheduleComposite();
406}
407
[email protected]37a6f302011-07-11 23:43:08408void RenderWidget::OnSwapBuffersPosted() {
409 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
[email protected]aa4117f2011-12-09 22:19:21410
411 if (using_asynchronous_swapbuffers_) {
412 ViewHostMsg_UpdateRect* msg = NULL;
413 // pending_update_params_ can be NULL if the swap doesn't correspond to an
414 // DoDeferredUpdate compositing pass, hence doesn't require an UpdateRect
415 // message.
416 if (pending_update_params_.get()) {
417 msg = new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_);
418 pending_update_params_.reset();
419 }
420 updates_pending_swap_.push_back(msg);
[email protected]37a6f302011-07-11 23:43:08421 num_swapbuffers_complete_pending_++;
[email protected]aa4117f2011-12-09 22:19:21422 }
[email protected]37a6f302011-07-11 23:43:08423}
424
425void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24426 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
427 // When compositing deactivates, we reset the swapbuffers pending count. The
428 // swapbuffers acks may still arrive, however.
429 if (num_swapbuffers_complete_pending_ == 0) {
430 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
431 return;
432 }
[email protected]aa4117f2011-12-09 22:19:21433 DCHECK(!updates_pending_swap_.empty());
434 ViewHostMsg_UpdateRect* msg = updates_pending_swap_.front();
435 updates_pending_swap_.pop_front();
436 // msg can be NULL if the swap doesn't correspond to an DoDeferredUpdate
437 // compositing pass, hence doesn't require an UpdateRect message.
438 if (msg)
439 Send(msg);
[email protected]65225772011-05-12 21:10:24440 num_swapbuffers_complete_pending_--;
441
442 // If update reply is still pending, then defer the update until that reply
443 // occurs.
444 if (update_reply_pending_){
445 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
446 return;
447 }
448
449 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06450 // when we were previously rendering. However, if an invalidation task is not
451 // posted, there may be software rendering work pending. In that case, don't
452 // early out.
453 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24454 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
455 return;
456 }
457
[email protected]65225772011-05-12 21:10:24458 // Continue painting if necessary...
459 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29460}
461
initial.commit09911bf2008-07-26 23:55:29462void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24463 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29464 void* iter = NULL;
465
466 const char* data;
467 int data_length;
[email protected]5dd768212009-08-13 23:34:49468 handling_input_event_ = true;
469 if (!message.ReadData(&iter, &data, &data_length)) {
470 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29471 return;
[email protected]5dd768212009-08-13 23:34:49472 }
initial.commit09911bf2008-07-26 23:55:29473
474 const WebInputEvent* input_event =
475 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48476
[email protected]b68a0e52011-12-08 15:11:12477 bool is_keyboard_shortcut = false;
478 // is_keyboard_shortcut flag is only available for RawKeyDown events.
479 if (input_event->type == WebInputEvent::RawKeyDown)
480 message.ReadBool(&iter, &is_keyboard_shortcut);
481
[email protected]67bfb83f2011-09-22 03:36:37482 bool prevent_default = false;
483 if (WebInputEvent::isMouseEventType(input_event->type)) {
[email protected]936c6f52011-12-13 01:35:26484 const WebMouseEvent& mouse_event =
485 *static_cast<const WebMouseEvent*>(input_event);
486 TRACE_EVENT2("renderer", "HandleMouseMove",
487 "x", mouse_event.x, "y", mouse_event.y);
488 prevent_default = WillHandleMouseEvent(mouse_event);
[email protected]67bfb83f2011-09-22 03:36:37489 }
490
491 bool processed = prevent_default;
[email protected]b68a0e52011-12-08 15:11:12492 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
493 suppress_next_char_events_ = false;
494 if (!processed && webwidget_)
495 processed = webwidget_->handleInputEvent(*input_event);
496 }
497
498 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
499 // it's not processed by webkit, then we need to suppress the upcoming Char
500 // events.
501 if (!processed && is_keyboard_shortcut)
502 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29503
[email protected]a9fb30aa2011-10-06 06:58:46504 IPC::Message* response =
505 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
506 processed);
[email protected]e2824412009-02-27 01:57:05507
[email protected]353a34c2010-05-28 23:35:17508 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03509 input_event->type == WebInputEvent::MouseWheel ||
510 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57511 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24512 // We want to rate limit the input events in this case, so we'll wait for
513 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17514 if (pending_input_event_ack_.get()) {
515 // As two different kinds of events could cause us to postpone an ack
516 // we send it now, if we have one pending. The Browser should never
517 // send us the same kind of event we are delaying the ack for.
518 Send(pending_input_event_ack_.release());
519 }
[email protected]12fbad812009-09-01 18:21:24520 pending_input_event_ack_.reset(response);
521 } else {
522 Send(response);
523 }
524
[email protected]5dd768212009-08-13 23:34:49525 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48526
[email protected]67bfb83f2011-09-22 03:36:37527 if (!prevent_default) {
528 if (WebInputEvent::isKeyboardEventType(input_event->type))
529 DidHandleKeyEvent();
530 if (WebInputEvent::isMouseEventType(input_event->type))
531 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24532 if (WebInputEvent::isTouchEventType(input_event->type))
533 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37534 }
initial.commit09911bf2008-07-26 23:55:29535}
536
537void RenderWidget::OnMouseCaptureLost() {
538 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28539 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29540}
541
542void RenderWidget::OnSetFocus(bool enable) {
543 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33544 if (webwidget_)
545 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29546}
547
548void RenderWidget::ClearFocus() {
549 // We may have got the focus from the browser before this gets processed, in
550 // which case we do not want to unfocus ourself.
551 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28552 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29553}
554
[email protected]2d5d09d52009-06-15 14:29:21555void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00556 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21557 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06558 TRACE_EVENT2("renderer", "PaintRect",
559 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00560 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21561
562 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00563 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
564 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03565
[email protected]699ab0d2009-04-23 23:19:14566 // If there is a custom background, tile it.
567 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14568 SkPaint paint;
569 SkShader* shader = SkShader::CreateBitmapShader(background_,
570 SkShader::kRepeat_TileMode,
571 SkShader::kRepeat_TileMode);
572 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20573
574 // Use kSrc_Mode to handle background_ transparency properly.
575 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
576
577 // Canvas could contain multiple update rects. Clip to given rect so that
578 // we don't accidentally clear other update rects.
579 canvas->save();
580 SkRect clip;
581 clip.set(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
582 SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()));
583 canvas->clipRect(clip);
[email protected]699ab0d2009-04-23 23:19:14584 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20585 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14586 }
587
[email protected]719b36f2010-12-22 20:36:46588 // First see if this rect is a plugin that can paint itself faster.
589 TransportDIB* optimized_dib = NULL;
590 gfx::Rect optimized_copy_rect, optimized_copy_location;
591 webkit::ppapi::PluginInstance* optimized_instance =
592 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
593 &optimized_copy_location,
594 &optimized_copy_rect);
595 if (optimized_instance) {
596 // This plugin can be optimize-painted and we can just ask it to paint
597 // itself. We don't actually need the TransportDIB in this case.
598 //
599 // This is an optimization for PPAPI plugins that know they're on top of
600 // the page content. If this rect is inside such a plugin, we can save some
601 // time and avoid re-rendering the page content which we know will be
602 // covered by the plugin later (this time can be significant, especially
603 // for a playing movie that is invalidating a lot).
604 //
605 // In the plugin movie case, hopefully the similar call to
606 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
607 // painting, because that avoids copying the plugin image to a different
608 // paint rect. Unfortunately, if anything on the page is animating other
609 // than the movie, it break this optimization since the union of the
610 // invalid regions will be larger than the plugin.
611 //
612 // This code optimizes that case, where we can still avoid painting in
613 // WebKit and filling the background (which can be slow) and just painting
614 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
615 // required.
616 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27617 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46618 } else {
619 // Normal painting case.
620 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
621
622 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35623 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46624 }
initial.commit09911bf2008-07-26 23:55:29625
[email protected]4fb66842009-12-04 21:41:00626 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00627 canvas->restore();
628}
629
630void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
631 skia::PlatformCanvas* canvas) {
632 static bool kPaintBorder =
633 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
634 if (!kPaintBorder)
635 return;
636
[email protected]53d3f302009-12-21 04:42:05637 // Cycle through these colors to help distinguish new paint rects.
638 const SkColor colors[] = {
639 SkColorSetARGB(0x3F, 0xFF, 0, 0),
640 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
641 SkColorSetARGB(0x3F, 0, 0, 0xFF),
642 };
643 static int color_selector = 0;
644
[email protected]4fb66842009-12-04 21:41:00645 SkPaint paint;
646 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05647 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00648 paint.setStrokeWidth(1);
649
650 SkIRect irect;
651 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
652 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29653}
654
[email protected]52ccd0ea2011-02-16 01:09:05655void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30656 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]52ccd0ea2011-02-16 01:09:05657 animation_task_posted_ = false;
[email protected]921244e42011-07-20 16:36:30658 if (!animation_update_pending_) {
659 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59660 return;
[email protected]921244e42011-07-20 16:36:30661 }
[email protected]bd37ae252011-06-03 01:28:18662 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59663 // Record when we fired (according to base::Time::Now()) relative to when
664 // we posted the task to quantify how much the base::Time/base::TimeTicks
665 // skew is affecting animations.
666 base::TimeDelta animation_callback_delay = base::Time::Now() -
667 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
668 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
669 animation_callback_delay,
670 base::TimeDelta::FromMilliseconds(0),
671 base::TimeDelta::FromMilliseconds(30),
672 25);
673 }
[email protected]65225772011-05-12 21:10:24674 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24675}
676
[email protected]52ccd0ea2011-02-16 01:09:05677void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59678 if (!animation_update_pending_)
679 return;
[email protected]bd37ae252011-06-03 01:28:18680
681 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
682 int animationInterval = IsRenderingVSynced() ? 16 : 0;
683
[email protected]7c4329e2011-02-18 22:02:59684 base::Time now = base::Time::Now();
[email protected]46d8c8292011-12-12 18:15:05685 if (now >= animation_floor_time_ || is_accelerated_compositing_active_) {
[email protected]921244e42011-07-20 16:36:30686 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]bd37ae252011-06-03 01:28:18687 animation_floor_time_ = now +
688 base::TimeDelta::FromMilliseconds(animationInterval);
689 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59690 // running animation callbacks so that if a callback requests another
691 // we'll be sure to run it at the proper time.
[email protected]32876ae2011-11-15 22:25:21692 MessageLoop::current()->PostDelayedTask(
693 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this),
694 animationInterval);
[email protected]7c4329e2011-02-18 22:02:59695 animation_task_posted_ = true;
696 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30697 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59698 return;
[email protected]5f8b1022011-01-21 23:34:50699 }
[email protected]bd37ae252011-06-03 01:28:18700 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]7c4329e2011-02-18 22:02:59701 if (animation_task_posted_)
702 return;
703 // This code uses base::Time::Now() to calculate the floor and next fire
704 // time because javascript's Date object uses base::Time::Now(). The
705 // message loop uses base::TimeTicks, which on windows can have a
706 // different granularity than base::Time.
707 // The upshot of all this is that this function might be called before
708 // base::Time::Now() has advanced past the animation_floor_time_. To
709 // avoid exposing this delay to javascript, we keep posting delayed
710 // tasks until base::Time::Now() has advanced far enough.
711 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
712 animation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21713 MessageLoop::current()->PostDelayedTask(
714 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this), delay);
[email protected]5f8b1022011-01-21 23:34:50715}
716
[email protected]bd37ae252011-06-03 01:28:18717bool RenderWidget::IsRenderingVSynced() {
718 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
719 // not caught by this check. This will lead to artificially low frame rates
720 // for people who force vsync off at a driver level and expect Chrome to speed
721 // up.
722 return !has_disable_gpu_vsync_switch_;
723}
724
[email protected]65225772011-05-12 21:10:24725void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06726 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24727 invalidation_task_posted_ = false;
728 DoDeferredUpdateAndSendInputAck();
729}
730
731void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05732 DoDeferredUpdate();
733
734 if (pending_input_event_ack_.get())
735 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21736}
737
[email protected]552e6002009-11-19 05:24:57738void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58739 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08740
[email protected]65225772011-05-12 21:10:24741 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29742 return;
[email protected]aa4117f2011-12-09 22:19:21743 if (update_reply_pending_) {
[email protected]65225772011-05-12 21:10:24744 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
745 return;
746 }
[email protected]9ca84622011-06-02 23:46:39747 if (is_accelerated_compositing_active_ &&
748 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24749 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
750 return;
751 }
initial.commit09911bf2008-07-26 23:55:29752
[email protected]552e6002009-11-19 05:24:57753 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29754 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57755 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29756 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24757 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29758 return;
759 }
760
[email protected]0fb93f52011-05-18 23:13:56761 // Tracking of frame rate jitter
762 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]52ccd0ea2011-02-16 01:09:05763 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50764
[email protected]f98d7e3c2010-09-13 22:30:46765 // Layout may generate more invalidation. It may also enable the
766 // GPU acceleration, so make sure to run layout before we send the
767 // GpuRenderingActivated message.
768 webwidget_->layout();
769
[email protected]5f8b1022011-01-21 23:34:50770 // Suppress painting if nothing is dirty. This has to be done after updating
771 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24772 if (!paint_aggregator_.HasPendingUpdate()) {
773 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]5f8b1022011-01-21 23:34:50774 return;
[email protected]65225772011-05-12 21:10:24775 }
[email protected]5f8b1022011-01-21 23:34:50776
[email protected]872ae5b2011-05-26 20:20:50777 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56778 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
779 if(is_accelerated_compositing_active_)
780 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
781 delay,
782 base::TimeDelta::FromMilliseconds(1),
783 base::TimeDelta::FromMilliseconds(60),
784 30);
785 else
786 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
787 delay,
788 base::TimeDelta::FromMilliseconds(1),
789 base::TimeDelta::FromMilliseconds(60),
790 30);
[email protected]872ae5b2011-05-26 20:20:50791
792 // Calculate filtered time per frame:
793 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
794 filtered_time_per_frame_ =
795 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56796 }
797 last_do_deferred_update_time_ = frame_begin_ticks;
798
[email protected]552e6002009-11-19 05:24:57799 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29800 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30801 PaintAggregator::PendingUpdate update;
802 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29803
[email protected]53d3f302009-12-21 04:42:05804 gfx::Rect scroll_damage = update.GetScrollDamage();
805 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29806
[email protected]ca4847f2010-09-24 05:39:15807 // A plugin may be able to do an optimized paint. First check this, in which
808 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46809 // This optimization allows PPAPI plugins that declare themselves on top of
810 // the page (like a traditional windowed plugin) to be able to animate (think
811 // movie playing) without repeatedly re-painting the page underneath, or
812 // copying the plugin backing store (since we can send the plugin's backing
813 // store directly to the browser).
814 //
815 // This optimization only works when the entire invalid region is contained
816 // within the plugin. There is a related optimization in PaintRect for the
817 // case where there may be multiple invalid regions.
[email protected]ca4847f2010-09-24 05:39:15818 TransportDIB* dib = NULL;
[email protected]ca4847f2010-09-24 05:39:15819 gfx::Rect optimized_copy_rect, optimized_copy_location;
[email protected]aa4117f2011-12-09 22:19:21820 DCHECK(!pending_update_params_.get());
821 pending_update_params_.reset(new ViewHostMsg_UpdateRect_Params);
822 pending_update_params_->dx = update.scroll_delta.x();
823 pending_update_params_->dy = update.scroll_delta.y();
824 pending_update_params_->scroll_rect = update.scroll_rect;
825 pending_update_params_->view_size = size_;
826 pending_update_params_->resizer_rect = resizer_rect_;
827 pending_update_params_->plugin_window_moves.swap(plugin_window_moves_);
828 pending_update_params_->flags = next_paint_flags_;
829 pending_update_params_->scroll_offset = GetScrollOffset();
830 pending_update_params_->needs_ack = true;
831 next_paint_flags_ = 0;
832
[email protected]ca4847f2010-09-24 05:39:15833 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56834 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15835 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
836 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27837 // Only update the part of the plugin that actually changed.
838 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]aa4117f2011-12-09 22:19:21839 pending_update_params_->bitmap = dib->id();
840 pending_update_params_->bitmap_rect = optimized_copy_location;
841 pending_update_params_->copy_rects.push_back(optimized_copy_rect);
[email protected]a79d8a632010-11-18 22:35:56842 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46843 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15844 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35845 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
846 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46847 if (!canvas.get()) {
848 NOTREACHED();
849 return;
850 }
[email protected]cef3362f2009-12-21 17:48:45851
[email protected]f98d7e3c2010-09-13 22:30:46852 // We may get back a smaller canvas than we asked for.
853 // TODO(darin): This seems like it could cause painting problems!
854 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
855 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
856 bounds.set_width(canvas->getDevice()->width());
857 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05858
[email protected]f98d7e3c2010-09-13 22:30:46859 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
860
[email protected]aa4117f2011-12-09 22:19:21861 pending_update_params_->bitmap = current_paint_buf_->id();
862 pending_update_params_->bitmap_rect = bounds;
863
864 std::vector<gfx::Rect>& copy_rects = pending_update_params_->copy_rects;
[email protected]f98d7e3c2010-09-13 22:30:46865 // The scroll damage is just another rectangle to paint and copy.
866 copy_rects.swap(update.paint_rects);
867 if (!scroll_damage.IsEmpty())
868 copy_rects.push_back(scroll_damage);
869
870 for (size_t i = 0; i < copy_rects.size(); ++i)
871 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
872 } else { // Accelerated compositing path
873 // Begin painting.
[email protected]aa4117f2011-12-09 22:19:21874 // If painting is done via the gpu process then we don't set any damage
875 // rects to save the browser process from doing unecessary work.
876 pending_update_params_->bitmap_rect = bounds;
877 pending_update_params_->scroll_rect = gfx::Rect();
878 // We don't need an ack, because we're not sharing a DIB with the browser.
879 // If it needs to (e.g. composited UI), the GPU process does its own ACK
880 // with the browser for the GPU surface.
881 pending_update_params_->needs_ack = false;
[email protected]50bd6452010-11-27 19:39:42882 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46883 }
884
[email protected]936c6f52011-12-13 01:35:26885 // If we're holding a pending input event ACK, send the ACK before sending the
886 // UpdateReply message so we can receive another input event before the
887 // UpdateRect_ACK on platforms where the UpdateRect_ACK is sent from within
888 // the UpdateRect IPC message handler.
889 if (pending_input_event_ack_.get())
890 Send(pending_input_event_ack_.release());
891
[email protected]aa4117f2011-12-09 22:19:21892 // If composite() called SwapBuffers, pending_update_params_ will be reset (in
893 // OnSwapBuffersPosted), meaning a message has been added to the
894 // updates_pending_swap_ queue, that will be sent later. Otherwise, we send
895 // the message now.
896 if (pending_update_params_.get()) {
897 // sending an ack to browser process that the paint is complete...
898 update_reply_pending_ = pending_update_params_->needs_ack;
899 Send(new ViewHostMsg_UpdateRect(routing_id_, *pending_update_params_));
900 pending_update_params_.reset();
[email protected]b167ca662010-05-14 00:05:34901 }
[email protected]53d3f302009-12-21 04:42:05902
[email protected]e99ef6f2011-10-16 01:13:00903 UpdateTextInputState();
904 UpdateSelectionBounds();
[email protected]00c39612010-03-06 02:53:28905
906 // Let derived classes know we've painted.
907 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29908}
909
910///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46911// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29912
[email protected]4873c7d2009-07-16 06:36:28913void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57914 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48915 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57916 gfx::Rect damaged_rect = view_rect.Intersect(rect);
917 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29918 return;
919
[email protected]552e6002009-11-19 05:24:57920 paint_aggregator_.InvalidateRect(damaged_rect);
921
922 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24923 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57924 return;
925 if (!paint_aggregator_.HasPendingUpdate())
926 return;
[email protected]aa4117f2011-12-09 22:19:21927 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:24928 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
929 return;
930
931 // When GPU rendering, combine pending animations and invalidations into
932 // a single update.
933 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57934 return;
935
936 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29937 // 1) Ensures that we call WebView::Paint without a bunch of other junk
938 // on the call stack.
939 // 2) Allows us to collect more damage rects before painting to help coalesce
940 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24941 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21942 MessageLoop::current()->PostTask(
943 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:29944}
945
[email protected]4873c7d2009-07-16 06:36:28946void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46947 // Drop scrolls on the floor when we are in compositing mode.
948 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56949 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46950 return;
951
[email protected]552e6002009-11-19 05:24:57952 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48953 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57954 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
955 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29956 return;
957
[email protected]552e6002009-11-19 05:24:57958 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
959
960 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24961 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57962 return;
963 if (!paint_aggregator_.HasPendingUpdate())
964 return;
[email protected]aa4117f2011-12-09 22:19:21965 if (update_reply_pending_ ||
[email protected]65225772011-05-12 21:10:24966 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
967 return;
968
969 // When GPU rendering, combine pending animations and invalidations into
970 // a single update.
971 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57972 return;
973
974 // Perform updating asynchronously. This serves two purposes:
975 // 1) Ensures that we call WebView::Paint without a bunch of other junk
976 // on the call stack.
977 // 2) Allows us to collect more damage rects before painting to help coalesce
978 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24979 invalidation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:21980 MessageLoop::current()->PostTask(
981 FROM_HERE, base::Bind(&RenderWidget::InvalidationCallback, this));
initial.commit09911bf2008-07-26 23:55:29982}
983
[email protected]244ac1892011-12-02 17:04:47984void RenderWidget::didAutoResize(const WebSize& new_size) {
985 size_ = new_size;
986}
987
[email protected]3da12cb2011-10-08 02:25:04988void RenderWidget::didActivateCompositor(int compositor_identifier) {
[email protected]ea162f92011-10-04 23:08:22989 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
990
[email protected]f3150172011-10-22 02:28:45991 CompositorThread* compositor_thread =
992 RenderThreadImpl::current()->compositor_thread();
993 if (compositor_thread)
994 compositor_thread->AddCompositor(routing_id_, compositor_identifier);
[email protected]3da12cb2011-10-08 02:25:04995
[email protected]aa4117f2011-12-09 22:19:21996 if (!is_accelerated_compositing_active_) {
997 // When not in accelerated compositing mode, in certain cases (e.g. waiting
998 // for a resize or if no backing store) the RenderWidgetHost is blocking the
999 // browser's UI thread for some time, waiting for an UpdateRect. If we are
1000 // going to switch to accelerated compositing, the GPU process may need
1001 // round-trips to the browser's UI thread before finishing the frame,
1002 // causing deadlocks if we delay the UpdateRect until we receive the
1003 // OnSwapBuffersComplete. So send a dummy message that will unblock the
1004 // browser's UI thread.
1005 Send(new ViewHostMsg_UpdateIsDelayed(routing_id_));
1006 }
1007
[email protected]ea162f92011-10-04 23:08:221008 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:421009 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:241010 routing_id_, is_accelerated_compositing_active_));
1011
[email protected]c3d45532011-10-07 19:20:401012 // Note: asynchronous swapbuffer support currently only matters if
1013 // compositing scheduling happens on the RenderWidget.
[email protected]ea162f92011-10-04 23:08:221014 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
[email protected]ea162f92011-10-04 23:08:221015}
1016
1017void RenderWidget::didDeactivateCompositor() {
1018 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
1019
1020 is_accelerated_compositing_active_ = false;
1021 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
1022 routing_id_, is_accelerated_compositing_active_));
1023
[email protected]ea162f92011-10-04 23:08:221024 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:241025 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:561026}
1027
[email protected]58264a32011-11-17 23:36:151028void RenderWidget::didCommitAndDrawCompositorFrame() {
[email protected]b5db7eb2011-11-29 09:11:501029 TRACE_EVENT0("gpu", "RenderWidget::didCommitAndDrawCompositorFrame");
1030 // Notify subclasses.
1031 DidFlushPaint();
[email protected]58264a32011-11-17 23:36:151032}
1033
1034void RenderWidget::didCompleteSwapBuffers() {
[email protected]aa4117f2011-12-09 22:19:211035 if (update_reply_pending_)
[email protected]58264a32011-11-17 23:36:151036 return;
1037
1038 if (!next_paint_flags_ && !plugin_window_moves_.size())
1039 return;
1040
1041 ViewHostMsg_UpdateRect_Params params;
1042 params.view_size = size_;
1043 params.resizer_rect = resizer_rect_;
1044 params.plugin_window_moves.swap(plugin_window_moves_);
1045 params.flags = next_paint_flags_;
1046 params.scroll_offset = GetScrollOffset();
[email protected]b0dda9e22011-12-13 20:30:121047 params.needs_ack = false;
[email protected]58264a32011-11-17 23:36:151048
1049 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
1050 next_paint_flags_ = 0;
1051}
1052
[email protected]f98d7e3c2010-09-13 22:30:461053void RenderWidget::scheduleComposite() {
[email protected]c3d45532011-10-07 19:20:401054 if (WebWidgetHandlesCompositorScheduling())
1055 webwidget_->composite(false);
1056 else {
1057 // TODO(nduca): replace with something a little less hacky. The reason this
1058 // hack is still used is because the Invalidate-DoDeferredUpdate loop
1059 // contains a lot of host-renderer synchronization logic that is still
1060 // important for the accelerated compositing case. The option of simply
1061 // duplicating all that code is less desirable than "faking out" the
1062 // invalidation path using a magical damage rect.
1063 didInvalidateRect(WebRect(0, 0, 1, 1));
1064 }
[email protected]f98d7e3c2010-09-13 22:30:461065}
1066
[email protected]5f8b1022011-01-21 23:34:501067void RenderWidget::scheduleAnimation() {
[email protected]921244e42011-07-20 16:36:301068 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ee3d3ad2011-02-04 00:42:211069 if (!animation_update_pending_) {
1070 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:051071 if (!animation_task_posted_) {
1072 animation_task_posted_ = true;
[email protected]32876ae2011-11-15 22:25:211073 MessageLoop::current()->PostTask(
1074 FROM_HERE, base::Bind(&RenderWidget::AnimationCallback, this));
[email protected]52ccd0ea2011-02-16 01:09:051075 }
[email protected]ee3d3ad2011-02-04 00:42:211076 }
[email protected]5f8b1022011-01-21 23:34:501077}
1078
[email protected]4873c7d2009-07-16 06:36:281079void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301080 // TODO(darin): Eliminate this temporary.
1081 WebCursor cursor(cursor_info);
1082
initial.commit09911bf2008-07-26 23:55:291083 // Only send a SetCursor message if we need to make a change.
1084 if (!current_cursor_.IsEqual(cursor)) {
1085 current_cursor_ = cursor;
1086 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
1087 }
1088}
1089
1090// We are supposed to get a single call to Show for a newly created RenderWidget
1091// that was created via RenderWidget::CreateWebView. So, we wait until this
1092// point to dispatch the ShowWidget message.
1093//
1094// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281095// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291096//
[email protected]4873c7d2009-07-16 06:36:281097void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291098 DCHECK(!did_show_) << "received extraneous Show call";
1099 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1100 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1101
[email protected]8de12d942010-11-17 20:42:441102 if (did_show_)
1103 return;
1104
1105 did_show_ = true;
1106 // NOTE: initial_pos_ may still have its default values at this point, but
1107 // that's okay. It'll be ignored if as_popup is false, or the browser
1108 // process will impose a default position otherwise.
1109 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1110 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291111}
1112
[email protected]4873c7d2009-07-16 06:36:281113void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291114}
1115
[email protected]4873c7d2009-07-16 06:36:281116void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291117}
1118
[email protected]2533ce12009-05-09 00:02:241119void RenderWidget::DoDeferredClose() {
1120 Send(new ViewHostMsg_Close(routing_id_));
1121}
1122
[email protected]4873c7d2009-07-16 06:36:281123void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:291124 // If a page calls window.close() twice, we'll end up here twice, but that's
1125 // OK. It is safe to send multiple Close messages.
1126
[email protected]2533ce12009-05-09 00:02:241127 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1128 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1129 // could be closed before the JS finishes executing. So instead, post a
1130 // message back to the message loop, which won't run until the JS is
1131 // complete, and then the Close message can be sent.
[email protected]32876ae2011-11-15 22:25:211132 MessageLoop::current()->PostTask(
1133 FROM_HERE, base::Bind(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291134}
1135
1136void RenderWidget::Close() {
1137 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281138 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291139 webwidget_ = NULL;
1140 }
1141}
1142
[email protected]4873c7d2009-07-16 06:36:281143WebRect RenderWidget::windowRect() {
1144 if (pending_window_rect_count_)
1145 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241146
[email protected]b3f2b912009-04-09 16:18:521147 gfx::Rect rect;
1148 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281149 return rect;
initial.commit09911bf2008-07-26 23:55:291150}
1151
[email protected]8a9d6ca32011-06-06 20:11:301152void RenderWidget::setToolTipText(const WebKit::WebString& text,
1153 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541154 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301155}
1156
[email protected]4873c7d2009-07-16 06:36:281157void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291158 if (did_show_) {
1159 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241160 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291161 } else {
1162 initial_pos_ = pos;
1163 }
1164}
1165
[email protected]2533ce12009-05-09 00:02:241166void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1167 pending_window_rect_ = rect;
1168 pending_window_rect_count_++;
1169}
1170
[email protected]4873c7d2009-07-16 06:36:281171WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241172 if (pending_window_rect_count_) {
1173 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1174 // the RootWindowRect is probably going to return wrong results since the
1175 // browser may not have processed the Move yet. There isn't really anything
1176 // good to do in this case, and it shouldn't happen - since this size is
1177 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281178 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241179 }
1180
[email protected]b3f2b912009-04-09 16:18:521181 gfx::Rect rect;
1182 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281183 return rect;
[email protected]d4547452008-08-28 18:36:371184}
1185
[email protected]4873c7d2009-07-16 06:36:281186WebRect RenderWidget::windowResizerRect() {
1187 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191188}
1189
[email protected]fa7b1dc2010-06-23 17:53:041190void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031191 // To prevent this renderer process from sending unnecessary IPC messages to
1192 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041193 // only during the input method attached to the browser process is active.
1194 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291195}
1196
[email protected]fa7b1dc2010-06-23 17:53:041197void RenderWidget::OnImeSetComposition(
1198 const string16& text,
1199 const std::vector<WebCompositionUnderline>& underlines,
1200 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281201 if (!webwidget_)
1202 return;
[email protected]d4cff272011-05-02 15:46:011203 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041204 text, WebVector<WebCompositionUnderline>(underlines),
1205 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011206 // Setting the IME composition was successful. Send the new composition
1207 // range to the browser.
1208 ui::Range range(ui::Range::InvalidRange());
1209 size_t location, length;
1210 if (webwidget_->compositionRange(&location, &length)) {
1211 range.set_start(location);
1212 range.set_end(location + length);
1213 }
1214 // The IME was cancelled via the Esc key, so just send back the caret.
1215 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1216 range.set_start(location);
1217 range.set_end(location + length);
1218 }
1219 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
1220 } else {
[email protected]fa7b1dc2010-06-23 17:53:041221 // If we failed to set the composition text, then we need to let the browser
1222 // process to cancel the input method's ongoing composition session, to make
1223 // sure we are in a consistent state.
1224 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011225
1226 // Send an updated IME range with just the caret range.
1227 ui::Range range(ui::Range::InvalidRange());
1228 size_t location, length;
1229 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1230 range.set_start(location);
1231 range.set_end(location + length);
1232 }
1233 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:261234 }
[email protected]fa7b1dc2010-06-23 17:53:041235}
1236
[email protected]4de6d1692011-10-12 08:45:441237void RenderWidget::OnImeConfirmComposition(
1238 const string16& text, const ui::Range& replacement_range) {
1239 if (webwidget_) {
[email protected]bb953fd2011-10-31 09:11:571240 handling_input_event_ = true;
[email protected]6b349652011-01-05 18:36:241241 webwidget_->confirmComposition(text);
[email protected]bb953fd2011-10-31 09:11:571242 handling_input_event_ = false;
[email protected]4de6d1692011-10-12 08:45:441243 }
[email protected]d4cff272011-05-02 15:46:011244 // Send an updated IME range with just the caret range.
1245 ui::Range range(ui::Range::InvalidRange());
1246 size_t location, length;
1247 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1248 range.set_start(location);
1249 range.set_end(location + length);
1250 }
1251 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:291252}
1253
[email protected]948f7ab72010-05-28 23:48:081254// This message causes the renderer to render an image of the
1255// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491256void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091257 int tag,
[email protected]948f7ab72010-05-28 23:48:081258 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491259 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001260 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1261 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251262 // Close our unused handle.
1263#if defined(OS_WIN)
1264 ::CloseHandle(dib_handle);
1265#elif defined(OS_MACOSX)
1266 base::SharedMemory::CloseHandle(dib_handle);
1267#endif
1268 }
[email protected]d65adb12010-04-28 17:26:491269 return;
[email protected]45c6aad32010-11-11 04:46:251270 }
[email protected]d65adb12010-04-28 17:26:491271
[email protected]948f7ab72010-05-28 23:48:081272 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491273 // If one of these is empty, then we just return the dib we were
1274 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091275 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491276 return;
1277 }
1278
1279 // Map the given DIB ID into this process, and unmap it at the end
1280 // of this function.
[email protected]45c6aad32010-11-11 04:46:251281 scoped_ptr<TransportDIB> paint_at_size_buffer(
1282 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301283
1284 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491285 float x_scale = static_cast<float>(desired_size.width()) /
1286 static_cast<float>(canvas_size.width());
1287 float y_scale = static_cast<float>(desired_size.height()) /
1288 static_cast<float>(canvas_size.height());
1289
[email protected]ee8d6fd2010-05-26 17:05:481290 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491291 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1292 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481293 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491294
[email protected]36808ad2010-10-20 19:18:301295 scoped_ptr<skia::PlatformCanvas> canvas(
1296 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1297 canvas_size.height()));
1298 if (!canvas.get()) {
1299 NOTREACHED();
1300 return;
1301 }
1302
[email protected]d65adb12010-04-28 17:26:491303 // Reset bounds to what we actually received, but they should be the
1304 // same.
1305 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1306 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1307 bounds.set_width(canvas->getDevice()->width());
1308 bounds.set_height(canvas->getDevice()->height());
1309
1310 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081311 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491312 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1313
[email protected]948f7ab72010-05-28 23:48:081314 // Have to make sure we're laid out at the right size before
1315 // rendering.
1316 gfx::Size old_size = webwidget_->size();
1317 webwidget_->resize(page_size);
1318 webwidget_->layout();
1319
[email protected]d65adb12010-04-28 17:26:491320 // Paint the entire thing (using original bounds, not scaled bounds).
1321 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1322 canvas->restore();
1323
[email protected]948f7ab72010-05-28 23:48:081324 // Return the widget to its previous size.
1325 webwidget_->resize(old_size);
1326
[email protected]c88c9442010-07-19 18:55:091327 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491328}
1329
[email protected]ec7dc112008-08-06 05:30:121330void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1331 // During shutdown we can just ignore this message.
1332 if (!webwidget_)
1333 return;
1334
1335 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561336 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461337 scheduleComposite();
1338 } else {
1339 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1340 didInvalidateRect(repaint_rect);
1341 }
[email protected]ec7dc112008-08-06 05:30:121342}
1343
[email protected]4873c7d2009-07-16 06:36:281344void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111345 if (!webwidget_)
1346 return;
[email protected]4873c7d2009-07-16 06:36:281347 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111348}
1349
[email protected]719b36f2010-12-22 20:36:461350webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151351 const gfx::Rect& paint_bounds,
1352 TransportDIB** dib,
1353 gfx::Rect* location,
1354 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461355 // Bare RenderWidgets don't support optimized plugin painting.
1356 return NULL;
[email protected]ca4847f2010-09-24 05:39:151357}
1358
[email protected]bcaf2272011-02-15 15:29:431359gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521360 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431361 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521362}
1363
[email protected]bee16aab2009-08-26 15:55:031364void RenderWidget::SetHidden(bool hidden) {
1365 if (is_hidden_ == hidden)
1366 return;
1367
1368 // The status has changed. Tell the RenderThread about it.
1369 is_hidden_ = hidden;
1370 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271371 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031372 else
[email protected]380244092011-10-07 17:26:271373 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031374}
1375
[email protected]2b624c562011-10-27 22:58:261376void RenderWidget::WillToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261377 if (!webwidget_)
1378 return;
1379
1380 if (is_fullscreen_) {
1381 webwidget_->willExitFullScreen();
1382 } else {
1383 webwidget_->willEnterFullScreen();
1384 }
[email protected]2b624c562011-10-27 22:58:261385}
1386
1387void RenderWidget::DidToggleFullscreen() {
[email protected]2b624c562011-10-27 22:58:261388 if (!webwidget_)
1389 return;
1390
1391 if (is_fullscreen_) {
1392 webwidget_->didEnterFullScreen();
1393 } else {
1394 webwidget_->didExitFullScreen();
1395 }
[email protected]2b624c562011-10-27 22:58:261396}
1397
[email protected]699ab0d2009-04-23 23:19:141398void RenderWidget::SetBackground(const SkBitmap& background) {
1399 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461400
[email protected]699ab0d2009-04-23 23:19:141401 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281402 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141403}
1404
[email protected]674741932009-02-04 23:44:461405bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051406 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461407}
1408
1409bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051410 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461411}
1412
1413void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051414 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461415}
1416
1417void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051418 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461419}
1420
1421void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051422 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461423}
1424
[email protected]e99ef6f2011-10-16 01:13:001425void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041426 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291427 return;
[email protected]fa7b1dc2010-06-23 17:53:041428
[email protected]ad26ef42011-06-17 07:59:451429 ui::TextInputType new_type = GetTextInputType();
1430 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001431 // Only sends text input type and compose inline to the browser process if
1432 // they are changed.
1433 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451434 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041435 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451436 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001437 Send(new ViewHostMsg_TextInputStateChanged(
1438 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291439 }
initial.commit09911bf2008-07-26 23:55:291440}
1441
[email protected]3f783362011-10-21 22:40:501442void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1443 WebRect start_webrect;
1444 WebRect end_webrect;
1445 webwidget_->selectionBounds(start_webrect, end_webrect);
1446 *start = start_webrect;
1447 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321448}
1449
[email protected]e99ef6f2011-10-16 01:13:001450void RenderWidget::UpdateSelectionBounds() {
1451 if (!webwidget_)
1452 return;
1453
[email protected]3f783362011-10-21 22:40:501454 gfx::Rect start_rect;
1455 gfx::Rect end_rect;
1456 GetSelectionBounds(&start_rect, &end_rect);
[email protected]e99ef6f2011-10-16 01:13:001457 if (selection_start_rect_ == start_rect && selection_end_rect_ == end_rect)
1458 return;
1459
1460 selection_start_rect_ = start_rect;
1461 selection_end_rect_ = end_rect;
1462 Send(new ViewHostMsg_SelectionBoundsChanged(
1463 routing_id_, selection_start_rect_, selection_end_rect_));
1464}
1465
[email protected]73bf95812011-10-12 11:38:321466// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451467COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1468 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1469COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1470 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1471COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1472 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181473COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1474 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1475COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1476 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1477COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1478 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1479COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1480 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1481COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1482 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451483
1484ui::TextInputType RenderWidget::GetTextInputType() {
1485 if (webwidget_) {
1486 int type = webwidget_->textInputType();
1487 // Check the type is in the range representable by ui::TextInputType.
[email protected]caf38ed2011-07-28 13:15:181488 DCHECK(type <= ui::TEXT_INPUT_TYPE_URL) <<
[email protected]ad26ef42011-06-17 07:59:451489 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1490 return static_cast<ui::TextInputType>(type);
1491 }
1492 return ui::TEXT_INPUT_TYPE_NONE;
1493}
1494
1495bool RenderWidget::CanComposeInline() {
1496 return true;
[email protected]56ea1a62011-05-30 07:05:571497}
1498
[email protected]4873c7d2009-07-16 06:36:281499WebScreenInfo RenderWidget::screenInfo() {
1500 WebScreenInfo results;
1501 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1502 return results;
1503}
1504
[email protected]fa7b1dc2010-06-23 17:53:041505void RenderWidget::resetInputMethod() {
1506 if (!input_method_is_active_)
1507 return;
1508
1509 // If the last text input type is not None, then we should finish any
1510 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451511 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041512 // If a composition text exists, then we need to let the browser process
1513 // to cancel the input method's ongoing composition session.
1514 if (webwidget_->confirmComposition())
1515 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1516 }
[email protected]d4cff272011-05-02 15:46:011517
1518 // Send an updated IME range with the current caret rect.
1519 ui::Range range(ui::Range::InvalidRange());
1520 size_t location, length;
1521 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1522 range.set_start(location);
1523 range.set_end(location + length);
1524 }
1525 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041526}
1527
[email protected]f103ab72009-09-02 17:10:591528void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501529 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291530 size_t i = 0;
1531 for (; i < plugin_window_moves_.size(); ++i) {
1532 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581533 if (move.rects_valid) {
1534 plugin_window_moves_[i] = move;
1535 } else {
1536 plugin_window_moves_[i].visible = move.visible;
1537 }
initial.commit09911bf2008-07-26 23:55:291538 break;
1539 }
1540 }
1541
1542 if (i == plugin_window_moves_.size())
1543 plugin_window_moves_.push_back(move);
1544}
[email protected]268654772009-08-06 23:02:041545
1546void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1547 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1548 i != plugin_window_moves_.end(); ++i) {
1549 if (i->window == window) {
1550 plugin_window_moves_.erase(i);
1551 break;
1552 }
1553 }
1554}
[email protected]67bfb83f2011-09-22 03:36:371555
1556bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1557 return false;
1558}
[email protected]c3d45532011-10-07 19:20:401559
1560bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1561 return false;
1562}