summaryrefslogtreecommitdiffstats
path: root/content/browser/gpu/gpu_surface_tracker.cc
blob: 909c39d99b5d2a09673340df7beefabdb4c25b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/gpu/gpu_surface_tracker.h"

#include "base/logging.h"

#if defined(OS_WIN)
#include "ui/gfx/surface/accelerated_surface_win.h"
#endif

GpuSurfaceTracker::GpuSurfaceTracker()
    : next_surface_id_(1) {
}

GpuSurfaceTracker::~GpuSurfaceTracker() {
}

GpuSurfaceTracker* GpuSurfaceTracker::GetInstance() {
  return Singleton<GpuSurfaceTracker>::get();
}

int GpuSurfaceTracker::AddSurfaceForRenderer(int renderer_id,
                                             int render_widget_id) {
  base::AutoLock lock(lock_);
  SurfaceInfo info = {
    renderer_id,
    render_widget_id,
    gfx::kNullAcceleratedWidget
  };
  int surface_id = next_surface_id_++;
  surface_map_[surface_id] = info;
  return surface_id;
}

int GpuSurfaceTracker::LookupSurfaceForRenderer(int renderer_id,
                                                int render_widget_id) {
  base::AutoLock lock(lock_);
  for (SurfaceMap::iterator it = surface_map_.begin(); it != surface_map_.end();
       ++it) {
    const SurfaceInfo& info = it->second;
    if (info.renderer_id == renderer_id &&
        info.render_widget_id == render_widget_id) {
      return it->first;
    }
  }
  return 0;
}

int GpuSurfaceTracker::AddSurfaceForNativeWidget(
    gfx::AcceleratedWidget widget) {
  base::AutoLock lock(lock_);
  SurfaceInfo info = { 0, 0, widget };
  int surface_id = next_surface_id_++;
  surface_map_[surface_id] = info;
  return surface_id;
}

void GpuSurfaceTracker::RemoveSurface(int surface_id) {
  base::AutoLock lock(lock_);
  DCHECK(surface_map_.find(surface_id) != surface_map_.end());
  surface_map_.erase(surface_id);
}

bool GpuSurfaceTracker::GetRenderWidgetIDForSurface(int surface_id,
                                                    int* renderer_id,
                                                    int* render_widget_id) {
  base::AutoLock lock(lock_);
  SurfaceMap::iterator it = surface_map_.find(surface_id);
  if (it == surface_map_.end())
    return false;
  const SurfaceInfo& info = it->second;
  *renderer_id = info.renderer_id;
  *render_widget_id = info.render_widget_id;
  return true;
}

void GpuSurfaceTracker::SetSurfaceHandle(int surface_id,
                                         gfx::GLSurfaceHandle handle) {
  base::AutoLock lock(lock_);
  DCHECK(surface_map_.find(surface_id) != surface_map_.end());
  SurfaceInfo& info = surface_map_[surface_id];
  info.handle = handle;
}

gfx::GLSurfaceHandle GpuSurfaceTracker::GetSurfaceHandle(int surface_id) {
  base::AutoLock lock(lock_);
  DCHECK(surface_map_.find(surface_id) != surface_map_.end());
  return surface_map_[surface_id].handle;
}

#if defined(OS_WIN) && !defined(USE_AURA)

void GpuSurfaceTracker::AsyncPresentAndAcknowledge(
    int surface_id,
    const gfx::Size& size,
    int64 surface_handle,
    const base::Closure& completion_task) {
  base::AutoLock lock(lock_);

  SurfaceMap::iterator it = surface_map_.find(surface_id);
  if (it == surface_map_.end() || !it->second.handle.accelerated_surface) {
    completion_task.Run();
    return;
  }

  it->second.handle.accelerated_surface->AsyncPresentAndAcknowledge(
      it->second.handle.handle,
      size,
      surface_handle,
      completion_task);
}

#endif  // OS_WIN