diff options
author | gman@chromium.org <gman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-03 19:14:10 +0000 |
---|---|---|
committer | gman@chromium.org <gman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-03 19:14:10 +0000 |
commit | 066849e369dae48bf61ae0cf70c9e9acaf9f1045 (patch) | |
tree | 479aebcba9d2f1d3b054dc3ea64baa9a7c753f15 /gpu/command_buffer/client | |
parent | 0411509f65aae2b1ba684bf87343a14253246de0 (diff) | |
download | chromium_src-066849e369dae48bf61ae0cf70c9e9acaf9f1045.zip chromium_src-066849e369dae48bf61ae0cf70c9e9acaf9f1045.tar.gz chromium_src-066849e369dae48bf61ae0cf70c9e9acaf9f1045.tar.bz2 |
Adds support for shared resources.
It's not clear how to test this easily
it seems like we an integration test
is needed at some point. I did run the
conformance tests with share_resources
set to true and it rand without crashing.
TEST=unit tests
BUG=none
Review URL: http://codereview.chromium.org/1817002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46264 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gpu/command_buffer/client')
-rw-r--r-- | gpu/command_buffer/client/gles2_c_lib_autogen.h | 11 | ||||
-rw-r--r-- | gpu/command_buffer/client/gles2_cmd_helper_autogen.h | 21 | ||||
-rw-r--r-- | gpu/command_buffer/client/gles2_demo.cc | 3 | ||||
-rw-r--r-- | gpu/command_buffer/client/gles2_implementation.cc | 158 | ||||
-rw-r--r-- | gpu/command_buffer/client/gles2_implementation.h | 52 | ||||
-rw-r--r-- | gpu/command_buffer/client/gles2_implementation_autogen.h | 39 | ||||
-rw-r--r-- | gpu/command_buffer/client/gles2_implementation_unittest.cc | 4 | ||||
-rw-r--r-- | gpu/command_buffer/client/id_allocator.cc | 27 | ||||
-rw-r--r-- | gpu/command_buffer/client/id_allocator.h | 62 | ||||
-rw-r--r-- | gpu/command_buffer/client/id_allocator_test.cc | 86 | ||||
-rw-r--r-- | gpu/command_buffer/client/ring_buffer.h | 14 |
11 files changed, 229 insertions, 248 deletions
diff --git a/gpu/command_buffer/client/gles2_c_lib_autogen.h b/gpu/command_buffer/client/gles2_c_lib_autogen.h index c3e445a..73cd6ce 100644 --- a/gpu/command_buffer/client/gles2_c_lib_autogen.h +++ b/gpu/command_buffer/client/gles2_c_lib_autogen.h @@ -508,6 +508,17 @@ GLuint GLES2GetMaxValueInBuffer( return gles2::GetGLContext()->GetMaxValueInBuffer( buffer_id, count, type, offset); } +void GLES2GenSharedIds( + GLuint namespace_id, GLuint id_offset, GLsizei n, GLuint* ids) { + gles2::GetGLContext()->GenSharedIds(namespace_id, id_offset, n, ids); +} +void GLES2DeleteSharedIds(GLuint namespace_id, GLsizei n, const GLuint* ids) { + gles2::GetGLContext()->DeleteSharedIds(namespace_id, n, ids); +} +void GLES2RegisterSharedIds( + GLuint namespace_id, GLsizei n, const GLuint* ids) { + gles2::GetGLContext()->RegisterSharedIds(namespace_id, n, ids); +} #endif // GPU_COMMAND_BUFFER_CLIENT_GLES2_C_LIB_AUTOGEN_H_ diff --git a/gpu/command_buffer/client/gles2_cmd_helper_autogen.h b/gpu/command_buffer/client/gles2_cmd_helper_autogen.h index bb37150..652579d 100644 --- a/gpu/command_buffer/client/gles2_cmd_helper_autogen.h +++ b/gpu/command_buffer/client/gles2_cmd_helper_autogen.h @@ -1127,5 +1127,26 @@ c.Init(buffer_id, count, type, offset, result_shm_id, result_shm_offset); } + void GenSharedIds( + GLuint namespace_id, GLuint id_offset, GLsizei n, uint32 ids_shm_id, + uint32 ids_shm_offset) { + gles2::GenSharedIds& c = GetCmdSpace<gles2::GenSharedIds>(); + c.Init(namespace_id, id_offset, n, ids_shm_id, ids_shm_offset); + } + + void DeleteSharedIds( + GLuint namespace_id, GLsizei n, uint32 ids_shm_id, + uint32 ids_shm_offset) { + gles2::DeleteSharedIds& c = GetCmdSpace<gles2::DeleteSharedIds>(); + c.Init(namespace_id, n, ids_shm_id, ids_shm_offset); + } + + void RegisterSharedIds( + GLuint namespace_id, GLsizei n, uint32 ids_shm_id, + uint32 ids_shm_offset) { + gles2::RegisterSharedIds& c = GetCmdSpace<gles2::RegisterSharedIds>(); + c.Init(namespace_id, n, ids_shm_id, ids_shm_offset); + } + #endif // GPU_COMMAND_BUFFER_CLIENT_GLES2_CMD_HELPER_AUTOGEN_H_ diff --git a/gpu/command_buffer/client/gles2_demo.cc b/gpu/command_buffer/client/gles2_demo.cc index 3e9ea77..b2a0497 100644 --- a/gpu/command_buffer/client/gles2_demo.cc +++ b/gpu/command_buffer/client/gles2_demo.cc @@ -84,7 +84,8 @@ bool GLES2Demo::Setup(void* hwnd, int32 size) { gles2::SetGLContext(new GLES2Implementation(helper, transfer_buffer.size, transfer_buffer.ptr, - transfer_buffer_id)); + transfer_buffer_id, + false)); GLFromCPPInit(); diff --git a/gpu/command_buffer/client/gles2_implementation.cc b/gpu/command_buffer/client/gles2_implementation.cc index 3886b95..551e361 100644 --- a/gpu/command_buffer/client/gles2_implementation.cc +++ b/gpu/command_buffer/client/gles2_implementation.cc @@ -6,6 +6,7 @@ #include "../client/gles2_implementation.h" #include "../common/gles2_cmd_utils.h" +#include "../common/id_allocator.h" namespace gpu { namespace gles2 { @@ -21,6 +22,71 @@ static GLsizei RoundUpToMultipleOf4(GLsizei size) { return (size + 3) & ~3; } +// An id handler for non-shared ids. +class NonSharedIdHandler : public IdHandlerInterface { + public: + NonSharedIdHandler() { } + virtual ~NonSharedIdHandler() { } + + // Overridden from IdHandlerInterface. + virtual void MakeIds(GLuint id_offset, GLsizei n, GLuint* ids) { + if (id_offset == 0) { + for (GLsizei ii = 0; ii < n; ++ii) { + ids[ii] = id_allocator_.AllocateID(); + } + } else { + for (GLsizei ii = 0; ii < n; ++ii) { + ids[ii] = id_allocator_.AllocateIDAtOrAbove(id_offset); + id_offset = ids[ii] + 1; + } + } + } + + // Overridden from IdHandlerInterface. + virtual void FreeIds(GLsizei n, const GLuint* ids) { + for (GLsizei ii = 0; ii < n; ++ii) { + id_allocator_.FreeID(ids[ii]); + } + } + + // Overridden from IdHandlerInterface. + virtual bool MarkAsUsedForBind(GLuint id) { + return id == 0 ? true : id_allocator_.MarkAsUsed(id); + } + private: + IdAllocator id_allocator_; +}; + +// An id handler for shared ids. +class SharedIdHandler : public IdHandlerInterface { + public: + SharedIdHandler( + GLES2Implementation* gles2, + id_namespaces::IdNamespaces id_namespace) + : gles2_(gles2), + id_namespace_(id_namespace) { + } + + virtual ~SharedIdHandler() { } + + virtual void MakeIds(GLuint id_offset, GLsizei n, GLuint* ids) { + gles2_->GenSharedIds(id_namespace_, id_offset, n, ids); + } + + virtual void FreeIds(GLsizei n, const GLuint* ids) { + gles2_->DeleteSharedIds(id_namespace_, n, ids); + } + + virtual bool MarkAsUsedForBind(GLuint) { // NOLINT + // This has no meaning for shared resources. + return true; + } + + private: + GLES2Implementation* gles2_; + id_namespaces::IdNamespaces id_namespace_; +}; + // This class tracks VertexAttribPointers and helps emulate client side buffers. // // The way client side buffers work is we shadow all the Vertex Attribs so we @@ -331,7 +397,8 @@ GLES2Implementation::GLES2Implementation( GLES2CmdHelper* helper, size_t transfer_buffer_size, void* transfer_buffer, - int32 transfer_buffer_id) + int32 transfer_buffer_id, + bool share_resources) : util_(0), // TODO(gman): Get real number of compressed texture formats. helper_(helper), transfer_buffer_( @@ -345,45 +412,49 @@ GLES2Implementation::GLES2Implementation( #if defined(GLES2_SUPPORT_CLIENT_SIDE_BUFFERS) bound_array_buffer_id_(0), bound_element_array_buffer_id_(0), + client_side_array_id_(0), + client_side_element_array_id_(0), #endif error_bits_(0) { // Allocate space for simple GL results. result_buffer_ = transfer_buffer; result_shm_offset_ = 0; + if (share_resources) { + buffer_id_handler_.reset( + new SharedIdHandler(this, id_namespaces::kBuffers)); + framebuffer_id_handler_.reset( + new SharedIdHandler(this, id_namespaces::kFramebuffers)); + renderbuffer_id_handler_.reset( + new SharedIdHandler(this, id_namespaces::kRenderbuffers)); + program_and_shader_id_handler_.reset( + new SharedIdHandler(this, id_namespaces::kProgramsAndShaders)); + texture_id_handler_.reset( + new SharedIdHandler(this, id_namespaces::kTextures)); + } else { + buffer_id_handler_.reset(new NonSharedIdHandler()); + framebuffer_id_handler_.reset(new NonSharedIdHandler()); + renderbuffer_id_handler_.reset(new NonSharedIdHandler()); + program_and_shader_id_handler_.reset(new NonSharedIdHandler()); + texture_id_handler_.reset(new NonSharedIdHandler()); + } + #if defined(GLES2_SUPPORT_CLIENT_SIDE_BUFFERS) GLint max_vertex_attribs; GetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs); - buffer_id_allocator_.MarkAsUsed(kClientSideArrayId); - buffer_id_allocator_.MarkAsUsed(kClientSideElementArrayId); - reserved_ids_[0] = kClientSideArrayId; - reserved_ids_[1] = kClientSideElementArrayId; + buffer_id_handler_->MakeIds( + kClientSideArrayId, arraysize(reserved_ids_), &reserved_ids_[0]); client_side_buffer_helper_.reset(new ClientSideBufferHelper( max_vertex_attribs, - kClientSideArrayId, - kClientSideElementArrayId)); + reserved_ids_[0], + reserved_ids_[1])); #endif } GLES2Implementation::~GLES2Implementation() { - GLuint buffers[] = { kClientSideArrayId, kClientSideElementArrayId, }; - DeleteBuffers(arraysize(buffers), &buffers[0]); -} - -void GLES2Implementation::MakeIds( - IdAllocator* id_allocator, GLsizei n, GLuint* ids) { - for (GLsizei ii = 0; ii < n; ++ii) { - ids[ii] = id_allocator->AllocateID(); - } -} - -void GLES2Implementation::FreeIds( - IdAllocator* id_allocator, GLsizei n, const GLuint* ids) { - for (GLsizei ii = 0; ii < n; ++ii) { - id_allocator->FreeID(ids[ii]); - } + DeleteBuffers(arraysize(reserved_ids_), &reserved_ids_[0]); } void GLES2Implementation::WaitForCmd() { @@ -580,6 +651,39 @@ void GLES2Implementation::SwapBuffers() { Flush(); } +void GLES2Implementation::GenSharedIds( + GLuint namespace_id, GLuint id_offset, GLsizei n, GLuint* ids) { + GLint* id_buffer = transfer_buffer_.AllocTyped<GLint>(n); + helper_->GenSharedIds(namespace_id, id_offset, n, + transfer_buffer_id_, + transfer_buffer_.GetOffset(id_buffer)); + WaitForCmd(); + memcpy(ids, id_buffer, sizeof(*ids) * n); + transfer_buffer_.FreePendingToken(id_buffer, helper_->InsertToken()); +} + +void GLES2Implementation::DeleteSharedIds( + GLuint namespace_id, GLsizei n, const GLuint* ids) { + GLint* id_buffer = transfer_buffer_.AllocTyped<GLint>(n); + memcpy(id_buffer, ids, sizeof(*ids) * n); + helper_->DeleteSharedIds(namespace_id, n, + transfer_buffer_id_, + transfer_buffer_.GetOffset(id_buffer)); + WaitForCmd(); + transfer_buffer_.FreePendingToken(id_buffer, helper_->InsertToken()); +} + +void GLES2Implementation::RegisterSharedIds( + GLuint namespace_id, GLsizei n, const GLuint* ids) { + GLint* id_buffer = transfer_buffer_.AllocTyped<GLint>(n); + memcpy(id_buffer, ids, sizeof(*ids) * n); + helper_->RegisterSharedIds(namespace_id, n, + transfer_buffer_id_, + transfer_buffer_.GetOffset(id_buffer)); + WaitForCmd(); + transfer_buffer_.FreePendingToken(id_buffer, helper_->InsertToken()); +} + void GLES2Implementation::BindAttribLocation( GLuint program, GLuint index, const char* name) { SetBucketAsString(kResultBucketId, name); @@ -1155,7 +1259,7 @@ void GLES2Implementation::ReadPixels( } transfer_buffer_.FreePendingToken(buffer, helper_->InsertToken()); // If it was not marked as successful exit. - if (*result == 0) { + if (*result != 0) { return; } yoffset += num_rows; @@ -1222,9 +1326,7 @@ void GLES2Implementation::BindBuffer(GLenum target, GLuint buffer) { SetGLError(GL_INVALID_OPERATION, "glBindBuffer: reserved buffer id"); return; } - if (buffer != 0) { - buffer_id_allocator_.MarkAsUsed(buffer); - } + buffer_id_handler_->MarkAsUsedForBind(buffer); switch (target) { case GL_ARRAY_BUFFER: bound_array_buffer_id_ = buffer; @@ -1239,7 +1341,7 @@ void GLES2Implementation::BindBuffer(GLenum target, GLuint buffer) { } void GLES2Implementation::DeleteBuffers(GLsizei n, const GLuint* buffers) { - FreeIds(&buffer_id_allocator_, n, buffers); + buffer_id_handler_->FreeIds(n, buffers); for (GLsizei ii = 0; ii < n; ++ii) { if (buffers[ii] == bound_array_buffer_id_) { bound_array_buffer_id_ = 0; diff --git a/gpu/command_buffer/client/gles2_implementation.h b/gpu/command_buffer/client/gles2_implementation.h index 4d0e53e..5ff41a2 100644 --- a/gpu/command_buffer/client/gles2_implementation.h +++ b/gpu/command_buffer/client/gles2_implementation.h @@ -11,7 +11,6 @@ #include "../common/gles2_cmd_utils.h" #include "../common/scoped_ptr.h" #include "../client/gles2_cmd_helper.h" -#include "../client/id_allocator.h" #include "../client/ring_buffer.h" #define GLES2_SUPPORT_CLIENT_SIDE_BUFFERS 1 @@ -21,6 +20,22 @@ namespace gles2 { class ClientSideBufferHelper; +// Base class for IdHandlers +class IdHandlerInterface { + public: + IdHandlerInterface() { } + virtual ~IdHandlerInterface() { } + + // Makes some ids at or above id_offset. + virtual void MakeIds(GLuint id_offset, GLsizei n, GLuint* ids) = 0; + + // Frees some ids. + virtual void FreeIds(GLsizei n, const GLuint* ids) = 0; + + // Marks an id as used for glBind functions. id = 0 does nothing. + virtual bool MarkAsUsedForBind(GLuint id) = 0; +}; + // This class emulates GLES2 over command buffers. It can be used by a client // program so that the program does not need deal with shared memory and command // buffer management. See gl2_lib.h. Note that there is a performance gain to @@ -49,7 +64,8 @@ class GLES2Implementation { GLES2CmdHelper* helper, size_t transfer_buffer_size, void* transfer_buffer, - int32 transfer_buffer_id); + int32 transfer_buffer_id, + bool share_resources); ~GLES2Implementation(); @@ -126,17 +142,15 @@ class GLES2Implementation { } #endif - void MakeIds(IdAllocator* id_allocator, GLsizei n, GLuint* ids); + GLuint MakeTextureId() { + GLuint id; + texture_id_handler_->MakeIds(0, 1, &id); + return id; + } - void FreeIds(IdAllocator* id_allocator, GLsizei n, const GLuint* ids); - - GLuint MakeTextureId() { - return texture_id_allocator_.AllocateID(); - } - - void FreeTextureId(GLuint id) { - texture_id_allocator_.FreeID(id); - } + void FreeTextureId(GLuint id) { + texture_id_handler_->FreeIds(1, &id); + } private: // Wraps RingBufferWrapper to provide aligned allocations. @@ -231,11 +245,11 @@ class GLES2Implementation { GLES2Util util_; GLES2CmdHelper* helper_; - IdAllocator buffer_id_allocator_; - IdAllocator framebuffer_id_allocator_; - IdAllocator renderbuffer_id_allocator_; - IdAllocator program_and_shader_id_allocator_; - IdAllocator texture_id_allocator_; + scoped_ptr<IdHandlerInterface> buffer_id_handler_; + scoped_ptr<IdHandlerInterface> framebuffer_id_handler_; + scoped_ptr<IdHandlerInterface> renderbuffer_id_handler_; + scoped_ptr<IdHandlerInterface> program_and_shader_id_handler_; + scoped_ptr<IdHandlerInterface> texture_id_handler_; AlignedRingBuffer transfer_buffer_; int transfer_buffer_id_; void* result_buffer_; @@ -255,6 +269,10 @@ class GLES2Implementation { // The currently bound element array buffer. GLuint bound_element_array_buffer_id_; + // GL names for the buffers used to emulate client side buffers. + GLuint client_side_array_id_; + GLuint client_side_element_array_id_; + // Info for each vertex attribute saved so we can simulate client side // buffers. scoped_ptr<ClientSideBufferHelper> client_side_buffer_helper_; diff --git a/gpu/command_buffer/client/gles2_implementation_autogen.h b/gpu/command_buffer/client/gles2_implementation_autogen.h index 06bcdcc..63d0b53 100644 --- a/gpu/command_buffer/client/gles2_implementation_autogen.h +++ b/gpu/command_buffer/client/gles2_implementation_autogen.h @@ -25,9 +25,7 @@ void BindFramebuffer(GLenum target, GLuint framebuffer) { GL_INVALID_OPERATION, "BindFramebuffer: framebuffer reserved id"); return; } - if (framebuffer != 0) { - framebuffer_id_allocator_.MarkAsUsed(framebuffer); - } + framebuffer_id_handler_->MarkAsUsedForBind(framebuffer); helper_->BindFramebuffer(target, framebuffer); } @@ -37,9 +35,7 @@ void BindRenderbuffer(GLenum target, GLuint renderbuffer) { GL_INVALID_OPERATION, "BindRenderbuffer: renderbuffer reserved id"); return; } - if (renderbuffer != 0) { - renderbuffer_id_allocator_.MarkAsUsed(renderbuffer); - } + renderbuffer_id_handler_->MarkAsUsedForBind(renderbuffer); helper_->BindRenderbuffer(target, renderbuffer); } @@ -48,9 +44,7 @@ void BindTexture(GLenum target, GLuint texture) { SetGLError(GL_INVALID_OPERATION, "BindTexture: texture reserved id"); return; } - if (texture != 0) { - texture_id_allocator_.MarkAsUsed(texture); - } + texture_id_handler_->MarkAsUsedForBind(texture); helper_->BindTexture(target, texture); } @@ -156,14 +150,14 @@ void CopyTexSubImage2D( GLuint CreateProgram() { GLuint client_id; - MakeIds(&program_and_shader_id_allocator_, 1, &client_id); + program_and_shader_id_handler_->MakeIds(0, 1, &client_id); helper_->CreateProgram(client_id); return client_id; } GLuint CreateShader(GLenum type) { GLuint client_id; - MakeIds(&program_and_shader_id_allocator_, 1, &client_id); + program_and_shader_id_handler_->MakeIds(0, 1, &client_id); helper_->CreateShader(type, client_id); return client_id; } @@ -173,25 +167,27 @@ void CullFace(GLenum mode) { } void DeleteFramebuffers(GLsizei n, const GLuint* framebuffers) { - FreeIds(&framebuffer_id_allocator_, n, framebuffers); + framebuffer_id_handler_->FreeIds(n, framebuffers); helper_->DeleteFramebuffersImmediate(n, framebuffers); } void DeleteProgram(GLuint program) { + program_and_shader_id_handler_->FreeIds(1, &program); helper_->DeleteProgram(program); } void DeleteRenderbuffers(GLsizei n, const GLuint* renderbuffers) { - FreeIds(&renderbuffer_id_allocator_, n, renderbuffers); + renderbuffer_id_handler_->FreeIds(n, renderbuffers); helper_->DeleteRenderbuffersImmediate(n, renderbuffers); } void DeleteShader(GLuint shader) { + program_and_shader_id_handler_->FreeIds(1, &shader); helper_->DeleteShader(shader); } void DeleteTextures(GLsizei n, const GLuint* textures) { - FreeIds(&texture_id_allocator_, n, textures); + texture_id_handler_->FreeIds(n, textures); helper_->DeleteTexturesImmediate(n, textures); } @@ -244,7 +240,7 @@ void FrontFace(GLenum mode) { } void GenBuffers(GLsizei n, GLuint* buffers) { - MakeIds(&buffer_id_allocator_, n, buffers); + buffer_id_handler_->MakeIds(0, n, buffers); helper_->GenBuffersImmediate(n, buffers); } @@ -253,17 +249,17 @@ void GenerateMipmap(GLenum target) { } void GenFramebuffers(GLsizei n, GLuint* framebuffers) { - MakeIds(&framebuffer_id_allocator_, n, framebuffers); + framebuffer_id_handler_->MakeIds(0, n, framebuffers); helper_->GenFramebuffersImmediate(n, framebuffers); } void GenRenderbuffers(GLsizei n, GLuint* renderbuffers) { - MakeIds(&renderbuffer_id_allocator_, n, renderbuffers); + renderbuffer_id_handler_->MakeIds(0, n, renderbuffers); helper_->GenRenderbuffersImmediate(n, renderbuffers); } void GenTextures(GLsizei n, GLuint* textures) { - MakeIds(&texture_id_allocator_, n, textures); + texture_id_handler_->MakeIds(0, n, textures); helper_->GenTexturesImmediate(n, textures); } @@ -759,5 +755,12 @@ GLuint GetMaxValueInBuffer( return *result; } +void GenSharedIds( + GLuint namespace_id, GLuint id_offset, GLsizei n, GLuint* ids); + +void DeleteSharedIds(GLuint namespace_id, GLsizei n, const GLuint* ids); + +void RegisterSharedIds(GLuint namespace_id, GLsizei n, const GLuint* ids); + #endif // GPU_COMMAND_BUFFER_CLIENT_GLES2_IMPLEMENTATION_AUTOGEN_H_ diff --git a/gpu/command_buffer/client/gles2_implementation_unittest.cc b/gpu/command_buffer/client/gles2_implementation_unittest.cc index 93710fc..e43bf84 100644 --- a/gpu/command_buffer/client/gles2_implementation_unittest.cc +++ b/gpu/command_buffer/client/gles2_implementation_unittest.cc @@ -152,7 +152,6 @@ class GLES2ImplementationTest : public testing::Test { command_buffer_.reset(new MockGLES2CommandBuffer()); command_buffer_->Initialize(kNumCommandEntries); - EXPECT_EQ(kTransferBufferId, command_buffer_->CreateTransferBuffer(kTransferBufferSize)); transfer_buffer_ = command_buffer_->GetTransferBuffer(kTransferBufferId); @@ -171,7 +170,8 @@ class GLES2ImplementationTest : public testing::Test { helper_.get(), kTransferBufferSize, transfer_buffer_.ptr, - kTransferBufferId)); + kTransferBufferId, + false)); EXPECT_CALL(*command_buffer_, OnFlush(_)).Times(1).RetiresOnSaturation(); helper_->CommandBufferHelper::Flush(); diff --git a/gpu/command_buffer/client/id_allocator.cc b/gpu/command_buffer/client/id_allocator.cc deleted file mode 100644 index 2d244d0..0000000 --- a/gpu/command_buffer/client/id_allocator.cc +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file contains the implementation of IdAllocator. - -#include "../client/id_allocator.h" -#include "../common/logging.h" - -namespace gpu { - -IdAllocator::IdAllocator() { -} - -unsigned int IdAllocator::FindFirstFree() const { - ResourceId id = 1; - for (ResourceIdSet::const_iterator it = used_ids_.begin(); - it != used_ids_.end(); ++it) { - if ((*it) != id) { - return id; - } - ++id; - } - return id; -} - -} // namespace gpu diff --git a/gpu/command_buffer/client/id_allocator.h b/gpu/command_buffer/client/id_allocator.h deleted file mode 100644 index 615f020..0000000 --- a/gpu/command_buffer/client/id_allocator.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file contains the definition of the IdAllocator class. - -#ifndef GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ -#define GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ - -#include <set> -#include <utility> -#include "../common/types.h" - -namespace gpu { - -// A resource ID, key to the resource maps. -typedef uint32 ResourceId; -// Invalid resource ID. -static const ResourceId kInvalidResource = 0u; - -// A class to manage the allocation of resource IDs. -class IdAllocator { - public: - IdAllocator(); - - // Allocates a new resource ID. - ResourceId AllocateID() { - ResourceId id = FindFirstFree(); - MarkAsUsed(id); - return id; - } - - // Marks an id as used. Returns false if id was already used. - bool MarkAsUsed(ResourceId id) { - std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id); - return result.second; - } - - // Frees a resource ID. - void FreeID(ResourceId id) { - used_ids_.erase(id); - } - - // Checks whether or not a resource ID is in use. - bool InUse(ResourceId id) const { - return used_ids_.find(id) != used_ids_.end(); - } - - private: - // TODO(gman): This would work much better with ranges. - typedef std::set<ResourceId> ResourceIdSet; - - ResourceId FindFirstFree() const; - - ResourceIdSet used_ids_; - - DISALLOW_COPY_AND_ASSIGN(IdAllocator); -}; - -} // namespace gpu - -#endif // GPU_COMMAND_BUFFER_CLIENT_ID_ALLOCATOR_H_ diff --git a/gpu/command_buffer/client/id_allocator_test.cc b/gpu/command_buffer/client/id_allocator_test.cc deleted file mode 100644 index eafadd7..0000000 --- a/gpu/command_buffer/client/id_allocator_test.cc +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file has the unit tests for the IdAllocator class. - -#include "gpu/command_buffer/client/id_allocator.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace gpu { - -class IdAllocatorTest : public testing::Test { - protected: - virtual void SetUp() {} - virtual void TearDown() {} - - IdAllocator* id_allocator() { return &id_allocator_; } - - private: - IdAllocator id_allocator_; -}; - -// Checks basic functionality: AllocateID, FreeID, InUse. -TEST_F(IdAllocatorTest, TestBasic) { - IdAllocator *allocator = id_allocator(); - // Check that resource 1 is not in use - EXPECT_FALSE(allocator->InUse(1)); - - // Allocate an ID, check that it's in use. - ResourceId id1 = allocator->AllocateID(); - EXPECT_TRUE(allocator->InUse(id1)); - - // Allocate another ID, check that it's in use, and different from the first - // one. - ResourceId id2 = allocator->AllocateID(); - EXPECT_TRUE(allocator->InUse(id2)); - EXPECT_NE(id1, id2); - - // Free one of the IDs, check that it's not in use any more. - allocator->FreeID(id1); - EXPECT_FALSE(allocator->InUse(id1)); - - // Frees the other ID, check that it's not in use any more. - allocator->FreeID(id2); - EXPECT_FALSE(allocator->InUse(id2)); -} - -// Checks that the resource IDs are re-used after being freed. -TEST_F(IdAllocatorTest, TestAdvanced) { - IdAllocator *allocator = id_allocator(); - - // Allocate a significant number of resources. - const unsigned int kNumResources = 100; - ResourceId ids[kNumResources]; - for (unsigned int i = 0; i < kNumResources; ++i) { - ids[i] = allocator->AllocateID(); - EXPECT_TRUE(allocator->InUse(ids[i])); - } - - // Check that a new allocation re-uses the resource we just freed. - ResourceId id1 = ids[kNumResources / 2]; - allocator->FreeID(id1); - EXPECT_FALSE(allocator->InUse(id1)); - ResourceId id2 = allocator->AllocateID(); - EXPECT_TRUE(allocator->InUse(id2)); - EXPECT_EQ(id1, id2); -} - -// Check that we can choose our own ids and they won't be reused. -TEST_F(IdAllocatorTest, MarkAsUsed) { - IdAllocator* allocator = id_allocator(); - ResourceId id = allocator->AllocateID(); - allocator->FreeID(id); - EXPECT_FALSE(allocator->InUse(id)); - EXPECT_TRUE(allocator->MarkAsUsed(id)); - EXPECT_TRUE(allocator->InUse(id)); - ResourceId id2 = allocator->AllocateID(); - EXPECT_NE(id, id2); - EXPECT_TRUE(allocator->MarkAsUsed(id2 + 1)); - ResourceId id3 = allocator->AllocateID(); - // Checks our algorithm. If the algorithm changes this check should be - // changed. - EXPECT_EQ(id3, id2 + 2); -} - -} // namespace gpu diff --git a/gpu/command_buffer/client/ring_buffer.h b/gpu/command_buffer/client/ring_buffer.h index 0b55661..38ee912 100644 --- a/gpu/command_buffer/client/ring_buffer.h +++ b/gpu/command_buffer/client/ring_buffer.h @@ -128,7 +128,7 @@ class RingBufferWrapper { // Returns: // the pointer to the allocated memory block, or NULL if out of // memory. - void *Alloc(unsigned int size) { + void* Alloc(unsigned int size) { RingBuffer::Offset offset = allocator_.Alloc(size); return GetPointer(offset); } @@ -144,8 +144,8 @@ class RingBufferWrapper { // Returns: // the pointer to the allocated memory block, or NULL if out of // memory. - template <typename T> T *AllocTyped(unsigned int count) { - return static_cast<T *>(Alloc(count * sizeof(T))); + template <typename T> T* AllocTyped(unsigned int count) { + return static_cast<T*>(Alloc(count * sizeof(T))); } // Frees a block of memory, pending the passage of a token. That memory won't @@ -154,18 +154,18 @@ class RingBufferWrapper { // Parameters: // pointer: the pointer to the memory block to free. // token: the token value to wait for before re-using the memory. - void FreePendingToken(void *pointer, unsigned int token) { + void FreePendingToken(void* pointer, unsigned int token) { DCHECK(pointer); allocator_.FreePendingToken(GetOffset(pointer), token); } // Gets a pointer to a memory block given the base memory and the offset. - void *GetPointer(RingBuffer::Offset offset) { + void* GetPointer(RingBuffer::Offset offset) { return static_cast<int8*>(base_) + offset; } // Gets the offset to a memory block given the base memory and the address. - RingBuffer::Offset GetOffset(void *pointer) { + RingBuffer::Offset GetOffset(void* pointer) { return static_cast<int8*>(pointer) - static_cast<int8*>(base_); } @@ -182,7 +182,7 @@ class RingBufferWrapper { private: RingBuffer allocator_; - void *base_; + void* base_; RingBuffer::Offset base_offset_; DISALLOW_IMPLICIT_CONSTRUCTORS(RingBufferWrapper); }; |