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 "gpu/command_buffer/common/mailbox_holder.h"
14 #include "ui/compositor/reflector.h"
15 #include "ui/gfx/size.h"
17 namespace base
{ class MessageLoopProxy
; }
19 namespace gfx
{ class Rect
; }
29 class BrowserCompositorOutputSurface
;
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
{
37 ui::Compositor
* mirrored_compositor
,
38 ui::Layer
* mirroring_layer
,
39 IDMap
<BrowserCompositorOutputSurface
>* output_surface_map
,
40 base::MessageLoopProxy
* compositor_thread_loop
,
43 ui::Compositor
* mirrored_compositor() {
44 return GetMain().mirrored_compositor
;
47 void InitOnImplThread(const gpu::MailboxHolder
& mailbox_holder
);
49 void ShutdownOnImplThread();
51 // Post a task to attach the reflector to the output surface onto ImplThread.
52 void ReattachToOutputSurfaceFromMainThread(
53 BrowserCompositorOutputSurface
* surface
);
55 // ui::Reflector implementation.
56 void OnMirroringCompositorResized() override
;
58 // Called in |BrowserCompositorOutputSurface::SwapBuffers| to copy
59 // the full screen image to the |texture_id_|. This must be called
63 // Called in |BrowserCompositorOutputSurface::PostSubBuffer| copy
64 // the sub image given by |rect| to the texture.This must be called
66 void OnPostSubBuffer(gfx::Rect rect
);
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
72 void CreateSharedTextureOnMainThread(gfx::Size size
);
74 // Called when the source surface is bound and available. This must
75 // be called on ImplThread.
76 void OnSourceSurfaceReady(BrowserCompositorOutputSurface
* surface
);
78 void DetachFromOutputSurface();
81 struct MainThreadData
{
82 MainThreadData(ui::Compositor
* mirrored_compositor
,
83 ui::Layer
* mirroring_layer
);
85 scoped_refptr
<OwnedMailbox
> mailbox
;
86 bool needs_set_mailbox
;
87 ui::Compositor
* mirrored_compositor
;
88 ui::Layer
* mirroring_layer
;
91 struct ImplThreadData
{
92 explicit ImplThreadData(
93 IDMap
<BrowserCompositorOutputSurface
>* output_surface_map
);
95 IDMap
<BrowserCompositorOutputSurface
>* output_surface_map
;
96 BrowserCompositorOutputSurface
* output_surface
;
97 scoped_ptr
<GLHelper
> gl_helper
;
99 gpu::MailboxHolder mailbox_holder
;
102 ~ReflectorImpl() override
;
104 void AttachToOutputSurfaceOnImplThread(
105 const gpu::MailboxHolder
& mailbox_holder
,
106 BrowserCompositorOutputSurface
* surface
);
108 void UpdateTextureSizeOnMainThread(gfx::Size size
);
110 // Request full redraw on mirroring compositor.
111 void FullRedrawOnMainThread(gfx::Size size
);
113 void UpdateSubBufferOnMainThread(gfx::Size size
, gfx::Rect rect
);
115 // Request full redraw on mirrored compositor so that
116 // the full content will be copied to mirroring compositor.
117 void FullRedrawContentOnMainThread();
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
) {}
123 MainThreadData
& GetMain();
124 ImplThreadData
& GetImpl();
126 // Must be accessed only on ImplThread, through GetImpl().
127 ImplThreadData impl_unsafe_
;
129 // Must be accessed only on MainThread, through GetMain().
130 MainThreadData main_unsafe_
;
132 // Can be accessed on both.
133 scoped_refptr
<base::MessageLoopProxy
> impl_message_loop_
;
134 scoped_refptr
<base::MessageLoopProxy
> main_message_loop_
;
138 } // namespace content
140 #endif // CONTENT_BROWSER_COMPOSITOR_REFLECTOR_IMPL_H_