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