Home | History | Annotate | Download | only in hwui
      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 
     17 #ifndef ANDROID_HWUI_RESOURCE_CACHE_H
     18 #define ANDROID_HWUI_RESOURCE_CACHE_H
     19 
     20 #include <cutils/compiler.h>
     21 
     22 #include <SkBitmap.h>
     23 
     24 #include <utils/KeyedVector.h>
     25 #include <utils/Singleton.h>
     26 
     27 #include <androidfw/ResourceTypes.h>
     28 
     29 #include "Layer.h"
     30 
     31 namespace android {
     32 namespace uirenderer {
     33 
     34 /**
     35  * Type of Resource being cached
     36  */
     37 enum ResourceType {
     38     kBitmap,
     39     kNinePatch,
     40     kPath
     41 };
     42 
     43 class ResourceReference {
     44 public:
     45 
     46     ResourceReference(ResourceType type) {
     47         refCount = 0; recycled = false; destroyed = false; resourceType = type;
     48     }
     49 
     50     int refCount;
     51     bool recycled;
     52     bool destroyed;
     53     ResourceType resourceType;
     54 };
     55 
     56 class ANDROID_API ResourceCache: public Singleton<ResourceCache> {
     57     ResourceCache();
     58     ~ResourceCache();
     59 
     60     friend class Singleton<ResourceCache>;
     61 
     62 public:
     63 
     64     /**
     65      * When using these two methods, make sure to only invoke the *Locked()
     66      * variants of increment/decrementRefcount(), recyle() and destructor()
     67      */
     68     void lock();
     69     void unlock();
     70 
     71     void incrementRefcount(const SkPath* resource);
     72     void incrementRefcount(const SkBitmap* resource);
     73     void incrementRefcount(const Res_png_9patch* resource);
     74 
     75     void incrementRefcountLocked(const SkPath* resource);
     76     void incrementRefcountLocked(const SkBitmap* resource);
     77     void incrementRefcountLocked(const Res_png_9patch* resource);
     78 
     79     void decrementRefcount(const SkBitmap* resource);
     80     void decrementRefcount(const SkPath* resource);
     81     void decrementRefcount(const Res_png_9patch* resource);
     82 
     83     void decrementRefcountLocked(const SkBitmap* resource);
     84     void decrementRefcountLocked(const SkPath* resource);
     85     void decrementRefcountLocked(const Res_png_9patch* resource);
     86 
     87     void destructor(SkPath* resource);
     88     void destructor(const SkBitmap* resource);
     89     void destructor(Res_png_9patch* resource);
     90 
     91     void destructorLocked(SkPath* resource);
     92     void destructorLocked(const SkBitmap* resource);
     93     void destructorLocked(Res_png_9patch* resource);
     94 
     95     bool recycle(SkBitmap* resource);
     96     bool recycleLocked(SkBitmap* resource);
     97 
     98 private:
     99     void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref);
    100 
    101     void incrementRefcount(void* resource, ResourceType resourceType);
    102     void incrementRefcountLocked(void* resource, ResourceType resourceType);
    103 
    104     void decrementRefcount(void* resource);
    105     void decrementRefcountLocked(void* resource);
    106 
    107     void logCache();
    108 
    109     /**
    110      * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI
    111      * thread, but destroying resources may be called from the GC thread, the finalizer thread,
    112      * or a reference queue finalization thread.
    113      */
    114     mutable Mutex mLock;
    115 
    116     KeyedVector<const void*, ResourceReference*>* mCache;
    117 };
    118 
    119 }; // namespace uirenderer
    120 }; // namespace android
    121 
    122 #endif // ANDROID_HWUI_RESOURCE_CACHE_H
    123