summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/valuebuffer_manager.cc
blob: 96a50aa0db288dfe26d41cb5afc482126110fc01 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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/gl_utils.h"
#include "gpu/command_buffer/service/program_manager.h"
#include "gpu/command_buffer/service/valuebuffer_manager.h"

namespace gpu {
namespace gles2 {

SubscriptionRefSet::Observer::~Observer() {
}

SubscriptionRefSet::SubscriptionRefSet() {
}

SubscriptionRefSet::~SubscriptionRefSet() {
  // Make sure no valuebuffers are still holding references to targets
  DCHECK(reference_set_.empty());
}

void SubscriptionRefSet::AddSubscription(unsigned int target) {
  RefSet::iterator it = reference_set_.find(target);
  if (it == reference_set_.end()) {
    reference_set_.insert(std::make_pair(target, 1));
    FOR_EACH_OBSERVER(Observer, observers_, OnAddSubscription(target));
  } else {
    ++it->second;
  }
}

void SubscriptionRefSet::RemoveSubscription(unsigned int target) {
  RefSet::iterator it = reference_set_.find(target);
  DCHECK(it != reference_set_.end());
  if (it->second == 1) {
    reference_set_.erase(it);
    FOR_EACH_OBSERVER(Observer, observers_, OnRemoveSubscription(target));
  } else {
    --it->second;
  }
}

void SubscriptionRefSet::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void SubscriptionRefSet::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

Valuebuffer::Valuebuffer(ValuebufferManager* manager, unsigned int client_id)
    : manager_(manager), client_id_(client_id), has_been_bound_(false) {
  manager_->StartTracking(this);
  active_state_map_ = new ValueStateMap();
}

Valuebuffer::~Valuebuffer() {
  if (manager_) {
    for (SubscriptionSet::const_iterator it = subscriptions_.begin();
        it != subscriptions_.end(); ++it) {
      manager_->NotifyRemoveSubscription(*it);
    }
    manager_->StopTracking(this);
    manager_ = NULL;
  }
}

void Valuebuffer::AddSubscription(unsigned int subscription) {
  if (subscriptions_.find(subscription) == subscriptions_.end()) {
    subscriptions_.insert(subscription);
    manager_->NotifyAddSubscription(subscription);
  }
}

void Valuebuffer::RemoveSubscription(unsigned int subscription) {
  SubscriptionSet::iterator it = subscriptions_.find(subscription);
  if (subscriptions_.find(subscription) != subscriptions_.end()) {
    subscriptions_.erase(it);
    manager_->NotifyRemoveSubscription(subscription);
  }
}

bool Valuebuffer::IsSubscribed(unsigned int subscription) {
  return subscriptions_.find(subscription) != subscriptions_.end();
}

const ValueState* Valuebuffer::GetState(unsigned int target) const {
  return active_state_map_->GetState(target);
}

void Valuebuffer::UpdateState(const ValueStateMap* pending_state) {
  DCHECK(pending_state);
  for (SubscriptionSet::const_iterator it = subscriptions_.begin();
       it != subscriptions_.end(); ++it) {
    const ValueState *state = pending_state->GetState(*it);
    if (state != NULL) {
      active_state_map_->UpdateState(*it, *state);
    }
  }
}

ValuebufferManager::ValuebufferManager(SubscriptionRefSet* ref_set,
                                       ValueStateMap* state_map)
    : valuebuffer_count_(0),
      pending_state_map_(state_map),
      subscription_ref_set_(ref_set) {
}

ValuebufferManager::~ValuebufferManager() {
  DCHECK(valuebuffer_map_.empty());
  // If this triggers, that means something is keeping a reference to
  // a Valuebuffer belonging to this.
  CHECK_EQ(valuebuffer_count_, 0u);
}

void ValuebufferManager::Destroy() {
  valuebuffer_map_.clear();
}

void ValuebufferManager::StartTracking(Valuebuffer* /* valuebuffer */) {
  ++valuebuffer_count_;
}

void ValuebufferManager::StopTracking(Valuebuffer* /* valuebuffer */) {
  --valuebuffer_count_;
}

void ValuebufferManager::NotifyAddSubscription(unsigned int target) {
  subscription_ref_set_->AddSubscription(target);
}
void ValuebufferManager::NotifyRemoveSubscription(unsigned int target) {
  subscription_ref_set_->RemoveSubscription(target);
}

void ValuebufferManager::CreateValuebuffer(unsigned int client_id) {
  scoped_refptr<Valuebuffer> valuebuffer(new Valuebuffer(this, client_id));
  std::pair<ValuebufferMap::iterator, bool> result =
      valuebuffer_map_.insert(std::make_pair(client_id, valuebuffer));
  DCHECK(result.second);
}

Valuebuffer* ValuebufferManager::GetValuebuffer(unsigned int client_id) {
  ValuebufferMap::iterator it = valuebuffer_map_.find(client_id);
  return it != valuebuffer_map_.end() ? it->second.get() : NULL;
}

void ValuebufferManager::RemoveValuebuffer(unsigned int client_id) {
  ValuebufferMap::iterator it = valuebuffer_map_.find(client_id);
  if (it != valuebuffer_map_.end()) {
    Valuebuffer* valuebuffer = it->second.get();
    valuebuffer->MarkAsDeleted();
    valuebuffer_map_.erase(it);
  }
}

void ValuebufferManager::UpdateValuebufferState(Valuebuffer* valuebuffer) {
  DCHECK(valuebuffer);
  valuebuffer->UpdateState(pending_state_map_.get());
}

uint32 ValuebufferManager::ApiTypeForSubscriptionTarget(unsigned int target) {
  switch (target) {
    case GL_MOUSE_POSITION_CHROMIUM:
      return Program::kUniform2i;
  }
  NOTREACHED() << "Unhandled uniform subscription target " << target;
  return Program::kUniformNone;
}

}  // namespace gles2
}  // namespace gpu