Index: gpu/command_buffer/service/mailbox_synchronizer.cc |
diff --git a/gpu/command_buffer/service/mailbox_synchronizer.cc b/gpu/command_buffer/service/mailbox_synchronizer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3858a808c035952c700dc8db320c70cb768d5e0d |
--- /dev/null |
+++ b/gpu/command_buffer/service/mailbox_synchronizer.cc |
@@ -0,0 +1,134 @@ |
+// Copyright (c) 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. |
+ |
+#include "gpu/command_buffer/service/mailbox_synchronizer.h" |
+ |
+#include "base/bind.h" |
+#include "gpu/command_buffer/service/mailbox_manager.h" |
+#include "gpu/command_buffer/service/texture_manager.h" |
+ |
+namespace gpu { |
+namespace gles2 { |
+ |
+namespace { |
+ |
+MailboxSynchronizer* g_instance = NULL; |
+ |
+} // anonymous namespace |
+ |
+// static |
+bool MailboxSynchronizer::Initialize() { |
+ DCHECK(!g_instance); |
+ g_instance = new MailboxSynchronizer; |
+ return true; |
+} |
+ |
+// static |
+void MailboxSynchronizer::Terminate() { |
+ DCHECK(g_instance); |
+ delete g_instance; |
+ g_instance = NULL; |
+} |
+ |
+// static |
+MailboxSynchronizer* MailboxSynchronizer::GetInstance() { |
+ return g_instance; |
+} |
+ |
+MailboxSynchronizer::TargetName::TargetName(unsigned target, |
+ const Mailbox& mailbox) |
+ : target(target), mailbox(mailbox) {} |
+ |
+MailboxSynchronizer::MailboxSynchronizer() {} |
+ |
+MailboxSynchronizer::~MailboxSynchronizer() { |
+} |
+ |
+Texture* MailboxSynchronizer::CreateTextureFromMailbox(MailboxManager* manager, |
+ unsigned target, |
+ const Mailbox& mailbox) { |
+ base::AutoLock lock(lock_); |
+ TargetName target_name(target, mailbox); |
+ MailboxMap::const_iterator it = mailboxes_.find(target_name); |
+ if (it != mailboxes_.end()) { |
+ linked_ptr<GlobalMailbox> info = it->second; |
+ DCHECK(find(info->refs.begin(), info->refs.end(), manager) == |
+ info->refs.end()); |
+ Texture* texture = info->definition.CreateTexture(); |
+ if (texture) |
+ it->second->refs.insert(manager); |
+ return texture; |
+ } |
+ |
+ return NULL; |
+} |
+ |
+void MailboxSynchronizer::TextureDeleted(MailboxManager* manager, |
+ unsigned target, |
+ const Mailbox& mailbox) { |
+ base::AutoLock lock(lock_); |
+ TargetName target_name(target, mailbox); |
+ MailboxMap::iterator it = mailboxes_.find(target_name); |
+ if (it != mailboxes_.end()) { |
+ size_t count = it->second->refs.erase(manager); |
+ DCHECK_EQ(count, 1U); |
+ if (it->second->refs.empty()) |
+ mailboxes_.erase(it); |
+ } |
+} |
+ |
+void MailboxSynchronizer::PushTextureUpdates(MailboxManager* manager) { |
+ base::AutoLock lock(lock_); |
+ scoped_refptr<SharedGLFence> fence(new SharedGLFence); |
+ for (MailboxManager::MailboxToTextureMap::const_iterator texture_it = |
+ manager->mailbox_to_textures_.begin(); |
+ texture_it != manager->mailbox_to_textures_.end(); |
+ texture_it++) { |
+ TargetName target_name(texture_it->first.target, texture_it->first.mailbox); |
+ Texture* texture = texture_it->second->first; |
+ // TODO(sievers) |
+ bool needs_mips = texture->min_filter() != GL_NEAREST && |
+ texture->min_filter() != GL_LINEAR; |
+ if (target_name.target != GL_TEXTURE_2D || needs_mips) |
+ continue; |
+ |
+ MailboxMap::iterator it = mailboxes_.find(target_name); |
+ if (it != mailboxes_.end()) { |
+ // TODO: Different texture produced into an existing mailbox needs to |
+ // decouple it from updates to previously shared textures in other |
+ // contexts. |
+ bool texture_has_image = |
+ texture->GetLevelImage(texture->target(), 0) != NULL; |
+ it->second->definition = TextureDefinition( |
+ target_name.target, |
+ texture, |
+ texture_has_image ? it->second->definition.image() : NULL, |
+ fence); |
+ it->second->refs.insert(manager); |
+ } else { |
+ // TODO: If the texture is associated with another mailbox though, |
+ // we need to reuse the same EGLImage. |
+ mailboxes_[target_name] = make_linked_ptr(new GlobalMailbox( |
+ TextureDefinition(target_name.target, texture, NULL, fence))); |
+ mailboxes_[target_name]->refs.insert(manager); |
+ } |
+ } |
+} |
+ |
+void MailboxSynchronizer::PullTextureUpdates(MailboxManager* manager) { |
+ base::AutoLock lock(lock_); |
+ for (MailboxManager::MailboxToTextureMap::const_iterator texture_it = |
+ manager->mailbox_to_textures_.begin(); |
+ texture_it != manager->mailbox_to_textures_.end(); |
+ texture_it++) { |
+ TargetName target_name(texture_it->first.target, texture_it->first.mailbox); |
+ MailboxMap::iterator it = mailboxes_.find(target_name); |
+ if (it != mailboxes_.end()) { |
+ it->second->definition.UpdateTexture(texture_it->second->first); |
+ } |
+ } |
+} |
+ |
+} // namespace gles2 |
+} // namespace gpu |