summaryrefslogtreecommitdiff
path: root/opengl/tests/EGLTest/egl_cache_test.cpp
blob: ce5818229acf267c7f0cd2b002198e73c3076f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "EGL_test"
// #define LOG_NDEBUG 0

#include <gtest/gtest.h>

#include <utils/Log.h>

#include <android-base/test_utils.h>

#include "egl_cache.h"
#include "MultifileBlobCache.h"
#include "egl_display.h"

#include <fstream>
#include <memory>

using namespace std::literals;

namespace android {

class EGLCacheTest : public ::testing::TestWithParam<egl_cache_t::EGLCacheMode> {
protected:
    virtual void SetUp() {
        // Terminate to clean up any previous cache in this process
        mCache->terminate();

        mTempFile.reset(new TemporaryFile());
        mCache->setCacheFilename(&mTempFile->path[0]);
        mCache->setCacheLimit(1024);
        mCache->setCacheMode(mCacheMode);
    }

    virtual void TearDown() {
        mCache->terminate();
        mCache->setCacheFilename("");
        mTempFile.reset(nullptr);
    }

    std::string getCachefileName();

    egl_cache_t* mCache = egl_cache_t::get();
    std::unique_ptr<TemporaryFile> mTempFile;
    egl_cache_t::EGLCacheMode mCacheMode = GetParam();
};

TEST_P(EGLCacheTest, UninitializedCacheAlwaysMisses) {
    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->setBlob("abcd", 4, "efgh", 4);
    ASSERT_EQ(0, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ(0xee, buf[0]);
    ASSERT_EQ(0xee, buf[1]);
    ASSERT_EQ(0xee, buf[2]);
    ASSERT_EQ(0xee, buf[3]);
}

TEST_P(EGLCacheTest, InitializedCacheAlwaysHits) {
    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
    mCache->setBlob("abcd", 4, "efgh", 4);
    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ('e', buf[0]);
    ASSERT_EQ('f', buf[1]);
    ASSERT_EQ('g', buf[2]);
    ASSERT_EQ('h', buf[3]);
}

TEST_P(EGLCacheTest, TerminatedCacheAlwaysMisses) {
    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
    mCache->setBlob("abcd", 4, "efgh", 4);
    mCache->terminate();
    ASSERT_EQ(0, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ(0xee, buf[0]);
    ASSERT_EQ(0xee, buf[1]);
    ASSERT_EQ(0xee, buf[2]);
    ASSERT_EQ(0xee, buf[3]);
}

TEST_P(EGLCacheTest, ReinitializedCacheContainsValues) {
    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
    mCache->setBlob("abcd", 4, "efgh", 4);
    mCache->terminate();
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ('e', buf[0]);
    ASSERT_EQ('f', buf[1]);
    ASSERT_EQ('g', buf[2]);
    ASSERT_EQ('h', buf[3]);
}

std::string EGLCacheTest::getCachefileName() {
    // Return the monolithic filename unless we find the multifile dir
    std::string cachePath = &mTempFile->path[0];
    std::string multifileDirName = cachePath + ".multifile";
    std::string cachefileName = "";

    struct stat info;
    if (stat(multifileDirName.c_str(), &info) == 0) {
        // Ensure we only have one file to manage
        int entryFileCount = 0;

        // We have a multifile dir. Return the only entry file in it.
        DIR* dir;
        struct dirent* entry;
        if ((dir = opendir(multifileDirName.c_str())) != nullptr) {
            while ((entry = readdir(dir)) != nullptr) {
                if (entry->d_name == "."s || entry->d_name == ".."s ||
                    strcmp(entry->d_name, kMultifileBlobCacheStatusFile) == 0) {
                    continue;
                }
                cachefileName = multifileDirName + "/" + entry->d_name;
                entryFileCount++;
            }
        } else {
            printf("Unable to open %s, error: %s\n",
                   multifileDirName.c_str(), std::strerror(errno));
        }

        if (entryFileCount != 1) {
            // If there was more than one real file in the directory, this
            // violates test assumptions
            cachefileName = "";
        }
    } else {
        printf("Unable to stat %s, error: %s\n",
               multifileDirName.c_str(), std::strerror(errno));
    }

    return cachefileName;
}

TEST_P(EGLCacheTest, ModifiedCacheBeginMisses) {
    // Skip if not in multifile mode
    if (mCacheMode == egl_cache_t::EGLCacheMode::Monolithic) {
        GTEST_SKIP() << "Skipping test designed for multifile";
    }

    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));

    mCache->setBlob("abcd", 4, "efgh", 4);
    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ('e', buf[0]);
    ASSERT_EQ('f', buf[1]);
    ASSERT_EQ('g', buf[2]);
    ASSERT_EQ('h', buf[3]);

    // Ensure the cache file is written to disk
    mCache->terminate();

    // Depending on the cache mode, the file will be in different locations
    std::string cachefileName = getCachefileName();
    ASSERT_TRUE(cachefileName.length() > 0);

    // Stomp on the beginning of the cache file, breaking the key match
    const char* stomp = "BADF00D";
    std::fstream fs(cachefileName);
    fs.seekp(0, std::ios_base::beg);
    fs.write(stomp, strlen(stomp));
    fs.flush();
    fs.close();

    // Ensure no cache hit
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
    uint8_t buf2[4] = { 0xee, 0xee, 0xee, 0xee };
    // getBlob may return junk for required size, but should not return a cache hit
    mCache->getBlob("abcd", 4, buf2, 4);
    ASSERT_EQ(0xee, buf2[0]);
    ASSERT_EQ(0xee, buf2[1]);
    ASSERT_EQ(0xee, buf2[2]);
    ASSERT_EQ(0xee, buf2[3]);
}

TEST_P(EGLCacheTest, ModifiedCacheEndMisses) {
    // Skip if not in multifile mode
    if (mCacheMode == egl_cache_t::EGLCacheMode::Monolithic) {
        GTEST_SKIP() << "Skipping test designed for multifile";
    }

    uint8_t buf[16] = { 0xee, 0xee, 0xee, 0xee,
                        0xee, 0xee, 0xee, 0xee,
                        0xee, 0xee, 0xee, 0xee,
                        0xee, 0xee, 0xee, 0xee };

    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));

    mCache->setBlob("abcdefghij", 10, "klmnopqrstuvwxyz", 16);
    ASSERT_EQ(16, mCache->getBlob("abcdefghij", 10, buf, 16));
    ASSERT_EQ('w', buf[12]);
    ASSERT_EQ('x', buf[13]);
    ASSERT_EQ('y', buf[14]);
    ASSERT_EQ('z', buf[15]);

    // Ensure the cache file is written to disk
    mCache->terminate();

    // Depending on the cache mode, the file will be in different locations
    std::string cachefileName = getCachefileName();
    ASSERT_TRUE(cachefileName.length() > 0);

    // Stomp on the END of the cache file, modifying its contents
    const char* stomp = "BADF00D";
    std::fstream fs(cachefileName);
    fs.seekp(-strlen(stomp), std::ios_base::end);
    fs.write(stomp, strlen(stomp));
    fs.flush();
    fs.close();

    // Ensure no cache hit
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));
    uint8_t buf2[16] = { 0xee, 0xee, 0xee, 0xee,
                         0xee, 0xee, 0xee, 0xee,
                         0xee, 0xee, 0xee, 0xee,
                         0xee, 0xee, 0xee, 0xee };

    // getBlob may return junk for required size, but should not return a cache hit
    mCache->getBlob("abcdefghij", 10, buf2, 16);
    ASSERT_EQ(0xee, buf2[0]);
    ASSERT_EQ(0xee, buf2[1]);
    ASSERT_EQ(0xee, buf2[2]);
    ASSERT_EQ(0xee, buf2[3]);
}

