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/layer.h"
23 #include "ui/compositor/reflector.h"
24 #include "ui/compositor/test/in_process_context_provider.h"
25 #include "ui/gl/gl_implementation.h"
26 #include "ui/gl/test/gl_surface_test_support.h"
31 class FakeReflector
: public Reflector
{
34 ~FakeReflector() override
{}
35 void OnMirroringCompositorResized() override
{}
36 void AddMirroringLayer(Layer
* layer
) override
{}
37 void RemoveMirroringLayer(Layer
* layer
) override
{}
40 // An OutputSurface implementation that directly draws and swaps to an actual
42 class DirectOutputSurface
: public cc::OutputSurface
{
44 explicit DirectOutputSurface(
45 const scoped_refptr
<cc::ContextProvider
>& context_provider
)
46 : cc::OutputSurface(context_provider
), weak_ptr_factory_(this) {}
48 ~DirectOutputSurface() override
{}
50 // cc::OutputSurface implementation
51 void SwapBuffers(cc::CompositorFrame
* frame
) override
{
52 DCHECK(context_provider_
.get());
53 DCHECK(frame
->gl_frame_data
);
54 if (frame
->gl_frame_data
->sub_buffer_rect
==
55 gfx::Rect(frame
->gl_frame_data
->size
)) {
56 context_provider_
->ContextSupport()->Swap();
58 context_provider_
->ContextSupport()->PartialSwapBuffers(
59 frame
->gl_frame_data
->sub_buffer_rect
);
62 context_provider_
->ContextGL()->InsertSyncPointCHROMIUM();
63 context_provider_
->ContextSupport()->SignalSyncPoint(
64 sync_point
, base::Bind(&OutputSurface::OnSwapBuffersComplete
,
65 weak_ptr_factory_
.GetWeakPtr()));
66 client_
->DidSwapBuffers();
70 base::WeakPtrFactory
<DirectOutputSurface
> weak_ptr_factory_
;
72 DISALLOW_COPY_AND_ASSIGN(DirectOutputSurface
);
77 InProcessContextFactory::InProcessContextFactory(
78 bool context_factory_for_test
,
79 cc::SurfaceManager
* surface_manager
)
80 : next_surface_id_namespace_(1u),
81 use_test_surface_(true),
82 context_factory_for_test_(context_factory_for_test
),
83 surface_manager_(surface_manager
) {
84 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone
)
85 << "If running tests, ensure that main() is calling "
86 << "gfx::GLSurfaceTestSupport::InitializeOneOff()";
88 Layer::InitializeUILayerSettings();
91 InProcessContextFactory::~InProcessContextFactory() {
92 DCHECK(per_compositor_data_
.empty());
95 void InProcessContextFactory::CreateOutputSurface(
96 base::WeakPtr
<Compositor
> compositor
) {
97 gpu::gles2::ContextCreationAttribHelper attribs
;
98 attribs
.alpha_size
= 8;
99 attribs
.blue_size
= 8;
100 attribs
.green_size
= 8;
101 attribs
.red_size
= 8;
102 attribs
.depth_size
= 0;
103 attribs
.stencil_size
= 0;
105 attribs
.sample_buffers
= 0;
106 attribs
.fail_if_major_perf_caveat
= false;
107 attribs
.bind_generates_resource
= false;
109 scoped_refptr
<InProcessContextProvider
> context_provider
=
110 InProcessContextProvider::Create(attribs
, &gpu_memory_buffer_manager_
,
112 compositor
->widget(), "UICompositor");
114 scoped_ptr
<cc::OutputSurface
> real_output_surface
;
116 if (use_test_surface_
) {
117 bool flipped_output_surface
= false;
118 real_output_surface
= make_scoped_ptr(new cc::PixelTestOutputSurface(
119 context_provider
, flipped_output_surface
));
121 real_output_surface
=
122 make_scoped_ptr(new DirectOutputSurface(context_provider
));
125 if (surface_manager_
) {
126 scoped_ptr
<cc::OnscreenDisplayClient
> display_client(
127 new cc::OnscreenDisplayClient(
128 real_output_surface
.Pass(), surface_manager_
,
129 GetSharedBitmapManager(), GetGpuMemoryBufferManager(),
130 compositor
->GetRendererSettings(), compositor
->task_runner()));
131 scoped_ptr
<cc::SurfaceDisplayOutputSurface
> surface_output_surface(
132 new cc::SurfaceDisplayOutputSurface(surface_manager_
,
133 compositor
->surface_id_allocator(),
135 display_client
->set_surface_output_surface(surface_output_surface
.get());
136 surface_output_surface
->set_display_client(display_client
.get());
138 compositor
->SetOutputSurface(surface_output_surface
.Pass());
140 delete per_compositor_data_
[compositor
.get()];
141 per_compositor_data_
[compositor
.get()] = display_client
.release();
143 compositor
->SetOutputSurface(real_output_surface
.Pass());
147 scoped_ptr
<Reflector
> InProcessContextFactory::CreateReflector(
148 Compositor
* mirrored_compositor
,
149 Layer
* mirroring_layer
) {
150 return make_scoped_ptr(new FakeReflector
);
153 void InProcessContextFactory::RemoveReflector(Reflector
* reflector
) {
156 scoped_refptr
<cc::ContextProvider
>
157 InProcessContextFactory::SharedMainThreadContextProvider() {
158 if (shared_main_thread_contexts_
.get() &&
159 !shared_main_thread_contexts_
->DestroyedOnMainThread())
160 return shared_main_thread_contexts_
;
162 shared_main_thread_contexts_
= InProcessContextProvider::CreateOffscreen(
163 &gpu_memory_buffer_manager_
, &image_factory_
);
164 if (shared_main_thread_contexts_
.get() &&
165 !shared_main_thread_contexts_
->BindToCurrentThread())
166 shared_main_thread_contexts_
= NULL
;
168 return shared_main_thread_contexts_
;
171 void InProcessContextFactory::RemoveCompositor(Compositor
* compositor
) {
172 if (!per_compositor_data_
.count(compositor
))
174 delete per_compositor_data_
[compositor
];
175 per_compositor_data_
.erase(compositor
);
178 bool InProcessContextFactory::DoesCreateTestContexts() {
179 return context_factory_for_test_
;
182 uint32
InProcessContextFactory::GetImageTextureTarget(
183 gfx::GpuMemoryBuffer::Format format
,
184 gfx::GpuMemoryBuffer::Usage usage
) {
185 return GL_TEXTURE_2D
;
188 cc::SharedBitmapManager
* InProcessContextFactory::GetSharedBitmapManager() {
189 return &shared_bitmap_manager_
;
192 gpu::GpuMemoryBufferManager
*
193 InProcessContextFactory::GetGpuMemoryBufferManager() {
194 return &gpu_memory_buffer_manager_
;
197 cc::TaskGraphRunner
* InProcessContextFactory::GetTaskGraphRunner() {
198 return &task_graph_runner_
;
201 scoped_ptr
<cc::SurfaceIdAllocator
>
202 InProcessContextFactory::CreateSurfaceIdAllocator() {
203 return make_scoped_ptr(
204 new cc::SurfaceIdAllocator(next_surface_id_namespace_
++));
207 void InProcessContextFactory::ResizeDisplay(ui::Compositor
* compositor
,
208 const gfx::Size
& size
) {
209 if (!per_compositor_data_
.count(compositor
))
211 per_compositor_data_
[compositor
]->display()->Resize(size
);