blob: f228f79e50cb742cebeb89d420f425069b0e320b [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]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"
26#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
27#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
[email protected]d4cff272011-05-02 15:46:0128#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
[email protected]8bd0fe62011-01-17 06:44:3729#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
30#include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h"
31#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
[email protected]8a9d6ca32011-06-06 20:11:3032#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
[email protected]08397d52011-02-05 01:53:3833#include "ui/gfx/point.h"
34#include "ui/gfx/size.h"
[email protected]b9b751f22011-03-25 14:04:1235#include "ui/gfx/surface/transport_dib.h"
[email protected]bd37ae252011-06-03 01:28:1836#include "ui/gfx/gl/gl_switches.h"
[email protected]8c89e7792009-08-19 21:18:3437#include "webkit/glue/webkit_glue.h"
[email protected]191eb3f72010-12-21 06:27:5038#include "webkit/plugins/npapi/webplugin.h"
[email protected]719b36f2010-12-22 20:36:4639#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
[email protected]661eb9d2009-02-03 02:11:4840
41#if defined(OS_POSIX)
[email protected]6b889fb2010-03-23 20:09:4942#include "ipc/ipc_channel_posix.h"
[email protected]d5282e72009-05-13 13:16:5243#include "third_party/skia/include/core/SkPixelRef.h"
44#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:4845#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:4446
[email protected]8bd0fe62011-01-17 06:44:3747#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h"
initial.commit09911bf2008-07-26 23:55:2948
[email protected]fa7b1dc2010-06-23 17:53:0449using WebKit::WebCompositionUnderline;
[email protected]7c51b0ee2009-07-08 21:49:3050using WebKit::WebCursorInfo;
[email protected]62cb33cae2009-03-27 23:30:2251using WebKit::WebInputEvent;
[email protected]6a8ddba52010-09-05 04:38:0652using WebKit::WebMouseEvent;
[email protected]4873c7d2009-07-16 06:36:2853using WebKit::WebNavigationPolicy;
54using WebKit::WebPopupMenu;
[email protected]88efb7ec2009-07-14 16:32:5955using WebKit::WebPopupMenuInfo;
[email protected]484955942010-08-19 16:13:1856using WebKit::WebPopupType;
[email protected]d4cff272011-05-02 15:46:0157using WebKit::WebRange;
[email protected]b3f2b912009-04-09 16:18:5258using WebKit::WebRect;
[email protected]12456fa2009-04-01 23:07:1959using WebKit::WebScreenInfo;
[email protected]b3f2b912009-04-09 16:18:5260using WebKit::WebSize;
[email protected]4873c7d2009-07-16 06:36:2861using WebKit::WebTextDirection;
[email protected]2d0f2e92011-10-03 09:02:2462using WebKit::WebTouchEvent;
[email protected]fa7b1dc2010-06-23 17:53:0463using WebKit::WebVector;
[email protected]484955942010-08-19 16:13:1864using WebKit::WebWidget;
[email protected]380244092011-10-07 17:26:2765using content::RenderThread;
[email protected]62cb33cae2009-03-27 23:30:2266
[email protected]380244092011-10-07 17:26:2767RenderWidget::RenderWidget(WebKit::WebPopupType popup_type)
initial.commit09911bf2008-07-26 23:55:2968 : routing_id_(MSG_ROUTING_NONE),
[email protected]c5b3b5e2009-02-13 06:41:1169 webwidget_(NULL),
initial.commit09911bf2008-07-26 23:55:2970 opener_id_(MSG_ROUTING_NONE),
[email protected]659f73f2009-10-13 13:43:4271 host_window_(0),
[email protected]b4d08452010-10-05 17:34:3572 current_paint_buf_(NULL),
initial.commit09911bf2008-07-26 23:55:2973 next_paint_flags_(0),
[email protected]872ae5b2011-05-26 20:20:5074 filtered_time_per_frame_(1.0f/60.0f),
[email protected]53d3f302009-12-21 04:42:0575 update_reply_pending_(false),
[email protected]65225772011-05-12 21:10:2476 using_asynchronous_swapbuffers_(false),
77 num_swapbuffers_complete_pending_(0),
initial.commit09911bf2008-07-26 23:55:2978 did_show_(false),
initial.commit09911bf2008-07-26 23:55:2979 is_hidden_(false),
80 needs_repainting_on_restore_(false),
81 has_focus_(false),
[email protected]5dd768212009-08-13 23:34:4982 handling_input_event_(false),
[email protected]661eb9d2009-02-03 02:11:4883 closing_(false),
[email protected]992db4c2011-05-12 15:37:1584 is_swapped_out_(false),
[email protected]fa7b1dc2010-06-23 17:53:0485 input_method_is_active_(false),
[email protected]ad26ef42011-06-17 07:59:4586 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
87 can_compose_inline_(true),
[email protected]3e2b375b2010-04-07 17:03:1288 popup_type_(popup_type),
[email protected]867125a02009-12-10 06:01:4889 pending_window_rect_count_(0),
[email protected]edbcde932010-05-07 17:10:4690 suppress_next_char_events_(false),
[email protected]5f8b1022011-01-21 23:34:5091 is_accelerated_compositing_active_(false),
[email protected]ee3d3ad2011-02-04 00:42:2192 animation_update_pending_(false),
[email protected]65225772011-05-12 21:10:2493 animation_task_posted_(false),
94 invalidation_task_posted_(false) {
[email protected]8930d472009-02-21 08:05:2895 RenderProcess::current()->AddRefProcess();
[email protected]380244092011-10-07 17:26:2796 DCHECK(RenderThread::Get());
[email protected]bd37ae252011-06-03 01:28:1897 has_disable_gpu_vsync_switch_ = CommandLine::ForCurrentProcess()->HasSwitch(
98 switches::kDisableGpuVsync);
initial.commit09911bf2008-07-26 23:55:2999}
100
101RenderWidget::~RenderWidget() {
[email protected]c5b3b5e2009-02-13 06:41:11102 DCHECK(!webwidget_) << "Leaking our WebWidget!";
[email protected]b4d08452010-10-05 17:34:35103 if (current_paint_buf_) {
104 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
105 current_paint_buf_ = NULL;
initial.commit09911bf2008-07-26 23:55:29106 }
[email protected]992db4c2011-05-12 15:37:15107 // If we are swapped out, we have released already.
108 if (!is_swapped_out_)
109 RenderProcess::current()->ReleaseProcess();
initial.commit09911bf2008-07-26 23:55:29110}
111
[email protected]484955942010-08-19 16:13:18112// static
[email protected]8085dbc82008-09-26 22:53:44113RenderWidget* RenderWidget::Create(int32 opener_id,
[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]380244092011-10-07 17:26:27116 scoped_refptr<RenderWidget> widget(new RenderWidget(popup_type));
initial.commit09911bf2008-07-26 23:55:29117 widget->Init(opener_id); // adds reference
118 return widget;
119}
120
[email protected]484955942010-08-19 16:13:18121// static
122WebWidget* RenderWidget::CreateWebWidget(RenderWidget* render_widget) {
123 switch (render_widget->popup_type_) {
[email protected]e2356242010-11-16 22:33:03124 case WebKit::WebPopupTypeNone: // Nothing to create.
[email protected]484955942010-08-19 16:13:18125 break;
126 case WebKit::WebPopupTypeSelect:
127 case WebKit::WebPopupTypeSuggestion:
128 return WebPopupMenu::create(render_widget);
129 default:
130 NOTREACHED();
131 }
132 return NULL;
133}
134
initial.commit09911bf2008-07-26 23:55:29135void RenderWidget::Init(int32 opener_id) {
[email protected]484955942010-08-19 16:13:18136 DoInit(opener_id,
137 RenderWidget::CreateWebWidget(this),
138 new ViewHostMsg_CreateWidget(opener_id, popup_type_, &routing_id_));
139}
140
[email protected]484955942010-08-19 16:13:18141void RenderWidget::DoInit(int32 opener_id,
[email protected]6a8ddba52010-09-05 04:38:06142 WebWidget* web_widget,
[email protected]484955942010-08-19 16:13:18143 IPC::SyncMessage* create_widget_message) {
initial.commit09911bf2008-07-26 23:55:29144 DCHECK(!webwidget_);
145
146 if (opener_id != MSG_ROUTING_NONE)
147 opener_id_ = opener_id;
148
[email protected]484955942010-08-19 16:13:18149 webwidget_ = web_widget;
initial.commit09911bf2008-07-26 23:55:29150
[email protected]380244092011-10-07 17:26:27151 bool result = RenderThread::Get()->Send(create_widget_message);
initial.commit09911bf2008-07-26 23:55:29152 if (result) {
[email protected]380244092011-10-07 17:26:27153 RenderThread::Get()->AddRoute(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29154 // Take a reference on behalf of the RenderThread. This will be balanced
155 // when we receive ViewMsg_Close.
156 AddRef();
157 } else {
158 DCHECK(false);
159 }
160}
161
162// This is used to complete pending inits and non-pending inits. For non-
163// pending cases, the parent will be the same as the current parent. This
164// indicates we do not need to reparent or anything.
[email protected]2d7c8552011-06-27 19:21:55165void RenderWidget::CompleteInit(gfx::NativeViewId parent_hwnd) {
initial.commit09911bf2008-07-26 23:55:29166 DCHECK(routing_id_ != MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29167
168 host_window_ = parent_hwnd;
169
[email protected]6de74452009-02-25 18:04:59170 Send(new ViewHostMsg_RenderViewReady(routing_id_));
initial.commit09911bf2008-07-26 23:55:29171}
172
[email protected]992db4c2011-05-12 15:37:15173void RenderWidget::SetSwappedOut(bool is_swapped_out) {
174 // We should only toggle between states.
175 DCHECK(is_swapped_out_ != is_swapped_out);
176 is_swapped_out_ = is_swapped_out;
177
178 // If we are swapping out, we will call ReleaseProcess, allowing the process
179 // to exit if all of its RenderViews are swapped out. We wait until the
180 // WasSwappedOut call to do this, to avoid showing the sad tab.
181 // If we are swapping in, we call AddRefProcess to prevent the process from
182 // exiting.
183 if (!is_swapped_out)
184 RenderProcess::current()->AddRefProcess();
185}
186
[email protected]a95986a82010-12-24 06:19:28187bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
188 bool handled = true;
189 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
190 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose)
191 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck)
192 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize)
193 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden)
194 IPC_MESSAGE_HANDLER(ViewMsg_WasRestored, OnWasRestored)
[email protected]992db4c2011-05-12 15:37:15195 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut)
[email protected]a95986a82010-12-24 06:19:28196 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck)
197 IPC_MESSAGE_HANDLER(ViewMsg_HandleInputEvent, OnHandleInputEvent)
198 IPC_MESSAGE_HANDLER(ViewMsg_MouseCaptureLost, OnMouseCaptureLost)
199 IPC_MESSAGE_HANDLER(ViewMsg_SetFocus, OnSetFocus)
200 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive)
201 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition)
202 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition)
203 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnMsgPaintAtSize)
204 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnMsgRepaint)
205 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection)
206 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck)
207 IPC_MESSAGE_UNHANDLED(handled = false)
208 IPC_END_MESSAGE_MAP()
209 return handled;
210}
initial.commit09911bf2008-07-26 23:55:29211
212bool RenderWidget::Send(IPC::Message* message) {
[email protected]992db4c2011-05-12 15:37:15213 // Don't send any messages after the browser has told us to close, and filter
214 // most outgoing messages while swapped out.
215 if ((is_swapped_out_ &&
216 !content::SwappedOutMessages::CanSendWhileSwappedOut(message)) ||
217 closing_) {
initial.commit09911bf2008-07-26 23:55:29218 delete message;
219 return false;
220 }
221
222 // If given a messsage without a routing ID, then assign our routing ID.
223 if (message->routing_id() == MSG_ROUTING_NONE)
224 message->set_routing_id(routing_id_);
225
[email protected]380244092011-10-07 17:26:27226 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44227}
228
initial.commit09911bf2008-07-26 23:55:29229// Got a response from the browser after the renderer decided to create a new
230// view.
[email protected]c03a6e72011-04-19 21:42:06231void RenderWidget::OnCreatingNewAck(
[email protected]2d7c8552011-06-27 19:21:55232 gfx::NativeViewId parent) {
initial.commit09911bf2008-07-26 23:55:29233 DCHECK(routing_id_ != MSG_ROUTING_NONE);
234
[email protected]2d7c8552011-06-27 19:21:55235 CompleteInit(parent);
initial.commit09911bf2008-07-26 23:55:29236}
237
238void RenderWidget::OnClose() {
239 if (closing_)
240 return;
241 closing_ = true;
242
243 // Browser correspondence is no longer needed at this point.
[email protected]bee16aab2009-08-26 15:55:03244 if (routing_id_ != MSG_ROUTING_NONE) {
[email protected]380244092011-10-07 17:26:27245 RenderThread::Get()->RemoveRoute(routing_id_);
[email protected]bee16aab2009-08-26 15:55:03246 SetHidden(false);
247 }
initial.commit09911bf2008-07-26 23:55:29248
initial.commit09911bf2008-07-26 23:55:29249 // If there is a Send call on the stack, then it could be dangerous to close
[email protected]d3fc25652009-02-24 22:31:25250 // now. Post a task that only gets invoked when there are no nested message
251 // loops.
252 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
253 NewRunnableMethod(this, &RenderWidget::Close));
254
255 // Balances the AddRef taken when we called AddRoute.
256 Release();
initial.commit09911bf2008-07-26 23:55:29257}
258
[email protected]f21c613a2009-02-12 14:46:17259void RenderWidget::OnResize(const gfx::Size& new_size,
260 const gfx::Rect& resizer_rect) {
initial.commit09911bf2008-07-26 23:55:29261 // During shutdown we can just ignore this message.
262 if (!webwidget_)
263 return;
264
[email protected]e2356242010-11-16 22:33:03265 // We shouldn't be asked to resize to our current size.
266 DCHECK(size_ != new_size || resizer_rect_ != resizer_rect);
267
[email protected]f21c613a2009-02-12 14:46:17268 // Remember the rect where the resize corner will be drawn.
269 resizer_rect_ = resizer_rect;
270
[email protected]e2356242010-11-16 22:33:03271 if (size_ == new_size)
272 return;
273
initial.commit09911bf2008-07-26 23:55:29274 // TODO(darin): We should not need to reset this here.
[email protected]bee16aab2009-08-26 15:55:03275 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29276 needs_repainting_on_restore_ = false;
277
initial.commit09911bf2008-07-26 23:55:29278 size_ = new_size;
279
280 // We should not be sent a Resize message if we have not ACK'd the previous
281 DCHECK(!next_paint_is_resize_ack());
282
[email protected]552e6002009-11-19 05:24:57283 paint_aggregator_.ClearPendingUpdate();
284
initial.commit09911bf2008-07-26 23:55:29285 // When resizing, we want to wait to paint before ACK'ing the resize. This
286 // ensures that we only resize as fast as we can paint. We only need to send
287 // an ACK if we are resized to a non-empty rect.
[email protected]4873c7d2009-07-16 06:36:28288 webwidget_->resize(new_size);
initial.commit09911bf2008-07-26 23:55:29289 if (!new_size.IsEmpty()) {
[email protected]a79d8a632010-11-18 22:35:56290 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46291 // Resize should have caused an invalidation of the entire view.
292 DCHECK(paint_aggregator_.HasPendingUpdate());
293 }
[email protected]2d5d09d52009-06-15 14:29:21294
initial.commit09911bf2008-07-26 23:55:29295 // We will send the Resize_ACK flag once we paint again.
296 set_next_paint_is_resize_ack();
297 }
298}
299
300void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31301 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29302 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03303 SetHidden(true);
initial.commit09911bf2008-07-26 23:55:29304}
305
306void RenderWidget::OnWasRestored(bool needs_repainting) {
[email protected]9c3085f2011-06-09 02:10:31307 TRACE_EVENT0("renderer", "RenderWidget::OnWasRestored");
initial.commit09911bf2008-07-26 23:55:29308 // During shutdown we can just ignore this message.
309 if (!webwidget_)
310 return;
311
312 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03313 SetHidden(false);
initial.commit09911bf2008-07-26 23:55:29314
315 if (!needs_repainting && !needs_repainting_on_restore_)
316 return;
317 needs_repainting_on_restore_ = false;
318
[email protected]d65adb12010-04-28 17:26:49319 // Tag the next paint as a restore ack, which is picked up by
320 // DoDeferredUpdate when it sends out the next PaintRect message.
initial.commit09911bf2008-07-26 23:55:29321 set_next_paint_is_restore_ack();
322
323 // Generate a full repaint.
[email protected]a79d8a632010-11-18 22:35:56324 if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46325 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
326 } else {
[email protected]f98d7e3c2010-09-13 22:30:46327 scheduleComposite();
[email protected]f98d7e3c2010-09-13 22:30:46328 }
initial.commit09911bf2008-07-26 23:55:29329}
330
[email protected]992db4c2011-05-12 15:37:15331void RenderWidget::OnWasSwappedOut() {
332 // If we have been swapped out and no one else is using this process,
333 // it's safe to exit now. If we get swapped back in, we will call
334 // AddRefProcess in SetSwappedOut.
335 if (is_swapped_out_)
336 RenderProcess::current()->ReleaseProcess();
337}
338
[email protected]53d3f302009-12-21 04:42:05339void RenderWidget::OnRequestMoveAck() {
340 DCHECK(pending_window_rect_count_);
341 pending_window_rect_count_--;
342}
343
344void RenderWidget::OnUpdateRectAck() {
[email protected]366ae242011-05-10 02:23:58345 TRACE_EVENT0("renderer", "RenderWidget::OnUpdateRectAck");
[email protected]53d3f302009-12-21 04:42:05346 DCHECK(update_reply_pending());
347 update_reply_pending_ = false;
348
[email protected]b4d08452010-10-05 17:34:35349 // If we sent an UpdateRect message with a zero-sized bitmap, then we should
350 // have no current paint buffer.
351 if (current_paint_buf_) {
352 RenderProcess::current()->ReleaseTransportDIB(current_paint_buf_);
353 current_paint_buf_ = NULL;
354 }
355
[email protected]65225772011-05-12 21:10:24356 // If swapbuffers is still pending, then defer the update until the
357 // swapbuffers occurs.
358 if (num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
359 TRACE_EVENT0("renderer", "EarlyOut_SwapStillPending");
360 return;
361 }
362
[email protected]00c39612010-03-06 02:53:28363 // Notify subclasses.
364 DidFlushPaint();
[email protected]a2f6bc112009-06-27 16:27:25365
initial.commit09911bf2008-07-26 23:55:29366 // Continue painting if necessary...
[email protected]65225772011-05-12 21:10:24367 DoDeferredUpdateAndSendInputAck();
368}
369
370bool RenderWidget::SupportsAsynchronousSwapBuffers()
371{
372 return false;
373}
374
375void RenderWidget::OnSwapBuffersAborted()
376{
377 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersAborted");
378 num_swapbuffers_complete_pending_ = 0;
379 using_asynchronous_swapbuffers_ = false;
380 // Schedule another frame so the compositor learns about it.
381 scheduleComposite();
382}
383
[email protected]37a6f302011-07-11 23:43:08384void RenderWidget::OnSwapBuffersPosted() {
385 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersPosted");
386 if (using_asynchronous_swapbuffers_)
387 num_swapbuffers_complete_pending_++;
388}
389
390void RenderWidget::OnSwapBuffersComplete() {
[email protected]65225772011-05-12 21:10:24391 TRACE_EVENT0("renderer", "RenderWidget::OnSwapBuffersComplete");
392 // When compositing deactivates, we reset the swapbuffers pending count. The
393 // swapbuffers acks may still arrive, however.
394 if (num_swapbuffers_complete_pending_ == 0) {
395 TRACE_EVENT0("renderer", "EarlyOut_ZeroSwapbuffersPending");
396 return;
397 }
398 num_swapbuffers_complete_pending_--;
399
400 // If update reply is still pending, then defer the update until that reply
401 // occurs.
402 if (update_reply_pending_){
403 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
404 return;
405 }
406
407 // If we are not accelerated rendering, then this is a stale swapbuffers from
[email protected]50312bf2011-06-22 23:30:06408 // when we were previously rendering. However, if an invalidation task is not
409 // posted, there may be software rendering work pending. In that case, don't
410 // early out.
411 if (!is_accelerated_compositing_active_ && invalidation_task_posted_) {
[email protected]65225772011-05-12 21:10:24412 TRACE_EVENT0("renderer", "EarlyOut_AcceleratedCompositingOff");
413 return;
414 }
415
416 // Notify subclasses.
[email protected]50312bf2011-06-22 23:30:06417 if(is_accelerated_compositing_active_)
418 DidFlushPaint();
[email protected]65225772011-05-12 21:10:24419
420 // Continue painting if necessary...
421 DoDeferredUpdateAndSendInputAck();
initial.commit09911bf2008-07-26 23:55:29422}
423
initial.commit09911bf2008-07-26 23:55:29424void RenderWidget::OnHandleInputEvent(const IPC::Message& message) {
[email protected]65225772011-05-12 21:10:24425 TRACE_EVENT0("renderer", "RenderWidget::OnHandleInputEvent");
initial.commit09911bf2008-07-26 23:55:29426 void* iter = NULL;
427
428 const char* data;
429 int data_length;
[email protected]5dd768212009-08-13 23:34:49430 handling_input_event_ = true;
431 if (!message.ReadData(&iter, &data, &data_length)) {
432 handling_input_event_ = false;
initial.commit09911bf2008-07-26 23:55:29433 return;
[email protected]5dd768212009-08-13 23:34:49434 }
initial.commit09911bf2008-07-26 23:55:29435
436 const WebInputEvent* input_event =
437 reinterpret_cast<const WebInputEvent*>(data);
[email protected]867125a02009-12-10 06:01:48438
439 bool is_keyboard_shortcut = false;
440 // is_keyboard_shortcut flag is only available for RawKeyDown events.
441 if (input_event->type == WebInputEvent::RawKeyDown)
442 message.ReadBool(&iter, &is_keyboard_shortcut);
443
[email protected]67bfb83f2011-09-22 03:36:37444 bool prevent_default = false;
445 if (WebInputEvent::isMouseEventType(input_event->type)) {
446 prevent_default = WillHandleMouseEvent(
447 *(static_cast<const WebMouseEvent*>(input_event)));
448 }
449
450 bool processed = prevent_default;
[email protected]867125a02009-12-10 06:01:48451 if (input_event->type != WebInputEvent::Char || !suppress_next_char_events_) {
452 suppress_next_char_events_ = false;
[email protected]67bfb83f2011-09-22 03:36:37453 if (!processed && webwidget_)
[email protected]867125a02009-12-10 06:01:48454 processed = webwidget_->handleInputEvent(*input_event);
455 }
456
457 // If this RawKeyDown event corresponds to a browser keyboard shortcut and
458 // it's not processed by webkit, then we need to suppress the upcoming Char
459 // events.
460 if (!processed && is_keyboard_shortcut)
461 suppress_next_char_events_ = true;
initial.commit09911bf2008-07-26 23:55:29462
[email protected]a9fb30aa2011-10-06 06:58:46463 IPC::Message* response =
464 new ViewHostMsg_HandleInputEvent_ACK(routing_id_, input_event->type,
465 processed);
[email protected]e2824412009-02-27 01:57:05466
[email protected]353a34c2010-05-28 23:35:17467 if ((input_event->type == WebInputEvent::MouseMove ||
[email protected]d93fc462011-03-14 23:03:03468 input_event->type == WebInputEvent::MouseWheel ||
469 input_event->type == WebInputEvent::TouchMove) &&
[email protected]552e6002009-11-19 05:24:57470 paint_aggregator_.HasPendingUpdate()) {
[email protected]12fbad812009-09-01 18:21:24471 // We want to rate limit the input events in this case, so we'll wait for
472 // painting to finish before ACKing this message.
[email protected]353a34c2010-05-28 23:35:17473 if (pending_input_event_ack_.get()) {
474 // As two different kinds of events could cause us to postpone an ack
475 // we send it now, if we have one pending. The Browser should never
476 // send us the same kind of event we are delaying the ack for.
477 Send(pending_input_event_ack_.release());
478 }
[email protected]12fbad812009-09-01 18:21:24479 pending_input_event_ack_.reset(response);
480 } else {
481 Send(response);
482 }
483
[email protected]5dd768212009-08-13 23:34:49484 handling_input_event_ = false;
[email protected]446705872009-09-10 07:22:48485
[email protected]67bfb83f2011-09-22 03:36:37486 if (!prevent_default) {
487 if (WebInputEvent::isKeyboardEventType(input_event->type))
488 DidHandleKeyEvent();
489 if (WebInputEvent::isMouseEventType(input_event->type))
490 DidHandleMouseEvent(*(static_cast<const WebMouseEvent*>(input_event)));
[email protected]2d0f2e92011-10-03 09:02:24491 if (WebInputEvent::isTouchEventType(input_event->type))
492 DidHandleTouchEvent(*(static_cast<const WebTouchEvent*>(input_event)));
[email protected]67bfb83f2011-09-22 03:36:37493 }
initial.commit09911bf2008-07-26 23:55:29494}
495
496void RenderWidget::OnMouseCaptureLost() {
497 if (webwidget_)
[email protected]4873c7d2009-07-16 06:36:28498 webwidget_->mouseCaptureLost();
initial.commit09911bf2008-07-26 23:55:29499}
500
501void RenderWidget::OnSetFocus(bool enable) {
502 has_focus_ = enable;
[email protected]9d166af2010-03-02 22:04:33503 if (webwidget_)
504 webwidget_->setFocus(enable);
initial.commit09911bf2008-07-26 23:55:29505}
506
507void RenderWidget::ClearFocus() {
508 // We may have got the focus from the browser before this gets processed, in
509 // which case we do not want to unfocus ourself.
510 if (!has_focus_ && webwidget_)
[email protected]4873c7d2009-07-16 06:36:28511 webwidget_->setFocus(false);
initial.commit09911bf2008-07-26 23:55:29512}
513
[email protected]2d5d09d52009-06-15 14:29:21514void RenderWidget::PaintRect(const gfx::Rect& rect,
[email protected]4fb66842009-12-04 21:41:00515 const gfx::Point& canvas_origin,
[email protected]2d5d09d52009-06-15 14:29:21516 skia::PlatformCanvas* canvas) {
[email protected]50312bf2011-06-22 23:30:06517 TRACE_EVENT2("renderer", "PaintRect",
518 "width", rect.width(), "height", rect.height());
[email protected]4fb66842009-12-04 21:41:00519 canvas->save();
[email protected]2d5d09d52009-06-15 14:29:21520
521 // Bring the canvas into the coordinate system of the paint rect.
[email protected]4fb66842009-12-04 21:41:00522 canvas->translate(static_cast<SkScalar>(-canvas_origin.x()),
523 static_cast<SkScalar>(-canvas_origin.y()));
[email protected]96c3499a2009-05-02 18:31:03524
[email protected]699ab0d2009-04-23 23:19:14525 // If there is a custom background, tile it.
526 if (!background_.empty()) {
[email protected]699ab0d2009-04-23 23:19:14527 SkPaint paint;
528 SkShader* shader = SkShader::CreateBitmapShader(background_,
529 SkShader::kRepeat_TileMode,
530 SkShader::kRepeat_TileMode);
531 paint.setShader(shader)->unref();
[email protected]8860e4f52009-06-25 01:01:52532 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
[email protected]699ab0d2009-04-23 23:19:14533 canvas->drawPaint(paint);
[email protected]699ab0d2009-04-23 23:19:14534 }
535
[email protected]719b36f2010-12-22 20:36:46536 // First see if this rect is a plugin that can paint itself faster.
537 TransportDIB* optimized_dib = NULL;
538 gfx::Rect optimized_copy_rect, optimized_copy_location;
539 webkit::ppapi::PluginInstance* optimized_instance =
540 GetBitmapForOptimizedPluginPaint(rect, &optimized_dib,
541 &optimized_copy_location,
542 &optimized_copy_rect);
543 if (optimized_instance) {
544 // This plugin can be optimize-painted and we can just ask it to paint
545 // itself. We don't actually need the TransportDIB in this case.
546 //
547 // This is an optimization for PPAPI plugins that know they're on top of
548 // the page content. If this rect is inside such a plugin, we can save some
549 // time and avoid re-rendering the page content which we know will be
550 // covered by the plugin later (this time can be significant, especially
551 // for a playing movie that is invalidating a lot).
552 //
553 // In the plugin movie case, hopefully the similar call to
554 // GetBitmapForOptimizedPluginPaint in DoDeferredUpdate handles the
555 // painting, because that avoids copying the plugin image to a different
556 // paint rect. Unfortunately, if anything on the page is animating other
557 // than the movie, it break this optimization since the union of the
558 // invalid regions will be larger than the plugin.
559 //
560 // This code optimizes that case, where we can still avoid painting in
561 // WebKit and filling the background (which can be slow) and just painting
562 // the plugin. Unlike the DoDeferredUpdate case, an extra copy is still
563 // required.
564 optimized_instance->Paint(webkit_glue::ToWebCanvas(canvas),
[email protected]2df1b362011-01-21 21:22:27565 optimized_copy_location, rect);
[email protected]719b36f2010-12-22 20:36:46566 } else {
567 // Normal painting case.
568 webwidget_->paint(webkit_glue::ToWebCanvas(canvas), rect);
569
570 // Flush to underlying bitmap. TODO(darin): is this needed?
[email protected]62f2e802011-05-26 14:28:35571 skia::GetTopDevice(*canvas)->accessBitmap(false);
[email protected]719b36f2010-12-22 20:36:46572 }
initial.commit09911bf2008-07-26 23:55:29573
[email protected]4fb66842009-12-04 21:41:00574 PaintDebugBorder(rect, canvas);
[email protected]4fb66842009-12-04 21:41:00575 canvas->restore();
576}
577
578void RenderWidget::PaintDebugBorder(const gfx::Rect& rect,
579 skia::PlatformCanvas* canvas) {
580 static bool kPaintBorder =
581 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowPaintRects);
582 if (!kPaintBorder)
583 return;
584
[email protected]53d3f302009-12-21 04:42:05585 // Cycle through these colors to help distinguish new paint rects.
586 const SkColor colors[] = {
587 SkColorSetARGB(0x3F, 0xFF, 0, 0),
588 SkColorSetARGB(0x3F, 0xFF, 0, 0xFF),
589 SkColorSetARGB(0x3F, 0, 0, 0xFF),
590 };
591 static int color_selector = 0;
592
[email protected]4fb66842009-12-04 21:41:00593 SkPaint paint;
594 paint.setStyle(SkPaint::kStroke_Style);
[email protected]53d3f302009-12-21 04:42:05595 paint.setColor(colors[color_selector++ % arraysize(colors)]);
[email protected]4fb66842009-12-04 21:41:00596 paint.setStrokeWidth(1);
597
598 SkIRect irect;
599 irect.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1);
600 canvas->drawIRect(irect, paint);
initial.commit09911bf2008-07-26 23:55:29601}
602
[email protected]52ccd0ea2011-02-16 01:09:05603void RenderWidget::AnimationCallback() {
[email protected]921244e42011-07-20 16:36:30604 TRACE_EVENT0("renderer", "RenderWidget::AnimationCallback");
[email protected]52ccd0ea2011-02-16 01:09:05605 animation_task_posted_ = false;
[email protected]921244e42011-07-20 16:36:30606 if (!animation_update_pending_) {
607 TRACE_EVENT0("renderer", "EarlyOut_NoAnimationUpdatePending");
[email protected]7c4329e2011-02-18 22:02:59608 return;
[email protected]921244e42011-07-20 16:36:30609 }
[email protected]bd37ae252011-06-03 01:28:18610 if (!animation_floor_time_.is_null() && IsRenderingVSynced()) {
[email protected]7c4329e2011-02-18 22:02:59611 // Record when we fired (according to base::Time::Now()) relative to when
612 // we posted the task to quantify how much the base::Time/base::TimeTicks
613 // skew is affecting animations.
614 base::TimeDelta animation_callback_delay = base::Time::Now() -
615 (animation_floor_time_ - base::TimeDelta::FromMilliseconds(16));
616 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AnimationCallbackDelayTime",
617 animation_callback_delay,
618 base::TimeDelta::FromMilliseconds(0),
619 base::TimeDelta::FromMilliseconds(30),
620 25);
621 }
[email protected]65225772011-05-12 21:10:24622 DoDeferredUpdateAndSendInputAck();
[email protected]12fbad812009-09-01 18:21:24623}
624
[email protected]52ccd0ea2011-02-16 01:09:05625void RenderWidget::AnimateIfNeeded() {
[email protected]7c4329e2011-02-18 22:02:59626 if (!animation_update_pending_)
627 return;
[email protected]bd37ae252011-06-03 01:28:18628
629 // Target 60FPS if vsync is on. Go as fast as we can if vsync is off.
630 int animationInterval = IsRenderingVSynced() ? 16 : 0;
631
[email protected]7c4329e2011-02-18 22:02:59632 base::Time now = base::Time::Now();
633 if (now >= animation_floor_time_) {
[email protected]921244e42011-07-20 16:36:30634 TRACE_EVENT0("renderer", "RenderWidget::AnimateIfNeeded")
[email protected]bd37ae252011-06-03 01:28:18635 animation_floor_time_ = now +
636 base::TimeDelta::FromMilliseconds(animationInterval);
637 // Set a timer to call us back after animationInterval before
[email protected]7c4329e2011-02-18 22:02:59638 // running animation callbacks so that if a callback requests another
639 // we'll be sure to run it at the proper time.
640 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
[email protected]bd37ae252011-06-03 01:28:18641 this, &RenderWidget::AnimationCallback), animationInterval);
[email protected]7c4329e2011-02-18 22:02:59642 animation_task_posted_ = true;
643 animation_update_pending_ = false;
[email protected]a5922cc2011-05-24 23:06:30644 webwidget_->animate(0.0);
[email protected]7c4329e2011-02-18 22:02:59645 return;
[email protected]5f8b1022011-01-21 23:34:50646 }
[email protected]bd37ae252011-06-03 01:28:18647 TRACE_EVENT0("renderer", "EarlyOut_AnimatedTooRecently");
[email protected]7c4329e2011-02-18 22:02:59648 if (animation_task_posted_)
649 return;
650 // This code uses base::Time::Now() to calculate the floor and next fire
651 // time because javascript's Date object uses base::Time::Now(). The
652 // message loop uses base::TimeTicks, which on windows can have a
653 // different granularity than base::Time.
654 // The upshot of all this is that this function might be called before
655 // base::Time::Now() has advanced past the animation_floor_time_. To
656 // avoid exposing this delay to javascript, we keep posting delayed
657 // tasks until base::Time::Now() has advanced far enough.
658 int64 delay = (animation_floor_time_ - now).InMillisecondsRoundedUp();
659 animation_task_posted_ = true;
660 MessageLoop::current()->PostDelayedTask(FROM_HERE,
661 NewRunnableMethod(this, &RenderWidget::AnimationCallback), delay);
[email protected]5f8b1022011-01-21 23:34:50662}
663
[email protected]bd37ae252011-06-03 01:28:18664bool RenderWidget::IsRenderingVSynced() {
665 // TODO(nduca): Forcing a driver to disable vsync (e.g. in a control panel) is
666 // not caught by this check. This will lead to artificially low frame rates
667 // for people who force vsync off at a driver level and expect Chrome to speed
668 // up.
669 return !has_disable_gpu_vsync_switch_;
670}
671
[email protected]65225772011-05-12 21:10:24672void RenderWidget::InvalidationCallback() {
[email protected]50312bf2011-06-22 23:30:06673 TRACE_EVENT0("renderer", "RenderWidget::InvalidationCallback");
[email protected]65225772011-05-12 21:10:24674 invalidation_task_posted_ = false;
675 DoDeferredUpdateAndSendInputAck();
676}
677
678void RenderWidget::DoDeferredUpdateAndSendInputAck() {
[email protected]52ccd0ea2011-02-16 01:09:05679 DoDeferredUpdate();
680
681 if (pending_input_event_ack_.get())
682 Send(pending_input_event_ack_.release());
[email protected]ee3d3ad2011-02-04 00:42:21683}
684
[email protected]552e6002009-11-19 05:24:57685void RenderWidget::DoDeferredUpdate() {
[email protected]366ae242011-05-10 02:23:58686 TRACE_EVENT0("renderer", "RenderWidget::DoDeferredUpdate");
[email protected]71e2f0a2011-03-15 22:25:08687
[email protected]65225772011-05-12 21:10:24688 if (!webwidget_)
initial.commit09911bf2008-07-26 23:55:29689 return;
[email protected]65225772011-05-12 21:10:24690 if (update_reply_pending()) {
691 TRACE_EVENT0("renderer", "EarlyOut_UpdateReplyPending");
692 return;
693 }
[email protected]9ca84622011-06-02 23:46:39694 if (is_accelerated_compositing_active_ &&
695 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending) {
[email protected]65225772011-05-12 21:10:24696 TRACE_EVENT0("renderer", "EarlyOut_MaxSwapBuffersPending");
697 return;
698 }
initial.commit09911bf2008-07-26 23:55:29699
[email protected]552e6002009-11-19 05:24:57700 // Suppress updating when we are hidden.
initial.commit09911bf2008-07-26 23:55:29701 if (is_hidden_ || size_.IsEmpty()) {
[email protected]552e6002009-11-19 05:24:57702 paint_aggregator_.ClearPendingUpdate();
initial.commit09911bf2008-07-26 23:55:29703 needs_repainting_on_restore_ = true;
[email protected]65225772011-05-12 21:10:24704 TRACE_EVENT0("renderer", "EarlyOut_NotVisible");
initial.commit09911bf2008-07-26 23:55:29705 return;
706 }
707
[email protected]0fb93f52011-05-18 23:13:56708 // Tracking of frame rate jitter
709 base::TimeTicks frame_begin_ticks = base::TimeTicks::Now();
[email protected]52ccd0ea2011-02-16 01:09:05710 AnimateIfNeeded();
[email protected]5f8b1022011-01-21 23:34:50711
[email protected]f98d7e3c2010-09-13 22:30:46712 // Layout may generate more invalidation. It may also enable the
713 // GPU acceleration, so make sure to run layout before we send the
714 // GpuRenderingActivated message.
715 webwidget_->layout();
716
[email protected]5f8b1022011-01-21 23:34:50717 // Suppress painting if nothing is dirty. This has to be done after updating
718 // animations running layout as these may generate further invalidations.
[email protected]65225772011-05-12 21:10:24719 if (!paint_aggregator_.HasPendingUpdate()) {
720 TRACE_EVENT0("renderer", "EarlyOut_NoPendingUpdate");
[email protected]5f8b1022011-01-21 23:34:50721 return;
[email protected]65225772011-05-12 21:10:24722 }
[email protected]5f8b1022011-01-21 23:34:50723
[email protected]872ae5b2011-05-26 20:20:50724 if (!last_do_deferred_update_time_.is_null()) {
[email protected]0fb93f52011-05-18 23:13:56725 base::TimeDelta delay = frame_begin_ticks - last_do_deferred_update_time_;
726 if(is_accelerated_compositing_active_)
727 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.AccelDoDeferredUpdateDelay",
728 delay,
729 base::TimeDelta::FromMilliseconds(1),
730 base::TimeDelta::FromMilliseconds(60),
731 30);
732 else
733 UMA_HISTOGRAM_CUSTOM_TIMES("Renderer4.SoftwareDoDeferredUpdateDelay",
734 delay,
735 base::TimeDelta::FromMilliseconds(1),
736 base::TimeDelta::FromMilliseconds(60),
737 30);
[email protected]872ae5b2011-05-26 20:20:50738
739 // Calculate filtered time per frame:
740 float frame_time_elapsed = static_cast<float>(delay.InSecondsF());
741 filtered_time_per_frame_ =
742 0.9f * filtered_time_per_frame_ + 0.1f * frame_time_elapsed;
[email protected]0fb93f52011-05-18 23:13:56743 }
744 last_do_deferred_update_time_ = frame_begin_ticks;
745
[email protected]552e6002009-11-19 05:24:57746 // OK, save the pending update to a local since painting may cause more
initial.commit09911bf2008-07-26 23:55:29747 // invalidation. Some WebCore rendering objects only layout when painted.
[email protected]dd015812010-12-06 23:39:30748 PaintAggregator::PendingUpdate update;
749 paint_aggregator_.PopPendingUpdate(&update);
initial.commit09911bf2008-07-26 23:55:29750
[email protected]53d3f302009-12-21 04:42:05751 gfx::Rect scroll_damage = update.GetScrollDamage();
752 gfx::Rect bounds = update.GetPaintBounds().Union(scroll_damage);
initial.commit09911bf2008-07-26 23:55:29753
[email protected]27543452011-03-25 00:14:00754 // Compositing the page may disable accelerated compositing.
755 bool accelerated_compositing_was_active = is_accelerated_compositing_active_;
756
[email protected]ca4847f2010-09-24 05:39:15757 // A plugin may be able to do an optimized paint. First check this, in which
758 // case we can skip all of the bitmap generation and regular paint code.
[email protected]719b36f2010-12-22 20:36:46759 // This optimization allows PPAPI plugins that declare themselves on top of
760 // the page (like a traditional windowed plugin) to be able to animate (think
761 // movie playing) without repeatedly re-painting the page underneath, or
762 // copying the plugin backing store (since we can send the plugin's backing
763 // store directly to the browser).
764 //
765 // This optimization only works when the entire invalid region is contained
766 // within the plugin. There is a related optimization in PaintRect for the
767 // case where there may be multiple invalid regions.
[email protected]2650bd52011-03-25 00:34:44768 TransportDIB::Id dib_id = TransportDIB::Id();
[email protected]ca4847f2010-09-24 05:39:15769 TransportDIB* dib = NULL;
[email protected]cef3362f2009-12-21 17:48:45770 std::vector<gfx::Rect> copy_rects;
[email protected]ca4847f2010-09-24 05:39:15771 gfx::Rect optimized_copy_rect, optimized_copy_location;
772 if (update.scroll_rect.IsEmpty() &&
[email protected]a79d8a632010-11-18 22:35:56773 !is_accelerated_compositing_active_ &&
[email protected]ca4847f2010-09-24 05:39:15774 GetBitmapForOptimizedPluginPaint(bounds, &dib, &optimized_copy_location,
775 &optimized_copy_rect)) {
[email protected]2df1b362011-01-21 21:22:27776 // Only update the part of the plugin that actually changed.
777 optimized_copy_rect = optimized_copy_rect.Intersect(bounds);
[email protected]ca4847f2010-09-24 05:39:15778 bounds = optimized_copy_location;
779 copy_rects.push_back(optimized_copy_rect);
780 dib_id = dib->id();
[email protected]a79d8a632010-11-18 22:35:56781 } else if (!is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:46782 // Compute a buffer for painting and cache it.
[email protected]ca4847f2010-09-24 05:39:15783 scoped_ptr<skia::PlatformCanvas> canvas(
[email protected]b4d08452010-10-05 17:34:35784 RenderProcess::current()->GetDrawingCanvas(&current_paint_buf_,
785 bounds));
[email protected]f98d7e3c2010-09-13 22:30:46786 if (!canvas.get()) {
787 NOTREACHED();
788 return;
789 }
[email protected]cef3362f2009-12-21 17:48:45790
[email protected]f98d7e3c2010-09-13 22:30:46791 // We may get back a smaller canvas than we asked for.
792 // TODO(darin): This seems like it could cause painting problems!
793 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
794 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
795 bounds.set_width(canvas->getDevice()->width());
796 bounds.set_height(canvas->getDevice()->height());
[email protected]53d3f302009-12-21 04:42:05797
[email protected]f98d7e3c2010-09-13 22:30:46798 HISTOGRAM_COUNTS_100("MPArch.RW_PaintRectCount", update.paint_rects.size());
799
[email protected]f98d7e3c2010-09-13 22:30:46800 // The scroll damage is just another rectangle to paint and copy.
801 copy_rects.swap(update.paint_rects);
802 if (!scroll_damage.IsEmpty())
803 copy_rects.push_back(scroll_damage);
804
805 for (size_t i = 0; i < copy_rects.size(); ++i)
806 PaintRect(copy_rects[i], bounds.origin(), canvas.get());
[email protected]ca4847f2010-09-24 05:39:15807
[email protected]b4d08452010-10-05 17:34:35808 dib_id = current_paint_buf_->id();
[email protected]f98d7e3c2010-09-13 22:30:46809 } else { // Accelerated compositing path
810 // Begin painting.
[email protected]50bd6452010-11-27 19:39:42811 webwidget_->composite(false);
[email protected]f98d7e3c2010-09-13 22:30:46812 }
813
814 // sending an ack to browser process that the paint is complete...
[email protected]53d3f302009-12-21 04:42:05815 ViewHostMsg_UpdateRect_Params params;
[email protected]36808ad2010-10-20 19:18:30816 params.bitmap = dib_id;
[email protected]53d3f302009-12-21 04:42:05817 params.bitmap_rect = bounds;
818 params.dx = update.scroll_delta.x();
819 params.dy = update.scroll_delta.y();
[email protected]27543452011-03-25 00:14:00820 if (accelerated_compositing_was_active) {
[email protected]b167ca662010-05-14 00:05:34821 // If painting is done via the gpu process then we clear out all damage
822 // rects to save the browser process from doing unecessary work.
823 params.scroll_rect = gfx::Rect();
824 params.copy_rects.clear();
825 } else {
826 params.scroll_rect = update.scroll_rect;
827 params.copy_rects.swap(copy_rects); // TODO(darin): clip to bounds?
828 }
[email protected]53d3f302009-12-21 04:42:05829 params.view_size = size_;
[email protected]e2356242010-11-16 22:33:03830 params.resizer_rect = resizer_rect_;
[email protected]53d3f302009-12-21 04:42:05831 params.plugin_window_moves.swap(plugin_window_moves_);
832 params.flags = next_paint_flags_;
[email protected]d54169e92011-01-21 09:19:52833 params.scroll_offset = GetScrollOffset();
[email protected]53d3f302009-12-21 04:42:05834
835 update_reply_pending_ = true;
836 Send(new ViewHostMsg_UpdateRect(routing_id_, params));
837 next_paint_flags_ = 0;
838
[email protected]fa7b1dc2010-06-23 17:53:04839 UpdateInputMethod();
[email protected]00c39612010-03-06 02:53:28840
841 // Let derived classes know we've painted.
842 DidInitiatePaint();
initial.commit09911bf2008-07-26 23:55:29843}
844
845///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:46846// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:29847
[email protected]4873c7d2009-07-16 06:36:28848void RenderWidget::didInvalidateRect(const WebRect& rect) {
[email protected]552e6002009-11-19 05:24:57849 // The invalidated rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48850 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57851 gfx::Rect damaged_rect = view_rect.Intersect(rect);
852 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29853 return;
854
[email protected]552e6002009-11-19 05:24:57855 paint_aggregator_.InvalidateRect(damaged_rect);
856
857 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24858 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57859 return;
860 if (!paint_aggregator_.HasPendingUpdate())
861 return;
[email protected]65225772011-05-12 21:10:24862 if (update_reply_pending() ||
863 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
864 return;
865
866 // When GPU rendering, combine pending animations and invalidations into
867 // a single update.
868 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57869 return;
870
871 // Perform updating asynchronously. This serves two purposes:
initial.commit09911bf2008-07-26 23:55:29872 // 1) Ensures that we call WebView::Paint without a bunch of other junk
873 // on the call stack.
874 // 2) Allows us to collect more damage rects before painting to help coalesce
875 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24876 invalidation_task_posted_ = true;
initial.commit09911bf2008-07-26 23:55:29877 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]65225772011-05-12 21:10:24878 this, &RenderWidget::InvalidationCallback));
initial.commit09911bf2008-07-26 23:55:29879}
880
[email protected]4873c7d2009-07-16 06:36:28881void RenderWidget::didScrollRect(int dx, int dy, const WebRect& clip_rect) {
[email protected]f98d7e3c2010-09-13 22:30:46882 // Drop scrolls on the floor when we are in compositing mode.
883 // TODO(nduca): stop WebViewImpl from sending scrolls in the first place.
[email protected]a79d8a632010-11-18 22:35:56884 if (is_accelerated_compositing_active_)
[email protected]f98d7e3c2010-09-13 22:30:46885 return;
886
[email protected]552e6002009-11-19 05:24:57887 // The scrolled rect might be outside the bounds of the view.
[email protected]ee8d6fd2010-05-26 17:05:48888 gfx::Rect view_rect(size_);
[email protected]552e6002009-11-19 05:24:57889 gfx::Rect damaged_rect = view_rect.Intersect(clip_rect);
890 if (damaged_rect.IsEmpty())
initial.commit09911bf2008-07-26 23:55:29891 return;
892
[email protected]552e6002009-11-19 05:24:57893 paint_aggregator_.ScrollRect(dx, dy, damaged_rect);
894
895 // We may not need to schedule another call to DoDeferredUpdate.
[email protected]65225772011-05-12 21:10:24896 if (invalidation_task_posted_)
[email protected]552e6002009-11-19 05:24:57897 return;
898 if (!paint_aggregator_.HasPendingUpdate())
899 return;
[email protected]65225772011-05-12 21:10:24900 if (update_reply_pending() ||
901 num_swapbuffers_complete_pending_ >= kMaxSwapBuffersPending)
902 return;
903
904 // When GPU rendering, combine pending animations and invalidations into
905 // a single update.
906 if (is_accelerated_compositing_active_ && animation_task_posted_)
[email protected]552e6002009-11-19 05:24:57907 return;
908
909 // Perform updating asynchronously. This serves two purposes:
910 // 1) Ensures that we call WebView::Paint without a bunch of other junk
911 // on the call stack.
912 // 2) Allows us to collect more damage rects before painting to help coalesce
913 // the work that we will need to do.
[email protected]65225772011-05-12 21:10:24914 invalidation_task_posted_ = true;
initial.commit09911bf2008-07-26 23:55:29915 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]65225772011-05-12 21:10:24916 this, &RenderWidget::InvalidationCallback));
initial.commit09911bf2008-07-26 23:55:29917}
918
[email protected]3da12cb2011-10-08 02:25:04919void RenderWidget::didActivateCompositor(int compositor_identifier) {
[email protected]ea162f92011-10-04 23:08:22920 TRACE_EVENT0("gpu", "RenderWidget::didActivateCompositor");
921
[email protected]3da12cb2011-10-08 02:25:04922 RenderThreadImpl::current()->compositor_thread()->
923 AddCompositor(routing_id_, compositor_identifier);
924
[email protected]ea162f92011-10-04 23:08:22925 is_accelerated_compositing_active_ = true;
[email protected]50bd6452010-11-27 19:39:42926 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
[email protected]65225772011-05-12 21:10:24927 routing_id_, is_accelerated_compositing_active_));
928
[email protected]c3d45532011-10-07 19:20:40929 // Note: asynchronous swapbuffer support currently only matters if
930 // compositing scheduling happens on the RenderWidget.
[email protected]ea162f92011-10-04 23:08:22931 using_asynchronous_swapbuffers_ = SupportsAsynchronousSwapBuffers();
[email protected]ea162f92011-10-04 23:08:22932}
933
934void RenderWidget::didDeactivateCompositor() {
935 TRACE_EVENT0("gpu", "RenderWidget::didDeactivateCompositor");
936
937 is_accelerated_compositing_active_ = false;
938 Send(new ViewHostMsg_DidActivateAcceleratedCompositing(
939 routing_id_, is_accelerated_compositing_active_));
940
[email protected]ea162f92011-10-04 23:08:22941 if (using_asynchronous_swapbuffers_)
[email protected]65225772011-05-12 21:10:24942 using_asynchronous_swapbuffers_ = false;
[email protected]a79d8a632010-11-18 22:35:56943}
944
[email protected]f98d7e3c2010-09-13 22:30:46945void RenderWidget::scheduleComposite() {
[email protected]c3d45532011-10-07 19:20:40946 if (WebWidgetHandlesCompositorScheduling())
947 webwidget_->composite(false);
948 else {
949 // TODO(nduca): replace with something a little less hacky. The reason this
950 // hack is still used is because the Invalidate-DoDeferredUpdate loop
951 // contains a lot of host-renderer synchronization logic that is still
952 // important for the accelerated compositing case. The option of simply
953 // duplicating all that code is less desirable than "faking out" the
954 // invalidation path using a magical damage rect.
955 didInvalidateRect(WebRect(0, 0, 1, 1));
956 }
[email protected]f98d7e3c2010-09-13 22:30:46957}
958
[email protected]5f8b1022011-01-21 23:34:50959void RenderWidget::scheduleAnimation() {
[email protected]921244e42011-07-20 16:36:30960 TRACE_EVENT0("gpu", "RenderWidget::scheduleAnimation");
[email protected]ee3d3ad2011-02-04 00:42:21961 if (!animation_update_pending_) {
962 animation_update_pending_ = true;
[email protected]52ccd0ea2011-02-16 01:09:05963 if (!animation_task_posted_) {
964 animation_task_posted_ = true;
965 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
966 this, &RenderWidget::AnimationCallback));
967 }
[email protected]ee3d3ad2011-02-04 00:42:21968 }
[email protected]5f8b1022011-01-21 23:34:50969}
970
[email protected]4873c7d2009-07-16 06:36:28971void RenderWidget::didChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:30972 // TODO(darin): Eliminate this temporary.
973 WebCursor cursor(cursor_info);
974
initial.commit09911bf2008-07-26 23:55:29975 // Only send a SetCursor message if we need to make a change.
976 if (!current_cursor_.IsEqual(cursor)) {
977 current_cursor_ = cursor;
978 Send(new ViewHostMsg_SetCursor(routing_id_, cursor));
979 }
980}
981
982// We are supposed to get a single call to Show for a newly created RenderWidget
983// that was created via RenderWidget::CreateWebView. So, we wait until this
984// point to dispatch the ShowWidget message.
985//
986// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:28987// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:29988//
[email protected]4873c7d2009-07-16 06:36:28989void RenderWidget::show(WebNavigationPolicy) {
initial.commit09911bf2008-07-26 23:55:29990 DCHECK(!did_show_) << "received extraneous Show call";
991 DCHECK(routing_id_ != MSG_ROUTING_NONE);
992 DCHECK(opener_id_ != MSG_ROUTING_NONE);
993
[email protected]8de12d942010-11-17 20:42:44994 if (did_show_)
995 return;
996
997 did_show_ = true;
998 // NOTE: initial_pos_ may still have its default values at this point, but
999 // that's okay. It'll be ignored if as_popup is false, or the browser
1000 // process will impose a default position otherwise.
1001 Send(new ViewHostMsg_ShowWidget(opener_id_, routing_id_, initial_pos_));
1002 SetPendingWindowRect(initial_pos_);
initial.commit09911bf2008-07-26 23:55:291003}
1004
[email protected]4873c7d2009-07-16 06:36:281005void RenderWidget::didFocus() {
initial.commit09911bf2008-07-26 23:55:291006}
1007
[email protected]4873c7d2009-07-16 06:36:281008void RenderWidget::didBlur() {
initial.commit09911bf2008-07-26 23:55:291009}
1010
[email protected]2533ce12009-05-09 00:02:241011void RenderWidget::DoDeferredClose() {
1012 Send(new ViewHostMsg_Close(routing_id_));
1013}
1014
[email protected]4873c7d2009-07-16 06:36:281015void RenderWidget::closeWidgetSoon() {
initial.commit09911bf2008-07-26 23:55:291016 // If a page calls window.close() twice, we'll end up here twice, but that's
1017 // OK. It is safe to send multiple Close messages.
1018
[email protected]2533ce12009-05-09 00:02:241019 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1020 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1021 // could be closed before the JS finishes executing. So instead, post a
1022 // message back to the message loop, which won't run until the JS is
1023 // complete, and then the Close message can be sent.
[email protected]75ae4492009-07-10 00:05:151024 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]2533ce12009-05-09 00:02:241025 this, &RenderWidget::DoDeferredClose));
initial.commit09911bf2008-07-26 23:55:291026}
1027
1028void RenderWidget::Close() {
1029 if (webwidget_) {
[email protected]4873c7d2009-07-16 06:36:281030 webwidget_->close();
initial.commit09911bf2008-07-26 23:55:291031 webwidget_ = NULL;
1032 }
1033}
1034
[email protected]4873c7d2009-07-16 06:36:281035WebRect RenderWidget::windowRect() {
1036 if (pending_window_rect_count_)
1037 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241038
[email protected]b3f2b912009-04-09 16:18:521039 gfx::Rect rect;
1040 Send(new ViewHostMsg_GetWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281041 return rect;
initial.commit09911bf2008-07-26 23:55:291042}
1043
[email protected]8a9d6ca32011-06-06 20:11:301044void RenderWidget::setToolTipText(const WebKit::WebString& text,
1045 WebTextDirection hint) {
[email protected]5a395b72011-08-08 19:13:541046 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint));
[email protected]8a9d6ca32011-06-06 20:11:301047}
1048
[email protected]4873c7d2009-07-16 06:36:281049void RenderWidget::setWindowRect(const WebRect& pos) {
initial.commit09911bf2008-07-26 23:55:291050 if (did_show_) {
1051 Send(new ViewHostMsg_RequestMove(routing_id_, pos));
[email protected]2533ce12009-05-09 00:02:241052 SetPendingWindowRect(pos);
initial.commit09911bf2008-07-26 23:55:291053 } else {
1054 initial_pos_ = pos;
1055 }
1056}
1057
[email protected]2533ce12009-05-09 00:02:241058void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1059 pending_window_rect_ = rect;
1060 pending_window_rect_count_++;
1061}
1062
[email protected]4873c7d2009-07-16 06:36:281063WebRect RenderWidget::rootWindowRect() {
[email protected]2533ce12009-05-09 00:02:241064 if (pending_window_rect_count_) {
1065 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1066 // the RootWindowRect is probably going to return wrong results since the
1067 // browser may not have processed the Move yet. There isn't really anything
1068 // good to do in this case, and it shouldn't happen - since this size is
1069 // only really needed for windowToScreen, which is only used for Popups.
[email protected]4873c7d2009-07-16 06:36:281070 return pending_window_rect_;
[email protected]2533ce12009-05-09 00:02:241071 }
1072
[email protected]b3f2b912009-04-09 16:18:521073 gfx::Rect rect;
1074 Send(new ViewHostMsg_GetRootWindowRect(routing_id_, host_window_, &rect));
[email protected]4873c7d2009-07-16 06:36:281075 return rect;
[email protected]d4547452008-08-28 18:36:371076}
1077
[email protected]4873c7d2009-07-16 06:36:281078WebRect RenderWidget::windowResizerRect() {
1079 return resizer_rect_;
[email protected]c04b6362008-11-21 18:54:191080}
1081
[email protected]fa7b1dc2010-06-23 17:53:041082void RenderWidget::OnSetInputMethodActive(bool is_active) {
[email protected]c4bb35a2008-10-31 17:54:031083 // To prevent this renderer process from sending unnecessary IPC messages to
1084 // a browser process, we permit the renderer process to send IPC messages
[email protected]fa7b1dc2010-06-23 17:53:041085 // only during the input method attached to the browser process is active.
1086 input_method_is_active_ = is_active;
initial.commit09911bf2008-07-26 23:55:291087}
1088
[email protected]fa7b1dc2010-06-23 17:53:041089void RenderWidget::OnImeSetComposition(
1090 const string16& text,
1091 const std::vector<WebCompositionUnderline>& underlines,
1092 int selection_start, int selection_end) {
[email protected]4873c7d2009-07-16 06:36:281093 if (!webwidget_)
1094 return;
[email protected]d4cff272011-05-02 15:46:011095 if (webwidget_->setComposition(
[email protected]fa7b1dc2010-06-23 17:53:041096 text, WebVector<WebCompositionUnderline>(underlines),
1097 selection_start, selection_end)) {
[email protected]d4cff272011-05-02 15:46:011098 // Setting the IME composition was successful. Send the new composition
1099 // range to the browser.
1100 ui::Range range(ui::Range::InvalidRange());
1101 size_t location, length;
1102 if (webwidget_->compositionRange(&location, &length)) {
1103 range.set_start(location);
1104 range.set_end(location + length);
1105 }
1106 // The IME was cancelled via the Esc key, so just send back the caret.
1107 else if (webwidget_->caretOrSelectionRange(&location, &length)) {
1108 range.set_start(location);
1109 range.set_end(location + length);
1110 }
1111 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
1112 } else {
[email protected]fa7b1dc2010-06-23 17:53:041113 // If we failed to set the composition text, then we need to let the browser
1114 // process to cancel the input method's ongoing composition session, to make
1115 // sure we are in a consistent state.
1116 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
[email protected]d4cff272011-05-02 15:46:011117
1118 // Send an updated IME range with just the caret range.
1119 ui::Range range(ui::Range::InvalidRange());
1120 size_t location, length;
1121 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1122 range.set_start(location);
1123 range.set_end(location + length);
1124 }
1125 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]7f00efa2010-04-15 05:01:261126 }
[email protected]fa7b1dc2010-06-23 17:53:041127}
1128
[email protected]4de6d1692011-10-12 08:45:441129void RenderWidget::OnImeConfirmComposition(
1130 const string16& text, const ui::Range& replacement_range) {
1131 if (webwidget_) {
1132 // TODO(thakis): Uncomment this once
1133 // https://bugs.webkit.org/show_bug.cgi?id=69846 has landed.
1134 // if (replacement_range.IsValid()) {
1135 // webwidget_->setSelectionOnFocusedFrame(replacement_range.start(),
1136 // replacement_range.end());
1137 // }
[email protected]6b349652011-01-05 18:36:241138 webwidget_->confirmComposition(text);
[email protected]4de6d1692011-10-12 08:45:441139 }
[email protected]d4cff272011-05-02 15:46:011140 // Send an updated IME range with just the caret range.
1141 ui::Range range(ui::Range::InvalidRange());
1142 size_t location, length;
1143 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1144 range.set_start(location);
1145 range.set_end(location + length);
1146 }
1147 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
initial.commit09911bf2008-07-26 23:55:291148}
1149
[email protected]948f7ab72010-05-28 23:48:081150// This message causes the renderer to render an image of the
1151// desired_size, regardless of whether the tab is hidden or not.
[email protected]d65adb12010-04-28 17:26:491152void RenderWidget::OnMsgPaintAtSize(const TransportDIB::Handle& dib_handle,
[email protected]c88c9442010-07-19 18:55:091153 int tag,
[email protected]948f7ab72010-05-28 23:48:081154 const gfx::Size& page_size,
[email protected]d65adb12010-04-28 17:26:491155 const gfx::Size& desired_size) {
[email protected]27543452011-03-25 00:14:001156 if (!webwidget_ || !TransportDIB::is_valid_handle(dib_handle)) {
1157 if (TransportDIB::is_valid_handle(dib_handle)) {
[email protected]45c6aad32010-11-11 04:46:251158 // Close our unused handle.
1159#if defined(OS_WIN)
1160 ::CloseHandle(dib_handle);
1161#elif defined(OS_MACOSX)
1162 base::SharedMemory::CloseHandle(dib_handle);
1163#endif
1164 }
[email protected]d65adb12010-04-28 17:26:491165 return;
[email protected]45c6aad32010-11-11 04:46:251166 }
[email protected]d65adb12010-04-28 17:26:491167
[email protected]948f7ab72010-05-28 23:48:081168 if (page_size.IsEmpty() || desired_size.IsEmpty()) {
[email protected]d65adb12010-04-28 17:26:491169 // If one of these is empty, then we just return the dib we were
1170 // given, to avoid leaking it.
[email protected]c88c9442010-07-19 18:55:091171 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, desired_size));
[email protected]d65adb12010-04-28 17:26:491172 return;
1173 }
1174
1175 // Map the given DIB ID into this process, and unmap it at the end
1176 // of this function.
[email protected]45c6aad32010-11-11 04:46:251177 scoped_ptr<TransportDIB> paint_at_size_buffer(
1178 TransportDIB::CreateWithHandle(dib_handle));
[email protected]36808ad2010-10-20 19:18:301179
1180 gfx::Size canvas_size = page_size;
[email protected]d65adb12010-04-28 17:26:491181 float x_scale = static_cast<float>(desired_size.width()) /
1182 static_cast<float>(canvas_size.width());
1183 float y_scale = static_cast<float>(desired_size.height()) /
1184 static_cast<float>(canvas_size.height());
1185
[email protected]ee8d6fd2010-05-26 17:05:481186 gfx::Rect orig_bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491187 canvas_size.set_width(static_cast<int>(canvas_size.width() * x_scale));
1188 canvas_size.set_height(static_cast<int>(canvas_size.height() * y_scale));
[email protected]ee8d6fd2010-05-26 17:05:481189 gfx::Rect bounds(canvas_size);
[email protected]d65adb12010-04-28 17:26:491190
[email protected]36808ad2010-10-20 19:18:301191 scoped_ptr<skia::PlatformCanvas> canvas(
1192 paint_at_size_buffer->GetPlatformCanvas(canvas_size.width(),
1193 canvas_size.height()));
1194 if (!canvas.get()) {
1195 NOTREACHED();
1196 return;
1197 }
1198
[email protected]d65adb12010-04-28 17:26:491199 // Reset bounds to what we actually received, but they should be the
1200 // same.
1201 DCHECK_EQ(bounds.width(), canvas->getDevice()->width());
1202 DCHECK_EQ(bounds.height(), canvas->getDevice()->height());
1203 bounds.set_width(canvas->getDevice()->width());
1204 bounds.set_height(canvas->getDevice()->height());
1205
1206 canvas->save();
[email protected]948f7ab72010-05-28 23:48:081207 // Add the scale factor to the canvas, so that we'll get the desired size.
[email protected]d65adb12010-04-28 17:26:491208 canvas->scale(SkFloatToScalar(x_scale), SkFloatToScalar(y_scale));
1209
[email protected]948f7ab72010-05-28 23:48:081210 // Have to make sure we're laid out at the right size before
1211 // rendering.
1212 gfx::Size old_size = webwidget_->size();
1213 webwidget_->resize(page_size);
1214 webwidget_->layout();
1215
[email protected]d65adb12010-04-28 17:26:491216 // Paint the entire thing (using original bounds, not scaled bounds).
1217 PaintRect(orig_bounds, orig_bounds.origin(), canvas.get());
1218 canvas->restore();
1219
[email protected]948f7ab72010-05-28 23:48:081220 // Return the widget to its previous size.
1221 webwidget_->resize(old_size);
1222
[email protected]c88c9442010-07-19 18:55:091223 Send(new ViewHostMsg_PaintAtSize_ACK(routing_id_, tag, bounds.size()));
[email protected]d65adb12010-04-28 17:26:491224}
1225
[email protected]ec7dc112008-08-06 05:30:121226void RenderWidget::OnMsgRepaint(const gfx::Size& size_to_paint) {
1227 // During shutdown we can just ignore this message.
1228 if (!webwidget_)
1229 return;
1230
1231 set_next_paint_is_repaint_ack();
[email protected]a79d8a632010-11-18 22:35:561232 if (is_accelerated_compositing_active_) {
[email protected]f98d7e3c2010-09-13 22:30:461233 scheduleComposite();
[email protected]f98d7e3c2010-09-13 22:30:461234 } else {
1235 gfx::Rect repaint_rect(size_to_paint.width(), size_to_paint.height());
1236 didInvalidateRect(repaint_rect);
1237 }
[email protected]ec7dc112008-08-06 05:30:121238}
1239
[email protected]4873c7d2009-07-16 06:36:281240void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
[email protected]07f953332009-03-25 04:31:111241 if (!webwidget_)
1242 return;
[email protected]4873c7d2009-07-16 06:36:281243 webwidget_->setTextDirection(direction);
[email protected]07f953332009-03-25 04:31:111244}
1245
[email protected]719b36f2010-12-22 20:36:461246webkit::ppapi::PluginInstance* RenderWidget::GetBitmapForOptimizedPluginPaint(
[email protected]ca4847f2010-09-24 05:39:151247 const gfx::Rect& paint_bounds,
1248 TransportDIB** dib,
1249 gfx::Rect* location,
1250 gfx::Rect* clip) {
[email protected]719b36f2010-12-22 20:36:461251 // Bare RenderWidgets don't support optimized plugin painting.
1252 return NULL;
[email protected]ca4847f2010-09-24 05:39:151253}
1254
[email protected]bcaf2272011-02-15 15:29:431255gfx::Point RenderWidget::GetScrollOffset() {
[email protected]d54169e92011-01-21 09:19:521256 // Bare RenderWidgets don't support scroll offset.
[email protected]bcaf2272011-02-15 15:29:431257 return gfx::Point(0, 0);
[email protected]d54169e92011-01-21 09:19:521258}
1259
[email protected]bee16aab2009-08-26 15:55:031260void RenderWidget::SetHidden(bool hidden) {
1261 if (is_hidden_ == hidden)
1262 return;
1263
1264 // The status has changed. Tell the RenderThread about it.
1265 is_hidden_ = hidden;
1266 if (is_hidden_)
[email protected]380244092011-10-07 17:26:271267 RenderThread::Get()->WidgetHidden();
[email protected]bee16aab2009-08-26 15:55:031268 else
[email protected]380244092011-10-07 17:26:271269 RenderThread::Get()->WidgetRestored();
[email protected]bee16aab2009-08-26 15:55:031270}
1271
[email protected]699ab0d2009-04-23 23:19:141272void RenderWidget::SetBackground(const SkBitmap& background) {
1273 background_ = background;
[email protected]f98d7e3c2010-09-13 22:30:461274
[email protected]699ab0d2009-04-23 23:19:141275 // Generate a full repaint.
[email protected]4873c7d2009-07-16 06:36:281276 didInvalidateRect(gfx::Rect(size_.width(), size_.height()));
[email protected]699ab0d2009-04-23 23:19:141277}
1278
[email protected]674741932009-02-04 23:44:461279bool RenderWidget::next_paint_is_resize_ack() const {
[email protected]53d3f302009-12-21 04:42:051280 return ViewHostMsg_UpdateRect_Flags::is_resize_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461281}
1282
1283bool RenderWidget::next_paint_is_restore_ack() const {
[email protected]53d3f302009-12-21 04:42:051284 return ViewHostMsg_UpdateRect_Flags::is_restore_ack(next_paint_flags_);
[email protected]674741932009-02-04 23:44:461285}
1286
1287void RenderWidget::set_next_paint_is_resize_ack() {
[email protected]53d3f302009-12-21 04:42:051288 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
[email protected]674741932009-02-04 23:44:461289}
1290
1291void RenderWidget::set_next_paint_is_restore_ack() {
[email protected]53d3f302009-12-21 04:42:051292 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_RESTORE_ACK;
[email protected]674741932009-02-04 23:44:461293}
1294
1295void RenderWidget::set_next_paint_is_repaint_ack() {
[email protected]53d3f302009-12-21 04:42:051296 next_paint_flags_ |= ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
[email protected]674741932009-02-04 23:44:461297}
1298
[email protected]fa7b1dc2010-06-23 17:53:041299void RenderWidget::UpdateInputMethod() {
1300 if (!input_method_is_active_)
initial.commit09911bf2008-07-26 23:55:291301 return;
[email protected]fa7b1dc2010-06-23 17:53:041302
[email protected]ad26ef42011-06-17 07:59:451303 ui::TextInputType new_type = GetTextInputType();
1304 bool new_can_compose_inline = CanComposeInline();
[email protected]73bf95812011-10-12 11:38:321305 WebRect new_caret_bounds = GetCaretBounds();
[email protected]fa7b1dc2010-06-23 17:53:041306
1307 // Only sends text input type and caret bounds to the browser process if they
1308 // are changed.
[email protected]ad26ef42011-06-17 07:59:451309 if (text_input_type_ != new_type || caret_bounds_ != new_caret_bounds ||
1310 can_compose_inline_ != new_can_compose_inline) {
[email protected]fa7b1dc2010-06-23 17:53:041311 text_input_type_ = new_type;
[email protected]ad26ef42011-06-17 07:59:451312 can_compose_inline_ = new_can_compose_inline;
[email protected]fa7b1dc2010-06-23 17:53:041313 caret_bounds_ = new_caret_bounds;
1314 Send(new ViewHostMsg_ImeUpdateTextInputState(
[email protected]ad26ef42011-06-17 07:59:451315 routing_id(), new_type, new_can_compose_inline, new_caret_bounds));
initial.commit09911bf2008-07-26 23:55:291316 }
initial.commit09911bf2008-07-26 23:55:291317}
1318
[email protected]73bf95812011-10-12 11:38:321319gfx::Rect RenderWidget::GetCaretBounds() {
1320 if (!webwidget_)
1321 return gfx::Rect();
1322 return webwidget_->caretOrSelectionBounds();
1323}
1324
1325// Check WebKit::WebTextInputType and ui::TextInputType is kept in sync.
[email protected]ad26ef42011-06-17 07:59:451326COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \
1327 int(ui::TEXT_INPUT_TYPE_NONE), mismatching_enums);
1328COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \
1329 int(ui::TEXT_INPUT_TYPE_TEXT), mismatching_enums);
1330COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \
1331 int(ui::TEXT_INPUT_TYPE_PASSWORD), mismatching_enums);
[email protected]caf38ed2011-07-28 13:15:181332COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \
1333 int(ui::TEXT_INPUT_TYPE_SEARCH), mismatching_enums);
1334COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \
1335 int(ui::TEXT_INPUT_TYPE_EMAIL), mismatching_enums);
1336COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \
1337 int(ui::TEXT_INPUT_TYPE_NUMBER), mismatching_enums);
1338COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \
1339 int(ui::TEXT_INPUT_TYPE_TELEPHONE), mismatching_enums);
1340COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \
1341 int(ui::TEXT_INPUT_TYPE_URL), mismatching_enums);
[email protected]ad26ef42011-06-17 07:59:451342
1343ui::TextInputType RenderWidget::GetTextInputType() {
1344 if (webwidget_) {
1345 int type = webwidget_->textInputType();
1346 // Check the type is in the range representable by ui::TextInputType.
[email protected]caf38ed2011-07-28 13:15:181347 DCHECK(type <= ui::TEXT_INPUT_TYPE_URL) <<
[email protected]ad26ef42011-06-17 07:59:451348 "WebKit::WebTextInputType and ui::TextInputType not synchronized";
1349 return static_cast<ui::TextInputType>(type);
1350 }
1351 return ui::TEXT_INPUT_TYPE_NONE;
1352}
1353
1354bool RenderWidget::CanComposeInline() {
1355 return true;
[email protected]56ea1a62011-05-30 07:05:571356}
1357
[email protected]4873c7d2009-07-16 06:36:281358WebScreenInfo RenderWidget::screenInfo() {
1359 WebScreenInfo results;
1360 Send(new ViewHostMsg_GetScreenInfo(routing_id_, host_window_, &results));
1361 return results;
1362}
1363
[email protected]fa7b1dc2010-06-23 17:53:041364void RenderWidget::resetInputMethod() {
1365 if (!input_method_is_active_)
1366 return;
1367
1368 // If the last text input type is not None, then we should finish any
1369 // ongoing composition regardless of the new text input type.
[email protected]ad26ef42011-06-17 07:59:451370 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) {
[email protected]fa7b1dc2010-06-23 17:53:041371 // If a composition text exists, then we need to let the browser process
1372 // to cancel the input method's ongoing composition session.
1373 if (webwidget_->confirmComposition())
1374 Send(new ViewHostMsg_ImeCancelComposition(routing_id()));
1375 }
[email protected]d4cff272011-05-02 15:46:011376
1377 // Send an updated IME range with the current caret rect.
1378 ui::Range range(ui::Range::InvalidRange());
1379 size_t location, length;
1380 if (webwidget_->caretOrSelectionRange(&location, &length)) {
1381 range.set_start(location);
1382 range.set_end(location + length);
1383 }
1384 Send(new ViewHostMsg_ImeCompositionRangeChanged(routing_id(), range));
[email protected]fa7b1dc2010-06-23 17:53:041385}
1386
[email protected]f103ab72009-09-02 17:10:591387void RenderWidget::SchedulePluginMove(
[email protected]191eb3f72010-12-21 06:27:501388 const webkit::npapi::WebPluginGeometry& move) {
initial.commit09911bf2008-07-26 23:55:291389 size_t i = 0;
1390 for (; i < plugin_window_moves_.size(); ++i) {
1391 if (plugin_window_moves_[i].window == move.window) {
[email protected]16f89d02009-08-26 17:17:581392 if (move.rects_valid) {
1393 plugin_window_moves_[i] = move;
1394 } else {
1395 plugin_window_moves_[i].visible = move.visible;
1396 }
initial.commit09911bf2008-07-26 23:55:291397 break;
1398 }
1399 }
1400
1401 if (i == plugin_window_moves_.size())
1402 plugin_window_moves_.push_back(move);
1403}
[email protected]268654772009-08-06 23:02:041404
1405void RenderWidget::CleanupWindowInPluginMoves(gfx::PluginWindowHandle window) {
1406 for (WebPluginGeometryVector::iterator i = plugin_window_moves_.begin();
1407 i != plugin_window_moves_.end(); ++i) {
1408 if (i->window == window) {
1409 plugin_window_moves_.erase(i);
1410 break;
1411 }
1412 }
1413}
[email protected]67bfb83f2011-09-22 03:36:371414
1415bool RenderWidget::WillHandleMouseEvent(const WebKit::WebMouseEvent& event) {
1416 return false;
1417}
[email protected]c3d45532011-10-07 19:20:401418
1419bool RenderWidget::WebWidgetHandlesCompositorScheduling() const {
1420 return false;
1421}