xref: /aosp_15_r20/external/angle/src/tests/egl_tests/EGLProgramCacheControlTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2017 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // EGLProgramCacheControlTest:
7 //   Unit tests for the EGL_ANGLE_program_cache_control extension.
8 
9 #include "common/angleutils.h"
10 #include "test_utils/ANGLETest.h"
11 #include "test_utils/gl_raii.h"
12 #include "util/EGLWindow.h"
13 
14 using namespace angle;
15 
16 constexpr EGLint kEnabledCacheSize = 0x10000;
17 constexpr char kEGLExtName[]       = "EGL_ANGLE_program_cache_control";
18 
19 void TestCacheProgram(PlatformMethods *platform,
20                       const ProgramKeyType &key,
21                       size_t programSize,
22                       const uint8_t *programBytes);
23 
24 class EGLProgramCacheControlTest : public ANGLETest<>
25 {
26   public:
onCache(const ProgramKeyType & key,size_t programSize,const uint8_t * programBytes)27     void onCache(const ProgramKeyType &key, size_t programSize, const uint8_t *programBytes)
28     {
29         mCachedKey = key;
30         mCachedBinary.assign(&programBytes[0], &programBytes[programSize]);
31     }
32 
33   protected:
EGLProgramCacheControlTest()34     EGLProgramCacheControlTest()
35     {
36         // Test flakiness was noticed when reusing displays.
37         forceNewDisplay();
38         setDeferContextInit(true);
39         setContextProgramCacheEnabled(true);
40         gDefaultPlatformMethods.cacheProgram = TestCacheProgram;
41     }
42 
testSetUp()43     void testSetUp() override
44     {
45         if (extensionAvailable())
46         {
47             EGLDisplay display = getEGLWindow()->getDisplay();
48             eglProgramCacheResizeANGLE(display, kEnabledCacheSize, EGL_PROGRAM_CACHE_RESIZE_ANGLE);
49             ASSERT_EGL_SUCCESS();
50         }
51 
52         ASSERT_TRUE(getEGLWindow()->initializeContext());
53     }
54 
testTearDown()55     void testTearDown() override { gDefaultPlatformMethods.cacheProgram = DefaultCacheProgram; }
56 
extensionAvailable()57     bool extensionAvailable()
58     {
59         EGLDisplay display = getEGLWindow()->getDisplay();
60         return IsEGLDisplayExtensionEnabled(display, kEGLExtName);
61     }
62 
programBinaryAvailable()63     bool programBinaryAvailable()
64     {
65         return (getClientMajorVersion() >= 3 || IsGLExtensionEnabled("GL_OES_get_program_binary"));
66     }
67 
68     ProgramKeyType mCachedKey;
69     std::vector<uint8_t> mCachedBinary;
70 };
71 
TestCacheProgram(PlatformMethods * platform,const ProgramKeyType & key,size_t programSize,const uint8_t * programBytes)72 void TestCacheProgram(PlatformMethods *platform,
73                       const ProgramKeyType &key,
74                       size_t programSize,
75                       const uint8_t *programBytes)
76 {
77     auto *testPlatformContext = static_cast<TestPlatformContext *>(platform->context);
78     auto *testCase =
79         reinterpret_cast<EGLProgramCacheControlTest *>(testPlatformContext->currentTest);
80     testCase->onCache(key, programSize, programBytes);
81 }
82 
83 // Tests error conditions of the APIs.
TEST_P(EGLProgramCacheControlTest,NegativeAPI)84 TEST_P(EGLProgramCacheControlTest, NegativeAPI)
85 {
86     ANGLE_SKIP_TEST_IF(!extensionAvailable());
87 
88     constexpr char kDefaultKey[]        = "defaultMakeItLongEnough";
89     constexpr char kDefaultBinary[]     = "defaultMakeItLongEnough";
90     constexpr EGLint kDefaultKeySize    = static_cast<EGLint>(ArraySize(kDefaultKey));
91     constexpr EGLint kDefaultBinarySize = static_cast<EGLint>(ArraySize(kDefaultBinary));
92 
93     // Test that passing an invalid display to the entry point methods fails.
94     eglProgramCacheGetAttribANGLE(EGL_NO_DISPLAY, EGL_PROGRAM_CACHE_KEY_LENGTH_ANGLE);
95     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
96 
97     eglProgramCachePopulateANGLE(EGL_NO_DISPLAY, kDefaultKey, kDefaultKeySize, kDefaultBinary,
98                                  kDefaultBinarySize);
99     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
100 
101     EGLint tempKeySize    = 0;
102     EGLint tempBinarySize = 0;
103     eglProgramCacheQueryANGLE(EGL_NO_DISPLAY, 0, nullptr, &tempKeySize, nullptr, &tempBinarySize);
104     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
105 
106     eglProgramCacheResizeANGLE(EGL_NO_DISPLAY, 0, EGL_PROGRAM_CACHE_TRIM_ANGLE);
107     EXPECT_EGL_ERROR(EGL_BAD_DISPLAY);
108 
109     // Test querying properties with bad parameters.
110     EGLDisplay display = getEGLWindow()->getDisplay();
111     eglProgramCacheGetAttribANGLE(display, EGL_PROGRAM_CACHE_RESIZE_ANGLE);
112     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
113 
114     // Test populating with invalid parameters.
115     EGLint keySize = eglProgramCacheGetAttribANGLE(display, EGL_PROGRAM_CACHE_KEY_LENGTH_ANGLE);
116     EXPECT_GT(kDefaultKeySize, keySize);
117     eglProgramCachePopulateANGLE(display, kDefaultKey, keySize + 1, kDefaultBinary,
118                                  kDefaultBinarySize);
119     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
120 
121     eglProgramCachePopulateANGLE(display, kDefaultKey, keySize, kDefaultBinary, -1);
122     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
123 
124     eglProgramCachePopulateANGLE(display, nullptr, keySize, kDefaultBinary, kDefaultBinarySize);
125     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
126 
127     eglProgramCachePopulateANGLE(display, kDefaultKey, keySize, nullptr, kDefaultBinarySize);
128     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
129 
130     // Test querying cache entries with invalid parameters.
131     eglProgramCachePopulateANGLE(display, kDefaultKey, keySize, kDefaultBinary, kDefaultBinarySize);
132     ASSERT_EGL_SUCCESS();
133 
134     EGLint cacheSize = eglProgramCacheGetAttribANGLE(display, EGL_PROGRAM_CACHE_SIZE_ANGLE);
135     ASSERT_EQ(1, cacheSize);
136 
137     eglProgramCacheQueryANGLE(display, -1, nullptr, &tempKeySize, nullptr, &tempBinarySize);
138     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
139 
140     eglProgramCacheQueryANGLE(display, 1, nullptr, &tempKeySize, nullptr, &tempBinarySize);
141     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
142 
143     eglProgramCacheQueryANGLE(display, 0, nullptr, nullptr, nullptr, &tempBinarySize);
144     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
145 
146     eglProgramCacheQueryANGLE(display, 0, nullptr, &tempKeySize, nullptr, nullptr);
147     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
148 
149     eglProgramCacheQueryANGLE(display, 0, nullptr, &tempKeySize, nullptr, &tempBinarySize);
150     ASSERT_EGL_SUCCESS();
151     ASSERT_EQ(keySize, tempKeySize);
152     ASSERT_EQ(kDefaultBinarySize, tempBinarySize);
153 
154     std::vector<uint8_t> tempKey(tempKeySize + 5);
155     std::vector<uint8_t> tempBinary(tempBinarySize + 5);
156 
157     tempKeySize--;
158 
159     eglProgramCacheQueryANGLE(display, 0, tempKey.data(), &tempKeySize, tempBinary.data(),
160                               &tempBinarySize);
161     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
162 
163     tempKeySize++;
164     tempBinarySize--;
165 
166     eglProgramCacheQueryANGLE(display, 0, tempKey.data(), &tempKeySize, tempBinary.data(),
167                               &tempBinarySize);
168     EXPECT_EGL_ERROR(EGL_BAD_ACCESS);
169 
170     // Test resizing with invalid parameters.
171     eglProgramCacheResizeANGLE(display, -1, EGL_PROGRAM_CACHE_TRIM_ANGLE);
172     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
173 
174     eglProgramCacheResizeANGLE(display, 0, EGL_PROGRAM_CACHE_KEY_LENGTH_ANGLE);
175     EXPECT_EGL_ERROR(EGL_BAD_PARAMETER);
176 }
177 
178 // Tests a basic use case.
TEST_P(EGLProgramCacheControlTest,SaveAndReload)179 TEST_P(EGLProgramCacheControlTest, SaveAndReload)
180 {
181     ANGLE_SKIP_TEST_IF(!extensionAvailable() || !programBinaryAvailable());
182 
183     constexpr char kVS[] = "attribute vec4 position; void main() { gl_Position = position; }";
184     constexpr char kFS[] = "void main() { gl_FragColor = vec4(1, 0, 0, 1); }";
185 
186     mCachedBinary.clear();
187     // Link a program, which will miss the cache.
188     {
189         glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
190         glClear(GL_COLOR_BUFFER_BIT);
191         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
192 
193         ANGLE_GL_PROGRAM(program, kVS, kFS);
194         drawQuad(program, "position", 0.5f);
195         EXPECT_GL_NO_ERROR();
196         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
197     }
198     // Assert that the cache insertion added a program to the cache.
199     EXPECT_TRUE(!mCachedBinary.empty());
200 
201     EGLDisplay display = getEGLWindow()->getDisplay();
202 
203     EGLint keySize    = 0;
204     EGLint binarySize = 0;
205     eglProgramCacheQueryANGLE(display, 0, nullptr, &keySize, nullptr, &binarySize);
206     EXPECT_EQ(static_cast<EGLint>(mCachedKey.size()), keySize);
207     ASSERT_EGL_SUCCESS();
208 
209     ProgramKeyType keyBuffer;
210     std::vector<uint8_t> binaryBuffer(binarySize);
211     eglProgramCacheQueryANGLE(display, 0, keyBuffer.data(), &keySize, binaryBuffer.data(),
212                               &binarySize);
213     ASSERT_EGL_SUCCESS();
214 
215     EXPECT_EQ(mCachedKey, keyBuffer);
216     EXPECT_EQ(mCachedBinary, binaryBuffer);
217 
218     // Restart EGL and GL.
219     recreateTestFixture();
220 
221     // Warm up the cache.
222     EGLint newCacheSize = eglProgramCacheGetAttribANGLE(display, EGL_PROGRAM_CACHE_SIZE_ANGLE);
223     EXPECT_EQ(0, newCacheSize);
224     eglProgramCachePopulateANGLE(display, keyBuffer.data(), keySize, binaryBuffer.data(),
225                                  binarySize);
226 
227     mCachedBinary.clear();
228 
229     // Link a program, which will hit the cache.
230     {
231         glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
232         glClear(GL_COLOR_BUFFER_BIT);
233         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
234 
235         ANGLE_GL_PROGRAM(program, kVS, kFS);
236         drawQuad(program, "position", 0.5f);
237         EXPECT_GL_NO_ERROR();
238         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
239     }
240 
241     // Verify no new shader was compiled.
242     EXPECT_TRUE(mCachedBinary.empty());
243 }
244 
245 // Tests that trying to link a program without correct shaders doesn't buggily call the cache.
TEST_P(EGLProgramCacheControlTest,LinkProgramWithBadShaders)246 TEST_P(EGLProgramCacheControlTest, LinkProgramWithBadShaders)
247 {
248     ANGLE_SKIP_TEST_IF(!extensionAvailable());
249 
250     GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
251 
252     GLuint program = glCreateProgram();
253     glAttachShader(program, shader);
254     glLinkProgram(program);
255 
256     GLint linkStatus = 0;
257     glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
258     EXPECT_GL_FALSE(linkStatus);
259     EXPECT_GL_NO_ERROR();
260 
261     glDeleteShader(shader);
262     glDeleteProgram(program);
263 }
264 
265 // Tests the program cache can be disabled.
TEST_P(EGLProgramCacheControlTest,DisableProgramCache)266 TEST_P(EGLProgramCacheControlTest, DisableProgramCache)
267 {
268     ANGLE_SKIP_TEST_IF(!extensionAvailable() || !programBinaryAvailable());
269 
270     // Disable context program cache, and recreate context.
271     setContextProgramCacheEnabled(false);
272     recreateTestFixture();
273 
274     constexpr char kVS[] = "attribute vec4 position; void main() { gl_Position = position; }";
275     constexpr char kFS[] = "void main() { gl_FragColor = vec4(1, 0, 0, 1); }";
276 
277     mCachedBinary.clear();
278     // Link a program, which will miss the cache.
279     {
280         glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
281         glClear(GL_COLOR_BUFFER_BIT);
282         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
283 
284         ANGLE_GL_PROGRAM(program, kVS, kFS);
285         drawQuad(program, "position", 0.5f);
286         EXPECT_GL_NO_ERROR();
287         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
288     }
289 
290     // Expect that no program binary was inserted into the cache.
291     EXPECT_TRUE(mCachedBinary.empty());
292 }
293 
294 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLProgramCacheControlTest);
295 ANGLE_INSTANTIATE_TEST(EGLProgramCacheControlTest,
296                        ES2_D3D9(),
297                        ES2_D3D11(),
298                        ES2_OPENGL(),
299                        ES2_VULKAN());
300