Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_view_mac.h
index e141633..584b0df 100644 (file)
@@ -6,6 +6,7 @@
 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_MAC_H_
 
 #import <Cocoa/Cocoa.h>
+#include <IOSurface/IOSurfaceAPI.h>
 #include <list>
 #include <map>
 #include <string>
 #include "base/memory/scoped_ptr.h"
 #include "base/memory/weak_ptr.h"
 #include "base/time/time.h"
-#include "content/browser/accessibility/browser_accessibility_delegate_mac.h"
+#include "content/browser/compositor/browser_compositor_view_mac.h"
+#include "content/browser/compositor/delegated_frame_host.h"
+#include "content/browser/compositor/io_surface_layer_mac.h"
+#include "content/browser/renderer_host/display_link_mac.h"
 #include "content/browser/renderer_host/render_widget_host_view_base.h"
 #include "content/browser/renderer_host/software_frame_manager.h"
+#include "content/common/content_export.h"
+#include "content/common/cursors/webcursor.h"
 #include "content/common/edit_command.h"
 #import "content/public/browser/render_widget_host_view_mac_base.h"
 #include "ipc/ipc_sender.h"
 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
 #include "ui/base/cocoa/base_view.h"
-#include "webkit/common/cursors/webcursor.h"
+#include "ui/base/cocoa/remote_layer_api.h"
+#include "ui/gfx/display_observer.h"
 
 namespace content {
-class CompositingIOSurfaceMac;
-class CompositingIOSurfaceContext;
+class BrowserCompositorviewMac;
 class RenderWidgetHostViewMac;
 class RenderWidgetHostViewMacEditCommandHelper;
+class WebContents;
+}
+
+namespace ui {
+class Compositor;
+class Layer;
 }
 
-@class CompositingIOSurfaceLayer;
 @class FullscreenWindowManager;
 @protocol RenderWidgetHostViewMacDelegate;
 @class ToolTip;
@@ -49,8 +60,7 @@ class RenderWidgetHostViewMacEditCommandHelper;
 @interface RenderWidgetHostViewCocoa
     : BaseView <RenderWidgetHostViewMacBase,
                 RenderWidgetHostViewMacOwner,
