blob: e7f17126f2a6c7ed04564d7932f82be16b3b4f40 [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
23#include <utils/BlobCache.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080024#include <utils/Mutex.h>
Jamie Gennis98c63832011-11-07 17:03:54 -080025#include <utils/String8.h>
Jamie Gennis76601082011-11-06 14:14:33 -080026#include <utils/StrongPointer.h>
27
Jamie Gennisaca51c02011-11-03 17:42:43 -070028// ----------------------------------------------------------------------------
29namespace android {
30// ----------------------------------------------------------------------------
31
32class egl_display_t;
33
Jamie Gennis98c63832011-11-07 17:03:54 -080034class EGLAPI egl_cache_t {
Jamie Gennisaca51c02011-11-03 17:42:43 -070035public:
36
Jamie Gennis76601082011-11-06 14:14:33 -080037 // get returns a pointer to the singleton egl_cache_t object. This
38 // singleton object will never be destroyed.
Jamie Gennisaca51c02011-11-03 17:42:43 -070039 static egl_cache_t* get();
40
Jamie Gennis76601082011-11-06 14:14:33 -080041 // initialize puts the egl_cache_t into an initialized state, such that it
42 // is able to insert and retrieve entries from the cache. This should be
43 // called when EGL is initialized. When not in the initialized state the
44 // getBlob and setBlob methods will return without performing any cache
45 // operations.
Jamie Gennisaca51c02011-11-03 17:42:43 -070046 void initialize(egl_display_t* display);
Jamie Gennis76601082011-11-06 14:14:33 -080047
48 // terminate puts the egl_cache_t back into the uninitialized state. When
49 // in this state the getBlob and setBlob methods will return without
50 // performing any cache operations.
51 void terminate();
52
53 // setBlob attempts to insert a new key/value blob pair into the cache.
54 // This will be called by the hardware vendor's EGL implementation via the
55 // EGL_ANDROID_blob_cache extension.
Jamie Gennisc42fcf02011-11-09 15:35:34 -080056 void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
57 EGLsizeiANDROID valueSize);
Jamie Gennis76601082011-11-06 14:14:33 -080058
59 // getBlob attempts to retrieve the value blob associated with a given key
60 // blob from cache. This will be called by the hardware vendor's EGL
61 // implementation via the EGL_ANDROID_blob_cache extension.
Jamie Gennisc42fcf02011-11-09 15:35:34 -080062 EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
63 void* value, EGLsizeiANDROID valueSize);
Jamie Gennis76601082011-11-06 14:14:33 -080064
Jamie Gennis98c63832011-11-07 17:03:54 -080065 // setCacheFilename sets the name of the file that should be used to store
66 // cache contents from one program invocation to another.
67 void setCacheFilename(const char* filename);
68
Jamie Gennis76601082011-11-06 14:14:33 -080069private:
70 // Creation and (the lack of) destruction is handled internally.
71 egl_cache_t();
72 ~egl_cache_t();
73
74 // Copying is disallowed.
75 egl_cache_t(const egl_cache_t&); // not implemented
76 void operator=(const egl_cache_t&); // not implemented
77
78 // getBlobCacheLocked returns the BlobCache object being used to store the
79 // key/value blob pairs. If the BlobCache object has not yet been created,
80 // this will do so, loading the serialized cache contents from disk if
81 // possible.
82 sp<BlobCache> getBlobCacheLocked();
83
84 // saveBlobCache attempts to save the current contents of mBlobCache to
85 // disk.
86 void saveBlobCacheLocked();
87
88 // loadBlobCache attempts to load the saved cache contents from disk into
89 // mBlobCache.
90 void loadBlobCacheLocked();
91
92 // mInitialized indicates whether the egl_cache_t is in the initialized
93 // state. It is initialized to false at construction time, and gets set to
94 // true when initialize is called. It is set back to false when terminate
95 // is called. When in this state, the cache behaves as normal. When not,
96 // the getBlob and setBlob methods will return without performing any cache
97 // operations.
98 bool mInitialized;
99
100 // mBlobCache is the cache in which the key/value blob pairs are stored. It
101 // is initially NULL, and will be initialized by getBlobCacheLocked the
102 // first time it's needed.
103 sp<BlobCache> mBlobCache;
104
Jamie Gennis98c63832011-11-07 17:03:54 -0800105 // mFilename is the name of the file for storing cache contents in between
106 // program invocations. It is initialized to an empty string at
107 // construction time, and can be set with the setCacheFilename method. An
108 // empty string indicates that the cache should not be saved to or restored
109 // from disk.
110 String8 mFilename;
111
Jamie Gennis99c3d702011-11-08 17:59:36 -0800112 // mSavePending indicates whether or not a deferred save operation is
113 // pending. Each time a key/value pair is inserted into the cache via
114 // setBlob, a deferred save is initiated if one is not already pending.
115 // This will wait some amount of time and then trigger a save of the cache
116 // contents to disk.
117 bool mSavePending;
118
Jamie Gennis76601082011-11-06 14:14:33 -0800119 // mMutex is the mutex used to prevent concurrent access to the member
120 // variables. It must be locked whenever the member variables are accessed.
121 mutable Mutex mMutex;
Jamie Gennis98c63832011-11-07 17:03:54 -0800122
123 // sCache is the singleton egl_cache_t object.
124 static egl_cache_t sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700125};
126
127// ----------------------------------------------------------------------------
128}; // namespace android
129// ----------------------------------------------------------------------------
Jamie Gennis76601082011-11-06 14:14:33 -0800130
131#endif // ANDROID_EGL_CACHE_H