| 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/metrics/histogram.h" | |
| 9 #include "base/sha1.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "gpu/command_buffer/service/gl_utils.h" | |
| 12 #include "gpu/command_buffer/service/gpu_switches.h" | |
| 13 #include "ui/gl/gl_bindings.h" | |
| 14 | |
| 15 namespace { | |
| 16 size_t GetCacheSize() { | |
| 17 size_t size; | |
| 18 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 19 if (command_line->HasSwitch(switches::kGpuProgramCacheSizeKb) && | |
| 20 base::StringToSizeT(command_line->GetSwitchValueNative( | |
| 21 switches::kGpuProgramCacheSizeKb), | |
| 22 &size)) { | |
| 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() | |
| 33 : max_size_bytes_(GetCacheSize()), | |
| 34 curr_size_bytes_(0) { } | |
| 35 | |
| 36 MemoryProgramCache::MemoryProgramCache(const size_t max_cache_size_bytes) | |
| 37 : max_size_bytes_(max_cache_size_bytes), | |
| 38 curr_size_bytes_(0) {} | |
| 39 | |
| 40 MemoryProgramCache::~MemoryProgramCache() {} | |
| 41 | |
| 42 void MemoryProgramCache::ClearBackend() { | |
| 43 curr_size_bytes_ = 0; | |
| 44 store_.clear(); | |
| 45 eviction_helper_.Clear(); | |
| 46 } | |
| 47 | |
| 48 ProgramCache::ProgramLoadResult MemoryProgramCache::LoadLinkedProgram( | |
| 49 GLuint program, | |
| 50 ShaderManager::ShaderInfo* shader_a, | |
| 51 ShaderManager::ShaderInfo* shader_b, | |
| 52 const LocationMap* bind_attrib_location_map) const { | |
| 53 char a_sha[kHashLength]; | |
| 54 char b_sha[kHashLength]; | |
| 55 ComputeShaderHash(*shader_a->deferred_compilation_source(), a_sha); | |
| 56 ComputeShaderHash(*shader_b->deferred_compilation_source(), b_sha); | |
| 57 | |
| 58 char sha[kHashLength]; | |
| 59 ComputeProgramHash(a_sha, | |
| 60 b_sha, | |
| 61 bind_attrib_location_map, | |
| 62 sha); | |
| 63 const std::string sha_string(sha, kHashLength); | |
| 64 | |
| 65 StoreMap::const_iterator found = store_.find(sha_string); | |
| 66 if (found == store_.end()) { | |
| 67 return PROGRAM_LOAD_FAILURE; | |
| 68 } | |
| 69 const scoped_refptr<ProgramCacheValue> value = found->second; | |
| 70 glProgramBinary(program, | |
| 71 value->format, | |
| 72 static_cast<const GLvoid*>(value->data.get()), | |
| 73 value->length); | |
| 74 shader_a->set_attrib_map(value->attrib_map_0); | |
| 75 shader_a->set_uniform_map(value->uniform_map_0); | |
| 76 shader_b->set_attrib_map(value->attrib_map_1); | |
| 77 shader_b->set_uniform_map(value->uniform_map_1); | |
| 78 return PROGRAM_LOAD_SUCCESS; | |
| 79 } | |
| 80 | |
| 81 void MemoryProgramCache::SaveLinkedProgram( | |
| 82 GLuint program, | |
| 83 const ShaderManager::ShaderInfo* shader_a, | |
| 84 const ShaderManager::ShaderInfo* shader_b, | |
| 85 const LocationMap* bind_attrib_location_map) { | |
| 86 GLsizei length; | |
| 87 GLenum format; | |
| 88 GLsizei buffer_length = 0; | |
| 89 glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH_OES, &buffer_length); | |
| 90 if (static_cast<unsigned int>(buffer_length) > max_size_bytes_) { | |
| 91 return; | |
| 92 } | |
| 93 scoped_array<char> binary(new char[buffer_length]); | |
| 94 glGetProgramBinary(program, | |
| 95 buffer_length, | |
| 96 &length, | |
| 97 &format, | |
| 98 binary.get()); | |
| 99 if (length == 0) { | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 char a_sha[kHashLength]; | |
| 104 char b_sha[kHashLength]; | |
| 105 ComputeShaderHash(*shader_a->deferred_compilation_source(), a_sha); | |
| 106 ComputeShaderHash(*shader_b->deferred_compilation_source(), b_sha); | |
| 107 | |
| 108 char sha[kHashLength]; | |
| 109 ComputeProgramHash(a_sha, | |
| 110 b_sha, | |
| 111 bind_attrib_location_map, | |
| 112 sha); | |
| 113 const std::string sha_string(sha, sizeof(sha)); | |
| 114 | |
| 115 if (store_.find(sha_string) != store_.end()) { | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 while (curr_size_bytes_ + length > max_size_bytes_) { | |
| 120 DCHECK(!eviction_helper_.IsEmpty()); | |
| 121 const std::string* program = eviction_helper_.PeekKey(); | |
| 122 const StoreMap::iterator found = store_.find(*program); | |
| 123 const ProgramCacheValue* evicting = found->second.get(); | |
| 124 curr_size_bytes_ -= evicting->length; | |
| 125 Evict(*program, evicting->shader_0_hash, evicting->shader_1_hash); | |
| 126 store_.erase(found); | |
| 127 eviction_helper_.PopKey(); | |
| 128 } | |
| 129 store_[sha_string] = new ProgramCacheValue(length, | |
| 130 format, | |
| 131 binary.release(), | |
| 132 a_sha, | |
| 133 shader_a->attrib_map(), | |
| 134 shader_a->uniform_map(), | |
| 135 b_sha, | |
| 136 shader_b->attrib_map(), | |
| 137 shader_b->uniform_map()); | |
| 138 curr_size_bytes_ += length; | |
| 139 eviction_helper_.KeyUsed(sha_string); | |
| 140 LinkedProgramCacheSuccess(sha_string, | |
| 141 std::string(a_sha, kHashLength), | |
| 142 std::string(b_sha, kHashLength)); | |
| 143 } | |
| 144 | |
| 145 MemoryProgramCache::ProgramCacheValue::ProgramCacheValue( | |
| 146 GLsizei _length, | |
| 147 GLenum _format, | |
| 148 const char* _data, | |
| 149 const char* _shader_0_hash, | |
| 150 const ShaderTranslator::VariableMap& _attrib_map_0, | |
| 151 const ShaderTranslator::VariableMap& _uniform_map_0, | |
| 152 const char* _shader_1_hash, | |
| 153 const ShaderTranslator::VariableMap& _attrib_map_1, | |
| 154 const ShaderTranslator::VariableMap& _uniform_map_1) | |
| 155 : length(_length), | |
| 156 format(_format), | |
| 157 data(_data), | |
| 158 shader_0_hash(_shader_0_hash, kHashLength), | |
| 159 attrib_map_0(_attrib_map_0), | |
| 160 uniform_map_0(_uniform_map_0), | |
| 161 shader_1_hash(_shader_1_hash, kHashLength), | |
| 162 attrib_map_1(_attrib_map_1), | |
| 163 uniform_map_1(_uniform_map_1) {} | |
| 164 | |
| 165 MemoryProgramCache::ProgramCacheValue::~ProgramCacheValue() {} | |
| 166 | |
| 167 } // namespace gles2 | |
| 168 } // namespace gpu | |
| OLD | NEW |