Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / program_cache.cc
blobee1a736b9e09211be166be4acdc6a1a3e1485962
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"
10 #include "third_party/angle/src/common/version.h"
12 namespace gpu {
13 namespace gles2 {
15 ProgramCache::ProgramCache() {}
16 ProgramCache::~ProgramCache() {}
18 void ProgramCache::Clear() {
19 ClearBackend();
20 link_status_.clear();
23 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus(
24 const std::string& shader_signature_a,
25 const std::string& shader_signature_b,
26 const std::map<std::string, GLint>* bind_attrib_location_map,
27 const std::vector<std::string>& transform_feedback_varyings,
28 GLenum transform_feedback_buffer_mode) const {
29 char a_sha[kHashLength];
30 char b_sha[kHashLength];
31 ComputeShaderHash(shader_signature_a, a_sha);
32 ComputeShaderHash(shader_signature_b, b_sha);
34 char sha[kHashLength];
35 ComputeProgramHash(a_sha,
36 b_sha,
37 bind_attrib_location_map,
38 transform_feedback_varyings,
39 transform_feedback_buffer_mode,
40 sha);
41 const std::string sha_string(sha, kHashLength);
43 LinkStatusMap::const_iterator found = link_status_.find(sha_string);
44 if (found == link_status_.end()) {
45 return ProgramCache::LINK_UNKNOWN;
46 } else {
47 return found->second;
51 void ProgramCache::LinkedProgramCacheSuccess(
52 const std::string& shader_signature_a,
53 const std::string& shader_signature_b,
54 const LocationMap* bind_attrib_location_map,
55 const std::vector<std::string>& transform_feedback_varyings,
56 GLenum transform_feedback_buffer_mode) {
57 char a_sha[kHashLength];
58 char b_sha[kHashLength];
59 ComputeShaderHash(shader_signature_a, a_sha);
60 ComputeShaderHash(shader_signature_b, b_sha);
61 char sha[kHashLength];
62 ComputeProgramHash(a_sha,
63 b_sha,
64 bind_attrib_location_map,
65 transform_feedback_varyings,
66 transform_feedback_buffer_mode,
67 sha);
68 const std::string sha_string(sha, kHashLength);
70 LinkedProgramCacheSuccess(sha_string);
73 void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash) {
74 link_status_[program_hash] = LINK_SUCCEEDED;
77 void ProgramCache::ComputeShaderHash(
78 const std::string& str,
79 char* result) const {
80 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()),
81 str.length(), reinterpret_cast<unsigned char*>(result));
84 void ProgramCache::Evict(const std::string& program_hash) {
85 link_status_.erase(program_hash);
88 namespace {
89 size_t CalculateMapSize(const std::map<std::string, GLint>* map) {
90 if (!map) {
91 return 0;
93 size_t total = 0;
94 for (auto it = map->begin(); it != map->end(); ++it) {
95 total += 4 + it->first.length();
97 return total;
100 size_t CalculateVaryingsSize(const std::vector<std::string>& varyings) {
101 size_t total = 0;
102 for (auto& varying : varyings) {
103 total += 1 + varying.length();
105 return total;
107 } // anonymous namespace
109 void ProgramCache::ComputeProgramHash(
110 const char* hashed_shader_0,
111 const char* hashed_shader_1,
112 const std::map<std::string, GLint>* bind_attrib_location_map,
113 const std::vector<std::string>& transform_feedback_varyings,
114 GLenum transform_feedback_buffer_mode,
115 char* result) const {
116 const size_t shader0_size = kHashLength;
117 const size_t shader1_size = kHashLength;
118 const size_t angle_commit_size = ANGLE_COMMIT_HASH_SIZE;
119 const size_t map_size = CalculateMapSize(bind_attrib_location_map);
120 const size_t var_size = CalculateVaryingsSize(transform_feedback_varyings);
121 const size_t total_size = shader0_size + shader1_size + angle_commit_size
122 + map_size + var_size + sizeof(transform_feedback_buffer_mode);
124 scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]);
125 memcpy(buffer.get(), hashed_shader_0, shader0_size);
126 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size);
127 size_t current_pos = shader0_size + shader1_size;
128 memcpy(&buffer[current_pos], ANGLE_COMMIT_HASH, angle_commit_size);
129 current_pos += angle_commit_size;
130 if (map_size != 0) {
131 // copy our map
132 for (auto it = bind_attrib_location_map->begin();
133 it != bind_attrib_location_map->end();
134 ++it) {
135 const size_t name_size = it->first.length();
136 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size);
137 current_pos += name_size;
138 const GLint value = it->second;
139 buffer[current_pos++] = value >> 24;
140 buffer[current_pos++] = static_cast<unsigned char>(value >> 16);
141 buffer[current_pos++] = static_cast<unsigned char>(value >> 8);
142 buffer[current_pos++] = static_cast<unsigned char>(value);
146 if (var_size != 0) {
147 // copy transform feedback varyings
148 for (auto& varying : transform_feedback_varyings) {
149 const size_t name_size = varying.length();
150 memcpy(&buffer.get()[current_pos], varying.c_str(), name_size);
151 current_pos += name_size;
152 buffer[current_pos++] = ' ';
155 memcpy(&buffer[current_pos], &transform_feedback_buffer_mode,
156 sizeof(transform_feedback_buffer_mode));
157 base::SHA1HashBytes(buffer.get(),
158 total_size, reinterpret_cast<unsigned char*>(result));
161 } // namespace gles2
162 } // namespace gpu