cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / gpu / command_buffer / common / id_allocator.cc
blobb881e051a5a28c84b8e6567aaa2b9451b0f9fa8a
1 // Copyright (c) 2011 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 // This file contains the implementation of IdAllocator.
7 #include "gpu/command_buffer/common/id_allocator.h"
8 #include "gpu/command_buffer/common/logging.h"
10 namespace gpu {
12 IdAllocatorInterface::~IdAllocatorInterface() {
15 IdAllocator::IdAllocator() {}
17 IdAllocator::~IdAllocator() {}
19 ResourceId IdAllocator::AllocateID() {
20 ResourceId id;
21 ResourceIdSet::iterator iter = free_ids_.begin();
22 if (iter != free_ids_.end()) {
23 id = *iter;
24 } else {
25 id = LastUsedId() + 1;
26 if (!id) {
27 // We wrapped around to 0.
28 id = FindFirstUnusedId();
31 MarkAsUsed(id);
32 return id;
35 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
36 ResourceId id;
37 ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
38 if (iter != free_ids_.end()) {
39 id = *iter;
40 } else if (LastUsedId() < desired_id) {
41 id = desired_id;
42 } else {
43 id = LastUsedId() + 1;
44 if (!id) {
45 // We wrapped around to 0.
46 id = FindFirstUnusedId();
49 MarkAsUsed(id);
50 return id;
53 bool IdAllocator::MarkAsUsed(ResourceId id) {
54 GPU_DCHECK(id);
55 free_ids_.erase(id);
56 std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
57 return result.second;
60 void IdAllocator::FreeID(ResourceId id) {
61 if (id) {
62 used_ids_.erase(id);
63 free_ids_.insert(id);
67 bool IdAllocator::InUse(ResourceId id) const {
68 return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
71 ResourceId IdAllocator::LastUsedId() const {
72 if (used_ids_.empty()) {
73 return 0u;
74 } else {
75 return *used_ids_.rbegin();
79 ResourceId IdAllocator::FindFirstUnusedId() const {
80 ResourceId id = 1;
81 for (ResourceIdSet::const_iterator it = used_ids_.begin();
82 it != used_ids_.end(); ++it) {
83 if ((*it) != id) {
84 return id;
86 ++id;
88 return id;
91 NonReusedIdAllocator::NonReusedIdAllocator() : last_id_(0) {
94 NonReusedIdAllocator::~NonReusedIdAllocator() {
97 ResourceId NonReusedIdAllocator::AllocateID() {
98 return ++last_id_;
101 ResourceId NonReusedIdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
102 if (desired_id > last_id_)
103 last_id_ = desired_id;
105 return ++last_id_;
108 bool NonReusedIdAllocator::MarkAsUsed(ResourceId id) {
109 GPU_NOTREACHED();
110 return false;
113 void NonReusedIdAllocator::FreeID(ResourceId id) {
116 bool NonReusedIdAllocator::InUse(ResourceId id) const {
117 GPU_NOTREACHED();
118 return false;
121 } // namespace gpu