Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #pragma once |
| 18 | |
| 19 | #include <cutils/compiler.h> |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | #include <GrContextOptions.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class BlobCache; |
| 29 | class FileBlobCache; |
| 30 | |
| 31 | namespace uirenderer { |
| 32 | namespace skiapipeline { |
| 33 | |
| 34 | class ShaderCache : public GrContextOptions::PersistentCache { |
| 35 | public: |
| 36 | /** |
| 37 | * "get" returns a pointer to the singleton ShaderCache object. This |
| 38 | * singleton object will never be destroyed. |
| 39 | */ |
| 40 | ANDROID_API static ShaderCache& get(); |
| 41 | |
| 42 | /** |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame^] | 43 | * initShaderDiskCache" loads the serialized cache contents from disk, |
| 44 | * optionally checks that the on-disk cache matches a provided identity, |
| 45 | * and puts the ShaderCache into an initialized state, such that it is |
| 46 | * able to insert and retrieve entries from the cache. If identity is |
| 47 | * non-null and validation fails, the cache is initialized but contains |
| 48 | * no data. If size is less than zero, the cache is initilaized but |
| 49 | * contains no data. |
| 50 | * |
| 51 | * This should be called when HWUI pipeline is initialized. When not in |
| 52 | * the initialized state the load and store methods will return without |
| 53 | * performing any cache operations. |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 54 | */ |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame^] | 55 | virtual void initShaderDiskCache(const void *identity, ssize_t size); |
| 56 | |
| 57 | virtual void initShaderDiskCache() { initShaderDiskCache(nullptr, 0); } |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * "setFilename" sets the name of the file that should be used to store |
| 61 | * cache contents from one program invocation to another. This function does not perform any |
| 62 | * disk operation and it should be invoked before "initShaderCache". |
| 63 | */ |
| 64 | virtual void setFilename(const char* filename); |
| 65 | |
| 66 | /** |
| 67 | * "load" attempts to retrieve the value blob associated with a given key |
| 68 | * blob from cache. This will be called by Skia, when it needs to compile a new SKSL shader. |
| 69 | */ |
| 70 | sk_sp<SkData> load(const SkData& key) override; |
| 71 | |
| 72 | /** |
| 73 | * "store" attempts to insert a new key/value blob pair into the cache. |
| 74 | * This will be called by Skia after it compiled a new SKSL shader |
| 75 | */ |
| 76 | void store(const SkData& key, const SkData& data) override; |
| 77 | |
| 78 | private: |
| 79 | // Creation and (the lack of) destruction is handled internally. |
| 80 | ShaderCache(); |
| 81 | |
| 82 | // Copying is disallowed. |
| 83 | ShaderCache(const ShaderCache&) = delete; |
| 84 | void operator=(const ShaderCache&) = delete; |
| 85 | |
| 86 | /** |
| 87 | * "getBlobCacheLocked" returns the BlobCache object being used to store the |
| 88 | * key/value blob pairs. If the BlobCache object has not yet been created, |
| 89 | * this will do so, loading the serialized cache contents from disk if |
| 90 | * possible. |
| 91 | */ |
| 92 | BlobCache* getBlobCacheLocked(); |
| 93 | |
| 94 | /** |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame^] | 95 | * "validateCache" updates the cache to match the given identity. If the |
| 96 | * cache currently has the wrong identity, all entries in the cache are cleared. |
| 97 | */ |
| 98 | bool validateCache(const void* identity, ssize_t size); |
| 99 | |
| 100 | /** |
| 101 | * "saveToDiskLocked" attemps to save the current contents of the cache to |
| 102 | * disk. If the identity hash exists, we will insert the identity hash into |
| 103 | * the cache for next validation. |
| 104 | */ |
| 105 | void saveToDiskLocked(); |
| 106 | |
| 107 | /** |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 108 | * "mInitialized" indicates whether the ShaderCache is in the initialized |
| 109 | * state. It is initialized to false at construction time, and gets set to |
| 110 | * true when initialize is called. |
| 111 | * When in this state, the cache behaves as normal. When not, |
| 112 | * the load and store methods will return without performing any cache |
| 113 | * operations. |
| 114 | */ |
| 115 | bool mInitialized = false; |
| 116 | |
| 117 | /** |
| 118 | * "mBlobCache" is the cache in which the key/value blob pairs are stored. It |
| 119 | * is initially NULL, and will be initialized by getBlobCacheLocked the |
| 120 | * first time it's needed. |
| 121 | * The blob cache contains the Android build number. We treat version mismatches as an empty |
| 122 | * cache (logic implemented in BlobCache::unflatten). |
| 123 | */ |
| 124 | std::unique_ptr<FileBlobCache> mBlobCache; |
| 125 | |
| 126 | /** |
| 127 | * "mFilename" is the name of the file for storing cache contents in between |
| 128 | * program invocations. It is initialized to an empty string at |
| 129 | * construction time, and can be set with the setCacheFilename method. An |
| 130 | * empty string indicates that the cache should not be saved to or restored |
| 131 | * from disk. |
| 132 | */ |
| 133 | std::string mFilename; |
| 134 | |
| 135 | /** |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame^] | 136 | * "mIDHash" is the current identity hash for the cache validation. It is |
| 137 | * initialized to an empty vector at construction time, and its content is |
| 138 | * generated in the call of the validateCache method. An empty vector |
| 139 | * indicates that cache validation is not performed, and the hash should |
| 140 | * not be stored on disk. |
| 141 | */ |
| 142 | std::vector<uint8_t> mIDHash; |
| 143 | |
| 144 | /** |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 145 | * "mSavePending" indicates whether or not a deferred save operation is |
| 146 | * pending. Each time a key/value pair is inserted into the cache via |
| 147 | * load, a deferred save is initiated if one is not already pending. |
| 148 | * This will wait some amount of time and then trigger a save of the cache |
| 149 | * contents to disk. |
| 150 | */ |
| 151 | bool mSavePending = false; |
| 152 | |
| 153 | /** |
| 154 | * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function. |
| 155 | */ |
| 156 | size_t mObservedBlobValueSize = 20*1024; |
| 157 | |
| 158 | /** |
| 159 | * The time in seconds to wait before saving newly inserted cache entries. |
| 160 | */ |
| 161 | unsigned int mDeferredSaveDelay = 4; |
| 162 | |
| 163 | /** |
| 164 | * "mMutex" is the mutex used to prevent concurrent access to the member |
| 165 | * variables. It must be locked whenever the member variables are accessed. |
| 166 | */ |
| 167 | mutable std::mutex mMutex; |
| 168 | |
| 169 | /** |
| 170 | * "sCache" is the singleton ShaderCache object. |
| 171 | */ |
| 172 | static ShaderCache sCache; |
| 173 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame^] | 174 | /** |
| 175 | * "sIDKey" is the cache key of the identity hash |
| 176 | */ |
| 177 | static constexpr uint8_t sIDKey = 0; |
| 178 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 179 | friend class ShaderCacheTestUtils; //used for unit testing |
| 180 | }; |
| 181 | |
| 182 | } /* namespace skiapipeline */ |
| 183 | } /* namespace uirenderer */ |
| 184 | } /* namespace android */ |