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.
5 #include "gpu/command_buffer/service/mailbox_manager.h"
9 #include "base/rand_util.h"
10 #include "crypto/hmac.h"
11 #include "gpu/command_buffer/service/texture_manager.h"
16 MailboxName::MailboxName() {
17 std::fill(key, key + sizeof(key), 0);
18 std::fill(signature, signature + sizeof(signature), 0);
21 MailboxManager::MailboxManager()
22 : hmac_(crypto::HMAC::SHA256),
23 mailbox_to_textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) {
24 base::RandBytes(private_key_, sizeof(private_key_));
25 bool success = hmac_.Init(
26 base::StringPiece(private_key_, sizeof(private_key_)));
28 DCHECK(!IsMailboxNameValid(MailboxName()));
31 MailboxManager::~MailboxManager() {
32 DCHECK(mailbox_to_textures_.empty());
33 DCHECK(textures_to_mailboxes_.empty());
36 void MailboxManager::GenerateMailboxName(MailboxName* name) {
37 base::RandBytes(name->key, sizeof(name->key));
38 SignMailboxName(name);
41 Texture* MailboxManager::ConsumeTexture(unsigned target,
42 const MailboxName& name) {
43 MailboxToTextureMap::iterator it =
44 mailbox_to_textures_.find(TargetName(target, name));
45 if (it == mailbox_to_textures_.end())
48 DCHECK(IsMailboxNameValid(name));
49 return it->second->first;
52 bool MailboxManager::ProduceTexture(unsigned target,
53 const MailboxName& name,
55 if (!IsMailboxNameValid(name))
58 texture->SetMailboxManager(this);
59 TargetName target_name(target, name);
60 MailboxToTextureMap::iterator it = mailbox_to_textures_.find(target_name);
61 if (it != mailbox_to_textures_.end()) {
62 TextureToMailboxMap::iterator texture_it = it->second;
63 mailbox_to_textures_.erase(it);
64 textures_to_mailboxes_.erase(texture_it);
66 TextureToMailboxMap::iterator texture_it =
67 textures_to_mailboxes_.insert(std::make_pair(texture, target_name));
68 mailbox_to_textures_.insert(std::make_pair(target_name, texture_it));
69 DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());
74 void MailboxManager::TextureDeleted(Texture* texture) {
75 std::pair<TextureToMailboxMap::iterator,
76 TextureToMailboxMap::iterator> range =
77 textures_to_mailboxes_.equal_range(texture);
78 for (TextureToMailboxMap::iterator it = range.first;
79 it != range.second; ++it) {
80 size_t count = mailbox_to_textures_.erase(it->second);
83 textures_to_mailboxes_.erase(range.first, range.second);
84 DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());
87 void MailboxManager::SignMailboxName(MailboxName* name) {
88 bool success = hmac_.Sign(
89 base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)),
90 reinterpret_cast<unsigned char*>(name->signature),
91 sizeof(name->signature));
95 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) {
97 base::StringPiece(reinterpret_cast<const char*>(name.key),
99 base::StringPiece(reinterpret_cast<const char*>(name.signature),
100 sizeof(name.signature)));
103 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name)
108 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs,
109 const MailboxManager::TargetName& rhs) {
110 return memcmp(&lhs, &rhs, sizeof(lhs)) < 0;