blob: b0abe2c58dcc0b6e80e397b9a6ba97b13d7b4de3 [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 Haase5c13d892010-10-08 08:37:55 -070035};
36
37class ResourceReference {
38public:
39
40 ResourceReference() { refCount = 0; recycled = false; destroyed = false;}
41 ResourceReference(ResourceType type) {
42 refCount = 0; recycled = false; destroyed = false; resourceType = type;
43 }
44
45 int refCount;
46 bool recycled;
47 bool destroyed;
48 ResourceType resourceType;
49};
50
51class ResourceCache {
52 KeyedVector<void *, ResourceReference *>* mCache;
53public:
54 ResourceCache();
55 ~ResourceCache();
56 void incrementRefcount(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070057 void incrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070058 void incrementRefcount(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070059 void incrementRefcount(const void* resource, ResourceType resourceType);
60 void decrementRefcount(void* resource);
61 void decrementRefcount(SkBitmap* resource);
62 void decrementRefcount(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070063 void decrementRefcount(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070064 void recycle(void* resource);
65 void recycle(SkBitmap* resource);
66 void destructor(SkBitmap* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070067 void destructor(SkiaShader* resource);
Chet Haasead93c2b2010-10-22 16:17:12 -070068 void destructor(SkiaColorFilter* resource);
Chet Haase5c13d892010-10-08 08:37:55 -070069private:
70 void deleteResourceReference(void* resource, ResourceReference* ref);
71 void incrementRefcount(void* resource, ResourceType resourceType);
72 void logCache();
Chet Haasee7d22952010-11-11 16:30:16 -080073
74 /**
75 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
76 * thread, but destroying resources may be called from the GC thread, the finalizer thread,
77 * or a reference queue finalization thread.
78 */
79 mutable Mutex mLock;
Chet Haase5c13d892010-10-08 08:37:55 -070080};
81
82}; // namespace uirenderer
83}; // namespace android
84
Romain Guy5b3b3522010-10-27 18:57:51 -070085#endif // ANDROID_HWUI_RESOURCE_CACHE_H