| OLD | NEW |
| (Empty) | |
| 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 "program_cache.h" |
| 6 |
| 7 #include "base/sha1.h" |
| 8 |
| 9 namespace gpu { |
| 10 namespace gles2 { |
| 11 |
| 12 ProgramCache::~ProgramCache() { |
| 13 compile_status_map::iterator compile_status_iter; |
| 14 for (compile_status_iter = shader_status_.begin(); |
| 15 compile_status_iter != shader_status_.end();) { |
| 16 const unsigned char* key = compile_status_iter->first; |
| 17 ++compile_status_iter; |
| 18 shader_status_.erase(key); |
| 19 delete[] key; |
| 20 } |
| 21 |
| 22 link_status_map::iterator link_status_iter; |
| 23 for (link_status_iter = link_status_.begin(); |
| 24 link_status_iter != link_status_.end();) { |
| 25 CachedProgramKey key = link_status_iter->first; |
| 26 ++link_status_iter; |
| 27 link_status_.erase(key); |
| 28 delete[] key.hashed_src_0; |
| 29 delete[] key.hashed_src_1; |
| 30 delete key.bind_attrib_location_map; |
| 31 } |
| 32 } |
| 33 |
| 34 ProgramCache::CompiledShaderStatus ProgramCache::GetShaderCompilationStatus( |
| 35 const std::string& shader_src) const { |
| 36 |
| 37 unsigned char sha[kHashLength]; |
| 38 computeHash(shader_src, sha); |
| 39 |
| 40 compile_status_map::const_iterator found = shader_status_.find(sha); |
| 41 |
| 42 if (found == shader_status_.end()) { |
| 43 return ProgramCache::COMPILATION_UNKNOWN; |
| 44 } else { |
| 45 return found->second; |
| 46 } |
| 47 } |
| 48 |
| 49 void ProgramCache::SetShaderCompilationStatus( |
| 50 const std::string& shader_src, ProgramCache::CompiledShaderStatus status) { |
| 51 unsigned char sha[kHashLength]; |
| 52 computeHash(shader_src, sha); |
| 53 |
| 54 if (shader_status_.find(sha) == shader_status_.end()) { |
| 55 unsigned char* allocated_sha = new unsigned char[kHashLength]; |
| 56 memcpy(allocated_sha, sha, kHashLength); |
| 57 |
| 58 shader_status_[allocated_sha] = status; |
| 59 } else { |
| 60 shader_status_[sha] = status; |
| 61 } |
| 62 |
| 63 } |
| 64 |
| 65 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus( |
| 66 const std::string& untranslated_a, |
| 67 const std::string& untranslated_b, |
| 68 std::map<std::string, GLint>* bind_attrib_location_map) const { |
| 69 unsigned char sha1_a[kHashLength]; |
| 70 unsigned char sha1_b[kHashLength]; |
| 71 computeHash(untranslated_a, sha1_a); |
| 72 computeHash(untranslated_b, sha1_b); |
| 73 |
| 74 CachedProgramKey key(sha1_a, sha1_b, bind_attrib_location_map); |
| 75 link_status_map::const_iterator found = link_status_.find(key); |
| 76 if (found == link_status_.end()) { |
| 77 return ProgramCache::LINK_UNKNOWN; |
| 78 } else { |
| 79 return found->second; |
| 80 } |
| 81 } |
| 82 |
| 83 void ProgramCache::SetLinkedProgramStatus( |
| 84 const std::string& untranslated_a, |
| 85 const std::string& untranslated_b, |
| 86 std::map<std::string, GLint>* bind_attrib_location_map, |
| 87 ProgramCache::LinkedProgramStatus status) { |
| 88 typedef std::map<std::string, GLint> location_map; |
| 89 unsigned char sha1_a[kHashLength]; |
| 90 unsigned char sha1_b[kHashLength]; |
| 91 computeHash(untranslated_a, sha1_a); |
| 92 computeHash(untranslated_b, sha1_b); |
| 93 |
| 94 CachedProgramKey key(sha1_a, sha1_b, bind_attrib_location_map); |
| 95 link_status_map::iterator found = link_status_.find(key); |
| 96 if (found == link_status_.end()) { |
| 97 unsigned char* allocated_sha_a = new unsigned char[kHashLength]; |
| 98 unsigned char* allocated_sha_b = new unsigned char[kHashLength]; |
| 99 location_map* allocated_location_map = bind_attrib_location_map != NULL ? |
| 100 new location_map(*bind_attrib_location_map) : |
| 101 NULL; |
| 102 memcpy(allocated_sha_a, sha1_a, kHashLength); |
| 103 memcpy(allocated_sha_b, sha1_b, kHashLength); |
| 104 |
| 105 CachedProgramKey allocated_key(allocated_sha_a, |
| 106 allocated_sha_b, |
| 107 allocated_location_map); |
| 108 link_status_[allocated_key] = status; |
| 109 } else { |
| 110 found->second = status; |
| 111 } |
| 112 } |
| 113 |
| 114 void ProgramCache::computeHash(const std::string& str, |
| 115 unsigned char* hashResult) const { |
| 116 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), |
| 117 str.length(), hashResult); |
| 118 hashResult[kHashLength-1] = '\0'; |
| 119 } |
| 120 } // namespace gles2 |
| 121 } // namespace gpu |
| OLD | NEW |