egl_cache.h 4.9 KB

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