1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_SHADER_CACHE_H 17 #define OHOS_SHADER_CACHE_H 18 #ifndef USE_ROSEN_DRAWING 19 #if defined(NEW_SKIA) 20 #include <include/gpu/GrDirectContext.h> 21 #else 22 #include <include/gpu/GrContext.h> 23 #endif 24 #include <include/gpu/GrContextOptions.h> 25 #else 26 #include "image/gpu_context.h" 27 #endif 28 #include <mutex> 29 #include <memory> 30 #include <string> 31 #include <vector> 32 #include "cache_data.h" 33 34 namespace OHOS { 35 namespace Rosen { 36 #ifndef USE_ROSEN_DRAWING 37 class ShaderCache : public GrContextOptions::PersistentCache { 38 #else 39 class ShaderCache : public Drawing::GPUContextOptions::PersistentCache { 40 #endif 41 public: 42 static ShaderCache& Instance(); 43 44 virtual void InitShaderCache(const char *identity, const size_t size, bool isUni); InitShaderCache()45 virtual void InitShaderCache() 46 { 47 InitShaderCache(nullptr, 0, false); 48 } 49 50 virtual void SetFilePath(const std::string& filename); 51 52 #ifndef USE_ROSEN_DRAWING 53 sk_sp<SkData> load(const SkData &key) override; 54 void store(const SkData &key, const SkData &data) override; 55 #else 56 std::shared_ptr<Drawing::Data> Load(const Drawing::Data &key) override; 57 void Store(const Drawing::Data &key, const Drawing::Data &data) override; 58 #endif 59 IfInitialized()60 bool IfInitialized() const 61 { 62 return initialized_; 63 } 64 65 size_t QuerryShaderSize() const; 66 67 size_t QuerryShaderNum() const; 68 69 void CleanAllShaders() const; 70 71 private: 72 ShaderCache() = default; 73 ~ShaderCache(); 74 ShaderCache(const ShaderCache &) = delete; 75 void operator=(const ShaderCache &) = delete; 76 77 void WriteToDisk(); 78 79 bool initialized_ = false; 80 std::unique_ptr<CacheData> cacheData_; 81 std::string filePath_; 82 std::vector<uint8_t> idHash_; 83 mutable std::mutex mutex_; 84 85 bool savePending_ = false; 86 unsigned int saveDelaySeconds_ = 3; 87 88 size_t bufferSize_ = 16 * 1024; 89 bool cacheDirty_ = false; 90 91 static constexpr uint8_t ID_KEY = 0; 92 93 static constexpr size_t MAX_KEY_SIZE = 1024; 94 static constexpr size_t MAX_VALUE_SIZE = MAX_KEY_SIZE * 1024; 95 static constexpr size_t MAX_TOTAL_SIZE = MAX_VALUE_SIZE * 4; 96 static constexpr size_t MAX_UNIRENDER_SIZE = MAX_VALUE_SIZE * 10; 97 }; 98 } // namespace Rosen 99 } // namespace OHOS 100 #endif // OHOS_SHADER_CACHE_H 101