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_
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"
17 // A MemoryTracker is used to propagate per-ContextGroup memory usage
18 // statistics to the global GpuMemoryManager.
19 class MemoryTracker
: public base::RefCounted
<MemoryTracker
> {
26 virtual void TrackMemoryAllocatedChange(size_t old_size
,
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
35 virtual uint64_t ClientTracingId() const = 0;
37 // Raw ID identifying the GPU client for whom memory is being allocated.
38 virtual int ClientId() const = 0;
41 friend class base::RefCounted
<MemoryTracker
>;
43 virtual ~MemoryTracker() {};
46 DISALLOW_COPY_AND_ASSIGN(MemoryTracker
);
49 // A MemoryTypeTracker tracks the use of a particular type of memory (buffer,
50 // texture, or renderbuffer) and forward the result to a specified
52 class MemoryTypeTracker
{
54 MemoryTypeTracker(MemoryTracker
* memory_tracker
, MemoryTracker::Pool pool
)
55 : memory_tracker_(memory_tracker
),
57 has_done_update_(false),
59 mem_represented_at_last_update_(0) {
60 UpdateMemRepresented();
63 ~MemoryTypeTracker() {
64 UpdateMemRepresented();
67 void TrackMemAlloc(size_t bytes
) {
68 mem_represented_
+= bytes
;
69 UpdateMemRepresented();
72 void TrackMemFree(size_t bytes
) {
73 DCHECK(bytes
<= mem_represented_
);
74 mem_represented_
-= bytes
;
75 UpdateMemRepresented();
78 size_t GetMemRepresented() const {
79 return mem_represented_at_last_update_
;
82 // Ensure a certain amount of GPU memory is free. Returns true on success.
83 bool EnsureGPUMemoryAvailable(size_t size_needed
) {
84 if (memory_tracker_
) {
85 return memory_tracker_
->EnsureGPUMemoryAvailable(size_needed
);
91 void UpdateMemRepresented() {
92 // Skip redundant updates only if we have already done an update.
93 if (!has_done_update_
&&
94 mem_represented_
== mem_represented_at_last_update_
) {
97 if (memory_tracker_
) {
98 memory_tracker_
->TrackMemoryAllocatedChange(
99 mem_represented_at_last_update_
,
103 has_done_update_
= true;
104 mem_represented_at_last_update_
= mem_represented_
;
107 MemoryTracker
* memory_tracker_
;
108 MemoryTracker::Pool pool_
;
109 bool has_done_update_
;
110 size_t mem_represented_
;
111 size_t mem_represented_at_last_update_
;
113 DISALLOW_COPY_AND_ASSIGN(MemoryTypeTracker
);
119 #endif // GPU_COMMAND_BUFFER_SERVICE_MEMORY_TRACKING_H_