Handle account removal correctly on all platforms.
[chromium-blink-merge.git] / gpu / command_buffer / client / gpu_memory_buffer_tracker.cc
blob9ffe0e33e2d4410fadfc9e4f0fe499ca2149ad1a
1 // Copyright (c) 2013 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 "gpu/command_buffer/client/gpu_memory_buffer_tracker.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/command_buffer/client/gles2_implementation.h"
9 #include "gpu/command_buffer/client/gpu_control.h"
11 namespace gpu {
12 namespace gles2 {
14 GpuMemoryBufferTracker::GpuMemoryBufferTracker(GpuControl* gpu_control)
15 : gpu_control_(gpu_control) {
18 GpuMemoryBufferTracker::~GpuMemoryBufferTracker() {
19 while (!buffers_.empty()) {
20 RemoveBuffer(buffers_.begin()->first);
24 int32 GpuMemoryBufferTracker::CreateBuffer(size_t width,
25 size_t height,
26 int32 internalformat,
27 int32 usage) {
28 int32 image_id = 0;
29 DCHECK(gpu_control_);
30 gfx::GpuMemoryBuffer* buffer = gpu_control_->CreateGpuMemoryBuffer(
31 width, height, internalformat, usage, &image_id);
32 if (!buffer)
33 return 0;
35 std::pair<BufferMap::iterator, bool> result =
36 buffers_.insert(std::make_pair(image_id, buffer));
37 DCHECK(result.second);
39 return image_id;
42 gfx::GpuMemoryBuffer* GpuMemoryBufferTracker::GetBuffer(int32 image_id) {
43 BufferMap::iterator it = buffers_.find(image_id);
44 return (it != buffers_.end()) ? it->second : NULL;
47 void GpuMemoryBufferTracker::RemoveBuffer(int32 image_id) {
48 BufferMap::iterator buffer_it = buffers_.find(image_id);
49 if (buffer_it != buffers_.end())
50 buffers_.erase(buffer_it);
51 DCHECK(gpu_control_);
52 gpu_control_->DestroyGpuMemoryBuffer(image_id);
55 } // namespace gles2
56 } // namespace gpu