blob: ea0c1b5bef71d35d985cb5f7bd715f129f4e5576 [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 Haasead93c2b2010-10-22 16:17:12 -070023#include <SkiaColorFilter.h>
Chet Haase5c13d892010-10-08 08:37:55 -070024#include <SkiaShader.h>
Romain Guye3b0a012013-06-26 15:45:41 -070025
Chet Haase5c13d892010-10-08 08:37:55 -070026#include <utils/KeyedVector.h>
Romain Guye3b0a012013-06-26 15:45:41 -070027
28#include <androidfw/ResourceTypes.h>
29
Chet Haase603f6de2012-09-14 15:31:25 -070030#include "Layer.h"
Chet Haase5c13d892010-10-08 08:37:55 -070031
32namespace android {
33namespace uirenderer {
34
35/**
36 * Type of Resource being cached
37 */
38enum ResourceType {
39 kBitmap,
Chet Haase5c13d892010-10-08 08:37:55 -070040 kShader,
Chet Haasead93c2b2010-10-22 16:17:12 -070041 kColorFilter,
Romain Guye3b0a012013-06-26 15:45:41 -070042 kNinePatch,
Chet Haase5a7e8282011-02-04 12:50:55 -080043 kPath,
Chet Haase603f6de2012-09-14 15:31:25 -070044 kLayer
Chet Haase5c13d892010-10-08 08:37:55 -070045};
46
47class ResourceReference {
48public:
49
50 ResourceReference() { refCount = 0; recycled = false; destroyed = false;}
51 ResourceReference(ResourceType type) {
52 refCount = 0; recycled = false; destroyed = false; resourceType = type;
53 }
54
55 int refCount;
56 bool recycled;
57 bool destroyed;
58 ResourceType resourceType;
59};
60
Romain Guy79537452011-10-12 13:48:51 -070061class ANDROID_API ResourceCache {
Chet Haase5c13d892010-10-08 08:37:55 -070062public:
63 ResourceCache();
64 ~ResourceCache();
Romain Guy58ecc202012-09-07 11:58:36 -070065
66 /**
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
Chet Haase5a7e8282011-02-04 12:50:55 -080073 void incrementRefcount(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070074 void incrementRefcount(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070075 void incrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070076 void incrementRefcount(SkiaColorFilter* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070077 void incrementRefcount(Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070078 void incrementRefcount(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070079
80 void incrementRefcountLocked(SkPath* resource);
81 void incrementRefcountLocked(SkBitmap* resource);
82 void incrementRefcountLocked(SkiaShader* resource);
83 void incrementRefcountLocked(SkiaColorFilter* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070084 void incrementRefcountLocked(Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070085 void incrementRefcountLocked(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070086
Chet Haase5c13d892010-10-08 08:37:55 -070087 void decrementRefcount(SkBitmap* resource);
Chet Haase5a7e8282011-02-04 12:50:55 -080088 void decrementRefcount(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070089 void decrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070090 void decrementRefcount(SkiaColorFilter* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070091 void decrementRefcount(Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070092 void decrementRefcount(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070093
94 void decrementRefcountLocked(SkBitmap* resource);
95 void decrementRefcountLocked(SkPath* resource);
96 void decrementRefcountLocked(SkiaShader* resource);
97 void decrementRefcountLocked(SkiaColorFilter* resource);
Romain Guye3b0a012013-06-26 15:45:41 -070098 void decrementRefcountLocked(Res_png_9patch* resource);
Chet Haase603f6de2012-09-14 15:31:25 -070099 void decrementRefcountLocked(Layer* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700100
Chet Haase5a7e8282011-02-04 12:50:55 -0800101 void destructor(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700102 void destructor(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700103 void destructor(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -0700104 void destructor(SkiaColorFilter* resource);
Romain Guye3b0a012013-06-26 15:45:41 -0700105 void destructor(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700106
107 void destructorLocked(SkPath* resource);
108 void destructorLocked(SkBitmap* resource);
109 void destructorLocked(SkiaShader* resource);
110 void destructorLocked(SkiaColorFilter* resource);
Romain Guye3b0a012013-06-26 15:45:41 -0700111 void destructorLocked(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700112
Chet Haase547e6652012-10-22 15:07:26 -0700113 bool recycle(SkBitmap* resource);
114 bool recycleLocked(SkBitmap* resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700115
Chet Haase5c13d892010-10-08 08:37:55 -0700116private:
Romain Guy97dc9172012-09-23 17:46:45 -0700117 void deleteResourceReferenceLocked(void* resource, ResourceReference* ref);
Romain Guy58ecc202012-09-07 11:58:36 -0700118
Chet Haase5c13d892010-10-08 08:37:55 -0700119 void incrementRefcount(void* resource, ResourceType resourceType);
Romain Guy58ecc202012-09-07 11:58:36 -0700120 void incrementRefcountLocked(void* resource, ResourceType resourceType);
121
122 void decrementRefcount(void* resource);
123 void decrementRefcountLocked(void* resource);
124
Chet Haase5c13d892010-10-08 08:37:55 -0700125 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -0800126
127 /**
128 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
129 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
130 * or a reference queue finalization thread.
131 */
132 mutable Mutex mLock;
Romain Guy58ecc202012-09-07 11:58:36 -0700133
134 KeyedVector<void*, ResourceReference*>* mCache;
Chet Haase5c13d892010-10-08 08:37:55 -0700135};
136
137}; // namespace uirenderer
138}; // namespace android
139
Romain Guy5b3b3522010-10-27 18:57:51 -0700140#endif // ANDROID_HWUI_RESOURCE_CACHE_H