blob: 3f02e07e118564a9c8e9c5820f7a1568d4c43ae3 [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]38b592902011-04-16 02:08:4215#include "content/common/content_switches.h"
[email protected]992db4c2011-05-12 15:37:1516#include "content/common/swapped_out_messages.h"
[email protected]778574e2011-03-21 22:03:5017#include "content/common/view_messages.h"
[email protected]8704f89b2011-04-15 00:30:0518#include "content/renderer/render_process.h"
[email protected]10e6ab572011-04-14 23:42:0019#include "content/renderer/render_thread.h"
[email protected]8d6cba42011-09-02 10:05:1920#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]484955942010-08-19 16:13:1821#include "ipc/ipc_sync_message.h"
[email protected]661eb9d2009-02-03 02:11:4822#include "skia/ext/platform_canvas.h"
[email protected]d5282e72009-05-13 13:16:5223#include "third_party/skia/include/core/SkShader.h"
[email protected]8bd0fe62011-01-17 06:44:3724#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
25#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
26#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0127#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3728#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
29#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
30#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
[email protected]8a9d6ca32011-06-06 20:11:3031#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
[email protected]08397d52011-02-05 01:53:3832#include "ui/gfx/point.h"
33#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1234#include "ui/gfx/surface/transport_dib.h"
[email protected]bd37ae252011-06-03 01:28:1835#include "ui/gfx/gl/gl_switches.h"
[email protected]8c89e7792009-08-19 21:18:3436#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5037#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4638#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4839
40#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4941#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5242#include "third_party/skia/include/core/SkPixelRef.h"
43#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4844#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4445
[email protected]8bd0fe62011-01-17 06:44:3746#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2947
[email protected]fa7b1dc2010-06-23 17:53:0448using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3049using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2250using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0651using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2852using WebKit::WebNavigationPolicy;
53using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5954using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1855using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0156using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5257using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1958using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5259using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2860using WebKit::WebTextDirection;
[email protected]fa7b1dc2010-06-23 17:53:0461using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1862using WebKit::WebWidget;
[email protected]62cb33cae2009-03-27 23:30:2263
[email protected]3e2b375b2010-04-07 17:03:1264RenderWidget::RenderWidget(RenderThreadBase* render_thread,
65 WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2966 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1167 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2968 opener_id_(MSG_ROUTING_NONE),
[email protected]8085dbc82008-09-26 22:53:4469 render_thread_(render_thread),
[email protected]659f73f2009-10-13 13:43:4270 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3571 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2972 next_paint_flags_(0),
[email protected]872ae5b2011-05-26 20:20:5073 filtered_time_per_frame_(1.0f/60.0f),
[email protected]53d3f302009-12-21 04:42:0574 update_reply_pending_(false),
[email protected]65225772011-05-12 21:10:2475 using_asynchronous_swapbuffers_(false),
76 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2977 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2978 is_hidden_(false),
79 needs_repainting_on_restore_(false),
80 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4981 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4882 closing_(false),
[email protected]992db4c2011-05-12 15:37:1583 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0484 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:4585 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
86 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:1287 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4888 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4689 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5090 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2191 animation_update_pending_(false),
[email protected]65225772011-05-12 21:10:2492 animation_task_posted_(false),
93 invalidation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2894 RenderProcess::current()->AddRefProcess();
[email protected]8085dbc82008-09-26 22:53:4495 DCHECK(render_thread_);
[email protected]bd37ae252011-06-03 01:28:1896 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
97 switches::kDisableGpuVsync);
initial.commit09911bf2008-07-26 23:55:2998}
99
100RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11101 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:35102 if (current_paint_buf_) {
103 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
104 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29105 }
[email protected]992db4c2011-05-12 15:37:15106 // If we are swapped out, we have released already.
107 if (!is_swapped_out_)
108 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29109}
110
[email protected]484955942010-08-19 16:13:18111// static
[email protected]8085dbc82008-09-26 22:53:44112RenderWidget* RenderWidget::Create(int32 opener_id,
[email protected]0ebf3872008-11-07 21:35:03113 RenderThreadBase* render_thread,
[email protected]3e2b375b2010-04-07 17:03:12114 WebKit::WebPopupType popup_type) {
initial.commit09911bf2008-07-26 23:55:29115 DCHECK(opener_id != MSG_ROUTING_NONE);
[email protected]ad8e04a2010-11-01 04:16:27116 scoped_refptr<RenderWidget> widget(new RenderWidget(render_thread,
117 popup_type));
initial.commit09911bf2008-07-26 23:55:29118 widget->Init(opener_id); // adds reference
119 return widget;
120}
121
[email protected]484955942010-08-19 16:13:18122// static
123WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
124 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03125 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18126 break;
127 case WebKit::WebPopupTypeSelect:
128 case WebKit::WebPopupTypeSuggestion:
129 return WebPopupMenu::create(render_widget);
130 default:
131 NOTREACHED();
132 }
133 return NULL;
134}
135
initial.commit09911bf2008-07-26 23:55:29136void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18137 DoInit(opener_id,
138 RenderWidget::CreateWebWidget(this),
139 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
140}
141
[email protected]484955942010-08-19 16:13:18142void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06143 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18144 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29145 DCHECK(!webwidget_);
146
147 if (opener_id != MSG_ROUTING_NONE)
148 opener_id_ = opener_id;
149
[email protected]484955942010-08-19 16:13:18150 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29151
[email protected]484955942010-08-19 16:13:18152 bool result = render_thread_->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29153 if (result) {
[email protected]8085dbc82008-09-26 22:53:44154 render_thread_->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29155 // Take a reference on behalf of the RenderThread. This will be balanced
156 // when we receive ViewMsg_Close.
157 AddRef();
158 } else {
159 DCHECK(false);
160 }
161}
162
163// This is used to complete pending inits and non-pending inits. For non-
164// pending cases, the parent will be the same as the current parent. This
165// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55166void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29167 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29168
169 host_window_ = parent_hwnd;
initial.commit09911bf2008-07-26 23:55:29170
[email protected]6de74452009-02-25 18:04:59171 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29172}
173
[email protected]992db4c2011-05-12 15:37:15174void RenderWidget::SetSwappedOut(bool is_swapped_out) {
175 // We should only toggle between states.
176 DCHECK(is_swapped_out_ != is_swapped_out);
177 is_swapped_out_ = is_swapped_out;
178
179 // If we are swapping out, we will call ReleaseProcess, allowing the process
180 // to exit if all of its RenderViews are swapped out. We wait until the
181 // WasSwappedOut call to do this, to avoid showing the sad tab.
182 // If we are swapping in, we call AddRefProcess to prevent the process from
183 // exiting.
184 if (!is_swapped_out)
185 RenderProcess::current()->AddRefProcess();
186}
187
[email protected]a95986a82010-12-24 06:19:28188bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
189 bool handled = true;
190 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
191 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
192 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
193 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
194 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
195 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15196 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28197 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
198 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
199 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
200 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
201 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
202 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
203 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
204 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
205 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
206 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
207 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
[email protected]a95986a82010-12-24 06:19:28208 IPC_MESSAGE_UNHANDLED(handled = false)
209 IPC_END_MESSAGE_MAP()
210 return handled;
211}
initial.commit09911bf2008-07-26 23:55:29212
213bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15214 // Don't send any messages after the browser has told us to close, and filter
215 // most outgoing messages while swapped out.
216 if ((is_swapped_out_ &&
217 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
218 closing_) {
initial.commit09911bf2008-07-26 23:55:29219 delete message;
220 return false;
221 }
222
223 // If given a messsage without a routing ID, then assign our routing ID.
224 if (message->routing_id() == MSG_ROUTING_NONE)
225 message->set_routing_id(routing_id_);
226
[email protected]d3fc25652009-02-24 22:31:25227 return render_thread_->Send(message);
[email protected]8085dbc82008-09-26 22:53:44228}
229
initial.commit09911bf2008-07-26 23:55:29230// Got a response from the browser after the renderer decided to create a new
231// view.
[email protected]c03a6e72011-04-19 21:42:06232void RenderWidget::OnCreatingNewAck(
[email protected]2d7c8552011-06-27 19:21:55233 gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29234 DCHECK(routing_id_ != MSG_ROUTING_NONE);
235
[email protected]2d7c8552011-06-27 19:21:55236 CompleteInit(parent);
initial.commit09911bf2008-07-26 23:55:29237}
238
239void RenderWidget::OnClose() {
240 if (closing_)
241 return;
242 closing_ = true;
243
244 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03245 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]8085dbc82008-09-26 22:53:44246 render_thread_->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03247 SetHidden(false);
248 }
initial.commit09911bf2008-07-26 23:55:29249
initial.commit09911bf2008-07-26 23:55:29250 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25251 // now. Post a task that only gets invoked when there are no nested message
252 // loops.
253 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
254 NewRunnableMethod(this, &RenderWidget::Close));
255
256 // Balances the AddRef taken when we called AddRoute.
257 Release();
initial.commit09911bf2008-07-26 23:55:29258}
259
[email protected]f21c613a2009-02-12 14:46:17260void RenderWidget::OnResize(const gfx::Size& new_size,
261 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29262 // During shutdown we can just ignore this message.
263 if (!webwidget_)
264 return;
265
[email protected]e2356242010-11-16 22:33:03266 // We shouldn't be asked to resize to our current size.
267 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
268
[email protected]f21c613a2009-02-12 14:46:17269 // Remember the rect where the resize corner will be drawn.
270 resizer_rect_ = resizer_rect;
271
[email protected]e2356242010-11-16 22:33:03272 if (size_ == new_size)
273 return;
274
initial.commit09911bf2008-07-26 23:55:29275 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03276 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29277 needs_repainting_on_restore_ = false;
278
initial.commit09911bf2008-07-26 23:55:29279 size_ = new_size;
280
281 // We should not be sent a Resize message if we have not ACK'd the previous
282 DCHECK(!next_paint_is_resize_ack());
283
[email protected]552e6002009-11-19 05:24:57284 paint_aggregator_.ClearPendingUpdate();
285
initial.commit09911bf2008-07-26 23:55:29286 // When resizing, we want to wait to paint before ACK'ing the resize. This
287 // ensures that we only resize as fast as we can paint. We only need to send
288 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28289 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29290 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56291 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46292 // Resize should have caused an invalidation of the entire view.
293 DCHECK(paint_aggregator_.HasPendingUpdate());
294 }
[email protected]2d5d09d52009-06-15 14:29:21295
initial.commit09911bf2008-07-26 23:55:29296 // We will send the Resize_ACK flag once we paint again.
297 set_next_paint_is_resize_ack();
298 }
299}
300
301void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31302 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29303 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03304 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29305}
306
307void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31308 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29309 // During shutdown we can just ignore this message.
310 if (!webwidget_)
311 return;
312
313 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03314 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29315
316 if (!needs_repainting && !needs_repainting_on_restore_)
317 return;
318 needs_repainting_on_restore_ = false;
319
[email protected]d65adb12010-04-28 17:26:49320 // Tag the next paint as a restore ack, which is picked up by
321 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29322 set_next_paint_is_restore_ack();
323
324 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56325 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46326 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
327 } else {
[email protected]9c3085f2011-06-09 02:10:31328#ifdef WTF_USE_THREADED_COMPOSITING
[email protected]a5922cc2011-05-24 23:06:30329 webwidget_->composite(false);
330#else
[email protected]f98d7e3c2010-09-13 22:30:46331 scheduleComposite();
[email protected]a5922cc2011-05-24 23:06:30332#endif
[email protected]f98d7e3c2010-09-13 22:30:46333 }
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
468 IPC::Message* response = new ViewHostMsg_HandleInputEvent_ACK(routing_id_);
469 response->WriteInt(input_event->type);
[email protected]e2824412009-02-27 01:57:05470 response->WriteBool(processed);
471
[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)));
496 }
initial.commit09911bf2008-07-26 23:55:29497}
498
499void RenderWidget::OnMouseCaptureLost() {
500 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28501 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29502}
503
504void RenderWidget::OnSetFocus(bool enable) {
505 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33506 if (webwidget_)
507 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29508}
509
510void RenderWidget::ClearFocus() {
511 // We may have got the focus from the browser before this gets processed, in
512 // which case we do not want to unfocus ourself.
513 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28514 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29515}
516
[email protected]2d5d09d52009-06-15 14:29:21517void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00518 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21519 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06520 TRACE_EVENT2("renderer", "PaintRect",
521 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00522 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21523
524 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00525 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
526 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03527
[email protected]699ab0d2009-04-23 23:19:14528 // If there is a custom background, tile it.
529 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14530 SkPaint paint;
531 SkShader* shader = SkShader::CreateBitmapShader(background_,
532 SkShader::kRepeat_TileMode,
533 SkShader::kRepeat_TileMode);
534 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52535 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14536 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14537 }
538
[email protected]719b36f2010-12-22 20:36:46539 // First see if this rect is a plugin that can paint itself faster.
540 TransportDIB* optimized_dib = NULL;
541 gfx::Rect optimized_copy_rect, optimized_copy_location;
542 webkit::ppapi::PluginInstance* optimized_instance =
543 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
544 &optimized_copy_location,
545 &optimized_copy_rect);
546 if (optimized_instance) {
547 // This plugin can be optimize-painted and we can just ask it to paint
548 // itself. We don't actually need the TransportDIB in this case.
549 //
550 // This is an optimization for PPAPI plugins that know they're on top of
551 // the page content. If this rect is inside such a plugin, we can save some
552 // time and avoid re-rendering the page content which we know will be
553 // covered by the plugin later (this time can be significant, especially
554 // for a playing movie that is invalidating a lot).
555 //
556 // In the plugin movie case, hopefully the similar call to
557 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
558 // painting, because that avoids copying the plugin image to a different
559 // paint rect. Unfortunately, if anything on the page is animating other
560 // than the movie, it break this optimization since the union of the
561 // invalid regions will be larger than the plugin.
562 //
563 // This code optimizes that case, where we can still avoid painting in
564 // WebKit and filling the background (which can be slow) and just painting
565 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
566 // required.
567 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27568 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46569 } else {
570 // Normal painting case.
571 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
572
573 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35574 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46575 }
initial.commit09911bf2008-07-26 23:55:29576
[email protected]4fb66842009-12-04 21:41:00577 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00578 canvas->restore();
579}
580
581void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
582 skia::PlatformCanvas* canvas) {
583 static bool kPaintBorder =
584 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
585 if (!kPaintBorder)
586 return;
587
[email protected]53d3f302009-12-21 04:42:05588 // Cycle through these colors to help distinguish new paint rects.
589 const SkColor colors[] = {
590 SkColorSetARGB(0x3F, 0xFF, 0, 0),
591 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
592 SkColorSetARGB(0x3F, 0, 0, 0xFF),
593 };
594 static int color_selector = 0;
595
[email protected]4fb66842009-12-04 21:41:00596 SkPaint paint;
597 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05598 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00599 paint.setStrokeWidth(1);
600
601 SkIRect irect;
602 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
603 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29604}
605
[email protected]52ccd0ea2011-02-16 01:09:05606void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30607 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]52ccd0ea2011-02-16 01:09:05608 animation_task_posted_ = false;
[email protected]921244e42011-07-20 16:36:30609 if (!animation_update_pending_) {
610 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59611 return;
[email protected]921244e42011-07-20 16:36:30612 }
[email protected]bd37ae252011-06-03 01:28:18613 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59614 // Record when we fired (according to base::Time::Now()) relative to when
615 // we posted the task to quantify how much the base::Time/base::TimeTicks
616 // skew is affecting animations.
617 base::TimeDelta animation_callback_delay = base::Time::Now() -
618 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
619 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
620 animation_callback_delay,
621 base::TimeDelta::FromMilliseconds(0),
622 base::TimeDelta::FromMilliseconds(30),
623 25);
624 }
[email protected]65225772011-05-12 21:10:24625 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24626}
627
[email protected]52ccd0ea2011-02-16 01:09:05628void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59629 if (!animation_update_pending_)
630 return;
[email protected]bd37ae252011-06-03 01:28:18631
632 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
633 int animationInterval = IsRenderingVSynced() ? 16 : 0;
634
[email protected]7c4329e2011-02-18 22:02:59635 base::Time now = base::Time::Now();
636 if (now >= animation_floor_time_) {
[email protected]921244e42011-07-20 16:36:30637 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]bd37ae252011-06-03 01:28:18638 animation_floor_time_ = now +
639 base::TimeDelta::FromMilliseconds(animationInterval);
640 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59641 // running animation callbacks so that if a callback requests another
642 // we'll be sure to run it at the proper time.
643 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
[email protected]bd37ae252011-06-03 01:28:18644 this, &RenderWidget::AnimationCallback), animationInterval);
[email protected]7c4329e2011-02-18 22:02:59645 animation_task_posted_ = true;
646 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30647#ifdef WEBWIDGET_HAS_ANIMATE_CHANGES
648 webwidget_->animate(0.0);
649#else
[email protected]7c4329e2011-02-18 22:02:59650 webwidget_->animate();
[email protected]a5922cc2011-05-24 23:06:30651#endif
[email protected]7c4329e2011-02-18 22:02:59652 return;
[email protected]5f8b1022011-01-21 23:34:50653 }
[email protected]bd37ae252011-06-03 01:28:18654 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]7c4329e2011-02-18 22:02:59655 if (animation_task_posted_)
656 return;
657 // This code uses base::Time::Now() to calculate the floor and next fire
658 // time because javascript's Date object uses base::Time::Now(). The
659 // message loop uses base::TimeTicks, which on windows can have a
660 // different granularity than base::Time.
661 // The upshot of all this is that this function might be called before
662 // base::Time::Now() has advanced past the animation_floor_time_. To
663 // avoid exposing this delay to javascript, we keep posting delayed
664 // tasks until base::Time::Now() has advanced far enough.
665 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
666 animation_task_posted_ = true;
667 MessageLoop::current()->PostDelayedTask(FROM_HERE,
668 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50669}
670
[email protected]bd37ae252011-06-03 01:28:18671bool RenderWidget::IsRenderingVSynced() {
672 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
673 // not caught by this check. This will lead to artificially low frame rates
674 // for people who force vsync off at a driver level and expect Chrome to speed
675 // up.
676 return !has_disable_gpu_vsync_switch_;
677}
678
[email protected]65225772011-05-12 21:10:24679void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06680 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24681 invalidation_task_posted_ = false;
682 DoDeferredUpdateAndSendInputAck();
683}
684
685void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05686 DoDeferredUpdate();
687
688 if (pending_input_event_ack_.get())
689 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21690}
691
[email protected]552e6002009-11-19 05:24:57692void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58693 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08694
[email protected]65225772011-05-12 21:10:24695 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29696 return;
[email protected]65225772011-05-12 21:10:24697 if (update_reply_pending()) {
698 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
699 return;
700 }
[email protected]9ca84622011-06-02 23:46:39701 if (is_accelerated_compositing_active_ &&
702 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24703 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
704 return;
705 }
initial.commit09911bf2008-07-26 23:55:29706
[email protected]552e6002009-11-19 05:24:57707 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29708 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57709 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29710 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24711 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29712 return;
713 }
714
[email protected]0fb93f52011-05-18 23:13:56715 // Tracking of frame rate jitter
716 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]52ccd0ea2011-02-16 01:09:05717 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50718
[email protected]f98d7e3c2010-09-13 22:30:46719 // Layout may generate more invalidation. It may also enable the
720 // GPU acceleration, so make sure to run layout before we send the
721 // GpuRenderingActivated message.
722 webwidget_->layout();
723
[email protected]5f8b1022011-01-21 23:34:50724 // Suppress painting if nothing is dirty. This has to be done after updating
725 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24726 if (!paint_aggregator_.HasPendingUpdate()) {
727 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]5f8b1022011-01-21 23:34:50728 return;
[email protected]65225772011-05-12 21:10:24729 }
[email protected]5f8b1022011-01-21 23:34:50730
[email protected]872ae5b2011-05-26 20:20:50731 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56732 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
733 if(is_accelerated_compositing_active_)
734 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
735 delay,
736 base::TimeDelta::FromMilliseconds(1),
737 base::TimeDelta::FromMilliseconds(60),
738 30);
739 else
740 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
741 delay,
742 base::TimeDelta::FromMilliseconds(1),
743 base::TimeDelta::FromMilliseconds(60),
744 30);
[email protected]872ae5b2011-05-26 20:20:50745
746 // Calculate filtered time per frame:
747 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
748 filtered_time_per_frame_ =
749 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56750 }
751 last_do_deferred_update_time_ = frame_begin_ticks;
752
[email protected]552e6002009-11-19 05:24:57753 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29754 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30755 PaintAggregator::PendingUpdate update;
756 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29757
[email protected]53d3f302009-12-21 04:42:05758 gfx::Rect scroll_damage = update.GetScrollDamage();
759 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29760
[email protected]27543452011-03-25 00:14:00761 // Compositing the page may disable accelerated compositing.
762 bool accelerated_compositing_was_active = is_accelerated_compositing_active_;
763
[email protected]ca4847f2010-09-24 05:39:15764 // A plugin may be able to do an optimized paint. First check this, in which
765 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46766 // This optimization allows PPAPI plugins that declare themselves on top of
767 // the page (like a traditional windowed plugin) to be able to animate (think
768 // movie playing) without repeatedly re-painting the page underneath, or
769 // copying the plugin backing store (since we can send the plugin's backing
770 // store directly to the browser).
771 //
772 // This optimization only works when the entire invalid region is contained
773 // within the plugin. There is a related optimization in PaintRect for the
774 // case where there may be multiple invalid regions.
[email protected]2650bd52011-03-25 00:34:44775 TransportDIB::Id dib_id = TransportDIB::Id();
[email protected]ca4847f2010-09-24 05:39:15776 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45777 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15778 gfx::Rect optimized_copy_rect, optimized_copy_location;
779 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56780 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15781 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
782 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27783 // Only update the part of the plugin that actually changed.
784 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15785 bounds = optimized_copy_location;
786 copy_rects.push_back(optimized_copy_rect);
787 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56788 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46789 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15790 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35791 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
792 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46793 if (!canvas.get()) {
794 NOTREACHED();
795 return;
796 }
[email protected]cef3362f2009-12-21 17:48:45797
[email protected]f98d7e3c2010-09-13 22:30:46798 // We may get back a smaller canvas than we asked for.
799 // TODO(darin): This seems like it could cause painting problems!
800 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
801 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
802 bounds.set_width(canvas->getDevice()->width());
803 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05804
[email protected]f98d7e3c2010-09-13 22:30:46805 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
806
[email protected]f98d7e3c2010-09-13 22:30:46807 // The scroll damage is just another rectangle to paint and copy.
808 copy_rects.swap(update.paint_rects);
809 if (!scroll_damage.IsEmpty())
810 copy_rects.push_back(scroll_damage);
811
812 for (size_t i = 0; i < copy_rects.size(); ++i)
813 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15814
[email protected]b4d08452010-10-05 17:34:35815 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46816 } else { // Accelerated compositing path
817 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42818 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46819 }
820
821 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05822 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30823 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05824 params.bitmap_rect = bounds;
825 params.dx = update.scroll_delta.x();
826 params.dy = update.scroll_delta.y();
[email protected]27543452011-03-25 00:14:00827 if (accelerated_compositing_was_active) {
[email protected]b167ca662010-05-14 00:05:34828 // If painting is done via the gpu process then we clear out all damage
829 // rects to save the browser process from doing unecessary work.
830 params.scroll_rect = gfx::Rect();
831 params.copy_rects.clear();
832 } else {
833 params.scroll_rect = update.scroll_rect;
834 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
835 }
[email protected]53d3f302009-12-21 04:42:05836 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03837 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05838 params.plugin_window_moves.swap(plugin_window_moves_);
839 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52840 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05841
842 update_reply_pending_ = true;
843 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
844 next_paint_flags_ = 0;
845
[email protected]fa7b1dc2010-06-23 17:53:04846 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28847
848 // Let derived classes know we've painted.
849 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29850}
851
852///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46853// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29854
[email protected]4873c7d2009-07-16 06:36:28855void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57856 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48857 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57858 gfx::Rect damaged_rect = view_rect.Intersect(rect);
859 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29860 return;
861
[email protected]552e6002009-11-19 05:24:57862 paint_aggregator_.InvalidateRect(damaged_rect);
863
864 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24865 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57866 return;
867 if (!paint_aggregator_.HasPendingUpdate())
868 return;
[email protected]65225772011-05-12 21:10:24869 if (update_reply_pending() ||
870 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
871 return;
872
873 // When GPU rendering, combine pending animations and invalidations into
874 // a single update.
875 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57876 return;
877
878 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29879 // 1) Ensures that we call WebView::Paint without a bunch of other junk
880 // on the call stack.
881 // 2) Allows us to collect more damage rects before painting to help coalesce
882 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24883 invalidation_task_posted_ = true;
initial.commit09911bf2008-07-26 23:55:29884 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]65225772011-05-12 21:10:24885 this, &RenderWidget::InvalidationCallback));
initial.commit09911bf2008-07-26 23:55:29886}
887
[email protected]4873c7d2009-07-16 06:36:28888void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46889 // Drop scrolls on the floor when we are in compositing mode.
890 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56891 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46892 return;
893
[email protected]552e6002009-11-19 05:24:57894 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48895 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57896 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
897 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29898 return;
899
[email protected]552e6002009-11-19 05:24:57900 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
901
902 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24903 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57904 return;
905 if (!paint_aggregator_.HasPendingUpdate())
906 return;
[email protected]65225772011-05-12 21:10:24907 if (update_reply_pending() ||
908 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
909 return;
910
911 // When GPU rendering, combine pending animations and invalidations into
912 // a single update.
913 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57914 return;
915
916 // Perform updating asynchronously. This serves two purposes:
917 // 1) Ensures that we call WebView::Paint without a bunch of other junk
918 // on the call stack.
919 // 2) Allows us to collect more damage rects before painting to help coalesce
920 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24921 invalidation_task_posted_ = true;
initial.commit09911bf2008-07-26 23:55:29922 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]65225772011-05-12 21:10:24923 this, &RenderWidget::InvalidationCallback));
initial.commit09911bf2008-07-26 23:55:29924}
925
[email protected]a79d8a632010-11-18 22:35:56926void RenderWidget::didActivateAcceleratedCompositing(bool active) {
[email protected]0c8fabf2011-06-14 19:35:22927 TRACE_EVENT1("gpu", "RenderWidget::didActivateAcceleratedCompositing",
928 "active", active);
[email protected]a79d8a632010-11-18 22:35:56929 is_accelerated_compositing_active_ = active;
[email protected]50bd6452010-11-27 19:39:42930 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:24931 routing_id_, is_accelerated_compositing_active_));
932
[email protected]5834f4392011-09-13 20:06:17933#ifndef WTF_USE_THREADED_COMPOSITING
[email protected]65225772011-05-12 21:10:24934 if (active)
935 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
936 else if (using_asynchronous_swapbuffers_)
937 using_asynchronous_swapbuffers_ = false;
[email protected]5834f4392011-09-13 20:06:17938#endif
[email protected]a79d8a632010-11-18 22:35:56939}
940
[email protected]f98d7e3c2010-09-13 22:30:46941void RenderWidget::scheduleComposite() {
[email protected]a5922cc2011-05-24 23:06:30942#if WTF_USE_THREADED_COMPOSITING
943 NOTREACHED();
944#else
[email protected]f98d7e3c2010-09-13 22:30:46945 // TODO(nduca): replace with something a little less hacky. The reason this
946 // hack is still used is because the Invalidate-DoDeferredUpdate loop
947 // contains a lot of host-renderer synchronization logic that is still
948 // important for the accelerated compositing case. The option of simply
949 // duplicating all that code is less desirable than "faking out" the
950 // invalidation path using a magical damage rect.
[email protected]e2356242010-11-16 22:33:03951 didInvalidateRect(WebRect(0, 0, 1, 1));
[email protected]a5922cc2011-05-24 23:06:30952#endif
[email protected]f98d7e3c2010-09-13 22:30:46953}
954
[email protected]5f8b1022011-01-21 23:34:50955void RenderWidget::scheduleAnimation() {
[email protected]921244e42011-07-20 16:36:30956 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ee3d3ad2011-02-04 00:42:21957 if (!animation_update_pending_) {
958 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05959 if (!animation_task_posted_) {
960 animation_task_posted_ = true;
961 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
962 this, &RenderWidget::AnimationCallback));
963 }
[email protected]ee3d3ad2011-02-04 00:42:21964 }
[email protected]5f8b1022011-01-21 23:34:50965}
966
[email protected]4873c7d2009-07-16 06:36:28967void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30968 // TODO(darin): Eliminate this temporary.
969 WebCursor cursor(cursor_info);
970
initial.commit09911bf2008-07-26 23:55:29971 // Only send a SetCursor message if we need to make a change.
972 if (!current_cursor_.IsEqual(cursor)) {
973 current_cursor_ = cursor;
974 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
975 }
976}
977
978// We are supposed to get a single call to Show for a newly created RenderWidget
979// that was created via RenderWidget::CreateWebView. So, we wait until this
980// point to dispatch the ShowWidget message.
981//
982// This method provides us with the information about how to display the newly
983// created RenderWidget (i.e., as a constrained popup or as a new tab).
984//
[email protected]4873c7d2009-07-16 06:36:28985void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29986 DCHECK(!did_show_) << "received extraneous Show call";
987 DCHECK(routing_id_ != MSG_ROUTING_NONE);
988 DCHECK(opener_id_ != MSG_ROUTING_NONE);
989
[email protected]8de12d942010-11-17 20:42:44990 if (did_show_)
991 return;
992
993 did_show_ = true;
994 // NOTE: initial_pos_ may still have its default values at this point, but
995 // that's okay. It'll be ignored if as_popup is false, or the browser
996 // process will impose a default position otherwise.
997 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
998 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:29999}
1000
[email protected]4873c7d2009-07-16 06:36:281001void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291002}
1003
[email protected]4873c7d2009-07-16 06:36:281004void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291005}
1006
[email protected]2533ce12009-05-09 00:02:241007void RenderWidget::DoDeferredClose() {
1008 Send(new ViewHostMsg_Close(routing_id_));
1009}
1010
[email protected]4873c7d2009-07-16 06:36:281011void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:291012 // If a page calls window.close() twice, we'll end up here twice, but that's
1013 // OK. It is safe to send multiple Close messages.
1014
[email protected]2533ce12009-05-09 00:02:241015 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1016 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1017 // could be closed before the JS finishes executing. So instead, post a
1018 // message back to the message loop, which won't run until the JS is
1019 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:151020 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:241021 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:291022}
1023
1024void RenderWidget::Close() {
1025 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281026 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291027 webwidget_ = NULL;
1028 }
1029}
1030
[email protected]4873c7d2009-07-16 06:36:281031WebRect RenderWidget::windowRect() {
1032 if (pending_window_rect_count_)
1033 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241034
[email protected]b3f2b912009-04-09 16:18:521035 gfx::Rect rect;
1036 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281037 return rect;
initial.commit09911bf2008-07-26 23:55:291038}
1039
[email protected]8a9d6ca32011-06-06 20:11:301040void RenderWidget::setToolTipText(const WebKit::WebString& text,
1041 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541042 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301043}
1044
[email protected]4873c7d2009-07-16 06:36:281045void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291046 if (did_show_) {
1047 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241048 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291049 } else {
1050 initial_pos_ = pos;
1051 }
1052}
1053
[email protected]2533ce12009-05-09 00:02:241054void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1055 pending_window_rect_ = rect;
1056 pending_window_rect_count_++;
1057}
1058
[email protected]4873c7d2009-07-16 06:36:281059WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241060 if (pending_window_rect_count_) {
1061 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1062 // the RootWindowRect is probably going to return wrong results since the
1063 // browser may not have processed the Move yet. There isn't really anything
1064 // good to do in this case, and it shouldn't happen - since this size is
1065 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281066 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241067 }
1068
[email protected]b3f2b912009-04-09 16:18:521069 gfx::Rect rect;
1070 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281071 return rect;
[email protected]d4547452008-08-28 18:36:371072}
1073
[email protected]4873c7d2009-07-16 06:36:281074WebRect RenderWidget::windowResizerRect() {
1075 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191076}
1077
[email protected]fa7b1dc2010-06-23 17:53:041078void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031079 // To prevent this renderer process from sending unnecessary IPC messages to
1080 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041081 // only during the input method attached to the browser process is active.
1082 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291083}
1084
[email protected]fa7b1dc2010-06-23 17:53:041085void RenderWidget::OnImeSetComposition(
1086 const string16& text,
1087 const std::vector<WebCompositionUnderline>& underlines,
1088 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281089 if (!webwidget_)
1090 return;
[email protected]d4cff272011-05-02 15:46:011091 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041092 text, WebVector<WebCompositionUnderline>(underlines),
1093 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011094 // Setting the IME composition was successful. Send the new composition
1095 // range to the browser.
1096 ui::Range range(ui::Range::InvalidRange());
1097 size_t location, length;
1098 if (webwidget_->compositionRange(&location, &length)) {
1099 range.set_start(location);
1100 range.set_end(location + length);
1101 }
1102 // The IME was cancelled via the Esc key, so just send back the caret.
1103 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1104 range.set_start(location);
1105 range.set_end(location + length);
1106 }
1107 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
1108 } else {
[email protected]fa7b1dc2010-06-23 17:53:041109 // If we failed to set the composition text, then we need to let the browser
1110 // process to cancel the input method's ongoing composition session, to make
1111 // sure we are in a consistent state.
1112 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011113
1114 // Send an updated IME range with just the caret range.
1115 ui::Range range(ui::Range::InvalidRange());
1116 size_t location, length;
1117 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1118 range.set_start(location);
1119 range.set_end(location + length);
1120 }
1121 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:261122 }
[email protected]fa7b1dc2010-06-23 17:53:041123}
1124
[email protected]6b349652011-01-05 18:36:241125void RenderWidget::OnImeConfirmComposition(const string16& text) {
[email protected]fa7b1dc2010-06-23 17:53:041126 if (webwidget_)
[email protected]6b349652011-01-05 18:36:241127 webwidget_->confirmComposition(text);
[email protected]d4cff272011-05-02 15:46:011128 // Send an updated IME range with just the caret range.
1129 ui::Range range(ui::Range::InvalidRange());
1130 size_t location, length;
1131 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1132 range.set_start(location);
1133 range.set_end(location + length);
1134 }
1135 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:291136}
1137
[email protected]948f7ab72010-05-28 23:48:081138// This message causes the renderer to render an image of the
1139// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491140void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091141 int tag,
[email protected]948f7ab72010-05-28 23:48:081142 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491143 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001144 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1145 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251146 // Close our unused handle.
1147#if defined(OS_WIN)
1148 ::CloseHandle(dib_handle);
1149#elif defined(OS_MACOSX)
1150 base::SharedMemory::CloseHandle(dib_handle);
1151#endif
1152 }
[email protected]d65adb12010-04-28 17:26:491153 return;
[email protected]45c6aad32010-11-11 04:46:251154 }
[email protected]d65adb12010-04-28 17:26:491155
[email protected]948f7ab72010-05-28 23:48:081156 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491157 // If one of these is empty, then we just return the dib we were
1158 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091159 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491160 return;
1161 }
1162
1163 // Map the given DIB ID into this process, and unmap it at the end
1164 // of this function.
[email protected]45c6aad32010-11-11 04:46:251165 scoped_ptr<TransportDIB> paint_at_size_buffer(
1166 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301167
1168 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491169 float x_scale = static_cast<float>(desired_size.width()) /
1170 static_cast<float>(canvas_size.width());
1171 float y_scale = static_cast<float>(desired_size.height()) /
1172 static_cast<float>(canvas_size.height());
1173
[email protected]ee8d6fd2010-05-26 17:05:481174 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491175 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1176 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481177 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491178
[email protected]36808ad2010-10-20 19:18:301179 scoped_ptr<skia::PlatformCanvas> canvas(
1180 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1181 canvas_size.height()));
1182 if (!canvas.get()) {
1183 NOTREACHED();
1184 return;
1185 }
1186
[email protected]d65adb12010-04-28 17:26:491187 // Reset bounds to what we actually received, but they should be the
1188 // same.
1189 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1190 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1191 bounds.set_width(canvas->getDevice()->width());
1192 bounds.set_height(canvas->getDevice()->height());
1193
1194 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081195 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491196 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1197
[email protected]948f7ab72010-05-28 23:48:081198 // Have to make sure we're laid out at the right size before
1199 // rendering.
1200 gfx::Size old_size = webwidget_->size();
1201 webwidget_->resize(page_size);
1202 webwidget_->layout();
1203
[email protected]d65adb12010-04-28 17:26:491204 // Paint the entire thing (using original bounds, not scaled bounds).
1205 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1206 canvas->restore();
1207
[email protected]948f7ab72010-05-28 23:48:081208 // Return the widget to its previous size.
1209 webwidget_->resize(old_size);
1210
[email protected]c88c9442010-07-19 18:55:091211 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491212}
1213
[email protected]ec7dc112008-08-06 05:30:121214void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1215 // During shutdown we can just ignore this message.
1216 if (!webwidget_)
1217 return;
1218
1219 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561220 if (is_accelerated_compositing_active_) {
[email protected]a5922cc2011-05-24 23:06:301221#ifndef WTF_USE_THREADED_COMPOSITING
[email protected]f98d7e3c2010-09-13 22:30:461222 scheduleComposite();
[email protected]a5922cc2011-05-24 23:06:301223#else
1224#ifdef WEBWIDGET_HAS_THREADED_COMPOSITING_CHANGES
1225 webwidget_->composite(false);
1226#endif
1227#endif
[email protected]f98d7e3c2010-09-13 22:30:461228 } else {
1229 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1230 didInvalidateRect(repaint_rect);
1231 }
[email protected]ec7dc112008-08-06 05:30:121232}
1233
[email protected]4873c7d2009-07-16 06:36:281234void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111235 if (!webwidget_)
1236 return;
[email protected]4873c7d2009-07-16 06:36:281237 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111238}
1239
[email protected]719b36f2010-12-22 20:36:461240webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151241 const gfx::Rect& paint_bounds,
1242 TransportDIB** dib,
1243 gfx::Rect* location,
1244 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461245 // Bare RenderWidgets don't support optimized plugin painting.
1246 return NULL;
[email protected]ca4847f2010-09-24 05:39:151247}
1248
[email protected]bcaf2272011-02-15 15:29:431249gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521250 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431251 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521252}
1253
[email protected]bee16aab2009-08-26 15:55:031254void RenderWidget::SetHidden(bool hidden) {
1255 if (is_hidden_ == hidden)
1256 return;
1257
1258 // The status has changed. Tell the RenderThread about it.
1259 is_hidden_ = hidden;
1260 if (is_hidden_)
1261 render_thread_->WidgetHidden();
1262 else
1263 render_thread_->WidgetRestored();
1264}
1265
[email protected]699ab0d2009-04-23 23:19:141266void RenderWidget::SetBackground(const SkBitmap& background) {
1267 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461268
[email protected]699ab0d2009-04-23 23:19:141269 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281270 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141271}
1272
[email protected]674741932009-02-04 23:44:461273bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051274 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461275}
1276
1277bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051278 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461279}
1280
1281void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051282 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461283}
1284
1285void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051286 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461287}
1288
1289void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051290 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461291}
1292
[email protected]fa7b1dc2010-06-23 17:53:041293void RenderWidget::UpdateInputMethod() {
1294 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291295 return;
[email protected]fa7b1dc2010-06-23 17:53:041296
[email protected]ad26ef42011-06-17 07:59:451297 ui::TextInputType new_type = GetTextInputType();
1298 bool new_can_compose_inline = CanComposeInline();
[email protected]fa7b1dc2010-06-23 17:53:041299 WebRect new_caret_bounds;
1300
[email protected]56ea1a62011-05-30 07:05:571301 if (webwidget_)
1302 new_caret_bounds = webwidget_->caretOrSelectionBounds();
[email protected]fa7b1dc2010-06-23 17:53:041303
1304 // Only sends text input type and caret bounds to the browser process if they
1305 // are changed.
[email protected]ad26ef42011-06-17 07:59:451306 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds ||
1307 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041308 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451309 can_compose_inline_ = new_can_compose_inline;
[email protected]fa7b1dc2010-06-23 17:53:041310 caret_bounds_ = new_caret_bounds;
1311 Send(new ViewHostMsg_ImeUpdateTextInputState(
[email protected]ad26ef42011-06-17 07:59:451312 routing_id(), new_type, new_can_compose_inline, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291313 }
initial.commit09911bf2008-07-26 23:55:291314}
1315
[email protected]ad26ef42011-06-17 07:59:451316COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1317 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1318COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1319 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1320COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1321 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181322COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1323 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1324COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1325 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1326COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1327 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1328COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1329 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1330COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1331 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451332
1333ui::TextInputType RenderWidget::GetTextInputType() {
1334 if (webwidget_) {
1335 int type = webwidget_->textInputType();
1336 // Check the type is in the range representable by ui::TextInputType.
[email protected]caf38ed2011-07-28 13:15:181337 DCHECK(type <= ui::TEXT_INPUT_TYPE_URL) <<
[email protected]ad26ef42011-06-17 07:59:451338 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1339 return static_cast<ui::TextInputType>(type);
1340 }
1341 return ui::TEXT_INPUT_TYPE_NONE;
1342}
1343
1344bool RenderWidget::CanComposeInline() {
1345 return true;
[email protected]56ea1a62011-05-30 07:05:571346}
1347
[email protected]4873c7d2009-07-16 06:36:281348WebScreenInfo RenderWidget::screenInfo() {
1349 WebScreenInfo results;
1350 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1351 return results;
1352}
1353
[email protected]fa7b1dc2010-06-23 17:53:041354void RenderWidget::resetInputMethod() {
1355 if (!input_method_is_active_)
1356 return;
1357
1358 // If the last text input type is not None, then we should finish any
1359 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451360 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041361 // If a composition text exists, then we need to let the browser process
1362 // to cancel the input method's ongoing composition session.
1363 if (webwidget_->confirmComposition())
1364 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1365 }
[email protected]d4cff272011-05-02 15:46:011366
1367 // Send an updated IME range with the current caret rect.
1368 ui::Range range(ui::Range::InvalidRange());
1369 size_t location, length;
1370 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1371 range.set_start(location);
1372 range.set_end(location + length);
1373 }
1374 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041375}
1376
[email protected]f103ab72009-09-02 17:10:591377void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501378 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291379 size_t i = 0;
1380 for (; i < plugin_window_moves_.size(); ++i) {
1381 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581382 if (move.rects_valid) {
1383 plugin_window_moves_[i] = move;
1384 } else {
1385 plugin_window_moves_[i].visible = move.visible;
1386 }
initial.commit09911bf2008-07-26 23:55:291387 break;
1388 }
1389 }
1390
1391 if (i == plugin_window_moves_.size())
1392 plugin_window_moves_.push_back(move);
1393}
[email protected]268654772009-08-06 23:02:041394
1395void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1396 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1397 i != plugin_window_moves_.end(); ++i) {
1398 if (i->window == window) {
1399 plugin_window_moves_.erase(i);
1400 break;
1401 }
1402 }
1403}
[email protected]67bfb83f2011-09-22 03:36:371404
1405bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1406 return false;
1407}