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/gl_utils.h" |
no sievers
2013/05/24 01:31:38
nit: I think gl_utils.h is not needed anymore.
piman
2013/05/24 02:32:22
Done.
| |
12 #include "gpu/command_buffer/service/texture_definition.h" | 12 #include "gpu/command_buffer/service/texture_manager.h" |
13 | 13 |
14 namespace gpu { | 14 namespace gpu { |
15 namespace gles2 { | 15 namespace gles2 { |
16 | 16 |
17 MailboxName::MailboxName() { | 17 MailboxName::MailboxName() { |
18 std::fill(key, key + sizeof(key), 0); | 18 std::fill(key, key + sizeof(key), 0); |
19 std::fill(signature, signature + sizeof(signature), 0); | 19 std::fill(signature, signature + sizeof(signature), 0); |
20 } | 20 } |
21 | 21 |
22 MailboxManager::MailboxManager() | 22 MailboxManager::MailboxManager() |
23 : hmac_(crypto::HMAC::SHA256), | 23 : hmac_(crypto::HMAC::SHA256), |
24 textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { | 24 textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) { |
25 base::RandBytes(private_key_, sizeof(private_key_)); | 25 base::RandBytes(private_key_, sizeof(private_key_)); |
26 bool success = hmac_.Init( | 26 bool success = hmac_.Init( |
27 base::StringPiece(private_key_, sizeof(private_key_))); | 27 base::StringPiece(private_key_, sizeof(private_key_))); |
28 DCHECK(success); | 28 DCHECK(success); |
29 DCHECK(!IsMailboxNameValid(MailboxName())); | 29 DCHECK(!IsMailboxNameValid(MailboxName())); |
30 } | 30 } |
31 | 31 |
32 MailboxManager::~MailboxManager() { | 32 MailboxManager::~MailboxManager() { |
33 DCHECK(!textures_.size()); | 33 DCHECK(!textures_.size()); |
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 = textures_.find(TargetName(target, name)); |
47 textures_.find(TargetName(target, name)); | |
48 if (it == textures_.end()) | 47 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 |
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 TextureMap::iterator it = textures_.find(TargetName(target, name)); |
66 if (it != textures_.end()) { | 61 if (it != textures_.end()) |
67 NOTREACHED(); | 62 it->second = texture; |
68 GLuint service_id = it->second.definition->ReleaseServiceId(); | 63 else |
69 glDeleteTextures(1, &service_id); | 64 textures_.insert(std::make_pair(TargetName(target, name), texture)); |
70 it->second = OwnedTextureDefinition(definition, owner); | |
71 } else { | |
72 textures_.insert(std::make_pair( | |
73 TargetName(target, name), | |
74 OwnedTextureDefinition(definition, owner))); | |
75 } | |
76 | 65 |
77 return true; | 66 return true; |
78 } | 67 } |
79 | 68 |
80 void MailboxManager::DestroyOwnedTextures(TextureManager* owner, | 69 void MailboxManager::TextureDeleted(Texture* texture) { |
81 bool have_context) { | 70 TextureMap::iterator it = textures_.begin(); |
82 TextureDefinitionMap::iterator it = textures_.begin(); | |
83 while (it != textures_.end()) { | 71 while (it != textures_.end()) { |
apatrick_chromium
2013/05/23 18:37:11
This might now be a more common operation. Before
piman
2013/05/24 02:32:22
Done.
| |
84 TextureDefinitionMap::iterator current_it = it; | 72 TextureMap::iterator current_it = it; |
85 ++it; | 73 ++it; |
86 if (current_it->second.owner == owner) { | 74 if (current_it->second == texture) |
87 GLuint service_id = current_it->second.definition->ReleaseServiceId(); | |
88 if (have_context) | |
89 glDeleteTextures(1, &service_id); | |
90 textures_.erase(current_it); | 75 textures_.erase(current_it); |
91 } | |
92 } | 76 } |
93 } | 77 } |
94 | 78 |
95 void MailboxManager::SignMailboxName(MailboxName* name) { | 79 void MailboxManager::SignMailboxName(MailboxName* name) { |
96 bool success = hmac_.Sign( | 80 bool success = hmac_.Sign( |
97 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), | 81 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)), |
98 reinterpret_cast<unsigned char*>(name->signature), | 82 reinterpret_cast<unsigned char*>(name->signature), |
99 sizeof(name->signature)); | 83 sizeof(name->signature)); |
100 DCHECK(success); | 84 DCHECK(success); |
101 } | 85 } |
102 | 86 |
103 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { | 87 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) { |
104 return hmac_.Verify( | 88 return hmac_.Verify( |
105 base::StringPiece(reinterpret_cast<const char*>(name.key), | 89 base::StringPiece(reinterpret_cast<const char*>(name.key), |
106 sizeof(name.key)), | 90 sizeof(name.key)), |
107 base::StringPiece(reinterpret_cast<const char*>(name.signature), | 91 base::StringPiece(reinterpret_cast<const char*>(name.signature), |
108 sizeof(name.signature))); | 92 sizeof(name.signature))); |
109 } | 93 } |
110 | 94 |
111 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) | 95 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name) |
112 : target(target), | 96 : target(target), |
113 name(name) { | 97 name(name) { |
114 } | 98 } |
115 | 99 |
116 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, | 100 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs, |
117 const MailboxManager::TargetName& rhs) { | 101 const MailboxManager::TargetName& rhs) { |
118 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; | 102 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0; |
119 } | 103 } |
120 | 104 |
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 | 105 } // namespace gles2 |
132 } // namespace gpu | 106 } // namespace gpu |
OLD | NEW |