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 #ifndef CACHEMANAGER_H 18 #define CACHEMANAGER_H 19 20 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration 21 #include <GrDirectContext.h> 22 #endif 23 #include <SkSurface.h> 24 #include <utils/String8.h> 25 #include <vector> 26 #include "utils/TimeUtils.h" 27 28 namespace android { 29 30 class Surface; 31 32 namespace uirenderer { 33 34 class RenderState; 35 36 namespace renderthread { 37 38 class IRenderPipeline; 39 class RenderThread; 40 41 class CacheManager { 42 public: 43 enum class TrimMemoryMode { Complete, UiHidden }; 44 45 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration 46 void configureContext(GrContextOptions* context, const void* identity, ssize_t size); 47 #endif 48 void trimMemory(TrimMemoryMode mode); 49 void trimStaleResources(); 50 void dumpMemoryUsage(String8& log, const RenderState* renderState = nullptr); 51 void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage); 52 getCacheSize()53 size_t getCacheSize() const { return mMaxResourceBytes; } getBackgroundCacheSize()54 size_t getBackgroundCacheSize() const { return mBackgroundResourceBytes; } 55 void onFrameCompleted(); 56 57 void performDeferredCleanup(nsecs_t cleanupOlderThanMillis); 58 59 private: 60 friend class RenderThread; 61 62 explicit CacheManager(); 63 64 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration 65 void reset(sk_sp<GrDirectContext> grContext); 66 #endif 67 void destroy(); 68 69 const size_t mMaxSurfaceArea; 70 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration 71 sk_sp<GrDirectContext> mGrContext; 72 #endif 73 74 const size_t mMaxResourceBytes; 75 const size_t mBackgroundResourceBytes; 76 77 const size_t mMaxGpuFontAtlasBytes; 78 const size_t mMaxCpuFontCacheBytes; 79 const size_t mBackgroundCpuFontCacheBytes; 80 }; 81 82 } /* namespace renderthread */ 83 } /* namespace uirenderer */ 84 } /* namespace android */ 85 86 #endif /* CACHEMANAGER_H */ 87