4dd4bc4d68f4076c8371127fac2f5a45e317f6e8
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / program_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/program_manager.h"
6
7 #include <algorithm>
8 #include <set>
9 #include <utility>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/command_line.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/metrics/histogram.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/time/time.h"
19 #include "gpu/command_buffer/common/gles2_cmd_format.h"
20 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
21 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
22 #include "gpu/command_buffer/service/gpu_switches.h"
23 #include "gpu/command_buffer/service/program_cache.h"
24 #include "gpu/command_buffer/service/shader_manager.h"
25 #include "gpu/command_buffer/service/shader_translator.h"
26 #include "third_party/re2/re2/re2.h"
27
28 using base::TimeDelta;
29 using base::TimeTicks;
30
31 namespace gpu {
32 namespace gles2 {
33
34 namespace {
35
36 struct UniformType {
37   explicit UniformType(const ShaderTranslator::VariableInfo uniform)
38       : type(uniform.type),
39         size(uniform.size),
40         precision(uniform.precision) { }
41
42   UniformType()
43       : type(0),
44         size(0),
45         precision(SH_PRECISION_MEDIUMP) { }
46
47   bool operator==(const UniformType& other) const {
48     return type == other.type &&
49         size == other.size &&
50         precision == other.precision;
51   }
52
53   int type;
54   int size;
55   int precision;
56 };
57
58 int ShaderTypeToIndex(GLenum shader_type) {
59   switch (shader_type) {
60     case GL_VERTEX_SHADER:
61       return 0;
62     case GL_FRAGMENT_SHADER:
63       return 1;
64     default:
65       NOTREACHED();
66       return 0;
67   }
68 }
69
70 // Given a name like "foo.bar[123].moo[456]" sets new_name to "foo.bar[123].moo"
71 // and sets element_index to 456. returns false if element expression was not a
72 // whole decimal number. For example: "foo[1b2]"
73 bool GetUniformNameSansElement(
74     const std::string& name, int* element_index, std::string* new_name) {
75   DCHECK(element_index);
76   DCHECK(new_name);
77   if (name.size() < 3 || name[name.size() - 1] != ']') {
78     *element_index = 0;
79     *new_name = name;
80     return true;
81   }
82
83   // Look for an array specification.
84   size_t open_pos = name.find_last_of('[');
85   if (open_pos == std::string::npos ||
86       open_pos >= name.size() - 2) {
87     return false;
88   }
89
90   GLint index = 0;
91   size_t last = name.size() - 1;
92   for (size_t pos = open_pos + 1; pos < last; ++pos) {
93     int8 digit = name[pos] - '0';
94     if (digit < 0 || digit > 9) {
95       return false;
96     }
97     index = index * 10 + digit;
98   }
99
100   *element_index = index;
101   *new_name = name.substr(0, open_pos);
102   return true;
103 }
104
105 bool IsBuiltInVarying(const std::string& name) {
106   // Built-in variables.
107   const char* kBuiltInVaryings[] = {
108       "gl_FragCoord",
109       "gl_FrontFacing",
110       "gl_PointCoord"
111   };
112   for (size_t ii = 0; ii < arraysize(kBuiltInVaryings); ++ii) {
113     if (name == kBuiltInVaryings[ii])
114       return true;
115   }
116   return false;
117 }
118
119 }  // anonymous namespace.
120
121 Program::UniformInfo::UniformInfo()
122     : size(0),
123       type(GL_NONE),
124       fake_location_base(0),
125       is_array(false) {
126 }
127
128 Program::UniformInfo::UniformInfo(GLsizei _size,
129                                   GLenum _type,
130                                   int _fake_location_base,
131                                   const std::string& _name)
132     : size(_size),
133       type(_type),
134       accepts_api_type(0),
135       fake_location_base(_fake_location_base),
136       is_array(false),
137       name(_name) {
138   switch (type) {
139     case GL_INT:
140       accepts_api_type = kUniform1i;
141       break;
142     case GL_INT_VEC2:
143       accepts_api_type = kUniform2i;
144       break;
145     case GL_INT_VEC3:
146       accepts_api_type = kUniform3i;
147       break;
148     case GL_INT_VEC4:
149       accepts_api_type = kUniform4i;
150       break;
151
152     case GL_BOOL:
153       accepts_api_type = kUniform1i | kUniform1f;
154       break;
155     case GL_BOOL_VEC2:
156       accepts_api_type = kUniform2i | kUniform2f;
157       break;
158     case GL_BOOL_VEC3:
159       accepts_api_type = kUniform3i | kUniform3f;
160       break;
161     case GL_BOOL_VEC4:
162       accepts_api_type = kUniform4i | kUniform4f;
163       break;
164
165     case GL_FLOAT:
166       accepts_api_type = kUniform1f;
167       break;
168     case GL_FLOAT_VEC2:
169       accepts_api_type = kUniform2f;
170       break;
171     case GL_FLOAT_VEC3:
172       accepts_api_type = kUniform3f;
173       break;
174     case GL_FLOAT_VEC4:
175       accepts_api_type = kUniform4f;
176       break;
177
178     case GL_FLOAT_MAT2:
179       accepts_api_type = kUniformMatrix2f;
180       break;
181     case GL_FLOAT_MAT3:
182       accepts_api_type = kUniformMatrix3f;
183       break;
184     case GL_FLOAT_MAT4:
185       accepts_api_type = kUniformMatrix4f;
186       break;
187
188     case GL_SAMPLER_2D:
189     case GL_SAMPLER_2D_RECT_ARB:
190     case GL_SAMPLER_CUBE:
191     case GL_SAMPLER_3D_OES:
192     case GL_SAMPLER_EXTERNAL_OES:
193       accepts_api_type = kUniform1i;
194       break;
195     default:
196       NOTREACHED() << "Unhandled UniformInfo type " << type;
197       break;
198   }
199 }
200
201 Program::UniformInfo::~UniformInfo() {}
202
203 bool ProgramManager::IsInvalidPrefix(const char* name, size_t length) {
204   static const char kInvalidPrefix[] = { 'g', 'l', '_' };
205   return (length >= sizeof(kInvalidPrefix) &&
206       memcmp(name, kInvalidPrefix, sizeof(kInvalidPrefix)) == 0);
207 }
208
209 Program::Program(
210     ProgramManager* manager, GLuint service_id)
211     : manager_(manager),
212       use_count_(0),
213       max_attrib_name_length_(0),
214       max_uniform_name_length_(0),
215       service_id_(service_id),
216       deleted_(false),
217       valid_(false),
218       link_status_(false),
219       uniforms_cleared_(false),
220       num_uniforms_(0) {
221   manager_->StartTracking(this);
222 }
223
224 void Program::Reset() {
225   valid_ = false;
226   link_status_ = false;
227   num_uniforms_ = 0;
228   max_uniform_name_length_ = 0;
229   max_attrib_name_length_ = 0;
230   attrib_infos_.clear();
231   uniform_infos_.clear();
232   sampler_indices_.clear();
233   attrib_location_to_index_map_.clear();
234 }
235
236 std::string Program::ProcessLogInfo(
237     const std::string& log) {
238   std::string output;
239   re2::StringPiece input(log);
240   std::string prior_log;
241   std::string hashed_name;
242   while (RE2::Consume(&input,
243                       "(.*?)(webgl_[0123456789abcdefABCDEF]+)",
244                       &prior_log,
245                       &hashed_name)) {
246     output += prior_log;
247
248     const std::string* original_name =
249         GetOriginalNameFromHashedName(hashed_name);
250     if (original_name)
251       output += *original_name;
252     else
253       output += hashed_name;
254   }
255
256   return output + input.as_string();
257 }
258
259 void Program::UpdateLogInfo() {
260   GLint max_len = 0;
261   glGetProgramiv(service_id_, GL_INFO_LOG_LENGTH, &max_len);
262   if (max_len == 0) {
263     set_log_info(NULL);
264     return;
265   }
266   scoped_ptr<char[]> temp(new char[max_len]);
267   GLint len = 0;
268   glGetProgramInfoLog(service_id_, max_len, &len, temp.get());
269   DCHECK(max_len == 0 || len < max_len);
270   DCHECK(len == 0 || temp[len] == '\0');
271   std::string log(temp.get(), len);
272   set_log_info(ProcessLogInfo(log).c_str());
273 }
274
275 void Program::ClearUniforms(
276     std::vector<uint8>* zero_buffer) {
277   DCHECK(zero_buffer);
278   if (uniforms_cleared_) {
279     return;
280   }
281   uniforms_cleared_ = true;
282   for (size_t ii = 0; ii < uniform_infos_.size(); ++ii) {
283     const UniformInfo& uniform_info = uniform_infos_[ii];
284     if (!uniform_info.IsValid()) {
285       continue;
286     }
287     GLint location = uniform_info.element_locations[0];
288     GLsizei size = uniform_info.size;
289     uint32 unit_size =  GLES2Util::GetGLDataTypeSizeForUniforms(
290         uniform_info.type);
291     uint32 size_needed = size * unit_size;
292     if (size_needed > zero_buffer->size()) {
293       zero_buffer->resize(size_needed, 0u);
294     }
295     const void* zero = &(*zero_buffer)[0];
296     switch (uniform_info.type) {
297     case GL_FLOAT:
298       glUniform1fv(location, size, reinterpret_cast<const GLfloat*>(zero));
299       break;
300     case GL_FLOAT_VEC2:
301       glUniform2fv(location, size, reinterpret_cast<const GLfloat*>(zero));
302       break;
303     case GL_FLOAT_VEC3:
304       glUniform3fv(location, size, reinterpret_cast<const GLfloat*>(zero));
305       break;
306     case GL_FLOAT_VEC4:
307       glUniform4fv(location, size, reinterpret_cast<const GLfloat*>(zero));
308       break;
309     case GL_INT:
310     case GL_BOOL:
311     case GL_SAMPLER_2D:
312     case GL_SAMPLER_CUBE:
313     case GL_SAMPLER_EXTERNAL_OES:
314     case GL_SAMPLER_3D_OES:
315     case GL_SAMPLER_2D_RECT_ARB:
316       glUniform1iv(location, size, reinterpret_cast<const GLint*>(zero));
317       break;
318     case GL_INT_VEC2:
319     case GL_BOOL_VEC2:
320       glUniform2iv(location, size, reinterpret_cast<const GLint*>(zero));
321       break;
322     case GL_INT_VEC3:
323     case GL_BOOL_VEC3:
324       glUniform3iv(location, size, reinterpret_cast<const GLint*>(zero));
325       break;
326     case GL_INT_VEC4:
327     case GL_BOOL_VEC4:
328       glUniform4iv(location, size, reinterpret_cast<const GLint*>(zero));
329       break;
330     case GL_FLOAT_MAT2:
331       glUniformMatrix2fv(
332           location, size, false, reinterpret_cast<const GLfloat*>(zero));
333       break;
334     case GL_FLOAT_MAT3:
335       glUniformMatrix3fv(
336           location, size, false, reinterpret_cast<const GLfloat*>(zero));
337       break;
338     case GL_FLOAT_MAT4:
339       glUniformMatrix4fv(
340           location, size, false, reinterpret_cast<const GLfloat*>(zero));
341       break;
342     default:
343       NOTREACHED();
344       break;
345     }
346   }
347 }
348
349 namespace {
350
351 struct UniformData {
352   UniformData() : size(-1), type(GL_NONE), location(0), added(false) {
353   }
354   std::string queried_name;
355   std::string corrected_name;
356   std::string original_name;
357   GLsizei size;
358   GLenum type;
359   GLint location;
360   bool added;
361 };
362
363 struct UniformDataComparer {
364   bool operator()(const UniformData& lhs, const UniformData& rhs) const {
365     return lhs.queried_name < rhs.queried_name;
366   }
367 };
368
369 }  // anonymous namespace
370
371 void Program::Update() {
372   Reset();
373   UpdateLogInfo();
374   link_status_ = true;
375   uniforms_cleared_ = false;
376   GLint num_attribs = 0;
377   GLint max_len = 0;
378   GLint max_location = -1;
379   glGetProgramiv(service_id_, GL_ACTIVE_ATTRIBUTES, &num_attribs);
380   glGetProgramiv(service_id_, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max_len);
381   // TODO(gman): Should we check for error?
382   scoped_ptr<char[]> name_buffer(new char[max_len]);
383   for (GLint ii = 0; ii < num_attribs; ++ii) {
384     GLsizei length = 0;
385     GLsizei size = 0;
386     GLenum type = 0;
387     glGetActiveAttrib(
388         service_id_, ii, max_len, &length, &size, &type, name_buffer.get());
389     DCHECK(max_len == 0 || length < max_len);
390     DCHECK(length == 0 || name_buffer[length] == '\0');
391     if (!ProgramManager::IsInvalidPrefix(name_buffer.get(), length)) {
392       std::string name;
393       std::string original_name;
394       GetCorrectedVariableInfo(
395           false, name_buffer.get(), &name, &original_name, &size, &type);
396       // TODO(gman): Should we check for error?
397       GLint location = glGetAttribLocation(service_id_, name_buffer.get());
398       if (location > max_location) {
399         max_location = location;
400       }
401       attrib_infos_.push_back(
402           VertexAttrib(size, type, original_name, location));
403       max_attrib_name_length_ = std::max(
404           max_attrib_name_length_, static_cast<GLsizei>(original_name.size()));
405     }
406   }
407
408   // Create attrib location to index map.
409   attrib_location_to_index_map_.resize(max_location + 1);
410   for (GLint ii = 0; ii <= max_location; ++ii) {
411     attrib_location_to_index_map_[ii] = -1;
412   }
413   for (size_t ii = 0; ii < attrib_infos_.size(); ++ii) {
414     const VertexAttrib& info = attrib_infos_[ii];
415     attrib_location_to_index_map_[info.location] = ii;
416   }
417
418 #if !defined(NDEBUG)
419   if (CommandLine::ForCurrentProcess()->HasSwitch(
420       switches::kEnableGPUServiceLoggingGPU)) {
421     DVLOG(1) << "----: attribs for service_id: " << service_id();
422     for (size_t ii = 0; ii < attrib_infos_.size(); ++ii) {
423       const VertexAttrib& info = attrib_infos_[ii];
424       DVLOG(1) << ii << ": loc = " << info.location
425                << ", size = " << info.size
426                << ", type = " << GLES2Util::GetStringEnum(info.type)
427                << ", name = " << info.name;
428     }
429   }
430 #endif
431
432   max_len = 0;
433   GLint num_uniforms = 0;
434   glGetProgramiv(service_id_, GL_ACTIVE_UNIFORMS, &num_uniforms);
435   glGetProgramiv(service_id_, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_len);
436   name_buffer.reset(new char[max_len]);
437
438   // Reads all the names.
439   std::vector<UniformData> uniform_data;
440   for (GLint ii = 0; ii < num_uniforms; ++ii) {
441     GLsizei length = 0;
442     UniformData data;
443     glGetActiveUniform(
444         service_id_, ii, max_len, &length,
445         &data.size, &data.type, name_buffer.get());
446     DCHECK(max_len == 0 || length < max_len);
447     DCHECK(length == 0 || name_buffer[length] == '\0');
448     if (!ProgramManager::IsInvalidPrefix(name_buffer.get(), length)) {
449       data.queried_name = std::string(name_buffer.get());
450       GetCorrectedVariableInfo(
451           true, name_buffer.get(), &data.corrected_name, &data.original_name,
452           &data.size, &data.type);
453       uniform_data.push_back(data);
454     }
455   }
456
457   // NOTE: We don't care if 2 uniforms are bound to the same location.
458   // One of them will take preference. The spec allows this, same as
459   // BindAttribLocation.
460   //
461   // The reason we don't check is if we were to fail we'd have to
462   // restore the previous program but since we've already linked successfully
463   // at this point the previous program is gone.
464
465   // Assigns the uniforms with bindings.
466   size_t next_available_index = 0;
467   for (size_t ii = 0; ii < uniform_data.size(); ++ii) {
468     UniformData& data = uniform_data[ii];
469     data.location = glGetUniformLocation(
470         service_id_, data.queried_name.c_str());
471     // remove "[0]"
472     std::string short_name;
473     int element_index = 0;
474     bool good ALLOW_UNUSED = GetUniformNameSansElement(
475         data.queried_name, &element_index, &short_name);\
476     DCHECK(good);
477     LocationMap::const_iterator it = bind_uniform_location_map_.find(
478         short_name);
479     if (it != bind_uniform_location_map_.end()) {
480       data.added = AddUniformInfo(
481           data.size, data.type, data.location, it->second, data.corrected_name,
482           data.original_name, &next_available_index);
483     }
484   }
485
486   // Assigns the uniforms that were not bound.
487   for (size_t ii = 0; ii < uniform_data.size(); ++ii) {
488     const UniformData& data = uniform_data[ii];
489     if (!data.added) {
490       AddUniformInfo(
491           data.size, data.type, data.location, -1, data.corrected_name,
492           data.original_name, &next_available_index);
493     }
494   }
495
496 #if !defined(NDEBUG)
497   if (CommandLine::ForCurrentProcess()->HasSwitch(
498       switches::kEnableGPUServiceLoggingGPU)) {
499     DVLOG(1) << "----: uniforms for service_id: " << service_id();
500     for (size_t ii = 0; ii < uniform_infos_.size(); ++ii) {
501       const UniformInfo& info = uniform_infos_[ii];
502       if (info.IsValid()) {
503         DVLOG(1) << ii << ": loc = " << info.element_locations[0]
504                  << ", size = " << info.size
505                  << ", type = " << GLES2Util::GetStringEnum(info.type)
506                  << ", name = " << info.name;
507       }
508     }
509   }
510 #endif
511
512   valid_ = true;
513 }
514
515 void Program::ExecuteBindAttribLocationCalls() {
516   for (LocationMap::const_iterator it = bind_attrib_location_map_.begin();
517        it != bind_attrib_location_map_.end(); ++it) {
518     const std::string* mapped_name = GetAttribMappedName(it->first);
519     if (mapped_name && *mapped_name != it->first)
520       glBindAttribLocation(service_id_, it->second, mapped_name->c_str());
521   }
522 }
523
524 bool Program::Link(ShaderManager* manager,
525                    ShaderTranslator* vertex_translator,
526                    ShaderTranslator* fragment_translator,
527                    Program::VaryingsPackingOption varyings_packing_option,
528                    const ShaderCacheCallback& shader_callback) {
529   ClearLinkStatus();
530   if (!CanLink()) {
531     set_log_info("missing shaders");
532     return false;
533   }
534   if (DetectAttribLocationBindingConflicts()) {
535     set_log_info("glBindAttribLocation() conflicts");
536     return false;
537   }
538   std::string conflicting_name;
539   if (DetectUniformsMismatch(&conflicting_name)) {
540     std::string info_log = "Uniforms with the same name but different "
541                            "type/precision: " + conflicting_name;
542     set_log_info(ProcessLogInfo(info_log).c_str());
543     return false;
544   }
545   if (DetectVaryingsMismatch(&conflicting_name)) {
546     std::string info_log = "Varyings with the same name but different type, "
547                            "or statically used varyings in fragment shader are "
548                            "not declared in vertex shader: " + conflicting_name;
549     set_log_info(ProcessLogInfo(info_log).c_str());
550     return false;
551   }
552   if (DetectGlobalNameConflicts(&conflicting_name)) {
553     std::string info_log = "Name conflicts between an uniform and an "
554                            "attribute: " + conflicting_name;
555     set_log_info(ProcessLogInfo(info_log).c_str());
556     return false;
557   }
558   if (!CheckVaryingsPacking(varyings_packing_option)) {
559     set_log_info("Varyings over maximum register limit");
560     return false;
561   }
562
563   TimeTicks before_time = TimeTicks::HighResNow();
564   bool link = true;
565   ProgramCache* cache = manager_->program_cache_;
566   if (cache) {
567     DCHECK(!attached_shaders_[0]->signature_source().empty() &&
568            !attached_shaders_[1]->signature_source().empty());
569     ProgramCache::LinkedProgramStatus status = cache->GetLinkedProgramStatus(
570         attached_shaders_[0]->signature_source(),
571         vertex_translator,
572         attached_shaders_[1]->signature_source(),
573         fragment_translator,
574         &bind_attrib_location_map_);
575
576     if (status == ProgramCache::LINK_SUCCEEDED) {
577       ProgramCache::ProgramLoadResult success =
578           cache->LoadLinkedProgram(service_id(),
579                                    attached_shaders_[0].get(),
580                                    vertex_translator,
581                                    attached_shaders_[1].get(),
582                                    fragment_translator,
583                                    &bind_attrib_location_map_,
584                                    shader_callback);
585       link = success != ProgramCache::PROGRAM_LOAD_SUCCESS;
586       UMA_HISTOGRAM_BOOLEAN("GPU.ProgramCache.LoadBinarySuccess", !link);
587     }
588   }
589
590   if (link) {
591     ExecuteBindAttribLocationCalls();
592     before_time = TimeTicks::HighResNow();
593     if (cache && gfx::g_driver_gl.ext.b_GL_ARB_get_program_binary) {
594       glProgramParameteri(service_id(),
595                           PROGRAM_BINARY_RETRIEVABLE_HINT,
596                           GL_TRUE);
597     }
598     glLinkProgram(service_id());
599   }
600
601   GLint success = 0;
602   glGetProgramiv(service_id(), GL_LINK_STATUS, &success);
603   if (success == GL_TRUE) {
604     Update();
605     if (link) {
606       if (cache) {
607         cache->SaveLinkedProgram(service_id(),
608                                  attached_shaders_[0].get(),
609                                  vertex_translator,
610                                  attached_shaders_[1].get(),
611                                  fragment_translator,
612                                  &bind_attrib_location_map_,
613                                  shader_callback);
614       }
615       UMA_HISTOGRAM_CUSTOM_COUNTS(
616           "GPU.ProgramCache.BinaryCacheMissTime",
617           (TimeTicks::HighResNow() - before_time).InMicroseconds(),
618           0,
619           TimeDelta::FromSeconds(10).InMicroseconds(),
620           50);
621     } else {
622       UMA_HISTOGRAM_CUSTOM_COUNTS(
623           "GPU.ProgramCache.BinaryCacheHitTime",
624           (TimeTicks::HighResNow() - before_time).InMicroseconds(),
625           0,
626           TimeDelta::FromSeconds(1).InMicroseconds(),
627           50);
628     }
629   } else {
630     UpdateLogInfo();
631   }
632   return success == GL_TRUE;
633 }
634
635 void Program::Validate() {
636   if (!IsValid()) {
637     set_log_info("program not linked");
638     return;
639   }
640   glValidateProgram(service_id());
641   UpdateLogInfo();
642 }
643
644 GLint Program::GetUniformFakeLocation(
645     const std::string& name) const {
646   bool getting_array_location = false;
647   size_t open_pos = std::string::npos;
648   int index = 0;
649   if (!GLES2Util::ParseUniformName(
650       name, &open_pos, &index, &getting_array_location)) {
651     return -1;
652   }
653   for (GLuint ii = 0; ii < uniform_infos_.size(); ++ii) {
654     const UniformInfo& info = uniform_infos_[ii];
655     if (!info.IsValid()) {
656       continue;
657     }
658     if (info.name == name ||
659         (info.is_array &&
660          info.name.compare(0, info.name.size() - 3, name) == 0)) {
661       return info.fake_location_base;
662     } else if (getting_array_location && info.is_array) {
663       // Look for an array specification.
664       size_t open_pos_2 = info.name.find_last_of('[');
665       if (open_pos_2 == open_pos &&
666           name.compare(0, open_pos, info.name, 0, open_pos) == 0) {
667         if (index >= 0 && index < info.size) {
668           DCHECK_GT(static_cast<int>(info.element_locations.size()), index);
669           if (info.element_locations[index] == -1)
670             return -1;
671           return ProgramManager::MakeFakeLocation(
672               info.fake_location_base, index);
673         }
674       }
675     }
676   }
677   return -1;
678 }
679
680 GLint Program::GetAttribLocation(
681     const std::string& name) const {
682   for (GLuint ii = 0; ii < attrib_infos_.size(); ++ii) {
683     const VertexAttrib& info = attrib_infos_[ii];
684     if (info.name == name) {
685       return info.location;
686     }
687   }
688   return -1;
689 }
690
691 const Program::UniformInfo*
692     Program::GetUniformInfoByFakeLocation(
693         GLint fake_location, GLint* real_location, GLint* array_index) const {
694   DCHECK(real_location);
695   DCHECK(array_index);
696   if (fake_location < 0) {
697     return NULL;
698   }
699
700   GLint uniform_index = GetUniformInfoIndexFromFakeLocation(fake_location);
701   if (uniform_index >= 0 &&
702       static_cast<size_t>(uniform_index) < uniform_infos_.size()) {
703     const UniformInfo& uniform_info = uniform_infos_[uniform_index];
704     if (!uniform_info.IsValid()) {
705       return NULL;
706     }
707     GLint element_index = GetArrayElementIndexFromFakeLocation(fake_location);
708     if (element_index < uniform_info.size) {
709       *real_location = uniform_info.element_locations[element_index];
710       *array_index = element_index;
711       return &uniform_info;
712     }
713   }
714   return NULL;
715 }
716
717 const std::string* Program::GetAttribMappedName(
718     const std::string& original_name) const {
719   for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
720     Shader* shader = attached_shaders_[ii].get();
721     if (shader) {
722       const std::string* mapped_name =
723           shader->GetAttribMappedName(original_name);
724       if (mapped_name)
725         return mapped_name;
726     }
727   }
728   return NULL;
729 }
730
731 const std::string* Program::GetOriginalNameFromHashedName(
732     const std::string& hashed_name) const {
733   for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
734     Shader* shader = attached_shaders_[ii].get();
735     if (shader) {
736       const std::string* original_name =
737           shader->GetOriginalNameFromHashedName(hashed_name);
738       if (original_name)
739         return original_name;
740     }
741   }
742   return NULL;
743 }
744
745 bool Program::SetUniformLocationBinding(
746     const std::string& name, GLint location) {
747   std::string short_name;
748   int element_index = 0;
749   if (!GetUniformNameSansElement(name, &element_index, &short_name) ||
750       element_index != 0) {
751     return false;
752   }
753
754   bind_uniform_location_map_[short_name] = location;
755   return true;
756 }
757
758 // Note: This is only valid to call right after a program has been linked
759 // successfully.
760 void Program::GetCorrectedVariableInfo(
761     bool use_uniforms,
762     const std::string& name, std::string* corrected_name,
763     std::string* original_name,
764     GLsizei* size, GLenum* type) const {
765   DCHECK(corrected_name);
766   DCHECK(original_name);
767   DCHECK(size);
768   DCHECK(type);
769   const char* kArraySpec = "[0]";
770   for (int jj = 0; jj < 2; ++jj) {
771     std::string test_name(name + ((jj == 1) ? kArraySpec : ""));
772     for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
773       Shader* shader = attached_shaders_[ii].get();
774       if (shader) {
775         const Shader::VariableInfo* variable_info =
776             use_uniforms ? shader->GetUniformInfo(test_name) :
777                            shader->GetAttribInfo(test_name);
778         // Note: There is an assuption here that if an attrib is defined in more
779         // than 1 attached shader their types and sizes match. Should we check
780         // for that case?
781         if (variable_info) {
782           *corrected_name = test_name;
783           *original_name = variable_info->name;
784           *type = variable_info->type;
785           *size = variable_info->size;
786           return;
787         }
788       }
789     }
790   }
791   *corrected_name = name;
792   *original_name = name;
793 }
794
795 bool Program::AddUniformInfo(
796         GLsizei size, GLenum type, GLint location, GLint fake_base_location,
797         const std::string& name, const std::string& original_name,
798         size_t* next_available_index) {
799   DCHECK(next_available_index);
800   const char* kArraySpec = "[0]";
801   size_t uniform_index =
802       fake_base_location >= 0 ? fake_base_location : *next_available_index;
803   if (uniform_infos_.size() < uniform_index + 1) {
804     uniform_infos_.resize(uniform_index + 1);
805   }
806
807   // return if this location is already in use.
808   if (uniform_infos_[uniform_index].IsValid()) {
809     DCHECK_GE(fake_base_location, 0);
810     return false;
811   }
812
813   uniform_infos_[uniform_index] = UniformInfo(
814       size, type, uniform_index, original_name);
815   ++num_uniforms_;
816
817   UniformInfo& info = uniform_infos_[uniform_index];
818   info.element_locations.resize(size);
819   info.element_locations[0] = location;
820   DCHECK_GE(size, 0);
821   size_t num_texture_units = info.IsSampler() ? static_cast<size_t>(size) : 0u;
822   info.texture_units.clear();
823   info.texture_units.resize(num_texture_units, 0);
824
825   if (size > 1) {
826     // Go through the array element locations looking for a match.
827     // We can skip the first element because it's the same as the
828     // the location without the array operators.
829     size_t array_pos = name.rfind(kArraySpec);
830     std::string base_name = name;
831     if (name.size() > 3) {
832       if (array_pos != name.size() - 3) {
833         info.name = name + kArraySpec;
834       } else {
835         base_name = name.substr(0, name.size() - 3);
836       }
837     }
838     for (GLsizei ii = 1; ii < info.size; ++ii) {
839       std::string element_name(base_name + "[" + base::IntToString(ii) + "]");
840       info.element_locations[ii] =
841           glGetUniformLocation(service_id_, element_name.c_str());
842     }
843   }
844
845   info.is_array =
846      (size > 1 ||
847       (info.name.size() > 3 &&
848        info.name.rfind(kArraySpec) == info.name.size() - 3));
849
850   if (info.IsSampler()) {
851     sampler_indices_.push_back(info.fake_location_base);
852   }
853   max_uniform_name_length_ =
854       std::max(max_uniform_name_length_,
855                static_cast<GLsizei>(info.name.size()));
856
857   while (*next_available_index < uniform_infos_.size() &&
858          uniform_infos_[*next_available_index].IsValid()) {
859     *next_available_index = *next_available_index + 1;
860   }
861
862   return true;
863 }
864
865 const Program::UniformInfo*
866     Program::GetUniformInfo(
867         GLint index) const {
868   if (static_cast<size_t>(index) >= uniform_infos_.size()) {
869     return NULL;
870   }
871
872   const UniformInfo& info = uniform_infos_[index];
873   return info.IsValid() ? &info : NULL;
874 }
875
876 bool Program::SetSamplers(
877     GLint num_texture_units, GLint fake_location,
878     GLsizei count, const GLint* value) {
879   if (fake_location < 0) {
880     return true;
881   }
882   GLint uniform_index = GetUniformInfoIndexFromFakeLocation(fake_location);
883   if (uniform_index >= 0 &&
884       static_cast<size_t>(uniform_index) < uniform_infos_.size()) {
885     UniformInfo& info = uniform_infos_[uniform_index];
886     if (!info.IsValid()) {
887       return false;
888     }
889     GLint element_index = GetArrayElementIndexFromFakeLocation(fake_location);
890     if (element_index < info.size) {
891       count = std::min(info.size - element_index, count);
892       if (info.IsSampler() && count > 0) {
893         for (GLsizei ii = 0; ii < count; ++ii) {
894           if (value[ii] < 0 || value[ii] >= num_texture_units) {
895             return false;
896           }
897         }
898         std::copy(value, value + count,
899                   info.texture_units.begin() + element_index);
900         return true;
901       }
902     }
903   }
904   return true;
905 }
906
907 void Program::GetProgramiv(GLenum pname, GLint* params) {
908   switch (pname) {
909     case GL_ACTIVE_ATTRIBUTES:
910       *params = attrib_infos_.size();
911       break;
912     case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
913       // Notice +1 to accomodate NULL terminator.
914       *params = max_attrib_name_length_ + 1;
915       break;
916     case GL_ACTIVE_UNIFORMS:
917       *params = num_uniforms_;
918       break;
919     case GL_ACTIVE_UNIFORM_MAX_LENGTH:
920       // Notice +1 to accomodate NULL terminator.
921       *params = max_uniform_name_length_ + 1;
922       break;
923     case GL_LINK_STATUS:
924       *params = link_status_;
925       break;
926     case GL_INFO_LOG_LENGTH:
927       // Notice +1 to accomodate NULL terminator.
928       *params = log_info_.get() ? (log_info_->size() + 1) : 0;
929       break;
930     case GL_DELETE_STATUS:
931       *params = deleted_;
932       break;
933     case GL_VALIDATE_STATUS:
934       if (!IsValid()) {
935         *params = GL_FALSE;
936       } else {
937         glGetProgramiv(service_id_, pname, params);
938       }
939       break;
940     default:
941       glGetProgramiv(service_id_, pname, params);
942       break;
943   }
944 }
945
946 bool Program::AttachShader(
947     ShaderManager* shader_manager,
948     Shader* shader) {
949   DCHECK(shader_manager);
950   DCHECK(shader);
951   int index = ShaderTypeToIndex(shader->shader_type());
952   if (attached_shaders_[index].get() != NULL) {
953     return false;
954   }
955   attached_shaders_[index] = scoped_refptr<Shader>(shader);
956   shader_manager->UseShader(shader);
957   return true;
958 }
959
960 bool Program::DetachShader(
961     ShaderManager* shader_manager,
962     Shader* shader) {
963   DCHECK(shader_manager);
964   DCHECK(shader);
965   if (attached_shaders_[ShaderTypeToIndex(shader->shader_type())].get() !=
966       shader) {
967     return false;
968   }
969   attached_shaders_[ShaderTypeToIndex(shader->shader_type())] = NULL;
970   shader_manager->UnuseShader(shader);
971   return true;
972 }
973
974 void Program::DetachShaders(ShaderManager* shader_manager) {
975   DCHECK(shader_manager);
976   for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
977     if (attached_shaders_[ii].get()) {
978       DetachShader(shader_manager, attached_shaders_[ii].get());
979     }
980   }
981 }
982
983 bool Program::CanLink() const {
984   for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
985     if (!attached_shaders_[ii].get() || !attached_shaders_[ii]->valid()) {
986       return false;
987     }
988   }
989   return true;
990 }
991
992 bool Program::DetectAttribLocationBindingConflicts() const {
993   std::set<GLint> location_binding_used;
994   for (LocationMap::const_iterator it = bind_attrib_location_map_.begin();
995        it != bind_attrib_location_map_.end(); ++it) {
996     // Find out if an attribute is declared in this program's shaders.
997     bool active = false;
998     for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
999       if (!attached_shaders_[ii].get() || !attached_shaders_[ii]->valid())
1000         continue;
1001       if (attached_shaders_[ii]->GetAttribInfo(it->first)) {
1002         active = true;
1003         break;
1004       }
1005     }
1006     if (active) {
1007       std::pair<std::set<GLint>::iterator, bool> result =
1008           location_binding_used.insert(it->second);
1009       if (!result.second)
1010         return true;
1011     }
1012   }
1013   return false;
1014 }
1015
1016 bool Program::DetectUniformsMismatch(std::string* conflicting_name) const {
1017   typedef std::map<std::string, UniformType> UniformMap;
1018   UniformMap uniform_map;
1019   for (int ii = 0; ii < kMaxAttachedShaders; ++ii) {
1020     const ShaderTranslator::VariableMap& shader_uniforms =
1021         attached_shaders_[ii]->uniform_map();
1022     for (ShaderTranslator::VariableMap::const_iterator iter =
1023              shader_uniforms.begin();
1024          iter != shader_uniforms.end(); ++iter) {
1025       const std::string& name = iter->first;
1026       UniformType type(iter->second);
1027       UniformMap::iterator map_entry = uniform_map.find(name);
1028       if (map_entry == uniform_map.end()) {
1029         uniform_map[name] = type;
1030       } else {
1031         // If a uniform is already in the map, i.e., it has already been
1032         // declared by other shader, then the type and precision must match.
1033         if (map_entry->second == type)
1034           continue;
1035         *conflicting_name = name;
1036         return true;
1037       }
1038     }
1039   }
1040   return false;
1041 }
1042
1043 bool Program::DetectVaryingsMismatch(std::string* conflicting_name) const {
1044   DCHECK(attached_shaders_[0].get() &&
1045          attached_shaders_[0]->shader_type() == GL_VERTEX_SHADER &&
1046          attached_shaders_[1].get() &&
1047          attached_shaders_[1]->shader_type() == GL_FRAGMENT_SHADER);
1048   const ShaderTranslator::VariableMap* vertex_varyings =
1049       &(attached_shaders_[0]->varying_map());
1050   const ShaderTranslator::VariableMap* fragment_varyings =
1051       &(attached_shaders_[1]->varying_map());
1052
1053   for (ShaderTranslator::VariableMap::const_iterator iter =
1054            fragment_varyings->begin();
1055        iter != fragment_varyings->end(); ++iter) {
1056     const std::string& name = iter->first;
1057     if (IsBuiltInVarying(name))
1058       continue;
1059
1060     ShaderTranslator::VariableMap::const_iterator hit =
1061         vertex_varyings->find(name);
1062     if (hit == vertex_varyings->end()) {
1063       if (iter->second.static_use) {
1064         *conflicting_name = name;
1065         return true;
1066       }
1067       continue;
1068     }
1069
1070     if (hit->second.type != iter->second.type ||
1071         hit->second.size != iter->second.size) {
1072       *conflicting_name = name;
1073       return true;
1074     }
1075
1076   }
1077   return false;
1078 }
1079
1080 bool Program::DetectGlobalNameConflicts(std::string* conflicting_name) const {
1081   DCHECK(attached_shaders_[0].get() &&
1082          attached_shaders_[0]->shader_type() == GL_VERTEX_SHADER &&
1083          attached_shaders_[1].get() &&
1084          attached_shaders_[1]->shader_type() == GL_FRAGMENT_SHADER);
1085   const ShaderTranslator::VariableMap* uniforms[2];
1086   uniforms[0] = &(attached_shaders_[0]->uniform_map());
1087   uniforms[1] = &(attached_shaders_[1]->uniform_map());
1088   const ShaderTranslator::VariableMap* attribs =
1089       &(attached_shaders_[0]->attrib_map());
1090
1091   for (ShaderTranslator::VariableMap::const_iterator iter =
1092            attribs->begin(); iter != attribs->end(); ++iter) {
1093     for (int ii = 0; ii < 2; ++ii) {
1094       if (uniforms[ii]->find(iter->first) != uniforms[ii]->end()) {
1095         *conflicting_name = iter->first;
1096         return true;
1097       }
1098     }
1099   }
1100   return false;
1101 }
1102
1103 bool Program::CheckVaryingsPacking(
1104     Program::VaryingsPackingOption option) const {
1105   DCHECK(attached_shaders_[0].get() &&
1106          attached_shaders_[0]->shader_type() == GL_VERTEX_SHADER &&
1107          attached_shaders_[1].get() &&
1108          attached_shaders_[1]->shader_type() == GL_FRAGMENT_SHADER);
1109   const ShaderTranslator::VariableMap* vertex_varyings =
1110       &(attached_shaders_[0]->varying_map());
1111   const ShaderTranslator::VariableMap* fragment_varyings =
1112       &(attached_shaders_[1]->varying_map());
1113
1114   std::map<std::string, ShVariableInfo> combined_map;
1115
1116   for (ShaderTranslator::VariableMap::const_iterator iter =
1117            fragment_varyings->begin();
1118        iter != fragment_varyings->end(); ++iter) {
1119     if (!iter->second.static_use && option == kCountOnlyStaticallyUsed)
1120       continue;
1121     if (!IsBuiltInVarying(iter->first)) {
1122       ShaderTranslator::VariableMap::const_iterator vertex_iter =
1123           vertex_varyings->find(iter->first);
1124       if (vertex_iter == vertex_varyings->end() ||
1125           (!vertex_iter->second.static_use &&
1126            option == kCountOnlyStaticallyUsed))
1127         continue;
1128     }
1129
1130     ShVariableInfo var;
1131     var.type = static_cast<sh::GLenum>(iter->second.type);
1132     var.size = iter->second.size;
1133     combined_map[iter->first] = var;
1134   }
1135
1136   if (combined_map.size() == 0)
1137     return true;
1138   scoped_ptr<ShVariableInfo[]> variables(
1139       new ShVariableInfo[combined_map.size()]);
1140   size_t index = 0;
1141   for (std::map<std::string, ShVariableInfo>::const_iterator iter =
1142            combined_map.begin();
1143        iter != combined_map.end(); ++iter) {
1144     variables[index].type = iter->second.type;
1145     variables[index].size = iter->second.size;
1146     ++index;
1147   }
1148   return ShCheckVariablesWithinPackingLimits(
1149       static_cast<int>(manager_->max_varying_vectors()),
1150       variables.get(),
1151       combined_map.size()) == 1;
1152 }
1153
1154 static uint32 ComputeOffset(const void* start, const void* position) {
1155   return static_cast<const uint8*>(position) -
1156          static_cast<const uint8*>(start);
1157 }
1158
1159 void Program::GetProgramInfo(
1160     ProgramManager* manager, CommonDecoder::Bucket* bucket) const {
1161   // NOTE: It seems to me the math in here does not need check for overflow
1162   // because the data being calucated from has various small limits. The max
1163   // number of attribs + uniforms is somewhere well under 1024. The maximum size
1164   // of an identifier is 256 characters.
1165   uint32 num_locations = 0;
1166   uint32 total_string_size = 0;
1167
1168   for (size_t ii = 0; ii < attrib_infos_.size(); ++ii) {
1169     const VertexAttrib& info = attrib_infos_[ii];
1170     num_locations += 1;
1171     total_string_size += info.name.size();
1172   }
1173
1174   for (size_t ii = 0; ii < uniform_infos_.size(); ++ii) {
1175     const UniformInfo& info = uniform_infos_[ii];
1176     if (info.IsValid()) {
1177       num_locations += info.element_locations.size();
1178       total_string_size += info.name.size();
1179     }
1180   }
1181
1182   uint32 num_inputs = attrib_infos_.size() + num_uniforms_;
1183   uint32 input_size = num_inputs * sizeof(ProgramInput);
1184   uint32 location_size = num_locations * sizeof(int32);
1185   uint32 size = sizeof(ProgramInfoHeader) +
1186       input_size + location_size + total_string_size;
1187
1188   bucket->SetSize(size);
1189   ProgramInfoHeader* header = bucket->GetDataAs<ProgramInfoHeader*>(0, size);
1190   ProgramInput* inputs = bucket->GetDataAs<ProgramInput*>(
1191       sizeof(ProgramInfoHeader), input_size);
1192   int32* locations = bucket->GetDataAs<int32*>(
1193       sizeof(ProgramInfoHeader) + input_size, location_size);
1194   char* strings = bucket->GetDataAs<char*>(
1195       sizeof(ProgramInfoHeader) + input_size + location_size,
1196       total_string_size);
1197   DCHECK(header);
1198   DCHECK(inputs);
1199   DCHECK(locations);
1200   DCHECK(strings);
1201
1202   header->link_status = link_status_;
1203   header->num_attribs = attrib_infos_.size();
1204   header->num_uniforms = num_uniforms_;
1205
1206   for (size_t ii = 0; ii < attrib_infos_.size(); ++ii) {
1207     const VertexAttrib& info = attrib_infos_[ii];
1208     inputs->size = info.size;
1209     inputs->type = info.type;
1210     inputs->location_offset = ComputeOffset(header, locations);
1211     inputs->name_offset = ComputeOffset(header, strings);
1212     inputs->name_length = info.name.size();
1213     *locations++ = info.location;
1214     memcpy(strings, info.name.c_str(), info.name.size());
1215     strings += info.name.size();
1216     ++inputs;
1217   }
1218
1219   for (size_t ii = 0; ii < uniform_infos_.size(); ++ii) {
1220     const UniformInfo& info = uniform_infos_[ii];
1221     if (info.IsValid()) {
1222       inputs->size = info.size;
1223       inputs->type = info.type;
1224       inputs->location_offset = ComputeOffset(header, locations);
1225       inputs->name_offset = ComputeOffset(header, strings);
1226       inputs->name_length = info.name.size();
1227       DCHECK(static_cast<size_t>(info.size) == info.element_locations.size());
1228       for (size_t jj = 0; jj < info.element_locations.size(); ++jj) {
1229         if (info.element_locations[jj] == -1)
1230           *locations++ = -1;
1231         else
1232           *locations++ = ProgramManager::MakeFakeLocation(ii, jj);
1233       }
1234       memcpy(strings, info.name.c_str(), info.name.size());
1235       strings += info.name.size();
1236       ++inputs;
1237     }
1238   }
1239
1240   DCHECK_EQ(ComputeOffset(header, strings), size);
1241 }
1242
1243 Program::~Program() {
1244   if (manager_) {
1245     if (manager_->have_context_) {
1246       glDeleteProgram(service_id());
1247     }
1248     manager_->StopTracking(this);
1249     manager_ = NULL;
1250   }
1251 }
1252
1253
1254 ProgramManager::ProgramManager(ProgramCache* program_cache,
1255                                uint32 max_varying_vectors)
1256     : program_count_(0),
1257       have_context_(true),
1258       program_cache_(program_cache),
1259       max_varying_vectors_(max_varying_vectors) { }
1260
1261 ProgramManager::~ProgramManager() {
1262   DCHECK(programs_.empty());
1263 }
1264
1265 void ProgramManager::Destroy(bool have_context) {
1266   have_context_ = have_context;
1267   programs_.clear();
1268 }
1269
1270 void ProgramManager::StartTracking(Program* /* program */) {
1271   ++program_count_;
1272 }
1273
1274 void ProgramManager::StopTracking(Program* /* program */) {
1275   --program_count_;
1276 }
1277
1278 Program* ProgramManager::CreateProgram(
1279     GLuint client_id, GLuint service_id) {
1280   std::pair<ProgramMap::iterator, bool> result =
1281       programs_.insert(
1282           std::make_pair(client_id,
1283                          scoped_refptr<Program>(
1284                              new Program(this, service_id))));
1285   DCHECK(result.second);
1286   return result.first->second.get();
1287 }
1288
1289 Program* ProgramManager::GetProgram(GLuint client_id) {
1290   ProgramMap::iterator it = programs_.find(client_id);
1291   return it != programs_.end() ? it->second.get() : NULL;
1292 }
1293
1294 bool ProgramManager::GetClientId(GLuint service_id, GLuint* client_id) const {
1295   // This doesn't need to be fast. It's only used during slow queries.
1296   for (ProgramMap::const_iterator it = programs_.begin();
1297        it != programs_.end(); ++it) {
1298     if (it->second->service_id() == service_id) {
1299       *client_id = it->first;
1300       return true;
1301     }
1302   }
1303   return false;
1304 }
1305
1306 ProgramCache* ProgramManager::program_cache() const {
1307   return program_cache_;
1308 }
1309
1310 bool ProgramManager::IsOwned(Program* program) {
1311   for (ProgramMap::iterator it = programs_.begin();
1312        it != programs_.end(); ++it) {
1313     if (it->second.get() == program) {
1314       return true;
1315     }
1316   }
1317   return false;
1318 }
1319
1320 void ProgramManager::RemoveProgramInfoIfUnused(
1321     ShaderManager* shader_manager, Program* program) {
1322   DCHECK(shader_manager);
1323   DCHECK(program);
1324   DCHECK(IsOwned(program));
1325   if (program->IsDeleted() && !program->InUse()) {
1326     program->DetachShaders(shader_manager);
1327     for (ProgramMap::iterator it = programs_.begin();
1328          it != programs_.end(); ++it) {
1329       if (it->second.get() == program) {
1330         programs_.erase(it);
1331         return;
1332       }
1333     }
1334     NOTREACHED();
1335   }
1336 }
1337
1338 void ProgramManager::MarkAsDeleted(
1339     ShaderManager* shader_manager,
1340     Program* program) {
1341   DCHECK(shader_manager);
1342   DCHECK(program);
1343   DCHECK(IsOwned(program));
1344   program->MarkAsDeleted();
1345   RemoveProgramInfoIfUnused(shader_manager, program);
1346 }
1347
1348 void ProgramManager::UseProgram(Program* program) {
1349   DCHECK(program);
1350   DCHECK(IsOwned(program));
1351   program->IncUseCount();
1352 }
1353
1354 void ProgramManager::UnuseProgram(
1355     ShaderManager* shader_manager,
1356     Program* program) {
1357   DCHECK(shader_manager);
1358   DCHECK(program);
1359   DCHECK(IsOwned(program));
1360   program->DecUseCount();
1361   RemoveProgramInfoIfUnused(shader_manager, program);
1362 }
1363
1364 void ProgramManager::ClearUniforms(Program* program) {
1365   DCHECK(program);
1366   program->ClearUniforms(&zero_);
1367 }
1368
1369 int32 ProgramManager::MakeFakeLocation(int32 index, int32 element) {
1370   return index + element * 0x10000;
1371 }
1372
1373 }  // namespace gles2
1374 }  // namespace gpu