| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_EGL_CACHE_H | 
|  | 18 | #define ANDROID_EGL_CACHE_H | 
|  | 19 |  | 
|  | 20 | #include <EGL/egl.h> | 
|  | 21 | #include <EGL/eglext.h> | 
|  | 22 |  | 
| Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 23 | #include "BlobCache.h" | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 24 |  | 
| Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 25 | #include <memory> | 
| Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 26 | #include <mutex> | 
|  | 27 | #include <string> | 
|  | 28 |  | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 29 | // ---------------------------------------------------------------------------- | 
|  | 30 | namespace android { | 
|  | 31 | // ---------------------------------------------------------------------------- | 
|  | 32 |  | 
|  | 33 | class egl_display_t; | 
|  | 34 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 35 | class EGLAPI egl_cache_t { | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 36 | public: | 
|  | 37 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 38 | // get returns a pointer to the singleton egl_cache_t object.  This | 
|  | 39 | // singleton object will never be destroyed. | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 40 | static egl_cache_t* get(); | 
|  | 41 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 42 | // initialize puts the egl_cache_t into an initialized state, such that it | 
|  | 43 | // is able to insert and retrieve entries from the cache.  This should be | 
|  | 44 | // called when EGL is initialized.  When not in the initialized state the | 
|  | 45 | // getBlob and setBlob methods will return without performing any cache | 
|  | 46 | // operations. | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 47 | void initialize(egl_display_t* display); | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 48 |  | 
|  | 49 | // terminate puts the egl_cache_t back into the uninitialized state.  When | 
|  | 50 | // in this state the getBlob and setBlob methods will return without | 
|  | 51 | // performing any cache operations. | 
|  | 52 | void terminate(); | 
|  | 53 |  | 
|  | 54 | // setBlob attempts to insert a new key/value blob pair into the cache. | 
|  | 55 | // This will be called by the hardware vendor's EGL implementation via the | 
|  | 56 | // EGL_ANDROID_blob_cache extension. | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 57 | void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value, | 
|  | 58 | EGLsizeiANDROID valueSize); | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 59 |  | 
|  | 60 | // getBlob attempts to retrieve the value blob associated with a given key | 
|  | 61 | // blob from cache.  This will be called by the hardware vendor's EGL | 
|  | 62 | // implementation via the EGL_ANDROID_blob_cache extension. | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 63 | EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, | 
|  | 64 | void* value, EGLsizeiANDROID valueSize); | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 65 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 66 | // setCacheFilename sets the name of the file that should be used to store | 
|  | 67 | // cache contents from one program invocation to another. | 
|  | 68 | void setCacheFilename(const char* filename); | 
|  | 69 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 70 | private: | 
|  | 71 | // Creation and (the lack of) destruction is handled internally. | 
|  | 72 | egl_cache_t(); | 
|  | 73 | ~egl_cache_t(); | 
|  | 74 |  | 
|  | 75 | // Copying is disallowed. | 
|  | 76 | egl_cache_t(const egl_cache_t&); // not implemented | 
|  | 77 | void operator=(const egl_cache_t&); // not implemented | 
|  | 78 |  | 
|  | 79 | // getBlobCacheLocked returns the BlobCache object being used to store the | 
|  | 80 | // key/value blob pairs.  If the BlobCache object has not yet been created, | 
|  | 81 | // this will do so, loading the serialized cache contents from disk if | 
|  | 82 | // possible. | 
| Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 83 | BlobCache* getBlobCacheLocked(); | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | // saveBlobCache attempts to save the current contents of mBlobCache to | 
|  | 86 | // disk. | 
|  | 87 | void saveBlobCacheLocked(); | 
|  | 88 |  | 
|  | 89 | // loadBlobCache attempts to load the saved cache contents from disk into | 
|  | 90 | // mBlobCache. | 
|  | 91 | void loadBlobCacheLocked(); | 
|  | 92 |  | 
|  | 93 | // mInitialized indicates whether the egl_cache_t is in the initialized | 
|  | 94 | // state.  It is initialized to false at construction time, and gets set to | 
|  | 95 | // true when initialize is called.  It is set back to false when terminate | 
|  | 96 | // is called.  When in this state, the cache behaves as normal.  When not, | 
|  | 97 | // the getBlob and setBlob methods will return without performing any cache | 
|  | 98 | // operations. | 
|  | 99 | bool mInitialized; | 
|  | 100 |  | 
|  | 101 | // mBlobCache is the cache in which the key/value blob pairs are stored.  It | 
|  | 102 | // is initially NULL, and will be initialized by getBlobCacheLocked the | 
|  | 103 | // first time it's needed. | 
| Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 104 | std::unique_ptr<BlobCache> mBlobCache; | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 105 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 106 | // mFilename is the name of the file for storing cache contents in between | 
|  | 107 | // program invocations.  It is initialized to an empty string at | 
|  | 108 | // construction time, and can be set with the setCacheFilename method.  An | 
|  | 109 | // empty string indicates that the cache should not be saved to or restored | 
|  | 110 | // from disk. | 
| Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 111 | std::string mFilename; | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 112 |  | 
| Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 113 | // mSavePending indicates whether or not a deferred save operation is | 
|  | 114 | // pending.  Each time a key/value pair is inserted into the cache via | 
|  | 115 | // setBlob, a deferred save is initiated if one is not already pending. | 
|  | 116 | // This will wait some amount of time and then trigger a save of the cache | 
|  | 117 | // contents to disk. | 
|  | 118 | bool mSavePending; | 
|  | 119 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 120 | // mMutex is the mutex used to prevent concurrent access to the member | 
|  | 121 | // variables. It must be locked whenever the member variables are accessed. | 
| Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 122 | mutable std::mutex mMutex; | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | // sCache is the singleton egl_cache_t object. | 
|  | 125 | static egl_cache_t sCache; | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 126 | }; | 
|  | 127 |  | 
|  | 128 | // ---------------------------------------------------------------------------- | 
|  | 129 | }; // namespace android | 
|  | 130 | // ---------------------------------------------------------------------------- | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 131 |  | 
|  | 132 | #endif // ANDROID_EGL_CACHE_H |