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 #include "ui/compositor/test/in_process_context_factory.h"
8 #include "base/command_line.h"
9 #include "base/threading/thread.h"
10 #include "cc/output/compositor_frame.h"
11 #include "cc/output/context_provider.h"
12 #include "cc/output/output_surface_client.h"
13 #include "cc/surfaces/surface_id_allocator.h"
14 #include "cc/test/pixel_test_output_surface.h"
15 #include "cc/test/test_shared_bitmap_manager.h"
16 #include "gpu/command_buffer/client/context_support.h"
17 #include "gpu/command_buffer/client/gles2_interface.h"
18 #include "ui/compositor/compositor_switches.h"
19 #include "ui/compositor/reflector.h"
20 #include "ui/gl/gl_implementation.h"
21 #include "ui/gl/gl_surface.h"
22 #include "webkit/common/gpu/context_provider_in_process.h"
23 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h"
24 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
29 // An OutputSurface implementation that directly draws and swaps to an actual
31 class DirectOutputSurface
: public cc::OutputSurface
{
33 explicit DirectOutputSurface(
34 const scoped_refptr
<cc::ContextProvider
>& context_provider
)
35 : cc::OutputSurface(context_provider
), weak_ptr_factory_(this) {}
37 ~DirectOutputSurface() override
{}
39 // cc::OutputSurface implementation
40 void SwapBuffers(cc::CompositorFrame
* frame
) override
{
41 DCHECK(context_provider_
.get());
42 DCHECK(frame
->gl_frame_data
);
43 if (frame
->gl_frame_data
->sub_buffer_rect
==
44 gfx::Rect(frame
->gl_frame_data
->size
)) {
45 context_provider_
->ContextSupport()->Swap();
47 context_provider_
->ContextSupport()->PartialSwapBuffers(
48 frame
->gl_frame_data
->sub_buffer_rect
);
51 context_provider_
->ContextGL()->InsertSyncPointCHROMIUM();
52 context_provider_
->ContextSupport()->SignalSyncPoint(
53 sync_point
, base::Bind(&OutputSurface::OnSwapBuffersComplete
,
54 weak_ptr_factory_
.GetWeakPtr()));
55 client_
->DidSwapBuffers();
59 base::WeakPtrFactory
<DirectOutputSurface
> weak_ptr_factory_
;
61 DISALLOW_COPY_AND_ASSIGN(DirectOutputSurface
);
66 InProcessContextFactory::InProcessContextFactory()
67 : next_surface_id_namespace_(1u), use_test_surface_(true) {
68 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone
)
69 << "If running tests, ensure that main() is calling "
70 << "gfx::GLSurface::InitializeOneOffForTests()";
72 #if defined(OS_CHROMEOS)
73 bool use_thread
= !CommandLine::ForCurrentProcess()->HasSwitch(
74 switches::kUIDisableThreadedCompositing
);
76 bool use_thread
= false;
79 compositor_thread_
.reset(new base::Thread("Browser Compositor"));
80 compositor_thread_
->Start();
84 InProcessContextFactory::~InProcessContextFactory() {}
86 void InProcessContextFactory::CreateOutputSurface(
87 base::WeakPtr
<Compositor
> compositor
,
88 bool software_fallback
) {
89 DCHECK(!software_fallback
);
90 blink::WebGraphicsContext3D::Attributes attrs
;
92 attrs
.stencil
= false;
93 attrs
.antialias
= false;
94 attrs
.shareResources
= true;
95 bool lose_context_when_out_of_memory
= true;
97 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl
;
98 scoped_ptr
<WebGraphicsContext3DInProcessCommandBufferImpl
> context3d(
99 WebGraphicsContext3DInProcessCommandBufferImpl::CreateViewContext(
100 attrs
, lose_context_when_out_of_memory
, compositor
->widget()));
103 using webkit::gpu::ContextProviderInProcess
;
104 scoped_refptr
<ContextProviderInProcess
> context_provider
=
105 ContextProviderInProcess::Create(context3d
.Pass(), "UICompositor");
107 if (use_test_surface_
) {
108 bool flipped_output_surface
= false;
109 compositor
->SetOutputSurface(make_scoped_ptr(new cc::PixelTestOutputSurface(
110 context_provider
, flipped_output_surface
)));
112 compositor
->SetOutputSurface(
113 make_scoped_ptr(new DirectOutputSurface(context_provider
)));
117 scoped_refptr
<Reflector
> InProcessContextFactory::CreateReflector(
118 Compositor
* mirroed_compositor
,
119 Layer
* mirroring_layer
) {
120 return new Reflector();
123 void InProcessContextFactory::RemoveReflector(
124 scoped_refptr
<Reflector
> reflector
) {}
126 scoped_refptr
<cc::ContextProvider
>
127 InProcessContextFactory::SharedMainThreadContextProvider() {
128 if (shared_main_thread_contexts_
.get() &&
129 !shared_main_thread_contexts_
->DestroyedOnMainThread())
130 return shared_main_thread_contexts_
;
132 bool lose_context_when_out_of_memory
= false;
133 shared_main_thread_contexts_
=
134 webkit::gpu::ContextProviderInProcess::CreateOffscreen(
135 lose_context_when_out_of_memory
);
136 if (shared_main_thread_contexts_
.get() &&
137 !shared_main_thread_contexts_
->BindToCurrentThread())
138 shared_main_thread_contexts_
= NULL
;
140 return shared_main_thread_contexts_
;
143 void InProcessContextFactory::RemoveCompositor(Compositor
* compositor
) {}
145 bool InProcessContextFactory::DoesCreateTestContexts() { return false; }
147 cc::SharedBitmapManager
* InProcessContextFactory::GetSharedBitmapManager() {
148 return &shared_bitmap_manager_
;
151 gpu::GpuMemoryBufferManager
*
152 InProcessContextFactory::GetGpuMemoryBufferManager() {
153 return &gpu_memory_buffer_manager_
;
156 base::MessageLoopProxy
* InProcessContextFactory::GetCompositorMessageLoop() {
157 if (!compositor_thread_
)
159 return compositor_thread_
->message_loop_proxy().get();
162 scoped_ptr
<cc::SurfaceIdAllocator
>
163 InProcessContextFactory::CreateSurfaceIdAllocator() {
164 return make_scoped_ptr(
165 new cc::SurfaceIdAllocator(next_surface_id_namespace_
++));