1// Copyright (c) 2012 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_GPU_GPU_SURFACE_TRACKER_H_
6#define CONTENT_BROWSER_GPU_GPU_SURFACE_TRACKER_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/singleton.h"
13#include "base/synchronization/lock.h"
14#include "content/common/gpu/gpu_surface_lookup.h"
15#include "ui/gfx/native_widget_types.h"
16#include "ui/gfx/size.h"
17
18namespace content {
19
20// This class is responsible for managing rendering surfaces exposed to the
21// GPU process. Every surface gets registered to this class, and gets an ID.
22// All calls to and from the GPU process, with the exception of
23// CreateViewCommandBuffer, refer to the rendering surface by its ID.
24// This class is thread safe.
25//
26// Note: The ID can exist before the actual native handle for the surface is
27// created, for example to allow giving a reference to it to a renderer, so that
28// it is unamibiguously identified.
29class GpuSurfaceTracker : public GpuSurfaceLookup {
30 public:
31  // Base class for reference counting surfaces. We store a
32  // reference to an instance of this class in the surface_map_
33  // and GpuProcessHost (if the GPU process is drawing to
34  // the surface with a Command Buffer). The reference count ensures that
35  // we don't destroy the object until it's released from both places.
36  //
37  // This is especially important on Android and GTK where the surface must
38  // not be destroyed when the WebContents is closed if the GPU is still
39  // drawing to it. Those platforms extend this class with the functionality
40  // they need to implement on tear down (see SurfaceRefPluginWindow for GTK and
41  // SurfaceRefAndroid for Android).
42  class SurfaceRef : public base::RefCountedThreadSafe<SurfaceRef> {
43   protected:
44    SurfaceRef() { }
45    virtual ~SurfaceRef() { }
46
47   private:
48    friend class base::RefCountedThreadSafe<SurfaceRef>;
49    DISALLOW_COPY_AND_ASSIGN(SurfaceRef);
50  };
51
52  // GpuSurfaceLookup implementation:
53  // Returns the native widget associated with a given surface_id.
54  virtual gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) OVERRIDE;
55
56  // Gets the global instance of the surface tracker.
57  static GpuSurfaceTracker* Get() { return GetInstance(); }
58
59  // Adds a surface for a given RenderWidgetHost. |renderer_id| is the renderer
60  // process ID, |render_widget_id| is the RenderWidgetHost route id within that
61  // renderer. Returns the surface ID.
62  int AddSurfaceForRenderer(int renderer_id, int render_widget_id);
63
64  // Looks up a surface for a given RenderWidgetHost. Returns the surface
65  // ID, or 0 if not found.
66  // Note: This is an O(N) lookup.
67  int LookupSurfaceForRenderer(int renderer_id, int render_widget_id);
68
69  // Adds a surface for a native widget. Returns the surface ID.
70  int AddSurfaceForNativeWidget(gfx::AcceleratedWidget widget);
71
72  // Removes a given existing surface.
73  void RemoveSurface(int surface_id);
74
75  // Gets the renderer process ID and RenderWidgetHost route id for a given
76  // surface, returning true if the surface is found (and corresponds to a
77  // RenderWidgetHost), or false if not.
78  bool GetRenderWidgetIDForSurface(int surface_id,
79                                   int* renderer_id,
80                                   int* render_widget_id);
81
82  // Sets the native handle for the given surface.
83  // Note: This is an O(log N) lookup.
84  void SetSurfaceHandle(int surface_id, const gfx::GLSurfaceHandle& handle);
85
86  // Sets the native widget associated with the surface_id.
87  void SetNativeWidget(
88      int surface_id,
89      gfx::AcceleratedWidget widget,
90      SurfaceRef* surface_ref);
91
92  // Gets the native handle for the given surface.
93  // Note: This is an O(log N) lookup.
94  gfx::GLSurfaceHandle GetSurfaceHandle(int surface_id);
95
96  // Returns the number of surfaces currently registered with the tracker.
97  std::size_t GetSurfaceCount();
98
99  // Gets the global instance of the surface tracker. Identical to Get(), but
100  // named that way for the implementation of Singleton.
101  static GpuSurfaceTracker* GetInstance();
102
103  scoped_refptr<SurfaceRef> GetSurfaceRefForSurface(int surface_id) {
104    return surface_map_[surface_id].surface_ref;
105  }
106
107 private:
108  struct SurfaceInfo {
109    SurfaceInfo();
110    SurfaceInfo(int renderer_id,
111                int render_widget_id,
112                const gfx::AcceleratedWidget& native_widget,
113                const gfx::GLSurfaceHandle& handle,
114                const scoped_refptr<SurfaceRef>& surface_ref);
115    ~SurfaceInfo();
116    int renderer_id;
117    int render_widget_id;
118    gfx::AcceleratedWidget native_widget;
119    gfx::GLSurfaceHandle handle;
120    scoped_refptr<SurfaceRef> surface_ref;
121  };
122  typedef std::map<int, SurfaceInfo> SurfaceMap;
123
124  friend struct DefaultSingletonTraits<GpuSurfaceTracker>;
125
126  GpuSurfaceTracker();
127  virtual ~GpuSurfaceTracker();
128
129  base::Lock lock_;
130  SurfaceMap surface_map_;
131  int next_surface_id_;
132
133  DISALLOW_COPY_AND_ASSIGN(GpuSurfaceTracker);
134};
135
136}  // namespace content
137
138#endif  // CONTENT_BROWSER_GPU_GPU_SURFACE_TRACKER_H_
139