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 
     79  private:
     80   virtual ~ReflectorImpl();
     81 
     82   void UpdateTextureSizeOnMainThread(gfx::Size size);
     83 
     84   // Request full redraw on mirroring compositor.
     85   void FullRedrawOnMainThread(gfx::Size size);
     86 
     87   void UpdateSubBufferOnMainThread(gfx::Size size, gfx::Rect rect);
     88 
     89   // Request full redraw on mirrored compositor so that
     90   // the full content will be copied to mirroring compositor.
     91   void FullRedrawContentOnMainThread();
     92 
     93   // This exists just to hold a reference to a ReflectorImpl in a post task,
     94   // so the ReflectorImpl gets deleted when the function returns.
     95   static void DeleteOnMainThread(scoped_refptr<ReflectorImpl> reflector) {}
     96 
     97   // These variables are initialized on MainThread before
     98   // the reflector is attached to the output surface. Once
     99   // attached, they must be accessed only on ImplThraed unless
    100   // the context is lost. When the context is lost, these
    101   // will be re-ininitiailzed when the new output-surface
    102   // is created on MainThread.
    103   int texture_id_;
    104   gfx::Size texture_size_;
    105 
    106   // Must be accessed only on ImplThread.
    107   IDMap<BrowserCompositorOutputSurface>* output_surface_map_;
    108   scoped_ptr<GLHelper> gl_helper_;
    109 
    110   // Must be accessed only on MainThread.
    111   ui::Compositor* mirrored_compositor_;
    112   ui::Compositor* mirroring_compositor_;
    113   ui::Layer* mirroring_layer_;
    114   scoped_refptr<ui::Texture> shared_texture_;
    115   scoped_refptr<base::MessageLoopProxy> impl_message_loop_;
    116   scoped_refptr<base::MessageLoopProxy> main_message_loop_;
    117   int surface_id_;
    118 };
    119 
    120 }  // namespace content
    121 
    122 #endif  // CONTENT_BROWSER_AURA_REFLECTOR_IMPL_H_
    123