- add sources.
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / mailbox_manager.cc
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 <algorithm>
8
9 #include "base/rand_util.h"
10 #include "crypto/hmac.h"
11 #include "gpu/command_buffer/service/texture_manager.h"
12
13 namespace gpu {
14 namespace gles2 {
15
16 MailboxName::MailboxName() {
17   std::fill(key, key + sizeof(key), 0);
18   std::fill(signature, signature + sizeof(signature), 0);
19 }
20
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_)));
27   DCHECK(success);
28   DCHECK(!IsMailboxNameValid(MailboxName()));
29 }
30
31 MailboxManager::~MailboxManager() {
32   DCHECK(mailbox_to_textures_.empty());
33   DCHECK(textures_to_mailboxes_.empty());
34 }
35
36 void MailboxManager::GenerateMailboxName(MailboxName* name) {
37   base::RandBytes(name->key, sizeof(name->key));
38   SignMailboxName(name);
39 }
40
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())
46     return NULL;
47
48   DCHECK(IsMailboxNameValid(name));
49   return it->second->first;
50 }
51
52 bool MailboxManager::ProduceTexture(unsigned target,
53                                     const MailboxName& name,
54                                     Texture* texture) {
55   if (!IsMailboxNameValid(name))
56     return false;
57
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);
65   }
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());
70
71   return true;
72 }
73
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);
81     DCHECK(count == 1);
82   }
83   textures_to_mailboxes_.erase(range.first, range.second);
84   DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());
85 }
86
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));
92   DCHECK(success);
93 }
94
95 bool MailboxManager::IsMailboxNameValid(const MailboxName& name) {
96   return hmac_.Verify(
97       base::StringPiece(reinterpret_cast<const char*>(name.key),
98           sizeof(name.key)),
99       base::StringPiece(reinterpret_cast<const char*>(name.signature),
100           sizeof(name.signature)));
101 }
102
103 MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name)
104     : target(target),
105       name(name) {
106 }
107
108 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs,
109                                     const MailboxManager::TargetName& rhs) {
110   return memcmp(&lhs, &rhs, sizeof(lhs)) < 0;
111 }
112
113 }  // namespace gles2
114 }  // namespace gpu