Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / gpu / command_buffer / service / program_cache.cc
blobb219086c046c95e1c37d1315f8706a80daacfdd0
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.
5 #include "gpu/command_buffer/service/program_cache.h"
7 #include <string>
8 #include "base/memory/scoped_ptr.h"
9 #include "gpu/command_buffer/service/shader_manager.h"
11 namespace gpu {
12 namespace gles2 {
14 ProgramCache::ProgramCache() {}
15 ProgramCache::~ProgramCache() {}
17 void ProgramCache::Clear() {
18 ClearBackend();
19 link_status_.clear();
22 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus(
23 const std::string& shader_signature_a,
24 const std::string& shader_signature_b,
25 const std::map<std::string, GLint>* bind_attrib_location_map) const {
26 char a_sha[kHashLength];
27 char b_sha[kHashLength];
28 ComputeShaderHash(shader_signature_a, a_sha);
29 ComputeShaderHash(shader_signature_b, b_sha);
31 char sha[kHashLength];
32 ComputeProgramHash(a_sha,
33 b_sha,
34 bind_attrib_location_map,
35 sha);
36 const std::string sha_string(sha, kHashLength);
38 LinkStatusMap::const_iterator found = link_status_.find(sha_string);
39 if (found == link_status_.end()) {
40 return ProgramCache::LINK_UNKNOWN;
41 } else {
42 return found->second;
46 void ProgramCache::LinkedProgramCacheSuccess(
47 const std::string& shader_signature_a,
48 const std::string& shader_signature_b,
49 const LocationMap* bind_attrib_location_map) {
50 char a_sha[kHashLength];
51 char b_sha[kHashLength];
52 ComputeShaderHash(shader_signature_a, a_sha);
53 ComputeShaderHash(shader_signature_b, b_sha);
54 char sha[kHashLength];
55 ComputeProgramHash(a_sha,
56 b_sha,
57 bind_attrib_location_map,
58 sha);
59 const std::string sha_string(sha, kHashLength);
61 LinkedProgramCacheSuccess(sha_string);
64 void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash) {
65 link_status_[program_hash] = LINK_SUCCEEDED;
68 void ProgramCache::ComputeShaderHash(
69 const std::string& str,
70 char* result) const {
71 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()),
72 str.length(), reinterpret_cast<unsigned char*>(result));
75 void ProgramCache::Evict(const std::string& program_hash) {
76 link_status_.erase(program_hash);
79 namespace {
80 size_t CalculateMapSize(const std::map<std::string, GLint>* map) {
81 if (!map) {
82 return 0;
84 std::map<std::string, GLint>::const_iterator it;
85 size_t total = 0;
86 for (it = map->begin(); it != map->end(); ++it) {
87 total += 4 + it->first.length();
89 return total;
91 } // anonymous namespace
93 void ProgramCache::ComputeProgramHash(
94 const char* hashed_shader_0,
95 const char* hashed_shader_1,
96 const std::map<std::string, GLint>* bind_attrib_location_map,
97 char* result) const {
98 const size_t shader0_size = kHashLength;
99 const size_t shader1_size = kHashLength;
100 const size_t map_size = CalculateMapSize(bind_attrib_location_map);
101 const size_t total_size = shader0_size + shader1_size + map_size;
103 scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]);
104 memcpy(buffer.get(), hashed_shader_0, shader0_size);
105 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size);
106 if (map_size != 0) {
107 // copy our map
108 size_t current_pos = shader0_size + shader1_size;
109 std::map<std::string, GLint>::const_iterator it;
110 for (it = bind_attrib_location_map->begin();
111 it != bind_attrib_location_map->end();
112 ++it) {
113 const size_t name_size = it->first.length();
114 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size);
115 current_pos += name_size;
116 const GLint value = it->second;
117 buffer[current_pos++] = value >> 24;
118 buffer[current_pos++] = static_cast<unsigned char>(value >> 16);
119 buffer[current_pos++] = static_cast<unsigned char>(value >> 8);
120 buffer[current_pos++] = static_cast<unsigned char>(value);
123 base::SHA1HashBytes(buffer.get(),
124 total_size, reinterpret_cast<unsigned char*>(result));
127 } // namespace gles2
128 } // namespace gpu