blob: 8cf466baa4e88f3d76857994047fb931ce48b975 [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>
25#include <utils/KeyedVector.h>
26
27namespace android {
28namespace uirenderer {
29
30/**
31 * Type of Resource being cached
32 */
33enum ResourceType {
34 kBitmap,
Chet Haase5c13d892010-10-08 08:37:55 -070035 kShader,
Chet Haasead93c2b2010-10-22 16:17:12 -070036 kColorFilter,
Chet Haase5a7e8282011-02-04 12:50:55 -080037 kPath,
Chet Haase5c13d892010-10-08 08:37:55 -070038};
39
40class ResourceReference {
41public:
42
43 ResourceReference() { refCount = 0; recycled = false; destroyed = false;}
44 ResourceReference(ResourceType type) {
45 refCount = 0; recycled = false; destroyed = false; resourceType = type;
46 }
47
48 int refCount;
49 bool recycled;
50 bool destroyed;
51 ResourceType resourceType;
52};
53
Romain Guy79537452011-10-12 13:48:51 -070054class ANDROID_API ResourceCache {
Chet Haase5c13d892010-10-08 08:37:55 -070055 KeyedVector<void *, ResourceReference *>* mCache;
56public:
57 ResourceCache();
58 ~ResourceCache();
Chet Haase5a7e8282011-02-04 12:50:55 -080059 void incrementRefcount(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070060 void incrementRefcount(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070061 void incrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070062 void incrementRefcount(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070063 void incrementRefcount(const void* resource, ResourceType resourceType);
64 void decrementRefcount(void* resource);
65 void decrementRefcount(SkBitmap* resource);
Chet Haase5a7e8282011-02-04 12:50:55 -080066 void decrementRefcount(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070067 void decrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070068 void decrementRefcount(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070069 void recycle(SkBitmap* resource);
Chet Haase5a7e8282011-02-04 12:50:55 -080070 void destructor(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070071 void destructor(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070072 void destructor(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070073 void destructor(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070074private:
75 void deleteResourceReference(void* resource, ResourceReference* ref);
76 void incrementRefcount(void* resource, ResourceType resourceType);
77 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -080078
79 /**
80 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
81 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
82 * or a reference queue finalization thread.
83 */
84 mutable Mutex mLock;
Chet Haase5c13d892010-10-08 08:37:55 -070085};
86
87}; // namespace uirenderer
88}; // namespace android
89
Romain Guy5b3b3522010-10-27 18:57:51 -070090#endif // ANDROID_HWUI_RESOURCE_CACHE_H