xref: /aosp_15_r20/external/angle/src/tests/egl_tests/EGLBlobCacheTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // EGLBlobCacheTest:
7*8975f5c5SAndroid Build Coastguard Worker //   Unit tests for the EGL_ANDROID_blob_cache extension.
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker // Must be included first to prevent errors with "None".
10*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/ANGLETest.h"
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker #include <map>
13*8975f5c5SAndroid Build Coastguard Worker #include <vector>
14*8975f5c5SAndroid Build Coastguard Worker 
15*8975f5c5SAndroid Build Coastguard Worker #include "common/PackedEnums.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "common/angleutils.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/ANGLETest.h"
18*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/MultiThreadSteps.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/gl_raii.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "util/EGLWindow.h"
21*8975f5c5SAndroid Build Coastguard Worker #include "util/test_utils.h"
22*8975f5c5SAndroid Build Coastguard Worker 
23*8975f5c5SAndroid Build Coastguard Worker using namespace angle;
24*8975f5c5SAndroid Build Coastguard Worker 
25*8975f5c5SAndroid Build Coastguard Worker constexpr char kEGLExtName[] = "EGL_ANDROID_blob_cache";
26*8975f5c5SAndroid Build Coastguard Worker 
27*8975f5c5SAndroid Build Coastguard Worker enum class CacheOpResult
28*8975f5c5SAndroid Build Coastguard Worker {
29*8975f5c5SAndroid Build Coastguard Worker     SetSuccess,
30*8975f5c5SAndroid Build Coastguard Worker     GetNotFound,
31*8975f5c5SAndroid Build Coastguard Worker     GetMemoryTooSmall,
32*8975f5c5SAndroid Build Coastguard Worker     GetSuccess,
33*8975f5c5SAndroid Build Coastguard Worker     ValueNotSet,
34*8975f5c5SAndroid Build Coastguard Worker     EnumCount
35*8975f5c5SAndroid Build Coastguard Worker };
36*8975f5c5SAndroid Build Coastguard Worker 
37*8975f5c5SAndroid Build Coastguard Worker angle::PackedEnumMap<CacheOpResult, std::string> kCacheOpToString = {
38*8975f5c5SAndroid Build Coastguard Worker     {CacheOpResult::SetSuccess, "SetSuccess"},
39*8975f5c5SAndroid Build Coastguard Worker     {CacheOpResult::GetNotFound, "GetNotFound"},
40*8975f5c5SAndroid Build Coastguard Worker     {CacheOpResult::GetMemoryTooSmall, "GetMemoryTooSmall"},
41*8975f5c5SAndroid Build Coastguard Worker     {CacheOpResult::GetSuccess, "GetSuccess"},
42*8975f5c5SAndroid Build Coastguard Worker     {CacheOpResult::ValueNotSet, "ValueNotSet"},
43*8975f5c5SAndroid Build Coastguard Worker };
44*8975f5c5SAndroid Build Coastguard Worker 
operator <<(std::ostream & os,CacheOpResult result)45*8975f5c5SAndroid Build Coastguard Worker std::ostream &operator<<(std::ostream &os, CacheOpResult result)
46*8975f5c5SAndroid Build Coastguard Worker {
47*8975f5c5SAndroid Build Coastguard Worker     return os << kCacheOpToString[result];
48*8975f5c5SAndroid Build Coastguard Worker }
49*8975f5c5SAndroid Build Coastguard Worker 
50*8975f5c5SAndroid Build Coastguard Worker namespace
51*8975f5c5SAndroid Build Coastguard Worker {
52*8975f5c5SAndroid Build Coastguard Worker std::map<std::vector<uint8_t>, std::vector<uint8_t>> gApplicationCache;
53*8975f5c5SAndroid Build Coastguard Worker CacheOpResult gLastCacheOpResult = CacheOpResult::ValueNotSet;
54*8975f5c5SAndroid Build Coastguard Worker 
SetBlob(const void * key,EGLsizeiANDROID keySize,const void * value,EGLsizeiANDROID valueSize)55*8975f5c5SAndroid Build Coastguard Worker void SetBlob(const void *key, EGLsizeiANDROID keySize, const void *value, EGLsizeiANDROID valueSize)
56*8975f5c5SAndroid Build Coastguard Worker {
57*8975f5c5SAndroid Build Coastguard Worker     std::vector<uint8_t> keyVec(keySize);
58*8975f5c5SAndroid Build Coastguard Worker     memcpy(keyVec.data(), key, keySize);
59*8975f5c5SAndroid Build Coastguard Worker 
60*8975f5c5SAndroid Build Coastguard Worker     std::vector<uint8_t> valueVec(valueSize);
61*8975f5c5SAndroid Build Coastguard Worker     memcpy(valueVec.data(), value, valueSize);
62*8975f5c5SAndroid Build Coastguard Worker 
63*8975f5c5SAndroid Build Coastguard Worker     gApplicationCache[keyVec] = valueVec;
64*8975f5c5SAndroid Build Coastguard Worker 
65*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::SetSuccess;
66*8975f5c5SAndroid Build Coastguard Worker }
67*8975f5c5SAndroid Build Coastguard Worker 
SetCorruptedBlob(const void * key,EGLsizeiANDROID keySize,const void * value,EGLsizeiANDROID valueSize)68*8975f5c5SAndroid Build Coastguard Worker void SetCorruptedBlob(const void *key,
69*8975f5c5SAndroid Build Coastguard Worker                       EGLsizeiANDROID keySize,
70*8975f5c5SAndroid Build Coastguard Worker                       const void *value,
71*8975f5c5SAndroid Build Coastguard Worker                       EGLsizeiANDROID valueSize)
72*8975f5c5SAndroid Build Coastguard Worker {
73*8975f5c5SAndroid Build Coastguard Worker     std::vector<uint8_t> keyVec(keySize);
74*8975f5c5SAndroid Build Coastguard Worker     memcpy(keyVec.data(), key, keySize);
75*8975f5c5SAndroid Build Coastguard Worker 
76*8975f5c5SAndroid Build Coastguard Worker     std::vector<uint8_t> valueVec(valueSize);
77*8975f5c5SAndroid Build Coastguard Worker     memcpy(valueVec.data(), value, valueSize);
78*8975f5c5SAndroid Build Coastguard Worker 
79*8975f5c5SAndroid Build Coastguard Worker     // Corrupt the data
80*8975f5c5SAndroid Build Coastguard Worker     ++valueVec[valueVec.size() / 2];
81*8975f5c5SAndroid Build Coastguard Worker     ++valueVec[valueVec.size() / 3];
82*8975f5c5SAndroid Build Coastguard Worker     ++valueVec[valueVec.size() / 4];
83*8975f5c5SAndroid Build Coastguard Worker     ++valueVec[2 * valueVec.size() / 3];
84*8975f5c5SAndroid Build Coastguard Worker     ++valueVec[3 * valueVec.size() / 4];
85*8975f5c5SAndroid Build Coastguard Worker 
86*8975f5c5SAndroid Build Coastguard Worker     gApplicationCache[keyVec] = valueVec;
87*8975f5c5SAndroid Build Coastguard Worker 
88*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::SetSuccess;
89*8975f5c5SAndroid Build Coastguard Worker }
90*8975f5c5SAndroid Build Coastguard Worker 
GetBlob(const void * key,EGLsizeiANDROID keySize,void * value,EGLsizeiANDROID valueSize)91*8975f5c5SAndroid Build Coastguard Worker EGLsizeiANDROID GetBlob(const void *key,
92*8975f5c5SAndroid Build Coastguard Worker                         EGLsizeiANDROID keySize,
93*8975f5c5SAndroid Build Coastguard Worker                         void *value,
94*8975f5c5SAndroid Build Coastguard Worker                         EGLsizeiANDROID valueSize)
95*8975f5c5SAndroid Build Coastguard Worker {
96*8975f5c5SAndroid Build Coastguard Worker     std::vector<uint8_t> keyVec(keySize);
97*8975f5c5SAndroid Build Coastguard Worker     memcpy(keyVec.data(), key, keySize);
98*8975f5c5SAndroid Build Coastguard Worker 
99*8975f5c5SAndroid Build Coastguard Worker     auto entry = gApplicationCache.find(keyVec);
100*8975f5c5SAndroid Build Coastguard Worker     if (entry == gApplicationCache.end())
101*8975f5c5SAndroid Build Coastguard Worker     {
102*8975f5c5SAndroid Build Coastguard Worker         // A compile+link operation can generate multiple queries to the cache; one per shader and
103*8975f5c5SAndroid Build Coastguard Worker         // one for link.  For the purposes of the test, make sure that any of these hitting the
104*8975f5c5SAndroid Build Coastguard Worker         // cache is considered a success, particularly because it's valid for the pipeline cache
105*8975f5c5SAndroid Build Coastguard Worker         // entry not to exist in the cache.
106*8975f5c5SAndroid Build Coastguard Worker         if (gLastCacheOpResult != CacheOpResult::GetSuccess)
107*8975f5c5SAndroid Build Coastguard Worker         {
108*8975f5c5SAndroid Build Coastguard Worker             gLastCacheOpResult = CacheOpResult::GetNotFound;
109*8975f5c5SAndroid Build Coastguard Worker         }
110*8975f5c5SAndroid Build Coastguard Worker         return 0;
111*8975f5c5SAndroid Build Coastguard Worker     }
112*8975f5c5SAndroid Build Coastguard Worker 
113*8975f5c5SAndroid Build Coastguard Worker     if (entry->second.size() <= static_cast<size_t>(valueSize))
114*8975f5c5SAndroid Build Coastguard Worker     {
115*8975f5c5SAndroid Build Coastguard Worker         memcpy(value, entry->second.data(), entry->second.size());
116*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::GetSuccess;
117*8975f5c5SAndroid Build Coastguard Worker     }
118*8975f5c5SAndroid Build Coastguard Worker     else
119*8975f5c5SAndroid Build Coastguard Worker     {
120*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::GetMemoryTooSmall;
121*8975f5c5SAndroid Build Coastguard Worker     }
122*8975f5c5SAndroid Build Coastguard Worker 
123*8975f5c5SAndroid Build Coastguard Worker     return entry->second.size();
124*8975f5c5SAndroid Build Coastguard Worker }
125*8975f5c5SAndroid Build Coastguard Worker 
WaitProgramBinaryReady(GLuint program)126*8975f5c5SAndroid Build Coastguard Worker void WaitProgramBinaryReady(GLuint program)
127*8975f5c5SAndroid Build Coastguard Worker {
128*8975f5c5SAndroid Build Coastguard Worker     // Using GL_ANGLE_program_binary_readiness_query, wait for post-link tasks to finish.
129*8975f5c5SAndroid Build Coastguard Worker     // Otherwise, the program binary may not yet be cached.  Only needed when a |set| operation is
130*8975f5c5SAndroid Build Coastguard Worker     // expected.
131*8975f5c5SAndroid Build Coastguard Worker     if (!IsGLExtensionEnabled("GL_ANGLE_program_binary_readiness_query"))
132*8975f5c5SAndroid Build Coastguard Worker     {
133*8975f5c5SAndroid Build Coastguard Worker         return;
134*8975f5c5SAndroid Build Coastguard Worker     }
135*8975f5c5SAndroid Build Coastguard Worker 
136*8975f5c5SAndroid Build Coastguard Worker     GLint ready = false;
137*8975f5c5SAndroid Build Coastguard Worker     while (!ready)
138*8975f5c5SAndroid Build Coastguard Worker     {
139*8975f5c5SAndroid Build Coastguard Worker         glGetProgramiv(program, GL_PROGRAM_BINARY_READY_ANGLE, &ready);
140*8975f5c5SAndroid Build Coastguard Worker         angle::Sleep(0);
141*8975f5c5SAndroid Build Coastguard Worker     }
142*8975f5c5SAndroid Build Coastguard Worker }
143*8975f5c5SAndroid Build Coastguard Worker }  // anonymous namespace
144*8975f5c5SAndroid Build Coastguard Worker 
145*8975f5c5SAndroid Build Coastguard Worker class EGLBlobCacheTest : public ANGLETest<>
146*8975f5c5SAndroid Build Coastguard Worker {
147*8975f5c5SAndroid Build Coastguard Worker   protected:
EGLBlobCacheTest()148*8975f5c5SAndroid Build Coastguard Worker     EGLBlobCacheTest() : mHasBlobCache(false)
149*8975f5c5SAndroid Build Coastguard Worker     {
150*8975f5c5SAndroid Build Coastguard Worker         // Force disply caching off. Blob cache functions require it.
151*8975f5c5SAndroid Build Coastguard Worker         forceNewDisplay();
152*8975f5c5SAndroid Build Coastguard Worker     }
153*8975f5c5SAndroid Build Coastguard Worker 
testSetUp()154*8975f5c5SAndroid Build Coastguard Worker     void testSetUp() override
155*8975f5c5SAndroid Build Coastguard Worker     {
156*8975f5c5SAndroid Build Coastguard Worker         EGLDisplay display = getEGLWindow()->getDisplay();
157*8975f5c5SAndroid Build Coastguard Worker         mHasBlobCache      = IsEGLDisplayExtensionEnabled(display, kEGLExtName);
158*8975f5c5SAndroid Build Coastguard Worker     }
159*8975f5c5SAndroid Build Coastguard Worker 
testTearDown()160*8975f5c5SAndroid Build Coastguard Worker     void testTearDown() override { gApplicationCache.clear(); }
161*8975f5c5SAndroid Build Coastguard Worker 
programBinaryAvailable()162*8975f5c5SAndroid Build Coastguard Worker     bool programBinaryAvailable() { return IsGLExtensionEnabled("GL_OES_get_program_binary"); }
163*8975f5c5SAndroid Build Coastguard Worker 
164*8975f5c5SAndroid Build Coastguard Worker     bool mHasBlobCache;
165*8975f5c5SAndroid Build Coastguard Worker };
166*8975f5c5SAndroid Build Coastguard Worker 
167*8975f5c5SAndroid Build Coastguard Worker // Makes sure the extension exists and works
TEST_P(EGLBlobCacheTest,Functional)168*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, Functional)
169*8975f5c5SAndroid Build Coastguard Worker {
170*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
171*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
172*8975f5c5SAndroid Build Coastguard Worker 
173*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
174*8975f5c5SAndroid Build Coastguard Worker 
175*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
176*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
177*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
178*8975f5c5SAndroid Build Coastguard Worker 
179*8975f5c5SAndroid Build Coastguard Worker     constexpr char kVertexShaderSrc[] = R"(attribute vec4 aTest;
180*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
181*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
182*8975f5c5SAndroid Build Coastguard Worker void main()
183*8975f5c5SAndroid Build Coastguard Worker {
184*8975f5c5SAndroid Build Coastguard Worker     vTest        = aTest;
185*8975f5c5SAndroid Build Coastguard Worker     gl_Position  = vec4(aPosition, 0.0, 1.0);
186*8975f5c5SAndroid Build Coastguard Worker     gl_PointSize = 1.0;
187*8975f5c5SAndroid Build Coastguard Worker })";
188*8975f5c5SAndroid Build Coastguard Worker 
189*8975f5c5SAndroid Build Coastguard Worker     constexpr char kFragmentShaderSrc[] = R"(precision mediump float;
190*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
191*8975f5c5SAndroid Build Coastguard Worker void main()
192*8975f5c5SAndroid Build Coastguard Worker {
193*8975f5c5SAndroid Build Coastguard Worker     gl_FragColor = vTest;
194*8975f5c5SAndroid Build Coastguard Worker })";
195*8975f5c5SAndroid Build Coastguard Worker 
196*8975f5c5SAndroid Build Coastguard Worker     constexpr char kVertexShaderSrc2[] = R"(attribute vec4 aTest;
197*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
198*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
199*8975f5c5SAndroid Build Coastguard Worker void main()
200*8975f5c5SAndroid Build Coastguard Worker {
201*8975f5c5SAndroid Build Coastguard Worker     vTest        = aTest;
202*8975f5c5SAndroid Build Coastguard Worker     gl_Position  = vec4(aPosition, 1.0, 1.0);
203*8975f5c5SAndroid Build Coastguard Worker     gl_PointSize = 1.0;
204*8975f5c5SAndroid Build Coastguard Worker })";
205*8975f5c5SAndroid Build Coastguard Worker 
206*8975f5c5SAndroid Build Coastguard Worker     constexpr char kFragmentShaderSrc2[] = R"(precision mediump float;
207*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
208*8975f5c5SAndroid Build Coastguard Worker void main()
209*8975f5c5SAndroid Build Coastguard Worker {
210*8975f5c5SAndroid Build Coastguard Worker     gl_FragColor = vTest - vec4(0.0, 1.0, 0.0, 0.0);
211*8975f5c5SAndroid Build Coastguard Worker })";
212*8975f5c5SAndroid Build Coastguard Worker 
213*8975f5c5SAndroid Build Coastguard Worker     // Compile a shader so it puts something in the cache.  Note that with Vulkan, some optional
214*8975f5c5SAndroid Build Coastguard Worker     // link subtasks may run beyond link, and so the caching is delayed.  An explicit wait on these
215*8975f5c5SAndroid Build Coastguard Worker     // tasks is done for this reason.
216*8975f5c5SAndroid Build Coastguard Worker     if (programBinaryAvailable())
217*8975f5c5SAndroid Build Coastguard Worker     {
218*8975f5c5SAndroid Build Coastguard Worker         ANGLE_GL_PROGRAM(program, kVertexShaderSrc, kFragmentShaderSrc);
219*8975f5c5SAndroid Build Coastguard Worker         WaitProgramBinaryReady(program);
220*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
221*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
222*8975f5c5SAndroid Build Coastguard Worker 
223*8975f5c5SAndroid Build Coastguard Worker         // Compile the same shader again, so it would try to retrieve it from the cache
224*8975f5c5SAndroid Build Coastguard Worker         program.makeRaster(kVertexShaderSrc, kFragmentShaderSrc);
225*8975f5c5SAndroid Build Coastguard Worker         ASSERT_TRUE(program.valid());
226*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::GetSuccess, gLastCacheOpResult);
227*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
228*8975f5c5SAndroid Build Coastguard Worker 
229*8975f5c5SAndroid Build Coastguard Worker         // Compile another shader, which should create a new entry
230*8975f5c5SAndroid Build Coastguard Worker         program.makeRaster(kVertexShaderSrc2, kFragmentShaderSrc2);
231*8975f5c5SAndroid Build Coastguard Worker         ASSERT_TRUE(program.valid());
232*8975f5c5SAndroid Build Coastguard Worker         WaitProgramBinaryReady(program);
233*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
234*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
235*8975f5c5SAndroid Build Coastguard Worker 
236*8975f5c5SAndroid Build Coastguard Worker         // Compile the first shader again, which should still reside in the cache
237*8975f5c5SAndroid Build Coastguard Worker         program.makeRaster(kVertexShaderSrc, kFragmentShaderSrc);
238*8975f5c5SAndroid Build Coastguard Worker         ASSERT_TRUE(program.valid());
239*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::GetSuccess, gLastCacheOpResult);
240*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
241*8975f5c5SAndroid Build Coastguard Worker 
242*8975f5c5SAndroid Build Coastguard Worker         // Make sure deleting the program doesn't result in a binary save.  Regression test for a
243*8975f5c5SAndroid Build Coastguard Worker         // bug where the binary was re-cached after being loaded.
244*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(0);
245*8975f5c5SAndroid Build Coastguard Worker         program.reset();
246*8975f5c5SAndroid Build Coastguard Worker 
247*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::ValueNotSet, gLastCacheOpResult);
248*8975f5c5SAndroid Build Coastguard Worker     }
249*8975f5c5SAndroid Build Coastguard Worker }
250*8975f5c5SAndroid Build Coastguard Worker 
251*8975f5c5SAndroid Build Coastguard Worker // Makes sure the caching is always done without an explicit wait for post-link events (if any)
TEST_P(EGLBlobCacheTest,FunctionalWithoutWait)252*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, FunctionalWithoutWait)
253*8975f5c5SAndroid Build Coastguard Worker {
254*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
255*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
256*8975f5c5SAndroid Build Coastguard Worker 
257*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
258*8975f5c5SAndroid Build Coastguard Worker 
259*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
260*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
261*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
262*8975f5c5SAndroid Build Coastguard Worker 
263*8975f5c5SAndroid Build Coastguard Worker     constexpr char kVertexShaderSrc[] = R"(attribute vec4 aTest;
264*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
265*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
266*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest2;
267*8975f5c5SAndroid Build Coastguard Worker void main()
268*8975f5c5SAndroid Build Coastguard Worker {
269*8975f5c5SAndroid Build Coastguard Worker     vTest        = aTest;
270*8975f5c5SAndroid Build Coastguard Worker     vTest2       = aTest;
271*8975f5c5SAndroid Build Coastguard Worker     gl_Position  = vec4(aPosition, 1.0, 1.0);
272*8975f5c5SAndroid Build Coastguard Worker     gl_PointSize = 1.0;
273*8975f5c5SAndroid Build Coastguard Worker })";
274*8975f5c5SAndroid Build Coastguard Worker 
275*8975f5c5SAndroid Build Coastguard Worker     constexpr char kFragmentShaderSrc[] = R"(precision mediump float;
276*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
277*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest2;
278*8975f5c5SAndroid Build Coastguard Worker void main()
279*8975f5c5SAndroid Build Coastguard Worker {
280*8975f5c5SAndroid Build Coastguard Worker     gl_FragColor = vTest + vTest2 - vec4(0.0, 1.0, 0.0, 0.0);
281*8975f5c5SAndroid Build Coastguard Worker })";
282*8975f5c5SAndroid Build Coastguard Worker 
283*8975f5c5SAndroid Build Coastguard Worker     if (programBinaryAvailable())
284*8975f5c5SAndroid Build Coastguard Worker     {
285*8975f5c5SAndroid Build Coastguard Worker         // Make the conditions ideal for Vulkan's warm up task to match the draw call.
286*8975f5c5SAndroid Build Coastguard Worker         constexpr uint32_t kSize = 1;
287*8975f5c5SAndroid Build Coastguard Worker         GLTexture color;
288*8975f5c5SAndroid Build Coastguard Worker         glBindTexture(GL_TEXTURE_2D, color);
289*8975f5c5SAndroid Build Coastguard Worker         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
290*8975f5c5SAndroid Build Coastguard Worker                      nullptr);
291*8975f5c5SAndroid Build Coastguard Worker 
292*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
293*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
294*8975f5c5SAndroid Build Coastguard Worker         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
295*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
296*8975f5c5SAndroid Build Coastguard Worker 
297*8975f5c5SAndroid Build Coastguard Worker         ANGLE_GL_PROGRAM(program, kVertexShaderSrc, kFragmentShaderSrc);
298*8975f5c5SAndroid Build Coastguard Worker 
299*8975f5c5SAndroid Build Coastguard Worker         // First, draw with the program.  In the Vulkan backend, this can lead to a wait on the warm
300*8975f5c5SAndroid Build Coastguard Worker         // up task since the description matches the one needed for the draw.
301*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(program);
302*8975f5c5SAndroid Build Coastguard Worker         glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
303*8975f5c5SAndroid Build Coastguard Worker 
304*8975f5c5SAndroid Build Coastguard Worker         // Delete the program to make sure caching the binary can no longer be delayed.
305*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(0);
306*8975f5c5SAndroid Build Coastguard Worker         program.reset();
307*8975f5c5SAndroid Build Coastguard Worker 
308*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
309*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
310*8975f5c5SAndroid Build Coastguard Worker     }
311*8975f5c5SAndroid Build Coastguard Worker }
312*8975f5c5SAndroid Build Coastguard Worker 
313*8975f5c5SAndroid Build Coastguard Worker // Tests error conditions of the APIs.
TEST_P(EGLBlobCacheTest,NegativeAPI)314*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, NegativeAPI)
315*8975f5c5SAndroid Build Coastguard Worker {
316*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
317*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
318*8975f5c5SAndroid Build Coastguard Worker 
319*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
320*8975f5c5SAndroid Build Coastguard Worker 
321*8975f5c5SAndroid Build Coastguard Worker     // Test bad display
322*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(EGL_NO_DISPLAY, nullptr, nullptr);
323*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
324*8975f5c5SAndroid Build Coastguard Worker 
325*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(EGL_NO_DISPLAY, SetBlob, GetBlob);
326*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
327*8975f5c5SAndroid Build Coastguard Worker 
328*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
329*8975f5c5SAndroid Build Coastguard Worker 
330*8975f5c5SAndroid Build Coastguard Worker     // Test bad arguments
331*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, nullptr, nullptr);
332*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
333*8975f5c5SAndroid Build Coastguard Worker 
334*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, nullptr);
335*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
336*8975f5c5SAndroid Build Coastguard Worker 
337*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, nullptr, GetBlob);
338*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
339*8975f5c5SAndroid Build Coastguard Worker 
340*8975f5c5SAndroid Build Coastguard Worker     // Set the arguments once and test setting them again (which should fail)
341*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
342*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
343*8975f5c5SAndroid Build Coastguard Worker 
344*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
345*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
346*8975f5c5SAndroid Build Coastguard Worker 
347*8975f5c5SAndroid Build Coastguard Worker     // Try again with bad parameters
348*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(EGL_NO_DISPLAY, nullptr, nullptr);
349*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
350*8975f5c5SAndroid Build Coastguard Worker 
351*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, nullptr, nullptr);
352*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
353*8975f5c5SAndroid Build Coastguard Worker 
354*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, nullptr);
355*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
356*8975f5c5SAndroid Build Coastguard Worker 
357*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, nullptr, GetBlob);
358*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
359*8975f5c5SAndroid Build Coastguard Worker }
360*8975f5c5SAndroid Build Coastguard Worker 
361*8975f5c5SAndroid Build Coastguard Worker // Regression test for including the fragment output locatins in the program key.
362*8975f5c5SAndroid Build Coastguard Worker // http://anglebug.com/42263144
TEST_P(EGLBlobCacheTest,FragmentOutputLocationKey)363*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, FragmentOutputLocationKey)
364*8975f5c5SAndroid Build Coastguard Worker {
365*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
366*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
367*8975f5c5SAndroid Build Coastguard Worker 
368*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!EnsureGLExtensionEnabled("GL_EXT_blend_func_extended") ||
369*8975f5c5SAndroid Build Coastguard Worker                        getClientMajorVersion() < 3);
370*8975f5c5SAndroid Build Coastguard Worker 
371*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
372*8975f5c5SAndroid Build Coastguard Worker 
373*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
374*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
375*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
376*8975f5c5SAndroid Build Coastguard Worker 
377*8975f5c5SAndroid Build Coastguard Worker     // Compile a shader so it puts something in the cache
378*8975f5c5SAndroid Build Coastguard Worker     if (programBinaryAvailable())
379*8975f5c5SAndroid Build Coastguard Worker     {
380*8975f5c5SAndroid Build Coastguard Worker         glEnable(GL_SCISSOR_TEST);
381*8975f5c5SAndroid Build Coastguard Worker         glScissor(0, 0, 1, 1);
382*8975f5c5SAndroid Build Coastguard Worker 
383*8975f5c5SAndroid Build Coastguard Worker         constexpr char kFragmentShaderSrc[] = R"(#version 300 es
384*8975f5c5SAndroid Build Coastguard Worker #extension GL_EXT_blend_func_extended : require
385*8975f5c5SAndroid Build Coastguard Worker precision mediump float;
386*8975f5c5SAndroid Build Coastguard Worker uniform vec4 src;
387*8975f5c5SAndroid Build Coastguard Worker uniform vec4 src1;
388*8975f5c5SAndroid Build Coastguard Worker out vec4 FragData;
389*8975f5c5SAndroid Build Coastguard Worker out vec4 SecondaryFragData;
390*8975f5c5SAndroid Build Coastguard Worker void main() {
391*8975f5c5SAndroid Build Coastguard Worker     FragData = src;
392*8975f5c5SAndroid Build Coastguard Worker     SecondaryFragData = src1;
393*8975f5c5SAndroid Build Coastguard Worker })";
394*8975f5c5SAndroid Build Coastguard Worker 
395*8975f5c5SAndroid Build Coastguard Worker         constexpr char kVertexShaderSrc[] = R"(#version 300 es
396*8975f5c5SAndroid Build Coastguard Worker in vec4 position;
397*8975f5c5SAndroid Build Coastguard Worker void main() {
398*8975f5c5SAndroid Build Coastguard Worker     gl_Position = position;
399*8975f5c5SAndroid Build Coastguard Worker })";
400*8975f5c5SAndroid Build Coastguard Worker 
401*8975f5c5SAndroid Build Coastguard Worker         GLuint program = CompileProgram(kVertexShaderSrc, kFragmentShaderSrc, [](GLuint p) {
402*8975f5c5SAndroid Build Coastguard Worker             glBindFragDataLocationEXT(p, 0, "FragData[0]");
403*8975f5c5SAndroid Build Coastguard Worker             glBindFragDataLocationIndexedEXT(p, 0, 1, "SecondaryFragData[0]");
404*8975f5c5SAndroid Build Coastguard Worker         });
405*8975f5c5SAndroid Build Coastguard Worker         ASSERT_NE(0u, program);
406*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(program);
407*8975f5c5SAndroid Build Coastguard Worker         glDrawArrays(GL_TRIANGLES, 0, 3);
408*8975f5c5SAndroid Build Coastguard Worker         WaitProgramBinaryReady(program);
409*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
410*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
411*8975f5c5SAndroid Build Coastguard Worker 
412*8975f5c5SAndroid Build Coastguard Worker         // Re-link the program with different fragment output bindings
413*8975f5c5SAndroid Build Coastguard Worker         program = CompileProgram(kVertexShaderSrc, kFragmentShaderSrc, [](GLuint p) {
414*8975f5c5SAndroid Build Coastguard Worker             glBindFragDataLocationEXT(p, 0, "FragData");
415*8975f5c5SAndroid Build Coastguard Worker             glBindFragDataLocationIndexedEXT(p, 0, 1, "SecondaryFragData");
416*8975f5c5SAndroid Build Coastguard Worker         });
417*8975f5c5SAndroid Build Coastguard Worker         ASSERT_NE(0u, program);
418*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(program);
419*8975f5c5SAndroid Build Coastguard Worker         glDrawArrays(GL_TRIANGLES, 0, 3);
420*8975f5c5SAndroid Build Coastguard Worker         WaitProgramBinaryReady(program);
421*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
422*8975f5c5SAndroid Build Coastguard Worker         gLastCacheOpResult = CacheOpResult::ValueNotSet;
423*8975f5c5SAndroid Build Coastguard Worker     }
424*8975f5c5SAndroid Build Coastguard Worker }
425*8975f5c5SAndroid Build Coastguard Worker 
426*8975f5c5SAndroid Build Coastguard Worker // Checks that the shader cache, which is used when this extension is available, is working
427*8975f5c5SAndroid Build Coastguard Worker // properly.
TEST_P(EGLBlobCacheTest,ShaderCacheFunctional)428*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, ShaderCacheFunctional)
429*8975f5c5SAndroid Build Coastguard Worker {
430*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
431*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
432*8975f5c5SAndroid Build Coastguard Worker 
433*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsVulkan());
434*8975f5c5SAndroid Build Coastguard Worker 
435*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
436*8975f5c5SAndroid Build Coastguard Worker 
437*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
438*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
439*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
440*8975f5c5SAndroid Build Coastguard Worker 
441*8975f5c5SAndroid Build Coastguard Worker     constexpr char kVertexShaderSrc[] = R"(attribute vec4 aTest;
442*8975f5c5SAndroid Build Coastguard Worker attribute vec2 aPosition;
443*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
444*8975f5c5SAndroid Build Coastguard Worker void main()
445*8975f5c5SAndroid Build Coastguard Worker {
446*8975f5c5SAndroid Build Coastguard Worker     vTest        = aTest;
447*8975f5c5SAndroid Build Coastguard Worker     gl_Position  = vec4(aPosition, 0.0, 1.0);
448*8975f5c5SAndroid Build Coastguard Worker     gl_PointSize = 1.0;
449*8975f5c5SAndroid Build Coastguard Worker })";
450*8975f5c5SAndroid Build Coastguard Worker 
451*8975f5c5SAndroid Build Coastguard Worker     constexpr char kFragmentShaderSrc[] = R"(precision mediump float;
452*8975f5c5SAndroid Build Coastguard Worker varying vec4 vTest;
453*8975f5c5SAndroid Build Coastguard Worker void main()
454*8975f5c5SAndroid Build Coastguard Worker {
455*8975f5c5SAndroid Build Coastguard Worker     gl_FragColor = vTest;
456*8975f5c5SAndroid Build Coastguard Worker })";
457*8975f5c5SAndroid Build Coastguard Worker 
458*8975f5c5SAndroid Build Coastguard Worker     // Compile a shader so it puts something in the cache
459*8975f5c5SAndroid Build Coastguard Worker     GLuint shaderID = CompileShader(GL_VERTEX_SHADER, kVertexShaderSrc);
460*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
461*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
462*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
463*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
464*8975f5c5SAndroid Build Coastguard Worker 
465*8975f5c5SAndroid Build Coastguard Worker     // Compile the same shader again, so it would try to retrieve it from the cache
466*8975f5c5SAndroid Build Coastguard Worker     shaderID = CompileShader(GL_VERTEX_SHADER, kVertexShaderSrc);
467*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
468*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::GetSuccess, gLastCacheOpResult);
469*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
470*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
471*8975f5c5SAndroid Build Coastguard Worker 
472*8975f5c5SAndroid Build Coastguard Worker     // Compile another shader, which should create a new entry
473*8975f5c5SAndroid Build Coastguard Worker     shaderID = CompileShader(GL_FRAGMENT_SHADER, kFragmentShaderSrc);
474*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
475*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
476*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
477*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
478*8975f5c5SAndroid Build Coastguard Worker 
479*8975f5c5SAndroid Build Coastguard Worker     // Compile the first shader again, which should still reside in the cache
480*8975f5c5SAndroid Build Coastguard Worker     shaderID = CompileShader(GL_VERTEX_SHADER, kVertexShaderSrc);
481*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
482*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::GetSuccess, gLastCacheOpResult);
483*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
484*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
485*8975f5c5SAndroid Build Coastguard Worker }
486*8975f5c5SAndroid Build Coastguard Worker 
487*8975f5c5SAndroid Build Coastguard Worker // Tests compiling a program in multiple threads, then fetching the compiled program/shaders from
488*8975f5c5SAndroid Build Coastguard Worker // the cache. We then perform a draw call and test the result to ensure nothing was corrupted.
TEST_P(EGLBlobCacheTest,ThreadSafety)489*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, ThreadSafety)
490*8975f5c5SAndroid Build Coastguard Worker {
491*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
492*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
493*8975f5c5SAndroid Build Coastguard Worker 
494*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsVulkan());
495*8975f5c5SAndroid Build Coastguard Worker 
496*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
497*8975f5c5SAndroid Build Coastguard Worker 
498*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
499*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
500*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
501*8975f5c5SAndroid Build Coastguard Worker 
502*8975f5c5SAndroid Build Coastguard Worker     auto threadFunc = [&](int threadID, EGLDisplay dpy, EGLSurface surface, EGLContext context) {
503*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface, surface, context));
504*8975f5c5SAndroid Build Coastguard Worker 
505*8975f5c5SAndroid Build Coastguard Worker         ANGLE_GL_PROGRAM(unusedProgramTemp1, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
506*8975f5c5SAndroid Build Coastguard Worker 
507*8975f5c5SAndroid Build Coastguard Worker         // Insert a new entry into the cache unique to this thread.
508*8975f5c5SAndroid Build Coastguard Worker         std::stringstream ss;
509*8975f5c5SAndroid Build Coastguard Worker         ss << essl1_shaders::vs::Simple() << "//" << threadID;
510*8975f5c5SAndroid Build Coastguard Worker         std::string newEntryVSSource = ss.str().c_str();
511*8975f5c5SAndroid Build Coastguard Worker         ANGLE_GL_PROGRAM(unusedProgramTemp2, newEntryVSSource.c_str(), essl1_shaders::fs::Red());
512*8975f5c5SAndroid Build Coastguard Worker 
513*8975f5c5SAndroid Build Coastguard Worker         // Clean up
514*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
515*8975f5c5SAndroid Build Coastguard Worker     };
516*8975f5c5SAndroid Build Coastguard Worker 
517*8975f5c5SAndroid Build Coastguard Worker     constexpr int kNumThreads = 32;
518*8975f5c5SAndroid Build Coastguard Worker 
519*8975f5c5SAndroid Build Coastguard Worker     std::vector<LockStepThreadFunc> threadFuncs(kNumThreads);
520*8975f5c5SAndroid Build Coastguard Worker     for (int i = 0; i < kNumThreads; ++i)
521*8975f5c5SAndroid Build Coastguard Worker     {
522*8975f5c5SAndroid Build Coastguard Worker         threadFuncs[i] = [=](EGLDisplay dpy, EGLSurface surface, EGLContext context) {
523*8975f5c5SAndroid Build Coastguard Worker             return threadFunc(i, dpy, surface, context);
524*8975f5c5SAndroid Build Coastguard Worker         };
525*8975f5c5SAndroid Build Coastguard Worker     }
526*8975f5c5SAndroid Build Coastguard Worker 
527*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
528*8975f5c5SAndroid Build Coastguard Worker 
529*8975f5c5SAndroid Build Coastguard Worker     RunLockStepThreads(getEGLWindow(), threadFuncs.size(), threadFuncs.data());
530*8975f5c5SAndroid Build Coastguard Worker 
531*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
532*8975f5c5SAndroid Build Coastguard Worker 
533*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
534*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::GetSuccess, gLastCacheOpResult);
535*8975f5c5SAndroid Build Coastguard Worker 
536*8975f5c5SAndroid Build Coastguard Worker     drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
537*8975f5c5SAndroid Build Coastguard Worker 
538*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
539*8975f5c5SAndroid Build Coastguard Worker }
540*8975f5c5SAndroid Build Coastguard Worker 
541*8975f5c5SAndroid Build Coastguard Worker // Makes sure ANGLE recovers from corrupted cache.
TEST_P(EGLBlobCacheTest,CacheCorruption)542*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheTest, CacheCorruption)
543*8975f5c5SAndroid Build Coastguard Worker {
544*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!getEGLWindow()->isFeatureEnabled(Feature::CacheCompiledShader));
545*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(getEGLWindow()->isFeatureEnabled(Feature::DisableProgramCaching));
546*8975f5c5SAndroid Build Coastguard Worker 
547*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
548*8975f5c5SAndroid Build Coastguard Worker 
549*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
550*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetCorruptedBlob, GetBlob);
551*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
552*8975f5c5SAndroid Build Coastguard Worker 
553*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!programBinaryAvailable());
554*8975f5c5SAndroid Build Coastguard Worker 
555*8975f5c5SAndroid Build Coastguard Worker     // Compile the program once and draw with it
556*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
557*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
558*8975f5c5SAndroid Build Coastguard Worker 
559*8975f5c5SAndroid Build Coastguard Worker     const GLint colorUniformLocation =
560*8975f5c5SAndroid Build Coastguard Worker         glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
561*8975f5c5SAndroid Build Coastguard Worker     ASSERT_NE(colorUniformLocation, -1);
562*8975f5c5SAndroid Build Coastguard Worker 
563*8975f5c5SAndroid Build Coastguard Worker     glUniform4f(colorUniformLocation, 1, 0, 0, 1);
564*8975f5c5SAndroid Build Coastguard Worker     drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
565*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
566*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
567*8975f5c5SAndroid Build Coastguard Worker 
568*8975f5c5SAndroid Build Coastguard Worker     WaitProgramBinaryReady(program);
569*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
570*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
571*8975f5c5SAndroid Build Coastguard Worker 
572*8975f5c5SAndroid Build Coastguard Worker     // Compile/link the same program again, so it would try to retrieve it from the cache.  GetBlob
573*8975f5c5SAndroid Build Coastguard Worker     // should return success, but because the cache is corrupt, ANGLE should redo the compile/link
574*8975f5c5SAndroid Build Coastguard Worker     // and set the blob again.
575*8975f5c5SAndroid Build Coastguard Worker     program.makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
576*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(program.valid());
577*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
578*8975f5c5SAndroid Build Coastguard Worker 
579*8975f5c5SAndroid Build Coastguard Worker     glUniform4f(colorUniformLocation, 0, 1, 0, 1);
580*8975f5c5SAndroid Build Coastguard Worker     drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
581*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
582*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
583*8975f5c5SAndroid Build Coastguard Worker 
584*8975f5c5SAndroid Build Coastguard Worker     WaitProgramBinaryReady(program);
585*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
586*8975f5c5SAndroid Build Coastguard Worker }
587*8975f5c5SAndroid Build Coastguard Worker 
588*8975f5c5SAndroid Build Coastguard Worker class EGLBlobCacheInternalRejectionTest : public EGLBlobCacheTest
589*8975f5c5SAndroid Build Coastguard Worker {};
590*8975f5c5SAndroid Build Coastguard Worker 
591*8975f5c5SAndroid Build Coastguard Worker // Makes sure ANGLE recovers from internal (backend) rejection of the program blob, while everything
592*8975f5c5SAndroid Build Coastguard Worker // seems fine to ANGLE.
TEST_P(EGLBlobCacheInternalRejectionTest,Functional)593*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheInternalRejectionTest, Functional)
594*8975f5c5SAndroid Build Coastguard Worker {
595*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
596*8975f5c5SAndroid Build Coastguard Worker 
597*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
598*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
599*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
600*8975f5c5SAndroid Build Coastguard Worker 
601*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!programBinaryAvailable());
602*8975f5c5SAndroid Build Coastguard Worker 
603*8975f5c5SAndroid Build Coastguard Worker     // Compile the program once and draw with it
604*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
605*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
606*8975f5c5SAndroid Build Coastguard Worker 
607*8975f5c5SAndroid Build Coastguard Worker     const GLint colorUniformLocation =
608*8975f5c5SAndroid Build Coastguard Worker         glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
609*8975f5c5SAndroid Build Coastguard Worker     ASSERT_NE(colorUniformLocation, -1);
610*8975f5c5SAndroid Build Coastguard Worker 
611*8975f5c5SAndroid Build Coastguard Worker     glUniform4f(colorUniformLocation, 1, 0, 0, 1);
612*8975f5c5SAndroid Build Coastguard Worker     drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
613*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
614*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
615*8975f5c5SAndroid Build Coastguard Worker 
616*8975f5c5SAndroid Build Coastguard Worker     WaitProgramBinaryReady(program);
617*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
618*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
619*8975f5c5SAndroid Build Coastguard Worker 
620*8975f5c5SAndroid Build Coastguard Worker     // Compile/link the same program again, so it would try to retrieve it from the cache.  GetBlob
621*8975f5c5SAndroid Build Coastguard Worker     // should return success, and ANGLE would think the program is fine.  After ANGLE internal
622*8975f5c5SAndroid Build Coastguard Worker     // updates, the backend should reject the program binary, at which point ANGLE should redo the
623*8975f5c5SAndroid Build Coastguard Worker     // compile/link and set the blob again.
624*8975f5c5SAndroid Build Coastguard Worker     program.makeRaster(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
625*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(program.valid());
626*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
627*8975f5c5SAndroid Build Coastguard Worker 
628*8975f5c5SAndroid Build Coastguard Worker     glUniform4f(colorUniformLocation, 0, 1, 0, 1);
629*8975f5c5SAndroid Build Coastguard Worker     drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
630*8975f5c5SAndroid Build Coastguard Worker     EXPECT_GL_NO_ERROR();
631*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
632*8975f5c5SAndroid Build Coastguard Worker 
633*8975f5c5SAndroid Build Coastguard Worker     WaitProgramBinaryReady(program);
634*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
635*8975f5c5SAndroid Build Coastguard Worker }
636*8975f5c5SAndroid Build Coastguard Worker 
637*8975f5c5SAndroid Build Coastguard Worker // Makes sure ANGLE recovers from internal (backend) rejection of the shader blob, while everything
638*8975f5c5SAndroid Build Coastguard Worker // seems fine to ANGLE.
TEST_P(EGLBlobCacheInternalRejectionTest,ShaderCacheFunctional)639*8975f5c5SAndroid Build Coastguard Worker TEST_P(EGLBlobCacheInternalRejectionTest, ShaderCacheFunctional)
640*8975f5c5SAndroid Build Coastguard Worker {
641*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsVulkan());
642*8975f5c5SAndroid Build Coastguard Worker 
643*8975f5c5SAndroid Build Coastguard Worker     EGLDisplay display = getEGLWindow()->getDisplay();
644*8975f5c5SAndroid Build Coastguard Worker 
645*8975f5c5SAndroid Build Coastguard Worker     EXPECT_TRUE(mHasBlobCache);
646*8975f5c5SAndroid Build Coastguard Worker     eglSetBlobCacheFuncsANDROID(display, SetBlob, GetBlob);
647*8975f5c5SAndroid Build Coastguard Worker     ASSERT_EGL_SUCCESS();
648*8975f5c5SAndroid Build Coastguard Worker 
649*8975f5c5SAndroid Build Coastguard Worker     // Compile a shader so it puts something in the cache
650*8975f5c5SAndroid Build Coastguard Worker     GLuint shaderID = CompileShader(GL_VERTEX_SHADER, essl1_shaders::vs::Simple());
651*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
652*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
653*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
654*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
655*8975f5c5SAndroid Build Coastguard Worker 
656*8975f5c5SAndroid Build Coastguard Worker     // Compile another shader, which should create a new entry
657*8975f5c5SAndroid Build Coastguard Worker     shaderID = CompileShader(GL_FRAGMENT_SHADER, essl1_shaders::fs::UniformColor());
658*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
659*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
660*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
661*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
662*8975f5c5SAndroid Build Coastguard Worker 
663*8975f5c5SAndroid Build Coastguard Worker     // Compile the first shader again, which should still reside in the cache, but is corrupted.
664*8975f5c5SAndroid Build Coastguard Worker     // The cached entry should be discarded and compilation performed again (which sets another
665*8975f5c5SAndroid Build Coastguard Worker     // entry in the cache).
666*8975f5c5SAndroid Build Coastguard Worker     shaderID = CompileShader(GL_VERTEX_SHADER, essl1_shaders::vs::Simple());
667*8975f5c5SAndroid Build Coastguard Worker     ASSERT_TRUE(shaderID != 0);
668*8975f5c5SAndroid Build Coastguard Worker     EXPECT_EQ(CacheOpResult::SetSuccess, gLastCacheOpResult);
669*8975f5c5SAndroid Build Coastguard Worker     gLastCacheOpResult = CacheOpResult::ValueNotSet;
670*8975f5c5SAndroid Build Coastguard Worker     glDeleteShader(shaderID);
671*8975f5c5SAndroid Build Coastguard Worker }
672*8975f5c5SAndroid Build Coastguard Worker 
673*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST(EGLBlobCacheTest,
674*8975f5c5SAndroid Build Coastguard Worker                        ES2_D3D9(),
675*8975f5c5SAndroid Build Coastguard Worker                        ES2_D3D11(),
676*8975f5c5SAndroid Build Coastguard Worker                        ES3_D3D11(),
677*8975f5c5SAndroid Build Coastguard Worker                        ES2_OPENGL(),
678*8975f5c5SAndroid Build Coastguard Worker                        ES3_OPENGL(),
679*8975f5c5SAndroid Build Coastguard Worker                        ES3_OPENGLES(),
680*8975f5c5SAndroid Build Coastguard Worker                        ES2_OPENGLES(),
681*8975f5c5SAndroid Build Coastguard Worker                        ES2_METAL(),
682*8975f5c5SAndroid Build Coastguard Worker                        ES3_METAL(),
683*8975f5c5SAndroid Build Coastguard Worker                        // Note: For the Vulkan backend, disable reads and writes for the global
684*8975f5c5SAndroid Build Coastguard Worker                        // pipeline cache, so it does not interfere with the test's expectations of
685*8975f5c5SAndroid Build Coastguard Worker                        // when the cache should and shouldn't be hit.
686*8975f5c5SAndroid Build Coastguard Worker                        ES2_VULKAN()
687*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
688*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache),
689*8975f5c5SAndroid Build Coastguard Worker                        ES3_VULKAN_SWIFTSHADER()
690*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
691*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache),
692*8975f5c5SAndroid Build Coastguard Worker                        ES3_VULKAN()
693*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::AsyncCommandQueue)
694*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
695*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache),
696*8975f5c5SAndroid Build Coastguard Worker                        ES2_VULKAN_SWIFTSHADER()
697*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::AsyncCommandQueue)
698*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
699*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache),
700*8975f5c5SAndroid Build Coastguard Worker                        ES2_VULKAN_SWIFTSHADER()
701*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::EnableParallelCompileAndLink)
702*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
703*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache),
704*8975f5c5SAndroid Build Coastguard Worker                        ES3_VULKAN()
705*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::EnableParallelCompileAndLink)
706*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
707*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache),
708*8975f5c5SAndroid Build Coastguard Worker                        ES2_VULKAN_SWIFTSHADER()
709*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::EnableParallelCompileAndLink)
710*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::AsyncCommandQueue)
711*8975f5c5SAndroid Build Coastguard Worker                            .enable(Feature::DisablePipelineCacheLoadForTesting)
712*8975f5c5SAndroid Build Coastguard Worker                            .disable(Feature::SyncMonolithicPipelinesToBlobCache));
713*8975f5c5SAndroid Build Coastguard Worker 
714*8975f5c5SAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLBlobCacheInternalRejectionTest);
715*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST(EGLBlobCacheInternalRejectionTest,
716*8975f5c5SAndroid Build Coastguard Worker                        ES2_OPENGL().enable(Feature::CorruptProgramBinaryForTesting),
717*8975f5c5SAndroid Build Coastguard Worker                        ES2_OPENGLES().enable(Feature::CorruptProgramBinaryForTesting));
718