TEST_P(EGLCacheTest, TerminatedCacheBelowCacheLimit) {
    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));

    mCache->setBlob("abcd", 4, "efgh", 4);
    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ('e', buf[0]);
    ASSERT_EQ('f', buf[1]);
    ASSERT_EQ('g', buf[2]);
    ASSERT_EQ('h', buf[3]);

    mCache->setBlob("ijkl", 4, "mnop", 4);
    ASSERT_EQ(4, mCache->getBlob("ijkl", 4, buf, 4));
    ASSERT_EQ('m', buf[0]);
    ASSERT_EQ('n', buf[1]);
    ASSERT_EQ('o', buf[2]);
    ASSERT_EQ('p', buf[3]);

    mCache->setBlob("qrst", 4, "uvwx", 4);
    ASSERT_EQ(4, mCache->getBlob("qrst", 4, buf, 4));
    ASSERT_EQ('u', buf[0]);
    ASSERT_EQ('v', buf[1]);
    ASSERT_EQ('w', buf[2]);
    ASSERT_EQ('x', buf[3]);

    // Cache should contain both the key and the value
    // So 8 bytes per entry, at least 24 bytes
    ASSERT_GE(mCache->getCacheSize(), 24);

    // Set the new limit and initialize cache
    mCache->terminate();
    mCache->setCacheLimit(4);
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));

    // Ensure the new limit is respected
    ASSERT_LE(mCache->getCacheSize(), 4);
}

