Home | History | Annotate | Download | only in renderer_host
      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.
      4 
      5 #ifndef CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
      6 #define CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/cancelable_callback.h"
     10 #include "base/compiler_specific.h"
     11 #include "base/containers/scoped_ptr_hash_map.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "cc/resources/ui_resource_client.h"
     15 #include "cc/trees/layer_tree_host_client.h"
     16 #include "cc/trees/layer_tree_host_single_thread_client.h"
     17 #include "content/browser/android/ui_resource_provider_impl.h"
     18 #include "content/browser/renderer_host/image_transport_factory_android.h"
     19 #include "content/common/content_export.h"
     20 #include "content/public/browser/android/compositor.h"
     21 #include "third_party/khronos/GLES2/gl2.h"
     22 #include "ui/base/android/window_android_compositor.h"
     23 
     24 class SkBitmap;
     25 struct ANativeWindow;
     26 
     27 namespace cc {
     28 class Layer;
     29 class LayerTreeHost;
     30 }
     31 
     32 namespace content {
     33 class CompositorClient;
     34 class UIResourceProvider;
     35 
     36 // -----------------------------------------------------------------------------
     37 // Browser-side compositor that manages a tree of content and UI layers.
     38 // -----------------------------------------------------------------------------
     39 class CONTENT_EXPORT CompositorImpl
     40     : public Compositor,
     41       public cc::LayerTreeHostClient,
     42       public cc::LayerTreeHostSingleThreadClient,
     43       public ImageTransportFactoryAndroidObserver,
     44       public ui::WindowAndroidCompositor {
     45  public:
     46   CompositorImpl(CompositorClient* client, gfx::NativeWindow root_window);
     47   virtual ~CompositorImpl();
     48 
     49   static bool IsInitialized();
     50 
     51   // Creates a surface texture and returns a surface texture id. Returns -1 on
     52   // failure.
     53   static int CreateSurfaceTexture(int child_process_id);
     54 
     55   // Destroy all surface textures associated with |child_process_id|.
     56   static void DestroyAllSurfaceTextures(int child_process_id);
     57 
     58  private:
     59   // Compositor implementation.
     60   virtual void SetRootLayer(scoped_refptr<cc::Layer> root) OVERRIDE;
     61   virtual void SetWindowSurface(ANativeWindow* window) OVERRIDE;
     62   virtual void SetSurface(jobject surface) OVERRIDE;
     63   virtual void SetVisible(bool visible) OVERRIDE;
     64   virtual void setDeviceScaleFactor(float factor) OVERRIDE;
     65   virtual void SetWindowBounds(const gfx::Size& size) OVERRIDE;
     66   virtual void SetHasTransparentBackground(bool flag) OVERRIDE;
     67   virtual void SetNeedsComposite() OVERRIDE;
     68   virtual UIResourceProvider& GetUIResourceProvider() OVERRIDE;
     69 
     70   // LayerTreeHostClient implementation.
     71   virtual void WillBeginMainFrame(int frame_id) OVERRIDE {}
     72   virtual void DidBeginMainFrame() OVERRIDE {}
     73   virtual void Animate(base::TimeTicks frame_begin_time) OVERRIDE {}
     74   virtual void Layout() OVERRIDE;
     75   virtual void ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
     76                                    float page_scale) OVERRIDE {}
     77   virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(bool fallback)
     78       OVERRIDE;
     79   virtual void DidInitializeOutputSurface() OVERRIDE {}
     80   virtual void WillCommit() OVERRIDE {}
     81   virtual void DidCommit() OVERRIDE;
     82   virtual void DidCommitAndDrawFrame() OVERRIDE {}
     83   virtual void DidCompleteSwapBuffers() OVERRIDE;
     84 
     85   // LayerTreeHostSingleThreadClient implementation.
     86   virtual void ScheduleComposite() OVERRIDE;
     87   virtual void ScheduleAnimation() OVERRIDE;
     88   virtual void DidPostSwapBuffers() OVERRIDE;
     89   virtual void DidAbortSwapBuffers() OVERRIDE;
     90 
     91   // ImageTransportFactoryAndroidObserver implementation.
     92   virtual void OnLostResources() OVERRIDE;
     93 
     94   // WindowAndroidCompositor implementation.
     95   virtual void AttachLayerForReadback(scoped_refptr<cc::Layer> layer) OVERRIDE;
     96   virtual void RequestCopyOfOutputOnRootLayer(
     97       scoped_ptr<cc::CopyOutputRequest> request) OVERRIDE;
     98   virtual void OnVSync(base::TimeTicks frame_time,
     99                        base::TimeDelta vsync_period) OVERRIDE;
    100   virtual void SetNeedsAnimate() OVERRIDE;
    101 
    102   enum CompositingTrigger {
    103     DO_NOT_COMPOSITE,
    104     COMPOSITE_IMMEDIATELY,
    105     COMPOSITE_EVENTUALLY,
    106   };
    107   void PostComposite(CompositingTrigger trigger);
    108   void Composite(CompositingTrigger trigger);
    109 
    110   bool WillCompositeThisFrame() const {
    111     return current_composite_task_ &&
    112            !current_composite_task_->callback().is_null();
    113   }
    114   bool DidCompositeThisFrame() const {
    115     return current_composite_task_ &&
    116            current_composite_task_->callback().is_null();
    117   }
    118   bool WillComposite() const {
    119     return WillCompositeThisFrame() ||
    120            composite_on_vsync_trigger_ != DO_NOT_COMPOSITE;
    121   }
    122   void CancelComposite() {
    123     DCHECK(WillComposite());
    124     if (WillCompositeThisFrame())
    125       current_composite_task_->Cancel();
    126     current_composite_task_.reset();
    127     composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
    128     will_composite_immediately_ = false;
    129   }
    130   cc::UIResourceId GenerateUIResourceFromUIResourceBitmap(
    131       const cc::UIResourceBitmap& bitmap,
    132       bool is_transient);
    133   void OnGpuChannelEstablished();
    134 
    135   // root_layer_ is the persistent internal root layer, while subroot_layer_
    136   // is the one attached by the compositor client.
    137   scoped_refptr<cc::Layer> root_layer_;
    138   scoped_refptr<cc::Layer> subroot_layer_;
    139 
    140   scoped_ptr<cc::LayerTreeHost> host_;
    141   content::UIResourceProviderImpl ui_resource_provider_;
    142 
    143   gfx::Size size_;
    144   bool has_transparent_background_;
    145   float device_scale_factor_;
    146 
    147   ANativeWindow* window_;
    148   int surface_id_;
    149 
    150   CompositorClient* client_;
    151 
    152   gfx::NativeWindow root_window_;
    153 
    154   // Used locally to track whether a call to LTH::Composite() did result in
    155   // a posted SwapBuffers().
    156   bool did_post_swapbuffers_;
    157 
    158   // Used locally to inhibit ScheduleComposite() during Layout().
    159   bool ignore_schedule_composite_;
    160 
    161   // Whether we need to composite in general because of any invalidation or
    162   // explicit request.
    163   bool needs_composite_;
    164 
    165   // Whether we need to update animations on the next composite.
    166   bool needs_animate_;
    167 
    168   // Whether we posted a task and are about to composite.
    169   bool will_composite_immediately_;
    170 
    171   // How we should schedule Composite during the next vsync.
    172   CompositingTrigger composite_on_vsync_trigger_;
    173 
    174   // The Composite operation scheduled for the current vsync interval.
    175   scoped_ptr<base::CancelableClosure> current_composite_task_;
    176 
    177   // The number of SwapBuffer calls that have not returned and ACK'd from
    178   // the GPU thread.
    179   unsigned int pending_swapbuffers_;
    180 
    181   base::TimeDelta vsync_period_;
    182   base::TimeTicks last_vsync_;
    183 
    184   base::WeakPtrFactory<CompositorImpl> weak_factory_;
    185 
    186   DISALLOW_COPY_AND_ASSIGN(CompositorImpl);
    187 };
    188 
    189 } // namespace content
    190 
    191 #endif  // CONTENT_BROWSER_RENDERER_HOST_COMPOSITOR_IMPL_ANDROID_H_
    192