Home | History | Annotate | Download | only in compositor
      1 // Copyright 2014 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_COMPOSITOR_REFLECTOR_IMPL_H_
      6 #define CONTENT_BROWSER_COMPOSITOR_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 "base/synchronization/lock.h"
     12 #include "content/browser/compositor/image_transport_factory.h"
     13 #include "gpu/command_buffer/common/mailbox_holder.h"
     14 #include "ui/compositor/reflector.h"
     15 #include "ui/gfx/size.h"
     16 
     17 namespace base { class MessageLoopProxy; }
     18 
     19 namespace gfx { class Rect; }
     20 
     21 namespace ui {
     22 class Compositor;
     23 class Layer;
     24 }
     25 
     26 namespace content {
     27 
     28 class OwnedMailbox;
     29 class BrowserCompositorOutputSurface;
     30 
     31 // A reflector implementation that copies the framebuffer content
     32 // to the texture, then draw it onto the mirroring compositor.
     33 class ReflectorImpl : public base::SupportsWeakPtr<ReflectorImpl>,
     34                       public ui::Reflector {
     35  public:
     36   ReflectorImpl(
     37       ui::Compositor* mirrored_compositor,
     38       ui::Layer* mirroring_layer,
     39       IDMap<BrowserCompositorOutputSurface>* output_surface_map,
     40       base::MessageLoopProxy* compositor_thread_loop,
     41       int surface_id);
     42 
     43   ui::Compositor* mirrored_compositor() {
     44     return GetMain().mirrored_compositor;
     45   }
     46 
     47   void InitOnImplThread(const gpu::MailboxHolder& mailbox_holder);
     48   void Shutdown();
     49   void ShutdownOnImplThread();
     50 
     51   // Post a task to attach the reflector to the output surface onto ImplThread.
     52   void ReattachToOutputSurfaceFromMainThread(
     53       BrowserCompositorOutputSurface* surface);
     54 
     55   // ui::Reflector implementation.
     56   virtual void OnMirroringCompositorResized() OVERRIDE;
     57 
     58   // Called in |BrowserCompositorOutputSurface::SwapBuffers| to copy
     59   // the full screen image to the |texture_id_|. This must be called
     60   // on ImplThread.
     61   void OnSwapBuffers();
     62 
     63   // Called in |BrowserCompositorOutputSurface::PostSubBuffer| copy
     64   // the sub image given by |rect| to the texture.This must be called
     65   // on ImplThread.
     66   void OnPostSubBuffer(gfx::Rect rect);
     67 
     68   // Create a shared texture that will be used to copy the content of
     69   // mirrored compositor to the mirroring compositor.  This should
     70   // be posted to the main thread when the output is attached in
     71   // impl thread.
     72   void CreateSharedTextureOnMainThread(gfx::Size size);
     73 
     74   // Called when the source surface is bound and available. This must
     75   // be called on ImplThread.
     76   void OnSourceSurfaceReady(BrowserCompositorOutputSurface* surface);
     77 
     78   void DetachFromOutputSurface();
     79 
     80  private:
     81   struct MainThreadData {
     82     MainThreadData(ui::Compositor* mirrored_compositor,
     83                    ui::Layer* mirroring_layer);
     84     ~MainThreadData();
     85     scoped_refptr<OwnedMailbox> mailbox;
     86     bool needs_set_mailbox;
     87     ui::Compositor* mirrored_compositor;
     88     ui::Layer* mirroring_layer;
     89   };
     90 
     91   struct ImplThreadData {
     92     explicit ImplThreadData(
     93         IDMap<BrowserCompositorOutputSurface>* output_surface_map);
     94     ~ImplThreadData();
     95     IDMap<BrowserCompositorOutputSurface>* output_surface_map;
     96     BrowserCompositorOutputSurface* output_surface;
     97     scoped_ptr<GLHelper> gl_helper;
     98     unsigned texture_id;
     99     gpu::MailboxHolder mailbox_holder;
    100   };
    101 
    102   virtual ~ReflectorImpl();
    103 
    104   void AttachToOutputSurfaceOnImplThread(
    105       const gpu::MailboxHolder& mailbox_holder,
    106       BrowserCompositorOutputSurface* surface);
    107 
    108   void UpdateTextureSizeOnMainThread(gfx::Size size);
    109 
    110   // Request full redraw on mirroring compositor.
    111   void FullRedrawOnMainThread(gfx::Size size);
    112 
    113   void UpdateSubBufferOnMainThread(gfx::Size size, gfx::Rect rect);
    114 
    115   // Request full redraw on mirrored compositor so that
    116   // the full content will be copied to mirroring compositor.
    117   void FullRedrawContentOnMainThread();
    118 
    119   // This exists just to hold a reference to a ReflectorImpl in a post task,
    120   // so the ReflectorImpl gets deleted when the function returns.
    121   static void DeleteOnMainThread(scoped_refptr<ReflectorImpl> reflector) {}
    122 
    123   MainThreadData& GetMain();
    124   ImplThreadData& GetImpl();
    125 
    126   // Must be accessed only on ImplThread, through GetImpl().
    127   ImplThreadData impl_unsafe_;
    128 
    129   // Must be accessed only on MainThread, through GetMain().
    130   MainThreadData main_unsafe_;
    131 
    132   // Can be accessed on both.
    133   scoped_refptr<base::MessageLoopProxy> impl_message_loop_;
    134   scoped_refptr<base::MessageLoopProxy> main_message_loop_;
    135   int surface_id_;
    136 };
    137 
    138 }  // namespace content
    139 
    140 #endif  // CONTENT_BROWSER_COMPOSITOR_REFLECTOR_IMPL_H_
    141