ResourceCache.h revision 2dc236b2bae13b9a0ed9b3f7320502aecd7983b3
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 <SkPath.h>
24
25#include <utils/KeyedVector.h>
26
27#include <androidfw/ResourceTypes.h>
28
29namespace android {
30namespace uirenderer {
31
32class Layer;
33
34/**
35 * Type of Resource being cached
36 */
37enum ResourceType {
38    kBitmap,
39    kNinePatch,
40    kPath,
41    kLayer
42};
43
44class ResourceReference {
45public:
46
47    ResourceReference() { refCount = 0; recycled = false; destroyed = false;}
48    ResourceReference(ResourceType type) {
49        refCount = 0; recycled = false; destroyed = false; resourceType = type;
50    }
51
52    int refCount;
53    bool recycled;
54    bool destroyed;
55    ResourceType resourceType;
56};
57
58class ANDROID_API ResourceCache {
59public:
60    ResourceCache();
61    ~ResourceCache();
62
63    /**
64     * When using these two methods, make sure to only invoke the *Locked()
65     * variants of increment/decrementRefcount(), recyle() and destructor()
66     */
67    void lock();
68    void unlock();
69
70    void incrementRefcount(const SkPath* resource);
71    void incrementRefcount(const SkBitmap* resource);
72    void incrementRefcount(const Res_png_9patch* resource);
73    void incrementRefcount(Layer* resource);
74
75    void incrementRefcountLocked(const SkPath* resource);
76    void incrementRefcountLocked(const SkBitmap* resource);
77    void incrementRefcountLocked(const Res_png_9patch* resource);
78    void incrementRefcountLocked(Layer* resource);
79
80    void decrementRefcount(const SkBitmap* resource);
81    void decrementRefcount(const SkPath* resource);
82    void decrementRefcount(const Res_png_9patch* resource);
83    void decrementRefcount(Layer* resource);
84
85    void decrementRefcountLocked(const SkBitmap* resource);
86    void decrementRefcountLocked(const SkPath* resource);
87    void decrementRefcountLocked(const Res_png_9patch* resource);
88    void decrementRefcountLocked(Layer* resource);
89
90    void destructor(SkPath* resource);
91    void destructor(const SkBitmap* resource);
92    void destructor(Res_png_9patch* resource);
93
94    void destructorLocked(SkPath* resource);
95    void destructorLocked(const SkBitmap* resource);
96    void destructorLocked(Res_png_9patch* resource);
97
98    bool recycle(SkBitmap* resource);
99    bool recycleLocked(SkBitmap* resource);
100
101private:
102    void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
103
104    void incrementRefcount(void* resource, ResourceType resourceType);
105    void incrementRefcountLocked(void* resource, ResourceType resourceType);
106
107    void decrementRefcount(void* resource);
108    void decrementRefcountLocked(void* resource);
109
110    void logCache();
111
112    /**
113     * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
114     * thread, but destroying resources may be called from the GC thread, the finalizer thread,
115     * or a reference queue finalization thread.
116     */
117    mutable Mutex mLock;
118
119    KeyedVector<const void*, ResourceReference*>* mCache;
120};
121
122}; // namespace uirenderer
123}; // namespace android
124
125#endif // ANDROID_HWUI_RESOURCE_CACHE_H
126