summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/mailbox_manager_sync.h
blob: ff16dd7e84c938b34d1c6abd63414e790e39797f (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
// Copyright 2014 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_SYNC_H_
#define GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_SYNC_H_

#include <map>
#include <utility>

#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "gpu/command_buffer/common/constants.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/service/mailbox_manager.h"
#include "gpu/command_buffer/service/texture_definition.h"
#include "gpu/gpu_export.h"

namespace gpu {
namespace gles2 {

class Texture;
class TextureManager;

// Manages resources scoped beyond the context or context group level
// and across threads and driver level share groups by synchronizing
// texture state.
class GPU_EXPORT MailboxManagerSync : public MailboxManager {
 public:
  MailboxManagerSync();

  // MailboxManager implementation:
  Texture* ConsumeTexture(const Mailbox& mailbox) override;
  void ProduceTexture(const Mailbox& mailbox, Texture* texture) override;
  bool UsesSync() override;
  void PushTextureUpdates(const SyncToken& token) override;
  void PullTextureUpdates(const SyncToken& token) override;
  void TextureDeleted(Texture* texture) override;

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

  static bool SkipTextureWorkarounds(const Texture* texture);

  ~MailboxManagerSync() override;

  class TextureGroup : public base::RefCounted<TextureGroup> {
   public:
    explicit TextureGroup(const TextureDefinition& definition);
    static TextureGroup* FromName(const Mailbox& name);

    void AddName(const Mailbox& name);
    void RemoveName(const Mailbox& name);

    void AddTexture(MailboxManagerSync* manager, Texture* texture);
    // Returns true if there are other textures left in the group after removal.
    bool RemoveTexture(MailboxManagerSync* manager, Texture* texture);
    Texture* FindTexture(MailboxManagerSync* manager);

    const TextureDefinition& GetDefinition() { return definition_; }
    void SetDefinition(TextureDefinition definition) {
      definition_ = definition;
    }

   private:
    friend class base::RefCounted<TextureGroup>;
    ~TextureGroup();

    typedef std::vector<std::pair<MailboxManagerSync*, Texture*>> TextureList;
    std::vector<Mailbox> names_;
    TextureList textures_;
    TextureDefinition definition_;

    typedef std::map<Mailbox, scoped_refptr<TextureGroup>>
        MailboxToGroupMap;
    static base::LazyInstance<MailboxToGroupMap> mailbox_to_group_;
  };

  struct TextureGroupRef {
    TextureGroupRef(unsigned version, TextureGroup* group);
    ~TextureGroupRef();
    unsigned version;
    scoped_refptr<TextureGroup> group;
  };
  static void UpdateDefinitionLocked(Texture* texture,
                                     TextureGroupRef* group_ref);

  typedef std::map<Texture*, TextureGroupRef> TextureToGroupMap;
  TextureToGroupMap texture_to_group_;

  DISALLOW_COPY_AND_ASSIGN(MailboxManagerSync);
};

}  // namespage gles2
}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_MAILBOX_MANAGER_SYNC_H_