• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "egl_cache.h"
18 
19 #include <log/log.h>
20 #include <private/EGL/cache.h>
21 #include <unistd.h>
22 
23 #include <thread>
24 
25 #include "../egl_impl.h"
26 #include "egl_display.h"
27 
28 // Cache size limits.
29 static const size_t maxKeySize = 12 * 1024;
30 static const size_t maxValueSize = 64 * 1024;
31 static const size_t maxTotalSize = 2 * 1024 * 1024;
32 
33 // The time in seconds to wait before saving newly inserted cache entries.
34 static const unsigned int deferredSaveDelay = 4;
35 
36 namespace android {
37 
38 #define BC_EXT_STR "EGL_ANDROID_blob_cache"
39 
40 // called from android_view_ThreadedRenderer.cpp
egl_set_cache_filename(const char * filename)41 void egl_set_cache_filename(const char* filename) {
42     egl_cache_t::get()->setCacheFilename(filename);
43 }
44 
45 //
46 // Callback functions passed to EGL.
47 //
setBlob(const void * key,EGLsizeiANDROID keySize,const void * value,EGLsizeiANDROID valueSize)48 static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
49                     EGLsizeiANDROID valueSize) {
50     egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
51 }
52 
getBlob(const void * key,EGLsizeiANDROID keySize,void * value,EGLsizeiANDROID valueSize)53 static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
54                                EGLsizeiANDROID valueSize) {
55     return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
56 }
57 
58 //
59 // egl_cache_t definition
60 //
egl_cache_t()61 egl_cache_t::egl_cache_t() : mInitialized(false) {}
62 
~egl_cache_t()63 egl_cache_t::~egl_cache_t() {}
64 
65 egl_cache_t egl_cache_t::sCache;
66 
get()67 egl_cache_t* egl_cache_t::get() {
68     return &sCache;
69 }
70 
initialize(egl_display_t * display)71 void egl_cache_t::initialize(egl_display_t* display) {
72     std::lock_guard<std::mutex> lock(mMutex);
73 
74     egl_connection_t* const cnx = &gEGLImpl;
75     if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
76         const char* exts = display->disp.queryString.extensions;
77         size_t bcExtLen = strlen(BC_EXT_STR);
78         size_t extsLen = strlen(exts);
79         bool equal = !strcmp(BC_EXT_STR, exts);
80         bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen + 1);
81         bool atEnd = (bcExtLen + 1) < extsLen &&
82                 !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen + 1));
83         bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
84         if (equal || atStart || atEnd || inMiddle) {
85             PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
86             eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
87                     cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
88             if (eglSetBlobCacheFuncsANDROID == nullptr) {
89                 ALOGE("EGL_ANDROID_blob_cache advertised, "
90                       "but unable to get eglSetBlobCacheFuncsANDROID");
91                 return;
92             }
93 
94             eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
95             EGLint err = cnx->egl.eglGetError();
96             if (err != EGL_SUCCESS) {
97                 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
98                       "%#x",
99                       err);
100             }
101         }
102     }
103 
104     mInitialized = true;
105 }
106 
terminate()107 void egl_cache_t::terminate() {
108     std::lock_guard<std::mutex> lock(mMutex);
109     if (mBlobCache) {
110         mBlobCache->writeToFile();
111     }
112     mBlobCache = nullptr;
113 }
114 
setBlob(const void * key,EGLsizeiANDROID keySize,const void * value,EGLsizeiANDROID valueSize)115 void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
116                           EGLsizeiANDROID valueSize) {
117     std::lock_guard<std::mutex> lock(mMutex);
118 
119     if (keySize < 0 || valueSize < 0) {
120         ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
121         return;
122     }
123 
124     if (mInitialized) {
125         BlobCache* bc = getBlobCacheLocked();
126         bc->set(key, keySize, value, valueSize);
127 
128         if (!mSavePending) {
129             mSavePending = true;
130             std::thread deferredSaveThread([this]() {
131                 sleep(deferredSaveDelay);
132                 std::lock_guard<std::mutex> lock(mMutex);
133                 if (mInitialized && mBlobCache) {
134                     mBlobCache->writeToFile();
135                 }
136                 mSavePending = false;
137             });
138             deferredSaveThread.detach();
139         }
140     }
141 }
142 
getBlob(const void * key,EGLsizeiANDROID keySize,void * value,EGLsizeiANDROID valueSize)143 EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
144                                      EGLsizeiANDROID valueSize) {
145     std::lock_guard<std::mutex> lock(mMutex);
146 
147     if (keySize < 0 || valueSize < 0) {
148         ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
149         return 0;
150     }
151 
152     if (mInitialized) {
153         BlobCache* bc = getBlobCacheLocked();
154         return bc->get(key, keySize, value, valueSize);
155     }
156     return 0;
157 }
158 
setCacheFilename(const char * filename)159 void egl_cache_t::setCacheFilename(const char* filename) {
160     std::lock_guard<std::mutex> lock(mMutex);
161     mFilename = filename;
162 }
163 
getBlobCacheLocked()164 BlobCache* egl_cache_t::getBlobCacheLocked() {
165     if (mBlobCache == nullptr) {
166         mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
167     }
168     return mBlobCache.get();
169 }
170 
171 }; // namespace android
172