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 // This file contains the definition of the IdAllocator class.
7 #ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_
8 #define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_
13 #include "base/compiler_specific.h"
14 #include "gpu/command_buffer/common/types.h"
15 #include "gpu/gpu_export.h"
19 // A resource ID, key to the resource maps.
20 typedef uint32 ResourceId
;
21 // Invalid resource ID.
22 static const ResourceId kInvalidResource
= 0u;
24 class GPU_EXPORT IdAllocatorInterface
{
26 virtual ~IdAllocatorInterface();
28 // Allocates a new resource ID.
29 virtual ResourceId
AllocateID() = 0;
31 // Allocates an Id starting at or above desired_id.
32 // Note: may wrap if it starts near limit.
33 virtual ResourceId
AllocateIDAtOrAbove(ResourceId desired_id
) = 0;
35 // Marks an id as used. Returns false if id was already used.
36 virtual bool MarkAsUsed(ResourceId id
) = 0;
38 // Frees a resource ID.
39 virtual void FreeID(ResourceId id
) = 0;
41 // Checks whether or not a resource ID is in use.
42 virtual bool InUse(ResourceId id
) const = 0;
45 // A class to manage the allocation of resource IDs.
46 class GPU_EXPORT IdAllocator
: public IdAllocatorInterface
{
49 virtual ~IdAllocator();
51 // Implement IdAllocatorInterface.
52 virtual ResourceId
AllocateID() OVERRIDE
;
53 virtual ResourceId
AllocateIDAtOrAbove(ResourceId desired_id
) OVERRIDE
;
54 virtual bool MarkAsUsed(ResourceId id
) OVERRIDE
;
55 virtual void FreeID(ResourceId id
) OVERRIDE
;
56 virtual bool InUse(ResourceId id
) const OVERRIDE
;
59 // TODO(gman): This would work much better with ranges or a hash table.
60 typedef std::set
<ResourceId
> ResourceIdSet
;
62 // The highest ID on the used list.
63 ResourceId
LastUsedId() const;
65 // Lowest ID that isn't on the used list. This is slow, use as a last resort.
66 ResourceId
FindFirstUnusedId() const;
68 ResourceIdSet used_ids_
;
69 ResourceIdSet free_ids_
;
71 DISALLOW_COPY_AND_ASSIGN(IdAllocator
);
74 // A class to manage the allocation of resource IDs that are never reused. This
75 // implementation does not track which IDs are currently used. It is useful for
76 // shared and programs which cannot be implicitly created by binding a
77 // previously unused ID.
78 class NonReusedIdAllocator
: public IdAllocatorInterface
{
80 NonReusedIdAllocator();
81 virtual ~NonReusedIdAllocator();
83 // Implement IdAllocatorInterface.
84 virtual ResourceId
AllocateID() OVERRIDE
;
85 virtual ResourceId
AllocateIDAtOrAbove(ResourceId desired_id
) OVERRIDE
;
86 virtual bool MarkAsUsed(ResourceId id
) OVERRIDE
;
87 virtual void FreeID(ResourceId id
) OVERRIDE
;
88 virtual bool InUse(ResourceId id
) const OVERRIDE
;
93 DISALLOW_COPY_AND_ASSIGN(NonReusedIdAllocator
);
98 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_