Roll src/third_party/skia 591a2ca:b3fe1b8
[chromium-blink-merge.git] / ui / compositor / test / in_process_context_factory.cc
blob4597a868554e288afbed3b3e24fa15d150588dd5
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/gl_surface.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 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();
57 } else {
58 context_provider_->ContextSupport()->PartialSwapBuffers(
59 frame->gl_frame_data->sub_buffer_rect);
61 uint32_t sync_point =
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();
69 private:
70 base::WeakPtrFactory<DirectOutputSurface> weak_ptr_factory_;
72 DISALLOW_COPY_AND_ASSIGN(DirectOutputSurface);
75 } // namespace
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::GLSurface::InitializeOneOffForTests()";
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;
104 attribs.samples = 0;
105 attribs.sample_buffers = 0;
106 attribs.fail_if_major_perf_caveat = false;
107 attribs.bind_generates_resource = false;
108 bool lose_context_when_out_of_memory = true;
110 scoped_refptr<InProcessContextProvider> context_provider =
111 InProcessContextProvider::Create(attribs, &gpu_memory_buffer_manager_,
112 &image_factory_,
113 lose_context_when_out_of_memory,
114 compositor->widget(), "UICompositor");
116 scoped_ptr<cc::OutputSurface> real_output_surface;
118 if (use_test_surface_) {
119 bool flipped_output_surface = false;
120 real_output_surface = make_scoped_ptr(new cc::PixelTestOutputSurface(
121 context_provider, flipped_output_surface));
122 } else {
123 real_output_surface =
124 make_scoped_ptr(new DirectOutputSurface(context_provider));
127 if (surface_manager_) {
128 scoped_ptr<cc::OnscreenDisplayClient> display_client(
129 new cc::OnscreenDisplayClient(
130 real_output_surface.Pass(), surface_manager_,
131 GetSharedBitmapManager(), GetGpuMemoryBufferManager(),
132 compositor->GetRendererSettings(), compositor->task_runner()));
133 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
134 new cc::SurfaceDisplayOutputSurface(surface_manager_,
135 compositor->surface_id_allocator(),
136 context_provider));
137 display_client->set_surface_output_surface(surface_output_surface.get());
138 surface_output_surface->set_display_client(display_client.get());
140 compositor->SetOutputSurface(surface_output_surface.Pass());
142 delete per_compositor_data_[compositor.get()];
143 per_compositor_data_[compositor.get()] = display_client.release();
144 } else {
145 compositor->SetOutputSurface(real_output_surface.Pass());
149 scoped_ptr<Reflector> InProcessContextFactory::CreateReflector(
150 Compositor* mirrored_compositor,
151 Layer* mirroring_layer) {
152 return make_scoped_ptr(new FakeReflector);
155 void InProcessContextFactory::RemoveReflector(Reflector* reflector) {
158 scoped_refptr<cc::ContextProvider>
159 InProcessContextFactory::SharedMainThreadContextProvider() {
160 if (shared_main_thread_contexts_.get() &&
161 !shared_main_thread_contexts_->DestroyedOnMainThread())
162 return shared_main_thread_contexts_;
164 bool lose_context_when_out_of_memory = false;
165 shared_main_thread_contexts_ = InProcessContextProvider::CreateOffscreen(
166 &gpu_memory_buffer_manager_, &image_factory_,
167 lose_context_when_out_of_memory);
168 if (shared_main_thread_contexts_.get() &&
169 !shared_main_thread_contexts_->BindToCurrentThread())
170 shared_main_thread_contexts_ = NULL;
172 return shared_main_thread_contexts_;
175 void InProcessContextFactory::RemoveCompositor(Compositor* compositor) {
176 if (!per_compositor_data_.count(compositor))
177 return;
178 delete per_compositor_data_[compositor];
179 per_compositor_data_.erase(compositor);
182 bool InProcessContextFactory::DoesCreateTestContexts() {
183 return context_factory_for_test_;
186 uint32 InProcessContextFactory::GetImageTextureTarget(
187 gfx::GpuMemoryBuffer::Format format,
188 gfx::GpuMemoryBuffer::Usage 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 return make_scoped_ptr(
208 new cc::SurfaceIdAllocator(next_surface_id_namespace_++));
211 void InProcessContextFactory::ResizeDisplay(ui::Compositor* compositor,
212 const gfx::Size& size) {
213 if (!per_compositor_data_.count(compositor))
214 return;
215 per_compositor_data_[compositor]->display()->Resize(size);
218 } // namespace ui