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