| 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 <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 10 #include "crypto/hmac.h" | 11 #include "crypto/hmac.h" |
| 11 #include "gpu/command_buffer/service/gl_utils.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 |
| 21 const MailboxManager::TexturePoolId MailboxManager::TexturePoolNone = |
| 22 std::make_pair(TEXTURE_POOL_NONE, 0); |
| 23 |
| 22 MailboxManager::MailboxManager() | 24 MailboxManager::MailboxManager() |
| 23 : hmac_(crypto::HMAC::SHA256), | 25 : hmac_(crypto::HMAC::SHA256), |
| 24 textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { | 26 textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { |
| 25 base::RandBytes(private_key_, sizeof(private_key_)); | 27 base::RandBytes(private_key_, sizeof(private_key_)); |
| 26 bool success = hmac_.Init( | 28 bool success = hmac_.Init( |
| 27 base::StringPiece(private_key_, sizeof(private_key_))); | 29 base::StringPiece(private_key_, sizeof(private_key_))); |
| 28 DCHECK(success); | 30 DCHECK(success); |
| 29 DCHECK(!IsMailboxNameValid(MailboxName())); | 31 DCHECK(!IsMailboxNameValid(MailboxName())); |
| 30 } | 32 } |
| 31 | 33 |
| 32 MailboxManager::~MailboxManager() { | 34 MailboxManager::~MailboxManager() { |
| 33 DCHECK(!textures_.size()); | 35 DCHECK(!textures_.size()); |
| 34 } | 36 } |
| 35 | 37 |
| 36 void MailboxManager::GenerateMailboxName(MailboxName* name) { | 38 void MailboxManager::GenerateMailboxName(MailboxName* name) { |
| 37 base::RandBytes(name->key, sizeof(name->key)); | 39 base::RandBytes(name->key, sizeof(name->key)); |
| 38 SignMailboxName(name); | 40 SignMailboxName(name); |
| 39 } | 41 } |
| 40 | 42 |
| 41 TextureDefinition* MailboxManager::ConsumeTexture(unsigned target, | 43 TextureDefinition* MailboxManager::ConsumeTexture(unsigned target, |
| 42 const MailboxName& name) { | 44 const MailboxName& name) { |
| 43 if (!IsMailboxNameValid(name)) | 45 if (!IsMailboxNameValid(name)) |
| 44 return NULL; | 46 return NULL; |
| 45 | 47 |
| 46 TextureDefinitionMap::iterator it = | 48 TextureDefinitionMap::iterator it = |
| 47 textures_.find(TargetName(target, name)); | 49 textures_.find(TargetName(target, name)); |
| 48 if (it == textures_.end()) | 50 if (it == textures_.end()) |
| 49 return NULL; | 51 return NULL; |
| 50 | 52 |
| 51 TextureDefinition* definition = it->second.definition.release(); | 53 TextureDefinition* definition = it->second.definition; |
| 54 TexturePoolId pool_id = it->second.pool_id; |
| 55 |
| 56 if (pool_id != TexturePoolNone) |
| 57 StartSharedTextureTransfer(definition); |
| 58 |
| 52 textures_.erase(it); | 59 textures_.erase(it); |
| 53 | 60 |
| 61 definition->RemoveObserver(this); |
| 54 return definition; | 62 return definition; |
| 55 } | 63 } |
| 56 | 64 |
| 57 bool MailboxManager::ProduceTexture(unsigned target, | 65 bool MailboxManager::ProduceTexture(unsigned target, |
| 58 const MailboxName& name, | 66 const MailboxName& name, |
| 59 TextureDefinition* definition, | 67 TextureDefinition* definition, |
| 60 TextureManager* owner) { | 68 TexturePoolId pool_id) { |
| 61 if (!IsMailboxNameValid(name)) | 69 if (!IsMailboxNameValid(name)) |
| 62 return false; | 70 return false; |
| 63 | 71 |
| 64 TextureDefinitionMap::iterator it = | 72 TextureDefinitionMap::iterator it = |
| 65 textures_.find(TargetName(target, name)); | 73 textures_.find(TargetName(target, name)); |
| 74 MailboxTexture texture(definition, pool_id); |
| 66 if (it != textures_.end()) { | 75 if (it != textures_.end()) { |
| 67 NOTREACHED(); | 76 NOTREACHED(); |
| 68 GLuint service_id = it->second.definition->ReleaseServiceId(); | 77 it->second = texture; |
| 69 glDeleteTextures(1, &service_id); | |
| 70 it->second = OwnedTextureDefinition(definition, owner); | |
| 71 } else { | 78 } else { |
| 72 textures_.insert(std::make_pair( | 79 textures_.insert(std::make_pair(TargetName(target, name), texture)); |
| 73 TargetName(target, name), | |
| 74 OwnedTextureDefinition(definition, owner))); | |
| 75 } | 80 } |
| 76 | 81 |
| 82 if (pool_id != TexturePoolNone) |
| 83 StartUsingSharedTexture(definition); |
| 84 |
| 85 definition->AddObserver(this); |
| 77 return true; | 86 return true; |
| 78 } | 87 } |
| 79 | 88 |
| 80 void MailboxManager::DestroyOwnedTextures(TextureManager* owner, | 89 void MailboxManager::OnDestroySharedTexture( |
| 81 bool have_context) { | 90 const TextureDefinition* definition) { |
| 91 TextureDefinitionMap::iterator it = textures_.begin(); |
| 92 bool found = false; |
| 93 while (it != textures_.end()) { |
| 94 TextureDefinitionMap::iterator current_it = it; |
| 95 ++it; |
| 96 if (current_it->second.definition == definition) { |
| 97 DCHECK(!found); |
| 98 textures_.erase(current_it); |
| 99 found = true; |
| 100 } |
| 101 } |
| 102 DCHECK(found); |
| 103 } |
| 104 |
| 105 void MailboxManager::RemoveTexturesFromPool(TexturePoolId pool_id, |
| 106 bool have_context) { |
| 107 DCHECK(pool_id != TexturePoolNone); |
| 82 TextureDefinitionMap::iterator it = textures_.begin(); | 108 TextureDefinitionMap::iterator it = textures_.begin(); |
| 83 while (it != textures_.end()) { | 109 while (it != textures_.end()) { |
| 84 TextureDefinitionMap::iterator current_it = it; | 110 TextureDefinitionMap::iterator current_it = it; |
| 85 ++it; | 111 ++it; |
| 86 if (current_it->second.owner == owner) { | 112 if (current_it->second.pool_id == pool_id) { |
| 87 GLuint service_id = current_it->second.definition->ReleaseServiceId(); | 113 StopUsingSharedTexture(current_it->second.definition, have_context); |
| 88 if (have_context) | |
| 89 glDeleteTextures(1, &service_id); | |
| 90 textures_.erase(current_it); | 114 textures_.erase(current_it); |
| 91 } | 115 } |
| 92 } | 116 } |
| 93 } | 117 } |
| 94 | 118 |
| 95 void MailboxManager::SignMailboxName(MailboxName* name) { | 119 void MailboxManager::SignMailboxName(MailboxName* name) { |
| 96 bool success = hmac_.Sign( | 120 bool success = hmac_.Sign( |
| 97 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), | 121 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), |
| 98 reinterpret_cast<unsigned char*>(name->signature), | 122 reinterpret_cast<unsigned char*>(name->signature), |
| 99 sizeof(name->signature)); | 123 sizeof(name->signature)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 111 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) | 135 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) |
| 112 : target(target), | 136 : target(target), |
| 113 name(name) { | 137 name(name) { |
| 114 } | 138 } |
| 115 | 139 |
| 116 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, | 140 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, |
| 117 const MailboxManager::TargetName& rhs) { | 141 const MailboxManager::TargetName& rhs) { |
| 118 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; | 142 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; |
| 119 } | 143 } |
| 120 | 144 |
| 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 | 145 } // namespace gles2 |
| 132 } // namespace gpu | 146 } // namespace gpu |
| OLD | NEW |