blob: fd3f9fd05d58d55927e30a5dded96472bbaea697 [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:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070044 explicit ResourceReference(ResourceType type) {
John Reck1bcacfd2017-11-03 10:12:19 -070045 refCount = 0;
46 destroyed = false;
47 resourceType = type;
Chet Haase5c13d892010-10-08 08:37:55 -070048 }
49
50 int refCount;
Chet Haase5c13d892010-10-08 08:37:55 -070051 bool destroyed;
52 ResourceType resourceType;
53};
54
John Reck1bcacfd2017-11-03 10:12:19 -070055class ANDROID_API ResourceCache : public Singleton<ResourceCache> {
Chet Haase5c13d892010-10-08 08:37:55 -070056 ResourceCache();
57 ~ResourceCache();
Romain Guy58ecc202012-09-07 11:58:36 -070058
John Recka35778c72014-11-06 09:45:10 -080059 friend class Singleton<ResourceCache>;
60
61public:
Romain Guy58ecc202012-09-07 11:58:36 -070062 /**
63 * When using these two methods, make sure to only invoke the *Locked()
64 * variants of increment/decrementRefcount(), recyle() and destructor()
65 */
66 void lock();
67 void unlock();
68
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050069 void incrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070070
Chris Craikd218a922014-01-02 17:13:34 -080071 void decrementRefcount(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070072
Chris Craikd218a922014-01-02 17:13:34 -080073 void decrementRefcountLocked(const Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070074
Romain Guye3b0a012013-06-26 15:45:41 -070075 void destructor(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070076
Romain Guye3b0a012013-06-26 15:45:41 -070077 void destructorLocked(Res_png_9patch* resource);
Romain Guy58ecc202012-09-07 11:58:36 -070078
Chet Haase5c13d892010-10-08 08:37:55 -070079private:
Chris Craikd218a922014-01-02 17:13:34 -080080 void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
Romain Guy58ecc202012-09-07 11:58:36 -070081
Chet Haase5c13d892010-10-08 08:37:55 -070082 void incrementRefcount(void* resource, ResourceType resourceType);
Romain Guy58ecc202012-09-07 11:58:36 -070083 void incrementRefcountLocked(void* resource, ResourceType resourceType);
84
85 void decrementRefcount(void* resource);
86 void decrementRefcountLocked(void* resource);
87
Chet Haase5c13d892010-10-08 08:37:55 -070088 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -080089
90 /**
91 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
92 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
93 * or a reference queue finalization thread.
94 */
95 mutable Mutex mLock;
Romain Guy58ecc202012-09-07 11:58:36 -070096
Chris Craikd218a922014-01-02 17:13:34 -080097 KeyedVector<const void*, ResourceReference*>* mCache;
Chet Haase5c13d892010-10-08 08:37:55 -070098};
99
John Reck1bcacfd2017-11-03 10:12:19 -0700100}; // namespace uirenderer
101}; // namespace android
Chet Haase5c13d892010-10-08 08:37:55 -0700102
John Reck1bcacfd2017-11-03 10:12:19 -0700103#endif // ANDROID_HWUI_RESOURCE_CACHE_H