1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2017 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // MemoryProgramCache: Stores compiled and linked programs in memory so they don't
7*8975f5c5SAndroid Build Coastguard Worker // always have to be re-compiled. Can be used in conjunction with the platform
8*8975f5c5SAndroid Build Coastguard Worker // layer to warm up the cache from disk.
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard Worker // Include zlib first, otherwise FAR gets defined elsewhere.
11*8975f5c5SAndroid Build Coastguard Worker #define USE_SYSTEM_ZLIB
12*8975f5c5SAndroid Build Coastguard Worker #include "compression_utils_portable.h"
13*8975f5c5SAndroid Build Coastguard Worker
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/MemoryProgramCache.h"
15*8975f5c5SAndroid Build Coastguard Worker
16*8975f5c5SAndroid Build Coastguard Worker #include <GLSLANG/ShaderVars.h>
17*8975f5c5SAndroid Build Coastguard Worker #include <anglebase/sha1.h>
18*8975f5c5SAndroid Build Coastguard Worker
19*8975f5c5SAndroid Build Coastguard Worker #include "common/BinaryStream.h"
20*8975f5c5SAndroid Build Coastguard Worker #include "common/angle_version_info.h"
21*8975f5c5SAndroid Build Coastguard Worker #include "common/utilities.h"
22*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Context.h"
23*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Debug.h"
24*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/Uniform.h"
25*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/capture/FrameCapture.h"
26*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/histogram_macros.h"
27*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/ProgramImpl.h"
28*8975f5c5SAndroid Build Coastguard Worker #include "platform/PlatformMethods.h"
29*8975f5c5SAndroid Build Coastguard Worker
30*8975f5c5SAndroid Build Coastguard Worker namespace gl
31*8975f5c5SAndroid Build Coastguard Worker {
32*8975f5c5SAndroid Build Coastguard Worker
33*8975f5c5SAndroid Build Coastguard Worker namespace
34*8975f5c5SAndroid Build Coastguard Worker {
35*8975f5c5SAndroid Build Coastguard Worker
36*8975f5c5SAndroid Build Coastguard Worker // Limit decompressed programs to 10MB. If they're larger then this there is a good chance the data
37*8975f5c5SAndroid Build Coastguard Worker // is not what we expect. This limits the amount of memory we will allocate based on a binary blob
38*8975f5c5SAndroid Build Coastguard Worker // we believe is compressed data.
39*8975f5c5SAndroid Build Coastguard Worker static constexpr size_t kMaxUncompressedProgramSize = 10 * 1024 * 1024;
40*8975f5c5SAndroid Build Coastguard Worker
WriteProgramBindings(BinaryOutputStream * stream,const ProgramBindings & bindings)41*8975f5c5SAndroid Build Coastguard Worker void WriteProgramBindings(BinaryOutputStream *stream, const ProgramBindings &bindings)
42*8975f5c5SAndroid Build Coastguard Worker {
43*8975f5c5SAndroid Build Coastguard Worker for (const auto &binding : bindings.getStableIterationMap())
44*8975f5c5SAndroid Build Coastguard Worker {
45*8975f5c5SAndroid Build Coastguard Worker stream->writeString(binding.first);
46*8975f5c5SAndroid Build Coastguard Worker stream->writeInt(binding.second);
47*8975f5c5SAndroid Build Coastguard Worker }
48*8975f5c5SAndroid Build Coastguard Worker }
49*8975f5c5SAndroid Build Coastguard Worker
WriteProgramAliasedBindings(BinaryOutputStream * stream,const ProgramAliasedBindings & bindings)50*8975f5c5SAndroid Build Coastguard Worker void WriteProgramAliasedBindings(BinaryOutputStream *stream, const ProgramAliasedBindings &bindings)
51*8975f5c5SAndroid Build Coastguard Worker {
52*8975f5c5SAndroid Build Coastguard Worker for (const auto &binding : bindings.getStableIterationMap())
53*8975f5c5SAndroid Build Coastguard Worker {
54*8975f5c5SAndroid Build Coastguard Worker stream->writeString(binding.first);
55*8975f5c5SAndroid Build Coastguard Worker stream->writeInt(binding.second.location);
56*8975f5c5SAndroid Build Coastguard Worker }
57*8975f5c5SAndroid Build Coastguard Worker }
58*8975f5c5SAndroid Build Coastguard Worker
59*8975f5c5SAndroid Build Coastguard Worker } // anonymous namespace
60*8975f5c5SAndroid Build Coastguard Worker
MemoryProgramCache(egl::BlobCache & blobCache)61*8975f5c5SAndroid Build Coastguard Worker MemoryProgramCache::MemoryProgramCache(egl::BlobCache &blobCache) : mBlobCache(blobCache) {}
62*8975f5c5SAndroid Build Coastguard Worker
~MemoryProgramCache()63*8975f5c5SAndroid Build Coastguard Worker MemoryProgramCache::~MemoryProgramCache() {}
64*8975f5c5SAndroid Build Coastguard Worker
ComputeHash(const Context * context,const Program * program,egl::BlobCache::Key * hashOut)65*8975f5c5SAndroid Build Coastguard Worker void MemoryProgramCache::ComputeHash(const Context *context,
66*8975f5c5SAndroid Build Coastguard Worker const Program *program,
67*8975f5c5SAndroid Build Coastguard Worker egl::BlobCache::Key *hashOut)
68*8975f5c5SAndroid Build Coastguard Worker {
69*8975f5c5SAndroid Build Coastguard Worker // Compute the program hash. Start with the shader hashes.
70*8975f5c5SAndroid Build Coastguard Worker BinaryOutputStream hashStream;
71*8975f5c5SAndroid Build Coastguard Worker ShaderBitSet shaders;
72*8975f5c5SAndroid Build Coastguard Worker for (ShaderType shaderType : AllShaderTypes())
73*8975f5c5SAndroid Build Coastguard Worker {
74*8975f5c5SAndroid Build Coastguard Worker Shader *shader = program->getAttachedShader(shaderType);
75*8975f5c5SAndroid Build Coastguard Worker if (shader)
76*8975f5c5SAndroid Build Coastguard Worker {
77*8975f5c5SAndroid Build Coastguard Worker shaders.set(shaderType);
78*8975f5c5SAndroid Build Coastguard Worker shader->writeShaderKey(&hashStream);
79*8975f5c5SAndroid Build Coastguard Worker }
80*8975f5c5SAndroid Build Coastguard Worker }
81*8975f5c5SAndroid Build Coastguard Worker
82*8975f5c5SAndroid Build Coastguard Worker hashStream.writeInt(shaders.bits());
83*8975f5c5SAndroid Build Coastguard Worker
84*8975f5c5SAndroid Build Coastguard Worker // Add some ANGLE metadata and Context properties, such as version and back-end.
85*8975f5c5SAndroid Build Coastguard Worker hashStream.writeString(angle::GetANGLEShaderProgramVersion());
86*8975f5c5SAndroid Build Coastguard Worker hashStream.writeInt(angle::GetANGLESHVersion());
87*8975f5c5SAndroid Build Coastguard Worker hashStream.writeInt(context->getClientMajorVersion());
88*8975f5c5SAndroid Build Coastguard Worker hashStream.writeInt(context->getClientMinorVersion());
89*8975f5c5SAndroid Build Coastguard Worker hashStream.writeString(reinterpret_cast<const char *>(context->getString(GL_RENDERER)));
90*8975f5c5SAndroid Build Coastguard Worker
91*8975f5c5SAndroid Build Coastguard Worker // Hash pre-link program properties.
92*8975f5c5SAndroid Build Coastguard Worker WriteProgramBindings(&hashStream, program->getAttributeBindings());
93*8975f5c5SAndroid Build Coastguard Worker WriteProgramAliasedBindings(&hashStream, program->getUniformLocationBindings());
94*8975f5c5SAndroid Build Coastguard Worker WriteProgramAliasedBindings(&hashStream, program->getFragmentOutputLocations());
95*8975f5c5SAndroid Build Coastguard Worker WriteProgramAliasedBindings(&hashStream, program->getFragmentOutputIndexes());
96*8975f5c5SAndroid Build Coastguard Worker for (const std::string &transformFeedbackVaryingName :
97*8975f5c5SAndroid Build Coastguard Worker program->getState().getTransformFeedbackVaryingNames())
98*8975f5c5SAndroid Build Coastguard Worker {
99*8975f5c5SAndroid Build Coastguard Worker hashStream.writeString(transformFeedbackVaryingName);
100*8975f5c5SAndroid Build Coastguard Worker }
101*8975f5c5SAndroid Build Coastguard Worker hashStream.writeInt(program->getTransformFeedbackBufferMode());
102*8975f5c5SAndroid Build Coastguard Worker
103*8975f5c5SAndroid Build Coastguard Worker // Include the status of FrameCapture, which adds source strings to the binary
104*8975f5c5SAndroid Build Coastguard Worker hashStream.writeBool(context->getShareGroup()->getFrameCaptureShared()->enabled());
105*8975f5c5SAndroid Build Coastguard Worker
106*8975f5c5SAndroid Build Coastguard Worker // Call the secure SHA hashing function.
107*8975f5c5SAndroid Build Coastguard Worker const std::vector<uint8_t> &programKey = hashStream.getData();
108*8975f5c5SAndroid Build Coastguard Worker angle::base::SHA1HashBytes(programKey.data(), programKey.size(), hashOut->data());
109*8975f5c5SAndroid Build Coastguard Worker }
110*8975f5c5SAndroid Build Coastguard Worker
getProgram(const Context * context,Program * program,egl::BlobCache::Key * hashOut,egl::CacheGetResult * resultOut)111*8975f5c5SAndroid Build Coastguard Worker angle::Result MemoryProgramCache::getProgram(const Context *context,
112*8975f5c5SAndroid Build Coastguard Worker Program *program,
113*8975f5c5SAndroid Build Coastguard Worker egl::BlobCache::Key *hashOut,
114*8975f5c5SAndroid Build Coastguard Worker egl::CacheGetResult *resultOut)
115*8975f5c5SAndroid Build Coastguard Worker {
116*8975f5c5SAndroid Build Coastguard Worker *resultOut = egl::CacheGetResult::NotFound;
117*8975f5c5SAndroid Build Coastguard Worker
118*8975f5c5SAndroid Build Coastguard Worker // If caching is effectively disabled, don't bother calculating the hash.
119*8975f5c5SAndroid Build Coastguard Worker if (!mBlobCache.isCachingEnabled(context))
120*8975f5c5SAndroid Build Coastguard Worker {
121*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
122*8975f5c5SAndroid Build Coastguard Worker }
123*8975f5c5SAndroid Build Coastguard Worker
124*8975f5c5SAndroid Build Coastguard Worker ComputeHash(context, program, hashOut);
125*8975f5c5SAndroid Build Coastguard Worker
126*8975f5c5SAndroid Build Coastguard Worker angle::MemoryBuffer uncompressedData;
127*8975f5c5SAndroid Build Coastguard Worker switch (mBlobCache.getAndDecompress(context, context->getScratchBuffer(), *hashOut,
128*8975f5c5SAndroid Build Coastguard Worker kMaxUncompressedProgramSize, &uncompressedData))
129*8975f5c5SAndroid Build Coastguard Worker {
130*8975f5c5SAndroid Build Coastguard Worker case egl::BlobCache::GetAndDecompressResult::NotFound:
131*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
132*8975f5c5SAndroid Build Coastguard Worker
133*8975f5c5SAndroid Build Coastguard Worker case egl::BlobCache::GetAndDecompressResult::DecompressFailure:
134*8975f5c5SAndroid Build Coastguard Worker ANGLE_PERF_WARNING(context->getState().getDebug(), GL_DEBUG_SEVERITY_LOW,
135*8975f5c5SAndroid Build Coastguard Worker "Error decompressing program binary data fetched from cache.");
136*8975f5c5SAndroid Build Coastguard Worker remove(*hashOut);
137*8975f5c5SAndroid Build Coastguard Worker // Consider this blob "not found". As far as the rest of the code is considered,
138*8975f5c5SAndroid Build Coastguard Worker // corrupted cache might as well not have existed.
139*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
140*8975f5c5SAndroid Build Coastguard Worker
141*8975f5c5SAndroid Build Coastguard Worker case egl::BlobCache::GetAndDecompressResult::Success:
142*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(program->loadBinary(context, uncompressedData.data(),
143*8975f5c5SAndroid Build Coastguard Worker static_cast<int>(uncompressedData.size()), resultOut));
144*8975f5c5SAndroid Build Coastguard Worker
145*8975f5c5SAndroid Build Coastguard Worker // Result is either Success or Rejected
146*8975f5c5SAndroid Build Coastguard Worker ASSERT(*resultOut != egl::CacheGetResult::NotFound);
147*8975f5c5SAndroid Build Coastguard Worker
148*8975f5c5SAndroid Build Coastguard Worker // If cache load failed, evict the entry
149*8975f5c5SAndroid Build Coastguard Worker if (*resultOut == egl::CacheGetResult::Rejected)
150*8975f5c5SAndroid Build Coastguard Worker {
151*8975f5c5SAndroid Build Coastguard Worker ANGLE_PERF_WARNING(context->getState().getDebug(), GL_DEBUG_SEVERITY_LOW,
152*8975f5c5SAndroid Build Coastguard Worker "Failed to load program binary from cache.");
153*8975f5c5SAndroid Build Coastguard Worker remove(*hashOut);
154*8975f5c5SAndroid Build Coastguard Worker }
155*8975f5c5SAndroid Build Coastguard Worker
156*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
157*8975f5c5SAndroid Build Coastguard Worker }
158*8975f5c5SAndroid Build Coastguard Worker
159*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
160*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
161*8975f5c5SAndroid Build Coastguard Worker }
162*8975f5c5SAndroid Build Coastguard Worker
getAt(size_t index,const egl::BlobCache::Key ** hashOut,egl::BlobCache::Value * programOut)163*8975f5c5SAndroid Build Coastguard Worker bool MemoryProgramCache::getAt(size_t index,
164*8975f5c5SAndroid Build Coastguard Worker const egl::BlobCache::Key **hashOut,
165*8975f5c5SAndroid Build Coastguard Worker egl::BlobCache::Value *programOut)
166*8975f5c5SAndroid Build Coastguard Worker {
167*8975f5c5SAndroid Build Coastguard Worker return mBlobCache.getAt(index, hashOut, programOut);
168*8975f5c5SAndroid Build Coastguard Worker }
169*8975f5c5SAndroid Build Coastguard Worker
remove(const egl::BlobCache::Key & programHash)170*8975f5c5SAndroid Build Coastguard Worker void MemoryProgramCache::remove(const egl::BlobCache::Key &programHash)
171*8975f5c5SAndroid Build Coastguard Worker {
172*8975f5c5SAndroid Build Coastguard Worker mBlobCache.remove(programHash);
173*8975f5c5SAndroid Build Coastguard Worker }
174*8975f5c5SAndroid Build Coastguard Worker
putProgram(const egl::BlobCache::Key & programHash,const Context * context,Program * program)175*8975f5c5SAndroid Build Coastguard Worker angle::Result MemoryProgramCache::putProgram(const egl::BlobCache::Key &programHash,
176*8975f5c5SAndroid Build Coastguard Worker const Context *context,
177*8975f5c5SAndroid Build Coastguard Worker Program *program)
178*8975f5c5SAndroid Build Coastguard Worker {
179*8975f5c5SAndroid Build Coastguard Worker // If caching is effectively disabled, don't bother serializing the program.
180*8975f5c5SAndroid Build Coastguard Worker if (!mBlobCache.isCachingEnabled(context))
181*8975f5c5SAndroid Build Coastguard Worker {
182*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
183*8975f5c5SAndroid Build Coastguard Worker }
184*8975f5c5SAndroid Build Coastguard Worker
185*8975f5c5SAndroid Build Coastguard Worker ANGLE_TRY(program->serialize(context));
186*8975f5c5SAndroid Build Coastguard Worker const angle::MemoryBuffer &serializedProgram = program->getSerializedBinary();
187*8975f5c5SAndroid Build Coastguard Worker
188*8975f5c5SAndroid Build Coastguard Worker angle::MemoryBuffer compressedData;
189*8975f5c5SAndroid Build Coastguard Worker if (!angle::CompressBlob(serializedProgram.size(), serializedProgram.data(), &compressedData))
190*8975f5c5SAndroid Build Coastguard Worker {
191*8975f5c5SAndroid Build Coastguard Worker ANGLE_PERF_WARNING(context->getState().getDebug(), GL_DEBUG_SEVERITY_LOW,
192*8975f5c5SAndroid Build Coastguard Worker "Error compressing binary data.");
193*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
194*8975f5c5SAndroid Build Coastguard Worker }
195*8975f5c5SAndroid Build Coastguard Worker
196*8975f5c5SAndroid Build Coastguard Worker {
197*8975f5c5SAndroid Build Coastguard Worker std::scoped_lock<angle::SimpleMutex> lock(mBlobCache.getMutex());
198*8975f5c5SAndroid Build Coastguard Worker // TODO: http://anglebug.com/42266037
199*8975f5c5SAndroid Build Coastguard Worker // This was a workaround for Chrome until it added support for EGL_ANDROID_blob_cache,
200*8975f5c5SAndroid Build Coastguard Worker // tracked by http://anglebug.com/42261225. This issue has since been closed, but removing
201*8975f5c5SAndroid Build Coastguard Worker // this still causes a test failure.
202*8975f5c5SAndroid Build Coastguard Worker auto *platform = ANGLEPlatformCurrent();
203*8975f5c5SAndroid Build Coastguard Worker platform->cacheProgram(platform, programHash, compressedData.size(), compressedData.data());
204*8975f5c5SAndroid Build Coastguard Worker }
205*8975f5c5SAndroid Build Coastguard Worker
206*8975f5c5SAndroid Build Coastguard Worker mBlobCache.put(context, programHash, std::move(compressedData));
207*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
208*8975f5c5SAndroid Build Coastguard Worker }
209*8975f5c5SAndroid Build Coastguard Worker
updateProgram(const Context * context,Program * program)210*8975f5c5SAndroid Build Coastguard Worker angle::Result MemoryProgramCache::updateProgram(const Context *context, Program *program)
211*8975f5c5SAndroid Build Coastguard Worker {
212*8975f5c5SAndroid Build Coastguard Worker egl::BlobCache::Key programHash;
213*8975f5c5SAndroid Build Coastguard Worker ComputeHash(context, program, &programHash);
214*8975f5c5SAndroid Build Coastguard Worker return putProgram(programHash, context, program);
215*8975f5c5SAndroid Build Coastguard Worker }
216*8975f5c5SAndroid Build Coastguard Worker
putBinary(const egl::BlobCache::Key & programHash,const uint8_t * binary,size_t length)217*8975f5c5SAndroid Build Coastguard Worker bool MemoryProgramCache::putBinary(const egl::BlobCache::Key &programHash,
218*8975f5c5SAndroid Build Coastguard Worker const uint8_t *binary,
219*8975f5c5SAndroid Build Coastguard Worker size_t length)
220*8975f5c5SAndroid Build Coastguard Worker {
221*8975f5c5SAndroid Build Coastguard Worker // Copy the binary.
222*8975f5c5SAndroid Build Coastguard Worker angle::MemoryBuffer newEntry;
223*8975f5c5SAndroid Build Coastguard Worker if (!newEntry.resize(length))
224*8975f5c5SAndroid Build Coastguard Worker {
225*8975f5c5SAndroid Build Coastguard Worker return false;
226*8975f5c5SAndroid Build Coastguard Worker }
227*8975f5c5SAndroid Build Coastguard Worker memcpy(newEntry.data(), binary, length);
228*8975f5c5SAndroid Build Coastguard Worker
229*8975f5c5SAndroid Build Coastguard Worker // Store the binary.
230*8975f5c5SAndroid Build Coastguard Worker mBlobCache.populate(programHash, std::move(newEntry));
231*8975f5c5SAndroid Build Coastguard Worker
232*8975f5c5SAndroid Build Coastguard Worker return true;
233*8975f5c5SAndroid Build Coastguard Worker }
234*8975f5c5SAndroid Build Coastguard Worker
clear()235*8975f5c5SAndroid Build Coastguard Worker void MemoryProgramCache::clear()
236*8975f5c5SAndroid Build Coastguard Worker {
237*8975f5c5SAndroid Build Coastguard Worker mBlobCache.clear();
238*8975f5c5SAndroid Build Coastguard Worker }
239*8975f5c5SAndroid Build Coastguard Worker
resize(size_t maxCacheSizeBytes)240*8975f5c5SAndroid Build Coastguard Worker void MemoryProgramCache::resize(size_t maxCacheSizeBytes)
241*8975f5c5SAndroid Build Coastguard Worker {
242*8975f5c5SAndroid Build Coastguard Worker mBlobCache.resize(maxCacheSizeBytes);
243*8975f5c5SAndroid Build Coastguard Worker }
244*8975f5c5SAndroid Build Coastguard Worker
entryCount() const245*8975f5c5SAndroid Build Coastguard Worker size_t MemoryProgramCache::entryCount() const
246*8975f5c5SAndroid Build Coastguard Worker {
247*8975f5c5SAndroid Build Coastguard Worker return mBlobCache.entryCount();
248*8975f5c5SAndroid Build Coastguard Worker }
249*8975f5c5SAndroid Build Coastguard Worker
trim(size_t limit)250*8975f5c5SAndroid Build Coastguard Worker size_t MemoryProgramCache::trim(size_t limit)
251*8975f5c5SAndroid Build Coastguard Worker {
252*8975f5c5SAndroid Build Coastguard Worker return mBlobCache.trim(limit);
253*8975f5c5SAndroid Build Coastguard Worker }
254*8975f5c5SAndroid Build Coastguard Worker
size() const255*8975f5c5SAndroid Build Coastguard Worker size_t MemoryProgramCache::size() const
256*8975f5c5SAndroid Build Coastguard Worker {
257*8975f5c5SAndroid Build Coastguard Worker return mBlobCache.size();
258*8975f5c5SAndroid Build Coastguard Worker }
259*8975f5c5SAndroid Build Coastguard Worker
maxSize() const260*8975f5c5SAndroid Build Coastguard Worker size_t MemoryProgramCache::maxSize() const
261*8975f5c5SAndroid Build Coastguard Worker {
262*8975f5c5SAndroid Build Coastguard Worker return mBlobCache.maxSize();
263*8975f5c5SAndroid Build Coastguard Worker }
264*8975f5c5SAndroid Build Coastguard Worker
265*8975f5c5SAndroid Build Coastguard Worker } // namespace gl
266