| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_RESOURCE_CACHE_H | 
 | 18 | #define ANDROID_HWUI_RESOURCE_CACHE_H | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 19 |  | 
 | 20 | #include <SkBitmap.h> | 
| Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 21 | #include <SkiaColorFilter.h> | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 22 | #include <SkiaShader.h> | 
 | 23 | #include <utils/KeyedVector.h> | 
 | 24 |  | 
 | 25 | namespace android { | 
 | 26 | namespace uirenderer { | 
 | 27 |  | 
 | 28 | /** | 
 | 29 |  * Type of Resource being cached | 
 | 30 |  */ | 
 | 31 | enum ResourceType { | 
 | 32 |     kBitmap, | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 33 |     kShader, | 
| Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 34 |     kColorFilter, | 
| Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 35 |     kPath, | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 36 | }; | 
 | 37 |  | 
 | 38 | class ResourceReference { | 
 | 39 | public: | 
 | 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 |  | 
 | 52 | class ResourceCache { | 
 | 53 |     KeyedVector<void *, ResourceReference *>* mCache; | 
 | 54 | public: | 
 | 55 |     ResourceCache(); | 
 | 56 |     ~ResourceCache(); | 
| Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 57 |     void incrementRefcount(SkPath* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 58 |     void incrementRefcount(SkBitmap* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 59 |     void incrementRefcount(SkiaShader* resource); | 
| Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 60 |     void incrementRefcount(SkiaColorFilter* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 61 |     void incrementRefcount(const void* resource, ResourceType resourceType); | 
 | 62 |     void decrementRefcount(void* resource); | 
 | 63 |     void decrementRefcount(SkBitmap* resource); | 
| Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 64 |     void decrementRefcount(SkPath* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 65 |     void decrementRefcount(SkiaShader* resource); | 
| Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 66 |     void decrementRefcount(SkiaColorFilter* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 67 |     void recycle(SkBitmap* resource); | 
| Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 68 |     void destructor(SkPath* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 69 |     void destructor(SkBitmap* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 70 |     void destructor(SkiaShader* resource); | 
| Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 71 |     void destructor(SkiaColorFilter* resource); | 
| Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 72 | private: | 
 | 73 |     void deleteResourceReference(void* resource, ResourceReference* ref); | 
 | 74 |     void incrementRefcount(void* resource, ResourceType resourceType); | 
 | 75 |     void logCache(); | 
| Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 76 |  | 
 | 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 Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 83 | }; | 
 | 84 |  | 
 | 85 | }; // namespace uirenderer | 
 | 86 | }; // namespace android | 
 | 87 |  | 
| Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 88 | #endif // ANDROID_HWUI_RESOURCE_CACHE_H |