blob: d10a6154b7d32cbc3e932ca1e87bb39a2ff8b9b4 [file] [log] [blame]
Jamie Gennisaca51c02011-11-03 17:42:43 -07001/*
2 ** Copyright 2011, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
Jamie Gennis76601082011-11-06 14:14:33 -080017#ifndef ANDROID_EGL_CACHE_H
18#define ANDROID_EGL_CACHE_H
19
20#include <EGL/egl.h>
21#include <EGL/eglext.h>
22
Mathias Agopianb7f9a242017-03-08 22:29:31 -080023#include <memory>
Mathias Agopian65421432017-03-08 11:49:05 -080024#include <mutex>
25#include <string>
26
Yiwei Zhang8af03062020-08-12 21:28:15 -070027#include "FileBlobCache.h"
28
Jamie Gennisaca51c02011-11-03 17:42:43 -070029namespace android {
Jamie Gennisaca51c02011-11-03 17:42:43 -070030
31class egl_display_t;
32
Jamie Gennis98c63832011-11-07 17:03:54 -080033class EGLAPI egl_cache_t {
Jamie Gennisaca51c02011-11-03 17:42:43 -070034public:
Jamie Gennis76601082011-11-06 14:14:33 -080035 // get returns a pointer to the singleton egl_cache_t object. This
36 // singleton object will never be destroyed.
Jamie Gennisaca51c02011-11-03 17:42:43 -070037 static egl_cache_t* get();
38
Jamie Gennis76601082011-11-06 14:14:33 -080039 // initialize puts the egl_cache_t into an initialized state, such that it
40 // is able to insert and retrieve entries from the cache. This should be
41 // called when EGL is initialized. When not in the initialized state the
42 // getBlob and setBlob methods will return without performing any cache
43 // operations.
Jamie Gennisaca51c02011-11-03 17:42:43 -070044 void initialize(egl_display_t* display);
Jamie Gennis76601082011-11-06 14:14:33 -080045
46 // terminate puts the egl_cache_t back into the uninitialized state. When
47 // in this state the getBlob and setBlob methods will return without
48 // performing any cache operations.
49 void terminate();
50
51 // setBlob attempts to insert a new key/value blob pair into the cache.
52 // This will be called by the hardware vendor's EGL implementation via the
53 // EGL_ANDROID_blob_cache extension.
Jamie Gennisc42fcf02011-11-09 15:35:34 -080054 void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
55 EGLsizeiANDROID valueSize);
Jamie Gennis76601082011-11-06 14:14:33 -080056
57 // getBlob attempts to retrieve the value blob associated with a given key
58 // blob from cache. This will be called by the hardware vendor's EGL
59 // implementation via the EGL_ANDROID_blob_cache extension.
Jamie Gennisc42fcf02011-11-09 15:35:34 -080060 EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
61 void* value, EGLsizeiANDROID valueSize);
Jamie Gennis76601082011-11-06 14:14:33 -080062
Jamie Gennis98c63832011-11-07 17:03:54 -080063 // setCacheFilename sets the name of the file that should be used to store
64 // cache contents from one program invocation to another.
65 void setCacheFilename(const char* filename);
66
Jamie Gennis76601082011-11-06 14:14:33 -080067private:
68 // Creation and (the lack of) destruction is handled internally.
69 egl_cache_t();
70 ~egl_cache_t();
71
72 // Copying is disallowed.
73 egl_cache_t(const egl_cache_t&); // not implemented
74 void operator=(const egl_cache_t&); // not implemented
75
76 // getBlobCacheLocked returns the BlobCache object being used to store the
77 // key/value blob pairs. If the BlobCache object has not yet been created,
78 // this will do so, loading the serialized cache contents from disk if
79 // possible.
Mathias Agopianb7f9a242017-03-08 22:29:31 -080080 BlobCache* getBlobCacheLocked();
Jamie Gennis76601082011-11-06 14:14:33 -080081
Jamie Gennis76601082011-11-06 14:14:33 -080082 // mInitialized indicates whether the egl_cache_t is in the initialized
83 // state. It is initialized to false at construction time, and gets set to
84 // true when initialize is called. It is set back to false when terminate
85 // is called. When in this state, the cache behaves as normal. When not,
86 // the getBlob and setBlob methods will return without performing any cache
87 // operations.
88 bool mInitialized;
89
90 // mBlobCache is the cache in which the key/value blob pairs are stored. It
91 // is initially NULL, and will be initialized by getBlobCacheLocked the
92 // first time it's needed.
Stan Iliev9e7cd072017-10-09 15:56:10 -040093 std::unique_ptr<FileBlobCache> mBlobCache;
Jamie Gennis76601082011-11-06 14:14:33 -080094
Jamie Gennis98c63832011-11-07 17:03:54 -080095 // mFilename is the name of the file for storing cache contents in between
96 // program invocations. It is initialized to an empty string at
97 // construction time, and can be set with the setCacheFilename method. An
98 // empty string indicates that the cache should not be saved to or restored
99 // from disk.
Mathias Agopian65421432017-03-08 11:49:05 -0800100 std::string mFilename;
Jamie Gennis98c63832011-11-07 17:03:54 -0800101
Jamie Gennis99c3d702011-11-08 17:59:36 -0800102 // mSavePending indicates whether or not a deferred save operation is
103 // pending. Each time a key/value pair is inserted into the cache via
104 // setBlob, a deferred save is initiated if one is not already pending.
105 // This will wait some amount of time and then trigger a save of the cache
106 // contents to disk.
107 bool mSavePending;
108
Jamie Gennis76601082011-11-06 14:14:33 -0800109 // mMutex is the mutex used to prevent concurrent access to the member
110 // variables. It must be locked whenever the member variables are accessed.
Mathias Agopian65421432017-03-08 11:49:05 -0800111 mutable std::mutex mMutex;
Jamie Gennis98c63832011-11-07 17:03:54 -0800112
113 // sCache is the singleton egl_cache_t object.
114 static egl_cache_t sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700115};
116
Jamie Gennisaca51c02011-11-03 17:42:43 -0700117}; // namespace android
Jamie Gennis76601082011-11-06 14:14:33 -0800118
119#endif // ANDROID_EGL_CACHE_H