• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 ANDROID_HWUI_RESOURCE_CACHE_H
18 #define ANDROID_HWUI_RESOURCE_CACHE_H
19 
20 #include <cutils/compiler.h>
21 
22 #include <SkBitmap.h>
23 #include <SkPixelRef.h>
24 
25 #include <utils/KeyedVector.h>
26 #include <utils/Singleton.h>
27 
28 #include <androidfw/ResourceTypes.h>
29 
30 namespace android {
31 namespace uirenderer {
32 
33 class Layer;
34 
35 /**
36  * Type of Resource being cached
37  */
38 enum ResourceType {
39     kNinePatch,
40 };
41 
42 class ResourceReference {
43 public:
44 
ResourceReference(ResourceType type)45     explicit ResourceReference(ResourceType type) {
46         refCount = 0; destroyed = false; resourceType = type;
47     }
48 
49     int refCount;
50     bool destroyed;
51     ResourceType resourceType;
52 };
53 
54 class ANDROID_API ResourceCache: public Singleton<ResourceCache> {
55     ResourceCache();
56     ~ResourceCache();
57 
58     friend class Singleton<ResourceCache>;
59 
60 public:
61 
62     /**
63      * When using these two methods, make sure to only invoke the *Locked()
64      * variants of increment/decrementRefcount(), recyle() and destructor()
65      */
66     void lock();
67     void unlock();
68 
69     void incrementRefcount(const Res_png_9patch* resource);
70 
71     void decrementRefcount(const Res_png_9patch* resource);
72 
73     void decrementRefcountLocked(const Res_png_9patch* resource);
74 
75     void destructor(Res_png_9patch* resource);
76 
77     void destructorLocked(Res_png_9patch* resource);
78 
79 private:
80     void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
81 
82     void incrementRefcount(void* resource, ResourceType resourceType);
83     void incrementRefcountLocked(void* resource, ResourceType resourceType);
84 
85     void decrementRefcount(void* resource);
86     void decrementRefcountLocked(void* resource);
87 
88     void logCache();
89 
90     /**
91      * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
92      * thread, but destroying resources may be called from the GC thread, the finalizer thread,
93      * or a reference queue finalization thread.
94      */
95     mutable Mutex mLock;
96 
97     KeyedVector<const void*, ResourceReference*>* mCache;
98 };
99 
100 }; // namespace uirenderer
101 }; // namespace android
102 
103 #endif // ANDROID_HWUI_RESOURCE_CACHE_H
104