OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "crypto/secure_hash.h" |
| 6 #include "crypto/symmetric_key.h" |
| 7 #include "gpu/command_buffer/service/gl_utils.h" |
| 8 #include "gpu/command_buffer/service/mailbox_manager.h" |
| 9 #include "gpu/command_buffer/service/texture_definition.h" |
| 10 |
| 11 namespace gpu { |
| 12 namespace gles2 { |
| 13 |
| 14 MailboxManager::MailboxManager() { |
| 15 crypto::SymmetricKey* key = |
| 16 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, |
| 17 sizeof(private_key_) * 8); |
| 18 std::string raw_key; |
| 19 key->GetRawKey(&raw_key); |
| 20 |
| 21 DCHECK(raw_key.length() == sizeof(private_key_)); |
| 22 |
| 23 raw_key.copy(reinterpret_cast<char*>(private_key_), sizeof(private_key_)); |
| 24 } |
| 25 |
| 26 MailboxManager::~MailboxManager() { |
| 27 } |
| 28 |
| 29 void MailboxManager::GenerateMailboxName(MailboxName* name) { |
| 30 crypto::SymmetricKey* key = |
| 31 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, |
| 32 sizeof(name->key) * 8); |
| 33 std::string raw_key; |
| 34 key->GetRawKey(&raw_key); |
| 35 |
| 36 DCHECK(raw_key.length() == sizeof(name->key)); |
| 37 |
| 38 raw_key.copy(reinterpret_cast<char*>(name->key), sizeof(name->key)); |
| 39 |
| 40 SignMailboxName(name); |
| 41 } |
| 42 |
| 43 TextureDefinition* MailboxManager::ConsumeTexture(unsigned target, |
| 44 const MailboxName& name) { |
| 45 if (!IsMailboxNameValid(name)) |
| 46 return NULL; |
| 47 |
| 48 TextureDefinitionMap::iterator it = |
| 49 textures_.find(TargetName(target, name)); |
| 50 if (it == textures_.end()) { |
| 51 NOTREACHED(); |
| 52 return NULL; |
| 53 } |
| 54 |
| 55 TextureDefinition* definition = it->second.definition.release(); |
| 56 textures_.erase(it); |
| 57 |
| 58 return definition; |
| 59 } |
| 60 |
| 61 bool MailboxManager::ProduceTexture(unsigned target, |
| 62 const MailboxName& name, |
| 63 TextureDefinition* definition, |
| 64 TextureManager* owner) { |
| 65 if (!IsMailboxNameValid(name)) |
| 66 return false; |
| 67 |
| 68 TextureDefinitionMap::iterator it = |
| 69 textures_.find(TargetName(target, name)); |
| 70 if (it != textures_.end()) { |
| 71 NOTREACHED(); |
| 72 GLuint service_id = it->second.definition->ReleaseServiceId(); |
| 73 glDeleteTextures(1, &service_id); |
| 74 it->second = OwnedTextureDefinition(definition, owner); |
| 75 } else { |
| 76 textures_.insert(std::make_pair( |
| 77 TargetName(target, name), |
| 78 OwnedTextureDefinition(definition, owner))); |
| 79 } |
| 80 |
| 81 return true; |
| 82 } |
| 83 |
| 84 void MailboxManager::DestroyOwnedTextures(TextureManager* owner, |
| 85 bool have_context) { |
| 86 TextureDefinitionMap::iterator it = textures_.begin(); |
| 87 while (it != textures_.end()) { |
| 88 if (it->second.owner == owner) { |
| 89 NOTREACHED(); |
| 90 GLuint service_id = it->second.definition->ReleaseServiceId(); |
| 91 if (have_context) |
| 92 glDeleteTextures(1, &service_id); |
| 93 it = textures_.erase(it); |
| 94 } else { |
| 95 ++it; |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 void MailboxManager::SignMailboxName(MailboxName* name) { |
| 101 scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create( |
| 102 crypto::SecureHash::SHA256)); |
| 103 |
| 104 hash->Update(name->key, sizeof(name->key)); |
| 105 hash->Update(private_key_, sizeof(private_key_)); |
| 106 hash->Finish(name->signature, sizeof(name->signature)); |
| 107 } |
| 108 |
| 109 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { |
| 110 MailboxName signed_name = name; |
| 111 SignMailboxName(&signed_name); |
| 112 |
| 113 return memcmp(name.signature, |
| 114 signed_name.signature, |
| 115 sizeof(name.signature)) == 0; |
| 116 } |
| 117 |
| 118 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) |
| 119 : target(target), |
| 120 name(name) { |
| 121 } |
| 122 |
| 123 size_t MailboxManager::TargetNameCompare::operator() ( |
| 124 const TargetName& value) const { |
| 125 size_t hash = value.target; |
| 126 for (int i = 0; i < arraysize(value.name.key); ++i) |
| 127 hash ^= value.name.key[i]; |
| 128 return hash; |
| 129 } |
| 130 |
| 131 int MailboxManager::TargetNameCompare::operator() ( |
| 132 const TargetName& lhs, |
| 133 const TargetName& rhs) const { |
| 134 return memcmp(&lhs, &rhs, sizeof(lhs)); |
| 135 } |
| 136 |
| 137 MailboxManager::OwnedTextureDefinition::OwnedTextureDefinition( |
| 138 TextureDefinition* definition, |
| 139 TextureManager* owner) |
| 140 : definition(definition), |
| 141 owner(owner) { |
| 142 } |
| 143 |
| 144 MailboxManager::OwnedTextureDefinition::~OwnedTextureDefinition() { |
| 145 } |
| 146 } // namespace gles2 |
| 147 } // namespace gpu |
OLD | NEW |