Chromium Code Reviews| Index: gpu/command_buffer/service/mailbox_manager.cc |
| diff --git a/gpu/command_buffer/service/mailbox_manager.cc b/gpu/command_buffer/service/mailbox_manager.cc |
| index 90193934f3a4dd40ba797266978847b653241e69..4782d5e7decc24583e84f095d0164bc0cf6781df 100644 |
| --- a/gpu/command_buffer/service/mailbox_manager.cc |
| +++ b/gpu/command_buffer/service/mailbox_manager.cc |
| @@ -8,8 +8,7 @@ |
| #include "base/rand_util.h" |
| #include "crypto/hmac.h" |
| -#include "gpu/command_buffer/service/gl_utils.h" |
| -#include "gpu/command_buffer/service/texture_definition.h" |
| +#include "gpu/command_buffer/service/texture_manager.h" |
| namespace gpu { |
| namespace gles2 { |
| @@ -21,7 +20,7 @@ MailboxName::MailboxName() { |
| MailboxManager::MailboxManager() |
| : hmac_(crypto::HMAC::SHA256), |
| - textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { |
| + mailbox_to_textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { |
| base::RandBytes(private_key_, sizeof(private_key_)); |
| bool success = hmac_.Init( |
| base::StringPiece(private_key_, sizeof(private_key_))); |
| @@ -30,7 +29,8 @@ MailboxManager::MailboxManager() |
| } |
| MailboxManager::~MailboxManager() { |
| - DCHECK(!textures_.size()); |
| + DCHECK(mailbox_to_textures_.empty()); |
| + DCHECK(textures_to_mailboxes_.empty()); |
| } |
| void MailboxManager::GenerateMailboxName(MailboxName* name) { |
| @@ -38,58 +38,62 @@ void MailboxManager::GenerateMailboxName(MailboxName* name) { |
| SignMailboxName(name); |
| } |
| -TextureDefinition* MailboxManager::ConsumeTexture(unsigned target, |
| - const MailboxName& name) { |
| +Texture* MailboxManager::ConsumeTexture(unsigned target, |
| + const MailboxName& name) { |
| if (!IsMailboxNameValid(name)) |
| return NULL; |
| - TextureDefinitionMap::iterator it = |
| - textures_.find(TargetName(target, name)); |
| - if (it == textures_.end()) |
| + TextureMap::iterator it = mailbox_to_textures_.find(TargetName(target, name)); |
| + if (it == mailbox_to_textures_.end()) |
| return NULL; |
| - TextureDefinition* definition = it->second.definition.release(); |
| - textures_.erase(it); |
| - |
| - return definition; |
| + return it->second; |
| } |
|
apatrick_chromium
2013/05/24 20:51:58
I believe we have discovered an interview question
piman
2013/05/24 23:03:27
Thanks for the suggestion! I did the map<mailbox,
|
| bool MailboxManager::ProduceTexture(unsigned target, |
| const MailboxName& name, |
| - TextureDefinition* definition, |
| - TextureManager* owner) { |
| + Texture* texture) { |
| if (!IsMailboxNameValid(name)) |
| return false; |
| - TextureDefinitionMap::iterator it = |
| - textures_.find(TargetName(target, name)); |
| - if (it != textures_.end()) { |
| - NOTREACHED(); |
| - GLuint service_id = it->second.definition->ReleaseServiceId(); |
| - glDeleteTextures(1, &service_id); |
| - it->second = OwnedTextureDefinition(definition, owner); |
| + texture->SetMailboxManager(this); |
| + TargetName target_name(target, name); |
| + TextureMap::iterator it = mailbox_to_textures_.find(target_name); |
| + if (it != mailbox_to_textures_.end()) { |
| + std::pair<TextureReverseMap::iterator, TextureReverseMap::iterator> range = |
| + textures_to_mailboxes_.equal_range(it->second); |
| + DCHECK(range.first != range.second); |
| + bool found = false; |
| + for (TextureReverseMap::iterator it2 = range.first; |
| + it2 != range.second; ++it) { |
| + if (!memcmp(&it->second, &it2->first, sizeof(it->second))) { |
| + textures_to_mailboxes_.erase(it2); |
| + found = true; |
| + break; |
| + } |
| + } |
| + DCHECK(found); |
| + it->second = texture; |
| } else { |
| - textures_.insert(std::make_pair( |
| - TargetName(target, name), |
| - OwnedTextureDefinition(definition, owner))); |
| + mailbox_to_textures_.insert(std::make_pair(target_name, texture)); |
| } |
| + textures_to_mailboxes_.insert(std::make_pair(texture, target_name)); |
| + DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size()); |
| return true; |
| } |
| -void MailboxManager::DestroyOwnedTextures(TextureManager* owner, |
| - bool have_context) { |
| - TextureDefinitionMap::iterator it = textures_.begin(); |
| - while (it != textures_.end()) { |
| - TextureDefinitionMap::iterator current_it = it; |
| - ++it; |
| - if (current_it->second.owner == owner) { |
| - GLuint service_id = current_it->second.definition->ReleaseServiceId(); |
| - if (have_context) |
| - glDeleteTextures(1, &service_id); |
| - textures_.erase(current_it); |
| - } |
| +void MailboxManager::TextureDeleted(Texture* texture) { |
| + std::pair<TextureReverseMap::iterator, TextureReverseMap::iterator> range = |
| + textures_to_mailboxes_.equal_range(texture); |
| + DCHECK(range.first != range.second); |
| + for (TextureReverseMap::iterator it = range.first; |
| + it != range.second; ++it) { |
| + size_t count = mailbox_to_textures_.erase(it->second); |
| + DCHECK(count == 1); |
| } |
| + textures_to_mailboxes_.erase(range.first, range.second); |
| + DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size()); |
| } |
| void MailboxManager::SignMailboxName(MailboxName* name) { |
| @@ -118,15 +122,5 @@ bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, |
| return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; |
| } |
| -MailboxManager::OwnedTextureDefinition::OwnedTextureDefinition( |
| - TextureDefinition* definition, |
| - TextureManager* owner) |
| - : definition(definition), |
| - owner(owner) { |
| -} |
| - |
| -MailboxManager::OwnedTextureDefinition::~OwnedTextureDefinition() { |
| -} |
| - |
| } // namespace gles2 |
| } // namespace gpu |