PatchCache.h revision 3b748a44c6bd2ea05fe16839caf73dbe50bd7ae9
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_PATCH_CACHE_H 18#define ANDROID_HWUI_PATCH_CACHE_H 19 20#include <GLES2/gl2.h> 21 22#include <utils/LruCache.h> 23 24#include <androidfw/ResourceTypes.h> 25 26#include "AssetAtlas.h" 27#include "Debug.h" 28#include "Patch.h" 29 30namespace android { 31namespace uirenderer { 32 33/////////////////////////////////////////////////////////////////////////////// 34// Defines 35/////////////////////////////////////////////////////////////////////////////// 36 37// Debug 38#if DEBUG_PATCHES 39 #define PATCH_LOGD(...) ALOGD(__VA_ARGS__) 40#else 41 #define PATCH_LOGD(...) 42#endif 43 44/////////////////////////////////////////////////////////////////////////////// 45// Cache 46/////////////////////////////////////////////////////////////////////////////// 47 48class Caches; 49 50class PatchCache { 51public: 52 PatchCache(); 53 ~PatchCache(); 54 void init(Caches& caches); 55 56 const Patch* get(const AssetAtlas::Entry* entry, 57 const uint32_t bitmapWidth, const uint32_t bitmapHeight, 58 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch); 59 void clear(); 60 61 uint32_t getSize() const { 62 return mSize; 63 } 64 65 uint32_t getMaxSize() const { 66 return mMaxSize; 67 } 68 69 GLuint getMeshBuffer() const { 70 return mMeshBuffer; 71 } 72 73private: 74 void clearCache(); 75 76 struct PatchDescription { 77 PatchDescription(): mPatch(NULL), mBitmapWidth(0), mBitmapHeight(0), 78 mPixelWidth(0), mPixelHeight(0) { 79 } 80 81 PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight, 82 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch): 83 mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight), 84 mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) { 85 } 86 87 hash_t hash() const; 88 89 static int compare(const PatchDescription& lhs, const PatchDescription& rhs); 90 91 bool operator==(const PatchDescription& other) const { 92 return compare(*this, other) == 0; 93 } 94 95 bool operator!=(const PatchDescription& other) const { 96 return compare(*this, other) != 0; 97 } 98 99 friend inline int strictly_order_type(const PatchDescription& lhs, 100 const PatchDescription& rhs) { 101 return PatchDescription::compare(lhs, rhs) < 0; 102 } 103 104 friend inline int compare_type(const PatchDescription& lhs, 105 const PatchDescription& rhs) { 106 return PatchDescription::compare(lhs, rhs); 107 } 108 109 friend inline hash_t hash_type(const PatchDescription& entry) { 110 return entry.hash(); 111 } 112 113 private: 114 const Res_png_9patch* mPatch; 115 uint32_t mBitmapWidth; 116 uint32_t mBitmapHeight; 117 float mPixelWidth; 118 float mPixelHeight; 119 120 }; // struct PatchDescription 121 122 uint32_t mMaxSize; 123 uint32_t mSize; 124 LruCache<PatchDescription, Patch*> mCache; 125 126 GLuint mMeshBuffer; 127}; // class PatchCache 128 129}; // namespace uirenderer 130}; // namespace android 131 132#endif // ANDROID_HWUI_PATCH_CACHE_H 133