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
;
16 class NoBackendProgramCache
: public ProgramCache
{
18 ProgramLoadResult
LoadLinkedProgram(
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
,
49 const std::string
shaString(sha
, kHashLength
);
51 LinkedProgramCacheSuccess(shaString
);
54 void ComputeShaderHash(const std::string
& shader
,
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
,
63 ProgramCache::ComputeProgramHash(hashed_shader_0
,
65 bind_attrib_location_map
,
69 void Evict(const std::string
& program_hash
) {
70 ProgramCache::Evict(program_hash
);
74 class ProgramCacheTest
: public testing::Test
{
77 cache_(new NoBackendProgramCache()) { }
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
);
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
,
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
,
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
,
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
);
186 EXPECT_EQ(ProgramCache::LINK_UNKNOWN
,
187 cache_
->GetLinkedProgramStatus(shader1
, shader2
, NULL
));
188 EXPECT_EQ(ProgramCache::LINK_UNKNOWN
,
189 cache_
->GetLinkedProgramStatus(shader1
, shader3
, NULL
));