Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / mailbox_manager.cc
1 // Copyright (c) 2012 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_manager.h"
6
7 #include <algorithm>
8
9 #include "crypto/random.h"
10 #include "gpu/command_buffer/service/texture_manager.h"
11
12 namespace gpu {
13 namespace gles2 {
14
15 MailboxManager::MailboxManager()
16     : mailbox_to_textures_(std::ptr_fun(&MailboxManager::TargetNameLess)) {
17 }
18
19 MailboxManager::~MailboxManager() {
20   DCHECK(mailbox_to_textures_.empty());
21   DCHECK(textures_to_mailboxes_.empty());
22 }
23
24 Texture* MailboxManager::ConsumeTexture(unsigned target,
25                                         const Mailbox& mailbox) {
26   MailboxToTextureMap::iterator it =
27       mailbox_to_textures_.find(TargetName(target, mailbox));
28   if (it == mailbox_to_textures_.end())
29     return NULL;
30
31   return it->second->first;
32 }
33
34 void MailboxManager::ProduceTexture(unsigned target,
35                                     const Mailbox& mailbox,
36                                     Texture* texture) {
37   texture->SetMailboxManager(this);
38   TargetName target_name(target, mailbox);
39   MailboxToTextureMap::iterator it = mailbox_to_textures_.find(target_name);
40   if (it != mailbox_to_textures_.end()) {
41     TextureToMailboxMap::iterator texture_it = it->second;
42     mailbox_to_textures_.erase(it);
43     textures_to_mailboxes_.erase(texture_it);
44   }
45   TextureToMailboxMap::iterator texture_it =
46       textures_to_mailboxes_.insert(std::make_pair(texture, target_name));
47   mailbox_to_textures_.insert(std::make_pair(target_name, texture_it));
48   DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());
49 }
50
51 void MailboxManager::TextureDeleted(Texture* texture) {
52   std::pair<TextureToMailboxMap::iterator,
53             TextureToMailboxMap::iterator> range =
54       textures_to_mailboxes_.equal_range(texture);
55   for (TextureToMailboxMap::iterator it = range.first;
56        it != range.second; ++it) {
57     size_t count = mailbox_to_textures_.erase(it->second);
58     DCHECK(count == 1);
59   }
60   textures_to_mailboxes_.erase(range.first, range.second);
61   DCHECK_EQ(mailbox_to_textures_.size(), textures_to_mailboxes_.size());
62 }
63
64 MailboxManager::TargetName::TargetName(unsigned target, const Mailbox& mailbox)
65     : target(target),
66       mailbox(mailbox) {
67 }
68
69 bool MailboxManager::TargetNameLess(const MailboxManager::TargetName& lhs,
70                                     const MailboxManager::TargetName& rhs) {
71   return memcmp(&lhs, &rhs, sizeof(lhs)) < 0;
72 }
73
74 }  // namespace gles2
75 }  // namespace gpu