| 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() |
| 33 : max_size_bytes_(GetCacheSize()), |
| 34 curr_size_bytes_(0) { } |
| 35 |
| 36 void MemoryProgramCache::ClearBackend() { |
| 37 curr_size_bytes_ = 0; |
| 38 store_.clear(); |
| 39 eviction_helper_.Clear(); |
| 40 } |
| 41 |
| 42 ProgramCache::ProgramLoadResult MemoryProgramCache::LoadLinkedProgram( |
| 43 const GLuint program, |
| 44 ShaderManager::ShaderInfo* shader_a, |
| 45 ShaderManager::ShaderInfo* shader_b, |
| 46 const LocationMap* bind_attrib_location_map) const { |
| 47 char a_sha[kHashLength]; |
| 48 char b_sha[kHashLength]; |
| 49 ComputeShaderHash(*shader_a->source(), a_sha); |
| 50 ComputeShaderHash(*shader_b->source(), b_sha); |
| 51 |
| 52 char sha[kHashLength]; |
| 53 ComputeProgramHash(a_sha, |
| 54 b_sha, |
| 55 bind_attrib_location_map, |
| 56 sha); |
| 57 const std::string sha_string(sha, kHashLength); |
| 58 |
| 59 StoreMap::const_iterator found = store_.find(sha_string); |
| 60 if (found == store_.end()) { |
| 61 return PROGRAM_LOAD_FAILURE; |
| 62 } |
| 63 const scoped_refptr<ProgramCacheValue> value = found->second; |
| 64 glProgramBinary(program, |
| 65 value->format, |
| 66 static_cast<const GLvoid*>(value->data.get()), |
| 67 value->length); |
| 68 shader_a->SetAttribMap(value->attrib_map_0); |
| 69 shader_a->SetUniformMap(value->uniform_map_0); |
| 70 shader_b->SetAttribMap(value->attrib_map_1); |
| 71 shader_b->SetUniformMap(value->uniform_map_1); |
| 72 return PROGRAM_LOAD_SUCCESS; |
| 73 } |
| 74 |
| 75 void MemoryProgramCache::SaveLinkedProgram( |
| 76 const GLuint program, |
| 77 const ShaderManager::ShaderInfo* shader_a, |
| 78 const ShaderManager::ShaderInfo* shader_b, |
| 79 const LocationMap* bind_attrib_location_map) { |
| 80 GLsizei length; |
| 81 GLenum format; |
| 82 GLsizei buffer_length = 0; |
| 83 glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH_OES, &buffer_length); |
| 84 if (static_cast<unsigned int>(buffer_length) > max_size_bytes_) { |
| 85 return; |
| 86 } |
| 87 scoped_array<char> binary(new char[buffer_length]); |
| 88 glGetProgramBinary(program, |
| 89 buffer_length, |
| 90 &length, |
| 91 &format, |
| 92 binary.get()); |
| 93 if (length == 0) { |
| 94 return; |
| 95 } |
| 96 |
| 97 char a_sha[kHashLength]; |
| 98 char b_sha[kHashLength]; |
| 99 ComputeShaderHash(*shader_a->source(), a_sha); |
| 100 ComputeShaderHash(*shader_b->source(), b_sha); |
| 101 |
| 102 scoped_refptr<ProgramCacheValue> value( |
| 103 new ProgramCacheValue(length, |
| 104 format, |
| 105 binary.release(), |
| 106 a_sha, |
| 107 shader_a->attrib_map(), |
| 108 shader_a->uniform_map(), |
| 109 b_sha, |
| 110 shader_b->attrib_map(), |
| 111 shader_b->uniform_map())); |
| 112 char sha[kHashLength]; |
| 113 ComputeProgramHash(a_sha, |
| 114 b_sha, |
| 115 bind_attrib_location_map, |
| 116 sha); |
| 117 const std::string sha_string(sha, sizeof(sha)); |
| 118 |
| 119 if (store_.find(sha_string) != store_.end()) { |
| 120 return; |
| 121 } |
| 122 |
| 123 while (curr_size_bytes_ + length > max_size_bytes_) { |
| 124 DCHECK(!eviction_helper_.IsEmpty()); |
| 125 const std::string* program = eviction_helper_.PeekKey(); |
| 126 const StoreMap::iterator found = store_.find(*program); |
| 127 const ProgramCacheValue* evicting = found->second.get(); |
| 128 curr_size_bytes_ -= evicting->length; |
| 129 Evict(*program, evicting->shader_0_hash, evicting->shader_1_hash); |
| 130 store_.erase(found); |
| 131 eviction_helper_.PopKey(); |
| 132 } |
| 133 store_[sha_string] = value; |
| 134 curr_size_bytes_ += length; |
| 135 eviction_helper_.KeyUsed(sha_string); |
| 136 LinkedProgramCacheSuccess(sha_string, |
| 137 std::string(a_sha, kHashLength), |
| 138 std::string(b_sha, kHashLength)); |
| 139 } |
| 140 |
| 141 } // namespace gles2 |
| 142 } // namespace gpu |
| OLD | NEW |