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/memory_program_cache.h" | |
| 6 #include "base/bind.h" | |
| 7 #include "base/file_path.h" | |
| 8 #include "base/sha1.h" | |
| 9 #include "net/disk_cache/disk_cache.h" | |
| 10 #include "ui/gl/gl_bindings.h" | |
| 11 | |
| 12 namespace gpu { | |
| 13 namespace gles2 { | |
| 14 | |
| 15 namespace { | |
| 16 typedef std::map<std::string, GLint> BindAttribMap; | |
| 17 } // anonymous namespace | |
| 18 | |
| 19 MemoryProgramCache::~MemoryProgramCache() { | |
| 20 StoreMap::const_iterator it; | |
| 21 for(it = store_.begin(); it != store_.end(); ++it) { | |
| 22 delete[] (char*)it->second.data; | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 void MemoryProgramCache::ClearBackend() { | |
| 27 curr_size_bytes_ = 0; | |
| 28 store_.clear(); | |
| 29 lru_helper_.Clear(); | |
| 30 } | |
| 31 | |
| 32 bool MemoryProgramCache::LoadLinkedProgram( | |
| 33 const GLuint program, | |
| 34 ShaderManager::ShaderInfo* shader_a, | |
| 35 ShaderManager::ShaderInfo* shader_b, | |
| 36 const BindAttribMap* bind_attrib_location_map) const { | |
| 37 char a_sha[kHashLength]; | |
| 38 char b_sha[kHashLength]; | |
| 39 ComputeShaderHash(*shader_a->source(), a_sha); | |
| 40 ComputeShaderHash(*shader_b->source(), b_sha); | |
| 41 | |
| 42 char sha[kHashLength]; | |
| 43 ComputeProgramHash(a_sha, | |
| 44 b_sha, | |
| 45 bind_attrib_location_map, | |
| 46 sha); | |
| 47 const std::string shaString(sha, kHashLength); | |
| 48 | |
| 49 StoreMap::const_iterator found = store_.find(shaString); | |
| 50 if(found == store_.end()) { | |
| 51 return false; | |
| 52 } | |
| 53 StoreValue value = found->second; | |
| 54 glProgramBinary(program, value.format, value.data, value.length); | |
| 55 shader_a->SetAttribMap(value.attrib_map_0); | |
| 56 shader_a->SetUniformMap(value.uniform_map_0); | |
| 57 shader_b->SetAttribMap(value.attrib_map_1); | |
| 58 shader_b->SetUniformMap(value.uniform_map_1); | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 void MemoryProgramCache::SaveLinkedProgram( | |
| 63 const GLuint program, | |
| 64 const ShaderManager::ShaderInfo* shader_a, | |
| 65 const ShaderManager::ShaderInfo* shader_b, | |
| 66 const BindAttribMap* bind_attrib_location_map) { | |
| 67 GLsizei length; | |
| 68 GLenum format; | |
| 69 GLsizei bufferlength = 0; | |
| 70 glGetProgramiv(program, 0x8741, &bufferlength); | |
| 71 GLvoid* binary = new char[bufferlength]; | |
|
greggman
2012/06/25 18:53:03
binary will be leaked if length == 0 below
I thin
dmurph
2012/06/26 02:32:56
Done.
| |
| 72 glGetProgramBinary(program, | |
| 73 bufferlength, | |
| 74 &length, | |
| 75 &format, | |
| 76 binary); | |
| 77 if(length == 0) { | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 char a_sha[kHashLength]; | |
| 82 char b_sha[kHashLength]; | |
| 83 ComputeShaderHash(*shader_a->source(), a_sha); | |
| 84 ComputeShaderHash(*shader_b->source(), b_sha); | |
| 85 | |
| 86 StoreValue value(length, | |
| 87 format, | |
| 88 binary, | |
| 89 a_sha, | |
| 90 shader_a->attrib_map(), | |
| 91 shader_a->uniform_map(), | |
| 92 b_sha, | |
| 93 shader_b->attrib_map(), | |
| 94 shader_b->uniform_map()); | |
| 95 | |
| 96 char sha[kHashLength]; | |
| 97 ComputeProgramHash(a_sha, | |
| 98 b_sha, | |
| 99 bind_attrib_location_map, | |
| 100 sha); | |
| 101 const std::string shaString(sha, kHashLength); | |
| 102 | |
| 103 if(store_.find(shaString) != store_.end()) { | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 while(curr_size_bytes_ + length > max_size_bytes_) { | |
| 108 if(lru_helper_.IsEmpty()) { | |
| 109 return; | |
| 110 } | |
| 111 std::string program = lru_helper_.EvictKey(); | |
|
greggman
2012/06/25 18:53:03
could this be const str::string& program? Then no
dmurph
2012/06/26 02:32:56
Done.
| |
| 112 StoreValue evictedValue = store_[program]; | |
|
greggman
2012/06/25 18:53:03
style: variables use under_score
greggman
2012/06/25 18:53:03
This line copies relatively large structure just f
dmurph
2012/06/26 02:32:56
Done.
| |
| 113 store_.erase(program); | |
| 114 delete[] (char*)evictedValue.data; | |
| 115 curr_size_bytes_ -= evictedValue.length; | |
| 116 Evict(program, evictedValue.shader_0_hash, evictedValue.shader_1_hash); | |
| 117 } | |
| 118 store_[shaString] = value; | |
| 119 curr_size_bytes_ += length; | |
| 120 lru_helper_.KeyUsed(shaString); | |
| 121 LinkedProgramCacheSuccess(shaString, | |
| 122 std::string(a_sha, kHashLength), | |
| 123 std::string(b_sha, kHashLength)); | |
| 124 } | |
| 125 | |
| 126 } // namespace gles2 | |
| 127 } // namespace gpu | |
| OLD | NEW |