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#ifndef CONTENT_BROWSER_COMPOSITOR_GPU_PROCESS_TRANSPORT_FACTORY_H_
6#define CONTENT_BROWSER_COMPOSITOR_GPU_PROCESS_TRANSPORT_FACTORY_H_
7
8#include <map>
9
10#include "base/id_map.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
15#include "content/browser/compositor/image_transport_factory.h"
16#include "ui/compositor/compositor.h"
17
18namespace base {
19class Thread;
20}
21
22namespace cc {
23class SurfaceManager;
24}
25
26namespace content {
27class BrowserCompositorOutputSurface;
28class BrowserCompositorOutputSurfaceProxy;
29class CompositorSwapClient;
30class ContextProviderCommandBuffer;
31class ReflectorImpl;
32class WebGraphicsContext3DCommandBufferImpl;
33
34class GpuProcessTransportFactory
35    : public ui::ContextFactory,
36      public ImageTransportFactory {
37 public:
38  GpuProcessTransportFactory();
39
40  virtual ~GpuProcessTransportFactory();
41
42  scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
43  CreateOffscreenCommandBufferContext();
44
45  // ui::ContextFactory implementation.
46  virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface(
47      ui::Compositor* compositor, bool software_fallback) OVERRIDE;
48  virtual scoped_refptr<ui::Reflector> CreateReflector(
49      ui::Compositor* source,
50      ui::Layer* target) OVERRIDE;
51  virtual void RemoveReflector(
52      scoped_refptr<ui::Reflector> reflector) OVERRIDE;
53  virtual void RemoveCompositor(ui::Compositor* compositor) OVERRIDE;
54  virtual scoped_refptr<cc::ContextProvider>
55      SharedMainThreadContextProvider() OVERRIDE;
56  virtual bool DoesCreateTestContexts() OVERRIDE;
57  virtual cc::SharedBitmapManager* GetSharedBitmapManager() OVERRIDE;
58  virtual base::MessageLoopProxy* GetCompositorMessageLoop() OVERRIDE;
59
60  // ImageTransportFactory implementation.
61  virtual ui::ContextFactory* GetContextFactory() OVERRIDE;
62  virtual gfx::GLSurfaceHandle GetSharedSurfaceHandle() OVERRIDE;
63  virtual scoped_ptr<cc::SurfaceIdAllocator> CreateSurfaceIdAllocator()
64      OVERRIDE;
65  virtual cc::SurfaceManager* GetSurfaceManager() OVERRIDE;
66  virtual GLHelper* GetGLHelper() OVERRIDE;
67  virtual void AddObserver(ImageTransportFactoryObserver* observer) OVERRIDE;
68  virtual void RemoveObserver(
69      ImageTransportFactoryObserver* observer) OVERRIDE;
70#if defined(OS_MACOSX)
71  virtual void OnSurfaceDisplayed(int surface_id) OVERRIDE;
72#endif
73
74 private:
75  struct PerCompositorData;
76
77  PerCompositorData* CreatePerCompositorData(ui::Compositor* compositor);
78  scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
79      CreateContextCommon(int surface_id);
80
81  void OnLostMainThreadSharedContextInsideCallback();
82  void OnLostMainThreadSharedContext();
83
84  typedef std::map<ui::Compositor*, PerCompositorData*> PerCompositorDataMap;
85  scoped_ptr<base::Thread> compositor_thread_;
86  PerCompositorDataMap per_compositor_data_;
87  scoped_refptr<ContextProviderCommandBuffer> shared_main_thread_contexts_;
88  scoped_ptr<GLHelper> gl_helper_;
89  ObserverList<ImageTransportFactoryObserver> observer_list_;
90  scoped_ptr<cc::SurfaceManager> surface_manager_;
91  uint32_t next_surface_id_namespace_;
92
93  // The contents of this map and its methods may only be used on the compositor
94  // thread.
95  IDMap<BrowserCompositorOutputSurface> output_surface_map_;
96
97  scoped_refptr<BrowserCompositorOutputSurfaceProxy> output_surface_proxy_;
98
99  base::WeakPtrFactory<GpuProcessTransportFactory> callback_factory_;
100
101  DISALLOW_COPY_AND_ASSIGN(GpuProcessTransportFactory);
102};
103
104}  // namespace content
105
106#endif  // CONTENT_BROWSER_COMPOSITOR_GPU_PROCESS_TRANSPORT_FACTORY_H_
107