1// Copyright 2014 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_SYNCHRONIZER_H_
6#define GPU_COMMAND_BUFFER_SERVICE_MAILBOX_SYNCHRONIZER_H_
7
8#include "gpu/command_buffer/common/mailbox.h"
9
10#include <map>
11#include <set>
12
13#include "base/memory/linked_ptr.h"
14#include "base/synchronization/lock.h"
15#include "gpu/command_buffer/service/texture_definition.h"
16#include "gpu/gpu_export.h"
17
18namespace gpu {
19namespace gles2 {
20
21class MailboxManager;
22class Texture;
23
24// A thread-safe proxy that can be used to emulate texture sharing across
25// share-groups.
26class MailboxSynchronizer {
27 public:
28  ~MailboxSynchronizer();
29
30  GPU_EXPORT static bool Initialize();
31  GPU_EXPORT static void Terminate();
32  static MailboxSynchronizer* GetInstance();
33
34  // Create a texture from a globally visible mailbox.
35  Texture* CreateTextureFromMailbox(unsigned target, const Mailbox& mailbox);
36
37  void PushTextureUpdates(MailboxManager* manager);
38  void PullTextureUpdates(MailboxManager* manager);
39
40  void TextureDeleted(Texture* texture);
41
42 private:
43  MailboxSynchronizer();
44
45  struct TargetName {
46    TargetName(unsigned target, const Mailbox& mailbox);
47    bool operator<(const TargetName& rhs) const {
48      return memcmp(this, &rhs, sizeof(rhs)) < 0;
49    }
50    bool operator!=(const TargetName& rhs) const {
51      return memcmp(this, &rhs, sizeof(rhs)) != 0;
52    }
53    bool operator==(const TargetName& rhs) const {
54      return !operator!=(rhs);
55    }
56    unsigned target;
57    Mailbox mailbox;
58  };
59
60  base::Lock lock_;
61
62  struct TextureGroup {
63    explicit TextureGroup(const TextureDefinition& definition);
64    ~TextureGroup();
65
66    TextureDefinition definition;
67    std::set<TargetName> mailboxes;
68   private:
69    DISALLOW_COPY_AND_ASSIGN(TextureGroup);
70  };
71
72  struct TextureVersion {
73    explicit TextureVersion(linked_ptr<TextureGroup> group);
74    ~TextureVersion();
75
76    unsigned int version;
77    linked_ptr<TextureGroup> group;
78  };
79  typedef std::map<Texture*, TextureVersion> TextureMap;
80  TextureMap textures_;
81
82  linked_ptr<TextureGroup> GetGroupForMailboxLocked(
83      const TargetName& target_name);
84  void ReassociateMailboxLocked(
85      const TargetName& target_name,
86      TextureGroup* group);
87  void UpdateTextureLocked(Texture* texture, TextureVersion& texture_version);
88
89  DISALLOW_COPY_AND_ASSIGN(MailboxSynchronizer);
90};
91
92}  // namespage gles2
93}  // namespace gpu
94
95#endif  // GPU_COMMAND_BUFFER_SERVICE_MAILBOX_SYNCHRONIZER_H_
96
97