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 void MailboxSynchronizer::ReassociateMailboxLocked( |
| 62 const TargetName& target_name, |
| 63 TextureGroup* group) { |
| 64 lock_.AssertAcquired(); |
| 65 for (TextureMap::iterator it = textures_.begin(); it != textures_.end(); |
| 66 it++) { |
| 67 std::set<TargetName>::iterator mb_it = |
| 68 it->second.group->mailboxes.find(target_name); |
| 69 if (it->second.group != group && |
| 70 mb_it != it->second.group->mailboxes.end()) { |
| 71 it->second.group->mailboxes.erase(mb_it); |
| 72 } |
| 73 } |
| 74 group->mailboxes.insert(target_name); |
| 75 } |
| 76 |
| 77 linked_ptr<MailboxSynchronizer::TextureGroup> |
| 78 MailboxSynchronizer::GetGroupForMailboxLocked(const TargetName& target_name) { |
| 79 lock_.AssertAcquired(); |
| 80 for (TextureMap::iterator it = textures_.begin(); it != textures_.end(); |
| 81 it++) { |
| 82 std::set<TargetName>::const_iterator mb_it = |
| 83 it->second.group->mailboxes.find(target_name); |
| 84 if (mb_it != it->second.group->mailboxes.end()) |
| 85 return it->second.group; |
| 86 } |
| 87 return make_linked_ptr<MailboxSynchronizer::TextureGroup>(NULL); |
| 88 } |
| 89 |
| 90 Texture* MailboxSynchronizer::CreateTextureFromMailbox(unsigned target, |
| 91 const Mailbox& mailbox) { |
| 92 base::AutoLock lock(lock_); |
| 93 TargetName target_name(target, mailbox); |
| 94 linked_ptr<TextureGroup> group = GetGroupForMailboxLocked(target_name); |
| 95 if (group.get()) { |
| 96 Texture* new_texture = group->definition.CreateTexture(); |
| 97 if (new_texture) |
| 98 textures_.insert(std::make_pair(new_texture, TextureVersion(group))); |
| 99 return new_texture; |
| 100 } |
| 101 |
| 102 return NULL; |
| 103 } |
| 104 |
| 105 void MailboxSynchronizer::TextureDeleted(Texture* texture) { |
| 106 base::AutoLock lock(lock_); |
| 107 TextureMap::iterator it = textures_.find(texture); |
| 108 if (it != textures_.end()) { |
| 109 // TODO: We could avoid the update if this was the last ref. |
| 110 UpdateTextureLocked(it->first, it->second); |
| 111 textures_.erase(it); |
| 112 } |
| 113 } |
| 114 |
| 115 void MailboxSynchronizer::PushTextureUpdates(MailboxManager* manager) { |
| 116 base::AutoLock lock(lock_); |
| 117 for (MailboxManager::MailboxToTextureMap::const_iterator texture_it = |
| 118 manager->mailbox_to_textures_.begin(); |
| 119 texture_it != manager->mailbox_to_textures_.end(); |
| 120 texture_it++) { |
| 121 TargetName target_name(texture_it->first.target, texture_it->first.mailbox); |
| 122 Texture* texture = texture_it->second->first; |
| 123 // TODO(sievers): crbug.com/352274 |
| 124 // Should probably only fail if it already *has* mipmaps, while allowing |
| 125 // incomplete textures here. Also reconsider how to fail otherwise. |
| 126 bool needs_mips = texture->min_filter() != GL_NEAREST && |
| 127 texture->min_filter() != GL_LINEAR; |
| 128 if (target_name.target != GL_TEXTURE_2D || needs_mips) |
| 129 continue; |
| 130 |
| 131 TextureMap::iterator it = textures_.find(texture); |
| 132 if (it != textures_.end()) { |
| 133 TextureVersion& texture_version = it->second; |
| 134 TextureGroup* group = texture_version.group.get(); |
| 135 std::set<TargetName>::const_iterator mb_it = |
| 136 group->mailboxes.find(target_name); |
| 137 if (mb_it == group->mailboxes.end()) { |
| 138 // We previously did not associate this texture with the given mailbox. |
| 139 // Unlink other texture groups from the mailbox. |
| 140 ReassociateMailboxLocked(target_name, group); |
| 141 } |
| 142 UpdateTextureLocked(texture, texture_version); |
| 143 |
| 144 } else { |
| 145 linked_ptr<TextureGroup> group = make_linked_ptr(new TextureGroup( |
| 146 TextureDefinition(target_name.target, texture, 1, NULL))); |
| 147 |
| 148 // Unlink other textures from this mailbox in case the name is not new. |
| 149 ReassociateMailboxLocked(target_name, group.get()); |
| 150 textures_.insert(std::make_pair(texture, TextureVersion(group))); |
| 151 } |
| 152 } |
| 153 // Make sure all write fences are flushed. |
| 154 glFlush(); |
| 155 } |
| 156 |
| 157 void MailboxSynchronizer::UpdateTextureLocked(Texture* texture, |
| 158 TextureVersion& texture_version) { |
| 159 lock_.AssertAcquired(); |
| 160 gfx::GLImage* gl_image = texture->GetLevelImage(texture->target(), 0); |
| 161 TextureGroup* group = texture_version.group.get(); |
| 162 scoped_refptr<NativeImageBuffer> image_buffer = group->definition.image(); |
| 163 |
| 164 // Make sure we don't clobber with an older version |
| 165 if (!group->definition.IsOlderThan(texture_version.version)) |
| 166 return; |
| 167 |
| 168 // Also don't push redundant updates. Note that it would break the |
| 169 // versioning. |
| 170 if (group->definition.Matches(texture)) |
| 171 return; |
| 172 |
| 173 if (gl_image && !image_buffer->IsClient(gl_image)) { |
| 174 LOG(ERROR) << "MailboxSync: Incompatible attachment"; |
| 175 return; |
| 176 } |
| 177 |
| 178 group->definition = TextureDefinition(texture->target(), |
| 179 texture, |
| 180 ++texture_version.version, |
| 181 gl_image ? image_buffer : NULL); |
| 182 } |
| 183 |
| 184 void MailboxSynchronizer::PullTextureUpdates(MailboxManager* manager) { |
| 185 base::AutoLock lock(lock_); |
| 186 for (MailboxManager::MailboxToTextureMap::const_iterator texture_it = |
| 187 manager->mailbox_to_textures_.begin(); |
| 188 texture_it != manager->mailbox_to_textures_.end(); |
| 189 texture_it++) { |
| 190 Texture* texture = texture_it->second->first; |
| 191 TextureMap::iterator it = textures_.find(texture); |
| 192 if (it != textures_.end()) { |
| 193 TextureDefinition& definition = it->second.group->definition; |
| 194 if (it->second.version == definition.version() || |
| 195 definition.IsOlderThan(it->second.version)) |
| 196 continue; |
| 197 it->second.version = definition.version(); |
| 198 definition.UpdateTexture(texture); |
| 199 } |
| 200 } |
| 201 } |
| 202 |
| 203 } // namespace gles2 |
| 204 } // namespace gpu |
OLD | NEW |