| 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 "gpu/command_buffer/service/program_cache.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 |
| 9 namespace gpu { |
| 10 namespace gles2 { |
| 11 |
| 12 ProgramCache::~ProgramCache() {} |
| 13 |
| 14 void ProgramCache::Clear() { |
| 15 shader_status_.clear(); |
| 16 link_status_.clear(); |
| 17 ClearBackend(); |
| 18 } |
| 19 |
| 20 ProgramCache::CompiledShaderStatus ProgramCache::GetShaderCompilationStatus( |
| 21 const std::string& shader_src) const { |
| 22 char sha[kHashLength]; |
| 23 ComputeShaderHash(shader_src, sha); |
| 24 const std::string sha_string(sha, kHashLength); |
| 25 |
| 26 CompileStatusMap::const_iterator found = shader_status_.find(sha_string); |
| 27 |
| 28 if (found == shader_status_.end()) { |
| 29 return ProgramCache::COMPILATION_UNKNOWN; |
| 30 } else { |
| 31 return found->second.status; |
| 32 } |
| 33 } |
| 34 |
| 35 void ProgramCache::ShaderCompilationSucceeded( |
| 36 const std::string& shader_src) { |
| 37 char sha[kHashLength]; |
| 38 ComputeShaderHash(shader_src, sha); |
| 39 const std::string sha_string(sha, kHashLength); |
| 40 |
| 41 shader_status_[sha_string] = CompiledShaderInfo(COMPILATION_SUCCEEDED); |
| 42 } |
| 43 |
| 44 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus( |
| 45 const std::string& untranslated_a, |
| 46 const std::string& untranslated_b, |
| 47 const std::map<std::string, GLint>* bind_attrib_location_map) const { |
| 48 char a_sha[kHashLength]; |
| 49 char b_sha[kHashLength]; |
| 50 ComputeShaderHash(untranslated_a, a_sha); |
| 51 ComputeShaderHash(untranslated_b, b_sha); |
| 52 |
| 53 char sha[kHashLength]; |
| 54 ComputeProgramHash(a_sha, |
| 55 b_sha, |
| 56 bind_attrib_location_map, |
| 57 sha); |
| 58 const std::string sha_string(sha, kHashLength); |
| 59 |
| 60 LinkStatusMap::const_iterator found = link_status_.find(sha_string); |
| 61 if (found == link_status_.end()) { |
| 62 return ProgramCache::LINK_UNKNOWN; |
| 63 } else { |
| 64 return found->second; |
| 65 } |
| 66 } |
| 67 |
| 68 void ProgramCache::LinkedProgramCacheSuccess( |
| 69 const std::string& shader_a, |
| 70 const std::string& shader_b, |
| 71 const LocationMap* bind_attrib_location_map) { |
| 72 char a_sha[kHashLength]; |
| 73 char b_sha[kHashLength]; |
| 74 ComputeShaderHash(shader_a, a_sha); |
| 75 ComputeShaderHash(shader_b, b_sha); |
| 76 char sha[kHashLength]; |
| 77 ComputeProgramHash(a_sha, |
| 78 b_sha, |
| 79 bind_attrib_location_map, |
| 80 sha); |
| 81 const std::string sha_string(sha, kHashLength); |
| 82 |
| 83 LinkedProgramCacheSuccess(sha_string, |
| 84 std::string(a_sha, kHashLength), |
| 85 std::string(b_sha, kHashLength)); |
| 86 } |
| 87 |
| 88 void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash, |
| 89 const std::string& shader_a_hash, |
| 90 const std::string& shader_b_hash) { |
| 91 link_status_[program_hash] = LINK_SUCCEEDED; |
| 92 shader_status_[shader_a_hash].ref_count++; |
| 93 shader_status_[shader_b_hash].ref_count++; |
| 94 } |
| 95 |
| 96 void ProgramCache::ComputeShaderHash(const std::string& str, |
| 97 char* result) const { |
| 98 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), |
| 99 str.length(), reinterpret_cast<unsigned char*>(result)); |
| 100 } |
| 101 |
| 102 void ProgramCache::Evict(const std::string& program_hash, |
| 103 const std::string& shader_0_hash, |
| 104 const std::string& shader_1_hash) { |
| 105 CompiledShaderInfo info0 = shader_status_[shader_0_hash]; |
| 106 CompiledShaderInfo info1 = shader_status_[shader_1_hash]; |
| 107 DCHECK(info0.ref_count > 0); |
| 108 DCHECK(info1.ref_count > 0); |
| 109 if (--info0.ref_count <= 0) { |
| 110 shader_status_.erase(shader_0_hash); |
| 111 } else { |
| 112 shader_status_[shader_0_hash] = info0; |
| 113 } |
| 114 if (--info1.ref_count <= 0) { |
| 115 shader_status_.erase(shader_1_hash); |
| 116 } else { |
| 117 shader_status_[shader_1_hash] = info1; |
| 118 } |
| 119 link_status_.erase(program_hash); |
| 120 } |
| 121 |
| 122 namespace { |
| 123 size_t CalculateMapSize(const std::map<std::string, GLint>* map) { |
| 124 if (!map) { |
| 125 return 0; |
| 126 } |
| 127 std::map<std::string, GLint>::const_iterator it; |
| 128 size_t total = 0; |
| 129 for (it = map->begin(); it != map->end(); ++it) { |
| 130 total += 4 + it->first.length(); |
| 131 } |
| 132 return total; |
| 133 } |
| 134 } // anonymous namespace |
| 135 |
| 136 void ProgramCache::ComputeProgramHash( |
| 137 const char* hashed_shader_0, |
| 138 const char* hashed_shader_1, |
| 139 const std::map<std::string, GLint>* bind_attrib_location_map, |
| 140 char* result) const { |
| 141 const size_t shader0_size = kHashLength; |
| 142 const size_t shader1_size = kHashLength; |
| 143 const size_t map_size = CalculateMapSize(bind_attrib_location_map); |
| 144 const size_t total_size = shader0_size + shader1_size + map_size; |
| 145 |
| 146 scoped_array<unsigned char> buffer(new unsigned char[total_size]); |
| 147 memcpy(buffer.get(), hashed_shader_0, shader0_size); |
| 148 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size); |
| 149 if (map_size != 0) { |
| 150 // copy our map |
| 151 size_t current_pos = shader0_size + shader1_size; |
| 152 std::map<std::string, GLint>::const_iterator it; |
| 153 for (it = bind_attrib_location_map->begin(); |
| 154 it != bind_attrib_location_map->end(); |
| 155 ++it) { |
| 156 const size_t name_size = it->first.length(); |
| 157 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); |
| 158 current_pos += name_size; |
| 159 const GLint value = it->second; |
| 160 buffer[current_pos++] = value >> 24; |
| 161 buffer[current_pos++] = value >> 16; |
| 162 buffer[current_pos++] = value >> 8; |
| 163 buffer[current_pos++] = value; |
| 164 } |
| 165 } |
| 166 base::SHA1HashBytes(buffer.get(), |
| 167 total_size, reinterpret_cast<unsigned char*>(result)); |
| 168 } |
| 169 |
| 170 } // namespace gles2 |
| 171 } // namespace gpu |
| OLD | NEW |