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.
4
5#include "ui/compositor/test/in_process_context_factory.h"
6
7#include "base/command_line.h"
8#include "base/threading/thread.h"
9#include "cc/output/output_surface.h"
10#include "cc/test/test_shared_bitmap_manager.h"
11#include "ui/compositor/compositor_switches.h"
12#include "ui/compositor/reflector.h"
13#include "ui/gl/gl_implementation.h"
14#include "ui/gl/gl_surface.h"
15#include "webkit/common/gpu/context_provider_in_process.h"
16#include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h"
17#include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
18
19namespace ui {
20
21InProcessContextFactory::InProcessContextFactory()
22    : shared_bitmap_manager_(new cc::TestSharedBitmapManager()) {
23  DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone)
24      << "If running tests, ensure that main() is calling "
25      << "gfx::GLSurface::InitializeOneOffForTests()";
26
27#if defined(OS_CHROMEOS)
28  bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch(
29      switches::kUIDisableThreadedCompositing);
30#else
31  bool use_thread = false;
32#endif
33  if (use_thread) {
34    compositor_thread_.reset(new base::Thread("Browser Compositor"));
35    compositor_thread_->Start();
36  }
37}
38
39InProcessContextFactory::~InProcessContextFactory() {}
40
41scoped_ptr<cc::OutputSurface> InProcessContextFactory::CreateOutputSurface(
42    Compositor* compositor,
43    bool software_fallback) {
44  DCHECK(!software_fallback);
45  blink::WebGraphicsContext3D::Attributes attrs;
46  attrs.depth = false;
47  attrs.stencil = false;
48  attrs.antialias = false;
49  attrs.shareResources = true;
50  bool lose_context_when_out_of_memory = true;
51
52  using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
53  scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d(
54      WebGraphicsContext3DInProcessCommandBufferImpl::CreateViewContext(
55          attrs, lose_context_when_out_of_memory, compositor->widget()));
56  CHECK(context3d);
57
58  using webkit::gpu::ContextProviderInProcess;
59  scoped_refptr<ContextProviderInProcess> context_provider =
60      ContextProviderInProcess::Create(context3d.Pass(), "UICompositor");
61
62  return make_scoped_ptr(new cc::OutputSurface(context_provider));
63}
64
65scoped_refptr<Reflector> InProcessContextFactory::CreateReflector(
66    Compositor* mirroed_compositor,
67    Layer* mirroring_layer) {
68  return new Reflector();
69}
70
71void InProcessContextFactory::RemoveReflector(
72    scoped_refptr<Reflector> reflector) {}
73
74scoped_refptr<cc::ContextProvider>
75InProcessContextFactory::SharedMainThreadContextProvider() {
76  if (shared_main_thread_contexts_.get() &&
77      !shared_main_thread_contexts_->DestroyedOnMainThread())
78    return shared_main_thread_contexts_;
79
80  bool lose_context_when_out_of_memory = false;
81  shared_main_thread_contexts_ =
82      webkit::gpu::ContextProviderInProcess::CreateOffscreen(
83          lose_context_when_out_of_memory);
84  if (shared_main_thread_contexts_.get() &&
85      !shared_main_thread_contexts_->BindToCurrentThread())
86    shared_main_thread_contexts_ = NULL;
87
88  return shared_main_thread_contexts_;
89}
90
91void InProcessContextFactory::RemoveCompositor(Compositor* compositor) {}
92
93bool InProcessContextFactory::DoesCreateTestContexts() { return false; }
94
95cc::SharedBitmapManager* InProcessContextFactory::GetSharedBitmapManager() {
96  return shared_bitmap_manager_.get();
97}
98
99base::MessageLoopProxy* InProcessContextFactory::GetCompositorMessageLoop() {
100  if (!compositor_thread_)
101    return NULL;
102  return compositor_thread_->message_loop_proxy().get();
103}
104
105}  // namespace ui
106