blob: c6cd7bfd0bf2ebf98bc29f46652b1888563ac2bc [file] [log] [blame]
Chet Haase5c13d892010-10-08 08:37:55 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_RESOURCE_CACHE_H
18#define ANDROID_HWUI_RESOURCE_CACHE_H
Chet Haase5c13d892010-10-08 08:37:55 -070019
Romain Guy79537452011-10-12 13:48:51 -070020#include <cutils/compiler.h>
21
Chet Haase5c13d892010-10-08 08:37:55 -070022#include <SkBitmap.h>
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include <SkPath.h>
Romain Guye3b0a012013-06-26 15:45:41 -070024
Chet Haase5c13d892010-10-08 08:37:55 -070025#include <utils/KeyedVector.h>
John Recka35778c72014-11-06 09:45:10 -080026#include <utils/Singleton.h>
Romain Guye3b0a012013-06-26 15:45:41 -070027
28#include <androidfw/ResourceTypes.h>
29
Chet Haase5c13d892010-10-08 08:37:55 -070030namespace android {
31namespace uirenderer {
32
Tom Hudson2dc236b2014-10-15 15:46:42 -040033class Layer;
34
Chet Haase5c13d892010-10-08 08:37:55 -070035/**
36 * Type of Resource being cached
37 */
38enum ResourceType {
39 kBitmap,
Romain Guye3b0a012013-06-26 15:45:41 -070040 kNinePatch,
John Reck0e89e2b2014-10-31 14:49:06 -070041 kPath
Chet Haase5c13d892010-10-08 08:37:55 -070042};
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
John Recka35778c72014-11-06 09:45:10 -080058class ANDROID_API ResourceCache: public Singleton<ResourceCache> {
Chet Haase5c13d892010-10-08 08:37:55 -070059 ResourceCache();
60 ~ResourceCache();
Romain Guy58ecc202012-09-07 11:58:36 -070061
John Recka35778c72014-11-06 09:45:10 -080062 friend class Singleton<ResourceCache>;
63
64public:
65
Romain Guy58ecc202012-09-07 11:58:36 -070066 /**
67 * When using these two methods, make sure to only invoke the *Locked()
68 * variants of increment/decrementRefcount(), recyle() and destructor()
69 */
70 void lock();
71 void unlock();
72
Chris Craikd218a922014-01-02 17:13:34 -080073 void incrementRefcount(const SkPath* resource);
74 void incrementRefcount(const SkBitmap* resource);
Chris Craikd218a922014-01-02 17:13:34 -080075 void incrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070076
Chris Craikd218a922014-01-02 17:13:34 -080077 void incrementRefcountLocked(const SkPath* resource);
78 void incrementRefcountLocked(const SkBitmap* resource);
Chris Craikd218a922014-01-02 17:13:34 -080079 void incrementRefcountLocked(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070080
Chris Craikd218a922014-01-02 17:13:34 -080081 void decrementRefcount(const SkBitmap* resource);
82 void decrementRefcount(const SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -080083 void decrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070084
Chris Craikd218a922014-01-02 17:13:34 -080085 void decrementRefcountLocked(const SkBitmap* resource);
86 void decrementRefcountLocked(const SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -080087 void decrementRefcountLocked(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070088
Chet Haase5a7e8282011-02-04 12:50:55 -080089 void destructor(SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -080090 void destructor(const SkBitmap* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070091 void destructor(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070092
93 void destructorLocked(SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -080094 void destructorLocked(const SkBitmap* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070095 void destructorLocked(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070096
Chet Haase547e6652012-10-22 15:07:26 -070097 bool recycle(SkBitmap* resource);
98 bool recycleLocked(SkBitmap* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070099
Chet Haase5c13d892010-10-08 08:37:55 -0700100private:
Chris Craikd218a922014-01-02 17:13:34 -0800101 void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
Romain Guy58ecc202012-09-07 11:58:36 -0700102
Chet Haase5c13d892010-10-08 08:37:55 -0700103 void incrementRefcount(void* resource, ResourceType resourceType);
Romain Guy58ecc202012-09-07 11:58:36 -0700104 void incrementRefcountLocked(void* resource, ResourceType resourceType);
105
106 void decrementRefcount(void* resource);
107 void decrementRefcountLocked(void* resource);
108
Chet Haase5c13d892010-10-08 08:37:55 -0700109 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -0800110
111 /**
112 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
113 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
114 * or a reference queue finalization thread.
115 */
116 mutable Mutex mLock;
Romain Guy58ecc202012-09-07 11:58:36 -0700117
Chris Craikd218a922014-01-02 17:13:34 -0800118 KeyedVector<const void*, ResourceReference*>* mCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700119};
120
121}; // namespace uirenderer
122}; // namespace android
123
Romain Guy5b3b3522010-10-27 18:57:51 -0700124#endif // ANDROID_HWUI_RESOURCE_CACHE_H