blob: 97eac4c5e8d09b846b7759d11399882a2fa10da2 [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]4fb66842009-12-04 21:41:007#include "base/command_line.h"
[email protected]366ae242011-05-10 02:23:588#include "base/debug/trace_event.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/histogram.h"
[email protected]8a9d6ca32011-06-06 20:11:3013#include "base/utf_string_conversions.h"
[email protected]661eb9d2009-02-03 02:11:4814#include "build/build_config.h"
[email protected]992db4c2011-05-12 15:37:1515#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5016#include "content/common/view_messages.h"
[email protected]c08950d22011-10-13 22:20:2917#include "content/public/common/content_switches.h"
[email protected]3da12cb2011-10-08 02:25:0418#include "content/renderer/gpu/compositor_thread.h"
[email protected]8704f89b2011-04-15 00:30:0519#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4420#include "content/renderer/render_thread_impl.h"
[email protected]8d6cba42011-09-02 10:05:1921#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1822#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4823#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5224#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3725#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
[email protected]e99ef6f2011-10-16 01:13:0026#include "third_party/WebKit/Source/WebKit/chromium/public/WebPoint.h"
[email protected]8bd0fe62011-01-17 06:44:3727#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
28#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0129#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3730#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
31#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
32#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
[email protected]8a9d6ca32011-06-06 20:11:3033#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
[email protected]08397d52011-02-05 01:53:3834#include "ui/gfx/point.h"
35#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1236#include "ui/gfx/surface/transport_dib.h"
[email protected]bd37ae252011-06-03 01:28:1837#include "ui/gfx/gl/gl_switches.h"
[email protected]8c89e7792009-08-19 21:18:3438#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5039#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4640#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4841
42#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4943#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5244#include "third_party/skia/include/core/SkPixelRef.h"
45#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4846#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4447
[email protected]8bd0fe62011-01-17 06:44:3748#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2949
[email protected]fa7b1dc2010-06-23 17:53:0450using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3051using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2252using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0653using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2854using WebKit::WebNavigationPolicy;
[email protected]e99ef6f2011-10-16 01:13:0055using WebKit::WebPoint;
[email protected]4873c7d2009-07-16 06:36:2856using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5957using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1858using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0159using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5260using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1961using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5262using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2863using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2464using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0465using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1866using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2767using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2268
[email protected]380244092011-10-07 17:26:2769RenderWidget::RenderWidget(WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2970 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1171 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2972 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73fa2009-10-13 13:43:4273 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3574 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2975 next_paint_flags_(0),
[email protected]872ae5b2011-05-26 20:20:5076 filtered_time_per_frame_(1.0f/60.0f),
[email protected]53d3f302009-12-21 04:42:0577 update_reply_pending_(false),
[email protected]65225772011-05-12 21:10:2478 using_asynchronous_swapbuffers_(false),
79 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2980 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2981 is_hidden_(false),
[email protected]ee41e7d22011-10-14 19:34:0982 is_fullscreen_(false),
initial.commit09911bf2008-07-26 23:55:2983 needs_repainting_on_restore_(false),
84 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4985 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4886 closing_(false),
[email protected]992db4c2011-05-12 15:37:1587 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0488 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:4589 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
90 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:1291 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4892 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4693 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5094 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2195 animation_update_pending_(false),
[email protected]65225772011-05-12 21:10:2496 animation_task_posted_(false),
97 invalidation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2898 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:2799 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:18100 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
101 switches::kDisableGpuVsync);
initial.commit09911bf2008-07-26 23:55:29102}
103
104RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11105 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:35106 if (current_paint_buf_) {
107 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
108 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29109 }
[email protected]992db4c2011-05-12 15:37:15110 // If we are swapped out, we have released already.
111 if (!is_swapped_out_)
112 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29113}
114
[email protected]484955942010-08-19 16:13:18115// static
[email protected]8085dbc82008-09-26 22:53:44116RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]3e2b375b2010-04-07 17:03:12117 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29118 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27119 scoped_refptr<RenderWidget> widget(new RenderWidget(popup_type));
initial.commit09911bf2008-07-26 23:55:29120 widget->Init(opener_id); // adds reference
121 return widget;
122}
123
[email protected]484955942010-08-19 16:13:18124// static
125WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
126 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03127 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18128 break;
129 case WebKit::WebPopupTypeSelect:
130 case WebKit::WebPopupTypeSuggestion:
131 return WebPopupMenu::create(render_widget);
132 default:
133 NOTREACHED();
134 }
135 return NULL;
136}
137
initial.commit09911bf2008-07-26 23:55:29138void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18139 DoInit(opener_id,
140 RenderWidget::CreateWebWidget(this),
141 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
142}
143
[email protected]484955942010-08-19 16:13:18144void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06145 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18146 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29147 DCHECK(!webwidget_);
148
149 if (opener_id != MSG_ROUTING_NONE)
150 opener_id_ = opener_id;
151
[email protected]484955942010-08-19 16:13:18152 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29153
[email protected]380244092011-10-07 17:26:27154 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29155 if (result) {
[email protected]380244092011-10-07 17:26:27156 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29157 // Take a reference on behalf of the RenderThread. This will be balanced
158 // when we receive ViewMsg_Close.
159 AddRef();
160 } else {
161 DCHECK(false);
162 }
163}
164
165// This is used to complete pending inits and non-pending inits. For non-
166// pending cases, the parent will be the same as the current parent. This
167// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55168void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29169 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29170
171 host_window_ = parent_hwnd;
172
[email protected]6de74452009-02-25 18:04:59173 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29174}
175
[email protected]992db4c2011-05-12 15:37:15176void RenderWidget::SetSwappedOut(bool is_swapped_out) {
177 // We should only toggle between states.
178 DCHECK(is_swapped_out_ != is_swapped_out);
179 is_swapped_out_ = is_swapped_out;
180
181 // If we are swapping out, we will call ReleaseProcess, allowing the process
182 // to exit if all of its RenderViews are swapped out. We wait until the
183 // WasSwappedOut call to do this, to avoid showing the sad tab.
184 // If we are swapping in, we call AddRefProcess to prevent the process from
185 // exiting.
186 if (!is_swapped_out)
187 RenderProcess::current()->AddRefProcess();
188}
189
[email protected]a95986a82010-12-24 06:19:28190bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
191 bool handled = true;
192 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
193 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
194 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
195 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
196 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
197 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15198 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28199 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
200 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
201 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
202 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
203 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
204 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
205 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
206 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
207 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
208 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
209 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
210 IPC_MESSAGE_UNHANDLED(handled = false)
211 IPC_END_MESSAGE_MAP()
212 return handled;
213}
initial.commit09911bf2008-07-26 23:55:29214
215bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15216 // Don't send any messages after the browser has told us to close, and filter
217 // most outgoing messages while swapped out.
218 if ((is_swapped_out_ &&
219 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
220 closing_) {
initial.commit09911bf2008-07-26 23:55:29221 delete message;
222 return false;
223 }
224
225 // If given a messsage without a routing ID, then assign our routing ID.
226 if (message->routing_id() == MSG_ROUTING_NONE)
227 message->set_routing_id(routing_id_);
228
[email protected]380244092011-10-07 17:26:27229 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44230}
231
initial.commit09911bf2008-07-26 23:55:29232// Got a response from the browser after the renderer decided to create a new
233// view.
[email protected]c03a6e72011-04-19 21:42:06234void RenderWidget::OnCreatingNewAck(
[email protected]2d7c8552011-06-27 19:21:55235 gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29236 DCHECK(routing_id_ != MSG_ROUTING_NONE);
237
[email protected]2d7c8552011-06-27 19:21:55238 CompleteInit(parent);
initial.commit09911bf2008-07-26 23:55:29239}
240
241void RenderWidget::OnClose() {
242 if (closing_)
243 return;
244 closing_ = true;
245
246 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03247 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27248 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03249 SetHidden(false);
250 }
initial.commit09911bf2008-07-26 23:55:29251
initial.commit09911bf2008-07-26 23:55:29252 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25253 // now. Post a task that only gets invoked when there are no nested message
254 // loops.
255 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
256 NewRunnableMethod(this, &RenderWidget::Close));
257
258 // Balances the AddRef taken when we called AddRoute.
259 Release();
initial.commit09911bf2008-07-26 23:55:29260}
261
[email protected]f21c613a2009-02-12 14:46:17262void RenderWidget::OnResize(const gfx::Size& new_size,
[email protected]ee41e7d22011-10-14 19:34:09263 const gfx::Rect& resizer_rect,
264 bool is_fullscreen) {
initial.commit09911bf2008-07-26 23:55:29265 // During shutdown we can just ignore this message.
266 if (!webwidget_)
267 return;
268
[email protected]e2356242010-11-16 22:33:03269 // We shouldn't be asked to resize to our current size.
270 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
271
[email protected]f21c613a2009-02-12 14:46:17272 // Remember the rect where the resize corner will be drawn.
273 resizer_rect_ = resizer_rect;
274
[email protected]e2356242010-11-16 22:33:03275 if (size_ == new_size)
276 return;
277
initial.commit09911bf2008-07-26 23:55:29278 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03279 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29280 needs_repainting_on_restore_ = false;
281
initial.commit09911bf2008-07-26 23:55:29282 size_ = new_size;
[email protected]ee41e7d22011-10-14 19:34:09283 is_fullscreen_ = is_fullscreen;
initial.commit09911bf2008-07-26 23:55:29284
285 // We should not be sent a Resize message if we have not ACK'd the previous
286 DCHECK(!next_paint_is_resize_ack());
287
[email protected]552e6002009-11-19 05:24:57288 paint_aggregator_.ClearPendingUpdate();
289
initial.commit09911bf2008-07-26 23:55:29290 // When resizing, we want to wait to paint before ACK'ing the resize. This
291 // ensures that we only resize as fast as we can paint. We only need to send
292 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28293 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29294 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56295 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46296 // Resize should have caused an invalidation of the entire view.
297 DCHECK(paint_aggregator_.HasPendingUpdate());
298 }
[email protected]2d5d09d52009-06-15 14:29:21299
initial.commit09911bf2008-07-26 23:55:29300 // We will send the Resize_ACK flag once we paint again.
301 set_next_paint_is_resize_ack();
302 }
303}
304
305void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31306 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29307 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03308 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29309}
310
311void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31312 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29313 // During shutdown we can just ignore this message.
314 if (!webwidget_)
315 return;
316
317 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03318 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29319
320 if (!needs_repainting && !needs_repainting_on_restore_)
321 return;
322 needs_repainting_on_restore_ = false;
323
[email protected]d65adb12010-04-28 17:26:49324 // Tag the next paint as a restore ack, which is picked up by
325 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29326 set_next_paint_is_restore_ack();
327
328 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56329 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46330 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
331 } else {
332 scheduleComposite();
333 }
initial.commit09911bf2008-07-26 23:55:29334}
335
[email protected]992db4c2011-05-12 15:37:15336void RenderWidget::OnWasSwappedOut() {
337 // If we have been swapped out and no one else is using this process,
338 // it's safe to exit now. If we get swapped back in, we will call
339 // AddRefProcess in SetSwappedOut.
340 if (is_swapped_out_)
341 RenderProcess::current()->ReleaseProcess();
342}
343
[email protected]53d3f302009-12-21 04:42:05344void RenderWidget::OnRequestMoveAck() {
345 DCHECK(pending_window_rect_count_);
346 pending_window_rect_count_--;
347}
348
349void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58350 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]53d3f302009-12-21 04:42:05351 DCHECK(update_reply_pending());
352 update_reply_pending_ = false;
353
[email protected]b4d08452010-10-05 17:34:35354 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
355 // have no current paint buffer.
356 if (current_paint_buf_) {
357 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
358 current_paint_buf_ = NULL;
359 }
360
[email protected]65225772011-05-12 21:10:24361 // If swapbuffers is still pending, then defer the update until the
362 // swapbuffers occurs.
363 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
364 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
365 return;
366 }
367
[email protected]00c39612010-03-06 02:53:28368 // Notify subclasses.
369 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25370
initial.commit09911bf2008-07-26 23:55:29371 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24372 DoDeferredUpdateAndSendInputAck();
373}
374
375bool RenderWidget::SupportsAsynchronousSwapBuffers()
376{
377 return false;
378}
379
380void RenderWidget::OnSwapBuffersAborted()
381{
382 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
383 num_swapbuffers_complete_pending_ = 0;
384 using_asynchronous_swapbuffers_ = false;
385 // Schedule another frame so the compositor learns about it.
386 scheduleComposite();
387}
388
[email protected]37a6f302011-07-11 23:43:08389void RenderWidget::OnSwapBuffersPosted() {
390 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
391 if (using_asynchronous_swapbuffers_)
392 num_swapbuffers_complete_pending_++;
393}
394
395void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24396 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
397 // When compositing deactivates, we reset the swapbuffers pending count. The
398 // swapbuffers acks may still arrive, however.
399 if (num_swapbuffers_complete_pending_ == 0) {
400 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
401 return;
402 }
403 num_swapbuffers_complete_pending_--;
404
405 // If update reply is still pending, then defer the update until that reply
406 // occurs.
407 if (update_reply_pending_){
408 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
409 return;
410 }
411
412 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06413 // when we were previously rendering. However, if an invalidation task is not
414 // posted, there may be software rendering work pending. In that case, don't
415 // early out.
416 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24417 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
418 return;
419 }
420
421 // Notify subclasses.
[email protected]50312bf2011-06-22 23:30:06422 if(is_accelerated_compositing_active_)
423 DidFlushPaint();
[email protected]65225772011-05-12 21:10:24424
425 // Continue painting if necessary...
426 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29427}
428
initial.commit09911bf2008-07-26 23:55:29429void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24430 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29431 void* iter = NULL;
432
433 const char* data;
434 int data_length;
[email protected]5dd768212009-08-13 23:34:49435 handling_input_event_ = true;
436 if (!message.ReadData(&iter, &data, &data_length)) {
437 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29438 return;
[email protected]5dd768212009-08-13 23:34:49439 }
initial.commit09911bf2008-07-26 23:55:29440
441 const WebInputEvent* input_event =
442 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48443
444 bool is_keyboard_shortcut = false;
445 // is_keyboard_shortcut flag is only available for RawKeyDown events.
446 if (input_event->type == WebInputEvent::RawKeyDown)
447 message.ReadBool(&iter, &is_keyboard_shortcut);
448
[email protected]67bfb83f2011-09-22 03:36:37449 bool prevent_default = false;
450 if (WebInputEvent::isMouseEventType(input_event->type)) {
451 prevent_default = WillHandleMouseEvent(
452 *(static_cast<const WebMouseEvent*>(input_event)));
453 }
454
455 bool processed = prevent_default;
[email protected]867125a02009-12-10 06:01:48456 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
457 suppress_next_char_events_ = false;
[email protected]67bfb83f2011-09-22 03:36:37458 if (!processed && webwidget_)
[email protected]867125a02009-12-10 06:01:48459 processed = webwidget_->handleInputEvent(*input_event);
460 }
461
462 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
463 // it's not processed by webkit, then we need to suppress the upcoming Char
464 // events.
465 if (!processed && is_keyboard_shortcut)
466 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29467
[email protected]a9fb30aa2011-10-06 06:58:46468 IPC::Message* response =
469 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
470 processed);
[email protected]e2824412009-02-27 01:57:05471
[email protected]353a34c2010-05-28 23:35:17472 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03473 input_event->type == WebInputEvent::MouseWheel ||
474 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57475 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24476 // We want to rate limit the input events in this case, so we'll wait for
477 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17478 if (pending_input_event_ack_.get()) {
479 // As two different kinds of events could cause us to postpone an ack
480 // we send it now, if we have one pending. The Browser should never
481 // send us the same kind of event we are delaying the ack for.
482 Send(pending_input_event_ack_.release());
483 }
[email protected]12fbad812009-09-01 18:21:24484 pending_input_event_ack_.reset(response);
485 } else {
486 Send(response);
487 }
488
[email protected]5dd768212009-08-13 23:34:49489 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48490
[email protected]67bfb83f2011-09-22 03:36:37491 if (!prevent_default) {
492 if (WebInputEvent::isKeyboardEventType(input_event->type))
493 DidHandleKeyEvent();
494 if (WebInputEvent::isMouseEventType(input_event->type))
495 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24496 if (WebInputEvent::isTouchEventType(input_event->type))
497 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37498 }
initial.commit09911bf2008-07-26 23:55:29499}
500
501void RenderWidget::OnMouseCaptureLost() {
502 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28503 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29504}
505
506void RenderWidget::OnSetFocus(bool enable) {
507 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33508 if (webwidget_)
509 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29510}
511
512void RenderWidget::ClearFocus() {
513 // We may have got the focus from the browser before this gets processed, in
514 // which case we do not want to unfocus ourself.
515 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28516 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29517}
518
[email protected]2d5d09d52009-06-15 14:29:21519void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00520 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21521 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06522 TRACE_EVENT2("renderer", "PaintRect",
523 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00524 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21525
526 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00527 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
528 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03529
[email protected]699ab0d2009-04-23 23:19:14530 // If there is a custom background, tile it.
531 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14532 SkPaint paint;
533 SkShader* shader = SkShader::CreateBitmapShader(background_,
534 SkShader::kRepeat_TileMode,
535 SkShader::kRepeat_TileMode);
536 paint.setShader(shader)->unref();
[email protected]fb10ec5b2011-10-24 17:54:20537
538 // Use kSrc_Mode to handle background_ transparency properly.
539 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
540
541 // Canvas could contain multiple update rects. Clip to given rect so that
542 // we don't accidentally clear other update rects.
543 canvas->save();
544 SkRect clip;
545 clip.set(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
546 SkIntToScalar(rect.right()), SkIntToScalar(rect.bottom()));
547 canvas->clipRect(clip);
[email protected]699ab0d2009-04-23 23:19:14548 canvas->drawPaint(paint);
[email protected]fb10ec5b2011-10-24 17:54:20549 canvas->restore();
[email protected]699ab0d2009-04-23 23:19:14550 }
551
[email protected]719b36f2010-12-22 20:36:46552 // First see if this rect is a plugin that can paint itself faster.
553 TransportDIB* optimized_dib = NULL;
554 gfx::Rect optimized_copy_rect, optimized_copy_location;
555 webkit::ppapi::PluginInstance* optimized_instance =
556 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
557 &optimized_copy_location,
558 &optimized_copy_rect);
559 if (optimized_instance) {
560 // This plugin can be optimize-painted and we can just ask it to paint
561 // itself. We don't actually need the TransportDIB in this case.
562 //
563 // This is an optimization for PPAPI plugins that know they're on top of
564 // the page content. If this rect is inside such a plugin, we can save some
565 // time and avoid re-rendering the page content which we know will be
566 // covered by the plugin later (this time can be significant, especially
567 // for a playing movie that is invalidating a lot).
568 //
569 // In the plugin movie case, hopefully the similar call to
570 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
571 // painting, because that avoids copying the plugin image to a different
572 // paint rect. Unfortunately, if anything on the page is animating other
573 // than the movie, it break this optimization since the union of the
574 // invalid regions will be larger than the plugin.
575 //
576 // This code optimizes that case, where we can still avoid painting in
577 // WebKit and filling the background (which can be slow) and just painting
578 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
579 // required.
580 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27581 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46582 } else {
583 // Normal painting case.
584 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
585
586 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35587 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46588 }
initial.commit09911bf2008-07-26 23:55:29589
[email protected]4fb66842009-12-04 21:41:00590 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00591 canvas->restore();
592}
593
594void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
595 skia::PlatformCanvas* canvas) {
596 static bool kPaintBorder =
597 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
598 if (!kPaintBorder)
599 return;
600
[email protected]53d3f302009-12-21 04:42:05601 // Cycle through these colors to help distinguish new paint rects.
602 const SkColor colors[] = {
603 SkColorSetARGB(0x3F, 0xFF, 0, 0),
604 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
605 SkColorSetARGB(0x3F, 0, 0, 0xFF),
606 };
607 static int color_selector = 0;
608
[email protected]4fb66842009-12-04 21:41:00609 SkPaint paint;
610 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05611 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00612 paint.setStrokeWidth(1);
613
614 SkIRect irect;
615 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
616 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29617}
618
[email protected]52ccd0ea2011-02-16 01:09:05619void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30620 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]52ccd0ea2011-02-16 01:09:05621 animation_task_posted_ = false;
[email protected]921244e42011-07-20 16:36:30622 if (!animation_update_pending_) {
623 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59624 return;
[email protected]921244e42011-07-20 16:36:30625 }
[email protected]bd37ae252011-06-03 01:28:18626 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59627 // Record when we fired (according to base::Time::Now()) relative to when
628 // we posted the task to quantify how much the base::Time/base::TimeTicks
629 // skew is affecting animations.
630 base::TimeDelta animation_callback_delay = base::Time::Now() -
631 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
632 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
633 animation_callback_delay,
634 base::TimeDelta::FromMilliseconds(0),
635 base::TimeDelta::FromMilliseconds(30),
636 25);
637 }
[email protected]65225772011-05-12 21:10:24638 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24639}
640
[email protected]52ccd0ea2011-02-16 01:09:05641void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59642 if (!animation_update_pending_)
643 return;
[email protected]bd37ae252011-06-03 01:28:18644
645 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
646 int animationInterval = IsRenderingVSynced() ? 16 : 0;
647
[email protected]7c4329e2011-02-18 22:02:59648 base::Time now = base::Time::Now();
649 if (now >= animation_floor_time_) {
[email protected]921244e42011-07-20 16:36:30650 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]bd37ae252011-06-03 01:28:18651 animation_floor_time_ = now +
652 base::TimeDelta::FromMilliseconds(animationInterval);
653 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59654 // running animation callbacks so that if a callback requests another
655 // we'll be sure to run it at the proper time.
656 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
[email protected]bd37ae252011-06-03 01:28:18657 this, &RenderWidget::AnimationCallback), animationInterval);
[email protected]7c4329e2011-02-18 22:02:59658 animation_task_posted_ = true;
659 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30660 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59661 return;
[email protected]5f8b1022011-01-21 23:34:50662 }
[email protected]bd37ae252011-06-03 01:28:18663 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]7c4329e2011-02-18 22:02:59664 if (animation_task_posted_)
665 return;
666 // This code uses base::Time::Now() to calculate the floor and next fire
667 // time because javascript's Date object uses base::Time::Now(). The
668 // message loop uses base::TimeTicks, which on windows can have a
669 // different granularity than base::Time.
670 // The upshot of all this is that this function might be called before
671 // base::Time::Now() has advanced past the animation_floor_time_. To
672 // avoid exposing this delay to javascript, we keep posting delayed
673 // tasks until base::Time::Now() has advanced far enough.
674 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
675 animation_task_posted_ = true;
676 MessageLoop::current()->PostDelayedTask(FROM_HERE,
677 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50678}
679
[email protected]bd37ae252011-06-03 01:28:18680bool RenderWidget::IsRenderingVSynced() {
681 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
682 // not caught by this check. This will lead to artificially low frame rates
683 // for people who force vsync off at a driver level and expect Chrome to speed
684 // up.
685 return !has_disable_gpu_vsync_switch_;
686}
687
[email protected]65225772011-05-12 21:10:24688void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06689 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24690 invalidation_task_posted_ = false;
691 DoDeferredUpdateAndSendInputAck();
692}
693
694void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05695 DoDeferredUpdate();
696
697 if (pending_input_event_ack_.get())
698 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21699}
700
[email protected]552e6002009-11-19 05:24:57701void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58702 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08703
[email protected]65225772011-05-12 21:10:24704 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29705 return;
[email protected]65225772011-05-12 21:10:24706 if (update_reply_pending()) {
707 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
708 return;
709 }
[email protected]9ca84622011-06-02 23:46:39710 if (is_accelerated_compositing_active_ &&
711 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24712 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
713 return;
714 }
initial.commit09911bf2008-07-26 23:55:29715
[email protected]552e6002009-11-19 05:24:57716 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29717 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57718 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29719 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24720 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29721 return;
722 }
723
[email protected]0fb93f52011-05-18 23:13:56724 // Tracking of frame rate jitter
725 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]52ccd0ea2011-02-16 01:09:05726 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50727
[email protected]f98d7e3c2010-09-13 22:30:46728 // Layout may generate more invalidation. It may also enable the
729 // GPU acceleration, so make sure to run layout before we send the
730 // GpuRenderingActivated message.
731 webwidget_->layout();
732
[email protected]5f8b1022011-01-21 23:34:50733 // Suppress painting if nothing is dirty. This has to be done after updating
734 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24735 if (!paint_aggregator_.HasPendingUpdate()) {
736 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]5f8b1022011-01-21 23:34:50737 return;
[email protected]65225772011-05-12 21:10:24738 }
[email protected]5f8b1022011-01-21 23:34:50739
[email protected]872ae5b2011-05-26 20:20:50740 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56741 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
742 if(is_accelerated_compositing_active_)
743 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
744 delay,
745 base::TimeDelta::FromMilliseconds(1),
746 base::TimeDelta::FromMilliseconds(60),
747 30);
748 else
749 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
750 delay,
751 base::TimeDelta::FromMilliseconds(1),
752 base::TimeDelta::FromMilliseconds(60),
753 30);
[email protected]872ae5b2011-05-26 20:20:50754
755 // Calculate filtered time per frame:
756 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
757 filtered_time_per_frame_ =
758 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56759 }
760 last_do_deferred_update_time_ = frame_begin_ticks;
761
[email protected]552e6002009-11-19 05:24:57762 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29763 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30764 PaintAggregator::PendingUpdate update;
765 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29766
[email protected]53d3f302009-12-21 04:42:05767 gfx::Rect scroll_damage = update.GetScrollDamage();
768 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29769
[email protected]27543452011-03-25 00:14:00770 // Compositing the page may disable accelerated compositing.
771 bool accelerated_compositing_was_active = is_accelerated_compositing_active_;
772
[email protected]ca4847f2010-09-24 05:39:15773 // A plugin may be able to do an optimized paint. First check this, in which
774 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46775 // This optimization allows PPAPI plugins that declare themselves on top of
776 // the page (like a traditional windowed plugin) to be able to animate (think
777 // movie playing) without repeatedly re-painting the page underneath, or
778 // copying the plugin backing store (since we can send the plugin's backing
779 // store directly to the browser).
780 //
781 // This optimization only works when the entire invalid region is contained
782 // within the plugin. There is a related optimization in PaintRect for the
783 // case where there may be multiple invalid regions.
[email protected]2650bd52011-03-25 00:34:44784 TransportDIB::Id dib_id = TransportDIB::Id();
[email protected]ca4847f2010-09-24 05:39:15785 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45786 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15787 gfx::Rect optimized_copy_rect, optimized_copy_location;
788 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56789 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15790 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
791 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27792 // Only update the part of the plugin that actually changed.
793 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15794 bounds = optimized_copy_location;
795 copy_rects.push_back(optimized_copy_rect);
796 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56797 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46798 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15799 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35800 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
801 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46802 if (!canvas.get()) {
803 NOTREACHED();
804 return;
805 }
[email protected]cef3362f2009-12-21 17:48:45806
[email protected]f98d7e3c2010-09-13 22:30:46807 // We may get back a smaller canvas than we asked for.
808 // TODO(darin): This seems like it could cause painting problems!
809 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
810 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
811 bounds.set_width(canvas->getDevice()->width());
812 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05813
[email protected]f98d7e3c2010-09-13 22:30:46814 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
815
[email protected]f98d7e3c2010-09-13 22:30:46816 // The scroll damage is just another rectangle to paint and copy.
817 copy_rects.swap(update.paint_rects);
818 if (!scroll_damage.IsEmpty())
819 copy_rects.push_back(scroll_damage);
820
821 for (size_t i = 0; i < copy_rects.size(); ++i)
822 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15823
[email protected]b4d08452010-10-05 17:34:35824 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46825 } else { // Accelerated compositing path
826 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42827 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46828 }
829
830 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05831 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30832 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05833 params.bitmap_rect = bounds;
834 params.dx = update.scroll_delta.x();
835 params.dy = update.scroll_delta.y();
[email protected]27543452011-03-25 00:14:00836 if (accelerated_compositing_was_active) {
[email protected]b167ca662010-05-14 00:05:34837 // If painting is done via the gpu process then we clear out all damage
838 // rects to save the browser process from doing unecessary work.
839 params.scroll_rect = gfx::Rect();
840 params.copy_rects.clear();
841 } else {
842 params.scroll_rect = update.scroll_rect;
843 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
844 }
[email protected]53d3f302009-12-21 04:42:05845 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03846 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05847 params.plugin_window_moves.swap(plugin_window_moves_);
848 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52849 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05850
851 update_reply_pending_ = true;
852 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
853 next_paint_flags_ = 0;
854
[email protected]e99ef6f2011-10-16 01:13:00855 UpdateTextInputState();
856 UpdateSelectionBounds();
[email protected]00c39612010-03-06 02:53:28857
858 // Let derived classes know we've painted.
859 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29860}
861
862///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46863// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29864
[email protected]4873c7d2009-07-16 06:36:28865void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57866 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48867 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57868 gfx::Rect damaged_rect = view_rect.Intersect(rect);
869 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29870 return;
871
[email protected]552e6002009-11-19 05:24:57872 paint_aggregator_.InvalidateRect(damaged_rect);
873
874 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24875 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57876 return;
877 if (!paint_aggregator_.HasPendingUpdate())
878 return;
[email protected]65225772011-05-12 21:10:24879 if (update_reply_pending() ||
880 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
881 return;
882
883 // When GPU rendering, combine pending animations and invalidations into
884 // a single update.
885 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57886 return;
887
888 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29889 // 1) Ensures that we call WebView::Paint without a bunch of other junk
890 // on the call stack.
891 // 2) Allows us to collect more damage rects before painting to help coalesce
892 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24893 invalidation_task_posted_ = true;
initial.commit09911bf2008-07-26 23:55:29894 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]65225772011-05-12 21:10:24895 this, &RenderWidget::InvalidationCallback));
initial.commit09911bf2008-07-26 23:55:29896}
897
[email protected]4873c7d2009-07-16 06:36:28898void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46899 // Drop scrolls on the floor when we are in compositing mode.
900 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56901 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46902 return;
903
[email protected]552e6002009-11-19 05:24:57904 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48905 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57906 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
907 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29908 return;
909
[email protected]552e6002009-11-19 05:24:57910 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
911
912 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24913 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57914 return;
915 if (!paint_aggregator_.HasPendingUpdate())
916 return;
[email protected]65225772011-05-12 21:10:24917 if (update_reply_pending() ||
918 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
919 return;
920
921 // When GPU rendering, combine pending animations and invalidations into
922 // a single update.
923 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57924 return;
925
926 // Perform updating asynchronously. This serves two purposes:
927 // 1) Ensures that we call WebView::Paint without a bunch of other junk
928 // on the call stack.
929 // 2) Allows us to collect more damage rects before painting to help coalesce
930 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24931 invalidation_task_posted_ = true;
initial.commit09911bf2008-07-26 23:55:29932 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]65225772011-05-12 21:10:24933 this, &RenderWidget::InvalidationCallback));
initial.commit09911bf2008-07-26 23:55:29934}
935
[email protected]3da12cb2011-10-08 02:25:04936void RenderWidget::didActivateCompositor(int compositor_identifier) {
[email protected]ea162f92011-10-04 23:08:22937 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
938
[email protected]f3150172011-10-22 02:28:45939 CompositorThread* compositor_thread =
940 RenderThreadImpl::current()->compositor_thread();
941 if (compositor_thread)
942 compositor_thread->AddCompositor(routing_id_, compositor_identifier);
[email protected]3da12cb2011-10-08 02:25:04943
[email protected]ea162f92011-10-04 23:08:22944 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:42945 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:24946 routing_id_, is_accelerated_compositing_active_));
947
[email protected]c3d45532011-10-07 19:20:40948 // Note: asynchronous swapbuffer support currently only matters if
949 // compositing scheduling happens on the RenderWidget.
[email protected]ea162f92011-10-04 23:08:22950 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
[email protected]ea162f92011-10-04 23:08:22951}
952
953void RenderWidget::didDeactivateCompositor() {
954 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
955
956 is_accelerated_compositing_active_ = false;
957 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
958 routing_id_, is_accelerated_compositing_active_));
959
[email protected]ea162f92011-10-04 23:08:22960 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:24961 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:56962}
963
[email protected]f98d7e3c2010-09-13 22:30:46964void RenderWidget::scheduleComposite() {
[email protected]c3d45532011-10-07 19:20:40965 if (WebWidgetHandlesCompositorScheduling())
966 webwidget_->composite(false);
967 else {
968 // TODO(nduca): replace with something a little less hacky. The reason this
969 // hack is still used is because the Invalidate-DoDeferredUpdate loop
970 // contains a lot of host-renderer synchronization logic that is still
971 // important for the accelerated compositing case. The option of simply
972 // duplicating all that code is less desirable than "faking out" the
973 // invalidation path using a magical damage rect.
974 didInvalidateRect(WebRect(0, 0, 1, 1));
975 }
[email protected]f98d7e3c2010-09-13 22:30:46976}
977
[email protected]5f8b1022011-01-21 23:34:50978void RenderWidget::scheduleAnimation() {
[email protected]921244e42011-07-20 16:36:30979 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ee3d3ad2011-02-04 00:42:21980 if (!animation_update_pending_) {
981 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05982 if (!animation_task_posted_) {
983 animation_task_posted_ = true;
984 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
985 this, &RenderWidget::AnimationCallback));
986 }
[email protected]ee3d3ad2011-02-04 00:42:21987 }
[email protected]5f8b1022011-01-21 23:34:50988}
989
[email protected]4873c7d2009-07-16 06:36:28990void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30991 // TODO(darin): Eliminate this temporary.
992 WebCursor cursor(cursor_info);
993
initial.commit09911bf2008-07-26 23:55:29994 // Only send a SetCursor message if we need to make a change.
995 if (!current_cursor_.IsEqual(cursor)) {
996 current_cursor_ = cursor;
997 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
998 }
999}
1000
1001// We are supposed to get a single call to Show for a newly created RenderWidget
1002// that was created via RenderWidget::CreateWebView. So, we wait until this
1003// point to dispatch the ShowWidget message.
1004//
1005// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281006// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291007//
[email protected]4873c7d2009-07-16 06:36:281008void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:291009 DCHECK(!did_show_) << "received extraneous Show call";
1010 DCHECK(routing_id_ != MSG_ROUTING_NONE);
1011 DCHECK(opener_id_ != MSG_ROUTING_NONE);
1012
[email protected]8de12d942010-11-17 20:42:441013 if (did_show_)
1014 return;
1015
1016 did_show_ = true;
1017 // NOTE: initial_pos_ may still have its default values at this point, but
1018 // that's okay. It'll be ignored if as_popup is false, or the browser
1019 // process will impose a default position otherwise.
1020 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1021 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291022}
1023
[email protected]4873c7d2009-07-16 06:36:281024void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291025}
1026
[email protected]4873c7d2009-07-16 06:36:281027void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291028}
1029
[email protected]2533ce12009-05-09 00:02:241030void RenderWidget::DoDeferredClose() {
1031 Send(new ViewHostMsg_Close(routing_id_));
1032}
1033
[email protected]4873c7d2009-07-16 06:36:281034void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:291035 // If a page calls window.close() twice, we'll end up here twice, but that's
1036 // OK. It is safe to send multiple Close messages.
1037
[email protected]2533ce12009-05-09 00:02:241038 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1039 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1040 // could be closed before the JS finishes executing. So instead, post a
1041 // message back to the message loop, which won't run until the JS is
1042 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:151043 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:241044 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:291045}
1046
1047void RenderWidget::Close() {
1048 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281049 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291050 webwidget_ = NULL;
1051 }
1052}
1053
[email protected]4873c7d2009-07-16 06:36:281054WebRect RenderWidget::windowRect() {
1055 if (pending_window_rect_count_)
1056 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241057
[email protected]b3f2b912009-04-09 16:18:521058 gfx::Rect rect;
1059 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281060 return rect;
initial.commit09911bf2008-07-26 23:55:291061}
1062
[email protected]8a9d6ca32011-06-06 20:11:301063void RenderWidget::setToolTipText(const WebKit::WebString& text,
1064 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541065 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301066}
1067
[email protected]4873c7d2009-07-16 06:36:281068void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291069 if (did_show_) {
1070 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241071 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291072 } else {
1073 initial_pos_ = pos;
1074 }
1075}
1076
[email protected]2533ce12009-05-09 00:02:241077void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1078 pending_window_rect_ = rect;
1079 pending_window_rect_count_++;
1080}
1081
[email protected]4873c7d2009-07-16 06:36:281082WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241083 if (pending_window_rect_count_) {
1084 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1085 // the RootWindowRect is probably going to return wrong results since the
1086 // browser may not have processed the Move yet. There isn't really anything
1087 // good to do in this case, and it shouldn't happen - since this size is
1088 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281089 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241090 }
1091
[email protected]b3f2b912009-04-09 16:18:521092 gfx::Rect rect;
1093 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281094 return rect;
[email protected]d4547452008-08-28 18:36:371095}
1096
[email protected]4873c7d2009-07-16 06:36:281097WebRect RenderWidget::windowResizerRect() {
1098 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191099}
1100
[email protected]fa7b1dc2010-06-23 17:53:041101void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031102 // To prevent this renderer process from sending unnecessary IPC messages to
1103 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041104 // only during the input method attached to the browser process is active.
1105 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291106}
1107
[email protected]fa7b1dc2010-06-23 17:53:041108void RenderWidget::OnImeSetComposition(
1109 const string16& text,
1110 const std::vector<WebCompositionUnderline>& underlines,
1111 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281112 if (!webwidget_)
1113 return;
[email protected]d4cff272011-05-02 15:46:011114 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041115 text, WebVector<WebCompositionUnderline>(underlines),
1116 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011117 // Setting the IME composition was successful. Send the new composition
1118 // range to the browser.
1119 ui::Range range(ui::Range::InvalidRange());
1120 size_t location, length;
1121 if (webwidget_->compositionRange(&location, &length)) {
1122 range.set_start(location);
1123 range.set_end(location + length);
1124 }
1125 // The IME was cancelled via the Esc key, so just send back the caret.
1126 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1127 range.set_start(location);
1128 range.set_end(location + length);
1129 }
1130 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
1131 } else {
[email protected]fa7b1dc2010-06-23 17:53:041132 // If we failed to set the composition text, then we need to let the browser
1133 // process to cancel the input method's ongoing composition session, to make
1134 // sure we are in a consistent state.
1135 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011136
1137 // Send an updated IME range with just the caret range.
1138 ui::Range range(ui::Range::InvalidRange());
1139 size_t location, length;
1140 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1141 range.set_start(location);
1142 range.set_end(location + length);
1143 }
1144 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:261145 }
[email protected]fa7b1dc2010-06-23 17:53:041146}
1147
[email protected]4de6d1692011-10-12 08:45:441148void RenderWidget::OnImeConfirmComposition(
1149 const string16& text, const ui::Range& replacement_range) {
1150 if (webwidget_) {
[email protected]6b349652011-01-05 18:36:241151 webwidget_->confirmComposition(text);
[email protected]4de6d1692011-10-12 08:45:441152 }
[email protected]d4cff272011-05-02 15:46:011153 // Send an updated IME range with just the caret range.
1154 ui::Range range(ui::Range::InvalidRange());
1155 size_t location, length;
1156 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1157 range.set_start(location);
1158 range.set_end(location + length);
1159 }
1160 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:291161}
1162
[email protected]948f7ab72010-05-28 23:48:081163// This message causes the renderer to render an image of the
1164// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491165void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091166 int tag,
[email protected]948f7ab72010-05-28 23:48:081167 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491168 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001169 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1170 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251171 // Close our unused handle.
1172#if defined(OS_WIN)
1173 ::CloseHandle(dib_handle);
1174#elif defined(OS_MACOSX)
1175 base::SharedMemory::CloseHandle(dib_handle);
1176#endif
1177 }
[email protected]d65adb12010-04-28 17:26:491178 return;
[email protected]45c6aad32010-11-11 04:46:251179 }
[email protected]d65adb12010-04-28 17:26:491180
[email protected]948f7ab72010-05-28 23:48:081181 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491182 // If one of these is empty, then we just return the dib we were
1183 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091184 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491185 return;
1186 }
1187
1188 // Map the given DIB ID into this process, and unmap it at the end
1189 // of this function.
[email protected]45c6aad32010-11-11 04:46:251190 scoped_ptr<TransportDIB> paint_at_size_buffer(
1191 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301192
1193 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491194 float x_scale = static_cast<float>(desired_size.width()) /
1195 static_cast<float>(canvas_size.width());
1196 float y_scale = static_cast<float>(desired_size.height()) /
1197 static_cast<float>(canvas_size.height());
1198
[email protected]ee8d6fd2010-05-26 17:05:481199 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491200 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1201 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481202 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491203
[email protected]36808ad2010-10-20 19:18:301204 scoped_ptr<skia::PlatformCanvas> canvas(
1205 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1206 canvas_size.height()));
1207 if (!canvas.get()) {
1208 NOTREACHED();
1209 return;
1210 }
1211
[email protected]d65adb12010-04-28 17:26:491212 // Reset bounds to what we actually received, but they should be the
1213 // same.
1214 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1215 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1216 bounds.set_width(canvas->getDevice()->width());
1217 bounds.set_height(canvas->getDevice()->height());
1218
1219 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081220 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491221 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1222
[email protected]948f7ab72010-05-28 23:48:081223 // Have to make sure we're laid out at the right size before
1224 // rendering.
1225 gfx::Size old_size = webwidget_->size();
1226 webwidget_->resize(page_size);
1227 webwidget_->layout();
1228
[email protected]d65adb12010-04-28 17:26:491229 // Paint the entire thing (using original bounds, not scaled bounds).
1230 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1231 canvas->restore();
1232
[email protected]948f7ab72010-05-28 23:48:081233 // Return the widget to its previous size.
1234 webwidget_->resize(old_size);
1235
[email protected]c88c9442010-07-19 18:55:091236 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491237}
1238
[email protected]ec7dc112008-08-06 05:30:121239void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1240 // During shutdown we can just ignore this message.
1241 if (!webwidget_)
1242 return;
1243
1244 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561245 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461246 scheduleComposite();
1247 } else {
1248 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1249 didInvalidateRect(repaint_rect);
1250 }
[email protected]ec7dc112008-08-06 05:30:121251}
1252
[email protected]4873c7d2009-07-16 06:36:281253void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111254 if (!webwidget_)
1255 return;
[email protected]4873c7d2009-07-16 06:36:281256 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111257}
1258
[email protected]719b36f2010-12-22 20:36:461259webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151260 const gfx::Rect& paint_bounds,
1261 TransportDIB** dib,
1262 gfx::Rect* location,
1263 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461264 // Bare RenderWidgets don't support optimized plugin painting.
1265 return NULL;
[email protected]ca4847f2010-09-24 05:39:151266}
1267
[email protected]bcaf2272011-02-15 15:29:431268gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521269 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431270 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521271}
1272
[email protected]bee16aab2009-08-26 15:55:031273void RenderWidget::SetHidden(bool hidden) {
1274 if (is_hidden_ == hidden)
1275 return;
1276
1277 // The status has changed. Tell the RenderThread about it.
1278 is_hidden_ = hidden;
1279 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271280 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031281 else
[email protected]380244092011-10-07 17:26:271282 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031283}
1284
[email protected]699ab0d2009-04-23 23:19:141285void RenderWidget::SetBackground(const SkBitmap& background) {
1286 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461287
[email protected]699ab0d2009-04-23 23:19:141288 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281289 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141290}
1291
[email protected]674741932009-02-04 23:44:461292bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051293 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461294}
1295
1296bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051297 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461298}
1299
1300void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051301 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461302}
1303
1304void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051305 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461306}
1307
1308void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051309 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461310}
1311
[email protected]e99ef6f2011-10-16 01:13:001312void RenderWidget::UpdateTextInputState() {
[email protected]fa7b1dc2010-06-23 17:53:041313 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291314 return;
[email protected]fa7b1dc2010-06-23 17:53:041315
[email protected]ad26ef42011-06-17 07:59:451316 ui::TextInputType new_type = GetTextInputType();
1317 bool new_can_compose_inline = CanComposeInline();
[email protected]e99ef6f2011-10-16 01:13:001318 // Only sends text input type and compose inline to the browser process if
1319 // they are changed.
1320 if (text_input_type_ != new_type ||
[email protected]ad26ef42011-06-17 07:59:451321 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041322 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451323 can_compose_inline_ = new_can_compose_inline;
[email protected]e99ef6f2011-10-16 01:13:001324 Send(new ViewHostMsg_TextInputStateChanged(
1325 routing_id(), new_type, new_can_compose_inline));
initial.commit09911bf2008-07-26 23:55:291326 }
initial.commit09911bf2008-07-26 23:55:291327}
1328
[email protected]3f783362011-10-21 22:40:501329void RenderWidget::GetSelectionBounds(gfx::Rect* start, gfx::Rect* end) {
1330 WebRect start_webrect;
1331 WebRect end_webrect;
1332 webwidget_->selectionBounds(start_webrect, end_webrect);
1333 *start = start_webrect;
1334 *end = end_webrect;
[email protected]73bf95812011-10-12 11:38:321335}
1336
[email protected]e99ef6f2011-10-16 01:13:001337void RenderWidget::UpdateSelectionBounds() {
1338 if (!webwidget_)
1339 return;
1340
[email protected]3f783362011-10-21 22:40:501341 gfx::Rect start_rect;
1342 gfx::Rect end_rect;
1343 GetSelectionBounds(&start_rect, &end_rect);
[email protected]e99ef6f2011-10-16 01:13:001344 if (selection_start_rect_ == start_rect && selection_end_rect_ == end_rect)
1345 return;
1346
1347 selection_start_rect_ = start_rect;
1348 selection_end_rect_ = end_rect;
1349 Send(new ViewHostMsg_SelectionBoundsChanged(
1350 routing_id_, selection_start_rect_, selection_end_rect_));
1351}
1352
[email protected]73bf95812011-10-12 11:38:321353// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451354COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1355 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1356COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1357 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1358COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1359 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181360COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1361 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1362COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1363 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1364COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1365 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1366COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1367 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1368COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1369 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451370
1371ui::TextInputType RenderWidget::GetTextInputType() {
1372 if (webwidget_) {
1373 int type = webwidget_->textInputType();
1374 // Check the type is in the range representable by ui::TextInputType.
[email protected]caf38ed2011-07-28 13:15:181375 DCHECK(type <= ui::TEXT_INPUT_TYPE_URL) <<
[email protected]ad26ef42011-06-17 07:59:451376 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1377 return static_cast<ui::TextInputType>(type);
1378 }
1379 return ui::TEXT_INPUT_TYPE_NONE;
1380}
1381
1382bool RenderWidget::CanComposeInline() {
1383 return true;
[email protected]56ea1a62011-05-30 07:05:571384}
1385
[email protected]4873c7d2009-07-16 06:36:281386WebScreenInfo RenderWidget::screenInfo() {
1387 WebScreenInfo results;
1388 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1389 return results;
1390}
1391
[email protected]fa7b1dc2010-06-23 17:53:041392void RenderWidget::resetInputMethod() {
1393 if (!input_method_is_active_)
1394 return;
1395
1396 // If the last text input type is not None, then we should finish any
1397 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451398 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041399 // If a composition text exists, then we need to let the browser process
1400 // to cancel the input method's ongoing composition session.
1401 if (webwidget_->confirmComposition())
1402 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1403 }
[email protected]d4cff272011-05-02 15:46:011404
1405 // Send an updated IME range with the current caret rect.
1406 ui::Range range(ui::Range::InvalidRange());
1407 size_t location, length;
1408 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1409 range.set_start(location);
1410 range.set_end(location + length);
1411 }
1412 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041413}
1414
[email protected]f103ab72009-09-02 17:10:591415void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501416 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291417 size_t i = 0;
1418 for (; i < plugin_window_moves_.size(); ++i) {
1419 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581420 if (move.rects_valid) {
1421 plugin_window_moves_[i] = move;
1422 } else {
1423 plugin_window_moves_[i].visible = move.visible;
1424 }
initial.commit09911bf2008-07-26 23:55:291425 break;
1426 }
1427 }
1428
1429 if (i == plugin_window_moves_.size())
1430 plugin_window_moves_.push_back(move);
1431}
[email protected]268654772009-08-06 23:02:041432
1433void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1434 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1435 i != plugin_window_moves_.end(); ++i) {
1436 if (i->window == window) {
1437 plugin_window_moves_.erase(i);
1438 break;
1439 }
1440 }
1441}
[email protected]67bfb83f2011-09-22 03:36:371442
1443bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1444 return false;
1445}
[email protected]c3d45532011-10-07 19:20:401446
1447bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1448 return false;
1449}