Chromium Code Reviews| 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(const std::string& program_hash, | |
| 69 const std::string& shader_a_hash, | |
| 70 const std::string& shader_b_hash) { | |
| 71 link_status_[program_hash] = LINK_SUCCEEDED; | |
| 72 shader_status_[shader_a_hash].ref_count++; | |
| 73 shader_status_[shader_b_hash].ref_count++; | |
| 74 } | |
| 75 | |
| 76 void ProgramCache::ComputeShaderHash(const std::string& str, | |
| 77 char* result) const { | |
| 78 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), | |
| 79 str.length(), reinterpret_cast<unsigned char*>(result)); | |
| 80 } | |
| 81 | |
| 82 void ProgramCache::Evict(const std::string& program_hash, | |
| 83 const std::string& shader_0_hash, | |
| 84 const std::string& shader_1_hash) { | |
| 85 CompiledShaderInfo info0 = shader_status_[shader_0_hash]; | |
| 86 CompiledShaderInfo info1 = shader_status_[shader_1_hash]; | |
| 87 if (--info0.ref_count <= 0) { | |
|
greggman
2012/06/26 23:00:27
Is it an error if this goes < 0? If so add a DCHE
dmurph
2012/07/04 00:01:29
Done.
| |
| 88 shader_status_.erase(shader_0_hash); | |
| 89 } else { | |
| 90 shader_status_[shader_0_hash] = info0; | |
| 91 } | |
| 92 if (--info1.ref_count <= 0) { | |
| 93 shader_status_.erase(shader_1_hash); | |
| 94 } else { | |
| 95 shader_status_[shader_1_hash] = info1; | |
| 96 } | |
| 97 link_status_.erase(program_hash); | |
| 98 } | |
| 99 | |
| 100 namespace { | |
| 101 size_t calculateMapSize(const std::map<std::string, GLint>* map) { | |
| 102 if (!map) { | |
| 103 return 0; | |
| 104 } | |
| 105 std::map<std::string, GLint>::const_iterator it; | |
| 106 size_t total = 0; | |
| 107 for (it = map->begin(); it != map->end(); ++it) { | |
| 108 total += 4 + it->first.length(); | |
| 109 } | |
| 110 return total; | |
| 111 } | |
| 112 } // anonymous namespace | |
| 113 | |
| 114 void ProgramCache::ComputeProgramHash( | |
| 115 const char* hashed_shader_0, | |
| 116 const char* hashed_shader_1, | |
| 117 const std::map<std::string, GLint>* bind_attrib_location_map, | |
| 118 char* result) const { | |
| 119 const size_t shader0_size = kHashLength; | |
| 120 const size_t shader1_size = kHashLength; | |
| 121 const size_t map_size = calculateMapSize(bind_attrib_location_map); | |
| 122 const size_t total_size = shader0_size + shader1_size + map_size; | |
| 123 | |
| 124 scoped_array<char> buffer(new char[total_size]); | |
|
greggman
2012/06/26 23:00:27
if you change this to unsigned char do the casts a
dmurph
2012/07/04 00:01:29
Yeah, done.
| |
| 125 memcpy(buffer.get(), hashed_shader_0, shader0_size); | |
| 126 memcpy(&buffer.get()[shader0_size], hashed_shader_1, shader1_size); | |
| 127 if (map_size != 0) { | |
| 128 // copy our map | |
| 129 size_t current_pos = shader0_size + shader1_size; | |
| 130 std::map<std::string, GLint>::const_iterator it; | |
| 131 for (it = bind_attrib_location_map->begin(); | |
| 132 it != bind_attrib_location_map->end(); | |
| 133 ++it) { | |
| 134 const size_t name_size = it->first.length(); | |
| 135 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); | |
| 136 current_pos += name_size; | |
| 137 const GLint value = it->second; | |
| 138 buffer.get()[current_pos++] = static_cast<char>(value >> 24); | |
| 139 buffer.get()[current_pos++] = static_cast<char>(value >> 16); | |
| 140 buffer.get()[current_pos++] = static_cast<char>(value >> 8); | |
| 141 buffer.get()[current_pos++] = static_cast<char>(value); | |
| 142 } | |
| 143 } | |
| 144 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(buffer.get()), | |
| 145 total_size, reinterpret_cast<unsigned char*>(result)); | |
| 146 } | |
| 147 | |
| 148 } // namespace gles2 | |
| 149 } // namespace gpu | |
| OLD | NEW |