TEST_P(EGLCacheTest, TrimCacheOnOverflow) {
    // Skip if not in multifile mode
    if (mCacheMode == egl_cache_t::EGLCacheMode::Monolithic) {
        GTEST_SKIP() << "Skipping test designed for multifile";
    }

    uint8_t buf[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));

    // Set one value in the cache
    mCache->setBlob("abcd", 4, "efgh", 4);
    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ('e', buf[0]);
    ASSERT_EQ('f', buf[1]);
    ASSERT_EQ('g', buf[2]);
    ASSERT_EQ('h', buf[3]);

    // Get the size of cache with a single entry
    size_t cacheEntrySize = mCache->getCacheSize();

    // Now reinitialize the cache, using max size equal to a single entry
    mCache->terminate();
    mCache->setCacheLimit(cacheEntrySize);
    mCache->initialize(egl_display_t::get(EGL_DEFAULT_DISPLAY));

    // Ensure our cache still has original value
    ASSERT_EQ(4, mCache->getBlob("abcd", 4, buf, 4));
    ASSERT_EQ('e', buf[0]);
    ASSERT_EQ('f', buf[1]);
    ASSERT_EQ('g', buf[2]);
    ASSERT_EQ('h', buf[3]);

    // Set another value, which should overflow the cache and trim
    mCache->setBlob("ijkl", 4, "mnop", 4);
    ASSERT_EQ(4, mCache->getBlob("ijkl", 4, buf, 4));
    ASSERT_EQ('m', buf[0]);
    ASSERT_EQ('n', buf[1]);
    ASSERT_EQ('o', buf[2]);
    ASSERT_EQ('p', buf[3]);

    // The cache should still be under the limit
    ASSERT_TRUE(mCache->getCacheSize() == cacheEntrySize);

    // And no cache hit on trimmed entry
    uint8_t buf2[4] = { 0xee, 0xee, 0xee, 0xee };
    mCache->getBlob("abcd", 4, buf2, 4);
    ASSERT_EQ(0xee, buf2[0]);
    ASSERT_EQ(0xee, buf2[1]);
    ASSERT_EQ(0xee, buf2[2]);
    ASSERT_EQ(0xee, buf2[3]);
}

INSTANTIATE_TEST_CASE_P(MonolithicCacheTests,
        EGLCacheTest, ::testing::Values(egl_cache_t::EGLCacheMode::Monolithic));
INSTANTIATE_TEST_CASE_P(MultifileCacheTests,
        EGLCacheTest, ::testing::Values(egl_cache_t::EGLCacheMode::Multifile));
}