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 #include "content/browser/gpu/gpu_surface_tracker.h"
7 #if defined(OS_ANDROID)
8 #include <android/native_window_jni.h>
9 #endif // defined(OS_ANDROID)
11 #include "base/logging.h"
15 GpuSurfaceTracker::GpuSurfaceTracker()
16 : next_surface_id_(1) {
17 GpuSurfaceLookup::InitInstance(this);
20 GpuSurfaceTracker::~GpuSurfaceTracker() {
21 GpuSurfaceLookup::InitInstance(NULL
);
24 GpuSurfaceTracker
* GpuSurfaceTracker::GetInstance() {
25 return Singleton
<GpuSurfaceTracker
>::get();
28 int GpuSurfaceTracker::AddSurfaceForRenderer(int renderer_id
,
29 int render_widget_id
) {
30 base::AutoLock
lock(lock_
);
31 int surface_id
= next_surface_id_
++;
32 surface_map_
[surface_id
] =
33 SurfaceInfo(renderer_id
, render_widget_id
, gfx::kNullAcceleratedWidget
,
34 gfx::GLSurfaceHandle(), NULL
);
38 int GpuSurfaceTracker::LookupSurfaceForRenderer(int renderer_id
,
39 int render_widget_id
) {
40 base::AutoLock
lock(lock_
);
41 for (SurfaceMap::iterator it
= surface_map_
.begin(); it
!= surface_map_
.end();
43 const SurfaceInfo
& info
= it
->second
;
44 if (info
.renderer_id
== renderer_id
&&
45 info
.render_widget_id
== render_widget_id
) {
52 int GpuSurfaceTracker::AddSurfaceForNativeWidget(
53 gfx::AcceleratedWidget widget
) {
54 base::AutoLock
lock(lock_
);
55 int surface_id
= next_surface_id_
++;
56 surface_map_
[surface_id
] =
57 SurfaceInfo(0, 0, widget
, gfx::GLSurfaceHandle(), NULL
);
61 void GpuSurfaceTracker::RemoveSurface(int surface_id
) {
62 base::AutoLock
lock(lock_
);
63 DCHECK(surface_map_
.find(surface_id
) != surface_map_
.end());
64 surface_map_
.erase(surface_id
);
67 bool GpuSurfaceTracker::GetRenderWidgetIDForSurface(int surface_id
,
69 int* render_widget_id
) {
70 base::AutoLock
lock(lock_
);
71 SurfaceMap::iterator it
= surface_map_
.find(surface_id
);
72 if (it
== surface_map_
.end())
74 const SurfaceInfo
& info
= it
->second
;
75 if (!info
.handle
.is_transport())
77 *renderer_id
= info
.renderer_id
;
78 *render_widget_id
= info
.render_widget_id
;
82 void GpuSurfaceTracker::SetSurfaceHandle(int surface_id
,
83 const gfx::GLSurfaceHandle
& handle
) {
84 base::AutoLock
lock(lock_
);
85 DCHECK(surface_map_
.find(surface_id
) != surface_map_
.end());
86 SurfaceInfo
& info
= surface_map_
[surface_id
];
90 gfx::GLSurfaceHandle
GpuSurfaceTracker::GetSurfaceHandle(int surface_id
) {
91 base::AutoLock
lock(lock_
);
92 SurfaceMap::iterator it
= surface_map_
.find(surface_id
);
93 if (it
== surface_map_
.end())
94 return gfx::GLSurfaceHandle();
95 return it
->second
.handle
;
98 gfx::AcceleratedWidget
GpuSurfaceTracker::AcquireNativeWidget(int surface_id
) {
99 base::AutoLock
lock(lock_
);
100 SurfaceMap::iterator it
= surface_map_
.find(surface_id
);
101 if (it
== surface_map_
.end())
102 return gfx::kNullAcceleratedWidget
;
104 #if defined(OS_ANDROID)
105 if (it
->second
.native_widget
!= gfx::kNullAcceleratedWidget
)
106 ANativeWindow_acquire(it
->second
.native_widget
);
107 #endif // defined(OS_ANDROID)
109 return it
->second
.native_widget
;
112 void GpuSurfaceTracker::SetNativeWidget(
113 int surface_id
, gfx::AcceleratedWidget widget
,
114 SurfaceRef
* surface_ref
) {
115 base::AutoLock
lock(lock_
);
116 SurfaceMap::iterator it
= surface_map_
.find(surface_id
);
117 DCHECK(it
!= surface_map_
.end());
118 SurfaceInfo
& info
= it
->second
;
119 info
.native_widget
= widget
;
120 info
.surface_ref
= surface_ref
;
123 std::size_t GpuSurfaceTracker::GetSurfaceCount() {
124 base::AutoLock
lock(lock_
);
125 return surface_map_
.size();
128 GpuSurfaceTracker::SurfaceInfo::SurfaceInfo()
131 native_widget(gfx::kNullAcceleratedWidget
) { }
133 GpuSurfaceTracker::SurfaceInfo::SurfaceInfo(
135 int render_widget_id
,
136 const gfx::AcceleratedWidget
& native_widget
,
137 const gfx::GLSurfaceHandle
& handle
,
138 const scoped_refptr
<SurfaceRef
>& surface_ref
)
139 : renderer_id(renderer_id
),
140 render_widget_id(render_widget_id
),
141 native_widget(native_widget
),
143 surface_ref(surface_ref
) { }
145 GpuSurfaceTracker::SurfaceInfo::~SurfaceInfo() { }
148 } // namespace content