| 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 MailboxToTextureMap::iterator it = |
| 47 textures_.find(TargetName(target, name)); | 47 mailbox_to_textures_.find(TargetName(target, name)); |
| 48 if (it == textures_.end()) | 48 if (it == mailbox_to_textures_.end()) |
| 49 return NULL; | 49 return NULL; |
| 50 | 50 |
| 51 TextureDefinition* definition = it->second.definition.release(); | 51 return it->second->first; |
| 52 textures_.erase(it); | |
| 53 | |
| 54 return definition; | |
| 55 } | 52 } |
| 56 | 53 |
| 57 bool MailboxManager::ProduceTexture(unsigned target, | 54 bool MailboxManager::ProduceTexture(unsigned target, |
| 58 const MailboxName& name, | 55 const MailboxName& name, |
| 59 TextureDefinition* definition, | 56 Texture* texture) { |
| 60 TextureManager* owner) { | |
| 61 if (!IsMailboxNameValid(name)) | 57 if (!IsMailboxNameValid(name)) |
| 62 return false; | 58 return false; |
| 63 | 59 |
| 64 TextureDefinitionMap::iterator it = | 60 texture->SetMailboxManager(this); |
| 65 textures_.find(TargetName(target, name)); | 61 TargetName target_name(target, name); |
| 66 if (it != textures_.end()) { | 62 MailboxToTextureMap::iterator it = mailbox_to_textures_.find(target_name); |
| 67 NOTREACHED(); | 63 if (it != mailbox_to_textures_.end()) { |
| 68 GLuint service_id = it->second.definition->ReleaseServiceId(); | 64 TextureToMailboxMap::iterator texture_it = it->second; |
| 69 glDeleteTextures(1, &service_id); | 65 mailbox_to_textures_.erase(it); |
| 70 it->second = OwnedTextureDefinition(definition, owner); | 66 textures_to_mailboxes_.erase(texture_it); |
| 71 } else { | |
| 72 textures_.insert(std::make_pair( | |
| 73 TargetName(target, name), | |
| 74 OwnedTextureDefinition(definition, owner))); | |
| 75 } | 67 } |
| 68 TextureToMailboxMap::iterator texture_it = |
| 69 textures_to_mailboxes_.insert(std::make_pair(texture, target_name)); |
| 70 mailbox_to_textures_.insert(std::make_pair(target_name, texture_it)); |
| 71 DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size()); |
| 76 | 72 |
| 77 return true; | 73 return true; |
| 78 } | 74 } |
| 79 | 75 |
| 80 void MailboxManager::DestroyOwnedTextures(TextureManager* owner, | 76 void MailboxManager::TextureDeleted(Texture* texture) { |
| 81 bool have_context) { | 77 std::pair<TextureToMailboxMap::iterator, |
| 82 TextureDefinitionMap::iterator it = textures_.begin(); | 78 TextureToMailboxMap::iterator> range = |
| 83 while (it != textures_.end()) { | 79 textures_to_mailboxes_.equal_range(texture); |
| 84 TextureDefinitionMap::iterator current_it = it; | 80 DCHECK(range.first != range.second); |
| 85 ++it; | 81 for (TextureToMailboxMap::iterator it = range.first; |
| 86 if (current_it->second.owner == owner) { | 82 it != range.second; ++it) { |
| 87 GLuint service_id = current_it->second.definition->ReleaseServiceId(); | 83 size_t count = mailbox_to_textures_.erase(it->second); |
| 88 if (have_context) | 84 DCHECK(count == 1); |
| 89 glDeleteTextures(1, &service_id); | |
| 90 textures_.erase(current_it); | |
| 91 } | |
| 92 } | 85 } |
| 86 textures_to_mailboxes_.erase(range.first, range.second); |
| 87 DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size()); |
| 93 } | 88 } |
| 94 | 89 |
| 95 void MailboxManager::SignMailboxName(MailboxName* name) { | 90 void MailboxManager::SignMailboxName(MailboxName* name) { |
| 96 bool success = hmac_.Sign( | 91 bool success = hmac_.Sign( |
| 97 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), | 92 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), |
| 98 reinterpret_cast<unsigned char*>(name->signature), | 93 reinterpret_cast<unsigned char*>(name->signature), |
| 99 sizeof(name->signature)); | 94 sizeof(name->signature)); |
| 100 DCHECK(success); | 95 DCHECK(success); |
| 101 } | 96 } |
| 102 | 97 |
| 103 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { | 98 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { |
| 104 return hmac_.Verify( | 99 return hmac_.Verify( |
| 105 base::StringPiece(reinterpret_cast<const char*>(name.key), | 100 base::StringPiece(reinterpret_cast<const char*>(name.key), |
| 106 sizeof(name.key)), | 101 sizeof(name.key)), |
| 107 base::StringPiece(reinterpret_cast<const char*>(name.signature), | 102 base::StringPiece(reinterpret_cast<const char*>(name.signature), |
| 108 sizeof(name.signature))); | 103 sizeof(name.signature))); |
| 109 } | 104 } |
| 110 | 105 |
| 111 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) | 106 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) |
| 112 : target(target), | 107 : target(target), |
| 113 name(name) { | 108 name(name) { |
| 114 } | 109 } |
| 115 | 110 |
| 116 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, | 111 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, |
| 117 const MailboxManager::TargetName& rhs) { | 112 const MailboxManager::TargetName& rhs) { |
| 118 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; | 113 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; |
| 119 } | 114 } |
| 120 | 115 |
| 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 | 116 } // namespace gles2 |
| 132 } // namespace gpu | 117 } // namespace gpu |
| OLD | NEW |