blob: fae55d14fead55f0c083b30047dfb22a202ae542 [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>
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050023#include <SkPixelRef.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 {
Romain Guye3b0a012013-06-26 15:45:41 -070039 kNinePatch,
Chet Haase5c13d892010-10-08 08:37:55 -070040};
41
42class ResourceReference {
43public:
44
Chet Haase5c13d892010-10-08 08:37:55 -070045 ResourceReference(ResourceType type) {
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050046 refCount = 0; destroyed = false; resourceType = type;
Chet Haase5c13d892010-10-08 08:37:55 -070047 }
48
49 int refCount;
Chet Haase5c13d892010-10-08 08:37:55 -070050 bool destroyed;
51 ResourceType resourceType;
52};
53
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050054class BitmapKey {
55public:
56 BitmapKey(const SkBitmap* bitmap)
57 : mRefCount(1)
58 , mBitmapDimensions(bitmap->dimensions())
59 , mPixelRefOrigin(bitmap->pixelRefOrigin())
60 , mPixelRefStableID(bitmap->pixelRef()->getStableID()) { }
61
62 void operator=(const BitmapKey& other);
63 bool operator==(const BitmapKey& other) const;
64 bool operator<(const BitmapKey& other) const;
65
66private:
67 // This constructor is only used by the KeyedVector implementation
68 BitmapKey()
69 : mRefCount(-1)
70 , mBitmapDimensions(SkISize::Make(0,0))
71 , mPixelRefOrigin(SkIPoint::Make(0,0))
72 , mPixelRefStableID(0) { }
73
74 // reference count of all HWUI object using this bitmap
75 mutable int mRefCount;
76
77 SkISize mBitmapDimensions;
78 SkIPoint mPixelRefOrigin;
79 uint32_t mPixelRefStableID;
80
81 friend class ResourceCache;
Chris Craik03188872015-02-02 18:39:33 -080082 friend struct android::key_value_pair_t<BitmapKey, SkBitmap*>;
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050083};
84
John Recka35778c72014-11-06 09:45:10 -080085class ANDROID_API ResourceCache: public Singleton<ResourceCache> {
Chet Haase5c13d892010-10-08 08:37:55 -070086 ResourceCache();
87 ~ResourceCache();
Romain Guy58ecc202012-09-07 11:58:36 -070088
John Recka35778c72014-11-06 09:45:10 -080089 friend class Singleton<ResourceCache>;
90
91public:
92
Romain Guy58ecc202012-09-07 11:58:36 -070093 /**
94 * When using these two methods, make sure to only invoke the *Locked()
95 * variants of increment/decrementRefcount(), recyle() and destructor()
96 */
97 void lock();
98 void unlock();
99
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500100 /**
101 * The cache stores a copy of the provided resource or refs an existing resource
102 * if the bitmap has previously been inserted and returns the cached copy.
103 */
104 const SkBitmap* insert(const SkBitmap* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700105
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500106 void incrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700107
Chris Craikd218a922014-01-02 17:13:34 -0800108 void decrementRefcount(const SkBitmap* resource);
Chris Craikd218a922014-01-02 17:13:34 -0800109 void decrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700110
Chris Craikd218a922014-01-02 17:13:34 -0800111 void decrementRefcountLocked(const SkBitmap* resource);
Chris Craikd218a922014-01-02 17:13:34 -0800112 void decrementRefcountLocked(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700113
Romain Guye3b0a012013-06-26 15:45:41 -0700114 void destructor(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700115
Romain Guye3b0a012013-06-26 15:45:41 -0700116 void destructorLocked(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700117
Chet Haase5c13d892010-10-08 08:37:55 -0700118private:
Chris Craikd218a922014-01-02 17:13:34 -0800119 void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
Romain Guy58ecc202012-09-07 11:58:36 -0700120
Chet Haase5c13d892010-10-08 08:37:55 -0700121 void incrementRefcount(void* resource, ResourceType resourceType);
Romain Guy58ecc202012-09-07 11:58:36 -0700122 void incrementRefcountLocked(void* resource, ResourceType resourceType);
123
124 void decrementRefcount(void* resource);
125 void decrementRefcountLocked(void* resource);
126
Chet Haase5c13d892010-10-08 08:37:55 -0700127 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -0800128
129 /**
130 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
131 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
132 * or a reference queue finalization thread.
133 */
134 mutable Mutex mLock;
Romain Guy58ecc202012-09-07 11:58:36 -0700135
Chris Craikd218a922014-01-02 17:13:34 -0800136 KeyedVector<const void*, ResourceReference*>* mCache;
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500137 KeyedVector<BitmapKey, SkBitmap*> mBitmapCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700138};
139
140}; // namespace uirenderer
141}; // namespace android
142
Romain Guy5b3b3522010-10-27 18:57:51 -0700143#endif // ANDROID_HWUI_RESOURCE_CACHE_H