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/onscreen_display_client.h"
14 #include "cc/surfaces/surface_display_output_surface.h"
15 #include "cc/surfaces/surface_id_allocator.h"
16 #include "cc/test/pixel_test_output_surface.h"
17 #include "cc/test/test_shared_bitmap_manager.h"
18 #include "gpu/command_buffer/client/context_support.h"
19 #include "gpu/command_buffer/client/gles2_interface.h"
20 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
21 #include "ui/compositor/compositor_switches.h"
22 #include "ui/compositor/reflector.h"
23 #include "ui/compositor/test/in_process_context_provider.h"
24 #include "ui/gl/gl_implementation.h"
25 #include "ui/gl/gl_surface.h"
30 class FakeReflector
: public Reflector
{
33 ~FakeReflector() override
{}
34 void OnMirroringCompositorResized() override
{}
37 // An OutputSurface implementation that directly draws and swaps to an actual
39 class DirectOutputSurface
: public cc::OutputSurface
{
41 explicit DirectOutputSurface(
42 const scoped_refptr
<cc::ContextProvider
>& context_provider
)
43 : cc::OutputSurface(context_provider
), weak_ptr_factory_(this) {}
45 ~DirectOutputSurface() override
{}
47 // cc::OutputSurface implementation
48 void SwapBuffers(cc::CompositorFrame
* frame
) override
{
49 DCHECK(context_provider_
.get());
50 DCHECK(frame
->gl_frame_data
);
51 if (frame
->gl_frame_data
->sub_buffer_rect
==
52 gfx::Rect(frame
->gl_frame_data
->size
)) {
53 context_provider_
->ContextSupport()->Swap();
55 context_provider_
->ContextSupport()->PartialSwapBuffers(
56 frame
->gl_frame_data
->sub_buffer_rect
);
59 context_provider_
->ContextGL()->InsertSyncPointCHROMIUM();
60 context_provider_
->ContextSupport()->SignalSyncPoint(
61 sync_point
, base::Bind(&OutputSurface::OnSwapBuffersComplete
,
62 weak_ptr_factory_
.GetWeakPtr()));
63 client_
->DidSwapBuffers();
67 base::WeakPtrFactory
<DirectOutputSurface
> weak_ptr_factory_
;
69 DISALLOW_COPY_AND_ASSIGN(DirectOutputSurface
);
74 InProcessContextFactory::InProcessContextFactory(
75 bool context_factory_for_test
,
76 cc::SurfaceManager
* surface_manager
)
77 : next_surface_id_namespace_(1u),
78 use_test_surface_(true),
79 context_factory_for_test_(context_factory_for_test
),
80 surface_manager_(surface_manager
) {
81 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone
)
82 << "If running tests, ensure that main() is calling "
83 << "gfx::GLSurface::InitializeOneOffForTests()";
86 InProcessContextFactory::~InProcessContextFactory() {
87 DCHECK(per_compositor_data_
.empty());
90 void InProcessContextFactory::CreateOutputSurface(
91 base::WeakPtr
<Compositor
> compositor
) {
92 gpu::gles2::ContextCreationAttribHelper attribs
;
93 attribs
.alpha_size
= 8;
94 attribs
.blue_size
= 8;
95 attribs
.green_size
= 8;
97 attribs
.depth_size
= 0;
98 attribs
.stencil_size
= 0;
100 attribs
.sample_buffers
= 0;
101 attribs
.fail_if_major_perf_caveat
= false;
102 attribs
.bind_generates_resource
= false;
103 bool lose_context_when_out_of_memory
= true;
105 scoped_refptr
<InProcessContextProvider
> context_provider
=
106 InProcessContextProvider::Create(attribs
, lose_context_when_out_of_memory
,
107 compositor
->widget(), "UICompositor");
109 scoped_ptr
<cc::OutputSurface
> real_output_surface
;
111 if (use_test_surface_
) {
112 bool flipped_output_surface
= false;
113 real_output_surface
= make_scoped_ptr(new cc::PixelTestOutputSurface(
114 context_provider
, flipped_output_surface
));
116 real_output_surface
=
117 make_scoped_ptr(new DirectOutputSurface(context_provider
));
120 if (surface_manager_
) {
121 scoped_ptr
<cc::OnscreenDisplayClient
> display_client(
122 new cc::OnscreenDisplayClient(
123 real_output_surface
.Pass(), surface_manager_
,
124 GetSharedBitmapManager(), GetGpuMemoryBufferManager(),
125 compositor
->GetRendererSettings(), compositor
->task_runner()));
126 scoped_ptr
<cc::SurfaceDisplayOutputSurface
> surface_output_surface(
127 new cc::SurfaceDisplayOutputSurface(surface_manager_
,
128 compositor
->surface_id_allocator(),
130 display_client
->set_surface_output_surface(surface_output_surface
.get());
131 surface_output_surface
->set_display_client(display_client
.get());
133 compositor
->SetOutputSurface(surface_output_surface
.Pass());
135 delete per_compositor_data_
[compositor
.get()];
136 per_compositor_data_
[compositor
.get()] = display_client
.release();
138 compositor
->SetOutputSurface(real_output_surface
.Pass());
142 scoped_ptr
<Reflector
> InProcessContextFactory::CreateReflector(
143 Compositor
* mirrored_compositor
,
144 Layer
* mirroring_layer
) {
145 return make_scoped_ptr(new FakeReflector
);
148 void InProcessContextFactory::RemoveReflector(Reflector
* reflector
) {
151 scoped_refptr
<cc::ContextProvider
>
152 InProcessContextFactory::SharedMainThreadContextProvider() {
153 if (shared_main_thread_contexts_
.get() &&
154 !shared_main_thread_contexts_
->DestroyedOnMainThread())
155 return shared_main_thread_contexts_
;
157 bool lose_context_when_out_of_memory
= false;
158 shared_main_thread_contexts_
= InProcessContextProvider::CreateOffscreen(
159 lose_context_when_out_of_memory
);
160 if (shared_main_thread_contexts_
.get() &&
161 !shared_main_thread_contexts_
->BindToCurrentThread())
162 shared_main_thread_contexts_
= NULL
;
164 return shared_main_thread_contexts_
;
167 void InProcessContextFactory::RemoveCompositor(Compositor
* compositor
) {
168 if (!per_compositor_data_
.count(compositor
))
170 delete per_compositor_data_
[compositor
];
171 per_compositor_data_
.erase(compositor
);
174 bool InProcessContextFactory::DoesCreateTestContexts() {
175 return context_factory_for_test_
;
178 uint32
InProcessContextFactory::GetImageTextureTarget() {
179 return GL_TEXTURE_2D
;
182 cc::SharedBitmapManager
* InProcessContextFactory::GetSharedBitmapManager() {
183 return &shared_bitmap_manager_
;
186 gpu::GpuMemoryBufferManager
*
187 InProcessContextFactory::GetGpuMemoryBufferManager() {
188 return &gpu_memory_buffer_manager_
;
191 cc::TaskGraphRunner
* InProcessContextFactory::GetTaskGraphRunner() {
192 return &task_graph_runner_
;
195 scoped_ptr
<cc::SurfaceIdAllocator
>
196 InProcessContextFactory::CreateSurfaceIdAllocator() {
197 return make_scoped_ptr(
198 new cc::SurfaceIdAllocator(next_surface_id_namespace_
++));
201 void InProcessContextFactory::ResizeDisplay(ui::Compositor
* compositor
,
202 const gfx::Size
& size
) {
203 if (!per_compositor_data_
.count(compositor
))
205 per_compositor_data_
[compositor
]->display()->Resize(size
);