blob: 2a38910951c4484f951599f8d1ab869dcc85353f [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
20#include <SkBitmap.h>
Chet Haasead93c2b2010-10-22 16:17:12 -070021#include <SkiaColorFilter.h>
Chet Haase5c13d892010-10-08 08:37:55 -070022#include <SkiaShader.h>
23#include <utils/KeyedVector.h>
24
25namespace android {
26namespace uirenderer {
27
28/**
29 * Type of Resource being cached
30 */
31enum ResourceType {
32 kBitmap,
Chet Haase5c13d892010-10-08 08:37:55 -070033 kShader,
Chet Haasead93c2b2010-10-22 16:17:12 -070034 kColorFilter,
Chet Haase5a7e8282011-02-04 12:50:55 -080035 kPath,
Chet Haase5c13d892010-10-08 08:37:55 -070036};
37
38class ResourceReference {
39public:
40
41 ResourceReference() { refCount = 0; recycled = false; destroyed = false;}
42 ResourceReference(ResourceType type) {
43 refCount = 0; recycled = false; destroyed = false; resourceType = type;
44 }
45
46 int refCount;
47 bool recycled;
48 bool destroyed;
49 ResourceType resourceType;
50};
51
52class ResourceCache {
53 KeyedVector<void *, ResourceReference *>* mCache;
54public:
55 ResourceCache();
56 ~ResourceCache();
Chet Haase5a7e8282011-02-04 12:50:55 -080057 void incrementRefcount(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070058 void incrementRefcount(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070059 void incrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070060 void incrementRefcount(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070061 void incrementRefcount(const void* resource, ResourceType resourceType);
62 void decrementRefcount(void* resource);
63 void decrementRefcount(SkBitmap* resource);
Chet Haase5a7e8282011-02-04 12:50:55 -080064 void decrementRefcount(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070065 void decrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070066 void decrementRefcount(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070067 void recycle(SkBitmap* resource);
Chet Haase5a7e8282011-02-04 12:50:55 -080068 void destructor(SkPath* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070069 void destructor(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070070 void destructor(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070071 void destructor(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070072private:
73 void deleteResourceReference(void* resource, ResourceReference* ref);
74 void incrementRefcount(void* resource, ResourceType resourceType);
75 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -080076
77 /**
78 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
79 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
80 * or a reference queue finalization thread.
81 */
82 mutable Mutex mLock;
Chet Haase5c13d892010-10-08 08:37:55 -070083};
84
85}; // namespace uirenderer
86}; // namespace android
87
Romain Guy5b3b3522010-10-27 18:57:51 -070088#endif // ANDROID_HWUI_RESOURCE_CACHE_H