Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / service / memory_tracking.h
blob14bf29cd088d45d950c1223a4c09795fc8bf1003
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 GPU_COMMAND_BUFFER_SERVICE_MEMORY_TRACKING_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_MEMORY_TRACKING_H_
8 #include <string>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/trace_event/trace_event.h"
14 namespace gpu {
15 namespace gles2 {
17 // A MemoryTracker is used to propagate per-ContextGroup memory usage
18 // statistics to the global GpuMemoryManager.
19 class MemoryTracker : public base::RefCounted<MemoryTracker> {
20 public:
21 enum Pool {
22 kUnmanaged,
23 kManaged
26 virtual void TrackMemoryAllocatedChange(size_t old_size,
27 size_t new_size,
28 Pool pool) = 0;
30 // Ensure a certain amount of GPU memory is free. Returns true on success.
31 virtual bool EnsureGPUMemoryAvailable(size_t size_needed) = 0;
33 // Tracing id which identifies the GPU client for whom memory is being
34 // allocated.
35 virtual uint64_t ClientTracingId() const = 0;
37 // Identifies the share group within which memory is being allocated.
38 virtual uint64_t ShareGroupTracingGUID() const = 0;
40 // Raw ID identifying the GPU client for whom memory is being allocated.
41 virtual int ClientId() const = 0;
43 protected:
44 friend class base::RefCounted<MemoryTracker>;
45 MemoryTracker() {}
46 virtual ~MemoryTracker() {};
48 private:
49 DISALLOW_COPY_AND_ASSIGN(MemoryTracker);
52 // A MemoryTypeTracker tracks the use of a particular type of memory (buffer,
53 // texture, or renderbuffer) and forward the result to a specified
54 // MemoryTracker.
55 class MemoryTypeTracker {
56 public:
57 MemoryTypeTracker(MemoryTracker* memory_tracker, MemoryTracker::Pool pool)
58 : memory_tracker_(memory_tracker),
59 pool_(pool),
60 has_done_update_(false),
61 mem_represented_(0),
62 mem_represented_at_last_update_(0) {
63 UpdateMemRepresented();
66 ~MemoryTypeTracker() {
67 UpdateMemRepresented();
70 void TrackMemAlloc(size_t bytes) {
71 mem_represented_ += bytes;
72 UpdateMemRepresented();
75 void TrackMemFree(size_t bytes) {
76 DCHECK(bytes <= mem_represented_);
77 mem_represented_ -= bytes;
78 UpdateMemRepresented();
81 size_t GetMemRepresented() const {
82 return mem_represented_at_last_update_;
85 // Ensure a certain amount of GPU memory is free. Returns true on success.
86 bool EnsureGPUMemoryAvailable(size_t size_needed) {
87 if (memory_tracker_) {
88 return memory_tracker_->EnsureGPUMemoryAvailable(size_needed);
90 return true;
93 private:
94 void UpdateMemRepresented() {
95 // Skip redundant updates only if we have already done an update.
96 if (!has_done_update_ &&
97 mem_represented_ == mem_represented_at_last_update_) {
98 return;
100 if (memory_tracker_) {
101 memory_tracker_->TrackMemoryAllocatedChange(
102 mem_represented_at_last_update_,
103 mem_represented_,
104 pool_);
106 has_done_update_ = true;
107 mem_represented_at_last_update_ = mem_represented_;
110 MemoryTracker* memory_tracker_;
111 MemoryTracker::Pool pool_;
112 bool has_done_update_;
113 size_t mem_represented_;
114 size_t mem_represented_at_last_update_;
116 DISALLOW_COPY_AND_ASSIGN(MemoryTypeTracker);
119 } // namespace gles2
120 } // namespace gpu
122 #endif // GPU_COMMAND_BUFFER_SERVICE_MEMORY_TRACKING_H_