1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_RENDER_WIDGET_H_
6 #define CONTENT_RENDERER_RENDER_WIDGET_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time.h"
16 #include "base/timer.h"
17 #include "cc/rendering_stats.h"
18 #include "content/common/content_export.h"
19 #include "content/renderer/paint_aggregator.h"
20 #include "ipc/ipc_listener.h"
21 #include "ipc/ipc_sender.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCompositionUnderline.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupType.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextInputInfo.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWidgetClient.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
28 #include "third_party/skia/include/core/SkBitmap.h"
29 #include "ui/base/ime/text_input_type.h"
30 #include "ui/base/range/range.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "ui/gfx/rect.h"
33 #include "ui/gfx/vector2d.h"
34 #include "ui/surface/transport_dib.h"
35 #include "webkit/glue/webcursor.h"
37 struct ViewHostMsg_UpdateRect_Params
;
38 class ViewHostMsg_UpdateRect
;
45 class WebGestureEvent
;
48 struct WebRenderingStatsImpl
;
59 struct WebPluginGeometry
;
68 struct GpuRenderingStats
;
69 class RenderWidgetTest
;
71 // RenderWidget provides a communication bridge between a WebWidget and
72 // a RenderWidgetHost, the latter of which lives in a different process.
73 class CONTENT_EXPORT RenderWidget
74 : public IPC::Listener
,
76 NON_EXPORTED_BASE(virtual public WebKit::WebWidgetClient
),
77 public base::RefCounted
<RenderWidget
> {
79 // Creates a new RenderWidget. The opener_id is the routing ID of the
80 // RenderView that this widget lives inside.
81 static RenderWidget
* Create(int32 opener_id
,
82 WebKit::WebPopupType popup_type
,
83 const WebKit::WebScreenInfo
& screen_info
);
85 // Creates a WebWidget based on the popup type.
86 static WebKit::WebWidget
* CreateWebWidget(RenderWidget
* render_widget
);
88 // The compositing surface assigned by the RenderWidgetHost
89 // (or RenderViewHost). Will be gfx::kNullPluginWindow if not assigned yet,
90 // in which case we should not create any GPU command buffers with it.
91 // The routing ID assigned by the RenderProcess. Will be MSG_ROUTING_NONE if
92 // not yet assigned a view ID, in which case, the process MUST NOT send
93 // messages with this ID to the parent.
94 int32
routing_id() const {
98 int32
surface_id() const {
102 // May return NULL when the window is closing.
103 WebKit::WebWidget
* webwidget() const { return webwidget_
; }
105 gfx::Size
size() const { return size_
; }
106 bool has_focus() const { return has_focus_
; }
107 bool is_fullscreen() const { return is_fullscreen_
; }
108 bool is_hidden() const { return is_hidden_
; }
111 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
114 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
116 // WebKit::WebWidgetClient
117 virtual void willBeginCompositorFrame();
118 virtual void didInvalidateRect(const WebKit::WebRect
&);
119 virtual void didScrollRect(int dx
, int dy
,
120 const WebKit::WebRect
& clipRect
);
121 virtual void didAutoResize(const WebKit::WebSize
& new_size
);
122 virtual void didActivateCompositor(int input_handler_identifier
);
123 virtual void didDeactivateCompositor();
124 virtual void didBecomeReadyForAdditionalInput();
125 virtual void didCommitAndDrawCompositorFrame();
126 virtual void didCompleteSwapBuffers();
127 virtual void scheduleComposite();
128 virtual void scheduleAnimation();
129 virtual void didFocus();
130 virtual void didBlur();
131 virtual void didChangeCursor(const WebKit::WebCursorInfo
&);
132 virtual void closeWidgetSoon();
133 virtual void show(WebKit::WebNavigationPolicy
);
134 virtual void runModal() {}
135 virtual WebKit::WebRect
windowRect();
136 virtual void setToolTipText(const WebKit::WebString
& text
,
137 WebKit::WebTextDirection hint
);
138 virtual void setWindowRect(const WebKit::WebRect
&);
139 virtual WebKit::WebRect
windowResizerRect();
140 virtual WebKit::WebRect
rootWindowRect();
141 virtual WebKit::WebScreenInfo
screenInfo();
142 virtual float deviceScaleFactor();
143 virtual void resetInputMethod();
145 // Called when a plugin is moved. These events are queued up and sent with
146 // the next paint or scroll message to the host.
147 void SchedulePluginMove(const webkit::npapi::WebPluginGeometry
& move
);
149 // Called when a plugin window has been destroyed, to make sure the currently
150 // pending moves don't try to reference it.
151 void CleanupWindowInPluginMoves(gfx::PluginWindowHandle window
);
153 // Fills in a WebRenderingStatsImpl struct containing information about
154 // rendering, e.g. count of frames rendered, time spent painting.
155 // This call is relatively expensive in threaded compositing mode,
156 // as it blocks on the compositor thread.
157 void GetRenderingStats(WebKit::WebRenderingStatsImpl
&) const;
159 // Fills in a GpuRenderingStats struct containing information about
160 // GPU rendering, e.g. count of texture uploads performed, time spent
162 // This call is relatively expensive as it blocks on the GPU process
163 bool GetGpuRenderingStats(GpuRenderingStats
*) const;
165 // Callback for use with BeginSmoothScroll.
166 typedef base::Callback
<void()> SmoothScrollCompletionCallback
;
168 // Directs the host to begin a smooth scroll. This scroll should have the same
169 // performance characteristics as a user-initiated scroll. Returns an ID of
170 // the scroll gesture.
171 void BeginSmoothScroll(bool scroll_down
,
172 const SmoothScrollCompletionCallback
& callback
,
173 int pixels_to_scroll
,
177 // Close the underlying WebWidget.
178 virtual void Close();
180 float filtered_time_per_frame() const {
181 return filtered_time_per_frame_
;
190 // Friend RefCounted so that the dtor can be non-public. Using this class
191 // without ref-counting is an error.
192 friend class base::RefCounted
<RenderWidget
>;
194 friend class RenderWidgetTest
;
201 RenderWidget(WebKit::WebPopupType popup_type
,
202 const WebKit::WebScreenInfo
& screen_info
,
205 virtual ~RenderWidget();
207 // Initializes this view with the given opener. CompleteInit must be called
209 bool Init(int32 opener_id
);
211 // Called by Init and subclasses to perform initialization.
212 bool DoInit(int32 opener_id
,
213 WebKit::WebWidget
* web_widget
,
214 IPC::SyncMessage
* create_widget_message
);
216 // Finishes creation of a pending view started with Init.
219 // Sets whether this RenderWidget has been swapped out to be displayed by
220 // a RenderWidget in a different process. If so, no new IPC messages will be
221 // sent (only ACKs) and the process is free to exit when there are no other
222 // active RenderWidgets.
223 void SetSwappedOut(bool is_swapped_out
);
225 // Paints the given rectangular region of the WebWidget into canvas (a
226 // shared memory segment returned by AllocPaintBuf on Windows). The caller
227 // must ensure that the given rect fits within the bounds of the WebWidget.
228 void PaintRect(const gfx::Rect
& rect
, const gfx::Point
& canvas_origin
,
231 // Paints a border at the given rect for debugging purposes.
232 void PaintDebugBorder(const gfx::Rect
& rect
, SkCanvas
* canvas
);
234 bool IsRenderingVSynced();
235 void AnimationCallback();
236 void AnimateIfNeeded();
237 void InvalidationCallback();
238 void DoDeferredUpdateAndSendInputAck();
239 void DoDeferredUpdate();
240 void DoDeferredClose();
241 void DoDeferredSetWindowRect(const WebKit::WebRect
& pos
);
243 // Set the background of the render widget to a bitmap. The bitmap will be
244 // tiled in both directions if it isn't big enough to fill the area. This is
245 // mainly intended to be used in conjuction with WebView::SetIsTransparent().
246 virtual void SetBackground(const SkBitmap
& bitmap
);
248 // Resizes the render widget.
249 void Resize(const gfx::Size
& new_size
,
250 const gfx::Rect
& resizer_rect
,
252 ResizeAck resize_ack
);
254 // RenderWidget IPC message handlers
256 void OnCreatingNewAck();
257 virtual void OnResize(const gfx::Size
& new_size
,
258 const gfx::Rect
& resizer_rect
,
260 void OnChangeResizeRect(const gfx::Rect
& resizer_rect
);
261 virtual void OnWasHidden();
262 virtual void OnWasShown(bool needs_repainting
);
263 virtual void OnWasSwappedOut();
264 void OnUpdateRectAck();
265 void OnCreateVideoAck(int32 video_id
);
266 void OnUpdateVideoAck(int32 video_id
);
267 void OnRequestMoveAck();
268 void OnHandleInputEvent(const WebKit::WebInputEvent
* event
,
269 bool keyboard_shortcut
);
270 void OnMouseCaptureLost();
271 virtual void OnSetFocus(bool enable
);
272 void OnSetInputMethodActive(bool is_active
);
273 virtual void OnImeSetComposition(
274 const string16
& text
,
275 const std::vector
<WebKit::WebCompositionUnderline
>& underlines
,
278 virtual void OnImeConfirmComposition(
279 const string16
& text
, const ui::Range
& replacement_range
);
280 void OnMsgPaintAtSize(const TransportDIB::Handle
& dib_id
,
282 const gfx::Size
& page_size
,
283 const gfx::Size
& desired_size
);
284 void OnMsgRepaint(const gfx::Size
& size_to_paint
);
285 void OnMsgSmoothScrollCompleted(int gesture_id
);
286 void OnSetTextDirection(WebKit::WebTextDirection direction
);
288 void OnScreenInfoChanged(const WebKit::WebScreenInfo
& screen_info
);
289 void OnUpdateScreenRects(const gfx::Rect
& view_screen_rect
,
290 const gfx::Rect
& window_screen_rect
);
292 virtual void SetDeviceScaleFactor(float device_scale_factor
);
294 // Override points to notify derived classes that a paint has happened.
295 // WillInitiatePaint happens when we're about to generate a new bitmap and
296 // send it to the browser. DidInitiatePaint happens when that has completed,
297 // and subsequent rendering won't affect the painted content. DidFlushPaint
298 // happens once we've received the ACK that the screen has been updated.
299 // For a given paint operation, these overrides will always be called in the
300 // order WillInitiatePaint, DidInitiatePaint, DidFlushPaint.
301 virtual void WillInitiatePaint() {}
302 virtual void DidInitiatePaint() {}
303 virtual void DidFlushPaint() {}
305 // Override and return true when the widget is rendered with a graphics
306 // context that supports asynchronous swapbuffers. When returning true, the
307 // subclass must call OnSwapBuffersPosted() when swap is posted,
308 // OnSwapBuffersComplete() when swaps complete, and OnSwapBuffersAborted if
309 // the context is lost.
310 virtual bool SupportsAsynchronousSwapBuffers();
312 virtual bool ForceCompositingModeEnabled();
314 // Notifies scheduler that the RenderWidget's subclass has finished or aborted
316 void OnSwapBuffersPosted();
317 void OnSwapBuffersComplete();
318 void OnSwapBuffersAborted();
320 // Detects if a suitable opaque plugin covers the given paint bounds with no
321 // compositing necessary.
323 // Returns the plugin instance that's the source of the paint if the paint
324 // can be handled by just blitting the plugin bitmap. In this case, the
325 // location, clipping, and ID of the backing store will be filled into the
326 // given output parameters.
328 // A return value of null means optimized painting can not be used and we
329 // should continue with the normal painting code path.
330 virtual webkit::ppapi::PluginInstance
* GetBitmapForOptimizedPluginPaint(
331 const gfx::Rect
& paint_bounds
,
335 float* scale_factor
);
337 // Gets the scroll offset of this widget, if this widget has a notion of
339 virtual gfx::Vector2d
GetScrollOffset();
341 // Sets the "hidden" state of this widget. All accesses to is_hidden_ should
342 // use this method so that we can properly inform the RenderThread of our
344 void SetHidden(bool hidden
);
346 void WillToggleFullscreen();
347 void DidToggleFullscreen();
349 bool next_paint_is_resize_ack() const;
350 bool next_paint_is_restore_ack() const;
351 void set_next_paint_is_resize_ack();
352 void set_next_paint_is_restore_ack();
353 void set_next_paint_is_repaint_ack();
355 void set_throttle_input_events(bool throttle_input_events
) {
356 throttle_input_events_
= throttle_input_events
;
359 // Checks if the text input state and compose inline mode have been changed.
360 // If they are changed, the new value will be sent to the browser process.
361 // |show_ime_if_needed| should be SHOW_IME_IF_NEEDED iff the update may cause
362 // the ime to be displayed, e.g. after a tap on an input field on mobile.
363 void UpdateTextInputState(ShowIme show_ime
);
365 // Checks if the selection bounds have been changed. If they are changed,
366 // the new value will be sent to the browser process.
367 virtual void UpdateSelectionBounds();
369 // Checks if the composition range or composition character bounds have been
370 // changed. If they are changed, the new value will be sent to the browser
372 virtual void UpdateCompositionInfo(
373 const ui::Range
& range
,
374 const std::vector
<gfx::Rect
>& character_bounds
);
376 // Override point to obtain that the current input method state and caret
378 virtual ui::TextInputType
GetTextInputType();
379 virtual void GetSelectionBounds(gfx::Rect
* start
, gfx::Rect
* end
);
380 virtual ui::TextInputType
WebKitToUiTextInputType(
381 WebKit::WebTextInputType type
);
383 // Override point to obtain that the current composition character bounds.
384 // In the case of surrogate pairs, the character is treated as two characters:
385 // the bounds for first character is actual one, and the bounds for second
386 // character is zero width rectangle.
387 virtual void GetCompositionCharacterBounds(
388 std::vector
<gfx::Rect
>* character_bounds
);
390 // Returns true if the composition range or composition character bounds
391 // should be sent to the browser process.
392 bool ShouldUpdateCompositionInfo(
393 const ui::Range
& range
,
394 const std::vector
<gfx::Rect
>& bounds
);
396 // Override point to obtain that the current input method state about
398 virtual bool CanComposeInline();
400 // Tells the renderer it does not have focus. Used to prevent us from getting
401 // the focus on our own when the browser did not focus us.
404 // Set the pending window rect.
405 // Because the real render_widget is hosted in another process, there is
406 // a time period where we may have set a new window rect which has not yet
407 // been processed by the browser. So we maintain a pending window rect
408 // size. If JS code sets the WindowRect, and then immediately calls
409 // GetWindowRect() we'll use this pending window rect as the size.
410 void SetPendingWindowRect(const WebKit::WebRect
& r
);
412 // Called by OnHandleInputEvent() to notify subclasses that a key event was
414 virtual void DidHandleKeyEvent() {}
416 // Called by OnHandleInputEvent() to notify subclasses that a mouse event is
417 // about to be handled.
418 // Returns true if no further handling is needed. In that case, the event
419 // won't be sent to WebKit or trigger DidHandleMouseEvent().
420 virtual bool WillHandleMouseEvent(const WebKit::WebMouseEvent
& event
);
422 // Called by OnHandleInputEvent() to notify subclasses that a gesture event is
423 // about to be handled.
424 // Returns true if no further handling is needed. In that case, the event
425 // won't be sent to WebKit.
426 virtual bool WillHandleGestureEvent(const WebKit::WebGestureEvent
& event
);
428 // Called by OnHandleInputEvent() to notify subclasses that a mouse event was
430 virtual void DidHandleMouseEvent(const WebKit::WebMouseEvent
& event
) {}
432 // Called by OnHandleInputEvent() to notify subclasses that a touch event was
434 virtual void DidHandleTouchEvent(const WebKit::WebTouchEvent
& event
) {}
436 // Check whether the WebWidget has any touch event handlers registered
437 // at the given point.
438 virtual bool HasTouchEventHandlersAt(const gfx::Point
& point
) const;
440 // Should return true if the underlying WebWidget is responsible for
441 // the scheduling of compositing requests.
442 virtual bool WebWidgetHandlesCompositorScheduling() const;
444 // Routing ID that allows us to communicate to the parent browser process
445 // RenderWidgetHost. When MSG_ROUTING_NONE, no messages may be sent.
450 // We are responsible for destroying this object via its Close method.
451 WebKit::WebWidget
* webwidget_
;
453 // Set to the ID of the view that initiated creating this view, if any. When
454 // the view was initiated by the browser (the common case), this will be
455 // MSG_ROUTING_NONE. This is used in determining ownership when opening
456 // child tabs. See RenderWidget::createWebViewWithRequest.
458 // This ID may refer to an invalid view if that view is closed before this
462 // The position where this view should be initially shown.
463 gfx::Rect initial_pos_
;
467 // We store the current cursor object so we can avoid spamming SetCursor
469 WebCursor current_cursor_
;
471 // The size of the RenderWidget.
474 // The TransportDIB that is being used to transfer an image to the browser.
475 TransportDIB
* current_paint_buf_
;
477 PaintAggregator paint_aggregator_
;
479 // The area that must be reserved for drawing the resize corner.
480 gfx::Rect resizer_rect_
;
482 // Flags for the next ViewHostMsg_UpdateRect message.
483 int next_paint_flags_
;
485 // Filtered time per frame based on UpdateRect messages.
486 float filtered_time_per_frame_
;
488 // True if we are expecting an UpdateRect_ACK message (i.e., that a
489 // UpdateRect message has been sent).
490 bool update_reply_pending_
;
492 // True if we need to send an UpdateRect message to notify the browser about
493 // an already-completed auto-resize.
494 bool need_update_rect_for_auto_resize_
;
496 // True if the underlying graphics context supports asynchronous swap.
497 // Cached on the RenderWidget because determining support is costly.
498 bool using_asynchronous_swapbuffers_
;
500 // Number of OnSwapBuffersComplete we are expecting. Incremented each time
501 // WebWidget::composite has been been performed when the RenderWidget subclass
502 // SupportsAsynchronousSwapBuffers. Decremented in OnSwapBuffers. Will block
504 int num_swapbuffers_complete_pending_
;
506 // When accelerated rendering is on, is the maximum number of swapbuffers that
507 // can be outstanding before we start throttling based on
508 // OnSwapBuffersComplete callback.
509 static const int kMaxSwapBuffersPending
= 2;
511 // Set to true if we should ignore RenderWidget::Show calls.
514 // Indicates that we shouldn't bother generated paint events.
517 // Indicates that we are in fullscreen mode.
520 // Indicates that we should be repainted when restored. This flag is set to
521 // true if we receive an invalidation / scroll event from webkit while our
522 // is_hidden_ flag is set to true. This is used to force a repaint once we
523 // restore to account for the fact that our host would not know about the
524 // invalidation / scroll event(s) from webkit while we are hidden.
525 bool needs_repainting_on_restore_
;
527 // Indicates whether we have been focused/unfocused by the browser.
530 // Are we currently handling an input event?
531 bool handling_input_event_
;
533 // True if we have requested this widget be closed. No more messages will
534 // be sent, except for a Close.
537 // Whether this RenderWidget is currently swapped out, such that the view is
538 // being rendered by another process. If all RenderWidgets in a process are
539 // swapped out, the process can exit.
540 bool is_swapped_out_
;
542 // Indicates if an input method is active in the browser process.
543 bool input_method_is_active_
;
545 // Stores information about the current text input.
546 WebKit::WebTextInputInfo text_input_info_
;
548 // Stores the current text input type of |webwidget_|.
549 ui::TextInputType text_input_type_
;
551 // Stores the current type of composition text rendering of |webwidget_|.
552 bool can_compose_inline_
;
554 // Stores the current selection bounds.
555 gfx::Rect selection_start_rect_
;
556 gfx::Rect selection_end_rect_
;
558 // Stores the current composition character bounds.
559 std::vector
<gfx::Rect
> composition_character_bounds_
;
561 // Stores the current composition range.
562 ui::Range composition_range_
;
564 // The kind of popup this widget represents, NONE if not a popup.
565 WebKit::WebPopupType popup_type_
;
567 // Holds all the needed plugin window moves for a scroll.
568 typedef std::vector
<webkit::npapi::WebPluginGeometry
> WebPluginGeometryVector
;
569 WebPluginGeometryVector plugin_window_moves_
;
571 // A custom background for the widget.
572 SkBitmap background_
;
574 // While we are waiting for the browser to update window sizes, we track the
575 // pending size temporarily.
576 int pending_window_rect_count_
;
577 WebKit::WebRect pending_window_rect_
;
579 // The screen rects of the view and the window that contains it.
580 gfx::Rect view_screen_rect_
;
581 gfx::Rect window_screen_rect_
;
583 scoped_ptr
<IPC::Message
> pending_input_event_ack_
;
585 // Indicates if the next sequence of Char events should be suppressed or not.
586 bool suppress_next_char_events_
;
588 // Set to true if painting to the window is handled by the accelerated
590 bool is_accelerated_compositing_active_
;
592 base::OneShotTimer
<RenderWidget
> animation_timer_
;
593 base::Time animation_floor_time_
;
594 bool animation_update_pending_
;
595 bool invalidation_task_posted_
;
597 bool has_disable_gpu_vsync_switch_
;
598 base::TimeTicks last_do_deferred_update_time_
;
600 cc::RenderingStats software_stats_
;
602 // UpdateRect parameters for the current compositing pass. This is used to
603 // pass state between DoDeferredUpdate and OnSwapBuffersPosted.
604 scoped_ptr
<ViewHostMsg_UpdateRect_Params
> pending_update_params_
;
606 // Queue of UpdateRect messages corresponding to a SwapBuffers. We want to
607 // delay sending of UpdateRect until the corresponding SwapBuffers has been
608 // executed. Since we can have several in flight, we need to keep them in a
609 // queue. Note: some SwapBuffers may not correspond to an update, in which
610 // case NULL is added to the queue.
611 std::deque
<ViewHostMsg_UpdateRect
*> updates_pending_swap_
;
613 // Properties of the screen hosting this RenderWidget instance.
614 WebKit::WebScreenInfo screen_info_
;
616 // The device scale factor. This value is computed from the DPI entries in
617 // |screen_info_| on some platforms, and defaults to 1 on other platforms.
618 float device_scale_factor_
;
620 // Specifies whether input event throttling is enabled for this widget.
621 bool throttle_input_events_
;
623 // State associated with the BeginSmoothScroll synthetic scrolling function.
624 int next_smooth_scroll_gesture_id_
;
625 typedef std::map
<int, SmoothScrollCompletionCallback
>
626 PendingSmoothScrollGestureMap
;
627 PendingSmoothScrollGestureMap pending_smooth_scroll_gestures_
;
629 // Specified whether the compositor will run in its own thread.
630 bool is_threaded_compositing_enabled_
;
632 DISALLOW_COPY_AND_ASSIGN(RenderWidget
);
635 } // namespace content
637 #endif // CONTENT_RENDERER_RENDER_WIDGET_H_