Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 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_BROWSER_VIEW_RENDERER_H_
      6 #define ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_
      7 
      8 #include "android_webview/browser/global_tile_manager.h"
      9 #include "android_webview/browser/global_tile_manager_client.h"
     10 #include "android_webview/browser/parent_compositor_draw_constraints.h"
     11 #include "android_webview/browser/shared_renderer_state.h"
     12 #include "base/android/scoped_java_ref.h"
     13 #include "base/callback.h"
     14 #include "base/cancelable_callback.h"
     15 #include "base/values.h"
     16 #include "content/public/browser/android/synchronous_compositor.h"
     17 #include "content/public/browser/android/synchronous_compositor_client.h"
     18 #include "skia/ext/refptr.h"
     19 #include "ui/gfx/rect.h"
     20 #include "ui/gfx/vector2d_f.h"
     21 
     22 class SkCanvas;
     23 class SkPicture;
     24 struct AwDrawGLInfo;
     25 struct AwDrawSWFunctionTable;
     26 
     27 namespace content {
     28 class ContentViewCore;
     29 struct SynchronousCompositorMemoryPolicy;
     30 class WebContents;
     31 }
     32 
     33 namespace android_webview {
     34 
     35 class BrowserViewRendererClient;
     36 
     37 // Delegate to perform rendering actions involving Java objects.
     38 class BrowserViewRendererJavaHelper {
     39  public:
     40   static BrowserViewRendererJavaHelper* GetInstance();
     41 
     42   typedef base::Callback<bool(SkCanvas*)> RenderMethod;
     43 
     44   // Try obtaining the native SkCanvas from |java_canvas| and call
     45   // |render_source| with it. If that fails, allocate an auxilary bitmap
     46   // for |render_source| to render into, then copy the bitmap into
     47   // |java_canvas|.
     48   virtual bool RenderViaAuxilaryBitmapIfNeeded(
     49       jobject java_canvas,
     50       const gfx::Vector2d& scroll_correction,
     51       const gfx::Size& auxiliary_bitmap_size,
     52       RenderMethod render_source) = 0;
     53 
     54  protected:
     55   virtual ~BrowserViewRendererJavaHelper() {}
     56 };
     57 
     58 // Interface for all the WebView-specific content rendering operations.
     59 // Provides software and hardware rendering and the Capture Picture API.
     60 class BrowserViewRenderer : public content::SynchronousCompositorClient,
     61                             public GlobalTileManagerClient {
     62  public:
     63   static void CalculateTileMemoryPolicy(bool use_zero_copy);
     64 
     65   BrowserViewRenderer(
     66       BrowserViewRendererClient* client,
     67       SharedRendererState* shared_renderer_state,
     68       content::WebContents* web_contents,
     69       const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
     70 
     71   virtual ~BrowserViewRenderer();
     72 
     73   // Main handler for view drawing: performs a SW draw immediately, or sets up
     74   // a subsequent GL Draw (via BrowserViewRendererClient::RequestDrawGL) and
     75   // returns true. A false return value indicates nothing was or will be drawn.
     76   // |java_canvas| is the target of the draw. |is_hardware_canvas| indicates
     77   // a GL Draw maybe possible on this canvas. |scroll| if the view's current
     78   // scroll offset. |clip| is the canvas's clip bounds. |global_visible_rect|
     79   // is the intersection of the view size and the window in window coordinates.
     80   bool OnDraw(jobject java_canvas,
     81               bool is_hardware_canvas,
     82               const gfx::Vector2d& scroll,
     83               const gfx::Rect& global_visible_rect);
     84 
     85   // CapturePicture API methods.
     86   skia::RefPtr<SkPicture> CapturePicture(int width, int height);
     87   void EnableOnNewPicture(bool enabled);
     88 
     89   void ClearView();
     90 
     91   // View update notifications.
     92   void SetIsPaused(bool paused);
     93   void SetViewVisibility(bool visible);
     94   void SetWindowVisibility(bool visible);
     95   void OnSizeChanged(int width, int height);
     96   void OnAttachedToWindow(int width, int height);
     97   void OnDetachedFromWindow();
     98 
     99   // Sets the scale for logical<->physical pixel conversions.
    100   void SetDipScale(float dip_scale);
    101 
    102   // Set the root layer scroll offset to |new_value|.
    103   void ScrollTo(gfx::Vector2d new_value);
    104 
    105   // Android views hierarchy gluing.
    106   bool IsVisible() const;
    107   gfx::Rect GetScreenRect() const;
    108   bool attached_to_window() const { return attached_to_window_; }
    109   bool hardware_enabled() const { return hardware_enabled_; }
    110   void ReleaseHardware();
    111 
    112   // Set the memory policy in shared renderer state and request the tiles from
    113   // GlobalTileManager. The actually amount of memory allowed by
    114   // GlobalTileManager may not be equal to what's requested in |policy|.
    115   void RequestMemoryPolicy(content::SynchronousCompositorMemoryPolicy& policy);
    116 
    117   void TrimMemory(const int level, const bool visible);
    118 
    119   // SynchronousCompositorClient overrides.
    120   virtual void DidInitializeCompositor(
    121       content::SynchronousCompositor* compositor) OVERRIDE;
    122   virtual void DidDestroyCompositor(content::SynchronousCompositor* compositor)
    123       OVERRIDE;
    124   virtual void SetContinuousInvalidate(bool invalidate) OVERRIDE;
    125   virtual void DidUpdateContent() OVERRIDE;
    126   virtual gfx::Vector2dF GetTotalRootLayerScrollOffset() OVERRIDE;
    127   virtual void UpdateRootLayerState(
    128       const gfx::Vector2dF& total_scroll_offset_dip,
    129       const gfx::Vector2dF& max_scroll_offset_dip,
    130       const gfx::SizeF& scrollable_size_dip,
    131       float page_scale_factor,
    132       float min_page_scale_factor,
    133       float max_page_scale_factor) OVERRIDE;
    134   virtual bool IsExternalFlingActive() const OVERRIDE;
    135   virtual void DidOverscroll(gfx::Vector2dF accumulated_overscroll,
    136                              gfx::Vector2dF latest_overscroll_delta,
    137                              gfx::Vector2dF current_fling_velocity) OVERRIDE;
    138 
    139   // GlobalTileManagerClient overrides.
    140   virtual content::SynchronousCompositorMemoryPolicy GetMemoryPolicy()
    141       const OVERRIDE;
    142   virtual void SetMemoryPolicy(
    143       content::SynchronousCompositorMemoryPolicy new_policy,
    144       bool effective_immediately) OVERRIDE;
    145 
    146   void UpdateParentDrawConstraints();
    147   void DidSkipCommitFrame();
    148 
    149  private:
    150   void SetTotalRootLayerScrollOffset(gfx::Vector2dF new_value_dip);
    151   // Checks the continuous invalidate and block invalidate state, and schedule
    152   // invalidates appropriately. If |force_invalidate| is true, then send a view
    153   // invalidate regardless of compositor expectation. If |skip_reschedule_tick|
    154   // is true and if there is already a pending fallback tick, don't reschedule
    155   // them.
    156   void EnsureContinuousInvalidation(bool force_invalidate,
    157                                     bool skip_reschedule_tick);
    158   bool OnDrawSoftware(jobject java_canvas);
    159   bool CompositeSW(SkCanvas* canvas);
    160   void DidComposite();
    161   void DidSkipCompositeInDraw();
    162   scoped_ptr<base::Value> RootLayerStateAsValue(
    163       const gfx::Vector2dF& total_scroll_offset_dip,
    164       const gfx::SizeF& scrollable_size_dip);
    165 
    166   bool OnDrawHardware(jobject java_canvas);
    167   scoped_ptr<cc::CompositorFrame> CompositeHw();
    168   void ReturnUnusedResource(scoped_ptr<cc::CompositorFrame> frame);
    169   void ReturnResourceFromParent();
    170 
    171   // If we call up view invalidate and OnDraw is not called before a deadline,
    172   // then we keep ticking the SynchronousCompositor so it can make progress.
    173   // Do this in a two stage tick due to native MessageLoop favors delayed task,
    174   // so ensure delayed task is inserted only after the draw task returns.
    175   void PostFallbackTick();
    176   void FallbackTickFired();
    177 
    178   // Force invoke the compositor to run produce a 1x1 software frame that is
    179   // immediately discarded. This is a hack to force invoke parts of the
    180   // compositor that are not directly exposed here.
    181   void ForceFakeCompositeSW();
    182 
    183   void EnforceMemoryPolicyImmediately(
    184       content::SynchronousCompositorMemoryPolicy policy);
    185 
    186   gfx::Vector2d max_scroll_offset() const;
    187 
    188   content::SynchronousCompositorMemoryPolicy CalculateDesiredMemoryPolicy();
    189   // For debug tracing or logging. Return the string representation of this
    190   // view renderer's state and the |draw_info| if provided.
    191   std::string ToString(AwDrawGLInfo* draw_info) const;
    192 
    193   BrowserViewRendererClient* client_;
    194   SharedRendererState* shared_renderer_state_;
    195   content::WebContents* web_contents_;
    196   scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
    197 
    198   content::SynchronousCompositor* compositor_;
    199 
    200   bool is_paused_;
    201   bool view_visible_;
    202   bool window_visible_;  // Only applicable if |attached_to_window_| is true.
    203   bool attached_to_window_;
    204   bool hardware_enabled_;
    205   float dip_scale_;
    206   float page_scale_factor_;
    207   bool on_new_picture_enable_;
    208   bool clear_view_;
    209 
    210   gfx::Vector2d last_on_draw_scroll_offset_;
    211   gfx::Rect last_on_draw_global_visible_rect_;
    212 
    213   // The draw constraints from the parent compositor. These are only used for
    214   // tiling priority.
    215   ParentCompositorDrawConstraints parent_draw_constraints_;
    216 
    217   // When true, we should continuously invalidate and keep drawing, for example
    218   // to drive animation. This value is set by the compositor and should always
    219   // reflect the expectation of the compositor and not be reused for other
    220   // states.
    221   bool compositor_needs_continuous_invalidate_;
    222 
    223   bool invalidate_after_composite_;
    224 
    225   // Used to block additional invalidates while one is already pending.
    226   bool block_invalidates_;
    227 
    228   base::CancelableClosure post_fallback_tick_;
    229   base::CancelableClosure fallback_tick_fired_;
    230   bool fallback_tick_pending_;
    231 
    232   int width_;
    233   int height_;
    234 
    235   // Current scroll offset in CSS pixels.
    236   gfx::Vector2dF scroll_offset_dip_;
    237 
    238   // Max scroll offset in CSS pixels.
    239   gfx::Vector2dF max_scroll_offset_dip_;
    240 
    241   // Used to prevent rounding errors from accumulating enough to generate
    242   // visible skew (especially noticeable when scrolling up and down in the same
    243   // spot over a period of time).
    244   gfx::Vector2dF overscroll_rounding_error_;
    245 
    246   GlobalTileManager::Key tile_manager_key_;
    247   content::SynchronousCompositorMemoryPolicy memory_policy_;
    248 
    249   DISALLOW_COPY_AND_ASSIGN(BrowserViewRenderer);
    250 };
    251 
    252 }  // namespace android_webview
    253 
    254 #endif  // ANDROID_WEBVIEW_BROWSER_BROWSER_VIEW_RENDERER_H_
    255