blob: 4097ba42c44890adc862b24ae4fbcb20c30cf619 [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>
Chet Haase5c13d892010-10-08 08:37:55 -070023#include <SkiaShader.h>
Romain Guye3b0a012013-06-26 15:45:41 -070024
Chet Haase5c13d892010-10-08 08:37:55 -070025#include <utils/KeyedVector.h>
Romain Guye3b0a012013-06-26 15:45:41 -070026
27#include <androidfw/ResourceTypes.h>
28
Chet Haase603f6de2012-09-14 15:31:25 -070029#include "Layer.h"
Chet Haase5c13d892010-10-08 08:37:55 -070030
31namespace android {
32namespace uirenderer {
33
34/**
35 * Type of Resource being cached
36 */
37enum ResourceType {
38 kBitmap,
Chet Haase5c13d892010-10-08 08:37:55 -070039 kShader,
Romain Guye3b0a012013-06-26 15:45:41 -070040 kNinePatch,
Chet Haase5a7e8282011-02-04 12:50:55 -080041 kPath,
Chet Haase603f6de2012-09-14 15:31:25 -070042 kLayer
Chet Haase5c13d892010-10-08 08:37:55 -070043};
44
45class ResourceReference {
46public:
47
48 ResourceReference() { refCount = 0; recycled = false; destroyed = false;}
49 ResourceReference(ResourceType type) {
50 refCount = 0; recycled = false; destroyed = false; resourceType = type;
51 }
52
53 int refCount;
54 bool recycled;
55 bool destroyed;
56 ResourceType resourceType;
57};
58
Romain Guy79537452011-10-12 13:48:51 -070059class ANDROID_API ResourceCache {
Chet Haase5c13d892010-10-08 08:37:55 -070060public:
61 ResourceCache();
62 ~ResourceCache();
Romain Guy58ecc202012-09-07 11:58:36 -070063
64 /**
65 * When using these two methods, make sure to only invoke the *Locked()
66 * variants of increment/decrementRefcount(), recyle() and destructor()
67 */
68 void lock();
69 void unlock();
70
Chris Craikd218a922014-01-02 17:13:34 -080071 void incrementRefcount(const SkPath* resource);
72 void incrementRefcount(const SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070073 void incrementRefcount(SkiaShader* resource);
Chris Craikd218a922014-01-02 17:13:34 -080074 void incrementRefcount(const Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070075 void incrementRefcount(Layer* 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);
Romain Guy58ecc202012-09-07 11:58:36 -070079 void incrementRefcountLocked(SkiaShader* resource);
Chris Craikd218a922014-01-02 17:13:34 -080080 void incrementRefcountLocked(const Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070081 void incrementRefcountLocked(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070082
Chris Craikd218a922014-01-02 17:13:34 -080083 void decrementRefcount(const SkBitmap* resource);
84 void decrementRefcount(const SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070085 void decrementRefcount(SkiaShader* resource);
Chris Craikd218a922014-01-02 17:13:34 -080086 void decrementRefcount(const Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070087 void decrementRefcount(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070088
Chris Craikd218a922014-01-02 17:13:34 -080089 void decrementRefcountLocked(const SkBitmap* resource);
90 void decrementRefcountLocked(const SkPath* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070091 void decrementRefcountLocked(SkiaShader* resource);
Chris Craikd218a922014-01-02 17:13:34 -080092 void decrementRefcountLocked(const Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070093 void decrementRefcountLocked(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070094
Chet Haase5a7e8282011-02-04 12:50:55 -080095 void destructor(SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -080096 void destructor(const SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070097 void destructor(SkiaShader* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070098 void destructor(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070099
100 void destructorLocked(SkPath* resource);
Chris Craikd218a922014-01-02 17:13:34 -0800101 void destructorLocked(const SkBitmap* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700102 void destructorLocked(SkiaShader* resource);
Romain Guye3b0a012013-06-26 15:45:41 -0700103 void destructorLocked(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700104
Chet Haase547e6652012-10-22 15:07:26 -0700105 bool recycle(SkBitmap* resource);
106 bool recycleLocked(SkBitmap* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700107
Chet Haase5c13d892010-10-08 08:37:55 -0700108private:
Chris Craikd218a922014-01-02 17:13:34 -0800109 void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
Romain Guy58ecc202012-09-07 11:58:36 -0700110
Chet Haase5c13d892010-10-08 08:37:55 -0700111 void incrementRefcount(void* resource, ResourceType resourceType);
Romain Guy58ecc202012-09-07 11:58:36 -0700112 void incrementRefcountLocked(void* resource, ResourceType resourceType);
113
114 void decrementRefcount(void* resource);
115 void decrementRefcountLocked(void* resource);
116
Chet Haase5c13d892010-10-08 08:37:55 -0700117 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -0800118
119 /**
120 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
121 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
122 * or a reference queue finalization thread.
123 */
124 mutable Mutex mLock;
Romain Guy58ecc202012-09-07 11:58:36 -0700125
Chris Craikd218a922014-01-02 17:13:34 -0800126 KeyedVector<const void*, ResourceReference*>* mCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700127};
128
129}; // namespace uirenderer
130}; // namespace android
131
Romain Guy5b3b3522010-10-27 18:57:51 -0700132#endif // ANDROID_HWUI_RESOURCE_CACHE_H