- add sources.
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / mailbox_manager.h
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 #ifndef GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_
7
8 #include <functional>
9 #include <map>
10
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "crypto/hmac.h"
14 #include "gpu/command_buffer/common/constants.h"
15 #include "gpu/gpu_export.h"
16
17 // From gl2/gl2ext.h.
18 #ifndef GL_MAILBOX_SIZE_CHROMIUM
19 #define GL_MAILBOX_SIZE_CHROMIUM 64
20 #endif
21
22 typedef signed char GLbyte;
23
24 namespace gpu {
25 namespace gles2 {
26
27 class Texture;
28 class TextureManager;
29
30 // Identifies a mailbox where a texture definition can be stored for
31 // transferring textures between contexts that are not in the same context
32 // group. It is a random key signed with a hash of a private key.
33 struct GPU_EXPORT MailboxName {
34   MailboxName();
35   GLbyte key[GL_MAILBOX_SIZE_CHROMIUM / 2];
36   GLbyte signature[GL_MAILBOX_SIZE_CHROMIUM / 2];
37 };
38
39 // Manages resources scoped beyond the context or context group level.
40 class GPU_EXPORT MailboxManager : public base::RefCounted<MailboxManager> {
41  public:
42   MailboxManager();
43
44   // Generate a unique mailbox name signed with the manager's private key.
45   void GenerateMailboxName(MailboxName* name);
46
47   // Look up the texture definition from the named mailbox.
48   Texture* ConsumeTexture(unsigned target, const MailboxName& name);
49
50   // Put the texture into the named mailbox.
51   bool ProduceTexture(unsigned target,
52                       const MailboxName& name,
53                       Texture* texture);
54
55   // Destroy any mailbox that reference the given texture.
56   void TextureDeleted(Texture* texture);
57
58   std::string private_key() {
59     return std::string(private_key_, sizeof(private_key_));
60   }
61
62  private:
63   friend class base::RefCounted<MailboxManager>;
64
65   ~MailboxManager();
66
67   void SignMailboxName(MailboxName* name);
68   bool IsMailboxNameValid(const MailboxName& name);
69
70   struct TargetName {
71     TargetName(unsigned target, const MailboxName& name);
72     unsigned target;
73     MailboxName name;
74   };
75
76   static bool TargetNameLess(const TargetName& lhs, const TargetName& rhs);
77
78   // This is a bidirectional map between mailbox and textures. We can have
79   // multiple mailboxes per texture, but one texture per mailbox. We keep an
80   // iterator in the MailboxToTextureMap to be able to manage changes to
81   // the TextureToMailboxMap efficiently.
82   typedef std::multimap<Texture*, TargetName> TextureToMailboxMap;
83   typedef std::map<
84       TargetName,
85       TextureToMailboxMap::iterator,
86       std::pointer_to_binary_function<
87           const TargetName&, const TargetName&, bool> > MailboxToTextureMap;
88
89   char private_key_[GL_MAILBOX_SIZE_CHROMIUM / 2];
90   crypto::HMAC hmac_;
91   MailboxToTextureMap mailbox_to_textures_;
92   TextureToMailboxMap textures_to_mailboxes_;
93
94   DISALLOW_COPY_AND_ASSIGN(MailboxManager);
95 };
96 }  // namespage gles2
97 }  // namespace gpu
98
99 #endif  // GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_
100
101