blob: c6483ac3166333ad41e014be97c7a0b895904ee7 [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>
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050024#include <SkPixelRef.h>
Romain Guye3b0a012013-06-26 15:45:41 -070025
Chet Haase5c13d892010-10-08 08:37:55 -070026#include <utils/KeyedVector.h>
John Recka35778c72014-11-06 09:45:10 -080027#include <utils/Singleton.h>
Romain Guye3b0a012013-06-26 15:45:41 -070028
29#include <androidfw/ResourceTypes.h>
30
Chet Haase5c13d892010-10-08 08:37:55 -070031namespace android {
32namespace uirenderer {
33
Tom Hudson2dc236b2014-10-15 15:46:42 -040034class Layer;
35
Chet Haase5c13d892010-10-08 08:37:55 -070036/**
37 * Type of Resource being cached
38 */
39enum ResourceType {
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
Chet Haase5c13d892010-10-08 08:37:55 -070047 ResourceReference(ResourceType type) {
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050048 refCount = 0; destroyed = false; resourceType = type;
Chet Haase5c13d892010-10-08 08:37:55 -070049 }
50
51 int refCount;
Chet Haase5c13d892010-10-08 08:37:55 -070052 bool destroyed;
53 ResourceType resourceType;
54};
55
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050056class BitmapKey {
57public:
58 BitmapKey(const SkBitmap* bitmap)
59 : mRefCount(1)
60 , mBitmapDimensions(bitmap->dimensions())
61 , mPixelRefOrigin(bitmap->pixelRefOrigin())
62 , mPixelRefStableID(bitmap->pixelRef()->getStableID()) { }
63
64 void operator=(const BitmapKey& other);
65 bool operator==(const BitmapKey& other) const;
66 bool operator<(const BitmapKey& other) const;
67
68private:
69 // This constructor is only used by the KeyedVector implementation
70 BitmapKey()
71 : mRefCount(-1)
72 , mBitmapDimensions(SkISize::Make(0,0))
73 , mPixelRefOrigin(SkIPoint::Make(0,0))
74 , mPixelRefStableID(0) { }
75
76 // reference count of all HWUI object using this bitmap
77 mutable int mRefCount;
78
79 SkISize mBitmapDimensions;
80 SkIPoint mPixelRefOrigin;
81 uint32_t mPixelRefStableID;
82
83 friend class ResourceCache;
84 friend class android::key_value_pair_t<BitmapKey, SkBitmap*>;
85};
86
John Recka35778c72014-11-06 09:45:10 -080087class ANDROID_API ResourceCache: public Singleton<ResourceCache> {
Chet Haase5c13d892010-10-08 08:37:55 -070088 ResourceCache();
89 ~ResourceCache();
Romain Guy58ecc202012-09-07 11:58:36 -070090
John Recka35778c72014-11-06 09:45:10 -080091 friend class Singleton<ResourceCache>;
92
93public:
94
Romain Guy58ecc202012-09-07 11:58:36 -070095 /**
96 * When using these two methods, make sure to only invoke the *Locked()
97 * variants of increment/decrementRefcount(), recyle() and destructor()
98 */
99 void lock();
100 void unlock();
101
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500102 /**
103 * The cache stores a copy of the provided resource or refs an existing resource
104 * if the bitmap has previously been inserted and returns the cached copy.
105 */
106 const SkBitmap* insert(const SkBitmap* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700107
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500108 void incrementRefcount(const SkPath* resource);
109 void incrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700110
Chris Craikd218a922014-01-02 17:13:34 -0800111 void decrementRefcount(const SkBitmap* resource);
112 void decrementRefcount(const SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -0800113 void decrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700114
Chris Craikd218a922014-01-02 17:13:34 -0800115 void decrementRefcountLocked(const SkBitmap* resource);
116 void decrementRefcountLocked(const SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -0800117 void decrementRefcountLocked(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700118
Chet Haase5a7e8282011-02-04 12:50:55 -0800119 void destructor(SkPath* resource);
Romain Guye3b0a012013-06-26 15:45:41 -0700120 void destructor(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700121
122 void destructorLocked(SkPath* resource);
Romain Guye3b0a012013-06-26 15:45:41 -0700123 void destructorLocked(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700124
Chet Haase5c13d892010-10-08 08:37:55 -0700125private:
Chris Craikd218a922014-01-02 17:13:34 -0800126 void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
Romain Guy58ecc202012-09-07 11:58:36 -0700127
Chet Haase5c13d892010-10-08 08:37:55 -0700128 void incrementRefcount(void* resource, ResourceType resourceType);
Romain Guy58ecc202012-09-07 11:58:36 -0700129 void incrementRefcountLocked(void* resource, ResourceType resourceType);
130
131 void decrementRefcount(void* resource);
132 void decrementRefcountLocked(void* resource);
133
Chet Haase5c13d892010-10-08 08:37:55 -0700134 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -0800135
136 /**
137 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
138 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
139 * or a reference queue finalization thread.
140 */
141 mutable Mutex mLock;
Romain Guy58ecc202012-09-07 11:58:36 -0700142
Chris Craikd218a922014-01-02 17:13:34 -0800143 KeyedVector<const void*, ResourceReference*>* mCache;
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500144 KeyedVector<BitmapKey, SkBitmap*> mBitmapCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700145};
146
147}; // namespace uirenderer
148}; // namespace android
149
Romain Guy5b3b3522010-10-27 18:57:51 -0700150#endif // ANDROID_HWUI_RESOURCE_CACHE_H