-                NSTextInputClient,
-                BrowserAccessibilityDelegateCocoa> {
+                NSTextInputClient> {
  @private
   scoped_ptr<content::RenderWidgetHostViewMac> renderWidgetHostView_;
   // This ivar is the cocoa delegate of the NSResponder.
@@ -139,14 +149,6 @@ class RenderWidgetHostViewMacEditCommandHelper;
   // Event monitor for scroll wheel end event.
   id endWheelMonitor_;
 
-  // OpenGL Support:
-
-  // recursive globalFrameDidChange protection:
-  BOOL handlingGlobalFrameDidChange_;
-
-  // The scale factor of the display this view is in.
-  float deviceScaleFactor_;
-
   // If true then escape key down events are suppressed until the first escape
   // key up event. (The up event is suppressed as well). This is used by the
   // flash fullscreen code to avoid sending a key up event without a matching
@@ -198,10 +200,17 @@ class RenderWidgetHostImpl;
 //     references to it must become NULL."
 //
 // RenderWidgetHostView class hierarchy described in render_widget_host_view.h.
-class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
-                                public IPC::Sender,
-                                public SoftwareFrameManagerClient {
+class CONTENT_EXPORT RenderWidgetHostViewMac
+    : public RenderWidgetHostViewBase,
+      public DelegatedFrameHostClient,
+      public BrowserCompositorViewMacClient,
+      public IPC::Sender,
+      public gfx::DisplayObserver {
  public:
+  // The view will associate itself with the given widget. The native view must
+  // be hooked up immediately to the view hierarchy, or else when it is
+  // deleted it will delete this out from under the caller.
+  explicit RenderWidgetHostViewMac(RenderWidgetHost* widget);
   virtual ~RenderWidgetHostViewMac();
 
   RenderWidgetHostViewCocoa* cocoa_view() const { return cocoa_view_; }
@@ -211,7 +220,7 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   // |delegate| should be set at most once.
   CONTENT_EXPORT void SetDelegate(
     NSObject<RenderWidgetHostViewMacDelegate>* delegate);
-  void SetAllowOverlappingViews(bool overlapping);
+  void SetAllowPauseForResizeOrRepaint(bool allow);
 
   // RenderWidgetHostView implementation.
   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
@@ -219,6 +228,7 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
   virtual void SetSize(const gfx::Size& size) OVERRIDE;
   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
+  virtual gfx::Vector2dF GetLastScrollOffset() const OVERRIDE;
   virtual gfx::NativeView GetNativeView() const OVERRIDE;
   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
@@ -238,9 +248,9 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   virtual void SpeakSelection() OVERRIDE;
   virtual bool IsSpeaking() const OVERRIDE;
   virtual void StopSpeaking() OVERRIDE;
-  virtual void SetBackground(const SkBitmap& background) OVERRIDE;
+  virtual void SetBackgroundOpaque(bool opaque) OVERRIDE;
 
-  // Implementation of RenderWidgetHostViewPort.
+  // Implementation of RenderWidgetHostViewBase.
   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
                            const gfx::Rect& pos) OVERRIDE;
   virtual void InitAsFullscreen(
@@ -248,7 +258,6 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   virtual void WasShown() OVERRIDE;
   virtual void WasHidden() OVERRIDE;
   virtual void MovePluginWindows(
-      const gfx::Vector2d& scroll_offset,
       const std::vector<WebPluginGeometry>& moves) OVERRIDE;
   virtual void Focus() OVERRIDE;
   virtual void Blur() OVERRIDE;
@@ -261,13 +270,9 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   virtual void ImeCompositionRangeChanged(
       const gfx::Range& range,
       const std::vector<gfx::Rect>& character_bounds) OVERRIDE;
-  virtual void DidUpdateBackingStore(
-      const gfx::Rect& scroll_rect,
-      const gfx::Vector2d& scroll_delta,
-      const std::vector<gfx::Rect>& copy_rects,
-      const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE;
   virtual void RenderProcessGone(base::TerminationStatus status,
                                  int error_code) OVERRIDE;
+  virtual void RenderWidgetHostGone() OVERRIDE;
   virtual void Destroy() OVERRIDE;
   virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE;
   virtual void SelectionChanged(const base::string16& text,
@@ -275,13 +280,11 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
                                 const gfx::Range& range) OVERRIDE;
   virtual void SelectionBoundsChanged(
       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
-  virtual void ScrollOffsetChanged() OVERRIDE;
-  virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
   virtual void CopyFromCompositingSurface(
       const gfx::Rect& src_subrect,
       const gfx::Size& dst_size,
       const base::Callback<void(bool, const SkBitmap&)>& callback,
-      SkBitmap::Config config) OVERRIDE;
+      SkColorType color_type) OVERRIDE;
   virtual void CopyFromCompositingSurfaceToVideoFrame(
       const gfx::Rect& src_subrect,
       const scoped_refptr<media::VideoFrame>& target,
@@ -293,10 +296,13 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   virtual void EndFrameSubscription() OVERRIDE;
   virtual void OnSwapCompositorFrame(
       uint32 output_surface_id, scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
-  virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
   virtual void AcceleratedSurfaceInitialized(int host_id,
                                              int route_id) OVERRIDE;
-  virtual void CreateBrowserAccessibilityManagerIfNeeded() OVERRIDE;
+  virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
+      BrowserAccessibilityDelegate* delegate) OVERRIDE;
+  virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds)
+      OVERRIDE;
+  virtual void AccessibilityShowMenu(const gfx::Point& point) OVERRIDE;
   virtual bool PostProcessEventForPluginIme(
       const NativeWebKeyboardEvent& event) OVERRIDE;
 
@@ -313,22 +319,21 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
 
-  virtual void SetHasHorizontalScrollbar(
-      bool has_horizontal_scrollbar) OVERRIDE;
-  virtual void SetScrollOffsetPinning(
-      bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE;
   virtual bool LockMouse() OVERRIDE;
   virtual void UnlockMouse() OVERRIDE;
-  virtual void UnhandledWheelEvent(
-      const blink::WebMouseWheelEvent& event) OVERRIDE;
+  virtual void WheelEventAck(const blink::WebMouseWheelEvent& event,
+                             InputEventAckState ack_result) OVERRIDE;
 
   // IPC::Sender implementation.
   virtual bool Send(IPC::Message* message) OVERRIDE;
 
-  // SoftwareFrameManagerClient implementation:
-  virtual void SoftwareFrameWasFreed(
-      uint32 output_surface_id, unsigned frame_id) OVERRIDE;
-  virtual void ReleaseReferencesToSoftwareFrame() OVERRIDE;
+  virtual SkColorType PreferredReadbackFormat() OVERRIDE;
+
+  // gfx::DisplayObserver implementation.
+  virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
+  virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
+  virtual void OnDisplayMetricsChanged(const gfx::Display& display,
+                                       uint32_t metrics) OVERRIDE;
 
   // Forwards the mouse event to the renderer.
   void ForwardMouseEvent(const blink::WebMouseEvent& event);
@@ -337,36 +342,11 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
 
   void SetTextInputActive(bool active);
 
-  // Change this view to use CoreAnimation to draw.
-  void EnableCoreAnimation();
-
   // Sends completed plugin IME notification and text back to the renderer.
   void PluginImeCompositionCompleted(const base::string16& text, int plugin_id);
 
   const std::string& selected_text() const { return selected_text_; }
 
-  // Update the IOSurface to be drawn and call setNeedsDisplay on
-  // |cocoa_view_|.
-  void CompositorSwapBuffers(uint64 surface_handle,
-                             const gfx::Size& size,
-                             float scale_factor,
-                             const std::vector<ui::LatencyInfo>& latency_info);
-
-  // Draw the IOSurface by making its context current to this view.
-  bool DrawIOSurfaceWithoutCoreAnimation();
-
-  // Called when a GPU error is detected. Deletes all compositing state.
-  void GotAcceleratedCompositingError();
-
-  // Sets the overlay view, which should be drawn in the same IOSurface
-  // atop of this view, if both views are drawing accelerated content.
-  // Overlay is stored as a weak ptr.
-  void SetOverlayView(RenderWidgetHostViewMac* overlay,
-                      const gfx::Point& offset);
-
-  // Removes the previously set overlay view.
-  void RemoveOverlayView();
-
   // Returns true and stores first rectangle for character range if the
   // requested |range| is already cached, otherwise returns false.
   // Exposed for testing.
@@ -391,6 +371,8 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   gfx::Range ConvertCharacterRangeToCompositionRange(
       const gfx::Range& request_range);
 
+  WebContents* GetWebContents();
+
   // These member variables should be private, but the associated ObjC class
   // needs access to them and can't be made a friend.
 
@@ -398,67 +380,25 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   // someone (other than superview) has retained |cocoa_view_|.
   RenderWidgetHostImpl* render_widget_host_;
 
-  // This is true when we are currently painting and thus should handle extra
-  // paint requests by expanding the invalid rect rather than actually painting.
-  bool about_to_validate_and_paint_;
-
-  // This is true when we have already scheduled a call to
-  // |-callSetNeedsDisplayInRect:| but it has not been fulfilled yet.  Used to
-  // prevent us from scheduling multiple calls.
-  bool call_set_needs_display_in_rect_pending_;
-
-  // Whether last rendered frame was accelerated.
-  bool last_frame_was_accelerated_;
-
-  // The invalid rect that needs to be painted by callSetNeedsDisplayInRect.
-  // This value is only meaningful when
-  // |call_set_needs_display_in_rect_pending_| is true.
-  NSRect invalid_rect_;
-
-  // The time at which this view started displaying white pixels as a result of
-  // not having anything to paint (empty backing store from renderer). This
-  // value returns true for is_null() if we are not recording whiteout times.
-  base::TimeTicks whiteout_start_time_;
-
-  // The time it took after this view was selected for it to be fully painted.
-  base::TimeTicks web_contents_switch_paint_time_;
-
   // Current text input type.
   ui::TextInputType text_input_type_;
   bool can_compose_inline_;
 
-  base::scoped_nsobject<CALayer> software_layer_;
-
-  // Accelerated compositing structures. These may be dynamically created and
-  // destroyed together in Create/DestroyCompositedIOSurfaceAndLayer.
-  base::scoped_nsobject<CompositingIOSurfaceLayer> compositing_iosurface_layer_;
-  scoped_ptr<CompositingIOSurfaceMac> compositing_iosurface_;
-  scoped_refptr<CompositingIOSurfaceContext> compositing_iosurface_context_;
+  // The background CoreAnimation layer which is hosted by |cocoa_view_|.
+  base::scoped_nsobject<CALayer> background_layer_;
 
-  // Timer used to dynamically transition the compositing layer in and out of
-  // asynchronous mode.
-  base::DelayTimer<RenderWidgetHostViewMac>
-      compositing_iosurface_layer_async_timer_;
+  // Delegated frame management and compositior.
+  scoped_ptr<DelegatedFrameHost> delegated_frame_host_;
+  scoped_ptr<ui::Layer> root_layer_;
 
-  // This holds the current software compositing framebuffer, if any.
-  scoped_ptr<SoftwareFrameManager> software_frame_manager_;
+  // Container for the NSView drawn by the browser compositor.
+  scoped_ptr<BrowserCompositorViewMac> browser_compositor_view_;
 
-  // Whether to allow overlapping views.
-  bool allow_overlapping_views_;
-
-  // Whether to use the CoreAnimation path to draw content.
-  bool use_core_animation_;
-
-  // Latency info to send back when the next frame appears on the
-  // screen.
-  std::vector<ui::LatencyInfo> pending_latency_info_;
-
-  // When taking a screenshot when using CoreAnimation, add a delay of
-  // a few frames to ensure that the contents have reached the screen
-  // before reporting latency info.
-  uint32 pending_latency_info_delay_;
-  base::WeakPtrFactory<RenderWidgetHostViewMac>
-      pending_latency_info_delay_weak_ptr_factory_;
+  // Placeholder that is allocated while browser_compositor_view_ is NULL,
+  // indicating that a BrowserCompositorViewMac may be allocated. This is to
+  // help in recycling the internals of BrowserCompositorViewMac.
+  scoped_ptr<BrowserCompositorViewPlaceholderMac>
+      browser_compositor_view_placeholder_;
 
   NSWindow* pepper_fullscreen_window() const {
     return pepper_fullscreen_window_;
@@ -470,28 +410,41 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
     return fullscreen_parent_host_view_;
   }
 
-  RenderWidgetHostViewFrameSubscriber* frame_subscriber() const {
-    return frame_subscriber_.get();
-  }
-
   int window_number() const;
 
-  float scale_factor() const;
+  // The scale factor for the screen that the view is currently on.
+  float ViewScaleFactor() const;
+
+  // Update the scale factor for the backing store and for any CALayers.
+  void UpdateBackingStoreScaleFactor();
+
+  // Ensure that the display link is associated with the correct display.
+  void UpdateDisplayLink();
 
-  void AddPendingLatencyInfo(
-      const std::vector<ui::LatencyInfo>& latency_info);
-  void SendPendingLatencyInfoToHost();
-  void TickPendingLatencyInfoDelay();
+  void PauseForPendingResizeOrRepaintsAndDraw();
+
+  // DelegatedFrameHostClient implementation.
+  virtual ui::Compositor* GetCompositor() const OVERRIDE;
+  virtual ui::Layer* GetLayer() OVERRIDE;
+  virtual RenderWidgetHostImpl* GetHost() OVERRIDE;
+  virtual bool IsVisible() OVERRIDE;
+  virtual scoped_ptr<ResizeLock> CreateResizeLock(
+      bool defer_compositor_lock) OVERRIDE;
+  virtual gfx::Size DesiredFrameSize() OVERRIDE;
+  virtual float CurrentDeviceScaleFactor() OVERRIDE;
+  virtual gfx::Size ConvertViewSizeToPixel(const gfx::Size& size) OVERRIDE;
+  virtual DelegatedFrameHost* GetDelegatedFrameHost() const OVERRIDE;
+
+  // BrowserCompositorViewMacClient implementation.
+  virtual bool BrowserCompositorViewShouldAckImmediately() const OVERRIDE;
+  virtual void BrowserCompositorViewFrameSwapped(
+      const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE;
+  virtual NSView* BrowserCompositorSuperview() OVERRIDE;
+  virtual ui::Layer* BrowserCompositorRootLayer() OVERRIDE;
 
  private:
-  friend class RenderWidgetHostView;
   friend class RenderWidgetHostViewMacTest;
 
-  // The view will associate itself with the given widget. The native view must
-  // be hooked up immediately to the view hierarchy, or else when it is
-  // deleted it will delete this out from under the caller.
-  explicit RenderWidgetHostViewMac(RenderWidgetHost* widget);
-
   // Returns whether this render view is a popup (autocomplete window).
   bool IsPopup() const;
 
@@ -499,34 +452,23 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   // invoke it from the message loop.
   void ShutdownHost();
 
-  bool CreateCompositedIOSurface();
-  bool CreateCompositedIOSurfaceLayer();
-  enum DestroyContextBehavior {
-    kLeaveContextBoundToView,
-    kDestroyContext,
-  };
-  void DestroyCompositedIOSurfaceAndLayer(DestroyContextBehavior
-      destroy_context_behavior);
-
-  // Unbind the GL context (if any) that is bound to |cocoa_view_|.
-  void ClearBoundContextDrawable();
+  // Tear down all components of the browser compositor in an order that will
+  // ensure no dangling references.
+  void ShutdownBrowserCompositor();
 
-  // Called when a GPU SwapBuffers is received.
-  void GotAcceleratedFrame();
-
-  // Called when a software DIB is received.
-  void GotSoftwareFrame();
-
-  // Called if it has been a quarter-second since a GPU SwapBuffers has been
-  // received. In this case, switch from polling for frames to pushing them.
-  void TimerSinceGotAcceleratedFrameFired();
+  void EnsureBrowserCompositorView();
+  void DestroyBrowserCompositorView();
 
+  // IPC message handlers.
   void OnPluginFocusChanged(bool focused, int plugin_id);
   void OnStartPluginIme();
+  void OnGetRenderedTextCompleted(const std::string& text);
+
+  // Send updated vsync parameters to the renderer.
+  void SendVSyncParametersToRenderer();
 
-  // Convert |rect| from the views coordinate (upper-left origin) into
-  // the OpenGL coordinate (lower-left origin) and scale for HiDPI displays.
-  gfx::Rect GetScaledOpenGLPixelRect(const gfx::Rect& rect);
+  // Dispatches a TTS session.
+  void SpeakText(const std::string& text);
 
   // The associated view. This is weak and is inserted into the view hierarchy
   // to own this RenderWidgetHostViewMac object. Set to nil at the start of the
@@ -536,6 +478,12 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   // Indicates if the page is loading.
   bool is_loading_;
 
+  // Whether it's allowed to pause waiting for a new frame.
+  bool allow_pause_for_resize_or_repaint_;
+
+  // The last scroll offset of the view.
+  gfx::Vector2dF last_scroll_offset_;
+
   // The text to be shown in the tooltip, supplied by the renderer.
   base::string16 tooltip_text_;
 
@@ -554,26 +502,13 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   // Our parent host view, if this is fullscreen.  NULL otherwise.
   RenderWidgetHostViewMac* fullscreen_parent_host_view_;
 
-  // The overlay view which is rendered above this one in the same
-  // accelerated IOSurface.
-  // Overlay view has |underlay_view_| set to this view.
-  base::WeakPtr<RenderWidgetHostViewMac> overlay_view_;
-
-  // Offset at which overlay view should be rendered.
-  gfx::Point overlay_view_offset_;
+  // Display link for getting vsync info.
+  scoped_refptr<DisplayLinkMac> display_link_;
 
-  // The underlay view which this view is rendered above in the same
-  // accelerated IOSurface.
-  // Underlay view has |overlay_view_| set to this view.
-  base::WeakPtr<RenderWidgetHostViewMac> underlay_view_;
-
-  // Set to true when |underlay_view_| has drawn this view. After that point,
-  // this view should not draw again until |underlay_view_| is changed.
-  bool underlay_view_has_drawn_;
-
-  // Factory used to safely reference overlay view set in SetOverlayView.
-  base::WeakPtrFactory<RenderWidgetHostViewMac>
-      overlay_view_weak_factory_;
+  // The current VSync timebase and interval. This is zero until the first call
+  // to SendVSyncParametersToRenderer(), and refreshed regularly thereafter.
+  base::TimeTicks vsync_timebase_;
+  base::TimeDelta vsync_interval_;
 
   // The current composition character range and its bounds.
   gfx::Range composition_range_;
@@ -582,11 +517,6 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase,
   // The current caret bounds.
   gfx::Rect caret_rect_;
 
-  // Subscriber that listens to frame presentation events.
-  scoped_ptr<RenderWidgetHostViewFrameSubscriber> frame_subscriber_;
-
-  base::WeakPtrFactory<RenderWidgetHostViewMac>
-      software_frame_weak_ptr_factory_;
   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewMac);
 };