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
|
// Copyright (c) 2011 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/shader_manager.h"
#include "base/logging.h"
namespace gpu {
namespace gles2 {
ShaderManager::ShaderInfo::ShaderInfo(GLuint service_id, GLenum shader_type)
: use_count_(0),
service_id_(service_id),
shader_type_(shader_type),
valid_(false) {
}
ShaderManager::ShaderInfo::~ShaderInfo() {
}
void ShaderManager::ShaderInfo::IncUseCount() {
++use_count_;
}
void ShaderManager::ShaderInfo::DecUseCount() {
--use_count_;
DCHECK_GE(use_count_, 0);
}
void ShaderManager::ShaderInfo::MarkAsDeleted() {
DCHECK_NE(service_id_, 0u);
service_id_ = 0;
}
void ShaderManager::ShaderInfo::SetStatus(
bool valid, const char* log, ShaderTranslatorInterface* translator) {
valid_ = valid;
log_info_.reset(log ? new std::string(log) : NULL);
if (translator && valid) {
attrib_map_ = translator->attrib_map();
uniform_map_ = translator->uniform_map();
} else {
attrib_map_.clear();
uniform_map_.clear();
}
}
const ShaderManager::ShaderInfo::VariableInfo*
ShaderManager::ShaderInfo::GetAttribInfo(
const std::string& name) const {
VariableMap::const_iterator it = attrib_map_.find(name);
return it != attrib_map_.end() ? &it->second : NULL;
}
const ShaderManager::ShaderInfo::VariableInfo*
ShaderManager::ShaderInfo::GetUniformInfo(
const std::string& name) const {
VariableMap::const_iterator it = uniform_map_.find(name);
return it != uniform_map_.end() ? &it->second : NULL;
}
ShaderManager::ShaderManager() {}
ShaderManager::~ShaderManager() {
DCHECK(shader_infos_.empty());
}
void ShaderManager::Destroy(bool have_context) {
while (!shader_infos_.empty()) {
if (have_context) {
ShaderInfo* info = shader_infos_.begin()->second;
if (!info->IsDeleted()) {
glDeleteShader(info->service_id());
info->MarkAsDeleted();
}
}
shader_infos_.erase(shader_infos_.begin());
}
}
ShaderManager::ShaderInfo* ShaderManager::CreateShaderInfo(
GLuint client_id,
GLuint service_id,
GLenum shader_type) {
std::pair<ShaderInfoMap::iterator, bool> result =
shader_infos_.insert(std::make_pair(
client_id, ShaderInfo::Ref(new ShaderInfo(service_id, shader_type))));
DCHECK(result.second);
return result.first->second;
}
ShaderManager::ShaderInfo* ShaderManager::GetShaderInfo(GLuint client_id) {
ShaderInfoMap::iterator it = shader_infos_.find(client_id);
return it != shader_infos_.end() ? it->second : NULL;
}
bool ShaderManager::GetClientId(GLuint service_id, GLuint* client_id) const {
// This doesn't need to be fast. It's only used during slow queries.
for (ShaderInfoMap::const_iterator it = shader_infos_.begin();
it != shader_infos_.end(); ++it) {
if (it->second->service_id() == service_id) {
*client_id = it->first;
return true;
}
}
return false;
}
bool ShaderManager::IsOwned(ShaderManager::ShaderInfo* info) {
for (ShaderInfoMap::iterator it = shader_infos_.begin();
it != shader_infos_.end(); ++it) {
if (it->second.get() == info) {
return true;
}
}
return false;
}
void ShaderManager::RemoveShaderInfoIfUnused(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
if (info->IsDeleted() && !info->InUse()) {
for (ShaderInfoMap::iterator it = shader_infos_.begin();
it != shader_infos_.end(); ++it) {
if (it->second.get() == info) {
shader_infos_.erase(it);
return;
}
}
NOTREACHED();
}
}
void ShaderManager::MarkAsDeleted(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
info->MarkAsDeleted();
RemoveShaderInfoIfUnused(info);
}
void ShaderManager::UseShader(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
info->IncUseCount();
}
void ShaderManager::UnuseShader(ShaderManager::ShaderInfo* info) {
DCHECK(info);
DCHECK(IsOwned(info));
info->DecUseCount();
RemoveShaderInfoIfUnused(info);
}
} // namespace gles2
} // namespace gpu
|