Chromium Code Reviews| 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 #include "gpu/command_buffer/service/mailbox_manager.h" | 5 #include "gpu/command_buffer/service/mailbox_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "crypto/hmac.h" | 10 #include "crypto/hmac.h" |
| 11 #include "gpu/command_buffer/service/gl_utils.h" | 11 #include "gpu/command_buffer/service/texture_manager.h" |
| 12 #include "gpu/command_buffer/service/texture_definition.h" | |
| 13 | 12 |
| 14 namespace gpu { | 13 namespace gpu { |
| 15 namespace gles2 { | 14 namespace gles2 { |
| 16 | 15 |
| 17 MailboxName::MailboxName() { | 16 MailboxName::MailboxName() { |
| 18 std::fill(key, key + sizeof(key), 0); | 17 std::fill(key, key + sizeof(key), 0); |
| 19 std::fill(signature, signature + sizeof(signature), 0); | 18 std::fill(signature, signature + sizeof(signature), 0); |
| 20 } | 19 } |
| 21 | 20 |
| 22 MailboxManager::MailboxManager() | 21 MailboxManager::MailboxManager() |
| 23 : hmac_(crypto::HMAC::SHA256), | 22 : hmac_(crypto::HMAC::SHA256), |
| 24 textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { | 23 mailbox_to_textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { |
| 25 base::RandBytes(private_key_, sizeof(private_key_)); | 24 base::RandBytes(private_key_, sizeof(private_key_)); |
| 26 bool success = hmac_.Init( | 25 bool success = hmac_.Init( |
| 27 base::StringPiece(private_key_, sizeof(private_key_))); | 26 base::StringPiece(private_key_, sizeof(private_key_))); |
| 28 DCHECK(success); | 27 DCHECK(success); |
| 29 DCHECK(!IsMailboxNameValid(MailboxName())); | 28 DCHECK(!IsMailboxNameValid(MailboxName())); |
| 30 } | 29 } |
| 31 | 30 |
| 32 MailboxManager::~MailboxManager() { | 31 MailboxManager::~MailboxManager() { |
| 33 DCHECK(!textures_.size()); | 32 DCHECK(mailbox_to_textures_.empty()); |
| 33 DCHECK(textures_to_mailboxes_.empty()); | |
| 34 } | 34 } |
| 35 | 35 |
| 36 void MailboxManager::GenerateMailboxName(MailboxName* name) { | 36 void MailboxManager::GenerateMailboxName(MailboxName* name) { |
| 37 base::RandBytes(name->key, sizeof(name->key)); | 37 base::RandBytes(name->key, sizeof(name->key)); |
| 38 SignMailboxName(name); | 38 SignMailboxName(name); |
| 39 } | 39 } |
| 40 | 40 |
| 41 TextureDefinition* MailboxManager::ConsumeTexture(unsigned target, | 41 Texture* MailboxManager::ConsumeTexture(unsigned target, |
| 42 const MailboxName& name) { | 42 const MailboxName& name) { |
| 43 if (!IsMailboxNameValid(name)) | 43 if (!IsMailboxNameValid(name)) |
| 44 return NULL; | 44 return NULL; |
| 45 | 45 |
| 46 TextureDefinitionMap::iterator it = | 46 TextureMap::iterator it = mailbox_to_textures_.find(TargetName(target, name)); |
| 47 textures_.find(TargetName(target, name)); | 47 if (it == mailbox_to_textures_.end()) |
| 48 if (it == textures_.end()) | |
| 49 return NULL; | 48 return NULL; |
| 50 | 49 |
| 51 TextureDefinition* definition = it->second.definition.release(); | 50 return it->second; |
| 52 textures_.erase(it); | |
| 53 | |
| 54 return definition; | |
| 55 } | 51 } |
| 56 | 52 |
|
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,
| |
| 57 bool MailboxManager::ProduceTexture(unsigned target, | 53 bool MailboxManager::ProduceTexture(unsigned target, |
| 58 const MailboxName& name, | 54 const MailboxName& name, |
| 59 TextureDefinition* definition, | 55 Texture* texture) { |
| 60 TextureManager* owner) { | |
| 61 if (!IsMailboxNameValid(name)) | 56 if (!IsMailboxNameValid(name)) |
| 62 return false; | 57 return false; |
| 63 | 58 |
| 64 TextureDefinitionMap::iterator it = | 59 texture->SetMailboxManager(this); |
| 65 textures_.find(TargetName(target, name)); | 60 TargetName target_name(target, name); |
| 66 if (it != textures_.end()) { | 61 TextureMap::iterator it = mailbox_to_textures_.find(target_name); |
| 67 NOTREACHED(); | 62 if (it != mailbox_to_textures_.end()) { |
| 68 GLuint service_id = it->second.definition->ReleaseServiceId(); | 63 std::pair<TextureReverseMap::iterator, TextureReverseMap::iterator> range = |
| 69 glDeleteTextures(1, &service_id); | 64 textures_to_mailboxes_.equal_range(it->second); |
| 70 it->second = OwnedTextureDefinition(definition, owner); | 65 DCHECK(range.first != range.second); |
| 66 bool found = false; | |
| 67 for (TextureReverseMap::iterator it2 = range.first; | |
| 68 it2 != range.second; ++it) { | |
| 69 if (!memcmp(&it->second, &it2->first, sizeof(it->second))) { | |
| 70 textures_to_mailboxes_.erase(it2); | |
| 71 found = true; | |
| 72 break; | |
| 73 } | |
| 74 } | |
| 75 DCHECK(found); | |
| 76 it->second = texture; | |
| 71 } else { | 77 } else { |
| 72 textures_.insert(std::make_pair( | 78 mailbox_to_textures_.insert(std::make_pair(target_name, texture)); |
| 73 TargetName(target, name), | |
| 74 OwnedTextureDefinition(definition, owner))); | |
| 75 } | 79 } |
| 80 textures_to_mailboxes_.insert(std::make_pair(texture, target_name)); | |
| 81 DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size()); | |
| 76 | 82 |
| 77 return true; | 83 return true; |
| 78 } | 84 } |
| 79 | 85 |
| 80 void MailboxManager::DestroyOwnedTextures(TextureManager* owner, | 86 void MailboxManager::TextureDeleted(Texture* texture) { |
| 81 bool have_context) { | 87 std::pair<TextureReverseMap::iterator, TextureReverseMap::iterator> range = |
| 82 TextureDefinitionMap::iterator it = textures_.begin(); | 88 textures_to_mailboxes_.equal_range(texture); |
| 83 while (it != textures_.end()) { | 89 DCHECK(range.first != range.second); |
| 84 TextureDefinitionMap::iterator current_it = it; | 90 for (TextureReverseMap::iterator it = range.first; |
| 85 ++it; | 91 it != range.second; ++it) { |
| 86 if (current_it->second.owner == owner) { | 92 size_t count = mailbox_to_textures_.erase(it->second); |
| 87 GLuint service_id = current_it->second.definition->ReleaseServiceId(); | 93 DCHECK(count == 1); |
| 88 if (have_context) | |
| 89 glDeleteTextures(1, &service_id); | |
| 90 textures_.erase(current_it); | |
| 91 } | |
| 92 } | 94 } |
| 95 textures_to_mailboxes_.erase(range.first, range.second); | |
| 96 DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size()); | |
| 93 } | 97 } |
| 94 | 98 |
| 95 void MailboxManager::SignMailboxName(MailboxName* name) { | 99 void MailboxManager::SignMailboxName(MailboxName* name) { |
| 96 bool success = hmac_.Sign( | 100 bool success = hmac_.Sign( |
| 97 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), | 101 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), |
| 98 reinterpret_cast<unsigned char*>(name->signature), | 102 reinterpret_cast<unsigned char*>(name->signature), |
| 99 sizeof(name->signature)); | 103 sizeof(name->signature)); |
| 100 DCHECK(success); | 104 DCHECK(success); |
| 101 } | 105 } |
| 102 | 106 |
| 103 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { | 107 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { |
| 104 return hmac_.Verify( | 108 return hmac_.Verify( |
| 105 base::StringPiece(reinterpret_cast<const char*>(name.key), | 109 base::StringPiece(reinterpret_cast<const char*>(name.key), |
| 106 sizeof(name.key)), | 110 sizeof(name.key)), |
| 107 base::StringPiece(reinterpret_cast<const char*>(name.signature), | 111 base::StringPiece(reinterpret_cast<const char*>(name.signature), |
| 108 sizeof(name.signature))); | 112 sizeof(name.signature))); |
| 109 } | 113 } |
| 110 | 114 |
| 111 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) | 115 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) |
| 112 : target(target), | 116 : target(target), |
| 113 name(name) { | 117 name(name) { |
| 114 } | 118 } |
| 115 | 119 |
| 116 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, | 120 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, |
| 117 const MailboxManager::TargetName& rhs) { | 121 const MailboxManager::TargetName& rhs) { |
| 118 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; | 122 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; |
| 119 } | 123 } |
| 120 | 124 |
| 121 MailboxManager::OwnedTextureDefinition::OwnedTextureDefinition( | |
| 122 TextureDefinition* definition, | |
| 123 TextureManager* owner) | |
| 124 : definition(definition), | |
| 125 owner(owner) { | |
| 126 } | |
| 127 | |
| 128 MailboxManager::OwnedTextureDefinition::~OwnedTextureDefinition() { | |
| 129 } | |
| 130 | |
| 131 } // namespace gles2 | 125 } // namespace gles2 |
| 132 } // namespace gpu | 126 } // namespace gpu |
| OLD | NEW |