| Index: gpu/command_buffer/service/program_cache.cc
|
| diff --git a/gpu/command_buffer/service/program_cache.cc b/gpu/command_buffer/service/program_cache.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2b19e501fc017358e16b0f32f19fd7bf98d0168c
|
| --- /dev/null
|
| +++ b/gpu/command_buffer/service/program_cache.cc
|
| @@ -0,0 +1,121 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "program_cache.h"
|
| +
|
| +#include "base/sha1.h"
|
| +
|
| +namespace gpu {
|
| +namespace gles2 {
|
| +
|
| +ProgramCache::~ProgramCache() {
|
| + compile_status_map::iterator compile_status_iter;
|
| + for (compile_status_iter = shader_status_.begin();
|
| + compile_status_iter != shader_status_.end();) {
|
| + const unsigned char* key = compile_status_iter->first;
|
| + ++compile_status_iter;
|
| + shader_status_.erase(key);
|
| + delete[] key;
|
| + }
|
| +
|
| + link_status_map::iterator link_status_iter;
|
| + for (link_status_iter = link_status_.begin();
|
| + link_status_iter != link_status_.end();) {
|
| + CachedProgramKey key = link_status_iter->first;
|
| + ++link_status_iter;
|
| + link_status_.erase(key);
|
| + delete[] key.hashed_src_0;
|
| + delete[] key.hashed_src_1;
|
| + delete key.bind_attrib_location_map;
|
| + }
|
| +}
|
| +
|
| +ProgramCache::CompiledShaderStatus ProgramCache::GetShaderCompilationStatus(
|
| + const std::string& shader_src) const {
|
| +
|
| + unsigned char sha[kHashLength];
|
| + computeHash(shader_src, sha);
|
| +
|
| + compile_status_map::const_iterator found = shader_status_.find(sha);
|
| +
|
| + if (found == shader_status_.end()) {
|
| + return ProgramCache::COMPILATION_UNKNOWN;
|
| + } else {
|
| + return found->second;
|
| + }
|
| +}
|
| +
|
| +void ProgramCache::SetShaderCompilationStatus(
|
| + const std::string& shader_src, ProgramCache::CompiledShaderStatus status) {
|
| + unsigned char sha[kHashLength];
|
| + computeHash(shader_src, sha);
|
| +
|
| + if (shader_status_.find(sha) == shader_status_.end()) {
|
| + unsigned char* allocated_sha = new unsigned char[kHashLength];
|
| + memcpy(allocated_sha, sha, kHashLength);
|
| +
|
| + shader_status_[allocated_sha] = status;
|
| + } else {
|
| + shader_status_[sha] = status;
|
| + }
|
| +
|
| +}
|
| +
|
| +ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus(
|
| + const std::string& untranslated_a,
|
| + const std::string& untranslated_b,
|
| + std::map<std::string, GLint>* bind_attrib_location_map) const {
|
| + unsigned char sha1_a[kHashLength];
|
| + unsigned char sha1_b[kHashLength];
|
| + computeHash(untranslated_a, sha1_a);
|
| + computeHash(untranslated_b, sha1_b);
|
| +
|
| + CachedProgramKey key(sha1_a, sha1_b, bind_attrib_location_map);
|
| + link_status_map::const_iterator found = link_status_.find(key);
|
| + if (found == link_status_.end()) {
|
| + return ProgramCache::LINK_UNKNOWN;
|
| + } else {
|
| + return found->second;
|
| + }
|
| +}
|
| +
|
| +void ProgramCache::SetLinkedProgramStatus(
|
| + const std::string& untranslated_a,
|
| + const std::string& untranslated_b,
|
| + std::map<std::string, GLint>* bind_attrib_location_map,
|
| + ProgramCache::LinkedProgramStatus status) {
|
| + typedef std::map<std::string, GLint> location_map;
|
| + unsigned char sha1_a[kHashLength];
|
| + unsigned char sha1_b[kHashLength];
|
| + computeHash(untranslated_a, sha1_a);
|
| + computeHash(untranslated_b, sha1_b);
|
| +
|
| + CachedProgramKey key(sha1_a, sha1_b, bind_attrib_location_map);
|
| + link_status_map::iterator found = link_status_.find(key);
|
| + if (found == link_status_.end()) {
|
| + unsigned char* allocated_sha_a = new unsigned char[kHashLength];
|
| + unsigned char* allocated_sha_b = new unsigned char[kHashLength];
|
| + location_map* allocated_location_map = bind_attrib_location_map != NULL ?
|
| + new location_map(*bind_attrib_location_map) :
|
| + NULL;
|
| + memcpy(allocated_sha_a, sha1_a, kHashLength);
|
| + memcpy(allocated_sha_b, sha1_b, kHashLength);
|
| +
|
| + CachedProgramKey allocated_key(allocated_sha_a,
|
| + allocated_sha_b,
|
| + allocated_location_map);
|
| + link_status_[allocated_key] = status;
|
| + } else {
|
| + found->second = status;
|
| + }
|
| +}
|
| +
|
| +void ProgramCache::computeHash(const std::string& str,
|
| + unsigned char* hashResult) const {
|
| + base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()),
|
| + str.length(), hashResult);
|
| + hashResult[kHashLength-1] = '\0';
|
| +}
|
| +} // namespace gles2
|
| +} // namespace gpu
|
|
|