| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains the definition of the IdAllocator class. | 5 // This file contains the definition of the IdAllocator class. |
| 6 | 6 |
| 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ | 7 #ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ |
| 8 #define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ | 8 #define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ |
| 9 | 9 |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 #include <set> | 12 #include <set> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "gpu/gpu_export.h" | 17 #include "gpu/gpu_export.h" |
| 18 | 18 |
| 19 namespace gpu { | 19 namespace gpu { |
| 20 | 20 |
| 21 // A resource ID, key to the resource maps. | 21 // A resource ID, key to the resource maps. |
| 22 typedef uint32_t ResourceId; | 22 typedef uint32_t ResourceId; |
| 23 // Invalid resource ID. | 23 // Invalid resource ID. |
| 24 static const ResourceId kInvalidResource = 0u; | 24 static const ResourceId kInvalidResource = 0u; |
| 25 | 25 |
| 26 class GPU_EXPORT IdAllocatorInterface { | 26 // A class to manage the allocation of resource IDs. |
| 27 class GPU_EXPORT IdAllocator { |
| 27 public: | 28 public: |
| 28 virtual ~IdAllocatorInterface(); | 29 IdAllocator(); |
| 30 ~IdAllocator(); |
| 29 | 31 |
| 30 // Allocates a new resource ID. | 32 // Allocates a new resource ID. |
| 31 virtual ResourceId AllocateID() = 0; | 33 ResourceId AllocateID(); |
| 32 | 34 |
| 33 // Allocates an Id starting at or above desired_id. | 35 // Allocates an Id starting at or above desired_id. |
| 34 // Note: may wrap if it starts near limit. | 36 // Note: may wrap if it starts near limit. |
| 35 virtual ResourceId AllocateIDAtOrAbove(ResourceId desired_id) = 0; | 37 ResourceId AllocateIDAtOrAbove(ResourceId desired_id); |
| 36 | 38 |
| 37 // Marks an id as used. Returns false if id was already used. | 39 // Marks an id as used. Returns false if id was already used. |
| 38 virtual bool MarkAsUsed(ResourceId id) = 0; | 40 bool MarkAsUsed(ResourceId id); |
| 39 | 41 |
| 40 // Frees a resource ID. | 42 // Frees a resource ID. |
| 41 virtual void FreeID(ResourceId id) = 0; | 43 void FreeID(ResourceId id); |
| 42 | 44 |
| 43 // Checks whether or not a resource ID is in use. | 45 // Checks whether or not a resource ID is in use. |
| 44 virtual bool InUse(ResourceId id) const = 0; | 46 bool InUse(ResourceId id) const; |
| 45 }; | |
| 46 | |
| 47 // A class to manage the allocation of resource IDs. | |
| 48 class GPU_EXPORT IdAllocator : public IdAllocatorInterface { | |
| 49 public: | |
| 50 IdAllocator(); | |
| 51 virtual ~IdAllocator(); | |
| 52 | |
| 53 // Implement IdAllocatorInterface. | |
| 54 virtual ResourceId AllocateID() OVERRIDE; | |
| 55 virtual ResourceId AllocateIDAtOrAbove(ResourceId desired_id) OVERRIDE; | |
| 56 virtual bool MarkAsUsed(ResourceId id) OVERRIDE; | |
| 57 virtual void FreeID(ResourceId id) OVERRIDE; | |
| 58 virtual bool InUse(ResourceId id) const OVERRIDE; | |
| 59 | 47 |
| 60 private: | 48 private: |
| 61 // TODO(gman): This would work much better with ranges or a hash table. | 49 // TODO(gman): This would work much better with ranges or a hash table. |
| 62 typedef std::set<ResourceId> ResourceIdSet; | 50 typedef std::set<ResourceId> ResourceIdSet; |
| 63 | 51 |
| 64 // The highest ID on the used list. | 52 // The highest ID on the used list. |
| 65 ResourceId LastUsedId() const; | 53 ResourceId LastUsedId() const; |
| 66 | 54 |
| 67 // Lowest ID that isn't on the used list. This is slow, use as a last resort. | 55 // Lowest ID that isn't on the used list. This is slow, use as a last resort. |
| 68 ResourceId FindFirstUnusedId() const; | 56 ResourceId FindFirstUnusedId() const; |
| 69 | 57 |
| 70 ResourceIdSet used_ids_; | 58 ResourceIdSet used_ids_; |
| 71 ResourceIdSet free_ids_; | 59 ResourceIdSet free_ids_; |
| 72 | 60 |
| 73 DISALLOW_COPY_AND_ASSIGN(IdAllocator); | 61 DISALLOW_COPY_AND_ASSIGN(IdAllocator); |
| 74 }; | 62 }; |
| 75 | 63 |
| 76 // A class to manage the allocation of resource IDs that are never reused. This | |
| 77 // implementation does not track which IDs are currently used. It is useful for | |
| 78 // shared and programs which cannot be implicitly created by binding a | |
| 79 // previously unused ID. | |
| 80 class NonReusedIdAllocator : public IdAllocatorInterface { | |
| 81 public: | |
| 82 NonReusedIdAllocator(); | |
| 83 virtual ~NonReusedIdAllocator(); | |
| 84 | |
| 85 // Implement IdAllocatorInterface. | |
| 86 virtual ResourceId AllocateID() OVERRIDE; | |
| 87 virtual ResourceId AllocateIDAtOrAbove(ResourceId desired_id) OVERRIDE; | |
| 88 virtual bool MarkAsUsed(ResourceId id) OVERRIDE; | |
| 89 virtual void FreeID(ResourceId id) OVERRIDE; | |
| 90 virtual bool InUse(ResourceId id) const OVERRIDE; | |
| 91 | |
| 92 private: | |
| 93 ResourceId last_id_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(NonReusedIdAllocator); | |
| 96 }; | |
| 97 | |
| 98 } // namespace gpu | 64 } // namespace gpu |
| 99 | 65 |
| 100 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ | 66 #endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ |
| OLD | NEW |