summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/mailbox_manager.cc
blob: bb2f7262a2f11ad3810d84bba3b4c1fcfe59ee59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/mailbox_manager.h"

#include <algorithm>

#include "base/rand_util.h"
#include "crypto/hmac.h"
#include "gpu/command_buffer/service/texture_manager.h"

namespace gpu {
namespace gles2 {

MailboxName::MailboxName() {
  std::fill(key, key + sizeof(key), 0);
  std::fill(signature, signature + sizeof(signature), 0);
}

MailboxManager::MailboxManager()
    : hmac_(crypto::HMAC::SHA256),
      mailbox_to_textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) {
  base::RandBytes(private_key_, sizeof(private_key_));
  bool success = hmac_.Init(
      base::StringPiece(private_key_, sizeof(private_key_)));
  DCHECK(success);
  DCHECK(!IsMailboxNameValid(MailboxName()));
}

MailboxManager::~MailboxManager() {
  DCHECK(mailbox_to_textures_.empty());
  DCHECK(textures_to_mailboxes_.empty());
}

void MailboxManager::GenerateMailboxName(MailboxName* name) {
  base::RandBytes(name->key, sizeof(name->key));
  SignMailboxName(name);
}

Texture* MailboxManager::ConsumeTexture(unsigned target,
                                        const MailboxName& name) {
  MailboxToTextureMap::iterator it =
      mailbox_to_textures_.find(TargetName(target, name));
  if (it == mailbox_to_textures_.end())
    return NULL;

  DCHECK(IsMailboxNameValid(name));
  return it->second->first;
}

bool MailboxManager::ProduceTexture(unsigned target,
                                    const MailboxName& name,
                                    Texture* texture) {
  if (!IsMailboxNameValid(name))
    return false;

  texture->SetMailboxManager(this);
  TargetName target_name(target, name);
  MailboxToTextureMap::iterator it = mailbox_to_textures_.find(target_name);
  if (it != mailbox_to_textures_.end()) {
    TextureToMailboxMap::iterator texture_it = it->second;
    mailbox_to_textures_.erase(it);
    textures_to_mailboxes_.erase(texture_it);
  }
  TextureToMailboxMap::iterator texture_it =
      textures_to_mailboxes_.insert(std::make_pair(texture, target_name));
  mailbox_to_textures_.insert(std::make_pair(target_name, texture_it));
  DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());

  return true;
}

void MailboxManager::TextureDeleted(Texture* texture) {
  std::pair<TextureToMailboxMap::iterator,
            TextureToMailboxMap::iterator> range =
      textures_to_mailboxes_.equal_range(texture);
  for (TextureToMailboxMap::iterator it = range.first;
       it != range.second; ++it) {
    size_t count = mailbox_to_textures_.erase(it->second);
    DCHECK(count == 1);
  }
  textures_to_mailboxes_.erase(range.first, range.second);
  DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());
}

void MailboxManager::SignMailboxName(MailboxName* name) {
  bool success = hmac_.Sign(
      base::StringPiece(reinterpret_cast<char*>(name->key), sizeof(name->key)),
      reinterpret_cast<unsigned char*>(name->signature),
      sizeof(name->signature));
  DCHECK(success);
}

bool MailboxManager::IsMailboxNameValid(const MailboxName& name) {
  return hmac_.Verify(
      base::StringPiece(reinterpret_cast<const char*>(name.key),
          sizeof(name.key)),
      base::StringPiece(reinterpret_cast<const char*>(name.signature),
          sizeof(name.signature)));
}

MailboxManager::TargetName::TargetName(unsigned target, const MailboxName& name)
    : target(target),
      name(name) {
}

bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs,
                                    const MailboxManager::TargetName& rhs) {
  return memcmp(&lhs, &rhs, sizeof(lhs)) < 0;
}

}  // namespace gles2
}  // namespace gpu