summaryrefslogtreecommitdiff
path: root/chromium/gpu/command_buffer/service/mailbox_manager.h
blob: c97302813a000dbaf80571bae28d7dc99164a3dc (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
// 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.

#ifndef GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_
#define GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_

#include <functional>
#include <map>

#include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h"
#include "crypto/hmac.h"
#include "gpu/command_buffer/common/constants.h"
#include "gpu/gpu_export.h"

// From gl2/gl2ext.h.
#ifndef GL_MAILBOX_SIZE_CHROMIUM
#define GL_MAILBOX_SIZE_CHROMIUM 64
#endif

typedef signed char GLbyte;

namespace gpu {
namespace gles2 {

class Texture;
class TextureManager;

// Identifies a mailbox where a texture definition can be stored for
// transferring textures between contexts that are not in the same context
// group. It is a random key signed with a hash of a private key.
struct GPU_EXPORT MailboxName {
  MailboxName();
  GLbyte key[GL_MAILBOX_SIZE_CHROMIUM / 2];
  GLbyte signature[GL_MAILBOX_SIZE_CHROMIUM / 2];
};

// Manages resources scoped beyond the context or context group level.
class GPU_EXPORT MailboxManager : public base::RefCounted<MailboxManager> {
 public:
  MailboxManager();

  // Generate a unique mailbox name signed with the manager's private key.
  void GenerateMailboxName(MailboxName* name);

  // Look up the texture definition from the named mailbox.
  Texture* ConsumeTexture(unsigned target, const MailboxName& name);

  // Put the texture into the named mailbox.
  bool ProduceTexture(unsigned target,
                      const MailboxName& name,
                      Texture* texture);

  // Destroy any mailbox that reference the given texture.
  void TextureDeleted(Texture* texture);

  std::string private_key() {
    return std::string(private_key_, sizeof(private_key_));
  }

 private:
  friend class base::RefCounted<MailboxManager>;

  ~MailboxManager();

  void SignMailboxName(MailboxName* name);
  bool IsMailboxNameValid(const MailboxName& name);

  struct TargetName {
    TargetName(unsigned target, const MailboxName& name);
    unsigned target;
    MailboxName name;
  };

  static bool TargetNameLess(const TargetName& lhs, const TargetName& rhs);

  // This is a bidirectional map between mailbox and textures. We can have
  // multiple mailboxes per texture, but one texture per mailbox. We keep an
  // iterator in the MailboxToTextureMap to be able to manage changes to
  // the TextureToMailboxMap efficiently.
  typedef std::multimap<Texture*, TargetName> TextureToMailboxMap;
  typedef std::map<
      TargetName,
      TextureToMailboxMap::iterator,
      std::pointer_to_binary_function<
          const TargetName&, const TargetName&, bool> > MailboxToTextureMap;

  char private_key_[GL_MAILBOX_SIZE_CHROMIUM / 2];
  crypto::HMAC hmac_;
  MailboxToTextureMap mailbox_to_textures_;
  TextureToMailboxMap textures_to_mailboxes_;

  DISALLOW_COPY_AND_ASSIGN(MailboxManager);
};
}  // namespage gles2
}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_