Home | History | Annotate | Download | only in aura
      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 CONTENT_BROWSER_AURA_REFLECTOR_IMPL_H_
      6 #define CONTENT_BROWSER_AURA_REFLECTOR_IMPL_H_
      7 
      8 #include "base/id_map.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "content/browser/aura/image_transport_factory.h"
     12 #include "ui/compositor/reflector.h"
     13 #include "ui/gfx/size.h"
     14 
     15 namespace base { class MessageLoopProxy; }
     16 
     17 namespace gfx { class Rect; }
     18 
     19 namespace ui {
     20 class Compositor;
     21 class Layer;
     22 }
     23 
     24 namespace content {
     25 
     26 class BrowserCompositorOutputSurface;
     27 
     28 // A reflector implementation that copies the framebuffer content
     29 // to the texture, then draw it onto the mirroring compositor.
     30 class ReflectorImpl : public ImageTransportFactoryObserver,
     31                       public base::SupportsWeakPtr<ReflectorImpl>,
     32                       public ui::Reflector {
     33  public:
     34   ReflectorImpl(
     35       ui::Compositor* mirrored_compositor,
     36       ui::Layer* mirroring_layer,
     37       IDMap<BrowserCompositorOutputSurface>* output_surface_map,
     38       int surface_id);
     39 
     40   ui::Compositor* mirrored_compositor() {
     41     return mirrored_compositor_;
     42   }
     43 
     44   void InitOnImplThread();
     45   void Shutdown();
     46   void ShutdownOnImplThread();
     47 
     48   // This must be called on ImplThread, or before the surface is passed to
     49   // ImplThread.
     50   void AttachToOutputSurface(BrowserCompositorOutputSurface* surface);
     51 
     52   // ui::Reflector implementation.
     53   virtual void OnMirroringCompositorResized() OVERRIDE;
     54 
     55   // ImageTransportFactoryObsever implementation.
     56   virtual void OnLostResources() OVERRIDE;
     57 
     58   // Called when the output surface's size has changed.
     59   // This must be called on ImplThread.
     60   void OnReshape(gfx::Size size);
     61 
     62   // Called in |BrowserCompositorOutputSurface::SwapBuffers| to copy
     63   // the full screen image to the |texture_id_|. This must be called
     64   // on ImplThread.
     65   void OnSwapBuffers();
     66 
     67   // Called in |BrowserCompositorOutputSurface::PostSubBuffer| copy
     68   // the sub image given by |rect| to the texture.This must be called
     69   // on ImplThread.
     70   void OnPostSubBuffer(gfx::Rect rect);
     71 
     72   // Create a shared texture that will be used to copy the content of
     73   // mirrored compositor to the mirroring compositor.  This must be
     74   // called before the reflector is attached to OutputSurface to avoid
     75   // race with ImplThread accessing |texture_id_|.
     76   void CreateSharedTexture();
     77 
     78   // Called when the source surface is bound and available. This must
     79   // be called on ImplThread.
     80   void OnSourceSurfaceReady(int surface_id);
     81 
     82  private:
     83   virtual ~ReflectorImpl();
     84 
     85   void UpdateTextureSizeOnMainThread(gfx::Size size);
     86 
     87   // Request full redraw on mirroring compositor.
     88   void FullRedrawOnMainThread(gfx::Size size);
     89 
     90   void UpdateSubBufferOnMainThread(gfx::Size size, gfx::Rect rect);
     91 
     92   // Request full redraw on mirrored compositor so that
     93   // the full content will be copied to mirroring compositor.
     94   void FullRedrawContentOnMainThread();
     95 
     96   // This exists just to hold a reference to a ReflectorImpl in a post task,
     97   // so the ReflectorImpl gets deleted when the function returns.
     98   static void DeleteOnMainThread(scoped_refptr<ReflectorImpl> reflector) {}
     99 
    100   // These variables are initialized on MainThread before
    101   // the reflector is attached to the output surface. Once
    102   // attached, they must be accessed only on ImplThraed unless
    103   // the context is lost. When the context is lost, these
    104   // will be re-ininitiailzed when the new output-surface
    105   // is created on MainThread.
    106   int texture_id_;
    107   gfx::Size texture_size_;
    108 
    109   // Must be accessed only on ImplThread.
    110   IDMap<BrowserCompositorOutputSurface>* output_surface_map_;
    111   scoped_ptr<GLHelper> gl_helper_;
    112 
    113   // Must be accessed only on MainThread.
    114   ui::Compositor* mirrored_compositor_;
    115   ui::Compositor* mirroring_compositor_;
    116   ui::Layer* mirroring_layer_;
    117   scoped_refptr<ui::Texture> shared_texture_;
    118   scoped_refptr<base::MessageLoopProxy> impl_message_loop_;
    119   scoped_refptr<base::MessageLoopProxy> main_message_loop_;
    120   int surface_id_;
    121 };
    122 
    123 }  // namespace content
    124 
    125 #endif  // CONTENT_BROWSER_AURA_REFLECTOR_IMPL_H_
    126