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