Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 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.
      4 
      5 #ifndef ANDROID_WEBVIEW_BROWSER_IN_PROCESS_VIEW_RENDERER_H_
      6 #define ANDROID_WEBVIEW_BROWSER_IN_PROCESS_VIEW_RENDERER_H_
      7 
      8 #include <string>
      9 
     10 #include "android_webview/browser/browser_view_renderer.h"
     11 #include "android_webview/browser/gl_view_renderer_manager.h"
     12 #include "base/cancelable_callback.h"
     13 #include "content/public/browser/android/synchronous_compositor.h"
     14 #include "content/public/browser/android/synchronous_compositor_client.h"
     15 #include "ui/gfx/vector2d_f.h"
     16 
     17 namespace content {
     18 class SynchronousCompositor;
     19 class WebContents;
     20 }
     21 
     22 typedef void* EGLContext;
     23 class SkCanvas;
     24 
     25 namespace android_webview {
     26 
     27 class AwGLSurface;
     28 
     29 // Provides RenderViewHost wrapper functionality for sending WebView-specific
     30 // IPC messages to the renderer and from there to WebKit.
     31 class InProcessViewRenderer : public BrowserViewRenderer,
     32                               public content::SynchronousCompositorClient {
     33  public:
     34   static void CalculateTileMemoryPolicy();
     35 
     36   InProcessViewRenderer(BrowserViewRenderer::Client* client,
     37                         JavaHelper* java_helper,
     38                         content::WebContents* web_contents);
     39   virtual ~InProcessViewRenderer();
     40 
     41   static InProcessViewRenderer* FromWebContents(
     42       content::WebContents* contents);
     43 
     44   // TODO(joth): consider extracting this to its own utility class.
     45   typedef base::Callback<bool(SkCanvas*)> RenderMethod;
     46   static bool RenderViaAuxilaryBitmapIfNeeded(
     47       jobject java_canvas,
     48       JavaHelper* java_helper,
     49       const gfx::Vector2d& scroll_correction,
     50       const gfx::Rect& clip,
     51       RenderMethod render_source,
     52       void* owner_key);
     53 
     54   // BrowserViewRenderer overrides
     55   virtual bool OnDraw(jobject java_canvas,
     56                       bool is_hardware_canvas,
     57                       const gfx::Vector2d& scroll_,
     58                       const gfx::Rect& clip) OVERRIDE;
     59   virtual void DrawGL(AwDrawGLInfo* draw_info) OVERRIDE;
     60   virtual void SetGlobalVisibleRect(const gfx::Rect& visible_rect) OVERRIDE;
     61   virtual skia::RefPtr<SkPicture> CapturePicture(int width,
     62                                                  int height) OVERRIDE;
     63   virtual void EnableOnNewPicture(bool enabled) OVERRIDE;
     64   virtual void SetIsPaused(bool paused) OVERRIDE;
     65   virtual void SetViewVisibility(bool visible) OVERRIDE;
     66   virtual void SetWindowVisibility(bool visible) OVERRIDE;
     67   virtual void OnSizeChanged(int width, int height) OVERRIDE;
     68   virtual void ScrollTo(gfx::Vector2d new_value) OVERRIDE;
     69   virtual void OnAttachedToWindow(int width, int height) OVERRIDE;
     70   virtual void OnDetachedFromWindow() OVERRIDE;
     71   virtual void SetDipScale(float dip_scale) OVERRIDE;
     72   virtual bool IsAttachedToWindow() OVERRIDE;
     73   virtual bool IsVisible() OVERRIDE;
     74   virtual gfx::Rect GetScreenRect() OVERRIDE;
     75   virtual void TrimMemory(int level) OVERRIDE;
     76 
     77   // SynchronousCompositorClient overrides
     78   virtual void DidInitializeCompositor(
     79       content::SynchronousCompositor* compositor) OVERRIDE;
     80   virtual void DidDestroyCompositor(
     81       content::SynchronousCompositor* compositor) OVERRIDE;
     82   virtual void SetContinuousInvalidate(bool invalidate) OVERRIDE;
     83   virtual void SetMaxRootLayerScrollOffset(gfx::Vector2dF new_value) OVERRIDE;
     84   virtual void SetTotalRootLayerScrollOffset(
     85       gfx::Vector2dF new_value_css) OVERRIDE;
     86   virtual void DidUpdateContent() OVERRIDE;
     87   virtual gfx::Vector2dF GetTotalRootLayerScrollOffset() OVERRIDE;
     88   virtual bool IsExternalFlingActive() const OVERRIDE;
     89   virtual void SetRootLayerPageScaleFactor(float page_scale_factor) OVERRIDE;
     90   virtual void SetRootLayerScrollableSize(gfx::SizeF scrollable_size) OVERRIDE;
     91   virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll,
     92                              gfx::Vector2dF latest_overscroll_delta,
     93                              gfx::Vector2dF current_fling_velocity) OVERRIDE;
     94 
     95   void WebContentsGone();
     96   bool RequestProcessGL();
     97 
     98  private:
     99   // Checks the continuous invalidate and block invalidate state, and schedule
    100   // invalidates appropriately. If |invalidate_ignore_compositor| is true,
    101   // then send a view invalidate regardless of compositor expectation.
    102   void EnsureContinuousInvalidation(
    103       AwDrawGLInfo* draw_info,
    104       bool invalidate_ignore_compositor);
    105   bool DrawSWInternal(jobject java_canvas,
    106                       const gfx::Rect& clip_bounds);
    107   bool CompositeSW(SkCanvas* canvas);
    108 
    109   void UpdateCachedGlobalVisibleRect();
    110 
    111   // If we call up view invalidate and OnDraw is not called before a deadline,
    112   // then we keep ticking the SynchronousCompositor so it can make progress.
    113   void FallbackTickFired();
    114   void ForceFakeCompositeSW();
    115 
    116   void NoLongerExpectsDrawGL();
    117 
    118   bool InitializeHwDraw();
    119 
    120   gfx::Vector2d max_scroll_offset() const;
    121 
    122   void SetMemoryPolicy(content::SynchronousCompositorMemoryPolicy& new_policy);
    123 
    124   // For debug tracing or logging. Return the string representation of this
    125   // view renderer's state and the |draw_info| if provided.
    126   std::string ToString(AwDrawGLInfo* draw_info) const;
    127 
    128   BrowserViewRenderer::Client* client_;
    129   BrowserViewRenderer::JavaHelper* java_helper_;
    130   content::WebContents* web_contents_;
    131   content::SynchronousCompositor* compositor_;
    132 
    133   bool is_paused_;
    134   bool view_visible_;
    135   bool window_visible_;  // Only applicable if |attached_to_window_| is true.
    136   bool attached_to_window_;
    137   float dip_scale_;
    138   float page_scale_factor_;
    139   bool on_new_picture_enable_;
    140 
    141   // When true, we should continuously invalidate and keep drawing, for example
    142   // to drive animation. This value is set by the compositor and should always
    143   // reflect the expectation of the compositor and not be reused for other
    144   // states.
    145   bool compositor_needs_continuous_invalidate_;
    146 
    147   // Used to block additional invalidates while one is already pending or before
    148   // compositor draw which may switch continuous_invalidate on and off in the
    149   // process.
    150   bool block_invalidates_;
    151 
    152   // Holds a callback to FallbackTickFired while it is pending.
    153   base::CancelableClosure fallback_tick_;
    154 
    155   int width_;
    156   int height_;
    157 
    158   bool hardware_initialized_;
    159   bool hardware_failed_;
    160   scoped_refptr<AwGLSurface> gl_surface_;
    161 
    162   // Used only for detecting Android View System context changes.
    163   // Not to be used between draw calls.
    164   EGLContext last_egl_context_;
    165 
    166   // Should always call UpdateCachedGlobalVisibleRect before using this.
    167   gfx::Rect cached_global_visible_rect_;
    168 
    169   // Last View scroll when View.onDraw() was called.
    170   gfx::Vector2d scroll_at_start_of_frame_;
    171 
    172   // Current scroll offset in CSS pixels.
    173   gfx::Vector2dF scroll_offset_dip_;
    174 
    175   // Max scroll offset in CSS pixels.
    176   gfx::Vector2dF max_scroll_offset_dip_;
    177 
    178   // Used to prevent rounding errors from accumulating enough to generate
    179   // visible skew (especially noticeable when scrolling up and down in the same
    180   // spot over a period of time).
    181   gfx::Vector2dF overscroll_rounding_error_;
    182 
    183   GLViewRendererManager::Key manager_key_;
    184 
    185   content::SynchronousCompositorMemoryPolicy memory_policy_;
    186 
    187   DISALLOW_COPY_AND_ASSIGN(InProcessViewRenderer);
    188 };
    189 
    190 }  // namespace android_webview
    191 
    192 #endif  // ANDROID_WEBVIEW_BROWSER_IN_PROCESS_VIEW_RENDERER_H_
    193