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 /** 43 * "initShaderDiskCache" loads the serialized cache contents from disk and puts the ShaderCache 44 * into an initialized state, such that it is able to insert and retrieve entries from the 45 * cache. This should be called when HWUI pipeline is initialized. When not in the initialized 46 * state the load and store methods will return without performing any cache operations. 47 */ 48 virtual void initShaderDiskCache(); 49 50 /** 51 * "setFilename" sets the name of the file that should be used to store 52 * cache contents from one program invocation to another. This function does not perform any 53 * disk operation and it should be invoked before "initShaderCache". 54 */ 55 virtual void setFilename(const char* filename); 56 57 /** 58 * "load" attempts to retrieve the value blob associated with a given key 59 * blob from cache. This will be called by Skia, when it needs to compile a new SKSL shader. 60 */ 61 sk_sp<SkData> load(const SkData& key) override; 62 63 /** 64 * "store" attempts to insert a new key/value blob pair into the cache. 65 * This will be called by Skia after it compiled a new SKSL shader 66 */ 67 void store(const SkData& key, const SkData& data) override; 68 69 private: 70 // Creation and (the lack of) destruction is handled internally. 71 ShaderCache(); 72 73 // Copying is disallowed. 74 ShaderCache(const ShaderCache&) = delete; 75 void operator=(const ShaderCache&) = delete; 76 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 */ 83 BlobCache* getBlobCacheLocked(); 84 85 /** 86 * "mInitialized" indicates whether the ShaderCache is in the initialized 87 * state. It is initialized to false at construction time, and gets set to 88 * true when initialize is called. 89 * When in this state, the cache behaves as normal. When not, 90 * the load and store methods will return without performing any cache 91 * operations. 92 */ 93 bool mInitialized = false; 94 95 /** 96 * "mBlobCache" is the cache in which the key/value blob pairs are stored. It 97 * is initially NULL, and will be initialized by getBlobCacheLocked the 98 * first time it's needed. 99 * The blob cache contains the Android build number. We treat version mismatches as an empty 100 * cache (logic implemented in BlobCache::unflatten). 101 */ 102 std::unique_ptr<FileBlobCache> mBlobCache; 103 104 /** 105 * "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 */ 111 std::string mFilename; 112 113 /** 114 * "mSavePending" indicates whether or not a deferred save operation is 115 * pending. Each time a key/value pair is inserted into the cache via 116 * load, a deferred save is initiated if one is not already pending. 117 * This will wait some amount of time and then trigger a save of the cache 118 * contents to disk. 119 */ 120 bool mSavePending = false; 121 122 /** 123 * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function. 124 */ 125 size_t mObservedBlobValueSize = 20*1024; 126 127 /** 128 * The time in seconds to wait before saving newly inserted cache entries. 129 */ 130 unsigned int mDeferredSaveDelay = 4; 131 132 /** 133 * "mMutex" is the mutex used to prevent concurrent access to the member 134 * variables. It must be locked whenever the member variables are accessed. 135 */ 136 mutable std::mutex mMutex; 137 138 /** 139 * "sCache" is the singleton ShaderCache object. 140 */ 141 static ShaderCache sCache; 142 143 friend class ShaderCacheTestUtils; //used for unit testing 144 }; 145 146 } /* namespace skiapipeline */ 147 } /* namespace uirenderer */ 148 } /* namespace android */ 149