Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / gpu / command_buffer / service / program_cache_unittest.cc
blob7a4cbcdb1274521f8b60a409adcaf9a516b258de
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 "base/memory/scoped_ptr.h"
8 #include "gpu/command_buffer/service/mocks.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using ::testing::Return;
13 namespace gpu {
14 namespace gles2 {
16 class NoBackendProgramCache : public ProgramCache {
17 public:
18 ProgramLoadResult LoadLinkedProgram(
19 GLuint /* program */,
20 Shader* /* shader_a */,
21 Shader* /* shader_b */,
22 const LocationMap* /* bind_attrib_location_map */,
23 const ShaderCacheCallback& /* callback */) override {
24 return PROGRAM_LOAD_SUCCESS;
26 void SaveLinkedProgram(GLuint /* program */,
27 const Shader* /* shader_a */,
28 const Shader* /* shader_b */,
29 const LocationMap* /* bind_attrib_location_map */,
30 const ShaderCacheCallback& /* callback */) override {}
32 void LoadProgram(const std::string& /* program */) override {}
34 void ClearBackend() override {}
36 void SaySuccessfullyCached(const std::string& shader1,
37 const std::string& shader2,
38 std::map<std::string, GLint>* attrib_map) {
39 char a_sha[kHashLength];
40 char b_sha[kHashLength];
41 ComputeShaderHash(shader1, a_sha);
42 ComputeShaderHash(shader2, b_sha);
44 char sha[kHashLength];
45 ComputeProgramHash(a_sha,
46 b_sha,
47 attrib_map,
48 sha);
49 const std::string shaString(sha, kHashLength);
51 LinkedProgramCacheSuccess(shaString);
54 void ComputeShaderHash(const std::string& shader,
55 char* result) const {
56 ProgramCache::ComputeShaderHash(shader, result);
59 void ComputeProgramHash(const char* hashed_shader_0,
60 const char* hashed_shader_1,
61 const LocationMap* bind_attrib_location_map,
62 char* result) const {
63 ProgramCache::ComputeProgramHash(hashed_shader_0,
64 hashed_shader_1,
65 bind_attrib_location_map,
66 result);
69 void Evict(const std::string& program_hash) {
70 ProgramCache::Evict(program_hash);
74 class ProgramCacheTest : public testing::Test {
75 public:
76 ProgramCacheTest() :
77 cache_(new NoBackendProgramCache()) { }
79 protected:
80 scoped_ptr<NoBackendProgramCache> cache_;
83 TEST_F(ProgramCacheTest, LinkStatusSave) {
84 const std::string shader1 = "abcd1234";
85 const std::string shader2 = "abcda sda b1~#4 bbbbb1234";
87 std::string shader_a = shader1;
88 std::string shader_b = shader2;
89 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
90 cache_->GetLinkedProgramStatus(
91 shader_a, shader_b, NULL));
92 cache_->SaySuccessfullyCached(shader_a, shader_b, NULL);
94 shader_a.clear();
95 shader_b.clear();
97 // make sure it was copied
98 EXPECT_EQ(ProgramCache::LINK_SUCCEEDED,
99 cache_->GetLinkedProgramStatus(
100 shader1, shader2, NULL));
103 TEST_F(ProgramCacheTest, LinkUnknownOnFragmentSourceChange) {
104 const std::string shader1 = "abcd1234";
105 std::string shader2 = "abcda sda b1~#4 bbbbb1234";
106 cache_->SaySuccessfullyCached(shader1, shader2, NULL);
108 shader2 = "different!";
109 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
110 cache_->GetLinkedProgramStatus(shader1, shader2, NULL));
113 TEST_F(ProgramCacheTest, LinkUnknownOnVertexSourceChange) {
114 std::string shader1 = "abcd1234";
115 const std::string shader2 = "abcda sda b1~#4 bbbbb1234";
116 cache_->SaySuccessfullyCached(shader1, shader2, NULL);
118 shader1 = "different!";
119 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
120 cache_->GetLinkedProgramStatus(shader1, shader2, NULL));
123 TEST_F(ProgramCacheTest, StatusEviction) {
124 const std::string shader1 = "abcd1234";
125 const std::string shader2 = "abcda sda b1~#4 bbbbb1234";
126 cache_->SaySuccessfullyCached(shader1, shader2, NULL);
127 char a_sha[ProgramCache::kHashLength];
128 char b_sha[ProgramCache::kHashLength];
129 cache_->ComputeShaderHash(shader1, a_sha);
130 cache_->ComputeShaderHash(shader2, b_sha);
132 char sha[ProgramCache::kHashLength];
133 cache_->ComputeProgramHash(a_sha,
134 b_sha,
135 NULL,
136 sha);
137 cache_->Evict(std::string(sha, ProgramCache::kHashLength));
138 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
139 cache_->GetLinkedProgramStatus(shader1, shader2, NULL));
142 TEST_F(ProgramCacheTest, EvictionWithReusedShader) {
143 const std::string shader1 = "abcd1234";
144 const std::string shader2 = "abcda sda b1~#4 bbbbb1234";
145 const std::string shader3 = "asbjbbjj239a";
146 cache_->SaySuccessfullyCached(shader1, shader2, NULL);
147 cache_->SaySuccessfullyCached(shader1, shader3, NULL);
149 char a_sha[ProgramCache::kHashLength];
150 char b_sha[ProgramCache::kHashLength];
151 char c_sha[ProgramCache::kHashLength];
152 cache_->ComputeShaderHash(shader1, a_sha);
153 cache_->ComputeShaderHash(shader2, b_sha);
154 cache_->ComputeShaderHash(shader3, c_sha);
156 char sha[ProgramCache::kHashLength];
157 cache_->ComputeProgramHash(a_sha,
158 b_sha,
159 NULL,
160 sha);
161 cache_->Evict(std::string(sha, ProgramCache::kHashLength));
162 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
163 cache_->GetLinkedProgramStatus(shader1, shader2, NULL));
164 EXPECT_EQ(ProgramCache::LINK_SUCCEEDED,
165 cache_->GetLinkedProgramStatus(shader1, shader3, NULL));
168 cache_->ComputeProgramHash(a_sha,
169 c_sha,
170 NULL,
171 sha);
172 cache_->Evict(std::string(sha, ProgramCache::kHashLength));
173 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
174 cache_->GetLinkedProgramStatus(shader1, shader2, NULL));
175 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
176 cache_->GetLinkedProgramStatus(shader1, shader3, NULL));
179 TEST_F(ProgramCacheTest, StatusClear) {
180 const std::string shader1 = "abcd1234";
181 const std::string shader2 = "abcda sda b1~#4 bbbbb1234";
182 const std::string shader3 = "asbjbbjj239a";
183 cache_->SaySuccessfullyCached(shader1, shader2, NULL);
184 cache_->SaySuccessfullyCached(shader1, shader3, NULL);
185 cache_->Clear();
186 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
187 cache_->GetLinkedProgramStatus(shader1, shader2, NULL));
188 EXPECT_EQ(ProgramCache::LINK_UNKNOWN,
189 cache_->GetLinkedProgramStatus(shader1, shader3, NULL));
192 } // namespace gles2
193 } // namespace gpu