Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / compositor / test / in_process_context_factory.cc
blob942947d5b1b020f3ebfcfe21cc7cf2097d2067cb
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"
7 #include "base/bind.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"
28 namespace ui {
29 namespace {
31 class FakeReflector : public Reflector {
32 public:
33 FakeReflector() {}
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
41 // GL surface.
42 class DirectOutputSurface : public cc::OutputSurface {
43 public:
44 DirectOutputSurface(
45 const scoped_refptr<cc::ContextProvider>& context_provider,
46 const scoped_refptr<cc::ContextProvider>& worker_context_provider)
47 : cc::OutputSurface(context_provider, worker_context_provider),
48 weak_ptr_factory_(this) {}
50 ~DirectOutputSurface() override {}
52 // cc::OutputSurface implementation
53 void SwapBuffers(cc::CompositorFrame* frame) override {
54 DCHECK(context_provider_.get());
55 DCHECK(frame->gl_frame_data);
56 if (frame->gl_frame_data->sub_buffer_rect ==
57 gfx::Rect(frame->gl_frame_data->size)) {
58 context_provider_->ContextSupport()->Swap();
59 } else {
60 context_provider_->ContextSupport()->PartialSwapBuffers(
61 frame->gl_frame_data->sub_buffer_rect);
63 uint32_t sync_point =
64 context_provider_->ContextGL()->InsertSyncPointCHROMIUM();
65 context_provider_->ContextSupport()->SignalSyncPoint(
66 sync_point, base::Bind(&OutputSurface::OnSwapBuffersComplete,
67 weak_ptr_factory_.GetWeakPtr()));
68 client_->DidSwapBuffers();
71 private:
72 base::WeakPtrFactory<DirectOutputSurface> weak_ptr_factory_;
74 DISALLOW_COPY_AND_ASSIGN(DirectOutputSurface);
77 } // namespace
79 InProcessContextFactory::InProcessContextFactory(
80 bool context_factory_for_test,
81 cc::SurfaceManager* surface_manager)
82 : next_surface_id_namespace_(1u),
83 use_test_surface_(true),
84 context_factory_for_test_(context_factory_for_test),
85 surface_manager_(surface_manager) {
86 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone)
87 << "If running tests, ensure that main() is calling "
88 << "gfx::GLSurfaceTestSupport::InitializeOneOff()";
90 Layer::InitializeUILayerSettings();
93 InProcessContextFactory::~InProcessContextFactory() {
94 DCHECK(per_compositor_data_.empty());
97 void InProcessContextFactory::CreateOutputSurface(
98 base::WeakPtr<Compositor> compositor) {
99 gpu::gles2::ContextCreationAttribHelper attribs;
100 attribs.alpha_size = 8;
101 attribs.blue_size = 8;
102 attribs.green_size = 8;
103 attribs.red_size = 8;
104 attribs.depth_size = 0;
105 attribs.stencil_size = 0;
106 attribs.samples = 0;
107 attribs.sample_buffers = 0;
108 attribs.fail_if_major_perf_caveat = false;
109 attribs.bind_generates_resource = false;
111 scoped_refptr<InProcessContextProvider> context_provider =
112 InProcessContextProvider::Create(attribs, &gpu_memory_buffer_manager_,
113 &image_factory_,
114 compositor->widget(), "UICompositor");
115 scoped_refptr<InProcessContextProvider> worker_context_provider =
116 InProcessContextProvider::CreateOffscreen(&gpu_memory_buffer_manager_,
117 &image_factory_);
119 scoped_ptr<cc::OutputSurface> real_output_surface;
121 if (use_test_surface_) {
122 bool flipped_output_surface = false;
123 real_output_surface = make_scoped_ptr(new cc::PixelTestOutputSurface(
124 context_provider, worker_context_provider, flipped_output_surface));
125 } else {
126 real_output_surface = make_scoped_ptr(
127 new DirectOutputSurface(context_provider, worker_context_provider));
130 if (surface_manager_) {
131 scoped_ptr<cc::OnscreenDisplayClient> display_client(
132 new cc::OnscreenDisplayClient(
133 real_output_surface.Pass(), surface_manager_,
134 GetSharedBitmapManager(), GetGpuMemoryBufferManager(),
135 compositor->GetRendererSettings(), compositor->task_runner()));
136 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
137 new cc::SurfaceDisplayOutputSurface(
138 surface_manager_, compositor->surface_id_allocator(),
139 context_provider, worker_context_provider));
140 display_client->set_surface_output_surface(surface_output_surface.get());
141 surface_output_surface->set_display_client(display_client.get());
143 compositor->SetOutputSurface(surface_output_surface.Pass());
145 delete per_compositor_data_[compositor.get()];
146 per_compositor_data_[compositor.get()] = display_client.release();
147 } else {
148 compositor->SetOutputSurface(real_output_surface.Pass());
152 scoped_ptr<Reflector> InProcessContextFactory::CreateReflector(
153 Compositor* mirrored_compositor,
154 Layer* mirroring_layer) {
155 return make_scoped_ptr(new FakeReflector);
158 void InProcessContextFactory::RemoveReflector(Reflector* reflector) {
161 scoped_refptr<cc::ContextProvider>
162 InProcessContextFactory::SharedMainThreadContextProvider() {
163 if (shared_main_thread_contexts_.get() &&
164 !shared_main_thread_contexts_->DestroyedOnMainThread())
165 return shared_main_thread_contexts_;
167 shared_main_thread_contexts_ = InProcessContextProvider::CreateOffscreen(
168 &gpu_memory_buffer_manager_, &image_factory_);
169 if (shared_main_thread_contexts_.get() &&
170 !shared_main_thread_contexts_->BindToCurrentThread())
171 shared_main_thread_contexts_ = NULL;
173 return shared_main_thread_contexts_;
176 void InProcessContextFactory::RemoveCompositor(Compositor* compositor) {
177 if (!per_compositor_data_.count(compositor))
178 return;
179 delete per_compositor_data_[compositor];
180 per_compositor_data_.erase(compositor);
183 bool InProcessContextFactory::DoesCreateTestContexts() {
184 return context_factory_for_test_;
187 uint32 InProcessContextFactory::GetImageTextureTarget(gfx::BufferFormat format,
188 gfx::BufferUsage usage) {
189 return GL_TEXTURE_2D;
192 cc::SharedBitmapManager* InProcessContextFactory::GetSharedBitmapManager() {
193 return &shared_bitmap_manager_;
196 gpu::GpuMemoryBufferManager*
197 InProcessContextFactory::GetGpuMemoryBufferManager() {
198 return &gpu_memory_buffer_manager_;
201 cc::TaskGraphRunner* InProcessContextFactory::GetTaskGraphRunner() {
202 return &task_graph_runner_;
205 scoped_ptr<cc::SurfaceIdAllocator>
206 InProcessContextFactory::CreateSurfaceIdAllocator() {
207 scoped_ptr<cc::SurfaceIdAllocator> allocator(
208 new cc::SurfaceIdAllocator(next_surface_id_namespace_++));
209 if (surface_manager_)
210 allocator->RegisterSurfaceIdNamespace(surface_manager_);
211 return allocator;
214 void InProcessContextFactory::ResizeDisplay(ui::Compositor* compositor,
215 const gfx::Size& size) {
216 if (!per_compositor_data_.count(compositor))
217 return;
218 per_compositor_data_[compositor]->display()->Resize(size);
221 } // namespace ui