summaryrefslogtreecommitdiffstats
path: root/content/common/gpu/gpu_memory_manager.h
blob: 4135d2fbd9a498f06ee6088e51395ac28ffa2b04 (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
// 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.

#ifndef CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_
#define CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_

#include <stdint.h>

#include <list>
#include <map>

#include "base/cancelable_callback.h"
#include "base/containers/hash_tables.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "content/common/content_export.h"
#include "gpu/command_buffer/common/gpu_memory_allocation.h"
#include "gpu/command_buffer/service/memory_tracking.h"

namespace gpu {
struct VideoMemoryUsageStats;
}

namespace content {

class GpuChannelManager;
class GpuMemoryTrackingGroup;

class CONTENT_EXPORT GpuMemoryManager :
    public base::SupportsWeakPtr<GpuMemoryManager> {
 public:
  explicit GpuMemoryManager(GpuChannelManager* channel_manager);
  ~GpuMemoryManager();

  // Retrieve GPU Resource consumption statistics for the task manager
  void GetVideoMemoryUsageStats(
      gpu::VideoMemoryUsageStats* video_memory_usage_stats) const;

  GpuMemoryTrackingGroup* CreateTrackingGroup(
      base::ProcessId pid, gpu::gles2::MemoryTracker* memory_tracker);

  uint64_t GetTrackerMemoryUsage(gpu::gles2::MemoryTracker* tracker) const;

 private:
  friend class GpuMemoryManagerTest;
  friend class GpuMemoryTrackingGroup;
  friend class GpuMemoryManagerClientState;

  typedef std::map<gpu::gles2::MemoryTracker*, GpuMemoryTrackingGroup*>
      TrackingGroupMap;

  // Send memory usage stats to the browser process.
  void SendUmaStatsToHost();

  // Get the current number of bytes allocated.
  uint64_t GetCurrentUsage() const { return bytes_allocated_current_; }

  // GpuMemoryTrackingGroup interface
  void TrackMemoryAllocatedChange(GpuMemoryTrackingGroup* tracking_group,
                                  uint64_t old_size,
                                  uint64_t new_size);
  void OnDestroyTrackingGroup(GpuMemoryTrackingGroup* tracking_group);
  bool EnsureGPUMemoryAvailable(uint64_t size_needed);

  GpuChannelManager* channel_manager_;

  // All context groups' tracking structures
  TrackingGroupMap tracking_groups_;

  // The current total memory usage, and historical maximum memory usage
  uint64_t bytes_allocated_current_;
  uint64_t bytes_allocated_historical_max_;

  DISALLOW_COPY_AND_ASSIGN(GpuMemoryManager);
};

}  // namespace content

#endif  // CONTENT_COMMON_GPU_GPU_MEMORY_MANAGER_H_