gpu: Change Produce/ConsumeTexture to allow texture sharing
[chromium-blink-merge.git] / gpu / command_buffer / service / mailbox_manager.h
blobc97302813a000dbaf80571bae28d7dc99164a3dc
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 #ifndef GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_
8 #include <functional>
9 #include <map>
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"
17 // From gl2/gl2ext.h.
18 #ifndef GL_MAILBOX_SIZE_CHROMIUM
19 #define GL_MAILBOX_SIZE_CHROMIUM 64
20 #endif
22 typedef signed char GLbyte;
24 namespace gpu {
25 namespace gles2 {
27 class Texture;
28 class TextureManager;
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];
39 // Manages resources scoped beyond the context or context group level.
40 class GPU_EXPORT MailboxManager : public base::RefCounted<MailboxManager> {
41 public:
42 MailboxManager();
44 // Generate a unique mailbox name signed with the manager's private key.
45 void GenerateMailboxName(MailboxName* name);
47 // Look up the texture definition from the named mailbox.
48 Texture* ConsumeTexture(unsigned target, const MailboxName& name);
50 // Put the texture into the named mailbox.
51 bool ProduceTexture(unsigned target,
52 const MailboxName& name,
53 Texture* texture);
55 // Destroy any mailbox that reference the given texture.
56 void TextureDeleted(Texture* texture);
58 std::string private_key() {
59 return std::string(private_key_, sizeof(private_key_));
62 private:
63 friend class base::RefCounted<MailboxManager>;
65 ~MailboxManager();
67 void SignMailboxName(MailboxName* name);
68 bool IsMailboxNameValid(const MailboxName& name);
70 struct TargetName {
71 TargetName(unsigned target, const MailboxName& name);
72 unsigned target;
73 MailboxName name;
76 static bool TargetNameLess(const TargetName& lhs, const TargetName& rhs);
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;
89 char private_key_[GL_MAILBOX_SIZE_CHROMIUM / 2];
90 crypto::HMAC hmac_;
91 MailboxToTextureMap mailbox_to_textures_;
92 TextureToMailboxMap textures_to_mailboxes_;
94 DISALLOW_COPY_AND_ASSIGN(MailboxManager);
96 } // namespage gles2
97 } // namespace gpu
99 #endif // GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_H_