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 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/sha1.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "gpu/command_buffer/service/gl_utils.h" | |
| 11 #include "gpu/command_buffer/service/gpu_switches.h" | |
| 12 #include "ui/gl/gl_bindings.h" | |
| 13 | |
| 14 namespace { | |
| 15 size_t GetCacheSize() { | |
| 16 size_t size; | |
| 17 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 18 if (command_line->HasSwitch(switches::kGpuProgramCacheSizeKb) && | |
| 19 base::StringToSizeT(command_line->GetSwitchValueNative( | |
| 20 switches::kGpuProgramCacheSizeKb), | |
| 21 &size) && | |
| 22 size >= 0) { | |
| 23 return size; | |
| 24 } | |
| 25 return gpu::gles2::MemoryProgramCache::kDefaultMaxProgramCacheMemoryBytes; | |
| 26 } | |
| 27 } // anonymous namespace | |
| 28 | |
| 29 namespace gpu { | |
| 30 namespace gles2 { | |
| 31 | |
| 32 MemoryProgramCache::MemoryProgramCache() : max_size_bytes_(GetCacheSize()), | |
|
greggman
2012/07/10 21:43:30
style: If they can't fit one one line the style is
dmurph
2012/07/11 23:32:52
Done.
| |
| 33 curr_size_bytes_(0) { } | |
| 34 | |
| 35 void MemoryProgramCache::ClearBackend() { | |
| 36 curr_size_bytes_ = 0; | |
| 37 store_.clear(); | |
| 38 eviction_helper_.Clear(); | |
| 39 } | |
| 40 | |
| 41 ProgramCache::ProgramLoadResult MemoryProgramCache::LoadLinkedProgram( | |
| 42 const GLuint program, | |
| 43 ShaderManager::ShaderInfo* shader_a, | |
| 44 ShaderManager::ShaderInfo* shader_b, | |
| 45 const LocationMap* bind_attrib_location_map) const { | |
| 46 char a_sha[kHashLength]; | |
| 47 char b_sha[kHashLength]; | |
| 48 ComputeShaderHash(*shader_a->source(), a_sha); | |
| 49 ComputeShaderHash(*shader_b->source(), b_sha); | |
| 50 | |
| 51 char sha[kHashLength]; | |
| 52 ComputeProgramHash(a_sha, | |
| 53 b_sha, | |
| 54 bind_attrib_location_map, | |
| 55 sha); | |
| 56 const std::string sha_string(sha, kHashLength); | |
| 57 | |
| 58 StoreMap::const_iterator found = store_.find(sha_string); | |
| 59 if (found == store_.end()) { | |
| 60 return PROGRAM_LOAD_FAILURE; | |
| 61 } | |
| 62 const scoped_refptr<ProgramCacheValue> value = found->second; | |
| 63 glProgramBinary(program, | |
| 64 value->format, | |
| 65 static_cast<const GLvoid*>(value->data.get()), | |
| 66 value->length); | |
| 67 shader_a->SetAttribMap(value->attrib_map_0); | |
| 68 shader_a->SetUniformMap(value->uniform_map_0); | |
| 69 shader_b->SetAttribMap(value->attrib_map_1); | |
| 70 shader_b->SetUniformMap(value->uniform_map_1); | |
| 71 return PROGRAM_LOAD_SUCCESS; | |
| 72 } | |
| 73 | |
| 74 void MemoryProgramCache::SaveLinkedProgram( | |
| 75 const GLuint program, | |
| 76 const ShaderManager::ShaderInfo* shader_a, | |
| 77 const ShaderManager::ShaderInfo* shader_b, | |
| 78 const LocationMap* bind_attrib_location_map) { | |
| 79 GLsizei length; | |
| 80 GLenum format; | |
| 81 GLsizei buffer_length = 0; | |
| 82 glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH_OES, &buffer_length); | |
| 83 if (static_cast<unsigned int>(buffer_length) > max_size_bytes_) { | |
| 84 return; | |
| 85 } | |
| 86 scoped_array<char> binary(new char[buffer_length]); | |
| 87 glGetProgramBinary(program, | |
| 88 buffer_length, | |
| 89 &length, | |
| 90 &format, | |
| 91 binary.get()); | |
| 92 if (length == 0) { | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 char a_sha[kHashLength]; | |
| 97 char b_sha[kHashLength]; | |
| 98 ComputeShaderHash(*shader_a->source(), a_sha); | |
| 99 ComputeShaderHash(*shader_b->source(), b_sha); | |
| 100 | |
| 101 scoped_refptr<ProgramCacheValue> value( | |
| 102 new ProgramCacheValue(length, | |
| 103 format, | |
| 104 binary.release(), | |
| 105 a_sha, | |
| 106 shader_a->attrib_map(), | |
| 107 shader_a->uniform_map(), | |
| 108 b_sha, | |
| 109 shader_b->attrib_map(), | |
| 110 shader_b->uniform_map())); | |
| 111 char sha[kHashLength]; | |
| 112 ComputeProgramHash(a_sha, | |
| 113 b_sha, | |
| 114 bind_attrib_location_map, | |
| 115 sha); | |
| 116 const std::string sha_string(sha, kHashLength); | |
|
greggman
2012/07/10 21:43:30
how about
const std::string sha_string(sha, sizeof
dmurph
2012/07/11 23:32:52
Sure, done.
| |
| 117 | |
| 118 if (store_.find(sha_string) != store_.end()) { | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 while (curr_size_bytes_ + length > max_size_bytes_) { | |
| 123 DCHECK(!eviction_helper_.IsEmpty()); | |
| 124 const std::string& program = eviction_helper_.PeekKey(); | |
| 125 const StoreMap::iterator found = store_.find(program); | |
| 126 const ProgramCacheValue* evicting = found->second.get(); | |
| 127 curr_size_bytes_ -= evicting->length; | |
| 128 Evict(program, evicting->shader_0_hash, evicting->shader_1_hash); | |
| 129 store_.erase(found); | |
| 130 eviction_helper_.PopKey(); | |
| 131 } | |
| 132 store_[sha_string] = value; | |
| 133 curr_size_bytes_ += length; | |
| 134 eviction_helper_.KeyUsed(sha_string); | |
| 135 LinkedProgramCacheSuccess(sha_string, | |
| 136 std::string(a_sha, kHashLength), | |
| 137 std::string(b_sha, kHashLength)); | |
| 138 } | |
| 139 | |
| 140 } // namespace gles2 | |
| 141 } // namespace gpu | |
| OLD | NEW |