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.
5 #ifndef CONTENT_BROWSER_COMPOSITOR_REFLECTOR_IMPL_H_
6 #define CONTENT_BROWSER_COMPOSITOR_REFLECTOR_IMPL_H_
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 "ui/compositor/reflector.h"
14 #include "ui/gfx/size.h"
16 namespace base
{ class MessageLoopProxy
; }
18 namespace gfx
{ class Rect
; }
27 class BrowserCompositorOutputSurface
;
29 // A reflector implementation that copies the framebuffer content
30 // to the texture, then draw it onto the mirroring compositor.
31 class ReflectorImpl
: public ImageTransportFactoryObserver
,
32 public base::SupportsWeakPtr
<ReflectorImpl
>,
33 public ui::Reflector
{
36 ui::Compositor
* mirrored_compositor
,
37 ui::Layer
* mirroring_layer
,
38 IDMap
<BrowserCompositorOutputSurface
>* output_surface_map
,
41 ui::Compositor
* mirrored_compositor() {
42 return mirrored_compositor_
;
45 void InitOnImplThread();
47 void ShutdownOnImplThread();
49 // Post a task to attach the reflector to the output surface onto ImplThread.
50 void ReattachToOutputSurfaceFromMainThread(
51 BrowserCompositorOutputSurface
* surface
);
53 // ui::Reflector implementation.
54 virtual void OnMirroringCompositorResized() OVERRIDE
;
56 // ImageTransportFactoryObsever implementation.
57 virtual void OnLostResources() OVERRIDE
;
59 // Called when the output surface's size has changed.
60 // This must be called on ImplThread.
61 void OnReshape(gfx::Size size
);
63 // Called in |BrowserCompositorOutputSurface::SwapBuffers| to copy
64 // the full screen image to the |texture_id_|. This must be called
68 // Called in |BrowserCompositorOutputSurface::PostSubBuffer| copy
69 // the sub image given by |rect| to the texture.This must be called
71 void OnPostSubBuffer(gfx::Rect rect
);
73 // Create a shared texture that will be used to copy the content of
74 // mirrored compositor to the mirroring compositor. This should
75 // be posted to the main thread when the output is attached in
77 void CreateSharedTextureOnMainThread(gfx::Size size
);
79 // Called when the source surface is bound and available. This must
80 // be called on ImplThread.
81 void OnSourceSurfaceReady(int surface_id
);
84 virtual ~ReflectorImpl();
86 void AttachToOutputSurfaceOnImplThread(
87 BrowserCompositorOutputSurface
* surface
);
89 void UpdateTextureSizeOnMainThread(gfx::Size size
);
91 // Request full redraw on mirroring compositor.
92 void FullRedrawOnMainThread(gfx::Size size
);
94 void UpdateSubBufferOnMainThread(gfx::Size size
, gfx::Rect rect
);
96 // Request full redraw on mirrored compositor so that
97 // the full content will be copied to mirroring compositor.
98 void FullRedrawContentOnMainThread();
100 // This exists just to hold a reference to a ReflectorImpl in a post task,
101 // so the ReflectorImpl gets deleted when the function returns.
102 static void DeleteOnMainThread(scoped_refptr
<ReflectorImpl
> reflector
) {}
104 // These variables are initialized on MainThread before
105 // the reflector is attached to the output surface. Once
106 // attached, they must be accessed only on ImplThraed unless
107 // the context is lost. When the context is lost, these
108 // will be re-ininitiailzed when the new output-surface
109 // is created on MainThread.
111 base::Lock texture_lock_
;
112 gfx::Size texture_size_
;
114 // Must be accessed only on ImplThread.
115 IDMap
<BrowserCompositorOutputSurface
>* output_surface_map_
;
116 scoped_ptr
<GLHelper
> gl_helper_
;
118 // Must be accessed only on MainThread.
119 ui::Compositor
* mirrored_compositor_
;
120 ui::Compositor
* mirroring_compositor_
;
121 ui::Layer
* mirroring_layer_
;
122 scoped_refptr
<ui::Texture
> shared_texture_
;
123 scoped_refptr
<base::MessageLoopProxy
> impl_message_loop_
;
124 scoped_refptr
<base::MessageLoopProxy
> main_message_loop_
;
128 } // namespace content
130 #endif // CONTENT_BROWSER_COMPOSITOR_REFLECTOR_IMPL_H_