| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 #include "gpu/command_buffer/service/mailbox_synchronizer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "gpu/command_buffer/service/mailbox_manager.h" |
| 9 #include "gpu/command_buffer/service/texture_manager.h" |
| 10 |
| 11 namespace gpu { |
| 12 namespace gles2 { |
| 13 |
| 14 namespace { |
| 15 |
| 16 MailboxSynchronizer* g_instance = NULL; |
| 17 |
| 18 } // anonymous namespace |
| 19 |
| 20 // static |
| 21 bool MailboxSynchronizer::Initialize() { |
| 22 DCHECK(!g_instance); |
| 23 g_instance = new MailboxSynchronizer; |
| 24 return true; |
| 25 } |
| 26 |
| 27 // static |
| 28 void MailboxSynchronizer::Terminate() { |
| 29 DCHECK(g_instance); |
| 30 delete g_instance; |
| 31 g_instance = NULL; |
| 32 } |
| 33 |
| 34 // static |
| 35 MailboxSynchronizer* MailboxSynchronizer::GetInstance() { |
| 36 return g_instance; |
| 37 } |
| 38 |
| 39 MailboxSynchronizer::TargetName::TargetName(unsigned target, |
| 40 const Mailbox& mailbox) |
| 41 : target(target), mailbox(mailbox) {} |
| 42 |
| 43 MailboxSynchronizer::TextureGroup::TextureGroup( |
| 44 const TextureDefinition& definition) |
| 45 : definition(definition) {} |
| 46 |
| 47 MailboxSynchronizer::TextureGroup::~TextureGroup() {} |
| 48 |
| 49 MailboxSynchronizer::TextureVersion::TextureVersion( |
| 50 linked_ptr<TextureGroup> group) |
| 51 : version(group->definition.version()), group(group) {} |
| 52 |
| 53 MailboxSynchronizer::TextureVersion::~TextureVersion() {} |
| 54 |
| 55 MailboxSynchronizer::MailboxSynchronizer() {} |
| 56 |
| 57 MailboxSynchronizer::~MailboxSynchronizer() { |
| 58 DCHECK_EQ(0U, textures_.size()); |
| 59 } |
| 60 |
| 61 linked_ptr<MailboxSynchronizer::TextureGroup> |
| 62 MailboxSynchronizer::ReassociateMailboxLocked(const TargetName& target_name, |
| 63 TextureGroup* group) { |
| 64 lock_.AssertAcquired(); |
| 65 linked_ptr<TextureGroup> old_group; |
| 66 for (TextureMap::iterator it = textures_.begin(); it != textures_.end(); |
| 67 it++) { |
| 68 std::set<TargetName>::iterator mb_it = |
| 69 it->second.group->mailboxes.find(target_name); |
| 70 if (it->second.group != group && |
| 71 mb_it != it->second.group->mailboxes.end()) { |
| 72 old_group = it->second.group; |
| 73 it->second.group->mailboxes.erase(mb_it); |
| 74 } |
| 75 } |
| 76 group->mailboxes.insert(target_name); |
| 77 return old_group; |
| 78 } |
| 79 |
| 80 linked_ptr<MailboxSynchronizer::TextureGroup> |
| 81 MailboxSynchronizer::GetGroupForMailboxLocked(const TargetName& target_name) { |
| 82 lock_.AssertAcquired(); |
| 83 for (TextureMap::iterator it = textures_.begin(); it != textures_.end(); |
| 84 it++) { |
| 85 std::set<TargetName>::const_iterator mb_it = |
| 86 it->second.group->mailboxes.find(target_name); |
| 87 if (mb_it != it->second.group->mailboxes.end()) |
| 88 return it->second.group; |
| 89 } |
| 90 return make_linked_ptr<MailboxSynchronizer::TextureGroup>(NULL); |
| 91 } |
| 92 |
| 93 Texture* MailboxSynchronizer::CreateTextureFromMailbox(unsigned target, |
| 94 const Mailbox& mailbox) { |
| 95 base::AutoLock lock(lock_); |
| 96 TargetName target_name(target, mailbox); |
| 97 linked_ptr<TextureGroup> group = GetGroupForMailboxLocked(target_name); |
| 98 if (group.get()) { |
| 99 Texture* new_texture = group->definition.CreateTexture(); |
| 100 if (new_texture) |
| 101 textures_.insert(std::make_pair(new_texture, TextureVersion(group))); |
| 102 return new_texture; |
| 103 } |
| 104 |
| 105 return NULL; |
| 106 } |
| 107 |
| 108 void MailboxSynchronizer::TextureDeleted(Texture* texture) { |
| 109 base::AutoLock lock(lock_); |
| 110 textures_.erase(texture); |
| 111 } |
| 112 |
| 113 void MailboxSynchronizer::PushTextureUpdates(MailboxManager* manager) { |
| 114 base::AutoLock lock(lock_); |
| 115 for (MailboxManager::MailboxToTextureMap::const_iterator texture_it = |
| 116 manager->mailbox_to_textures_.begin(); |
| 117 texture_it != manager->mailbox_to_textures_.end(); |
| 118 texture_it++) { |
| 119 TargetName target_name(texture_it->first.target, texture_it->first.mailbox); |
| 120 Texture* texture = texture_it->second->first; |
| 121 // TODO(sievers) |
| 122 bool needs_mips = texture->min_filter() != GL_NEAREST && |
| 123 texture->min_filter() != GL_LINEAR; |
| 124 if (target_name.target != GL_TEXTURE_2D || needs_mips) |
| 125 continue; |
| 126 |
| 127 TextureMap::iterator it = textures_.find(texture); |
| 128 if (it != textures_.end()) { |
| 129 gfx::GLImage* gl_image = texture->GetLevelImage(texture->target(), 0); |
| 130 TextureGroup* group = it->second.group.get(); |
| 131 std::set<TargetName>::const_iterator mb_it = |
| 132 group->mailboxes.find(target_name); |
| 133 scoped_refptr<NativeImageBuffer> image = group->definition.image(); |
| 134 if (mb_it == group->mailboxes.end()) { |
| 135 // We previously did not associate this texture with the given mailbox. |
| 136 // Unlink other texture groups from the mailbox. |
| 137 linked_ptr<TextureGroup> old_group = |
| 138 ReassociateMailboxLocked(target_name, group); |
| 139 image = old_group->definition.image(); |
| 140 } |
| 141 |
| 142 // Make sure we don't clobber with an older version |
| 143 if (group->definition.IsNewerThan(it->second.version)) |
| 144 continue; |
| 145 |
| 146 // Also don't push redundant updates. Note that it would break the |
| 147 // versioning. |
| 148 if (group->definition.Matches(texture)) |
| 149 continue; |
| 150 |
| 151 if (gl_image && !image->IsClient(gl_image)) { |
| 152 LOG(ERROR) << "MailboxSync: Incompatible attachment"; |
| 153 return; |
| 154 } |
| 155 |
| 156 group->definition = TextureDefinition( |
| 157 target_name.target, |
| 158 texture, |
| 159 ++it->second.version, |
| 160 gl_image ? image : NULL); |
| 161 } else { |
| 162 linked_ptr<TextureGroup> group = make_linked_ptr(new TextureGroup( |
| 163 TextureDefinition(target_name.target, texture, 1, NULL))); |
| 164 |
| 165 // Unlink other textures from this mailbox in case the name is not new. |
| 166 ReassociateMailboxLocked(target_name, group.get()); |
| 167 textures_.insert(std::make_pair(texture, TextureVersion(group))); |
| 168 } |
| 169 } |
| 170 // Make sure all write fences are flushed. |
| 171 glFlush(); |
| 172 } |
| 173 |
| 174 void MailboxSynchronizer::PullTextureUpdates(MailboxManager* manager) { |
| 175 base::AutoLock lock(lock_); |
| 176 for (MailboxManager::MailboxToTextureMap::const_iterator texture_it = |
| 177 manager->mailbox_to_textures_.begin(); |
| 178 texture_it != manager->mailbox_to_textures_.end(); |
| 179 texture_it++) { |
| 180 Texture* texture = texture_it->second->first; |
| 181 TextureMap::iterator it = textures_.find(texture); |
| 182 if (it != textures_.end()) { |
| 183 TextureDefinition& definition = it->second.group->definition; |
| 184 if (!definition.IsNewerThan(it->second.version)) |
| 185 continue; |
| 186 it->second.version = definition.version(); |
| 187 definition.UpdateTexture(texture); |
| 188 } |
| 189 } |
| 190 } |
| 191 |
| 192 } // namespace gles2 |
| 193 } // namespace gpu |
| OLD | NEW |